blob: 1ed3211463a0975d5f1438f0005fb3730335fd18 [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/*
1526 * Generate an ISN_JUMP instruction.
1527 */
1528 static int
1529generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1530{
1531 isn_T *isn;
1532 garray_T *stack = &cctx->ctx_type_stack;
1533
Bram Moolenaar080457c2020-03-03 21:53:32 +01001534 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1536 return FAIL;
1537 isn->isn_arg.jump.jump_when = when;
1538 isn->isn_arg.jump.jump_where = where;
1539
1540 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1541 --stack->ga_len;
1542
1543 return OK;
1544}
1545
1546 static int
1547generate_FOR(cctx_T *cctx, int loop_idx)
1548{
1549 isn_T *isn;
1550 garray_T *stack = &cctx->ctx_type_stack;
1551
Bram Moolenaar080457c2020-03-03 21:53:32 +01001552 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1554 return FAIL;
1555 isn->isn_arg.forloop.for_idx = loop_idx;
1556
1557 if (ga_grow(stack, 1) == FAIL)
1558 return FAIL;
1559 // type doesn't matter, will be stored next
1560 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1561 ++stack->ga_len;
1562
1563 return OK;
1564}
1565
1566/*
1567 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001568 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 * Return FAIL if the number of arguments is wrong.
1570 */
1571 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001572generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573{
1574 isn_T *isn;
1575 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001576 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001577 type_T *argtypes[MAX_FUNC_ARGS];
1578 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579
Bram Moolenaar080457c2020-03-03 21:53:32 +01001580 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001581 argoff = check_internal_func(func_idx, argcount);
1582 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 return FAIL;
1584
Bram Moolenaar389df252020-07-09 21:20:47 +02001585 if (method_call && argoff > 1)
1586 {
1587 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1588 return FAIL;
1589 isn->isn_arg.shuffle.shfl_item = argcount;
1590 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1591 }
1592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1594 return FAIL;
1595 isn->isn_arg.bfunc.cbf_idx = func_idx;
1596 isn->isn_arg.bfunc.cbf_argcount = argcount;
1597
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001598 for (i = 0; i < argcount; ++i)
1599 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 stack->ga_len -= argcount; // drop the arguments
1602 if (ga_grow(stack, 1) == FAIL)
1603 return FAIL;
1604 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001605 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 ++stack->ga_len; // add return value
1607
1608 return OK;
1609}
1610
1611/*
1612 * Generate an ISN_DCALL or ISN_UCALL instruction.
1613 * Return FAIL if the number of arguments is wrong.
1614 */
1615 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001616generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617{
1618 isn_T *isn;
1619 garray_T *stack = &cctx->ctx_type_stack;
1620 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001621 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622
Bram Moolenaar080457c2020-03-03 21:53:32 +01001623 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 if (argcount > regular_args && !has_varargs(ufunc))
1625 {
1626 semsg(_(e_toomanyarg), ufunc->uf_name);
1627 return FAIL;
1628 }
1629 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1630 {
1631 semsg(_(e_toofewarg), ufunc->uf_name);
1632 return FAIL;
1633 }
1634
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001635 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001636 {
1637 int i;
1638
1639 for (i = 0; i < argcount; ++i)
1640 {
1641 type_T *expected;
1642 type_T *actual;
1643
1644 if (i < regular_args)
1645 {
1646 if (ufunc->uf_arg_types == NULL)
1647 continue;
1648 expected = ufunc->uf_arg_types[i];
1649 }
1650 else
1651 expected = ufunc->uf_va_type->tt_member;
1652 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001653 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001654 {
1655 arg_type_mismatch(expected, actual, i + 1);
1656 return FAIL;
1657 }
1658 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001659 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001660 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001661 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001662 }
1663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001665 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001666 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001668 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 {
1670 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1671 isn->isn_arg.dfunc.cdf_argcount = argcount;
1672 }
1673 else
1674 {
1675 // A user function may be deleted and redefined later, can't use the
1676 // ufunc pointer, need to look it up again at runtime.
1677 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1678 isn->isn_arg.ufunc.cuf_argcount = argcount;
1679 }
1680
1681 stack->ga_len -= argcount; // drop the arguments
1682 if (ga_grow(stack, 1) == FAIL)
1683 return FAIL;
1684 // add return value
1685 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1686 ++stack->ga_len;
1687
1688 return OK;
1689}
1690
1691/*
1692 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1693 */
1694 static int
1695generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1696{
1697 isn_T *isn;
1698 garray_T *stack = &cctx->ctx_type_stack;
1699
Bram Moolenaar080457c2020-03-03 21:53:32 +01001700 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1702 return FAIL;
1703 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1704 isn->isn_arg.ufunc.cuf_argcount = argcount;
1705
1706 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001707 if (ga_grow(stack, 1) == FAIL)
1708 return FAIL;
1709 // add return value
1710 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1711 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712
1713 return OK;
1714}
1715
1716/*
1717 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001718 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 */
1720 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001721generate_PCALL(
1722 cctx_T *cctx,
1723 int argcount,
1724 char_u *name,
1725 type_T *type,
1726 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727{
1728 isn_T *isn;
1729 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001730 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731
Bram Moolenaar080457c2020-03-03 21:53:32 +01001732 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001733
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001734 if (type->tt_type == VAR_ANY)
1735 ret_type = &t_any;
1736 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001737 {
1738 if (type->tt_argcount != -1)
1739 {
1740 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1741
1742 if (argcount < type->tt_min_argcount - varargs)
1743 {
1744 semsg(_(e_toofewarg), "[reference]");
1745 return FAIL;
1746 }
1747 if (!varargs && argcount > type->tt_argcount)
1748 {
1749 semsg(_(e_toomanyarg), "[reference]");
1750 return FAIL;
1751 }
1752 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001753 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001754 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001755 else
1756 {
1757 semsg(_("E1085: Not a callable type: %s"), name);
1758 return FAIL;
1759 }
1760
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1762 return FAIL;
1763 isn->isn_arg.pfunc.cpf_top = at_top;
1764 isn->isn_arg.pfunc.cpf_argcount = argcount;
1765
1766 stack->ga_len -= argcount; // drop the arguments
1767
1768 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001769 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001771 // If partial is above the arguments it must be cleared and replaced with
1772 // the return value.
1773 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1774 return FAIL;
1775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 return OK;
1777}
1778
1779/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001780 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 */
1782 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001783generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784{
1785 isn_T *isn;
1786 garray_T *stack = &cctx->ctx_type_stack;
1787 type_T *type;
1788
Bram Moolenaar080457c2020-03-03 21:53:32 +01001789 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001790 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001792 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001794 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001796 if (type->tt_type != VAR_DICT && type != &t_any)
1797 {
1798 emsg(_(e_dictreq));
1799 return FAIL;
1800 }
1801 // change dict type to dict member type
1802 if (type->tt_type == VAR_DICT)
1803 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804
1805 return OK;
1806}
1807
1808/*
1809 * Generate an ISN_ECHO instruction.
1810 */
1811 static int
1812generate_ECHO(cctx_T *cctx, int with_white, int count)
1813{
1814 isn_T *isn;
1815
Bram Moolenaar080457c2020-03-03 21:53:32 +01001816 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1818 return FAIL;
1819 isn->isn_arg.echo.echo_with_white = with_white;
1820 isn->isn_arg.echo.echo_count = count;
1821
1822 return OK;
1823}
1824
Bram Moolenaarad39c092020-02-26 18:23:43 +01001825/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001826 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001827 */
1828 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001829generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001830{
1831 isn_T *isn;
1832
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001833 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001834 return FAIL;
1835 isn->isn_arg.number = count;
1836
1837 return OK;
1838}
1839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 static int
1841generate_EXEC(cctx_T *cctx, char_u *line)
1842{
1843 isn_T *isn;
1844
Bram Moolenaar080457c2020-03-03 21:53:32 +01001845 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1847 return FAIL;
1848 isn->isn_arg.string = vim_strsave(line);
1849 return OK;
1850}
1851
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001852 static int
1853generate_EXECCONCAT(cctx_T *cctx, int count)
1854{
1855 isn_T *isn;
1856
1857 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1858 return FAIL;
1859 isn->isn_arg.number = count;
1860 return OK;
1861}
1862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863/*
1864 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001865 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001867 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1869{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 lvar_T *lvar;
1871
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001872 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001874 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001875 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 }
1877
1878 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001879 return NULL;
1880 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001882 // Every local variable uses the next entry on the stack. We could re-use
1883 // the last ones when leaving a scope, but then variables used in a closure
1884 // might get overwritten. To keep things simple do not re-use stack
1885 // entries. This is less efficient, but memory is cheap these days.
1886 lvar->lv_idx = cctx->ctx_locals_count++;
1887
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001888 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 lvar->lv_const = isConst;
1890 lvar->lv_type = type;
1891
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001892 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893}
1894
1895/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001896 * Remove local variables above "new_top".
1897 */
1898 static void
1899unwind_locals(cctx_T *cctx, int new_top)
1900{
1901 if (cctx->ctx_locals.ga_len > new_top)
1902 {
1903 int idx;
1904 lvar_T *lvar;
1905
1906 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1907 {
1908 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1909 vim_free(lvar->lv_name);
1910 }
1911 }
1912 cctx->ctx_locals.ga_len = new_top;
1913}
1914
1915/*
1916 * Free all local variables.
1917 */
1918 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001919free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001920{
1921 unwind_locals(cctx, 0);
1922 ga_clear(&cctx->ctx_locals);
1923}
1924
1925/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 * Skip over a type definition and return a pointer to just after it.
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001927 * When "optional" is TRUE then a leading "?" is accepted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 */
1929 char_u *
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001930skip_type(char_u *start, int optional)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931{
1932 char_u *p = start;
1933
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001934 if (optional && *p == '?')
1935 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936 while (ASCII_ISALNUM(*p) || *p == '_')
1937 ++p;
1938
1939 // Skip over "<type>"; this is permissive about white space.
1940 if (*skipwhite(p) == '<')
1941 {
1942 p = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001943 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 p = skipwhite(p);
1945 if (*p == '>')
1946 ++p;
1947 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001948 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
1949 && STRNCMP("func", start, 4) == 0)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001950 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001951 if (*p == '(')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001952 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001953 // handle func(args): type
1954 ++p;
1955 while (*p != ')' && *p != NUL)
1956 {
1957 char_u *sp = p;
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001958
Bram Moolenaarace61322020-07-26 18:16:58 +02001959 if (STRNCMP(p, "...", 3) == 0)
1960 p += 3;
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001961 p = skip_type(p, TRUE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001962 if (p == sp)
1963 return p; // syntax error
1964 if (*p == ',')
1965 p = skipwhite(p + 1);
1966 }
1967 if (*p == ')')
1968 {
1969 if (p[1] == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001970 p = skip_type(skipwhite(p + 2), FALSE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001971 else
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001972 ++p;
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001973 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001974 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001975 else
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001976 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001977 // handle func: return_type
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001978 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001979 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001980 }
1981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 return p;
1983}
1984
1985/*
1986 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001987 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 * Returns NULL in case of failure.
1989 */
1990 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001991parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992{
1993 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001994 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995
1996 if (**arg != '<')
1997 {
1998 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001999 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 else
2001 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002002 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 }
2004 *arg = skipwhite(*arg + 1);
2005
Bram Moolenaard77a8522020-04-03 21:59:57 +02002006 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007
2008 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002009 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 {
2011 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002012 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 }
2014 ++*arg;
2015
2016 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002017 return get_list_type(member_type, type_gap);
2018 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019}
2020
2021/*
2022 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02002023 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 */
2025 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002026parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027{
2028 char_u *p = *arg;
2029 size_t len;
2030
2031 // skip over the first word
2032 while (ASCII_ISALNUM(*p) || *p == '_')
2033 ++p;
2034 len = p - *arg;
2035
2036 switch (**arg)
2037 {
2038 case 'a':
2039 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
2040 {
2041 *arg += len;
2042 return &t_any;
2043 }
2044 break;
2045 case 'b':
2046 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
2047 {
2048 *arg += len;
2049 return &t_bool;
2050 }
2051 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
2052 {
2053 *arg += len;
2054 return &t_blob;
2055 }
2056 break;
2057 case 'c':
2058 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
2059 {
2060 *arg += len;
2061 return &t_channel;
2062 }
2063 break;
2064 case 'd':
2065 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
2066 {
2067 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002068 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 }
2070 break;
2071 case 'f':
2072 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
2073 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01002074#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 *arg += len;
2076 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002077#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002078 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002079 return &t_any;
2080#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 }
2082 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2083 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002084 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002085 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002086 int argcount = -1;
2087 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002088 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002089 type_T *arg_type[MAX_FUNC_ARGS + 1];
2090
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002091 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002093 if (**arg == '(')
2094 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002095 // "func" may or may not return a value, "func()" does
2096 // not return a value.
2097 ret_type = &t_void;
2098
Bram Moolenaard77a8522020-04-03 21:59:57 +02002099 p = ++*arg;
2100 argcount = 0;
2101 while (*p != NUL && *p != ')')
2102 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002103 if (*p == '?')
2104 {
2105 if (first_optional == -1)
2106 first_optional = argcount;
2107 ++p;
2108 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002109 else if (STRNCMP(p, "...", 3) == 0)
2110 {
2111 flags |= TTFLAG_VARARGS;
2112 p += 3;
2113 }
Bram Moolenaar01865ad2020-07-26 18:33:09 +02002114 else if (first_optional != -1)
2115 {
2116 emsg(_("E1007: mandatory argument after optional argument"));
2117 return &t_any;
2118 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002119
2120 arg_type[argcount++] = parse_type(&p, type_gap);
2121
2122 // Nothing comes after "...{type}".
2123 if (flags & TTFLAG_VARARGS)
2124 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002125
Bram Moolenaard77a8522020-04-03 21:59:57 +02002126 if (*p != ',' && *skipwhite(p) == ',')
2127 {
2128 semsg(_(e_no_white_before), ",");
2129 return &t_any;
2130 }
2131 if (*p == ',')
2132 {
2133 ++p;
2134 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002135 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002136 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002137 return &t_any;
2138 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002139 }
2140 p = skipwhite(p);
2141 if (argcount == MAX_FUNC_ARGS)
2142 {
2143 emsg(_("E740: Too many argument types"));
2144 return &t_any;
2145 }
2146 }
2147
2148 p = skipwhite(p);
2149 if (*p != ')')
2150 {
2151 emsg(_(e_missing_close));
2152 return &t_any;
2153 }
2154 *arg = p + 1;
2155 }
2156 if (**arg == ':')
2157 {
2158 // parse return type
2159 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002160 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002161 semsg(_(e_white_after), ":");
2162 *arg = skipwhite(*arg);
2163 ret_type = parse_type(arg, type_gap);
2164 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002165 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002166 type = get_func_type(ret_type, argcount, type_gap);
2167 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002168 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002169 type = alloc_func_type(ret_type, argcount, type_gap);
2170 type->tt_flags = flags;
2171 if (argcount > 0)
2172 {
2173 type->tt_argcount = argcount;
2174 type->tt_min_argcount = first_optional == -1
2175 ? argcount : first_optional;
2176 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002177 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002178 return &t_any;
2179 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002180 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002181 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002182 }
2183 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 }
2185 break;
2186 case 'j':
2187 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2188 {
2189 *arg += len;
2190 return &t_job;
2191 }
2192 break;
2193 case 'l':
2194 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2195 {
2196 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002197 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 }
2199 break;
2200 case 'n':
2201 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2202 {
2203 *arg += len;
2204 return &t_number;
2205 }
2206 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 case 's':
2208 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2209 {
2210 *arg += len;
2211 return &t_string;
2212 }
2213 break;
2214 case 'v':
2215 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2216 {
2217 *arg += len;
2218 return &t_void;
2219 }
2220 break;
2221 }
2222
2223 semsg(_("E1010: Type not recognized: %s"), *arg);
2224 return &t_any;
2225}
2226
2227/*
2228 * Check if "type1" and "type2" are exactly the same.
2229 */
2230 static int
2231equal_type(type_T *type1, type_T *type2)
2232{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002233 int i;
2234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 if (type1->tt_type != type2->tt_type)
2236 return FALSE;
2237 switch (type1->tt_type)
2238 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002240 case VAR_ANY:
2241 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 case VAR_SPECIAL:
2243 case VAR_BOOL:
2244 case VAR_NUMBER:
2245 case VAR_FLOAT:
2246 case VAR_STRING:
2247 case VAR_BLOB:
2248 case VAR_JOB:
2249 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002250 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 case VAR_LIST:
2252 case VAR_DICT:
2253 return equal_type(type1->tt_member, type2->tt_member);
2254 case VAR_FUNC:
2255 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002256 if (!equal_type(type1->tt_member, type2->tt_member)
2257 || type1->tt_argcount != type2->tt_argcount)
2258 return FALSE;
2259 if (type1->tt_argcount < 0
2260 || type1->tt_args == NULL || type2->tt_args == NULL)
2261 return TRUE;
2262 for (i = 0; i < type1->tt_argcount; ++i)
2263 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2264 return FALSE;
2265 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 }
2267 return TRUE;
2268}
2269
2270/*
2271 * Find the common type of "type1" and "type2" and put it in "dest".
2272 * "type2" and "dest" may be the same.
2273 */
2274 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002275common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276{
2277 if (equal_type(type1, type2))
2278 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002279 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 return;
2281 }
2282
2283 if (type1->tt_type == type2->tt_type)
2284 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2286 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002287 type_T *common;
2288
Bram Moolenaard77a8522020-04-03 21:59:57 +02002289 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002290 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002291 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002292 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002293 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 return;
2295 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002296 if (type1->tt_type == VAR_FUNC)
2297 {
2298 type_T *common;
2299
2300 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2301 if (type1->tt_argcount == type2->tt_argcount
2302 && type1->tt_argcount >= 0)
2303 {
2304 int argcount = type1->tt_argcount;
2305 int i;
2306
2307 *dest = alloc_func_type(common, argcount, type_gap);
2308 if (type1->tt_args != NULL && type2->tt_args != NULL)
2309 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002310 if (func_type_add_arg_types(*dest, argcount,
2311 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002312 for (i = 0; i < argcount; ++i)
2313 common_type(type1->tt_args[i], type2->tt_args[i],
2314 &(*dest)->tt_args[i], type_gap);
2315 }
2316 }
2317 else
2318 *dest = alloc_func_type(common, -1, type_gap);
2319 return;
2320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 }
2322
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002323 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324}
2325
2326 char *
2327vartype_name(vartype_T type)
2328{
2329 switch (type)
2330 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002331 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002332 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 case VAR_SPECIAL: return "special";
2335 case VAR_BOOL: return "bool";
2336 case VAR_NUMBER: return "number";
2337 case VAR_FLOAT: return "float";
2338 case VAR_STRING: return "string";
2339 case VAR_BLOB: return "blob";
2340 case VAR_JOB: return "job";
2341 case VAR_CHANNEL: return "channel";
2342 case VAR_LIST: return "list";
2343 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002344
2345 case VAR_FUNC:
2346 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002348 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349}
2350
2351/*
2352 * Return the name of a type.
2353 * The result may be in allocated memory, in which case "tofree" is set.
2354 */
2355 char *
2356type_name(type_T *type, char **tofree)
2357{
2358 char *name = vartype_name(type->tt_type);
2359
2360 *tofree = NULL;
2361 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2362 {
2363 char *member_free;
2364 char *member_name = type_name(type->tt_member, &member_free);
2365 size_t len;
2366
2367 len = STRLEN(name) + STRLEN(member_name) + 3;
2368 *tofree = alloc(len);
2369 if (*tofree != NULL)
2370 {
2371 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2372 vim_free(member_free);
2373 return *tofree;
2374 }
2375 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002376 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002377 {
2378 garray_T ga;
2379 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002380 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002381
2382 ga_init2(&ga, 1, 100);
2383 if (ga_grow(&ga, 20) == FAIL)
2384 return "[unknown]";
2385 *tofree = ga.ga_data;
2386 STRCPY(ga.ga_data, "func(");
2387 ga.ga_len += 5;
2388
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002389 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002390 {
2391 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002392 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002393 int len;
2394
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002395 if (type->tt_args == NULL)
2396 arg_type = "[unknown]";
2397 else
2398 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002399 if (i > 0)
2400 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002401 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002402 ga.ga_len += 2;
2403 }
2404 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002405 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002406 {
2407 vim_free(arg_free);
2408 return "[unknown]";
2409 }
2410 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002411 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002412 {
2413 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2414 ga.ga_len += 3;
2415 }
2416 else if (i >= type->tt_min_argcount)
2417 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002418 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002419 ga.ga_len += len;
2420 vim_free(arg_free);
2421 }
2422
2423 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002424 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002425 else
2426 {
2427 char *ret_free;
2428 char *ret_name = type_name(type->tt_member, &ret_free);
2429 int len;
2430
2431 len = (int)STRLEN(ret_name) + 4;
2432 if (ga_grow(&ga, len) == FAIL)
2433 {
2434 vim_free(ret_free);
2435 return "[unknown]";
2436 }
2437 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002438 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2439 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002440 vim_free(ret_free);
2441 }
2442 return ga.ga_data;
2443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444
2445 return name;
2446}
2447
2448/*
2449 * Find "name" in script-local items of script "sid".
2450 * Returns the index in "sn_var_vals" if found.
2451 * If found but not in "sn_var_vals" returns -1.
2452 * If not found returns -2.
2453 */
2454 int
2455get_script_item_idx(int sid, char_u *name, int check_writable)
2456{
2457 hashtab_T *ht;
2458 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002459 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 int idx;
2461
2462 // First look the name up in the hashtable.
2463 if (sid <= 0 || sid > script_items.ga_len)
2464 return -1;
2465 ht = &SCRIPT_VARS(sid);
2466 di = find_var_in_ht(ht, 0, name, TRUE);
2467 if (di == NULL)
2468 return -2;
2469
2470 // Now find the svar_T index in sn_var_vals.
2471 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2472 {
2473 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2474
2475 if (sv->sv_tv == &di->di_tv)
2476 {
2477 if (check_writable && sv->sv_const)
2478 semsg(_(e_readonlyvar), name);
2479 return idx;
2480 }
2481 }
2482 return -1;
2483}
2484
2485/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002486 * Find "name" in imported items of the current script or in "cctx" if not
2487 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 */
2489 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002490find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002492 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 int idx;
2494
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002495 if (current_sctx.sc_sid <= 0)
2496 return NULL;
2497 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 if (cctx != NULL)
2499 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2500 {
2501 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2502 + idx;
2503
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002504 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2505 : STRLEN(import->imp_name) == len
2506 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 return import;
2508 }
2509
2510 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2511 {
2512 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2513
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002514 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2515 : STRLEN(import->imp_name) == len
2516 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 return import;
2518 }
2519 return NULL;
2520}
2521
2522/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002523 * Free all imported variables.
2524 */
2525 static void
2526free_imported(cctx_T *cctx)
2527{
2528 int idx;
2529
2530 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2531 {
2532 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2533
2534 vim_free(import->imp_name);
2535 }
2536 ga_clear(&cctx->ctx_imports);
2537}
2538
2539/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002540 * Return TRUE if "p" points at a "#" but not at "#{".
2541 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002542 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002543vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002544{
2545 return p[0] == '#' && p[1] != '{';
2546}
2547
2548/*
2549 * Return a pointer to the next line that isn't empty or only contains a
2550 * comment. Skips over white space.
2551 * Returns NULL if there is none.
2552 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002553 char_u *
2554peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002555{
2556 int lnum = cctx->ctx_lnum;
2557
2558 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2559 {
2560 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002561 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002562
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002563 if (line == NULL)
2564 break;
2565 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002566 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002567 return p;
2568 }
2569 return NULL;
2570}
2571
2572/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002573 * Called when checking for a following operator at "arg". When the rest of
2574 * the line is empty or only a comment, peek the next line. If there is a next
2575 * line return a pointer to it and set "nextp".
2576 * Otherwise skip over white space.
2577 */
2578 static char_u *
2579may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2580{
2581 char_u *p = skipwhite(arg);
2582
2583 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002584 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002585 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002586 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002587 if (*nextp != NULL)
2588 return *nextp;
2589 }
2590 return p;
2591}
2592
2593/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002594 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002595 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002596 * Returns NULL when at the end.
2597 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002598 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002599next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002600{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002601 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002602
2603 do
2604 {
2605 ++cctx->ctx_lnum;
2606 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002607 {
2608 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002609 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002610 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002611 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002612 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002613 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002614 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002615 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002616 return line;
2617}
2618
2619/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002620 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002621 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002622 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2623 */
2624 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002625may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002626{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002627 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002628 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002629 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002630
2631 if (next == NULL)
2632 return FAIL;
2633 *arg = skipwhite(next);
2634 }
2635 return OK;
2636}
2637
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002638/*
2639 * Idem, and give an error when failed.
2640 */
2641 static int
2642may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2643{
2644 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2645 {
2646 emsg(_("E1097: line incomplete"));
2647 return FAIL;
2648 }
2649 return OK;
2650}
2651
2652
Bram Moolenaara5565e42020-05-09 15:44:01 +02002653// Structure passed between the compile_expr* functions to keep track of
2654// constants that have been parsed but for which no code was produced yet. If
2655// possible expressions on these constants are applied at compile time. If
2656// that is not possible, the code to push the constants needs to be generated
2657// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002658// Using 50 should be more than enough of 5 levels of ().
2659#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002660typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002661 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002662 int pp_used; // active entries in pp_tv[]
2663} ppconst_T;
2664
Bram Moolenaar1c747212020-05-09 18:28:34 +02002665static int compile_expr0(char_u **arg, cctx_T *cctx);
2666static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2667
Bram Moolenaara5565e42020-05-09 15:44:01 +02002668/*
2669 * Generate a PUSH instruction for "tv".
2670 * "tv" will be consumed or cleared.
2671 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2672 */
2673 static int
2674generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2675{
2676 if (tv != NULL)
2677 {
2678 switch (tv->v_type)
2679 {
2680 case VAR_UNKNOWN:
2681 break;
2682 case VAR_BOOL:
2683 generate_PUSHBOOL(cctx, tv->vval.v_number);
2684 break;
2685 case VAR_SPECIAL:
2686 generate_PUSHSPEC(cctx, tv->vval.v_number);
2687 break;
2688 case VAR_NUMBER:
2689 generate_PUSHNR(cctx, tv->vval.v_number);
2690 break;
2691#ifdef FEAT_FLOAT
2692 case VAR_FLOAT:
2693 generate_PUSHF(cctx, tv->vval.v_float);
2694 break;
2695#endif
2696 case VAR_BLOB:
2697 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2698 tv->vval.v_blob = NULL;
2699 break;
2700 case VAR_STRING:
2701 generate_PUSHS(cctx, tv->vval.v_string);
2702 tv->vval.v_string = NULL;
2703 break;
2704 default:
2705 iemsg("constant type not supported");
2706 clear_tv(tv);
2707 return FAIL;
2708 }
2709 tv->v_type = VAR_UNKNOWN;
2710 }
2711 return OK;
2712}
2713
2714/*
2715 * Generate code for any ppconst entries.
2716 */
2717 static int
2718generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2719{
2720 int i;
2721 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002722 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002723
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002724 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002725 for (i = 0; i < ppconst->pp_used; ++i)
2726 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2727 ret = FAIL;
2728 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002729 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002730 return ret;
2731}
2732
2733/*
2734 * Clear ppconst constants. Used when failing.
2735 */
2736 static void
2737clear_ppconst(ppconst_T *ppconst)
2738{
2739 int i;
2740
2741 for (i = 0; i < ppconst->pp_used; ++i)
2742 clear_tv(&ppconst->pp_tv[i]);
2743 ppconst->pp_used = 0;
2744}
2745
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002746/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002747 * Generate an instruction to load script-local variable "name", without the
2748 * leading "s:".
2749 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 */
2751 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002752compile_load_scriptvar(
2753 cctx_T *cctx,
2754 char_u *name, // variable NUL terminated
2755 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002756 char_u **end, // end of variable
2757 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002759 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2761 imported_T *import;
2762
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002763 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002765 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002766 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2767 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 }
2769 if (idx >= 0)
2770 {
2771 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2772
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002773 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 current_sctx.sc_sid, idx, sv->sv_type);
2775 return OK;
2776 }
2777
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002778 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779 if (import != NULL)
2780 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002781 if (import->imp_all)
2782 {
2783 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002784 char_u *exp_name;
2785 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002786 ufunc_T *ufunc;
2787 type_T *type;
2788
2789 // Used "import * as Name", need to lookup the member.
2790 if (*p != '.')
2791 {
2792 semsg(_("E1060: expected dot after name: %s"), start);
2793 return FAIL;
2794 }
2795 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002796 if (VIM_ISWHITE(*p))
2797 {
2798 emsg(_("E1074: no white space allowed after dot"));
2799 return FAIL;
2800 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002801
Bram Moolenaar1c991142020-07-04 13:15:31 +02002802 // isolate one name
2803 exp_name = p;
2804 while (eval_isnamec(*p))
2805 ++p;
2806 cc = *p;
2807 *p = NUL;
2808
2809 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2810 *p = cc;
2811 p = skipwhite(p);
2812
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002813 // TODO: what if it is a function?
2814 if (idx < 0)
2815 return FAIL;
2816 *end = p;
2817
2818 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2819 import->imp_sid,
2820 idx,
2821 type);
2822 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002823 else if (import->imp_funcname != NULL)
2824 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002825 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002826 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2827 import->imp_sid,
2828 import->imp_var_vals_idx,
2829 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 return OK;
2831 }
2832
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002833 if (error)
2834 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 return FAIL;
2836}
2837
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002838 static int
2839generate_funcref(cctx_T *cctx, char_u *name)
2840{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002841 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002842
2843 if (ufunc == NULL)
2844 return FAIL;
2845
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002846 // Need to compile any default values to get the argument types.
2847 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2848 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2849 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002850 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002851}
2852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853/*
2854 * Compile a variable name into a load instruction.
2855 * "end" points to just after the name.
2856 * When "error" is FALSE do not give an error when not found.
2857 */
2858 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002859compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860{
2861 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002862 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002863 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002865 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866
2867 if (*(*arg + 1) == ':')
2868 {
2869 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002870 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002871 {
2872 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002874 switch (**arg)
2875 {
2876 case 'g': isn_type = ISN_LOADGDICT; break;
2877 case 'w': isn_type = ISN_LOADWDICT; break;
2878 case 't': isn_type = ISN_LOADTDICT; break;
2879 case 'b': isn_type = ISN_LOADBDICT; break;
2880 default:
2881 semsg(_(e_namespace), *arg);
2882 goto theend;
2883 }
2884 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2885 goto theend;
2886 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 else
2889 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002890 isntype_T isn_type = ISN_DROP;
2891
2892 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2893 if (name == NULL)
2894 return FAIL;
2895
2896 switch (**arg)
2897 {
2898 case 'v': res = generate_LOADV(cctx, name, error);
2899 break;
2900 case 's': res = compile_load_scriptvar(cctx, name,
2901 NULL, NULL, error);
2902 break;
2903 case 'g': isn_type = ISN_LOADG; break;
2904 case 'w': isn_type = ISN_LOADW; break;
2905 case 't': isn_type = ISN_LOADT; break;
2906 case 'b': isn_type = ISN_LOADB; break;
2907 default: semsg(_(e_namespace), *arg);
2908 goto theend;
2909 }
2910 if (isn_type != ISN_DROP)
2911 {
2912 // Global, Buffer-local, Window-local and Tabpage-local
2913 // variables can be defined later, thus we don't check if it
2914 // exists, give error at runtime.
2915 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 }
2918 }
2919 else
2920 {
2921 size_t len = end - *arg;
2922 int idx;
2923 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002924 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925
2926 name = vim_strnsave(*arg, end - *arg);
2927 if (name == NULL)
2928 return FAIL;
2929
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002930 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002932 if (!gen_load_outer)
2933 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 }
2935 else
2936 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002937 lvar_T *lvar = lookup_local(*arg, len, cctx);
2938
2939 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002941 type = lvar->lv_type;
2942 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002943 if (lvar->lv_from_outer)
2944 gen_load_outer = TRUE;
2945 else
2946 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 }
2948 else
2949 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002950 // "var" can be script-local even without using "s:" if it
2951 // already exists.
2952 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2953 == SCRIPT_VERSION_VIM9
2954 || lookup_script(*arg, len) == OK)
2955 res = compile_load_scriptvar(cctx, name, *arg, &end,
2956 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002957
Bram Moolenaara5565e42020-05-09 15:44:01 +02002958 // When the name starts with an uppercase letter or "x:" it
2959 // can be a user defined function.
2960 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2961 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 }
2963 }
2964 if (gen_load)
2965 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002966 if (gen_load_outer)
2967 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 }
2969
2970 *arg = end;
2971
2972theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002973 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 semsg(_(e_var_notfound), name);
2975 vim_free(name);
2976 return res;
2977}
2978
2979/*
2980 * Compile the argument expressions.
2981 * "arg" points to just after the "(" and is advanced to after the ")"
2982 */
2983 static int
2984compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2985{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002986 char_u *p = *arg;
2987 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988
Bram Moolenaare6085c52020-04-12 20:19:16 +02002989 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002991 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2992 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002993 if (*p == ')')
2994 {
2995 *arg = p + 1;
2996 return OK;
2997 }
2998
Bram Moolenaara5565e42020-05-09 15:44:01 +02002999 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 return FAIL;
3001 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003002
3003 if (*p != ',' && *skipwhite(p) == ',')
3004 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02003005 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003006 p = skipwhite(p);
3007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003009 {
3010 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003011 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02003012 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003013 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003014 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003015 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003017failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003018 emsg(_(e_missing_close));
3019 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020}
3021
3022/*
3023 * Compile a function call: name(arg1, arg2)
3024 * "arg" points to "name", "arg + varlen" to the "(".
3025 * "argcount_init" is 1 for "value->method()"
3026 * Instructions:
3027 * EVAL arg1
3028 * EVAL arg2
3029 * BCALL / DCALL / UCALL
3030 */
3031 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003032compile_call(
3033 char_u **arg,
3034 size_t varlen,
3035 cctx_T *cctx,
3036 ppconst_T *ppconst,
3037 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038{
3039 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003040 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 int argcount = argcount_init;
3042 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003043 char_u fname_buf[FLEN_FIXED + 1];
3044 char_u *tofree = NULL;
3045 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003047 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048
Bram Moolenaara5565e42020-05-09 15:44:01 +02003049 // we can evaluate "has('name')" at compile time
3050 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3051 {
3052 char_u *s = skipwhite(*arg + varlen + 1);
3053 typval_T argvars[2];
3054
3055 argvars[0].v_type = VAR_UNKNOWN;
3056 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003057 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003058 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003059 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003060 s = skipwhite(s);
3061 if (*s == ')' && argvars[0].v_type == VAR_STRING)
3062 {
3063 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3064
3065 *arg = s + 1;
3066 argvars[1].v_type = VAR_UNKNOWN;
3067 tv->v_type = VAR_NUMBER;
3068 tv->vval.v_number = 0;
3069 f_has(argvars, tv);
3070 clear_tv(&argvars[0]);
3071 ++ppconst->pp_used;
3072 return OK;
3073 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003074 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003075 }
3076
3077 if (generate_ppconst(cctx, ppconst) == FAIL)
3078 return FAIL;
3079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 if (varlen >= sizeof(namebuf))
3081 {
3082 semsg(_("E1011: name too long: %s"), name);
3083 return FAIL;
3084 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003085 vim_strncpy(namebuf, *arg, varlen);
3086 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087
3088 *arg = skipwhite(*arg + varlen + 1);
3089 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003090 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003092 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 {
3094 int idx;
3095
3096 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003097 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003099 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003100 else
3101 semsg(_(e_unknownfunc), namebuf);
3102 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 }
3104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003106 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003108 {
3109 res = generate_CALL(cctx, ufunc, argcount);
3110 goto theend;
3111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112
3113 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003114 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003116 if (STRNCMP(namebuf, "g:", 2) != 0
3117 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003118 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003119 garray_T *stack = &cctx->ctx_type_stack;
3120 type_T *type;
3121
3122 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3123 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003124 goto theend;
3125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003127 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003128 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003129 if (STRNCMP(namebuf, "g:", 2) == 0)
3130 res = generate_UCALL(cctx, name, argcount);
3131 else
3132 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003133
3134theend:
3135 vim_free(tofree);
3136 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137}
3138
3139// like NAMESPACE_CHAR but with 'a' and 'l'.
3140#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3141
3142/*
3143 * Find the end of a variable or function name. Unlike find_name_end() this
3144 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003145 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 * Return a pointer to just after the name. Equal to "arg" if there is no
3147 * valid name.
3148 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003149 static char_u *
3150to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151{
3152 char_u *p;
3153
3154 // Quick check for valid starting character.
3155 if (!eval_isnamec1(*arg))
3156 return arg;
3157
3158 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3159 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3160 // and can be used in slice "[n:]".
3161 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003162 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3164 break;
3165 return p;
3166}
3167
3168/*
3169 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003170 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 */
3172 char_u *
3173to_name_const_end(char_u *arg)
3174{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003175 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 typval_T rettv;
3177
3178 if (p == arg && *arg == '[')
3179 {
3180
3181 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003182 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 p = arg;
3184 }
3185 else if (p == arg && *arg == '#' && arg[1] == '{')
3186 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003187 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003189 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 p = arg;
3191 }
3192 else if (p == arg && *arg == '{')
3193 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003194 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003196 // Can be "{x -> ret}()".
3197 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003199 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 if (ret != OK)
3201 p = arg;
3202 }
3203
3204 return p;
3205}
3206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207/*
3208 * parse a list: [expr, expr]
3209 * "*arg" points to the '['.
3210 */
3211 static int
3212compile_list(char_u **arg, cctx_T *cctx)
3213{
3214 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003215 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 int count = 0;
3217
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003218 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003220 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003221 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003222 semsg(_(e_list_end), *arg);
3223 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003224 }
3225 if (*p == ']')
3226 {
3227 ++p;
3228 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003229 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003230 p += STRLEN(p);
3231 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003232 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003233 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 break;
3235 ++count;
3236 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003237 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003239 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3240 {
3241 semsg(_(e_white_after), ",");
3242 return FAIL;
3243 }
3244 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003245 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 p = skipwhite(p);
3247 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003248 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249
3250 generate_NEWLIST(cctx, count);
3251 return OK;
3252}
3253
3254/*
3255 * parse a lambda: {arg, arg -> expr}
3256 * "*arg" points to the '{'.
3257 */
3258 static int
3259compile_lambda(char_u **arg, cctx_T *cctx)
3260{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 typval_T rettv;
3262 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003263 evalarg_T evalarg;
3264
3265 CLEAR_FIELD(evalarg);
3266 evalarg.eval_flags = EVAL_EVALUATE;
3267 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268
3269 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003270 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003274 ++ufunc->uf_refcount;
3275 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003276 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277
3278 // The function will have one line: "return {expr}".
3279 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003280 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003282 clear_evalarg(&evalarg, NULL);
3283
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003284 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003285 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003286
3287 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 return FAIL;
3289}
3290
3291/*
3292 * Compile a lamda call: expr->{lambda}(args)
3293 * "arg" points to the "{".
3294 */
3295 static int
3296compile_lambda_call(char_u **arg, cctx_T *cctx)
3297{
3298 ufunc_T *ufunc;
3299 typval_T rettv;
3300 int argcount = 1;
3301 int ret = FAIL;
3302
3303 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003304 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 return FAIL;
3306
3307 if (**arg != '(')
3308 {
3309 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003310 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 else
3312 semsg(_(e_missing_paren), "lambda");
3313 clear_tv(&rettv);
3314 return FAIL;
3315 }
3316
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 ufunc = rettv.vval.v_partial->pt_func;
3318 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003319 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003320 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003321
3322 // The function will have one line: "return {expr}".
3323 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003324 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325
3326 // compile the arguments
3327 *arg = skipwhite(*arg + 1);
3328 if (compile_arguments(arg, cctx, &argcount) == OK)
3329 // call the compiled function
3330 ret = generate_CALL(cctx, ufunc, argcount);
3331
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003332 if (ret == FAIL)
3333 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 return ret;
3335}
3336
3337/*
3338 * parse a dict: {'key': val} or #{key: val}
3339 * "*arg" points to the '{'.
3340 */
3341 static int
3342compile_dict(char_u **arg, cctx_T *cctx, int literal)
3343{
3344 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003345 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 int count = 0;
3347 dict_T *d = dict_alloc();
3348 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003349 char_u *whitep = *arg;
3350 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351
3352 if (d == NULL)
3353 return FAIL;
3354 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003355 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 {
3357 char_u *key = NULL;
3358
Bram Moolenaar23c55272020-06-21 16:58:13 +02003359 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003360 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003361 *arg = NULL;
3362 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003363 }
3364
3365 if (**arg == '}')
3366 break;
3367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 if (literal)
3369 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003370 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371
Bram Moolenaar2c330432020-04-13 14:41:35 +02003372 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 {
3374 semsg(_("E1014: Invalid key: %s"), *arg);
3375 return FAIL;
3376 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003377 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 if (generate_PUSHS(cctx, key) == FAIL)
3379 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003380 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 }
3382 else
3383 {
3384 isn_T *isn;
3385
Bram Moolenaara5565e42020-05-09 15:44:01 +02003386 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3389 if (isn->isn_type == ISN_PUSHS)
3390 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003391 else
3392 {
3393 type_T *keytype = ((type_T **)stack->ga_data)
3394 [stack->ga_len - 1];
3395 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3396 return FAIL;
3397 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 }
3399
3400 // Check for duplicate keys, if using string keys.
3401 if (key != NULL)
3402 {
3403 item = dict_find(d, key, -1);
3404 if (item != NULL)
3405 {
3406 semsg(_(e_duplicate_key), key);
3407 goto failret;
3408 }
3409 item = dictitem_alloc(key);
3410 if (item != NULL)
3411 {
3412 item->di_tv.v_type = VAR_UNKNOWN;
3413 item->di_tv.v_lock = 0;
3414 if (dict_add(d, item) == FAIL)
3415 dictitem_free(item);
3416 }
3417 }
3418
3419 *arg = skipwhite(*arg);
3420 if (**arg != ':')
3421 {
3422 semsg(_(e_missing_dict_colon), *arg);
3423 return FAIL;
3424 }
3425
Bram Moolenaar2c330432020-04-13 14:41:35 +02003426 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003428 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003429 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003430 *arg = NULL;
3431 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003432 }
3433
Bram Moolenaara5565e42020-05-09 15:44:01 +02003434 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 return FAIL;
3436 ++count;
3437
Bram Moolenaar2c330432020-04-13 14:41:35 +02003438 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003439 *arg = skipwhite(*arg);
3440 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003441 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003442 *arg = NULL;
3443 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 if (**arg == '}')
3446 break;
3447 if (**arg != ',')
3448 {
3449 semsg(_(e_missing_dict_comma), *arg);
3450 goto failret;
3451 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003452 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 *arg = skipwhite(*arg + 1);
3454 }
3455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 *arg = *arg + 1;
3457
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003458 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003459 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003460 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003461 *arg += STRLEN(*arg);
3462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 dict_unref(d);
3464 return generate_NEWDICT(cctx, count);
3465
3466failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003467 if (*arg == NULL)
3468 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 dict_unref(d);
3470 return FAIL;
3471}
3472
3473/*
3474 * Compile "&option".
3475 */
3476 static int
3477compile_get_option(char_u **arg, cctx_T *cctx)
3478{
3479 typval_T rettv;
3480 char_u *start = *arg;
3481 int ret;
3482
3483 // parse the option and get the current value to get the type.
3484 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003485 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 if (ret == OK)
3487 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003488 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 char_u *name = vim_strnsave(start, *arg - start);
3490 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3491
3492 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3493 vim_free(name);
3494 }
3495 clear_tv(&rettv);
3496
3497 return ret;
3498}
3499
3500/*
3501 * Compile "$VAR".
3502 */
3503 static int
3504compile_get_env(char_u **arg, cctx_T *cctx)
3505{
3506 char_u *start = *arg;
3507 int len;
3508 int ret;
3509 char_u *name;
3510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 ++*arg;
3512 len = get_env_len(arg);
3513 if (len == 0)
3514 {
3515 semsg(_(e_syntax_at), start - 1);
3516 return FAIL;
3517 }
3518
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003519 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 name = vim_strnsave(start, len + 1);
3521 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3522 vim_free(name);
3523 return ret;
3524}
3525
3526/*
3527 * Compile "@r".
3528 */
3529 static int
3530compile_get_register(char_u **arg, cctx_T *cctx)
3531{
3532 int ret;
3533
3534 ++*arg;
3535 if (**arg == NUL)
3536 {
3537 semsg(_(e_syntax_at), *arg - 1);
3538 return FAIL;
3539 }
3540 if (!valid_yank_reg(**arg, TRUE))
3541 {
3542 emsg_invreg(**arg);
3543 return FAIL;
3544 }
3545 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3546 ++*arg;
3547 return ret;
3548}
3549
3550/*
3551 * Apply leading '!', '-' and '+' to constant "rettv".
3552 */
3553 static int
3554apply_leader(typval_T *rettv, char_u *start, char_u *end)
3555{
3556 char_u *p = end;
3557
3558 // this works from end to start
3559 while (p > start)
3560 {
3561 --p;
3562 if (*p == '-' || *p == '+')
3563 {
3564 // only '-' has an effect, for '+' we only check the type
3565#ifdef FEAT_FLOAT
3566 if (rettv->v_type == VAR_FLOAT)
3567 {
3568 if (*p == '-')
3569 rettv->vval.v_float = -rettv->vval.v_float;
3570 }
3571 else
3572#endif
3573 {
3574 varnumber_T val;
3575 int error = FALSE;
3576
3577 // tv_get_number_chk() accepts a string, but we don't want that
3578 // here
3579 if (check_not_string(rettv) == FAIL)
3580 return FAIL;
3581 val = tv_get_number_chk(rettv, &error);
3582 clear_tv(rettv);
3583 if (error)
3584 return FAIL;
3585 if (*p == '-')
3586 val = -val;
3587 rettv->v_type = VAR_NUMBER;
3588 rettv->vval.v_number = val;
3589 }
3590 }
3591 else
3592 {
3593 int v = tv2bool(rettv);
3594
3595 // '!' is permissive in the type.
3596 clear_tv(rettv);
3597 rettv->v_type = VAR_BOOL;
3598 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3599 }
3600 }
3601 return OK;
3602}
3603
3604/*
3605 * Recognize v: variables that are constants and set "rettv".
3606 */
3607 static void
3608get_vim_constant(char_u **arg, typval_T *rettv)
3609{
3610 if (STRNCMP(*arg, "v:true", 6) == 0)
3611 {
3612 rettv->v_type = VAR_BOOL;
3613 rettv->vval.v_number = VVAL_TRUE;
3614 *arg += 6;
3615 }
3616 else if (STRNCMP(*arg, "v:false", 7) == 0)
3617 {
3618 rettv->v_type = VAR_BOOL;
3619 rettv->vval.v_number = VVAL_FALSE;
3620 *arg += 7;
3621 }
3622 else if (STRNCMP(*arg, "v:null", 6) == 0)
3623 {
3624 rettv->v_type = VAR_SPECIAL;
3625 rettv->vval.v_number = VVAL_NULL;
3626 *arg += 6;
3627 }
3628 else if (STRNCMP(*arg, "v:none", 6) == 0)
3629 {
3630 rettv->v_type = VAR_SPECIAL;
3631 rettv->vval.v_number = VVAL_NONE;
3632 *arg += 6;
3633 }
3634}
3635
Bram Moolenaar61a89812020-05-07 16:58:17 +02003636 static exptype_T
3637get_compare_type(char_u *p, int *len, int *type_is)
3638{
3639 exptype_T type = EXPR_UNKNOWN;
3640 int i;
3641
3642 switch (p[0])
3643 {
3644 case '=': if (p[1] == '=')
3645 type = EXPR_EQUAL;
3646 else if (p[1] == '~')
3647 type = EXPR_MATCH;
3648 break;
3649 case '!': if (p[1] == '=')
3650 type = EXPR_NEQUAL;
3651 else if (p[1] == '~')
3652 type = EXPR_NOMATCH;
3653 break;
3654 case '>': if (p[1] != '=')
3655 {
3656 type = EXPR_GREATER;
3657 *len = 1;
3658 }
3659 else
3660 type = EXPR_GEQUAL;
3661 break;
3662 case '<': if (p[1] != '=')
3663 {
3664 type = EXPR_SMALLER;
3665 *len = 1;
3666 }
3667 else
3668 type = EXPR_SEQUAL;
3669 break;
3670 case 'i': if (p[1] == 's')
3671 {
3672 // "is" and "isnot"; but not a prefix of a name
3673 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3674 *len = 5;
3675 i = p[*len];
3676 if (!isalnum(i) && i != '_')
3677 {
3678 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3679 *type_is = TRUE;
3680 }
3681 }
3682 break;
3683 }
3684 return type;
3685}
3686
3687/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 * Compile code to apply '-', '+' and '!'.
3689 */
3690 static int
3691compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3692{
3693 char_u *p = end;
3694
3695 // this works from end to start
3696 while (p > start)
3697 {
3698 --p;
3699 if (*p == '-' || *p == '+')
3700 {
3701 int negate = *p == '-';
3702 isn_T *isn;
3703
3704 // TODO: check type
3705 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3706 {
3707 --p;
3708 if (*p == '-')
3709 negate = !negate;
3710 }
3711 // only '-' has an effect, for '+' we only check the type
3712 if (negate)
3713 isn = generate_instr(cctx, ISN_NEGATENR);
3714 else
3715 isn = generate_instr(cctx, ISN_CHECKNR);
3716 if (isn == NULL)
3717 return FAIL;
3718 }
3719 else
3720 {
3721 int invert = TRUE;
3722
3723 while (p > start && p[-1] == '!')
3724 {
3725 --p;
3726 invert = !invert;
3727 }
3728 if (generate_2BOOL(cctx, invert) == FAIL)
3729 return FAIL;
3730 }
3731 }
3732 return OK;
3733}
3734
3735/*
3736 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003737 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 */
3739 static int
3740compile_subscript(
3741 char_u **arg,
3742 cctx_T *cctx,
3743 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003744 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003745 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746{
3747 for (;;)
3748 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003749 char_u *p = skipwhite(*arg);
3750
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003751 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003752 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003753 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003754
3755 // If a following line starts with "->{" or "->X" advance to that
3756 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003757 // Also if a following line starts with ".x".
3758 if (next != NULL &&
3759 ((next[0] == '-' && next[1] == '>'
3760 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003761 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003762 {
3763 next = next_line_from_context(cctx, TRUE);
3764 if (next == NULL)
3765 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003766 *arg = next;
3767 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003768 }
3769 }
3770
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003771 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3772 // not a function call.
3773 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003775 garray_T *stack = &cctx->ctx_type_stack;
3776 type_T *type;
3777 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003779 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003780 return FAIL;
3781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003783 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3784
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003785 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3787 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003788 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 return FAIL;
3790 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003791 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003793 char_u *pstart = p;
3794
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003795 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003796 return FAIL;
3797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 // something->method()
3799 // Apply the '!', '-' and '+' first:
3800 // -1.0->func() works like (-1.0)->func()
3801 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3802 return FAIL;
3803 *start_leader = end_leader; // don't apply again later
3804
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003805 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003806 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003807 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 if (**arg == '{')
3809 {
3810 // lambda call: list->{lambda}
3811 if (compile_lambda_call(arg, cctx) == FAIL)
3812 return FAIL;
3813 }
3814 else
3815 {
3816 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003817 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003818 if (!eval_isnamec1(*p))
3819 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003820 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003821 return FAIL;
3822 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003823 if (ASCII_ISALPHA(*p) && p[1] == ':')
3824 p += 2;
3825 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 ;
3827 if (*p != '(')
3828 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003829 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 return FAIL;
3831 }
3832 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003833 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 return FAIL;
3835 }
3836 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003837 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003839 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003840 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003841 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003842
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003843 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003844 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003845 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003846 // TODO: blob index
3847 // TODO: more arguments
3848 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003849 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003850 return FAIL;
3851
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003852 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003853 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003854 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003855 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003856 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 return FAIL;
3858
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003859 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3860 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 if (**arg != ']')
3862 {
3863 emsg(_(e_missbrac));
3864 return FAIL;
3865 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003866 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003868 // We can index a list and a dict. If we don't know the type
3869 // we can use the index value type.
3870 // TODO: If we don't know use an instruction to figure it out at
3871 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003872 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003873 vtype = (*typep)->tt_type;
3874 if (*typep == &t_any)
3875 {
3876 type_T *valtype = ((type_T **)stack->ga_data)
3877 [stack->ga_len - 1];
3878 if (valtype == &t_string)
3879 vtype = VAR_DICT;
3880 }
3881 if (vtype == VAR_DICT)
3882 {
3883 if ((*typep)->tt_type == VAR_DICT)
3884 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003885 else
3886 {
3887 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3888 return FAIL;
3889 *typep = &t_any;
3890 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003891 if (may_generate_2STRING(-1, cctx) == FAIL)
3892 return FAIL;
3893 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3894 return FAIL;
3895 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003896 else if (vtype == VAR_STRING)
3897 {
3898 *typep = &t_number;
3899 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3900 return FAIL;
3901 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003902 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003903 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003904 if ((*typep)->tt_type == VAR_LIST)
3905 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003906 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003907 return FAIL;
3908 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003909 else
3910 {
3911 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003912 return FAIL;
3913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003915 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003917 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003918 return FAIL;
3919
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003920 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003921 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3922 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003924 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003925 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 while (eval_isnamec(*p))
3927 MB_PTR_ADV(p);
3928 if (p == *arg)
3929 {
3930 semsg(_(e_syntax_at), *arg);
3931 return FAIL;
3932 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003933 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 return FAIL;
3935 *arg = p;
3936 }
3937 else
3938 break;
3939 }
3940
3941 // TODO - see handle_subscript():
3942 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3943 // Don't do this when "Func" is already a partial that was bound
3944 // explicitly (pt_auto is FALSE).
3945
3946 return OK;
3947}
3948
3949/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003950 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3951 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003953 * If the value is a constant "ppconst->pp_ret" will be set.
3954 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003955 *
3956 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 */
3958
3959/*
3960 * number number constant
3961 * 0zFFFFFFFF Blob constant
3962 * "string" string constant
3963 * 'string' literal string constant
3964 * &option-name option value
3965 * @r register contents
3966 * identifier variable value
3967 * function() function call
3968 * $VAR environment variable
3969 * (expression) nested expression
3970 * [expr, expr] List
3971 * {key: val, key: val} Dictionary
3972 * #{key: val, key: val} Dictionary with literal keys
3973 *
3974 * Also handle:
3975 * ! in front logical NOT
3976 * - in front unary minus
3977 * + in front unary plus (ignored)
3978 * trailing (arg) funcref/partial call
3979 * trailing [] subscript in String or List
3980 * trailing .name entry in Dictionary
3981 * trailing ->name() method call
3982 */
3983 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003984compile_expr7(
3985 char_u **arg,
3986 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003987 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 char_u *start_leader, *end_leader;
3990 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003991 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003992 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993
3994 /*
3995 * Skip '!', '-' and '+' characters. They are handled later.
3996 */
3997 start_leader = *arg;
3998 while (**arg == '!' || **arg == '-' || **arg == '+')
3999 *arg = skipwhite(*arg + 1);
4000 end_leader = *arg;
4001
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004002 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 switch (**arg)
4004 {
4005 /*
4006 * Number constant.
4007 */
4008 case '0': // also for blob starting with 0z
4009 case '1':
4010 case '2':
4011 case '3':
4012 case '4':
4013 case '5':
4014 case '6':
4015 case '7':
4016 case '8':
4017 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004018 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 return FAIL;
4020 break;
4021
4022 /*
4023 * String constant: "string".
4024 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004025 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 return FAIL;
4027 break;
4028
4029 /*
4030 * Literal string constant: 'str''ing'.
4031 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004032 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 return FAIL;
4034 break;
4035
4036 /*
4037 * Constant Vim variable.
4038 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004039 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 ret = NOTDONE;
4041 break;
4042
4043 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004044 * "true" constant
4045 */
4046 case 't': if (STRNCMP(*arg, "true", 4) == 0
4047 && !eval_isnamec((*arg)[4]))
4048 {
4049 *arg += 4;
4050 rettv->v_type = VAR_BOOL;
4051 rettv->vval.v_number = VVAL_TRUE;
4052 }
4053 else
4054 ret = NOTDONE;
4055 break;
4056
4057 /*
4058 * "false" constant
4059 */
4060 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4061 && !eval_isnamec((*arg)[5]))
4062 {
4063 *arg += 5;
4064 rettv->v_type = VAR_BOOL;
4065 rettv->vval.v_number = VVAL_FALSE;
4066 }
4067 else
4068 ret = NOTDONE;
4069 break;
4070
4071 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 * List: [expr, expr]
4073 */
4074 case '[': ret = compile_list(arg, cctx);
4075 break;
4076
4077 /*
4078 * Dictionary: #{key: val, key: val}
4079 */
4080 case '#': if ((*arg)[1] == '{')
4081 {
4082 ++*arg;
4083 ret = compile_dict(arg, cctx, TRUE);
4084 }
4085 else
4086 ret = NOTDONE;
4087 break;
4088
4089 /*
4090 * Lambda: {arg, arg -> expr}
4091 * Dictionary: {'key': val, 'key': val}
4092 */
4093 case '{': {
4094 char_u *start = skipwhite(*arg + 1);
4095
4096 // Find out what comes after the arguments.
4097 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004098 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 if (ret != FAIL && *start == '>')
4100 ret = compile_lambda(arg, cctx);
4101 else
4102 ret = compile_dict(arg, cctx, FALSE);
4103 }
4104 break;
4105
4106 /*
4107 * Option value: &name
4108 */
4109 case '&': ret = compile_get_option(arg, cctx);
4110 break;
4111
4112 /*
4113 * Environment variable: $VAR.
4114 */
4115 case '$': ret = compile_get_env(arg, cctx);
4116 break;
4117
4118 /*
4119 * Register contents: @r.
4120 */
4121 case '@': ret = compile_get_register(arg, cctx);
4122 break;
4123 /*
4124 * nested expression: (expression).
4125 */
4126 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004127
4128 // recursive!
4129 if (ppconst->pp_used <= PPSIZE - 10)
4130 {
4131 ret = compile_expr1(arg, cctx, ppconst);
4132 }
4133 else
4134 {
4135 // Not enough space in ppconst, flush constants.
4136 if (generate_ppconst(cctx, ppconst) == FAIL)
4137 return FAIL;
4138 ret = compile_expr0(arg, cctx);
4139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 *arg = skipwhite(*arg);
4141 if (**arg == ')')
4142 ++*arg;
4143 else if (ret == OK)
4144 {
4145 emsg(_(e_missing_close));
4146 ret = FAIL;
4147 }
4148 break;
4149
4150 default: ret = NOTDONE;
4151 break;
4152 }
4153 if (ret == FAIL)
4154 return FAIL;
4155
Bram Moolenaar1c747212020-05-09 18:28:34 +02004156 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 {
4158 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004159 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004161 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 return FAIL;
4163 }
4164 start_leader = end_leader; // don't apply again below
4165
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004166 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004167 clear_tv(rettv);
4168 else
4169 // A constant expression can possibly be handled compile time,
4170 // return the value instead of generating code.
4171 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 }
4173 else if (ret == NOTDONE)
4174 {
4175 char_u *p;
4176 int r;
4177
4178 if (!eval_isnamec1(**arg))
4179 {
4180 semsg(_("E1015: Name expected: %s"), *arg);
4181 return FAIL;
4182 }
4183
4184 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004185 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004187 {
4188 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4189 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004191 {
4192 if (generate_ppconst(cctx, ppconst) == FAIL)
4193 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 if (r == FAIL)
4197 return FAIL;
4198 }
4199
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004200 // Handle following "[]", ".member", etc.
4201 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004202 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004203 ppconst) == FAIL)
4204 return FAIL;
4205 if (ppconst->pp_used > 0)
4206 {
4207 // apply the '!', '-' and '+' before the constant
4208 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4209 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4210 return FAIL;
4211 return OK;
4212 }
4213 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004215 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216}
4217
4218/*
4219 * * number multiplication
4220 * / number division
4221 * % number modulo
4222 */
4223 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004224compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225{
4226 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004227 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004228 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004230 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004231 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 return FAIL;
4233
4234 /*
4235 * Repeat computing, until no "*", "/" or "%" is following.
4236 */
4237 for (;;)
4238 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004239 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 if (*op != '*' && *op != '/' && *op != '%')
4241 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004242 if (next != NULL)
4243 {
4244 *arg = next_line_from_context(cctx, TRUE);
4245 op = skipwhite(*arg);
4246 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004247
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004248 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 {
4250 char_u buf[3];
4251
4252 vim_strncpy(buf, op, 1);
4253 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004254 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 }
4256 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004257 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004260 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004261 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004263
4264 if (ppconst->pp_used == ppconst_used + 2
4265 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4266 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004267 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004268 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4269 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004270 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004272 // both are numbers: compute the result
4273 switch (*op)
4274 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004275 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004276 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004277 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004278 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004279 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004280 break;
4281 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004282 tv1->vval.v_number = res;
4283 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004284 }
4285 else
4286 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004287 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004288 generate_two_op(cctx, op);
4289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 }
4291
4292 return OK;
4293}
4294
4295/*
4296 * + number addition
4297 * - number subtraction
4298 * .. string concatenation
4299 */
4300 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004301compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302{
4303 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004304 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004306 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307
4308 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004309 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 return FAIL;
4311
4312 /*
4313 * Repeat computing, until no "+", "-" or ".." is following.
4314 */
4315 for (;;)
4316 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004317 op = may_peek_next_line(cctx, *arg, &next);
4318 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 break;
4320 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004321 if (next != NULL)
4322 {
4323 *arg = next_line_from_context(cctx, TRUE);
4324 op = skipwhite(*arg);
4325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004327 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 {
4329 char_u buf[3];
4330
4331 vim_strncpy(buf, op, oplen);
4332 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004333 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 }
4335
4336 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004337 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004338 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004340 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004341 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 return FAIL;
4343
Bram Moolenaara5565e42020-05-09 15:44:01 +02004344 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004345 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004346 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4347 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4348 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4349 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004351 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4352 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004353
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004354 // concat/subtract/add constant numbers
4355 if (*op == '+')
4356 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4357 else if (*op == '-')
4358 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4359 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004360 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004361 // concatenate constant strings
4362 char_u *s1 = tv1->vval.v_string;
4363 char_u *s2 = tv2->vval.v_string;
4364 size_t len1 = STRLEN(s1);
4365
4366 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4367 if (tv1->vval.v_string == NULL)
4368 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004369 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004370 return FAIL;
4371 }
4372 mch_memmove(tv1->vval.v_string, s1, len1);
4373 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004374 vim_free(s1);
4375 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004376 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004377 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 }
4379 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004380 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004381 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004382 if (*op == '.')
4383 {
4384 if (may_generate_2STRING(-2, cctx) == FAIL
4385 || may_generate_2STRING(-1, cctx) == FAIL)
4386 return FAIL;
4387 generate_instr_drop(cctx, ISN_CONCAT, 1);
4388 }
4389 else
4390 generate_two_op(cctx, op);
4391 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 }
4393
4394 return OK;
4395}
4396
4397/*
4398 * expr5a == expr5b
4399 * expr5a =~ expr5b
4400 * expr5a != expr5b
4401 * expr5a !~ expr5b
4402 * expr5a > expr5b
4403 * expr5a >= expr5b
4404 * expr5a < expr5b
4405 * expr5a <= expr5b
4406 * expr5a is expr5b
4407 * expr5a isnot expr5b
4408 *
4409 * Produces instructions:
4410 * EVAL expr5a Push result of "expr5a"
4411 * EVAL expr5b Push result of "expr5b"
4412 * COMPARE one of the compare instructions
4413 */
4414 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004415compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416{
4417 exptype_T type = EXPR_UNKNOWN;
4418 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004419 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004422 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423
4424 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004425 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 return FAIL;
4427
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004428 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004429 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430
4431 /*
4432 * If there is a comparative operator, use it.
4433 */
4434 if (type != EXPR_UNKNOWN)
4435 {
4436 int ic = FALSE; // Default: do not ignore case
4437
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004438 if (next != NULL)
4439 {
4440 *arg = next_line_from_context(cctx, TRUE);
4441 p = skipwhite(*arg);
4442 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 if (type_is && (p[len] == '?' || p[len] == '#'))
4444 {
4445 semsg(_(e_invexpr2), *arg);
4446 return FAIL;
4447 }
4448 // extra question mark appended: ignore case
4449 if (p[len] == '?')
4450 {
4451 ic = TRUE;
4452 ++len;
4453 }
4454 // extra '#' appended: match case (ignored)
4455 else if (p[len] == '#')
4456 ++len;
4457 // nothing appended: match case
4458
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004459 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 {
4461 char_u buf[7];
4462
4463 vim_strncpy(buf, p, len);
4464 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004465 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 }
4467
4468 // get the second variable
4469 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004470 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004471 return FAIL;
4472
Bram Moolenaara5565e42020-05-09 15:44:01 +02004473 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 return FAIL;
4475
Bram Moolenaara5565e42020-05-09 15:44:01 +02004476 if (ppconst->pp_used == ppconst_used + 2)
4477 {
4478 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4479 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4480 int ret;
4481
4482 // Both sides are a constant, compute the result now.
4483 // First check for a valid combination of types, this is more
4484 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004485 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004486 ret = FAIL;
4487 else
4488 {
4489 ret = typval_compare(tv1, tv2, type, ic);
4490 tv1->v_type = VAR_BOOL;
4491 tv1->vval.v_number = tv1->vval.v_number
4492 ? VVAL_TRUE : VVAL_FALSE;
4493 clear_tv(tv2);
4494 --ppconst->pp_used;
4495 }
4496 return ret;
4497 }
4498
4499 generate_ppconst(cctx, ppconst);
4500 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 }
4502
4503 return OK;
4504}
4505
Bram Moolenaar7f141552020-05-09 17:35:53 +02004506static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508/*
4509 * Compile || or &&.
4510 */
4511 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004512compile_and_or(
4513 char_u **arg,
4514 cctx_T *cctx,
4515 char *op,
4516 ppconst_T *ppconst,
4517 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004519 char_u *next;
4520 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 int opchar = *op;
4522
4523 if (p[0] == opchar && p[1] == opchar)
4524 {
4525 garray_T *instr = &cctx->ctx_instr;
4526 garray_T end_ga;
4527
4528 /*
4529 * Repeat until there is no following "||" or "&&"
4530 */
4531 ga_init2(&end_ga, sizeof(int), 10);
4532 while (p[0] == opchar && p[1] == opchar)
4533 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004534 if (next != NULL)
4535 {
4536 *arg = next_line_from_context(cctx, TRUE);
4537 p = skipwhite(*arg);
4538 }
4539
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004540 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4541 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004543 return FAIL;
4544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545
Bram Moolenaara5565e42020-05-09 15:44:01 +02004546 // TODO: use ppconst if the value is a constant
4547 generate_ppconst(cctx, ppconst);
4548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 if (ga_grow(&end_ga, 1) == FAIL)
4550 {
4551 ga_clear(&end_ga);
4552 return FAIL;
4553 }
4554 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4555 ++end_ga.ga_len;
4556 generate_JUMP(cctx, opchar == '|'
4557 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4558
4559 // eval the next expression
4560 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004561 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004562 return FAIL;
4563
Bram Moolenaara5565e42020-05-09 15:44:01 +02004564 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4565 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 {
4567 ga_clear(&end_ga);
4568 return FAIL;
4569 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004570
4571 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004573 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574
4575 // Fill in the end label in all jumps.
4576 while (end_ga.ga_len > 0)
4577 {
4578 isn_T *isn;
4579
4580 --end_ga.ga_len;
4581 isn = ((isn_T *)instr->ga_data)
4582 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4583 isn->isn_arg.jump.jump_where = instr->ga_len;
4584 }
4585 ga_clear(&end_ga);
4586 }
4587
4588 return OK;
4589}
4590
4591/*
4592 * expr4a && expr4a && expr4a logical AND
4593 *
4594 * Produces instructions:
4595 * EVAL expr4a Push result of "expr4a"
4596 * JUMP_AND_KEEP_IF_FALSE end
4597 * EVAL expr4b Push result of "expr4b"
4598 * JUMP_AND_KEEP_IF_FALSE end
4599 * EVAL expr4c Push result of "expr4c"
4600 * end:
4601 */
4602 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004603compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004605 int ppconst_used = ppconst->pp_used;
4606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004608 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 return FAIL;
4610
4611 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004612 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613}
4614
4615/*
4616 * expr3a || expr3b || expr3c logical OR
4617 *
4618 * Produces instructions:
4619 * EVAL expr3a Push result of "expr3a"
4620 * JUMP_AND_KEEP_IF_TRUE end
4621 * EVAL expr3b Push result of "expr3b"
4622 * JUMP_AND_KEEP_IF_TRUE end
4623 * EVAL expr3c Push result of "expr3c"
4624 * end:
4625 */
4626 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004627compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004629 int ppconst_used = ppconst->pp_used;
4630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004632 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 return FAIL;
4634
4635 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004636 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637}
4638
4639/*
4640 * Toplevel expression: expr2 ? expr1a : expr1b
4641 *
4642 * Produces instructions:
4643 * EVAL expr2 Push result of "expr"
4644 * JUMP_IF_FALSE alt jump if false
4645 * EVAL expr1a
4646 * JUMP_ALWAYS end
4647 * alt: EVAL expr1b
4648 * end:
4649 */
4650 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004651compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652{
4653 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004654 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004655 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656
Bram Moolenaar61a89812020-05-07 16:58:17 +02004657 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004658 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 return FAIL;
4660
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004661 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 if (*p == '?')
4663 {
4664 garray_T *instr = &cctx->ctx_instr;
4665 garray_T *stack = &cctx->ctx_type_stack;
4666 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004667 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004669 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004671 int has_const_expr = FALSE;
4672 int const_value = FALSE;
4673 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004675 if (next != NULL)
4676 {
4677 *arg = next_line_from_context(cctx, TRUE);
4678 p = skipwhite(*arg);
4679 }
4680
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004681 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4682 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004684 return FAIL;
4685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686
Bram Moolenaara5565e42020-05-09 15:44:01 +02004687 if (ppconst->pp_used == ppconst_used + 1)
4688 {
4689 // the condition is a constant, we know whether the ? or the :
4690 // expression is to be evaluated.
4691 has_const_expr = TRUE;
4692 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4693 clear_tv(&ppconst->pp_tv[ppconst_used]);
4694 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004695 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4696 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004697 }
4698 else
4699 {
4700 generate_ppconst(cctx, ppconst);
4701 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4702 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703
4704 // evaluate the second expression; any type is accepted
4705 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004706 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004707 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004708 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004709 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710
Bram Moolenaara5565e42020-05-09 15:44:01 +02004711 if (!has_const_expr)
4712 {
4713 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714
Bram Moolenaara5565e42020-05-09 15:44:01 +02004715 // remember the type and drop it
4716 --stack->ga_len;
4717 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718
Bram Moolenaara5565e42020-05-09 15:44:01 +02004719 end_idx = instr->ga_len;
4720 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4721
4722 // jump here from JUMP_IF_FALSE
4723 isn = ((isn_T *)instr->ga_data) + alt_idx;
4724 isn->isn_arg.jump.jump_where = instr->ga_len;
4725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726
4727 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004728 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 if (*p != ':')
4730 {
4731 emsg(_(e_missing_colon));
4732 return FAIL;
4733 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004734 if (next != NULL)
4735 {
4736 *arg = next_line_from_context(cctx, TRUE);
4737 p = skipwhite(*arg);
4738 }
4739
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004740 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4741 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004743 return FAIL;
4744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745
4746 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004747 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004748 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4749 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004751 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004752 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004753 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004754 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755
Bram Moolenaara5565e42020-05-09 15:44:01 +02004756 if (!has_const_expr)
4757 {
4758 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759
Bram Moolenaara5565e42020-05-09 15:44:01 +02004760 // If the types differ, the result has a more generic type.
4761 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4762 common_type(type1, type2, &type2, cctx->ctx_type_list);
4763
4764 // jump here from JUMP_ALWAYS
4765 isn = ((isn_T *)instr->ga_data) + end_idx;
4766 isn->isn_arg.jump.jump_where = instr->ga_len;
4767 }
4768
4769 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 }
4771 return OK;
4772}
4773
4774/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004775 * Toplevel expression.
4776 */
4777 static int
4778compile_expr0(char_u **arg, cctx_T *cctx)
4779{
4780 ppconst_T ppconst;
4781
4782 CLEAR_FIELD(ppconst);
4783 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4784 {
4785 clear_ppconst(&ppconst);
4786 return FAIL;
4787 }
4788 if (generate_ppconst(cctx, &ppconst) == FAIL)
4789 return FAIL;
4790 return OK;
4791}
4792
4793/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 * compile "return [expr]"
4795 */
4796 static char_u *
4797compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4798{
4799 char_u *p = arg;
4800 garray_T *stack = &cctx->ctx_type_stack;
4801 type_T *stack_type;
4802
4803 if (*p != NUL && *p != '|' && *p != '\n')
4804 {
4805 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004806 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807 return NULL;
4808
4809 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4810 if (set_return_type)
4811 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004812 else
4813 {
4814 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4815 && stack_type->tt_type != VAR_VOID
4816 && stack_type->tt_type != VAR_UNKNOWN)
4817 {
4818 emsg(_("E1096: Returning a value in a function without a return type"));
4819 return NULL;
4820 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004821 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4822 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 }
4826 else
4827 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004828 // "set_return_type" cannot be TRUE, only used for a lambda which
4829 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004830 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4831 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 {
4833 emsg(_("E1003: Missing return value"));
4834 return NULL;
4835 }
4836
4837 // No argument, return zero.
4838 generate_PUSHNR(cctx, 0);
4839 }
4840
4841 if (generate_instr(cctx, ISN_RETURN) == NULL)
4842 return NULL;
4843
4844 // "return val | endif" is possible
4845 return skipwhite(p);
4846}
4847
4848/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004849 * Get a line from the compilation context, compatible with exarg_T getline().
4850 * Return a pointer to the line in allocated memory.
4851 * Return NULL for end-of-file or some error.
4852 */
4853 static char_u *
4854exarg_getline(
4855 int c UNUSED,
4856 void *cookie,
4857 int indent UNUSED,
4858 int do_concat UNUSED)
4859{
4860 cctx_T *cctx = (cctx_T *)cookie;
4861
4862 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4863 {
4864 iemsg("Heredoc got to end");
4865 return NULL;
4866 }
4867 ++cctx->ctx_lnum;
4868 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4869 [cctx->ctx_lnum]);
4870}
4871
4872/*
4873 * Compile a nested :def command.
4874 */
4875 static char_u *
4876compile_nested_function(exarg_T *eap, cctx_T *cctx)
4877{
4878 char_u *name_start = eap->arg;
4879 char_u *name_end = to_name_end(eap->arg, FALSE);
4880 char_u *name = get_lambda_name();
4881 lvar_T *lvar;
4882 ufunc_T *ufunc;
4883
4884 eap->arg = name_end;
4885 eap->getline = exarg_getline;
4886 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004887 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004888 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004889 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004890
Bram Moolenaar822ba242020-05-24 23:00:18 +02004891 if (ufunc == NULL)
4892 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004893 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004894 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004895 return NULL;
4896
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004897 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004898 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004899 TRUE, ufunc->uf_func_type);
4900
4901 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4902 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4903 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004904
Bram Moolenaar61a89812020-05-07 16:58:17 +02004905 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004906 return (char_u *)"";
4907}
4908
4909/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 * Return the length of an assignment operator, or zero if there isn't one.
4911 */
4912 int
4913assignment_len(char_u *p, int *heredoc)
4914{
4915 if (*p == '=')
4916 {
4917 if (p[1] == '<' && p[2] == '<')
4918 {
4919 *heredoc = TRUE;
4920 return 3;
4921 }
4922 return 1;
4923 }
4924 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4925 return 2;
4926 if (STRNCMP(p, "..=", 3) == 0)
4927 return 3;
4928 return 0;
4929}
4930
4931// words that cannot be used as a variable
4932static char *reserved[] = {
4933 "true",
4934 "false",
4935 NULL
4936};
4937
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004938typedef enum {
4939 dest_local,
4940 dest_option,
4941 dest_env,
4942 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004943 dest_buffer,
4944 dest_window,
4945 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004946 dest_vimvar,
4947 dest_script,
4948 dest_reg,
4949} assign_dest_T;
4950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004952 * Generate the load instruction for "name".
4953 */
4954 static void
4955generate_loadvar(
4956 cctx_T *cctx,
4957 assign_dest_T dest,
4958 char_u *name,
4959 lvar_T *lvar,
4960 type_T *type)
4961{
4962 switch (dest)
4963 {
4964 case dest_option:
4965 // TODO: check the option exists
4966 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4967 break;
4968 case dest_global:
4969 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4970 break;
4971 case dest_buffer:
4972 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4973 break;
4974 case dest_window:
4975 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4976 break;
4977 case dest_tab:
4978 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4979 break;
4980 case dest_script:
4981 compile_load_scriptvar(cctx,
4982 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4983 break;
4984 case dest_env:
4985 // Include $ in the name here
4986 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4987 break;
4988 case dest_reg:
4989 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4990 break;
4991 case dest_vimvar:
4992 generate_LOADV(cctx, name + 2, TRUE);
4993 break;
4994 case dest_local:
4995 if (lvar->lv_from_outer)
4996 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4997 NULL, type);
4998 else
4999 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5000 break;
5001 }
5002}
5003
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005004 void
5005vim9_declare_error(char_u *name)
5006{
5007 char *scope = "";
5008
5009 switch (*name)
5010 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005011 case 'g': scope = _("global"); break;
5012 case 'b': scope = _("buffer"); break;
5013 case 'w': scope = _("window"); break;
5014 case 't': scope = _("tab"); break;
5015 case 'v': scope = "v:"; break;
5016 case '$': semsg(_(e_declare_env_var), name); return;
5017 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005018 }
5019 semsg(_(e_declare_var), scope, name);
5020}
5021
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005022/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005023 * Compile declaration and assignment:
5024 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005026 * Return NULL for an error.
5027 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 */
5029 static char_u *
5030compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5031{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005032 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005034 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 char_u *ret = NULL;
5036 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005037 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005040 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 int oplen = 0;
5043 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005044 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005045 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005046 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005050 // Skip over the "var" or "[var, var]" to get to any "=".
5051 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5052 if (p == NULL)
5053 return *arg == '[' ? arg : NULL;
5054
5055 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005057 // TODO: should we allow this, and figure out type inference from list
5058 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 return NULL;
5061 }
5062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063 sp = p;
5064 p = skipwhite(p);
5065 op = p;
5066 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005067
5068 if (var_count > 0 && oplen == 0)
5069 // can be something like "[1, 2]->func()"
5070 return arg;
5071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5073 {
5074 char_u buf[4];
5075
5076 vim_strncpy(buf, op, oplen);
5077 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005078 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005079 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 if (heredoc)
5082 {
5083 list_T *l;
5084 listitem_T *li;
5085
5086 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005087 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005089 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090
5091 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005092 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 {
5094 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5095 li->li_tv.vval.v_string = NULL;
5096 }
5097 generate_NEWLIST(cctx, l->lv_len);
5098 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005099 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 list_free(l);
5101 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005102 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005104 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005106 // for "[var, var] = expr" evaluate the expression here, loop over the
5107 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005108
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005109 p = skipwhite(op + oplen);
5110 if (compile_expr0(&p, cctx) == FAIL)
5111 return NULL;
5112 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005114 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005116 type_T *stacktype;
5117
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005118 stacktype = stack->ga_len == 0 ? &t_void
5119 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005120 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005122 emsg(_(e_cannot_use_void));
5123 goto theend;
5124 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005125 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005126 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005127 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005128 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5129 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 }
5131 }
5132
5133 /*
5134 * Loop over variables in "[var, var] = expr".
5135 * For "var = expr" and "let var: type" this is done only once.
5136 */
5137 if (var_count > 0)
5138 var_start = skipwhite(arg + 1); // skip over the "["
5139 else
5140 var_start = arg;
5141 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5142 {
5143 char_u *var_end = skip_var_one(var_start, FALSE);
5144 size_t varlen;
5145 int new_local = FALSE;
5146 int opt_type;
5147 int opt_flags = 0;
5148 assign_dest_T dest = dest_local;
5149 int vimvaridx = -1;
5150 lvar_T *lvar = NULL;
5151 lvar_T arg_lvar;
5152 int has_type = FALSE;
5153 int has_index = FALSE;
5154 int instr_count = -1;
5155
5156 p = (*var_start == '&' || *var_start == '$'
5157 || *var_start == '@') ? var_start + 1 : var_start;
5158 p = to_name_end(p, TRUE);
5159
5160 // "a: type" is declaring variable "a" with a type, not "a:".
5161 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5162 --var_end;
5163 if (is_decl && p == var_start + 2 && p[-1] == ':')
5164 --p;
5165
5166 varlen = p - var_start;
5167 vim_free(name);
5168 name = vim_strnsave(var_start, varlen);
5169 if (name == NULL)
5170 return NULL;
5171 if (!heredoc)
5172 type = &t_any;
5173
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005174 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005175 {
5176 if (*var_start == '&')
5177 {
5178 int cc;
5179 long numval;
5180
5181 dest = dest_option;
5182 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005184 emsg(_(e_const_option));
5185 goto theend;
5186 }
5187 if (is_decl)
5188 {
5189 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5190 goto theend;
5191 }
5192 p = var_start;
5193 p = find_option_end(&p, &opt_flags);
5194 if (p == NULL)
5195 {
5196 // cannot happen?
5197 emsg(_(e_letunexp));
5198 goto theend;
5199 }
5200 cc = *p;
5201 *p = NUL;
5202 opt_type = get_option_value(var_start + 1, &numval,
5203 NULL, opt_flags);
5204 *p = cc;
5205 if (opt_type == -3)
5206 {
5207 semsg(_(e_unknown_option), var_start);
5208 goto theend;
5209 }
5210 if (opt_type == -2 || opt_type == 0)
5211 type = &t_string;
5212 else
5213 type = &t_number; // both number and boolean option
5214 }
5215 else if (*var_start == '$')
5216 {
5217 dest = dest_env;
5218 type = &t_string;
5219 if (is_decl)
5220 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005221 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005222 goto theend;
5223 }
5224 }
5225 else if (*var_start == '@')
5226 {
5227 if (!valid_yank_reg(var_start[1], TRUE))
5228 {
5229 emsg_invreg(var_start[1]);
5230 goto theend;
5231 }
5232 dest = dest_reg;
5233 type = &t_string;
5234 if (is_decl)
5235 {
5236 semsg(_("E1066: Cannot declare a register: %s"), name);
5237 goto theend;
5238 }
5239 }
5240 else if (STRNCMP(var_start, "g:", 2) == 0)
5241 {
5242 dest = dest_global;
5243 if (is_decl)
5244 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005245 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005246 goto theend;
5247 }
5248 }
5249 else if (STRNCMP(var_start, "b:", 2) == 0)
5250 {
5251 dest = dest_buffer;
5252 if (is_decl)
5253 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005254 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005255 goto theend;
5256 }
5257 }
5258 else if (STRNCMP(var_start, "w:", 2) == 0)
5259 {
5260 dest = dest_window;
5261 if (is_decl)
5262 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005263 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005264 goto theend;
5265 }
5266 }
5267 else if (STRNCMP(var_start, "t:", 2) == 0)
5268 {
5269 dest = dest_tab;
5270 if (is_decl)
5271 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005272 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005273 goto theend;
5274 }
5275 }
5276 else if (STRNCMP(var_start, "v:", 2) == 0)
5277 {
5278 typval_T *vtv;
5279 int di_flags;
5280
5281 vimvaridx = find_vim_var(name + 2, &di_flags);
5282 if (vimvaridx < 0)
5283 {
5284 semsg(_(e_var_notfound), var_start);
5285 goto theend;
5286 }
5287 // We use the current value of "sandbox" here, is that OK?
5288 if (var_check_ro(di_flags, name, FALSE))
5289 goto theend;
5290 dest = dest_vimvar;
5291 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005292 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005293 if (is_decl)
5294 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005295 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005296 goto theend;
5297 }
5298 }
5299 else
5300 {
5301 int idx;
5302
5303 for (idx = 0; reserved[idx] != NULL; ++idx)
5304 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005305 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005306 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005307 goto theend;
5308 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005309
5310 lvar = lookup_local(var_start, varlen, cctx);
5311 if (lvar == NULL)
5312 {
5313 CLEAR_FIELD(arg_lvar);
5314 if (lookup_arg(var_start, varlen,
5315 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5316 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005317 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005318 if (is_decl)
5319 {
5320 semsg(_(e_used_as_arg), name);
5321 goto theend;
5322 }
5323 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005324 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005325 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005326 if (lvar != NULL)
5327 {
5328 if (is_decl)
5329 {
5330 semsg(_("E1017: Variable already declared: %s"), name);
5331 goto theend;
5332 }
5333 else if (lvar->lv_const)
5334 {
5335 semsg(_("E1018: Cannot assign to a constant: %s"),
5336 name);
5337 goto theend;
5338 }
5339 }
5340 else if (STRNCMP(var_start, "s:", 2) == 0
5341 || lookup_script(var_start, varlen) == OK
5342 || find_imported(var_start, varlen, cctx) != NULL)
5343 {
5344 dest = dest_script;
5345 if (is_decl)
5346 {
5347 semsg(_("E1054: Variable already declared in the script: %s"),
5348 name);
5349 goto theend;
5350 }
5351 }
5352 else if (name[1] == ':' && name[2] != NUL)
5353 {
5354 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5355 name);
5356 goto theend;
5357 }
5358 else if (!is_decl)
5359 {
5360 semsg(_("E1089: unknown variable: %s"), name);
5361 goto theend;
5362 }
5363 }
5364 }
5365
5366 // handle "a:name" as a name, not index "name" on "a"
5367 if (varlen > 1 || var_start[varlen] != ':')
5368 p = var_end;
5369
5370 if (dest != dest_option)
5371 {
5372 if (is_decl && *p == ':')
5373 {
5374 // parse optional type: "let var: type = expr"
5375 if (!VIM_ISWHITE(p[1]))
5376 {
5377 semsg(_(e_white_after), ":");
5378 goto theend;
5379 }
5380 p = skipwhite(p + 1);
5381 type = parse_type(&p, cctx->ctx_type_list);
5382 has_type = TRUE;
5383 }
5384 else if (lvar != NULL)
5385 type = lvar->lv_type;
5386 }
5387
5388 if (oplen == 3 && !heredoc && dest != dest_global
5389 && type->tt_type != VAR_STRING
5390 && type->tt_type != VAR_ANY)
5391 {
5392 emsg(_("E1019: Can only concatenate to string"));
5393 goto theend;
5394 }
5395
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005396 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005397 {
5398 if (oplen > 1 && !heredoc)
5399 {
5400 // +=, /=, etc. require an existing variable
5401 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5402 name);
5403 goto theend;
5404 }
5405
5406 // new local variable
5407 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5408 goto theend;
5409 lvar = reserve_local(cctx, var_start, varlen,
5410 cmdidx == CMD_const, type);
5411 if (lvar == NULL)
5412 goto theend;
5413 new_local = TRUE;
5414 }
5415
5416 member_type = type;
5417 if (var_end > var_start + varlen)
5418 {
5419 // Something follows after the variable: "var[idx]".
5420 if (is_decl)
5421 {
5422 emsg(_("E1087: cannot use an index when declaring a variable"));
5423 goto theend;
5424 }
5425
5426 if (var_start[varlen] == '[')
5427 {
5428 has_index = TRUE;
5429 if (type->tt_member == NULL)
5430 {
5431 semsg(_("E1088: cannot use an index on %s"), name);
5432 goto theend;
5433 }
5434 member_type = type->tt_member;
5435 }
5436 else
5437 {
5438 semsg("Not supported yet: %s", var_start);
5439 goto theend;
5440 }
5441 }
5442 else if (lvar == &arg_lvar)
5443 {
5444 semsg(_("E1090: Cannot assign to argument %s"), name);
5445 goto theend;
5446 }
5447
5448 if (!heredoc)
5449 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005450 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005451 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005452 if (oplen > 0 && var_count == 0)
5453 {
5454 // skip over the "=" and the expression
5455 p = skipwhite(op + oplen);
5456 compile_expr0(&p, cctx);
5457 }
5458 }
5459 else if (oplen > 0)
5460 {
5461 type_T *stacktype;
5462
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005463 // For "var = expr" evaluate the expression.
5464 if (var_count == 0)
5465 {
5466 int r;
5467
5468 // for "+=", "*=", "..=" etc. first load the current value
5469 if (*op != '=')
5470 {
5471 generate_loadvar(cctx, dest, name, lvar, type);
5472
5473 if (has_index)
5474 {
5475 // TODO: get member from list or dict
5476 emsg("Index with operation not supported yet");
5477 goto theend;
5478 }
5479 }
5480
5481 // Compile the expression. Temporarily hide the new local
5482 // variable here, it is not available to this expression.
5483 if (new_local)
5484 --cctx->ctx_locals.ga_len;
5485 instr_count = instr->ga_len;
5486 p = skipwhite(op + oplen);
5487 r = compile_expr0(&p, cctx);
5488 if (new_local)
5489 ++cctx->ctx_locals.ga_len;
5490 if (r == FAIL)
5491 goto theend;
5492 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005493 else if (semicolon && var_idx == var_count - 1)
5494 {
5495 // For "[var; var] = expr" get the rest of the list
5496 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5497 goto theend;
5498 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005499 else
5500 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005501 // For "[var, var] = expr" get the "var_idx" item from the
5502 // list.
5503 if (generate_GETITEM(cctx, var_idx) == FAIL)
5504 return FAIL;
5505 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005506
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005507 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005508 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005509 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005510 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005511 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005512 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005513 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005514 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005515 emsg(_(e_cannot_use_void));
5516 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005517 }
5518 else
5519 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005520 // An empty list or dict has a &t_void member,
5521 // for a variable that implies &t_any.
5522 if (stacktype == &t_list_empty)
5523 lvar->lv_type = &t_list_any;
5524 else if (stacktype == &t_dict_empty)
5525 lvar->lv_type = &t_dict_any;
5526 else
5527 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005528 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005529 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005530 else
5531 {
5532 type_T *use_type = lvar->lv_type;
5533
5534 if (has_index)
5535 {
5536 use_type = use_type->tt_member;
5537 if (use_type == NULL)
5538 use_type = &t_void;
5539 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005540 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005541 == FAIL)
5542 goto theend;
5543 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005544 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005545 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005546 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005547 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005549 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005551 emsg(_(e_const_req_value));
5552 goto theend;
5553 }
5554 else if (!has_type || dest == dest_option)
5555 {
5556 emsg(_(e_type_req));
5557 goto theend;
5558 }
5559 else
5560 {
5561 // variables are always initialized
5562 if (ga_grow(instr, 1) == FAIL)
5563 goto theend;
5564 switch (member_type->tt_type)
5565 {
5566 case VAR_BOOL:
5567 generate_PUSHBOOL(cctx, VVAL_FALSE);
5568 break;
5569 case VAR_FLOAT:
5570#ifdef FEAT_FLOAT
5571 generate_PUSHF(cctx, 0.0);
5572#endif
5573 break;
5574 case VAR_STRING:
5575 generate_PUSHS(cctx, NULL);
5576 break;
5577 case VAR_BLOB:
5578 generate_PUSHBLOB(cctx, NULL);
5579 break;
5580 case VAR_FUNC:
5581 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5582 break;
5583 case VAR_LIST:
5584 generate_NEWLIST(cctx, 0);
5585 break;
5586 case VAR_DICT:
5587 generate_NEWDICT(cctx, 0);
5588 break;
5589 case VAR_JOB:
5590 generate_PUSHJOB(cctx, NULL);
5591 break;
5592 case VAR_CHANNEL:
5593 generate_PUSHCHANNEL(cctx, NULL);
5594 break;
5595 case VAR_NUMBER:
5596 case VAR_UNKNOWN:
5597 case VAR_ANY:
5598 case VAR_PARTIAL:
5599 case VAR_VOID:
5600 case VAR_SPECIAL: // cannot happen
5601 generate_PUSHNR(cctx, 0);
5602 break;
5603 }
5604 }
5605 if (var_count == 0)
5606 end = p;
5607 }
5608
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005609 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005610 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005611 break;
5612
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005613 if (oplen > 0 && *op != '=')
5614 {
5615 type_T *expected = &t_number;
5616 type_T *stacktype;
5617
5618 // TODO: if type is known use float or any operation
5619
5620 if (*op == '.')
5621 expected = &t_string;
5622 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005623 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005624 goto theend;
5625
5626 if (*op == '.')
5627 generate_instr_drop(cctx, ISN_CONCAT, 1);
5628 else
5629 {
5630 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5631
5632 if (isn == NULL)
5633 goto theend;
5634 switch (*op)
5635 {
5636 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5637 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5638 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5639 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5640 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642 }
5643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005645 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005646 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005647 int r;
5648
5649 // Compile the "idx" in "var[idx]".
5650 if (new_local)
5651 --cctx->ctx_locals.ga_len;
5652 p = skipwhite(var_start + varlen + 1);
5653 r = compile_expr0(&p, cctx);
5654 if (new_local)
5655 ++cctx->ctx_locals.ga_len;
5656 if (r == FAIL)
5657 goto theend;
5658 if (*skipwhite(p) != ']')
5659 {
5660 emsg(_(e_missbrac));
5661 goto theend;
5662 }
5663 if (type->tt_type == VAR_DICT
5664 && may_generate_2STRING(-1, cctx) == FAIL)
5665 goto theend;
5666 if (type->tt_type == VAR_LIST
5667 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005668 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005669 {
5670 emsg(_(e_number_exp));
5671 goto theend;
5672 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005673
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005674 // Load the dict or list. On the stack we then have:
5675 // - value
5676 // - index
5677 // - variable
5678 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005679
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005680 if (type->tt_type == VAR_LIST)
5681 {
5682 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5683 return FAIL;
5684 }
5685 else if (type->tt_type == VAR_DICT)
5686 {
5687 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5688 return FAIL;
5689 }
5690 else
5691 {
5692 emsg(_(e_listreq));
5693 goto theend;
5694 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005695 }
5696 else
5697 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005698 switch (dest)
5699 {
5700 case dest_option:
5701 generate_STOREOPT(cctx, name + 1, opt_flags);
5702 break;
5703 case dest_global:
5704 // include g: with the name, easier to execute that way
5705 generate_STORE(cctx, ISN_STOREG, 0, name);
5706 break;
5707 case dest_buffer:
5708 // include b: with the name, easier to execute that way
5709 generate_STORE(cctx, ISN_STOREB, 0, name);
5710 break;
5711 case dest_window:
5712 // include w: with the name, easier to execute that way
5713 generate_STORE(cctx, ISN_STOREW, 0, name);
5714 break;
5715 case dest_tab:
5716 // include t: with the name, easier to execute that way
5717 generate_STORE(cctx, ISN_STORET, 0, name);
5718 break;
5719 case dest_env:
5720 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5721 break;
5722 case dest_reg:
5723 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5724 break;
5725 case dest_vimvar:
5726 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5727 break;
5728 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005729 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005730 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5731 imported_T *import = NULL;
5732 int sid = current_sctx.sc_sid;
5733 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005734
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005735 if (name[1] != ':')
5736 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005737 import = find_imported(name, 0, cctx);
5738 if (import != NULL)
5739 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005740 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005741
5742 idx = get_script_item_idx(sid, rawname, TRUE);
5743 // TODO: specific type
5744 if (idx < 0)
5745 {
5746 char_u *name_s = name;
5747
5748 // Include s: in the name for store_var()
5749 if (name[1] != ':')
5750 {
5751 int len = (int)STRLEN(name) + 3;
5752
5753 name_s = alloc(len);
5754 if (name_s == NULL)
5755 name_s = name;
5756 else
5757 vim_snprintf((char *)name_s, len,
5758 "s:%s", name);
5759 }
5760 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005761 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005762 if (name_s != name)
5763 vim_free(name_s);
5764 }
5765 else
5766 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005767 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005768 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005769 break;
5770 case dest_local:
5771 if (lvar != NULL)
5772 {
5773 isn_T *isn = ((isn_T *)instr->ga_data)
5774 + instr->ga_len - 1;
5775
5776 // optimization: turn "var = 123" from ISN_PUSHNR +
5777 // ISN_STORE into ISN_STORENR
5778 if (!lvar->lv_from_outer
5779 && instr->ga_len == instr_count + 1
5780 && isn->isn_type == ISN_PUSHNR)
5781 {
5782 varnumber_T val = isn->isn_arg.number;
5783
5784 isn->isn_type = ISN_STORENR;
5785 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5786 isn->isn_arg.storenr.stnr_val = val;
5787 if (stack->ga_len > 0)
5788 --stack->ga_len;
5789 }
5790 else if (lvar->lv_from_outer)
5791 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005792 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005793 else
5794 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5795 }
5796 break;
5797 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005798 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005799
5800 if (var_idx + 1 < var_count)
5801 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005802 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005803
5804 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005805 if (var_count > 0 && !semicolon)
5806 {
5807 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5808 goto theend;
5809 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005810
Bram Moolenaarb2097502020-07-19 17:17:02 +02005811 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005812
5813theend:
5814 vim_free(name);
5815 return ret;
5816}
5817
5818/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005819 * Check if "name" can be "unlet".
5820 */
5821 int
5822check_vim9_unlet(char_u *name)
5823{
5824 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5825 {
5826 semsg(_("E1081: Cannot unlet %s"), name);
5827 return FAIL;
5828 }
5829 return OK;
5830}
5831
5832/*
5833 * Callback passed to ex_unletlock().
5834 */
5835 static int
5836compile_unlet(
5837 lval_T *lvp,
5838 char_u *name_end,
5839 exarg_T *eap,
5840 int deep UNUSED,
5841 void *coookie)
5842{
5843 cctx_T *cctx = coookie;
5844
5845 if (lvp->ll_tv == NULL)
5846 {
5847 char_u *p = lvp->ll_name;
5848 int cc = *name_end;
5849 int ret = OK;
5850
5851 // Normal name. Only supports g:, w:, t: and b: namespaces.
5852 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005853 if (*p == '$')
5854 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5855 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005856 ret = FAIL;
5857 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005858 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005859
5860 *name_end = cc;
5861 return ret;
5862 }
5863
5864 // TODO: unlet {list}[idx]
5865 // TODO: unlet {dict}[key]
5866 emsg("Sorry, :unlet not fully implemented yet");
5867 return FAIL;
5868}
5869
5870/*
5871 * compile "unlet var", "lock var" and "unlock var"
5872 * "arg" points to "var".
5873 */
5874 static char_u *
5875compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5876{
5877 char_u *p = arg;
5878
5879 if (eap->cmdidx != CMD_unlet)
5880 {
5881 emsg("Sorry, :lock and unlock not implemented yet");
5882 return NULL;
5883 }
5884
5885 if (*p == '!')
5886 {
5887 p = skipwhite(p + 1);
5888 eap->forceit = TRUE;
5889 }
5890
5891 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5892 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5893}
5894
5895/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896 * Compile an :import command.
5897 */
5898 static char_u *
5899compile_import(char_u *arg, cctx_T *cctx)
5900{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005901 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902}
5903
5904/*
5905 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5906 */
5907 static int
5908compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5909{
5910 garray_T *instr = &cctx->ctx_instr;
5911 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5912
5913 if (endlabel == NULL)
5914 return FAIL;
5915 endlabel->el_next = *el;
5916 *el = endlabel;
5917 endlabel->el_end_label = instr->ga_len;
5918
5919 generate_JUMP(cctx, when, 0);
5920 return OK;
5921}
5922
5923 static void
5924compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5925{
5926 garray_T *instr = &cctx->ctx_instr;
5927
5928 while (*el != NULL)
5929 {
5930 endlabel_T *cur = (*el);
5931 isn_T *isn;
5932
5933 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5934 isn->isn_arg.jump.jump_where = instr->ga_len;
5935 *el = cur->el_next;
5936 vim_free(cur);
5937 }
5938}
5939
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005940 static void
5941compile_free_jump_to_end(endlabel_T **el)
5942{
5943 while (*el != NULL)
5944 {
5945 endlabel_T *cur = (*el);
5946
5947 *el = cur->el_next;
5948 vim_free(cur);
5949 }
5950}
5951
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005952/*
5953 * Create a new scope and set up the generic items.
5954 */
5955 static scope_T *
5956new_scope(cctx_T *cctx, scopetype_T type)
5957{
5958 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5959
5960 if (scope == NULL)
5961 return NULL;
5962 scope->se_outer = cctx->ctx_scope;
5963 cctx->ctx_scope = scope;
5964 scope->se_type = type;
5965 scope->se_local_count = cctx->ctx_locals.ga_len;
5966 return scope;
5967}
5968
5969/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005970 * Free the current scope and go back to the outer scope.
5971 */
5972 static void
5973drop_scope(cctx_T *cctx)
5974{
5975 scope_T *scope = cctx->ctx_scope;
5976
5977 if (scope == NULL)
5978 {
5979 iemsg("calling drop_scope() without a scope");
5980 return;
5981 }
5982 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005983 switch (scope->se_type)
5984 {
5985 case IF_SCOPE:
5986 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5987 case FOR_SCOPE:
5988 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5989 case WHILE_SCOPE:
5990 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5991 case TRY_SCOPE:
5992 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5993 case NO_SCOPE:
5994 case BLOCK_SCOPE:
5995 break;
5996 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005997 vim_free(scope);
5998}
5999
6000/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001 * compile "if expr"
6002 *
6003 * "if expr" Produces instructions:
6004 * EVAL expr Push result of "expr"
6005 * JUMP_IF_FALSE end
6006 * ... body ...
6007 * end:
6008 *
6009 * "if expr | else" Produces instructions:
6010 * EVAL expr Push result of "expr"
6011 * JUMP_IF_FALSE else
6012 * ... body ...
6013 * JUMP_ALWAYS end
6014 * else:
6015 * ... body ...
6016 * end:
6017 *
6018 * "if expr1 | elseif expr2 | else" Produces instructions:
6019 * EVAL expr Push result of "expr"
6020 * JUMP_IF_FALSE elseif
6021 * ... body ...
6022 * JUMP_ALWAYS end
6023 * elseif:
6024 * EVAL expr Push result of "expr"
6025 * JUMP_IF_FALSE else
6026 * ... body ...
6027 * JUMP_ALWAYS end
6028 * else:
6029 * ... body ...
6030 * end:
6031 */
6032 static char_u *
6033compile_if(char_u *arg, cctx_T *cctx)
6034{
6035 char_u *p = arg;
6036 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006037 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006039 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006040 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006041
Bram Moolenaara5565e42020-05-09 15:44:01 +02006042 CLEAR_FIELD(ppconst);
6043 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006044 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006045 clear_ppconst(&ppconst);
6046 return NULL;
6047 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006048 if (cctx->ctx_skip == SKIP_YES)
6049 clear_ppconst(&ppconst);
6050 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006051 {
6052 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006053 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006054 clear_ppconst(&ppconst);
6055 }
6056 else
6057 {
6058 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006059 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006060 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006061 return NULL;
6062 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063
6064 scope = new_scope(cctx, IF_SCOPE);
6065 if (scope == NULL)
6066 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006067 scope->se_skip_save = skip_save;
6068 // "is_had_return" will be reset if any block does not end in :return
6069 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006070
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006071 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006072 {
6073 // "where" is set when ":elseif", "else" or ":endif" is found
6074 scope->se_u.se_if.is_if_label = instr->ga_len;
6075 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6076 }
6077 else
6078 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006079
6080 return p;
6081}
6082
6083 static char_u *
6084compile_elseif(char_u *arg, cctx_T *cctx)
6085{
6086 char_u *p = arg;
6087 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006088 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006089 isn_T *isn;
6090 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006091 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006092
6093 if (scope == NULL || scope->se_type != IF_SCOPE)
6094 {
6095 emsg(_(e_elseif_without_if));
6096 return NULL;
6097 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006098 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006099 if (!cctx->ctx_had_return)
6100 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006101
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006102 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006103 {
6104 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006105 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006106 return NULL;
6107 // previous "if" or "elseif" jumps here
6108 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6109 isn->isn_arg.jump.jump_where = instr->ga_len;
6110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006112 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006113 CLEAR_FIELD(ppconst);
6114 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006115 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006116 clear_ppconst(&ppconst);
6117 return NULL;
6118 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006119 if (scope->se_skip_save == SKIP_YES)
6120 clear_ppconst(&ppconst);
6121 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006122 {
6123 // The expression results in a constant.
6124 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006125 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006126 clear_ppconst(&ppconst);
6127 scope->se_u.se_if.is_if_label = -1;
6128 }
6129 else
6130 {
6131 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006132 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006133 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006134 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006136 // "where" is set when ":elseif", "else" or ":endif" is found
6137 scope->se_u.se_if.is_if_label = instr->ga_len;
6138 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006140
6141 return p;
6142}
6143
6144 static char_u *
6145compile_else(char_u *arg, cctx_T *cctx)
6146{
6147 char_u *p = arg;
6148 garray_T *instr = &cctx->ctx_instr;
6149 isn_T *isn;
6150 scope_T *scope = cctx->ctx_scope;
6151
6152 if (scope == NULL || scope->se_type != IF_SCOPE)
6153 {
6154 emsg(_(e_else_without_if));
6155 return NULL;
6156 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006157 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006158 if (!cctx->ctx_had_return)
6159 scope->se_u.se_if.is_had_return = FALSE;
6160 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006161
Bram Moolenaarefd88552020-06-18 20:50:10 +02006162 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006163 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006164 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006165 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006166 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006167 if (!cctx->ctx_had_return
6168 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6169 JUMP_ALWAYS, cctx) == FAIL)
6170 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006171 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006172
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006173 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006174 {
6175 if (scope->se_u.se_if.is_if_label >= 0)
6176 {
6177 // previous "if" or "elseif" jumps here
6178 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6179 isn->isn_arg.jump.jump_where = instr->ga_len;
6180 scope->se_u.se_if.is_if_label = -1;
6181 }
6182 }
6183
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006184 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006185 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187
6188 return p;
6189}
6190
6191 static char_u *
6192compile_endif(char_u *arg, cctx_T *cctx)
6193{
6194 scope_T *scope = cctx->ctx_scope;
6195 ifscope_T *ifscope;
6196 garray_T *instr = &cctx->ctx_instr;
6197 isn_T *isn;
6198
6199 if (scope == NULL || scope->se_type != IF_SCOPE)
6200 {
6201 emsg(_(e_endif_without_if));
6202 return NULL;
6203 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006204 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006205 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006206 if (!cctx->ctx_had_return)
6207 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006208
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006209 if (scope->se_u.se_if.is_if_label >= 0)
6210 {
6211 // previous "if" or "elseif" jumps here
6212 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6213 isn->isn_arg.jump.jump_where = instr->ga_len;
6214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006215 // Fill in the "end" label in jumps at the end of the blocks.
6216 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006217 cctx->ctx_skip = scope->se_skip_save;
6218
6219 // If all the blocks end in :return and there is an :else then the
6220 // had_return flag is set.
6221 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006223 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224 return arg;
6225}
6226
6227/*
6228 * compile "for var in expr"
6229 *
6230 * Produces instructions:
6231 * PUSHNR -1
6232 * STORE loop-idx Set index to -1
6233 * EVAL expr Push result of "expr"
6234 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6235 * - if beyond end, jump to "end"
6236 * - otherwise get item from list and push it
6237 * STORE var Store item in "var"
6238 * ... body ...
6239 * JUMP top Jump back to repeat
6240 * end: DROP Drop the result of "expr"
6241 *
6242 */
6243 static char_u *
6244compile_for(char_u *arg, cctx_T *cctx)
6245{
6246 char_u *p;
6247 size_t varlen;
6248 garray_T *instr = &cctx->ctx_instr;
6249 garray_T *stack = &cctx->ctx_type_stack;
6250 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006251 lvar_T *loop_lvar; // loop iteration variable
6252 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 type_T *vartype;
6254
6255 // TODO: list of variables: "for [key, value] in dict"
6256 // parse "var"
6257 for (p = arg; eval_isnamec1(*p); ++p)
6258 ;
6259 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006260 var_lvar = lookup_local(arg, varlen, cctx);
6261 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262 {
6263 semsg(_("E1023: variable already defined: %s"), arg);
6264 return NULL;
6265 }
6266
6267 // consume "in"
6268 p = skipwhite(p);
6269 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6270 {
6271 emsg(_(e_missing_in));
6272 return NULL;
6273 }
6274 p = skipwhite(p + 2);
6275
6276
6277 scope = new_scope(cctx, FOR_SCOPE);
6278 if (scope == NULL)
6279 return NULL;
6280
6281 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006282 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6283 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006284 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006285 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006286 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289
6290 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006291 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6292 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006293 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006294 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006295 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006299 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006300
6301 // compile "expr", it remains on the stack until "endfor"
6302 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006303 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006304 {
6305 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006309 // Now that we know the type of "var", check that it is a list, now or at
6310 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006312 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006314 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 return NULL;
6316 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006317 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006318 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319
6320 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006321 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006323 generate_FOR(cctx, loop_lvar->lv_idx);
6324 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325
6326 return arg;
6327}
6328
6329/*
6330 * compile "endfor"
6331 */
6332 static char_u *
6333compile_endfor(char_u *arg, cctx_T *cctx)
6334{
6335 garray_T *instr = &cctx->ctx_instr;
6336 scope_T *scope = cctx->ctx_scope;
6337 forscope_T *forscope;
6338 isn_T *isn;
6339
6340 if (scope == NULL || scope->se_type != FOR_SCOPE)
6341 {
6342 emsg(_(e_for));
6343 return NULL;
6344 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006345 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006347 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348
6349 // At end of ":for" scope jump back to the FOR instruction.
6350 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6351
6352 // Fill in the "end" label in the FOR statement so it can jump here
6353 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6354 isn->isn_arg.forloop.for_end = instr->ga_len;
6355
6356 // Fill in the "end" label any BREAK statements
6357 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6358
6359 // Below the ":for" scope drop the "expr" list from the stack.
6360 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6361 return NULL;
6362
6363 vim_free(scope);
6364
6365 return arg;
6366}
6367
6368/*
6369 * compile "while expr"
6370 *
6371 * Produces instructions:
6372 * top: EVAL expr Push result of "expr"
6373 * JUMP_IF_FALSE end jump if false
6374 * ... body ...
6375 * JUMP top Jump back to repeat
6376 * end:
6377 *
6378 */
6379 static char_u *
6380compile_while(char_u *arg, cctx_T *cctx)
6381{
6382 char_u *p = arg;
6383 garray_T *instr = &cctx->ctx_instr;
6384 scope_T *scope;
6385
6386 scope = new_scope(cctx, WHILE_SCOPE);
6387 if (scope == NULL)
6388 return NULL;
6389
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006390 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006391
6392 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006393 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394 return NULL;
6395
6396 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006397 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398 JUMP_IF_FALSE, cctx) == FAIL)
6399 return FAIL;
6400
6401 return p;
6402}
6403
6404/*
6405 * compile "endwhile"
6406 */
6407 static char_u *
6408compile_endwhile(char_u *arg, cctx_T *cctx)
6409{
6410 scope_T *scope = cctx->ctx_scope;
6411
6412 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6413 {
6414 emsg(_(e_while));
6415 return NULL;
6416 }
6417 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006418 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419
6420 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006421 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006422
6423 // Fill in the "end" label in the WHILE statement so it can jump here.
6424 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006425 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006426
6427 vim_free(scope);
6428
6429 return arg;
6430}
6431
6432/*
6433 * compile "continue"
6434 */
6435 static char_u *
6436compile_continue(char_u *arg, cctx_T *cctx)
6437{
6438 scope_T *scope = cctx->ctx_scope;
6439
6440 for (;;)
6441 {
6442 if (scope == NULL)
6443 {
6444 emsg(_(e_continue));
6445 return NULL;
6446 }
6447 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6448 break;
6449 scope = scope->se_outer;
6450 }
6451
6452 // Jump back to the FOR or WHILE instruction.
6453 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006454 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6455 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006456 return arg;
6457}
6458
6459/*
6460 * compile "break"
6461 */
6462 static char_u *
6463compile_break(char_u *arg, cctx_T *cctx)
6464{
6465 scope_T *scope = cctx->ctx_scope;
6466 endlabel_T **el;
6467
6468 for (;;)
6469 {
6470 if (scope == NULL)
6471 {
6472 emsg(_(e_break));
6473 return NULL;
6474 }
6475 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6476 break;
6477 scope = scope->se_outer;
6478 }
6479
6480 // Jump to the end of the FOR or WHILE loop.
6481 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006482 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006484 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006485 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6486 return FAIL;
6487
6488 return arg;
6489}
6490
6491/*
6492 * compile "{" start of block
6493 */
6494 static char_u *
6495compile_block(char_u *arg, cctx_T *cctx)
6496{
6497 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6498 return NULL;
6499 return skipwhite(arg + 1);
6500}
6501
6502/*
6503 * compile end of block: drop one scope
6504 */
6505 static void
6506compile_endblock(cctx_T *cctx)
6507{
6508 scope_T *scope = cctx->ctx_scope;
6509
6510 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006511 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006512 vim_free(scope);
6513}
6514
6515/*
6516 * compile "try"
6517 * Creates a new scope for the try-endtry, pointing to the first catch and
6518 * finally.
6519 * Creates another scope for the "try" block itself.
6520 * TRY instruction sets up exception handling at runtime.
6521 *
6522 * "try"
6523 * TRY -> catch1, -> finally push trystack entry
6524 * ... try block
6525 * "throw {exception}"
6526 * EVAL {exception}
6527 * THROW create exception
6528 * ... try block
6529 * " catch {expr}"
6530 * JUMP -> finally
6531 * catch1: PUSH exeception
6532 * EVAL {expr}
6533 * MATCH
6534 * JUMP nomatch -> catch2
6535 * CATCH remove exception
6536 * ... catch block
6537 * " catch"
6538 * JUMP -> finally
6539 * catch2: CATCH remove exception
6540 * ... catch block
6541 * " finally"
6542 * finally:
6543 * ... finally block
6544 * " endtry"
6545 * ENDTRY pop trystack entry, may rethrow
6546 */
6547 static char_u *
6548compile_try(char_u *arg, cctx_T *cctx)
6549{
6550 garray_T *instr = &cctx->ctx_instr;
6551 scope_T *try_scope;
6552 scope_T *scope;
6553
6554 // scope that holds the jumps that go to catch/finally/endtry
6555 try_scope = new_scope(cctx, TRY_SCOPE);
6556 if (try_scope == NULL)
6557 return NULL;
6558
6559 // "catch" is set when the first ":catch" is found.
6560 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006561 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 if (generate_instr(cctx, ISN_TRY) == NULL)
6563 return NULL;
6564
6565 // scope for the try block itself
6566 scope = new_scope(cctx, BLOCK_SCOPE);
6567 if (scope == NULL)
6568 return NULL;
6569
6570 return arg;
6571}
6572
6573/*
6574 * compile "catch {expr}"
6575 */
6576 static char_u *
6577compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6578{
6579 scope_T *scope = cctx->ctx_scope;
6580 garray_T *instr = &cctx->ctx_instr;
6581 char_u *p;
6582 isn_T *isn;
6583
6584 // end block scope from :try or :catch
6585 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6586 compile_endblock(cctx);
6587 scope = cctx->ctx_scope;
6588
6589 // Error if not in a :try scope
6590 if (scope == NULL || scope->se_type != TRY_SCOPE)
6591 {
6592 emsg(_(e_catch));
6593 return NULL;
6594 }
6595
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006596 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 {
6598 emsg(_("E1033: catch unreachable after catch-all"));
6599 return NULL;
6600 }
6601
6602 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006603 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 JUMP_ALWAYS, cctx) == FAIL)
6605 return NULL;
6606
6607 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006608 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006609 if (isn->isn_arg.try.try_catch == 0)
6610 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006611 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006612 {
6613 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006614 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 isn->isn_arg.jump.jump_where = instr->ga_len;
6616 }
6617
6618 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006619 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006621 scope->se_u.se_try.ts_caught_all = TRUE;
6622 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006623 }
6624 else
6625 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006626 char_u *end;
6627 char_u *pat;
6628 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006629 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006630 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632 // Push v:exception, push {expr} and MATCH
6633 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6634
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006635 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006636 if (*end != *p)
6637 {
6638 semsg(_("E1067: Separator mismatch: %s"), p);
6639 vim_free(tofree);
6640 return FAIL;
6641 }
6642 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006643 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006644 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006645 len = (int)(end - tofree);
6646 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006647 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006648 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006649 if (pat == NULL)
6650 return FAIL;
6651 if (generate_PUSHS(cctx, pat) == FAIL)
6652 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6655 return NULL;
6656
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006657 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006658 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6659 return NULL;
6660 }
6661
6662 if (generate_instr(cctx, ISN_CATCH) == NULL)
6663 return NULL;
6664
6665 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6666 return NULL;
6667 return p;
6668}
6669
6670 static char_u *
6671compile_finally(char_u *arg, cctx_T *cctx)
6672{
6673 scope_T *scope = cctx->ctx_scope;
6674 garray_T *instr = &cctx->ctx_instr;
6675 isn_T *isn;
6676
6677 // end block scope from :try or :catch
6678 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6679 compile_endblock(cctx);
6680 scope = cctx->ctx_scope;
6681
6682 // Error if not in a :try scope
6683 if (scope == NULL || scope->se_type != TRY_SCOPE)
6684 {
6685 emsg(_(e_finally));
6686 return NULL;
6687 }
6688
6689 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006690 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006691 if (isn->isn_arg.try.try_finally != 0)
6692 {
6693 emsg(_(e_finally_dup));
6694 return NULL;
6695 }
6696
6697 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006698 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699
Bram Moolenaar585fea72020-04-02 22:33:21 +02006700 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006701 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 {
6703 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006704 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006705 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006706 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006707 }
6708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709 // TODO: set index in ts_finally_label jumps
6710
6711 return arg;
6712}
6713
6714 static char_u *
6715compile_endtry(char_u *arg, cctx_T *cctx)
6716{
6717 scope_T *scope = cctx->ctx_scope;
6718 garray_T *instr = &cctx->ctx_instr;
6719 isn_T *isn;
6720
6721 // end block scope from :catch or :finally
6722 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6723 compile_endblock(cctx);
6724 scope = cctx->ctx_scope;
6725
6726 // Error if not in a :try scope
6727 if (scope == NULL || scope->se_type != TRY_SCOPE)
6728 {
6729 if (scope == NULL)
6730 emsg(_(e_no_endtry));
6731 else if (scope->se_type == WHILE_SCOPE)
6732 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006733 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 emsg(_(e_endfor));
6735 else
6736 emsg(_(e_endif));
6737 return NULL;
6738 }
6739
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006740 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006741 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6742 {
6743 emsg(_("E1032: missing :catch or :finally"));
6744 return NULL;
6745 }
6746
6747 // Fill in the "end" label in jumps at the end of the blocks, if not done
6748 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006749 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006750
6751 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006752 if (isn->isn_arg.try.try_catch == 0)
6753 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754 if (isn->isn_arg.try.try_finally == 0)
6755 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006756
6757 if (scope->se_u.se_try.ts_catch_label != 0)
6758 {
6759 // Last catch without match jumps here
6760 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6761 isn->isn_arg.jump.jump_where = instr->ga_len;
6762 }
6763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 compile_endblock(cctx);
6765
6766 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6767 return NULL;
6768 return arg;
6769}
6770
6771/*
6772 * compile "throw {expr}"
6773 */
6774 static char_u *
6775compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6776{
6777 char_u *p = skipwhite(arg);
6778
Bram Moolenaara5565e42020-05-09 15:44:01 +02006779 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 return NULL;
6781 if (may_generate_2STRING(-1, cctx) == FAIL)
6782 return NULL;
6783 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6784 return NULL;
6785
6786 return p;
6787}
6788
6789/*
6790 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006791 * compile "echomsg expr"
6792 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006793 * compile "execute expr"
6794 */
6795 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006796compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006797{
6798 char_u *p = arg;
6799 int count = 0;
6800
6801 for (;;)
6802 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006803 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006804 return NULL;
6805 ++count;
6806 p = skipwhite(p);
6807 if (ends_excmd(*p))
6808 break;
6809 }
6810
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006811 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6812 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6813 else if (cmdidx == CMD_execute)
6814 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6815 else if (cmdidx == CMD_echomsg)
6816 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6817 else
6818 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819 return p;
6820}
6821
6822/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006823 * A command that is not compiled, execute with legacy code.
6824 */
6825 static char_u *
6826compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6827{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006828 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006829 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006830 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006831
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006832 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006833 goto theend;
6834
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006835 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006836 {
6837 long argt = excmd_get_argt(eap->cmdidx);
6838 int usefilter = FALSE;
6839
6840 has_expr = argt & (EX_XFILE | EX_EXPAND);
6841
6842 // If the command can be followed by a bar, find the bar and truncate
6843 // it, so that the following command can be compiled.
6844 // The '|' is overwritten with a NUL, it is put back below.
6845 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6846 && *eap->arg == '!')
6847 // :w !filter or :r !filter or :r! filter
6848 usefilter = TRUE;
6849 if ((argt & EX_TRLBAR) && !usefilter)
6850 {
6851 separate_nextcmd(eap);
6852 if (eap->nextcmd != NULL)
6853 nextcmd = eap->nextcmd;
6854 }
6855 }
6856
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006857 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6858 {
6859 // expand filename in "syntax include [@group] filename"
6860 has_expr = TRUE;
6861 eap->arg = skipwhite(eap->arg + 7);
6862 if (*eap->arg == '@')
6863 eap->arg = skiptowhite(eap->arg);
6864 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006865
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006866 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006867 {
6868 int count = 0;
6869 char_u *start = skipwhite(line);
6870
6871 // :cmd xxx`=expr1`yyy`=expr2`zzz
6872 // PUSHS ":cmd xxx"
6873 // eval expr1
6874 // PUSHS "yyy"
6875 // eval expr2
6876 // PUSHS "zzz"
6877 // EXECCONCAT 5
6878 for (;;)
6879 {
6880 if (p > start)
6881 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006882 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006883 ++count;
6884 }
6885 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006886 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006887 return NULL;
6888 may_generate_2STRING(-1, cctx);
6889 ++count;
6890 p = skipwhite(p);
6891 if (*p != '`')
6892 {
6893 emsg(_("E1083: missing backtick"));
6894 return NULL;
6895 }
6896 start = p + 1;
6897
6898 p = (char_u *)strstr((char *)start, "`=");
6899 if (p == NULL)
6900 {
6901 if (*skipwhite(start) != NUL)
6902 {
6903 generate_PUSHS(cctx, vim_strsave(start));
6904 ++count;
6905 }
6906 break;
6907 }
6908 }
6909 generate_EXECCONCAT(cctx, count);
6910 }
6911 else
6912 generate_EXEC(cctx, line);
6913
6914theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006915 if (*nextcmd != NUL)
6916 {
6917 // the parser expects a pointer to the bar, put it back
6918 --nextcmd;
6919 *nextcmd = '|';
6920 }
6921
6922 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006923}
6924
6925/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006926 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006927 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006928 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006929 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006930add_def_function(ufunc_T *ufunc)
6931{
6932 dfunc_T *dfunc;
6933
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006934 if (def_functions.ga_len == 0)
6935 {
6936 // The first position is not used, so that a zero uf_dfunc_idx means it
6937 // wasn't set.
6938 if (ga_grow(&def_functions, 1) == FAIL)
6939 return FAIL;
6940 ++def_functions.ga_len;
6941 }
6942
Bram Moolenaar09689a02020-05-09 22:50:08 +02006943 // Add the function to "def_functions".
6944 if (ga_grow(&def_functions, 1) == FAIL)
6945 return FAIL;
6946 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6947 CLEAR_POINTER(dfunc);
6948 dfunc->df_idx = def_functions.ga_len;
6949 ufunc->uf_dfunc_idx = dfunc->df_idx;
6950 dfunc->df_ufunc = ufunc;
6951 ++def_functions.ga_len;
6952 return OK;
6953}
6954
6955/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956 * After ex_function() has collected all the function lines: parse and compile
6957 * the lines into instructions.
6958 * Adds the function to "def_functions".
6959 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6960 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006961 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006962 * This can be used recursively through compile_lambda(), which may reallocate
6963 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006964 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006965 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006966 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006967compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006968{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969 char_u *line = NULL;
6970 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006972 cctx_T cctx;
6973 garray_T *instr;
6974 int called_emsg_before = called_emsg;
6975 int ret = FAIL;
6976 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006977 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006978 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006979 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006980
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006981 // When using a function that was compiled before: Free old instructions.
6982 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006983 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006985 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6986 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006987 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006989 else
6990 {
6991 if (add_def_function(ufunc) == FAIL)
6992 return FAIL;
6993 new_def_function = TRUE;
6994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006995
Bram Moolenaar985116a2020-07-12 17:31:09 +02006996 ufunc->uf_def_status = UF_COMPILING;
6997
Bram Moolenaara80faa82020-04-12 19:37:17 +02006998 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 cctx.ctx_ufunc = ufunc;
7000 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007001 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7003 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7004 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7005 cctx.ctx_type_list = &ufunc->uf_type_list;
7006 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7007 instr = &cctx.ctx_instr;
7008
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007009 // Set the context to the function, it may be compiled when called from
7010 // another script. Set the script version to the most modern one.
7011 // The line number will be set in next_line_from_context().
7012 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007013 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7014
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007015 // Make sure error messages are OK.
7016 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7017 if (do_estack_push)
7018 estack_push_ufunc(ufunc, 1);
7019
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007020 if (ufunc->uf_def_args.ga_len > 0)
7021 {
7022 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007023 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007024 int i;
7025 char_u *arg;
7026 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007027 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007028
7029 // Produce instructions for the default values of optional arguments.
7030 // Store the instruction index in uf_def_arg_idx[] so that we know
7031 // where to start when the function is called, depending on the number
7032 // of arguments.
7033 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7034 if (ufunc->uf_def_arg_idx == NULL)
7035 goto erret;
7036 for (i = 0; i < count; ++i)
7037 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007038 garray_T *stack = &cctx.ctx_type_stack;
7039 type_T *val_type;
7040 int arg_idx = first_def_arg + i;
7041
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007042 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7043 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007044 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007045 goto erret;
7046
7047 // If no type specified use the type of the default value.
7048 // Otherwise check that the default value type matches the
7049 // specified type.
7050 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7051 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007052 {
7053 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007054 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007055 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02007056 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007057 == FAIL)
7058 {
7059 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
7060 arg_idx + 1);
7061 goto erret;
7062 }
7063
7064 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007065 goto erret;
7066 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007067 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007068
7069 if (did_set_arg_type)
7070 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007071 }
7072
7073 /*
7074 * Loop over all the lines of the function and generate instructions.
7075 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 for (;;)
7077 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007078 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007079 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007080 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007081 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007082
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007083 // Bail out on the first error to avoid a flood of errors and report
7084 // the right line number when inside try/catch.
7085 if (emsg_before != called_emsg)
7086 goto erret;
7087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 if (line != NULL && *line == '|')
7089 // the line continues after a '|'
7090 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007091 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007092 && !(*line == '#' && (line == cctx.ctx_line_start
7093 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007095 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007096 goto erret;
7097 }
7098 else
7099 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007100 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007101 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007102 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007104 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007105 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007106
Bram Moolenaara80faa82020-04-12 19:37:17 +02007107 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007108 ea.cmdlinep = &line;
7109 ea.cmd = skipwhite(line);
7110
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007111 // Some things can be recognized by the first character.
7112 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007114 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007115 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007116 if (ea.cmd[1] != '{')
7117 {
7118 line = (char_u *)"";
7119 continue;
7120 }
7121 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007122
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007123 case '}':
7124 {
7125 // "}" ends a block scope
7126 scopetype_T stype = cctx.ctx_scope == NULL
7127 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007129 if (stype == BLOCK_SCOPE)
7130 {
7131 compile_endblock(&cctx);
7132 line = ea.cmd;
7133 }
7134 else
7135 {
7136 emsg(_("E1025: using } outside of a block scope"));
7137 goto erret;
7138 }
7139 if (line != NULL)
7140 line = skipwhite(ea.cmd + 1);
7141 continue;
7142 }
7143
7144 case '{':
7145 // "{" starts a block scope
7146 // "{'a': 1}->func() is something else
7147 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7148 {
7149 line = compile_block(ea.cmd, &cctx);
7150 continue;
7151 }
7152 break;
7153
7154 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007155 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007156 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157 }
7158
7159 /*
7160 * COMMAND MODIFIERS
7161 */
7162 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7163 {
7164 if (errormsg != NULL)
7165 goto erret;
7166 // empty line or comment
7167 line = (char_u *)"";
7168 continue;
7169 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007170 // TODO: use modifiers in the command
7171 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007172 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173
7174 // Skip ":call" to get to the function name.
7175 if (checkforcmd(&ea.cmd, "call", 3))
7176 ea.cmd = skipwhite(ea.cmd);
7177
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007178 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007179 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007180 char_u *pskip;
7181
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007182 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007183 // find what follows.
7184 // Skip over "var.member", "var[idx]" and the like.
7185 // Also "&opt = val", "$ENV = val" and "@r = val".
7186 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007187 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007188 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007189 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007191 char_u *var_end;
7192 int oplen;
7193 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007195 var_end = find_name_end(pskip, NULL, NULL,
7196 FNE_CHECK_START | FNE_INCL_BR);
7197 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007198 if (oplen > 0)
7199 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007200 size_t len = p - ea.cmd;
7201
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007202 // Recognize an assignment if we recognize the variable
7203 // name:
7204 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007205 // "local = expr" where "local" is a local var.
7206 // "script = expr" where "script" is a script-local var.
7207 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007208 // "&opt = expr"
7209 // "$ENV = expr"
7210 // "@r = expr"
7211 if (*ea.cmd == '&'
7212 || *ea.cmd == '$'
7213 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007214 || ((len) > 2 && ea.cmd[1] == ':')
7215 || lookup_local(ea.cmd, len, &cctx) != NULL
7216 || lookup_arg(ea.cmd, len, NULL, NULL,
7217 NULL, &cctx) == OK
7218 || lookup_script(ea.cmd, len) == OK
7219 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007220 {
7221 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007222 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007223 goto erret;
7224 continue;
7225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007226 }
7227 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007228
7229 if (*ea.cmd == '[')
7230 {
7231 // [var, var] = expr
7232 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7233 if (line == NULL)
7234 goto erret;
7235 if (line != ea.cmd)
7236 continue;
7237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007238 }
7239
7240 /*
7241 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007242 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007244 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007245 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007246 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007247 ea.cmd = skip_range(ea.cmd, NULL);
7248 if (ea.cmd > cmd && !starts_with_colon)
7249 {
7250 emsg(_(e_colon_required));
7251 goto erret;
7252 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007253 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007254 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007255 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007256 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007257
7258 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7259 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007260 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007261 {
7262 line += STRLEN(line);
7263 continue;
7264 }
7265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007266 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007267 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007268 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007269 // CMD_let cannot happen, compile_assignment() above is used
7270 iemsg("Command from find_ex_command() not handled");
7271 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273 }
7274
7275 p = skipwhite(p);
7276
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007277 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007278 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007279 && ea.cmdidx != CMD_elseif
7280 && ea.cmdidx != CMD_else
7281 && ea.cmdidx != CMD_endif)
7282 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007283 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007284 continue;
7285 }
7286
Bram Moolenaarefd88552020-06-18 20:50:10 +02007287 if (ea.cmdidx != CMD_elseif
7288 && ea.cmdidx != CMD_else
7289 && ea.cmdidx != CMD_endif
7290 && ea.cmdidx != CMD_endfor
7291 && ea.cmdidx != CMD_endwhile
7292 && ea.cmdidx != CMD_catch
7293 && ea.cmdidx != CMD_finally
7294 && ea.cmdidx != CMD_endtry)
7295 {
7296 if (cctx.ctx_had_return)
7297 {
7298 emsg(_("E1095: Unreachable code after :return"));
7299 goto erret;
7300 }
7301 }
7302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007303 switch (ea.cmdidx)
7304 {
7305 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007306 ea.arg = p;
7307 line = compile_nested_function(&ea, &cctx);
7308 break;
7309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007311 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312 goto erret;
7313
7314 case CMD_return:
7315 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007316 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 break;
7318
7319 case CMD_let:
7320 case CMD_const:
7321 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007322 if (line == p)
7323 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324 break;
7325
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007326 case CMD_unlet:
7327 case CMD_unlockvar:
7328 case CMD_lockvar:
7329 line = compile_unletlock(p, &ea, &cctx);
7330 break;
7331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332 case CMD_import:
7333 line = compile_import(p, &cctx);
7334 break;
7335
7336 case CMD_if:
7337 line = compile_if(p, &cctx);
7338 break;
7339 case CMD_elseif:
7340 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007341 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007342 break;
7343 case CMD_else:
7344 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007345 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346 break;
7347 case CMD_endif:
7348 line = compile_endif(p, &cctx);
7349 break;
7350
7351 case CMD_while:
7352 line = compile_while(p, &cctx);
7353 break;
7354 case CMD_endwhile:
7355 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007356 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 break;
7358
7359 case CMD_for:
7360 line = compile_for(p, &cctx);
7361 break;
7362 case CMD_endfor:
7363 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007364 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 break;
7366 case CMD_continue:
7367 line = compile_continue(p, &cctx);
7368 break;
7369 case CMD_break:
7370 line = compile_break(p, &cctx);
7371 break;
7372
7373 case CMD_try:
7374 line = compile_try(p, &cctx);
7375 break;
7376 case CMD_catch:
7377 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007378 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 break;
7380 case CMD_finally:
7381 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007382 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 break;
7384 case CMD_endtry:
7385 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007386 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 break;
7388 case CMD_throw:
7389 line = compile_throw(p, &cctx);
7390 break;
7391
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007392 case CMD_eval:
7393 if (compile_expr0(&p, &cctx) == FAIL)
7394 goto erret;
7395
7396 // drop the return value
7397 generate_instr_drop(&cctx, ISN_DROP, 1);
7398
7399 line = skipwhite(p);
7400 break;
7401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007404 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007405 case CMD_echomsg:
7406 case CMD_echoerr:
7407 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007408 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007410 // TODO: other commands with an expression argument
7411
Bram Moolenaar002262f2020-07-08 17:47:57 +02007412 case CMD_SIZE:
7413 semsg(_("E476: Invalid command: %s"), ea.cmd);
7414 goto erret;
7415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007417 // Not recognized, execute with do_cmdline_cmd().
7418 ea.arg = p;
7419 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 break;
7421 }
7422 if (line == NULL)
7423 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007424 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425
7426 if (cctx.ctx_type_stack.ga_len < 0)
7427 {
7428 iemsg("Type stack underflow");
7429 goto erret;
7430 }
7431 }
7432
7433 if (cctx.ctx_scope != NULL)
7434 {
7435 if (cctx.ctx_scope->se_type == IF_SCOPE)
7436 emsg(_(e_endif));
7437 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7438 emsg(_(e_endwhile));
7439 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7440 emsg(_(e_endfor));
7441 else
7442 emsg(_("E1026: Missing }"));
7443 goto erret;
7444 }
7445
Bram Moolenaarefd88552020-06-18 20:50:10 +02007446 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447 {
7448 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7449 {
7450 emsg(_("E1027: Missing return statement"));
7451 goto erret;
7452 }
7453
7454 // Return zero if there is no return at the end.
7455 generate_PUSHNR(&cctx, 0);
7456 generate_instr(&cctx, ISN_RETURN);
7457 }
7458
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007459 {
7460 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7461 + ufunc->uf_dfunc_idx;
7462 dfunc->df_deleted = FALSE;
7463 dfunc->df_instr = instr->ga_data;
7464 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007465 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007466 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007467 if (cctx.ctx_outer_used)
7468 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007469 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471
7472 ret = OK;
7473
7474erret:
7475 if (ret == FAIL)
7476 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007477 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007478 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7479 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007480
7481 for (idx = 0; idx < instr->ga_len; ++idx)
7482 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007484
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007485 // If using the last entry in the table and it was added above, we
7486 // might as well remove it.
7487 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007488 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007489 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007490 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007491 ufunc->uf_dfunc_idx = 0;
7492 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007493 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007494
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007495 while (cctx.ctx_scope != NULL)
7496 drop_scope(&cctx);
7497
Bram Moolenaar20431c92020-03-20 18:39:46 +01007498 // Don't execute this function body.
7499 ga_clear_strings(&ufunc->uf_lines);
7500
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007501 if (errormsg != NULL)
7502 emsg(errormsg);
7503 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007504 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007505 }
7506
7507 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007508 if (do_estack_push)
7509 estack_pop();
7510
Bram Moolenaar20431c92020-03-20 18:39:46 +01007511 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007512 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007514 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515}
7516
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007517 void
7518set_function_type(ufunc_T *ufunc)
7519{
7520 int varargs = ufunc->uf_va_name != NULL;
7521 int argcount = ufunc->uf_args.ga_len;
7522
7523 // Create a type for the function, with the return type and any
7524 // argument types.
7525 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7526 // The type is included in "tt_args".
7527 if (argcount > 0 || varargs)
7528 {
7529 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7530 argcount, &ufunc->uf_type_list);
7531 // Add argument types to the function type.
7532 if (func_type_add_arg_types(ufunc->uf_func_type,
7533 argcount + varargs,
7534 &ufunc->uf_type_list) == FAIL)
7535 return;
7536 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7537 ufunc->uf_func_type->tt_min_argcount =
7538 argcount - ufunc->uf_def_args.ga_len;
7539 if (ufunc->uf_arg_types == NULL)
7540 {
7541 int i;
7542
7543 // lambda does not have argument types.
7544 for (i = 0; i < argcount; ++i)
7545 ufunc->uf_func_type->tt_args[i] = &t_any;
7546 }
7547 else
7548 mch_memmove(ufunc->uf_func_type->tt_args,
7549 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7550 if (varargs)
7551 {
7552 ufunc->uf_func_type->tt_args[argcount] =
7553 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7554 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7555 }
7556 }
7557 else
7558 // No arguments, can use a predefined type.
7559 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7560 argcount, &ufunc->uf_type_list);
7561}
7562
7563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564/*
7565 * Delete an instruction, free what it contains.
7566 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007567 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568delete_instr(isn_T *isn)
7569{
7570 switch (isn->isn_type)
7571 {
7572 case ISN_EXEC:
7573 case ISN_LOADENV:
7574 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007575 case ISN_LOADB:
7576 case ISN_LOADW:
7577 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007578 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007579 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580 case ISN_PUSHEXC:
7581 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007582 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007583 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007584 case ISN_STOREB:
7585 case ISN_STOREW:
7586 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007587 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007588 vim_free(isn->isn_arg.string);
7589 break;
7590
7591 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007592 case ISN_STORES:
7593 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594 break;
7595
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007596 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007597 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007598 vim_free(isn->isn_arg.unlet.ul_name);
7599 break;
7600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007601 case ISN_STOREOPT:
7602 vim_free(isn->isn_arg.storeopt.so_name);
7603 break;
7604
7605 case ISN_PUSHBLOB: // push blob isn_arg.blob
7606 blob_unref(isn->isn_arg.blob);
7607 break;
7608
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007609 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007610#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007611 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007612#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007613 break;
7614
7615 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007616#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007617 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007618#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007619 break;
7620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007621 case ISN_UCALL:
7622 vim_free(isn->isn_arg.ufunc.cuf_name);
7623 break;
7624
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007625 case ISN_FUNCREF:
7626 {
7627 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7628 + isn->isn_arg.funcref.fr_func;
7629 func_ptr_unref(dfunc->df_ufunc);
7630 }
7631 break;
7632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007633 case ISN_2BOOL:
7634 case ISN_2STRING:
7635 case ISN_ADDBLOB:
7636 case ISN_ADDLIST:
7637 case ISN_BCALL:
7638 case ISN_CATCH:
7639 case ISN_CHECKNR:
7640 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007641 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 case ISN_COMPAREANY:
7643 case ISN_COMPAREBLOB:
7644 case ISN_COMPAREBOOL:
7645 case ISN_COMPAREDICT:
7646 case ISN_COMPAREFLOAT:
7647 case ISN_COMPAREFUNC:
7648 case ISN_COMPARELIST:
7649 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007650 case ISN_COMPARESPECIAL:
7651 case ISN_COMPARESTRING:
7652 case ISN_CONCAT:
7653 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007654 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007655 case ISN_DROP:
7656 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007657 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007658 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007659 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007660 case ISN_EXECCONCAT:
7661 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007662 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007663 case ISN_LISTINDEX:
7664 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007665 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007666 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007667 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007668 case ISN_JUMP:
7669 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007670 case ISN_LOADBDICT:
7671 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007672 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007673 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007674 case ISN_LOADSCRIPT:
7675 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007676 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007677 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007678 case ISN_NEGATENR:
7679 case ISN_NEWDICT:
7680 case ISN_NEWLIST:
7681 case ISN_OPNR:
7682 case ISN_OPFLOAT:
7683 case ISN_OPANY:
7684 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007685 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007686 case ISN_PUSHF:
7687 case ISN_PUSHNR:
7688 case ISN_PUSHBOOL:
7689 case ISN_PUSHSPEC:
7690 case ISN_RETURN:
7691 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007692 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007693 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007695 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007696 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007697 case ISN_STOREDICT:
7698 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007699 case ISN_THROW:
7700 case ISN_TRY:
7701 // nothing allocated
7702 break;
7703 }
7704}
7705
7706/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007707 * Free all instructions for "dfunc".
7708 */
7709 static void
7710delete_def_function_contents(dfunc_T *dfunc)
7711{
7712 int idx;
7713
7714 ga_clear(&dfunc->df_def_args_isn);
7715
7716 if (dfunc->df_instr != NULL)
7717 {
7718 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7719 delete_instr(dfunc->df_instr + idx);
7720 VIM_CLEAR(dfunc->df_instr);
7721 }
7722
7723 dfunc->df_deleted = TRUE;
7724}
7725
7726/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007727 * When a user function is deleted, clear the contents of any associated def
7728 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007729 */
7730 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007731clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007732{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007733 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007734 {
7735 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7736 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737
Bram Moolenaar20431c92020-03-20 18:39:46 +01007738 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007739 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740 }
7741}
7742
7743#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007744/*
7745 * Free all functions defined with ":def".
7746 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007747 void
7748free_def_functions(void)
7749{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007750 int idx;
7751
7752 for (idx = 0; idx < def_functions.ga_len; ++idx)
7753 {
7754 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7755
7756 delete_def_function_contents(dfunc);
7757 }
7758
7759 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007760}
7761#endif
7762
7763
7764#endif // FEAT_EVAL