blob: 97fa2e0cab1d944a8d1bf93d736ce1b2185497ab [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
146static char e_var_notfound[] = N_("E1001: variable not found: %s");
147static char e_syntax_at[] = N_("E1002: Syntax error at %s");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200148static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200149static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +0200150static char e_namespace[] = N_("E1075: Namespace not supported: %s");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151
Bram Moolenaar20431c92020-03-20 18:39:46 +0100152static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200153static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154
155/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200156 * Lookup variable "name" in the local scope and return it.
157 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160lookup_local(char_u *name, size_t len, cctx_T *cctx)
161{
162 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200163 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200166 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100172 if (STRNCMP(name, lvar->lv_name, len) == 0
173 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
175 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200176 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100178 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200179
180 // Find local in outer function scope.
181 if (cctx->ctx_outer != NULL)
182 {
183 lvar = lookup_local(name, len, cctx->ctx_outer);
184 if (lvar != NULL)
185 {
186 // TODO: are there situations we should not mark the outer scope as
187 // used?
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 return lvar;
191 }
192 }
193
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195}
196
197/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198 * Lookup an argument in the current function and an enclosing function.
199 * Returns the argument index in "idxp"
200 * Returns the argument type in "type"
201 * Sets "gen_load_outer" to TRUE if found in outer scope.
202 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203 */
204 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200205lookup_arg(
206 char_u *name,
207 size_t len,
208 int *idxp,
209 type_T **type,
210 int *gen_load_outer,
211 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212{
213 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200214 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100216 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
219 {
220 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
221
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200222 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
223 {
224 if (idxp != NULL)
225 {
226 // Arguments are located above the frame pointer. One further
227 // if there is a vararg argument
228 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
229 + STACK_FRAME_SIZE)
230 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
231
232 if (cctx->ctx_ufunc->uf_arg_types != NULL)
233 *type = cctx->ctx_ufunc->uf_arg_types[idx];
234 else
235 *type = &t_any;
236 }
237 return OK;
238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200241 va_name = cctx->ctx_ufunc->uf_va_name;
242 if (va_name != NULL
243 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
244 {
245 if (idxp != NULL)
246 {
247 // varargs is always the last argument
248 *idxp = -STACK_FRAME_SIZE - 1;
249 *type = cctx->ctx_ufunc->uf_va_type;
250 }
251 return OK;
252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200254 if (cctx->ctx_outer != NULL)
255 {
256 // Lookup the name for an argument of the outer function.
257 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
258 == OK)
259 {
260 *gen_load_outer = TRUE;
261 return OK;
262 }
263 }
264
265 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266}
267
268/*
269 * Lookup a variable in the current script.
270 * Returns OK or FAIL.
271 */
272 static int
273lookup_script(char_u *name, size_t len)
274{
275 int cc;
276 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
277 dictitem_T *di;
278
279 cc = name[len];
280 name[len] = NUL;
281 di = find_var_in_ht(ht, 0, name, TRUE);
282 name[len] = cc;
283 return di == NULL ? FAIL: OK;
284}
285
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100286/*
287 * Check if "p[len]" is already defined, either in script "import_sid" or in
288 * compilation context "cctx".
289 * Return FAIL and give an error if it defined.
290 */
291 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200292check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100293{
294 if (lookup_script(p, len) == OK
295 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200296 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100297 || find_imported(p, len, cctx) != NULL)))
298 {
299 semsg("E1073: imported name already defined: %s", p);
300 return FAIL;
301 }
302 return OK;
303}
304
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200305/*
306 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
307 * be freed later.
308 */
309 static type_T *
310alloc_type(garray_T *type_gap)
311{
312 type_T *type;
313
314 if (ga_grow(type_gap, 1) == FAIL)
315 return NULL;
316 type = ALLOC_CLEAR_ONE(type_T);
317 if (type != NULL)
318 {
319 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
320 ++type_gap->ga_len;
321 }
322 return type;
323}
324
Bram Moolenaar6110e792020-07-08 19:35:21 +0200325 void
326clear_type_list(garray_T *gap)
327{
328 while (gap->ga_len > 0)
329 vim_free(((type_T **)gap->ga_data)[--gap->ga_len]);
330 ga_clear(gap);
331}
332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100333 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200334get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335{
336 type_T *type;
337
338 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200339 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200341 if (member_type->tt_type == VAR_VOID
342 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100343 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100344 if (member_type->tt_type == VAR_BOOL)
345 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 if (member_type->tt_type == VAR_NUMBER)
347 return &t_list_number;
348 if (member_type->tt_type == VAR_STRING)
349 return &t_list_string;
350
351 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200352 type = alloc_type(type_gap);
353 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100354 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355 type->tt_type = VAR_LIST;
356 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200357 type->tt_argcount = 0;
358 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100359 return type;
360}
361
362 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200363get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364{
365 type_T *type;
366
367 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200368 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200370 if (member_type->tt_type == VAR_VOID
371 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100372 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100373 if (member_type->tt_type == VAR_BOOL)
374 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375 if (member_type->tt_type == VAR_NUMBER)
376 return &t_dict_number;
377 if (member_type->tt_type == VAR_STRING)
378 return &t_dict_string;
379
380 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200381 type = alloc_type(type_gap);
382 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100383 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 type->tt_type = VAR_DICT;
385 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200386 type->tt_argcount = 0;
387 type->tt_args = NULL;
388 return type;
389}
390
391/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200392 * Allocate a new type for a function.
393 */
394 static type_T *
395alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
396{
397 type_T *type = alloc_type(type_gap);
398
399 if (type == NULL)
400 return &t_any;
401 type->tt_type = VAR_FUNC;
402 type->tt_member = ret_type;
403 type->tt_argcount = argcount;
404 type->tt_args = NULL;
405 return type;
406}
407
408/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200409 * Get a function type, based on the return type "ret_type".
410 * If "argcount" is -1 or 0 a predefined type can be used.
411 * If "argcount" > 0 always create a new type, so that arguments can be added.
412 */
413 static type_T *
414get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
415{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200416 // recognize commonly used types
417 if (argcount <= 0)
418 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200419 if (ret_type == &t_unknown)
420 {
421 // (argcount == 0) is not possible
422 return &t_func_unknown;
423 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200424 if (ret_type == &t_void)
425 {
426 if (argcount == 0)
427 return &t_func_0_void;
428 else
429 return &t_func_void;
430 }
431 if (ret_type == &t_any)
432 {
433 if (argcount == 0)
434 return &t_func_0_any;
435 else
436 return &t_func_any;
437 }
438 if (ret_type == &t_number)
439 {
440 if (argcount == 0)
441 return &t_func_0_number;
442 else
443 return &t_func_number;
444 }
445 if (ret_type == &t_string)
446 {
447 if (argcount == 0)
448 return &t_func_0_string;
449 else
450 return &t_func_string;
451 }
452 }
453
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200454 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455}
456
Bram Moolenaara8c17702020-04-01 21:17:24 +0200457/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200458 * For a function type, reserve space for "argcount" argument types (including
459 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200460 */
461 static int
462func_type_add_arg_types(
463 type_T *functype,
464 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200465 garray_T *type_gap)
466{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200467 // To make it easy to free the space needed for the argument types, add the
468 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200469 if (ga_grow(type_gap, 1) == FAIL)
470 return FAIL;
471 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
472 if (functype->tt_args == NULL)
473 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200474 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
475 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200477 return OK;
478}
479
480/*
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200481 * Get a type_T for a typval_T.
482 * "type_list" is used to temporarily create types in.
Bram Moolenaara8c17702020-04-01 21:17:24 +0200483 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200484 type_T *
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200485typval2type(typval_T *tv, garray_T *type_gap)
Bram Moolenaara8c17702020-04-01 21:17:24 +0200486{
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200487 type_T *actual;
488 type_T *member_type;
489
Bram Moolenaara8c17702020-04-01 21:17:24 +0200490 if (tv->v_type == VAR_NUMBER)
491 return &t_number;
492 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200493 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200494 if (tv->v_type == VAR_STRING)
495 return &t_string;
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200496
497 if (tv->v_type == VAR_LIST
498 && tv->vval.v_list != NULL
499 && tv->vval.v_list->lv_first != NULL)
500 {
501 // Use the type of the first member, it is the most specific.
502 member_type = typval2type(&tv->vval.v_list->lv_first->li_tv, type_gap);
503 return get_list_type(member_type, type_gap);
504 }
505
506 if (tv->v_type == VAR_DICT
507 && tv->vval.v_dict != NULL
508 && tv->vval.v_dict->dv_hashtab.ht_used > 0)
509 {
510 dict_iterator_T iter;
511 typval_T *value;
512
513 // Use the type of the first value, it is the most specific.
514 dict_iterate_start(tv, &iter);
515 dict_iterate_next(&iter, &value);
516 member_type = typval2type(value, type_gap);
517 return get_dict_type(member_type, type_gap);
518 }
519
520 if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
521 {
522 char_u *name = NULL;
523 ufunc_T *ufunc = NULL;
524
525 if (tv->v_type == VAR_PARTIAL)
526 {
527 if (tv->vval.v_partial->pt_func != NULL)
528 ufunc = tv->vval.v_partial->pt_func;
529 else
530 name = tv->vval.v_partial->pt_name;
531 }
532 else
533 name = tv->vval.v_string;
534 if (name != NULL)
535 // TODO: how about a builtin function?
536 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200537 if (ufunc != NULL)
538 {
539 // May need to get the argument types from default values by
540 // compiling the function.
541 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
542 && compile_def_function(ufunc, TRUE, NULL) == FAIL)
543 return NULL;
544 if (ufunc->uf_func_type != NULL)
545 return ufunc->uf_func_type;
546 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200547 }
548
549 actual = alloc_type(type_gap);
550 if (actual == NULL)
551 return NULL;
552 actual->tt_type = tv->v_type;
553 actual->tt_member = &t_any;
554
555 return actual;
556}
557
558/*
559 * Get a type_T for a typval_T, used for v: variables.
560 * "type_list" is used to temporarily create types in.
561 */
562 type_T *
563typval2type_vimvar(typval_T *tv, garray_T *type_gap)
564{
Bram Moolenaara8c17702020-04-01 21:17:24 +0200565 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
566 return &t_list_string;
567 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
568 return &t_dict_any;
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200569 return typval2type(tv, type_gap);
570}
571
572
573/*
574 * Return FAIL if "expected" and "actual" don't match.
575 */
576 int
577check_typval_type(type_T *expected, typval_T *actual_tv)
578{
579 garray_T type_list;
580 type_T *actual_type;
581 int res = FAIL;
582
583 ga_init2(&type_list, sizeof(type_T *), 10);
584 actual_type = typval2type(actual_tv, &type_list);
585 if (actual_type != NULL)
586 res = check_type(expected, actual_type, TRUE);
587 clear_type_list(&type_list);
588 return res;
Bram Moolenaara8c17702020-04-01 21:17:24 +0200589}
590
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200591 static void
592type_mismatch(type_T *expected, type_T *actual)
593{
594 char *tofree1, *tofree2;
595
596 semsg(_("E1013: type mismatch, expected %s but got %s"),
597 type_name(expected, &tofree1), type_name(actual, &tofree2));
598 vim_free(tofree1);
599 vim_free(tofree2);
600}
601
602 static void
603arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
604{
605 char *tofree1, *tofree2;
606
607 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
608 argidx,
609 type_name(expected, &tofree1), type_name(actual, &tofree2));
610 vim_free(tofree1);
611 vim_free(tofree2);
612}
613
614/*
615 * Check if the expected and actual types match.
616 * Does not allow for assigning "any" to a specific type.
617 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200618 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200619check_type(type_T *expected, type_T *actual, int give_msg)
620{
621 int ret = OK;
622
623 // When expected is "unknown" we accept any actual type.
624 // When expected is "any" we accept any actual type except "void".
625 if (expected->tt_type != VAR_UNKNOWN
626 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
627
628 {
629 if (expected->tt_type != actual->tt_type)
630 {
631 if (give_msg)
632 type_mismatch(expected, actual);
633 return FAIL;
634 }
635 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
636 {
637 // "unknown" is used for an empty list or dict
638 if (actual->tt_member != &t_unknown)
639 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
640 }
641 else if (expected->tt_type == VAR_FUNC)
642 {
643 if (expected->tt_member != &t_unknown)
644 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
645 if (ret == OK && expected->tt_argcount != -1
646 && (actual->tt_argcount < expected->tt_min_argcount
647 || actual->tt_argcount > expected->tt_argcount))
648 ret = FAIL;
Bram Moolenaarb8070e32020-07-23 20:56:04 +0200649 if (expected->tt_args != NULL && actual->tt_args != NULL)
650 {
651 int i;
652
653 for (i = 0; i < expected->tt_argcount; ++i)
654 if (check_type(expected->tt_args[i], actual->tt_args[i],
655 FALSE) == FAIL)
656 {
657 ret = FAIL;
658 break;
659 }
660 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200661 }
662 if (ret == FAIL && give_msg)
663 type_mismatch(expected, actual);
664 }
665 return ret;
666}
667
Bram Moolenaar65b95452020-07-19 14:03:09 +0200668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669/////////////////////////////////////////////////////////////////////
670// Following generate_ functions expect the caller to call ga_grow().
671
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200672#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
673#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675/*
676 * Generate an instruction without arguments.
677 * Returns a pointer to the new instruction, NULL if failed.
678 */
679 static isn_T *
680generate_instr(cctx_T *cctx, isntype_T isn_type)
681{
682 garray_T *instr = &cctx->ctx_instr;
683 isn_T *isn;
684
Bram Moolenaar080457c2020-03-03 21:53:32 +0100685 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686 if (ga_grow(instr, 1) == FAIL)
687 return NULL;
688 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
689 isn->isn_type = isn_type;
690 isn->isn_lnum = cctx->ctx_lnum + 1;
691 ++instr->ga_len;
692
693 return isn;
694}
695
696/*
697 * Generate an instruction without arguments.
698 * "drop" will be removed from the stack.
699 * Returns a pointer to the new instruction, NULL if failed.
700 */
701 static isn_T *
702generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
703{
704 garray_T *stack = &cctx->ctx_type_stack;
705
Bram Moolenaar080457c2020-03-03 21:53:32 +0100706 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 stack->ga_len -= drop;
708 return generate_instr(cctx, isn_type);
709}
710
711/*
712 * Generate instruction "isn_type" and put "type" on the type stack.
713 */
714 static isn_T *
715generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
716{
717 isn_T *isn;
718 garray_T *stack = &cctx->ctx_type_stack;
719
720 if ((isn = generate_instr(cctx, isn_type)) == NULL)
721 return NULL;
722
723 if (ga_grow(stack, 1) == FAIL)
724 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200725 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726 ++stack->ga_len;
727
728 return isn;
729}
730
731/*
732 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
733 */
734 static int
735may_generate_2STRING(int offset, cctx_T *cctx)
736{
737 isn_T *isn;
738 garray_T *stack = &cctx->ctx_type_stack;
739 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
740
741 if ((*type)->tt_type == VAR_STRING)
742 return OK;
743 *type = &t_string;
744
745 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
746 return FAIL;
747 isn->isn_arg.number = offset;
748
749 return OK;
750}
751
752 static int
753check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
754{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200755 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200757 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 {
759 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200760 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 else
762 semsg(_("E1036: %c requires number or float arguments"), *op);
763 return FAIL;
764 }
765 return OK;
766}
767
768/*
769 * Generate an instruction with two arguments. The instruction depends on the
770 * type of the arguments.
771 */
772 static int
773generate_two_op(cctx_T *cctx, char_u *op)
774{
775 garray_T *stack = &cctx->ctx_type_stack;
776 type_T *type1;
777 type_T *type2;
778 vartype_T vartype;
779 isn_T *isn;
780
Bram Moolenaar080457c2020-03-03 21:53:32 +0100781 RETURN_OK_IF_SKIP(cctx);
782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 // Get the known type of the two items on the stack. If they are matching
784 // use a type-specific instruction. Otherwise fall back to runtime type
785 // checking.
786 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
787 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200788 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 if (type1->tt_type == type2->tt_type
790 && (type1->tt_type == VAR_NUMBER
791 || type1->tt_type == VAR_LIST
792#ifdef FEAT_FLOAT
793 || type1->tt_type == VAR_FLOAT
794#endif
795 || type1->tt_type == VAR_BLOB))
796 vartype = type1->tt_type;
797
798 switch (*op)
799 {
800 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200801 && type1->tt_type != VAR_ANY
802 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803 && check_number_or_float(
804 type1->tt_type, type2->tt_type, op) == FAIL)
805 return FAIL;
806 isn = generate_instr_drop(cctx,
807 vartype == VAR_NUMBER ? ISN_OPNR
808 : vartype == VAR_LIST ? ISN_ADDLIST
809 : vartype == VAR_BLOB ? ISN_ADDBLOB
810#ifdef FEAT_FLOAT
811 : vartype == VAR_FLOAT ? ISN_OPFLOAT
812#endif
813 : ISN_OPANY, 1);
814 if (isn != NULL)
815 isn->isn_arg.op.op_type = EXPR_ADD;
816 break;
817
818 case '-':
819 case '*':
820 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
821 op) == FAIL)
822 return FAIL;
823 if (vartype == VAR_NUMBER)
824 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
825#ifdef FEAT_FLOAT
826 else if (vartype == VAR_FLOAT)
827 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
828#endif
829 else
830 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
831 if (isn != NULL)
832 isn->isn_arg.op.op_type = *op == '*'
833 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
834 break;
835
Bram Moolenaar4c683752020-04-05 21:38:23 +0200836 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200838 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 && type2->tt_type != VAR_NUMBER))
840 {
841 emsg(_("E1035: % requires number arguments"));
842 return FAIL;
843 }
844 isn = generate_instr_drop(cctx,
845 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
846 if (isn != NULL)
847 isn->isn_arg.op.op_type = EXPR_REM;
848 break;
849 }
850
851 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200852 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 {
854 type_T *type = &t_any;
855
856#ifdef FEAT_FLOAT
857 // float+number and number+float results in float
858 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
859 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
860 type = &t_float;
861#endif
862 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
863 }
864
865 return OK;
866}
867
868/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200869 * Get the instruction to use for comparing "type1" with "type2"
870 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200872 static isntype_T
873get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874{
875 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876
Bram Moolenaar4c683752020-04-05 21:38:23 +0200877 if (type1 == VAR_UNKNOWN)
878 type1 = VAR_ANY;
879 if (type2 == VAR_UNKNOWN)
880 type2 = VAR_ANY;
881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 if (type1 == type2)
883 {
884 switch (type1)
885 {
886 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
887 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
888 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
889 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
890 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
891 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
892 case VAR_LIST: isntype = ISN_COMPARELIST; break;
893 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
894 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 default: isntype = ISN_COMPAREANY; break;
896 }
897 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200898 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
900 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
901 isntype = ISN_COMPAREANY;
902
903 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
904 && (isntype == ISN_COMPAREBOOL
905 || isntype == ISN_COMPARESPECIAL
906 || isntype == ISN_COMPARENR
907 || isntype == ISN_COMPAREFLOAT))
908 {
909 semsg(_("E1037: Cannot use \"%s\" with %s"),
910 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200911 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912 }
913 if (isntype == ISN_DROP
914 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
915 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
916 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
917 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
918 && exptype != EXPR_IS && exptype != EXPR_ISNOT
919 && (type1 == VAR_BLOB || type2 == VAR_BLOB
920 || type1 == VAR_LIST || type2 == VAR_LIST))))
921 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100922 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200924 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200926 return isntype;
927}
928
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200929 int
930check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
931{
932 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
933 return FAIL;
934 return OK;
935}
936
Bram Moolenaara5565e42020-05-09 15:44:01 +0200937/*
938 * Generate an ISN_COMPARE* instruction with a boolean result.
939 */
940 static int
941generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
942{
943 isntype_T isntype;
944 isn_T *isn;
945 garray_T *stack = &cctx->ctx_type_stack;
946 vartype_T type1;
947 vartype_T type2;
948
949 RETURN_OK_IF_SKIP(cctx);
950
951 // Get the known type of the two items on the stack. If they are matching
952 // use a type-specific instruction. Otherwise fall back to runtime type
953 // checking.
954 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
955 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
956 isntype = get_compare_isn(exptype, type1, type2);
957 if (isntype == ISN_DROP)
958 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959
960 if ((isn = generate_instr(cctx, isntype)) == NULL)
961 return FAIL;
962 isn->isn_arg.op.op_type = exptype;
963 isn->isn_arg.op.op_ic = ic;
964
965 // takes two arguments, puts one bool back
966 if (stack->ga_len >= 2)
967 {
968 --stack->ga_len;
969 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
970 }
971
972 return OK;
973}
974
975/*
976 * Generate an ISN_2BOOL instruction.
977 */
978 static int
979generate_2BOOL(cctx_T *cctx, int invert)
980{
981 isn_T *isn;
982 garray_T *stack = &cctx->ctx_type_stack;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
986 return FAIL;
987 isn->isn_arg.number = invert;
988
989 // type becomes bool
990 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
991
992 return OK;
993}
994
995 static int
996generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
997{
998 isn_T *isn;
999 garray_T *stack = &cctx->ctx_type_stack;
1000
Bram Moolenaar080457c2020-03-03 21:53:32 +01001001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
1003 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +02001004 // TODO: whole type, e.g. for a function also arg and return types
1005 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 isn->isn_arg.type.ct_off = offset;
1007
1008 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001009 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010
1011 return OK;
1012}
1013
1014/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001015 * Check that
1016 * - "actual" is "expected" type or
1017 * - "actual" is a type that can be "expected" type: add a runtime check; or
1018 * - return FAIL.
1019 */
1020 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001021need_type(
1022 type_T *actual,
1023 type_T *expected,
1024 int offset,
1025 cctx_T *cctx,
1026 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001027{
1028 if (check_type(expected, actual, FALSE) == OK)
1029 return OK;
1030 if (actual->tt_type != VAR_ANY
1031 && actual->tt_type != VAR_UNKNOWN
1032 && !(actual->tt_type == VAR_FUNC
1033 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
1034 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001035 if (!silent)
1036 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001037 return FAIL;
1038 }
1039 generate_TYPECHECK(cctx, expected, offset);
1040 return OK;
1041}
1042
1043/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 * Generate an ISN_PUSHNR instruction.
1045 */
1046 static int
1047generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1048{
1049 isn_T *isn;
1050
Bram Moolenaar080457c2020-03-03 21:53:32 +01001051 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1053 return FAIL;
1054 isn->isn_arg.number = number;
1055
1056 return OK;
1057}
1058
1059/*
1060 * Generate an ISN_PUSHBOOL instruction.
1061 */
1062 static int
1063generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1064{
1065 isn_T *isn;
1066
Bram Moolenaar080457c2020-03-03 21:53:32 +01001067 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1069 return FAIL;
1070 isn->isn_arg.number = number;
1071
1072 return OK;
1073}
1074
1075/*
1076 * Generate an ISN_PUSHSPEC instruction.
1077 */
1078 static int
1079generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1080{
1081 isn_T *isn;
1082
Bram Moolenaar080457c2020-03-03 21:53:32 +01001083 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1085 return FAIL;
1086 isn->isn_arg.number = number;
1087
1088 return OK;
1089}
1090
1091#ifdef FEAT_FLOAT
1092/*
1093 * Generate an ISN_PUSHF instruction.
1094 */
1095 static int
1096generate_PUSHF(cctx_T *cctx, float_T fnumber)
1097{
1098 isn_T *isn;
1099
Bram Moolenaar080457c2020-03-03 21:53:32 +01001100 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1102 return FAIL;
1103 isn->isn_arg.fnumber = fnumber;
1104
1105 return OK;
1106}
1107#endif
1108
1109/*
1110 * Generate an ISN_PUSHS instruction.
1111 * Consumes "str".
1112 */
1113 static int
1114generate_PUSHS(cctx_T *cctx, char_u *str)
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_PUSHS, &t_string)) == NULL)
1120 return FAIL;
1121 isn->isn_arg.string = str;
1122
1123 return OK;
1124}
1125
1126/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001127 * Generate an ISN_PUSHCHANNEL instruction.
1128 * Consumes "channel".
1129 */
1130 static int
1131generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1132{
1133 isn_T *isn;
1134
Bram Moolenaar080457c2020-03-03 21:53:32 +01001135 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001136 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1137 return FAIL;
1138 isn->isn_arg.channel = channel;
1139
1140 return OK;
1141}
1142
1143/*
1144 * Generate an ISN_PUSHJOB instruction.
1145 * Consumes "job".
1146 */
1147 static int
1148generate_PUSHJOB(cctx_T *cctx, job_T *job)
1149{
1150 isn_T *isn;
1151
Bram Moolenaar080457c2020-03-03 21:53:32 +01001152 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001153 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001154 return FAIL;
1155 isn->isn_arg.job = job;
1156
1157 return OK;
1158}
1159
1160/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 * Generate an ISN_PUSHBLOB instruction.
1162 * Consumes "blob".
1163 */
1164 static int
1165generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1166{
1167 isn_T *isn;
1168
Bram Moolenaar080457c2020-03-03 21:53:32 +01001169 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1171 return FAIL;
1172 isn->isn_arg.blob = blob;
1173
1174 return OK;
1175}
1176
1177/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001178 * Generate an ISN_PUSHFUNC instruction with name "name".
1179 * Consumes "name".
1180 */
1181 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001182generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001183{
1184 isn_T *isn;
1185
Bram Moolenaar080457c2020-03-03 21:53:32 +01001186 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001187 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001188 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001189 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001190
1191 return OK;
1192}
1193
1194/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001195 * Generate an ISN_GETITEM instruction with "index".
1196 */
1197 static int
1198generate_GETITEM(cctx_T *cctx, int index)
1199{
1200 isn_T *isn;
1201 garray_T *stack = &cctx->ctx_type_stack;
1202 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1203 type_T *item_type = &t_any;
1204
1205 RETURN_OK_IF_SKIP(cctx);
1206
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001207 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001208 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001209 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001210 emsg(_(e_listreq));
1211 return FAIL;
1212 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001213 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001214 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1215 return FAIL;
1216 isn->isn_arg.number = index;
1217
1218 // add the item type to the type stack
1219 if (ga_grow(stack, 1) == FAIL)
1220 return FAIL;
1221 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1222 ++stack->ga_len;
1223 return OK;
1224}
1225
1226/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001227 * Generate an ISN_SLICE instruction with "count".
1228 */
1229 static int
1230generate_SLICE(cctx_T *cctx, int count)
1231{
1232 isn_T *isn;
1233
1234 RETURN_OK_IF_SKIP(cctx);
1235 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1236 return FAIL;
1237 isn->isn_arg.number = count;
1238 return OK;
1239}
1240
1241/*
1242 * Generate an ISN_CHECKLEN instruction with "min_len".
1243 */
1244 static int
1245generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1246{
1247 isn_T *isn;
1248
1249 RETURN_OK_IF_SKIP(cctx);
1250
1251 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1252 return FAIL;
1253 isn->isn_arg.checklen.cl_min_len = min_len;
1254 isn->isn_arg.checklen.cl_more_OK = more_OK;
1255
1256 return OK;
1257}
1258
1259/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 * Generate an ISN_STORE instruction.
1261 */
1262 static int
1263generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1264{
1265 isn_T *isn;
1266
Bram Moolenaar080457c2020-03-03 21:53:32 +01001267 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1269 return FAIL;
1270 if (name != NULL)
1271 isn->isn_arg.string = vim_strsave(name);
1272 else
1273 isn->isn_arg.number = idx;
1274
1275 return OK;
1276}
1277
1278/*
1279 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1280 */
1281 static int
1282generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1283{
1284 isn_T *isn;
1285
Bram Moolenaar080457c2020-03-03 21:53:32 +01001286 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1288 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001289 isn->isn_arg.storenr.stnr_idx = idx;
1290 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291
1292 return OK;
1293}
1294
1295/*
1296 * Generate an ISN_STOREOPT instruction
1297 */
1298 static int
1299generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1300{
1301 isn_T *isn;
1302
Bram Moolenaar080457c2020-03-03 21:53:32 +01001303 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1305 return FAIL;
1306 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1307 isn->isn_arg.storeopt.so_flags = opt_flags;
1308
1309 return OK;
1310}
1311
1312/*
1313 * Generate an ISN_LOAD or similar instruction.
1314 */
1315 static int
1316generate_LOAD(
1317 cctx_T *cctx,
1318 isntype_T isn_type,
1319 int idx,
1320 char_u *name,
1321 type_T *type)
1322{
1323 isn_T *isn;
1324
Bram Moolenaar080457c2020-03-03 21:53:32 +01001325 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1327 return FAIL;
1328 if (name != NULL)
1329 isn->isn_arg.string = vim_strsave(name);
1330 else
1331 isn->isn_arg.number = idx;
1332
1333 return OK;
1334}
1335
1336/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001337 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001338 */
1339 static int
1340generate_LOADV(
1341 cctx_T *cctx,
1342 char_u *name,
1343 int error)
1344{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001345 int di_flags;
1346 int vidx = find_vim_var(name, &di_flags);
1347 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001348
Bram Moolenaar080457c2020-03-03 21:53:32 +01001349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001350 if (vidx < 0)
1351 {
1352 if (error)
1353 semsg(_(e_var_notfound), name);
1354 return FAIL;
1355 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001356 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001358 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001359}
1360
1361/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001362 * Generate an ISN_UNLET instruction.
1363 */
1364 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001365generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001366{
1367 isn_T *isn;
1368
1369 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001370 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001371 return FAIL;
1372 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1373 isn->isn_arg.unlet.ul_forceit = forceit;
1374
1375 return OK;
1376}
1377
1378/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 * Generate an ISN_LOADS instruction.
1380 */
1381 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001382generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001384 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001386 int sid,
1387 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388{
1389 isn_T *isn;
1390
Bram Moolenaar080457c2020-03-03 21:53:32 +01001391 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001392 if (isn_type == ISN_LOADS)
1393 isn = generate_instr_type(cctx, isn_type, type);
1394 else
1395 isn = generate_instr_drop(cctx, isn_type, 1);
1396 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001398 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1399 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400
1401 return OK;
1402}
1403
1404/*
1405 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1406 */
1407 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001408generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 cctx_T *cctx,
1410 isntype_T isn_type,
1411 int sid,
1412 int idx,
1413 type_T *type)
1414{
1415 isn_T *isn;
1416
Bram Moolenaar080457c2020-03-03 21:53:32 +01001417 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 if (isn_type == ISN_LOADSCRIPT)
1419 isn = generate_instr_type(cctx, isn_type, type);
1420 else
1421 isn = generate_instr_drop(cctx, isn_type, 1);
1422 if (isn == NULL)
1423 return FAIL;
1424 isn->isn_arg.script.script_sid = sid;
1425 isn->isn_arg.script.script_idx = idx;
1426 return OK;
1427}
1428
1429/*
1430 * Generate an ISN_NEWLIST instruction.
1431 */
1432 static int
1433generate_NEWLIST(cctx_T *cctx, int count)
1434{
1435 isn_T *isn;
1436 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 type_T *type;
1438 type_T *member;
1439
Bram Moolenaar080457c2020-03-03 21:53:32 +01001440 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1442 return FAIL;
1443 isn->isn_arg.number = count;
1444
1445 // drop the value types
1446 stack->ga_len -= count;
1447
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001448 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001449 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 if (count > 0)
1451 member = ((type_T **)stack->ga_data)[stack->ga_len];
1452 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001453 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001454 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455
1456 // add the list type to the type stack
1457 if (ga_grow(stack, 1) == FAIL)
1458 return FAIL;
1459 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1460 ++stack->ga_len;
1461
1462 return OK;
1463}
1464
1465/*
1466 * Generate an ISN_NEWDICT instruction.
1467 */
1468 static int
1469generate_NEWDICT(cctx_T *cctx, int count)
1470{
1471 isn_T *isn;
1472 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 type_T *type;
1474 type_T *member;
1475
Bram Moolenaar080457c2020-03-03 21:53:32 +01001476 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1478 return FAIL;
1479 isn->isn_arg.number = count;
1480
1481 // drop the key and value types
1482 stack->ga_len -= 2 * count;
1483
Bram Moolenaar436472f2020-02-20 22:54:43 +01001484 // Use the first value type for the list member type. Use "void" for an
1485 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 if (count > 0)
1487 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1488 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001489 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001490 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491
1492 // add the dict type to the type stack
1493 if (ga_grow(stack, 1) == FAIL)
1494 return FAIL;
1495 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1496 ++stack->ga_len;
1497
1498 return OK;
1499}
1500
1501/*
1502 * Generate an ISN_FUNCREF instruction.
1503 */
1504 static int
1505generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1506{
1507 isn_T *isn;
1508 garray_T *stack = &cctx->ctx_type_stack;
1509
Bram Moolenaar080457c2020-03-03 21:53:32 +01001510 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1512 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001513 isn->isn_arg.funcref.fr_func = dfunc_idx;
1514 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515
1516 if (ga_grow(stack, 1) == FAIL)
1517 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001518 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 // TODO: argument and return types
1520 ++stack->ga_len;
1521
1522 return OK;
1523}
1524
1525/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001526 * Generate an ISN_NEWFUNC instruction.
1527 */
1528 static int
1529generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1530{
1531 isn_T *isn;
1532 char_u *name;
1533
1534 RETURN_OK_IF_SKIP(cctx);
1535 name = vim_strsave(lambda_name);
1536 if (name == NULL)
1537 return FAIL;
1538 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1539 return FAIL;
1540 isn->isn_arg.newfunc.nf_lambda = name;
1541 isn->isn_arg.newfunc.nf_global = func_name;
1542
1543 return OK;
1544}
1545
1546/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 * Generate an ISN_JUMP instruction.
1548 */
1549 static int
1550generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1551{
1552 isn_T *isn;
1553 garray_T *stack = &cctx->ctx_type_stack;
1554
Bram Moolenaar080457c2020-03-03 21:53:32 +01001555 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1557 return FAIL;
1558 isn->isn_arg.jump.jump_when = when;
1559 isn->isn_arg.jump.jump_where = where;
1560
1561 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1562 --stack->ga_len;
1563
1564 return OK;
1565}
1566
1567 static int
1568generate_FOR(cctx_T *cctx, int loop_idx)
1569{
1570 isn_T *isn;
1571 garray_T *stack = &cctx->ctx_type_stack;
1572
Bram Moolenaar080457c2020-03-03 21:53:32 +01001573 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1575 return FAIL;
1576 isn->isn_arg.forloop.for_idx = loop_idx;
1577
1578 if (ga_grow(stack, 1) == FAIL)
1579 return FAIL;
1580 // type doesn't matter, will be stored next
1581 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1582 ++stack->ga_len;
1583
1584 return OK;
1585}
1586
1587/*
1588 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001589 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 * Return FAIL if the number of arguments is wrong.
1591 */
1592 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001593generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594{
1595 isn_T *isn;
1596 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001597 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001598 type_T *argtypes[MAX_FUNC_ARGS];
1599 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600
Bram Moolenaar080457c2020-03-03 21:53:32 +01001601 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001602 argoff = check_internal_func(func_idx, argcount);
1603 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 return FAIL;
1605
Bram Moolenaar389df252020-07-09 21:20:47 +02001606 if (method_call && argoff > 1)
1607 {
1608 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1609 return FAIL;
1610 isn->isn_arg.shuffle.shfl_item = argcount;
1611 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1612 }
1613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1615 return FAIL;
1616 isn->isn_arg.bfunc.cbf_idx = func_idx;
1617 isn->isn_arg.bfunc.cbf_argcount = argcount;
1618
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001619 for (i = 0; i < argcount; ++i)
1620 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 stack->ga_len -= argcount; // drop the arguments
1623 if (ga_grow(stack, 1) == FAIL)
1624 return FAIL;
1625 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001626 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 ++stack->ga_len; // add return value
1628
1629 return OK;
1630}
1631
1632/*
1633 * Generate an ISN_DCALL or ISN_UCALL instruction.
1634 * Return FAIL if the number of arguments is wrong.
1635 */
1636 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001637generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638{
1639 isn_T *isn;
1640 garray_T *stack = &cctx->ctx_type_stack;
1641 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001642 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643
Bram Moolenaar080457c2020-03-03 21:53:32 +01001644 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 if (argcount > regular_args && !has_varargs(ufunc))
1646 {
1647 semsg(_(e_toomanyarg), ufunc->uf_name);
1648 return FAIL;
1649 }
1650 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1651 {
1652 semsg(_(e_toofewarg), ufunc->uf_name);
1653 return FAIL;
1654 }
1655
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001656 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001657 {
1658 int i;
1659
1660 for (i = 0; i < argcount; ++i)
1661 {
1662 type_T *expected;
1663 type_T *actual;
1664
1665 if (i < regular_args)
1666 {
1667 if (ufunc->uf_arg_types == NULL)
1668 continue;
1669 expected = ufunc->uf_arg_types[i];
1670 }
1671 else
1672 expected = ufunc->uf_va_type->tt_member;
1673 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001674 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001675 {
1676 arg_type_mismatch(expected, actual, i + 1);
1677 return FAIL;
1678 }
1679 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001680 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001681 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001682 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001683 }
1684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001686 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001687 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001689 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 {
1691 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1692 isn->isn_arg.dfunc.cdf_argcount = argcount;
1693 }
1694 else
1695 {
1696 // A user function may be deleted and redefined later, can't use the
1697 // ufunc pointer, need to look it up again at runtime.
1698 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1699 isn->isn_arg.ufunc.cuf_argcount = argcount;
1700 }
1701
1702 stack->ga_len -= argcount; // drop the arguments
1703 if (ga_grow(stack, 1) == FAIL)
1704 return FAIL;
1705 // add return value
1706 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1707 ++stack->ga_len;
1708
1709 return OK;
1710}
1711
1712/*
1713 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1714 */
1715 static int
1716generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1717{
1718 isn_T *isn;
1719 garray_T *stack = &cctx->ctx_type_stack;
1720
Bram Moolenaar080457c2020-03-03 21:53:32 +01001721 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1723 return FAIL;
1724 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1725 isn->isn_arg.ufunc.cuf_argcount = argcount;
1726
1727 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001728 if (ga_grow(stack, 1) == FAIL)
1729 return FAIL;
1730 // add return value
1731 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1732 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733
1734 return OK;
1735}
1736
1737/*
1738 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001739 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 */
1741 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001742generate_PCALL(
1743 cctx_T *cctx,
1744 int argcount,
1745 char_u *name,
1746 type_T *type,
1747 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748{
1749 isn_T *isn;
1750 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001751 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752
Bram Moolenaar080457c2020-03-03 21:53:32 +01001753 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001754
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001755 if (type->tt_type == VAR_ANY)
1756 ret_type = &t_any;
1757 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001758 {
1759 if (type->tt_argcount != -1)
1760 {
1761 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1762
1763 if (argcount < type->tt_min_argcount - varargs)
1764 {
1765 semsg(_(e_toofewarg), "[reference]");
1766 return FAIL;
1767 }
1768 if (!varargs && argcount > type->tt_argcount)
1769 {
1770 semsg(_(e_toomanyarg), "[reference]");
1771 return FAIL;
1772 }
1773 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001774 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001775 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001776 else
1777 {
1778 semsg(_("E1085: Not a callable type: %s"), name);
1779 return FAIL;
1780 }
1781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1783 return FAIL;
1784 isn->isn_arg.pfunc.cpf_top = at_top;
1785 isn->isn_arg.pfunc.cpf_argcount = argcount;
1786
1787 stack->ga_len -= argcount; // drop the arguments
1788
1789 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001790 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001792 // If partial is above the arguments it must be cleared and replaced with
1793 // the return value.
1794 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1795 return FAIL;
1796
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 return OK;
1798}
1799
1800/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001801 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 */
1803 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001804generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805{
1806 isn_T *isn;
1807 garray_T *stack = &cctx->ctx_type_stack;
1808 type_T *type;
1809
Bram Moolenaar080457c2020-03-03 21:53:32 +01001810 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001811 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001813 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001815 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001817 if (type->tt_type != VAR_DICT && type != &t_any)
1818 {
1819 emsg(_(e_dictreq));
1820 return FAIL;
1821 }
1822 // change dict type to dict member type
1823 if (type->tt_type == VAR_DICT)
1824 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825
1826 return OK;
1827}
1828
1829/*
1830 * Generate an ISN_ECHO instruction.
1831 */
1832 static int
1833generate_ECHO(cctx_T *cctx, int with_white, int count)
1834{
1835 isn_T *isn;
1836
Bram Moolenaar080457c2020-03-03 21:53:32 +01001837 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1839 return FAIL;
1840 isn->isn_arg.echo.echo_with_white = with_white;
1841 isn->isn_arg.echo.echo_count = count;
1842
1843 return OK;
1844}
1845
Bram Moolenaarad39c092020-02-26 18:23:43 +01001846/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001847 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001848 */
1849 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001850generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001851{
1852 isn_T *isn;
1853
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001854 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001855 return FAIL;
1856 isn->isn_arg.number = count;
1857
1858 return OK;
1859}
1860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 static int
1862generate_EXEC(cctx_T *cctx, char_u *line)
1863{
1864 isn_T *isn;
1865
Bram Moolenaar080457c2020-03-03 21:53:32 +01001866 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1868 return FAIL;
1869 isn->isn_arg.string = vim_strsave(line);
1870 return OK;
1871}
1872
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001873 static int
1874generate_EXECCONCAT(cctx_T *cctx, int count)
1875{
1876 isn_T *isn;
1877
1878 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1879 return FAIL;
1880 isn->isn_arg.number = count;
1881 return OK;
1882}
1883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884/*
1885 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001886 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001888 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1890{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 lvar_T *lvar;
1892
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001893 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001895 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001896 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897 }
1898
1899 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001900 return NULL;
1901 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001903 // Every local variable uses the next entry on the stack. We could re-use
1904 // the last ones when leaving a scope, but then variables used in a closure
1905 // might get overwritten. To keep things simple do not re-use stack
1906 // entries. This is less efficient, but memory is cheap these days.
1907 lvar->lv_idx = cctx->ctx_locals_count++;
1908
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001909 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 lvar->lv_const = isConst;
1911 lvar->lv_type = type;
1912
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001913 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914}
1915
1916/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001917 * Remove local variables above "new_top".
1918 */
1919 static void
1920unwind_locals(cctx_T *cctx, int new_top)
1921{
1922 if (cctx->ctx_locals.ga_len > new_top)
1923 {
1924 int idx;
1925 lvar_T *lvar;
1926
1927 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1928 {
1929 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1930 vim_free(lvar->lv_name);
1931 }
1932 }
1933 cctx->ctx_locals.ga_len = new_top;
1934}
1935
1936/*
1937 * Free all local variables.
1938 */
1939 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001940free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001941{
1942 unwind_locals(cctx, 0);
1943 ga_clear(&cctx->ctx_locals);
1944}
1945
1946/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 * Skip over a type definition and return a pointer to just after it.
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001948 * When "optional" is TRUE then a leading "?" is accepted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 */
1950 char_u *
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001951skip_type(char_u *start, int optional)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952{
1953 char_u *p = start;
1954
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001955 if (optional && *p == '?')
1956 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 while (ASCII_ISALNUM(*p) || *p == '_')
1958 ++p;
1959
1960 // Skip over "<type>"; this is permissive about white space.
1961 if (*skipwhite(p) == '<')
1962 {
1963 p = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001964 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 p = skipwhite(p);
1966 if (*p == '>')
1967 ++p;
1968 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001969 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
1970 && STRNCMP("func", start, 4) == 0)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001971 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001972 if (*p == '(')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001973 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001974 // handle func(args): type
1975 ++p;
1976 while (*p != ')' && *p != NUL)
1977 {
1978 char_u *sp = p;
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001979
Bram Moolenaarace61322020-07-26 18:16:58 +02001980 if (STRNCMP(p, "...", 3) == 0)
1981 p += 3;
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001982 p = skip_type(p, TRUE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001983 if (p == sp)
1984 return p; // syntax error
1985 if (*p == ',')
1986 p = skipwhite(p + 1);
1987 }
1988 if (*p == ')')
1989 {
1990 if (p[1] == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001991 p = skip_type(skipwhite(p + 2), FALSE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001992 else
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001993 ++p;
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001994 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001995 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001996 else
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001997 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001998 // handle func: return_type
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001999 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002000 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002001 }
2002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 return p;
2004}
2005
2006/*
2007 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02002008 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 * Returns NULL in case of failure.
2010 */
2011 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002012parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013{
2014 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002015 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016
2017 if (**arg != '<')
2018 {
2019 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02002020 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 else
2022 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002023 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 }
2025 *arg = skipwhite(*arg + 1);
2026
Bram Moolenaard77a8522020-04-03 21:59:57 +02002027 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028
2029 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002030 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 {
2032 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002033 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 }
2035 ++*arg;
2036
2037 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002038 return get_list_type(member_type, type_gap);
2039 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040}
2041
2042/*
2043 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02002044 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 */
2046 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002047parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048{
2049 char_u *p = *arg;
2050 size_t len;
2051
2052 // skip over the first word
2053 while (ASCII_ISALNUM(*p) || *p == '_')
2054 ++p;
2055 len = p - *arg;
2056
2057 switch (**arg)
2058 {
2059 case 'a':
2060 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
2061 {
2062 *arg += len;
2063 return &t_any;
2064 }
2065 break;
2066 case 'b':
2067 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
2068 {
2069 *arg += len;
2070 return &t_bool;
2071 }
2072 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
2073 {
2074 *arg += len;
2075 return &t_blob;
2076 }
2077 break;
2078 case 'c':
2079 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
2080 {
2081 *arg += len;
2082 return &t_channel;
2083 }
2084 break;
2085 case 'd':
2086 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
2087 {
2088 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002089 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 }
2091 break;
2092 case 'f':
2093 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
2094 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01002095#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 *arg += len;
2097 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002098#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002099 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002100 return &t_any;
2101#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 }
2103 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2104 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002105 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002106 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002107 int argcount = -1;
2108 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002109 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002110 type_T *arg_type[MAX_FUNC_ARGS + 1];
2111
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002112 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002114 if (**arg == '(')
2115 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002116 // "func" may or may not return a value, "func()" does
2117 // not return a value.
2118 ret_type = &t_void;
2119
Bram Moolenaard77a8522020-04-03 21:59:57 +02002120 p = ++*arg;
2121 argcount = 0;
2122 while (*p != NUL && *p != ')')
2123 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002124 if (*p == '?')
2125 {
2126 if (first_optional == -1)
2127 first_optional = argcount;
2128 ++p;
2129 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002130 else if (STRNCMP(p, "...", 3) == 0)
2131 {
2132 flags |= TTFLAG_VARARGS;
2133 p += 3;
2134 }
Bram Moolenaar01865ad2020-07-26 18:33:09 +02002135 else if (first_optional != -1)
2136 {
2137 emsg(_("E1007: mandatory argument after optional argument"));
2138 return &t_any;
2139 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002140
2141 arg_type[argcount++] = parse_type(&p, type_gap);
2142
2143 // Nothing comes after "...{type}".
2144 if (flags & TTFLAG_VARARGS)
2145 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002146
Bram Moolenaard77a8522020-04-03 21:59:57 +02002147 if (*p != ',' && *skipwhite(p) == ',')
2148 {
2149 semsg(_(e_no_white_before), ",");
2150 return &t_any;
2151 }
2152 if (*p == ',')
2153 {
2154 ++p;
2155 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002156 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002157 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002158 return &t_any;
2159 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002160 }
2161 p = skipwhite(p);
2162 if (argcount == MAX_FUNC_ARGS)
2163 {
2164 emsg(_("E740: Too many argument types"));
2165 return &t_any;
2166 }
2167 }
2168
2169 p = skipwhite(p);
2170 if (*p != ')')
2171 {
2172 emsg(_(e_missing_close));
2173 return &t_any;
2174 }
2175 *arg = p + 1;
2176 }
2177 if (**arg == ':')
2178 {
2179 // parse return type
2180 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002181 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002182 semsg(_(e_white_after), ":");
2183 *arg = skipwhite(*arg);
2184 ret_type = parse_type(arg, type_gap);
2185 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002186 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002187 type = get_func_type(ret_type, argcount, type_gap);
2188 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002189 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002190 type = alloc_func_type(ret_type, argcount, type_gap);
2191 type->tt_flags = flags;
2192 if (argcount > 0)
2193 {
2194 type->tt_argcount = argcount;
2195 type->tt_min_argcount = first_optional == -1
2196 ? argcount : first_optional;
2197 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002198 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002199 return &t_any;
2200 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002201 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002202 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002203 }
2204 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 }
2206 break;
2207 case 'j':
2208 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2209 {
2210 *arg += len;
2211 return &t_job;
2212 }
2213 break;
2214 case 'l':
2215 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2216 {
2217 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002218 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 }
2220 break;
2221 case 'n':
2222 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2223 {
2224 *arg += len;
2225 return &t_number;
2226 }
2227 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 case 's':
2229 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2230 {
2231 *arg += len;
2232 return &t_string;
2233 }
2234 break;
2235 case 'v':
2236 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2237 {
2238 *arg += len;
2239 return &t_void;
2240 }
2241 break;
2242 }
2243
2244 semsg(_("E1010: Type not recognized: %s"), *arg);
2245 return &t_any;
2246}
2247
2248/*
2249 * Check if "type1" and "type2" are exactly the same.
2250 */
2251 static int
2252equal_type(type_T *type1, type_T *type2)
2253{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002254 int i;
2255
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 if (type1->tt_type != type2->tt_type)
2257 return FALSE;
2258 switch (type1->tt_type)
2259 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002261 case VAR_ANY:
2262 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 case VAR_SPECIAL:
2264 case VAR_BOOL:
2265 case VAR_NUMBER:
2266 case VAR_FLOAT:
2267 case VAR_STRING:
2268 case VAR_BLOB:
2269 case VAR_JOB:
2270 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002271 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 case VAR_LIST:
2273 case VAR_DICT:
2274 return equal_type(type1->tt_member, type2->tt_member);
2275 case VAR_FUNC:
2276 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002277 if (!equal_type(type1->tt_member, type2->tt_member)
2278 || type1->tt_argcount != type2->tt_argcount)
2279 return FALSE;
2280 if (type1->tt_argcount < 0
2281 || type1->tt_args == NULL || type2->tt_args == NULL)
2282 return TRUE;
2283 for (i = 0; i < type1->tt_argcount; ++i)
2284 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2285 return FALSE;
2286 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 }
2288 return TRUE;
2289}
2290
2291/*
2292 * Find the common type of "type1" and "type2" and put it in "dest".
2293 * "type2" and "dest" may be the same.
2294 */
2295 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002296common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297{
2298 if (equal_type(type1, type2))
2299 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002300 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 return;
2302 }
2303
2304 if (type1->tt_type == type2->tt_type)
2305 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2307 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002308 type_T *common;
2309
Bram Moolenaard77a8522020-04-03 21:59:57 +02002310 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002311 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002312 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002313 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002314 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 return;
2316 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002317 if (type1->tt_type == VAR_FUNC)
2318 {
2319 type_T *common;
2320
2321 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2322 if (type1->tt_argcount == type2->tt_argcount
2323 && type1->tt_argcount >= 0)
2324 {
2325 int argcount = type1->tt_argcount;
2326 int i;
2327
2328 *dest = alloc_func_type(common, argcount, type_gap);
2329 if (type1->tt_args != NULL && type2->tt_args != NULL)
2330 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002331 if (func_type_add_arg_types(*dest, argcount,
2332 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002333 for (i = 0; i < argcount; ++i)
2334 common_type(type1->tt_args[i], type2->tt_args[i],
2335 &(*dest)->tt_args[i], type_gap);
2336 }
2337 }
2338 else
2339 *dest = alloc_func_type(common, -1, type_gap);
2340 return;
2341 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 }
2343
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002344 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345}
2346
2347 char *
2348vartype_name(vartype_T type)
2349{
2350 switch (type)
2351 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002352 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002353 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355 case VAR_SPECIAL: return "special";
2356 case VAR_BOOL: return "bool";
2357 case VAR_NUMBER: return "number";
2358 case VAR_FLOAT: return "float";
2359 case VAR_STRING: return "string";
2360 case VAR_BLOB: return "blob";
2361 case VAR_JOB: return "job";
2362 case VAR_CHANNEL: return "channel";
2363 case VAR_LIST: return "list";
2364 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002365
2366 case VAR_FUNC:
2367 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002369 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370}
2371
2372/*
2373 * Return the name of a type.
2374 * The result may be in allocated memory, in which case "tofree" is set.
2375 */
2376 char *
2377type_name(type_T *type, char **tofree)
2378{
2379 char *name = vartype_name(type->tt_type);
2380
2381 *tofree = NULL;
2382 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2383 {
2384 char *member_free;
2385 char *member_name = type_name(type->tt_member, &member_free);
2386 size_t len;
2387
2388 len = STRLEN(name) + STRLEN(member_name) + 3;
2389 *tofree = alloc(len);
2390 if (*tofree != NULL)
2391 {
2392 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2393 vim_free(member_free);
2394 return *tofree;
2395 }
2396 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002397 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002398 {
2399 garray_T ga;
2400 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002401 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002402
2403 ga_init2(&ga, 1, 100);
2404 if (ga_grow(&ga, 20) == FAIL)
2405 return "[unknown]";
2406 *tofree = ga.ga_data;
2407 STRCPY(ga.ga_data, "func(");
2408 ga.ga_len += 5;
2409
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002410 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002411 {
2412 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002413 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002414 int len;
2415
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002416 if (type->tt_args == NULL)
2417 arg_type = "[unknown]";
2418 else
2419 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002420 if (i > 0)
2421 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002422 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002423 ga.ga_len += 2;
2424 }
2425 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002426 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002427 {
2428 vim_free(arg_free);
2429 return "[unknown]";
2430 }
2431 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002432 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002433 {
2434 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2435 ga.ga_len += 3;
2436 }
2437 else if (i >= type->tt_min_argcount)
2438 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002439 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002440 ga.ga_len += len;
2441 vim_free(arg_free);
2442 }
2443
2444 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002445 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002446 else
2447 {
2448 char *ret_free;
2449 char *ret_name = type_name(type->tt_member, &ret_free);
2450 int len;
2451
2452 len = (int)STRLEN(ret_name) + 4;
2453 if (ga_grow(&ga, len) == FAIL)
2454 {
2455 vim_free(ret_free);
2456 return "[unknown]";
2457 }
2458 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002459 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2460 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002461 vim_free(ret_free);
2462 }
2463 return ga.ga_data;
2464 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465
2466 return name;
2467}
2468
2469/*
2470 * Find "name" in script-local items of script "sid".
2471 * Returns the index in "sn_var_vals" if found.
2472 * If found but not in "sn_var_vals" returns -1.
2473 * If not found returns -2.
2474 */
2475 int
2476get_script_item_idx(int sid, char_u *name, int check_writable)
2477{
2478 hashtab_T *ht;
2479 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002480 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 int idx;
2482
2483 // First look the name up in the hashtable.
2484 if (sid <= 0 || sid > script_items.ga_len)
2485 return -1;
2486 ht = &SCRIPT_VARS(sid);
2487 di = find_var_in_ht(ht, 0, name, TRUE);
2488 if (di == NULL)
2489 return -2;
2490
2491 // Now find the svar_T index in sn_var_vals.
2492 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2493 {
2494 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2495
2496 if (sv->sv_tv == &di->di_tv)
2497 {
2498 if (check_writable && sv->sv_const)
2499 semsg(_(e_readonlyvar), name);
2500 return idx;
2501 }
2502 }
2503 return -1;
2504}
2505
2506/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002507 * Find "name" in imported items of the current script or in "cctx" if not
2508 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 */
2510 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002511find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002513 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 int idx;
2515
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002516 if (current_sctx.sc_sid <= 0)
2517 return NULL;
2518 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 if (cctx != NULL)
2520 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2521 {
2522 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2523 + idx;
2524
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002525 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2526 : STRLEN(import->imp_name) == len
2527 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 return import;
2529 }
2530
2531 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2532 {
2533 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2534
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002535 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2536 : STRLEN(import->imp_name) == len
2537 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 return import;
2539 }
2540 return NULL;
2541}
2542
2543/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002544 * Free all imported variables.
2545 */
2546 static void
2547free_imported(cctx_T *cctx)
2548{
2549 int idx;
2550
2551 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2552 {
2553 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2554
2555 vim_free(import->imp_name);
2556 }
2557 ga_clear(&cctx->ctx_imports);
2558}
2559
2560/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002561 * Return TRUE if "p" points at a "#" but not at "#{".
2562 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002563 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002564vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002565{
2566 return p[0] == '#' && p[1] != '{';
2567}
2568
2569/*
2570 * Return a pointer to the next line that isn't empty or only contains a
2571 * comment. Skips over white space.
2572 * Returns NULL if there is none.
2573 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002574 char_u *
2575peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002576{
2577 int lnum = cctx->ctx_lnum;
2578
2579 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2580 {
2581 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002582 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002583
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002584 if (line == NULL)
2585 break;
2586 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002587 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002588 return p;
2589 }
2590 return NULL;
2591}
2592
2593/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002594 * Called when checking for a following operator at "arg". When the rest of
2595 * the line is empty or only a comment, peek the next line. If there is a next
2596 * line return a pointer to it and set "nextp".
2597 * Otherwise skip over white space.
2598 */
2599 static char_u *
2600may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2601{
2602 char_u *p = skipwhite(arg);
2603
2604 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002605 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002606 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002607 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002608 if (*nextp != NULL)
2609 return *nextp;
2610 }
2611 return p;
2612}
2613
2614/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002615 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002616 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002617 * Returns NULL when at the end.
2618 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002619 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002620next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002621{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002622 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002623
2624 do
2625 {
2626 ++cctx->ctx_lnum;
2627 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002628 {
2629 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002630 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002631 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002632 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002633 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002634 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002635 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002636 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002637 return line;
2638}
2639
2640/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002641 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002642 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002643 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2644 */
2645 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002646may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002647{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002648 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002649 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002650 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002651
2652 if (next == NULL)
2653 return FAIL;
2654 *arg = skipwhite(next);
2655 }
2656 return OK;
2657}
2658
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002659/*
2660 * Idem, and give an error when failed.
2661 */
2662 static int
2663may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2664{
2665 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2666 {
2667 emsg(_("E1097: line incomplete"));
2668 return FAIL;
2669 }
2670 return OK;
2671}
2672
2673
Bram Moolenaara5565e42020-05-09 15:44:01 +02002674// Structure passed between the compile_expr* functions to keep track of
2675// constants that have been parsed but for which no code was produced yet. If
2676// possible expressions on these constants are applied at compile time. If
2677// that is not possible, the code to push the constants needs to be generated
2678// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002679// Using 50 should be more than enough of 5 levels of ().
2680#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002681typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002682 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002683 int pp_used; // active entries in pp_tv[]
2684} ppconst_T;
2685
Bram Moolenaar1c747212020-05-09 18:28:34 +02002686static int compile_expr0(char_u **arg, cctx_T *cctx);
2687static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2688
Bram Moolenaara5565e42020-05-09 15:44:01 +02002689/*
2690 * Generate a PUSH instruction for "tv".
2691 * "tv" will be consumed or cleared.
2692 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2693 */
2694 static int
2695generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2696{
2697 if (tv != NULL)
2698 {
2699 switch (tv->v_type)
2700 {
2701 case VAR_UNKNOWN:
2702 break;
2703 case VAR_BOOL:
2704 generate_PUSHBOOL(cctx, tv->vval.v_number);
2705 break;
2706 case VAR_SPECIAL:
2707 generate_PUSHSPEC(cctx, tv->vval.v_number);
2708 break;
2709 case VAR_NUMBER:
2710 generate_PUSHNR(cctx, tv->vval.v_number);
2711 break;
2712#ifdef FEAT_FLOAT
2713 case VAR_FLOAT:
2714 generate_PUSHF(cctx, tv->vval.v_float);
2715 break;
2716#endif
2717 case VAR_BLOB:
2718 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2719 tv->vval.v_blob = NULL;
2720 break;
2721 case VAR_STRING:
2722 generate_PUSHS(cctx, tv->vval.v_string);
2723 tv->vval.v_string = NULL;
2724 break;
2725 default:
2726 iemsg("constant type not supported");
2727 clear_tv(tv);
2728 return FAIL;
2729 }
2730 tv->v_type = VAR_UNKNOWN;
2731 }
2732 return OK;
2733}
2734
2735/*
2736 * Generate code for any ppconst entries.
2737 */
2738 static int
2739generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2740{
2741 int i;
2742 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002743 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002744
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002745 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002746 for (i = 0; i < ppconst->pp_used; ++i)
2747 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2748 ret = FAIL;
2749 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002750 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002751 return ret;
2752}
2753
2754/*
2755 * Clear ppconst constants. Used when failing.
2756 */
2757 static void
2758clear_ppconst(ppconst_T *ppconst)
2759{
2760 int i;
2761
2762 for (i = 0; i < ppconst->pp_used; ++i)
2763 clear_tv(&ppconst->pp_tv[i]);
2764 ppconst->pp_used = 0;
2765}
2766
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002767/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002768 * Generate an instruction to load script-local variable "name", without the
2769 * leading "s:".
2770 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 */
2772 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002773compile_load_scriptvar(
2774 cctx_T *cctx,
2775 char_u *name, // variable NUL terminated
2776 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002777 char_u **end, // end of variable
2778 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002780 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2782 imported_T *import;
2783
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002784 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002786 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002787 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2788 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 }
2790 if (idx >= 0)
2791 {
2792 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2793
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002794 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 current_sctx.sc_sid, idx, sv->sv_type);
2796 return OK;
2797 }
2798
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002799 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 if (import != NULL)
2801 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002802 if (import->imp_all)
2803 {
2804 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002805 char_u *exp_name;
2806 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002807 ufunc_T *ufunc;
2808 type_T *type;
2809
2810 // Used "import * as Name", need to lookup the member.
2811 if (*p != '.')
2812 {
2813 semsg(_("E1060: expected dot after name: %s"), start);
2814 return FAIL;
2815 }
2816 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002817 if (VIM_ISWHITE(*p))
2818 {
2819 emsg(_("E1074: no white space allowed after dot"));
2820 return FAIL;
2821 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002822
Bram Moolenaar1c991142020-07-04 13:15:31 +02002823 // isolate one name
2824 exp_name = p;
2825 while (eval_isnamec(*p))
2826 ++p;
2827 cc = *p;
2828 *p = NUL;
2829
2830 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2831 *p = cc;
2832 p = skipwhite(p);
2833
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002834 // TODO: what if it is a function?
2835 if (idx < 0)
2836 return FAIL;
2837 *end = p;
2838
2839 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2840 import->imp_sid,
2841 idx,
2842 type);
2843 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002844 else if (import->imp_funcname != NULL)
2845 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002846 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002847 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2848 import->imp_sid,
2849 import->imp_var_vals_idx,
2850 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 return OK;
2852 }
2853
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002854 if (error)
2855 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 return FAIL;
2857}
2858
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002859 static int
2860generate_funcref(cctx_T *cctx, char_u *name)
2861{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002862 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002863
2864 if (ufunc == NULL)
2865 return FAIL;
2866
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002867 // Need to compile any default values to get the argument types.
2868 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2869 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2870 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002871 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002872}
2873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874/*
2875 * Compile a variable name into a load instruction.
2876 * "end" points to just after the name.
2877 * When "error" is FALSE do not give an error when not found.
2878 */
2879 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002880compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881{
2882 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002883 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002884 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002886 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887
2888 if (*(*arg + 1) == ':')
2889 {
2890 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002891 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002892 {
2893 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002895 switch (**arg)
2896 {
2897 case 'g': isn_type = ISN_LOADGDICT; break;
2898 case 'w': isn_type = ISN_LOADWDICT; break;
2899 case 't': isn_type = ISN_LOADTDICT; break;
2900 case 'b': isn_type = ISN_LOADBDICT; break;
2901 default:
2902 semsg(_(e_namespace), *arg);
2903 goto theend;
2904 }
2905 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2906 goto theend;
2907 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002908 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 else
2910 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002911 isntype_T isn_type = ISN_DROP;
2912
2913 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2914 if (name == NULL)
2915 return FAIL;
2916
2917 switch (**arg)
2918 {
2919 case 'v': res = generate_LOADV(cctx, name, error);
2920 break;
2921 case 's': res = compile_load_scriptvar(cctx, name,
2922 NULL, NULL, error);
2923 break;
2924 case 'g': isn_type = ISN_LOADG; break;
2925 case 'w': isn_type = ISN_LOADW; break;
2926 case 't': isn_type = ISN_LOADT; break;
2927 case 'b': isn_type = ISN_LOADB; break;
2928 default: semsg(_(e_namespace), *arg);
2929 goto theend;
2930 }
2931 if (isn_type != ISN_DROP)
2932 {
2933 // Global, Buffer-local, Window-local and Tabpage-local
2934 // variables can be defined later, thus we don't check if it
2935 // exists, give error at runtime.
2936 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2937 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 }
2939 }
2940 else
2941 {
2942 size_t len = end - *arg;
2943 int idx;
2944 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002945 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946
2947 name = vim_strnsave(*arg, end - *arg);
2948 if (name == NULL)
2949 return FAIL;
2950
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002951 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002953 if (!gen_load_outer)
2954 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 }
2956 else
2957 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002958 lvar_T *lvar = lookup_local(*arg, len, cctx);
2959
2960 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002962 type = lvar->lv_type;
2963 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002964 if (lvar->lv_from_outer)
2965 gen_load_outer = TRUE;
2966 else
2967 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 }
2969 else
2970 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002971 // "var" can be script-local even without using "s:" if it
2972 // already exists.
2973 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2974 == SCRIPT_VERSION_VIM9
2975 || lookup_script(*arg, len) == OK)
2976 res = compile_load_scriptvar(cctx, name, *arg, &end,
2977 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002978
Bram Moolenaara5565e42020-05-09 15:44:01 +02002979 // When the name starts with an uppercase letter or "x:" it
2980 // can be a user defined function.
2981 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2982 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 }
2984 }
2985 if (gen_load)
2986 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002987 if (gen_load_outer)
2988 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 }
2990
2991 *arg = end;
2992
2993theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002994 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 semsg(_(e_var_notfound), name);
2996 vim_free(name);
2997 return res;
2998}
2999
3000/*
3001 * Compile the argument expressions.
3002 * "arg" points to just after the "(" and is advanced to after the ")"
3003 */
3004 static int
3005compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3006{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003007 char_u *p = *arg;
3008 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009
Bram Moolenaare6085c52020-04-12 20:19:16 +02003010 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003012 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3013 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003014 if (*p == ')')
3015 {
3016 *arg = p + 1;
3017 return OK;
3018 }
3019
Bram Moolenaara5565e42020-05-09 15:44:01 +02003020 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 return FAIL;
3022 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003023
3024 if (*p != ',' && *skipwhite(p) == ',')
3025 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02003026 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003027 p = skipwhite(p);
3028 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003030 {
3031 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003032 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02003033 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003034 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003035 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003036 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003038failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003039 emsg(_(e_missing_close));
3040 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041}
3042
3043/*
3044 * Compile a function call: name(arg1, arg2)
3045 * "arg" points to "name", "arg + varlen" to the "(".
3046 * "argcount_init" is 1 for "value->method()"
3047 * Instructions:
3048 * EVAL arg1
3049 * EVAL arg2
3050 * BCALL / DCALL / UCALL
3051 */
3052 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003053compile_call(
3054 char_u **arg,
3055 size_t varlen,
3056 cctx_T *cctx,
3057 ppconst_T *ppconst,
3058 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059{
3060 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003061 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 int argcount = argcount_init;
3063 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003064 char_u fname_buf[FLEN_FIXED + 1];
3065 char_u *tofree = NULL;
3066 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003068 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069
Bram Moolenaara5565e42020-05-09 15:44:01 +02003070 // we can evaluate "has('name')" at compile time
3071 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3072 {
3073 char_u *s = skipwhite(*arg + varlen + 1);
3074 typval_T argvars[2];
3075
3076 argvars[0].v_type = VAR_UNKNOWN;
3077 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003078 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003079 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003080 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003081 s = skipwhite(s);
3082 if (*s == ')' && argvars[0].v_type == VAR_STRING)
3083 {
3084 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3085
3086 *arg = s + 1;
3087 argvars[1].v_type = VAR_UNKNOWN;
3088 tv->v_type = VAR_NUMBER;
3089 tv->vval.v_number = 0;
3090 f_has(argvars, tv);
3091 clear_tv(&argvars[0]);
3092 ++ppconst->pp_used;
3093 return OK;
3094 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003095 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003096 }
3097
3098 if (generate_ppconst(cctx, ppconst) == FAIL)
3099 return FAIL;
3100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 if (varlen >= sizeof(namebuf))
3102 {
3103 semsg(_("E1011: name too long: %s"), name);
3104 return FAIL;
3105 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003106 vim_strncpy(namebuf, *arg, varlen);
3107 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108
3109 *arg = skipwhite(*arg + varlen + 1);
3110 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003111 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003113 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 {
3115 int idx;
3116
3117 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003118 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003120 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003121 else
3122 semsg(_(e_unknownfunc), namebuf);
3123 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 }
3125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003127 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003129 {
3130 res = generate_CALL(cctx, ufunc, argcount);
3131 goto theend;
3132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133
3134 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003135 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003137 if (STRNCMP(namebuf, "g:", 2) != 0
3138 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003139 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003140 garray_T *stack = &cctx->ctx_type_stack;
3141 type_T *type;
3142
3143 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3144 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003145 goto theend;
3146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003148 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003149 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003150 if (STRNCMP(namebuf, "g:", 2) == 0)
3151 res = generate_UCALL(cctx, name, argcount);
3152 else
3153 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003154
3155theend:
3156 vim_free(tofree);
3157 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158}
3159
3160// like NAMESPACE_CHAR but with 'a' and 'l'.
3161#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3162
3163/*
3164 * Find the end of a variable or function name. Unlike find_name_end() this
3165 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003166 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 * Return a pointer to just after the name. Equal to "arg" if there is no
3168 * valid name.
3169 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003170 static char_u *
3171to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172{
3173 char_u *p;
3174
3175 // Quick check for valid starting character.
3176 if (!eval_isnamec1(*arg))
3177 return arg;
3178
3179 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3180 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3181 // and can be used in slice "[n:]".
3182 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003183 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3185 break;
3186 return p;
3187}
3188
3189/*
3190 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003191 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 */
3193 char_u *
3194to_name_const_end(char_u *arg)
3195{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003196 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 typval_T rettv;
3198
3199 if (p == arg && *arg == '[')
3200 {
3201
3202 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003203 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 p = arg;
3205 }
3206 else if (p == arg && *arg == '#' && arg[1] == '{')
3207 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003208 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003210 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 p = arg;
3212 }
3213 else if (p == arg && *arg == '{')
3214 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003215 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003217 // Can be "{x -> ret}()".
3218 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003220 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 if (ret != OK)
3222 p = arg;
3223 }
3224
3225 return p;
3226}
3227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228/*
3229 * parse a list: [expr, expr]
3230 * "*arg" points to the '['.
3231 */
3232 static int
3233compile_list(char_u **arg, cctx_T *cctx)
3234{
3235 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003236 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 int count = 0;
3238
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003239 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003241 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003242 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003243 semsg(_(e_list_end), *arg);
3244 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003245 }
3246 if (*p == ']')
3247 {
3248 ++p;
3249 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003250 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003251 p += STRLEN(p);
3252 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003253 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003254 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 break;
3256 ++count;
3257 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003258 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003260 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3261 {
3262 semsg(_(e_white_after), ",");
3263 return FAIL;
3264 }
3265 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003266 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 p = skipwhite(p);
3268 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003269 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270
3271 generate_NEWLIST(cctx, count);
3272 return OK;
3273}
3274
3275/*
3276 * parse a lambda: {arg, arg -> expr}
3277 * "*arg" points to the '{'.
3278 */
3279 static int
3280compile_lambda(char_u **arg, cctx_T *cctx)
3281{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 typval_T rettv;
3283 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003284 evalarg_T evalarg;
3285
3286 CLEAR_FIELD(evalarg);
3287 evalarg.eval_flags = EVAL_EVALUATE;
3288 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289
3290 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003291 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003293
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003295 ++ufunc->uf_refcount;
3296 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003297 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298
3299 // The function will have one line: "return {expr}".
3300 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003301 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003303 clear_evalarg(&evalarg, NULL);
3304
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003305 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003306 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003307
3308 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 return FAIL;
3310}
3311
3312/*
3313 * Compile a lamda call: expr->{lambda}(args)
3314 * "arg" points to the "{".
3315 */
3316 static int
3317compile_lambda_call(char_u **arg, cctx_T *cctx)
3318{
3319 ufunc_T *ufunc;
3320 typval_T rettv;
3321 int argcount = 1;
3322 int ret = FAIL;
3323
3324 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003325 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 return FAIL;
3327
3328 if (**arg != '(')
3329 {
3330 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003331 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 else
3333 semsg(_(e_missing_paren), "lambda");
3334 clear_tv(&rettv);
3335 return FAIL;
3336 }
3337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 ufunc = rettv.vval.v_partial->pt_func;
3339 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003340 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003341 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003342
3343 // The function will have one line: "return {expr}".
3344 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003345 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346
3347 // compile the arguments
3348 *arg = skipwhite(*arg + 1);
3349 if (compile_arguments(arg, cctx, &argcount) == OK)
3350 // call the compiled function
3351 ret = generate_CALL(cctx, ufunc, argcount);
3352
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003353 if (ret == FAIL)
3354 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 return ret;
3356}
3357
3358/*
3359 * parse a dict: {'key': val} or #{key: val}
3360 * "*arg" points to the '{'.
3361 */
3362 static int
3363compile_dict(char_u **arg, cctx_T *cctx, int literal)
3364{
3365 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003366 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 int count = 0;
3368 dict_T *d = dict_alloc();
3369 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003370 char_u *whitep = *arg;
3371 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372
3373 if (d == NULL)
3374 return FAIL;
3375 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003376 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 {
3378 char_u *key = NULL;
3379
Bram Moolenaar23c55272020-06-21 16:58:13 +02003380 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003381 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003382 *arg = NULL;
3383 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003384 }
3385
3386 if (**arg == '}')
3387 break;
3388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 if (literal)
3390 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003391 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392
Bram Moolenaar2c330432020-04-13 14:41:35 +02003393 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 {
3395 semsg(_("E1014: Invalid key: %s"), *arg);
3396 return FAIL;
3397 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003398 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 if (generate_PUSHS(cctx, key) == FAIL)
3400 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003401 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 }
3403 else
3404 {
3405 isn_T *isn;
3406
Bram Moolenaara5565e42020-05-09 15:44:01 +02003407 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3410 if (isn->isn_type == ISN_PUSHS)
3411 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003412 else
3413 {
3414 type_T *keytype = ((type_T **)stack->ga_data)
3415 [stack->ga_len - 1];
3416 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3417 return FAIL;
3418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 }
3420
3421 // Check for duplicate keys, if using string keys.
3422 if (key != NULL)
3423 {
3424 item = dict_find(d, key, -1);
3425 if (item != NULL)
3426 {
3427 semsg(_(e_duplicate_key), key);
3428 goto failret;
3429 }
3430 item = dictitem_alloc(key);
3431 if (item != NULL)
3432 {
3433 item->di_tv.v_type = VAR_UNKNOWN;
3434 item->di_tv.v_lock = 0;
3435 if (dict_add(d, item) == FAIL)
3436 dictitem_free(item);
3437 }
3438 }
3439
3440 *arg = skipwhite(*arg);
3441 if (**arg != ':')
3442 {
3443 semsg(_(e_missing_dict_colon), *arg);
3444 return FAIL;
3445 }
3446
Bram Moolenaar2c330432020-04-13 14:41:35 +02003447 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003449 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003450 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003451 *arg = NULL;
3452 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003453 }
3454
Bram Moolenaara5565e42020-05-09 15:44:01 +02003455 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 return FAIL;
3457 ++count;
3458
Bram Moolenaar2c330432020-04-13 14:41:35 +02003459 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003460 *arg = skipwhite(*arg);
3461 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003462 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003463 *arg = NULL;
3464 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003465 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 if (**arg == '}')
3467 break;
3468 if (**arg != ',')
3469 {
3470 semsg(_(e_missing_dict_comma), *arg);
3471 goto failret;
3472 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003473 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 *arg = skipwhite(*arg + 1);
3475 }
3476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 *arg = *arg + 1;
3478
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003479 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003480 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003481 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003482 *arg += STRLEN(*arg);
3483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 dict_unref(d);
3485 return generate_NEWDICT(cctx, count);
3486
3487failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003488 if (*arg == NULL)
3489 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490 dict_unref(d);
3491 return FAIL;
3492}
3493
3494/*
3495 * Compile "&option".
3496 */
3497 static int
3498compile_get_option(char_u **arg, cctx_T *cctx)
3499{
3500 typval_T rettv;
3501 char_u *start = *arg;
3502 int ret;
3503
3504 // parse the option and get the current value to get the type.
3505 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003506 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 if (ret == OK)
3508 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003509 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 char_u *name = vim_strnsave(start, *arg - start);
3511 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3512
3513 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3514 vim_free(name);
3515 }
3516 clear_tv(&rettv);
3517
3518 return ret;
3519}
3520
3521/*
3522 * Compile "$VAR".
3523 */
3524 static int
3525compile_get_env(char_u **arg, cctx_T *cctx)
3526{
3527 char_u *start = *arg;
3528 int len;
3529 int ret;
3530 char_u *name;
3531
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 ++*arg;
3533 len = get_env_len(arg);
3534 if (len == 0)
3535 {
3536 semsg(_(e_syntax_at), start - 1);
3537 return FAIL;
3538 }
3539
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003540 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 name = vim_strnsave(start, len + 1);
3542 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3543 vim_free(name);
3544 return ret;
3545}
3546
3547/*
3548 * Compile "@r".
3549 */
3550 static int
3551compile_get_register(char_u **arg, cctx_T *cctx)
3552{
3553 int ret;
3554
3555 ++*arg;
3556 if (**arg == NUL)
3557 {
3558 semsg(_(e_syntax_at), *arg - 1);
3559 return FAIL;
3560 }
3561 if (!valid_yank_reg(**arg, TRUE))
3562 {
3563 emsg_invreg(**arg);
3564 return FAIL;
3565 }
3566 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3567 ++*arg;
3568 return ret;
3569}
3570
3571/*
3572 * Apply leading '!', '-' and '+' to constant "rettv".
3573 */
3574 static int
3575apply_leader(typval_T *rettv, char_u *start, char_u *end)
3576{
3577 char_u *p = end;
3578
3579 // this works from end to start
3580 while (p > start)
3581 {
3582 --p;
3583 if (*p == '-' || *p == '+')
3584 {
3585 // only '-' has an effect, for '+' we only check the type
3586#ifdef FEAT_FLOAT
3587 if (rettv->v_type == VAR_FLOAT)
3588 {
3589 if (*p == '-')
3590 rettv->vval.v_float = -rettv->vval.v_float;
3591 }
3592 else
3593#endif
3594 {
3595 varnumber_T val;
3596 int error = FALSE;
3597
3598 // tv_get_number_chk() accepts a string, but we don't want that
3599 // here
3600 if (check_not_string(rettv) == FAIL)
3601 return FAIL;
3602 val = tv_get_number_chk(rettv, &error);
3603 clear_tv(rettv);
3604 if (error)
3605 return FAIL;
3606 if (*p == '-')
3607 val = -val;
3608 rettv->v_type = VAR_NUMBER;
3609 rettv->vval.v_number = val;
3610 }
3611 }
3612 else
3613 {
3614 int v = tv2bool(rettv);
3615
3616 // '!' is permissive in the type.
3617 clear_tv(rettv);
3618 rettv->v_type = VAR_BOOL;
3619 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3620 }
3621 }
3622 return OK;
3623}
3624
3625/*
3626 * Recognize v: variables that are constants and set "rettv".
3627 */
3628 static void
3629get_vim_constant(char_u **arg, typval_T *rettv)
3630{
3631 if (STRNCMP(*arg, "v:true", 6) == 0)
3632 {
3633 rettv->v_type = VAR_BOOL;
3634 rettv->vval.v_number = VVAL_TRUE;
3635 *arg += 6;
3636 }
3637 else if (STRNCMP(*arg, "v:false", 7) == 0)
3638 {
3639 rettv->v_type = VAR_BOOL;
3640 rettv->vval.v_number = VVAL_FALSE;
3641 *arg += 7;
3642 }
3643 else if (STRNCMP(*arg, "v:null", 6) == 0)
3644 {
3645 rettv->v_type = VAR_SPECIAL;
3646 rettv->vval.v_number = VVAL_NULL;
3647 *arg += 6;
3648 }
3649 else if (STRNCMP(*arg, "v:none", 6) == 0)
3650 {
3651 rettv->v_type = VAR_SPECIAL;
3652 rettv->vval.v_number = VVAL_NONE;
3653 *arg += 6;
3654 }
3655}
3656
Bram Moolenaar696ba232020-07-29 21:20:41 +02003657 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003658get_compare_type(char_u *p, int *len, int *type_is)
3659{
3660 exptype_T type = EXPR_UNKNOWN;
3661 int i;
3662
3663 switch (p[0])
3664 {
3665 case '=': if (p[1] == '=')
3666 type = EXPR_EQUAL;
3667 else if (p[1] == '~')
3668 type = EXPR_MATCH;
3669 break;
3670 case '!': if (p[1] == '=')
3671 type = EXPR_NEQUAL;
3672 else if (p[1] == '~')
3673 type = EXPR_NOMATCH;
3674 break;
3675 case '>': if (p[1] != '=')
3676 {
3677 type = EXPR_GREATER;
3678 *len = 1;
3679 }
3680 else
3681 type = EXPR_GEQUAL;
3682 break;
3683 case '<': if (p[1] != '=')
3684 {
3685 type = EXPR_SMALLER;
3686 *len = 1;
3687 }
3688 else
3689 type = EXPR_SEQUAL;
3690 break;
3691 case 'i': if (p[1] == 's')
3692 {
3693 // "is" and "isnot"; but not a prefix of a name
3694 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3695 *len = 5;
3696 i = p[*len];
3697 if (!isalnum(i) && i != '_')
3698 {
3699 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3700 *type_is = TRUE;
3701 }
3702 }
3703 break;
3704 }
3705 return type;
3706}
3707
3708/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 * Compile code to apply '-', '+' and '!'.
3710 */
3711 static int
3712compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3713{
3714 char_u *p = end;
3715
3716 // this works from end to start
3717 while (p > start)
3718 {
3719 --p;
3720 if (*p == '-' || *p == '+')
3721 {
3722 int negate = *p == '-';
3723 isn_T *isn;
3724
3725 // TODO: check type
3726 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3727 {
3728 --p;
3729 if (*p == '-')
3730 negate = !negate;
3731 }
3732 // only '-' has an effect, for '+' we only check the type
3733 if (negate)
3734 isn = generate_instr(cctx, ISN_NEGATENR);
3735 else
3736 isn = generate_instr(cctx, ISN_CHECKNR);
3737 if (isn == NULL)
3738 return FAIL;
3739 }
3740 else
3741 {
3742 int invert = TRUE;
3743
3744 while (p > start && p[-1] == '!')
3745 {
3746 --p;
3747 invert = !invert;
3748 }
3749 if (generate_2BOOL(cctx, invert) == FAIL)
3750 return FAIL;
3751 }
3752 }
3753 return OK;
3754}
3755
3756/*
3757 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003758 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 */
3760 static int
3761compile_subscript(
3762 char_u **arg,
3763 cctx_T *cctx,
3764 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003765 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003766 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767{
3768 for (;;)
3769 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003770 char_u *p = skipwhite(*arg);
3771
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003772 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003773 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003774 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003775
3776 // If a following line starts with "->{" or "->X" advance to that
3777 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003778 // Also if a following line starts with ".x".
3779 if (next != NULL &&
3780 ((next[0] == '-' && next[1] == '>'
3781 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003782 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003783 {
3784 next = next_line_from_context(cctx, TRUE);
3785 if (next == NULL)
3786 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003787 *arg = next;
3788 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003789 }
3790 }
3791
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003792 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3793 // not a function call.
3794 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003796 garray_T *stack = &cctx->ctx_type_stack;
3797 type_T *type;
3798 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003800 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003801 return FAIL;
3802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003804 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3805
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003806 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3808 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003809 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 return FAIL;
3811 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003812 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003814 char_u *pstart = p;
3815
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003816 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003817 return FAIL;
3818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 // something->method()
3820 // Apply the '!', '-' and '+' first:
3821 // -1.0->func() works like (-1.0)->func()
3822 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3823 return FAIL;
3824 *start_leader = end_leader; // don't apply again later
3825
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003826 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003827 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003828 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 if (**arg == '{')
3830 {
3831 // lambda call: list->{lambda}
3832 if (compile_lambda_call(arg, cctx) == FAIL)
3833 return FAIL;
3834 }
3835 else
3836 {
3837 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003838 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003839 if (!eval_isnamec1(*p))
3840 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003841 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003842 return FAIL;
3843 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003844 if (ASCII_ISALPHA(*p) && p[1] == ':')
3845 p += 2;
3846 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 ;
3848 if (*p != '(')
3849 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003850 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 return FAIL;
3852 }
3853 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003854 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 return FAIL;
3856 }
3857 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003858 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003860 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003861 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003862 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003863
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003864 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003865 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003866 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003867 // TODO: blob index
3868 // TODO: more arguments
3869 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003870 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003871 return FAIL;
3872
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003873 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003874 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003875 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003876 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003877 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 return FAIL;
3879
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003880 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3881 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 if (**arg != ']')
3883 {
3884 emsg(_(e_missbrac));
3885 return FAIL;
3886 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003887 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003889 // We can index a list and a dict. If we don't know the type
3890 // we can use the index value type.
3891 // TODO: If we don't know use an instruction to figure it out at
3892 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003893 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003894 vtype = (*typep)->tt_type;
3895 if (*typep == &t_any)
3896 {
3897 type_T *valtype = ((type_T **)stack->ga_data)
3898 [stack->ga_len - 1];
3899 if (valtype == &t_string)
3900 vtype = VAR_DICT;
3901 }
3902 if (vtype == VAR_DICT)
3903 {
3904 if ((*typep)->tt_type == VAR_DICT)
3905 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003906 else
3907 {
3908 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3909 return FAIL;
3910 *typep = &t_any;
3911 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003912 if (may_generate_2STRING(-1, cctx) == FAIL)
3913 return FAIL;
3914 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3915 return FAIL;
3916 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003917 else if (vtype == VAR_STRING)
3918 {
3919 *typep = &t_number;
3920 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3921 return FAIL;
3922 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003923 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003924 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003925 if ((*typep)->tt_type == VAR_LIST)
3926 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003927 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003928 return FAIL;
3929 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003930 else
3931 {
3932 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003933 return FAIL;
3934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003936 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003938 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003939 return FAIL;
3940
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003941 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003942 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3943 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003945 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003946 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 while (eval_isnamec(*p))
3948 MB_PTR_ADV(p);
3949 if (p == *arg)
3950 {
3951 semsg(_(e_syntax_at), *arg);
3952 return FAIL;
3953 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003954 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 return FAIL;
3956 *arg = p;
3957 }
3958 else
3959 break;
3960 }
3961
3962 // TODO - see handle_subscript():
3963 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3964 // Don't do this when "Func" is already a partial that was bound
3965 // explicitly (pt_auto is FALSE).
3966
3967 return OK;
3968}
3969
3970/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003971 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3972 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003974 * If the value is a constant "ppconst->pp_ret" will be set.
3975 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003976 *
3977 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 */
3979
3980/*
3981 * number number constant
3982 * 0zFFFFFFFF Blob constant
3983 * "string" string constant
3984 * 'string' literal string constant
3985 * &option-name option value
3986 * @r register contents
3987 * identifier variable value
3988 * function() function call
3989 * $VAR environment variable
3990 * (expression) nested expression
3991 * [expr, expr] List
3992 * {key: val, key: val} Dictionary
3993 * #{key: val, key: val} Dictionary with literal keys
3994 *
3995 * Also handle:
3996 * ! in front logical NOT
3997 * - in front unary minus
3998 * + in front unary plus (ignored)
3999 * trailing (arg) funcref/partial call
4000 * trailing [] subscript in String or List
4001 * trailing .name entry in Dictionary
4002 * trailing ->name() method call
4003 */
4004 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004005compile_expr7(
4006 char_u **arg,
4007 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004008 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 char_u *start_leader, *end_leader;
4011 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004012 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004013 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014
4015 /*
4016 * Skip '!', '-' and '+' characters. They are handled later.
4017 */
4018 start_leader = *arg;
4019 while (**arg == '!' || **arg == '-' || **arg == '+')
4020 *arg = skipwhite(*arg + 1);
4021 end_leader = *arg;
4022
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004023 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 switch (**arg)
4025 {
4026 /*
4027 * Number constant.
4028 */
4029 case '0': // also for blob starting with 0z
4030 case '1':
4031 case '2':
4032 case '3':
4033 case '4':
4034 case '5':
4035 case '6':
4036 case '7':
4037 case '8':
4038 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004039 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 return FAIL;
4041 break;
4042
4043 /*
4044 * String constant: "string".
4045 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004046 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 return FAIL;
4048 break;
4049
4050 /*
4051 * Literal string constant: 'str''ing'.
4052 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004053 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 return FAIL;
4055 break;
4056
4057 /*
4058 * Constant Vim variable.
4059 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004060 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 ret = NOTDONE;
4062 break;
4063
4064 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004065 * "true" constant
4066 */
4067 case 't': if (STRNCMP(*arg, "true", 4) == 0
4068 && !eval_isnamec((*arg)[4]))
4069 {
4070 *arg += 4;
4071 rettv->v_type = VAR_BOOL;
4072 rettv->vval.v_number = VVAL_TRUE;
4073 }
4074 else
4075 ret = NOTDONE;
4076 break;
4077
4078 /*
4079 * "false" constant
4080 */
4081 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4082 && !eval_isnamec((*arg)[5]))
4083 {
4084 *arg += 5;
4085 rettv->v_type = VAR_BOOL;
4086 rettv->vval.v_number = VVAL_FALSE;
4087 }
4088 else
4089 ret = NOTDONE;
4090 break;
4091
4092 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 * List: [expr, expr]
4094 */
4095 case '[': ret = compile_list(arg, cctx);
4096 break;
4097
4098 /*
4099 * Dictionary: #{key: val, key: val}
4100 */
4101 case '#': if ((*arg)[1] == '{')
4102 {
4103 ++*arg;
4104 ret = compile_dict(arg, cctx, TRUE);
4105 }
4106 else
4107 ret = NOTDONE;
4108 break;
4109
4110 /*
4111 * Lambda: {arg, arg -> expr}
4112 * Dictionary: {'key': val, 'key': val}
4113 */
4114 case '{': {
4115 char_u *start = skipwhite(*arg + 1);
4116
4117 // Find out what comes after the arguments.
4118 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004119 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 if (ret != FAIL && *start == '>')
4121 ret = compile_lambda(arg, cctx);
4122 else
4123 ret = compile_dict(arg, cctx, FALSE);
4124 }
4125 break;
4126
4127 /*
4128 * Option value: &name
4129 */
4130 case '&': ret = compile_get_option(arg, cctx);
4131 break;
4132
4133 /*
4134 * Environment variable: $VAR.
4135 */
4136 case '$': ret = compile_get_env(arg, cctx);
4137 break;
4138
4139 /*
4140 * Register contents: @r.
4141 */
4142 case '@': ret = compile_get_register(arg, cctx);
4143 break;
4144 /*
4145 * nested expression: (expression).
4146 */
4147 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004148
4149 // recursive!
4150 if (ppconst->pp_used <= PPSIZE - 10)
4151 {
4152 ret = compile_expr1(arg, cctx, ppconst);
4153 }
4154 else
4155 {
4156 // Not enough space in ppconst, flush constants.
4157 if (generate_ppconst(cctx, ppconst) == FAIL)
4158 return FAIL;
4159 ret = compile_expr0(arg, cctx);
4160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 *arg = skipwhite(*arg);
4162 if (**arg == ')')
4163 ++*arg;
4164 else if (ret == OK)
4165 {
4166 emsg(_(e_missing_close));
4167 ret = FAIL;
4168 }
4169 break;
4170
4171 default: ret = NOTDONE;
4172 break;
4173 }
4174 if (ret == FAIL)
4175 return FAIL;
4176
Bram Moolenaar1c747212020-05-09 18:28:34 +02004177 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 {
4179 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004180 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004182 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 return FAIL;
4184 }
4185 start_leader = end_leader; // don't apply again below
4186
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004187 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004188 clear_tv(rettv);
4189 else
4190 // A constant expression can possibly be handled compile time,
4191 // return the value instead of generating code.
4192 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 }
4194 else if (ret == NOTDONE)
4195 {
4196 char_u *p;
4197 int r;
4198
4199 if (!eval_isnamec1(**arg))
4200 {
4201 semsg(_("E1015: Name expected: %s"), *arg);
4202 return FAIL;
4203 }
4204
4205 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004206 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004208 {
4209 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004212 {
4213 if (generate_ppconst(cctx, ppconst) == FAIL)
4214 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004216 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 if (r == FAIL)
4218 return FAIL;
4219 }
4220
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004221 // Handle following "[]", ".member", etc.
4222 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004223 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004224 ppconst) == FAIL)
4225 return FAIL;
4226 if (ppconst->pp_used > 0)
4227 {
4228 // apply the '!', '-' and '+' before the constant
4229 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4230 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4231 return FAIL;
4232 return OK;
4233 }
4234 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004236 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237}
4238
4239/*
4240 * * number multiplication
4241 * / number division
4242 * % number modulo
4243 */
4244 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004245compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246{
4247 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004248 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004249 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004251 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004252 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 return FAIL;
4254
4255 /*
4256 * Repeat computing, until no "*", "/" or "%" is following.
4257 */
4258 for (;;)
4259 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004260 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 if (*op != '*' && *op != '/' && *op != '%')
4262 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004263 if (next != NULL)
4264 {
4265 *arg = next_line_from_context(cctx, TRUE);
4266 op = skipwhite(*arg);
4267 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004268
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004269 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 {
4271 char_u buf[3];
4272
4273 vim_strncpy(buf, op, 1);
4274 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004275 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 }
4277 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004278 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004279 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004281 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004282 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004284
4285 if (ppconst->pp_used == ppconst_used + 2
4286 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4287 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004288 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004289 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4290 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004291 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004293 // both are numbers: compute the result
4294 switch (*op)
4295 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004296 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004297 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004298 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004299 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004300 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004301 break;
4302 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004303 tv1->vval.v_number = res;
4304 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004305 }
4306 else
4307 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004308 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004309 generate_two_op(cctx, op);
4310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 }
4312
4313 return OK;
4314}
4315
4316/*
4317 * + number addition
4318 * - number subtraction
4319 * .. string concatenation
4320 */
4321 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004322compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323{
4324 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004325 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004327 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328
4329 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004330 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 return FAIL;
4332
4333 /*
4334 * Repeat computing, until no "+", "-" or ".." is following.
4335 */
4336 for (;;)
4337 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004338 op = may_peek_next_line(cctx, *arg, &next);
4339 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 break;
4341 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004342 if (next != NULL)
4343 {
4344 *arg = next_line_from_context(cctx, TRUE);
4345 op = skipwhite(*arg);
4346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004348 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 {
4350 char_u buf[3];
4351
4352 vim_strncpy(buf, op, oplen);
4353 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004354 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 }
4356
4357 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004358 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004359 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004361 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004362 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 return FAIL;
4364
Bram Moolenaara5565e42020-05-09 15:44:01 +02004365 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004366 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004367 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4368 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4369 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4370 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004372 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4373 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004374
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004375 // concat/subtract/add constant numbers
4376 if (*op == '+')
4377 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4378 else if (*op == '-')
4379 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4380 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004381 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004382 // concatenate constant strings
4383 char_u *s1 = tv1->vval.v_string;
4384 char_u *s2 = tv2->vval.v_string;
4385 size_t len1 = STRLEN(s1);
4386
4387 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4388 if (tv1->vval.v_string == NULL)
4389 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004390 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004391 return FAIL;
4392 }
4393 mch_memmove(tv1->vval.v_string, s1, len1);
4394 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004395 vim_free(s1);
4396 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004397 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004398 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 }
4400 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004401 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004402 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004403 if (*op == '.')
4404 {
4405 if (may_generate_2STRING(-2, cctx) == FAIL
4406 || may_generate_2STRING(-1, cctx) == FAIL)
4407 return FAIL;
4408 generate_instr_drop(cctx, ISN_CONCAT, 1);
4409 }
4410 else
4411 generate_two_op(cctx, op);
4412 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 }
4414
4415 return OK;
4416}
4417
4418/*
4419 * expr5a == expr5b
4420 * expr5a =~ expr5b
4421 * expr5a != expr5b
4422 * expr5a !~ expr5b
4423 * expr5a > expr5b
4424 * expr5a >= expr5b
4425 * expr5a < expr5b
4426 * expr5a <= expr5b
4427 * expr5a is expr5b
4428 * expr5a isnot expr5b
4429 *
4430 * Produces instructions:
4431 * EVAL expr5a Push result of "expr5a"
4432 * EVAL expr5b Push result of "expr5b"
4433 * COMPARE one of the compare instructions
4434 */
4435 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004436compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437{
4438 exptype_T type = EXPR_UNKNOWN;
4439 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004440 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004443 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444
4445 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004446 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 return FAIL;
4448
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004449 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004450 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451
4452 /*
4453 * If there is a comparative operator, use it.
4454 */
4455 if (type != EXPR_UNKNOWN)
4456 {
4457 int ic = FALSE; // Default: do not ignore case
4458
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004459 if (next != NULL)
4460 {
4461 *arg = next_line_from_context(cctx, TRUE);
4462 p = skipwhite(*arg);
4463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 if (type_is && (p[len] == '?' || p[len] == '#'))
4465 {
4466 semsg(_(e_invexpr2), *arg);
4467 return FAIL;
4468 }
4469 // extra question mark appended: ignore case
4470 if (p[len] == '?')
4471 {
4472 ic = TRUE;
4473 ++len;
4474 }
4475 // extra '#' appended: match case (ignored)
4476 else if (p[len] == '#')
4477 ++len;
4478 // nothing appended: match case
4479
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004480 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 {
4482 char_u buf[7];
4483
4484 vim_strncpy(buf, p, len);
4485 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004486 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 }
4488
4489 // get the second variable
4490 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004491 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004492 return FAIL;
4493
Bram Moolenaara5565e42020-05-09 15:44:01 +02004494 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 return FAIL;
4496
Bram Moolenaara5565e42020-05-09 15:44:01 +02004497 if (ppconst->pp_used == ppconst_used + 2)
4498 {
4499 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4500 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4501 int ret;
4502
4503 // Both sides are a constant, compute the result now.
4504 // First check for a valid combination of types, this is more
4505 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004506 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004507 ret = FAIL;
4508 else
4509 {
4510 ret = typval_compare(tv1, tv2, type, ic);
4511 tv1->v_type = VAR_BOOL;
4512 tv1->vval.v_number = tv1->vval.v_number
4513 ? VVAL_TRUE : VVAL_FALSE;
4514 clear_tv(tv2);
4515 --ppconst->pp_used;
4516 }
4517 return ret;
4518 }
4519
4520 generate_ppconst(cctx, ppconst);
4521 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 }
4523
4524 return OK;
4525}
4526
Bram Moolenaar7f141552020-05-09 17:35:53 +02004527static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529/*
4530 * Compile || or &&.
4531 */
4532 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004533compile_and_or(
4534 char_u **arg,
4535 cctx_T *cctx,
4536 char *op,
4537 ppconst_T *ppconst,
4538 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004540 char_u *next;
4541 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 int opchar = *op;
4543
4544 if (p[0] == opchar && p[1] == opchar)
4545 {
4546 garray_T *instr = &cctx->ctx_instr;
4547 garray_T end_ga;
4548
4549 /*
4550 * Repeat until there is no following "||" or "&&"
4551 */
4552 ga_init2(&end_ga, sizeof(int), 10);
4553 while (p[0] == opchar && p[1] == opchar)
4554 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004555 if (next != NULL)
4556 {
4557 *arg = next_line_from_context(cctx, TRUE);
4558 p = skipwhite(*arg);
4559 }
4560
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004561 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4562 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004564 return FAIL;
4565 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566
Bram Moolenaara5565e42020-05-09 15:44:01 +02004567 // TODO: use ppconst if the value is a constant
4568 generate_ppconst(cctx, ppconst);
4569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 if (ga_grow(&end_ga, 1) == FAIL)
4571 {
4572 ga_clear(&end_ga);
4573 return FAIL;
4574 }
4575 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4576 ++end_ga.ga_len;
4577 generate_JUMP(cctx, opchar == '|'
4578 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4579
4580 // eval the next expression
4581 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004582 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004583 return FAIL;
4584
Bram Moolenaara5565e42020-05-09 15:44:01 +02004585 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4586 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 {
4588 ga_clear(&end_ga);
4589 return FAIL;
4590 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004591
4592 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004594 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595
4596 // Fill in the end label in all jumps.
4597 while (end_ga.ga_len > 0)
4598 {
4599 isn_T *isn;
4600
4601 --end_ga.ga_len;
4602 isn = ((isn_T *)instr->ga_data)
4603 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4604 isn->isn_arg.jump.jump_where = instr->ga_len;
4605 }
4606 ga_clear(&end_ga);
4607 }
4608
4609 return OK;
4610}
4611
4612/*
4613 * expr4a && expr4a && expr4a logical AND
4614 *
4615 * Produces instructions:
4616 * EVAL expr4a Push result of "expr4a"
4617 * JUMP_AND_KEEP_IF_FALSE end
4618 * EVAL expr4b Push result of "expr4b"
4619 * JUMP_AND_KEEP_IF_FALSE end
4620 * EVAL expr4c Push result of "expr4c"
4621 * end:
4622 */
4623 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004624compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004626 int ppconst_used = ppconst->pp_used;
4627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004629 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 return FAIL;
4631
4632 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004633 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634}
4635
4636/*
4637 * expr3a || expr3b || expr3c logical OR
4638 *
4639 * Produces instructions:
4640 * EVAL expr3a Push result of "expr3a"
4641 * JUMP_AND_KEEP_IF_TRUE end
4642 * EVAL expr3b Push result of "expr3b"
4643 * JUMP_AND_KEEP_IF_TRUE end
4644 * EVAL expr3c Push result of "expr3c"
4645 * end:
4646 */
4647 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004648compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004650 int ppconst_used = ppconst->pp_used;
4651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004653 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 return FAIL;
4655
4656 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004657 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658}
4659
4660/*
4661 * Toplevel expression: expr2 ? expr1a : expr1b
4662 *
4663 * Produces instructions:
4664 * EVAL expr2 Push result of "expr"
4665 * JUMP_IF_FALSE alt jump if false
4666 * EVAL expr1a
4667 * JUMP_ALWAYS end
4668 * alt: EVAL expr1b
4669 * end:
4670 */
4671 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004672compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673{
4674 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004675 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004676 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677
Bram Moolenaar61a89812020-05-07 16:58:17 +02004678 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004679 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 return FAIL;
4681
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004682 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 if (*p == '?')
4684 {
4685 garray_T *instr = &cctx->ctx_instr;
4686 garray_T *stack = &cctx->ctx_type_stack;
4687 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004688 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004690 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004692 int has_const_expr = FALSE;
4693 int const_value = FALSE;
4694 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004696 if (next != NULL)
4697 {
4698 *arg = next_line_from_context(cctx, TRUE);
4699 p = skipwhite(*arg);
4700 }
4701
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004702 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4703 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004705 return FAIL;
4706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707
Bram Moolenaara5565e42020-05-09 15:44:01 +02004708 if (ppconst->pp_used == ppconst_used + 1)
4709 {
4710 // the condition is a constant, we know whether the ? or the :
4711 // expression is to be evaluated.
4712 has_const_expr = TRUE;
4713 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4714 clear_tv(&ppconst->pp_tv[ppconst_used]);
4715 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004716 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4717 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718 }
4719 else
4720 {
4721 generate_ppconst(cctx, ppconst);
4722 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724
4725 // evaluate the second expression; any type is accepted
4726 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004727 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004728 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004729 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004730 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731
Bram Moolenaara5565e42020-05-09 15:44:01 +02004732 if (!has_const_expr)
4733 {
4734 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735
Bram Moolenaara5565e42020-05-09 15:44:01 +02004736 // remember the type and drop it
4737 --stack->ga_len;
4738 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739
Bram Moolenaara5565e42020-05-09 15:44:01 +02004740 end_idx = instr->ga_len;
4741 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4742
4743 // jump here from JUMP_IF_FALSE
4744 isn = ((isn_T *)instr->ga_data) + alt_idx;
4745 isn->isn_arg.jump.jump_where = instr->ga_len;
4746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747
4748 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004749 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750 if (*p != ':')
4751 {
4752 emsg(_(e_missing_colon));
4753 return FAIL;
4754 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004755 if (next != NULL)
4756 {
4757 *arg = next_line_from_context(cctx, TRUE);
4758 p = skipwhite(*arg);
4759 }
4760
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004761 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4762 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004764 return FAIL;
4765 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766
4767 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004768 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004769 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4770 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004772 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004773 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004774 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004775 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776
Bram Moolenaara5565e42020-05-09 15:44:01 +02004777 if (!has_const_expr)
4778 {
4779 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780
Bram Moolenaara5565e42020-05-09 15:44:01 +02004781 // If the types differ, the result has a more generic type.
4782 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4783 common_type(type1, type2, &type2, cctx->ctx_type_list);
4784
4785 // jump here from JUMP_ALWAYS
4786 isn = ((isn_T *)instr->ga_data) + end_idx;
4787 isn->isn_arg.jump.jump_where = instr->ga_len;
4788 }
4789
4790 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 }
4792 return OK;
4793}
4794
4795/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004796 * Toplevel expression.
4797 */
4798 static int
4799compile_expr0(char_u **arg, cctx_T *cctx)
4800{
4801 ppconst_T ppconst;
4802
4803 CLEAR_FIELD(ppconst);
4804 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4805 {
4806 clear_ppconst(&ppconst);
4807 return FAIL;
4808 }
4809 if (generate_ppconst(cctx, &ppconst) == FAIL)
4810 return FAIL;
4811 return OK;
4812}
4813
4814/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 * compile "return [expr]"
4816 */
4817 static char_u *
4818compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4819{
4820 char_u *p = arg;
4821 garray_T *stack = &cctx->ctx_type_stack;
4822 type_T *stack_type;
4823
4824 if (*p != NUL && *p != '|' && *p != '\n')
4825 {
4826 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004827 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 return NULL;
4829
4830 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4831 if (set_return_type)
4832 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004833 else
4834 {
4835 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4836 && stack_type->tt_type != VAR_VOID
4837 && stack_type->tt_type != VAR_UNKNOWN)
4838 {
4839 emsg(_("E1096: Returning a value in a function without a return type"));
4840 return NULL;
4841 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004842 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4843 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004845 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 }
4847 else
4848 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004849 // "set_return_type" cannot be TRUE, only used for a lambda which
4850 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004851 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4852 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 {
4854 emsg(_("E1003: Missing return value"));
4855 return NULL;
4856 }
4857
4858 // No argument, return zero.
4859 generate_PUSHNR(cctx, 0);
4860 }
4861
4862 if (generate_instr(cctx, ISN_RETURN) == NULL)
4863 return NULL;
4864
4865 // "return val | endif" is possible
4866 return skipwhite(p);
4867}
4868
4869/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004870 * Get a line from the compilation context, compatible with exarg_T getline().
4871 * Return a pointer to the line in allocated memory.
4872 * Return NULL for end-of-file or some error.
4873 */
4874 static char_u *
4875exarg_getline(
4876 int c UNUSED,
4877 void *cookie,
4878 int indent UNUSED,
4879 int do_concat UNUSED)
4880{
4881 cctx_T *cctx = (cctx_T *)cookie;
4882
4883 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4884 {
4885 iemsg("Heredoc got to end");
4886 return NULL;
4887 }
4888 ++cctx->ctx_lnum;
4889 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4890 [cctx->ctx_lnum]);
4891}
4892
4893/*
4894 * Compile a nested :def command.
4895 */
4896 static char_u *
4897compile_nested_function(exarg_T *eap, cctx_T *cctx)
4898{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004899 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004900 char_u *name_start = eap->arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004901 char_u *name_end = to_name_end(eap->arg, is_global);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004902 char_u *name = get_lambda_name();
4903 lvar_T *lvar;
4904 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004905 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004906
4907 eap->arg = name_end;
4908 eap->getline = exarg_getline;
4909 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004910 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004911 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004912 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004913
Bram Moolenaar822ba242020-05-24 23:00:18 +02004914 if (ufunc == NULL)
4915 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004916 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004917 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004918 return NULL;
4919
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004920 if (is_global)
4921 {
4922 char_u *func_name = vim_strnsave(name_start + 2,
4923 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004924
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004925 if (func_name == NULL)
4926 r = FAIL;
4927 else
4928 r = generate_NEWFUNC(cctx, name, func_name);
4929 }
4930 else
4931 {
4932 // Define a local variable for the function reference.
4933 lvar = reserve_local(cctx, name_start, name_end - name_start,
4934 TRUE, ufunc->uf_func_type);
4935 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL)
4936 return NULL;
4937 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4938 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004939
Bram Moolenaar61a89812020-05-07 16:58:17 +02004940 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004941 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004942}
4943
4944/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 * Return the length of an assignment operator, or zero if there isn't one.
4946 */
4947 int
4948assignment_len(char_u *p, int *heredoc)
4949{
4950 if (*p == '=')
4951 {
4952 if (p[1] == '<' && p[2] == '<')
4953 {
4954 *heredoc = TRUE;
4955 return 3;
4956 }
4957 return 1;
4958 }
4959 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4960 return 2;
4961 if (STRNCMP(p, "..=", 3) == 0)
4962 return 3;
4963 return 0;
4964}
4965
4966// words that cannot be used as a variable
4967static char *reserved[] = {
4968 "true",
4969 "false",
4970 NULL
4971};
4972
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004973typedef enum {
4974 dest_local,
4975 dest_option,
4976 dest_env,
4977 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004978 dest_buffer,
4979 dest_window,
4980 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004981 dest_vimvar,
4982 dest_script,
4983 dest_reg,
4984} assign_dest_T;
4985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004987 * Generate the load instruction for "name".
4988 */
4989 static void
4990generate_loadvar(
4991 cctx_T *cctx,
4992 assign_dest_T dest,
4993 char_u *name,
4994 lvar_T *lvar,
4995 type_T *type)
4996{
4997 switch (dest)
4998 {
4999 case dest_option:
5000 // TODO: check the option exists
5001 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5002 break;
5003 case dest_global:
5004 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5005 break;
5006 case dest_buffer:
5007 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5008 break;
5009 case dest_window:
5010 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5011 break;
5012 case dest_tab:
5013 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5014 break;
5015 case dest_script:
5016 compile_load_scriptvar(cctx,
5017 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5018 break;
5019 case dest_env:
5020 // Include $ in the name here
5021 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5022 break;
5023 case dest_reg:
5024 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5025 break;
5026 case dest_vimvar:
5027 generate_LOADV(cctx, name + 2, TRUE);
5028 break;
5029 case dest_local:
5030 if (lvar->lv_from_outer)
5031 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5032 NULL, type);
5033 else
5034 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5035 break;
5036 }
5037}
5038
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005039 void
5040vim9_declare_error(char_u *name)
5041{
5042 char *scope = "";
5043
5044 switch (*name)
5045 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005046 case 'g': scope = _("global"); break;
5047 case 'b': scope = _("buffer"); break;
5048 case 'w': scope = _("window"); break;
5049 case 't': scope = _("tab"); break;
5050 case 'v': scope = "v:"; break;
5051 case '$': semsg(_(e_declare_env_var), name); return;
5052 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005053 }
5054 semsg(_(e_declare_var), scope, name);
5055}
5056
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005057/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005058 * Compile declaration and assignment:
5059 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005061 * Return NULL for an error.
5062 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063 */
5064 static char_u *
5065compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5066{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005067 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005069 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 char_u *ret = NULL;
5071 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005072 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005073 int scriptvar_sid = 0;
5074 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005077 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 int oplen = 0;
5080 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005081 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005082 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005083 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005087 // Skip over the "var" or "[var, var]" to get to any "=".
5088 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5089 if (p == NULL)
5090 return *arg == '[' ? arg : NULL;
5091
5092 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005094 // TODO: should we allow this, and figure out type inference from list
5095 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005096 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 return NULL;
5098 }
5099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 sp = p;
5101 p = skipwhite(p);
5102 op = p;
5103 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005104
5105 if (var_count > 0 && oplen == 0)
5106 // can be something like "[1, 2]->func()"
5107 return arg;
5108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5110 {
5111 char_u buf[4];
5112
5113 vim_strncpy(buf, op, oplen);
5114 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005115 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005116 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118 if (heredoc)
5119 {
5120 list_T *l;
5121 listitem_T *li;
5122
5123 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005124 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005126 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127
5128 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005129 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 {
5131 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5132 li->li_tv.vval.v_string = NULL;
5133 }
5134 generate_NEWLIST(cctx, l->lv_len);
5135 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005136 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 list_free(l);
5138 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005139 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005140 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005141 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005143 // for "[var, var] = expr" evaluate the expression here, loop over the
5144 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005145
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005146 p = skipwhite(op + oplen);
5147 if (compile_expr0(&p, cctx) == FAIL)
5148 return NULL;
5149 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005150
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005151 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005153 type_T *stacktype;
5154
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005155 stacktype = stack->ga_len == 0 ? &t_void
5156 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005157 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005158 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005159 emsg(_(e_cannot_use_void));
5160 goto theend;
5161 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005162 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005163 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005164 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005165 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5166 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005167 }
5168 }
5169
5170 /*
5171 * Loop over variables in "[var, var] = expr".
5172 * For "var = expr" and "let var: type" this is done only once.
5173 */
5174 if (var_count > 0)
5175 var_start = skipwhite(arg + 1); // skip over the "["
5176 else
5177 var_start = arg;
5178 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5179 {
5180 char_u *var_end = skip_var_one(var_start, FALSE);
5181 size_t varlen;
5182 int new_local = FALSE;
5183 int opt_type;
5184 int opt_flags = 0;
5185 assign_dest_T dest = dest_local;
5186 int vimvaridx = -1;
5187 lvar_T *lvar = NULL;
5188 lvar_T arg_lvar;
5189 int has_type = FALSE;
5190 int has_index = FALSE;
5191 int instr_count = -1;
5192
5193 p = (*var_start == '&' || *var_start == '$'
5194 || *var_start == '@') ? var_start + 1 : var_start;
5195 p = to_name_end(p, TRUE);
5196
5197 // "a: type" is declaring variable "a" with a type, not "a:".
5198 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5199 --var_end;
5200 if (is_decl && p == var_start + 2 && p[-1] == ':')
5201 --p;
5202
5203 varlen = p - var_start;
5204 vim_free(name);
5205 name = vim_strnsave(var_start, varlen);
5206 if (name == NULL)
5207 return NULL;
5208 if (!heredoc)
5209 type = &t_any;
5210
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005211 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005212 {
5213 if (*var_start == '&')
5214 {
5215 int cc;
5216 long numval;
5217
5218 dest = dest_option;
5219 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005221 emsg(_(e_const_option));
5222 goto theend;
5223 }
5224 if (is_decl)
5225 {
5226 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5227 goto theend;
5228 }
5229 p = var_start;
5230 p = find_option_end(&p, &opt_flags);
5231 if (p == NULL)
5232 {
5233 // cannot happen?
5234 emsg(_(e_letunexp));
5235 goto theend;
5236 }
5237 cc = *p;
5238 *p = NUL;
5239 opt_type = get_option_value(var_start + 1, &numval,
5240 NULL, opt_flags);
5241 *p = cc;
5242 if (opt_type == -3)
5243 {
5244 semsg(_(e_unknown_option), var_start);
5245 goto theend;
5246 }
5247 if (opt_type == -2 || opt_type == 0)
5248 type = &t_string;
5249 else
5250 type = &t_number; // both number and boolean option
5251 }
5252 else if (*var_start == '$')
5253 {
5254 dest = dest_env;
5255 type = &t_string;
5256 if (is_decl)
5257 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005258 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005259 goto theend;
5260 }
5261 }
5262 else if (*var_start == '@')
5263 {
5264 if (!valid_yank_reg(var_start[1], TRUE))
5265 {
5266 emsg_invreg(var_start[1]);
5267 goto theend;
5268 }
5269 dest = dest_reg;
5270 type = &t_string;
5271 if (is_decl)
5272 {
5273 semsg(_("E1066: Cannot declare a register: %s"), name);
5274 goto theend;
5275 }
5276 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005277 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005278 {
5279 dest = dest_global;
5280 if (is_decl)
5281 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005282 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005283 goto theend;
5284 }
5285 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005286 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005287 {
5288 dest = dest_buffer;
5289 if (is_decl)
5290 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005291 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005292 goto theend;
5293 }
5294 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005295 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005296 {
5297 dest = dest_window;
5298 if (is_decl)
5299 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005300 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005301 goto theend;
5302 }
5303 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005304 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005305 {
5306 dest = dest_tab;
5307 if (is_decl)
5308 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005309 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005310 goto theend;
5311 }
5312 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005313 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005314 {
5315 typval_T *vtv;
5316 int di_flags;
5317
5318 vimvaridx = find_vim_var(name + 2, &di_flags);
5319 if (vimvaridx < 0)
5320 {
5321 semsg(_(e_var_notfound), var_start);
5322 goto theend;
5323 }
5324 // We use the current value of "sandbox" here, is that OK?
5325 if (var_check_ro(di_flags, name, FALSE))
5326 goto theend;
5327 dest = dest_vimvar;
5328 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005329 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005330 if (is_decl)
5331 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005332 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333 goto theend;
5334 }
5335 }
5336 else
5337 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005338 int idx;
5339 imported_T *import = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005340
5341 for (idx = 0; reserved[idx] != NULL; ++idx)
5342 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005343 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005344 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005345 goto theend;
5346 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005347
5348 lvar = lookup_local(var_start, varlen, cctx);
5349 if (lvar == NULL)
5350 {
5351 CLEAR_FIELD(arg_lvar);
5352 if (lookup_arg(var_start, varlen,
5353 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5354 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005355 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005356 if (is_decl)
5357 {
5358 semsg(_(e_used_as_arg), name);
5359 goto theend;
5360 }
5361 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005362 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005363 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005364 if (lvar != NULL)
5365 {
5366 if (is_decl)
5367 {
5368 semsg(_("E1017: Variable already declared: %s"), name);
5369 goto theend;
5370 }
5371 else if (lvar->lv_const)
5372 {
5373 semsg(_("E1018: Cannot assign to a constant: %s"),
5374 name);
5375 goto theend;
5376 }
5377 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005378 else if ((varlen > 1 && STRNCMP(var_start, "s:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005379 || lookup_script(var_start, varlen) == OK
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005380 || (import = find_imported(var_start, varlen, cctx))
5381 != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005383 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5384
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005385 if (is_decl)
5386 {
Bram Moolenaar33afa242020-07-29 19:18:00 +02005387 if ((varlen > 1 && STRNCMP(var_start, "s:", 2) == 0))
5388 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
5389 name);
5390 else
5391 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005392 name);
5393 goto theend;
5394 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005395 dest = dest_script;
5396
5397 // existing script-local variables should have a type
5398 scriptvar_sid = current_sctx.sc_sid;
5399 if (import != NULL)
5400 scriptvar_sid = import->imp_sid;
5401 scriptvar_idx = get_script_item_idx(scriptvar_sid,
5402 rawname, TRUE);
5403 if (scriptvar_idx >= 0)
5404 {
5405 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5406 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
5407 + scriptvar_idx;
5408 type = sv->sv_type;
5409 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005410 }
5411 else if (name[1] == ':' && name[2] != NUL)
5412 {
5413 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5414 name);
5415 goto theend;
5416 }
5417 else if (!is_decl)
5418 {
5419 semsg(_("E1089: unknown variable: %s"), name);
5420 goto theend;
5421 }
5422 }
5423 }
5424
5425 // handle "a:name" as a name, not index "name" on "a"
5426 if (varlen > 1 || var_start[varlen] != ':')
5427 p = var_end;
5428
5429 if (dest != dest_option)
5430 {
5431 if (is_decl && *p == ':')
5432 {
5433 // parse optional type: "let var: type = expr"
5434 if (!VIM_ISWHITE(p[1]))
5435 {
5436 semsg(_(e_white_after), ":");
5437 goto theend;
5438 }
5439 p = skipwhite(p + 1);
5440 type = parse_type(&p, cctx->ctx_type_list);
5441 has_type = TRUE;
5442 }
5443 else if (lvar != NULL)
5444 type = lvar->lv_type;
5445 }
5446
5447 if (oplen == 3 && !heredoc && dest != dest_global
5448 && type->tt_type != VAR_STRING
5449 && type->tt_type != VAR_ANY)
5450 {
5451 emsg(_("E1019: Can only concatenate to string"));
5452 goto theend;
5453 }
5454
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005455 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005456 {
5457 if (oplen > 1 && !heredoc)
5458 {
5459 // +=, /=, etc. require an existing variable
5460 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5461 name);
5462 goto theend;
5463 }
5464
5465 // new local variable
5466 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5467 goto theend;
5468 lvar = reserve_local(cctx, var_start, varlen,
5469 cmdidx == CMD_const, type);
5470 if (lvar == NULL)
5471 goto theend;
5472 new_local = TRUE;
5473 }
5474
5475 member_type = type;
5476 if (var_end > var_start + varlen)
5477 {
5478 // Something follows after the variable: "var[idx]".
5479 if (is_decl)
5480 {
5481 emsg(_("E1087: cannot use an index when declaring a variable"));
5482 goto theend;
5483 }
5484
5485 if (var_start[varlen] == '[')
5486 {
5487 has_index = TRUE;
5488 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005489 member_type = &t_any;
5490 else
5491 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005492 }
5493 else
5494 {
5495 semsg("Not supported yet: %s", var_start);
5496 goto theend;
5497 }
5498 }
5499 else if (lvar == &arg_lvar)
5500 {
5501 semsg(_("E1090: Cannot assign to argument %s"), name);
5502 goto theend;
5503 }
5504
5505 if (!heredoc)
5506 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005507 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005508 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005509 if (oplen > 0 && var_count == 0)
5510 {
5511 // skip over the "=" and the expression
5512 p = skipwhite(op + oplen);
5513 compile_expr0(&p, cctx);
5514 }
5515 }
5516 else if (oplen > 0)
5517 {
5518 type_T *stacktype;
5519
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005520 // For "var = expr" evaluate the expression.
5521 if (var_count == 0)
5522 {
5523 int r;
5524
5525 // for "+=", "*=", "..=" etc. first load the current value
5526 if (*op != '=')
5527 {
5528 generate_loadvar(cctx, dest, name, lvar, type);
5529
5530 if (has_index)
5531 {
5532 // TODO: get member from list or dict
5533 emsg("Index with operation not supported yet");
5534 goto theend;
5535 }
5536 }
5537
5538 // Compile the expression. Temporarily hide the new local
5539 // variable here, it is not available to this expression.
5540 if (new_local)
5541 --cctx->ctx_locals.ga_len;
5542 instr_count = instr->ga_len;
5543 p = skipwhite(op + oplen);
5544 r = compile_expr0(&p, cctx);
5545 if (new_local)
5546 ++cctx->ctx_locals.ga_len;
5547 if (r == FAIL)
5548 goto theend;
5549 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005550 else if (semicolon && var_idx == var_count - 1)
5551 {
5552 // For "[var; var] = expr" get the rest of the list
5553 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5554 goto theend;
5555 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005556 else
5557 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005558 // For "[var, var] = expr" get the "var_idx" item from the
5559 // list.
5560 if (generate_GETITEM(cctx, var_idx) == FAIL)
5561 return FAIL;
5562 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005563
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005564 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005565 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005566 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005567 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005568 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005569 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005570 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005571 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005572 emsg(_(e_cannot_use_void));
5573 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005574 }
5575 else
5576 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005577 // An empty list or dict has a &t_void member,
5578 // for a variable that implies &t_any.
5579 if (stacktype == &t_list_empty)
5580 lvar->lv_type = &t_list_any;
5581 else if (stacktype == &t_dict_empty)
5582 lvar->lv_type = &t_dict_any;
5583 else
5584 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005585 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005586 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005587 else
5588 {
5589 type_T *use_type = lvar->lv_type;
5590
5591 if (has_index)
5592 {
5593 use_type = use_type->tt_member;
5594 if (use_type == NULL)
5595 use_type = &t_void;
5596 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005597 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005598 == FAIL)
5599 goto theend;
5600 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005601 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005602 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005603 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005604 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005606 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005608 emsg(_(e_const_req_value));
5609 goto theend;
5610 }
5611 else if (!has_type || dest == dest_option)
5612 {
5613 emsg(_(e_type_req));
5614 goto theend;
5615 }
5616 else
5617 {
5618 // variables are always initialized
5619 if (ga_grow(instr, 1) == FAIL)
5620 goto theend;
5621 switch (member_type->tt_type)
5622 {
5623 case VAR_BOOL:
5624 generate_PUSHBOOL(cctx, VVAL_FALSE);
5625 break;
5626 case VAR_FLOAT:
5627#ifdef FEAT_FLOAT
5628 generate_PUSHF(cctx, 0.0);
5629#endif
5630 break;
5631 case VAR_STRING:
5632 generate_PUSHS(cctx, NULL);
5633 break;
5634 case VAR_BLOB:
5635 generate_PUSHBLOB(cctx, NULL);
5636 break;
5637 case VAR_FUNC:
5638 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5639 break;
5640 case VAR_LIST:
5641 generate_NEWLIST(cctx, 0);
5642 break;
5643 case VAR_DICT:
5644 generate_NEWDICT(cctx, 0);
5645 break;
5646 case VAR_JOB:
5647 generate_PUSHJOB(cctx, NULL);
5648 break;
5649 case VAR_CHANNEL:
5650 generate_PUSHCHANNEL(cctx, NULL);
5651 break;
5652 case VAR_NUMBER:
5653 case VAR_UNKNOWN:
5654 case VAR_ANY:
5655 case VAR_PARTIAL:
5656 case VAR_VOID:
5657 case VAR_SPECIAL: // cannot happen
5658 generate_PUSHNR(cctx, 0);
5659 break;
5660 }
5661 }
5662 if (var_count == 0)
5663 end = p;
5664 }
5665
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005666 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005667 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005668 break;
5669
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005670 if (oplen > 0 && *op != '=')
5671 {
5672 type_T *expected = &t_number;
5673 type_T *stacktype;
5674
5675 // TODO: if type is known use float or any operation
5676
5677 if (*op == '.')
5678 expected = &t_string;
5679 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005680 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005681 goto theend;
5682
5683 if (*op == '.')
5684 generate_instr_drop(cctx, ISN_CONCAT, 1);
5685 else
5686 {
5687 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5688
5689 if (isn == NULL)
5690 goto theend;
5691 switch (*op)
5692 {
5693 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5694 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5695 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5696 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5697 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005699 }
5700 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005701
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005702 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005703 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005704 int r;
5705
5706 // Compile the "idx" in "var[idx]".
5707 if (new_local)
5708 --cctx->ctx_locals.ga_len;
5709 p = skipwhite(var_start + varlen + 1);
5710 r = compile_expr0(&p, cctx);
5711 if (new_local)
5712 ++cctx->ctx_locals.ga_len;
5713 if (r == FAIL)
5714 goto theend;
5715 if (*skipwhite(p) != ']')
5716 {
5717 emsg(_(e_missbrac));
5718 goto theend;
5719 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005720 if (type == &t_any)
5721 {
5722 type_T *idx_type = ((type_T **)stack->ga_data)[
5723 stack->ga_len - 1];
5724 // Index on variable of unknown type: guess the type from the
5725 // index type: number is dict, otherwise dict.
5726 // TODO: should do the assignment at runtime
5727 if (idx_type->tt_type == VAR_NUMBER)
5728 type = &t_list_any;
5729 else
5730 type = &t_dict_any;
5731 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005732 if (type->tt_type == VAR_DICT
5733 && may_generate_2STRING(-1, cctx) == FAIL)
5734 goto theend;
5735 if (type->tt_type == VAR_LIST
5736 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005737 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005738 {
5739 emsg(_(e_number_exp));
5740 goto theend;
5741 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005742
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005743 // Load the dict or list. On the stack we then have:
5744 // - value
5745 // - index
5746 // - variable
5747 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005748
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005749 if (type->tt_type == VAR_LIST)
5750 {
5751 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5752 return FAIL;
5753 }
5754 else if (type->tt_type == VAR_DICT)
5755 {
5756 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5757 return FAIL;
5758 }
5759 else
5760 {
5761 emsg(_(e_listreq));
5762 goto theend;
5763 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005764 }
5765 else
5766 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005767 switch (dest)
5768 {
5769 case dest_option:
5770 generate_STOREOPT(cctx, name + 1, opt_flags);
5771 break;
5772 case dest_global:
5773 // include g: with the name, easier to execute that way
5774 generate_STORE(cctx, ISN_STOREG, 0, name);
5775 break;
5776 case dest_buffer:
5777 // include b: with the name, easier to execute that way
5778 generate_STORE(cctx, ISN_STOREB, 0, name);
5779 break;
5780 case dest_window:
5781 // include w: with the name, easier to execute that way
5782 generate_STORE(cctx, ISN_STOREW, 0, name);
5783 break;
5784 case dest_tab:
5785 // include t: with the name, easier to execute that way
5786 generate_STORE(cctx, ISN_STORET, 0, name);
5787 break;
5788 case dest_env:
5789 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5790 break;
5791 case dest_reg:
5792 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5793 break;
5794 case dest_vimvar:
5795 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5796 break;
5797 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005798 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005799 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005800 {
5801 char_u *name_s = name;
5802
5803 // Include s: in the name for store_var()
5804 if (name[1] != ':')
5805 {
5806 int len = (int)STRLEN(name) + 3;
5807
5808 name_s = alloc(len);
5809 if (name_s == NULL)
5810 name_s = name;
5811 else
5812 vim_snprintf((char *)name_s, len,
5813 "s:%s", name);
5814 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005815 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5816 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005817 if (name_s != name)
5818 vim_free(name_s);
5819 }
5820 else
5821 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005822 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005823 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005824 break;
5825 case dest_local:
5826 if (lvar != NULL)
5827 {
5828 isn_T *isn = ((isn_T *)instr->ga_data)
5829 + instr->ga_len - 1;
5830
5831 // optimization: turn "var = 123" from ISN_PUSHNR +
5832 // ISN_STORE into ISN_STORENR
5833 if (!lvar->lv_from_outer
5834 && instr->ga_len == instr_count + 1
5835 && isn->isn_type == ISN_PUSHNR)
5836 {
5837 varnumber_T val = isn->isn_arg.number;
5838
5839 isn->isn_type = ISN_STORENR;
5840 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5841 isn->isn_arg.storenr.stnr_val = val;
5842 if (stack->ga_len > 0)
5843 --stack->ga_len;
5844 }
5845 else if (lvar->lv_from_outer)
5846 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005847 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005848 else
5849 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5850 }
5851 break;
5852 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005853 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005854
5855 if (var_idx + 1 < var_count)
5856 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005858
5859 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005860 if (var_count > 0 && !semicolon)
5861 {
5862 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5863 goto theend;
5864 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005865
Bram Moolenaarb2097502020-07-19 17:17:02 +02005866 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005867
5868theend:
5869 vim_free(name);
5870 return ret;
5871}
5872
5873/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005874 * Check if "name" can be "unlet".
5875 */
5876 int
5877check_vim9_unlet(char_u *name)
5878{
5879 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5880 {
5881 semsg(_("E1081: Cannot unlet %s"), name);
5882 return FAIL;
5883 }
5884 return OK;
5885}
5886
5887/*
5888 * Callback passed to ex_unletlock().
5889 */
5890 static int
5891compile_unlet(
5892 lval_T *lvp,
5893 char_u *name_end,
5894 exarg_T *eap,
5895 int deep UNUSED,
5896 void *coookie)
5897{
5898 cctx_T *cctx = coookie;
5899
5900 if (lvp->ll_tv == NULL)
5901 {
5902 char_u *p = lvp->ll_name;
5903 int cc = *name_end;
5904 int ret = OK;
5905
5906 // Normal name. Only supports g:, w:, t: and b: namespaces.
5907 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005908 if (*p == '$')
5909 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5910 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005911 ret = FAIL;
5912 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005913 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005914
5915 *name_end = cc;
5916 return ret;
5917 }
5918
5919 // TODO: unlet {list}[idx]
5920 // TODO: unlet {dict}[key]
5921 emsg("Sorry, :unlet not fully implemented yet");
5922 return FAIL;
5923}
5924
5925/*
5926 * compile "unlet var", "lock var" and "unlock var"
5927 * "arg" points to "var".
5928 */
5929 static char_u *
5930compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5931{
5932 char_u *p = arg;
5933
5934 if (eap->cmdidx != CMD_unlet)
5935 {
5936 emsg("Sorry, :lock and unlock not implemented yet");
5937 return NULL;
5938 }
5939
5940 if (*p == '!')
5941 {
5942 p = skipwhite(p + 1);
5943 eap->forceit = TRUE;
5944 }
5945
5946 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5947 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5948}
5949
5950/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005951 * Compile an :import command.
5952 */
5953 static char_u *
5954compile_import(char_u *arg, cctx_T *cctx)
5955{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005956 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005957}
5958
5959/*
5960 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5961 */
5962 static int
5963compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5964{
5965 garray_T *instr = &cctx->ctx_instr;
5966 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5967
5968 if (endlabel == NULL)
5969 return FAIL;
5970 endlabel->el_next = *el;
5971 *el = endlabel;
5972 endlabel->el_end_label = instr->ga_len;
5973
5974 generate_JUMP(cctx, when, 0);
5975 return OK;
5976}
5977
5978 static void
5979compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5980{
5981 garray_T *instr = &cctx->ctx_instr;
5982
5983 while (*el != NULL)
5984 {
5985 endlabel_T *cur = (*el);
5986 isn_T *isn;
5987
5988 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5989 isn->isn_arg.jump.jump_where = instr->ga_len;
5990 *el = cur->el_next;
5991 vim_free(cur);
5992 }
5993}
5994
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005995 static void
5996compile_free_jump_to_end(endlabel_T **el)
5997{
5998 while (*el != NULL)
5999 {
6000 endlabel_T *cur = (*el);
6001
6002 *el = cur->el_next;
6003 vim_free(cur);
6004 }
6005}
6006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007/*
6008 * Create a new scope and set up the generic items.
6009 */
6010 static scope_T *
6011new_scope(cctx_T *cctx, scopetype_T type)
6012{
6013 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6014
6015 if (scope == NULL)
6016 return NULL;
6017 scope->se_outer = cctx->ctx_scope;
6018 cctx->ctx_scope = scope;
6019 scope->se_type = type;
6020 scope->se_local_count = cctx->ctx_locals.ga_len;
6021 return scope;
6022}
6023
6024/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006025 * Free the current scope and go back to the outer scope.
6026 */
6027 static void
6028drop_scope(cctx_T *cctx)
6029{
6030 scope_T *scope = cctx->ctx_scope;
6031
6032 if (scope == NULL)
6033 {
6034 iemsg("calling drop_scope() without a scope");
6035 return;
6036 }
6037 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006038 switch (scope->se_type)
6039 {
6040 case IF_SCOPE:
6041 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6042 case FOR_SCOPE:
6043 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6044 case WHILE_SCOPE:
6045 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6046 case TRY_SCOPE:
6047 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6048 case NO_SCOPE:
6049 case BLOCK_SCOPE:
6050 break;
6051 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006052 vim_free(scope);
6053}
6054
6055/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056 * compile "if expr"
6057 *
6058 * "if expr" Produces instructions:
6059 * EVAL expr Push result of "expr"
6060 * JUMP_IF_FALSE end
6061 * ... body ...
6062 * end:
6063 *
6064 * "if expr | else" Produces instructions:
6065 * EVAL expr Push result of "expr"
6066 * JUMP_IF_FALSE else
6067 * ... body ...
6068 * JUMP_ALWAYS end
6069 * else:
6070 * ... body ...
6071 * end:
6072 *
6073 * "if expr1 | elseif expr2 | else" Produces instructions:
6074 * EVAL expr Push result of "expr"
6075 * JUMP_IF_FALSE elseif
6076 * ... body ...
6077 * JUMP_ALWAYS end
6078 * elseif:
6079 * EVAL expr Push result of "expr"
6080 * JUMP_IF_FALSE else
6081 * ... body ...
6082 * JUMP_ALWAYS end
6083 * else:
6084 * ... body ...
6085 * end:
6086 */
6087 static char_u *
6088compile_if(char_u *arg, cctx_T *cctx)
6089{
6090 char_u *p = arg;
6091 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006092 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006093 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006094 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006095 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096
Bram Moolenaara5565e42020-05-09 15:44:01 +02006097 CLEAR_FIELD(ppconst);
6098 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006099 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006100 clear_ppconst(&ppconst);
6101 return NULL;
6102 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006103 if (cctx->ctx_skip == SKIP_YES)
6104 clear_ppconst(&ppconst);
6105 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006106 {
6107 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006108 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006109 clear_ppconst(&ppconst);
6110 }
6111 else
6112 {
6113 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006114 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006115 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006116 return NULL;
6117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118
6119 scope = new_scope(cctx, IF_SCOPE);
6120 if (scope == NULL)
6121 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006122 scope->se_skip_save = skip_save;
6123 // "is_had_return" will be reset if any block does not end in :return
6124 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006126 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006127 {
6128 // "where" is set when ":elseif", "else" or ":endif" is found
6129 scope->se_u.se_if.is_if_label = instr->ga_len;
6130 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6131 }
6132 else
6133 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134
6135 return p;
6136}
6137
6138 static char_u *
6139compile_elseif(char_u *arg, cctx_T *cctx)
6140{
6141 char_u *p = arg;
6142 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006143 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144 isn_T *isn;
6145 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006146 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006147
6148 if (scope == NULL || scope->se_type != IF_SCOPE)
6149 {
6150 emsg(_(e_elseif_without_if));
6151 return NULL;
6152 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006153 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006154 if (!cctx->ctx_had_return)
6155 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006157 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006158 {
6159 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006160 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006161 return NULL;
6162 // previous "if" or "elseif" jumps here
6163 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6164 isn->isn_arg.jump.jump_where = instr->ga_len;
6165 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006166
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006167 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006168 CLEAR_FIELD(ppconst);
6169 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006170 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006171 clear_ppconst(&ppconst);
6172 return NULL;
6173 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006174 if (scope->se_skip_save == SKIP_YES)
6175 clear_ppconst(&ppconst);
6176 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006177 {
6178 // The expression results in a constant.
6179 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006180 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006181 clear_ppconst(&ppconst);
6182 scope->se_u.se_if.is_if_label = -1;
6183 }
6184 else
6185 {
6186 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006187 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006188 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006189 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006190
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006191 // "where" is set when ":elseif", "else" or ":endif" is found
6192 scope->se_u.se_if.is_if_label = instr->ga_len;
6193 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195
6196 return p;
6197}
6198
6199 static char_u *
6200compile_else(char_u *arg, cctx_T *cctx)
6201{
6202 char_u *p = arg;
6203 garray_T *instr = &cctx->ctx_instr;
6204 isn_T *isn;
6205 scope_T *scope = cctx->ctx_scope;
6206
6207 if (scope == NULL || scope->se_type != IF_SCOPE)
6208 {
6209 emsg(_(e_else_without_if));
6210 return NULL;
6211 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006212 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006213 if (!cctx->ctx_had_return)
6214 scope->se_u.se_if.is_had_return = FALSE;
6215 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216
Bram Moolenaarefd88552020-06-18 20:50:10 +02006217 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006218 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006219 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006220 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006221 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006222 if (!cctx->ctx_had_return
6223 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6224 JUMP_ALWAYS, cctx) == FAIL)
6225 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006226 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006227
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006228 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006229 {
6230 if (scope->se_u.se_if.is_if_label >= 0)
6231 {
6232 // previous "if" or "elseif" jumps here
6233 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6234 isn->isn_arg.jump.jump_where = instr->ga_len;
6235 scope->se_u.se_if.is_if_label = -1;
6236 }
6237 }
6238
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006239 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006240 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242
6243 return p;
6244}
6245
6246 static char_u *
6247compile_endif(char_u *arg, cctx_T *cctx)
6248{
6249 scope_T *scope = cctx->ctx_scope;
6250 ifscope_T *ifscope;
6251 garray_T *instr = &cctx->ctx_instr;
6252 isn_T *isn;
6253
6254 if (scope == NULL || scope->se_type != IF_SCOPE)
6255 {
6256 emsg(_(e_endif_without_if));
6257 return NULL;
6258 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006259 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006260 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006261 if (!cctx->ctx_had_return)
6262 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006264 if (scope->se_u.se_if.is_if_label >= 0)
6265 {
6266 // previous "if" or "elseif" jumps here
6267 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6268 isn->isn_arg.jump.jump_where = instr->ga_len;
6269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270 // Fill in the "end" label in jumps at the end of the blocks.
6271 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006272 cctx->ctx_skip = scope->se_skip_save;
6273
6274 // If all the blocks end in :return and there is an :else then the
6275 // had_return flag is set.
6276 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006278 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279 return arg;
6280}
6281
6282/*
6283 * compile "for var in expr"
6284 *
6285 * Produces instructions:
6286 * PUSHNR -1
6287 * STORE loop-idx Set index to -1
6288 * EVAL expr Push result of "expr"
6289 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6290 * - if beyond end, jump to "end"
6291 * - otherwise get item from list and push it
6292 * STORE var Store item in "var"
6293 * ... body ...
6294 * JUMP top Jump back to repeat
6295 * end: DROP Drop the result of "expr"
6296 *
6297 */
6298 static char_u *
6299compile_for(char_u *arg, cctx_T *cctx)
6300{
6301 char_u *p;
6302 size_t varlen;
6303 garray_T *instr = &cctx->ctx_instr;
6304 garray_T *stack = &cctx->ctx_type_stack;
6305 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006306 lvar_T *loop_lvar; // loop iteration variable
6307 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 type_T *vartype;
6309
6310 // TODO: list of variables: "for [key, value] in dict"
6311 // parse "var"
6312 for (p = arg; eval_isnamec1(*p); ++p)
6313 ;
6314 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006315 var_lvar = lookup_local(arg, varlen, cctx);
6316 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317 {
6318 semsg(_("E1023: variable already defined: %s"), arg);
6319 return NULL;
6320 }
6321
6322 // consume "in"
6323 p = skipwhite(p);
6324 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6325 {
6326 emsg(_(e_missing_in));
6327 return NULL;
6328 }
6329 p = skipwhite(p + 2);
6330
6331
6332 scope = new_scope(cctx, FOR_SCOPE);
6333 if (scope == NULL)
6334 return NULL;
6335
6336 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006337 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6338 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006339 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006340 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006341 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006342 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006343 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344
6345 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006346 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6347 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006348 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006349 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006350 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006351 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006354 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355
6356 // compile "expr", it remains on the stack until "endfor"
6357 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006358 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006359 {
6360 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006363
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006364 // Now that we know the type of "var", check that it is a list, now or at
6365 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006367 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006368 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006369 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006370 return NULL;
6371 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006372 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006373 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006374
6375 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006376 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006377
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006378 generate_FOR(cctx, loop_lvar->lv_idx);
6379 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380
6381 return arg;
6382}
6383
6384/*
6385 * compile "endfor"
6386 */
6387 static char_u *
6388compile_endfor(char_u *arg, cctx_T *cctx)
6389{
6390 garray_T *instr = &cctx->ctx_instr;
6391 scope_T *scope = cctx->ctx_scope;
6392 forscope_T *forscope;
6393 isn_T *isn;
6394
6395 if (scope == NULL || scope->se_type != FOR_SCOPE)
6396 {
6397 emsg(_(e_for));
6398 return NULL;
6399 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006400 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006402 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403
6404 // At end of ":for" scope jump back to the FOR instruction.
6405 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6406
6407 // Fill in the "end" label in the FOR statement so it can jump here
6408 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6409 isn->isn_arg.forloop.for_end = instr->ga_len;
6410
6411 // Fill in the "end" label any BREAK statements
6412 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6413
6414 // Below the ":for" scope drop the "expr" list from the stack.
6415 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6416 return NULL;
6417
6418 vim_free(scope);
6419
6420 return arg;
6421}
6422
6423/*
6424 * compile "while expr"
6425 *
6426 * Produces instructions:
6427 * top: EVAL expr Push result of "expr"
6428 * JUMP_IF_FALSE end jump if false
6429 * ... body ...
6430 * JUMP top Jump back to repeat
6431 * end:
6432 *
6433 */
6434 static char_u *
6435compile_while(char_u *arg, cctx_T *cctx)
6436{
6437 char_u *p = arg;
6438 garray_T *instr = &cctx->ctx_instr;
6439 scope_T *scope;
6440
6441 scope = new_scope(cctx, WHILE_SCOPE);
6442 if (scope == NULL)
6443 return NULL;
6444
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006445 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446
6447 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006448 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449 return NULL;
6450
6451 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006452 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006453 JUMP_IF_FALSE, cctx) == FAIL)
6454 return FAIL;
6455
6456 return p;
6457}
6458
6459/*
6460 * compile "endwhile"
6461 */
6462 static char_u *
6463compile_endwhile(char_u *arg, cctx_T *cctx)
6464{
6465 scope_T *scope = cctx->ctx_scope;
6466
6467 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6468 {
6469 emsg(_(e_while));
6470 return NULL;
6471 }
6472 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006473 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006474
6475 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006476 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477
6478 // Fill in the "end" label in the WHILE statement so it can jump here.
6479 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006480 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006481
6482 vim_free(scope);
6483
6484 return arg;
6485}
6486
6487/*
6488 * compile "continue"
6489 */
6490 static char_u *
6491compile_continue(char_u *arg, cctx_T *cctx)
6492{
6493 scope_T *scope = cctx->ctx_scope;
6494
6495 for (;;)
6496 {
6497 if (scope == NULL)
6498 {
6499 emsg(_(e_continue));
6500 return NULL;
6501 }
6502 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6503 break;
6504 scope = scope->se_outer;
6505 }
6506
6507 // Jump back to the FOR or WHILE instruction.
6508 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006509 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6510 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511 return arg;
6512}
6513
6514/*
6515 * compile "break"
6516 */
6517 static char_u *
6518compile_break(char_u *arg, cctx_T *cctx)
6519{
6520 scope_T *scope = cctx->ctx_scope;
6521 endlabel_T **el;
6522
6523 for (;;)
6524 {
6525 if (scope == NULL)
6526 {
6527 emsg(_(e_break));
6528 return NULL;
6529 }
6530 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6531 break;
6532 scope = scope->se_outer;
6533 }
6534
6535 // Jump to the end of the FOR or WHILE loop.
6536 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006537 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006539 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6541 return FAIL;
6542
6543 return arg;
6544}
6545
6546/*
6547 * compile "{" start of block
6548 */
6549 static char_u *
6550compile_block(char_u *arg, cctx_T *cctx)
6551{
6552 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6553 return NULL;
6554 return skipwhite(arg + 1);
6555}
6556
6557/*
6558 * compile end of block: drop one scope
6559 */
6560 static void
6561compile_endblock(cctx_T *cctx)
6562{
6563 scope_T *scope = cctx->ctx_scope;
6564
6565 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006566 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006567 vim_free(scope);
6568}
6569
6570/*
6571 * compile "try"
6572 * Creates a new scope for the try-endtry, pointing to the first catch and
6573 * finally.
6574 * Creates another scope for the "try" block itself.
6575 * TRY instruction sets up exception handling at runtime.
6576 *
6577 * "try"
6578 * TRY -> catch1, -> finally push trystack entry
6579 * ... try block
6580 * "throw {exception}"
6581 * EVAL {exception}
6582 * THROW create exception
6583 * ... try block
6584 * " catch {expr}"
6585 * JUMP -> finally
6586 * catch1: PUSH exeception
6587 * EVAL {expr}
6588 * MATCH
6589 * JUMP nomatch -> catch2
6590 * CATCH remove exception
6591 * ... catch block
6592 * " catch"
6593 * JUMP -> finally
6594 * catch2: CATCH remove exception
6595 * ... catch block
6596 * " finally"
6597 * finally:
6598 * ... finally block
6599 * " endtry"
6600 * ENDTRY pop trystack entry, may rethrow
6601 */
6602 static char_u *
6603compile_try(char_u *arg, cctx_T *cctx)
6604{
6605 garray_T *instr = &cctx->ctx_instr;
6606 scope_T *try_scope;
6607 scope_T *scope;
6608
6609 // scope that holds the jumps that go to catch/finally/endtry
6610 try_scope = new_scope(cctx, TRY_SCOPE);
6611 if (try_scope == NULL)
6612 return NULL;
6613
6614 // "catch" is set when the first ":catch" is found.
6615 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006616 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 if (generate_instr(cctx, ISN_TRY) == NULL)
6618 return NULL;
6619
6620 // scope for the try block itself
6621 scope = new_scope(cctx, BLOCK_SCOPE);
6622 if (scope == NULL)
6623 return NULL;
6624
6625 return arg;
6626}
6627
6628/*
6629 * compile "catch {expr}"
6630 */
6631 static char_u *
6632compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6633{
6634 scope_T *scope = cctx->ctx_scope;
6635 garray_T *instr = &cctx->ctx_instr;
6636 char_u *p;
6637 isn_T *isn;
6638
6639 // end block scope from :try or :catch
6640 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6641 compile_endblock(cctx);
6642 scope = cctx->ctx_scope;
6643
6644 // Error if not in a :try scope
6645 if (scope == NULL || scope->se_type != TRY_SCOPE)
6646 {
6647 emsg(_(e_catch));
6648 return NULL;
6649 }
6650
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006651 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006652 {
6653 emsg(_("E1033: catch unreachable after catch-all"));
6654 return NULL;
6655 }
6656
6657 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006658 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659 JUMP_ALWAYS, cctx) == FAIL)
6660 return NULL;
6661
6662 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006663 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 if (isn->isn_arg.try.try_catch == 0)
6665 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006666 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006667 {
6668 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006669 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006670 isn->isn_arg.jump.jump_where = instr->ga_len;
6671 }
6672
6673 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006674 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006676 scope->se_u.se_try.ts_caught_all = TRUE;
6677 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 }
6679 else
6680 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006681 char_u *end;
6682 char_u *pat;
6683 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006684 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006685 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006686
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687 // Push v:exception, push {expr} and MATCH
6688 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6689
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006690 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006691 if (*end != *p)
6692 {
6693 semsg(_("E1067: Separator mismatch: %s"), p);
6694 vim_free(tofree);
6695 return FAIL;
6696 }
6697 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006698 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006699 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006700 len = (int)(end - tofree);
6701 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006702 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006703 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006704 if (pat == NULL)
6705 return FAIL;
6706 if (generate_PUSHS(cctx, pat) == FAIL)
6707 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6710 return NULL;
6711
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006712 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6714 return NULL;
6715 }
6716
6717 if (generate_instr(cctx, ISN_CATCH) == NULL)
6718 return NULL;
6719
6720 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6721 return NULL;
6722 return p;
6723}
6724
6725 static char_u *
6726compile_finally(char_u *arg, cctx_T *cctx)
6727{
6728 scope_T *scope = cctx->ctx_scope;
6729 garray_T *instr = &cctx->ctx_instr;
6730 isn_T *isn;
6731
6732 // end block scope from :try or :catch
6733 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6734 compile_endblock(cctx);
6735 scope = cctx->ctx_scope;
6736
6737 // Error if not in a :try scope
6738 if (scope == NULL || scope->se_type != TRY_SCOPE)
6739 {
6740 emsg(_(e_finally));
6741 return NULL;
6742 }
6743
6744 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006745 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746 if (isn->isn_arg.try.try_finally != 0)
6747 {
6748 emsg(_(e_finally_dup));
6749 return NULL;
6750 }
6751
6752 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006753 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754
Bram Moolenaar585fea72020-04-02 22:33:21 +02006755 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006756 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006757 {
6758 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006759 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006761 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 }
6763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 // TODO: set index in ts_finally_label jumps
6765
6766 return arg;
6767}
6768
6769 static char_u *
6770compile_endtry(char_u *arg, cctx_T *cctx)
6771{
6772 scope_T *scope = cctx->ctx_scope;
6773 garray_T *instr = &cctx->ctx_instr;
6774 isn_T *isn;
6775
6776 // end block scope from :catch or :finally
6777 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6778 compile_endblock(cctx);
6779 scope = cctx->ctx_scope;
6780
6781 // Error if not in a :try scope
6782 if (scope == NULL || scope->se_type != TRY_SCOPE)
6783 {
6784 if (scope == NULL)
6785 emsg(_(e_no_endtry));
6786 else if (scope->se_type == WHILE_SCOPE)
6787 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006788 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789 emsg(_(e_endfor));
6790 else
6791 emsg(_(e_endif));
6792 return NULL;
6793 }
6794
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006795 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006796 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6797 {
6798 emsg(_("E1032: missing :catch or :finally"));
6799 return NULL;
6800 }
6801
6802 // Fill in the "end" label in jumps at the end of the blocks, if not done
6803 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006804 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805
6806 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006807 if (isn->isn_arg.try.try_catch == 0)
6808 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809 if (isn->isn_arg.try.try_finally == 0)
6810 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006811
6812 if (scope->se_u.se_try.ts_catch_label != 0)
6813 {
6814 // Last catch without match jumps here
6815 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6816 isn->isn_arg.jump.jump_where = instr->ga_len;
6817 }
6818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819 compile_endblock(cctx);
6820
6821 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6822 return NULL;
6823 return arg;
6824}
6825
6826/*
6827 * compile "throw {expr}"
6828 */
6829 static char_u *
6830compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6831{
6832 char_u *p = skipwhite(arg);
6833
Bram Moolenaara5565e42020-05-09 15:44:01 +02006834 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006835 return NULL;
6836 if (may_generate_2STRING(-1, cctx) == FAIL)
6837 return NULL;
6838 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6839 return NULL;
6840
6841 return p;
6842}
6843
6844/*
6845 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006846 * compile "echomsg expr"
6847 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006848 * compile "execute expr"
6849 */
6850 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006851compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006852{
6853 char_u *p = arg;
6854 int count = 0;
6855
6856 for (;;)
6857 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006858 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006859 return NULL;
6860 ++count;
6861 p = skipwhite(p);
6862 if (ends_excmd(*p))
6863 break;
6864 }
6865
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006866 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6867 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6868 else if (cmdidx == CMD_execute)
6869 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6870 else if (cmdidx == CMD_echomsg)
6871 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6872 else
6873 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874 return p;
6875}
6876
6877/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006878 * A command that is not compiled, execute with legacy code.
6879 */
6880 static char_u *
6881compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6882{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006883 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006884 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006885 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006886
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006887 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006888 goto theend;
6889
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006890 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006891 {
6892 long argt = excmd_get_argt(eap->cmdidx);
6893 int usefilter = FALSE;
6894
6895 has_expr = argt & (EX_XFILE | EX_EXPAND);
6896
6897 // If the command can be followed by a bar, find the bar and truncate
6898 // it, so that the following command can be compiled.
6899 // The '|' is overwritten with a NUL, it is put back below.
6900 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6901 && *eap->arg == '!')
6902 // :w !filter or :r !filter or :r! filter
6903 usefilter = TRUE;
6904 if ((argt & EX_TRLBAR) && !usefilter)
6905 {
6906 separate_nextcmd(eap);
6907 if (eap->nextcmd != NULL)
6908 nextcmd = eap->nextcmd;
6909 }
6910 }
6911
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006912 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6913 {
6914 // expand filename in "syntax include [@group] filename"
6915 has_expr = TRUE;
6916 eap->arg = skipwhite(eap->arg + 7);
6917 if (*eap->arg == '@')
6918 eap->arg = skiptowhite(eap->arg);
6919 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006920
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006921 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006922 {
6923 int count = 0;
6924 char_u *start = skipwhite(line);
6925
6926 // :cmd xxx`=expr1`yyy`=expr2`zzz
6927 // PUSHS ":cmd xxx"
6928 // eval expr1
6929 // PUSHS "yyy"
6930 // eval expr2
6931 // PUSHS "zzz"
6932 // EXECCONCAT 5
6933 for (;;)
6934 {
6935 if (p > start)
6936 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006937 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006938 ++count;
6939 }
6940 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006941 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006942 return NULL;
6943 may_generate_2STRING(-1, cctx);
6944 ++count;
6945 p = skipwhite(p);
6946 if (*p != '`')
6947 {
6948 emsg(_("E1083: missing backtick"));
6949 return NULL;
6950 }
6951 start = p + 1;
6952
6953 p = (char_u *)strstr((char *)start, "`=");
6954 if (p == NULL)
6955 {
6956 if (*skipwhite(start) != NUL)
6957 {
6958 generate_PUSHS(cctx, vim_strsave(start));
6959 ++count;
6960 }
6961 break;
6962 }
6963 }
6964 generate_EXECCONCAT(cctx, count);
6965 }
6966 else
6967 generate_EXEC(cctx, line);
6968
6969theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006970 if (*nextcmd != NUL)
6971 {
6972 // the parser expects a pointer to the bar, put it back
6973 --nextcmd;
6974 *nextcmd = '|';
6975 }
6976
6977 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006978}
6979
6980/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006981 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006982 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006983 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006984 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006985add_def_function(ufunc_T *ufunc)
6986{
6987 dfunc_T *dfunc;
6988
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006989 if (def_functions.ga_len == 0)
6990 {
6991 // The first position is not used, so that a zero uf_dfunc_idx means it
6992 // wasn't set.
6993 if (ga_grow(&def_functions, 1) == FAIL)
6994 return FAIL;
6995 ++def_functions.ga_len;
6996 }
6997
Bram Moolenaar09689a02020-05-09 22:50:08 +02006998 // Add the function to "def_functions".
6999 if (ga_grow(&def_functions, 1) == FAIL)
7000 return FAIL;
7001 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7002 CLEAR_POINTER(dfunc);
7003 dfunc->df_idx = def_functions.ga_len;
7004 ufunc->uf_dfunc_idx = dfunc->df_idx;
7005 dfunc->df_ufunc = ufunc;
7006 ++def_functions.ga_len;
7007 return OK;
7008}
7009
7010/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 * After ex_function() has collected all the function lines: parse and compile
7012 * the lines into instructions.
7013 * Adds the function to "def_functions".
7014 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7015 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007016 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007017 * This can be used recursively through compile_lambda(), which may reallocate
7018 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007019 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007021 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007022compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024 char_u *line = NULL;
7025 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 cctx_T cctx;
7028 garray_T *instr;
7029 int called_emsg_before = called_emsg;
7030 int ret = FAIL;
7031 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007032 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007033 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007034 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007036 // When using a function that was compiled before: Free old instructions.
7037 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007038 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007039 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007040 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7041 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007042 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007044 else
7045 {
7046 if (add_def_function(ufunc) == FAIL)
7047 return FAIL;
7048 new_def_function = TRUE;
7049 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050
Bram Moolenaar985116a2020-07-12 17:31:09 +02007051 ufunc->uf_def_status = UF_COMPILING;
7052
Bram Moolenaara80faa82020-04-12 19:37:17 +02007053 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 cctx.ctx_ufunc = ufunc;
7055 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007056 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7058 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7059 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7060 cctx.ctx_type_list = &ufunc->uf_type_list;
7061 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7062 instr = &cctx.ctx_instr;
7063
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007064 // Set the context to the function, it may be compiled when called from
7065 // another script. Set the script version to the most modern one.
7066 // The line number will be set in next_line_from_context().
7067 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7069
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007070 // Make sure error messages are OK.
7071 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7072 if (do_estack_push)
7073 estack_push_ufunc(ufunc, 1);
7074
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007075 if (ufunc->uf_def_args.ga_len > 0)
7076 {
7077 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007078 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007079 int i;
7080 char_u *arg;
7081 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007082 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007083
7084 // Produce instructions for the default values of optional arguments.
7085 // Store the instruction index in uf_def_arg_idx[] so that we know
7086 // where to start when the function is called, depending on the number
7087 // of arguments.
7088 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7089 if (ufunc->uf_def_arg_idx == NULL)
7090 goto erret;
7091 for (i = 0; i < count; ++i)
7092 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007093 garray_T *stack = &cctx.ctx_type_stack;
7094 type_T *val_type;
7095 int arg_idx = first_def_arg + i;
7096
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007097 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7098 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007099 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007100 goto erret;
7101
7102 // If no type specified use the type of the default value.
7103 // Otherwise check that the default value type matches the
7104 // specified type.
7105 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7106 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007107 {
7108 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007109 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007110 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02007111 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007112 == FAIL)
7113 {
7114 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
7115 arg_idx + 1);
7116 goto erret;
7117 }
7118
7119 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007120 goto erret;
7121 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007122 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007123
7124 if (did_set_arg_type)
7125 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007126 }
7127
7128 /*
7129 * Loop over all the lines of the function and generate instructions.
7130 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131 for (;;)
7132 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007133 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007134 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007135 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007136 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007137
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007138 // Bail out on the first error to avoid a flood of errors and report
7139 // the right line number when inside try/catch.
7140 if (emsg_before != called_emsg)
7141 goto erret;
7142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 if (line != NULL && *line == '|')
7144 // the line continues after a '|'
7145 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007146 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007147 && !(*line == '#' && (line == cctx.ctx_line_start
7148 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007150 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 goto erret;
7152 }
7153 else
7154 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007155 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007156 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007157 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007160 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007161
Bram Moolenaara80faa82020-04-12 19:37:17 +02007162 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 ea.cmdlinep = &line;
7164 ea.cmd = skipwhite(line);
7165
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007166 // Some things can be recognized by the first character.
7167 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007169 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007170 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007171 if (ea.cmd[1] != '{')
7172 {
7173 line = (char_u *)"";
7174 continue;
7175 }
7176 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007178 case '}':
7179 {
7180 // "}" ends a block scope
7181 scopetype_T stype = cctx.ctx_scope == NULL
7182 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007184 if (stype == BLOCK_SCOPE)
7185 {
7186 compile_endblock(&cctx);
7187 line = ea.cmd;
7188 }
7189 else
7190 {
7191 emsg(_("E1025: using } outside of a block scope"));
7192 goto erret;
7193 }
7194 if (line != NULL)
7195 line = skipwhite(ea.cmd + 1);
7196 continue;
7197 }
7198
7199 case '{':
7200 // "{" starts a block scope
7201 // "{'a': 1}->func() is something else
7202 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7203 {
7204 line = compile_block(ea.cmd, &cctx);
7205 continue;
7206 }
7207 break;
7208
7209 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007210 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007211 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007212 }
7213
7214 /*
7215 * COMMAND MODIFIERS
7216 */
7217 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7218 {
7219 if (errormsg != NULL)
7220 goto erret;
7221 // empty line or comment
7222 line = (char_u *)"";
7223 continue;
7224 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007225 // TODO: use modifiers in the command
7226 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007227 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228
7229 // Skip ":call" to get to the function name.
7230 if (checkforcmd(&ea.cmd, "call", 3))
7231 ea.cmd = skipwhite(ea.cmd);
7232
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007233 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007235 char_u *pskip;
7236
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007237 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007238 // find what follows.
7239 // Skip over "var.member", "var[idx]" and the like.
7240 // Also "&opt = val", "$ENV = val" and "@r = val".
7241 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007242 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007243 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007244 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007245 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007246 char_u *var_end;
7247 int oplen;
7248 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007250 var_end = find_name_end(pskip, NULL, NULL,
7251 FNE_CHECK_START | FNE_INCL_BR);
7252 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007253 if (oplen > 0)
7254 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007255 size_t len = p - ea.cmd;
7256
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007257 // Recognize an assignment if we recognize the variable
7258 // name:
7259 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007260 // "local = expr" where "local" is a local var.
7261 // "script = expr" where "script" is a script-local var.
7262 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007263 // "&opt = expr"
7264 // "$ENV = expr"
7265 // "@r = expr"
7266 if (*ea.cmd == '&'
7267 || *ea.cmd == '$'
7268 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007269 || ((len) > 2 && ea.cmd[1] == ':')
7270 || lookup_local(ea.cmd, len, &cctx) != NULL
7271 || lookup_arg(ea.cmd, len, NULL, NULL,
7272 NULL, &cctx) == OK
7273 || lookup_script(ea.cmd, len) == OK
7274 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007275 {
7276 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007277 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007278 goto erret;
7279 continue;
7280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 }
7282 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007283
7284 if (*ea.cmd == '[')
7285 {
7286 // [var, var] = expr
7287 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7288 if (line == NULL)
7289 goto erret;
7290 if (line != ea.cmd)
7291 continue;
7292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007293 }
7294
7295 /*
7296 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007297 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007299 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007300 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007301 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007302 ea.cmd = skip_range(ea.cmd, NULL);
7303 if (ea.cmd > cmd && !starts_with_colon)
7304 {
7305 emsg(_(e_colon_required));
7306 goto erret;
7307 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007308 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007309 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007310 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007311 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312
7313 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7314 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007315 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007316 {
7317 line += STRLEN(line);
7318 continue;
7319 }
7320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007322 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007324 // CMD_let cannot happen, compile_assignment() above is used
7325 iemsg("Command from find_ex_command() not handled");
7326 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328 }
7329
7330 p = skipwhite(p);
7331
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007332 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007333 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007334 && ea.cmdidx != CMD_elseif
7335 && ea.cmdidx != CMD_else
7336 && ea.cmdidx != CMD_endif)
7337 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007338 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007339 continue;
7340 }
7341
Bram Moolenaarefd88552020-06-18 20:50:10 +02007342 if (ea.cmdidx != CMD_elseif
7343 && ea.cmdidx != CMD_else
7344 && ea.cmdidx != CMD_endif
7345 && ea.cmdidx != CMD_endfor
7346 && ea.cmdidx != CMD_endwhile
7347 && ea.cmdidx != CMD_catch
7348 && ea.cmdidx != CMD_finally
7349 && ea.cmdidx != CMD_endtry)
7350 {
7351 if (cctx.ctx_had_return)
7352 {
7353 emsg(_("E1095: Unreachable code after :return"));
7354 goto erret;
7355 }
7356 }
7357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007358 switch (ea.cmdidx)
7359 {
7360 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007361 ea.arg = p;
7362 line = compile_nested_function(&ea, &cctx);
7363 break;
7364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007366 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 goto erret;
7368
7369 case CMD_return:
7370 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007371 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 break;
7373
7374 case CMD_let:
7375 case CMD_const:
7376 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007377 if (line == p)
7378 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 break;
7380
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007381 case CMD_unlet:
7382 case CMD_unlockvar:
7383 case CMD_lockvar:
7384 line = compile_unletlock(p, &ea, &cctx);
7385 break;
7386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 case CMD_import:
7388 line = compile_import(p, &cctx);
7389 break;
7390
7391 case CMD_if:
7392 line = compile_if(p, &cctx);
7393 break;
7394 case CMD_elseif:
7395 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007396 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 break;
7398 case CMD_else:
7399 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007400 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 break;
7402 case CMD_endif:
7403 line = compile_endif(p, &cctx);
7404 break;
7405
7406 case CMD_while:
7407 line = compile_while(p, &cctx);
7408 break;
7409 case CMD_endwhile:
7410 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007411 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 break;
7413
7414 case CMD_for:
7415 line = compile_for(p, &cctx);
7416 break;
7417 case CMD_endfor:
7418 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007419 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 break;
7421 case CMD_continue:
7422 line = compile_continue(p, &cctx);
7423 break;
7424 case CMD_break:
7425 line = compile_break(p, &cctx);
7426 break;
7427
7428 case CMD_try:
7429 line = compile_try(p, &cctx);
7430 break;
7431 case CMD_catch:
7432 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007433 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434 break;
7435 case CMD_finally:
7436 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007437 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007438 break;
7439 case CMD_endtry:
7440 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007441 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007442 break;
7443 case CMD_throw:
7444 line = compile_throw(p, &cctx);
7445 break;
7446
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007447 case CMD_eval:
7448 if (compile_expr0(&p, &cctx) == FAIL)
7449 goto erret;
7450
7451 // drop the return value
7452 generate_instr_drop(&cctx, ISN_DROP, 1);
7453
7454 line = skipwhite(p);
7455 break;
7456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007459 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007460 case CMD_echomsg:
7461 case CMD_echoerr:
7462 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007463 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007465 // TODO: other commands with an expression argument
7466
Bram Moolenaarae616492020-07-28 20:07:27 +02007467 case CMD_append:
7468 case CMD_change:
7469 case CMD_insert:
7470 case CMD_xit:
7471 not_in_vim9(&ea);
7472 goto erret;
7473
Bram Moolenaar002262f2020-07-08 17:47:57 +02007474 case CMD_SIZE:
7475 semsg(_("E476: Invalid command: %s"), ea.cmd);
7476 goto erret;
7477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007479 // Not recognized, execute with do_cmdline_cmd().
7480 ea.arg = p;
7481 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007482 break;
7483 }
7484 if (line == NULL)
7485 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007486 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487
7488 if (cctx.ctx_type_stack.ga_len < 0)
7489 {
7490 iemsg("Type stack underflow");
7491 goto erret;
7492 }
7493 }
7494
7495 if (cctx.ctx_scope != NULL)
7496 {
7497 if (cctx.ctx_scope->se_type == IF_SCOPE)
7498 emsg(_(e_endif));
7499 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7500 emsg(_(e_endwhile));
7501 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7502 emsg(_(e_endfor));
7503 else
7504 emsg(_("E1026: Missing }"));
7505 goto erret;
7506 }
7507
Bram Moolenaarefd88552020-06-18 20:50:10 +02007508 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509 {
7510 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7511 {
7512 emsg(_("E1027: Missing return statement"));
7513 goto erret;
7514 }
7515
7516 // Return zero if there is no return at the end.
7517 generate_PUSHNR(&cctx, 0);
7518 generate_instr(&cctx, ISN_RETURN);
7519 }
7520
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007521 {
7522 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7523 + ufunc->uf_dfunc_idx;
7524 dfunc->df_deleted = FALSE;
7525 dfunc->df_instr = instr->ga_data;
7526 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007527 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007528 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007529 if (cctx.ctx_outer_used)
7530 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007531 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007533
7534 ret = OK;
7535
7536erret:
7537 if (ret == FAIL)
7538 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007539 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007540 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7541 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007542
7543 for (idx = 0; idx < instr->ga_len; ++idx)
7544 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007545 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007546
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007547 // If using the last entry in the table and it was added above, we
7548 // might as well remove it.
7549 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007550 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007551 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007552 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007553 ufunc->uf_dfunc_idx = 0;
7554 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007555 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007556
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007557 while (cctx.ctx_scope != NULL)
7558 drop_scope(&cctx);
7559
Bram Moolenaar20431c92020-03-20 18:39:46 +01007560 // Don't execute this function body.
7561 ga_clear_strings(&ufunc->uf_lines);
7562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563 if (errormsg != NULL)
7564 emsg(errormsg);
7565 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007566 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567 }
7568
7569 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007570 if (do_estack_push)
7571 estack_pop();
7572
Bram Moolenaar20431c92020-03-20 18:39:46 +01007573 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007574 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007575 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007576 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007577}
7578
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007579 void
7580set_function_type(ufunc_T *ufunc)
7581{
7582 int varargs = ufunc->uf_va_name != NULL;
7583 int argcount = ufunc->uf_args.ga_len;
7584
7585 // Create a type for the function, with the return type and any
7586 // argument types.
7587 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7588 // The type is included in "tt_args".
7589 if (argcount > 0 || varargs)
7590 {
7591 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7592 argcount, &ufunc->uf_type_list);
7593 // Add argument types to the function type.
7594 if (func_type_add_arg_types(ufunc->uf_func_type,
7595 argcount + varargs,
7596 &ufunc->uf_type_list) == FAIL)
7597 return;
7598 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7599 ufunc->uf_func_type->tt_min_argcount =
7600 argcount - ufunc->uf_def_args.ga_len;
7601 if (ufunc->uf_arg_types == NULL)
7602 {
7603 int i;
7604
7605 // lambda does not have argument types.
7606 for (i = 0; i < argcount; ++i)
7607 ufunc->uf_func_type->tt_args[i] = &t_any;
7608 }
7609 else
7610 mch_memmove(ufunc->uf_func_type->tt_args,
7611 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7612 if (varargs)
7613 {
7614 ufunc->uf_func_type->tt_args[argcount] =
7615 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7616 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7617 }
7618 }
7619 else
7620 // No arguments, can use a predefined type.
7621 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7622 argcount, &ufunc->uf_type_list);
7623}
7624
7625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626/*
7627 * Delete an instruction, free what it contains.
7628 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007629 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007630delete_instr(isn_T *isn)
7631{
7632 switch (isn->isn_type)
7633 {
7634 case ISN_EXEC:
7635 case ISN_LOADENV:
7636 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007637 case ISN_LOADB:
7638 case ISN_LOADW:
7639 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007641 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 case ISN_PUSHEXC:
7643 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007644 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007646 case ISN_STOREB:
7647 case ISN_STOREW:
7648 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007649 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007650 vim_free(isn->isn_arg.string);
7651 break;
7652
7653 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007654 case ISN_STORES:
7655 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007656 break;
7657
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007658 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007659 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007660 vim_free(isn->isn_arg.unlet.ul_name);
7661 break;
7662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007663 case ISN_STOREOPT:
7664 vim_free(isn->isn_arg.storeopt.so_name);
7665 break;
7666
7667 case ISN_PUSHBLOB: // push blob isn_arg.blob
7668 blob_unref(isn->isn_arg.blob);
7669 break;
7670
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007671 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007672#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007673 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007674#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007675 break;
7676
7677 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007678#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007679 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007680#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007681 break;
7682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683 case ISN_UCALL:
7684 vim_free(isn->isn_arg.ufunc.cuf_name);
7685 break;
7686
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007687 case ISN_FUNCREF:
7688 {
7689 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7690 + isn->isn_arg.funcref.fr_func;
7691 func_ptr_unref(dfunc->df_ufunc);
7692 }
7693 break;
7694
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007695 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007696 {
7697 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7698 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7699
7700 if (ufunc != NULL)
7701 {
7702 // Clear uf_dfunc_idx so that the function is deleted.
7703 clear_def_function(ufunc);
7704 ufunc->uf_dfunc_idx = 0;
7705 func_ptr_unref(ufunc);
7706 }
7707
7708 vim_free(lambda);
7709 vim_free(isn->isn_arg.newfunc.nf_global);
7710 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007711 break;
7712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713 case ISN_2BOOL:
7714 case ISN_2STRING:
7715 case ISN_ADDBLOB:
7716 case ISN_ADDLIST:
7717 case ISN_BCALL:
7718 case ISN_CATCH:
7719 case ISN_CHECKNR:
7720 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007721 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007722 case ISN_COMPAREANY:
7723 case ISN_COMPAREBLOB:
7724 case ISN_COMPAREBOOL:
7725 case ISN_COMPAREDICT:
7726 case ISN_COMPAREFLOAT:
7727 case ISN_COMPAREFUNC:
7728 case ISN_COMPARELIST:
7729 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007730 case ISN_COMPARESPECIAL:
7731 case ISN_COMPARESTRING:
7732 case ISN_CONCAT:
7733 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007734 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735 case ISN_DROP:
7736 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007737 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007738 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007739 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007740 case ISN_EXECCONCAT:
7741 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007742 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007743 case ISN_LISTINDEX:
7744 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007745 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007746 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007747 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007748 case ISN_JUMP:
7749 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007750 case ISN_LOADBDICT:
7751 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007752 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007754 case ISN_LOADSCRIPT:
7755 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007757 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758 case ISN_NEGATENR:
7759 case ISN_NEWDICT:
7760 case ISN_NEWLIST:
7761 case ISN_OPNR:
7762 case ISN_OPFLOAT:
7763 case ISN_OPANY:
7764 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007765 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007766 case ISN_PUSHF:
7767 case ISN_PUSHNR:
7768 case ISN_PUSHBOOL:
7769 case ISN_PUSHSPEC:
7770 case ISN_RETURN:
7771 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007772 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007773 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007774 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007775 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007776 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007777 case ISN_STOREDICT:
7778 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779 case ISN_THROW:
7780 case ISN_TRY:
7781 // nothing allocated
7782 break;
7783 }
7784}
7785
7786/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007787 * Free all instructions for "dfunc".
7788 */
7789 static void
7790delete_def_function_contents(dfunc_T *dfunc)
7791{
7792 int idx;
7793
7794 ga_clear(&dfunc->df_def_args_isn);
7795
7796 if (dfunc->df_instr != NULL)
7797 {
7798 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7799 delete_instr(dfunc->df_instr + idx);
7800 VIM_CLEAR(dfunc->df_instr);
7801 }
7802
7803 dfunc->df_deleted = TRUE;
7804}
7805
7806/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007807 * When a user function is deleted, clear the contents of any associated def
7808 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809 */
7810 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007811clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007812{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007813 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814 {
7815 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7816 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007817
Bram Moolenaar20431c92020-03-20 18:39:46 +01007818 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007819 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820 }
7821}
7822
7823#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007824/*
7825 * Free all functions defined with ":def".
7826 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007827 void
7828free_def_functions(void)
7829{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007830 int idx;
7831
7832 for (idx = 0; idx < def_functions.ga_len; ++idx)
7833 {
7834 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7835
7836 delete_def_function_contents(dfunc);
7837 }
7838
7839 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007840}
7841#endif
7842
7843
7844#endif // FEAT_EVAL