blob: 999833b5221d530d8c4469a5d0731bbb0a581596 [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 Moolenaara8c17702020-04-01 21:17:24 +0200481 * Return the type_T for a typval. Only for primitive types.
482 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200483 type_T *
Bram Moolenaara8c17702020-04-01 21:17:24 +0200484typval2type(typval_T *tv)
485{
486 if (tv->v_type == VAR_NUMBER)
487 return &t_number;
488 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200489 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200490 if (tv->v_type == VAR_STRING)
491 return &t_string;
492 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
493 return &t_list_string;
494 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
495 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200496 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200497}
498
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200499 static void
500type_mismatch(type_T *expected, type_T *actual)
501{
502 char *tofree1, *tofree2;
503
504 semsg(_("E1013: type mismatch, expected %s but got %s"),
505 type_name(expected, &tofree1), type_name(actual, &tofree2));
506 vim_free(tofree1);
507 vim_free(tofree2);
508}
509
510 static void
511arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
512{
513 char *tofree1, *tofree2;
514
515 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
516 argidx,
517 type_name(expected, &tofree1), type_name(actual, &tofree2));
518 vim_free(tofree1);
519 vim_free(tofree2);
520}
521
522/*
523 * Check if the expected and actual types match.
524 * Does not allow for assigning "any" to a specific type.
525 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200526 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200527check_type(type_T *expected, type_T *actual, int give_msg)
528{
529 int ret = OK;
530
531 // When expected is "unknown" we accept any actual type.
532 // When expected is "any" we accept any actual type except "void".
533 if (expected->tt_type != VAR_UNKNOWN
534 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
535
536 {
537 if (expected->tt_type != actual->tt_type)
538 {
539 if (give_msg)
540 type_mismatch(expected, actual);
541 return FAIL;
542 }
543 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
544 {
545 // "unknown" is used for an empty list or dict
546 if (actual->tt_member != &t_unknown)
547 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
548 }
549 else if (expected->tt_type == VAR_FUNC)
550 {
551 if (expected->tt_member != &t_unknown)
552 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
553 if (ret == OK && expected->tt_argcount != -1
554 && (actual->tt_argcount < expected->tt_min_argcount
555 || actual->tt_argcount > expected->tt_argcount))
556 ret = FAIL;
557 }
558 if (ret == FAIL && give_msg)
559 type_mismatch(expected, actual);
560 }
561 return ret;
562}
563
Bram Moolenaar65b95452020-07-19 14:03:09 +0200564/*
565 * Return FAIl if "expected" and "actual" don't match.
566 * TODO: better type comparison
567 */
568 int
569check_argtype(type_T *expected, typval_T *actual_tv)
570{
571 type_T actual;
572 type_T member;
573
574 // TODO: should should be done with more levels
575 CLEAR_FIELD(actual);
576 actual.tt_type = actual_tv->v_type;
577 if (actual_tv->v_type == VAR_LIST
578 && actual_tv->vval.v_list != NULL
579 && actual_tv->vval.v_list->lv_first != NULL)
580 {
581 // Use the type of the first member, it is the most specific.
582 CLEAR_FIELD(member);
583 member.tt_type = actual_tv->vval.v_list->lv_first->li_tv.v_type;
584 member.tt_member = &t_any;
585 actual.tt_member = &member;
586 }
587 else if (actual_tv->v_type == VAR_DICT
588 && actual_tv->vval.v_dict != NULL
589 && actual_tv->vval.v_dict->dv_hashtab.ht_used > 0)
590 {
591 dict_iterator_T iter;
592 typval_T *value;
593
594 // Use the type of the first value, it is the most specific.
595 dict_iterate_start(actual_tv, &iter);
596 dict_iterate_next(&iter, &value);
597 CLEAR_FIELD(member);
598 member.tt_type = value->v_type;
599 member.tt_member = &t_any;
600 actual.tt_member = &member;
601 }
Bram Moolenaar0f60e802020-07-22 20:16:11 +0200602 else if (actual_tv->v_type == VAR_FUNC || actual_tv->v_type == VAR_PARTIAL)
603 {
604 char_u *name = NULL;
605 ufunc_T *ufunc = NULL;
606
607 if (actual_tv->v_type == VAR_PARTIAL)
608 {
609 if (actual_tv->vval.v_partial->pt_func != NULL)
610 ufunc = actual_tv->vval.v_partial->pt_func;
611 else
612 name = actual_tv->vval.v_partial->pt_name;
613 }
614 else
615 name = actual_tv->vval.v_string;
616 if (name != NULL)
617 // TODO: how about a builtin function?
618 ufunc = find_func(name, FALSE, NULL);
619 if (ufunc != NULL && ufunc->uf_func_type != NULL)
620 actual = *ufunc->uf_func_type;
621 else
622 actual.tt_member = &t_any;
623 }
Bram Moolenaar65b95452020-07-19 14:03:09 +0200624 else
625 actual.tt_member = &t_any;
626 return check_type(expected, &actual, TRUE);
627}
628
629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100630/////////////////////////////////////////////////////////////////////
631// Following generate_ functions expect the caller to call ga_grow().
632
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200633#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
634#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636/*
637 * Generate an instruction without arguments.
638 * Returns a pointer to the new instruction, NULL if failed.
639 */
640 static isn_T *
641generate_instr(cctx_T *cctx, isntype_T isn_type)
642{
643 garray_T *instr = &cctx->ctx_instr;
644 isn_T *isn;
645
Bram Moolenaar080457c2020-03-03 21:53:32 +0100646 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647 if (ga_grow(instr, 1) == FAIL)
648 return NULL;
649 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
650 isn->isn_type = isn_type;
651 isn->isn_lnum = cctx->ctx_lnum + 1;
652 ++instr->ga_len;
653
654 return isn;
655}
656
657/*
658 * Generate an instruction without arguments.
659 * "drop" will be removed from the stack.
660 * Returns a pointer to the new instruction, NULL if failed.
661 */
662 static isn_T *
663generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
664{
665 garray_T *stack = &cctx->ctx_type_stack;
666
Bram Moolenaar080457c2020-03-03 21:53:32 +0100667 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668 stack->ga_len -= drop;
669 return generate_instr(cctx, isn_type);
670}
671
672/*
673 * Generate instruction "isn_type" and put "type" on the type stack.
674 */
675 static isn_T *
676generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
677{
678 isn_T *isn;
679 garray_T *stack = &cctx->ctx_type_stack;
680
681 if ((isn = generate_instr(cctx, isn_type)) == NULL)
682 return NULL;
683
684 if (ga_grow(stack, 1) == FAIL)
685 return NULL;
686 ((type_T **)stack->ga_data)[stack->ga_len] = type;
687 ++stack->ga_len;
688
689 return isn;
690}
691
692/*
693 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
694 */
695 static int
696may_generate_2STRING(int offset, cctx_T *cctx)
697{
698 isn_T *isn;
699 garray_T *stack = &cctx->ctx_type_stack;
700 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
701
702 if ((*type)->tt_type == VAR_STRING)
703 return OK;
704 *type = &t_string;
705
706 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
707 return FAIL;
708 isn->isn_arg.number = offset;
709
710 return OK;
711}
712
713 static int
714check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
715{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200716 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200718 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719 {
720 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200721 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 else
723 semsg(_("E1036: %c requires number or float arguments"), *op);
724 return FAIL;
725 }
726 return OK;
727}
728
729/*
730 * Generate an instruction with two arguments. The instruction depends on the
731 * type of the arguments.
732 */
733 static int
734generate_two_op(cctx_T *cctx, char_u *op)
735{
736 garray_T *stack = &cctx->ctx_type_stack;
737 type_T *type1;
738 type_T *type2;
739 vartype_T vartype;
740 isn_T *isn;
741
Bram Moolenaar080457c2020-03-03 21:53:32 +0100742 RETURN_OK_IF_SKIP(cctx);
743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744 // Get the known type of the two items on the stack. If they are matching
745 // use a type-specific instruction. Otherwise fall back to runtime type
746 // checking.
747 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
748 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200749 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 if (type1->tt_type == type2->tt_type
751 && (type1->tt_type == VAR_NUMBER
752 || type1->tt_type == VAR_LIST
753#ifdef FEAT_FLOAT
754 || type1->tt_type == VAR_FLOAT
755#endif
756 || type1->tt_type == VAR_BLOB))
757 vartype = type1->tt_type;
758
759 switch (*op)
760 {
761 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200762 && type1->tt_type != VAR_ANY
763 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 && check_number_or_float(
765 type1->tt_type, type2->tt_type, op) == FAIL)
766 return FAIL;
767 isn = generate_instr_drop(cctx,
768 vartype == VAR_NUMBER ? ISN_OPNR
769 : vartype == VAR_LIST ? ISN_ADDLIST
770 : vartype == VAR_BLOB ? ISN_ADDBLOB
771#ifdef FEAT_FLOAT
772 : vartype == VAR_FLOAT ? ISN_OPFLOAT
773#endif
774 : ISN_OPANY, 1);
775 if (isn != NULL)
776 isn->isn_arg.op.op_type = EXPR_ADD;
777 break;
778
779 case '-':
780 case '*':
781 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
782 op) == FAIL)
783 return FAIL;
784 if (vartype == VAR_NUMBER)
785 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
786#ifdef FEAT_FLOAT
787 else if (vartype == VAR_FLOAT)
788 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
789#endif
790 else
791 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
792 if (isn != NULL)
793 isn->isn_arg.op.op_type = *op == '*'
794 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
795 break;
796
Bram Moolenaar4c683752020-04-05 21:38:23 +0200797 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200799 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 && type2->tt_type != VAR_NUMBER))
801 {
802 emsg(_("E1035: % requires number arguments"));
803 return FAIL;
804 }
805 isn = generate_instr_drop(cctx,
806 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
807 if (isn != NULL)
808 isn->isn_arg.op.op_type = EXPR_REM;
809 break;
810 }
811
812 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200813 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814 {
815 type_T *type = &t_any;
816
817#ifdef FEAT_FLOAT
818 // float+number and number+float results in float
819 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
820 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
821 type = &t_float;
822#endif
823 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
824 }
825
826 return OK;
827}
828
829/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200830 * Get the instruction to use for comparing "type1" with "type2"
831 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200833 static isntype_T
834get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835{
836 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837
Bram Moolenaar4c683752020-04-05 21:38:23 +0200838 if (type1 == VAR_UNKNOWN)
839 type1 = VAR_ANY;
840 if (type2 == VAR_UNKNOWN)
841 type2 = VAR_ANY;
842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 if (type1 == type2)
844 {
845 switch (type1)
846 {
847 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
848 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
849 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
850 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
851 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
852 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
853 case VAR_LIST: isntype = ISN_COMPARELIST; break;
854 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
855 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856 default: isntype = ISN_COMPAREANY; break;
857 }
858 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200859 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
861 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
862 isntype = ISN_COMPAREANY;
863
864 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
865 && (isntype == ISN_COMPAREBOOL
866 || isntype == ISN_COMPARESPECIAL
867 || isntype == ISN_COMPARENR
868 || isntype == ISN_COMPAREFLOAT))
869 {
870 semsg(_("E1037: Cannot use \"%s\" with %s"),
871 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200872 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 }
874 if (isntype == ISN_DROP
875 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
876 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
877 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
878 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
879 && exptype != EXPR_IS && exptype != EXPR_ISNOT
880 && (type1 == VAR_BLOB || type2 == VAR_BLOB
881 || type1 == VAR_LIST || type2 == VAR_LIST))))
882 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100883 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200885 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200887 return isntype;
888}
889
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200890 int
891check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
892{
893 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
894 return FAIL;
895 return OK;
896}
897
Bram Moolenaara5565e42020-05-09 15:44:01 +0200898/*
899 * Generate an ISN_COMPARE* instruction with a boolean result.
900 */
901 static int
902generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
903{
904 isntype_T isntype;
905 isn_T *isn;
906 garray_T *stack = &cctx->ctx_type_stack;
907 vartype_T type1;
908 vartype_T type2;
909
910 RETURN_OK_IF_SKIP(cctx);
911
912 // Get the known type of the two items on the stack. If they are matching
913 // use a type-specific instruction. Otherwise fall back to runtime type
914 // checking.
915 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
916 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
917 isntype = get_compare_isn(exptype, type1, type2);
918 if (isntype == ISN_DROP)
919 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920
921 if ((isn = generate_instr(cctx, isntype)) == NULL)
922 return FAIL;
923 isn->isn_arg.op.op_type = exptype;
924 isn->isn_arg.op.op_ic = ic;
925
926 // takes two arguments, puts one bool back
927 if (stack->ga_len >= 2)
928 {
929 --stack->ga_len;
930 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
931 }
932
933 return OK;
934}
935
936/*
937 * Generate an ISN_2BOOL instruction.
938 */
939 static int
940generate_2BOOL(cctx_T *cctx, int invert)
941{
942 isn_T *isn;
943 garray_T *stack = &cctx->ctx_type_stack;
944
Bram Moolenaar080457c2020-03-03 21:53:32 +0100945 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
947 return FAIL;
948 isn->isn_arg.number = invert;
949
950 // type becomes bool
951 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
952
953 return OK;
954}
955
956 static int
957generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
958{
959 isn_T *isn;
960 garray_T *stack = &cctx->ctx_type_stack;
961
Bram Moolenaar080457c2020-03-03 21:53:32 +0100962 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
964 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200965 // TODO: whole type, e.g. for a function also arg and return types
966 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 isn->isn_arg.type.ct_off = offset;
968
969 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200970 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971
972 return OK;
973}
974
975/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200976 * Check that
977 * - "actual" is "expected" type or
978 * - "actual" is a type that can be "expected" type: add a runtime check; or
979 * - return FAIL.
980 */
981 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200982need_type(
983 type_T *actual,
984 type_T *expected,
985 int offset,
986 cctx_T *cctx,
987 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200988{
989 if (check_type(expected, actual, FALSE) == OK)
990 return OK;
991 if (actual->tt_type != VAR_ANY
992 && actual->tt_type != VAR_UNKNOWN
993 && !(actual->tt_type == VAR_FUNC
994 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
995 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200996 if (!silent)
997 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200998 return FAIL;
999 }
1000 generate_TYPECHECK(cctx, expected, offset);
1001 return OK;
1002}
1003
1004/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 * Generate an ISN_PUSHNR instruction.
1006 */
1007 static int
1008generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1009{
1010 isn_T *isn;
1011
Bram Moolenaar080457c2020-03-03 21:53:32 +01001012 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1014 return FAIL;
1015 isn->isn_arg.number = number;
1016
1017 return OK;
1018}
1019
1020/*
1021 * Generate an ISN_PUSHBOOL instruction.
1022 */
1023 static int
1024generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1025{
1026 isn_T *isn;
1027
Bram Moolenaar080457c2020-03-03 21:53:32 +01001028 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1030 return FAIL;
1031 isn->isn_arg.number = number;
1032
1033 return OK;
1034}
1035
1036/*
1037 * Generate an ISN_PUSHSPEC instruction.
1038 */
1039 static int
1040generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1041{
1042 isn_T *isn;
1043
Bram Moolenaar080457c2020-03-03 21:53:32 +01001044 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1046 return FAIL;
1047 isn->isn_arg.number = number;
1048
1049 return OK;
1050}
1051
1052#ifdef FEAT_FLOAT
1053/*
1054 * Generate an ISN_PUSHF instruction.
1055 */
1056 static int
1057generate_PUSHF(cctx_T *cctx, float_T fnumber)
1058{
1059 isn_T *isn;
1060
Bram Moolenaar080457c2020-03-03 21:53:32 +01001061 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1063 return FAIL;
1064 isn->isn_arg.fnumber = fnumber;
1065
1066 return OK;
1067}
1068#endif
1069
1070/*
1071 * Generate an ISN_PUSHS instruction.
1072 * Consumes "str".
1073 */
1074 static int
1075generate_PUSHS(cctx_T *cctx, char_u *str)
1076{
1077 isn_T *isn;
1078
Bram Moolenaar080457c2020-03-03 21:53:32 +01001079 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1081 return FAIL;
1082 isn->isn_arg.string = str;
1083
1084 return OK;
1085}
1086
1087/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001088 * Generate an ISN_PUSHCHANNEL instruction.
1089 * Consumes "channel".
1090 */
1091 static int
1092generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1093{
1094 isn_T *isn;
1095
Bram Moolenaar080457c2020-03-03 21:53:32 +01001096 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001097 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1098 return FAIL;
1099 isn->isn_arg.channel = channel;
1100
1101 return OK;
1102}
1103
1104/*
1105 * Generate an ISN_PUSHJOB instruction.
1106 * Consumes "job".
1107 */
1108 static int
1109generate_PUSHJOB(cctx_T *cctx, job_T *job)
1110{
1111 isn_T *isn;
1112
Bram Moolenaar080457c2020-03-03 21:53:32 +01001113 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001114 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001115 return FAIL;
1116 isn->isn_arg.job = job;
1117
1118 return OK;
1119}
1120
1121/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 * Generate an ISN_PUSHBLOB instruction.
1123 * Consumes "blob".
1124 */
1125 static int
1126generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1127{
1128 isn_T *isn;
1129
Bram Moolenaar080457c2020-03-03 21:53:32 +01001130 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1132 return FAIL;
1133 isn->isn_arg.blob = blob;
1134
1135 return OK;
1136}
1137
1138/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001139 * Generate an ISN_PUSHFUNC instruction with name "name".
1140 * Consumes "name".
1141 */
1142 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001143generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001144{
1145 isn_T *isn;
1146
Bram Moolenaar080457c2020-03-03 21:53:32 +01001147 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001148 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001149 return FAIL;
1150 isn->isn_arg.string = name;
1151
1152 return OK;
1153}
1154
1155/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001156 * Generate an ISN_GETITEM instruction with "index".
1157 */
1158 static int
1159generate_GETITEM(cctx_T *cctx, int index)
1160{
1161 isn_T *isn;
1162 garray_T *stack = &cctx->ctx_type_stack;
1163 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1164 type_T *item_type = &t_any;
1165
1166 RETURN_OK_IF_SKIP(cctx);
1167
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001168 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001169 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001170 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001171 emsg(_(e_listreq));
1172 return FAIL;
1173 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001174 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001175 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1176 return FAIL;
1177 isn->isn_arg.number = index;
1178
1179 // add the item type to the type stack
1180 if (ga_grow(stack, 1) == FAIL)
1181 return FAIL;
1182 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1183 ++stack->ga_len;
1184 return OK;
1185}
1186
1187/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001188 * Generate an ISN_SLICE instruction with "count".
1189 */
1190 static int
1191generate_SLICE(cctx_T *cctx, int count)
1192{
1193 isn_T *isn;
1194
1195 RETURN_OK_IF_SKIP(cctx);
1196 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1197 return FAIL;
1198 isn->isn_arg.number = count;
1199 return OK;
1200}
1201
1202/*
1203 * Generate an ISN_CHECKLEN instruction with "min_len".
1204 */
1205 static int
1206generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1207{
1208 isn_T *isn;
1209
1210 RETURN_OK_IF_SKIP(cctx);
1211
1212 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1213 return FAIL;
1214 isn->isn_arg.checklen.cl_min_len = min_len;
1215 isn->isn_arg.checklen.cl_more_OK = more_OK;
1216
1217 return OK;
1218}
1219
1220/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 * Generate an ISN_STORE instruction.
1222 */
1223 static int
1224generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1225{
1226 isn_T *isn;
1227
Bram Moolenaar080457c2020-03-03 21:53:32 +01001228 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1230 return FAIL;
1231 if (name != NULL)
1232 isn->isn_arg.string = vim_strsave(name);
1233 else
1234 isn->isn_arg.number = idx;
1235
1236 return OK;
1237}
1238
1239/*
1240 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1241 */
1242 static int
1243generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
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(cctx, ISN_STORENR)) == NULL)
1249 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001250 isn->isn_arg.storenr.stnr_idx = idx;
1251 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252
1253 return OK;
1254}
1255
1256/*
1257 * Generate an ISN_STOREOPT instruction
1258 */
1259 static int
1260generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1261{
1262 isn_T *isn;
1263
Bram Moolenaar080457c2020-03-03 21:53:32 +01001264 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1266 return FAIL;
1267 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1268 isn->isn_arg.storeopt.so_flags = opt_flags;
1269
1270 return OK;
1271}
1272
1273/*
1274 * Generate an ISN_LOAD or similar instruction.
1275 */
1276 static int
1277generate_LOAD(
1278 cctx_T *cctx,
1279 isntype_T isn_type,
1280 int idx,
1281 char_u *name,
1282 type_T *type)
1283{
1284 isn_T *isn;
1285
Bram Moolenaar080457c2020-03-03 21:53:32 +01001286 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1288 return FAIL;
1289 if (name != NULL)
1290 isn->isn_arg.string = vim_strsave(name);
1291 else
1292 isn->isn_arg.number = idx;
1293
1294 return OK;
1295}
1296
1297/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001298 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001299 */
1300 static int
1301generate_LOADV(
1302 cctx_T *cctx,
1303 char_u *name,
1304 int error)
1305{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001306 int di_flags;
1307 int vidx = find_vim_var(name, &di_flags);
1308 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001309
Bram Moolenaar080457c2020-03-03 21:53:32 +01001310 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001311 if (vidx < 0)
1312 {
1313 if (error)
1314 semsg(_(e_var_notfound), name);
1315 return FAIL;
1316 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001317 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001318
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001319 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001320}
1321
1322/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001323 * Generate an ISN_UNLET instruction.
1324 */
1325 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001326generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001327{
1328 isn_T *isn;
1329
1330 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001331 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001332 return FAIL;
1333 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1334 isn->isn_arg.unlet.ul_forceit = forceit;
1335
1336 return OK;
1337}
1338
1339/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 * Generate an ISN_LOADS instruction.
1341 */
1342 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001343generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001345 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001347 int sid,
1348 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349{
1350 isn_T *isn;
1351
Bram Moolenaar080457c2020-03-03 21:53:32 +01001352 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001353 if (isn_type == ISN_LOADS)
1354 isn = generate_instr_type(cctx, isn_type, type);
1355 else
1356 isn = generate_instr_drop(cctx, isn_type, 1);
1357 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001359 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1360 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361
1362 return OK;
1363}
1364
1365/*
1366 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1367 */
1368 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001369generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 cctx_T *cctx,
1371 isntype_T isn_type,
1372 int sid,
1373 int idx,
1374 type_T *type)
1375{
1376 isn_T *isn;
1377
Bram Moolenaar080457c2020-03-03 21:53:32 +01001378 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 if (isn_type == ISN_LOADSCRIPT)
1380 isn = generate_instr_type(cctx, isn_type, type);
1381 else
1382 isn = generate_instr_drop(cctx, isn_type, 1);
1383 if (isn == NULL)
1384 return FAIL;
1385 isn->isn_arg.script.script_sid = sid;
1386 isn->isn_arg.script.script_idx = idx;
1387 return OK;
1388}
1389
1390/*
1391 * Generate an ISN_NEWLIST instruction.
1392 */
1393 static int
1394generate_NEWLIST(cctx_T *cctx, int count)
1395{
1396 isn_T *isn;
1397 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 type_T *type;
1399 type_T *member;
1400
Bram Moolenaar080457c2020-03-03 21:53:32 +01001401 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1403 return FAIL;
1404 isn->isn_arg.number = count;
1405
1406 // drop the value types
1407 stack->ga_len -= count;
1408
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001409 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001410 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 if (count > 0)
1412 member = ((type_T **)stack->ga_data)[stack->ga_len];
1413 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001414 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001415 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416
1417 // add the list type to the type stack
1418 if (ga_grow(stack, 1) == FAIL)
1419 return FAIL;
1420 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1421 ++stack->ga_len;
1422
1423 return OK;
1424}
1425
1426/*
1427 * Generate an ISN_NEWDICT instruction.
1428 */
1429 static int
1430generate_NEWDICT(cctx_T *cctx, int count)
1431{
1432 isn_T *isn;
1433 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 type_T *type;
1435 type_T *member;
1436
Bram Moolenaar080457c2020-03-03 21:53:32 +01001437 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1439 return FAIL;
1440 isn->isn_arg.number = count;
1441
1442 // drop the key and value types
1443 stack->ga_len -= 2 * count;
1444
Bram Moolenaar436472f2020-02-20 22:54:43 +01001445 // Use the first value type for the list member type. Use "void" for an
1446 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 if (count > 0)
1448 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1449 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001450 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001451 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452
1453 // add the dict type to the type stack
1454 if (ga_grow(stack, 1) == FAIL)
1455 return FAIL;
1456 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1457 ++stack->ga_len;
1458
1459 return OK;
1460}
1461
1462/*
1463 * Generate an ISN_FUNCREF instruction.
1464 */
1465 static int
1466generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1467{
1468 isn_T *isn;
1469 garray_T *stack = &cctx->ctx_type_stack;
1470
Bram Moolenaar080457c2020-03-03 21:53:32 +01001471 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1473 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001474 isn->isn_arg.funcref.fr_func = dfunc_idx;
1475 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476
1477 if (ga_grow(stack, 1) == FAIL)
1478 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001479 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 // TODO: argument and return types
1481 ++stack->ga_len;
1482
1483 return OK;
1484}
1485
1486/*
1487 * Generate an ISN_JUMP instruction.
1488 */
1489 static int
1490generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1491{
1492 isn_T *isn;
1493 garray_T *stack = &cctx->ctx_type_stack;
1494
Bram Moolenaar080457c2020-03-03 21:53:32 +01001495 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1497 return FAIL;
1498 isn->isn_arg.jump.jump_when = when;
1499 isn->isn_arg.jump.jump_where = where;
1500
1501 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1502 --stack->ga_len;
1503
1504 return OK;
1505}
1506
1507 static int
1508generate_FOR(cctx_T *cctx, int loop_idx)
1509{
1510 isn_T *isn;
1511 garray_T *stack = &cctx->ctx_type_stack;
1512
Bram Moolenaar080457c2020-03-03 21:53:32 +01001513 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1515 return FAIL;
1516 isn->isn_arg.forloop.for_idx = loop_idx;
1517
1518 if (ga_grow(stack, 1) == FAIL)
1519 return FAIL;
1520 // type doesn't matter, will be stored next
1521 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1522 ++stack->ga_len;
1523
1524 return OK;
1525}
1526
1527/*
1528 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001529 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 * Return FAIL if the number of arguments is wrong.
1531 */
1532 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001533generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001534{
1535 isn_T *isn;
1536 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001537 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001538 type_T *argtypes[MAX_FUNC_ARGS];
1539 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540
Bram Moolenaar080457c2020-03-03 21:53:32 +01001541 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001542 argoff = check_internal_func(func_idx, argcount);
1543 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 return FAIL;
1545
Bram Moolenaar389df252020-07-09 21:20:47 +02001546 if (method_call && argoff > 1)
1547 {
1548 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1549 return FAIL;
1550 isn->isn_arg.shuffle.shfl_item = argcount;
1551 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1552 }
1553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1555 return FAIL;
1556 isn->isn_arg.bfunc.cbf_idx = func_idx;
1557 isn->isn_arg.bfunc.cbf_argcount = argcount;
1558
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001559 for (i = 0; i < argcount; ++i)
1560 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 stack->ga_len -= argcount; // drop the arguments
1563 if (ga_grow(stack, 1) == FAIL)
1564 return FAIL;
1565 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001566 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567 ++stack->ga_len; // add return value
1568
1569 return OK;
1570}
1571
1572/*
1573 * Generate an ISN_DCALL or ISN_UCALL instruction.
1574 * Return FAIL if the number of arguments is wrong.
1575 */
1576 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001577generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578{
1579 isn_T *isn;
1580 garray_T *stack = &cctx->ctx_type_stack;
1581 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001582 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583
Bram Moolenaar080457c2020-03-03 21:53:32 +01001584 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 if (argcount > regular_args && !has_varargs(ufunc))
1586 {
1587 semsg(_(e_toomanyarg), ufunc->uf_name);
1588 return FAIL;
1589 }
1590 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1591 {
1592 semsg(_(e_toofewarg), ufunc->uf_name);
1593 return FAIL;
1594 }
1595
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001596 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001597 {
1598 int i;
1599
1600 for (i = 0; i < argcount; ++i)
1601 {
1602 type_T *expected;
1603 type_T *actual;
1604
1605 if (i < regular_args)
1606 {
1607 if (ufunc->uf_arg_types == NULL)
1608 continue;
1609 expected = ufunc->uf_arg_types[i];
1610 }
1611 else
1612 expected = ufunc->uf_va_type->tt_member;
1613 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001614 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001615 {
1616 arg_type_mismatch(expected, actual, i + 1);
1617 return FAIL;
1618 }
1619 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001620 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001621 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001622 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001623 }
1624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001626 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001627 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001629 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 {
1631 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1632 isn->isn_arg.dfunc.cdf_argcount = argcount;
1633 }
1634 else
1635 {
1636 // A user function may be deleted and redefined later, can't use the
1637 // ufunc pointer, need to look it up again at runtime.
1638 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1639 isn->isn_arg.ufunc.cuf_argcount = argcount;
1640 }
1641
1642 stack->ga_len -= argcount; // drop the arguments
1643 if (ga_grow(stack, 1) == FAIL)
1644 return FAIL;
1645 // add return value
1646 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1647 ++stack->ga_len;
1648
1649 return OK;
1650}
1651
1652/*
1653 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1654 */
1655 static int
1656generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1657{
1658 isn_T *isn;
1659 garray_T *stack = &cctx->ctx_type_stack;
1660
Bram Moolenaar080457c2020-03-03 21:53:32 +01001661 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1663 return FAIL;
1664 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1665 isn->isn_arg.ufunc.cuf_argcount = argcount;
1666
1667 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001668 if (ga_grow(stack, 1) == FAIL)
1669 return FAIL;
1670 // add return value
1671 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1672 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673
1674 return OK;
1675}
1676
1677/*
1678 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001679 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 */
1681 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001682generate_PCALL(
1683 cctx_T *cctx,
1684 int argcount,
1685 char_u *name,
1686 type_T *type,
1687 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688{
1689 isn_T *isn;
1690 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001691 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692
Bram Moolenaar080457c2020-03-03 21:53:32 +01001693 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001694
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001695 if (type->tt_type == VAR_ANY)
1696 ret_type = &t_any;
1697 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001698 {
1699 if (type->tt_argcount != -1)
1700 {
1701 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1702
1703 if (argcount < type->tt_min_argcount - varargs)
1704 {
1705 semsg(_(e_toofewarg), "[reference]");
1706 return FAIL;
1707 }
1708 if (!varargs && argcount > type->tt_argcount)
1709 {
1710 semsg(_(e_toomanyarg), "[reference]");
1711 return FAIL;
1712 }
1713 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001714 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001715 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001716 else
1717 {
1718 semsg(_("E1085: Not a callable type: %s"), name);
1719 return FAIL;
1720 }
1721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1723 return FAIL;
1724 isn->isn_arg.pfunc.cpf_top = at_top;
1725 isn->isn_arg.pfunc.cpf_argcount = argcount;
1726
1727 stack->ga_len -= argcount; // drop the arguments
1728
1729 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001730 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001732 // If partial is above the arguments it must be cleared and replaced with
1733 // the return value.
1734 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1735 return FAIL;
1736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 return OK;
1738}
1739
1740/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001741 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 */
1743 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001744generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745{
1746 isn_T *isn;
1747 garray_T *stack = &cctx->ctx_type_stack;
1748 type_T *type;
1749
Bram Moolenaar080457c2020-03-03 21:53:32 +01001750 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001751 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001753 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001755 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001757 if (type->tt_type != VAR_DICT && type != &t_any)
1758 {
1759 emsg(_(e_dictreq));
1760 return FAIL;
1761 }
1762 // change dict type to dict member type
1763 if (type->tt_type == VAR_DICT)
1764 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765
1766 return OK;
1767}
1768
1769/*
1770 * Generate an ISN_ECHO instruction.
1771 */
1772 static int
1773generate_ECHO(cctx_T *cctx, int with_white, int count)
1774{
1775 isn_T *isn;
1776
Bram Moolenaar080457c2020-03-03 21:53:32 +01001777 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1779 return FAIL;
1780 isn->isn_arg.echo.echo_with_white = with_white;
1781 isn->isn_arg.echo.echo_count = count;
1782
1783 return OK;
1784}
1785
Bram Moolenaarad39c092020-02-26 18:23:43 +01001786/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001787 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001788 */
1789 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001790generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001791{
1792 isn_T *isn;
1793
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001794 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001795 return FAIL;
1796 isn->isn_arg.number = count;
1797
1798 return OK;
1799}
1800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 static int
1802generate_EXEC(cctx_T *cctx, char_u *line)
1803{
1804 isn_T *isn;
1805
Bram Moolenaar080457c2020-03-03 21:53:32 +01001806 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1808 return FAIL;
1809 isn->isn_arg.string = vim_strsave(line);
1810 return OK;
1811}
1812
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001813 static int
1814generate_EXECCONCAT(cctx_T *cctx, int count)
1815{
1816 isn_T *isn;
1817
1818 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1819 return FAIL;
1820 isn->isn_arg.number = count;
1821 return OK;
1822}
1823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824/*
1825 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001826 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001828 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1830{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 lvar_T *lvar;
1832
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001833 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001835 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001836 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 }
1838
1839 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001840 return NULL;
1841 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001843 // Every local variable uses the next entry on the stack. We could re-use
1844 // the last ones when leaving a scope, but then variables used in a closure
1845 // might get overwritten. To keep things simple do not re-use stack
1846 // entries. This is less efficient, but memory is cheap these days.
1847 lvar->lv_idx = cctx->ctx_locals_count++;
1848
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001849 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 lvar->lv_const = isConst;
1851 lvar->lv_type = type;
1852
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001853 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854}
1855
1856/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001857 * Remove local variables above "new_top".
1858 */
1859 static void
1860unwind_locals(cctx_T *cctx, int new_top)
1861{
1862 if (cctx->ctx_locals.ga_len > new_top)
1863 {
1864 int idx;
1865 lvar_T *lvar;
1866
1867 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1868 {
1869 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1870 vim_free(lvar->lv_name);
1871 }
1872 }
1873 cctx->ctx_locals.ga_len = new_top;
1874}
1875
1876/*
1877 * Free all local variables.
1878 */
1879 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001880free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001881{
1882 unwind_locals(cctx, 0);
1883 ga_clear(&cctx->ctx_locals);
1884}
1885
1886/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 * Skip over a type definition and return a pointer to just after it.
1888 */
1889 char_u *
1890skip_type(char_u *start)
1891{
1892 char_u *p = start;
1893
1894 while (ASCII_ISALNUM(*p) || *p == '_')
1895 ++p;
1896
1897 // Skip over "<type>"; this is permissive about white space.
1898 if (*skipwhite(p) == '<')
1899 {
1900 p = skipwhite(p);
1901 p = skip_type(skipwhite(p + 1));
1902 p = skipwhite(p);
1903 if (*p == '>')
1904 ++p;
1905 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001906 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
1907 && STRNCMP("func", start, 4) == 0)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001908 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001909 if (*p == '(')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001910 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001911 // handle func(args): type
1912 ++p;
1913 while (*p != ')' && *p != NUL)
1914 {
1915 char_u *sp = p;
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001916
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001917 p = skip_type(p);
1918 if (p == sp)
1919 return p; // syntax error
1920 if (*p == ',')
1921 p = skipwhite(p + 1);
1922 }
1923 if (*p == ')')
1924 {
1925 if (p[1] == ':')
1926 p = skip_type(skipwhite(p + 2));
1927 else
1928 p = skipwhite(p + 1);
1929 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001930 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001931 else
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001932 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001933 // handle func: return_type
1934 p = skip_type(skipwhite(p + 1));
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001935 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001936 }
1937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 return p;
1939}
1940
1941/*
1942 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001943 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 * Returns NULL in case of failure.
1945 */
1946 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001947parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948{
1949 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001950 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951
1952 if (**arg != '<')
1953 {
1954 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001955 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 else
1957 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001958 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 }
1960 *arg = skipwhite(*arg + 1);
1961
Bram Moolenaard77a8522020-04-03 21:59:57 +02001962 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963
1964 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001965 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 {
1967 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001968 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 }
1970 ++*arg;
1971
1972 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001973 return get_list_type(member_type, type_gap);
1974 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975}
1976
1977/*
1978 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001979 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 */
1981 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001982parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983{
1984 char_u *p = *arg;
1985 size_t len;
1986
1987 // skip over the first word
1988 while (ASCII_ISALNUM(*p) || *p == '_')
1989 ++p;
1990 len = p - *arg;
1991
1992 switch (**arg)
1993 {
1994 case 'a':
1995 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1996 {
1997 *arg += len;
1998 return &t_any;
1999 }
2000 break;
2001 case 'b':
2002 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
2003 {
2004 *arg += len;
2005 return &t_bool;
2006 }
2007 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
2008 {
2009 *arg += len;
2010 return &t_blob;
2011 }
2012 break;
2013 case 'c':
2014 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
2015 {
2016 *arg += len;
2017 return &t_channel;
2018 }
2019 break;
2020 case 'd':
2021 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
2022 {
2023 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002024 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 }
2026 break;
2027 case 'f':
2028 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
2029 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01002030#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 *arg += len;
2032 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002033#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002034 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002035 return &t_any;
2036#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037 }
2038 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2039 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002040 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002041 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002042 int argcount = -1;
2043 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002044 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002045 type_T *arg_type[MAX_FUNC_ARGS + 1];
2046
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002047 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002049 if (**arg == '(')
2050 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002051 // "func" may or may not return a value, "func()" does
2052 // not return a value.
2053 ret_type = &t_void;
2054
Bram Moolenaard77a8522020-04-03 21:59:57 +02002055 p = ++*arg;
2056 argcount = 0;
2057 while (*p != NUL && *p != ')')
2058 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002059 if (*p == '?')
2060 {
2061 if (first_optional == -1)
2062 first_optional = argcount;
2063 ++p;
2064 }
2065 else if (first_optional != -1)
2066 {
2067 emsg(_("E1007: mandatory argument after optional argument"));
2068 return &t_any;
2069 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002070 else if (STRNCMP(p, "...", 3) == 0)
2071 {
2072 flags |= TTFLAG_VARARGS;
2073 p += 3;
2074 }
2075
2076 arg_type[argcount++] = parse_type(&p, type_gap);
2077
2078 // Nothing comes after "...{type}".
2079 if (flags & TTFLAG_VARARGS)
2080 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002081
Bram Moolenaard77a8522020-04-03 21:59:57 +02002082 if (*p != ',' && *skipwhite(p) == ',')
2083 {
2084 semsg(_(e_no_white_before), ",");
2085 return &t_any;
2086 }
2087 if (*p == ',')
2088 {
2089 ++p;
2090 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002091 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002092 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002093 return &t_any;
2094 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002095 }
2096 p = skipwhite(p);
2097 if (argcount == MAX_FUNC_ARGS)
2098 {
2099 emsg(_("E740: Too many argument types"));
2100 return &t_any;
2101 }
2102 }
2103
2104 p = skipwhite(p);
2105 if (*p != ')')
2106 {
2107 emsg(_(e_missing_close));
2108 return &t_any;
2109 }
2110 *arg = p + 1;
2111 }
2112 if (**arg == ':')
2113 {
2114 // parse return type
2115 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002116 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002117 semsg(_(e_white_after), ":");
2118 *arg = skipwhite(*arg);
2119 ret_type = parse_type(arg, type_gap);
2120 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002121 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002122 type = get_func_type(ret_type, argcount, type_gap);
2123 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002124 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002125 type = alloc_func_type(ret_type, argcount, type_gap);
2126 type->tt_flags = flags;
2127 if (argcount > 0)
2128 {
2129 type->tt_argcount = argcount;
2130 type->tt_min_argcount = first_optional == -1
2131 ? argcount : first_optional;
2132 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002133 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002134 return &t_any;
2135 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002136 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002137 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002138 }
2139 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 }
2141 break;
2142 case 'j':
2143 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2144 {
2145 *arg += len;
2146 return &t_job;
2147 }
2148 break;
2149 case 'l':
2150 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2151 {
2152 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002153 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 }
2155 break;
2156 case 'n':
2157 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2158 {
2159 *arg += len;
2160 return &t_number;
2161 }
2162 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 case 's':
2164 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2165 {
2166 *arg += len;
2167 return &t_string;
2168 }
2169 break;
2170 case 'v':
2171 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2172 {
2173 *arg += len;
2174 return &t_void;
2175 }
2176 break;
2177 }
2178
2179 semsg(_("E1010: Type not recognized: %s"), *arg);
2180 return &t_any;
2181}
2182
2183/*
2184 * Check if "type1" and "type2" are exactly the same.
2185 */
2186 static int
2187equal_type(type_T *type1, type_T *type2)
2188{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002189 int i;
2190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 if (type1->tt_type != type2->tt_type)
2192 return FALSE;
2193 switch (type1->tt_type)
2194 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002196 case VAR_ANY:
2197 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 case VAR_SPECIAL:
2199 case VAR_BOOL:
2200 case VAR_NUMBER:
2201 case VAR_FLOAT:
2202 case VAR_STRING:
2203 case VAR_BLOB:
2204 case VAR_JOB:
2205 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002206 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 case VAR_LIST:
2208 case VAR_DICT:
2209 return equal_type(type1->tt_member, type2->tt_member);
2210 case VAR_FUNC:
2211 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002212 if (!equal_type(type1->tt_member, type2->tt_member)
2213 || type1->tt_argcount != type2->tt_argcount)
2214 return FALSE;
2215 if (type1->tt_argcount < 0
2216 || type1->tt_args == NULL || type2->tt_args == NULL)
2217 return TRUE;
2218 for (i = 0; i < type1->tt_argcount; ++i)
2219 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2220 return FALSE;
2221 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 }
2223 return TRUE;
2224}
2225
2226/*
2227 * Find the common type of "type1" and "type2" and put it in "dest".
2228 * "type2" and "dest" may be the same.
2229 */
2230 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002231common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232{
2233 if (equal_type(type1, type2))
2234 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002235 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 return;
2237 }
2238
2239 if (type1->tt_type == type2->tt_type)
2240 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2242 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002243 type_T *common;
2244
Bram Moolenaard77a8522020-04-03 21:59:57 +02002245 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002246 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002247 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002248 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002249 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 return;
2251 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002252 if (type1->tt_type == VAR_FUNC)
2253 {
2254 type_T *common;
2255
2256 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2257 if (type1->tt_argcount == type2->tt_argcount
2258 && type1->tt_argcount >= 0)
2259 {
2260 int argcount = type1->tt_argcount;
2261 int i;
2262
2263 *dest = alloc_func_type(common, argcount, type_gap);
2264 if (type1->tt_args != NULL && type2->tt_args != NULL)
2265 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002266 if (func_type_add_arg_types(*dest, argcount,
2267 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002268 for (i = 0; i < argcount; ++i)
2269 common_type(type1->tt_args[i], type2->tt_args[i],
2270 &(*dest)->tt_args[i], type_gap);
2271 }
2272 }
2273 else
2274 *dest = alloc_func_type(common, -1, type_gap);
2275 return;
2276 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 }
2278
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002279 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280}
2281
2282 char *
2283vartype_name(vartype_T type)
2284{
2285 switch (type)
2286 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002287 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002288 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 case VAR_SPECIAL: return "special";
2291 case VAR_BOOL: return "bool";
2292 case VAR_NUMBER: return "number";
2293 case VAR_FLOAT: return "float";
2294 case VAR_STRING: return "string";
2295 case VAR_BLOB: return "blob";
2296 case VAR_JOB: return "job";
2297 case VAR_CHANNEL: return "channel";
2298 case VAR_LIST: return "list";
2299 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002300
2301 case VAR_FUNC:
2302 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002304 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305}
2306
2307/*
2308 * Return the name of a type.
2309 * The result may be in allocated memory, in which case "tofree" is set.
2310 */
2311 char *
2312type_name(type_T *type, char **tofree)
2313{
2314 char *name = vartype_name(type->tt_type);
2315
2316 *tofree = NULL;
2317 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2318 {
2319 char *member_free;
2320 char *member_name = type_name(type->tt_member, &member_free);
2321 size_t len;
2322
2323 len = STRLEN(name) + STRLEN(member_name) + 3;
2324 *tofree = alloc(len);
2325 if (*tofree != NULL)
2326 {
2327 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2328 vim_free(member_free);
2329 return *tofree;
2330 }
2331 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002332 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002333 {
2334 garray_T ga;
2335 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002336 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002337
2338 ga_init2(&ga, 1, 100);
2339 if (ga_grow(&ga, 20) == FAIL)
2340 return "[unknown]";
2341 *tofree = ga.ga_data;
2342 STRCPY(ga.ga_data, "func(");
2343 ga.ga_len += 5;
2344
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002345 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002346 {
2347 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002348 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002349 int len;
2350
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002351 if (type->tt_args == NULL)
2352 arg_type = "[unknown]";
2353 else
2354 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002355 if (i > 0)
2356 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002357 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002358 ga.ga_len += 2;
2359 }
2360 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002361 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002362 {
2363 vim_free(arg_free);
2364 return "[unknown]";
2365 }
2366 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002367 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002368 {
2369 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2370 ga.ga_len += 3;
2371 }
2372 else if (i >= type->tt_min_argcount)
2373 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002374 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002375 ga.ga_len += len;
2376 vim_free(arg_free);
2377 }
2378
2379 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002380 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002381 else
2382 {
2383 char *ret_free;
2384 char *ret_name = type_name(type->tt_member, &ret_free);
2385 int len;
2386
2387 len = (int)STRLEN(ret_name) + 4;
2388 if (ga_grow(&ga, len) == FAIL)
2389 {
2390 vim_free(ret_free);
2391 return "[unknown]";
2392 }
2393 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002394 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2395 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002396 vim_free(ret_free);
2397 }
2398 return ga.ga_data;
2399 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400
2401 return name;
2402}
2403
2404/*
2405 * Find "name" in script-local items of script "sid".
2406 * Returns the index in "sn_var_vals" if found.
2407 * If found but not in "sn_var_vals" returns -1.
2408 * If not found returns -2.
2409 */
2410 int
2411get_script_item_idx(int sid, char_u *name, int check_writable)
2412{
2413 hashtab_T *ht;
2414 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002415 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 int idx;
2417
2418 // First look the name up in the hashtable.
2419 if (sid <= 0 || sid > script_items.ga_len)
2420 return -1;
2421 ht = &SCRIPT_VARS(sid);
2422 di = find_var_in_ht(ht, 0, name, TRUE);
2423 if (di == NULL)
2424 return -2;
2425
2426 // Now find the svar_T index in sn_var_vals.
2427 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2428 {
2429 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2430
2431 if (sv->sv_tv == &di->di_tv)
2432 {
2433 if (check_writable && sv->sv_const)
2434 semsg(_(e_readonlyvar), name);
2435 return idx;
2436 }
2437 }
2438 return -1;
2439}
2440
2441/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002442 * Find "name" in imported items of the current script or in "cctx" if not
2443 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 */
2445 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002446find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002448 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 int idx;
2450
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002451 if (current_sctx.sc_sid <= 0)
2452 return NULL;
2453 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 if (cctx != NULL)
2455 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2456 {
2457 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2458 + idx;
2459
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002460 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2461 : STRLEN(import->imp_name) == len
2462 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 return import;
2464 }
2465
2466 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2467 {
2468 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2469
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002470 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2471 : STRLEN(import->imp_name) == len
2472 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 return import;
2474 }
2475 return NULL;
2476}
2477
2478/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002479 * Free all imported variables.
2480 */
2481 static void
2482free_imported(cctx_T *cctx)
2483{
2484 int idx;
2485
2486 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2487 {
2488 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2489
2490 vim_free(import->imp_name);
2491 }
2492 ga_clear(&cctx->ctx_imports);
2493}
2494
2495/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002496 * Return TRUE if "p" points at a "#" but not at "#{".
2497 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002498 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002499vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002500{
2501 return p[0] == '#' && p[1] != '{';
2502}
2503
2504/*
2505 * Return a pointer to the next line that isn't empty or only contains a
2506 * comment. Skips over white space.
2507 * Returns NULL if there is none.
2508 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002509 char_u *
2510peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002511{
2512 int lnum = cctx->ctx_lnum;
2513
2514 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2515 {
2516 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002517 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002518
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002519 if (line == NULL)
2520 break;
2521 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002522 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002523 return p;
2524 }
2525 return NULL;
2526}
2527
2528/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002529 * Called when checking for a following operator at "arg". When the rest of
2530 * the line is empty or only a comment, peek the next line. If there is a next
2531 * line return a pointer to it and set "nextp".
2532 * Otherwise skip over white space.
2533 */
2534 static char_u *
2535may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2536{
2537 char_u *p = skipwhite(arg);
2538
2539 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002540 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002541 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002542 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002543 if (*nextp != NULL)
2544 return *nextp;
2545 }
2546 return p;
2547}
2548
2549/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002550 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002551 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002552 * Returns NULL when at the end.
2553 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002554 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002555next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002556{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002557 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002558
2559 do
2560 {
2561 ++cctx->ctx_lnum;
2562 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002563 {
2564 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002565 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002566 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002567 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002568 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002569 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002570 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002571 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002572 return line;
2573}
2574
2575/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002576 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002577 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002578 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2579 */
2580 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002581may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002582{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002583 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002584 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002585 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002586
2587 if (next == NULL)
2588 return FAIL;
2589 *arg = skipwhite(next);
2590 }
2591 return OK;
2592}
2593
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002594/*
2595 * Idem, and give an error when failed.
2596 */
2597 static int
2598may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2599{
2600 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2601 {
2602 emsg(_("E1097: line incomplete"));
2603 return FAIL;
2604 }
2605 return OK;
2606}
2607
2608
Bram Moolenaara5565e42020-05-09 15:44:01 +02002609// Structure passed between the compile_expr* functions to keep track of
2610// constants that have been parsed but for which no code was produced yet. If
2611// possible expressions on these constants are applied at compile time. If
2612// that is not possible, the code to push the constants needs to be generated
2613// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002614// Using 50 should be more than enough of 5 levels of ().
2615#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002616typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002617 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002618 int pp_used; // active entries in pp_tv[]
2619} ppconst_T;
2620
Bram Moolenaar1c747212020-05-09 18:28:34 +02002621static int compile_expr0(char_u **arg, cctx_T *cctx);
2622static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2623
Bram Moolenaara5565e42020-05-09 15:44:01 +02002624/*
2625 * Generate a PUSH instruction for "tv".
2626 * "tv" will be consumed or cleared.
2627 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2628 */
2629 static int
2630generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2631{
2632 if (tv != NULL)
2633 {
2634 switch (tv->v_type)
2635 {
2636 case VAR_UNKNOWN:
2637 break;
2638 case VAR_BOOL:
2639 generate_PUSHBOOL(cctx, tv->vval.v_number);
2640 break;
2641 case VAR_SPECIAL:
2642 generate_PUSHSPEC(cctx, tv->vval.v_number);
2643 break;
2644 case VAR_NUMBER:
2645 generate_PUSHNR(cctx, tv->vval.v_number);
2646 break;
2647#ifdef FEAT_FLOAT
2648 case VAR_FLOAT:
2649 generate_PUSHF(cctx, tv->vval.v_float);
2650 break;
2651#endif
2652 case VAR_BLOB:
2653 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2654 tv->vval.v_blob = NULL;
2655 break;
2656 case VAR_STRING:
2657 generate_PUSHS(cctx, tv->vval.v_string);
2658 tv->vval.v_string = NULL;
2659 break;
2660 default:
2661 iemsg("constant type not supported");
2662 clear_tv(tv);
2663 return FAIL;
2664 }
2665 tv->v_type = VAR_UNKNOWN;
2666 }
2667 return OK;
2668}
2669
2670/*
2671 * Generate code for any ppconst entries.
2672 */
2673 static int
2674generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2675{
2676 int i;
2677 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002678 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002679
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002680 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002681 for (i = 0; i < ppconst->pp_used; ++i)
2682 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2683 ret = FAIL;
2684 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002685 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002686 return ret;
2687}
2688
2689/*
2690 * Clear ppconst constants. Used when failing.
2691 */
2692 static void
2693clear_ppconst(ppconst_T *ppconst)
2694{
2695 int i;
2696
2697 for (i = 0; i < ppconst->pp_used; ++i)
2698 clear_tv(&ppconst->pp_tv[i]);
2699 ppconst->pp_used = 0;
2700}
2701
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002702/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002703 * Generate an instruction to load script-local variable "name", without the
2704 * leading "s:".
2705 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 */
2707 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002708compile_load_scriptvar(
2709 cctx_T *cctx,
2710 char_u *name, // variable NUL terminated
2711 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002712 char_u **end, // end of variable
2713 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002715 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2717 imported_T *import;
2718
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002719 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002721 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002722 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2723 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 }
2725 if (idx >= 0)
2726 {
2727 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2728
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002729 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 current_sctx.sc_sid, idx, sv->sv_type);
2731 return OK;
2732 }
2733
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002734 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 if (import != NULL)
2736 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002737 if (import->imp_all)
2738 {
2739 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002740 char_u *exp_name;
2741 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002742 ufunc_T *ufunc;
2743 type_T *type;
2744
2745 // Used "import * as Name", need to lookup the member.
2746 if (*p != '.')
2747 {
2748 semsg(_("E1060: expected dot after name: %s"), start);
2749 return FAIL;
2750 }
2751 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002752 if (VIM_ISWHITE(*p))
2753 {
2754 emsg(_("E1074: no white space allowed after dot"));
2755 return FAIL;
2756 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002757
Bram Moolenaar1c991142020-07-04 13:15:31 +02002758 // isolate one name
2759 exp_name = p;
2760 while (eval_isnamec(*p))
2761 ++p;
2762 cc = *p;
2763 *p = NUL;
2764
2765 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2766 *p = cc;
2767 p = skipwhite(p);
2768
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002769 // TODO: what if it is a function?
2770 if (idx < 0)
2771 return FAIL;
2772 *end = p;
2773
2774 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2775 import->imp_sid,
2776 idx,
2777 type);
2778 }
2779 else
2780 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002781 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002782 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2783 import->imp_sid,
2784 import->imp_var_vals_idx,
2785 import->imp_type);
2786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 return OK;
2788 }
2789
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002790 if (error)
2791 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 return FAIL;
2793}
2794
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002795 static int
2796generate_funcref(cctx_T *cctx, char_u *name)
2797{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002798 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002799
2800 if (ufunc == NULL)
2801 return FAIL;
2802
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002803 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2804 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002805}
2806
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807/*
2808 * Compile a variable name into a load instruction.
2809 * "end" points to just after the name.
2810 * When "error" is FALSE do not give an error when not found.
2811 */
2812 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002813compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814{
2815 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002816 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002817 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002819 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820
2821 if (*(*arg + 1) == ':')
2822 {
2823 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002824 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002825 {
2826 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002828 switch (**arg)
2829 {
2830 case 'g': isn_type = ISN_LOADGDICT; break;
2831 case 'w': isn_type = ISN_LOADWDICT; break;
2832 case 't': isn_type = ISN_LOADTDICT; break;
2833 case 'b': isn_type = ISN_LOADBDICT; break;
2834 default:
2835 semsg(_(e_namespace), *arg);
2836 goto theend;
2837 }
2838 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2839 goto theend;
2840 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002841 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 else
2843 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002844 isntype_T isn_type = ISN_DROP;
2845
2846 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2847 if (name == NULL)
2848 return FAIL;
2849
2850 switch (**arg)
2851 {
2852 case 'v': res = generate_LOADV(cctx, name, error);
2853 break;
2854 case 's': res = compile_load_scriptvar(cctx, name,
2855 NULL, NULL, error);
2856 break;
2857 case 'g': isn_type = ISN_LOADG; break;
2858 case 'w': isn_type = ISN_LOADW; break;
2859 case 't': isn_type = ISN_LOADT; break;
2860 case 'b': isn_type = ISN_LOADB; break;
2861 default: semsg(_(e_namespace), *arg);
2862 goto theend;
2863 }
2864 if (isn_type != ISN_DROP)
2865 {
2866 // Global, Buffer-local, Window-local and Tabpage-local
2867 // variables can be defined later, thus we don't check if it
2868 // exists, give error at runtime.
2869 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2870 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 }
2872 }
2873 else
2874 {
2875 size_t len = end - *arg;
2876 int idx;
2877 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002878 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879
2880 name = vim_strnsave(*arg, end - *arg);
2881 if (name == NULL)
2882 return FAIL;
2883
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002884 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002886 if (!gen_load_outer)
2887 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 }
2889 else
2890 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002891 lvar_T *lvar = lookup_local(*arg, len, cctx);
2892
2893 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002895 type = lvar->lv_type;
2896 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002897 if (lvar->lv_from_outer)
2898 gen_load_outer = TRUE;
2899 else
2900 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 }
2902 else
2903 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002904 // "var" can be script-local even without using "s:" if it
2905 // already exists.
2906 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2907 == SCRIPT_VERSION_VIM9
2908 || lookup_script(*arg, len) == OK)
2909 res = compile_load_scriptvar(cctx, name, *arg, &end,
2910 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002911
Bram Moolenaara5565e42020-05-09 15:44:01 +02002912 // When the name starts with an uppercase letter or "x:" it
2913 // can be a user defined function.
2914 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2915 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 }
2917 }
2918 if (gen_load)
2919 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002920 if (gen_load_outer)
2921 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 }
2923
2924 *arg = end;
2925
2926theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002927 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 semsg(_(e_var_notfound), name);
2929 vim_free(name);
2930 return res;
2931}
2932
2933/*
2934 * Compile the argument expressions.
2935 * "arg" points to just after the "(" and is advanced to after the ")"
2936 */
2937 static int
2938compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2939{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002940 char_u *p = *arg;
2941 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942
Bram Moolenaare6085c52020-04-12 20:19:16 +02002943 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002945 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2946 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002947 if (*p == ')')
2948 {
2949 *arg = p + 1;
2950 return OK;
2951 }
2952
Bram Moolenaara5565e42020-05-09 15:44:01 +02002953 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 return FAIL;
2955 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002956
2957 if (*p != ',' && *skipwhite(p) == ',')
2958 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002959 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002960 p = skipwhite(p);
2961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002963 {
2964 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002965 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002966 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002967 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002968 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002969 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002971failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002972 emsg(_(e_missing_close));
2973 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974}
2975
2976/*
2977 * Compile a function call: name(arg1, arg2)
2978 * "arg" points to "name", "arg + varlen" to the "(".
2979 * "argcount_init" is 1 for "value->method()"
2980 * Instructions:
2981 * EVAL arg1
2982 * EVAL arg2
2983 * BCALL / DCALL / UCALL
2984 */
2985 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002986compile_call(
2987 char_u **arg,
2988 size_t varlen,
2989 cctx_T *cctx,
2990 ppconst_T *ppconst,
2991 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992{
2993 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002994 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 int argcount = argcount_init;
2996 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002997 char_u fname_buf[FLEN_FIXED + 1];
2998 char_u *tofree = NULL;
2999 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003001 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002
Bram Moolenaara5565e42020-05-09 15:44:01 +02003003 // we can evaluate "has('name')" at compile time
3004 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3005 {
3006 char_u *s = skipwhite(*arg + varlen + 1);
3007 typval_T argvars[2];
3008
3009 argvars[0].v_type = VAR_UNKNOWN;
3010 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003011 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003012 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003013 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003014 s = skipwhite(s);
3015 if (*s == ')' && argvars[0].v_type == VAR_STRING)
3016 {
3017 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3018
3019 *arg = s + 1;
3020 argvars[1].v_type = VAR_UNKNOWN;
3021 tv->v_type = VAR_NUMBER;
3022 tv->vval.v_number = 0;
3023 f_has(argvars, tv);
3024 clear_tv(&argvars[0]);
3025 ++ppconst->pp_used;
3026 return OK;
3027 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003028 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003029 }
3030
3031 if (generate_ppconst(cctx, ppconst) == FAIL)
3032 return FAIL;
3033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 if (varlen >= sizeof(namebuf))
3035 {
3036 semsg(_("E1011: name too long: %s"), name);
3037 return FAIL;
3038 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003039 vim_strncpy(namebuf, *arg, varlen);
3040 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041
3042 *arg = skipwhite(*arg + varlen + 1);
3043 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003044 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003046 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 {
3048 int idx;
3049
3050 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003051 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003053 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003054 else
3055 semsg(_(e_unknownfunc), namebuf);
3056 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 }
3058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003060 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003062 {
3063 res = generate_CALL(cctx, ufunc, argcount);
3064 goto theend;
3065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066
3067 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003068 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003070 if (STRNCMP(namebuf, "g:", 2) != 0
3071 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003072 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003073 garray_T *stack = &cctx->ctx_type_stack;
3074 type_T *type;
3075
3076 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3077 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003078 goto theend;
3079 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003081 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003082 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003083 if (STRNCMP(namebuf, "g:", 2) == 0)
3084 res = generate_UCALL(cctx, name, argcount);
3085 else
3086 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003087
3088theend:
3089 vim_free(tofree);
3090 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091}
3092
3093// like NAMESPACE_CHAR but with 'a' and 'l'.
3094#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3095
3096/*
3097 * Find the end of a variable or function name. Unlike find_name_end() this
3098 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003099 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 * Return a pointer to just after the name. Equal to "arg" if there is no
3101 * valid name.
3102 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003103 static char_u *
3104to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105{
3106 char_u *p;
3107
3108 // Quick check for valid starting character.
3109 if (!eval_isnamec1(*arg))
3110 return arg;
3111
3112 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3113 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3114 // and can be used in slice "[n:]".
3115 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003116 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3118 break;
3119 return p;
3120}
3121
3122/*
3123 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003124 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 */
3126 char_u *
3127to_name_const_end(char_u *arg)
3128{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003129 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 typval_T rettv;
3131
3132 if (p == arg && *arg == '[')
3133 {
3134
3135 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003136 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 p = arg;
3138 }
3139 else if (p == arg && *arg == '#' && arg[1] == '{')
3140 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003141 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003143 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 p = arg;
3145 }
3146 else if (p == arg && *arg == '{')
3147 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003148 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003150 // Can be "{x -> ret}()".
3151 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003153 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 if (ret != OK)
3155 p = arg;
3156 }
3157
3158 return p;
3159}
3160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161/*
3162 * parse a list: [expr, expr]
3163 * "*arg" points to the '['.
3164 */
3165 static int
3166compile_list(char_u **arg, cctx_T *cctx)
3167{
3168 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003169 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 int count = 0;
3171
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003172 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003174 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003175 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003176 semsg(_(e_list_end), *arg);
3177 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003178 }
3179 if (*p == ']')
3180 {
3181 ++p;
3182 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003183 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003184 p += STRLEN(p);
3185 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003186 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003187 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 break;
3189 ++count;
3190 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003191 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003193 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3194 {
3195 semsg(_(e_white_after), ",");
3196 return FAIL;
3197 }
3198 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003199 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 p = skipwhite(p);
3201 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003202 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203
3204 generate_NEWLIST(cctx, count);
3205 return OK;
3206}
3207
3208/*
3209 * parse a lambda: {arg, arg -> expr}
3210 * "*arg" points to the '{'.
3211 */
3212 static int
3213compile_lambda(char_u **arg, cctx_T *cctx)
3214{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 typval_T rettv;
3216 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003217 evalarg_T evalarg;
3218
3219 CLEAR_FIELD(evalarg);
3220 evalarg.eval_flags = EVAL_EVALUATE;
3221 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222
3223 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003224 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003226
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003228 ++ufunc->uf_refcount;
3229 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003230 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231
3232 // The function will have one line: "return {expr}".
3233 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003234 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003236 clear_evalarg(&evalarg, NULL);
3237
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003238 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003239 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003240
3241 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 return FAIL;
3243}
3244
3245/*
3246 * Compile a lamda call: expr->{lambda}(args)
3247 * "arg" points to the "{".
3248 */
3249 static int
3250compile_lambda_call(char_u **arg, cctx_T *cctx)
3251{
3252 ufunc_T *ufunc;
3253 typval_T rettv;
3254 int argcount = 1;
3255 int ret = FAIL;
3256
3257 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003258 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 return FAIL;
3260
3261 if (**arg != '(')
3262 {
3263 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003264 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 else
3266 semsg(_(e_missing_paren), "lambda");
3267 clear_tv(&rettv);
3268 return FAIL;
3269 }
3270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 ufunc = rettv.vval.v_partial->pt_func;
3272 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003273 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003274 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003275
3276 // The function will have one line: "return {expr}".
3277 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003278 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279
3280 // compile the arguments
3281 *arg = skipwhite(*arg + 1);
3282 if (compile_arguments(arg, cctx, &argcount) == OK)
3283 // call the compiled function
3284 ret = generate_CALL(cctx, ufunc, argcount);
3285
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003286 if (ret == FAIL)
3287 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 return ret;
3289}
3290
3291/*
3292 * parse a dict: {'key': val} or #{key: val}
3293 * "*arg" points to the '{'.
3294 */
3295 static int
3296compile_dict(char_u **arg, cctx_T *cctx, int literal)
3297{
3298 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003299 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 int count = 0;
3301 dict_T *d = dict_alloc();
3302 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003303 char_u *whitep = *arg;
3304 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305
3306 if (d == NULL)
3307 return FAIL;
3308 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003309 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 {
3311 char_u *key = NULL;
3312
Bram Moolenaar23c55272020-06-21 16:58:13 +02003313 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003314 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003315 *arg = NULL;
3316 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003317 }
3318
3319 if (**arg == '}')
3320 break;
3321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 if (literal)
3323 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003324 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325
Bram Moolenaar2c330432020-04-13 14:41:35 +02003326 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 {
3328 semsg(_("E1014: Invalid key: %s"), *arg);
3329 return FAIL;
3330 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003331 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 if (generate_PUSHS(cctx, key) == FAIL)
3333 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003334 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 }
3336 else
3337 {
3338 isn_T *isn;
3339
Bram Moolenaara5565e42020-05-09 15:44:01 +02003340 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3343 if (isn->isn_type == ISN_PUSHS)
3344 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003345 else
3346 {
3347 type_T *keytype = ((type_T **)stack->ga_data)
3348 [stack->ga_len - 1];
3349 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3350 return FAIL;
3351 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 }
3353
3354 // Check for duplicate keys, if using string keys.
3355 if (key != NULL)
3356 {
3357 item = dict_find(d, key, -1);
3358 if (item != NULL)
3359 {
3360 semsg(_(e_duplicate_key), key);
3361 goto failret;
3362 }
3363 item = dictitem_alloc(key);
3364 if (item != NULL)
3365 {
3366 item->di_tv.v_type = VAR_UNKNOWN;
3367 item->di_tv.v_lock = 0;
3368 if (dict_add(d, item) == FAIL)
3369 dictitem_free(item);
3370 }
3371 }
3372
3373 *arg = skipwhite(*arg);
3374 if (**arg != ':')
3375 {
3376 semsg(_(e_missing_dict_colon), *arg);
3377 return FAIL;
3378 }
3379
Bram Moolenaar2c330432020-04-13 14:41:35 +02003380 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003382 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003383 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003384 *arg = NULL;
3385 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003386 }
3387
Bram Moolenaara5565e42020-05-09 15:44:01 +02003388 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 return FAIL;
3390 ++count;
3391
Bram Moolenaar2c330432020-04-13 14:41:35 +02003392 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003393 *arg = skipwhite(*arg);
3394 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003395 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003396 *arg = NULL;
3397 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 if (**arg == '}')
3400 break;
3401 if (**arg != ',')
3402 {
3403 semsg(_(e_missing_dict_comma), *arg);
3404 goto failret;
3405 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003406 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 *arg = skipwhite(*arg + 1);
3408 }
3409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 *arg = *arg + 1;
3411
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003412 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003413 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003414 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003415 *arg += STRLEN(*arg);
3416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 dict_unref(d);
3418 return generate_NEWDICT(cctx, count);
3419
3420failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003421 if (*arg == NULL)
3422 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 dict_unref(d);
3424 return FAIL;
3425}
3426
3427/*
3428 * Compile "&option".
3429 */
3430 static int
3431compile_get_option(char_u **arg, cctx_T *cctx)
3432{
3433 typval_T rettv;
3434 char_u *start = *arg;
3435 int ret;
3436
3437 // parse the option and get the current value to get the type.
3438 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003439 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 if (ret == OK)
3441 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003442 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 char_u *name = vim_strnsave(start, *arg - start);
3444 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3445
3446 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3447 vim_free(name);
3448 }
3449 clear_tv(&rettv);
3450
3451 return ret;
3452}
3453
3454/*
3455 * Compile "$VAR".
3456 */
3457 static int
3458compile_get_env(char_u **arg, cctx_T *cctx)
3459{
3460 char_u *start = *arg;
3461 int len;
3462 int ret;
3463 char_u *name;
3464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 ++*arg;
3466 len = get_env_len(arg);
3467 if (len == 0)
3468 {
3469 semsg(_(e_syntax_at), start - 1);
3470 return FAIL;
3471 }
3472
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003473 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 name = vim_strnsave(start, len + 1);
3475 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3476 vim_free(name);
3477 return ret;
3478}
3479
3480/*
3481 * Compile "@r".
3482 */
3483 static int
3484compile_get_register(char_u **arg, cctx_T *cctx)
3485{
3486 int ret;
3487
3488 ++*arg;
3489 if (**arg == NUL)
3490 {
3491 semsg(_(e_syntax_at), *arg - 1);
3492 return FAIL;
3493 }
3494 if (!valid_yank_reg(**arg, TRUE))
3495 {
3496 emsg_invreg(**arg);
3497 return FAIL;
3498 }
3499 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3500 ++*arg;
3501 return ret;
3502}
3503
3504/*
3505 * Apply leading '!', '-' and '+' to constant "rettv".
3506 */
3507 static int
3508apply_leader(typval_T *rettv, char_u *start, char_u *end)
3509{
3510 char_u *p = end;
3511
3512 // this works from end to start
3513 while (p > start)
3514 {
3515 --p;
3516 if (*p == '-' || *p == '+')
3517 {
3518 // only '-' has an effect, for '+' we only check the type
3519#ifdef FEAT_FLOAT
3520 if (rettv->v_type == VAR_FLOAT)
3521 {
3522 if (*p == '-')
3523 rettv->vval.v_float = -rettv->vval.v_float;
3524 }
3525 else
3526#endif
3527 {
3528 varnumber_T val;
3529 int error = FALSE;
3530
3531 // tv_get_number_chk() accepts a string, but we don't want that
3532 // here
3533 if (check_not_string(rettv) == FAIL)
3534 return FAIL;
3535 val = tv_get_number_chk(rettv, &error);
3536 clear_tv(rettv);
3537 if (error)
3538 return FAIL;
3539 if (*p == '-')
3540 val = -val;
3541 rettv->v_type = VAR_NUMBER;
3542 rettv->vval.v_number = val;
3543 }
3544 }
3545 else
3546 {
3547 int v = tv2bool(rettv);
3548
3549 // '!' is permissive in the type.
3550 clear_tv(rettv);
3551 rettv->v_type = VAR_BOOL;
3552 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3553 }
3554 }
3555 return OK;
3556}
3557
3558/*
3559 * Recognize v: variables that are constants and set "rettv".
3560 */
3561 static void
3562get_vim_constant(char_u **arg, typval_T *rettv)
3563{
3564 if (STRNCMP(*arg, "v:true", 6) == 0)
3565 {
3566 rettv->v_type = VAR_BOOL;
3567 rettv->vval.v_number = VVAL_TRUE;
3568 *arg += 6;
3569 }
3570 else if (STRNCMP(*arg, "v:false", 7) == 0)
3571 {
3572 rettv->v_type = VAR_BOOL;
3573 rettv->vval.v_number = VVAL_FALSE;
3574 *arg += 7;
3575 }
3576 else if (STRNCMP(*arg, "v:null", 6) == 0)
3577 {
3578 rettv->v_type = VAR_SPECIAL;
3579 rettv->vval.v_number = VVAL_NULL;
3580 *arg += 6;
3581 }
3582 else if (STRNCMP(*arg, "v:none", 6) == 0)
3583 {
3584 rettv->v_type = VAR_SPECIAL;
3585 rettv->vval.v_number = VVAL_NONE;
3586 *arg += 6;
3587 }
3588}
3589
Bram Moolenaar61a89812020-05-07 16:58:17 +02003590 static exptype_T
3591get_compare_type(char_u *p, int *len, int *type_is)
3592{
3593 exptype_T type = EXPR_UNKNOWN;
3594 int i;
3595
3596 switch (p[0])
3597 {
3598 case '=': if (p[1] == '=')
3599 type = EXPR_EQUAL;
3600 else if (p[1] == '~')
3601 type = EXPR_MATCH;
3602 break;
3603 case '!': if (p[1] == '=')
3604 type = EXPR_NEQUAL;
3605 else if (p[1] == '~')
3606 type = EXPR_NOMATCH;
3607 break;
3608 case '>': if (p[1] != '=')
3609 {
3610 type = EXPR_GREATER;
3611 *len = 1;
3612 }
3613 else
3614 type = EXPR_GEQUAL;
3615 break;
3616 case '<': if (p[1] != '=')
3617 {
3618 type = EXPR_SMALLER;
3619 *len = 1;
3620 }
3621 else
3622 type = EXPR_SEQUAL;
3623 break;
3624 case 'i': if (p[1] == 's')
3625 {
3626 // "is" and "isnot"; but not a prefix of a name
3627 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3628 *len = 5;
3629 i = p[*len];
3630 if (!isalnum(i) && i != '_')
3631 {
3632 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3633 *type_is = TRUE;
3634 }
3635 }
3636 break;
3637 }
3638 return type;
3639}
3640
3641/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 * Compile code to apply '-', '+' and '!'.
3643 */
3644 static int
3645compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3646{
3647 char_u *p = end;
3648
3649 // this works from end to start
3650 while (p > start)
3651 {
3652 --p;
3653 if (*p == '-' || *p == '+')
3654 {
3655 int negate = *p == '-';
3656 isn_T *isn;
3657
3658 // TODO: check type
3659 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3660 {
3661 --p;
3662 if (*p == '-')
3663 negate = !negate;
3664 }
3665 // only '-' has an effect, for '+' we only check the type
3666 if (negate)
3667 isn = generate_instr(cctx, ISN_NEGATENR);
3668 else
3669 isn = generate_instr(cctx, ISN_CHECKNR);
3670 if (isn == NULL)
3671 return FAIL;
3672 }
3673 else
3674 {
3675 int invert = TRUE;
3676
3677 while (p > start && p[-1] == '!')
3678 {
3679 --p;
3680 invert = !invert;
3681 }
3682 if (generate_2BOOL(cctx, invert) == FAIL)
3683 return FAIL;
3684 }
3685 }
3686 return OK;
3687}
3688
3689/*
3690 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003691 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 */
3693 static int
3694compile_subscript(
3695 char_u **arg,
3696 cctx_T *cctx,
3697 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003698 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003699 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700{
3701 for (;;)
3702 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003703 char_u *p = skipwhite(*arg);
3704
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003705 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003706 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003707 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003708
3709 // If a following line starts with "->{" or "->X" advance to that
3710 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003711 // Also if a following line starts with ".x".
3712 if (next != NULL &&
3713 ((next[0] == '-' && next[1] == '>'
3714 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3715 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003716 {
3717 next = next_line_from_context(cctx, TRUE);
3718 if (next == NULL)
3719 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003720 *arg = next;
3721 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003722 }
3723 }
3724
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003725 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003727 garray_T *stack = &cctx->ctx_type_stack;
3728 type_T *type;
3729 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003731 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003732 return FAIL;
3733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003735 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3736
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003737 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3739 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003740 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 return FAIL;
3742 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003743 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003745 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003746 return FAIL;
3747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 // something->method()
3749 // Apply the '!', '-' and '+' first:
3750 // -1.0->func() works like (-1.0)->func()
3751 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3752 return FAIL;
3753 *start_leader = end_leader; // don't apply again later
3754
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003755 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003756 *arg = skipwhite(p);
3757 if (may_get_next_line(p, arg, cctx) == FAIL)
3758 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 if (**arg == '{')
3760 {
3761 // lambda call: list->{lambda}
3762 if (compile_lambda_call(arg, cctx) == FAIL)
3763 return FAIL;
3764 }
3765 else
3766 {
3767 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003768 p = *arg;
3769 if (ASCII_ISALPHA(*p) && p[1] == ':')
3770 p += 2;
3771 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 ;
3773 if (*p != '(')
3774 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003775 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 return FAIL;
3777 }
3778 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003779 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 return FAIL;
3781 }
3782 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003783 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003785 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003786 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003787 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003788
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003789 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003790 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003791 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003792 // TODO: blob index
3793 // TODO: more arguments
3794 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003795 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003796 return FAIL;
3797
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003798 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003799 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003800 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003801 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003802 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 return FAIL;
3804
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003805 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3806 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 if (**arg != ']')
3808 {
3809 emsg(_(e_missbrac));
3810 return FAIL;
3811 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003812 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003814 // We can index a list and a dict. If we don't know the type
3815 // we can use the index value type.
3816 // TODO: If we don't know use an instruction to figure it out at
3817 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003818 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003819 vtype = (*typep)->tt_type;
3820 if (*typep == &t_any)
3821 {
3822 type_T *valtype = ((type_T **)stack->ga_data)
3823 [stack->ga_len - 1];
3824 if (valtype == &t_string)
3825 vtype = VAR_DICT;
3826 }
3827 if (vtype == VAR_DICT)
3828 {
3829 if ((*typep)->tt_type == VAR_DICT)
3830 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003831 else
3832 {
3833 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3834 return FAIL;
3835 *typep = &t_any;
3836 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003837 if (may_generate_2STRING(-1, cctx) == FAIL)
3838 return FAIL;
3839 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3840 return FAIL;
3841 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003842 else if (vtype == VAR_STRING)
3843 {
3844 *typep = &t_number;
3845 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3846 return FAIL;
3847 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003848 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003849 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003850 if ((*typep)->tt_type == VAR_LIST)
3851 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003852 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003853 return FAIL;
3854 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003855 else
3856 {
3857 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003858 return FAIL;
3859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003861 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003863 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003864 return FAIL;
3865
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003866 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003867 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3868 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003870 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 if (eval_isnamec1(*p))
3872 while (eval_isnamec(*p))
3873 MB_PTR_ADV(p);
3874 if (p == *arg)
3875 {
3876 semsg(_(e_syntax_at), *arg);
3877 return FAIL;
3878 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003879 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 return FAIL;
3881 *arg = p;
3882 }
3883 else
3884 break;
3885 }
3886
3887 // TODO - see handle_subscript():
3888 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3889 // Don't do this when "Func" is already a partial that was bound
3890 // explicitly (pt_auto is FALSE).
3891
3892 return OK;
3893}
3894
3895/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003896 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3897 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003899 * If the value is a constant "ppconst->pp_ret" will be set.
3900 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003901 *
3902 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 */
3904
3905/*
3906 * number number constant
3907 * 0zFFFFFFFF Blob constant
3908 * "string" string constant
3909 * 'string' literal string constant
3910 * &option-name option value
3911 * @r register contents
3912 * identifier variable value
3913 * function() function call
3914 * $VAR environment variable
3915 * (expression) nested expression
3916 * [expr, expr] List
3917 * {key: val, key: val} Dictionary
3918 * #{key: val, key: val} Dictionary with literal keys
3919 *
3920 * Also handle:
3921 * ! in front logical NOT
3922 * - in front unary minus
3923 * + in front unary plus (ignored)
3924 * trailing (arg) funcref/partial call
3925 * trailing [] subscript in String or List
3926 * trailing .name entry in Dictionary
3927 * trailing ->name() method call
3928 */
3929 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003930compile_expr7(
3931 char_u **arg,
3932 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003933 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 char_u *start_leader, *end_leader;
3936 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003937 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003938 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939
3940 /*
3941 * Skip '!', '-' and '+' characters. They are handled later.
3942 */
3943 start_leader = *arg;
3944 while (**arg == '!' || **arg == '-' || **arg == '+')
3945 *arg = skipwhite(*arg + 1);
3946 end_leader = *arg;
3947
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003948 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 switch (**arg)
3950 {
3951 /*
3952 * Number constant.
3953 */
3954 case '0': // also for blob starting with 0z
3955 case '1':
3956 case '2':
3957 case '3':
3958 case '4':
3959 case '5':
3960 case '6':
3961 case '7':
3962 case '8':
3963 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003964 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 return FAIL;
3966 break;
3967
3968 /*
3969 * String constant: "string".
3970 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003971 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 return FAIL;
3973 break;
3974
3975 /*
3976 * Literal string constant: 'str''ing'.
3977 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003978 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 return FAIL;
3980 break;
3981
3982 /*
3983 * Constant Vim variable.
3984 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003985 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 ret = NOTDONE;
3987 break;
3988
3989 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003990 * "true" constant
3991 */
3992 case 't': if (STRNCMP(*arg, "true", 4) == 0
3993 && !eval_isnamec((*arg)[4]))
3994 {
3995 *arg += 4;
3996 rettv->v_type = VAR_BOOL;
3997 rettv->vval.v_number = VVAL_TRUE;
3998 }
3999 else
4000 ret = NOTDONE;
4001 break;
4002
4003 /*
4004 * "false" constant
4005 */
4006 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4007 && !eval_isnamec((*arg)[5]))
4008 {
4009 *arg += 5;
4010 rettv->v_type = VAR_BOOL;
4011 rettv->vval.v_number = VVAL_FALSE;
4012 }
4013 else
4014 ret = NOTDONE;
4015 break;
4016
4017 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 * List: [expr, expr]
4019 */
4020 case '[': ret = compile_list(arg, cctx);
4021 break;
4022
4023 /*
4024 * Dictionary: #{key: val, key: val}
4025 */
4026 case '#': if ((*arg)[1] == '{')
4027 {
4028 ++*arg;
4029 ret = compile_dict(arg, cctx, TRUE);
4030 }
4031 else
4032 ret = NOTDONE;
4033 break;
4034
4035 /*
4036 * Lambda: {arg, arg -> expr}
4037 * Dictionary: {'key': val, 'key': val}
4038 */
4039 case '{': {
4040 char_u *start = skipwhite(*arg + 1);
4041
4042 // Find out what comes after the arguments.
4043 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004044 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 if (ret != FAIL && *start == '>')
4046 ret = compile_lambda(arg, cctx);
4047 else
4048 ret = compile_dict(arg, cctx, FALSE);
4049 }
4050 break;
4051
4052 /*
4053 * Option value: &name
4054 */
4055 case '&': ret = compile_get_option(arg, cctx);
4056 break;
4057
4058 /*
4059 * Environment variable: $VAR.
4060 */
4061 case '$': ret = compile_get_env(arg, cctx);
4062 break;
4063
4064 /*
4065 * Register contents: @r.
4066 */
4067 case '@': ret = compile_get_register(arg, cctx);
4068 break;
4069 /*
4070 * nested expression: (expression).
4071 */
4072 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004073
4074 // recursive!
4075 if (ppconst->pp_used <= PPSIZE - 10)
4076 {
4077 ret = compile_expr1(arg, cctx, ppconst);
4078 }
4079 else
4080 {
4081 // Not enough space in ppconst, flush constants.
4082 if (generate_ppconst(cctx, ppconst) == FAIL)
4083 return FAIL;
4084 ret = compile_expr0(arg, cctx);
4085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 *arg = skipwhite(*arg);
4087 if (**arg == ')')
4088 ++*arg;
4089 else if (ret == OK)
4090 {
4091 emsg(_(e_missing_close));
4092 ret = FAIL;
4093 }
4094 break;
4095
4096 default: ret = NOTDONE;
4097 break;
4098 }
4099 if (ret == FAIL)
4100 return FAIL;
4101
Bram Moolenaar1c747212020-05-09 18:28:34 +02004102 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 {
4104 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004105 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004107 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 return FAIL;
4109 }
4110 start_leader = end_leader; // don't apply again below
4111
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004112 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004113 clear_tv(rettv);
4114 else
4115 // A constant expression can possibly be handled compile time,
4116 // return the value instead of generating code.
4117 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 }
4119 else if (ret == NOTDONE)
4120 {
4121 char_u *p;
4122 int r;
4123
4124 if (!eval_isnamec1(**arg))
4125 {
4126 semsg(_("E1015: Name expected: %s"), *arg);
4127 return FAIL;
4128 }
4129
4130 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004131 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004133 {
4134 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004137 {
4138 if (generate_ppconst(cctx, ppconst) == FAIL)
4139 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 if (r == FAIL)
4143 return FAIL;
4144 }
4145
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004146 // Handle following "[]", ".member", etc.
4147 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004148 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004149 ppconst) == FAIL)
4150 return FAIL;
4151 if (ppconst->pp_used > 0)
4152 {
4153 // apply the '!', '-' and '+' before the constant
4154 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4155 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4156 return FAIL;
4157 return OK;
4158 }
4159 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004161 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162}
4163
4164/*
4165 * * number multiplication
4166 * / number division
4167 * % number modulo
4168 */
4169 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004170compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171{
4172 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004173 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004174 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004176 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004177 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 return FAIL;
4179
4180 /*
4181 * Repeat computing, until no "*", "/" or "%" is following.
4182 */
4183 for (;;)
4184 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004185 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 if (*op != '*' && *op != '/' && *op != '%')
4187 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004188 if (next != NULL)
4189 {
4190 *arg = next_line_from_context(cctx, TRUE);
4191 op = skipwhite(*arg);
4192 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004193
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004194 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 {
4196 char_u buf[3];
4197
4198 vim_strncpy(buf, op, 1);
4199 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004200 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 }
4202 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004203 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004204 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004206 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004207 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004209
4210 if (ppconst->pp_used == ppconst_used + 2
4211 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4212 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004213 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004214 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4215 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004216 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004218 // both are numbers: compute the result
4219 switch (*op)
4220 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004221 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004222 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004223 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004224 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004225 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004226 break;
4227 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004228 tv1->vval.v_number = res;
4229 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004230 }
4231 else
4232 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004233 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004234 generate_two_op(cctx, op);
4235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 }
4237
4238 return OK;
4239}
4240
4241/*
4242 * + number addition
4243 * - number subtraction
4244 * .. string concatenation
4245 */
4246 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004247compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248{
4249 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004250 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004252 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253
4254 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004255 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 return FAIL;
4257
4258 /*
4259 * Repeat computing, until no "+", "-" or ".." is following.
4260 */
4261 for (;;)
4262 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004263 op = may_peek_next_line(cctx, *arg, &next);
4264 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 break;
4266 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004267 if (next != NULL)
4268 {
4269 *arg = next_line_from_context(cctx, TRUE);
4270 op = skipwhite(*arg);
4271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004273 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 {
4275 char_u buf[3];
4276
4277 vim_strncpy(buf, op, oplen);
4278 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004279 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 }
4281
4282 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004283 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004284 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004286 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004287 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 return FAIL;
4289
Bram Moolenaara5565e42020-05-09 15:44:01 +02004290 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004291 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004292 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4293 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4294 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4295 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004297 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4298 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004299
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004300 // concat/subtract/add constant numbers
4301 if (*op == '+')
4302 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4303 else if (*op == '-')
4304 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4305 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004306 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004307 // concatenate constant strings
4308 char_u *s1 = tv1->vval.v_string;
4309 char_u *s2 = tv2->vval.v_string;
4310 size_t len1 = STRLEN(s1);
4311
4312 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4313 if (tv1->vval.v_string == NULL)
4314 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004315 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004316 return FAIL;
4317 }
4318 mch_memmove(tv1->vval.v_string, s1, len1);
4319 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004320 vim_free(s1);
4321 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004322 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004323 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 }
4325 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004326 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004327 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004328 if (*op == '.')
4329 {
4330 if (may_generate_2STRING(-2, cctx) == FAIL
4331 || may_generate_2STRING(-1, cctx) == FAIL)
4332 return FAIL;
4333 generate_instr_drop(cctx, ISN_CONCAT, 1);
4334 }
4335 else
4336 generate_two_op(cctx, op);
4337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 }
4339
4340 return OK;
4341}
4342
4343/*
4344 * expr5a == expr5b
4345 * expr5a =~ expr5b
4346 * expr5a != expr5b
4347 * expr5a !~ expr5b
4348 * expr5a > expr5b
4349 * expr5a >= expr5b
4350 * expr5a < expr5b
4351 * expr5a <= expr5b
4352 * expr5a is expr5b
4353 * expr5a isnot expr5b
4354 *
4355 * Produces instructions:
4356 * EVAL expr5a Push result of "expr5a"
4357 * EVAL expr5b Push result of "expr5b"
4358 * COMPARE one of the compare instructions
4359 */
4360 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004361compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362{
4363 exptype_T type = EXPR_UNKNOWN;
4364 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004365 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004368 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369
4370 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004371 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 return FAIL;
4373
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004374 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004375 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376
4377 /*
4378 * If there is a comparative operator, use it.
4379 */
4380 if (type != EXPR_UNKNOWN)
4381 {
4382 int ic = FALSE; // Default: do not ignore case
4383
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004384 if (next != NULL)
4385 {
4386 *arg = next_line_from_context(cctx, TRUE);
4387 p = skipwhite(*arg);
4388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 if (type_is && (p[len] == '?' || p[len] == '#'))
4390 {
4391 semsg(_(e_invexpr2), *arg);
4392 return FAIL;
4393 }
4394 // extra question mark appended: ignore case
4395 if (p[len] == '?')
4396 {
4397 ic = TRUE;
4398 ++len;
4399 }
4400 // extra '#' appended: match case (ignored)
4401 else if (p[len] == '#')
4402 ++len;
4403 // nothing appended: match case
4404
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004405 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 {
4407 char_u buf[7];
4408
4409 vim_strncpy(buf, p, len);
4410 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004411 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 }
4413
4414 // get the second variable
4415 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004416 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004417 return FAIL;
4418
Bram Moolenaara5565e42020-05-09 15:44:01 +02004419 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 return FAIL;
4421
Bram Moolenaara5565e42020-05-09 15:44:01 +02004422 if (ppconst->pp_used == ppconst_used + 2)
4423 {
4424 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4425 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4426 int ret;
4427
4428 // Both sides are a constant, compute the result now.
4429 // First check for a valid combination of types, this is more
4430 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004431 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004432 ret = FAIL;
4433 else
4434 {
4435 ret = typval_compare(tv1, tv2, type, ic);
4436 tv1->v_type = VAR_BOOL;
4437 tv1->vval.v_number = tv1->vval.v_number
4438 ? VVAL_TRUE : VVAL_FALSE;
4439 clear_tv(tv2);
4440 --ppconst->pp_used;
4441 }
4442 return ret;
4443 }
4444
4445 generate_ppconst(cctx, ppconst);
4446 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 }
4448
4449 return OK;
4450}
4451
Bram Moolenaar7f141552020-05-09 17:35:53 +02004452static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454/*
4455 * Compile || or &&.
4456 */
4457 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004458compile_and_or(
4459 char_u **arg,
4460 cctx_T *cctx,
4461 char *op,
4462 ppconst_T *ppconst,
4463 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004465 char_u *next;
4466 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 int opchar = *op;
4468
4469 if (p[0] == opchar && p[1] == opchar)
4470 {
4471 garray_T *instr = &cctx->ctx_instr;
4472 garray_T end_ga;
4473
4474 /*
4475 * Repeat until there is no following "||" or "&&"
4476 */
4477 ga_init2(&end_ga, sizeof(int), 10);
4478 while (p[0] == opchar && p[1] == opchar)
4479 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004480 if (next != NULL)
4481 {
4482 *arg = next_line_from_context(cctx, TRUE);
4483 p = skipwhite(*arg);
4484 }
4485
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004486 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4487 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004489 return FAIL;
4490 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491
Bram Moolenaara5565e42020-05-09 15:44:01 +02004492 // TODO: use ppconst if the value is a constant
4493 generate_ppconst(cctx, ppconst);
4494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 if (ga_grow(&end_ga, 1) == FAIL)
4496 {
4497 ga_clear(&end_ga);
4498 return FAIL;
4499 }
4500 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4501 ++end_ga.ga_len;
4502 generate_JUMP(cctx, opchar == '|'
4503 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4504
4505 // eval the next expression
4506 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004507 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004508 return FAIL;
4509
Bram Moolenaara5565e42020-05-09 15:44:01 +02004510 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4511 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 {
4513 ga_clear(&end_ga);
4514 return FAIL;
4515 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004516
4517 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004519 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520
4521 // Fill in the end label in all jumps.
4522 while (end_ga.ga_len > 0)
4523 {
4524 isn_T *isn;
4525
4526 --end_ga.ga_len;
4527 isn = ((isn_T *)instr->ga_data)
4528 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4529 isn->isn_arg.jump.jump_where = instr->ga_len;
4530 }
4531 ga_clear(&end_ga);
4532 }
4533
4534 return OK;
4535}
4536
4537/*
4538 * expr4a && expr4a && expr4a logical AND
4539 *
4540 * Produces instructions:
4541 * EVAL expr4a Push result of "expr4a"
4542 * JUMP_AND_KEEP_IF_FALSE end
4543 * EVAL expr4b Push result of "expr4b"
4544 * JUMP_AND_KEEP_IF_FALSE end
4545 * EVAL expr4c Push result of "expr4c"
4546 * end:
4547 */
4548 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004549compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004551 int ppconst_used = ppconst->pp_used;
4552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 return FAIL;
4556
4557 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004558 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559}
4560
4561/*
4562 * expr3a || expr3b || expr3c logical OR
4563 *
4564 * Produces instructions:
4565 * EVAL expr3a Push result of "expr3a"
4566 * JUMP_AND_KEEP_IF_TRUE end
4567 * EVAL expr3b Push result of "expr3b"
4568 * JUMP_AND_KEEP_IF_TRUE end
4569 * EVAL expr3c Push result of "expr3c"
4570 * end:
4571 */
4572 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004573compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004575 int ppconst_used = ppconst->pp_used;
4576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004578 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 return FAIL;
4580
4581 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004582 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583}
4584
4585/*
4586 * Toplevel expression: expr2 ? expr1a : expr1b
4587 *
4588 * Produces instructions:
4589 * EVAL expr2 Push result of "expr"
4590 * JUMP_IF_FALSE alt jump if false
4591 * EVAL expr1a
4592 * JUMP_ALWAYS end
4593 * alt: EVAL expr1b
4594 * end:
4595 */
4596 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598{
4599 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004600 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004601 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602
Bram Moolenaar61a89812020-05-07 16:58:17 +02004603 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004604 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 return FAIL;
4606
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004607 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 if (*p == '?')
4609 {
4610 garray_T *instr = &cctx->ctx_instr;
4611 garray_T *stack = &cctx->ctx_type_stack;
4612 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004613 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004615 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004617 int has_const_expr = FALSE;
4618 int const_value = FALSE;
4619 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004621 if (next != NULL)
4622 {
4623 *arg = next_line_from_context(cctx, TRUE);
4624 p = skipwhite(*arg);
4625 }
4626
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004627 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4628 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004630 return FAIL;
4631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632
Bram Moolenaara5565e42020-05-09 15:44:01 +02004633 if (ppconst->pp_used == ppconst_used + 1)
4634 {
4635 // the condition is a constant, we know whether the ? or the :
4636 // expression is to be evaluated.
4637 has_const_expr = TRUE;
4638 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4639 clear_tv(&ppconst->pp_tv[ppconst_used]);
4640 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004641 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4642 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004643 }
4644 else
4645 {
4646 generate_ppconst(cctx, ppconst);
4647 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649
4650 // evaluate the second expression; any type is accepted
4651 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004652 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004653 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004654 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004655 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656
Bram Moolenaara5565e42020-05-09 15:44:01 +02004657 if (!has_const_expr)
4658 {
4659 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660
Bram Moolenaara5565e42020-05-09 15:44:01 +02004661 // remember the type and drop it
4662 --stack->ga_len;
4663 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664
Bram Moolenaara5565e42020-05-09 15:44:01 +02004665 end_idx = instr->ga_len;
4666 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4667
4668 // jump here from JUMP_IF_FALSE
4669 isn = ((isn_T *)instr->ga_data) + alt_idx;
4670 isn->isn_arg.jump.jump_where = instr->ga_len;
4671 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672
4673 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004674 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 if (*p != ':')
4676 {
4677 emsg(_(e_missing_colon));
4678 return FAIL;
4679 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004680 if (next != NULL)
4681 {
4682 *arg = next_line_from_context(cctx, TRUE);
4683 p = skipwhite(*arg);
4684 }
4685
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004686 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4687 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004689 return FAIL;
4690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691
4692 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004693 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004694 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4695 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004697 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004698 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004699 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004700 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701
Bram Moolenaara5565e42020-05-09 15:44:01 +02004702 if (!has_const_expr)
4703 {
4704 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705
Bram Moolenaara5565e42020-05-09 15:44:01 +02004706 // If the types differ, the result has a more generic type.
4707 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4708 common_type(type1, type2, &type2, cctx->ctx_type_list);
4709
4710 // jump here from JUMP_ALWAYS
4711 isn = ((isn_T *)instr->ga_data) + end_idx;
4712 isn->isn_arg.jump.jump_where = instr->ga_len;
4713 }
4714
4715 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 }
4717 return OK;
4718}
4719
4720/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004721 * Toplevel expression.
4722 */
4723 static int
4724compile_expr0(char_u **arg, cctx_T *cctx)
4725{
4726 ppconst_T ppconst;
4727
4728 CLEAR_FIELD(ppconst);
4729 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4730 {
4731 clear_ppconst(&ppconst);
4732 return FAIL;
4733 }
4734 if (generate_ppconst(cctx, &ppconst) == FAIL)
4735 return FAIL;
4736 return OK;
4737}
4738
4739/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 * compile "return [expr]"
4741 */
4742 static char_u *
4743compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4744{
4745 char_u *p = arg;
4746 garray_T *stack = &cctx->ctx_type_stack;
4747 type_T *stack_type;
4748
4749 if (*p != NUL && *p != '|' && *p != '\n')
4750 {
4751 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004752 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 return NULL;
4754
4755 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4756 if (set_return_type)
4757 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004758 else
4759 {
4760 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4761 && stack_type->tt_type != VAR_VOID
4762 && stack_type->tt_type != VAR_UNKNOWN)
4763 {
4764 emsg(_("E1096: Returning a value in a function without a return type"));
4765 return NULL;
4766 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004767 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4768 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004770 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 }
4772 else
4773 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004774 // "set_return_type" cannot be TRUE, only used for a lambda which
4775 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004776 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4777 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 {
4779 emsg(_("E1003: Missing return value"));
4780 return NULL;
4781 }
4782
4783 // No argument, return zero.
4784 generate_PUSHNR(cctx, 0);
4785 }
4786
4787 if (generate_instr(cctx, ISN_RETURN) == NULL)
4788 return NULL;
4789
4790 // "return val | endif" is possible
4791 return skipwhite(p);
4792}
4793
4794/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004795 * Get a line from the compilation context, compatible with exarg_T getline().
4796 * Return a pointer to the line in allocated memory.
4797 * Return NULL for end-of-file or some error.
4798 */
4799 static char_u *
4800exarg_getline(
4801 int c UNUSED,
4802 void *cookie,
4803 int indent UNUSED,
4804 int do_concat UNUSED)
4805{
4806 cctx_T *cctx = (cctx_T *)cookie;
4807
4808 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4809 {
4810 iemsg("Heredoc got to end");
4811 return NULL;
4812 }
4813 ++cctx->ctx_lnum;
4814 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4815 [cctx->ctx_lnum]);
4816}
4817
4818/*
4819 * Compile a nested :def command.
4820 */
4821 static char_u *
4822compile_nested_function(exarg_T *eap, cctx_T *cctx)
4823{
4824 char_u *name_start = eap->arg;
4825 char_u *name_end = to_name_end(eap->arg, FALSE);
4826 char_u *name = get_lambda_name();
4827 lvar_T *lvar;
4828 ufunc_T *ufunc;
4829
4830 eap->arg = name_end;
4831 eap->getline = exarg_getline;
4832 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004833 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004834 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004835 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004836
Bram Moolenaar822ba242020-05-24 23:00:18 +02004837 if (ufunc == NULL)
4838 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004839 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004840 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004841 return NULL;
4842
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004843 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004844 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004845 TRUE, ufunc->uf_func_type);
4846
4847 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4848 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4849 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004850
Bram Moolenaar61a89812020-05-07 16:58:17 +02004851 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004852 return (char_u *)"";
4853}
4854
4855/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 * Return the length of an assignment operator, or zero if there isn't one.
4857 */
4858 int
4859assignment_len(char_u *p, int *heredoc)
4860{
4861 if (*p == '=')
4862 {
4863 if (p[1] == '<' && p[2] == '<')
4864 {
4865 *heredoc = TRUE;
4866 return 3;
4867 }
4868 return 1;
4869 }
4870 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4871 return 2;
4872 if (STRNCMP(p, "..=", 3) == 0)
4873 return 3;
4874 return 0;
4875}
4876
4877// words that cannot be used as a variable
4878static char *reserved[] = {
4879 "true",
4880 "false",
4881 NULL
4882};
4883
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004884typedef enum {
4885 dest_local,
4886 dest_option,
4887 dest_env,
4888 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004889 dest_buffer,
4890 dest_window,
4891 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004892 dest_vimvar,
4893 dest_script,
4894 dest_reg,
4895} assign_dest_T;
4896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004898 * Generate the load instruction for "name".
4899 */
4900 static void
4901generate_loadvar(
4902 cctx_T *cctx,
4903 assign_dest_T dest,
4904 char_u *name,
4905 lvar_T *lvar,
4906 type_T *type)
4907{
4908 switch (dest)
4909 {
4910 case dest_option:
4911 // TODO: check the option exists
4912 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4913 break;
4914 case dest_global:
4915 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4916 break;
4917 case dest_buffer:
4918 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4919 break;
4920 case dest_window:
4921 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4922 break;
4923 case dest_tab:
4924 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4925 break;
4926 case dest_script:
4927 compile_load_scriptvar(cctx,
4928 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4929 break;
4930 case dest_env:
4931 // Include $ in the name here
4932 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4933 break;
4934 case dest_reg:
4935 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4936 break;
4937 case dest_vimvar:
4938 generate_LOADV(cctx, name + 2, TRUE);
4939 break;
4940 case dest_local:
4941 if (lvar->lv_from_outer)
4942 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4943 NULL, type);
4944 else
4945 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4946 break;
4947 }
4948}
4949
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004950 void
4951vim9_declare_error(char_u *name)
4952{
4953 char *scope = "";
4954
4955 switch (*name)
4956 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004957 case 'g': scope = _("global"); break;
4958 case 'b': scope = _("buffer"); break;
4959 case 'w': scope = _("window"); break;
4960 case 't': scope = _("tab"); break;
4961 case 'v': scope = "v:"; break;
4962 case '$': semsg(_(e_declare_env_var), name); return;
4963 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004964 }
4965 semsg(_(e_declare_var), scope, name);
4966}
4967
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004968/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004969 * Compile declaration and assignment:
4970 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004972 * Return NULL for an error.
4973 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 */
4975 static char_u *
4976compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4977{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004978 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004980 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 char_u *ret = NULL;
4982 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004983 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004986 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 int oplen = 0;
4989 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004990 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004991 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004992 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004996 // Skip over the "var" or "[var, var]" to get to any "=".
4997 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4998 if (p == NULL)
4999 return *arg == '[' ? arg : NULL;
5000
5001 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005003 // TODO: should we allow this, and figure out type inference from list
5004 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005005 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006 return NULL;
5007 }
5008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009 sp = p;
5010 p = skipwhite(p);
5011 op = p;
5012 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005013
5014 if (var_count > 0 && oplen == 0)
5015 // can be something like "[1, 2]->func()"
5016 return arg;
5017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5019 {
5020 char_u buf[4];
5021
5022 vim_strncpy(buf, op, oplen);
5023 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005024 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005025 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027 if (heredoc)
5028 {
5029 list_T *l;
5030 listitem_T *li;
5031
5032 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005033 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005035 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036
5037 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005038 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 {
5040 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5041 li->li_tv.vval.v_string = NULL;
5042 }
5043 generate_NEWLIST(cctx, l->lv_len);
5044 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005045 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 list_free(l);
5047 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005048 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005050 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005052 // for "[var, var] = expr" evaluate the expression here, loop over the
5053 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005054
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005055 p = skipwhite(op + oplen);
5056 if (compile_expr0(&p, cctx) == FAIL)
5057 return NULL;
5058 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005060 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005062 type_T *stacktype;
5063
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005064 stacktype = stack->ga_len == 0 ? &t_void
5065 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005066 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005068 emsg(_(e_cannot_use_void));
5069 goto theend;
5070 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005071 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005072 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005073 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005074 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5075 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005076 }
5077 }
5078
5079 /*
5080 * Loop over variables in "[var, var] = expr".
5081 * For "var = expr" and "let var: type" this is done only once.
5082 */
5083 if (var_count > 0)
5084 var_start = skipwhite(arg + 1); // skip over the "["
5085 else
5086 var_start = arg;
5087 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5088 {
5089 char_u *var_end = skip_var_one(var_start, FALSE);
5090 size_t varlen;
5091 int new_local = FALSE;
5092 int opt_type;
5093 int opt_flags = 0;
5094 assign_dest_T dest = dest_local;
5095 int vimvaridx = -1;
5096 lvar_T *lvar = NULL;
5097 lvar_T arg_lvar;
5098 int has_type = FALSE;
5099 int has_index = FALSE;
5100 int instr_count = -1;
5101
5102 p = (*var_start == '&' || *var_start == '$'
5103 || *var_start == '@') ? var_start + 1 : var_start;
5104 p = to_name_end(p, TRUE);
5105
5106 // "a: type" is declaring variable "a" with a type, not "a:".
5107 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5108 --var_end;
5109 if (is_decl && p == var_start + 2 && p[-1] == ':')
5110 --p;
5111
5112 varlen = p - var_start;
5113 vim_free(name);
5114 name = vim_strnsave(var_start, varlen);
5115 if (name == NULL)
5116 return NULL;
5117 if (!heredoc)
5118 type = &t_any;
5119
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005120 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005121 {
5122 if (*var_start == '&')
5123 {
5124 int cc;
5125 long numval;
5126
5127 dest = dest_option;
5128 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 emsg(_(e_const_option));
5131 goto theend;
5132 }
5133 if (is_decl)
5134 {
5135 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5136 goto theend;
5137 }
5138 p = var_start;
5139 p = find_option_end(&p, &opt_flags);
5140 if (p == NULL)
5141 {
5142 // cannot happen?
5143 emsg(_(e_letunexp));
5144 goto theend;
5145 }
5146 cc = *p;
5147 *p = NUL;
5148 opt_type = get_option_value(var_start + 1, &numval,
5149 NULL, opt_flags);
5150 *p = cc;
5151 if (opt_type == -3)
5152 {
5153 semsg(_(e_unknown_option), var_start);
5154 goto theend;
5155 }
5156 if (opt_type == -2 || opt_type == 0)
5157 type = &t_string;
5158 else
5159 type = &t_number; // both number and boolean option
5160 }
5161 else if (*var_start == '$')
5162 {
5163 dest = dest_env;
5164 type = &t_string;
5165 if (is_decl)
5166 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005167 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005168 goto theend;
5169 }
5170 }
5171 else if (*var_start == '@')
5172 {
5173 if (!valid_yank_reg(var_start[1], TRUE))
5174 {
5175 emsg_invreg(var_start[1]);
5176 goto theend;
5177 }
5178 dest = dest_reg;
5179 type = &t_string;
5180 if (is_decl)
5181 {
5182 semsg(_("E1066: Cannot declare a register: %s"), name);
5183 goto theend;
5184 }
5185 }
5186 else if (STRNCMP(var_start, "g:", 2) == 0)
5187 {
5188 dest = dest_global;
5189 if (is_decl)
5190 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005191 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005192 goto theend;
5193 }
5194 }
5195 else if (STRNCMP(var_start, "b:", 2) == 0)
5196 {
5197 dest = dest_buffer;
5198 if (is_decl)
5199 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005200 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005201 goto theend;
5202 }
5203 }
5204 else if (STRNCMP(var_start, "w:", 2) == 0)
5205 {
5206 dest = dest_window;
5207 if (is_decl)
5208 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005209 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005210 goto theend;
5211 }
5212 }
5213 else if (STRNCMP(var_start, "t:", 2) == 0)
5214 {
5215 dest = dest_tab;
5216 if (is_decl)
5217 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005218 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005219 goto theend;
5220 }
5221 }
5222 else if (STRNCMP(var_start, "v:", 2) == 0)
5223 {
5224 typval_T *vtv;
5225 int di_flags;
5226
5227 vimvaridx = find_vim_var(name + 2, &di_flags);
5228 if (vimvaridx < 0)
5229 {
5230 semsg(_(e_var_notfound), var_start);
5231 goto theend;
5232 }
5233 // We use the current value of "sandbox" here, is that OK?
5234 if (var_check_ro(di_flags, name, FALSE))
5235 goto theend;
5236 dest = dest_vimvar;
5237 vtv = get_vim_var_tv(vimvaridx);
5238 type = typval2type(vtv);
5239 if (is_decl)
5240 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005241 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005242 goto theend;
5243 }
5244 }
5245 else
5246 {
5247 int idx;
5248
5249 for (idx = 0; reserved[idx] != NULL; ++idx)
5250 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005251 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005252 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005253 goto theend;
5254 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005255
5256 lvar = lookup_local(var_start, varlen, cctx);
5257 if (lvar == NULL)
5258 {
5259 CLEAR_FIELD(arg_lvar);
5260 if (lookup_arg(var_start, varlen,
5261 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5262 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005263 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005264 if (is_decl)
5265 {
5266 semsg(_(e_used_as_arg), name);
5267 goto theend;
5268 }
5269 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005270 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005271 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005272 if (lvar != NULL)
5273 {
5274 if (is_decl)
5275 {
5276 semsg(_("E1017: Variable already declared: %s"), name);
5277 goto theend;
5278 }
5279 else if (lvar->lv_const)
5280 {
5281 semsg(_("E1018: Cannot assign to a constant: %s"),
5282 name);
5283 goto theend;
5284 }
5285 }
5286 else if (STRNCMP(var_start, "s:", 2) == 0
5287 || lookup_script(var_start, varlen) == OK
5288 || find_imported(var_start, varlen, cctx) != NULL)
5289 {
5290 dest = dest_script;
5291 if (is_decl)
5292 {
5293 semsg(_("E1054: Variable already declared in the script: %s"),
5294 name);
5295 goto theend;
5296 }
5297 }
5298 else if (name[1] == ':' && name[2] != NUL)
5299 {
5300 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5301 name);
5302 goto theend;
5303 }
5304 else if (!is_decl)
5305 {
5306 semsg(_("E1089: unknown variable: %s"), name);
5307 goto theend;
5308 }
5309 }
5310 }
5311
5312 // handle "a:name" as a name, not index "name" on "a"
5313 if (varlen > 1 || var_start[varlen] != ':')
5314 p = var_end;
5315
5316 if (dest != dest_option)
5317 {
5318 if (is_decl && *p == ':')
5319 {
5320 // parse optional type: "let var: type = expr"
5321 if (!VIM_ISWHITE(p[1]))
5322 {
5323 semsg(_(e_white_after), ":");
5324 goto theend;
5325 }
5326 p = skipwhite(p + 1);
5327 type = parse_type(&p, cctx->ctx_type_list);
5328 has_type = TRUE;
5329 }
5330 else if (lvar != NULL)
5331 type = lvar->lv_type;
5332 }
5333
5334 if (oplen == 3 && !heredoc && dest != dest_global
5335 && type->tt_type != VAR_STRING
5336 && type->tt_type != VAR_ANY)
5337 {
5338 emsg(_("E1019: Can only concatenate to string"));
5339 goto theend;
5340 }
5341
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005342 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005343 {
5344 if (oplen > 1 && !heredoc)
5345 {
5346 // +=, /=, etc. require an existing variable
5347 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5348 name);
5349 goto theend;
5350 }
5351
5352 // new local variable
5353 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5354 goto theend;
5355 lvar = reserve_local(cctx, var_start, varlen,
5356 cmdidx == CMD_const, type);
5357 if (lvar == NULL)
5358 goto theend;
5359 new_local = TRUE;
5360 }
5361
5362 member_type = type;
5363 if (var_end > var_start + varlen)
5364 {
5365 // Something follows after the variable: "var[idx]".
5366 if (is_decl)
5367 {
5368 emsg(_("E1087: cannot use an index when declaring a variable"));
5369 goto theend;
5370 }
5371
5372 if (var_start[varlen] == '[')
5373 {
5374 has_index = TRUE;
5375 if (type->tt_member == NULL)
5376 {
5377 semsg(_("E1088: cannot use an index on %s"), name);
5378 goto theend;
5379 }
5380 member_type = type->tt_member;
5381 }
5382 else
5383 {
5384 semsg("Not supported yet: %s", var_start);
5385 goto theend;
5386 }
5387 }
5388 else if (lvar == &arg_lvar)
5389 {
5390 semsg(_("E1090: Cannot assign to argument %s"), name);
5391 goto theend;
5392 }
5393
5394 if (!heredoc)
5395 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005396 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005397 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005398 if (oplen > 0 && var_count == 0)
5399 {
5400 // skip over the "=" and the expression
5401 p = skipwhite(op + oplen);
5402 compile_expr0(&p, cctx);
5403 }
5404 }
5405 else if (oplen > 0)
5406 {
5407 type_T *stacktype;
5408
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005409 // For "var = expr" evaluate the expression.
5410 if (var_count == 0)
5411 {
5412 int r;
5413
5414 // for "+=", "*=", "..=" etc. first load the current value
5415 if (*op != '=')
5416 {
5417 generate_loadvar(cctx, dest, name, lvar, type);
5418
5419 if (has_index)
5420 {
5421 // TODO: get member from list or dict
5422 emsg("Index with operation not supported yet");
5423 goto theend;
5424 }
5425 }
5426
5427 // Compile the expression. Temporarily hide the new local
5428 // variable here, it is not available to this expression.
5429 if (new_local)
5430 --cctx->ctx_locals.ga_len;
5431 instr_count = instr->ga_len;
5432 p = skipwhite(op + oplen);
5433 r = compile_expr0(&p, cctx);
5434 if (new_local)
5435 ++cctx->ctx_locals.ga_len;
5436 if (r == FAIL)
5437 goto theend;
5438 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005439 else if (semicolon && var_idx == var_count - 1)
5440 {
5441 // For "[var; var] = expr" get the rest of the list
5442 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5443 goto theend;
5444 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005445 else
5446 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005447 // For "[var, var] = expr" get the "var_idx" item from the
5448 // list.
5449 if (generate_GETITEM(cctx, var_idx) == FAIL)
5450 return FAIL;
5451 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005452
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005453 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005454 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005455 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005456 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005457 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005458 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005459 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005460 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005461 emsg(_(e_cannot_use_void));
5462 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005463 }
5464 else
5465 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005466 // An empty list or dict has a &t_void member,
5467 // for a variable that implies &t_any.
5468 if (stacktype == &t_list_empty)
5469 lvar->lv_type = &t_list_any;
5470 else if (stacktype == &t_dict_empty)
5471 lvar->lv_type = &t_dict_any;
5472 else
5473 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005474 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005475 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005476 else
5477 {
5478 type_T *use_type = lvar->lv_type;
5479
5480 if (has_index)
5481 {
5482 use_type = use_type->tt_member;
5483 if (use_type == NULL)
5484 use_type = &t_void;
5485 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005486 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005487 == FAIL)
5488 goto theend;
5489 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005490 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005491 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005492 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005493 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005495 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005497 emsg(_(e_const_req_value));
5498 goto theend;
5499 }
5500 else if (!has_type || dest == dest_option)
5501 {
5502 emsg(_(e_type_req));
5503 goto theend;
5504 }
5505 else
5506 {
5507 // variables are always initialized
5508 if (ga_grow(instr, 1) == FAIL)
5509 goto theend;
5510 switch (member_type->tt_type)
5511 {
5512 case VAR_BOOL:
5513 generate_PUSHBOOL(cctx, VVAL_FALSE);
5514 break;
5515 case VAR_FLOAT:
5516#ifdef FEAT_FLOAT
5517 generate_PUSHF(cctx, 0.0);
5518#endif
5519 break;
5520 case VAR_STRING:
5521 generate_PUSHS(cctx, NULL);
5522 break;
5523 case VAR_BLOB:
5524 generate_PUSHBLOB(cctx, NULL);
5525 break;
5526 case VAR_FUNC:
5527 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5528 break;
5529 case VAR_LIST:
5530 generate_NEWLIST(cctx, 0);
5531 break;
5532 case VAR_DICT:
5533 generate_NEWDICT(cctx, 0);
5534 break;
5535 case VAR_JOB:
5536 generate_PUSHJOB(cctx, NULL);
5537 break;
5538 case VAR_CHANNEL:
5539 generate_PUSHCHANNEL(cctx, NULL);
5540 break;
5541 case VAR_NUMBER:
5542 case VAR_UNKNOWN:
5543 case VAR_ANY:
5544 case VAR_PARTIAL:
5545 case VAR_VOID:
5546 case VAR_SPECIAL: // cannot happen
5547 generate_PUSHNR(cctx, 0);
5548 break;
5549 }
5550 }
5551 if (var_count == 0)
5552 end = p;
5553 }
5554
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005555 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005556 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005557 break;
5558
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005559 if (oplen > 0 && *op != '=')
5560 {
5561 type_T *expected = &t_number;
5562 type_T *stacktype;
5563
5564 // TODO: if type is known use float or any operation
5565
5566 if (*op == '.')
5567 expected = &t_string;
5568 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005569 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005570 goto theend;
5571
5572 if (*op == '.')
5573 generate_instr_drop(cctx, ISN_CONCAT, 1);
5574 else
5575 {
5576 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5577
5578 if (isn == NULL)
5579 goto theend;
5580 switch (*op)
5581 {
5582 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5583 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5584 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5585 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5586 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5587 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588 }
5589 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005591 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005592 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005593 int r;
5594
5595 // Compile the "idx" in "var[idx]".
5596 if (new_local)
5597 --cctx->ctx_locals.ga_len;
5598 p = skipwhite(var_start + varlen + 1);
5599 r = compile_expr0(&p, cctx);
5600 if (new_local)
5601 ++cctx->ctx_locals.ga_len;
5602 if (r == FAIL)
5603 goto theend;
5604 if (*skipwhite(p) != ']')
5605 {
5606 emsg(_(e_missbrac));
5607 goto theend;
5608 }
5609 if (type->tt_type == VAR_DICT
5610 && may_generate_2STRING(-1, cctx) == FAIL)
5611 goto theend;
5612 if (type->tt_type == VAR_LIST
5613 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005614 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005615 {
5616 emsg(_(e_number_exp));
5617 goto theend;
5618 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005619
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005620 // Load the dict or list. On the stack we then have:
5621 // - value
5622 // - index
5623 // - variable
5624 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005625
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005626 if (type->tt_type == VAR_LIST)
5627 {
5628 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5629 return FAIL;
5630 }
5631 else if (type->tt_type == VAR_DICT)
5632 {
5633 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5634 return FAIL;
5635 }
5636 else
5637 {
5638 emsg(_(e_listreq));
5639 goto theend;
5640 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005641 }
5642 else
5643 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005644 switch (dest)
5645 {
5646 case dest_option:
5647 generate_STOREOPT(cctx, name + 1, opt_flags);
5648 break;
5649 case dest_global:
5650 // include g: with the name, easier to execute that way
5651 generate_STORE(cctx, ISN_STOREG, 0, name);
5652 break;
5653 case dest_buffer:
5654 // include b: with the name, easier to execute that way
5655 generate_STORE(cctx, ISN_STOREB, 0, name);
5656 break;
5657 case dest_window:
5658 // include w: with the name, easier to execute that way
5659 generate_STORE(cctx, ISN_STOREW, 0, name);
5660 break;
5661 case dest_tab:
5662 // include t: with the name, easier to execute that way
5663 generate_STORE(cctx, ISN_STORET, 0, name);
5664 break;
5665 case dest_env:
5666 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5667 break;
5668 case dest_reg:
5669 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5670 break;
5671 case dest_vimvar:
5672 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5673 break;
5674 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005675 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005676 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5677 imported_T *import = NULL;
5678 int sid = current_sctx.sc_sid;
5679 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005680
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005681 if (name[1] != ':')
5682 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005683 import = find_imported(name, 0, cctx);
5684 if (import != NULL)
5685 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005686 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005687
5688 idx = get_script_item_idx(sid, rawname, TRUE);
5689 // TODO: specific type
5690 if (idx < 0)
5691 {
5692 char_u *name_s = name;
5693
5694 // Include s: in the name for store_var()
5695 if (name[1] != ':')
5696 {
5697 int len = (int)STRLEN(name) + 3;
5698
5699 name_s = alloc(len);
5700 if (name_s == NULL)
5701 name_s = name;
5702 else
5703 vim_snprintf((char *)name_s, len,
5704 "s:%s", name);
5705 }
5706 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005707 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005708 if (name_s != name)
5709 vim_free(name_s);
5710 }
5711 else
5712 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005713 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005714 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005715 break;
5716 case dest_local:
5717 if (lvar != NULL)
5718 {
5719 isn_T *isn = ((isn_T *)instr->ga_data)
5720 + instr->ga_len - 1;
5721
5722 // optimization: turn "var = 123" from ISN_PUSHNR +
5723 // ISN_STORE into ISN_STORENR
5724 if (!lvar->lv_from_outer
5725 && instr->ga_len == instr_count + 1
5726 && isn->isn_type == ISN_PUSHNR)
5727 {
5728 varnumber_T val = isn->isn_arg.number;
5729
5730 isn->isn_type = ISN_STORENR;
5731 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5732 isn->isn_arg.storenr.stnr_val = val;
5733 if (stack->ga_len > 0)
5734 --stack->ga_len;
5735 }
5736 else if (lvar->lv_from_outer)
5737 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005738 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005739 else
5740 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5741 }
5742 break;
5743 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005744 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005745
5746 if (var_idx + 1 < var_count)
5747 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005748 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005749
5750 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005751 if (var_count > 0 && !semicolon)
5752 {
5753 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5754 goto theend;
5755 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005756
Bram Moolenaarb2097502020-07-19 17:17:02 +02005757 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005758
5759theend:
5760 vim_free(name);
5761 return ret;
5762}
5763
5764/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005765 * Check if "name" can be "unlet".
5766 */
5767 int
5768check_vim9_unlet(char_u *name)
5769{
5770 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5771 {
5772 semsg(_("E1081: Cannot unlet %s"), name);
5773 return FAIL;
5774 }
5775 return OK;
5776}
5777
5778/*
5779 * Callback passed to ex_unletlock().
5780 */
5781 static int
5782compile_unlet(
5783 lval_T *lvp,
5784 char_u *name_end,
5785 exarg_T *eap,
5786 int deep UNUSED,
5787 void *coookie)
5788{
5789 cctx_T *cctx = coookie;
5790
5791 if (lvp->ll_tv == NULL)
5792 {
5793 char_u *p = lvp->ll_name;
5794 int cc = *name_end;
5795 int ret = OK;
5796
5797 // Normal name. Only supports g:, w:, t: and b: namespaces.
5798 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005799 if (*p == '$')
5800 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5801 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005802 ret = FAIL;
5803 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005804 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005805
5806 *name_end = cc;
5807 return ret;
5808 }
5809
5810 // TODO: unlet {list}[idx]
5811 // TODO: unlet {dict}[key]
5812 emsg("Sorry, :unlet not fully implemented yet");
5813 return FAIL;
5814}
5815
5816/*
5817 * compile "unlet var", "lock var" and "unlock var"
5818 * "arg" points to "var".
5819 */
5820 static char_u *
5821compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5822{
5823 char_u *p = arg;
5824
5825 if (eap->cmdidx != CMD_unlet)
5826 {
5827 emsg("Sorry, :lock and unlock not implemented yet");
5828 return NULL;
5829 }
5830
5831 if (*p == '!')
5832 {
5833 p = skipwhite(p + 1);
5834 eap->forceit = TRUE;
5835 }
5836
5837 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5838 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5839}
5840
5841/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005842 * Compile an :import command.
5843 */
5844 static char_u *
5845compile_import(char_u *arg, cctx_T *cctx)
5846{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005847 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848}
5849
5850/*
5851 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5852 */
5853 static int
5854compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5855{
5856 garray_T *instr = &cctx->ctx_instr;
5857 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5858
5859 if (endlabel == NULL)
5860 return FAIL;
5861 endlabel->el_next = *el;
5862 *el = endlabel;
5863 endlabel->el_end_label = instr->ga_len;
5864
5865 generate_JUMP(cctx, when, 0);
5866 return OK;
5867}
5868
5869 static void
5870compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5871{
5872 garray_T *instr = &cctx->ctx_instr;
5873
5874 while (*el != NULL)
5875 {
5876 endlabel_T *cur = (*el);
5877 isn_T *isn;
5878
5879 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5880 isn->isn_arg.jump.jump_where = instr->ga_len;
5881 *el = cur->el_next;
5882 vim_free(cur);
5883 }
5884}
5885
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005886 static void
5887compile_free_jump_to_end(endlabel_T **el)
5888{
5889 while (*el != NULL)
5890 {
5891 endlabel_T *cur = (*el);
5892
5893 *el = cur->el_next;
5894 vim_free(cur);
5895 }
5896}
5897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898/*
5899 * Create a new scope and set up the generic items.
5900 */
5901 static scope_T *
5902new_scope(cctx_T *cctx, scopetype_T type)
5903{
5904 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5905
5906 if (scope == NULL)
5907 return NULL;
5908 scope->se_outer = cctx->ctx_scope;
5909 cctx->ctx_scope = scope;
5910 scope->se_type = type;
5911 scope->se_local_count = cctx->ctx_locals.ga_len;
5912 return scope;
5913}
5914
5915/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005916 * Free the current scope and go back to the outer scope.
5917 */
5918 static void
5919drop_scope(cctx_T *cctx)
5920{
5921 scope_T *scope = cctx->ctx_scope;
5922
5923 if (scope == NULL)
5924 {
5925 iemsg("calling drop_scope() without a scope");
5926 return;
5927 }
5928 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005929 switch (scope->se_type)
5930 {
5931 case IF_SCOPE:
5932 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5933 case FOR_SCOPE:
5934 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5935 case WHILE_SCOPE:
5936 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5937 case TRY_SCOPE:
5938 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5939 case NO_SCOPE:
5940 case BLOCK_SCOPE:
5941 break;
5942 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005943 vim_free(scope);
5944}
5945
5946/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005947 * compile "if expr"
5948 *
5949 * "if expr" Produces instructions:
5950 * EVAL expr Push result of "expr"
5951 * JUMP_IF_FALSE end
5952 * ... body ...
5953 * end:
5954 *
5955 * "if expr | else" Produces instructions:
5956 * EVAL expr Push result of "expr"
5957 * JUMP_IF_FALSE else
5958 * ... body ...
5959 * JUMP_ALWAYS end
5960 * else:
5961 * ... body ...
5962 * end:
5963 *
5964 * "if expr1 | elseif expr2 | else" Produces instructions:
5965 * EVAL expr Push result of "expr"
5966 * JUMP_IF_FALSE elseif
5967 * ... body ...
5968 * JUMP_ALWAYS end
5969 * elseif:
5970 * EVAL expr Push result of "expr"
5971 * JUMP_IF_FALSE else
5972 * ... body ...
5973 * JUMP_ALWAYS end
5974 * else:
5975 * ... body ...
5976 * end:
5977 */
5978 static char_u *
5979compile_if(char_u *arg, cctx_T *cctx)
5980{
5981 char_u *p = arg;
5982 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005983 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005984 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005985 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005986 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987
Bram Moolenaara5565e42020-05-09 15:44:01 +02005988 CLEAR_FIELD(ppconst);
5989 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005990 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005991 clear_ppconst(&ppconst);
5992 return NULL;
5993 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005994 if (cctx->ctx_skip == SKIP_YES)
5995 clear_ppconst(&ppconst);
5996 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005997 {
5998 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005999 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006000 clear_ppconst(&ppconst);
6001 }
6002 else
6003 {
6004 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006005 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006006 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006007 return NULL;
6008 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006009
6010 scope = new_scope(cctx, IF_SCOPE);
6011 if (scope == NULL)
6012 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006013 scope->se_skip_save = skip_save;
6014 // "is_had_return" will be reset if any block does not end in :return
6015 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006017 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006018 {
6019 // "where" is set when ":elseif", "else" or ":endif" is found
6020 scope->se_u.se_if.is_if_label = instr->ga_len;
6021 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6022 }
6023 else
6024 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006025
6026 return p;
6027}
6028
6029 static char_u *
6030compile_elseif(char_u *arg, cctx_T *cctx)
6031{
6032 char_u *p = arg;
6033 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006034 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035 isn_T *isn;
6036 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006037 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038
6039 if (scope == NULL || scope->se_type != IF_SCOPE)
6040 {
6041 emsg(_(e_elseif_without_if));
6042 return NULL;
6043 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006044 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006045 if (!cctx->ctx_had_return)
6046 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006047
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006048 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006049 {
6050 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006051 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006052 return NULL;
6053 // previous "if" or "elseif" jumps here
6054 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6055 isn->isn_arg.jump.jump_where = instr->ga_len;
6056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006057
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006058 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006059 CLEAR_FIELD(ppconst);
6060 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006061 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006062 clear_ppconst(&ppconst);
6063 return NULL;
6064 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006065 if (scope->se_skip_save == SKIP_YES)
6066 clear_ppconst(&ppconst);
6067 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006068 {
6069 // The expression results in a constant.
6070 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006071 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006072 clear_ppconst(&ppconst);
6073 scope->se_u.se_if.is_if_label = -1;
6074 }
6075 else
6076 {
6077 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006078 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006079 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006080 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006082 // "where" is set when ":elseif", "else" or ":endif" is found
6083 scope->se_u.se_if.is_if_label = instr->ga_len;
6084 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086
6087 return p;
6088}
6089
6090 static char_u *
6091compile_else(char_u *arg, cctx_T *cctx)
6092{
6093 char_u *p = arg;
6094 garray_T *instr = &cctx->ctx_instr;
6095 isn_T *isn;
6096 scope_T *scope = cctx->ctx_scope;
6097
6098 if (scope == NULL || scope->se_type != IF_SCOPE)
6099 {
6100 emsg(_(e_else_without_if));
6101 return NULL;
6102 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006103 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006104 if (!cctx->ctx_had_return)
6105 scope->se_u.se_if.is_had_return = FALSE;
6106 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107
Bram Moolenaarefd88552020-06-18 20:50:10 +02006108 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006109 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006110 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006111 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006112 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006113 if (!cctx->ctx_had_return
6114 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6115 JUMP_ALWAYS, cctx) == FAIL)
6116 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006117 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006118
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006119 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006120 {
6121 if (scope->se_u.se_if.is_if_label >= 0)
6122 {
6123 // previous "if" or "elseif" jumps here
6124 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6125 isn->isn_arg.jump.jump_where = instr->ga_len;
6126 scope->se_u.se_if.is_if_label = -1;
6127 }
6128 }
6129
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006130 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006131 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006133
6134 return p;
6135}
6136
6137 static char_u *
6138compile_endif(char_u *arg, cctx_T *cctx)
6139{
6140 scope_T *scope = cctx->ctx_scope;
6141 ifscope_T *ifscope;
6142 garray_T *instr = &cctx->ctx_instr;
6143 isn_T *isn;
6144
6145 if (scope == NULL || scope->se_type != IF_SCOPE)
6146 {
6147 emsg(_(e_endif_without_if));
6148 return NULL;
6149 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006150 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006151 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006152 if (!cctx->ctx_had_return)
6153 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006154
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006155 if (scope->se_u.se_if.is_if_label >= 0)
6156 {
6157 // previous "if" or "elseif" jumps here
6158 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6159 isn->isn_arg.jump.jump_where = instr->ga_len;
6160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006161 // Fill in the "end" label in jumps at the end of the blocks.
6162 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006163 cctx->ctx_skip = scope->se_skip_save;
6164
6165 // If all the blocks end in :return and there is an :else then the
6166 // had_return flag is set.
6167 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006168
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006169 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006170 return arg;
6171}
6172
6173/*
6174 * compile "for var in expr"
6175 *
6176 * Produces instructions:
6177 * PUSHNR -1
6178 * STORE loop-idx Set index to -1
6179 * EVAL expr Push result of "expr"
6180 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6181 * - if beyond end, jump to "end"
6182 * - otherwise get item from list and push it
6183 * STORE var Store item in "var"
6184 * ... body ...
6185 * JUMP top Jump back to repeat
6186 * end: DROP Drop the result of "expr"
6187 *
6188 */
6189 static char_u *
6190compile_for(char_u *arg, cctx_T *cctx)
6191{
6192 char_u *p;
6193 size_t varlen;
6194 garray_T *instr = &cctx->ctx_instr;
6195 garray_T *stack = &cctx->ctx_type_stack;
6196 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006197 lvar_T *loop_lvar; // loop iteration variable
6198 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006199 type_T *vartype;
6200
6201 // TODO: list of variables: "for [key, value] in dict"
6202 // parse "var"
6203 for (p = arg; eval_isnamec1(*p); ++p)
6204 ;
6205 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006206 var_lvar = lookup_local(arg, varlen, cctx);
6207 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006208 {
6209 semsg(_("E1023: variable already defined: %s"), arg);
6210 return NULL;
6211 }
6212
6213 // consume "in"
6214 p = skipwhite(p);
6215 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6216 {
6217 emsg(_(e_missing_in));
6218 return NULL;
6219 }
6220 p = skipwhite(p + 2);
6221
6222
6223 scope = new_scope(cctx, FOR_SCOPE);
6224 if (scope == NULL)
6225 return NULL;
6226
6227 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006228 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6229 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006230 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006231 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006232 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235
6236 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006237 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6238 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006239 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006240 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006241 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006245 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246
6247 // compile "expr", it remains on the stack until "endfor"
6248 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006249 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006250 {
6251 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006252 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006255 // Now that we know the type of "var", check that it is a list, now or at
6256 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006258 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006260 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 return NULL;
6262 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006263 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006264 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265
6266 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006267 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006268
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006269 generate_FOR(cctx, loop_lvar->lv_idx);
6270 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271
6272 return arg;
6273}
6274
6275/*
6276 * compile "endfor"
6277 */
6278 static char_u *
6279compile_endfor(char_u *arg, cctx_T *cctx)
6280{
6281 garray_T *instr = &cctx->ctx_instr;
6282 scope_T *scope = cctx->ctx_scope;
6283 forscope_T *forscope;
6284 isn_T *isn;
6285
6286 if (scope == NULL || scope->se_type != FOR_SCOPE)
6287 {
6288 emsg(_(e_for));
6289 return NULL;
6290 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006291 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006293 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294
6295 // At end of ":for" scope jump back to the FOR instruction.
6296 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6297
6298 // Fill in the "end" label in the FOR statement so it can jump here
6299 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6300 isn->isn_arg.forloop.for_end = instr->ga_len;
6301
6302 // Fill in the "end" label any BREAK statements
6303 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6304
6305 // Below the ":for" scope drop the "expr" list from the stack.
6306 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6307 return NULL;
6308
6309 vim_free(scope);
6310
6311 return arg;
6312}
6313
6314/*
6315 * compile "while expr"
6316 *
6317 * Produces instructions:
6318 * top: EVAL expr Push result of "expr"
6319 * JUMP_IF_FALSE end jump if false
6320 * ... body ...
6321 * JUMP top Jump back to repeat
6322 * end:
6323 *
6324 */
6325 static char_u *
6326compile_while(char_u *arg, cctx_T *cctx)
6327{
6328 char_u *p = arg;
6329 garray_T *instr = &cctx->ctx_instr;
6330 scope_T *scope;
6331
6332 scope = new_scope(cctx, WHILE_SCOPE);
6333 if (scope == NULL)
6334 return NULL;
6335
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006336 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337
6338 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006339 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006340 return NULL;
6341
6342 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006343 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344 JUMP_IF_FALSE, cctx) == FAIL)
6345 return FAIL;
6346
6347 return p;
6348}
6349
6350/*
6351 * compile "endwhile"
6352 */
6353 static char_u *
6354compile_endwhile(char_u *arg, cctx_T *cctx)
6355{
6356 scope_T *scope = cctx->ctx_scope;
6357
6358 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6359 {
6360 emsg(_(e_while));
6361 return NULL;
6362 }
6363 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006364 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006365
6366 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006367 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006368
6369 // Fill in the "end" label in the WHILE statement so it can jump here.
6370 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006371 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006372
6373 vim_free(scope);
6374
6375 return arg;
6376}
6377
6378/*
6379 * compile "continue"
6380 */
6381 static char_u *
6382compile_continue(char_u *arg, cctx_T *cctx)
6383{
6384 scope_T *scope = cctx->ctx_scope;
6385
6386 for (;;)
6387 {
6388 if (scope == NULL)
6389 {
6390 emsg(_(e_continue));
6391 return NULL;
6392 }
6393 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6394 break;
6395 scope = scope->se_outer;
6396 }
6397
6398 // Jump back to the FOR or WHILE instruction.
6399 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006400 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6401 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006402 return arg;
6403}
6404
6405/*
6406 * compile "break"
6407 */
6408 static char_u *
6409compile_break(char_u *arg, cctx_T *cctx)
6410{
6411 scope_T *scope = cctx->ctx_scope;
6412 endlabel_T **el;
6413
6414 for (;;)
6415 {
6416 if (scope == NULL)
6417 {
6418 emsg(_(e_break));
6419 return NULL;
6420 }
6421 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6422 break;
6423 scope = scope->se_outer;
6424 }
6425
6426 // Jump to the end of the FOR or WHILE loop.
6427 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006428 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006429 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006430 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6432 return FAIL;
6433
6434 return arg;
6435}
6436
6437/*
6438 * compile "{" start of block
6439 */
6440 static char_u *
6441compile_block(char_u *arg, cctx_T *cctx)
6442{
6443 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6444 return NULL;
6445 return skipwhite(arg + 1);
6446}
6447
6448/*
6449 * compile end of block: drop one scope
6450 */
6451 static void
6452compile_endblock(cctx_T *cctx)
6453{
6454 scope_T *scope = cctx->ctx_scope;
6455
6456 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006457 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006458 vim_free(scope);
6459}
6460
6461/*
6462 * compile "try"
6463 * Creates a new scope for the try-endtry, pointing to the first catch and
6464 * finally.
6465 * Creates another scope for the "try" block itself.
6466 * TRY instruction sets up exception handling at runtime.
6467 *
6468 * "try"
6469 * TRY -> catch1, -> finally push trystack entry
6470 * ... try block
6471 * "throw {exception}"
6472 * EVAL {exception}
6473 * THROW create exception
6474 * ... try block
6475 * " catch {expr}"
6476 * JUMP -> finally
6477 * catch1: PUSH exeception
6478 * EVAL {expr}
6479 * MATCH
6480 * JUMP nomatch -> catch2
6481 * CATCH remove exception
6482 * ... catch block
6483 * " catch"
6484 * JUMP -> finally
6485 * catch2: CATCH remove exception
6486 * ... catch block
6487 * " finally"
6488 * finally:
6489 * ... finally block
6490 * " endtry"
6491 * ENDTRY pop trystack entry, may rethrow
6492 */
6493 static char_u *
6494compile_try(char_u *arg, cctx_T *cctx)
6495{
6496 garray_T *instr = &cctx->ctx_instr;
6497 scope_T *try_scope;
6498 scope_T *scope;
6499
6500 // scope that holds the jumps that go to catch/finally/endtry
6501 try_scope = new_scope(cctx, TRY_SCOPE);
6502 if (try_scope == NULL)
6503 return NULL;
6504
6505 // "catch" is set when the first ":catch" is found.
6506 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006507 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006508 if (generate_instr(cctx, ISN_TRY) == NULL)
6509 return NULL;
6510
6511 // scope for the try block itself
6512 scope = new_scope(cctx, BLOCK_SCOPE);
6513 if (scope == NULL)
6514 return NULL;
6515
6516 return arg;
6517}
6518
6519/*
6520 * compile "catch {expr}"
6521 */
6522 static char_u *
6523compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6524{
6525 scope_T *scope = cctx->ctx_scope;
6526 garray_T *instr = &cctx->ctx_instr;
6527 char_u *p;
6528 isn_T *isn;
6529
6530 // end block scope from :try or :catch
6531 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6532 compile_endblock(cctx);
6533 scope = cctx->ctx_scope;
6534
6535 // Error if not in a :try scope
6536 if (scope == NULL || scope->se_type != TRY_SCOPE)
6537 {
6538 emsg(_(e_catch));
6539 return NULL;
6540 }
6541
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006542 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543 {
6544 emsg(_("E1033: catch unreachable after catch-all"));
6545 return NULL;
6546 }
6547
6548 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006549 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550 JUMP_ALWAYS, cctx) == FAIL)
6551 return NULL;
6552
6553 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006554 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006555 if (isn->isn_arg.try.try_catch == 0)
6556 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006557 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558 {
6559 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006560 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006561 isn->isn_arg.jump.jump_where = instr->ga_len;
6562 }
6563
6564 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006565 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006567 scope->se_u.se_try.ts_caught_all = TRUE;
6568 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006569 }
6570 else
6571 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006572 char_u *end;
6573 char_u *pat;
6574 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006575 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006576 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 // Push v:exception, push {expr} and MATCH
6579 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6580
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006581 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006582 if (*end != *p)
6583 {
6584 semsg(_("E1067: Separator mismatch: %s"), p);
6585 vim_free(tofree);
6586 return FAIL;
6587 }
6588 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006589 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006590 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006591 len = (int)(end - tofree);
6592 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006593 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006594 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006595 if (pat == NULL)
6596 return FAIL;
6597 if (generate_PUSHS(cctx, pat) == FAIL)
6598 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6601 return NULL;
6602
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006603 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6605 return NULL;
6606 }
6607
6608 if (generate_instr(cctx, ISN_CATCH) == NULL)
6609 return NULL;
6610
6611 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6612 return NULL;
6613 return p;
6614}
6615
6616 static char_u *
6617compile_finally(char_u *arg, cctx_T *cctx)
6618{
6619 scope_T *scope = cctx->ctx_scope;
6620 garray_T *instr = &cctx->ctx_instr;
6621 isn_T *isn;
6622
6623 // end block scope from :try or :catch
6624 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6625 compile_endblock(cctx);
6626 scope = cctx->ctx_scope;
6627
6628 // Error if not in a :try scope
6629 if (scope == NULL || scope->se_type != TRY_SCOPE)
6630 {
6631 emsg(_(e_finally));
6632 return NULL;
6633 }
6634
6635 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006636 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 if (isn->isn_arg.try.try_finally != 0)
6638 {
6639 emsg(_(e_finally_dup));
6640 return NULL;
6641 }
6642
6643 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006644 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006645
Bram Moolenaar585fea72020-04-02 22:33:21 +02006646 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006647 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006648 {
6649 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006650 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006652 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006653 }
6654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006655 // TODO: set index in ts_finally_label jumps
6656
6657 return arg;
6658}
6659
6660 static char_u *
6661compile_endtry(char_u *arg, cctx_T *cctx)
6662{
6663 scope_T *scope = cctx->ctx_scope;
6664 garray_T *instr = &cctx->ctx_instr;
6665 isn_T *isn;
6666
6667 // end block scope from :catch or :finally
6668 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6669 compile_endblock(cctx);
6670 scope = cctx->ctx_scope;
6671
6672 // Error if not in a :try scope
6673 if (scope == NULL || scope->se_type != TRY_SCOPE)
6674 {
6675 if (scope == NULL)
6676 emsg(_(e_no_endtry));
6677 else if (scope->se_type == WHILE_SCOPE)
6678 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006679 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006680 emsg(_(e_endfor));
6681 else
6682 emsg(_(e_endif));
6683 return NULL;
6684 }
6685
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006686 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6688 {
6689 emsg(_("E1032: missing :catch or :finally"));
6690 return NULL;
6691 }
6692
6693 // Fill in the "end" label in jumps at the end of the blocks, if not done
6694 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006695 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696
6697 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006698 if (isn->isn_arg.try.try_catch == 0)
6699 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 if (isn->isn_arg.try.try_finally == 0)
6701 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006702
6703 if (scope->se_u.se_try.ts_catch_label != 0)
6704 {
6705 // Last catch without match jumps here
6706 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6707 isn->isn_arg.jump.jump_where = instr->ga_len;
6708 }
6709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710 compile_endblock(cctx);
6711
6712 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6713 return NULL;
6714 return arg;
6715}
6716
6717/*
6718 * compile "throw {expr}"
6719 */
6720 static char_u *
6721compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6722{
6723 char_u *p = skipwhite(arg);
6724
Bram Moolenaara5565e42020-05-09 15:44:01 +02006725 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726 return NULL;
6727 if (may_generate_2STRING(-1, cctx) == FAIL)
6728 return NULL;
6729 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6730 return NULL;
6731
6732 return p;
6733}
6734
6735/*
6736 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006737 * compile "echomsg expr"
6738 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006739 * compile "execute expr"
6740 */
6741 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006742compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006743{
6744 char_u *p = arg;
6745 int count = 0;
6746
6747 for (;;)
6748 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006749 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006750 return NULL;
6751 ++count;
6752 p = skipwhite(p);
6753 if (ends_excmd(*p))
6754 break;
6755 }
6756
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006757 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6758 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6759 else if (cmdidx == CMD_execute)
6760 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6761 else if (cmdidx == CMD_echomsg)
6762 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6763 else
6764 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006765 return p;
6766}
6767
6768/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006769 * A command that is not compiled, execute with legacy code.
6770 */
6771 static char_u *
6772compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6773{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006774 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006775 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006776 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006777
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006778 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006779 goto theend;
6780
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006781 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006782 {
6783 long argt = excmd_get_argt(eap->cmdidx);
6784 int usefilter = FALSE;
6785
6786 has_expr = argt & (EX_XFILE | EX_EXPAND);
6787
6788 // If the command can be followed by a bar, find the bar and truncate
6789 // it, so that the following command can be compiled.
6790 // The '|' is overwritten with a NUL, it is put back below.
6791 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6792 && *eap->arg == '!')
6793 // :w !filter or :r !filter or :r! filter
6794 usefilter = TRUE;
6795 if ((argt & EX_TRLBAR) && !usefilter)
6796 {
6797 separate_nextcmd(eap);
6798 if (eap->nextcmd != NULL)
6799 nextcmd = eap->nextcmd;
6800 }
6801 }
6802
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006803 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6804 {
6805 // expand filename in "syntax include [@group] filename"
6806 has_expr = TRUE;
6807 eap->arg = skipwhite(eap->arg + 7);
6808 if (*eap->arg == '@')
6809 eap->arg = skiptowhite(eap->arg);
6810 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006811
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006812 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006813 {
6814 int count = 0;
6815 char_u *start = skipwhite(line);
6816
6817 // :cmd xxx`=expr1`yyy`=expr2`zzz
6818 // PUSHS ":cmd xxx"
6819 // eval expr1
6820 // PUSHS "yyy"
6821 // eval expr2
6822 // PUSHS "zzz"
6823 // EXECCONCAT 5
6824 for (;;)
6825 {
6826 if (p > start)
6827 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006828 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006829 ++count;
6830 }
6831 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006832 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006833 return NULL;
6834 may_generate_2STRING(-1, cctx);
6835 ++count;
6836 p = skipwhite(p);
6837 if (*p != '`')
6838 {
6839 emsg(_("E1083: missing backtick"));
6840 return NULL;
6841 }
6842 start = p + 1;
6843
6844 p = (char_u *)strstr((char *)start, "`=");
6845 if (p == NULL)
6846 {
6847 if (*skipwhite(start) != NUL)
6848 {
6849 generate_PUSHS(cctx, vim_strsave(start));
6850 ++count;
6851 }
6852 break;
6853 }
6854 }
6855 generate_EXECCONCAT(cctx, count);
6856 }
6857 else
6858 generate_EXEC(cctx, line);
6859
6860theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006861 if (*nextcmd != NUL)
6862 {
6863 // the parser expects a pointer to the bar, put it back
6864 --nextcmd;
6865 *nextcmd = '|';
6866 }
6867
6868 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006869}
6870
6871/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006872 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006873 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006874 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006875 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006876add_def_function(ufunc_T *ufunc)
6877{
6878 dfunc_T *dfunc;
6879
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006880 if (def_functions.ga_len == 0)
6881 {
6882 // The first position is not used, so that a zero uf_dfunc_idx means it
6883 // wasn't set.
6884 if (ga_grow(&def_functions, 1) == FAIL)
6885 return FAIL;
6886 ++def_functions.ga_len;
6887 }
6888
Bram Moolenaar09689a02020-05-09 22:50:08 +02006889 // Add the function to "def_functions".
6890 if (ga_grow(&def_functions, 1) == FAIL)
6891 return FAIL;
6892 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6893 CLEAR_POINTER(dfunc);
6894 dfunc->df_idx = def_functions.ga_len;
6895 ufunc->uf_dfunc_idx = dfunc->df_idx;
6896 dfunc->df_ufunc = ufunc;
6897 ++def_functions.ga_len;
6898 return OK;
6899}
6900
6901/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006902 * After ex_function() has collected all the function lines: parse and compile
6903 * the lines into instructions.
6904 * Adds the function to "def_functions".
6905 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6906 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006907 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006908 * This can be used recursively through compile_lambda(), which may reallocate
6909 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006910 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006911 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006912 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006913compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006915 char_u *line = NULL;
6916 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006917 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918 cctx_T cctx;
6919 garray_T *instr;
6920 int called_emsg_before = called_emsg;
6921 int ret = FAIL;
6922 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006923 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006924 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006925
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006926 // When using a function that was compiled before: Free old instructions.
6927 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006928 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006930 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6931 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006932 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006934 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006935 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936
Bram Moolenaar985116a2020-07-12 17:31:09 +02006937 ufunc->uf_def_status = UF_COMPILING;
6938
Bram Moolenaara80faa82020-04-12 19:37:17 +02006939 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940 cctx.ctx_ufunc = ufunc;
6941 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006942 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6944 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6945 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6946 cctx.ctx_type_list = &ufunc->uf_type_list;
6947 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6948 instr = &cctx.ctx_instr;
6949
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006950 // Set the context to the function, it may be compiled when called from
6951 // another script. Set the script version to the most modern one.
6952 // The line number will be set in next_line_from_context().
6953 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6955
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006956 // Make sure error messages are OK.
6957 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6958 if (do_estack_push)
6959 estack_push_ufunc(ufunc, 1);
6960
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006961 if (ufunc->uf_def_args.ga_len > 0)
6962 {
6963 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006964 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006965 int i;
6966 char_u *arg;
6967 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6968
6969 // Produce instructions for the default values of optional arguments.
6970 // Store the instruction index in uf_def_arg_idx[] so that we know
6971 // where to start when the function is called, depending on the number
6972 // of arguments.
6973 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6974 if (ufunc->uf_def_arg_idx == NULL)
6975 goto erret;
6976 for (i = 0; i < count; ++i)
6977 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006978 garray_T *stack = &cctx.ctx_type_stack;
6979 type_T *val_type;
6980 int arg_idx = first_def_arg + i;
6981
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006982 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6983 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006984 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006985 goto erret;
6986
6987 // If no type specified use the type of the default value.
6988 // Otherwise check that the default value type matches the
6989 // specified type.
6990 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6991 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6992 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006993 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006994 == FAIL)
6995 {
6996 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6997 arg_idx + 1);
6998 goto erret;
6999 }
7000
7001 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007002 goto erret;
7003 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007004 ufunc->uf_def_arg_idx[count] = instr->ga_len;
7005 }
7006
7007 /*
7008 * Loop over all the lines of the function and generate instructions.
7009 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010 for (;;)
7011 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007012 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007013 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007014 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007015 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007016
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007017 // Bail out on the first error to avoid a flood of errors and report
7018 // the right line number when inside try/catch.
7019 if (emsg_before != called_emsg)
7020 goto erret;
7021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007022 if (line != NULL && *line == '|')
7023 // the line continues after a '|'
7024 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007025 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007026 && !(*line == '#' && (line == cctx.ctx_line_start
7027 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007029 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007030 goto erret;
7031 }
7032 else
7033 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007034 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007035 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007036 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007037 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007039 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040
Bram Moolenaara80faa82020-04-12 19:37:17 +02007041 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042 ea.cmdlinep = &line;
7043 ea.cmd = skipwhite(line);
7044
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007045 // Some things can be recognized by the first character.
7046 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007048 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007049 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007050 if (ea.cmd[1] != '{')
7051 {
7052 line = (char_u *)"";
7053 continue;
7054 }
7055 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007057 case '}':
7058 {
7059 // "}" ends a block scope
7060 scopetype_T stype = cctx.ctx_scope == NULL
7061 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007063 if (stype == BLOCK_SCOPE)
7064 {
7065 compile_endblock(&cctx);
7066 line = ea.cmd;
7067 }
7068 else
7069 {
7070 emsg(_("E1025: using } outside of a block scope"));
7071 goto erret;
7072 }
7073 if (line != NULL)
7074 line = skipwhite(ea.cmd + 1);
7075 continue;
7076 }
7077
7078 case '{':
7079 // "{" starts a block scope
7080 // "{'a': 1}->func() is something else
7081 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7082 {
7083 line = compile_block(ea.cmd, &cctx);
7084 continue;
7085 }
7086 break;
7087
7088 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007089 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007090 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007091 }
7092
7093 /*
7094 * COMMAND MODIFIERS
7095 */
7096 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7097 {
7098 if (errormsg != NULL)
7099 goto erret;
7100 // empty line or comment
7101 line = (char_u *)"";
7102 continue;
7103 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007104 // TODO: use modifiers in the command
7105 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007106 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107
7108 // Skip ":call" to get to the function name.
7109 if (checkforcmd(&ea.cmd, "call", 3))
7110 ea.cmd = skipwhite(ea.cmd);
7111
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007112 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007114 char_u *pskip;
7115
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007116 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007117 // find what follows.
7118 // Skip over "var.member", "var[idx]" and the like.
7119 // Also "&opt = val", "$ENV = val" and "@r = val".
7120 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007121 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007122 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007123 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007124 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007125 char_u *var_end;
7126 int oplen;
7127 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007129 var_end = find_name_end(pskip, NULL, NULL,
7130 FNE_CHECK_START | FNE_INCL_BR);
7131 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007132 if (oplen > 0)
7133 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007134 size_t len = p - ea.cmd;
7135
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007136 // Recognize an assignment if we recognize the variable
7137 // name:
7138 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007139 // "local = expr" where "local" is a local var.
7140 // "script = expr" where "script" is a script-local var.
7141 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007142 // "&opt = expr"
7143 // "$ENV = expr"
7144 // "@r = expr"
7145 if (*ea.cmd == '&'
7146 || *ea.cmd == '$'
7147 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007148 || ((len) > 2 && ea.cmd[1] == ':')
7149 || lookup_local(ea.cmd, len, &cctx) != NULL
7150 || lookup_arg(ea.cmd, len, NULL, NULL,
7151 NULL, &cctx) == OK
7152 || lookup_script(ea.cmd, len) == OK
7153 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007154 {
7155 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007156 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007157 goto erret;
7158 continue;
7159 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007160 }
7161 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007162
7163 if (*ea.cmd == '[')
7164 {
7165 // [var, var] = expr
7166 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7167 if (line == NULL)
7168 goto erret;
7169 if (line != ea.cmd)
7170 continue;
7171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007172 }
7173
7174 /*
7175 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007176 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007178 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007179 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007180 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007181 ea.cmd = skip_range(ea.cmd, NULL);
7182 if (ea.cmd > cmd && !starts_with_colon)
7183 {
7184 emsg(_(e_colon_required));
7185 goto erret;
7186 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007187 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007188 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007189 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007190 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007191
7192 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7193 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007194 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007195 {
7196 line += STRLEN(line);
7197 continue;
7198 }
7199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007200 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007201 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007203 // CMD_let cannot happen, compile_assignment() above is used
7204 iemsg("Command from find_ex_command() not handled");
7205 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007207 }
7208
7209 p = skipwhite(p);
7210
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007211 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007212 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007213 && ea.cmdidx != CMD_elseif
7214 && ea.cmdidx != CMD_else
7215 && ea.cmdidx != CMD_endif)
7216 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007217 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007218 continue;
7219 }
7220
Bram Moolenaarefd88552020-06-18 20:50:10 +02007221 if (ea.cmdidx != CMD_elseif
7222 && ea.cmdidx != CMD_else
7223 && ea.cmdidx != CMD_endif
7224 && ea.cmdidx != CMD_endfor
7225 && ea.cmdidx != CMD_endwhile
7226 && ea.cmdidx != CMD_catch
7227 && ea.cmdidx != CMD_finally
7228 && ea.cmdidx != CMD_endtry)
7229 {
7230 if (cctx.ctx_had_return)
7231 {
7232 emsg(_("E1095: Unreachable code after :return"));
7233 goto erret;
7234 }
7235 }
7236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007237 switch (ea.cmdidx)
7238 {
7239 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007240 ea.arg = p;
7241 line = compile_nested_function(&ea, &cctx);
7242 break;
7243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007245 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007246 goto erret;
7247
7248 case CMD_return:
7249 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007250 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007251 break;
7252
7253 case CMD_let:
7254 case CMD_const:
7255 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007256 if (line == p)
7257 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 break;
7259
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007260 case CMD_unlet:
7261 case CMD_unlockvar:
7262 case CMD_lockvar:
7263 line = compile_unletlock(p, &ea, &cctx);
7264 break;
7265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007266 case CMD_import:
7267 line = compile_import(p, &cctx);
7268 break;
7269
7270 case CMD_if:
7271 line = compile_if(p, &cctx);
7272 break;
7273 case CMD_elseif:
7274 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007275 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276 break;
7277 case CMD_else:
7278 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007279 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007280 break;
7281 case CMD_endif:
7282 line = compile_endif(p, &cctx);
7283 break;
7284
7285 case CMD_while:
7286 line = compile_while(p, &cctx);
7287 break;
7288 case CMD_endwhile:
7289 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007290 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007291 break;
7292
7293 case CMD_for:
7294 line = compile_for(p, &cctx);
7295 break;
7296 case CMD_endfor:
7297 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007298 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007299 break;
7300 case CMD_continue:
7301 line = compile_continue(p, &cctx);
7302 break;
7303 case CMD_break:
7304 line = compile_break(p, &cctx);
7305 break;
7306
7307 case CMD_try:
7308 line = compile_try(p, &cctx);
7309 break;
7310 case CMD_catch:
7311 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007312 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007313 break;
7314 case CMD_finally:
7315 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007316 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 break;
7318 case CMD_endtry:
7319 line = compile_endtry(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 case CMD_throw:
7323 line = compile_throw(p, &cctx);
7324 break;
7325
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007326 case CMD_eval:
7327 if (compile_expr0(&p, &cctx) == FAIL)
7328 goto erret;
7329
7330 // drop the return value
7331 generate_instr_drop(&cctx, ISN_DROP, 1);
7332
7333 line = skipwhite(p);
7334 break;
7335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007338 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007339 case CMD_echomsg:
7340 case CMD_echoerr:
7341 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007342 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007343
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007344 // TODO: other commands with an expression argument
7345
Bram Moolenaar002262f2020-07-08 17:47:57 +02007346 case CMD_SIZE:
7347 semsg(_("E476: Invalid command: %s"), ea.cmd);
7348 goto erret;
7349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007351 // Not recognized, execute with do_cmdline_cmd().
7352 ea.arg = p;
7353 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354 break;
7355 }
7356 if (line == NULL)
7357 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007358 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007359
7360 if (cctx.ctx_type_stack.ga_len < 0)
7361 {
7362 iemsg("Type stack underflow");
7363 goto erret;
7364 }
7365 }
7366
7367 if (cctx.ctx_scope != NULL)
7368 {
7369 if (cctx.ctx_scope->se_type == IF_SCOPE)
7370 emsg(_(e_endif));
7371 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7372 emsg(_(e_endwhile));
7373 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7374 emsg(_(e_endfor));
7375 else
7376 emsg(_("E1026: Missing }"));
7377 goto erret;
7378 }
7379
Bram Moolenaarefd88552020-06-18 20:50:10 +02007380 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 {
7382 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7383 {
7384 emsg(_("E1027: Missing return statement"));
7385 goto erret;
7386 }
7387
7388 // Return zero if there is no return at the end.
7389 generate_PUSHNR(&cctx, 0);
7390 generate_instr(&cctx, ISN_RETURN);
7391 }
7392
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007393 {
7394 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7395 + ufunc->uf_dfunc_idx;
7396 dfunc->df_deleted = FALSE;
7397 dfunc->df_instr = instr->ga_data;
7398 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007399 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007400 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007401 if (cctx.ctx_outer_used)
7402 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007403 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007404 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405
7406 ret = OK;
7407
7408erret:
7409 if (ret == FAIL)
7410 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007411 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007412 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7413 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007414
7415 for (idx = 0; idx < instr->ga_len; ++idx)
7416 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007418
Bram Moolenaar45a15082020-05-25 00:28:33 +02007419 // if using the last entry in the table we might as well remove it
7420 if (!dfunc->df_deleted
7421 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007422 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007423 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007424
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007425 while (cctx.ctx_scope != NULL)
7426 drop_scope(&cctx);
7427
Bram Moolenaar20431c92020-03-20 18:39:46 +01007428 // Don't execute this function body.
7429 ga_clear_strings(&ufunc->uf_lines);
7430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431 if (errormsg != NULL)
7432 emsg(errormsg);
7433 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007434 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435 }
7436
7437 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007438 if (do_estack_push)
7439 estack_pop();
7440
Bram Moolenaar20431c92020-03-20 18:39:46 +01007441 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007442 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007444 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445}
7446
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007447 void
7448set_function_type(ufunc_T *ufunc)
7449{
7450 int varargs = ufunc->uf_va_name != NULL;
7451 int argcount = ufunc->uf_args.ga_len;
7452
7453 // Create a type for the function, with the return type and any
7454 // argument types.
7455 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7456 // The type is included in "tt_args".
7457 if (argcount > 0 || varargs)
7458 {
7459 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7460 argcount, &ufunc->uf_type_list);
7461 // Add argument types to the function type.
7462 if (func_type_add_arg_types(ufunc->uf_func_type,
7463 argcount + varargs,
7464 &ufunc->uf_type_list) == FAIL)
7465 return;
7466 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7467 ufunc->uf_func_type->tt_min_argcount =
7468 argcount - ufunc->uf_def_args.ga_len;
7469 if (ufunc->uf_arg_types == NULL)
7470 {
7471 int i;
7472
7473 // lambda does not have argument types.
7474 for (i = 0; i < argcount; ++i)
7475 ufunc->uf_func_type->tt_args[i] = &t_any;
7476 }
7477 else
7478 mch_memmove(ufunc->uf_func_type->tt_args,
7479 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7480 if (varargs)
7481 {
7482 ufunc->uf_func_type->tt_args[argcount] =
7483 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7484 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7485 }
7486 }
7487 else
7488 // No arguments, can use a predefined type.
7489 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7490 argcount, &ufunc->uf_type_list);
7491}
7492
7493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494/*
7495 * Delete an instruction, free what it contains.
7496 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007497 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007498delete_instr(isn_T *isn)
7499{
7500 switch (isn->isn_type)
7501 {
7502 case ISN_EXEC:
7503 case ISN_LOADENV:
7504 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007505 case ISN_LOADB:
7506 case ISN_LOADW:
7507 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007508 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007509 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510 case ISN_PUSHEXC:
7511 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007512 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007514 case ISN_STOREB:
7515 case ISN_STOREW:
7516 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007517 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 vim_free(isn->isn_arg.string);
7519 break;
7520
7521 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007522 case ISN_STORES:
7523 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007524 break;
7525
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007526 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007527 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007528 vim_free(isn->isn_arg.unlet.ul_name);
7529 break;
7530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007531 case ISN_STOREOPT:
7532 vim_free(isn->isn_arg.storeopt.so_name);
7533 break;
7534
7535 case ISN_PUSHBLOB: // push blob isn_arg.blob
7536 blob_unref(isn->isn_arg.blob);
7537 break;
7538
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007539 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007540#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007541 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007542#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007543 break;
7544
7545 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007546#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007547 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007548#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007549 break;
7550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551 case ISN_UCALL:
7552 vim_free(isn->isn_arg.ufunc.cuf_name);
7553 break;
7554
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007555 case ISN_FUNCREF:
7556 {
7557 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7558 + isn->isn_arg.funcref.fr_func;
7559 func_ptr_unref(dfunc->df_ufunc);
7560 }
7561 break;
7562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563 case ISN_2BOOL:
7564 case ISN_2STRING:
7565 case ISN_ADDBLOB:
7566 case ISN_ADDLIST:
7567 case ISN_BCALL:
7568 case ISN_CATCH:
7569 case ISN_CHECKNR:
7570 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007571 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572 case ISN_COMPAREANY:
7573 case ISN_COMPAREBLOB:
7574 case ISN_COMPAREBOOL:
7575 case ISN_COMPAREDICT:
7576 case ISN_COMPAREFLOAT:
7577 case ISN_COMPAREFUNC:
7578 case ISN_COMPARELIST:
7579 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580 case ISN_COMPARESPECIAL:
7581 case ISN_COMPARESTRING:
7582 case ISN_CONCAT:
7583 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007584 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585 case ISN_DROP:
7586 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007587 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007588 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007589 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007590 case ISN_EXECCONCAT:
7591 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007592 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007593 case ISN_LISTINDEX:
7594 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007595 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007596 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007597 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007598 case ISN_JUMP:
7599 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007600 case ISN_LOADBDICT:
7601 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007602 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007603 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007604 case ISN_LOADSCRIPT:
7605 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007606 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007607 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007608 case ISN_NEGATENR:
7609 case ISN_NEWDICT:
7610 case ISN_NEWLIST:
7611 case ISN_OPNR:
7612 case ISN_OPFLOAT:
7613 case ISN_OPANY:
7614 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007615 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007616 case ISN_PUSHF:
7617 case ISN_PUSHNR:
7618 case ISN_PUSHBOOL:
7619 case ISN_PUSHSPEC:
7620 case ISN_RETURN:
7621 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007622 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007623 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007625 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007627 case ISN_STOREDICT:
7628 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 case ISN_THROW:
7630 case ISN_TRY:
7631 // nothing allocated
7632 break;
7633 }
7634}
7635
7636/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007637 * Free all instructions for "dfunc".
7638 */
7639 static void
7640delete_def_function_contents(dfunc_T *dfunc)
7641{
7642 int idx;
7643
7644 ga_clear(&dfunc->df_def_args_isn);
7645
7646 if (dfunc->df_instr != NULL)
7647 {
7648 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7649 delete_instr(dfunc->df_instr + idx);
7650 VIM_CLEAR(dfunc->df_instr);
7651 }
7652
7653 dfunc->df_deleted = TRUE;
7654}
7655
7656/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007657 * When a user function is deleted, clear the contents of any associated def
7658 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007659 */
7660 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007661clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007662{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007663 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007664 {
7665 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7666 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667
Bram Moolenaar20431c92020-03-20 18:39:46 +01007668 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007669 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007670 }
7671}
7672
7673#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007674/*
7675 * Free all functions defined with ":def".
7676 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007677 void
7678free_def_functions(void)
7679{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007680 int idx;
7681
7682 for (idx = 0; idx < def_functions.ga_len; ++idx)
7683 {
7684 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7685
7686 delete_def_function_contents(dfunc);
7687 }
7688
7689 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007690}
7691#endif
7692
7693
7694#endif // FEAT_EVAL