blob: e032e23a92e0cdb1e63b624ce7981e3195fbf28a [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;
641 }
642 if (ret == FAIL && give_msg)
643 type_mismatch(expected, actual);
644 }
645 return ret;
646}
647
Bram Moolenaar65b95452020-07-19 14:03:09 +0200648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649/////////////////////////////////////////////////////////////////////
650// Following generate_ functions expect the caller to call ga_grow().
651
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200652#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
653#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655/*
656 * Generate an instruction without arguments.
657 * Returns a pointer to the new instruction, NULL if failed.
658 */
659 static isn_T *
660generate_instr(cctx_T *cctx, isntype_T isn_type)
661{
662 garray_T *instr = &cctx->ctx_instr;
663 isn_T *isn;
664
Bram Moolenaar080457c2020-03-03 21:53:32 +0100665 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 if (ga_grow(instr, 1) == FAIL)
667 return NULL;
668 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
669 isn->isn_type = isn_type;
670 isn->isn_lnum = cctx->ctx_lnum + 1;
671 ++instr->ga_len;
672
673 return isn;
674}
675
676/*
677 * Generate an instruction without arguments.
678 * "drop" will be removed from the stack.
679 * Returns a pointer to the new instruction, NULL if failed.
680 */
681 static isn_T *
682generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
683{
684 garray_T *stack = &cctx->ctx_type_stack;
685
Bram Moolenaar080457c2020-03-03 21:53:32 +0100686 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100687 stack->ga_len -= drop;
688 return generate_instr(cctx, isn_type);
689}
690
691/*
692 * Generate instruction "isn_type" and put "type" on the type stack.
693 */
694 static isn_T *
695generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
696{
697 isn_T *isn;
698 garray_T *stack = &cctx->ctx_type_stack;
699
700 if ((isn = generate_instr(cctx, isn_type)) == NULL)
701 return NULL;
702
703 if (ga_grow(stack, 1) == FAIL)
704 return NULL;
705 ((type_T **)stack->ga_data)[stack->ga_len] = type;
706 ++stack->ga_len;
707
708 return isn;
709}
710
711/*
712 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
713 */
714 static int
715may_generate_2STRING(int offset, cctx_T *cctx)
716{
717 isn_T *isn;
718 garray_T *stack = &cctx->ctx_type_stack;
719 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
720
721 if ((*type)->tt_type == VAR_STRING)
722 return OK;
723 *type = &t_string;
724
725 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
726 return FAIL;
727 isn->isn_arg.number = offset;
728
729 return OK;
730}
731
732 static int
733check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
734{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200735 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200737 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 {
739 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200740 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 else
742 semsg(_("E1036: %c requires number or float arguments"), *op);
743 return FAIL;
744 }
745 return OK;
746}
747
748/*
749 * Generate an instruction with two arguments. The instruction depends on the
750 * type of the arguments.
751 */
752 static int
753generate_two_op(cctx_T *cctx, char_u *op)
754{
755 garray_T *stack = &cctx->ctx_type_stack;
756 type_T *type1;
757 type_T *type2;
758 vartype_T vartype;
759 isn_T *isn;
760
Bram Moolenaar080457c2020-03-03 21:53:32 +0100761 RETURN_OK_IF_SKIP(cctx);
762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763 // Get the known type of the two items on the stack. If they are matching
764 // use a type-specific instruction. Otherwise fall back to runtime type
765 // checking.
766 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
767 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200768 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 if (type1->tt_type == type2->tt_type
770 && (type1->tt_type == VAR_NUMBER
771 || type1->tt_type == VAR_LIST
772#ifdef FEAT_FLOAT
773 || type1->tt_type == VAR_FLOAT
774#endif
775 || type1->tt_type == VAR_BLOB))
776 vartype = type1->tt_type;
777
778 switch (*op)
779 {
780 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200781 && type1->tt_type != VAR_ANY
782 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 && check_number_or_float(
784 type1->tt_type, type2->tt_type, op) == FAIL)
785 return FAIL;
786 isn = generate_instr_drop(cctx,
787 vartype == VAR_NUMBER ? ISN_OPNR
788 : vartype == VAR_LIST ? ISN_ADDLIST
789 : vartype == VAR_BLOB ? ISN_ADDBLOB
790#ifdef FEAT_FLOAT
791 : vartype == VAR_FLOAT ? ISN_OPFLOAT
792#endif
793 : ISN_OPANY, 1);
794 if (isn != NULL)
795 isn->isn_arg.op.op_type = EXPR_ADD;
796 break;
797
798 case '-':
799 case '*':
800 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
801 op) == FAIL)
802 return FAIL;
803 if (vartype == VAR_NUMBER)
804 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
805#ifdef FEAT_FLOAT
806 else if (vartype == VAR_FLOAT)
807 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
808#endif
809 else
810 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
811 if (isn != NULL)
812 isn->isn_arg.op.op_type = *op == '*'
813 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
814 break;
815
Bram Moolenaar4c683752020-04-05 21:38:23 +0200816 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200818 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 && type2->tt_type != VAR_NUMBER))
820 {
821 emsg(_("E1035: % requires number arguments"));
822 return FAIL;
823 }
824 isn = generate_instr_drop(cctx,
825 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
826 if (isn != NULL)
827 isn->isn_arg.op.op_type = EXPR_REM;
828 break;
829 }
830
831 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200832 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 {
834 type_T *type = &t_any;
835
836#ifdef FEAT_FLOAT
837 // float+number and number+float results in float
838 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
839 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
840 type = &t_float;
841#endif
842 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
843 }
844
845 return OK;
846}
847
848/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200849 * Get the instruction to use for comparing "type1" with "type2"
850 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200852 static isntype_T
853get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854{
855 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856
Bram Moolenaar4c683752020-04-05 21:38:23 +0200857 if (type1 == VAR_UNKNOWN)
858 type1 = VAR_ANY;
859 if (type2 == VAR_UNKNOWN)
860 type2 = VAR_ANY;
861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 if (type1 == type2)
863 {
864 switch (type1)
865 {
866 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
867 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
868 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
869 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
870 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
871 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
872 case VAR_LIST: isntype = ISN_COMPARELIST; break;
873 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
874 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875 default: isntype = ISN_COMPAREANY; break;
876 }
877 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200878 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
880 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
881 isntype = ISN_COMPAREANY;
882
883 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
884 && (isntype == ISN_COMPAREBOOL
885 || isntype == ISN_COMPARESPECIAL
886 || isntype == ISN_COMPARENR
887 || isntype == ISN_COMPAREFLOAT))
888 {
889 semsg(_("E1037: Cannot use \"%s\" with %s"),
890 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200891 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 }
893 if (isntype == ISN_DROP
894 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
895 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
896 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
897 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
898 && exptype != EXPR_IS && exptype != EXPR_ISNOT
899 && (type1 == VAR_BLOB || type2 == VAR_BLOB
900 || type1 == VAR_LIST || type2 == VAR_LIST))))
901 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100902 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200904 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200906 return isntype;
907}
908
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200909 int
910check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
911{
912 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
913 return FAIL;
914 return OK;
915}
916
Bram Moolenaara5565e42020-05-09 15:44:01 +0200917/*
918 * Generate an ISN_COMPARE* instruction with a boolean result.
919 */
920 static int
921generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
922{
923 isntype_T isntype;
924 isn_T *isn;
925 garray_T *stack = &cctx->ctx_type_stack;
926 vartype_T type1;
927 vartype_T type2;
928
929 RETURN_OK_IF_SKIP(cctx);
930
931 // Get the known type of the two items on the stack. If they are matching
932 // use a type-specific instruction. Otherwise fall back to runtime type
933 // checking.
934 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
935 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
936 isntype = get_compare_isn(exptype, type1, type2);
937 if (isntype == ISN_DROP)
938 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939
940 if ((isn = generate_instr(cctx, isntype)) == NULL)
941 return FAIL;
942 isn->isn_arg.op.op_type = exptype;
943 isn->isn_arg.op.op_ic = ic;
944
945 // takes two arguments, puts one bool back
946 if (stack->ga_len >= 2)
947 {
948 --stack->ga_len;
949 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
950 }
951
952 return OK;
953}
954
955/*
956 * Generate an ISN_2BOOL instruction.
957 */
958 static int
959generate_2BOOL(cctx_T *cctx, int invert)
960{
961 isn_T *isn;
962 garray_T *stack = &cctx->ctx_type_stack;
963
Bram Moolenaar080457c2020-03-03 21:53:32 +0100964 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
966 return FAIL;
967 isn->isn_arg.number = invert;
968
969 // type becomes bool
970 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
971
972 return OK;
973}
974
975 static int
976generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
977{
978 isn_T *isn;
979 garray_T *stack = &cctx->ctx_type_stack;
980
Bram Moolenaar080457c2020-03-03 21:53:32 +0100981 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
983 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200984 // TODO: whole type, e.g. for a function also arg and return types
985 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 isn->isn_arg.type.ct_off = offset;
987
988 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200989 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990
991 return OK;
992}
993
994/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200995 * Check that
996 * - "actual" is "expected" type or
997 * - "actual" is a type that can be "expected" type: add a runtime check; or
998 * - return FAIL.
999 */
1000 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001001need_type(
1002 type_T *actual,
1003 type_T *expected,
1004 int offset,
1005 cctx_T *cctx,
1006 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001007{
1008 if (check_type(expected, actual, FALSE) == OK)
1009 return OK;
1010 if (actual->tt_type != VAR_ANY
1011 && actual->tt_type != VAR_UNKNOWN
1012 && !(actual->tt_type == VAR_FUNC
1013 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
1014 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001015 if (!silent)
1016 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001017 return FAIL;
1018 }
1019 generate_TYPECHECK(cctx, expected, offset);
1020 return OK;
1021}
1022
1023/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 * Generate an ISN_PUSHNR instruction.
1025 */
1026 static int
1027generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1028{
1029 isn_T *isn;
1030
Bram Moolenaar080457c2020-03-03 21:53:32 +01001031 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1033 return FAIL;
1034 isn->isn_arg.number = number;
1035
1036 return OK;
1037}
1038
1039/*
1040 * Generate an ISN_PUSHBOOL instruction.
1041 */
1042 static int
1043generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1044{
1045 isn_T *isn;
1046
Bram Moolenaar080457c2020-03-03 21:53:32 +01001047 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1049 return FAIL;
1050 isn->isn_arg.number = number;
1051
1052 return OK;
1053}
1054
1055/*
1056 * Generate an ISN_PUSHSPEC instruction.
1057 */
1058 static int
1059generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1060{
1061 isn_T *isn;
1062
Bram Moolenaar080457c2020-03-03 21:53:32 +01001063 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1065 return FAIL;
1066 isn->isn_arg.number = number;
1067
1068 return OK;
1069}
1070
1071#ifdef FEAT_FLOAT
1072/*
1073 * Generate an ISN_PUSHF instruction.
1074 */
1075 static int
1076generate_PUSHF(cctx_T *cctx, float_T fnumber)
1077{
1078 isn_T *isn;
1079
Bram Moolenaar080457c2020-03-03 21:53:32 +01001080 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1082 return FAIL;
1083 isn->isn_arg.fnumber = fnumber;
1084
1085 return OK;
1086}
1087#endif
1088
1089/*
1090 * Generate an ISN_PUSHS instruction.
1091 * Consumes "str".
1092 */
1093 static int
1094generate_PUSHS(cctx_T *cctx, char_u *str)
1095{
1096 isn_T *isn;
1097
Bram Moolenaar080457c2020-03-03 21:53:32 +01001098 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1100 return FAIL;
1101 isn->isn_arg.string = str;
1102
1103 return OK;
1104}
1105
1106/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001107 * Generate an ISN_PUSHCHANNEL instruction.
1108 * Consumes "channel".
1109 */
1110 static int
1111generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1112{
1113 isn_T *isn;
1114
Bram Moolenaar080457c2020-03-03 21:53:32 +01001115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001116 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1117 return FAIL;
1118 isn->isn_arg.channel = channel;
1119
1120 return OK;
1121}
1122
1123/*
1124 * Generate an ISN_PUSHJOB instruction.
1125 * Consumes "job".
1126 */
1127 static int
1128generate_PUSHJOB(cctx_T *cctx, job_T *job)
1129{
1130 isn_T *isn;
1131
Bram Moolenaar080457c2020-03-03 21:53:32 +01001132 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001133 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001134 return FAIL;
1135 isn->isn_arg.job = job;
1136
1137 return OK;
1138}
1139
1140/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 * Generate an ISN_PUSHBLOB instruction.
1142 * Consumes "blob".
1143 */
1144 static int
1145generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1146{
1147 isn_T *isn;
1148
Bram Moolenaar080457c2020-03-03 21:53:32 +01001149 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1151 return FAIL;
1152 isn->isn_arg.blob = blob;
1153
1154 return OK;
1155}
1156
1157/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001158 * Generate an ISN_PUSHFUNC instruction with name "name".
1159 * Consumes "name".
1160 */
1161 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001162generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001163{
1164 isn_T *isn;
1165
Bram Moolenaar080457c2020-03-03 21:53:32 +01001166 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001167 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001168 return FAIL;
1169 isn->isn_arg.string = name;
1170
1171 return OK;
1172}
1173
1174/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001175 * Generate an ISN_GETITEM instruction with "index".
1176 */
1177 static int
1178generate_GETITEM(cctx_T *cctx, int index)
1179{
1180 isn_T *isn;
1181 garray_T *stack = &cctx->ctx_type_stack;
1182 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1183 type_T *item_type = &t_any;
1184
1185 RETURN_OK_IF_SKIP(cctx);
1186
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001187 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001188 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001189 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001190 emsg(_(e_listreq));
1191 return FAIL;
1192 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001193 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001194 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1195 return FAIL;
1196 isn->isn_arg.number = index;
1197
1198 // add the item type to the type stack
1199 if (ga_grow(stack, 1) == FAIL)
1200 return FAIL;
1201 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1202 ++stack->ga_len;
1203 return OK;
1204}
1205
1206/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001207 * Generate an ISN_SLICE instruction with "count".
1208 */
1209 static int
1210generate_SLICE(cctx_T *cctx, int count)
1211{
1212 isn_T *isn;
1213
1214 RETURN_OK_IF_SKIP(cctx);
1215 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1216 return FAIL;
1217 isn->isn_arg.number = count;
1218 return OK;
1219}
1220
1221/*
1222 * Generate an ISN_CHECKLEN instruction with "min_len".
1223 */
1224 static int
1225generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1226{
1227 isn_T *isn;
1228
1229 RETURN_OK_IF_SKIP(cctx);
1230
1231 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1232 return FAIL;
1233 isn->isn_arg.checklen.cl_min_len = min_len;
1234 isn->isn_arg.checklen.cl_more_OK = more_OK;
1235
1236 return OK;
1237}
1238
1239/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 * Generate an ISN_STORE instruction.
1241 */
1242 static int
1243generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1244{
1245 isn_T *isn;
1246
Bram Moolenaar080457c2020-03-03 21:53:32 +01001247 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1249 return FAIL;
1250 if (name != NULL)
1251 isn->isn_arg.string = vim_strsave(name);
1252 else
1253 isn->isn_arg.number = idx;
1254
1255 return OK;
1256}
1257
1258/*
1259 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1260 */
1261 static int
1262generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1263{
1264 isn_T *isn;
1265
Bram Moolenaar080457c2020-03-03 21:53:32 +01001266 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1268 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001269 isn->isn_arg.storenr.stnr_idx = idx;
1270 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271
1272 return OK;
1273}
1274
1275/*
1276 * Generate an ISN_STOREOPT instruction
1277 */
1278 static int
1279generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1280{
1281 isn_T *isn;
1282
Bram Moolenaar080457c2020-03-03 21:53:32 +01001283 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1285 return FAIL;
1286 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1287 isn->isn_arg.storeopt.so_flags = opt_flags;
1288
1289 return OK;
1290}
1291
1292/*
1293 * Generate an ISN_LOAD or similar instruction.
1294 */
1295 static int
1296generate_LOAD(
1297 cctx_T *cctx,
1298 isntype_T isn_type,
1299 int idx,
1300 char_u *name,
1301 type_T *type)
1302{
1303 isn_T *isn;
1304
Bram Moolenaar080457c2020-03-03 21:53:32 +01001305 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1307 return FAIL;
1308 if (name != NULL)
1309 isn->isn_arg.string = vim_strsave(name);
1310 else
1311 isn->isn_arg.number = idx;
1312
1313 return OK;
1314}
1315
1316/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001317 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001318 */
1319 static int
1320generate_LOADV(
1321 cctx_T *cctx,
1322 char_u *name,
1323 int error)
1324{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001325 int di_flags;
1326 int vidx = find_vim_var(name, &di_flags);
1327 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001328
Bram Moolenaar080457c2020-03-03 21:53:32 +01001329 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001330 if (vidx < 0)
1331 {
1332 if (error)
1333 semsg(_(e_var_notfound), name);
1334 return FAIL;
1335 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001336 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001337
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001338 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001339}
1340
1341/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001342 * Generate an ISN_UNLET instruction.
1343 */
1344 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001345generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001346{
1347 isn_T *isn;
1348
1349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001350 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001351 return FAIL;
1352 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1353 isn->isn_arg.unlet.ul_forceit = forceit;
1354
1355 return OK;
1356}
1357
1358/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 * Generate an ISN_LOADS instruction.
1360 */
1361 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001362generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001364 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001366 int sid,
1367 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368{
1369 isn_T *isn;
1370
Bram Moolenaar080457c2020-03-03 21:53:32 +01001371 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001372 if (isn_type == ISN_LOADS)
1373 isn = generate_instr_type(cctx, isn_type, type);
1374 else
1375 isn = generate_instr_drop(cctx, isn_type, 1);
1376 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001378 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1379 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380
1381 return OK;
1382}
1383
1384/*
1385 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1386 */
1387 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001388generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 cctx_T *cctx,
1390 isntype_T isn_type,
1391 int sid,
1392 int idx,
1393 type_T *type)
1394{
1395 isn_T *isn;
1396
Bram Moolenaar080457c2020-03-03 21:53:32 +01001397 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 if (isn_type == ISN_LOADSCRIPT)
1399 isn = generate_instr_type(cctx, isn_type, type);
1400 else
1401 isn = generate_instr_drop(cctx, isn_type, 1);
1402 if (isn == NULL)
1403 return FAIL;
1404 isn->isn_arg.script.script_sid = sid;
1405 isn->isn_arg.script.script_idx = idx;
1406 return OK;
1407}
1408
1409/*
1410 * Generate an ISN_NEWLIST instruction.
1411 */
1412 static int
1413generate_NEWLIST(cctx_T *cctx, int count)
1414{
1415 isn_T *isn;
1416 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 type_T *type;
1418 type_T *member;
1419
Bram Moolenaar080457c2020-03-03 21:53:32 +01001420 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1422 return FAIL;
1423 isn->isn_arg.number = count;
1424
1425 // drop the value types
1426 stack->ga_len -= count;
1427
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001428 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001429 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 if (count > 0)
1431 member = ((type_T **)stack->ga_data)[stack->ga_len];
1432 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001433 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001434 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435
1436 // add the list type to the type stack
1437 if (ga_grow(stack, 1) == FAIL)
1438 return FAIL;
1439 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1440 ++stack->ga_len;
1441
1442 return OK;
1443}
1444
1445/*
1446 * Generate an ISN_NEWDICT instruction.
1447 */
1448 static int
1449generate_NEWDICT(cctx_T *cctx, int count)
1450{
1451 isn_T *isn;
1452 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 type_T *type;
1454 type_T *member;
1455
Bram Moolenaar080457c2020-03-03 21:53:32 +01001456 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1458 return FAIL;
1459 isn->isn_arg.number = count;
1460
1461 // drop the key and value types
1462 stack->ga_len -= 2 * count;
1463
Bram Moolenaar436472f2020-02-20 22:54:43 +01001464 // Use the first value type for the list member type. Use "void" for an
1465 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 if (count > 0)
1467 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1468 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001469 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001470 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471
1472 // add the dict type to the type stack
1473 if (ga_grow(stack, 1) == FAIL)
1474 return FAIL;
1475 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1476 ++stack->ga_len;
1477
1478 return OK;
1479}
1480
1481/*
1482 * Generate an ISN_FUNCREF instruction.
1483 */
1484 static int
1485generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1486{
1487 isn_T *isn;
1488 garray_T *stack = &cctx->ctx_type_stack;
1489
Bram Moolenaar080457c2020-03-03 21:53:32 +01001490 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1492 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001493 isn->isn_arg.funcref.fr_func = dfunc_idx;
1494 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495
1496 if (ga_grow(stack, 1) == FAIL)
1497 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001498 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 // TODO: argument and return types
1500 ++stack->ga_len;
1501
1502 return OK;
1503}
1504
1505/*
1506 * Generate an ISN_JUMP instruction.
1507 */
1508 static int
1509generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1510{
1511 isn_T *isn;
1512 garray_T *stack = &cctx->ctx_type_stack;
1513
Bram Moolenaar080457c2020-03-03 21:53:32 +01001514 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1516 return FAIL;
1517 isn->isn_arg.jump.jump_when = when;
1518 isn->isn_arg.jump.jump_where = where;
1519
1520 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1521 --stack->ga_len;
1522
1523 return OK;
1524}
1525
1526 static int
1527generate_FOR(cctx_T *cctx, int loop_idx)
1528{
1529 isn_T *isn;
1530 garray_T *stack = &cctx->ctx_type_stack;
1531
Bram Moolenaar080457c2020-03-03 21:53:32 +01001532 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1534 return FAIL;
1535 isn->isn_arg.forloop.for_idx = loop_idx;
1536
1537 if (ga_grow(stack, 1) == FAIL)
1538 return FAIL;
1539 // type doesn't matter, will be stored next
1540 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1541 ++stack->ga_len;
1542
1543 return OK;
1544}
1545
1546/*
1547 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001548 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 * Return FAIL if the number of arguments is wrong.
1550 */
1551 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001552generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553{
1554 isn_T *isn;
1555 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001556 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001557 type_T *argtypes[MAX_FUNC_ARGS];
1558 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559
Bram Moolenaar080457c2020-03-03 21:53:32 +01001560 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001561 argoff = check_internal_func(func_idx, argcount);
1562 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 return FAIL;
1564
Bram Moolenaar389df252020-07-09 21:20:47 +02001565 if (method_call && argoff > 1)
1566 {
1567 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1568 return FAIL;
1569 isn->isn_arg.shuffle.shfl_item = argcount;
1570 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1571 }
1572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1574 return FAIL;
1575 isn->isn_arg.bfunc.cbf_idx = func_idx;
1576 isn->isn_arg.bfunc.cbf_argcount = argcount;
1577
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001578 for (i = 0; i < argcount; ++i)
1579 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 stack->ga_len -= argcount; // drop the arguments
1582 if (ga_grow(stack, 1) == FAIL)
1583 return FAIL;
1584 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001585 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 ++stack->ga_len; // add return value
1587
1588 return OK;
1589}
1590
1591/*
1592 * Generate an ISN_DCALL or ISN_UCALL instruction.
1593 * Return FAIL if the number of arguments is wrong.
1594 */
1595 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001596generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597{
1598 isn_T *isn;
1599 garray_T *stack = &cctx->ctx_type_stack;
1600 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001601 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602
Bram Moolenaar080457c2020-03-03 21:53:32 +01001603 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 if (argcount > regular_args && !has_varargs(ufunc))
1605 {
1606 semsg(_(e_toomanyarg), ufunc->uf_name);
1607 return FAIL;
1608 }
1609 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1610 {
1611 semsg(_(e_toofewarg), ufunc->uf_name);
1612 return FAIL;
1613 }
1614
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001615 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001616 {
1617 int i;
1618
1619 for (i = 0; i < argcount; ++i)
1620 {
1621 type_T *expected;
1622 type_T *actual;
1623
1624 if (i < regular_args)
1625 {
1626 if (ufunc->uf_arg_types == NULL)
1627 continue;
1628 expected = ufunc->uf_arg_types[i];
1629 }
1630 else
1631 expected = ufunc->uf_va_type->tt_member;
1632 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001633 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001634 {
1635 arg_type_mismatch(expected, actual, i + 1);
1636 return FAIL;
1637 }
1638 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001639 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001640 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001641 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001642 }
1643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001645 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001646 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001648 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 {
1650 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1651 isn->isn_arg.dfunc.cdf_argcount = argcount;
1652 }
1653 else
1654 {
1655 // A user function may be deleted and redefined later, can't use the
1656 // ufunc pointer, need to look it up again at runtime.
1657 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1658 isn->isn_arg.ufunc.cuf_argcount = argcount;
1659 }
1660
1661 stack->ga_len -= argcount; // drop the arguments
1662 if (ga_grow(stack, 1) == FAIL)
1663 return FAIL;
1664 // add return value
1665 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1666 ++stack->ga_len;
1667
1668 return OK;
1669}
1670
1671/*
1672 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1673 */
1674 static int
1675generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1676{
1677 isn_T *isn;
1678 garray_T *stack = &cctx->ctx_type_stack;
1679
Bram Moolenaar080457c2020-03-03 21:53:32 +01001680 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1682 return FAIL;
1683 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1684 isn->isn_arg.ufunc.cuf_argcount = argcount;
1685
1686 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001687 if (ga_grow(stack, 1) == FAIL)
1688 return FAIL;
1689 // add return value
1690 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1691 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692
1693 return OK;
1694}
1695
1696/*
1697 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001698 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 */
1700 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001701generate_PCALL(
1702 cctx_T *cctx,
1703 int argcount,
1704 char_u *name,
1705 type_T *type,
1706 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707{
1708 isn_T *isn;
1709 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001710 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711
Bram Moolenaar080457c2020-03-03 21:53:32 +01001712 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001713
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001714 if (type->tt_type == VAR_ANY)
1715 ret_type = &t_any;
1716 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001717 {
1718 if (type->tt_argcount != -1)
1719 {
1720 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1721
1722 if (argcount < type->tt_min_argcount - varargs)
1723 {
1724 semsg(_(e_toofewarg), "[reference]");
1725 return FAIL;
1726 }
1727 if (!varargs && argcount > type->tt_argcount)
1728 {
1729 semsg(_(e_toomanyarg), "[reference]");
1730 return FAIL;
1731 }
1732 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001733 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001734 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001735 else
1736 {
1737 semsg(_("E1085: Not a callable type: %s"), name);
1738 return FAIL;
1739 }
1740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1742 return FAIL;
1743 isn->isn_arg.pfunc.cpf_top = at_top;
1744 isn->isn_arg.pfunc.cpf_argcount = argcount;
1745
1746 stack->ga_len -= argcount; // drop the arguments
1747
1748 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001749 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001751 // If partial is above the arguments it must be cleared and replaced with
1752 // the return value.
1753 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1754 return FAIL;
1755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 return OK;
1757}
1758
1759/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001760 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 */
1762 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001763generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764{
1765 isn_T *isn;
1766 garray_T *stack = &cctx->ctx_type_stack;
1767 type_T *type;
1768
Bram Moolenaar080457c2020-03-03 21:53:32 +01001769 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001770 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001772 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001774 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001776 if (type->tt_type != VAR_DICT && type != &t_any)
1777 {
1778 emsg(_(e_dictreq));
1779 return FAIL;
1780 }
1781 // change dict type to dict member type
1782 if (type->tt_type == VAR_DICT)
1783 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784
1785 return OK;
1786}
1787
1788/*
1789 * Generate an ISN_ECHO instruction.
1790 */
1791 static int
1792generate_ECHO(cctx_T *cctx, int with_white, int count)
1793{
1794 isn_T *isn;
1795
Bram Moolenaar080457c2020-03-03 21:53:32 +01001796 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1798 return FAIL;
1799 isn->isn_arg.echo.echo_with_white = with_white;
1800 isn->isn_arg.echo.echo_count = count;
1801
1802 return OK;
1803}
1804
Bram Moolenaarad39c092020-02-26 18:23:43 +01001805/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001806 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001807 */
1808 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001809generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001810{
1811 isn_T *isn;
1812
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001813 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001814 return FAIL;
1815 isn->isn_arg.number = count;
1816
1817 return OK;
1818}
1819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 static int
1821generate_EXEC(cctx_T *cctx, char_u *line)
1822{
1823 isn_T *isn;
1824
Bram Moolenaar080457c2020-03-03 21:53:32 +01001825 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1827 return FAIL;
1828 isn->isn_arg.string = vim_strsave(line);
1829 return OK;
1830}
1831
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001832 static int
1833generate_EXECCONCAT(cctx_T *cctx, int count)
1834{
1835 isn_T *isn;
1836
1837 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1838 return FAIL;
1839 isn->isn_arg.number = count;
1840 return OK;
1841}
1842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843/*
1844 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001845 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001847 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1849{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 lvar_T *lvar;
1851
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001852 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001854 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001855 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 }
1857
1858 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001859 return NULL;
1860 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001862 // Every local variable uses the next entry on the stack. We could re-use
1863 // the last ones when leaving a scope, but then variables used in a closure
1864 // might get overwritten. To keep things simple do not re-use stack
1865 // entries. This is less efficient, but memory is cheap these days.
1866 lvar->lv_idx = cctx->ctx_locals_count++;
1867
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001868 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 lvar->lv_const = isConst;
1870 lvar->lv_type = type;
1871
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001872 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873}
1874
1875/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001876 * Remove local variables above "new_top".
1877 */
1878 static void
1879unwind_locals(cctx_T *cctx, int new_top)
1880{
1881 if (cctx->ctx_locals.ga_len > new_top)
1882 {
1883 int idx;
1884 lvar_T *lvar;
1885
1886 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1887 {
1888 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1889 vim_free(lvar->lv_name);
1890 }
1891 }
1892 cctx->ctx_locals.ga_len = new_top;
1893}
1894
1895/*
1896 * Free all local variables.
1897 */
1898 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001899free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001900{
1901 unwind_locals(cctx, 0);
1902 ga_clear(&cctx->ctx_locals);
1903}
1904
1905/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 * Skip over a type definition and return a pointer to just after it.
1907 */
1908 char_u *
1909skip_type(char_u *start)
1910{
1911 char_u *p = start;
1912
1913 while (ASCII_ISALNUM(*p) || *p == '_')
1914 ++p;
1915
1916 // Skip over "<type>"; this is permissive about white space.
1917 if (*skipwhite(p) == '<')
1918 {
1919 p = skipwhite(p);
1920 p = skip_type(skipwhite(p + 1));
1921 p = skipwhite(p);
1922 if (*p == '>')
1923 ++p;
1924 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001925 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
1926 && STRNCMP("func", start, 4) == 0)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001927 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001928 if (*p == '(')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001929 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001930 // handle func(args): type
1931 ++p;
1932 while (*p != ')' && *p != NUL)
1933 {
1934 char_u *sp = p;
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001935
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001936 p = skip_type(p);
1937 if (p == sp)
1938 return p; // syntax error
1939 if (*p == ',')
1940 p = skipwhite(p + 1);
1941 }
1942 if (*p == ')')
1943 {
1944 if (p[1] == ':')
1945 p = skip_type(skipwhite(p + 2));
1946 else
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001947 ++p;
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001948 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001949 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001950 else
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001951 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001952 // handle func: return_type
1953 p = skip_type(skipwhite(p + 1));
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001954 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001955 }
1956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 return p;
1958}
1959
1960/*
1961 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001962 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 * Returns NULL in case of failure.
1964 */
1965 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001966parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967{
1968 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001969 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970
1971 if (**arg != '<')
1972 {
1973 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001974 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 else
1976 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001977 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 }
1979 *arg = skipwhite(*arg + 1);
1980
Bram Moolenaard77a8522020-04-03 21:59:57 +02001981 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982
1983 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001984 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 {
1986 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001987 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 }
1989 ++*arg;
1990
1991 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001992 return get_list_type(member_type, type_gap);
1993 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994}
1995
1996/*
1997 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001998 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 */
2000 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002001parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002{
2003 char_u *p = *arg;
2004 size_t len;
2005
2006 // skip over the first word
2007 while (ASCII_ISALNUM(*p) || *p == '_')
2008 ++p;
2009 len = p - *arg;
2010
2011 switch (**arg)
2012 {
2013 case 'a':
2014 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
2015 {
2016 *arg += len;
2017 return &t_any;
2018 }
2019 break;
2020 case 'b':
2021 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
2022 {
2023 *arg += len;
2024 return &t_bool;
2025 }
2026 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
2027 {
2028 *arg += len;
2029 return &t_blob;
2030 }
2031 break;
2032 case 'c':
2033 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
2034 {
2035 *arg += len;
2036 return &t_channel;
2037 }
2038 break;
2039 case 'd':
2040 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
2041 {
2042 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002043 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 }
2045 break;
2046 case 'f':
2047 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
2048 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01002049#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 *arg += len;
2051 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002052#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002053 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002054 return &t_any;
2055#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 }
2057 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2058 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002059 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002060 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002061 int argcount = -1;
2062 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002063 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002064 type_T *arg_type[MAX_FUNC_ARGS + 1];
2065
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002066 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002068 if (**arg == '(')
2069 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002070 // "func" may or may not return a value, "func()" does
2071 // not return a value.
2072 ret_type = &t_void;
2073
Bram Moolenaard77a8522020-04-03 21:59:57 +02002074 p = ++*arg;
2075 argcount = 0;
2076 while (*p != NUL && *p != ')')
2077 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002078 if (*p == '?')
2079 {
2080 if (first_optional == -1)
2081 first_optional = argcount;
2082 ++p;
2083 }
2084 else if (first_optional != -1)
2085 {
2086 emsg(_("E1007: mandatory argument after optional argument"));
2087 return &t_any;
2088 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002089 else if (STRNCMP(p, "...", 3) == 0)
2090 {
2091 flags |= TTFLAG_VARARGS;
2092 p += 3;
2093 }
2094
2095 arg_type[argcount++] = parse_type(&p, type_gap);
2096
2097 // Nothing comes after "...{type}".
2098 if (flags & TTFLAG_VARARGS)
2099 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002100
Bram Moolenaard77a8522020-04-03 21:59:57 +02002101 if (*p != ',' && *skipwhite(p) == ',')
2102 {
2103 semsg(_(e_no_white_before), ",");
2104 return &t_any;
2105 }
2106 if (*p == ',')
2107 {
2108 ++p;
2109 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002110 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002111 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002112 return &t_any;
2113 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002114 }
2115 p = skipwhite(p);
2116 if (argcount == MAX_FUNC_ARGS)
2117 {
2118 emsg(_("E740: Too many argument types"));
2119 return &t_any;
2120 }
2121 }
2122
2123 p = skipwhite(p);
2124 if (*p != ')')
2125 {
2126 emsg(_(e_missing_close));
2127 return &t_any;
2128 }
2129 *arg = p + 1;
2130 }
2131 if (**arg == ':')
2132 {
2133 // parse return type
2134 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002135 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002136 semsg(_(e_white_after), ":");
2137 *arg = skipwhite(*arg);
2138 ret_type = parse_type(arg, type_gap);
2139 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002140 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002141 type = get_func_type(ret_type, argcount, type_gap);
2142 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002143 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002144 type = alloc_func_type(ret_type, argcount, type_gap);
2145 type->tt_flags = flags;
2146 if (argcount > 0)
2147 {
2148 type->tt_argcount = argcount;
2149 type->tt_min_argcount = first_optional == -1
2150 ? argcount : first_optional;
2151 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002152 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002153 return &t_any;
2154 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002155 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002156 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002157 }
2158 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 }
2160 break;
2161 case 'j':
2162 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2163 {
2164 *arg += len;
2165 return &t_job;
2166 }
2167 break;
2168 case 'l':
2169 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2170 {
2171 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002172 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 }
2174 break;
2175 case 'n':
2176 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2177 {
2178 *arg += len;
2179 return &t_number;
2180 }
2181 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 case 's':
2183 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2184 {
2185 *arg += len;
2186 return &t_string;
2187 }
2188 break;
2189 case 'v':
2190 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2191 {
2192 *arg += len;
2193 return &t_void;
2194 }
2195 break;
2196 }
2197
2198 semsg(_("E1010: Type not recognized: %s"), *arg);
2199 return &t_any;
2200}
2201
2202/*
2203 * Check if "type1" and "type2" are exactly the same.
2204 */
2205 static int
2206equal_type(type_T *type1, type_T *type2)
2207{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002208 int i;
2209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 if (type1->tt_type != type2->tt_type)
2211 return FALSE;
2212 switch (type1->tt_type)
2213 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002215 case VAR_ANY:
2216 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 case VAR_SPECIAL:
2218 case VAR_BOOL:
2219 case VAR_NUMBER:
2220 case VAR_FLOAT:
2221 case VAR_STRING:
2222 case VAR_BLOB:
2223 case VAR_JOB:
2224 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002225 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 case VAR_LIST:
2227 case VAR_DICT:
2228 return equal_type(type1->tt_member, type2->tt_member);
2229 case VAR_FUNC:
2230 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002231 if (!equal_type(type1->tt_member, type2->tt_member)
2232 || type1->tt_argcount != type2->tt_argcount)
2233 return FALSE;
2234 if (type1->tt_argcount < 0
2235 || type1->tt_args == NULL || type2->tt_args == NULL)
2236 return TRUE;
2237 for (i = 0; i < type1->tt_argcount; ++i)
2238 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2239 return FALSE;
2240 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 }
2242 return TRUE;
2243}
2244
2245/*
2246 * Find the common type of "type1" and "type2" and put it in "dest".
2247 * "type2" and "dest" may be the same.
2248 */
2249 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002250common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251{
2252 if (equal_type(type1, type2))
2253 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002254 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 return;
2256 }
2257
2258 if (type1->tt_type == type2->tt_type)
2259 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2261 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002262 type_T *common;
2263
Bram Moolenaard77a8522020-04-03 21:59:57 +02002264 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002265 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002266 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002267 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002268 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 return;
2270 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002271 if (type1->tt_type == VAR_FUNC)
2272 {
2273 type_T *common;
2274
2275 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2276 if (type1->tt_argcount == type2->tt_argcount
2277 && type1->tt_argcount >= 0)
2278 {
2279 int argcount = type1->tt_argcount;
2280 int i;
2281
2282 *dest = alloc_func_type(common, argcount, type_gap);
2283 if (type1->tt_args != NULL && type2->tt_args != NULL)
2284 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002285 if (func_type_add_arg_types(*dest, argcount,
2286 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002287 for (i = 0; i < argcount; ++i)
2288 common_type(type1->tt_args[i], type2->tt_args[i],
2289 &(*dest)->tt_args[i], type_gap);
2290 }
2291 }
2292 else
2293 *dest = alloc_func_type(common, -1, type_gap);
2294 return;
2295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 }
2297
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002298 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299}
2300
2301 char *
2302vartype_name(vartype_T type)
2303{
2304 switch (type)
2305 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002306 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002307 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 case VAR_SPECIAL: return "special";
2310 case VAR_BOOL: return "bool";
2311 case VAR_NUMBER: return "number";
2312 case VAR_FLOAT: return "float";
2313 case VAR_STRING: return "string";
2314 case VAR_BLOB: return "blob";
2315 case VAR_JOB: return "job";
2316 case VAR_CHANNEL: return "channel";
2317 case VAR_LIST: return "list";
2318 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002319
2320 case VAR_FUNC:
2321 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002323 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324}
2325
2326/*
2327 * Return the name of a type.
2328 * The result may be in allocated memory, in which case "tofree" is set.
2329 */
2330 char *
2331type_name(type_T *type, char **tofree)
2332{
2333 char *name = vartype_name(type->tt_type);
2334
2335 *tofree = NULL;
2336 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2337 {
2338 char *member_free;
2339 char *member_name = type_name(type->tt_member, &member_free);
2340 size_t len;
2341
2342 len = STRLEN(name) + STRLEN(member_name) + 3;
2343 *tofree = alloc(len);
2344 if (*tofree != NULL)
2345 {
2346 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2347 vim_free(member_free);
2348 return *tofree;
2349 }
2350 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002351 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002352 {
2353 garray_T ga;
2354 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002355 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002356
2357 ga_init2(&ga, 1, 100);
2358 if (ga_grow(&ga, 20) == FAIL)
2359 return "[unknown]";
2360 *tofree = ga.ga_data;
2361 STRCPY(ga.ga_data, "func(");
2362 ga.ga_len += 5;
2363
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002364 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002365 {
2366 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002367 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002368 int len;
2369
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002370 if (type->tt_args == NULL)
2371 arg_type = "[unknown]";
2372 else
2373 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002374 if (i > 0)
2375 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002376 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002377 ga.ga_len += 2;
2378 }
2379 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002380 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002381 {
2382 vim_free(arg_free);
2383 return "[unknown]";
2384 }
2385 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002386 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002387 {
2388 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2389 ga.ga_len += 3;
2390 }
2391 else if (i >= type->tt_min_argcount)
2392 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002393 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002394 ga.ga_len += len;
2395 vim_free(arg_free);
2396 }
2397
2398 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002399 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002400 else
2401 {
2402 char *ret_free;
2403 char *ret_name = type_name(type->tt_member, &ret_free);
2404 int len;
2405
2406 len = (int)STRLEN(ret_name) + 4;
2407 if (ga_grow(&ga, len) == FAIL)
2408 {
2409 vim_free(ret_free);
2410 return "[unknown]";
2411 }
2412 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002413 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2414 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002415 vim_free(ret_free);
2416 }
2417 return ga.ga_data;
2418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419
2420 return name;
2421}
2422
2423/*
2424 * Find "name" in script-local items of script "sid".
2425 * Returns the index in "sn_var_vals" if found.
2426 * If found but not in "sn_var_vals" returns -1.
2427 * If not found returns -2.
2428 */
2429 int
2430get_script_item_idx(int sid, char_u *name, int check_writable)
2431{
2432 hashtab_T *ht;
2433 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002434 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 int idx;
2436
2437 // First look the name up in the hashtable.
2438 if (sid <= 0 || sid > script_items.ga_len)
2439 return -1;
2440 ht = &SCRIPT_VARS(sid);
2441 di = find_var_in_ht(ht, 0, name, TRUE);
2442 if (di == NULL)
2443 return -2;
2444
2445 // Now find the svar_T index in sn_var_vals.
2446 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2447 {
2448 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2449
2450 if (sv->sv_tv == &di->di_tv)
2451 {
2452 if (check_writable && sv->sv_const)
2453 semsg(_(e_readonlyvar), name);
2454 return idx;
2455 }
2456 }
2457 return -1;
2458}
2459
2460/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002461 * Find "name" in imported items of the current script or in "cctx" if not
2462 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 */
2464 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002465find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002467 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 int idx;
2469
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002470 if (current_sctx.sc_sid <= 0)
2471 return NULL;
2472 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 if (cctx != NULL)
2474 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2475 {
2476 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2477 + idx;
2478
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002479 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2480 : STRLEN(import->imp_name) == len
2481 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 return import;
2483 }
2484
2485 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2486 {
2487 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2488
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002489 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2490 : STRLEN(import->imp_name) == len
2491 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 return import;
2493 }
2494 return NULL;
2495}
2496
2497/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002498 * Free all imported variables.
2499 */
2500 static void
2501free_imported(cctx_T *cctx)
2502{
2503 int idx;
2504
2505 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2506 {
2507 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2508
2509 vim_free(import->imp_name);
2510 }
2511 ga_clear(&cctx->ctx_imports);
2512}
2513
2514/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002515 * Return TRUE if "p" points at a "#" but not at "#{".
2516 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002517 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002518vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002519{
2520 return p[0] == '#' && p[1] != '{';
2521}
2522
2523/*
2524 * Return a pointer to the next line that isn't empty or only contains a
2525 * comment. Skips over white space.
2526 * Returns NULL if there is none.
2527 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002528 char_u *
2529peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002530{
2531 int lnum = cctx->ctx_lnum;
2532
2533 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2534 {
2535 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002536 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002537
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002538 if (line == NULL)
2539 break;
2540 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002541 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002542 return p;
2543 }
2544 return NULL;
2545}
2546
2547/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002548 * Called when checking for a following operator at "arg". When the rest of
2549 * the line is empty or only a comment, peek the next line. If there is a next
2550 * line return a pointer to it and set "nextp".
2551 * Otherwise skip over white space.
2552 */
2553 static char_u *
2554may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2555{
2556 char_u *p = skipwhite(arg);
2557
2558 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002559 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002560 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002561 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002562 if (*nextp != NULL)
2563 return *nextp;
2564 }
2565 return p;
2566}
2567
2568/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002569 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002570 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002571 * Returns NULL when at the end.
2572 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002573 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002574next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002575{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002576 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002577
2578 do
2579 {
2580 ++cctx->ctx_lnum;
2581 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002582 {
2583 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002584 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002585 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002586 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002587 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002588 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002589 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002590 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002591 return line;
2592}
2593
2594/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002595 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002596 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002597 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2598 */
2599 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002600may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002601{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002602 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002603 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002604 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002605
2606 if (next == NULL)
2607 return FAIL;
2608 *arg = skipwhite(next);
2609 }
2610 return OK;
2611}
2612
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002613/*
2614 * Idem, and give an error when failed.
2615 */
2616 static int
2617may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2618{
2619 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2620 {
2621 emsg(_("E1097: line incomplete"));
2622 return FAIL;
2623 }
2624 return OK;
2625}
2626
2627
Bram Moolenaara5565e42020-05-09 15:44:01 +02002628// Structure passed between the compile_expr* functions to keep track of
2629// constants that have been parsed but for which no code was produced yet. If
2630// possible expressions on these constants are applied at compile time. If
2631// that is not possible, the code to push the constants needs to be generated
2632// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002633// Using 50 should be more than enough of 5 levels of ().
2634#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002635typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002636 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002637 int pp_used; // active entries in pp_tv[]
2638} ppconst_T;
2639
Bram Moolenaar1c747212020-05-09 18:28:34 +02002640static int compile_expr0(char_u **arg, cctx_T *cctx);
2641static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2642
Bram Moolenaara5565e42020-05-09 15:44:01 +02002643/*
2644 * Generate a PUSH instruction for "tv".
2645 * "tv" will be consumed or cleared.
2646 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2647 */
2648 static int
2649generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2650{
2651 if (tv != NULL)
2652 {
2653 switch (tv->v_type)
2654 {
2655 case VAR_UNKNOWN:
2656 break;
2657 case VAR_BOOL:
2658 generate_PUSHBOOL(cctx, tv->vval.v_number);
2659 break;
2660 case VAR_SPECIAL:
2661 generate_PUSHSPEC(cctx, tv->vval.v_number);
2662 break;
2663 case VAR_NUMBER:
2664 generate_PUSHNR(cctx, tv->vval.v_number);
2665 break;
2666#ifdef FEAT_FLOAT
2667 case VAR_FLOAT:
2668 generate_PUSHF(cctx, tv->vval.v_float);
2669 break;
2670#endif
2671 case VAR_BLOB:
2672 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2673 tv->vval.v_blob = NULL;
2674 break;
2675 case VAR_STRING:
2676 generate_PUSHS(cctx, tv->vval.v_string);
2677 tv->vval.v_string = NULL;
2678 break;
2679 default:
2680 iemsg("constant type not supported");
2681 clear_tv(tv);
2682 return FAIL;
2683 }
2684 tv->v_type = VAR_UNKNOWN;
2685 }
2686 return OK;
2687}
2688
2689/*
2690 * Generate code for any ppconst entries.
2691 */
2692 static int
2693generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2694{
2695 int i;
2696 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002697 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002698
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002699 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002700 for (i = 0; i < ppconst->pp_used; ++i)
2701 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2702 ret = FAIL;
2703 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002704 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002705 return ret;
2706}
2707
2708/*
2709 * Clear ppconst constants. Used when failing.
2710 */
2711 static void
2712clear_ppconst(ppconst_T *ppconst)
2713{
2714 int i;
2715
2716 for (i = 0; i < ppconst->pp_used; ++i)
2717 clear_tv(&ppconst->pp_tv[i]);
2718 ppconst->pp_used = 0;
2719}
2720
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002721/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002722 * Generate an instruction to load script-local variable "name", without the
2723 * leading "s:".
2724 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 */
2726 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002727compile_load_scriptvar(
2728 cctx_T *cctx,
2729 char_u *name, // variable NUL terminated
2730 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002731 char_u **end, // end of variable
2732 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002734 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2736 imported_T *import;
2737
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002738 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002740 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002741 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2742 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 }
2744 if (idx >= 0)
2745 {
2746 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2747
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002748 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 current_sctx.sc_sid, idx, sv->sv_type);
2750 return OK;
2751 }
2752
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002753 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 if (import != NULL)
2755 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002756 if (import->imp_all)
2757 {
2758 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002759 char_u *exp_name;
2760 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002761 ufunc_T *ufunc;
2762 type_T *type;
2763
2764 // Used "import * as Name", need to lookup the member.
2765 if (*p != '.')
2766 {
2767 semsg(_("E1060: expected dot after name: %s"), start);
2768 return FAIL;
2769 }
2770 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002771 if (VIM_ISWHITE(*p))
2772 {
2773 emsg(_("E1074: no white space allowed after dot"));
2774 return FAIL;
2775 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002776
Bram Moolenaar1c991142020-07-04 13:15:31 +02002777 // isolate one name
2778 exp_name = p;
2779 while (eval_isnamec(*p))
2780 ++p;
2781 cc = *p;
2782 *p = NUL;
2783
2784 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2785 *p = cc;
2786 p = skipwhite(p);
2787
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002788 // TODO: what if it is a function?
2789 if (idx < 0)
2790 return FAIL;
2791 *end = p;
2792
2793 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2794 import->imp_sid,
2795 idx,
2796 type);
2797 }
2798 else
2799 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002800 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002801 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2802 import->imp_sid,
2803 import->imp_var_vals_idx,
2804 import->imp_type);
2805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 return OK;
2807 }
2808
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002809 if (error)
2810 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 return FAIL;
2812}
2813
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002814 static int
2815generate_funcref(cctx_T *cctx, char_u *name)
2816{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002817 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002818
2819 if (ufunc == NULL)
2820 return FAIL;
2821
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002822 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2823 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002824}
2825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826/*
2827 * Compile a variable name into a load instruction.
2828 * "end" points to just after the name.
2829 * When "error" is FALSE do not give an error when not found.
2830 */
2831 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002832compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833{
2834 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002835 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002836 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002838 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839
2840 if (*(*arg + 1) == ':')
2841 {
2842 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002843 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002844 {
2845 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002847 switch (**arg)
2848 {
2849 case 'g': isn_type = ISN_LOADGDICT; break;
2850 case 'w': isn_type = ISN_LOADWDICT; break;
2851 case 't': isn_type = ISN_LOADTDICT; break;
2852 case 'b': isn_type = ISN_LOADBDICT; break;
2853 default:
2854 semsg(_(e_namespace), *arg);
2855 goto theend;
2856 }
2857 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2858 goto theend;
2859 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002860 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 else
2862 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002863 isntype_T isn_type = ISN_DROP;
2864
2865 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2866 if (name == NULL)
2867 return FAIL;
2868
2869 switch (**arg)
2870 {
2871 case 'v': res = generate_LOADV(cctx, name, error);
2872 break;
2873 case 's': res = compile_load_scriptvar(cctx, name,
2874 NULL, NULL, error);
2875 break;
2876 case 'g': isn_type = ISN_LOADG; break;
2877 case 'w': isn_type = ISN_LOADW; break;
2878 case 't': isn_type = ISN_LOADT; break;
2879 case 'b': isn_type = ISN_LOADB; break;
2880 default: semsg(_(e_namespace), *arg);
2881 goto theend;
2882 }
2883 if (isn_type != ISN_DROP)
2884 {
2885 // Global, Buffer-local, Window-local and Tabpage-local
2886 // variables can be defined later, thus we don't check if it
2887 // exists, give error at runtime.
2888 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2889 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 }
2891 }
2892 else
2893 {
2894 size_t len = end - *arg;
2895 int idx;
2896 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002897 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898
2899 name = vim_strnsave(*arg, end - *arg);
2900 if (name == NULL)
2901 return FAIL;
2902
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002903 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002905 if (!gen_load_outer)
2906 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 }
2908 else
2909 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002910 lvar_T *lvar = lookup_local(*arg, len, cctx);
2911
2912 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002914 type = lvar->lv_type;
2915 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002916 if (lvar->lv_from_outer)
2917 gen_load_outer = TRUE;
2918 else
2919 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 }
2921 else
2922 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002923 // "var" can be script-local even without using "s:" if it
2924 // already exists.
2925 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2926 == SCRIPT_VERSION_VIM9
2927 || lookup_script(*arg, len) == OK)
2928 res = compile_load_scriptvar(cctx, name, *arg, &end,
2929 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002930
Bram Moolenaara5565e42020-05-09 15:44:01 +02002931 // When the name starts with an uppercase letter or "x:" it
2932 // can be a user defined function.
2933 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2934 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 }
2936 }
2937 if (gen_load)
2938 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002939 if (gen_load_outer)
2940 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 }
2942
2943 *arg = end;
2944
2945theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002946 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 semsg(_(e_var_notfound), name);
2948 vim_free(name);
2949 return res;
2950}
2951
2952/*
2953 * Compile the argument expressions.
2954 * "arg" points to just after the "(" and is advanced to after the ")"
2955 */
2956 static int
2957compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2958{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002959 char_u *p = *arg;
2960 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961
Bram Moolenaare6085c52020-04-12 20:19:16 +02002962 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002964 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2965 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002966 if (*p == ')')
2967 {
2968 *arg = p + 1;
2969 return OK;
2970 }
2971
Bram Moolenaara5565e42020-05-09 15:44:01 +02002972 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 return FAIL;
2974 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002975
2976 if (*p != ',' && *skipwhite(p) == ',')
2977 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002978 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002979 p = skipwhite(p);
2980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002982 {
2983 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002984 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002985 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002986 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002987 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002988 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002990failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002991 emsg(_(e_missing_close));
2992 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993}
2994
2995/*
2996 * Compile a function call: name(arg1, arg2)
2997 * "arg" points to "name", "arg + varlen" to the "(".
2998 * "argcount_init" is 1 for "value->method()"
2999 * Instructions:
3000 * EVAL arg1
3001 * EVAL arg2
3002 * BCALL / DCALL / UCALL
3003 */
3004 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003005compile_call(
3006 char_u **arg,
3007 size_t varlen,
3008 cctx_T *cctx,
3009 ppconst_T *ppconst,
3010 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011{
3012 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003013 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 int argcount = argcount_init;
3015 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003016 char_u fname_buf[FLEN_FIXED + 1];
3017 char_u *tofree = NULL;
3018 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003020 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021
Bram Moolenaara5565e42020-05-09 15:44:01 +02003022 // we can evaluate "has('name')" at compile time
3023 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3024 {
3025 char_u *s = skipwhite(*arg + varlen + 1);
3026 typval_T argvars[2];
3027
3028 argvars[0].v_type = VAR_UNKNOWN;
3029 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003030 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003031 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003032 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003033 s = skipwhite(s);
3034 if (*s == ')' && argvars[0].v_type == VAR_STRING)
3035 {
3036 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3037
3038 *arg = s + 1;
3039 argvars[1].v_type = VAR_UNKNOWN;
3040 tv->v_type = VAR_NUMBER;
3041 tv->vval.v_number = 0;
3042 f_has(argvars, tv);
3043 clear_tv(&argvars[0]);
3044 ++ppconst->pp_used;
3045 return OK;
3046 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003047 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003048 }
3049
3050 if (generate_ppconst(cctx, ppconst) == FAIL)
3051 return FAIL;
3052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 if (varlen >= sizeof(namebuf))
3054 {
3055 semsg(_("E1011: name too long: %s"), name);
3056 return FAIL;
3057 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003058 vim_strncpy(namebuf, *arg, varlen);
3059 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060
3061 *arg = skipwhite(*arg + varlen + 1);
3062 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003063 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003065 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 {
3067 int idx;
3068
3069 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003070 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003072 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003073 else
3074 semsg(_(e_unknownfunc), namebuf);
3075 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 }
3077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003079 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003081 {
3082 res = generate_CALL(cctx, ufunc, argcount);
3083 goto theend;
3084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085
3086 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003087 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003089 if (STRNCMP(namebuf, "g:", 2) != 0
3090 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003091 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003092 garray_T *stack = &cctx->ctx_type_stack;
3093 type_T *type;
3094
3095 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3096 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003097 goto theend;
3098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003100 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003101 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003102 if (STRNCMP(namebuf, "g:", 2) == 0)
3103 res = generate_UCALL(cctx, name, argcount);
3104 else
3105 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003106
3107theend:
3108 vim_free(tofree);
3109 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110}
3111
3112// like NAMESPACE_CHAR but with 'a' and 'l'.
3113#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3114
3115/*
3116 * Find the end of a variable or function name. Unlike find_name_end() this
3117 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003118 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 * Return a pointer to just after the name. Equal to "arg" if there is no
3120 * valid name.
3121 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003122 static char_u *
3123to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124{
3125 char_u *p;
3126
3127 // Quick check for valid starting character.
3128 if (!eval_isnamec1(*arg))
3129 return arg;
3130
3131 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3132 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3133 // and can be used in slice "[n:]".
3134 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003135 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3137 break;
3138 return p;
3139}
3140
3141/*
3142 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003143 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 */
3145 char_u *
3146to_name_const_end(char_u *arg)
3147{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003148 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 typval_T rettv;
3150
3151 if (p == arg && *arg == '[')
3152 {
3153
3154 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003155 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 p = arg;
3157 }
3158 else if (p == arg && *arg == '#' && arg[1] == '{')
3159 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003160 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003162 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 p = arg;
3164 }
3165 else if (p == arg && *arg == '{')
3166 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003167 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003169 // Can be "{x -> ret}()".
3170 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003172 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 if (ret != OK)
3174 p = arg;
3175 }
3176
3177 return p;
3178}
3179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180/*
3181 * parse a list: [expr, expr]
3182 * "*arg" points to the '['.
3183 */
3184 static int
3185compile_list(char_u **arg, cctx_T *cctx)
3186{
3187 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003188 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 int count = 0;
3190
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003191 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003193 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003194 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003195 semsg(_(e_list_end), *arg);
3196 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003197 }
3198 if (*p == ']')
3199 {
3200 ++p;
3201 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003202 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003203 p += STRLEN(p);
3204 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003205 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003206 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 break;
3208 ++count;
3209 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003210 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003212 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3213 {
3214 semsg(_(e_white_after), ",");
3215 return FAIL;
3216 }
3217 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003218 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 p = skipwhite(p);
3220 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003221 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222
3223 generate_NEWLIST(cctx, count);
3224 return OK;
3225}
3226
3227/*
3228 * parse a lambda: {arg, arg -> expr}
3229 * "*arg" points to the '{'.
3230 */
3231 static int
3232compile_lambda(char_u **arg, cctx_T *cctx)
3233{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 typval_T rettv;
3235 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003236 evalarg_T evalarg;
3237
3238 CLEAR_FIELD(evalarg);
3239 evalarg.eval_flags = EVAL_EVALUATE;
3240 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241
3242 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003243 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003247 ++ufunc->uf_refcount;
3248 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003249 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250
3251 // The function will have one line: "return {expr}".
3252 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003253 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003255 clear_evalarg(&evalarg, NULL);
3256
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003257 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003258 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003259
3260 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 return FAIL;
3262}
3263
3264/*
3265 * Compile a lamda call: expr->{lambda}(args)
3266 * "arg" points to the "{".
3267 */
3268 static int
3269compile_lambda_call(char_u **arg, cctx_T *cctx)
3270{
3271 ufunc_T *ufunc;
3272 typval_T rettv;
3273 int argcount = 1;
3274 int ret = FAIL;
3275
3276 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003277 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 return FAIL;
3279
3280 if (**arg != '(')
3281 {
3282 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003283 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 else
3285 semsg(_(e_missing_paren), "lambda");
3286 clear_tv(&rettv);
3287 return FAIL;
3288 }
3289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 ufunc = rettv.vval.v_partial->pt_func;
3291 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003292 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003293 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003294
3295 // The function will have one line: "return {expr}".
3296 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003297 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298
3299 // compile the arguments
3300 *arg = skipwhite(*arg + 1);
3301 if (compile_arguments(arg, cctx, &argcount) == OK)
3302 // call the compiled function
3303 ret = generate_CALL(cctx, ufunc, argcount);
3304
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003305 if (ret == FAIL)
3306 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 return ret;
3308}
3309
3310/*
3311 * parse a dict: {'key': val} or #{key: val}
3312 * "*arg" points to the '{'.
3313 */
3314 static int
3315compile_dict(char_u **arg, cctx_T *cctx, int literal)
3316{
3317 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003318 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 int count = 0;
3320 dict_T *d = dict_alloc();
3321 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003322 char_u *whitep = *arg;
3323 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324
3325 if (d == NULL)
3326 return FAIL;
3327 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003328 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 {
3330 char_u *key = NULL;
3331
Bram Moolenaar23c55272020-06-21 16:58:13 +02003332 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003333 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003334 *arg = NULL;
3335 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003336 }
3337
3338 if (**arg == '}')
3339 break;
3340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 if (literal)
3342 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003343 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344
Bram Moolenaar2c330432020-04-13 14:41:35 +02003345 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 {
3347 semsg(_("E1014: Invalid key: %s"), *arg);
3348 return FAIL;
3349 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003350 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 if (generate_PUSHS(cctx, key) == FAIL)
3352 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003353 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 }
3355 else
3356 {
3357 isn_T *isn;
3358
Bram Moolenaara5565e42020-05-09 15:44:01 +02003359 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3362 if (isn->isn_type == ISN_PUSHS)
3363 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003364 else
3365 {
3366 type_T *keytype = ((type_T **)stack->ga_data)
3367 [stack->ga_len - 1];
3368 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3369 return FAIL;
3370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 }
3372
3373 // Check for duplicate keys, if using string keys.
3374 if (key != NULL)
3375 {
3376 item = dict_find(d, key, -1);
3377 if (item != NULL)
3378 {
3379 semsg(_(e_duplicate_key), key);
3380 goto failret;
3381 }
3382 item = dictitem_alloc(key);
3383 if (item != NULL)
3384 {
3385 item->di_tv.v_type = VAR_UNKNOWN;
3386 item->di_tv.v_lock = 0;
3387 if (dict_add(d, item) == FAIL)
3388 dictitem_free(item);
3389 }
3390 }
3391
3392 *arg = skipwhite(*arg);
3393 if (**arg != ':')
3394 {
3395 semsg(_(e_missing_dict_colon), *arg);
3396 return FAIL;
3397 }
3398
Bram Moolenaar2c330432020-04-13 14:41:35 +02003399 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003401 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003402 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003403 *arg = NULL;
3404 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003405 }
3406
Bram Moolenaara5565e42020-05-09 15:44:01 +02003407 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 return FAIL;
3409 ++count;
3410
Bram Moolenaar2c330432020-04-13 14:41:35 +02003411 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003412 *arg = skipwhite(*arg);
3413 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003414 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003415 *arg = NULL;
3416 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003417 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 if (**arg == '}')
3419 break;
3420 if (**arg != ',')
3421 {
3422 semsg(_(e_missing_dict_comma), *arg);
3423 goto failret;
3424 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003425 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 *arg = skipwhite(*arg + 1);
3427 }
3428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 *arg = *arg + 1;
3430
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003431 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003432 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003433 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003434 *arg += STRLEN(*arg);
3435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 dict_unref(d);
3437 return generate_NEWDICT(cctx, count);
3438
3439failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003440 if (*arg == NULL)
3441 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 dict_unref(d);
3443 return FAIL;
3444}
3445
3446/*
3447 * Compile "&option".
3448 */
3449 static int
3450compile_get_option(char_u **arg, cctx_T *cctx)
3451{
3452 typval_T rettv;
3453 char_u *start = *arg;
3454 int ret;
3455
3456 // parse the option and get the current value to get the type.
3457 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003458 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 if (ret == OK)
3460 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003461 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 char_u *name = vim_strnsave(start, *arg - start);
3463 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3464
3465 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3466 vim_free(name);
3467 }
3468 clear_tv(&rettv);
3469
3470 return ret;
3471}
3472
3473/*
3474 * Compile "$VAR".
3475 */
3476 static int
3477compile_get_env(char_u **arg, cctx_T *cctx)
3478{
3479 char_u *start = *arg;
3480 int len;
3481 int ret;
3482 char_u *name;
3483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 ++*arg;
3485 len = get_env_len(arg);
3486 if (len == 0)
3487 {
3488 semsg(_(e_syntax_at), start - 1);
3489 return FAIL;
3490 }
3491
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003492 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 name = vim_strnsave(start, len + 1);
3494 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3495 vim_free(name);
3496 return ret;
3497}
3498
3499/*
3500 * Compile "@r".
3501 */
3502 static int
3503compile_get_register(char_u **arg, cctx_T *cctx)
3504{
3505 int ret;
3506
3507 ++*arg;
3508 if (**arg == NUL)
3509 {
3510 semsg(_(e_syntax_at), *arg - 1);
3511 return FAIL;
3512 }
3513 if (!valid_yank_reg(**arg, TRUE))
3514 {
3515 emsg_invreg(**arg);
3516 return FAIL;
3517 }
3518 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3519 ++*arg;
3520 return ret;
3521}
3522
3523/*
3524 * Apply leading '!', '-' and '+' to constant "rettv".
3525 */
3526 static int
3527apply_leader(typval_T *rettv, char_u *start, char_u *end)
3528{
3529 char_u *p = end;
3530
3531 // this works from end to start
3532 while (p > start)
3533 {
3534 --p;
3535 if (*p == '-' || *p == '+')
3536 {
3537 // only '-' has an effect, for '+' we only check the type
3538#ifdef FEAT_FLOAT
3539 if (rettv->v_type == VAR_FLOAT)
3540 {
3541 if (*p == '-')
3542 rettv->vval.v_float = -rettv->vval.v_float;
3543 }
3544 else
3545#endif
3546 {
3547 varnumber_T val;
3548 int error = FALSE;
3549
3550 // tv_get_number_chk() accepts a string, but we don't want that
3551 // here
3552 if (check_not_string(rettv) == FAIL)
3553 return FAIL;
3554 val = tv_get_number_chk(rettv, &error);
3555 clear_tv(rettv);
3556 if (error)
3557 return FAIL;
3558 if (*p == '-')
3559 val = -val;
3560 rettv->v_type = VAR_NUMBER;
3561 rettv->vval.v_number = val;
3562 }
3563 }
3564 else
3565 {
3566 int v = tv2bool(rettv);
3567
3568 // '!' is permissive in the type.
3569 clear_tv(rettv);
3570 rettv->v_type = VAR_BOOL;
3571 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3572 }
3573 }
3574 return OK;
3575}
3576
3577/*
3578 * Recognize v: variables that are constants and set "rettv".
3579 */
3580 static void
3581get_vim_constant(char_u **arg, typval_T *rettv)
3582{
3583 if (STRNCMP(*arg, "v:true", 6) == 0)
3584 {
3585 rettv->v_type = VAR_BOOL;
3586 rettv->vval.v_number = VVAL_TRUE;
3587 *arg += 6;
3588 }
3589 else if (STRNCMP(*arg, "v:false", 7) == 0)
3590 {
3591 rettv->v_type = VAR_BOOL;
3592 rettv->vval.v_number = VVAL_FALSE;
3593 *arg += 7;
3594 }
3595 else if (STRNCMP(*arg, "v:null", 6) == 0)
3596 {
3597 rettv->v_type = VAR_SPECIAL;
3598 rettv->vval.v_number = VVAL_NULL;
3599 *arg += 6;
3600 }
3601 else if (STRNCMP(*arg, "v:none", 6) == 0)
3602 {
3603 rettv->v_type = VAR_SPECIAL;
3604 rettv->vval.v_number = VVAL_NONE;
3605 *arg += 6;
3606 }
3607}
3608
Bram Moolenaar61a89812020-05-07 16:58:17 +02003609 static exptype_T
3610get_compare_type(char_u *p, int *len, int *type_is)
3611{
3612 exptype_T type = EXPR_UNKNOWN;
3613 int i;
3614
3615 switch (p[0])
3616 {
3617 case '=': if (p[1] == '=')
3618 type = EXPR_EQUAL;
3619 else if (p[1] == '~')
3620 type = EXPR_MATCH;
3621 break;
3622 case '!': if (p[1] == '=')
3623 type = EXPR_NEQUAL;
3624 else if (p[1] == '~')
3625 type = EXPR_NOMATCH;
3626 break;
3627 case '>': if (p[1] != '=')
3628 {
3629 type = EXPR_GREATER;
3630 *len = 1;
3631 }
3632 else
3633 type = EXPR_GEQUAL;
3634 break;
3635 case '<': if (p[1] != '=')
3636 {
3637 type = EXPR_SMALLER;
3638 *len = 1;
3639 }
3640 else
3641 type = EXPR_SEQUAL;
3642 break;
3643 case 'i': if (p[1] == 's')
3644 {
3645 // "is" and "isnot"; but not a prefix of a name
3646 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3647 *len = 5;
3648 i = p[*len];
3649 if (!isalnum(i) && i != '_')
3650 {
3651 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3652 *type_is = TRUE;
3653 }
3654 }
3655 break;
3656 }
3657 return type;
3658}
3659
3660/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 * Compile code to apply '-', '+' and '!'.
3662 */
3663 static int
3664compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3665{
3666 char_u *p = end;
3667
3668 // this works from end to start
3669 while (p > start)
3670 {
3671 --p;
3672 if (*p == '-' || *p == '+')
3673 {
3674 int negate = *p == '-';
3675 isn_T *isn;
3676
3677 // TODO: check type
3678 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3679 {
3680 --p;
3681 if (*p == '-')
3682 negate = !negate;
3683 }
3684 // only '-' has an effect, for '+' we only check the type
3685 if (negate)
3686 isn = generate_instr(cctx, ISN_NEGATENR);
3687 else
3688 isn = generate_instr(cctx, ISN_CHECKNR);
3689 if (isn == NULL)
3690 return FAIL;
3691 }
3692 else
3693 {
3694 int invert = TRUE;
3695
3696 while (p > start && p[-1] == '!')
3697 {
3698 --p;
3699 invert = !invert;
3700 }
3701 if (generate_2BOOL(cctx, invert) == FAIL)
3702 return FAIL;
3703 }
3704 }
3705 return OK;
3706}
3707
3708/*
3709 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003710 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 */
3712 static int
3713compile_subscript(
3714 char_u **arg,
3715 cctx_T *cctx,
3716 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003717 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003718 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719{
3720 for (;;)
3721 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003722 char_u *p = skipwhite(*arg);
3723
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003724 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003725 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003726 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003727
3728 // If a following line starts with "->{" or "->X" advance to that
3729 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003730 // Also if a following line starts with ".x".
3731 if (next != NULL &&
3732 ((next[0] == '-' && next[1] == '>'
3733 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3734 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003735 {
3736 next = next_line_from_context(cctx, TRUE);
3737 if (next == NULL)
3738 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003739 *arg = next;
3740 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003741 }
3742 }
3743
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003744 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003746 garray_T *stack = &cctx->ctx_type_stack;
3747 type_T *type;
3748 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003750 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003751 return FAIL;
3752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003754 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3755
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003756 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3758 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003759 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 return FAIL;
3761 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003762 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003764 char_u *pstart = p;
3765
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003766 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003767 return FAIL;
3768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 // something->method()
3770 // Apply the '!', '-' and '+' first:
3771 // -1.0->func() works like (-1.0)->func()
3772 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3773 return FAIL;
3774 *start_leader = end_leader; // don't apply again later
3775
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003776 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003777 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003778 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 if (**arg == '{')
3780 {
3781 // lambda call: list->{lambda}
3782 if (compile_lambda_call(arg, cctx) == FAIL)
3783 return FAIL;
3784 }
3785 else
3786 {
3787 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003788 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003789 if (!eval_isnamec1(*p))
3790 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003791 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003792 return FAIL;
3793 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003794 if (ASCII_ISALPHA(*p) && p[1] == ':')
3795 p += 2;
3796 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 ;
3798 if (*p != '(')
3799 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003800 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 return FAIL;
3802 }
3803 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003804 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 return FAIL;
3806 }
3807 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003808 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003810 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003811 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003812 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003813
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003814 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003815 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003816 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003817 // TODO: blob index
3818 // TODO: more arguments
3819 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003820 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003821 return FAIL;
3822
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003823 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003824 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003825 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003826 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003827 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 return FAIL;
3829
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003830 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3831 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 if (**arg != ']')
3833 {
3834 emsg(_(e_missbrac));
3835 return FAIL;
3836 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003837 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003839 // We can index a list and a dict. If we don't know the type
3840 // we can use the index value type.
3841 // TODO: If we don't know use an instruction to figure it out at
3842 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003843 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003844 vtype = (*typep)->tt_type;
3845 if (*typep == &t_any)
3846 {
3847 type_T *valtype = ((type_T **)stack->ga_data)
3848 [stack->ga_len - 1];
3849 if (valtype == &t_string)
3850 vtype = VAR_DICT;
3851 }
3852 if (vtype == VAR_DICT)
3853 {
3854 if ((*typep)->tt_type == VAR_DICT)
3855 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003856 else
3857 {
3858 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3859 return FAIL;
3860 *typep = &t_any;
3861 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003862 if (may_generate_2STRING(-1, cctx) == FAIL)
3863 return FAIL;
3864 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3865 return FAIL;
3866 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003867 else if (vtype == VAR_STRING)
3868 {
3869 *typep = &t_number;
3870 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3871 return FAIL;
3872 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003873 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003874 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003875 if ((*typep)->tt_type == VAR_LIST)
3876 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003877 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003878 return FAIL;
3879 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003880 else
3881 {
3882 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003883 return FAIL;
3884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003886 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003888 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003889 return FAIL;
3890
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003891 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003892 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3893 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003895 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 if (eval_isnamec1(*p))
3897 while (eval_isnamec(*p))
3898 MB_PTR_ADV(p);
3899 if (p == *arg)
3900 {
3901 semsg(_(e_syntax_at), *arg);
3902 return FAIL;
3903 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003904 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 return FAIL;
3906 *arg = p;
3907 }
3908 else
3909 break;
3910 }
3911
3912 // TODO - see handle_subscript():
3913 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3914 // Don't do this when "Func" is already a partial that was bound
3915 // explicitly (pt_auto is FALSE).
3916
3917 return OK;
3918}
3919
3920/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003921 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3922 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003924 * If the value is a constant "ppconst->pp_ret" will be set.
3925 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003926 *
3927 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 */
3929
3930/*
3931 * number number constant
3932 * 0zFFFFFFFF Blob constant
3933 * "string" string constant
3934 * 'string' literal string constant
3935 * &option-name option value
3936 * @r register contents
3937 * identifier variable value
3938 * function() function call
3939 * $VAR environment variable
3940 * (expression) nested expression
3941 * [expr, expr] List
3942 * {key: val, key: val} Dictionary
3943 * #{key: val, key: val} Dictionary with literal keys
3944 *
3945 * Also handle:
3946 * ! in front logical NOT
3947 * - in front unary minus
3948 * + in front unary plus (ignored)
3949 * trailing (arg) funcref/partial call
3950 * trailing [] subscript in String or List
3951 * trailing .name entry in Dictionary
3952 * trailing ->name() method call
3953 */
3954 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003955compile_expr7(
3956 char_u **arg,
3957 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003958 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 char_u *start_leader, *end_leader;
3961 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003962 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003963 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964
3965 /*
3966 * Skip '!', '-' and '+' characters. They are handled later.
3967 */
3968 start_leader = *arg;
3969 while (**arg == '!' || **arg == '-' || **arg == '+')
3970 *arg = skipwhite(*arg + 1);
3971 end_leader = *arg;
3972
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003973 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 switch (**arg)
3975 {
3976 /*
3977 * Number constant.
3978 */
3979 case '0': // also for blob starting with 0z
3980 case '1':
3981 case '2':
3982 case '3':
3983 case '4':
3984 case '5':
3985 case '6':
3986 case '7':
3987 case '8':
3988 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003989 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 return FAIL;
3991 break;
3992
3993 /*
3994 * String constant: "string".
3995 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003996 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997 return FAIL;
3998 break;
3999
4000 /*
4001 * Literal string constant: 'str''ing'.
4002 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004003 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 return FAIL;
4005 break;
4006
4007 /*
4008 * Constant Vim variable.
4009 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004010 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 ret = NOTDONE;
4012 break;
4013
4014 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004015 * "true" constant
4016 */
4017 case 't': if (STRNCMP(*arg, "true", 4) == 0
4018 && !eval_isnamec((*arg)[4]))
4019 {
4020 *arg += 4;
4021 rettv->v_type = VAR_BOOL;
4022 rettv->vval.v_number = VVAL_TRUE;
4023 }
4024 else
4025 ret = NOTDONE;
4026 break;
4027
4028 /*
4029 * "false" constant
4030 */
4031 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4032 && !eval_isnamec((*arg)[5]))
4033 {
4034 *arg += 5;
4035 rettv->v_type = VAR_BOOL;
4036 rettv->vval.v_number = VVAL_FALSE;
4037 }
4038 else
4039 ret = NOTDONE;
4040 break;
4041
4042 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 * List: [expr, expr]
4044 */
4045 case '[': ret = compile_list(arg, cctx);
4046 break;
4047
4048 /*
4049 * Dictionary: #{key: val, key: val}
4050 */
4051 case '#': if ((*arg)[1] == '{')
4052 {
4053 ++*arg;
4054 ret = compile_dict(arg, cctx, TRUE);
4055 }
4056 else
4057 ret = NOTDONE;
4058 break;
4059
4060 /*
4061 * Lambda: {arg, arg -> expr}
4062 * Dictionary: {'key': val, 'key': val}
4063 */
4064 case '{': {
4065 char_u *start = skipwhite(*arg + 1);
4066
4067 // Find out what comes after the arguments.
4068 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004069 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 if (ret != FAIL && *start == '>')
4071 ret = compile_lambda(arg, cctx);
4072 else
4073 ret = compile_dict(arg, cctx, FALSE);
4074 }
4075 break;
4076
4077 /*
4078 * Option value: &name
4079 */
4080 case '&': ret = compile_get_option(arg, cctx);
4081 break;
4082
4083 /*
4084 * Environment variable: $VAR.
4085 */
4086 case '$': ret = compile_get_env(arg, cctx);
4087 break;
4088
4089 /*
4090 * Register contents: @r.
4091 */
4092 case '@': ret = compile_get_register(arg, cctx);
4093 break;
4094 /*
4095 * nested expression: (expression).
4096 */
4097 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004098
4099 // recursive!
4100 if (ppconst->pp_used <= PPSIZE - 10)
4101 {
4102 ret = compile_expr1(arg, cctx, ppconst);
4103 }
4104 else
4105 {
4106 // Not enough space in ppconst, flush constants.
4107 if (generate_ppconst(cctx, ppconst) == FAIL)
4108 return FAIL;
4109 ret = compile_expr0(arg, cctx);
4110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 *arg = skipwhite(*arg);
4112 if (**arg == ')')
4113 ++*arg;
4114 else if (ret == OK)
4115 {
4116 emsg(_(e_missing_close));
4117 ret = FAIL;
4118 }
4119 break;
4120
4121 default: ret = NOTDONE;
4122 break;
4123 }
4124 if (ret == FAIL)
4125 return FAIL;
4126
Bram Moolenaar1c747212020-05-09 18:28:34 +02004127 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 {
4129 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004130 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004132 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 return FAIL;
4134 }
4135 start_leader = end_leader; // don't apply again below
4136
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004137 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004138 clear_tv(rettv);
4139 else
4140 // A constant expression can possibly be handled compile time,
4141 // return the value instead of generating code.
4142 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 }
4144 else if (ret == NOTDONE)
4145 {
4146 char_u *p;
4147 int r;
4148
4149 if (!eval_isnamec1(**arg))
4150 {
4151 semsg(_("E1015: Name expected: %s"), *arg);
4152 return FAIL;
4153 }
4154
4155 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004156 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004158 {
4159 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004162 {
4163 if (generate_ppconst(cctx, ppconst) == FAIL)
4164 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 if (r == FAIL)
4168 return FAIL;
4169 }
4170
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004171 // Handle following "[]", ".member", etc.
4172 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004173 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004174 ppconst) == FAIL)
4175 return FAIL;
4176 if (ppconst->pp_used > 0)
4177 {
4178 // apply the '!', '-' and '+' before the constant
4179 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4180 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4181 return FAIL;
4182 return OK;
4183 }
4184 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004186 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187}
4188
4189/*
4190 * * number multiplication
4191 * / number division
4192 * % number modulo
4193 */
4194 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004195compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196{
4197 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004198 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004199 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004201 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004202 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 return FAIL;
4204
4205 /*
4206 * Repeat computing, until no "*", "/" or "%" is following.
4207 */
4208 for (;;)
4209 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004210 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 if (*op != '*' && *op != '/' && *op != '%')
4212 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004213 if (next != NULL)
4214 {
4215 *arg = next_line_from_context(cctx, TRUE);
4216 op = skipwhite(*arg);
4217 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004218
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004219 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 {
4221 char_u buf[3];
4222
4223 vim_strncpy(buf, op, 1);
4224 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004225 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 }
4227 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004228 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004229 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004231 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004232 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004234
4235 if (ppconst->pp_used == ppconst_used + 2
4236 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4237 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004238 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004239 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4240 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004241 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004243 // both are numbers: compute the result
4244 switch (*op)
4245 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004246 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004247 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004248 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004249 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004250 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004251 break;
4252 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004253 tv1->vval.v_number = res;
4254 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004255 }
4256 else
4257 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004258 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004259 generate_two_op(cctx, op);
4260 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 }
4262
4263 return OK;
4264}
4265
4266/*
4267 * + number addition
4268 * - number subtraction
4269 * .. string concatenation
4270 */
4271 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004272compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273{
4274 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004275 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004277 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278
4279 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004280 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 return FAIL;
4282
4283 /*
4284 * Repeat computing, until no "+", "-" or ".." is following.
4285 */
4286 for (;;)
4287 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004288 op = may_peek_next_line(cctx, *arg, &next);
4289 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 break;
4291 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004292 if (next != NULL)
4293 {
4294 *arg = next_line_from_context(cctx, TRUE);
4295 op = skipwhite(*arg);
4296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004298 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 {
4300 char_u buf[3];
4301
4302 vim_strncpy(buf, op, oplen);
4303 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004304 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 }
4306
4307 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004308 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004309 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004311 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004312 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 return FAIL;
4314
Bram Moolenaara5565e42020-05-09 15:44:01 +02004315 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004316 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004317 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4318 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4319 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4320 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004322 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4323 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004324
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004325 // concat/subtract/add constant numbers
4326 if (*op == '+')
4327 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4328 else if (*op == '-')
4329 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4330 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004331 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004332 // concatenate constant strings
4333 char_u *s1 = tv1->vval.v_string;
4334 char_u *s2 = tv2->vval.v_string;
4335 size_t len1 = STRLEN(s1);
4336
4337 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4338 if (tv1->vval.v_string == NULL)
4339 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004340 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004341 return FAIL;
4342 }
4343 mch_memmove(tv1->vval.v_string, s1, len1);
4344 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004345 vim_free(s1);
4346 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004347 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004348 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 }
4350 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004351 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004352 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004353 if (*op == '.')
4354 {
4355 if (may_generate_2STRING(-2, cctx) == FAIL
4356 || may_generate_2STRING(-1, cctx) == FAIL)
4357 return FAIL;
4358 generate_instr_drop(cctx, ISN_CONCAT, 1);
4359 }
4360 else
4361 generate_two_op(cctx, op);
4362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 }
4364
4365 return OK;
4366}
4367
4368/*
4369 * expr5a == expr5b
4370 * expr5a =~ expr5b
4371 * expr5a != expr5b
4372 * expr5a !~ expr5b
4373 * expr5a > expr5b
4374 * expr5a >= expr5b
4375 * expr5a < expr5b
4376 * expr5a <= expr5b
4377 * expr5a is expr5b
4378 * expr5a isnot expr5b
4379 *
4380 * Produces instructions:
4381 * EVAL expr5a Push result of "expr5a"
4382 * EVAL expr5b Push result of "expr5b"
4383 * COMPARE one of the compare instructions
4384 */
4385 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004386compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387{
4388 exptype_T type = EXPR_UNKNOWN;
4389 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004390 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004393 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394
4395 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004396 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 return FAIL;
4398
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004399 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004400 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401
4402 /*
4403 * If there is a comparative operator, use it.
4404 */
4405 if (type != EXPR_UNKNOWN)
4406 {
4407 int ic = FALSE; // Default: do not ignore case
4408
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004409 if (next != NULL)
4410 {
4411 *arg = next_line_from_context(cctx, TRUE);
4412 p = skipwhite(*arg);
4413 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 if (type_is && (p[len] == '?' || p[len] == '#'))
4415 {
4416 semsg(_(e_invexpr2), *arg);
4417 return FAIL;
4418 }
4419 // extra question mark appended: ignore case
4420 if (p[len] == '?')
4421 {
4422 ic = TRUE;
4423 ++len;
4424 }
4425 // extra '#' appended: match case (ignored)
4426 else if (p[len] == '#')
4427 ++len;
4428 // nothing appended: match case
4429
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004430 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 {
4432 char_u buf[7];
4433
4434 vim_strncpy(buf, p, len);
4435 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004436 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 }
4438
4439 // get the second variable
4440 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004441 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004442 return FAIL;
4443
Bram Moolenaara5565e42020-05-09 15:44:01 +02004444 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 return FAIL;
4446
Bram Moolenaara5565e42020-05-09 15:44:01 +02004447 if (ppconst->pp_used == ppconst_used + 2)
4448 {
4449 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4450 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4451 int ret;
4452
4453 // Both sides are a constant, compute the result now.
4454 // First check for a valid combination of types, this is more
4455 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004456 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004457 ret = FAIL;
4458 else
4459 {
4460 ret = typval_compare(tv1, tv2, type, ic);
4461 tv1->v_type = VAR_BOOL;
4462 tv1->vval.v_number = tv1->vval.v_number
4463 ? VVAL_TRUE : VVAL_FALSE;
4464 clear_tv(tv2);
4465 --ppconst->pp_used;
4466 }
4467 return ret;
4468 }
4469
4470 generate_ppconst(cctx, ppconst);
4471 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 }
4473
4474 return OK;
4475}
4476
Bram Moolenaar7f141552020-05-09 17:35:53 +02004477static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479/*
4480 * Compile || or &&.
4481 */
4482 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004483compile_and_or(
4484 char_u **arg,
4485 cctx_T *cctx,
4486 char *op,
4487 ppconst_T *ppconst,
4488 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004490 char_u *next;
4491 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 int opchar = *op;
4493
4494 if (p[0] == opchar && p[1] == opchar)
4495 {
4496 garray_T *instr = &cctx->ctx_instr;
4497 garray_T end_ga;
4498
4499 /*
4500 * Repeat until there is no following "||" or "&&"
4501 */
4502 ga_init2(&end_ga, sizeof(int), 10);
4503 while (p[0] == opchar && p[1] == opchar)
4504 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004505 if (next != NULL)
4506 {
4507 *arg = next_line_from_context(cctx, TRUE);
4508 p = skipwhite(*arg);
4509 }
4510
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004511 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4512 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004514 return FAIL;
4515 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516
Bram Moolenaara5565e42020-05-09 15:44:01 +02004517 // TODO: use ppconst if the value is a constant
4518 generate_ppconst(cctx, ppconst);
4519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 if (ga_grow(&end_ga, 1) == FAIL)
4521 {
4522 ga_clear(&end_ga);
4523 return FAIL;
4524 }
4525 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4526 ++end_ga.ga_len;
4527 generate_JUMP(cctx, opchar == '|'
4528 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4529
4530 // eval the next expression
4531 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004532 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004533 return FAIL;
4534
Bram Moolenaara5565e42020-05-09 15:44:01 +02004535 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4536 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 {
4538 ga_clear(&end_ga);
4539 return FAIL;
4540 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004541
4542 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004544 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545
4546 // Fill in the end label in all jumps.
4547 while (end_ga.ga_len > 0)
4548 {
4549 isn_T *isn;
4550
4551 --end_ga.ga_len;
4552 isn = ((isn_T *)instr->ga_data)
4553 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4554 isn->isn_arg.jump.jump_where = instr->ga_len;
4555 }
4556 ga_clear(&end_ga);
4557 }
4558
4559 return OK;
4560}
4561
4562/*
4563 * expr4a && expr4a && expr4a logical AND
4564 *
4565 * Produces instructions:
4566 * EVAL expr4a Push result of "expr4a"
4567 * JUMP_AND_KEEP_IF_FALSE end
4568 * EVAL expr4b Push result of "expr4b"
4569 * JUMP_AND_KEEP_IF_FALSE end
4570 * EVAL expr4c Push result of "expr4c"
4571 * end:
4572 */
4573 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004574compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004576 int ppconst_used = ppconst->pp_used;
4577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004579 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 return FAIL;
4581
4582 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004583 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584}
4585
4586/*
4587 * expr3a || expr3b || expr3c logical OR
4588 *
4589 * Produces instructions:
4590 * EVAL expr3a Push result of "expr3a"
4591 * JUMP_AND_KEEP_IF_TRUE end
4592 * EVAL expr3b Push result of "expr3b"
4593 * JUMP_AND_KEEP_IF_TRUE end
4594 * EVAL expr3c Push result of "expr3c"
4595 * end:
4596 */
4597 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004598compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004600 int ppconst_used = ppconst->pp_used;
4601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004603 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 return FAIL;
4605
4606 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004607 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608}
4609
4610/*
4611 * Toplevel expression: expr2 ? expr1a : expr1b
4612 *
4613 * Produces instructions:
4614 * EVAL expr2 Push result of "expr"
4615 * JUMP_IF_FALSE alt jump if false
4616 * EVAL expr1a
4617 * JUMP_ALWAYS end
4618 * alt: EVAL expr1b
4619 * end:
4620 */
4621 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004622compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623{
4624 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004625 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004626 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627
Bram Moolenaar61a89812020-05-07 16:58:17 +02004628 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004629 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 return FAIL;
4631
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004632 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 if (*p == '?')
4634 {
4635 garray_T *instr = &cctx->ctx_instr;
4636 garray_T *stack = &cctx->ctx_type_stack;
4637 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004638 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004640 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004642 int has_const_expr = FALSE;
4643 int const_value = FALSE;
4644 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004646 if (next != NULL)
4647 {
4648 *arg = next_line_from_context(cctx, TRUE);
4649 p = skipwhite(*arg);
4650 }
4651
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004652 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4653 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004655 return FAIL;
4656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657
Bram Moolenaara5565e42020-05-09 15:44:01 +02004658 if (ppconst->pp_used == ppconst_used + 1)
4659 {
4660 // the condition is a constant, we know whether the ? or the :
4661 // expression is to be evaluated.
4662 has_const_expr = TRUE;
4663 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4664 clear_tv(&ppconst->pp_tv[ppconst_used]);
4665 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004666 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4667 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004668 }
4669 else
4670 {
4671 generate_ppconst(cctx, ppconst);
4672 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4673 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674
4675 // evaluate the second expression; any type is accepted
4676 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004677 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004678 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004679 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004680 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681
Bram Moolenaara5565e42020-05-09 15:44:01 +02004682 if (!has_const_expr)
4683 {
4684 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685
Bram Moolenaara5565e42020-05-09 15:44:01 +02004686 // remember the type and drop it
4687 --stack->ga_len;
4688 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689
Bram Moolenaara5565e42020-05-09 15:44:01 +02004690 end_idx = instr->ga_len;
4691 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4692
4693 // jump here from JUMP_IF_FALSE
4694 isn = ((isn_T *)instr->ga_data) + alt_idx;
4695 isn->isn_arg.jump.jump_where = instr->ga_len;
4696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697
4698 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004699 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 if (*p != ':')
4701 {
4702 emsg(_(e_missing_colon));
4703 return FAIL;
4704 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004705 if (next != NULL)
4706 {
4707 *arg = next_line_from_context(cctx, TRUE);
4708 p = skipwhite(*arg);
4709 }
4710
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004711 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4712 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004714 return FAIL;
4715 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716
4717 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004719 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4720 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004722 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004723 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004724 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004725 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726
Bram Moolenaara5565e42020-05-09 15:44:01 +02004727 if (!has_const_expr)
4728 {
4729 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730
Bram Moolenaara5565e42020-05-09 15:44:01 +02004731 // If the types differ, the result has a more generic type.
4732 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4733 common_type(type1, type2, &type2, cctx->ctx_type_list);
4734
4735 // jump here from JUMP_ALWAYS
4736 isn = ((isn_T *)instr->ga_data) + end_idx;
4737 isn->isn_arg.jump.jump_where = instr->ga_len;
4738 }
4739
4740 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741 }
4742 return OK;
4743}
4744
4745/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004746 * Toplevel expression.
4747 */
4748 static int
4749compile_expr0(char_u **arg, cctx_T *cctx)
4750{
4751 ppconst_T ppconst;
4752
4753 CLEAR_FIELD(ppconst);
4754 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4755 {
4756 clear_ppconst(&ppconst);
4757 return FAIL;
4758 }
4759 if (generate_ppconst(cctx, &ppconst) == FAIL)
4760 return FAIL;
4761 return OK;
4762}
4763
4764/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 * compile "return [expr]"
4766 */
4767 static char_u *
4768compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4769{
4770 char_u *p = arg;
4771 garray_T *stack = &cctx->ctx_type_stack;
4772 type_T *stack_type;
4773
4774 if (*p != NUL && *p != '|' && *p != '\n')
4775 {
4776 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004777 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 return NULL;
4779
4780 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4781 if (set_return_type)
4782 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004783 else
4784 {
4785 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4786 && stack_type->tt_type != VAR_VOID
4787 && stack_type->tt_type != VAR_UNKNOWN)
4788 {
4789 emsg(_("E1096: Returning a value in a function without a return type"));
4790 return NULL;
4791 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004792 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4793 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 }
4797 else
4798 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004799 // "set_return_type" cannot be TRUE, only used for a lambda which
4800 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004801 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4802 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803 {
4804 emsg(_("E1003: Missing return value"));
4805 return NULL;
4806 }
4807
4808 // No argument, return zero.
4809 generate_PUSHNR(cctx, 0);
4810 }
4811
4812 if (generate_instr(cctx, ISN_RETURN) == NULL)
4813 return NULL;
4814
4815 // "return val | endif" is possible
4816 return skipwhite(p);
4817}
4818
4819/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004820 * Get a line from the compilation context, compatible with exarg_T getline().
4821 * Return a pointer to the line in allocated memory.
4822 * Return NULL for end-of-file or some error.
4823 */
4824 static char_u *
4825exarg_getline(
4826 int c UNUSED,
4827 void *cookie,
4828 int indent UNUSED,
4829 int do_concat UNUSED)
4830{
4831 cctx_T *cctx = (cctx_T *)cookie;
4832
4833 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4834 {
4835 iemsg("Heredoc got to end");
4836 return NULL;
4837 }
4838 ++cctx->ctx_lnum;
4839 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4840 [cctx->ctx_lnum]);
4841}
4842
4843/*
4844 * Compile a nested :def command.
4845 */
4846 static char_u *
4847compile_nested_function(exarg_T *eap, cctx_T *cctx)
4848{
4849 char_u *name_start = eap->arg;
4850 char_u *name_end = to_name_end(eap->arg, FALSE);
4851 char_u *name = get_lambda_name();
4852 lvar_T *lvar;
4853 ufunc_T *ufunc;
4854
4855 eap->arg = name_end;
4856 eap->getline = exarg_getline;
4857 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004858 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004859 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004860 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004861
Bram Moolenaar822ba242020-05-24 23:00:18 +02004862 if (ufunc == NULL)
4863 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004864 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004865 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004866 return NULL;
4867
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004868 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004869 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004870 TRUE, ufunc->uf_func_type);
4871
4872 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4873 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4874 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004875
Bram Moolenaar61a89812020-05-07 16:58:17 +02004876 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004877 return (char_u *)"";
4878}
4879
4880/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 * Return the length of an assignment operator, or zero if there isn't one.
4882 */
4883 int
4884assignment_len(char_u *p, int *heredoc)
4885{
4886 if (*p == '=')
4887 {
4888 if (p[1] == '<' && p[2] == '<')
4889 {
4890 *heredoc = TRUE;
4891 return 3;
4892 }
4893 return 1;
4894 }
4895 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4896 return 2;
4897 if (STRNCMP(p, "..=", 3) == 0)
4898 return 3;
4899 return 0;
4900}
4901
4902// words that cannot be used as a variable
4903static char *reserved[] = {
4904 "true",
4905 "false",
4906 NULL
4907};
4908
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004909typedef enum {
4910 dest_local,
4911 dest_option,
4912 dest_env,
4913 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004914 dest_buffer,
4915 dest_window,
4916 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004917 dest_vimvar,
4918 dest_script,
4919 dest_reg,
4920} assign_dest_T;
4921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004923 * Generate the load instruction for "name".
4924 */
4925 static void
4926generate_loadvar(
4927 cctx_T *cctx,
4928 assign_dest_T dest,
4929 char_u *name,
4930 lvar_T *lvar,
4931 type_T *type)
4932{
4933 switch (dest)
4934 {
4935 case dest_option:
4936 // TODO: check the option exists
4937 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4938 break;
4939 case dest_global:
4940 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4941 break;
4942 case dest_buffer:
4943 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4944 break;
4945 case dest_window:
4946 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4947 break;
4948 case dest_tab:
4949 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4950 break;
4951 case dest_script:
4952 compile_load_scriptvar(cctx,
4953 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4954 break;
4955 case dest_env:
4956 // Include $ in the name here
4957 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4958 break;
4959 case dest_reg:
4960 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4961 break;
4962 case dest_vimvar:
4963 generate_LOADV(cctx, name + 2, TRUE);
4964 break;
4965 case dest_local:
4966 if (lvar->lv_from_outer)
4967 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4968 NULL, type);
4969 else
4970 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4971 break;
4972 }
4973}
4974
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004975 void
4976vim9_declare_error(char_u *name)
4977{
4978 char *scope = "";
4979
4980 switch (*name)
4981 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004982 case 'g': scope = _("global"); break;
4983 case 'b': scope = _("buffer"); break;
4984 case 'w': scope = _("window"); break;
4985 case 't': scope = _("tab"); break;
4986 case 'v': scope = "v:"; break;
4987 case '$': semsg(_(e_declare_env_var), name); return;
4988 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004989 }
4990 semsg(_(e_declare_var), scope, name);
4991}
4992
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004993/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004994 * Compile declaration and assignment:
4995 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004997 * Return NULL for an error.
4998 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 */
5000 static char_u *
5001compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5002{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005003 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005005 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006 char_u *ret = NULL;
5007 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005008 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005011 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 int oplen = 0;
5014 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005015 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005016 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005017 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005021 // Skip over the "var" or "[var, var]" to get to any "=".
5022 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5023 if (p == NULL)
5024 return *arg == '[' ? arg : NULL;
5025
5026 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005028 // TODO: should we allow this, and figure out type inference from list
5029 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005030 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 return NULL;
5032 }
5033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034 sp = p;
5035 p = skipwhite(p);
5036 op = p;
5037 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005038
5039 if (var_count > 0 && oplen == 0)
5040 // can be something like "[1, 2]->func()"
5041 return arg;
5042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5044 {
5045 char_u buf[4];
5046
5047 vim_strncpy(buf, op, oplen);
5048 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005049 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005050 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052 if (heredoc)
5053 {
5054 list_T *l;
5055 listitem_T *li;
5056
5057 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005058 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005060 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061
5062 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005063 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 {
5065 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5066 li->li_tv.vval.v_string = NULL;
5067 }
5068 generate_NEWLIST(cctx, l->lv_len);
5069 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005070 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071 list_free(l);
5072 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005073 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005074 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005075 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005077 // for "[var, var] = expr" evaluate the expression here, loop over the
5078 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005079
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005080 p = skipwhite(op + oplen);
5081 if (compile_expr0(&p, cctx) == FAIL)
5082 return NULL;
5083 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005085 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005087 type_T *stacktype;
5088
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005089 stacktype = stack->ga_len == 0 ? &t_void
5090 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005091 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005093 emsg(_(e_cannot_use_void));
5094 goto theend;
5095 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005096 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005097 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005098 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005099 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5100 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005101 }
5102 }
5103
5104 /*
5105 * Loop over variables in "[var, var] = expr".
5106 * For "var = expr" and "let var: type" this is done only once.
5107 */
5108 if (var_count > 0)
5109 var_start = skipwhite(arg + 1); // skip over the "["
5110 else
5111 var_start = arg;
5112 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5113 {
5114 char_u *var_end = skip_var_one(var_start, FALSE);
5115 size_t varlen;
5116 int new_local = FALSE;
5117 int opt_type;
5118 int opt_flags = 0;
5119 assign_dest_T dest = dest_local;
5120 int vimvaridx = -1;
5121 lvar_T *lvar = NULL;
5122 lvar_T arg_lvar;
5123 int has_type = FALSE;
5124 int has_index = FALSE;
5125 int instr_count = -1;
5126
5127 p = (*var_start == '&' || *var_start == '$'
5128 || *var_start == '@') ? var_start + 1 : var_start;
5129 p = to_name_end(p, TRUE);
5130
5131 // "a: type" is declaring variable "a" with a type, not "a:".
5132 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5133 --var_end;
5134 if (is_decl && p == var_start + 2 && p[-1] == ':')
5135 --p;
5136
5137 varlen = p - var_start;
5138 vim_free(name);
5139 name = vim_strnsave(var_start, varlen);
5140 if (name == NULL)
5141 return NULL;
5142 if (!heredoc)
5143 type = &t_any;
5144
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005145 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005146 {
5147 if (*var_start == '&')
5148 {
5149 int cc;
5150 long numval;
5151
5152 dest = dest_option;
5153 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005154 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005155 emsg(_(e_const_option));
5156 goto theend;
5157 }
5158 if (is_decl)
5159 {
5160 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5161 goto theend;
5162 }
5163 p = var_start;
5164 p = find_option_end(&p, &opt_flags);
5165 if (p == NULL)
5166 {
5167 // cannot happen?
5168 emsg(_(e_letunexp));
5169 goto theend;
5170 }
5171 cc = *p;
5172 *p = NUL;
5173 opt_type = get_option_value(var_start + 1, &numval,
5174 NULL, opt_flags);
5175 *p = cc;
5176 if (opt_type == -3)
5177 {
5178 semsg(_(e_unknown_option), var_start);
5179 goto theend;
5180 }
5181 if (opt_type == -2 || opt_type == 0)
5182 type = &t_string;
5183 else
5184 type = &t_number; // both number and boolean option
5185 }
5186 else if (*var_start == '$')
5187 {
5188 dest = dest_env;
5189 type = &t_string;
5190 if (is_decl)
5191 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005192 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005193 goto theend;
5194 }
5195 }
5196 else if (*var_start == '@')
5197 {
5198 if (!valid_yank_reg(var_start[1], TRUE))
5199 {
5200 emsg_invreg(var_start[1]);
5201 goto theend;
5202 }
5203 dest = dest_reg;
5204 type = &t_string;
5205 if (is_decl)
5206 {
5207 semsg(_("E1066: Cannot declare a register: %s"), name);
5208 goto theend;
5209 }
5210 }
5211 else if (STRNCMP(var_start, "g:", 2) == 0)
5212 {
5213 dest = dest_global;
5214 if (is_decl)
5215 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005216 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005217 goto theend;
5218 }
5219 }
5220 else if (STRNCMP(var_start, "b:", 2) == 0)
5221 {
5222 dest = dest_buffer;
5223 if (is_decl)
5224 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005225 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005226 goto theend;
5227 }
5228 }
5229 else if (STRNCMP(var_start, "w:", 2) == 0)
5230 {
5231 dest = dest_window;
5232 if (is_decl)
5233 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005234 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005235 goto theend;
5236 }
5237 }
5238 else if (STRNCMP(var_start, "t:", 2) == 0)
5239 {
5240 dest = dest_tab;
5241 if (is_decl)
5242 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005243 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005244 goto theend;
5245 }
5246 }
5247 else if (STRNCMP(var_start, "v:", 2) == 0)
5248 {
5249 typval_T *vtv;
5250 int di_flags;
5251
5252 vimvaridx = find_vim_var(name + 2, &di_flags);
5253 if (vimvaridx < 0)
5254 {
5255 semsg(_(e_var_notfound), var_start);
5256 goto theend;
5257 }
5258 // We use the current value of "sandbox" here, is that OK?
5259 if (var_check_ro(di_flags, name, FALSE))
5260 goto theend;
5261 dest = dest_vimvar;
5262 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005263 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005264 if (is_decl)
5265 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005266 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005267 goto theend;
5268 }
5269 }
5270 else
5271 {
5272 int idx;
5273
5274 for (idx = 0; reserved[idx] != NULL; ++idx)
5275 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005276 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005277 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005278 goto theend;
5279 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005280
5281 lvar = lookup_local(var_start, varlen, cctx);
5282 if (lvar == NULL)
5283 {
5284 CLEAR_FIELD(arg_lvar);
5285 if (lookup_arg(var_start, varlen,
5286 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5287 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005288 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005289 if (is_decl)
5290 {
5291 semsg(_(e_used_as_arg), name);
5292 goto theend;
5293 }
5294 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005295 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005296 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005297 if (lvar != NULL)
5298 {
5299 if (is_decl)
5300 {
5301 semsg(_("E1017: Variable already declared: %s"), name);
5302 goto theend;
5303 }
5304 else if (lvar->lv_const)
5305 {
5306 semsg(_("E1018: Cannot assign to a constant: %s"),
5307 name);
5308 goto theend;
5309 }
5310 }
5311 else if (STRNCMP(var_start, "s:", 2) == 0
5312 || lookup_script(var_start, varlen) == OK
5313 || find_imported(var_start, varlen, cctx) != NULL)
5314 {
5315 dest = dest_script;
5316 if (is_decl)
5317 {
5318 semsg(_("E1054: Variable already declared in the script: %s"),
5319 name);
5320 goto theend;
5321 }
5322 }
5323 else if (name[1] == ':' && name[2] != NUL)
5324 {
5325 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5326 name);
5327 goto theend;
5328 }
5329 else if (!is_decl)
5330 {
5331 semsg(_("E1089: unknown variable: %s"), name);
5332 goto theend;
5333 }
5334 }
5335 }
5336
5337 // handle "a:name" as a name, not index "name" on "a"
5338 if (varlen > 1 || var_start[varlen] != ':')
5339 p = var_end;
5340
5341 if (dest != dest_option)
5342 {
5343 if (is_decl && *p == ':')
5344 {
5345 // parse optional type: "let var: type = expr"
5346 if (!VIM_ISWHITE(p[1]))
5347 {
5348 semsg(_(e_white_after), ":");
5349 goto theend;
5350 }
5351 p = skipwhite(p + 1);
5352 type = parse_type(&p, cctx->ctx_type_list);
5353 has_type = TRUE;
5354 }
5355 else if (lvar != NULL)
5356 type = lvar->lv_type;
5357 }
5358
5359 if (oplen == 3 && !heredoc && dest != dest_global
5360 && type->tt_type != VAR_STRING
5361 && type->tt_type != VAR_ANY)
5362 {
5363 emsg(_("E1019: Can only concatenate to string"));
5364 goto theend;
5365 }
5366
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005367 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005368 {
5369 if (oplen > 1 && !heredoc)
5370 {
5371 // +=, /=, etc. require an existing variable
5372 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5373 name);
5374 goto theend;
5375 }
5376
5377 // new local variable
5378 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5379 goto theend;
5380 lvar = reserve_local(cctx, var_start, varlen,
5381 cmdidx == CMD_const, type);
5382 if (lvar == NULL)
5383 goto theend;
5384 new_local = TRUE;
5385 }
5386
5387 member_type = type;
5388 if (var_end > var_start + varlen)
5389 {
5390 // Something follows after the variable: "var[idx]".
5391 if (is_decl)
5392 {
5393 emsg(_("E1087: cannot use an index when declaring a variable"));
5394 goto theend;
5395 }
5396
5397 if (var_start[varlen] == '[')
5398 {
5399 has_index = TRUE;
5400 if (type->tt_member == NULL)
5401 {
5402 semsg(_("E1088: cannot use an index on %s"), name);
5403 goto theend;
5404 }
5405 member_type = type->tt_member;
5406 }
5407 else
5408 {
5409 semsg("Not supported yet: %s", var_start);
5410 goto theend;
5411 }
5412 }
5413 else if (lvar == &arg_lvar)
5414 {
5415 semsg(_("E1090: Cannot assign to argument %s"), name);
5416 goto theend;
5417 }
5418
5419 if (!heredoc)
5420 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005421 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005422 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005423 if (oplen > 0 && var_count == 0)
5424 {
5425 // skip over the "=" and the expression
5426 p = skipwhite(op + oplen);
5427 compile_expr0(&p, cctx);
5428 }
5429 }
5430 else if (oplen > 0)
5431 {
5432 type_T *stacktype;
5433
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005434 // For "var = expr" evaluate the expression.
5435 if (var_count == 0)
5436 {
5437 int r;
5438
5439 // for "+=", "*=", "..=" etc. first load the current value
5440 if (*op != '=')
5441 {
5442 generate_loadvar(cctx, dest, name, lvar, type);
5443
5444 if (has_index)
5445 {
5446 // TODO: get member from list or dict
5447 emsg("Index with operation not supported yet");
5448 goto theend;
5449 }
5450 }
5451
5452 // Compile the expression. Temporarily hide the new local
5453 // variable here, it is not available to this expression.
5454 if (new_local)
5455 --cctx->ctx_locals.ga_len;
5456 instr_count = instr->ga_len;
5457 p = skipwhite(op + oplen);
5458 r = compile_expr0(&p, cctx);
5459 if (new_local)
5460 ++cctx->ctx_locals.ga_len;
5461 if (r == FAIL)
5462 goto theend;
5463 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005464 else if (semicolon && var_idx == var_count - 1)
5465 {
5466 // For "[var; var] = expr" get the rest of the list
5467 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5468 goto theend;
5469 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005470 else
5471 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005472 // For "[var, var] = expr" get the "var_idx" item from the
5473 // list.
5474 if (generate_GETITEM(cctx, var_idx) == FAIL)
5475 return FAIL;
5476 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005477
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005478 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005479 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005480 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005481 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005482 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005483 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005484 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005485 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005486 emsg(_(e_cannot_use_void));
5487 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005488 }
5489 else
5490 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005491 // An empty list or dict has a &t_void member,
5492 // for a variable that implies &t_any.
5493 if (stacktype == &t_list_empty)
5494 lvar->lv_type = &t_list_any;
5495 else if (stacktype == &t_dict_empty)
5496 lvar->lv_type = &t_dict_any;
5497 else
5498 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005499 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005500 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005501 else
5502 {
5503 type_T *use_type = lvar->lv_type;
5504
5505 if (has_index)
5506 {
5507 use_type = use_type->tt_member;
5508 if (use_type == NULL)
5509 use_type = &t_void;
5510 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005511 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005512 == FAIL)
5513 goto theend;
5514 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005515 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005516 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005517 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005518 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005520 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005521 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005522 emsg(_(e_const_req_value));
5523 goto theend;
5524 }
5525 else if (!has_type || dest == dest_option)
5526 {
5527 emsg(_(e_type_req));
5528 goto theend;
5529 }
5530 else
5531 {
5532 // variables are always initialized
5533 if (ga_grow(instr, 1) == FAIL)
5534 goto theend;
5535 switch (member_type->tt_type)
5536 {
5537 case VAR_BOOL:
5538 generate_PUSHBOOL(cctx, VVAL_FALSE);
5539 break;
5540 case VAR_FLOAT:
5541#ifdef FEAT_FLOAT
5542 generate_PUSHF(cctx, 0.0);
5543#endif
5544 break;
5545 case VAR_STRING:
5546 generate_PUSHS(cctx, NULL);
5547 break;
5548 case VAR_BLOB:
5549 generate_PUSHBLOB(cctx, NULL);
5550 break;
5551 case VAR_FUNC:
5552 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5553 break;
5554 case VAR_LIST:
5555 generate_NEWLIST(cctx, 0);
5556 break;
5557 case VAR_DICT:
5558 generate_NEWDICT(cctx, 0);
5559 break;
5560 case VAR_JOB:
5561 generate_PUSHJOB(cctx, NULL);
5562 break;
5563 case VAR_CHANNEL:
5564 generate_PUSHCHANNEL(cctx, NULL);
5565 break;
5566 case VAR_NUMBER:
5567 case VAR_UNKNOWN:
5568 case VAR_ANY:
5569 case VAR_PARTIAL:
5570 case VAR_VOID:
5571 case VAR_SPECIAL: // cannot happen
5572 generate_PUSHNR(cctx, 0);
5573 break;
5574 }
5575 }
5576 if (var_count == 0)
5577 end = p;
5578 }
5579
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005580 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005581 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005582 break;
5583
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005584 if (oplen > 0 && *op != '=')
5585 {
5586 type_T *expected = &t_number;
5587 type_T *stacktype;
5588
5589 // TODO: if type is known use float or any operation
5590
5591 if (*op == '.')
5592 expected = &t_string;
5593 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005594 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005595 goto theend;
5596
5597 if (*op == '.')
5598 generate_instr_drop(cctx, ISN_CONCAT, 1);
5599 else
5600 {
5601 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5602
5603 if (isn == NULL)
5604 goto theend;
5605 switch (*op)
5606 {
5607 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5608 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5609 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5610 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5611 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5612 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613 }
5614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005615
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005616 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005617 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005618 int r;
5619
5620 // Compile the "idx" in "var[idx]".
5621 if (new_local)
5622 --cctx->ctx_locals.ga_len;
5623 p = skipwhite(var_start + varlen + 1);
5624 r = compile_expr0(&p, cctx);
5625 if (new_local)
5626 ++cctx->ctx_locals.ga_len;
5627 if (r == FAIL)
5628 goto theend;
5629 if (*skipwhite(p) != ']')
5630 {
5631 emsg(_(e_missbrac));
5632 goto theend;
5633 }
5634 if (type->tt_type == VAR_DICT
5635 && may_generate_2STRING(-1, cctx) == FAIL)
5636 goto theend;
5637 if (type->tt_type == VAR_LIST
5638 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005639 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005640 {
5641 emsg(_(e_number_exp));
5642 goto theend;
5643 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005644
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005645 // Load the dict or list. On the stack we then have:
5646 // - value
5647 // - index
5648 // - variable
5649 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005650
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005651 if (type->tt_type == VAR_LIST)
5652 {
5653 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5654 return FAIL;
5655 }
5656 else if (type->tt_type == VAR_DICT)
5657 {
5658 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5659 return FAIL;
5660 }
5661 else
5662 {
5663 emsg(_(e_listreq));
5664 goto theend;
5665 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005666 }
5667 else
5668 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005669 switch (dest)
5670 {
5671 case dest_option:
5672 generate_STOREOPT(cctx, name + 1, opt_flags);
5673 break;
5674 case dest_global:
5675 // include g: with the name, easier to execute that way
5676 generate_STORE(cctx, ISN_STOREG, 0, name);
5677 break;
5678 case dest_buffer:
5679 // include b: with the name, easier to execute that way
5680 generate_STORE(cctx, ISN_STOREB, 0, name);
5681 break;
5682 case dest_window:
5683 // include w: with the name, easier to execute that way
5684 generate_STORE(cctx, ISN_STOREW, 0, name);
5685 break;
5686 case dest_tab:
5687 // include t: with the name, easier to execute that way
5688 generate_STORE(cctx, ISN_STORET, 0, name);
5689 break;
5690 case dest_env:
5691 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5692 break;
5693 case dest_reg:
5694 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5695 break;
5696 case dest_vimvar:
5697 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5698 break;
5699 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005700 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005701 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5702 imported_T *import = NULL;
5703 int sid = current_sctx.sc_sid;
5704 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005705
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005706 if (name[1] != ':')
5707 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005708 import = find_imported(name, 0, cctx);
5709 if (import != NULL)
5710 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005711 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005712
5713 idx = get_script_item_idx(sid, rawname, TRUE);
5714 // TODO: specific type
5715 if (idx < 0)
5716 {
5717 char_u *name_s = name;
5718
5719 // Include s: in the name for store_var()
5720 if (name[1] != ':')
5721 {
5722 int len = (int)STRLEN(name) + 3;
5723
5724 name_s = alloc(len);
5725 if (name_s == NULL)
5726 name_s = name;
5727 else
5728 vim_snprintf((char *)name_s, len,
5729 "s:%s", name);
5730 }
5731 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005732 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005733 if (name_s != name)
5734 vim_free(name_s);
5735 }
5736 else
5737 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005738 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005739 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005740 break;
5741 case dest_local:
5742 if (lvar != NULL)
5743 {
5744 isn_T *isn = ((isn_T *)instr->ga_data)
5745 + instr->ga_len - 1;
5746
5747 // optimization: turn "var = 123" from ISN_PUSHNR +
5748 // ISN_STORE into ISN_STORENR
5749 if (!lvar->lv_from_outer
5750 && instr->ga_len == instr_count + 1
5751 && isn->isn_type == ISN_PUSHNR)
5752 {
5753 varnumber_T val = isn->isn_arg.number;
5754
5755 isn->isn_type = ISN_STORENR;
5756 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5757 isn->isn_arg.storenr.stnr_val = val;
5758 if (stack->ga_len > 0)
5759 --stack->ga_len;
5760 }
5761 else if (lvar->lv_from_outer)
5762 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005763 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005764 else
5765 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5766 }
5767 break;
5768 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005769 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005770
5771 if (var_idx + 1 < var_count)
5772 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005773 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005774
5775 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005776 if (var_count > 0 && !semicolon)
5777 {
5778 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5779 goto theend;
5780 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005781
Bram Moolenaarb2097502020-07-19 17:17:02 +02005782 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005783
5784theend:
5785 vim_free(name);
5786 return ret;
5787}
5788
5789/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005790 * Check if "name" can be "unlet".
5791 */
5792 int
5793check_vim9_unlet(char_u *name)
5794{
5795 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5796 {
5797 semsg(_("E1081: Cannot unlet %s"), name);
5798 return FAIL;
5799 }
5800 return OK;
5801}
5802
5803/*
5804 * Callback passed to ex_unletlock().
5805 */
5806 static int
5807compile_unlet(
5808 lval_T *lvp,
5809 char_u *name_end,
5810 exarg_T *eap,
5811 int deep UNUSED,
5812 void *coookie)
5813{
5814 cctx_T *cctx = coookie;
5815
5816 if (lvp->ll_tv == NULL)
5817 {
5818 char_u *p = lvp->ll_name;
5819 int cc = *name_end;
5820 int ret = OK;
5821
5822 // Normal name. Only supports g:, w:, t: and b: namespaces.
5823 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005824 if (*p == '$')
5825 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5826 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005827 ret = FAIL;
5828 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005829 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005830
5831 *name_end = cc;
5832 return ret;
5833 }
5834
5835 // TODO: unlet {list}[idx]
5836 // TODO: unlet {dict}[key]
5837 emsg("Sorry, :unlet not fully implemented yet");
5838 return FAIL;
5839}
5840
5841/*
5842 * compile "unlet var", "lock var" and "unlock var"
5843 * "arg" points to "var".
5844 */
5845 static char_u *
5846compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5847{
5848 char_u *p = arg;
5849
5850 if (eap->cmdidx != CMD_unlet)
5851 {
5852 emsg("Sorry, :lock and unlock not implemented yet");
5853 return NULL;
5854 }
5855
5856 if (*p == '!')
5857 {
5858 p = skipwhite(p + 1);
5859 eap->forceit = TRUE;
5860 }
5861
5862 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5863 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5864}
5865
5866/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005867 * Compile an :import command.
5868 */
5869 static char_u *
5870compile_import(char_u *arg, cctx_T *cctx)
5871{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005872 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005873}
5874
5875/*
5876 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5877 */
5878 static int
5879compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5880{
5881 garray_T *instr = &cctx->ctx_instr;
5882 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5883
5884 if (endlabel == NULL)
5885 return FAIL;
5886 endlabel->el_next = *el;
5887 *el = endlabel;
5888 endlabel->el_end_label = instr->ga_len;
5889
5890 generate_JUMP(cctx, when, 0);
5891 return OK;
5892}
5893
5894 static void
5895compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5896{
5897 garray_T *instr = &cctx->ctx_instr;
5898
5899 while (*el != NULL)
5900 {
5901 endlabel_T *cur = (*el);
5902 isn_T *isn;
5903
5904 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5905 isn->isn_arg.jump.jump_where = instr->ga_len;
5906 *el = cur->el_next;
5907 vim_free(cur);
5908 }
5909}
5910
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005911 static void
5912compile_free_jump_to_end(endlabel_T **el)
5913{
5914 while (*el != NULL)
5915 {
5916 endlabel_T *cur = (*el);
5917
5918 *el = cur->el_next;
5919 vim_free(cur);
5920 }
5921}
5922
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005923/*
5924 * Create a new scope and set up the generic items.
5925 */
5926 static scope_T *
5927new_scope(cctx_T *cctx, scopetype_T type)
5928{
5929 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5930
5931 if (scope == NULL)
5932 return NULL;
5933 scope->se_outer = cctx->ctx_scope;
5934 cctx->ctx_scope = scope;
5935 scope->se_type = type;
5936 scope->se_local_count = cctx->ctx_locals.ga_len;
5937 return scope;
5938}
5939
5940/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005941 * Free the current scope and go back to the outer scope.
5942 */
5943 static void
5944drop_scope(cctx_T *cctx)
5945{
5946 scope_T *scope = cctx->ctx_scope;
5947
5948 if (scope == NULL)
5949 {
5950 iemsg("calling drop_scope() without a scope");
5951 return;
5952 }
5953 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005954 switch (scope->se_type)
5955 {
5956 case IF_SCOPE:
5957 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5958 case FOR_SCOPE:
5959 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5960 case WHILE_SCOPE:
5961 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5962 case TRY_SCOPE:
5963 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5964 case NO_SCOPE:
5965 case BLOCK_SCOPE:
5966 break;
5967 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005968 vim_free(scope);
5969}
5970
5971/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005972 * compile "if expr"
5973 *
5974 * "if expr" Produces instructions:
5975 * EVAL expr Push result of "expr"
5976 * JUMP_IF_FALSE end
5977 * ... body ...
5978 * end:
5979 *
5980 * "if expr | else" Produces instructions:
5981 * EVAL expr Push result of "expr"
5982 * JUMP_IF_FALSE else
5983 * ... body ...
5984 * JUMP_ALWAYS end
5985 * else:
5986 * ... body ...
5987 * end:
5988 *
5989 * "if expr1 | elseif expr2 | else" Produces instructions:
5990 * EVAL expr Push result of "expr"
5991 * JUMP_IF_FALSE elseif
5992 * ... body ...
5993 * JUMP_ALWAYS end
5994 * elseif:
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 static char_u *
6004compile_if(char_u *arg, cctx_T *cctx)
6005{
6006 char_u *p = arg;
6007 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006008 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006009 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006010 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006011 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012
Bram Moolenaara5565e42020-05-09 15:44:01 +02006013 CLEAR_FIELD(ppconst);
6014 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006015 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006016 clear_ppconst(&ppconst);
6017 return NULL;
6018 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006019 if (cctx->ctx_skip == SKIP_YES)
6020 clear_ppconst(&ppconst);
6021 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006022 {
6023 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006024 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006025 clear_ppconst(&ppconst);
6026 }
6027 else
6028 {
6029 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006030 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006031 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006032 return NULL;
6033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034
6035 scope = new_scope(cctx, IF_SCOPE);
6036 if (scope == NULL)
6037 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006038 scope->se_skip_save = skip_save;
6039 // "is_had_return" will be reset if any block does not end in :return
6040 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006041
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006042 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006043 {
6044 // "where" is set when ":elseif", "else" or ":endif" is found
6045 scope->se_u.se_if.is_if_label = instr->ga_len;
6046 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6047 }
6048 else
6049 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050
6051 return p;
6052}
6053
6054 static char_u *
6055compile_elseif(char_u *arg, cctx_T *cctx)
6056{
6057 char_u *p = arg;
6058 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006059 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060 isn_T *isn;
6061 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006062 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063
6064 if (scope == NULL || scope->se_type != IF_SCOPE)
6065 {
6066 emsg(_(e_elseif_without_if));
6067 return NULL;
6068 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006069 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006070 if (!cctx->ctx_had_return)
6071 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006072
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006073 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006074 {
6075 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006077 return NULL;
6078 // previous "if" or "elseif" jumps here
6079 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6080 isn->isn_arg.jump.jump_where = instr->ga_len;
6081 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006083 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006084 CLEAR_FIELD(ppconst);
6085 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006086 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006087 clear_ppconst(&ppconst);
6088 return NULL;
6089 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006090 if (scope->se_skip_save == SKIP_YES)
6091 clear_ppconst(&ppconst);
6092 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006093 {
6094 // The expression results in a constant.
6095 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006096 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006097 clear_ppconst(&ppconst);
6098 scope->se_u.se_if.is_if_label = -1;
6099 }
6100 else
6101 {
6102 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006103 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006104 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006105 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006107 // "where" is set when ":elseif", "else" or ":endif" is found
6108 scope->se_u.se_if.is_if_label = instr->ga_len;
6109 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111
6112 return p;
6113}
6114
6115 static char_u *
6116compile_else(char_u *arg, cctx_T *cctx)
6117{
6118 char_u *p = arg;
6119 garray_T *instr = &cctx->ctx_instr;
6120 isn_T *isn;
6121 scope_T *scope = cctx->ctx_scope;
6122
6123 if (scope == NULL || scope->se_type != IF_SCOPE)
6124 {
6125 emsg(_(e_else_without_if));
6126 return NULL;
6127 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006128 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006129 if (!cctx->ctx_had_return)
6130 scope->se_u.se_if.is_had_return = FALSE;
6131 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006132
Bram Moolenaarefd88552020-06-18 20:50:10 +02006133 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006134 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006135 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006136 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006137 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006138 if (!cctx->ctx_had_return
6139 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6140 JUMP_ALWAYS, cctx) == FAIL)
6141 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006142 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006143
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006144 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006145 {
6146 if (scope->se_u.se_if.is_if_label >= 0)
6147 {
6148 // previous "if" or "elseif" jumps here
6149 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6150 isn->isn_arg.jump.jump_where = instr->ga_len;
6151 scope->se_u.se_if.is_if_label = -1;
6152 }
6153 }
6154
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006155 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006156 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006158
6159 return p;
6160}
6161
6162 static char_u *
6163compile_endif(char_u *arg, cctx_T *cctx)
6164{
6165 scope_T *scope = cctx->ctx_scope;
6166 ifscope_T *ifscope;
6167 garray_T *instr = &cctx->ctx_instr;
6168 isn_T *isn;
6169
6170 if (scope == NULL || scope->se_type != IF_SCOPE)
6171 {
6172 emsg(_(e_endif_without_if));
6173 return NULL;
6174 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006175 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006176 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006177 if (!cctx->ctx_had_return)
6178 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006179
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006180 if (scope->se_u.se_if.is_if_label >= 0)
6181 {
6182 // previous "if" or "elseif" jumps here
6183 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6184 isn->isn_arg.jump.jump_where = instr->ga_len;
6185 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006186 // Fill in the "end" label in jumps at the end of the blocks.
6187 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006188 cctx->ctx_skip = scope->se_skip_save;
6189
6190 // If all the blocks end in :return and there is an :else then the
6191 // had_return flag is set.
6192 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006194 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195 return arg;
6196}
6197
6198/*
6199 * compile "for var in expr"
6200 *
6201 * Produces instructions:
6202 * PUSHNR -1
6203 * STORE loop-idx Set index to -1
6204 * EVAL expr Push result of "expr"
6205 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6206 * - if beyond end, jump to "end"
6207 * - otherwise get item from list and push it
6208 * STORE var Store item in "var"
6209 * ... body ...
6210 * JUMP top Jump back to repeat
6211 * end: DROP Drop the result of "expr"
6212 *
6213 */
6214 static char_u *
6215compile_for(char_u *arg, cctx_T *cctx)
6216{
6217 char_u *p;
6218 size_t varlen;
6219 garray_T *instr = &cctx->ctx_instr;
6220 garray_T *stack = &cctx->ctx_type_stack;
6221 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006222 lvar_T *loop_lvar; // loop iteration variable
6223 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224 type_T *vartype;
6225
6226 // TODO: list of variables: "for [key, value] in dict"
6227 // parse "var"
6228 for (p = arg; eval_isnamec1(*p); ++p)
6229 ;
6230 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006231 var_lvar = lookup_local(arg, varlen, cctx);
6232 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 {
6234 semsg(_("E1023: variable already defined: %s"), arg);
6235 return NULL;
6236 }
6237
6238 // consume "in"
6239 p = skipwhite(p);
6240 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6241 {
6242 emsg(_(e_missing_in));
6243 return NULL;
6244 }
6245 p = skipwhite(p + 2);
6246
6247
6248 scope = new_scope(cctx, FOR_SCOPE);
6249 if (scope == NULL)
6250 return NULL;
6251
6252 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006253 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6254 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006255 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006256 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006257 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006258 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260
6261 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006262 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6263 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006264 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006265 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006266 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006269
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006270 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271
6272 // compile "expr", it remains on the stack until "endfor"
6273 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006274 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006275 {
6276 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006278 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006280 // Now that we know the type of "var", check that it is a list, now or at
6281 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006283 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006285 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 return NULL;
6287 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006288 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006289 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290
6291 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006292 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006294 generate_FOR(cctx, loop_lvar->lv_idx);
6295 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296
6297 return arg;
6298}
6299
6300/*
6301 * compile "endfor"
6302 */
6303 static char_u *
6304compile_endfor(char_u *arg, cctx_T *cctx)
6305{
6306 garray_T *instr = &cctx->ctx_instr;
6307 scope_T *scope = cctx->ctx_scope;
6308 forscope_T *forscope;
6309 isn_T *isn;
6310
6311 if (scope == NULL || scope->se_type != FOR_SCOPE)
6312 {
6313 emsg(_(e_for));
6314 return NULL;
6315 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006316 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006318 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319
6320 // At end of ":for" scope jump back to the FOR instruction.
6321 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6322
6323 // Fill in the "end" label in the FOR statement so it can jump here
6324 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6325 isn->isn_arg.forloop.for_end = instr->ga_len;
6326
6327 // Fill in the "end" label any BREAK statements
6328 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6329
6330 // Below the ":for" scope drop the "expr" list from the stack.
6331 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6332 return NULL;
6333
6334 vim_free(scope);
6335
6336 return arg;
6337}
6338
6339/*
6340 * compile "while expr"
6341 *
6342 * Produces instructions:
6343 * top: EVAL expr Push result of "expr"
6344 * JUMP_IF_FALSE end jump if false
6345 * ... body ...
6346 * JUMP top Jump back to repeat
6347 * end:
6348 *
6349 */
6350 static char_u *
6351compile_while(char_u *arg, cctx_T *cctx)
6352{
6353 char_u *p = arg;
6354 garray_T *instr = &cctx->ctx_instr;
6355 scope_T *scope;
6356
6357 scope = new_scope(cctx, WHILE_SCOPE);
6358 if (scope == NULL)
6359 return NULL;
6360
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006361 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006362
6363 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006364 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006365 return NULL;
6366
6367 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006368 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006369 JUMP_IF_FALSE, cctx) == FAIL)
6370 return FAIL;
6371
6372 return p;
6373}
6374
6375/*
6376 * compile "endwhile"
6377 */
6378 static char_u *
6379compile_endwhile(char_u *arg, cctx_T *cctx)
6380{
6381 scope_T *scope = cctx->ctx_scope;
6382
6383 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6384 {
6385 emsg(_(e_while));
6386 return NULL;
6387 }
6388 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006389 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006390
6391 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006392 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006393
6394 // Fill in the "end" label in the WHILE statement so it can jump here.
6395 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006396 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006397
6398 vim_free(scope);
6399
6400 return arg;
6401}
6402
6403/*
6404 * compile "continue"
6405 */
6406 static char_u *
6407compile_continue(char_u *arg, cctx_T *cctx)
6408{
6409 scope_T *scope = cctx->ctx_scope;
6410
6411 for (;;)
6412 {
6413 if (scope == NULL)
6414 {
6415 emsg(_(e_continue));
6416 return NULL;
6417 }
6418 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6419 break;
6420 scope = scope->se_outer;
6421 }
6422
6423 // Jump back to the FOR or WHILE instruction.
6424 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006425 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6426 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427 return arg;
6428}
6429
6430/*
6431 * compile "break"
6432 */
6433 static char_u *
6434compile_break(char_u *arg, cctx_T *cctx)
6435{
6436 scope_T *scope = cctx->ctx_scope;
6437 endlabel_T **el;
6438
6439 for (;;)
6440 {
6441 if (scope == NULL)
6442 {
6443 emsg(_(e_break));
6444 return NULL;
6445 }
6446 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6447 break;
6448 scope = scope->se_outer;
6449 }
6450
6451 // Jump to the end of the FOR or WHILE loop.
6452 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006453 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006455 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006456 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6457 return FAIL;
6458
6459 return arg;
6460}
6461
6462/*
6463 * compile "{" start of block
6464 */
6465 static char_u *
6466compile_block(char_u *arg, cctx_T *cctx)
6467{
6468 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6469 return NULL;
6470 return skipwhite(arg + 1);
6471}
6472
6473/*
6474 * compile end of block: drop one scope
6475 */
6476 static void
6477compile_endblock(cctx_T *cctx)
6478{
6479 scope_T *scope = cctx->ctx_scope;
6480
6481 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006482 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483 vim_free(scope);
6484}
6485
6486/*
6487 * compile "try"
6488 * Creates a new scope for the try-endtry, pointing to the first catch and
6489 * finally.
6490 * Creates another scope for the "try" block itself.
6491 * TRY instruction sets up exception handling at runtime.
6492 *
6493 * "try"
6494 * TRY -> catch1, -> finally push trystack entry
6495 * ... try block
6496 * "throw {exception}"
6497 * EVAL {exception}
6498 * THROW create exception
6499 * ... try block
6500 * " catch {expr}"
6501 * JUMP -> finally
6502 * catch1: PUSH exeception
6503 * EVAL {expr}
6504 * MATCH
6505 * JUMP nomatch -> catch2
6506 * CATCH remove exception
6507 * ... catch block
6508 * " catch"
6509 * JUMP -> finally
6510 * catch2: CATCH remove exception
6511 * ... catch block
6512 * " finally"
6513 * finally:
6514 * ... finally block
6515 * " endtry"
6516 * ENDTRY pop trystack entry, may rethrow
6517 */
6518 static char_u *
6519compile_try(char_u *arg, cctx_T *cctx)
6520{
6521 garray_T *instr = &cctx->ctx_instr;
6522 scope_T *try_scope;
6523 scope_T *scope;
6524
6525 // scope that holds the jumps that go to catch/finally/endtry
6526 try_scope = new_scope(cctx, TRY_SCOPE);
6527 if (try_scope == NULL)
6528 return NULL;
6529
6530 // "catch" is set when the first ":catch" is found.
6531 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006532 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 if (generate_instr(cctx, ISN_TRY) == NULL)
6534 return NULL;
6535
6536 // scope for the try block itself
6537 scope = new_scope(cctx, BLOCK_SCOPE);
6538 if (scope == NULL)
6539 return NULL;
6540
6541 return arg;
6542}
6543
6544/*
6545 * compile "catch {expr}"
6546 */
6547 static char_u *
6548compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6549{
6550 scope_T *scope = cctx->ctx_scope;
6551 garray_T *instr = &cctx->ctx_instr;
6552 char_u *p;
6553 isn_T *isn;
6554
6555 // end block scope from :try or :catch
6556 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6557 compile_endblock(cctx);
6558 scope = cctx->ctx_scope;
6559
6560 // Error if not in a :try scope
6561 if (scope == NULL || scope->se_type != TRY_SCOPE)
6562 {
6563 emsg(_(e_catch));
6564 return NULL;
6565 }
6566
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006567 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006568 {
6569 emsg(_("E1033: catch unreachable after catch-all"));
6570 return NULL;
6571 }
6572
6573 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006574 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575 JUMP_ALWAYS, cctx) == FAIL)
6576 return NULL;
6577
6578 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006579 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 if (isn->isn_arg.try.try_catch == 0)
6581 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006582 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583 {
6584 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006585 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 isn->isn_arg.jump.jump_where = instr->ga_len;
6587 }
6588
6589 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006590 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006592 scope->se_u.se_try.ts_caught_all = TRUE;
6593 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 }
6595 else
6596 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006597 char_u *end;
6598 char_u *pat;
6599 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006600 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006601 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 // Push v:exception, push {expr} and MATCH
6604 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6605
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006606 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006607 if (*end != *p)
6608 {
6609 semsg(_("E1067: Separator mismatch: %s"), p);
6610 vim_free(tofree);
6611 return FAIL;
6612 }
6613 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006614 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006615 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006616 len = (int)(end - tofree);
6617 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006618 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006619 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006620 if (pat == NULL)
6621 return FAIL;
6622 if (generate_PUSHS(cctx, pat) == FAIL)
6623 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6626 return NULL;
6627
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006628 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6630 return NULL;
6631 }
6632
6633 if (generate_instr(cctx, ISN_CATCH) == NULL)
6634 return NULL;
6635
6636 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6637 return NULL;
6638 return p;
6639}
6640
6641 static char_u *
6642compile_finally(char_u *arg, cctx_T *cctx)
6643{
6644 scope_T *scope = cctx->ctx_scope;
6645 garray_T *instr = &cctx->ctx_instr;
6646 isn_T *isn;
6647
6648 // end block scope from :try or :catch
6649 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6650 compile_endblock(cctx);
6651 scope = cctx->ctx_scope;
6652
6653 // Error if not in a :try scope
6654 if (scope == NULL || scope->se_type != TRY_SCOPE)
6655 {
6656 emsg(_(e_finally));
6657 return NULL;
6658 }
6659
6660 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006661 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006662 if (isn->isn_arg.try.try_finally != 0)
6663 {
6664 emsg(_(e_finally_dup));
6665 return NULL;
6666 }
6667
6668 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006669 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006670
Bram Moolenaar585fea72020-04-02 22:33:21 +02006671 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006672 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006673 {
6674 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006675 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006677 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 }
6679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006680 // TODO: set index in ts_finally_label jumps
6681
6682 return arg;
6683}
6684
6685 static char_u *
6686compile_endtry(char_u *arg, cctx_T *cctx)
6687{
6688 scope_T *scope = cctx->ctx_scope;
6689 garray_T *instr = &cctx->ctx_instr;
6690 isn_T *isn;
6691
6692 // end block scope from :catch or :finally
6693 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6694 compile_endblock(cctx);
6695 scope = cctx->ctx_scope;
6696
6697 // Error if not in a :try scope
6698 if (scope == NULL || scope->se_type != TRY_SCOPE)
6699 {
6700 if (scope == NULL)
6701 emsg(_(e_no_endtry));
6702 else if (scope->se_type == WHILE_SCOPE)
6703 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006704 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006705 emsg(_(e_endfor));
6706 else
6707 emsg(_(e_endif));
6708 return NULL;
6709 }
6710
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006711 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6713 {
6714 emsg(_("E1032: missing :catch or :finally"));
6715 return NULL;
6716 }
6717
6718 // Fill in the "end" label in jumps at the end of the blocks, if not done
6719 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006720 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721
6722 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006723 if (isn->isn_arg.try.try_catch == 0)
6724 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725 if (isn->isn_arg.try.try_finally == 0)
6726 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006727
6728 if (scope->se_u.se_try.ts_catch_label != 0)
6729 {
6730 // Last catch without match jumps here
6731 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6732 isn->isn_arg.jump.jump_where = instr->ga_len;
6733 }
6734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006735 compile_endblock(cctx);
6736
6737 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6738 return NULL;
6739 return arg;
6740}
6741
6742/*
6743 * compile "throw {expr}"
6744 */
6745 static char_u *
6746compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6747{
6748 char_u *p = skipwhite(arg);
6749
Bram Moolenaara5565e42020-05-09 15:44:01 +02006750 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751 return NULL;
6752 if (may_generate_2STRING(-1, cctx) == FAIL)
6753 return NULL;
6754 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6755 return NULL;
6756
6757 return p;
6758}
6759
6760/*
6761 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006762 * compile "echomsg expr"
6763 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006764 * compile "execute expr"
6765 */
6766 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006767compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006768{
6769 char_u *p = arg;
6770 int count = 0;
6771
6772 for (;;)
6773 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006774 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006775 return NULL;
6776 ++count;
6777 p = skipwhite(p);
6778 if (ends_excmd(*p))
6779 break;
6780 }
6781
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006782 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6783 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6784 else if (cmdidx == CMD_execute)
6785 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6786 else if (cmdidx == CMD_echomsg)
6787 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6788 else
6789 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790 return p;
6791}
6792
6793/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006794 * A command that is not compiled, execute with legacy code.
6795 */
6796 static char_u *
6797compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6798{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006799 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006800 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006801 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006802
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006803 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006804 goto theend;
6805
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006806 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006807 {
6808 long argt = excmd_get_argt(eap->cmdidx);
6809 int usefilter = FALSE;
6810
6811 has_expr = argt & (EX_XFILE | EX_EXPAND);
6812
6813 // If the command can be followed by a bar, find the bar and truncate
6814 // it, so that the following command can be compiled.
6815 // The '|' is overwritten with a NUL, it is put back below.
6816 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6817 && *eap->arg == '!')
6818 // :w !filter or :r !filter or :r! filter
6819 usefilter = TRUE;
6820 if ((argt & EX_TRLBAR) && !usefilter)
6821 {
6822 separate_nextcmd(eap);
6823 if (eap->nextcmd != NULL)
6824 nextcmd = eap->nextcmd;
6825 }
6826 }
6827
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006828 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6829 {
6830 // expand filename in "syntax include [@group] filename"
6831 has_expr = TRUE;
6832 eap->arg = skipwhite(eap->arg + 7);
6833 if (*eap->arg == '@')
6834 eap->arg = skiptowhite(eap->arg);
6835 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006836
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006837 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006838 {
6839 int count = 0;
6840 char_u *start = skipwhite(line);
6841
6842 // :cmd xxx`=expr1`yyy`=expr2`zzz
6843 // PUSHS ":cmd xxx"
6844 // eval expr1
6845 // PUSHS "yyy"
6846 // eval expr2
6847 // PUSHS "zzz"
6848 // EXECCONCAT 5
6849 for (;;)
6850 {
6851 if (p > start)
6852 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006853 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006854 ++count;
6855 }
6856 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006857 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006858 return NULL;
6859 may_generate_2STRING(-1, cctx);
6860 ++count;
6861 p = skipwhite(p);
6862 if (*p != '`')
6863 {
6864 emsg(_("E1083: missing backtick"));
6865 return NULL;
6866 }
6867 start = p + 1;
6868
6869 p = (char_u *)strstr((char *)start, "`=");
6870 if (p == NULL)
6871 {
6872 if (*skipwhite(start) != NUL)
6873 {
6874 generate_PUSHS(cctx, vim_strsave(start));
6875 ++count;
6876 }
6877 break;
6878 }
6879 }
6880 generate_EXECCONCAT(cctx, count);
6881 }
6882 else
6883 generate_EXEC(cctx, line);
6884
6885theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006886 if (*nextcmd != NUL)
6887 {
6888 // the parser expects a pointer to the bar, put it back
6889 --nextcmd;
6890 *nextcmd = '|';
6891 }
6892
6893 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006894}
6895
6896/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006897 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006898 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006899 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006900 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006901add_def_function(ufunc_T *ufunc)
6902{
6903 dfunc_T *dfunc;
6904
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006905 if (def_functions.ga_len == 0)
6906 {
6907 // The first position is not used, so that a zero uf_dfunc_idx means it
6908 // wasn't set.
6909 if (ga_grow(&def_functions, 1) == FAIL)
6910 return FAIL;
6911 ++def_functions.ga_len;
6912 }
6913
Bram Moolenaar09689a02020-05-09 22:50:08 +02006914 // Add the function to "def_functions".
6915 if (ga_grow(&def_functions, 1) == FAIL)
6916 return FAIL;
6917 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6918 CLEAR_POINTER(dfunc);
6919 dfunc->df_idx = def_functions.ga_len;
6920 ufunc->uf_dfunc_idx = dfunc->df_idx;
6921 dfunc->df_ufunc = ufunc;
6922 ++def_functions.ga_len;
6923 return OK;
6924}
6925
6926/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 * After ex_function() has collected all the function lines: parse and compile
6928 * the lines into instructions.
6929 * Adds the function to "def_functions".
6930 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6931 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006932 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006933 * This can be used recursively through compile_lambda(), which may reallocate
6934 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006935 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006937 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006938compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006939{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940 char_u *line = NULL;
6941 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943 cctx_T cctx;
6944 garray_T *instr;
6945 int called_emsg_before = called_emsg;
6946 int ret = FAIL;
6947 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006948 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006949 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006950 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006951
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006952 // When using a function that was compiled before: Free old instructions.
6953 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006954 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006956 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6957 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006958 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006960 else
6961 {
6962 if (add_def_function(ufunc) == FAIL)
6963 return FAIL;
6964 new_def_function = TRUE;
6965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966
Bram Moolenaar985116a2020-07-12 17:31:09 +02006967 ufunc->uf_def_status = UF_COMPILING;
6968
Bram Moolenaara80faa82020-04-12 19:37:17 +02006969 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970 cctx.ctx_ufunc = ufunc;
6971 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006972 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6974 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6975 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6976 cctx.ctx_type_list = &ufunc->uf_type_list;
6977 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6978 instr = &cctx.ctx_instr;
6979
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006980 // Set the context to the function, it may be compiled when called from
6981 // another script. Set the script version to the most modern one.
6982 // The line number will be set in next_line_from_context().
6983 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6985
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006986 // Make sure error messages are OK.
6987 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6988 if (do_estack_push)
6989 estack_push_ufunc(ufunc, 1);
6990
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006991 if (ufunc->uf_def_args.ga_len > 0)
6992 {
6993 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006994 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006995 int i;
6996 char_u *arg;
6997 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6998
6999 // Produce instructions for the default values of optional arguments.
7000 // Store the instruction index in uf_def_arg_idx[] so that we know
7001 // where to start when the function is called, depending on the number
7002 // of arguments.
7003 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7004 if (ufunc->uf_def_arg_idx == NULL)
7005 goto erret;
7006 for (i = 0; i < count; ++i)
7007 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007008 garray_T *stack = &cctx.ctx_type_stack;
7009 type_T *val_type;
7010 int arg_idx = first_def_arg + i;
7011
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007012 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7013 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007014 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007015 goto erret;
7016
7017 // If no type specified use the type of the default value.
7018 // Otherwise check that the default value type matches the
7019 // specified type.
7020 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7021 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
7022 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaare30f64b2020-07-15 19:48:20 +02007023 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007024 == FAIL)
7025 {
7026 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
7027 arg_idx + 1);
7028 goto erret;
7029 }
7030
7031 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007032 goto erret;
7033 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007034 ufunc->uf_def_arg_idx[count] = instr->ga_len;
7035 }
7036
7037 /*
7038 * Loop over all the lines of the function and generate instructions.
7039 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 for (;;)
7041 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007042 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007043 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007044 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007045 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007046
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007047 // Bail out on the first error to avoid a flood of errors and report
7048 // the right line number when inside try/catch.
7049 if (emsg_before != called_emsg)
7050 goto erret;
7051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052 if (line != NULL && *line == '|')
7053 // the line continues after a '|'
7054 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007055 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007056 && !(*line == '#' && (line == cctx.ctx_line_start
7057 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007059 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 goto erret;
7061 }
7062 else
7063 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007064 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007065 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007066 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007067 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007069 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007070
Bram Moolenaara80faa82020-04-12 19:37:17 +02007071 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 ea.cmdlinep = &line;
7073 ea.cmd = skipwhite(line);
7074
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007075 // Some things can be recognized by the first character.
7076 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007078 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007079 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007080 if (ea.cmd[1] != '{')
7081 {
7082 line = (char_u *)"";
7083 continue;
7084 }
7085 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007086
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007087 case '}':
7088 {
7089 // "}" ends a block scope
7090 scopetype_T stype = cctx.ctx_scope == NULL
7091 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007093 if (stype == BLOCK_SCOPE)
7094 {
7095 compile_endblock(&cctx);
7096 line = ea.cmd;
7097 }
7098 else
7099 {
7100 emsg(_("E1025: using } outside of a block scope"));
7101 goto erret;
7102 }
7103 if (line != NULL)
7104 line = skipwhite(ea.cmd + 1);
7105 continue;
7106 }
7107
7108 case '{':
7109 // "{" starts a block scope
7110 // "{'a': 1}->func() is something else
7111 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7112 {
7113 line = compile_block(ea.cmd, &cctx);
7114 continue;
7115 }
7116 break;
7117
7118 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007119 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007120 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 }
7122
7123 /*
7124 * COMMAND MODIFIERS
7125 */
7126 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7127 {
7128 if (errormsg != NULL)
7129 goto erret;
7130 // empty line or comment
7131 line = (char_u *)"";
7132 continue;
7133 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007134 // TODO: use modifiers in the command
7135 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007136 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007137
7138 // Skip ":call" to get to the function name.
7139 if (checkforcmd(&ea.cmd, "call", 3))
7140 ea.cmd = skipwhite(ea.cmd);
7141
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007142 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007144 char_u *pskip;
7145
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007146 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007147 // find what follows.
7148 // Skip over "var.member", "var[idx]" and the like.
7149 // Also "&opt = val", "$ENV = val" and "@r = val".
7150 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007151 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007152 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007153 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007155 char_u *var_end;
7156 int oplen;
7157 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007159 var_end = find_name_end(pskip, NULL, NULL,
7160 FNE_CHECK_START | FNE_INCL_BR);
7161 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007162 if (oplen > 0)
7163 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007164 size_t len = p - ea.cmd;
7165
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007166 // Recognize an assignment if we recognize the variable
7167 // name:
7168 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007169 // "local = expr" where "local" is a local var.
7170 // "script = expr" where "script" is a script-local var.
7171 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007172 // "&opt = expr"
7173 // "$ENV = expr"
7174 // "@r = expr"
7175 if (*ea.cmd == '&'
7176 || *ea.cmd == '$'
7177 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007178 || ((len) > 2 && ea.cmd[1] == ':')
7179 || lookup_local(ea.cmd, len, &cctx) != NULL
7180 || lookup_arg(ea.cmd, len, NULL, NULL,
7181 NULL, &cctx) == OK
7182 || lookup_script(ea.cmd, len) == OK
7183 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007184 {
7185 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007186 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007187 goto erret;
7188 continue;
7189 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190 }
7191 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007192
7193 if (*ea.cmd == '[')
7194 {
7195 // [var, var] = expr
7196 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7197 if (line == NULL)
7198 goto erret;
7199 if (line != ea.cmd)
7200 continue;
7201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 }
7203
7204 /*
7205 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007206 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007207 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007208 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007209 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007210 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007211 ea.cmd = skip_range(ea.cmd, NULL);
7212 if (ea.cmd > cmd && !starts_with_colon)
7213 {
7214 emsg(_(e_colon_required));
7215 goto erret;
7216 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007217 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007218 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007219 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007220 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007221
7222 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7223 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007224 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007225 {
7226 line += STRLEN(line);
7227 continue;
7228 }
7229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007230 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007231 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007233 // CMD_let cannot happen, compile_assignment() above is used
7234 iemsg("Command from find_ex_command() not handled");
7235 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007237 }
7238
7239 p = skipwhite(p);
7240
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007241 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007242 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007243 && ea.cmdidx != CMD_elseif
7244 && ea.cmdidx != CMD_else
7245 && ea.cmdidx != CMD_endif)
7246 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007247 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007248 continue;
7249 }
7250
Bram Moolenaarefd88552020-06-18 20:50:10 +02007251 if (ea.cmdidx != CMD_elseif
7252 && ea.cmdidx != CMD_else
7253 && ea.cmdidx != CMD_endif
7254 && ea.cmdidx != CMD_endfor
7255 && ea.cmdidx != CMD_endwhile
7256 && ea.cmdidx != CMD_catch
7257 && ea.cmdidx != CMD_finally
7258 && ea.cmdidx != CMD_endtry)
7259 {
7260 if (cctx.ctx_had_return)
7261 {
7262 emsg(_("E1095: Unreachable code after :return"));
7263 goto erret;
7264 }
7265 }
7266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007267 switch (ea.cmdidx)
7268 {
7269 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007270 ea.arg = p;
7271 line = compile_nested_function(&ea, &cctx);
7272 break;
7273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007275 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276 goto erret;
7277
7278 case CMD_return:
7279 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007280 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 break;
7282
7283 case CMD_let:
7284 case CMD_const:
7285 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007286 if (line == p)
7287 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007288 break;
7289
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007290 case CMD_unlet:
7291 case CMD_unlockvar:
7292 case CMD_lockvar:
7293 line = compile_unletlock(p, &ea, &cctx);
7294 break;
7295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007296 case CMD_import:
7297 line = compile_import(p, &cctx);
7298 break;
7299
7300 case CMD_if:
7301 line = compile_if(p, &cctx);
7302 break;
7303 case CMD_elseif:
7304 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007305 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306 break;
7307 case CMD_else:
7308 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007309 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 break;
7311 case CMD_endif:
7312 line = compile_endif(p, &cctx);
7313 break;
7314
7315 case CMD_while:
7316 line = compile_while(p, &cctx);
7317 break;
7318 case CMD_endwhile:
7319 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007320 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 break;
7322
7323 case CMD_for:
7324 line = compile_for(p, &cctx);
7325 break;
7326 case CMD_endfor:
7327 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007328 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007329 break;
7330 case CMD_continue:
7331 line = compile_continue(p, &cctx);
7332 break;
7333 case CMD_break:
7334 line = compile_break(p, &cctx);
7335 break;
7336
7337 case CMD_try:
7338 line = compile_try(p, &cctx);
7339 break;
7340 case CMD_catch:
7341 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007342 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007343 break;
7344 case CMD_finally:
7345 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007346 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347 break;
7348 case CMD_endtry:
7349 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007350 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 break;
7352 case CMD_throw:
7353 line = compile_throw(p, &cctx);
7354 break;
7355
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007356 case CMD_eval:
7357 if (compile_expr0(&p, &cctx) == FAIL)
7358 goto erret;
7359
7360 // drop the return value
7361 generate_instr_drop(&cctx, ISN_DROP, 1);
7362
7363 line = skipwhite(p);
7364 break;
7365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007368 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007369 case CMD_echomsg:
7370 case CMD_echoerr:
7371 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007372 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007374 // TODO: other commands with an expression argument
7375
Bram Moolenaar002262f2020-07-08 17:47:57 +02007376 case CMD_SIZE:
7377 semsg(_("E476: Invalid command: %s"), ea.cmd);
7378 goto erret;
7379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007381 // Not recognized, execute with do_cmdline_cmd().
7382 ea.arg = p;
7383 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384 break;
7385 }
7386 if (line == NULL)
7387 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007388 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007389
7390 if (cctx.ctx_type_stack.ga_len < 0)
7391 {
7392 iemsg("Type stack underflow");
7393 goto erret;
7394 }
7395 }
7396
7397 if (cctx.ctx_scope != NULL)
7398 {
7399 if (cctx.ctx_scope->se_type == IF_SCOPE)
7400 emsg(_(e_endif));
7401 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7402 emsg(_(e_endwhile));
7403 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7404 emsg(_(e_endfor));
7405 else
7406 emsg(_("E1026: Missing }"));
7407 goto erret;
7408 }
7409
Bram Moolenaarefd88552020-06-18 20:50:10 +02007410 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411 {
7412 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7413 {
7414 emsg(_("E1027: Missing return statement"));
7415 goto erret;
7416 }
7417
7418 // Return zero if there is no return at the end.
7419 generate_PUSHNR(&cctx, 0);
7420 generate_instr(&cctx, ISN_RETURN);
7421 }
7422
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007423 {
7424 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7425 + ufunc->uf_dfunc_idx;
7426 dfunc->df_deleted = FALSE;
7427 dfunc->df_instr = instr->ga_data;
7428 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007429 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007430 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007431 if (cctx.ctx_outer_used)
7432 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007433 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435
7436 ret = OK;
7437
7438erret:
7439 if (ret == FAIL)
7440 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007441 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007442 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7443 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007444
7445 for (idx = 0; idx < instr->ga_len; ++idx)
7446 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007448
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007449 // If using the last entry in the table and it was added above, we
7450 // might as well remove it.
7451 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007452 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007453 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007454 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007455 ufunc->uf_dfunc_idx = 0;
7456 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007457 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007458
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007459 while (cctx.ctx_scope != NULL)
7460 drop_scope(&cctx);
7461
Bram Moolenaar20431c92020-03-20 18:39:46 +01007462 // Don't execute this function body.
7463 ga_clear_strings(&ufunc->uf_lines);
7464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007465 if (errormsg != NULL)
7466 emsg(errormsg);
7467 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007468 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469 }
7470
7471 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007472 if (do_estack_push)
7473 estack_pop();
7474
Bram Moolenaar20431c92020-03-20 18:39:46 +01007475 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007476 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007478 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479}
7480
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007481 void
7482set_function_type(ufunc_T *ufunc)
7483{
7484 int varargs = ufunc->uf_va_name != NULL;
7485 int argcount = ufunc->uf_args.ga_len;
7486
7487 // Create a type for the function, with the return type and any
7488 // argument types.
7489 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7490 // The type is included in "tt_args".
7491 if (argcount > 0 || varargs)
7492 {
7493 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7494 argcount, &ufunc->uf_type_list);
7495 // Add argument types to the function type.
7496 if (func_type_add_arg_types(ufunc->uf_func_type,
7497 argcount + varargs,
7498 &ufunc->uf_type_list) == FAIL)
7499 return;
7500 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7501 ufunc->uf_func_type->tt_min_argcount =
7502 argcount - ufunc->uf_def_args.ga_len;
7503 if (ufunc->uf_arg_types == NULL)
7504 {
7505 int i;
7506
7507 // lambda does not have argument types.
7508 for (i = 0; i < argcount; ++i)
7509 ufunc->uf_func_type->tt_args[i] = &t_any;
7510 }
7511 else
7512 mch_memmove(ufunc->uf_func_type->tt_args,
7513 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7514 if (varargs)
7515 {
7516 ufunc->uf_func_type->tt_args[argcount] =
7517 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7518 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7519 }
7520 }
7521 else
7522 // No arguments, can use a predefined type.
7523 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7524 argcount, &ufunc->uf_type_list);
7525}
7526
7527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007528/*
7529 * Delete an instruction, free what it contains.
7530 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007531 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532delete_instr(isn_T *isn)
7533{
7534 switch (isn->isn_type)
7535 {
7536 case ISN_EXEC:
7537 case ISN_LOADENV:
7538 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007539 case ISN_LOADB:
7540 case ISN_LOADW:
7541 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007542 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007543 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007544 case ISN_PUSHEXC:
7545 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007546 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007547 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007548 case ISN_STOREB:
7549 case ISN_STOREW:
7550 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007551 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552 vim_free(isn->isn_arg.string);
7553 break;
7554
7555 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007556 case ISN_STORES:
7557 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007558 break;
7559
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007560 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007561 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007562 vim_free(isn->isn_arg.unlet.ul_name);
7563 break;
7564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565 case ISN_STOREOPT:
7566 vim_free(isn->isn_arg.storeopt.so_name);
7567 break;
7568
7569 case ISN_PUSHBLOB: // push blob isn_arg.blob
7570 blob_unref(isn->isn_arg.blob);
7571 break;
7572
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007573 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007574#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007575 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007576#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007577 break;
7578
7579 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007580#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007581 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007582#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007583 break;
7584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585 case ISN_UCALL:
7586 vim_free(isn->isn_arg.ufunc.cuf_name);
7587 break;
7588
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007589 case ISN_FUNCREF:
7590 {
7591 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7592 + isn->isn_arg.funcref.fr_func;
7593 func_ptr_unref(dfunc->df_ufunc);
7594 }
7595 break;
7596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007597 case ISN_2BOOL:
7598 case ISN_2STRING:
7599 case ISN_ADDBLOB:
7600 case ISN_ADDLIST:
7601 case ISN_BCALL:
7602 case ISN_CATCH:
7603 case ISN_CHECKNR:
7604 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007605 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007606 case ISN_COMPAREANY:
7607 case ISN_COMPAREBLOB:
7608 case ISN_COMPAREBOOL:
7609 case ISN_COMPAREDICT:
7610 case ISN_COMPAREFLOAT:
7611 case ISN_COMPAREFUNC:
7612 case ISN_COMPARELIST:
7613 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007614 case ISN_COMPARESPECIAL:
7615 case ISN_COMPARESTRING:
7616 case ISN_CONCAT:
7617 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007618 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007619 case ISN_DROP:
7620 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007621 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007622 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007623 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007624 case ISN_EXECCONCAT:
7625 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007627 case ISN_LISTINDEX:
7628 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007629 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007630 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007631 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007632 case ISN_JUMP:
7633 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007634 case ISN_LOADBDICT:
7635 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007636 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007637 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007638 case ISN_LOADSCRIPT:
7639 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007641 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 case ISN_NEGATENR:
7643 case ISN_NEWDICT:
7644 case ISN_NEWLIST:
7645 case ISN_OPNR:
7646 case ISN_OPFLOAT:
7647 case ISN_OPANY:
7648 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007649 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007650 case ISN_PUSHF:
7651 case ISN_PUSHNR:
7652 case ISN_PUSHBOOL:
7653 case ISN_PUSHSPEC:
7654 case ISN_RETURN:
7655 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007656 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007657 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007658 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007659 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007661 case ISN_STOREDICT:
7662 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007663 case ISN_THROW:
7664 case ISN_TRY:
7665 // nothing allocated
7666 break;
7667 }
7668}
7669
7670/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007671 * Free all instructions for "dfunc".
7672 */
7673 static void
7674delete_def_function_contents(dfunc_T *dfunc)
7675{
7676 int idx;
7677
7678 ga_clear(&dfunc->df_def_args_isn);
7679
7680 if (dfunc->df_instr != NULL)
7681 {
7682 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7683 delete_instr(dfunc->df_instr + idx);
7684 VIM_CLEAR(dfunc->df_instr);
7685 }
7686
7687 dfunc->df_deleted = TRUE;
7688}
7689
7690/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007691 * When a user function is deleted, clear the contents of any associated def
7692 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007693 */
7694 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007695clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007696{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007697 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007698 {
7699 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7700 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007701
Bram Moolenaar20431c92020-03-20 18:39:46 +01007702 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007703 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704 }
7705}
7706
7707#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007708/*
7709 * Free all functions defined with ":def".
7710 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007711 void
7712free_def_functions(void)
7713{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007714 int idx;
7715
7716 for (idx = 0; idx < def_functions.ga_len; ++idx)
7717 {
7718 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7719
7720 delete_def_function_contents(dfunc);
7721 }
7722
7723 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007724}
7725#endif
7726
7727
7728#endif // FEAT_EVAL