blob: 53bfb6c48e034eeaef7624e158159672f98db49f [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);
537 if (ufunc != NULL && ufunc->uf_func_type != NULL)
538 return ufunc->uf_func_type;
539 }
540
541 actual = alloc_type(type_gap);
542 if (actual == NULL)
543 return NULL;
544 actual->tt_type = tv->v_type;
545 actual->tt_member = &t_any;
546
547 return actual;
548}
549
550/*
551 * Get a type_T for a typval_T, used for v: variables.
552 * "type_list" is used to temporarily create types in.
553 */
554 type_T *
555typval2type_vimvar(typval_T *tv, garray_T *type_gap)
556{
Bram Moolenaara8c17702020-04-01 21:17:24 +0200557 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
558 return &t_list_string;
559 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
560 return &t_dict_any;
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200561 return typval2type(tv, type_gap);
562}
563
564
565/*
566 * Return FAIL if "expected" and "actual" don't match.
567 */
568 int
569check_typval_type(type_T *expected, typval_T *actual_tv)
570{
571 garray_T type_list;
572 type_T *actual_type;
573 int res = FAIL;
574
575 ga_init2(&type_list, sizeof(type_T *), 10);
576 actual_type = typval2type(actual_tv, &type_list);
577 if (actual_type != NULL)
578 res = check_type(expected, actual_type, TRUE);
579 clear_type_list(&type_list);
580 return res;
Bram Moolenaara8c17702020-04-01 21:17:24 +0200581}
582
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200583 static void
584type_mismatch(type_T *expected, type_T *actual)
585{
586 char *tofree1, *tofree2;
587
588 semsg(_("E1013: type mismatch, expected %s but got %s"),
589 type_name(expected, &tofree1), type_name(actual, &tofree2));
590 vim_free(tofree1);
591 vim_free(tofree2);
592}
593
594 static void
595arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
596{
597 char *tofree1, *tofree2;
598
599 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
600 argidx,
601 type_name(expected, &tofree1), type_name(actual, &tofree2));
602 vim_free(tofree1);
603 vim_free(tofree2);
604}
605
606/*
607 * Check if the expected and actual types match.
608 * Does not allow for assigning "any" to a specific type.
609 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200610 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200611check_type(type_T *expected, type_T *actual, int give_msg)
612{
613 int ret = OK;
614
615 // When expected is "unknown" we accept any actual type.
616 // When expected is "any" we accept any actual type except "void".
617 if (expected->tt_type != VAR_UNKNOWN
618 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
619
620 {
621 if (expected->tt_type != actual->tt_type)
622 {
623 if (give_msg)
624 type_mismatch(expected, actual);
625 return FAIL;
626 }
627 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
628 {
629 // "unknown" is used for an empty list or dict
630 if (actual->tt_member != &t_unknown)
631 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
632 }
633 else if (expected->tt_type == VAR_FUNC)
634 {
635 if (expected->tt_member != &t_unknown)
636 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
637 if (ret == OK && expected->tt_argcount != -1
638 && (actual->tt_argcount < expected->tt_min_argcount
639 || actual->tt_argcount > expected->tt_argcount))
640 ret = FAIL;
Bram Moolenaarb8070e32020-07-23 20:56:04 +0200641 if (expected->tt_args != NULL && actual->tt_args != NULL)
642 {
643 int i;
644
645 for (i = 0; i < expected->tt_argcount; ++i)
646 if (check_type(expected->tt_args[i], actual->tt_args[i],
647 FALSE) == FAIL)
648 {
649 ret = FAIL;
650 break;
651 }
652 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200653 }
654 if (ret == FAIL && give_msg)
655 type_mismatch(expected, actual);
656 }
657 return ret;
658}
659
Bram Moolenaar65b95452020-07-19 14:03:09 +0200660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100661/////////////////////////////////////////////////////////////////////
662// Following generate_ functions expect the caller to call ga_grow().
663
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200664#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
665#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100666
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667/*
668 * Generate an instruction without arguments.
669 * Returns a pointer to the new instruction, NULL if failed.
670 */
671 static isn_T *
672generate_instr(cctx_T *cctx, isntype_T isn_type)
673{
674 garray_T *instr = &cctx->ctx_instr;
675 isn_T *isn;
676
Bram Moolenaar080457c2020-03-03 21:53:32 +0100677 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678 if (ga_grow(instr, 1) == FAIL)
679 return NULL;
680 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
681 isn->isn_type = isn_type;
682 isn->isn_lnum = cctx->ctx_lnum + 1;
683 ++instr->ga_len;
684
685 return isn;
686}
687
688/*
689 * Generate an instruction without arguments.
690 * "drop" will be removed from the stack.
691 * Returns a pointer to the new instruction, NULL if failed.
692 */
693 static isn_T *
694generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
695{
696 garray_T *stack = &cctx->ctx_type_stack;
697
Bram Moolenaar080457c2020-03-03 21:53:32 +0100698 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699 stack->ga_len -= drop;
700 return generate_instr(cctx, isn_type);
701}
702
703/*
704 * Generate instruction "isn_type" and put "type" on the type stack.
705 */
706 static isn_T *
707generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
708{
709 isn_T *isn;
710 garray_T *stack = &cctx->ctx_type_stack;
711
712 if ((isn = generate_instr(cctx, isn_type)) == NULL)
713 return NULL;
714
715 if (ga_grow(stack, 1) == FAIL)
716 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200717 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 ++stack->ga_len;
719
720 return isn;
721}
722
723/*
724 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
725 */
726 static int
727may_generate_2STRING(int offset, cctx_T *cctx)
728{
729 isn_T *isn;
730 garray_T *stack = &cctx->ctx_type_stack;
731 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
732
733 if ((*type)->tt_type == VAR_STRING)
734 return OK;
735 *type = &t_string;
736
737 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
738 return FAIL;
739 isn->isn_arg.number = offset;
740
741 return OK;
742}
743
744 static int
745check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
746{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200747 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200749 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 {
751 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200752 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 else
754 semsg(_("E1036: %c requires number or float arguments"), *op);
755 return FAIL;
756 }
757 return OK;
758}
759
760/*
761 * Generate an instruction with two arguments. The instruction depends on the
762 * type of the arguments.
763 */
764 static int
765generate_two_op(cctx_T *cctx, char_u *op)
766{
767 garray_T *stack = &cctx->ctx_type_stack;
768 type_T *type1;
769 type_T *type2;
770 vartype_T vartype;
771 isn_T *isn;
772
Bram Moolenaar080457c2020-03-03 21:53:32 +0100773 RETURN_OK_IF_SKIP(cctx);
774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 // Get the known type of the two items on the stack. If they are matching
776 // use a type-specific instruction. Otherwise fall back to runtime type
777 // checking.
778 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
779 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200780 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 if (type1->tt_type == type2->tt_type
782 && (type1->tt_type == VAR_NUMBER
783 || type1->tt_type == VAR_LIST
784#ifdef FEAT_FLOAT
785 || type1->tt_type == VAR_FLOAT
786#endif
787 || type1->tt_type == VAR_BLOB))
788 vartype = type1->tt_type;
789
790 switch (*op)
791 {
792 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200793 && type1->tt_type != VAR_ANY
794 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795 && check_number_or_float(
796 type1->tt_type, type2->tt_type, op) == FAIL)
797 return FAIL;
798 isn = generate_instr_drop(cctx,
799 vartype == VAR_NUMBER ? ISN_OPNR
800 : vartype == VAR_LIST ? ISN_ADDLIST
801 : vartype == VAR_BLOB ? ISN_ADDBLOB
802#ifdef FEAT_FLOAT
803 : vartype == VAR_FLOAT ? ISN_OPFLOAT
804#endif
805 : ISN_OPANY, 1);
806 if (isn != NULL)
807 isn->isn_arg.op.op_type = EXPR_ADD;
808 break;
809
810 case '-':
811 case '*':
812 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
813 op) == FAIL)
814 return FAIL;
815 if (vartype == VAR_NUMBER)
816 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
817#ifdef FEAT_FLOAT
818 else if (vartype == VAR_FLOAT)
819 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
820#endif
821 else
822 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
823 if (isn != NULL)
824 isn->isn_arg.op.op_type = *op == '*'
825 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
826 break;
827
Bram Moolenaar4c683752020-04-05 21:38:23 +0200828 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200830 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831 && type2->tt_type != VAR_NUMBER))
832 {
833 emsg(_("E1035: % requires number arguments"));
834 return FAIL;
835 }
836 isn = generate_instr_drop(cctx,
837 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
838 if (isn != NULL)
839 isn->isn_arg.op.op_type = EXPR_REM;
840 break;
841 }
842
843 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200844 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 {
846 type_T *type = &t_any;
847
848#ifdef FEAT_FLOAT
849 // float+number and number+float results in float
850 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
851 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
852 type = &t_float;
853#endif
854 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
855 }
856
857 return OK;
858}
859
860/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200861 * Get the instruction to use for comparing "type1" with "type2"
862 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100863 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200864 static isntype_T
865get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866{
867 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868
Bram Moolenaar4c683752020-04-05 21:38:23 +0200869 if (type1 == VAR_UNKNOWN)
870 type1 = VAR_ANY;
871 if (type2 == VAR_UNKNOWN)
872 type2 = VAR_ANY;
873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 if (type1 == type2)
875 {
876 switch (type1)
877 {
878 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
879 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
880 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
881 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
882 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
883 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
884 case VAR_LIST: isntype = ISN_COMPARELIST; break;
885 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
886 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 default: isntype = ISN_COMPAREANY; break;
888 }
889 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200890 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
892 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
893 isntype = ISN_COMPAREANY;
894
895 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
896 && (isntype == ISN_COMPAREBOOL
897 || isntype == ISN_COMPARESPECIAL
898 || isntype == ISN_COMPARENR
899 || isntype == ISN_COMPAREFLOAT))
900 {
901 semsg(_("E1037: Cannot use \"%s\" with %s"),
902 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200903 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 }
905 if (isntype == ISN_DROP
906 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
907 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
908 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
909 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
910 && exptype != EXPR_IS && exptype != EXPR_ISNOT
911 && (type1 == VAR_BLOB || type2 == VAR_BLOB
912 || type1 == VAR_LIST || type2 == VAR_LIST))))
913 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100914 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200916 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200918 return isntype;
919}
920
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200921 int
922check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
923{
924 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
925 return FAIL;
926 return OK;
927}
928
Bram Moolenaara5565e42020-05-09 15:44:01 +0200929/*
930 * Generate an ISN_COMPARE* instruction with a boolean result.
931 */
932 static int
933generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
934{
935 isntype_T isntype;
936 isn_T *isn;
937 garray_T *stack = &cctx->ctx_type_stack;
938 vartype_T type1;
939 vartype_T type2;
940
941 RETURN_OK_IF_SKIP(cctx);
942
943 // Get the known type of the two items on the stack. If they are matching
944 // use a type-specific instruction. Otherwise fall back to runtime type
945 // checking.
946 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
947 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
948 isntype = get_compare_isn(exptype, type1, type2);
949 if (isntype == ISN_DROP)
950 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951
952 if ((isn = generate_instr(cctx, isntype)) == NULL)
953 return FAIL;
954 isn->isn_arg.op.op_type = exptype;
955 isn->isn_arg.op.op_ic = ic;
956
957 // takes two arguments, puts one bool back
958 if (stack->ga_len >= 2)
959 {
960 --stack->ga_len;
961 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
962 }
963
964 return OK;
965}
966
967/*
968 * Generate an ISN_2BOOL instruction.
969 */
970 static int
971generate_2BOOL(cctx_T *cctx, int invert)
972{
973 isn_T *isn;
974 garray_T *stack = &cctx->ctx_type_stack;
975
Bram Moolenaar080457c2020-03-03 21:53:32 +0100976 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
978 return FAIL;
979 isn->isn_arg.number = invert;
980
981 // type becomes bool
982 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
983
984 return OK;
985}
986
987 static int
988generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
989{
990 isn_T *isn;
991 garray_T *stack = &cctx->ctx_type_stack;
992
Bram Moolenaar080457c2020-03-03 21:53:32 +0100993 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
995 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200996 // TODO: whole type, e.g. for a function also arg and return types
997 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 isn->isn_arg.type.ct_off = offset;
999
1000 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001001 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002
1003 return OK;
1004}
1005
1006/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001007 * Check that
1008 * - "actual" is "expected" type or
1009 * - "actual" is a type that can be "expected" type: add a runtime check; or
1010 * - return FAIL.
1011 */
1012 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001013need_type(
1014 type_T *actual,
1015 type_T *expected,
1016 int offset,
1017 cctx_T *cctx,
1018 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001019{
1020 if (check_type(expected, actual, FALSE) == OK)
1021 return OK;
1022 if (actual->tt_type != VAR_ANY
1023 && actual->tt_type != VAR_UNKNOWN
1024 && !(actual->tt_type == VAR_FUNC
1025 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
1026 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001027 if (!silent)
1028 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001029 return FAIL;
1030 }
1031 generate_TYPECHECK(cctx, expected, offset);
1032 return OK;
1033}
1034
1035/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036 * Generate an ISN_PUSHNR instruction.
1037 */
1038 static int
1039generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1040{
1041 isn_T *isn;
1042
Bram Moolenaar080457c2020-03-03 21:53:32 +01001043 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1045 return FAIL;
1046 isn->isn_arg.number = number;
1047
1048 return OK;
1049}
1050
1051/*
1052 * Generate an ISN_PUSHBOOL instruction.
1053 */
1054 static int
1055generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1056{
1057 isn_T *isn;
1058
Bram Moolenaar080457c2020-03-03 21:53:32 +01001059 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1061 return FAIL;
1062 isn->isn_arg.number = number;
1063
1064 return OK;
1065}
1066
1067/*
1068 * Generate an ISN_PUSHSPEC instruction.
1069 */
1070 static int
1071generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1072{
1073 isn_T *isn;
1074
Bram Moolenaar080457c2020-03-03 21:53:32 +01001075 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1077 return FAIL;
1078 isn->isn_arg.number = number;
1079
1080 return OK;
1081}
1082
1083#ifdef FEAT_FLOAT
1084/*
1085 * Generate an ISN_PUSHF instruction.
1086 */
1087 static int
1088generate_PUSHF(cctx_T *cctx, float_T fnumber)
1089{
1090 isn_T *isn;
1091
Bram Moolenaar080457c2020-03-03 21:53:32 +01001092 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1094 return FAIL;
1095 isn->isn_arg.fnumber = fnumber;
1096
1097 return OK;
1098}
1099#endif
1100
1101/*
1102 * Generate an ISN_PUSHS instruction.
1103 * Consumes "str".
1104 */
1105 static int
1106generate_PUSHS(cctx_T *cctx, char_u *str)
1107{
1108 isn_T *isn;
1109
Bram Moolenaar080457c2020-03-03 21:53:32 +01001110 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1112 return FAIL;
1113 isn->isn_arg.string = str;
1114
1115 return OK;
1116}
1117
1118/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001119 * Generate an ISN_PUSHCHANNEL instruction.
1120 * Consumes "channel".
1121 */
1122 static int
1123generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1124{
1125 isn_T *isn;
1126
Bram Moolenaar080457c2020-03-03 21:53:32 +01001127 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001128 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1129 return FAIL;
1130 isn->isn_arg.channel = channel;
1131
1132 return OK;
1133}
1134
1135/*
1136 * Generate an ISN_PUSHJOB instruction.
1137 * Consumes "job".
1138 */
1139 static int
1140generate_PUSHJOB(cctx_T *cctx, job_T *job)
1141{
1142 isn_T *isn;
1143
Bram Moolenaar080457c2020-03-03 21:53:32 +01001144 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001145 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001146 return FAIL;
1147 isn->isn_arg.job = job;
1148
1149 return OK;
1150}
1151
1152/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153 * Generate an ISN_PUSHBLOB instruction.
1154 * Consumes "blob".
1155 */
1156 static int
1157generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1158{
1159 isn_T *isn;
1160
Bram Moolenaar080457c2020-03-03 21:53:32 +01001161 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1163 return FAIL;
1164 isn->isn_arg.blob = blob;
1165
1166 return OK;
1167}
1168
1169/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001170 * Generate an ISN_PUSHFUNC instruction with name "name".
1171 * Consumes "name".
1172 */
1173 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001174generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001175{
1176 isn_T *isn;
1177
Bram Moolenaar080457c2020-03-03 21:53:32 +01001178 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001179 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001180 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001181 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001182
1183 return OK;
1184}
1185
1186/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001187 * Generate an ISN_GETITEM instruction with "index".
1188 */
1189 static int
1190generate_GETITEM(cctx_T *cctx, int index)
1191{
1192 isn_T *isn;
1193 garray_T *stack = &cctx->ctx_type_stack;
1194 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1195 type_T *item_type = &t_any;
1196
1197 RETURN_OK_IF_SKIP(cctx);
1198
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001199 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001200 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001201 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001202 emsg(_(e_listreq));
1203 return FAIL;
1204 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001205 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001206 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1207 return FAIL;
1208 isn->isn_arg.number = index;
1209
1210 // add the item type to the type stack
1211 if (ga_grow(stack, 1) == FAIL)
1212 return FAIL;
1213 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1214 ++stack->ga_len;
1215 return OK;
1216}
1217
1218/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001219 * Generate an ISN_SLICE instruction with "count".
1220 */
1221 static int
1222generate_SLICE(cctx_T *cctx, int count)
1223{
1224 isn_T *isn;
1225
1226 RETURN_OK_IF_SKIP(cctx);
1227 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1228 return FAIL;
1229 isn->isn_arg.number = count;
1230 return OK;
1231}
1232
1233/*
1234 * Generate an ISN_CHECKLEN instruction with "min_len".
1235 */
1236 static int
1237generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1238{
1239 isn_T *isn;
1240
1241 RETURN_OK_IF_SKIP(cctx);
1242
1243 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1244 return FAIL;
1245 isn->isn_arg.checklen.cl_min_len = min_len;
1246 isn->isn_arg.checklen.cl_more_OK = more_OK;
1247
1248 return OK;
1249}
1250
1251/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252 * Generate an ISN_STORE instruction.
1253 */
1254 static int
1255generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1256{
1257 isn_T *isn;
1258
Bram Moolenaar080457c2020-03-03 21:53:32 +01001259 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1261 return FAIL;
1262 if (name != NULL)
1263 isn->isn_arg.string = vim_strsave(name);
1264 else
1265 isn->isn_arg.number = idx;
1266
1267 return OK;
1268}
1269
1270/*
1271 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1272 */
1273 static int
1274generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1275{
1276 isn_T *isn;
1277
Bram Moolenaar080457c2020-03-03 21:53:32 +01001278 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1280 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001281 isn->isn_arg.storenr.stnr_idx = idx;
1282 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283
1284 return OK;
1285}
1286
1287/*
1288 * Generate an ISN_STOREOPT instruction
1289 */
1290 static int
1291generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1292{
1293 isn_T *isn;
1294
Bram Moolenaar080457c2020-03-03 21:53:32 +01001295 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1297 return FAIL;
1298 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1299 isn->isn_arg.storeopt.so_flags = opt_flags;
1300
1301 return OK;
1302}
1303
1304/*
1305 * Generate an ISN_LOAD or similar instruction.
1306 */
1307 static int
1308generate_LOAD(
1309 cctx_T *cctx,
1310 isntype_T isn_type,
1311 int idx,
1312 char_u *name,
1313 type_T *type)
1314{
1315 isn_T *isn;
1316
Bram Moolenaar080457c2020-03-03 21:53:32 +01001317 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1319 return FAIL;
1320 if (name != NULL)
1321 isn->isn_arg.string = vim_strsave(name);
1322 else
1323 isn->isn_arg.number = idx;
1324
1325 return OK;
1326}
1327
1328/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001329 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001330 */
1331 static int
1332generate_LOADV(
1333 cctx_T *cctx,
1334 char_u *name,
1335 int error)
1336{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001337 int di_flags;
1338 int vidx = find_vim_var(name, &di_flags);
1339 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001340
Bram Moolenaar080457c2020-03-03 21:53:32 +01001341 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001342 if (vidx < 0)
1343 {
1344 if (error)
1345 semsg(_(e_var_notfound), name);
1346 return FAIL;
1347 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001348 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001349
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001350 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001351}
1352
1353/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001354 * Generate an ISN_UNLET instruction.
1355 */
1356 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001357generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001358{
1359 isn_T *isn;
1360
1361 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001362 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001363 return FAIL;
1364 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1365 isn->isn_arg.unlet.ul_forceit = forceit;
1366
1367 return OK;
1368}
1369
1370/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 * Generate an ISN_LOADS instruction.
1372 */
1373 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001374generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001376 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001378 int sid,
1379 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380{
1381 isn_T *isn;
1382
Bram Moolenaar080457c2020-03-03 21:53:32 +01001383 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001384 if (isn_type == ISN_LOADS)
1385 isn = generate_instr_type(cctx, isn_type, type);
1386 else
1387 isn = generate_instr_drop(cctx, isn_type, 1);
1388 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001390 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1391 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392
1393 return OK;
1394}
1395
1396/*
1397 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1398 */
1399 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001400generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 cctx_T *cctx,
1402 isntype_T isn_type,
1403 int sid,
1404 int idx,
1405 type_T *type)
1406{
1407 isn_T *isn;
1408
Bram Moolenaar080457c2020-03-03 21:53:32 +01001409 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 if (isn_type == ISN_LOADSCRIPT)
1411 isn = generate_instr_type(cctx, isn_type, type);
1412 else
1413 isn = generate_instr_drop(cctx, isn_type, 1);
1414 if (isn == NULL)
1415 return FAIL;
1416 isn->isn_arg.script.script_sid = sid;
1417 isn->isn_arg.script.script_idx = idx;
1418 return OK;
1419}
1420
1421/*
1422 * Generate an ISN_NEWLIST instruction.
1423 */
1424 static int
1425generate_NEWLIST(cctx_T *cctx, int count)
1426{
1427 isn_T *isn;
1428 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 type_T *type;
1430 type_T *member;
1431
Bram Moolenaar080457c2020-03-03 21:53:32 +01001432 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1434 return FAIL;
1435 isn->isn_arg.number = count;
1436
1437 // drop the value types
1438 stack->ga_len -= count;
1439
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001440 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001441 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 if (count > 0)
1443 member = ((type_T **)stack->ga_data)[stack->ga_len];
1444 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001445 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001446 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447
1448 // add the list type to the type stack
1449 if (ga_grow(stack, 1) == FAIL)
1450 return FAIL;
1451 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1452 ++stack->ga_len;
1453
1454 return OK;
1455}
1456
1457/*
1458 * Generate an ISN_NEWDICT instruction.
1459 */
1460 static int
1461generate_NEWDICT(cctx_T *cctx, int count)
1462{
1463 isn_T *isn;
1464 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 type_T *type;
1466 type_T *member;
1467
Bram Moolenaar080457c2020-03-03 21:53:32 +01001468 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1470 return FAIL;
1471 isn->isn_arg.number = count;
1472
1473 // drop the key and value types
1474 stack->ga_len -= 2 * count;
1475
Bram Moolenaar436472f2020-02-20 22:54:43 +01001476 // Use the first value type for the list member type. Use "void" for an
1477 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 if (count > 0)
1479 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1480 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001481 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001482 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483
1484 // add the dict type to the type stack
1485 if (ga_grow(stack, 1) == FAIL)
1486 return FAIL;
1487 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1488 ++stack->ga_len;
1489
1490 return OK;
1491}
1492
1493/*
1494 * Generate an ISN_FUNCREF instruction.
1495 */
1496 static int
1497generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1498{
1499 isn_T *isn;
1500 garray_T *stack = &cctx->ctx_type_stack;
1501
Bram Moolenaar080457c2020-03-03 21:53:32 +01001502 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1504 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001505 isn->isn_arg.funcref.fr_func = dfunc_idx;
1506 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507
1508 if (ga_grow(stack, 1) == FAIL)
1509 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001510 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 // TODO: argument and return types
1512 ++stack->ga_len;
1513
1514 return OK;
1515}
1516
1517/*
1518 * Generate an ISN_JUMP instruction.
1519 */
1520 static int
1521generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1522{
1523 isn_T *isn;
1524 garray_T *stack = &cctx->ctx_type_stack;
1525
Bram Moolenaar080457c2020-03-03 21:53:32 +01001526 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1528 return FAIL;
1529 isn->isn_arg.jump.jump_when = when;
1530 isn->isn_arg.jump.jump_where = where;
1531
1532 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1533 --stack->ga_len;
1534
1535 return OK;
1536}
1537
1538 static int
1539generate_FOR(cctx_T *cctx, int loop_idx)
1540{
1541 isn_T *isn;
1542 garray_T *stack = &cctx->ctx_type_stack;
1543
Bram Moolenaar080457c2020-03-03 21:53:32 +01001544 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1546 return FAIL;
1547 isn->isn_arg.forloop.for_idx = loop_idx;
1548
1549 if (ga_grow(stack, 1) == FAIL)
1550 return FAIL;
1551 // type doesn't matter, will be stored next
1552 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1553 ++stack->ga_len;
1554
1555 return OK;
1556}
1557
1558/*
1559 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001560 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 * Return FAIL if the number of arguments is wrong.
1562 */
1563 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001564generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565{
1566 isn_T *isn;
1567 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001568 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001569 type_T *argtypes[MAX_FUNC_ARGS];
1570 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571
Bram Moolenaar080457c2020-03-03 21:53:32 +01001572 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001573 argoff = check_internal_func(func_idx, argcount);
1574 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 return FAIL;
1576
Bram Moolenaar389df252020-07-09 21:20:47 +02001577 if (method_call && argoff > 1)
1578 {
1579 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1580 return FAIL;
1581 isn->isn_arg.shuffle.shfl_item = argcount;
1582 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1583 }
1584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1586 return FAIL;
1587 isn->isn_arg.bfunc.cbf_idx = func_idx;
1588 isn->isn_arg.bfunc.cbf_argcount = argcount;
1589
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001590 for (i = 0; i < argcount; ++i)
1591 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 stack->ga_len -= argcount; // drop the arguments
1594 if (ga_grow(stack, 1) == FAIL)
1595 return FAIL;
1596 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001597 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 ++stack->ga_len; // add return value
1599
1600 return OK;
1601}
1602
1603/*
1604 * Generate an ISN_DCALL or ISN_UCALL instruction.
1605 * Return FAIL if the number of arguments is wrong.
1606 */
1607 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001608generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609{
1610 isn_T *isn;
1611 garray_T *stack = &cctx->ctx_type_stack;
1612 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001613 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614
Bram Moolenaar080457c2020-03-03 21:53:32 +01001615 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 if (argcount > regular_args && !has_varargs(ufunc))
1617 {
1618 semsg(_(e_toomanyarg), ufunc->uf_name);
1619 return FAIL;
1620 }
1621 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1622 {
1623 semsg(_(e_toofewarg), ufunc->uf_name);
1624 return FAIL;
1625 }
1626
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001627 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001628 {
1629 int i;
1630
1631 for (i = 0; i < argcount; ++i)
1632 {
1633 type_T *expected;
1634 type_T *actual;
1635
1636 if (i < regular_args)
1637 {
1638 if (ufunc->uf_arg_types == NULL)
1639 continue;
1640 expected = ufunc->uf_arg_types[i];
1641 }
1642 else
1643 expected = ufunc->uf_va_type->tt_member;
1644 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001645 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001646 {
1647 arg_type_mismatch(expected, actual, i + 1);
1648 return FAIL;
1649 }
1650 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001651 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001652 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001653 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001654 }
1655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001657 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001658 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001660 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661 {
1662 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1663 isn->isn_arg.dfunc.cdf_argcount = argcount;
1664 }
1665 else
1666 {
1667 // A user function may be deleted and redefined later, can't use the
1668 // ufunc pointer, need to look it up again at runtime.
1669 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1670 isn->isn_arg.ufunc.cuf_argcount = argcount;
1671 }
1672
1673 stack->ga_len -= argcount; // drop the arguments
1674 if (ga_grow(stack, 1) == FAIL)
1675 return FAIL;
1676 // add return value
1677 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1678 ++stack->ga_len;
1679
1680 return OK;
1681}
1682
1683/*
1684 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1685 */
1686 static int
1687generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1688{
1689 isn_T *isn;
1690 garray_T *stack = &cctx->ctx_type_stack;
1691
Bram Moolenaar080457c2020-03-03 21:53:32 +01001692 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1694 return FAIL;
1695 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1696 isn->isn_arg.ufunc.cuf_argcount = argcount;
1697
1698 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001699 if (ga_grow(stack, 1) == FAIL)
1700 return FAIL;
1701 // add return value
1702 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1703 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704
1705 return OK;
1706}
1707
1708/*
1709 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001710 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 */
1712 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001713generate_PCALL(
1714 cctx_T *cctx,
1715 int argcount,
1716 char_u *name,
1717 type_T *type,
1718 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719{
1720 isn_T *isn;
1721 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001722 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723
Bram Moolenaar080457c2020-03-03 21:53:32 +01001724 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001725
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001726 if (type->tt_type == VAR_ANY)
1727 ret_type = &t_any;
1728 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001729 {
1730 if (type->tt_argcount != -1)
1731 {
1732 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1733
1734 if (argcount < type->tt_min_argcount - varargs)
1735 {
1736 semsg(_(e_toofewarg), "[reference]");
1737 return FAIL;
1738 }
1739 if (!varargs && argcount > type->tt_argcount)
1740 {
1741 semsg(_(e_toomanyarg), "[reference]");
1742 return FAIL;
1743 }
1744 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001745 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001746 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001747 else
1748 {
1749 semsg(_("E1085: Not a callable type: %s"), name);
1750 return FAIL;
1751 }
1752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1754 return FAIL;
1755 isn->isn_arg.pfunc.cpf_top = at_top;
1756 isn->isn_arg.pfunc.cpf_argcount = argcount;
1757
1758 stack->ga_len -= argcount; // drop the arguments
1759
1760 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001761 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001763 // If partial is above the arguments it must be cleared and replaced with
1764 // the return value.
1765 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1766 return FAIL;
1767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 return OK;
1769}
1770
1771/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001772 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 */
1774 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001775generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776{
1777 isn_T *isn;
1778 garray_T *stack = &cctx->ctx_type_stack;
1779 type_T *type;
1780
Bram Moolenaar080457c2020-03-03 21:53:32 +01001781 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001782 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001784 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001786 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001788 if (type->tt_type != VAR_DICT && type != &t_any)
1789 {
1790 emsg(_(e_dictreq));
1791 return FAIL;
1792 }
1793 // change dict type to dict member type
1794 if (type->tt_type == VAR_DICT)
1795 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796
1797 return OK;
1798}
1799
1800/*
1801 * Generate an ISN_ECHO instruction.
1802 */
1803 static int
1804generate_ECHO(cctx_T *cctx, int with_white, int count)
1805{
1806 isn_T *isn;
1807
Bram Moolenaar080457c2020-03-03 21:53:32 +01001808 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1810 return FAIL;
1811 isn->isn_arg.echo.echo_with_white = with_white;
1812 isn->isn_arg.echo.echo_count = count;
1813
1814 return OK;
1815}
1816
Bram Moolenaarad39c092020-02-26 18:23:43 +01001817/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001818 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001819 */
1820 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001821generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001822{
1823 isn_T *isn;
1824
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001825 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001826 return FAIL;
1827 isn->isn_arg.number = count;
1828
1829 return OK;
1830}
1831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 static int
1833generate_EXEC(cctx_T *cctx, char_u *line)
1834{
1835 isn_T *isn;
1836
Bram Moolenaar080457c2020-03-03 21:53:32 +01001837 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1839 return FAIL;
1840 isn->isn_arg.string = vim_strsave(line);
1841 return OK;
1842}
1843
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001844 static int
1845generate_EXECCONCAT(cctx_T *cctx, int count)
1846{
1847 isn_T *isn;
1848
1849 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1850 return FAIL;
1851 isn->isn_arg.number = count;
1852 return OK;
1853}
1854
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855/*
1856 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001857 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001859 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1861{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 lvar_T *lvar;
1863
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001864 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001866 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001867 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 }
1869
1870 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001871 return NULL;
1872 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001874 // Every local variable uses the next entry on the stack. We could re-use
1875 // the last ones when leaving a scope, but then variables used in a closure
1876 // might get overwritten. To keep things simple do not re-use stack
1877 // entries. This is less efficient, but memory is cheap these days.
1878 lvar->lv_idx = cctx->ctx_locals_count++;
1879
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001880 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 lvar->lv_const = isConst;
1882 lvar->lv_type = type;
1883
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001884 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885}
1886
1887/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001888 * Remove local variables above "new_top".
1889 */
1890 static void
1891unwind_locals(cctx_T *cctx, int new_top)
1892{
1893 if (cctx->ctx_locals.ga_len > new_top)
1894 {
1895 int idx;
1896 lvar_T *lvar;
1897
1898 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1899 {
1900 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1901 vim_free(lvar->lv_name);
1902 }
1903 }
1904 cctx->ctx_locals.ga_len = new_top;
1905}
1906
1907/*
1908 * Free all local variables.
1909 */
1910 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001911free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001912{
1913 unwind_locals(cctx, 0);
1914 ga_clear(&cctx->ctx_locals);
1915}
1916
1917/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 * Skip over a type definition and return a pointer to just after it.
1919 */
1920 char_u *
1921skip_type(char_u *start)
1922{
1923 char_u *p = start;
1924
1925 while (ASCII_ISALNUM(*p) || *p == '_')
1926 ++p;
1927
1928 // Skip over "<type>"; this is permissive about white space.
1929 if (*skipwhite(p) == '<')
1930 {
1931 p = skipwhite(p);
1932 p = skip_type(skipwhite(p + 1));
1933 p = skipwhite(p);
1934 if (*p == '>')
1935 ++p;
1936 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001937 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
1938 && STRNCMP("func", start, 4) == 0)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001939 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001940 if (*p == '(')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001941 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001942 // handle func(args): type
1943 ++p;
1944 while (*p != ')' && *p != NUL)
1945 {
1946 char_u *sp = p;
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001947
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001948 p = skip_type(p);
1949 if (p == sp)
1950 return p; // syntax error
1951 if (*p == ',')
1952 p = skipwhite(p + 1);
1953 }
1954 if (*p == ')')
1955 {
1956 if (p[1] == ':')
1957 p = skip_type(skipwhite(p + 2));
1958 else
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001959 ++p;
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001960 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001961 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001962 else
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001963 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001964 // handle func: return_type
1965 p = skip_type(skipwhite(p + 1));
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001966 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001967 }
1968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 return p;
1970}
1971
1972/*
1973 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001974 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 * Returns NULL in case of failure.
1976 */
1977 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001978parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979{
1980 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001981 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982
1983 if (**arg != '<')
1984 {
1985 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001986 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 else
1988 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001989 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 }
1991 *arg = skipwhite(*arg + 1);
1992
Bram Moolenaard77a8522020-04-03 21:59:57 +02001993 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994
1995 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001996 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 {
1998 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001999 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 }
2001 ++*arg;
2002
2003 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002004 return get_list_type(member_type, type_gap);
2005 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006}
2007
2008/*
2009 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02002010 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 */
2012 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002013parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014{
2015 char_u *p = *arg;
2016 size_t len;
2017
2018 // skip over the first word
2019 while (ASCII_ISALNUM(*p) || *p == '_')
2020 ++p;
2021 len = p - *arg;
2022
2023 switch (**arg)
2024 {
2025 case 'a':
2026 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
2027 {
2028 *arg += len;
2029 return &t_any;
2030 }
2031 break;
2032 case 'b':
2033 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
2034 {
2035 *arg += len;
2036 return &t_bool;
2037 }
2038 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
2039 {
2040 *arg += len;
2041 return &t_blob;
2042 }
2043 break;
2044 case 'c':
2045 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
2046 {
2047 *arg += len;
2048 return &t_channel;
2049 }
2050 break;
2051 case 'd':
2052 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
2053 {
2054 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002055 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 }
2057 break;
2058 case 'f':
2059 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
2060 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01002061#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 *arg += len;
2063 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002064#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002065 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002066 return &t_any;
2067#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 }
2069 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2070 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002071 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002072 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002073 int argcount = -1;
2074 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002075 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002076 type_T *arg_type[MAX_FUNC_ARGS + 1];
2077
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002078 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002080 if (**arg == '(')
2081 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002082 // "func" may or may not return a value, "func()" does
2083 // not return a value.
2084 ret_type = &t_void;
2085
Bram Moolenaard77a8522020-04-03 21:59:57 +02002086 p = ++*arg;
2087 argcount = 0;
2088 while (*p != NUL && *p != ')')
2089 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002090 if (*p == '?')
2091 {
2092 if (first_optional == -1)
2093 first_optional = argcount;
2094 ++p;
2095 }
2096 else if (first_optional != -1)
2097 {
2098 emsg(_("E1007: mandatory argument after optional argument"));
2099 return &t_any;
2100 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002101 else if (STRNCMP(p, "...", 3) == 0)
2102 {
2103 flags |= TTFLAG_VARARGS;
2104 p += 3;
2105 }
2106
2107 arg_type[argcount++] = parse_type(&p, type_gap);
2108
2109 // Nothing comes after "...{type}".
2110 if (flags & TTFLAG_VARARGS)
2111 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002112
Bram Moolenaard77a8522020-04-03 21:59:57 +02002113 if (*p != ',' && *skipwhite(p) == ',')
2114 {
2115 semsg(_(e_no_white_before), ",");
2116 return &t_any;
2117 }
2118 if (*p == ',')
2119 {
2120 ++p;
2121 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002122 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002123 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002124 return &t_any;
2125 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002126 }
2127 p = skipwhite(p);
2128 if (argcount == MAX_FUNC_ARGS)
2129 {
2130 emsg(_("E740: Too many argument types"));
2131 return &t_any;
2132 }
2133 }
2134
2135 p = skipwhite(p);
2136 if (*p != ')')
2137 {
2138 emsg(_(e_missing_close));
2139 return &t_any;
2140 }
2141 *arg = p + 1;
2142 }
2143 if (**arg == ':')
2144 {
2145 // parse return type
2146 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002147 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002148 semsg(_(e_white_after), ":");
2149 *arg = skipwhite(*arg);
2150 ret_type = parse_type(arg, type_gap);
2151 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002152 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002153 type = get_func_type(ret_type, argcount, type_gap);
2154 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002155 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002156 type = alloc_func_type(ret_type, argcount, type_gap);
2157 type->tt_flags = flags;
2158 if (argcount > 0)
2159 {
2160 type->tt_argcount = argcount;
2161 type->tt_min_argcount = first_optional == -1
2162 ? argcount : first_optional;
2163 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002164 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002165 return &t_any;
2166 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002167 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002168 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002169 }
2170 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 }
2172 break;
2173 case 'j':
2174 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2175 {
2176 *arg += len;
2177 return &t_job;
2178 }
2179 break;
2180 case 'l':
2181 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2182 {
2183 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002184 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 }
2186 break;
2187 case 'n':
2188 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2189 {
2190 *arg += len;
2191 return &t_number;
2192 }
2193 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 case 's':
2195 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2196 {
2197 *arg += len;
2198 return &t_string;
2199 }
2200 break;
2201 case 'v':
2202 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2203 {
2204 *arg += len;
2205 return &t_void;
2206 }
2207 break;
2208 }
2209
2210 semsg(_("E1010: Type not recognized: %s"), *arg);
2211 return &t_any;
2212}
2213
2214/*
2215 * Check if "type1" and "type2" are exactly the same.
2216 */
2217 static int
2218equal_type(type_T *type1, type_T *type2)
2219{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002220 int i;
2221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 if (type1->tt_type != type2->tt_type)
2223 return FALSE;
2224 switch (type1->tt_type)
2225 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002227 case VAR_ANY:
2228 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229 case VAR_SPECIAL:
2230 case VAR_BOOL:
2231 case VAR_NUMBER:
2232 case VAR_FLOAT:
2233 case VAR_STRING:
2234 case VAR_BLOB:
2235 case VAR_JOB:
2236 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002237 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 case VAR_LIST:
2239 case VAR_DICT:
2240 return equal_type(type1->tt_member, type2->tt_member);
2241 case VAR_FUNC:
2242 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002243 if (!equal_type(type1->tt_member, type2->tt_member)
2244 || type1->tt_argcount != type2->tt_argcount)
2245 return FALSE;
2246 if (type1->tt_argcount < 0
2247 || type1->tt_args == NULL || type2->tt_args == NULL)
2248 return TRUE;
2249 for (i = 0; i < type1->tt_argcount; ++i)
2250 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2251 return FALSE;
2252 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 }
2254 return TRUE;
2255}
2256
2257/*
2258 * Find the common type of "type1" and "type2" and put it in "dest".
2259 * "type2" and "dest" may be the same.
2260 */
2261 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002262common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263{
2264 if (equal_type(type1, type2))
2265 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002266 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 return;
2268 }
2269
2270 if (type1->tt_type == type2->tt_type)
2271 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2273 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002274 type_T *common;
2275
Bram Moolenaard77a8522020-04-03 21:59:57 +02002276 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002277 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002278 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002279 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002280 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 return;
2282 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002283 if (type1->tt_type == VAR_FUNC)
2284 {
2285 type_T *common;
2286
2287 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2288 if (type1->tt_argcount == type2->tt_argcount
2289 && type1->tt_argcount >= 0)
2290 {
2291 int argcount = type1->tt_argcount;
2292 int i;
2293
2294 *dest = alloc_func_type(common, argcount, type_gap);
2295 if (type1->tt_args != NULL && type2->tt_args != NULL)
2296 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002297 if (func_type_add_arg_types(*dest, argcount,
2298 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002299 for (i = 0; i < argcount; ++i)
2300 common_type(type1->tt_args[i], type2->tt_args[i],
2301 &(*dest)->tt_args[i], type_gap);
2302 }
2303 }
2304 else
2305 *dest = alloc_func_type(common, -1, type_gap);
2306 return;
2307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 }
2309
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002310 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311}
2312
2313 char *
2314vartype_name(vartype_T type)
2315{
2316 switch (type)
2317 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002318 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002319 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 case VAR_SPECIAL: return "special";
2322 case VAR_BOOL: return "bool";
2323 case VAR_NUMBER: return "number";
2324 case VAR_FLOAT: return "float";
2325 case VAR_STRING: return "string";
2326 case VAR_BLOB: return "blob";
2327 case VAR_JOB: return "job";
2328 case VAR_CHANNEL: return "channel";
2329 case VAR_LIST: return "list";
2330 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002331
2332 case VAR_FUNC:
2333 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002335 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336}
2337
2338/*
2339 * Return the name of a type.
2340 * The result may be in allocated memory, in which case "tofree" is set.
2341 */
2342 char *
2343type_name(type_T *type, char **tofree)
2344{
2345 char *name = vartype_name(type->tt_type);
2346
2347 *tofree = NULL;
2348 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2349 {
2350 char *member_free;
2351 char *member_name = type_name(type->tt_member, &member_free);
2352 size_t len;
2353
2354 len = STRLEN(name) + STRLEN(member_name) + 3;
2355 *tofree = alloc(len);
2356 if (*tofree != NULL)
2357 {
2358 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2359 vim_free(member_free);
2360 return *tofree;
2361 }
2362 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002363 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002364 {
2365 garray_T ga;
2366 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002367 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002368
2369 ga_init2(&ga, 1, 100);
2370 if (ga_grow(&ga, 20) == FAIL)
2371 return "[unknown]";
2372 *tofree = ga.ga_data;
2373 STRCPY(ga.ga_data, "func(");
2374 ga.ga_len += 5;
2375
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002376 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002377 {
2378 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002379 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002380 int len;
2381
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002382 if (type->tt_args == NULL)
2383 arg_type = "[unknown]";
2384 else
2385 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002386 if (i > 0)
2387 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002388 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002389 ga.ga_len += 2;
2390 }
2391 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002392 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002393 {
2394 vim_free(arg_free);
2395 return "[unknown]";
2396 }
2397 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002398 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002399 {
2400 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2401 ga.ga_len += 3;
2402 }
2403 else if (i >= type->tt_min_argcount)
2404 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002405 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002406 ga.ga_len += len;
2407 vim_free(arg_free);
2408 }
2409
2410 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002411 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002412 else
2413 {
2414 char *ret_free;
2415 char *ret_name = type_name(type->tt_member, &ret_free);
2416 int len;
2417
2418 len = (int)STRLEN(ret_name) + 4;
2419 if (ga_grow(&ga, len) == FAIL)
2420 {
2421 vim_free(ret_free);
2422 return "[unknown]";
2423 }
2424 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002425 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2426 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002427 vim_free(ret_free);
2428 }
2429 return ga.ga_data;
2430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431
2432 return name;
2433}
2434
2435/*
2436 * Find "name" in script-local items of script "sid".
2437 * Returns the index in "sn_var_vals" if found.
2438 * If found but not in "sn_var_vals" returns -1.
2439 * If not found returns -2.
2440 */
2441 int
2442get_script_item_idx(int sid, char_u *name, int check_writable)
2443{
2444 hashtab_T *ht;
2445 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002446 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 int idx;
2448
2449 // First look the name up in the hashtable.
2450 if (sid <= 0 || sid > script_items.ga_len)
2451 return -1;
2452 ht = &SCRIPT_VARS(sid);
2453 di = find_var_in_ht(ht, 0, name, TRUE);
2454 if (di == NULL)
2455 return -2;
2456
2457 // Now find the svar_T index in sn_var_vals.
2458 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2459 {
2460 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2461
2462 if (sv->sv_tv == &di->di_tv)
2463 {
2464 if (check_writable && sv->sv_const)
2465 semsg(_(e_readonlyvar), name);
2466 return idx;
2467 }
2468 }
2469 return -1;
2470}
2471
2472/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002473 * Find "name" in imported items of the current script or in "cctx" if not
2474 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 */
2476 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002477find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002479 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 int idx;
2481
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002482 if (current_sctx.sc_sid <= 0)
2483 return NULL;
2484 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 if (cctx != NULL)
2486 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2487 {
2488 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2489 + idx;
2490
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002491 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2492 : STRLEN(import->imp_name) == len
2493 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 return import;
2495 }
2496
2497 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2498 {
2499 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2500
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002501 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2502 : STRLEN(import->imp_name) == len
2503 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 return import;
2505 }
2506 return NULL;
2507}
2508
2509/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002510 * Free all imported variables.
2511 */
2512 static void
2513free_imported(cctx_T *cctx)
2514{
2515 int idx;
2516
2517 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2518 {
2519 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2520
2521 vim_free(import->imp_name);
2522 }
2523 ga_clear(&cctx->ctx_imports);
2524}
2525
2526/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002527 * Return TRUE if "p" points at a "#" but not at "#{".
2528 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002529 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002530vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002531{
2532 return p[0] == '#' && p[1] != '{';
2533}
2534
2535/*
2536 * Return a pointer to the next line that isn't empty or only contains a
2537 * comment. Skips over white space.
2538 * Returns NULL if there is none.
2539 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002540 char_u *
2541peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002542{
2543 int lnum = cctx->ctx_lnum;
2544
2545 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2546 {
2547 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002548 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002549
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002550 if (line == NULL)
2551 break;
2552 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002553 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002554 return p;
2555 }
2556 return NULL;
2557}
2558
2559/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002560 * Called when checking for a following operator at "arg". When the rest of
2561 * the line is empty or only a comment, peek the next line. If there is a next
2562 * line return a pointer to it and set "nextp".
2563 * Otherwise skip over white space.
2564 */
2565 static char_u *
2566may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2567{
2568 char_u *p = skipwhite(arg);
2569
2570 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002571 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002572 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002573 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002574 if (*nextp != NULL)
2575 return *nextp;
2576 }
2577 return p;
2578}
2579
2580/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002581 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002582 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002583 * Returns NULL when at the end.
2584 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002585 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002586next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002587{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002588 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002589
2590 do
2591 {
2592 ++cctx->ctx_lnum;
2593 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002594 {
2595 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002596 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002597 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002598 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002599 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002600 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002601 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002602 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002603 return line;
2604}
2605
2606/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002607 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002608 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002609 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2610 */
2611 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002612may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002613{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002614 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002615 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002616 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002617
2618 if (next == NULL)
2619 return FAIL;
2620 *arg = skipwhite(next);
2621 }
2622 return OK;
2623}
2624
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002625/*
2626 * Idem, and give an error when failed.
2627 */
2628 static int
2629may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2630{
2631 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2632 {
2633 emsg(_("E1097: line incomplete"));
2634 return FAIL;
2635 }
2636 return OK;
2637}
2638
2639
Bram Moolenaara5565e42020-05-09 15:44:01 +02002640// Structure passed between the compile_expr* functions to keep track of
2641// constants that have been parsed but for which no code was produced yet. If
2642// possible expressions on these constants are applied at compile time. If
2643// that is not possible, the code to push the constants needs to be generated
2644// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002645// Using 50 should be more than enough of 5 levels of ().
2646#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002647typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002648 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002649 int pp_used; // active entries in pp_tv[]
2650} ppconst_T;
2651
Bram Moolenaar1c747212020-05-09 18:28:34 +02002652static int compile_expr0(char_u **arg, cctx_T *cctx);
2653static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2654
Bram Moolenaara5565e42020-05-09 15:44:01 +02002655/*
2656 * Generate a PUSH instruction for "tv".
2657 * "tv" will be consumed or cleared.
2658 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2659 */
2660 static int
2661generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2662{
2663 if (tv != NULL)
2664 {
2665 switch (tv->v_type)
2666 {
2667 case VAR_UNKNOWN:
2668 break;
2669 case VAR_BOOL:
2670 generate_PUSHBOOL(cctx, tv->vval.v_number);
2671 break;
2672 case VAR_SPECIAL:
2673 generate_PUSHSPEC(cctx, tv->vval.v_number);
2674 break;
2675 case VAR_NUMBER:
2676 generate_PUSHNR(cctx, tv->vval.v_number);
2677 break;
2678#ifdef FEAT_FLOAT
2679 case VAR_FLOAT:
2680 generate_PUSHF(cctx, tv->vval.v_float);
2681 break;
2682#endif
2683 case VAR_BLOB:
2684 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2685 tv->vval.v_blob = NULL;
2686 break;
2687 case VAR_STRING:
2688 generate_PUSHS(cctx, tv->vval.v_string);
2689 tv->vval.v_string = NULL;
2690 break;
2691 default:
2692 iemsg("constant type not supported");
2693 clear_tv(tv);
2694 return FAIL;
2695 }
2696 tv->v_type = VAR_UNKNOWN;
2697 }
2698 return OK;
2699}
2700
2701/*
2702 * Generate code for any ppconst entries.
2703 */
2704 static int
2705generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2706{
2707 int i;
2708 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002709 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002710
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002711 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002712 for (i = 0; i < ppconst->pp_used; ++i)
2713 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2714 ret = FAIL;
2715 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002716 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002717 return ret;
2718}
2719
2720/*
2721 * Clear ppconst constants. Used when failing.
2722 */
2723 static void
2724clear_ppconst(ppconst_T *ppconst)
2725{
2726 int i;
2727
2728 for (i = 0; i < ppconst->pp_used; ++i)
2729 clear_tv(&ppconst->pp_tv[i]);
2730 ppconst->pp_used = 0;
2731}
2732
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002733/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002734 * Generate an instruction to load script-local variable "name", without the
2735 * leading "s:".
2736 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 */
2738 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002739compile_load_scriptvar(
2740 cctx_T *cctx,
2741 char_u *name, // variable NUL terminated
2742 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002743 char_u **end, // end of variable
2744 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002746 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2748 imported_T *import;
2749
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002750 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002752 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002753 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2754 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 }
2756 if (idx >= 0)
2757 {
2758 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2759
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002760 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 current_sctx.sc_sid, idx, sv->sv_type);
2762 return OK;
2763 }
2764
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002765 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 if (import != NULL)
2767 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002768 if (import->imp_all)
2769 {
2770 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002771 char_u *exp_name;
2772 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002773 ufunc_T *ufunc;
2774 type_T *type;
2775
2776 // Used "import * as Name", need to lookup the member.
2777 if (*p != '.')
2778 {
2779 semsg(_("E1060: expected dot after name: %s"), start);
2780 return FAIL;
2781 }
2782 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002783 if (VIM_ISWHITE(*p))
2784 {
2785 emsg(_("E1074: no white space allowed after dot"));
2786 return FAIL;
2787 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002788
Bram Moolenaar1c991142020-07-04 13:15:31 +02002789 // isolate one name
2790 exp_name = p;
2791 while (eval_isnamec(*p))
2792 ++p;
2793 cc = *p;
2794 *p = NUL;
2795
2796 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2797 *p = cc;
2798 p = skipwhite(p);
2799
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002800 // TODO: what if it is a function?
2801 if (idx < 0)
2802 return FAIL;
2803 *end = p;
2804
2805 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2806 import->imp_sid,
2807 idx,
2808 type);
2809 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002810 else if (import->imp_funcname != NULL)
2811 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002812 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002813 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2814 import->imp_sid,
2815 import->imp_var_vals_idx,
2816 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 return OK;
2818 }
2819
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002820 if (error)
2821 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 return FAIL;
2823}
2824
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002825 static int
2826generate_funcref(cctx_T *cctx, char_u *name)
2827{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002828 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002829
2830 if (ufunc == NULL)
2831 return FAIL;
2832
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002833 // Need to compile any default values to get the argument types.
2834 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2835 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2836 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002837 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002838}
2839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840/*
2841 * Compile a variable name into a load instruction.
2842 * "end" points to just after the name.
2843 * When "error" is FALSE do not give an error when not found.
2844 */
2845 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002846compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847{
2848 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002849 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002850 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002852 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853
2854 if (*(*arg + 1) == ':')
2855 {
2856 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002857 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002858 {
2859 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002861 switch (**arg)
2862 {
2863 case 'g': isn_type = ISN_LOADGDICT; break;
2864 case 'w': isn_type = ISN_LOADWDICT; break;
2865 case 't': isn_type = ISN_LOADTDICT; break;
2866 case 'b': isn_type = ISN_LOADBDICT; break;
2867 default:
2868 semsg(_(e_namespace), *arg);
2869 goto theend;
2870 }
2871 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2872 goto theend;
2873 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 else
2876 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002877 isntype_T isn_type = ISN_DROP;
2878
2879 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2880 if (name == NULL)
2881 return FAIL;
2882
2883 switch (**arg)
2884 {
2885 case 'v': res = generate_LOADV(cctx, name, error);
2886 break;
2887 case 's': res = compile_load_scriptvar(cctx, name,
2888 NULL, NULL, error);
2889 break;
2890 case 'g': isn_type = ISN_LOADG; break;
2891 case 'w': isn_type = ISN_LOADW; break;
2892 case 't': isn_type = ISN_LOADT; break;
2893 case 'b': isn_type = ISN_LOADB; break;
2894 default: semsg(_(e_namespace), *arg);
2895 goto theend;
2896 }
2897 if (isn_type != ISN_DROP)
2898 {
2899 // Global, Buffer-local, Window-local and Tabpage-local
2900 // variables can be defined later, thus we don't check if it
2901 // exists, give error at runtime.
2902 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2903 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 }
2905 }
2906 else
2907 {
2908 size_t len = end - *arg;
2909 int idx;
2910 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002911 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912
2913 name = vim_strnsave(*arg, end - *arg);
2914 if (name == NULL)
2915 return FAIL;
2916
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002917 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002919 if (!gen_load_outer)
2920 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 }
2922 else
2923 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002924 lvar_T *lvar = lookup_local(*arg, len, cctx);
2925
2926 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002928 type = lvar->lv_type;
2929 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002930 if (lvar->lv_from_outer)
2931 gen_load_outer = TRUE;
2932 else
2933 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 }
2935 else
2936 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002937 // "var" can be script-local even without using "s:" if it
2938 // already exists.
2939 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2940 == SCRIPT_VERSION_VIM9
2941 || lookup_script(*arg, len) == OK)
2942 res = compile_load_scriptvar(cctx, name, *arg, &end,
2943 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002944
Bram Moolenaara5565e42020-05-09 15:44:01 +02002945 // When the name starts with an uppercase letter or "x:" it
2946 // can be a user defined function.
2947 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2948 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 }
2950 }
2951 if (gen_load)
2952 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002953 if (gen_load_outer)
2954 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 }
2956
2957 *arg = end;
2958
2959theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002960 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 semsg(_(e_var_notfound), name);
2962 vim_free(name);
2963 return res;
2964}
2965
2966/*
2967 * Compile the argument expressions.
2968 * "arg" points to just after the "(" and is advanced to after the ")"
2969 */
2970 static int
2971compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2972{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002973 char_u *p = *arg;
2974 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975
Bram Moolenaare6085c52020-04-12 20:19:16 +02002976 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002978 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2979 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002980 if (*p == ')')
2981 {
2982 *arg = p + 1;
2983 return OK;
2984 }
2985
Bram Moolenaara5565e42020-05-09 15:44:01 +02002986 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 return FAIL;
2988 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002989
2990 if (*p != ',' && *skipwhite(p) == ',')
2991 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002992 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002993 p = skipwhite(p);
2994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002996 {
2997 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002998 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002999 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003000 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003001 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003002 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003004failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003005 emsg(_(e_missing_close));
3006 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007}
3008
3009/*
3010 * Compile a function call: name(arg1, arg2)
3011 * "arg" points to "name", "arg + varlen" to the "(".
3012 * "argcount_init" is 1 for "value->method()"
3013 * Instructions:
3014 * EVAL arg1
3015 * EVAL arg2
3016 * BCALL / DCALL / UCALL
3017 */
3018 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003019compile_call(
3020 char_u **arg,
3021 size_t varlen,
3022 cctx_T *cctx,
3023 ppconst_T *ppconst,
3024 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025{
3026 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003027 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 int argcount = argcount_init;
3029 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003030 char_u fname_buf[FLEN_FIXED + 1];
3031 char_u *tofree = NULL;
3032 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003034 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035
Bram Moolenaara5565e42020-05-09 15:44:01 +02003036 // we can evaluate "has('name')" at compile time
3037 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3038 {
3039 char_u *s = skipwhite(*arg + varlen + 1);
3040 typval_T argvars[2];
3041
3042 argvars[0].v_type = VAR_UNKNOWN;
3043 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003044 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003045 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003046 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003047 s = skipwhite(s);
3048 if (*s == ')' && argvars[0].v_type == VAR_STRING)
3049 {
3050 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3051
3052 *arg = s + 1;
3053 argvars[1].v_type = VAR_UNKNOWN;
3054 tv->v_type = VAR_NUMBER;
3055 tv->vval.v_number = 0;
3056 f_has(argvars, tv);
3057 clear_tv(&argvars[0]);
3058 ++ppconst->pp_used;
3059 return OK;
3060 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003061 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003062 }
3063
3064 if (generate_ppconst(cctx, ppconst) == FAIL)
3065 return FAIL;
3066
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 if (varlen >= sizeof(namebuf))
3068 {
3069 semsg(_("E1011: name too long: %s"), name);
3070 return FAIL;
3071 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003072 vim_strncpy(namebuf, *arg, varlen);
3073 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074
3075 *arg = skipwhite(*arg + varlen + 1);
3076 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003077 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003079 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 {
3081 int idx;
3082
3083 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003084 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003086 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003087 else
3088 semsg(_(e_unknownfunc), namebuf);
3089 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 }
3091
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003093 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003095 {
3096 res = generate_CALL(cctx, ufunc, argcount);
3097 goto theend;
3098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099
3100 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003101 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003103 if (STRNCMP(namebuf, "g:", 2) != 0
3104 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003105 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003106 garray_T *stack = &cctx->ctx_type_stack;
3107 type_T *type;
3108
3109 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3110 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003111 goto theend;
3112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003114 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003115 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003116 if (STRNCMP(namebuf, "g:", 2) == 0)
3117 res = generate_UCALL(cctx, name, argcount);
3118 else
3119 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003120
3121theend:
3122 vim_free(tofree);
3123 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124}
3125
3126// like NAMESPACE_CHAR but with 'a' and 'l'.
3127#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3128
3129/*
3130 * Find the end of a variable or function name. Unlike find_name_end() this
3131 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003132 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 * Return a pointer to just after the name. Equal to "arg" if there is no
3134 * valid name.
3135 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003136 static char_u *
3137to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138{
3139 char_u *p;
3140
3141 // Quick check for valid starting character.
3142 if (!eval_isnamec1(*arg))
3143 return arg;
3144
3145 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3146 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3147 // and can be used in slice "[n:]".
3148 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003149 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3151 break;
3152 return p;
3153}
3154
3155/*
3156 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003157 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 */
3159 char_u *
3160to_name_const_end(char_u *arg)
3161{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003162 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 typval_T rettv;
3164
3165 if (p == arg && *arg == '[')
3166 {
3167
3168 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003169 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 p = arg;
3171 }
3172 else if (p == arg && *arg == '#' && arg[1] == '{')
3173 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003174 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003176 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 p = arg;
3178 }
3179 else if (p == arg && *arg == '{')
3180 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003181 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003183 // Can be "{x -> ret}()".
3184 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003186 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 if (ret != OK)
3188 p = arg;
3189 }
3190
3191 return p;
3192}
3193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194/*
3195 * parse a list: [expr, expr]
3196 * "*arg" points to the '['.
3197 */
3198 static int
3199compile_list(char_u **arg, cctx_T *cctx)
3200{
3201 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003202 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 int count = 0;
3204
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003205 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003207 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003208 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003209 semsg(_(e_list_end), *arg);
3210 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003211 }
3212 if (*p == ']')
3213 {
3214 ++p;
3215 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003216 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003217 p += STRLEN(p);
3218 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003219 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003220 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 break;
3222 ++count;
3223 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003224 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003226 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3227 {
3228 semsg(_(e_white_after), ",");
3229 return FAIL;
3230 }
3231 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003232 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 p = skipwhite(p);
3234 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003235 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236
3237 generate_NEWLIST(cctx, count);
3238 return OK;
3239}
3240
3241/*
3242 * parse a lambda: {arg, arg -> expr}
3243 * "*arg" points to the '{'.
3244 */
3245 static int
3246compile_lambda(char_u **arg, cctx_T *cctx)
3247{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 typval_T rettv;
3249 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003250 evalarg_T evalarg;
3251
3252 CLEAR_FIELD(evalarg);
3253 evalarg.eval_flags = EVAL_EVALUATE;
3254 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255
3256 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003257 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003261 ++ufunc->uf_refcount;
3262 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003263 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264
3265 // The function will have one line: "return {expr}".
3266 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003267 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003269 clear_evalarg(&evalarg, NULL);
3270
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003271 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003272 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003273
3274 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 return FAIL;
3276}
3277
3278/*
3279 * Compile a lamda call: expr->{lambda}(args)
3280 * "arg" points to the "{".
3281 */
3282 static int
3283compile_lambda_call(char_u **arg, cctx_T *cctx)
3284{
3285 ufunc_T *ufunc;
3286 typval_T rettv;
3287 int argcount = 1;
3288 int ret = FAIL;
3289
3290 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003291 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 return FAIL;
3293
3294 if (**arg != '(')
3295 {
3296 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003297 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 else
3299 semsg(_(e_missing_paren), "lambda");
3300 clear_tv(&rettv);
3301 return FAIL;
3302 }
3303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 ufunc = rettv.vval.v_partial->pt_func;
3305 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003306 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003307 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003308
3309 // The function will have one line: "return {expr}".
3310 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003311 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312
3313 // compile the arguments
3314 *arg = skipwhite(*arg + 1);
3315 if (compile_arguments(arg, cctx, &argcount) == OK)
3316 // call the compiled function
3317 ret = generate_CALL(cctx, ufunc, argcount);
3318
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003319 if (ret == FAIL)
3320 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 return ret;
3322}
3323
3324/*
3325 * parse a dict: {'key': val} or #{key: val}
3326 * "*arg" points to the '{'.
3327 */
3328 static int
3329compile_dict(char_u **arg, cctx_T *cctx, int literal)
3330{
3331 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003332 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 int count = 0;
3334 dict_T *d = dict_alloc();
3335 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003336 char_u *whitep = *arg;
3337 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338
3339 if (d == NULL)
3340 return FAIL;
3341 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003342 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 {
3344 char_u *key = NULL;
3345
Bram Moolenaar23c55272020-06-21 16:58:13 +02003346 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003347 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003348 *arg = NULL;
3349 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003350 }
3351
3352 if (**arg == '}')
3353 break;
3354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 if (literal)
3356 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003357 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358
Bram Moolenaar2c330432020-04-13 14:41:35 +02003359 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 {
3361 semsg(_("E1014: Invalid key: %s"), *arg);
3362 return FAIL;
3363 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003364 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 if (generate_PUSHS(cctx, key) == FAIL)
3366 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003367 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 }
3369 else
3370 {
3371 isn_T *isn;
3372
Bram Moolenaara5565e42020-05-09 15:44:01 +02003373 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3376 if (isn->isn_type == ISN_PUSHS)
3377 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003378 else
3379 {
3380 type_T *keytype = ((type_T **)stack->ga_data)
3381 [stack->ga_len - 1];
3382 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3383 return FAIL;
3384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 }
3386
3387 // Check for duplicate keys, if using string keys.
3388 if (key != NULL)
3389 {
3390 item = dict_find(d, key, -1);
3391 if (item != NULL)
3392 {
3393 semsg(_(e_duplicate_key), key);
3394 goto failret;
3395 }
3396 item = dictitem_alloc(key);
3397 if (item != NULL)
3398 {
3399 item->di_tv.v_type = VAR_UNKNOWN;
3400 item->di_tv.v_lock = 0;
3401 if (dict_add(d, item) == FAIL)
3402 dictitem_free(item);
3403 }
3404 }
3405
3406 *arg = skipwhite(*arg);
3407 if (**arg != ':')
3408 {
3409 semsg(_(e_missing_dict_colon), *arg);
3410 return FAIL;
3411 }
3412
Bram Moolenaar2c330432020-04-13 14:41:35 +02003413 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003415 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003416 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003417 *arg = NULL;
3418 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003419 }
3420
Bram Moolenaara5565e42020-05-09 15:44:01 +02003421 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 return FAIL;
3423 ++count;
3424
Bram Moolenaar2c330432020-04-13 14:41:35 +02003425 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003426 *arg = skipwhite(*arg);
3427 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003428 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003429 *arg = NULL;
3430 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003431 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 if (**arg == '}')
3433 break;
3434 if (**arg != ',')
3435 {
3436 semsg(_(e_missing_dict_comma), *arg);
3437 goto failret;
3438 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003439 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 *arg = skipwhite(*arg + 1);
3441 }
3442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 *arg = *arg + 1;
3444
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003445 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003446 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003447 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003448 *arg += STRLEN(*arg);
3449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 dict_unref(d);
3451 return generate_NEWDICT(cctx, count);
3452
3453failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003454 if (*arg == NULL)
3455 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 dict_unref(d);
3457 return FAIL;
3458}
3459
3460/*
3461 * Compile "&option".
3462 */
3463 static int
3464compile_get_option(char_u **arg, cctx_T *cctx)
3465{
3466 typval_T rettv;
3467 char_u *start = *arg;
3468 int ret;
3469
3470 // parse the option and get the current value to get the type.
3471 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003472 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 if (ret == OK)
3474 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003475 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 char_u *name = vim_strnsave(start, *arg - start);
3477 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3478
3479 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3480 vim_free(name);
3481 }
3482 clear_tv(&rettv);
3483
3484 return ret;
3485}
3486
3487/*
3488 * Compile "$VAR".
3489 */
3490 static int
3491compile_get_env(char_u **arg, cctx_T *cctx)
3492{
3493 char_u *start = *arg;
3494 int len;
3495 int ret;
3496 char_u *name;
3497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 ++*arg;
3499 len = get_env_len(arg);
3500 if (len == 0)
3501 {
3502 semsg(_(e_syntax_at), start - 1);
3503 return FAIL;
3504 }
3505
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003506 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 name = vim_strnsave(start, len + 1);
3508 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3509 vim_free(name);
3510 return ret;
3511}
3512
3513/*
3514 * Compile "@r".
3515 */
3516 static int
3517compile_get_register(char_u **arg, cctx_T *cctx)
3518{
3519 int ret;
3520
3521 ++*arg;
3522 if (**arg == NUL)
3523 {
3524 semsg(_(e_syntax_at), *arg - 1);
3525 return FAIL;
3526 }
3527 if (!valid_yank_reg(**arg, TRUE))
3528 {
3529 emsg_invreg(**arg);
3530 return FAIL;
3531 }
3532 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3533 ++*arg;
3534 return ret;
3535}
3536
3537/*
3538 * Apply leading '!', '-' and '+' to constant "rettv".
3539 */
3540 static int
3541apply_leader(typval_T *rettv, char_u *start, char_u *end)
3542{
3543 char_u *p = end;
3544
3545 // this works from end to start
3546 while (p > start)
3547 {
3548 --p;
3549 if (*p == '-' || *p == '+')
3550 {
3551 // only '-' has an effect, for '+' we only check the type
3552#ifdef FEAT_FLOAT
3553 if (rettv->v_type == VAR_FLOAT)
3554 {
3555 if (*p == '-')
3556 rettv->vval.v_float = -rettv->vval.v_float;
3557 }
3558 else
3559#endif
3560 {
3561 varnumber_T val;
3562 int error = FALSE;
3563
3564 // tv_get_number_chk() accepts a string, but we don't want that
3565 // here
3566 if (check_not_string(rettv) == FAIL)
3567 return FAIL;
3568 val = tv_get_number_chk(rettv, &error);
3569 clear_tv(rettv);
3570 if (error)
3571 return FAIL;
3572 if (*p == '-')
3573 val = -val;
3574 rettv->v_type = VAR_NUMBER;
3575 rettv->vval.v_number = val;
3576 }
3577 }
3578 else
3579 {
3580 int v = tv2bool(rettv);
3581
3582 // '!' is permissive in the type.
3583 clear_tv(rettv);
3584 rettv->v_type = VAR_BOOL;
3585 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3586 }
3587 }
3588 return OK;
3589}
3590
3591/*
3592 * Recognize v: variables that are constants and set "rettv".
3593 */
3594 static void
3595get_vim_constant(char_u **arg, typval_T *rettv)
3596{
3597 if (STRNCMP(*arg, "v:true", 6) == 0)
3598 {
3599 rettv->v_type = VAR_BOOL;
3600 rettv->vval.v_number = VVAL_TRUE;
3601 *arg += 6;
3602 }
3603 else if (STRNCMP(*arg, "v:false", 7) == 0)
3604 {
3605 rettv->v_type = VAR_BOOL;
3606 rettv->vval.v_number = VVAL_FALSE;
3607 *arg += 7;
3608 }
3609 else if (STRNCMP(*arg, "v:null", 6) == 0)
3610 {
3611 rettv->v_type = VAR_SPECIAL;
3612 rettv->vval.v_number = VVAL_NULL;
3613 *arg += 6;
3614 }
3615 else if (STRNCMP(*arg, "v:none", 6) == 0)
3616 {
3617 rettv->v_type = VAR_SPECIAL;
3618 rettv->vval.v_number = VVAL_NONE;
3619 *arg += 6;
3620 }
3621}
3622
Bram Moolenaar61a89812020-05-07 16:58:17 +02003623 static exptype_T
3624get_compare_type(char_u *p, int *len, int *type_is)
3625{
3626 exptype_T type = EXPR_UNKNOWN;
3627 int i;
3628
3629 switch (p[0])
3630 {
3631 case '=': if (p[1] == '=')
3632 type = EXPR_EQUAL;
3633 else if (p[1] == '~')
3634 type = EXPR_MATCH;
3635 break;
3636 case '!': if (p[1] == '=')
3637 type = EXPR_NEQUAL;
3638 else if (p[1] == '~')
3639 type = EXPR_NOMATCH;
3640 break;
3641 case '>': if (p[1] != '=')
3642 {
3643 type = EXPR_GREATER;
3644 *len = 1;
3645 }
3646 else
3647 type = EXPR_GEQUAL;
3648 break;
3649 case '<': if (p[1] != '=')
3650 {
3651 type = EXPR_SMALLER;
3652 *len = 1;
3653 }
3654 else
3655 type = EXPR_SEQUAL;
3656 break;
3657 case 'i': if (p[1] == 's')
3658 {
3659 // "is" and "isnot"; but not a prefix of a name
3660 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3661 *len = 5;
3662 i = p[*len];
3663 if (!isalnum(i) && i != '_')
3664 {
3665 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3666 *type_is = TRUE;
3667 }
3668 }
3669 break;
3670 }
3671 return type;
3672}
3673
3674/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 * Compile code to apply '-', '+' and '!'.
3676 */
3677 static int
3678compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3679{
3680 char_u *p = end;
3681
3682 // this works from end to start
3683 while (p > start)
3684 {
3685 --p;
3686 if (*p == '-' || *p == '+')
3687 {
3688 int negate = *p == '-';
3689 isn_T *isn;
3690
3691 // TODO: check type
3692 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3693 {
3694 --p;
3695 if (*p == '-')
3696 negate = !negate;
3697 }
3698 // only '-' has an effect, for '+' we only check the type
3699 if (negate)
3700 isn = generate_instr(cctx, ISN_NEGATENR);
3701 else
3702 isn = generate_instr(cctx, ISN_CHECKNR);
3703 if (isn == NULL)
3704 return FAIL;
3705 }
3706 else
3707 {
3708 int invert = TRUE;
3709
3710 while (p > start && p[-1] == '!')
3711 {
3712 --p;
3713 invert = !invert;
3714 }
3715 if (generate_2BOOL(cctx, invert) == FAIL)
3716 return FAIL;
3717 }
3718 }
3719 return OK;
3720}
3721
3722/*
3723 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003724 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 */
3726 static int
3727compile_subscript(
3728 char_u **arg,
3729 cctx_T *cctx,
3730 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003731 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003732 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733{
3734 for (;;)
3735 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003736 char_u *p = skipwhite(*arg);
3737
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003738 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003739 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003740 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003741
3742 // If a following line starts with "->{" or "->X" advance to that
3743 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003744 // Also if a following line starts with ".x".
3745 if (next != NULL &&
3746 ((next[0] == '-' && next[1] == '>'
3747 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3748 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003749 {
3750 next = next_line_from_context(cctx, TRUE);
3751 if (next == NULL)
3752 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003753 *arg = next;
3754 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003755 }
3756 }
3757
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003758 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003760 garray_T *stack = &cctx->ctx_type_stack;
3761 type_T *type;
3762 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003764 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003765 return FAIL;
3766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003768 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3769
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003770 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3772 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003773 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 return FAIL;
3775 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003776 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003778 char_u *pstart = p;
3779
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003780 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003781 return FAIL;
3782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 // something->method()
3784 // Apply the '!', '-' and '+' first:
3785 // -1.0->func() works like (-1.0)->func()
3786 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3787 return FAIL;
3788 *start_leader = end_leader; // don't apply again later
3789
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003790 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003791 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003792 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 if (**arg == '{')
3794 {
3795 // lambda call: list->{lambda}
3796 if (compile_lambda_call(arg, cctx) == FAIL)
3797 return FAIL;
3798 }
3799 else
3800 {
3801 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003802 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003803 if (!eval_isnamec1(*p))
3804 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003805 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003806 return FAIL;
3807 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003808 if (ASCII_ISALPHA(*p) && p[1] == ':')
3809 p += 2;
3810 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 ;
3812 if (*p != '(')
3813 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003814 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 return FAIL;
3816 }
3817 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003818 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 return FAIL;
3820 }
3821 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003822 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003824 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003825 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003826 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003827
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003828 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003829 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003830 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003831 // TODO: blob index
3832 // TODO: more arguments
3833 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003834 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003835 return FAIL;
3836
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003837 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003838 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003839 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003840 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003841 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 return FAIL;
3843
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003844 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3845 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 if (**arg != ']')
3847 {
3848 emsg(_(e_missbrac));
3849 return FAIL;
3850 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003851 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003853 // We can index a list and a dict. If we don't know the type
3854 // we can use the index value type.
3855 // TODO: If we don't know use an instruction to figure it out at
3856 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003857 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003858 vtype = (*typep)->tt_type;
3859 if (*typep == &t_any)
3860 {
3861 type_T *valtype = ((type_T **)stack->ga_data)
3862 [stack->ga_len - 1];
3863 if (valtype == &t_string)
3864 vtype = VAR_DICT;
3865 }
3866 if (vtype == VAR_DICT)
3867 {
3868 if ((*typep)->tt_type == VAR_DICT)
3869 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003870 else
3871 {
3872 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3873 return FAIL;
3874 *typep = &t_any;
3875 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003876 if (may_generate_2STRING(-1, cctx) == FAIL)
3877 return FAIL;
3878 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3879 return FAIL;
3880 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003881 else if (vtype == VAR_STRING)
3882 {
3883 *typep = &t_number;
3884 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3885 return FAIL;
3886 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003887 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003888 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003889 if ((*typep)->tt_type == VAR_LIST)
3890 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003891 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003892 return FAIL;
3893 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003894 else
3895 {
3896 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003897 return FAIL;
3898 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003900 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003902 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003903 return FAIL;
3904
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003905 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003906 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3907 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003909 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 if (eval_isnamec1(*p))
3911 while (eval_isnamec(*p))
3912 MB_PTR_ADV(p);
3913 if (p == *arg)
3914 {
3915 semsg(_(e_syntax_at), *arg);
3916 return FAIL;
3917 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003918 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 return FAIL;
3920 *arg = p;
3921 }
3922 else
3923 break;
3924 }
3925
3926 // TODO - see handle_subscript():
3927 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3928 // Don't do this when "Func" is already a partial that was bound
3929 // explicitly (pt_auto is FALSE).
3930
3931 return OK;
3932}
3933
3934/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003935 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3936 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003938 * If the value is a constant "ppconst->pp_ret" will be set.
3939 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003940 *
3941 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 */
3943
3944/*
3945 * number number constant
3946 * 0zFFFFFFFF Blob constant
3947 * "string" string constant
3948 * 'string' literal string constant
3949 * &option-name option value
3950 * @r register contents
3951 * identifier variable value
3952 * function() function call
3953 * $VAR environment variable
3954 * (expression) nested expression
3955 * [expr, expr] List
3956 * {key: val, key: val} Dictionary
3957 * #{key: val, key: val} Dictionary with literal keys
3958 *
3959 * Also handle:
3960 * ! in front logical NOT
3961 * - in front unary minus
3962 * + in front unary plus (ignored)
3963 * trailing (arg) funcref/partial call
3964 * trailing [] subscript in String or List
3965 * trailing .name entry in Dictionary
3966 * trailing ->name() method call
3967 */
3968 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003969compile_expr7(
3970 char_u **arg,
3971 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003972 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 char_u *start_leader, *end_leader;
3975 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003976 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003977 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978
3979 /*
3980 * Skip '!', '-' and '+' characters. They are handled later.
3981 */
3982 start_leader = *arg;
3983 while (**arg == '!' || **arg == '-' || **arg == '+')
3984 *arg = skipwhite(*arg + 1);
3985 end_leader = *arg;
3986
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003987 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 switch (**arg)
3989 {
3990 /*
3991 * Number constant.
3992 */
3993 case '0': // also for blob starting with 0z
3994 case '1':
3995 case '2':
3996 case '3':
3997 case '4':
3998 case '5':
3999 case '6':
4000 case '7':
4001 case '8':
4002 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004003 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 return FAIL;
4005 break;
4006
4007 /*
4008 * String constant: "string".
4009 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004010 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 return FAIL;
4012 break;
4013
4014 /*
4015 * Literal string constant: 'str''ing'.
4016 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004017 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 return FAIL;
4019 break;
4020
4021 /*
4022 * Constant Vim variable.
4023 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004024 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 ret = NOTDONE;
4026 break;
4027
4028 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004029 * "true" constant
4030 */
4031 case 't': if (STRNCMP(*arg, "true", 4) == 0
4032 && !eval_isnamec((*arg)[4]))
4033 {
4034 *arg += 4;
4035 rettv->v_type = VAR_BOOL;
4036 rettv->vval.v_number = VVAL_TRUE;
4037 }
4038 else
4039 ret = NOTDONE;
4040 break;
4041
4042 /*
4043 * "false" constant
4044 */
4045 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4046 && !eval_isnamec((*arg)[5]))
4047 {
4048 *arg += 5;
4049 rettv->v_type = VAR_BOOL;
4050 rettv->vval.v_number = VVAL_FALSE;
4051 }
4052 else
4053 ret = NOTDONE;
4054 break;
4055
4056 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 * List: [expr, expr]
4058 */
4059 case '[': ret = compile_list(arg, cctx);
4060 break;
4061
4062 /*
4063 * Dictionary: #{key: val, key: val}
4064 */
4065 case '#': if ((*arg)[1] == '{')
4066 {
4067 ++*arg;
4068 ret = compile_dict(arg, cctx, TRUE);
4069 }
4070 else
4071 ret = NOTDONE;
4072 break;
4073
4074 /*
4075 * Lambda: {arg, arg -> expr}
4076 * Dictionary: {'key': val, 'key': val}
4077 */
4078 case '{': {
4079 char_u *start = skipwhite(*arg + 1);
4080
4081 // Find out what comes after the arguments.
4082 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004083 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 if (ret != FAIL && *start == '>')
4085 ret = compile_lambda(arg, cctx);
4086 else
4087 ret = compile_dict(arg, cctx, FALSE);
4088 }
4089 break;
4090
4091 /*
4092 * Option value: &name
4093 */
4094 case '&': ret = compile_get_option(arg, cctx);
4095 break;
4096
4097 /*
4098 * Environment variable: $VAR.
4099 */
4100 case '$': ret = compile_get_env(arg, cctx);
4101 break;
4102
4103 /*
4104 * Register contents: @r.
4105 */
4106 case '@': ret = compile_get_register(arg, cctx);
4107 break;
4108 /*
4109 * nested expression: (expression).
4110 */
4111 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004112
4113 // recursive!
4114 if (ppconst->pp_used <= PPSIZE - 10)
4115 {
4116 ret = compile_expr1(arg, cctx, ppconst);
4117 }
4118 else
4119 {
4120 // Not enough space in ppconst, flush constants.
4121 if (generate_ppconst(cctx, ppconst) == FAIL)
4122 return FAIL;
4123 ret = compile_expr0(arg, cctx);
4124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 *arg = skipwhite(*arg);
4126 if (**arg == ')')
4127 ++*arg;
4128 else if (ret == OK)
4129 {
4130 emsg(_(e_missing_close));
4131 ret = FAIL;
4132 }
4133 break;
4134
4135 default: ret = NOTDONE;
4136 break;
4137 }
4138 if (ret == FAIL)
4139 return FAIL;
4140
Bram Moolenaar1c747212020-05-09 18:28:34 +02004141 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 {
4143 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004144 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004146 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 return FAIL;
4148 }
4149 start_leader = end_leader; // don't apply again below
4150
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004151 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004152 clear_tv(rettv);
4153 else
4154 // A constant expression can possibly be handled compile time,
4155 // return the value instead of generating code.
4156 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 }
4158 else if (ret == NOTDONE)
4159 {
4160 char_u *p;
4161 int r;
4162
4163 if (!eval_isnamec1(**arg))
4164 {
4165 semsg(_("E1015: Name expected: %s"), *arg);
4166 return FAIL;
4167 }
4168
4169 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004170 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004172 {
4173 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004176 {
4177 if (generate_ppconst(cctx, ppconst) == FAIL)
4178 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 if (r == FAIL)
4182 return FAIL;
4183 }
4184
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004185 // Handle following "[]", ".member", etc.
4186 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004187 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004188 ppconst) == FAIL)
4189 return FAIL;
4190 if (ppconst->pp_used > 0)
4191 {
4192 // apply the '!', '-' and '+' before the constant
4193 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4194 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4195 return FAIL;
4196 return OK;
4197 }
4198 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004200 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201}
4202
4203/*
4204 * * number multiplication
4205 * / number division
4206 * % number modulo
4207 */
4208 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004209compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210{
4211 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004212 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004213 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004215 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004216 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 return FAIL;
4218
4219 /*
4220 * Repeat computing, until no "*", "/" or "%" is following.
4221 */
4222 for (;;)
4223 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004224 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 if (*op != '*' && *op != '/' && *op != '%')
4226 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004227 if (next != NULL)
4228 {
4229 *arg = next_line_from_context(cctx, TRUE);
4230 op = skipwhite(*arg);
4231 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004232
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004233 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 {
4235 char_u buf[3];
4236
4237 vim_strncpy(buf, op, 1);
4238 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004239 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 }
4241 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004242 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004243 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004245 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004246 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004248
4249 if (ppconst->pp_used == ppconst_used + 2
4250 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4251 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004252 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004253 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4254 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004255 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004257 // both are numbers: compute the result
4258 switch (*op)
4259 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004260 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004261 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004262 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004263 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004264 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004265 break;
4266 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004267 tv1->vval.v_number = res;
4268 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004269 }
4270 else
4271 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004272 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004273 generate_two_op(cctx, op);
4274 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 }
4276
4277 return OK;
4278}
4279
4280/*
4281 * + number addition
4282 * - number subtraction
4283 * .. string concatenation
4284 */
4285 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004286compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287{
4288 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004289 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004291 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292
4293 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004294 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 return FAIL;
4296
4297 /*
4298 * Repeat computing, until no "+", "-" or ".." is following.
4299 */
4300 for (;;)
4301 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004302 op = may_peek_next_line(cctx, *arg, &next);
4303 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 break;
4305 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004306 if (next != NULL)
4307 {
4308 *arg = next_line_from_context(cctx, TRUE);
4309 op = skipwhite(*arg);
4310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004312 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 {
4314 char_u buf[3];
4315
4316 vim_strncpy(buf, op, oplen);
4317 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004318 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 }
4320
4321 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004322 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004323 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004325 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004326 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 return FAIL;
4328
Bram Moolenaara5565e42020-05-09 15:44:01 +02004329 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004330 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004331 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4332 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4333 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4334 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004336 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4337 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004338
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004339 // concat/subtract/add constant numbers
4340 if (*op == '+')
4341 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4342 else if (*op == '-')
4343 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4344 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004345 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004346 // concatenate constant strings
4347 char_u *s1 = tv1->vval.v_string;
4348 char_u *s2 = tv2->vval.v_string;
4349 size_t len1 = STRLEN(s1);
4350
4351 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4352 if (tv1->vval.v_string == NULL)
4353 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004354 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004355 return FAIL;
4356 }
4357 mch_memmove(tv1->vval.v_string, s1, len1);
4358 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004359 vim_free(s1);
4360 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004361 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004362 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 }
4364 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004365 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004366 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004367 if (*op == '.')
4368 {
4369 if (may_generate_2STRING(-2, cctx) == FAIL
4370 || may_generate_2STRING(-1, cctx) == FAIL)
4371 return FAIL;
4372 generate_instr_drop(cctx, ISN_CONCAT, 1);
4373 }
4374 else
4375 generate_two_op(cctx, op);
4376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 }
4378
4379 return OK;
4380}
4381
4382/*
4383 * expr5a == expr5b
4384 * expr5a =~ expr5b
4385 * expr5a != expr5b
4386 * expr5a !~ expr5b
4387 * expr5a > expr5b
4388 * expr5a >= expr5b
4389 * expr5a < expr5b
4390 * expr5a <= expr5b
4391 * expr5a is expr5b
4392 * expr5a isnot expr5b
4393 *
4394 * Produces instructions:
4395 * EVAL expr5a Push result of "expr5a"
4396 * EVAL expr5b Push result of "expr5b"
4397 * COMPARE one of the compare instructions
4398 */
4399 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004400compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401{
4402 exptype_T type = EXPR_UNKNOWN;
4403 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004404 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004407 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408
4409 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004410 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 return FAIL;
4412
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004413 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004414 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415
4416 /*
4417 * If there is a comparative operator, use it.
4418 */
4419 if (type != EXPR_UNKNOWN)
4420 {
4421 int ic = FALSE; // Default: do not ignore case
4422
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004423 if (next != NULL)
4424 {
4425 *arg = next_line_from_context(cctx, TRUE);
4426 p = skipwhite(*arg);
4427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428 if (type_is && (p[len] == '?' || p[len] == '#'))
4429 {
4430 semsg(_(e_invexpr2), *arg);
4431 return FAIL;
4432 }
4433 // extra question mark appended: ignore case
4434 if (p[len] == '?')
4435 {
4436 ic = TRUE;
4437 ++len;
4438 }
4439 // extra '#' appended: match case (ignored)
4440 else if (p[len] == '#')
4441 ++len;
4442 // nothing appended: match case
4443
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004444 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 {
4446 char_u buf[7];
4447
4448 vim_strncpy(buf, p, len);
4449 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004450 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 }
4452
4453 // get the second variable
4454 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004455 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004456 return FAIL;
4457
Bram Moolenaara5565e42020-05-09 15:44:01 +02004458 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 return FAIL;
4460
Bram Moolenaara5565e42020-05-09 15:44:01 +02004461 if (ppconst->pp_used == ppconst_used + 2)
4462 {
4463 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4464 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4465 int ret;
4466
4467 // Both sides are a constant, compute the result now.
4468 // First check for a valid combination of types, this is more
4469 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004470 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004471 ret = FAIL;
4472 else
4473 {
4474 ret = typval_compare(tv1, tv2, type, ic);
4475 tv1->v_type = VAR_BOOL;
4476 tv1->vval.v_number = tv1->vval.v_number
4477 ? VVAL_TRUE : VVAL_FALSE;
4478 clear_tv(tv2);
4479 --ppconst->pp_used;
4480 }
4481 return ret;
4482 }
4483
4484 generate_ppconst(cctx, ppconst);
4485 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 }
4487
4488 return OK;
4489}
4490
Bram Moolenaar7f141552020-05-09 17:35:53 +02004491static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493/*
4494 * Compile || or &&.
4495 */
4496 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004497compile_and_or(
4498 char_u **arg,
4499 cctx_T *cctx,
4500 char *op,
4501 ppconst_T *ppconst,
4502 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004504 char_u *next;
4505 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 int opchar = *op;
4507
4508 if (p[0] == opchar && p[1] == opchar)
4509 {
4510 garray_T *instr = &cctx->ctx_instr;
4511 garray_T end_ga;
4512
4513 /*
4514 * Repeat until there is no following "||" or "&&"
4515 */
4516 ga_init2(&end_ga, sizeof(int), 10);
4517 while (p[0] == opchar && p[1] == opchar)
4518 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004519 if (next != NULL)
4520 {
4521 *arg = next_line_from_context(cctx, TRUE);
4522 p = skipwhite(*arg);
4523 }
4524
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004525 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4526 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004528 return FAIL;
4529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530
Bram Moolenaara5565e42020-05-09 15:44:01 +02004531 // TODO: use ppconst if the value is a constant
4532 generate_ppconst(cctx, ppconst);
4533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 if (ga_grow(&end_ga, 1) == FAIL)
4535 {
4536 ga_clear(&end_ga);
4537 return FAIL;
4538 }
4539 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4540 ++end_ga.ga_len;
4541 generate_JUMP(cctx, opchar == '|'
4542 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4543
4544 // eval the next expression
4545 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004546 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004547 return FAIL;
4548
Bram Moolenaara5565e42020-05-09 15:44:01 +02004549 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4550 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 {
4552 ga_clear(&end_ga);
4553 return FAIL;
4554 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004555
4556 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004558 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559
4560 // Fill in the end label in all jumps.
4561 while (end_ga.ga_len > 0)
4562 {
4563 isn_T *isn;
4564
4565 --end_ga.ga_len;
4566 isn = ((isn_T *)instr->ga_data)
4567 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4568 isn->isn_arg.jump.jump_where = instr->ga_len;
4569 }
4570 ga_clear(&end_ga);
4571 }
4572
4573 return OK;
4574}
4575
4576/*
4577 * expr4a && expr4a && expr4a logical AND
4578 *
4579 * Produces instructions:
4580 * EVAL expr4a Push result of "expr4a"
4581 * JUMP_AND_KEEP_IF_FALSE end
4582 * EVAL expr4b Push result of "expr4b"
4583 * JUMP_AND_KEEP_IF_FALSE end
4584 * EVAL expr4c Push result of "expr4c"
4585 * end:
4586 */
4587 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004588compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004590 int ppconst_used = ppconst->pp_used;
4591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004593 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 return FAIL;
4595
4596 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598}
4599
4600/*
4601 * expr3a || expr3b || expr3c logical OR
4602 *
4603 * Produces instructions:
4604 * EVAL expr3a Push result of "expr3a"
4605 * JUMP_AND_KEEP_IF_TRUE end
4606 * EVAL expr3b Push result of "expr3b"
4607 * JUMP_AND_KEEP_IF_TRUE end
4608 * EVAL expr3c Push result of "expr3c"
4609 * end:
4610 */
4611 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004612compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004614 int ppconst_used = ppconst->pp_used;
4615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004617 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 return FAIL;
4619
4620 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004621 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622}
4623
4624/*
4625 * Toplevel expression: expr2 ? expr1a : expr1b
4626 *
4627 * Produces instructions:
4628 * EVAL expr2 Push result of "expr"
4629 * JUMP_IF_FALSE alt jump if false
4630 * EVAL expr1a
4631 * JUMP_ALWAYS end
4632 * alt: EVAL expr1b
4633 * end:
4634 */
4635 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004636compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637{
4638 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004639 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004640 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641
Bram Moolenaar61a89812020-05-07 16:58:17 +02004642 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004643 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 return FAIL;
4645
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004646 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 if (*p == '?')
4648 {
4649 garray_T *instr = &cctx->ctx_instr;
4650 garray_T *stack = &cctx->ctx_type_stack;
4651 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004652 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004654 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004656 int has_const_expr = FALSE;
4657 int const_value = FALSE;
4658 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004660 if (next != NULL)
4661 {
4662 *arg = next_line_from_context(cctx, TRUE);
4663 p = skipwhite(*arg);
4664 }
4665
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004666 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4667 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004669 return FAIL;
4670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671
Bram Moolenaara5565e42020-05-09 15:44:01 +02004672 if (ppconst->pp_used == ppconst_used + 1)
4673 {
4674 // the condition is a constant, we know whether the ? or the :
4675 // expression is to be evaluated.
4676 has_const_expr = TRUE;
4677 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4678 clear_tv(&ppconst->pp_tv[ppconst_used]);
4679 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004680 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4681 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004682 }
4683 else
4684 {
4685 generate_ppconst(cctx, ppconst);
4686 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4687 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688
4689 // evaluate the second expression; any type is accepted
4690 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004691 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004692 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004693 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004694 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695
Bram Moolenaara5565e42020-05-09 15:44:01 +02004696 if (!has_const_expr)
4697 {
4698 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699
Bram Moolenaara5565e42020-05-09 15:44:01 +02004700 // remember the type and drop it
4701 --stack->ga_len;
4702 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703
Bram Moolenaara5565e42020-05-09 15:44:01 +02004704 end_idx = instr->ga_len;
4705 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4706
4707 // jump here from JUMP_IF_FALSE
4708 isn = ((isn_T *)instr->ga_data) + alt_idx;
4709 isn->isn_arg.jump.jump_where = instr->ga_len;
4710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711
4712 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004713 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 if (*p != ':')
4715 {
4716 emsg(_(e_missing_colon));
4717 return FAIL;
4718 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004719 if (next != NULL)
4720 {
4721 *arg = next_line_from_context(cctx, TRUE);
4722 p = skipwhite(*arg);
4723 }
4724
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004725 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4726 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004728 return FAIL;
4729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730
4731 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004732 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004733 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4734 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004736 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004737 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004738 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004739 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740
Bram Moolenaara5565e42020-05-09 15:44:01 +02004741 if (!has_const_expr)
4742 {
4743 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744
Bram Moolenaara5565e42020-05-09 15:44:01 +02004745 // If the types differ, the result has a more generic type.
4746 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4747 common_type(type1, type2, &type2, cctx->ctx_type_list);
4748
4749 // jump here from JUMP_ALWAYS
4750 isn = ((isn_T *)instr->ga_data) + end_idx;
4751 isn->isn_arg.jump.jump_where = instr->ga_len;
4752 }
4753
4754 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 }
4756 return OK;
4757}
4758
4759/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004760 * Toplevel expression.
4761 */
4762 static int
4763compile_expr0(char_u **arg, cctx_T *cctx)
4764{
4765 ppconst_T ppconst;
4766
4767 CLEAR_FIELD(ppconst);
4768 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4769 {
4770 clear_ppconst(&ppconst);
4771 return FAIL;
4772 }
4773 if (generate_ppconst(cctx, &ppconst) == FAIL)
4774 return FAIL;
4775 return OK;
4776}
4777
4778/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 * compile "return [expr]"
4780 */
4781 static char_u *
4782compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4783{
4784 char_u *p = arg;
4785 garray_T *stack = &cctx->ctx_type_stack;
4786 type_T *stack_type;
4787
4788 if (*p != NUL && *p != '|' && *p != '\n')
4789 {
4790 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004791 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 return NULL;
4793
4794 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4795 if (set_return_type)
4796 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004797 else
4798 {
4799 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4800 && stack_type->tt_type != VAR_VOID
4801 && stack_type->tt_type != VAR_UNKNOWN)
4802 {
4803 emsg(_("E1096: Returning a value in a function without a return type"));
4804 return NULL;
4805 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004806 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4807 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810 }
4811 else
4812 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004813 // "set_return_type" cannot be TRUE, only used for a lambda which
4814 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004815 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4816 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817 {
4818 emsg(_("E1003: Missing return value"));
4819 return NULL;
4820 }
4821
4822 // No argument, return zero.
4823 generate_PUSHNR(cctx, 0);
4824 }
4825
4826 if (generate_instr(cctx, ISN_RETURN) == NULL)
4827 return NULL;
4828
4829 // "return val | endif" is possible
4830 return skipwhite(p);
4831}
4832
4833/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004834 * Get a line from the compilation context, compatible with exarg_T getline().
4835 * Return a pointer to the line in allocated memory.
4836 * Return NULL for end-of-file or some error.
4837 */
4838 static char_u *
4839exarg_getline(
4840 int c UNUSED,
4841 void *cookie,
4842 int indent UNUSED,
4843 int do_concat UNUSED)
4844{
4845 cctx_T *cctx = (cctx_T *)cookie;
4846
4847 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4848 {
4849 iemsg("Heredoc got to end");
4850 return NULL;
4851 }
4852 ++cctx->ctx_lnum;
4853 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4854 [cctx->ctx_lnum]);
4855}
4856
4857/*
4858 * Compile a nested :def command.
4859 */
4860 static char_u *
4861compile_nested_function(exarg_T *eap, cctx_T *cctx)
4862{
4863 char_u *name_start = eap->arg;
4864 char_u *name_end = to_name_end(eap->arg, FALSE);
4865 char_u *name = get_lambda_name();
4866 lvar_T *lvar;
4867 ufunc_T *ufunc;
4868
4869 eap->arg = name_end;
4870 eap->getline = exarg_getline;
4871 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004872 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004873 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004874 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004875
Bram Moolenaar822ba242020-05-24 23:00:18 +02004876 if (ufunc == NULL)
4877 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004878 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004879 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004880 return NULL;
4881
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004882 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004883 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004884 TRUE, ufunc->uf_func_type);
4885
4886 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4887 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4888 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004889
Bram Moolenaar61a89812020-05-07 16:58:17 +02004890 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004891 return (char_u *)"";
4892}
4893
4894/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 * Return the length of an assignment operator, or zero if there isn't one.
4896 */
4897 int
4898assignment_len(char_u *p, int *heredoc)
4899{
4900 if (*p == '=')
4901 {
4902 if (p[1] == '<' && p[2] == '<')
4903 {
4904 *heredoc = TRUE;
4905 return 3;
4906 }
4907 return 1;
4908 }
4909 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4910 return 2;
4911 if (STRNCMP(p, "..=", 3) == 0)
4912 return 3;
4913 return 0;
4914}
4915
4916// words that cannot be used as a variable
4917static char *reserved[] = {
4918 "true",
4919 "false",
4920 NULL
4921};
4922
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004923typedef enum {
4924 dest_local,
4925 dest_option,
4926 dest_env,
4927 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004928 dest_buffer,
4929 dest_window,
4930 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004931 dest_vimvar,
4932 dest_script,
4933 dest_reg,
4934} assign_dest_T;
4935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004937 * Generate the load instruction for "name".
4938 */
4939 static void
4940generate_loadvar(
4941 cctx_T *cctx,
4942 assign_dest_T dest,
4943 char_u *name,
4944 lvar_T *lvar,
4945 type_T *type)
4946{
4947 switch (dest)
4948 {
4949 case dest_option:
4950 // TODO: check the option exists
4951 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4952 break;
4953 case dest_global:
4954 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4955 break;
4956 case dest_buffer:
4957 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4958 break;
4959 case dest_window:
4960 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4961 break;
4962 case dest_tab:
4963 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4964 break;
4965 case dest_script:
4966 compile_load_scriptvar(cctx,
4967 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4968 break;
4969 case dest_env:
4970 // Include $ in the name here
4971 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4972 break;
4973 case dest_reg:
4974 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4975 break;
4976 case dest_vimvar:
4977 generate_LOADV(cctx, name + 2, TRUE);
4978 break;
4979 case dest_local:
4980 if (lvar->lv_from_outer)
4981 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4982 NULL, type);
4983 else
4984 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4985 break;
4986 }
4987}
4988
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004989 void
4990vim9_declare_error(char_u *name)
4991{
4992 char *scope = "";
4993
4994 switch (*name)
4995 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004996 case 'g': scope = _("global"); break;
4997 case 'b': scope = _("buffer"); break;
4998 case 'w': scope = _("window"); break;
4999 case 't': scope = _("tab"); break;
5000 case 'v': scope = "v:"; break;
5001 case '$': semsg(_(e_declare_env_var), name); return;
5002 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005003 }
5004 semsg(_(e_declare_var), scope, name);
5005}
5006
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005007/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005008 * Compile declaration and assignment:
5009 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005011 * Return NULL for an error.
5012 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 */
5014 static char_u *
5015compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5016{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005017 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005019 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 char_u *ret = NULL;
5021 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005022 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005025 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027 int oplen = 0;
5028 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005029 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005030 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005031 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005035 // Skip over the "var" or "[var, var]" to get to any "=".
5036 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5037 if (p == NULL)
5038 return *arg == '[' ? arg : NULL;
5039
5040 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005042 // TODO: should we allow this, and figure out type inference from list
5043 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005044 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 return NULL;
5046 }
5047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048 sp = p;
5049 p = skipwhite(p);
5050 op = p;
5051 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005052
5053 if (var_count > 0 && oplen == 0)
5054 // can be something like "[1, 2]->func()"
5055 return arg;
5056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5058 {
5059 char_u buf[4];
5060
5061 vim_strncpy(buf, op, oplen);
5062 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005063 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005064 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 if (heredoc)
5067 {
5068 list_T *l;
5069 listitem_T *li;
5070
5071 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005072 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005074 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075
5076 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005077 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 {
5079 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5080 li->li_tv.vval.v_string = NULL;
5081 }
5082 generate_NEWLIST(cctx, l->lv_len);
5083 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005084 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 list_free(l);
5086 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005087 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005091 // for "[var, var] = expr" evaluate the expression here, loop over the
5092 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005093
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005094 p = skipwhite(op + oplen);
5095 if (compile_expr0(&p, cctx) == FAIL)
5096 return NULL;
5097 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005099 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005101 type_T *stacktype;
5102
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005103 stacktype = stack->ga_len == 0 ? &t_void
5104 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005105 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005107 emsg(_(e_cannot_use_void));
5108 goto theend;
5109 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005110 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005111 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005112 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005113 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5114 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005115 }
5116 }
5117
5118 /*
5119 * Loop over variables in "[var, var] = expr".
5120 * For "var = expr" and "let var: type" this is done only once.
5121 */
5122 if (var_count > 0)
5123 var_start = skipwhite(arg + 1); // skip over the "["
5124 else
5125 var_start = arg;
5126 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5127 {
5128 char_u *var_end = skip_var_one(var_start, FALSE);
5129 size_t varlen;
5130 int new_local = FALSE;
5131 int opt_type;
5132 int opt_flags = 0;
5133 assign_dest_T dest = dest_local;
5134 int vimvaridx = -1;
5135 lvar_T *lvar = NULL;
5136 lvar_T arg_lvar;
5137 int has_type = FALSE;
5138 int has_index = FALSE;
5139 int instr_count = -1;
5140
5141 p = (*var_start == '&' || *var_start == '$'
5142 || *var_start == '@') ? var_start + 1 : var_start;
5143 p = to_name_end(p, TRUE);
5144
5145 // "a: type" is declaring variable "a" with a type, not "a:".
5146 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5147 --var_end;
5148 if (is_decl && p == var_start + 2 && p[-1] == ':')
5149 --p;
5150
5151 varlen = p - var_start;
5152 vim_free(name);
5153 name = vim_strnsave(var_start, varlen);
5154 if (name == NULL)
5155 return NULL;
5156 if (!heredoc)
5157 type = &t_any;
5158
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005159 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005160 {
5161 if (*var_start == '&')
5162 {
5163 int cc;
5164 long numval;
5165
5166 dest = dest_option;
5167 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005169 emsg(_(e_const_option));
5170 goto theend;
5171 }
5172 if (is_decl)
5173 {
5174 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5175 goto theend;
5176 }
5177 p = var_start;
5178 p = find_option_end(&p, &opt_flags);
5179 if (p == NULL)
5180 {
5181 // cannot happen?
5182 emsg(_(e_letunexp));
5183 goto theend;
5184 }
5185 cc = *p;
5186 *p = NUL;
5187 opt_type = get_option_value(var_start + 1, &numval,
5188 NULL, opt_flags);
5189 *p = cc;
5190 if (opt_type == -3)
5191 {
5192 semsg(_(e_unknown_option), var_start);
5193 goto theend;
5194 }
5195 if (opt_type == -2 || opt_type == 0)
5196 type = &t_string;
5197 else
5198 type = &t_number; // both number and boolean option
5199 }
5200 else if (*var_start == '$')
5201 {
5202 dest = dest_env;
5203 type = &t_string;
5204 if (is_decl)
5205 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005206 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005207 goto theend;
5208 }
5209 }
5210 else if (*var_start == '@')
5211 {
5212 if (!valid_yank_reg(var_start[1], TRUE))
5213 {
5214 emsg_invreg(var_start[1]);
5215 goto theend;
5216 }
5217 dest = dest_reg;
5218 type = &t_string;
5219 if (is_decl)
5220 {
5221 semsg(_("E1066: Cannot declare a register: %s"), name);
5222 goto theend;
5223 }
5224 }
5225 else if (STRNCMP(var_start, "g:", 2) == 0)
5226 {
5227 dest = dest_global;
5228 if (is_decl)
5229 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005230 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005231 goto theend;
5232 }
5233 }
5234 else if (STRNCMP(var_start, "b:", 2) == 0)
5235 {
5236 dest = dest_buffer;
5237 if (is_decl)
5238 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005239 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005240 goto theend;
5241 }
5242 }
5243 else if (STRNCMP(var_start, "w:", 2) == 0)
5244 {
5245 dest = dest_window;
5246 if (is_decl)
5247 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005248 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005249 goto theend;
5250 }
5251 }
5252 else if (STRNCMP(var_start, "t:", 2) == 0)
5253 {
5254 dest = dest_tab;
5255 if (is_decl)
5256 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005257 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005258 goto theend;
5259 }
5260 }
5261 else if (STRNCMP(var_start, "v:", 2) == 0)
5262 {
5263 typval_T *vtv;
5264 int di_flags;
5265
5266 vimvaridx = find_vim_var(name + 2, &di_flags);
5267 if (vimvaridx < 0)
5268 {
5269 semsg(_(e_var_notfound), var_start);
5270 goto theend;
5271 }
5272 // We use the current value of "sandbox" here, is that OK?
5273 if (var_check_ro(di_flags, name, FALSE))
5274 goto theend;
5275 dest = dest_vimvar;
5276 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005277 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005278 if (is_decl)
5279 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005280 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005281 goto theend;
5282 }
5283 }
5284 else
5285 {
5286 int idx;
5287
5288 for (idx = 0; reserved[idx] != NULL; ++idx)
5289 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005290 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005291 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005292 goto theend;
5293 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005294
5295 lvar = lookup_local(var_start, varlen, cctx);
5296 if (lvar == NULL)
5297 {
5298 CLEAR_FIELD(arg_lvar);
5299 if (lookup_arg(var_start, varlen,
5300 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5301 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005302 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005303 if (is_decl)
5304 {
5305 semsg(_(e_used_as_arg), name);
5306 goto theend;
5307 }
5308 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005309 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005310 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005311 if (lvar != NULL)
5312 {
5313 if (is_decl)
5314 {
5315 semsg(_("E1017: Variable already declared: %s"), name);
5316 goto theend;
5317 }
5318 else if (lvar->lv_const)
5319 {
5320 semsg(_("E1018: Cannot assign to a constant: %s"),
5321 name);
5322 goto theend;
5323 }
5324 }
5325 else if (STRNCMP(var_start, "s:", 2) == 0
5326 || lookup_script(var_start, varlen) == OK
5327 || find_imported(var_start, varlen, cctx) != NULL)
5328 {
5329 dest = dest_script;
5330 if (is_decl)
5331 {
5332 semsg(_("E1054: Variable already declared in the script: %s"),
5333 name);
5334 goto theend;
5335 }
5336 }
5337 else if (name[1] == ':' && name[2] != NUL)
5338 {
5339 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5340 name);
5341 goto theend;
5342 }
5343 else if (!is_decl)
5344 {
5345 semsg(_("E1089: unknown variable: %s"), name);
5346 goto theend;
5347 }
5348 }
5349 }
5350
5351 // handle "a:name" as a name, not index "name" on "a"
5352 if (varlen > 1 || var_start[varlen] != ':')
5353 p = var_end;
5354
5355 if (dest != dest_option)
5356 {
5357 if (is_decl && *p == ':')
5358 {
5359 // parse optional type: "let var: type = expr"
5360 if (!VIM_ISWHITE(p[1]))
5361 {
5362 semsg(_(e_white_after), ":");
5363 goto theend;
5364 }
5365 p = skipwhite(p + 1);
5366 type = parse_type(&p, cctx->ctx_type_list);
5367 has_type = TRUE;
5368 }
5369 else if (lvar != NULL)
5370 type = lvar->lv_type;
5371 }
5372
5373 if (oplen == 3 && !heredoc && dest != dest_global
5374 && type->tt_type != VAR_STRING
5375 && type->tt_type != VAR_ANY)
5376 {
5377 emsg(_("E1019: Can only concatenate to string"));
5378 goto theend;
5379 }
5380
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005381 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382 {
5383 if (oplen > 1 && !heredoc)
5384 {
5385 // +=, /=, etc. require an existing variable
5386 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5387 name);
5388 goto theend;
5389 }
5390
5391 // new local variable
5392 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5393 goto theend;
5394 lvar = reserve_local(cctx, var_start, varlen,
5395 cmdidx == CMD_const, type);
5396 if (lvar == NULL)
5397 goto theend;
5398 new_local = TRUE;
5399 }
5400
5401 member_type = type;
5402 if (var_end > var_start + varlen)
5403 {
5404 // Something follows after the variable: "var[idx]".
5405 if (is_decl)
5406 {
5407 emsg(_("E1087: cannot use an index when declaring a variable"));
5408 goto theend;
5409 }
5410
5411 if (var_start[varlen] == '[')
5412 {
5413 has_index = TRUE;
5414 if (type->tt_member == NULL)
5415 {
5416 semsg(_("E1088: cannot use an index on %s"), name);
5417 goto theend;
5418 }
5419 member_type = type->tt_member;
5420 }
5421 else
5422 {
5423 semsg("Not supported yet: %s", var_start);
5424 goto theend;
5425 }
5426 }
5427 else if (lvar == &arg_lvar)
5428 {
5429 semsg(_("E1090: Cannot assign to argument %s"), name);
5430 goto theend;
5431 }
5432
5433 if (!heredoc)
5434 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005435 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005436 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005437 if (oplen > 0 && var_count == 0)
5438 {
5439 // skip over the "=" and the expression
5440 p = skipwhite(op + oplen);
5441 compile_expr0(&p, cctx);
5442 }
5443 }
5444 else if (oplen > 0)
5445 {
5446 type_T *stacktype;
5447
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005448 // For "var = expr" evaluate the expression.
5449 if (var_count == 0)
5450 {
5451 int r;
5452
5453 // for "+=", "*=", "..=" etc. first load the current value
5454 if (*op != '=')
5455 {
5456 generate_loadvar(cctx, dest, name, lvar, type);
5457
5458 if (has_index)
5459 {
5460 // TODO: get member from list or dict
5461 emsg("Index with operation not supported yet");
5462 goto theend;
5463 }
5464 }
5465
5466 // Compile the expression. Temporarily hide the new local
5467 // variable here, it is not available to this expression.
5468 if (new_local)
5469 --cctx->ctx_locals.ga_len;
5470 instr_count = instr->ga_len;
5471 p = skipwhite(op + oplen);
5472 r = compile_expr0(&p, cctx);
5473 if (new_local)
5474 ++cctx->ctx_locals.ga_len;
5475 if (r == FAIL)
5476 goto theend;
5477 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005478 else if (semicolon && var_idx == var_count - 1)
5479 {
5480 // For "[var; var] = expr" get the rest of the list
5481 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5482 goto theend;
5483 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005484 else
5485 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005486 // For "[var, var] = expr" get the "var_idx" item from the
5487 // list.
5488 if (generate_GETITEM(cctx, var_idx) == FAIL)
5489 return FAIL;
5490 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005491
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005492 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005493 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005494 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005495 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005496 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005497 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005498 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005499 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005500 emsg(_(e_cannot_use_void));
5501 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005502 }
5503 else
5504 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005505 // An empty list or dict has a &t_void member,
5506 // for a variable that implies &t_any.
5507 if (stacktype == &t_list_empty)
5508 lvar->lv_type = &t_list_any;
5509 else if (stacktype == &t_dict_empty)
5510 lvar->lv_type = &t_dict_any;
5511 else
5512 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005513 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005514 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005515 else
5516 {
5517 type_T *use_type = lvar->lv_type;
5518
5519 if (has_index)
5520 {
5521 use_type = use_type->tt_member;
5522 if (use_type == NULL)
5523 use_type = &t_void;
5524 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005525 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005526 == FAIL)
5527 goto theend;
5528 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005529 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005530 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005531 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005532 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005534 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005536 emsg(_(e_const_req_value));
5537 goto theend;
5538 }
5539 else if (!has_type || dest == dest_option)
5540 {
5541 emsg(_(e_type_req));
5542 goto theend;
5543 }
5544 else
5545 {
5546 // variables are always initialized
5547 if (ga_grow(instr, 1) == FAIL)
5548 goto theend;
5549 switch (member_type->tt_type)
5550 {
5551 case VAR_BOOL:
5552 generate_PUSHBOOL(cctx, VVAL_FALSE);
5553 break;
5554 case VAR_FLOAT:
5555#ifdef FEAT_FLOAT
5556 generate_PUSHF(cctx, 0.0);
5557#endif
5558 break;
5559 case VAR_STRING:
5560 generate_PUSHS(cctx, NULL);
5561 break;
5562 case VAR_BLOB:
5563 generate_PUSHBLOB(cctx, NULL);
5564 break;
5565 case VAR_FUNC:
5566 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5567 break;
5568 case VAR_LIST:
5569 generate_NEWLIST(cctx, 0);
5570 break;
5571 case VAR_DICT:
5572 generate_NEWDICT(cctx, 0);
5573 break;
5574 case VAR_JOB:
5575 generate_PUSHJOB(cctx, NULL);
5576 break;
5577 case VAR_CHANNEL:
5578 generate_PUSHCHANNEL(cctx, NULL);
5579 break;
5580 case VAR_NUMBER:
5581 case VAR_UNKNOWN:
5582 case VAR_ANY:
5583 case VAR_PARTIAL:
5584 case VAR_VOID:
5585 case VAR_SPECIAL: // cannot happen
5586 generate_PUSHNR(cctx, 0);
5587 break;
5588 }
5589 }
5590 if (var_count == 0)
5591 end = p;
5592 }
5593
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005594 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005595 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005596 break;
5597
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005598 if (oplen > 0 && *op != '=')
5599 {
5600 type_T *expected = &t_number;
5601 type_T *stacktype;
5602
5603 // TODO: if type is known use float or any operation
5604
5605 if (*op == '.')
5606 expected = &t_string;
5607 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005608 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005609 goto theend;
5610
5611 if (*op == '.')
5612 generate_instr_drop(cctx, ISN_CONCAT, 1);
5613 else
5614 {
5615 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5616
5617 if (isn == NULL)
5618 goto theend;
5619 switch (*op)
5620 {
5621 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5622 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5623 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5624 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5625 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005627 }
5628 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005629
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005630 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005631 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005632 int r;
5633
5634 // Compile the "idx" in "var[idx]".
5635 if (new_local)
5636 --cctx->ctx_locals.ga_len;
5637 p = skipwhite(var_start + varlen + 1);
5638 r = compile_expr0(&p, cctx);
5639 if (new_local)
5640 ++cctx->ctx_locals.ga_len;
5641 if (r == FAIL)
5642 goto theend;
5643 if (*skipwhite(p) != ']')
5644 {
5645 emsg(_(e_missbrac));
5646 goto theend;
5647 }
5648 if (type->tt_type == VAR_DICT
5649 && may_generate_2STRING(-1, cctx) == FAIL)
5650 goto theend;
5651 if (type->tt_type == VAR_LIST
5652 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005653 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005654 {
5655 emsg(_(e_number_exp));
5656 goto theend;
5657 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005658
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005659 // Load the dict or list. On the stack we then have:
5660 // - value
5661 // - index
5662 // - variable
5663 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005664
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005665 if (type->tt_type == VAR_LIST)
5666 {
5667 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5668 return FAIL;
5669 }
5670 else if (type->tt_type == VAR_DICT)
5671 {
5672 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5673 return FAIL;
5674 }
5675 else
5676 {
5677 emsg(_(e_listreq));
5678 goto theend;
5679 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005680 }
5681 else
5682 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005683 switch (dest)
5684 {
5685 case dest_option:
5686 generate_STOREOPT(cctx, name + 1, opt_flags);
5687 break;
5688 case dest_global:
5689 // include g: with the name, easier to execute that way
5690 generate_STORE(cctx, ISN_STOREG, 0, name);
5691 break;
5692 case dest_buffer:
5693 // include b: with the name, easier to execute that way
5694 generate_STORE(cctx, ISN_STOREB, 0, name);
5695 break;
5696 case dest_window:
5697 // include w: with the name, easier to execute that way
5698 generate_STORE(cctx, ISN_STOREW, 0, name);
5699 break;
5700 case dest_tab:
5701 // include t: with the name, easier to execute that way
5702 generate_STORE(cctx, ISN_STORET, 0, name);
5703 break;
5704 case dest_env:
5705 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5706 break;
5707 case dest_reg:
5708 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5709 break;
5710 case dest_vimvar:
5711 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5712 break;
5713 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005714 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005715 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5716 imported_T *import = NULL;
5717 int sid = current_sctx.sc_sid;
5718 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005719
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005720 if (name[1] != ':')
5721 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005722 import = find_imported(name, 0, cctx);
5723 if (import != NULL)
5724 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005725 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005726
5727 idx = get_script_item_idx(sid, rawname, TRUE);
5728 // TODO: specific type
5729 if (idx < 0)
5730 {
5731 char_u *name_s = name;
5732
5733 // Include s: in the name for store_var()
5734 if (name[1] != ':')
5735 {
5736 int len = (int)STRLEN(name) + 3;
5737
5738 name_s = alloc(len);
5739 if (name_s == NULL)
5740 name_s = name;
5741 else
5742 vim_snprintf((char *)name_s, len,
5743 "s:%s", name);
5744 }
5745 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005746 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005747 if (name_s != name)
5748 vim_free(name_s);
5749 }
5750 else
5751 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005752 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005753 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005754 break;
5755 case dest_local:
5756 if (lvar != NULL)
5757 {
5758 isn_T *isn = ((isn_T *)instr->ga_data)
5759 + instr->ga_len - 1;
5760
5761 // optimization: turn "var = 123" from ISN_PUSHNR +
5762 // ISN_STORE into ISN_STORENR
5763 if (!lvar->lv_from_outer
5764 && instr->ga_len == instr_count + 1
5765 && isn->isn_type == ISN_PUSHNR)
5766 {
5767 varnumber_T val = isn->isn_arg.number;
5768
5769 isn->isn_type = ISN_STORENR;
5770 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5771 isn->isn_arg.storenr.stnr_val = val;
5772 if (stack->ga_len > 0)
5773 --stack->ga_len;
5774 }
5775 else if (lvar->lv_from_outer)
5776 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005777 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005778 else
5779 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5780 }
5781 break;
5782 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005783 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005784
5785 if (var_idx + 1 < var_count)
5786 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005787 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005788
5789 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005790 if (var_count > 0 && !semicolon)
5791 {
5792 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5793 goto theend;
5794 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005795
Bram Moolenaarb2097502020-07-19 17:17:02 +02005796 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005797
5798theend:
5799 vim_free(name);
5800 return ret;
5801}
5802
5803/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005804 * Check if "name" can be "unlet".
5805 */
5806 int
5807check_vim9_unlet(char_u *name)
5808{
5809 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5810 {
5811 semsg(_("E1081: Cannot unlet %s"), name);
5812 return FAIL;
5813 }
5814 return OK;
5815}
5816
5817/*
5818 * Callback passed to ex_unletlock().
5819 */
5820 static int
5821compile_unlet(
5822 lval_T *lvp,
5823 char_u *name_end,
5824 exarg_T *eap,
5825 int deep UNUSED,
5826 void *coookie)
5827{
5828 cctx_T *cctx = coookie;
5829
5830 if (lvp->ll_tv == NULL)
5831 {
5832 char_u *p = lvp->ll_name;
5833 int cc = *name_end;
5834 int ret = OK;
5835
5836 // Normal name. Only supports g:, w:, t: and b: namespaces.
5837 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005838 if (*p == '$')
5839 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5840 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005841 ret = FAIL;
5842 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005843 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005844
5845 *name_end = cc;
5846 return ret;
5847 }
5848
5849 // TODO: unlet {list}[idx]
5850 // TODO: unlet {dict}[key]
5851 emsg("Sorry, :unlet not fully implemented yet");
5852 return FAIL;
5853}
5854
5855/*
5856 * compile "unlet var", "lock var" and "unlock var"
5857 * "arg" points to "var".
5858 */
5859 static char_u *
5860compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5861{
5862 char_u *p = arg;
5863
5864 if (eap->cmdidx != CMD_unlet)
5865 {
5866 emsg("Sorry, :lock and unlock not implemented yet");
5867 return NULL;
5868 }
5869
5870 if (*p == '!')
5871 {
5872 p = skipwhite(p + 1);
5873 eap->forceit = TRUE;
5874 }
5875
5876 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5877 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5878}
5879
5880/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005881 * Compile an :import command.
5882 */
5883 static char_u *
5884compile_import(char_u *arg, cctx_T *cctx)
5885{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005886 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005887}
5888
5889/*
5890 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5891 */
5892 static int
5893compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5894{
5895 garray_T *instr = &cctx->ctx_instr;
5896 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5897
5898 if (endlabel == NULL)
5899 return FAIL;
5900 endlabel->el_next = *el;
5901 *el = endlabel;
5902 endlabel->el_end_label = instr->ga_len;
5903
5904 generate_JUMP(cctx, when, 0);
5905 return OK;
5906}
5907
5908 static void
5909compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5910{
5911 garray_T *instr = &cctx->ctx_instr;
5912
5913 while (*el != NULL)
5914 {
5915 endlabel_T *cur = (*el);
5916 isn_T *isn;
5917
5918 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5919 isn->isn_arg.jump.jump_where = instr->ga_len;
5920 *el = cur->el_next;
5921 vim_free(cur);
5922 }
5923}
5924
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005925 static void
5926compile_free_jump_to_end(endlabel_T **el)
5927{
5928 while (*el != NULL)
5929 {
5930 endlabel_T *cur = (*el);
5931
5932 *el = cur->el_next;
5933 vim_free(cur);
5934 }
5935}
5936
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005937/*
5938 * Create a new scope and set up the generic items.
5939 */
5940 static scope_T *
5941new_scope(cctx_T *cctx, scopetype_T type)
5942{
5943 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5944
5945 if (scope == NULL)
5946 return NULL;
5947 scope->se_outer = cctx->ctx_scope;
5948 cctx->ctx_scope = scope;
5949 scope->se_type = type;
5950 scope->se_local_count = cctx->ctx_locals.ga_len;
5951 return scope;
5952}
5953
5954/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005955 * Free the current scope and go back to the outer scope.
5956 */
5957 static void
5958drop_scope(cctx_T *cctx)
5959{
5960 scope_T *scope = cctx->ctx_scope;
5961
5962 if (scope == NULL)
5963 {
5964 iemsg("calling drop_scope() without a scope");
5965 return;
5966 }
5967 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005968 switch (scope->se_type)
5969 {
5970 case IF_SCOPE:
5971 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5972 case FOR_SCOPE:
5973 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5974 case WHILE_SCOPE:
5975 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5976 case TRY_SCOPE:
5977 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5978 case NO_SCOPE:
5979 case BLOCK_SCOPE:
5980 break;
5981 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005982 vim_free(scope);
5983}
5984
5985/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986 * compile "if expr"
5987 *
5988 * "if expr" Produces instructions:
5989 * EVAL expr Push result of "expr"
5990 * JUMP_IF_FALSE end
5991 * ... body ...
5992 * end:
5993 *
5994 * "if expr | else" Produces instructions:
5995 * EVAL expr Push result of "expr"
5996 * JUMP_IF_FALSE else
5997 * ... body ...
5998 * JUMP_ALWAYS end
5999 * else:
6000 * ... body ...
6001 * end:
6002 *
6003 * "if expr1 | elseif expr2 | else" Produces instructions:
6004 * EVAL expr Push result of "expr"
6005 * JUMP_IF_FALSE elseif
6006 * ... body ...
6007 * JUMP_ALWAYS end
6008 * elseif:
6009 * EVAL expr Push result of "expr"
6010 * JUMP_IF_FALSE else
6011 * ... body ...
6012 * JUMP_ALWAYS end
6013 * else:
6014 * ... body ...
6015 * end:
6016 */
6017 static char_u *
6018compile_if(char_u *arg, cctx_T *cctx)
6019{
6020 char_u *p = arg;
6021 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006022 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006024 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006025 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026
Bram Moolenaara5565e42020-05-09 15:44:01 +02006027 CLEAR_FIELD(ppconst);
6028 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006029 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006030 clear_ppconst(&ppconst);
6031 return NULL;
6032 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006033 if (cctx->ctx_skip == SKIP_YES)
6034 clear_ppconst(&ppconst);
6035 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006036 {
6037 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006038 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006039 clear_ppconst(&ppconst);
6040 }
6041 else
6042 {
6043 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006044 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006045 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006046 return NULL;
6047 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048
6049 scope = new_scope(cctx, IF_SCOPE);
6050 if (scope == NULL)
6051 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006052 scope->se_skip_save = skip_save;
6053 // "is_had_return" will be reset if any block does not end in :return
6054 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006056 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006057 {
6058 // "where" is set when ":elseif", "else" or ":endif" is found
6059 scope->se_u.se_if.is_if_label = instr->ga_len;
6060 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6061 }
6062 else
6063 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064
6065 return p;
6066}
6067
6068 static char_u *
6069compile_elseif(char_u *arg, cctx_T *cctx)
6070{
6071 char_u *p = arg;
6072 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006073 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074 isn_T *isn;
6075 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006076 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006077
6078 if (scope == NULL || scope->se_type != IF_SCOPE)
6079 {
6080 emsg(_(e_elseif_without_if));
6081 return NULL;
6082 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006083 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006084 if (!cctx->ctx_had_return)
6085 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006087 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006088 {
6089 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006090 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006091 return NULL;
6092 // previous "if" or "elseif" jumps here
6093 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6094 isn->isn_arg.jump.jump_where = instr->ga_len;
6095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006097 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006098 CLEAR_FIELD(ppconst);
6099 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006100 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006101 clear_ppconst(&ppconst);
6102 return NULL;
6103 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006104 if (scope->se_skip_save == SKIP_YES)
6105 clear_ppconst(&ppconst);
6106 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006107 {
6108 // The expression results in a constant.
6109 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006110 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006111 clear_ppconst(&ppconst);
6112 scope->se_u.se_if.is_if_label = -1;
6113 }
6114 else
6115 {
6116 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006117 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006118 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006119 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006120
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006121 // "where" is set when ":elseif", "else" or ":endif" is found
6122 scope->se_u.se_if.is_if_label = instr->ga_len;
6123 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125
6126 return p;
6127}
6128
6129 static char_u *
6130compile_else(char_u *arg, cctx_T *cctx)
6131{
6132 char_u *p = arg;
6133 garray_T *instr = &cctx->ctx_instr;
6134 isn_T *isn;
6135 scope_T *scope = cctx->ctx_scope;
6136
6137 if (scope == NULL || scope->se_type != IF_SCOPE)
6138 {
6139 emsg(_(e_else_without_if));
6140 return NULL;
6141 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006142 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006143 if (!cctx->ctx_had_return)
6144 scope->se_u.se_if.is_had_return = FALSE;
6145 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146
Bram Moolenaarefd88552020-06-18 20:50:10 +02006147 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006148 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006149 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006150 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006151 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006152 if (!cctx->ctx_had_return
6153 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6154 JUMP_ALWAYS, cctx) == FAIL)
6155 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006156 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006157
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006158 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006159 {
6160 if (scope->se_u.se_if.is_if_label >= 0)
6161 {
6162 // previous "if" or "elseif" jumps here
6163 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6164 isn->isn_arg.jump.jump_where = instr->ga_len;
6165 scope->se_u.se_if.is_if_label = -1;
6166 }
6167 }
6168
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006169 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006170 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172
6173 return p;
6174}
6175
6176 static char_u *
6177compile_endif(char_u *arg, cctx_T *cctx)
6178{
6179 scope_T *scope = cctx->ctx_scope;
6180 ifscope_T *ifscope;
6181 garray_T *instr = &cctx->ctx_instr;
6182 isn_T *isn;
6183
6184 if (scope == NULL || scope->se_type != IF_SCOPE)
6185 {
6186 emsg(_(e_endif_without_if));
6187 return NULL;
6188 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006189 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006190 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006191 if (!cctx->ctx_had_return)
6192 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006194 if (scope->se_u.se_if.is_if_label >= 0)
6195 {
6196 // previous "if" or "elseif" jumps here
6197 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6198 isn->isn_arg.jump.jump_where = instr->ga_len;
6199 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200 // Fill in the "end" label in jumps at the end of the blocks.
6201 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006202 cctx->ctx_skip = scope->se_skip_save;
6203
6204 // If all the blocks end in :return and there is an :else then the
6205 // had_return flag is set.
6206 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006207
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006208 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006209 return arg;
6210}
6211
6212/*
6213 * compile "for var in expr"
6214 *
6215 * Produces instructions:
6216 * PUSHNR -1
6217 * STORE loop-idx Set index to -1
6218 * EVAL expr Push result of "expr"
6219 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6220 * - if beyond end, jump to "end"
6221 * - otherwise get item from list and push it
6222 * STORE var Store item in "var"
6223 * ... body ...
6224 * JUMP top Jump back to repeat
6225 * end: DROP Drop the result of "expr"
6226 *
6227 */
6228 static char_u *
6229compile_for(char_u *arg, cctx_T *cctx)
6230{
6231 char_u *p;
6232 size_t varlen;
6233 garray_T *instr = &cctx->ctx_instr;
6234 garray_T *stack = &cctx->ctx_type_stack;
6235 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006236 lvar_T *loop_lvar; // loop iteration variable
6237 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006238 type_T *vartype;
6239
6240 // TODO: list of variables: "for [key, value] in dict"
6241 // parse "var"
6242 for (p = arg; eval_isnamec1(*p); ++p)
6243 ;
6244 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006245 var_lvar = lookup_local(arg, varlen, cctx);
6246 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247 {
6248 semsg(_("E1023: variable already defined: %s"), arg);
6249 return NULL;
6250 }
6251
6252 // consume "in"
6253 p = skipwhite(p);
6254 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6255 {
6256 emsg(_(e_missing_in));
6257 return NULL;
6258 }
6259 p = skipwhite(p + 2);
6260
6261
6262 scope = new_scope(cctx, FOR_SCOPE);
6263 if (scope == NULL)
6264 return NULL;
6265
6266 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006267 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6268 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006269 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006270 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006271 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274
6275 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006276 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6277 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006278 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006279 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006280 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006282 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006283
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006284 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006285
6286 // compile "expr", it remains on the stack until "endfor"
6287 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006288 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006289 {
6290 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006294 // Now that we know the type of "var", check that it is a list, now or at
6295 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006297 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006299 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006300 return NULL;
6301 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006302 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006303 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304
6305 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006306 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006308 generate_FOR(cctx, loop_lvar->lv_idx);
6309 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310
6311 return arg;
6312}
6313
6314/*
6315 * compile "endfor"
6316 */
6317 static char_u *
6318compile_endfor(char_u *arg, cctx_T *cctx)
6319{
6320 garray_T *instr = &cctx->ctx_instr;
6321 scope_T *scope = cctx->ctx_scope;
6322 forscope_T *forscope;
6323 isn_T *isn;
6324
6325 if (scope == NULL || scope->se_type != FOR_SCOPE)
6326 {
6327 emsg(_(e_for));
6328 return NULL;
6329 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006330 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006331 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006332 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333
6334 // At end of ":for" scope jump back to the FOR instruction.
6335 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6336
6337 // Fill in the "end" label in the FOR statement so it can jump here
6338 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6339 isn->isn_arg.forloop.for_end = instr->ga_len;
6340
6341 // Fill in the "end" label any BREAK statements
6342 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6343
6344 // Below the ":for" scope drop the "expr" list from the stack.
6345 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6346 return NULL;
6347
6348 vim_free(scope);
6349
6350 return arg;
6351}
6352
6353/*
6354 * compile "while expr"
6355 *
6356 * Produces instructions:
6357 * top: EVAL expr Push result of "expr"
6358 * JUMP_IF_FALSE end jump if false
6359 * ... body ...
6360 * JUMP top Jump back to repeat
6361 * end:
6362 *
6363 */
6364 static char_u *
6365compile_while(char_u *arg, cctx_T *cctx)
6366{
6367 char_u *p = arg;
6368 garray_T *instr = &cctx->ctx_instr;
6369 scope_T *scope;
6370
6371 scope = new_scope(cctx, WHILE_SCOPE);
6372 if (scope == NULL)
6373 return NULL;
6374
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006375 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006376
6377 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006378 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006379 return NULL;
6380
6381 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006382 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383 JUMP_IF_FALSE, cctx) == FAIL)
6384 return FAIL;
6385
6386 return p;
6387}
6388
6389/*
6390 * compile "endwhile"
6391 */
6392 static char_u *
6393compile_endwhile(char_u *arg, cctx_T *cctx)
6394{
6395 scope_T *scope = cctx->ctx_scope;
6396
6397 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6398 {
6399 emsg(_(e_while));
6400 return NULL;
6401 }
6402 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006403 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006404
6405 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006406 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006407
6408 // Fill in the "end" label in the WHILE statement so it can jump here.
6409 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006410 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006411
6412 vim_free(scope);
6413
6414 return arg;
6415}
6416
6417/*
6418 * compile "continue"
6419 */
6420 static char_u *
6421compile_continue(char_u *arg, cctx_T *cctx)
6422{
6423 scope_T *scope = cctx->ctx_scope;
6424
6425 for (;;)
6426 {
6427 if (scope == NULL)
6428 {
6429 emsg(_(e_continue));
6430 return NULL;
6431 }
6432 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6433 break;
6434 scope = scope->se_outer;
6435 }
6436
6437 // Jump back to the FOR or WHILE instruction.
6438 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006439 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6440 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441 return arg;
6442}
6443
6444/*
6445 * compile "break"
6446 */
6447 static char_u *
6448compile_break(char_u *arg, cctx_T *cctx)
6449{
6450 scope_T *scope = cctx->ctx_scope;
6451 endlabel_T **el;
6452
6453 for (;;)
6454 {
6455 if (scope == NULL)
6456 {
6457 emsg(_(e_break));
6458 return NULL;
6459 }
6460 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6461 break;
6462 scope = scope->se_outer;
6463 }
6464
6465 // Jump to the end of the FOR or WHILE loop.
6466 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006467 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006469 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006470 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6471 return FAIL;
6472
6473 return arg;
6474}
6475
6476/*
6477 * compile "{" start of block
6478 */
6479 static char_u *
6480compile_block(char_u *arg, cctx_T *cctx)
6481{
6482 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6483 return NULL;
6484 return skipwhite(arg + 1);
6485}
6486
6487/*
6488 * compile end of block: drop one scope
6489 */
6490 static void
6491compile_endblock(cctx_T *cctx)
6492{
6493 scope_T *scope = cctx->ctx_scope;
6494
6495 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006496 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497 vim_free(scope);
6498}
6499
6500/*
6501 * compile "try"
6502 * Creates a new scope for the try-endtry, pointing to the first catch and
6503 * finally.
6504 * Creates another scope for the "try" block itself.
6505 * TRY instruction sets up exception handling at runtime.
6506 *
6507 * "try"
6508 * TRY -> catch1, -> finally push trystack entry
6509 * ... try block
6510 * "throw {exception}"
6511 * EVAL {exception}
6512 * THROW create exception
6513 * ... try block
6514 * " catch {expr}"
6515 * JUMP -> finally
6516 * catch1: PUSH exeception
6517 * EVAL {expr}
6518 * MATCH
6519 * JUMP nomatch -> catch2
6520 * CATCH remove exception
6521 * ... catch block
6522 * " catch"
6523 * JUMP -> finally
6524 * catch2: CATCH remove exception
6525 * ... catch block
6526 * " finally"
6527 * finally:
6528 * ... finally block
6529 * " endtry"
6530 * ENDTRY pop trystack entry, may rethrow
6531 */
6532 static char_u *
6533compile_try(char_u *arg, cctx_T *cctx)
6534{
6535 garray_T *instr = &cctx->ctx_instr;
6536 scope_T *try_scope;
6537 scope_T *scope;
6538
6539 // scope that holds the jumps that go to catch/finally/endtry
6540 try_scope = new_scope(cctx, TRY_SCOPE);
6541 if (try_scope == NULL)
6542 return NULL;
6543
6544 // "catch" is set when the first ":catch" is found.
6545 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006546 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 if (generate_instr(cctx, ISN_TRY) == NULL)
6548 return NULL;
6549
6550 // scope for the try block itself
6551 scope = new_scope(cctx, BLOCK_SCOPE);
6552 if (scope == NULL)
6553 return NULL;
6554
6555 return arg;
6556}
6557
6558/*
6559 * compile "catch {expr}"
6560 */
6561 static char_u *
6562compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6563{
6564 scope_T *scope = cctx->ctx_scope;
6565 garray_T *instr = &cctx->ctx_instr;
6566 char_u *p;
6567 isn_T *isn;
6568
6569 // end block scope from :try or :catch
6570 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6571 compile_endblock(cctx);
6572 scope = cctx->ctx_scope;
6573
6574 // Error if not in a :try scope
6575 if (scope == NULL || scope->se_type != TRY_SCOPE)
6576 {
6577 emsg(_(e_catch));
6578 return NULL;
6579 }
6580
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006581 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582 {
6583 emsg(_("E1033: catch unreachable after catch-all"));
6584 return NULL;
6585 }
6586
6587 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006588 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589 JUMP_ALWAYS, cctx) == FAIL)
6590 return NULL;
6591
6592 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006593 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 if (isn->isn_arg.try.try_catch == 0)
6595 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006596 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 {
6598 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006599 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 isn->isn_arg.jump.jump_where = instr->ga_len;
6601 }
6602
6603 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006604 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006605 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006606 scope->se_u.se_try.ts_caught_all = TRUE;
6607 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 }
6609 else
6610 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006611 char_u *end;
6612 char_u *pat;
6613 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006614 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006615 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 // Push v:exception, push {expr} and MATCH
6618 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6619
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006620 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006621 if (*end != *p)
6622 {
6623 semsg(_("E1067: Separator mismatch: %s"), p);
6624 vim_free(tofree);
6625 return FAIL;
6626 }
6627 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006628 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006629 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006630 len = (int)(end - tofree);
6631 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006632 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006633 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006634 if (pat == NULL)
6635 return FAIL;
6636 if (generate_PUSHS(cctx, pat) == FAIL)
6637 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006638
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6640 return NULL;
6641
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006642 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6644 return NULL;
6645 }
6646
6647 if (generate_instr(cctx, ISN_CATCH) == NULL)
6648 return NULL;
6649
6650 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6651 return NULL;
6652 return p;
6653}
6654
6655 static char_u *
6656compile_finally(char_u *arg, cctx_T *cctx)
6657{
6658 scope_T *scope = cctx->ctx_scope;
6659 garray_T *instr = &cctx->ctx_instr;
6660 isn_T *isn;
6661
6662 // end block scope from :try or :catch
6663 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6664 compile_endblock(cctx);
6665 scope = cctx->ctx_scope;
6666
6667 // Error if not in a :try scope
6668 if (scope == NULL || scope->se_type != TRY_SCOPE)
6669 {
6670 emsg(_(e_finally));
6671 return NULL;
6672 }
6673
6674 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006675 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676 if (isn->isn_arg.try.try_finally != 0)
6677 {
6678 emsg(_(e_finally_dup));
6679 return NULL;
6680 }
6681
6682 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006683 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006684
Bram Moolenaar585fea72020-04-02 22:33:21 +02006685 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006686 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687 {
6688 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006689 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006691 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692 }
6693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 // TODO: set index in ts_finally_label jumps
6695
6696 return arg;
6697}
6698
6699 static char_u *
6700compile_endtry(char_u *arg, cctx_T *cctx)
6701{
6702 scope_T *scope = cctx->ctx_scope;
6703 garray_T *instr = &cctx->ctx_instr;
6704 isn_T *isn;
6705
6706 // end block scope from :catch or :finally
6707 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6708 compile_endblock(cctx);
6709 scope = cctx->ctx_scope;
6710
6711 // Error if not in a :try scope
6712 if (scope == NULL || scope->se_type != TRY_SCOPE)
6713 {
6714 if (scope == NULL)
6715 emsg(_(e_no_endtry));
6716 else if (scope->se_type == WHILE_SCOPE)
6717 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006718 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 emsg(_(e_endfor));
6720 else
6721 emsg(_(e_endif));
6722 return NULL;
6723 }
6724
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006725 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6727 {
6728 emsg(_("E1032: missing :catch or :finally"));
6729 return NULL;
6730 }
6731
6732 // Fill in the "end" label in jumps at the end of the blocks, if not done
6733 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006734 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006735
6736 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006737 if (isn->isn_arg.try.try_catch == 0)
6738 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739 if (isn->isn_arg.try.try_finally == 0)
6740 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006741
6742 if (scope->se_u.se_try.ts_catch_label != 0)
6743 {
6744 // Last catch without match jumps here
6745 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6746 isn->isn_arg.jump.jump_where = instr->ga_len;
6747 }
6748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749 compile_endblock(cctx);
6750
6751 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6752 return NULL;
6753 return arg;
6754}
6755
6756/*
6757 * compile "throw {expr}"
6758 */
6759 static char_u *
6760compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6761{
6762 char_u *p = skipwhite(arg);
6763
Bram Moolenaara5565e42020-05-09 15:44:01 +02006764 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006765 return NULL;
6766 if (may_generate_2STRING(-1, cctx) == FAIL)
6767 return NULL;
6768 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6769 return NULL;
6770
6771 return p;
6772}
6773
6774/*
6775 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006776 * compile "echomsg expr"
6777 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006778 * compile "execute expr"
6779 */
6780 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006781compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006782{
6783 char_u *p = arg;
6784 int count = 0;
6785
6786 for (;;)
6787 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006788 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006789 return NULL;
6790 ++count;
6791 p = skipwhite(p);
6792 if (ends_excmd(*p))
6793 break;
6794 }
6795
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006796 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6797 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6798 else if (cmdidx == CMD_execute)
6799 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6800 else if (cmdidx == CMD_echomsg)
6801 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6802 else
6803 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 return p;
6805}
6806
6807/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006808 * A command that is not compiled, execute with legacy code.
6809 */
6810 static char_u *
6811compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6812{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006813 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006814 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006815 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006816
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006817 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006818 goto theend;
6819
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006820 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006821 {
6822 long argt = excmd_get_argt(eap->cmdidx);
6823 int usefilter = FALSE;
6824
6825 has_expr = argt & (EX_XFILE | EX_EXPAND);
6826
6827 // If the command can be followed by a bar, find the bar and truncate
6828 // it, so that the following command can be compiled.
6829 // The '|' is overwritten with a NUL, it is put back below.
6830 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6831 && *eap->arg == '!')
6832 // :w !filter or :r !filter or :r! filter
6833 usefilter = TRUE;
6834 if ((argt & EX_TRLBAR) && !usefilter)
6835 {
6836 separate_nextcmd(eap);
6837 if (eap->nextcmd != NULL)
6838 nextcmd = eap->nextcmd;
6839 }
6840 }
6841
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006842 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6843 {
6844 // expand filename in "syntax include [@group] filename"
6845 has_expr = TRUE;
6846 eap->arg = skipwhite(eap->arg + 7);
6847 if (*eap->arg == '@')
6848 eap->arg = skiptowhite(eap->arg);
6849 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006850
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006851 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006852 {
6853 int count = 0;
6854 char_u *start = skipwhite(line);
6855
6856 // :cmd xxx`=expr1`yyy`=expr2`zzz
6857 // PUSHS ":cmd xxx"
6858 // eval expr1
6859 // PUSHS "yyy"
6860 // eval expr2
6861 // PUSHS "zzz"
6862 // EXECCONCAT 5
6863 for (;;)
6864 {
6865 if (p > start)
6866 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006867 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006868 ++count;
6869 }
6870 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006871 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006872 return NULL;
6873 may_generate_2STRING(-1, cctx);
6874 ++count;
6875 p = skipwhite(p);
6876 if (*p != '`')
6877 {
6878 emsg(_("E1083: missing backtick"));
6879 return NULL;
6880 }
6881 start = p + 1;
6882
6883 p = (char_u *)strstr((char *)start, "`=");
6884 if (p == NULL)
6885 {
6886 if (*skipwhite(start) != NUL)
6887 {
6888 generate_PUSHS(cctx, vim_strsave(start));
6889 ++count;
6890 }
6891 break;
6892 }
6893 }
6894 generate_EXECCONCAT(cctx, count);
6895 }
6896 else
6897 generate_EXEC(cctx, line);
6898
6899theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006900 if (*nextcmd != NUL)
6901 {
6902 // the parser expects a pointer to the bar, put it back
6903 --nextcmd;
6904 *nextcmd = '|';
6905 }
6906
6907 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006908}
6909
6910/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006911 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006912 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006913 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006914 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006915add_def_function(ufunc_T *ufunc)
6916{
6917 dfunc_T *dfunc;
6918
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006919 if (def_functions.ga_len == 0)
6920 {
6921 // The first position is not used, so that a zero uf_dfunc_idx means it
6922 // wasn't set.
6923 if (ga_grow(&def_functions, 1) == FAIL)
6924 return FAIL;
6925 ++def_functions.ga_len;
6926 }
6927
Bram Moolenaar09689a02020-05-09 22:50:08 +02006928 // Add the function to "def_functions".
6929 if (ga_grow(&def_functions, 1) == FAIL)
6930 return FAIL;
6931 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6932 CLEAR_POINTER(dfunc);
6933 dfunc->df_idx = def_functions.ga_len;
6934 ufunc->uf_dfunc_idx = dfunc->df_idx;
6935 dfunc->df_ufunc = ufunc;
6936 ++def_functions.ga_len;
6937 return OK;
6938}
6939
6940/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 * After ex_function() has collected all the function lines: parse and compile
6942 * the lines into instructions.
6943 * Adds the function to "def_functions".
6944 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6945 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006946 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006947 * This can be used recursively through compile_lambda(), which may reallocate
6948 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006949 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006951 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006952compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954 char_u *line = NULL;
6955 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957 cctx_T cctx;
6958 garray_T *instr;
6959 int called_emsg_before = called_emsg;
6960 int ret = FAIL;
6961 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006962 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006963 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006964 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006965
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006966 // When using a function that was compiled before: Free old instructions.
6967 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006968 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006970 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6971 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006972 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006974 else
6975 {
6976 if (add_def_function(ufunc) == FAIL)
6977 return FAIL;
6978 new_def_function = TRUE;
6979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006980
Bram Moolenaar985116a2020-07-12 17:31:09 +02006981 ufunc->uf_def_status = UF_COMPILING;
6982
Bram Moolenaara80faa82020-04-12 19:37:17 +02006983 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 cctx.ctx_ufunc = ufunc;
6985 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006986 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6988 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6989 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6990 cctx.ctx_type_list = &ufunc->uf_type_list;
6991 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6992 instr = &cctx.ctx_instr;
6993
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006994 // Set the context to the function, it may be compiled when called from
6995 // another script. Set the script version to the most modern one.
6996 // The line number will be set in next_line_from_context().
6997 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006998 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6999
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007000 // Make sure error messages are OK.
7001 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7002 if (do_estack_push)
7003 estack_push_ufunc(ufunc, 1);
7004
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007005 if (ufunc->uf_def_args.ga_len > 0)
7006 {
7007 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007008 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007009 int i;
7010 char_u *arg;
7011 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007012 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007013
7014 // Produce instructions for the default values of optional arguments.
7015 // Store the instruction index in uf_def_arg_idx[] so that we know
7016 // where to start when the function is called, depending on the number
7017 // of arguments.
7018 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7019 if (ufunc->uf_def_arg_idx == NULL)
7020 goto erret;
7021 for (i = 0; i < count; ++i)
7022 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007023 garray_T *stack = &cctx.ctx_type_stack;
7024 type_T *val_type;
7025 int arg_idx = first_def_arg + i;
7026
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007027 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7028 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007029 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007030 goto erret;
7031
7032 // If no type specified use the type of the default value.
7033 // Otherwise check that the default value type matches the
7034 // specified type.
7035 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7036 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007037 {
7038 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007039 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007040 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02007041 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007042 == FAIL)
7043 {
7044 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
7045 arg_idx + 1);
7046 goto erret;
7047 }
7048
7049 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007050 goto erret;
7051 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007052 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007053
7054 if (did_set_arg_type)
7055 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007056 }
7057
7058 /*
7059 * Loop over all the lines of the function and generate instructions.
7060 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061 for (;;)
7062 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007063 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007064 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007065 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007066 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007067
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007068 // Bail out on the first error to avoid a flood of errors and report
7069 // the right line number when inside try/catch.
7070 if (emsg_before != called_emsg)
7071 goto erret;
7072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073 if (line != NULL && *line == '|')
7074 // the line continues after a '|'
7075 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007076 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007077 && !(*line == '#' && (line == cctx.ctx_line_start
7078 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007080 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081 goto erret;
7082 }
7083 else
7084 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007085 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007086 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007087 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007090 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007091
Bram Moolenaara80faa82020-04-12 19:37:17 +02007092 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093 ea.cmdlinep = &line;
7094 ea.cmd = skipwhite(line);
7095
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007096 // Some things can be recognized by the first character.
7097 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007098 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007099 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007100 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007101 if (ea.cmd[1] != '{')
7102 {
7103 line = (char_u *)"";
7104 continue;
7105 }
7106 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007108 case '}':
7109 {
7110 // "}" ends a block scope
7111 scopetype_T stype = cctx.ctx_scope == NULL
7112 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007114 if (stype == BLOCK_SCOPE)
7115 {
7116 compile_endblock(&cctx);
7117 line = ea.cmd;
7118 }
7119 else
7120 {
7121 emsg(_("E1025: using } outside of a block scope"));
7122 goto erret;
7123 }
7124 if (line != NULL)
7125 line = skipwhite(ea.cmd + 1);
7126 continue;
7127 }
7128
7129 case '{':
7130 // "{" starts a block scope
7131 // "{'a': 1}->func() is something else
7132 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7133 {
7134 line = compile_block(ea.cmd, &cctx);
7135 continue;
7136 }
7137 break;
7138
7139 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007140 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007141 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 }
7143
7144 /*
7145 * COMMAND MODIFIERS
7146 */
7147 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7148 {
7149 if (errormsg != NULL)
7150 goto erret;
7151 // empty line or comment
7152 line = (char_u *)"";
7153 continue;
7154 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007155 // TODO: use modifiers in the command
7156 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007157 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158
7159 // Skip ":call" to get to the function name.
7160 if (checkforcmd(&ea.cmd, "call", 3))
7161 ea.cmd = skipwhite(ea.cmd);
7162
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007163 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007165 char_u *pskip;
7166
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007167 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007168 // find what follows.
7169 // Skip over "var.member", "var[idx]" and the like.
7170 // Also "&opt = val", "$ENV = val" and "@r = val".
7171 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007172 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007173 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007174 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007176 char_u *var_end;
7177 int oplen;
7178 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007179
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007180 var_end = find_name_end(pskip, NULL, NULL,
7181 FNE_CHECK_START | FNE_INCL_BR);
7182 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007183 if (oplen > 0)
7184 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007185 size_t len = p - ea.cmd;
7186
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007187 // Recognize an assignment if we recognize the variable
7188 // name:
7189 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007190 // "local = expr" where "local" is a local var.
7191 // "script = expr" where "script" is a script-local var.
7192 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007193 // "&opt = expr"
7194 // "$ENV = expr"
7195 // "@r = expr"
7196 if (*ea.cmd == '&'
7197 || *ea.cmd == '$'
7198 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007199 || ((len) > 2 && ea.cmd[1] == ':')
7200 || lookup_local(ea.cmd, len, &cctx) != NULL
7201 || lookup_arg(ea.cmd, len, NULL, NULL,
7202 NULL, &cctx) == OK
7203 || lookup_script(ea.cmd, len) == OK
7204 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007205 {
7206 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007207 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007208 goto erret;
7209 continue;
7210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211 }
7212 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007213
7214 if (*ea.cmd == '[')
7215 {
7216 // [var, var] = expr
7217 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7218 if (line == NULL)
7219 goto erret;
7220 if (line != ea.cmd)
7221 continue;
7222 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007223 }
7224
7225 /*
7226 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007227 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007229 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007230 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007231 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007232 ea.cmd = skip_range(ea.cmd, NULL);
7233 if (ea.cmd > cmd && !starts_with_colon)
7234 {
7235 emsg(_(e_colon_required));
7236 goto erret;
7237 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007238 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007239 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007240 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007241 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007242
7243 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7244 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007245 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007246 {
7247 line += STRLEN(line);
7248 continue;
7249 }
7250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007251 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007252 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007254 // CMD_let cannot happen, compile_assignment() above is used
7255 iemsg("Command from find_ex_command() not handled");
7256 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 }
7259
7260 p = skipwhite(p);
7261
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007262 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007263 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007264 && ea.cmdidx != CMD_elseif
7265 && ea.cmdidx != CMD_else
7266 && ea.cmdidx != CMD_endif)
7267 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007268 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007269 continue;
7270 }
7271
Bram Moolenaarefd88552020-06-18 20:50:10 +02007272 if (ea.cmdidx != CMD_elseif
7273 && ea.cmdidx != CMD_else
7274 && ea.cmdidx != CMD_endif
7275 && ea.cmdidx != CMD_endfor
7276 && ea.cmdidx != CMD_endwhile
7277 && ea.cmdidx != CMD_catch
7278 && ea.cmdidx != CMD_finally
7279 && ea.cmdidx != CMD_endtry)
7280 {
7281 if (cctx.ctx_had_return)
7282 {
7283 emsg(_("E1095: Unreachable code after :return"));
7284 goto erret;
7285 }
7286 }
7287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007288 switch (ea.cmdidx)
7289 {
7290 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007291 ea.arg = p;
7292 line = compile_nested_function(&ea, &cctx);
7293 break;
7294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007296 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007297 goto erret;
7298
7299 case CMD_return:
7300 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007301 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007302 break;
7303
7304 case CMD_let:
7305 case CMD_const:
7306 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007307 if (line == p)
7308 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007309 break;
7310
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007311 case CMD_unlet:
7312 case CMD_unlockvar:
7313 case CMD_lockvar:
7314 line = compile_unletlock(p, &ea, &cctx);
7315 break;
7316
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 case CMD_import:
7318 line = compile_import(p, &cctx);
7319 break;
7320
7321 case CMD_if:
7322 line = compile_if(p, &cctx);
7323 break;
7324 case CMD_elseif:
7325 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007326 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007327 break;
7328 case CMD_else:
7329 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007330 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331 break;
7332 case CMD_endif:
7333 line = compile_endif(p, &cctx);
7334 break;
7335
7336 case CMD_while:
7337 line = compile_while(p, &cctx);
7338 break;
7339 case CMD_endwhile:
7340 line = compile_endwhile(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
7344 case CMD_for:
7345 line = compile_for(p, &cctx);
7346 break;
7347 case CMD_endfor:
7348 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007349 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 break;
7351 case CMD_continue:
7352 line = compile_continue(p, &cctx);
7353 break;
7354 case CMD_break:
7355 line = compile_break(p, &cctx);
7356 break;
7357
7358 case CMD_try:
7359 line = compile_try(p, &cctx);
7360 break;
7361 case CMD_catch:
7362 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007363 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364 break;
7365 case CMD_finally:
7366 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007367 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368 break;
7369 case CMD_endtry:
7370 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007371 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 break;
7373 case CMD_throw:
7374 line = compile_throw(p, &cctx);
7375 break;
7376
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007377 case CMD_eval:
7378 if (compile_expr0(&p, &cctx) == FAIL)
7379 goto erret;
7380
7381 // drop the return value
7382 generate_instr_drop(&cctx, ISN_DROP, 1);
7383
7384 line = skipwhite(p);
7385 break;
7386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007389 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007390 case CMD_echomsg:
7391 case CMD_echoerr:
7392 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007393 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007395 // TODO: other commands with an expression argument
7396
Bram Moolenaar002262f2020-07-08 17:47:57 +02007397 case CMD_SIZE:
7398 semsg(_("E476: Invalid command: %s"), ea.cmd);
7399 goto erret;
7400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007402 // Not recognized, execute with do_cmdline_cmd().
7403 ea.arg = p;
7404 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405 break;
7406 }
7407 if (line == NULL)
7408 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007409 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410
7411 if (cctx.ctx_type_stack.ga_len < 0)
7412 {
7413 iemsg("Type stack underflow");
7414 goto erret;
7415 }
7416 }
7417
7418 if (cctx.ctx_scope != NULL)
7419 {
7420 if (cctx.ctx_scope->se_type == IF_SCOPE)
7421 emsg(_(e_endif));
7422 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7423 emsg(_(e_endwhile));
7424 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7425 emsg(_(e_endfor));
7426 else
7427 emsg(_("E1026: Missing }"));
7428 goto erret;
7429 }
7430
Bram Moolenaarefd88552020-06-18 20:50:10 +02007431 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 {
7433 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7434 {
7435 emsg(_("E1027: Missing return statement"));
7436 goto erret;
7437 }
7438
7439 // Return zero if there is no return at the end.
7440 generate_PUSHNR(&cctx, 0);
7441 generate_instr(&cctx, ISN_RETURN);
7442 }
7443
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007444 {
7445 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7446 + ufunc->uf_dfunc_idx;
7447 dfunc->df_deleted = FALSE;
7448 dfunc->df_instr = instr->ga_data;
7449 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007450 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007451 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007452 if (cctx.ctx_outer_used)
7453 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007454 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456
7457 ret = OK;
7458
7459erret:
7460 if (ret == FAIL)
7461 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007462 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007463 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7464 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007465
7466 for (idx = 0; idx < instr->ga_len; ++idx)
7467 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007469
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007470 // If using the last entry in the table and it was added above, we
7471 // might as well remove it.
7472 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007473 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007474 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007475 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007476 ufunc->uf_dfunc_idx = 0;
7477 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007478 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007479
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007480 while (cctx.ctx_scope != NULL)
7481 drop_scope(&cctx);
7482
Bram Moolenaar20431c92020-03-20 18:39:46 +01007483 // Don't execute this function body.
7484 ga_clear_strings(&ufunc->uf_lines);
7485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 if (errormsg != NULL)
7487 emsg(errormsg);
7488 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007489 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007490 }
7491
7492 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007493 if (do_estack_push)
7494 estack_pop();
7495
Bram Moolenaar20431c92020-03-20 18:39:46 +01007496 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007497 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007498 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007499 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007500}
7501
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007502 void
7503set_function_type(ufunc_T *ufunc)
7504{
7505 int varargs = ufunc->uf_va_name != NULL;
7506 int argcount = ufunc->uf_args.ga_len;
7507
7508 // Create a type for the function, with the return type and any
7509 // argument types.
7510 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7511 // The type is included in "tt_args".
7512 if (argcount > 0 || varargs)
7513 {
7514 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7515 argcount, &ufunc->uf_type_list);
7516 // Add argument types to the function type.
7517 if (func_type_add_arg_types(ufunc->uf_func_type,
7518 argcount + varargs,
7519 &ufunc->uf_type_list) == FAIL)
7520 return;
7521 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7522 ufunc->uf_func_type->tt_min_argcount =
7523 argcount - ufunc->uf_def_args.ga_len;
7524 if (ufunc->uf_arg_types == NULL)
7525 {
7526 int i;
7527
7528 // lambda does not have argument types.
7529 for (i = 0; i < argcount; ++i)
7530 ufunc->uf_func_type->tt_args[i] = &t_any;
7531 }
7532 else
7533 mch_memmove(ufunc->uf_func_type->tt_args,
7534 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7535 if (varargs)
7536 {
7537 ufunc->uf_func_type->tt_args[argcount] =
7538 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7539 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7540 }
7541 }
7542 else
7543 // No arguments, can use a predefined type.
7544 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7545 argcount, &ufunc->uf_type_list);
7546}
7547
7548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549/*
7550 * Delete an instruction, free what it contains.
7551 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007552 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007553delete_instr(isn_T *isn)
7554{
7555 switch (isn->isn_type)
7556 {
7557 case ISN_EXEC:
7558 case ISN_LOADENV:
7559 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007560 case ISN_LOADB:
7561 case ISN_LOADW:
7562 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007564 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565 case ISN_PUSHEXC:
7566 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007567 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007569 case ISN_STOREB:
7570 case ISN_STOREW:
7571 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007572 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573 vim_free(isn->isn_arg.string);
7574 break;
7575
7576 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007577 case ISN_STORES:
7578 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007579 break;
7580
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007581 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007582 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007583 vim_free(isn->isn_arg.unlet.ul_name);
7584 break;
7585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007586 case ISN_STOREOPT:
7587 vim_free(isn->isn_arg.storeopt.so_name);
7588 break;
7589
7590 case ISN_PUSHBLOB: // push blob isn_arg.blob
7591 blob_unref(isn->isn_arg.blob);
7592 break;
7593
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007594 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007595#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007596 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007597#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007598 break;
7599
7600 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007601#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007602 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007603#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007604 break;
7605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007606 case ISN_UCALL:
7607 vim_free(isn->isn_arg.ufunc.cuf_name);
7608 break;
7609
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007610 case ISN_FUNCREF:
7611 {
7612 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7613 + isn->isn_arg.funcref.fr_func;
7614 func_ptr_unref(dfunc->df_ufunc);
7615 }
7616 break;
7617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007618 case ISN_2BOOL:
7619 case ISN_2STRING:
7620 case ISN_ADDBLOB:
7621 case ISN_ADDLIST:
7622 case ISN_BCALL:
7623 case ISN_CATCH:
7624 case ISN_CHECKNR:
7625 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007626 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627 case ISN_COMPAREANY:
7628 case ISN_COMPAREBLOB:
7629 case ISN_COMPAREBOOL:
7630 case ISN_COMPAREDICT:
7631 case ISN_COMPAREFLOAT:
7632 case ISN_COMPAREFUNC:
7633 case ISN_COMPARELIST:
7634 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635 case ISN_COMPARESPECIAL:
7636 case ISN_COMPARESTRING:
7637 case ISN_CONCAT:
7638 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007639 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640 case ISN_DROP:
7641 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007642 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007643 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007644 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007645 case ISN_EXECCONCAT:
7646 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007647 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007648 case ISN_LISTINDEX:
7649 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007650 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007651 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007652 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007653 case ISN_JUMP:
7654 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007655 case ISN_LOADBDICT:
7656 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007657 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007658 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007659 case ISN_LOADSCRIPT:
7660 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007661 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007662 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007663 case ISN_NEGATENR:
7664 case ISN_NEWDICT:
7665 case ISN_NEWLIST:
7666 case ISN_OPNR:
7667 case ISN_OPFLOAT:
7668 case ISN_OPANY:
7669 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007670 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007671 case ISN_PUSHF:
7672 case ISN_PUSHNR:
7673 case ISN_PUSHBOOL:
7674 case ISN_PUSHSPEC:
7675 case ISN_RETURN:
7676 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007677 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007678 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007679 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007680 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007682 case ISN_STOREDICT:
7683 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684 case ISN_THROW:
7685 case ISN_TRY:
7686 // nothing allocated
7687 break;
7688 }
7689}
7690
7691/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007692 * Free all instructions for "dfunc".
7693 */
7694 static void
7695delete_def_function_contents(dfunc_T *dfunc)
7696{
7697 int idx;
7698
7699 ga_clear(&dfunc->df_def_args_isn);
7700
7701 if (dfunc->df_instr != NULL)
7702 {
7703 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7704 delete_instr(dfunc->df_instr + idx);
7705 VIM_CLEAR(dfunc->df_instr);
7706 }
7707
7708 dfunc->df_deleted = TRUE;
7709}
7710
7711/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007712 * When a user function is deleted, clear the contents of any associated def
7713 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 */
7715 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007716clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007718 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007719 {
7720 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7721 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007722
Bram Moolenaar20431c92020-03-20 18:39:46 +01007723 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007724 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 }
7726}
7727
7728#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007729/*
7730 * Free all functions defined with ":def".
7731 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007732 void
7733free_def_functions(void)
7734{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007735 int idx;
7736
7737 for (idx = 0; idx < def_functions.ga_len; ++idx)
7738 {
7739 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7740
7741 delete_def_function_contents(dfunc);
7742 }
7743
7744 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745}
7746#endif
7747
7748
7749#endif // FEAT_EVAL