blob: 0e3e01ac73a03b019ead429816f04238be3b602d [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 }
602 else
603 actual.tt_member = &t_any;
604 return check_type(expected, &actual, TRUE);
605}
606
607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608/////////////////////////////////////////////////////////////////////
609// Following generate_ functions expect the caller to call ga_grow().
610
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200611#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
612#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614/*
615 * Generate an instruction without arguments.
616 * Returns a pointer to the new instruction, NULL if failed.
617 */
618 static isn_T *
619generate_instr(cctx_T *cctx, isntype_T isn_type)
620{
621 garray_T *instr = &cctx->ctx_instr;
622 isn_T *isn;
623
Bram Moolenaar080457c2020-03-03 21:53:32 +0100624 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 if (ga_grow(instr, 1) == FAIL)
626 return NULL;
627 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
628 isn->isn_type = isn_type;
629 isn->isn_lnum = cctx->ctx_lnum + 1;
630 ++instr->ga_len;
631
632 return isn;
633}
634
635/*
636 * Generate an instruction without arguments.
637 * "drop" will be removed from the stack.
638 * Returns a pointer to the new instruction, NULL if failed.
639 */
640 static isn_T *
641generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
642{
643 garray_T *stack = &cctx->ctx_type_stack;
644
Bram Moolenaar080457c2020-03-03 21:53:32 +0100645 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646 stack->ga_len -= drop;
647 return generate_instr(cctx, isn_type);
648}
649
650/*
651 * Generate instruction "isn_type" and put "type" on the type stack.
652 */
653 static isn_T *
654generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
655{
656 isn_T *isn;
657 garray_T *stack = &cctx->ctx_type_stack;
658
659 if ((isn = generate_instr(cctx, isn_type)) == NULL)
660 return NULL;
661
662 if (ga_grow(stack, 1) == FAIL)
663 return NULL;
664 ((type_T **)stack->ga_data)[stack->ga_len] = type;
665 ++stack->ga_len;
666
667 return isn;
668}
669
670/*
671 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
672 */
673 static int
674may_generate_2STRING(int offset, cctx_T *cctx)
675{
676 isn_T *isn;
677 garray_T *stack = &cctx->ctx_type_stack;
678 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
679
680 if ((*type)->tt_type == VAR_STRING)
681 return OK;
682 *type = &t_string;
683
684 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
685 return FAIL;
686 isn->isn_arg.number = offset;
687
688 return OK;
689}
690
691 static int
692check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
693{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200694 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200696 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 {
698 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200699 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 else
701 semsg(_("E1036: %c requires number or float arguments"), *op);
702 return FAIL;
703 }
704 return OK;
705}
706
707/*
708 * Generate an instruction with two arguments. The instruction depends on the
709 * type of the arguments.
710 */
711 static int
712generate_two_op(cctx_T *cctx, char_u *op)
713{
714 garray_T *stack = &cctx->ctx_type_stack;
715 type_T *type1;
716 type_T *type2;
717 vartype_T vartype;
718 isn_T *isn;
719
Bram Moolenaar080457c2020-03-03 21:53:32 +0100720 RETURN_OK_IF_SKIP(cctx);
721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 // Get the known type of the two items on the stack. If they are matching
723 // use a type-specific instruction. Otherwise fall back to runtime type
724 // checking.
725 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
726 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200727 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 if (type1->tt_type == type2->tt_type
729 && (type1->tt_type == VAR_NUMBER
730 || type1->tt_type == VAR_LIST
731#ifdef FEAT_FLOAT
732 || type1->tt_type == VAR_FLOAT
733#endif
734 || type1->tt_type == VAR_BLOB))
735 vartype = type1->tt_type;
736
737 switch (*op)
738 {
739 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200740 && type1->tt_type != VAR_ANY
741 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742 && check_number_or_float(
743 type1->tt_type, type2->tt_type, op) == FAIL)
744 return FAIL;
745 isn = generate_instr_drop(cctx,
746 vartype == VAR_NUMBER ? ISN_OPNR
747 : vartype == VAR_LIST ? ISN_ADDLIST
748 : vartype == VAR_BLOB ? ISN_ADDBLOB
749#ifdef FEAT_FLOAT
750 : vartype == VAR_FLOAT ? ISN_OPFLOAT
751#endif
752 : ISN_OPANY, 1);
753 if (isn != NULL)
754 isn->isn_arg.op.op_type = EXPR_ADD;
755 break;
756
757 case '-':
758 case '*':
759 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
760 op) == FAIL)
761 return FAIL;
762 if (vartype == VAR_NUMBER)
763 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
764#ifdef FEAT_FLOAT
765 else if (vartype == VAR_FLOAT)
766 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
767#endif
768 else
769 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
770 if (isn != NULL)
771 isn->isn_arg.op.op_type = *op == '*'
772 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
773 break;
774
Bram Moolenaar4c683752020-04-05 21:38:23 +0200775 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200777 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 && type2->tt_type != VAR_NUMBER))
779 {
780 emsg(_("E1035: % requires number arguments"));
781 return FAIL;
782 }
783 isn = generate_instr_drop(cctx,
784 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
785 if (isn != NULL)
786 isn->isn_arg.op.op_type = EXPR_REM;
787 break;
788 }
789
790 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200791 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792 {
793 type_T *type = &t_any;
794
795#ifdef FEAT_FLOAT
796 // float+number and number+float results in float
797 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
798 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
799 type = &t_float;
800#endif
801 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
802 }
803
804 return OK;
805}
806
807/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200808 * Get the instruction to use for comparing "type1" with "type2"
809 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200811 static isntype_T
812get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813{
814 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815
Bram Moolenaar4c683752020-04-05 21:38:23 +0200816 if (type1 == VAR_UNKNOWN)
817 type1 = VAR_ANY;
818 if (type2 == VAR_UNKNOWN)
819 type2 = VAR_ANY;
820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 if (type1 == type2)
822 {
823 switch (type1)
824 {
825 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
826 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
827 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
828 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
829 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
830 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
831 case VAR_LIST: isntype = ISN_COMPARELIST; break;
832 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
833 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834 default: isntype = ISN_COMPAREANY; break;
835 }
836 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200837 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
839 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
840 isntype = ISN_COMPAREANY;
841
842 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
843 && (isntype == ISN_COMPAREBOOL
844 || isntype == ISN_COMPARESPECIAL
845 || isntype == ISN_COMPARENR
846 || isntype == ISN_COMPAREFLOAT))
847 {
848 semsg(_("E1037: Cannot use \"%s\" with %s"),
849 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200850 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 }
852 if (isntype == ISN_DROP
853 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
854 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
855 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
856 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
857 && exptype != EXPR_IS && exptype != EXPR_ISNOT
858 && (type1 == VAR_BLOB || type2 == VAR_BLOB
859 || type1 == VAR_LIST || type2 == VAR_LIST))))
860 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100861 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200863 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200865 return isntype;
866}
867
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200868 int
869check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
870{
871 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
872 return FAIL;
873 return OK;
874}
875
Bram Moolenaara5565e42020-05-09 15:44:01 +0200876/*
877 * Generate an ISN_COMPARE* instruction with a boolean result.
878 */
879 static int
880generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
881{
882 isntype_T isntype;
883 isn_T *isn;
884 garray_T *stack = &cctx->ctx_type_stack;
885 vartype_T type1;
886 vartype_T type2;
887
888 RETURN_OK_IF_SKIP(cctx);
889
890 // Get the known type of the two items on the stack. If they are matching
891 // use a type-specific instruction. Otherwise fall back to runtime type
892 // checking.
893 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
894 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
895 isntype = get_compare_isn(exptype, type1, type2);
896 if (isntype == ISN_DROP)
897 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898
899 if ((isn = generate_instr(cctx, isntype)) == NULL)
900 return FAIL;
901 isn->isn_arg.op.op_type = exptype;
902 isn->isn_arg.op.op_ic = ic;
903
904 // takes two arguments, puts one bool back
905 if (stack->ga_len >= 2)
906 {
907 --stack->ga_len;
908 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
909 }
910
911 return OK;
912}
913
914/*
915 * Generate an ISN_2BOOL instruction.
916 */
917 static int
918generate_2BOOL(cctx_T *cctx, int invert)
919{
920 isn_T *isn;
921 garray_T *stack = &cctx->ctx_type_stack;
922
Bram Moolenaar080457c2020-03-03 21:53:32 +0100923 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
925 return FAIL;
926 isn->isn_arg.number = invert;
927
928 // type becomes bool
929 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
930
931 return OK;
932}
933
934 static int
935generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
936{
937 isn_T *isn;
938 garray_T *stack = &cctx->ctx_type_stack;
939
Bram Moolenaar080457c2020-03-03 21:53:32 +0100940 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
942 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200943 // TODO: whole type, e.g. for a function also arg and return types
944 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 isn->isn_arg.type.ct_off = offset;
946
947 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200948 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949
950 return OK;
951}
952
953/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200954 * Check that
955 * - "actual" is "expected" type or
956 * - "actual" is a type that can be "expected" type: add a runtime check; or
957 * - return FAIL.
958 */
959 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200960need_type(
961 type_T *actual,
962 type_T *expected,
963 int offset,
964 cctx_T *cctx,
965 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200966{
967 if (check_type(expected, actual, FALSE) == OK)
968 return OK;
969 if (actual->tt_type != VAR_ANY
970 && actual->tt_type != VAR_UNKNOWN
971 && !(actual->tt_type == VAR_FUNC
972 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
973 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200974 if (!silent)
975 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200976 return FAIL;
977 }
978 generate_TYPECHECK(cctx, expected, offset);
979 return OK;
980}
981
982/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 * Generate an ISN_PUSHNR instruction.
984 */
985 static int
986generate_PUSHNR(cctx_T *cctx, varnumber_T number)
987{
988 isn_T *isn;
989
Bram Moolenaar080457c2020-03-03 21:53:32 +0100990 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
992 return FAIL;
993 isn->isn_arg.number = number;
994
995 return OK;
996}
997
998/*
999 * Generate an ISN_PUSHBOOL instruction.
1000 */
1001 static int
1002generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1003{
1004 isn_T *isn;
1005
Bram Moolenaar080457c2020-03-03 21:53:32 +01001006 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1008 return FAIL;
1009 isn->isn_arg.number = number;
1010
1011 return OK;
1012}
1013
1014/*
1015 * Generate an ISN_PUSHSPEC instruction.
1016 */
1017 static int
1018generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1019{
1020 isn_T *isn;
1021
Bram Moolenaar080457c2020-03-03 21:53:32 +01001022 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1024 return FAIL;
1025 isn->isn_arg.number = number;
1026
1027 return OK;
1028}
1029
1030#ifdef FEAT_FLOAT
1031/*
1032 * Generate an ISN_PUSHF instruction.
1033 */
1034 static int
1035generate_PUSHF(cctx_T *cctx, float_T fnumber)
1036{
1037 isn_T *isn;
1038
Bram Moolenaar080457c2020-03-03 21:53:32 +01001039 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1041 return FAIL;
1042 isn->isn_arg.fnumber = fnumber;
1043
1044 return OK;
1045}
1046#endif
1047
1048/*
1049 * Generate an ISN_PUSHS instruction.
1050 * Consumes "str".
1051 */
1052 static int
1053generate_PUSHS(cctx_T *cctx, char_u *str)
1054{
1055 isn_T *isn;
1056
Bram Moolenaar080457c2020-03-03 21:53:32 +01001057 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1059 return FAIL;
1060 isn->isn_arg.string = str;
1061
1062 return OK;
1063}
1064
1065/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001066 * Generate an ISN_PUSHCHANNEL instruction.
1067 * Consumes "channel".
1068 */
1069 static int
1070generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1071{
1072 isn_T *isn;
1073
Bram Moolenaar080457c2020-03-03 21:53:32 +01001074 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001075 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1076 return FAIL;
1077 isn->isn_arg.channel = channel;
1078
1079 return OK;
1080}
1081
1082/*
1083 * Generate an ISN_PUSHJOB instruction.
1084 * Consumes "job".
1085 */
1086 static int
1087generate_PUSHJOB(cctx_T *cctx, job_T *job)
1088{
1089 isn_T *isn;
1090
Bram Moolenaar080457c2020-03-03 21:53:32 +01001091 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001092 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001093 return FAIL;
1094 isn->isn_arg.job = job;
1095
1096 return OK;
1097}
1098
1099/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 * Generate an ISN_PUSHBLOB instruction.
1101 * Consumes "blob".
1102 */
1103 static int
1104generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1105{
1106 isn_T *isn;
1107
Bram Moolenaar080457c2020-03-03 21:53:32 +01001108 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1110 return FAIL;
1111 isn->isn_arg.blob = blob;
1112
1113 return OK;
1114}
1115
1116/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001117 * Generate an ISN_PUSHFUNC instruction with name "name".
1118 * Consumes "name".
1119 */
1120 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001121generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001122{
1123 isn_T *isn;
1124
Bram Moolenaar080457c2020-03-03 21:53:32 +01001125 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001126 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001127 return FAIL;
1128 isn->isn_arg.string = name;
1129
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001134 * Generate an ISN_GETITEM instruction with "index".
1135 */
1136 static int
1137generate_GETITEM(cctx_T *cctx, int index)
1138{
1139 isn_T *isn;
1140 garray_T *stack = &cctx->ctx_type_stack;
1141 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1142 type_T *item_type = &t_any;
1143
1144 RETURN_OK_IF_SKIP(cctx);
1145
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001146 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001147 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001148 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001149 emsg(_(e_listreq));
1150 return FAIL;
1151 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001152 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001153 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1154 return FAIL;
1155 isn->isn_arg.number = index;
1156
1157 // add the item type to the type stack
1158 if (ga_grow(stack, 1) == FAIL)
1159 return FAIL;
1160 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1161 ++stack->ga_len;
1162 return OK;
1163}
1164
1165/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001166 * Generate an ISN_SLICE instruction with "count".
1167 */
1168 static int
1169generate_SLICE(cctx_T *cctx, int count)
1170{
1171 isn_T *isn;
1172
1173 RETURN_OK_IF_SKIP(cctx);
1174 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1175 return FAIL;
1176 isn->isn_arg.number = count;
1177 return OK;
1178}
1179
1180/*
1181 * Generate an ISN_CHECKLEN instruction with "min_len".
1182 */
1183 static int
1184generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1185{
1186 isn_T *isn;
1187
1188 RETURN_OK_IF_SKIP(cctx);
1189
1190 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1191 return FAIL;
1192 isn->isn_arg.checklen.cl_min_len = min_len;
1193 isn->isn_arg.checklen.cl_more_OK = more_OK;
1194
1195 return OK;
1196}
1197
1198/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 * Generate an ISN_STORE instruction.
1200 */
1201 static int
1202generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1203{
1204 isn_T *isn;
1205
Bram Moolenaar080457c2020-03-03 21:53:32 +01001206 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1208 return FAIL;
1209 if (name != NULL)
1210 isn->isn_arg.string = vim_strsave(name);
1211 else
1212 isn->isn_arg.number = idx;
1213
1214 return OK;
1215}
1216
1217/*
1218 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1219 */
1220 static int
1221generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1222{
1223 isn_T *isn;
1224
Bram Moolenaar080457c2020-03-03 21:53:32 +01001225 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1227 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001228 isn->isn_arg.storenr.stnr_idx = idx;
1229 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230
1231 return OK;
1232}
1233
1234/*
1235 * Generate an ISN_STOREOPT instruction
1236 */
1237 static int
1238generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1239{
1240 isn_T *isn;
1241
Bram Moolenaar080457c2020-03-03 21:53:32 +01001242 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1244 return FAIL;
1245 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1246 isn->isn_arg.storeopt.so_flags = opt_flags;
1247
1248 return OK;
1249}
1250
1251/*
1252 * Generate an ISN_LOAD or similar instruction.
1253 */
1254 static int
1255generate_LOAD(
1256 cctx_T *cctx,
1257 isntype_T isn_type,
1258 int idx,
1259 char_u *name,
1260 type_T *type)
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_type(cctx, isn_type, type)) == NULL)
1266 return FAIL;
1267 if (name != NULL)
1268 isn->isn_arg.string = vim_strsave(name);
1269 else
1270 isn->isn_arg.number = idx;
1271
1272 return OK;
1273}
1274
1275/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001276 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001277 */
1278 static int
1279generate_LOADV(
1280 cctx_T *cctx,
1281 char_u *name,
1282 int error)
1283{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001284 int di_flags;
1285 int vidx = find_vim_var(name, &di_flags);
1286 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001287
Bram Moolenaar080457c2020-03-03 21:53:32 +01001288 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001289 if (vidx < 0)
1290 {
1291 if (error)
1292 semsg(_(e_var_notfound), name);
1293 return FAIL;
1294 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001295 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001296
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001297 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001298}
1299
1300/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001301 * Generate an ISN_UNLET instruction.
1302 */
1303 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001304generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001305{
1306 isn_T *isn;
1307
1308 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001309 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001310 return FAIL;
1311 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1312 isn->isn_arg.unlet.ul_forceit = forceit;
1313
1314 return OK;
1315}
1316
1317/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 * Generate an ISN_LOADS instruction.
1319 */
1320 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001321generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001323 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001325 int sid,
1326 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327{
1328 isn_T *isn;
1329
Bram Moolenaar080457c2020-03-03 21:53:32 +01001330 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001331 if (isn_type == ISN_LOADS)
1332 isn = generate_instr_type(cctx, isn_type, type);
1333 else
1334 isn = generate_instr_drop(cctx, isn_type, 1);
1335 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001337 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1338 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339
1340 return OK;
1341}
1342
1343/*
1344 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1345 */
1346 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001347generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 cctx_T *cctx,
1349 isntype_T isn_type,
1350 int sid,
1351 int idx,
1352 type_T *type)
1353{
1354 isn_T *isn;
1355
Bram Moolenaar080457c2020-03-03 21:53:32 +01001356 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 if (isn_type == ISN_LOADSCRIPT)
1358 isn = generate_instr_type(cctx, isn_type, type);
1359 else
1360 isn = generate_instr_drop(cctx, isn_type, 1);
1361 if (isn == NULL)
1362 return FAIL;
1363 isn->isn_arg.script.script_sid = sid;
1364 isn->isn_arg.script.script_idx = idx;
1365 return OK;
1366}
1367
1368/*
1369 * Generate an ISN_NEWLIST instruction.
1370 */
1371 static int
1372generate_NEWLIST(cctx_T *cctx, int count)
1373{
1374 isn_T *isn;
1375 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 type_T *type;
1377 type_T *member;
1378
Bram Moolenaar080457c2020-03-03 21:53:32 +01001379 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1381 return FAIL;
1382 isn->isn_arg.number = count;
1383
1384 // drop the value types
1385 stack->ga_len -= count;
1386
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001387 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001388 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 if (count > 0)
1390 member = ((type_T **)stack->ga_data)[stack->ga_len];
1391 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001392 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001393 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394
1395 // add the list type to the type stack
1396 if (ga_grow(stack, 1) == FAIL)
1397 return FAIL;
1398 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1399 ++stack->ga_len;
1400
1401 return OK;
1402}
1403
1404/*
1405 * Generate an ISN_NEWDICT instruction.
1406 */
1407 static int
1408generate_NEWDICT(cctx_T *cctx, int count)
1409{
1410 isn_T *isn;
1411 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 type_T *type;
1413 type_T *member;
1414
Bram Moolenaar080457c2020-03-03 21:53:32 +01001415 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1417 return FAIL;
1418 isn->isn_arg.number = count;
1419
1420 // drop the key and value types
1421 stack->ga_len -= 2 * count;
1422
Bram Moolenaar436472f2020-02-20 22:54:43 +01001423 // Use the first value type for the list member type. Use "void" for an
1424 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 if (count > 0)
1426 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1427 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001428 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001429 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430
1431 // add the dict type to the type stack
1432 if (ga_grow(stack, 1) == FAIL)
1433 return FAIL;
1434 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1435 ++stack->ga_len;
1436
1437 return OK;
1438}
1439
1440/*
1441 * Generate an ISN_FUNCREF instruction.
1442 */
1443 static int
1444generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1445{
1446 isn_T *isn;
1447 garray_T *stack = &cctx->ctx_type_stack;
1448
Bram Moolenaar080457c2020-03-03 21:53:32 +01001449 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1451 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001452 isn->isn_arg.funcref.fr_func = dfunc_idx;
1453 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454
1455 if (ga_grow(stack, 1) == FAIL)
1456 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001457 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 // TODO: argument and return types
1459 ++stack->ga_len;
1460
1461 return OK;
1462}
1463
1464/*
1465 * Generate an ISN_JUMP instruction.
1466 */
1467 static int
1468generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1469{
1470 isn_T *isn;
1471 garray_T *stack = &cctx->ctx_type_stack;
1472
Bram Moolenaar080457c2020-03-03 21:53:32 +01001473 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1475 return FAIL;
1476 isn->isn_arg.jump.jump_when = when;
1477 isn->isn_arg.jump.jump_where = where;
1478
1479 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1480 --stack->ga_len;
1481
1482 return OK;
1483}
1484
1485 static int
1486generate_FOR(cctx_T *cctx, int loop_idx)
1487{
1488 isn_T *isn;
1489 garray_T *stack = &cctx->ctx_type_stack;
1490
Bram Moolenaar080457c2020-03-03 21:53:32 +01001491 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1493 return FAIL;
1494 isn->isn_arg.forloop.for_idx = loop_idx;
1495
1496 if (ga_grow(stack, 1) == FAIL)
1497 return FAIL;
1498 // type doesn't matter, will be stored next
1499 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1500 ++stack->ga_len;
1501
1502 return OK;
1503}
1504
1505/*
1506 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001507 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 * Return FAIL if the number of arguments is wrong.
1509 */
1510 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001511generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512{
1513 isn_T *isn;
1514 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001515 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001516 type_T *argtypes[MAX_FUNC_ARGS];
1517 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518
Bram Moolenaar080457c2020-03-03 21:53:32 +01001519 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001520 argoff = check_internal_func(func_idx, argcount);
1521 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 return FAIL;
1523
Bram Moolenaar389df252020-07-09 21:20:47 +02001524 if (method_call && argoff > 1)
1525 {
1526 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1527 return FAIL;
1528 isn->isn_arg.shuffle.shfl_item = argcount;
1529 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1530 }
1531
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1533 return FAIL;
1534 isn->isn_arg.bfunc.cbf_idx = func_idx;
1535 isn->isn_arg.bfunc.cbf_argcount = argcount;
1536
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001537 for (i = 0; i < argcount; ++i)
1538 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 stack->ga_len -= argcount; // drop the arguments
1541 if (ga_grow(stack, 1) == FAIL)
1542 return FAIL;
1543 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001544 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 ++stack->ga_len; // add return value
1546
1547 return OK;
1548}
1549
1550/*
1551 * Generate an ISN_DCALL or ISN_UCALL instruction.
1552 * Return FAIL if the number of arguments is wrong.
1553 */
1554 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001555generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556{
1557 isn_T *isn;
1558 garray_T *stack = &cctx->ctx_type_stack;
1559 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001560 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561
Bram Moolenaar080457c2020-03-03 21:53:32 +01001562 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 if (argcount > regular_args && !has_varargs(ufunc))
1564 {
1565 semsg(_(e_toomanyarg), ufunc->uf_name);
1566 return FAIL;
1567 }
1568 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1569 {
1570 semsg(_(e_toofewarg), ufunc->uf_name);
1571 return FAIL;
1572 }
1573
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001574 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001575 {
1576 int i;
1577
1578 for (i = 0; i < argcount; ++i)
1579 {
1580 type_T *expected;
1581 type_T *actual;
1582
1583 if (i < regular_args)
1584 {
1585 if (ufunc->uf_arg_types == NULL)
1586 continue;
1587 expected = ufunc->uf_arg_types[i];
1588 }
1589 else
1590 expected = ufunc->uf_va_type->tt_member;
1591 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001592 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001593 {
1594 arg_type_mismatch(expected, actual, i + 1);
1595 return FAIL;
1596 }
1597 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001598 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001599 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001600 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001601 }
1602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001604 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001605 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001607 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 {
1609 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1610 isn->isn_arg.dfunc.cdf_argcount = argcount;
1611 }
1612 else
1613 {
1614 // A user function may be deleted and redefined later, can't use the
1615 // ufunc pointer, need to look it up again at runtime.
1616 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1617 isn->isn_arg.ufunc.cuf_argcount = argcount;
1618 }
1619
1620 stack->ga_len -= argcount; // drop the arguments
1621 if (ga_grow(stack, 1) == FAIL)
1622 return FAIL;
1623 // add return value
1624 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1625 ++stack->ga_len;
1626
1627 return OK;
1628}
1629
1630/*
1631 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1632 */
1633 static int
1634generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1635{
1636 isn_T *isn;
1637 garray_T *stack = &cctx->ctx_type_stack;
1638
Bram Moolenaar080457c2020-03-03 21:53:32 +01001639 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1641 return FAIL;
1642 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1643 isn->isn_arg.ufunc.cuf_argcount = argcount;
1644
1645 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001646 if (ga_grow(stack, 1) == FAIL)
1647 return FAIL;
1648 // add return value
1649 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1650 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651
1652 return OK;
1653}
1654
1655/*
1656 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001657 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 */
1659 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001660generate_PCALL(
1661 cctx_T *cctx,
1662 int argcount,
1663 char_u *name,
1664 type_T *type,
1665 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666{
1667 isn_T *isn;
1668 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001669 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670
Bram Moolenaar080457c2020-03-03 21:53:32 +01001671 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001672
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001673 if (type->tt_type == VAR_ANY)
1674 ret_type = &t_any;
1675 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001676 {
1677 if (type->tt_argcount != -1)
1678 {
1679 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1680
1681 if (argcount < type->tt_min_argcount - varargs)
1682 {
1683 semsg(_(e_toofewarg), "[reference]");
1684 return FAIL;
1685 }
1686 if (!varargs && argcount > type->tt_argcount)
1687 {
1688 semsg(_(e_toomanyarg), "[reference]");
1689 return FAIL;
1690 }
1691 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001692 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001693 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001694 else
1695 {
1696 semsg(_("E1085: Not a callable type: %s"), name);
1697 return FAIL;
1698 }
1699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1701 return FAIL;
1702 isn->isn_arg.pfunc.cpf_top = at_top;
1703 isn->isn_arg.pfunc.cpf_argcount = argcount;
1704
1705 stack->ga_len -= argcount; // drop the arguments
1706
1707 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001708 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001710 // If partial is above the arguments it must be cleared and replaced with
1711 // the return value.
1712 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1713 return FAIL;
1714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715 return OK;
1716}
1717
1718/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001719 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 */
1721 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001722generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723{
1724 isn_T *isn;
1725 garray_T *stack = &cctx->ctx_type_stack;
1726 type_T *type;
1727
Bram Moolenaar080457c2020-03-03 21:53:32 +01001728 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001729 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001731 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001733 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001735 if (type->tt_type != VAR_DICT && type != &t_any)
1736 {
1737 emsg(_(e_dictreq));
1738 return FAIL;
1739 }
1740 // change dict type to dict member type
1741 if (type->tt_type == VAR_DICT)
1742 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743
1744 return OK;
1745}
1746
1747/*
1748 * Generate an ISN_ECHO instruction.
1749 */
1750 static int
1751generate_ECHO(cctx_T *cctx, int with_white, int count)
1752{
1753 isn_T *isn;
1754
Bram Moolenaar080457c2020-03-03 21:53:32 +01001755 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1757 return FAIL;
1758 isn->isn_arg.echo.echo_with_white = with_white;
1759 isn->isn_arg.echo.echo_count = count;
1760
1761 return OK;
1762}
1763
Bram Moolenaarad39c092020-02-26 18:23:43 +01001764/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001765 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001766 */
1767 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001768generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001769{
1770 isn_T *isn;
1771
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001772 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001773 return FAIL;
1774 isn->isn_arg.number = count;
1775
1776 return OK;
1777}
1778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 static int
1780generate_EXEC(cctx_T *cctx, char_u *line)
1781{
1782 isn_T *isn;
1783
Bram Moolenaar080457c2020-03-03 21:53:32 +01001784 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1786 return FAIL;
1787 isn->isn_arg.string = vim_strsave(line);
1788 return OK;
1789}
1790
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001791 static int
1792generate_EXECCONCAT(cctx_T *cctx, int count)
1793{
1794 isn_T *isn;
1795
1796 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1797 return FAIL;
1798 isn->isn_arg.number = count;
1799 return OK;
1800}
1801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802/*
1803 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001804 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001806 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1808{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 lvar_T *lvar;
1810
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001811 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001813 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001814 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 }
1816
1817 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001818 return NULL;
1819 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001821 // Every local variable uses the next entry on the stack. We could re-use
1822 // the last ones when leaving a scope, but then variables used in a closure
1823 // might get overwritten. To keep things simple do not re-use stack
1824 // entries. This is less efficient, but memory is cheap these days.
1825 lvar->lv_idx = cctx->ctx_locals_count++;
1826
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001827 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 lvar->lv_const = isConst;
1829 lvar->lv_type = type;
1830
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001831 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832}
1833
1834/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001835 * Remove local variables above "new_top".
1836 */
1837 static void
1838unwind_locals(cctx_T *cctx, int new_top)
1839{
1840 if (cctx->ctx_locals.ga_len > new_top)
1841 {
1842 int idx;
1843 lvar_T *lvar;
1844
1845 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1846 {
1847 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1848 vim_free(lvar->lv_name);
1849 }
1850 }
1851 cctx->ctx_locals.ga_len = new_top;
1852}
1853
1854/*
1855 * Free all local variables.
1856 */
1857 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001858free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001859{
1860 unwind_locals(cctx, 0);
1861 ga_clear(&cctx->ctx_locals);
1862}
1863
1864/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 * Skip over a type definition and return a pointer to just after it.
1866 */
1867 char_u *
1868skip_type(char_u *start)
1869{
1870 char_u *p = start;
1871
1872 while (ASCII_ISALNUM(*p) || *p == '_')
1873 ++p;
1874
1875 // Skip over "<type>"; this is permissive about white space.
1876 if (*skipwhite(p) == '<')
1877 {
1878 p = skipwhite(p);
1879 p = skip_type(skipwhite(p + 1));
1880 p = skipwhite(p);
1881 if (*p == '>')
1882 ++p;
1883 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001884 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1885 {
1886 // handle func(args): type
1887 ++p;
1888 while (*p != ')' && *p != NUL)
1889 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001890 char_u *sp = p;
1891
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001892 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001893 if (p == sp)
1894 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001895 if (*p == ',')
1896 p = skipwhite(p + 1);
1897 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001898 if (*p == ')')
1899 {
1900 if (p[1] == ':')
1901 p = skip_type(skipwhite(p + 2));
1902 else
1903 p = skipwhite(p + 1);
1904 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001905 }
1906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 return p;
1908}
1909
1910/*
1911 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001912 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 * Returns NULL in case of failure.
1914 */
1915 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001916parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917{
1918 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001919 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920
1921 if (**arg != '<')
1922 {
1923 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001924 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 else
1926 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001927 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 }
1929 *arg = skipwhite(*arg + 1);
1930
Bram Moolenaard77a8522020-04-03 21:59:57 +02001931 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932
1933 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001934 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 {
1936 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001937 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 }
1939 ++*arg;
1940
1941 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001942 return get_list_type(member_type, type_gap);
1943 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944}
1945
1946/*
1947 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001948 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 */
1950 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001951parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952{
1953 char_u *p = *arg;
1954 size_t len;
1955
1956 // skip over the first word
1957 while (ASCII_ISALNUM(*p) || *p == '_')
1958 ++p;
1959 len = p - *arg;
1960
1961 switch (**arg)
1962 {
1963 case 'a':
1964 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1965 {
1966 *arg += len;
1967 return &t_any;
1968 }
1969 break;
1970 case 'b':
1971 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1972 {
1973 *arg += len;
1974 return &t_bool;
1975 }
1976 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1977 {
1978 *arg += len;
1979 return &t_blob;
1980 }
1981 break;
1982 case 'c':
1983 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1984 {
1985 *arg += len;
1986 return &t_channel;
1987 }
1988 break;
1989 case 'd':
1990 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1991 {
1992 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001993 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 }
1995 break;
1996 case 'f':
1997 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1998 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001999#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 *arg += len;
2001 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002002#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002003 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002004 return &t_any;
2005#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 }
2007 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2008 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002009 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002010 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002011 int argcount = -1;
2012 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002013 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002014 type_T *arg_type[MAX_FUNC_ARGS + 1];
2015
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002016 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002018 if (**arg == '(')
2019 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002020 // "func" may or may not return a value, "func()" does
2021 // not return a value.
2022 ret_type = &t_void;
2023
Bram Moolenaard77a8522020-04-03 21:59:57 +02002024 p = ++*arg;
2025 argcount = 0;
2026 while (*p != NUL && *p != ')')
2027 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002028 if (*p == '?')
2029 {
2030 if (first_optional == -1)
2031 first_optional = argcount;
2032 ++p;
2033 }
2034 else if (first_optional != -1)
2035 {
2036 emsg(_("E1007: mandatory argument after optional argument"));
2037 return &t_any;
2038 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002039 else if (STRNCMP(p, "...", 3) == 0)
2040 {
2041 flags |= TTFLAG_VARARGS;
2042 p += 3;
2043 }
2044
2045 arg_type[argcount++] = parse_type(&p, type_gap);
2046
2047 // Nothing comes after "...{type}".
2048 if (flags & TTFLAG_VARARGS)
2049 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002050
Bram Moolenaard77a8522020-04-03 21:59:57 +02002051 if (*p != ',' && *skipwhite(p) == ',')
2052 {
2053 semsg(_(e_no_white_before), ",");
2054 return &t_any;
2055 }
2056 if (*p == ',')
2057 {
2058 ++p;
2059 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002060 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002061 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002062 return &t_any;
2063 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002064 }
2065 p = skipwhite(p);
2066 if (argcount == MAX_FUNC_ARGS)
2067 {
2068 emsg(_("E740: Too many argument types"));
2069 return &t_any;
2070 }
2071 }
2072
2073 p = skipwhite(p);
2074 if (*p != ')')
2075 {
2076 emsg(_(e_missing_close));
2077 return &t_any;
2078 }
2079 *arg = p + 1;
2080 }
2081 if (**arg == ':')
2082 {
2083 // parse return type
2084 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002085 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002086 semsg(_(e_white_after), ":");
2087 *arg = skipwhite(*arg);
2088 ret_type = parse_type(arg, type_gap);
2089 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002090 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002091 type = get_func_type(ret_type, argcount, type_gap);
2092 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002093 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002094 type = alloc_func_type(ret_type, argcount, type_gap);
2095 type->tt_flags = flags;
2096 if (argcount > 0)
2097 {
2098 type->tt_argcount = argcount;
2099 type->tt_min_argcount = first_optional == -1
2100 ? argcount : first_optional;
2101 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002102 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002103 return &t_any;
2104 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002105 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002106 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002107 }
2108 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 }
2110 break;
2111 case 'j':
2112 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2113 {
2114 *arg += len;
2115 return &t_job;
2116 }
2117 break;
2118 case 'l':
2119 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2120 {
2121 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002122 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 }
2124 break;
2125 case 'n':
2126 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2127 {
2128 *arg += len;
2129 return &t_number;
2130 }
2131 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 case 's':
2133 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2134 {
2135 *arg += len;
2136 return &t_string;
2137 }
2138 break;
2139 case 'v':
2140 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2141 {
2142 *arg += len;
2143 return &t_void;
2144 }
2145 break;
2146 }
2147
2148 semsg(_("E1010: Type not recognized: %s"), *arg);
2149 return &t_any;
2150}
2151
2152/*
2153 * Check if "type1" and "type2" are exactly the same.
2154 */
2155 static int
2156equal_type(type_T *type1, type_T *type2)
2157{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002158 int i;
2159
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 if (type1->tt_type != type2->tt_type)
2161 return FALSE;
2162 switch (type1->tt_type)
2163 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002165 case VAR_ANY:
2166 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 case VAR_SPECIAL:
2168 case VAR_BOOL:
2169 case VAR_NUMBER:
2170 case VAR_FLOAT:
2171 case VAR_STRING:
2172 case VAR_BLOB:
2173 case VAR_JOB:
2174 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002175 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 case VAR_LIST:
2177 case VAR_DICT:
2178 return equal_type(type1->tt_member, type2->tt_member);
2179 case VAR_FUNC:
2180 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002181 if (!equal_type(type1->tt_member, type2->tt_member)
2182 || type1->tt_argcount != type2->tt_argcount)
2183 return FALSE;
2184 if (type1->tt_argcount < 0
2185 || type1->tt_args == NULL || type2->tt_args == NULL)
2186 return TRUE;
2187 for (i = 0; i < type1->tt_argcount; ++i)
2188 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2189 return FALSE;
2190 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 }
2192 return TRUE;
2193}
2194
2195/*
2196 * Find the common type of "type1" and "type2" and put it in "dest".
2197 * "type2" and "dest" may be the same.
2198 */
2199 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002200common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201{
2202 if (equal_type(type1, type2))
2203 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002204 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 return;
2206 }
2207
2208 if (type1->tt_type == type2->tt_type)
2209 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2211 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002212 type_T *common;
2213
Bram Moolenaard77a8522020-04-03 21:59:57 +02002214 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002215 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002216 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002217 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002218 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 return;
2220 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002221 if (type1->tt_type == VAR_FUNC)
2222 {
2223 type_T *common;
2224
2225 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2226 if (type1->tt_argcount == type2->tt_argcount
2227 && type1->tt_argcount >= 0)
2228 {
2229 int argcount = type1->tt_argcount;
2230 int i;
2231
2232 *dest = alloc_func_type(common, argcount, type_gap);
2233 if (type1->tt_args != NULL && type2->tt_args != NULL)
2234 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002235 if (func_type_add_arg_types(*dest, argcount,
2236 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002237 for (i = 0; i < argcount; ++i)
2238 common_type(type1->tt_args[i], type2->tt_args[i],
2239 &(*dest)->tt_args[i], type_gap);
2240 }
2241 }
2242 else
2243 *dest = alloc_func_type(common, -1, type_gap);
2244 return;
2245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 }
2247
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002248 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249}
2250
2251 char *
2252vartype_name(vartype_T type)
2253{
2254 switch (type)
2255 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002256 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002257 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 case VAR_SPECIAL: return "special";
2260 case VAR_BOOL: return "bool";
2261 case VAR_NUMBER: return "number";
2262 case VAR_FLOAT: return "float";
2263 case VAR_STRING: return "string";
2264 case VAR_BLOB: return "blob";
2265 case VAR_JOB: return "job";
2266 case VAR_CHANNEL: return "channel";
2267 case VAR_LIST: return "list";
2268 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002269
2270 case VAR_FUNC:
2271 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002273 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274}
2275
2276/*
2277 * Return the name of a type.
2278 * The result may be in allocated memory, in which case "tofree" is set.
2279 */
2280 char *
2281type_name(type_T *type, char **tofree)
2282{
2283 char *name = vartype_name(type->tt_type);
2284
2285 *tofree = NULL;
2286 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2287 {
2288 char *member_free;
2289 char *member_name = type_name(type->tt_member, &member_free);
2290 size_t len;
2291
2292 len = STRLEN(name) + STRLEN(member_name) + 3;
2293 *tofree = alloc(len);
2294 if (*tofree != NULL)
2295 {
2296 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2297 vim_free(member_free);
2298 return *tofree;
2299 }
2300 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002301 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002302 {
2303 garray_T ga;
2304 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002305 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002306
2307 ga_init2(&ga, 1, 100);
2308 if (ga_grow(&ga, 20) == FAIL)
2309 return "[unknown]";
2310 *tofree = ga.ga_data;
2311 STRCPY(ga.ga_data, "func(");
2312 ga.ga_len += 5;
2313
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002314 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002315 {
2316 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002317 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002318 int len;
2319
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002320 if (type->tt_args == NULL)
2321 arg_type = "[unknown]";
2322 else
2323 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002324 if (i > 0)
2325 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002326 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002327 ga.ga_len += 2;
2328 }
2329 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002330 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002331 {
2332 vim_free(arg_free);
2333 return "[unknown]";
2334 }
2335 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002336 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002337 {
2338 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2339 ga.ga_len += 3;
2340 }
2341 else if (i >= type->tt_min_argcount)
2342 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002343 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002344 ga.ga_len += len;
2345 vim_free(arg_free);
2346 }
2347
2348 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002349 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002350 else
2351 {
2352 char *ret_free;
2353 char *ret_name = type_name(type->tt_member, &ret_free);
2354 int len;
2355
2356 len = (int)STRLEN(ret_name) + 4;
2357 if (ga_grow(&ga, len) == FAIL)
2358 {
2359 vim_free(ret_free);
2360 return "[unknown]";
2361 }
2362 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002363 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2364 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002365 vim_free(ret_free);
2366 }
2367 return ga.ga_data;
2368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369
2370 return name;
2371}
2372
2373/*
2374 * Find "name" in script-local items of script "sid".
2375 * Returns the index in "sn_var_vals" if found.
2376 * If found but not in "sn_var_vals" returns -1.
2377 * If not found returns -2.
2378 */
2379 int
2380get_script_item_idx(int sid, char_u *name, int check_writable)
2381{
2382 hashtab_T *ht;
2383 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002384 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 int idx;
2386
2387 // First look the name up in the hashtable.
2388 if (sid <= 0 || sid > script_items.ga_len)
2389 return -1;
2390 ht = &SCRIPT_VARS(sid);
2391 di = find_var_in_ht(ht, 0, name, TRUE);
2392 if (di == NULL)
2393 return -2;
2394
2395 // Now find the svar_T index in sn_var_vals.
2396 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2397 {
2398 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2399
2400 if (sv->sv_tv == &di->di_tv)
2401 {
2402 if (check_writable && sv->sv_const)
2403 semsg(_(e_readonlyvar), name);
2404 return idx;
2405 }
2406 }
2407 return -1;
2408}
2409
2410/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002411 * Find "name" in imported items of the current script or in "cctx" if not
2412 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 */
2414 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002415find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002417 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 int idx;
2419
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002420 if (current_sctx.sc_sid <= 0)
2421 return NULL;
2422 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 if (cctx != NULL)
2424 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2425 {
2426 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2427 + idx;
2428
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002429 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2430 : STRLEN(import->imp_name) == len
2431 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 return import;
2433 }
2434
2435 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2436 {
2437 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2438
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002439 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2440 : STRLEN(import->imp_name) == len
2441 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 return import;
2443 }
2444 return NULL;
2445}
2446
2447/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002448 * Free all imported variables.
2449 */
2450 static void
2451free_imported(cctx_T *cctx)
2452{
2453 int idx;
2454
2455 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2456 {
2457 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2458
2459 vim_free(import->imp_name);
2460 }
2461 ga_clear(&cctx->ctx_imports);
2462}
2463
2464/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002465 * Return TRUE if "p" points at a "#" but not at "#{".
2466 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002467 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002468vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002469{
2470 return p[0] == '#' && p[1] != '{';
2471}
2472
2473/*
2474 * Return a pointer to the next line that isn't empty or only contains a
2475 * comment. Skips over white space.
2476 * Returns NULL if there is none.
2477 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002478 char_u *
2479peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002480{
2481 int lnum = cctx->ctx_lnum;
2482
2483 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2484 {
2485 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002486 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002487
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002488 if (line == NULL)
2489 break;
2490 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002491 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002492 return p;
2493 }
2494 return NULL;
2495}
2496
2497/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002498 * Called when checking for a following operator at "arg". When the rest of
2499 * the line is empty or only a comment, peek the next line. If there is a next
2500 * line return a pointer to it and set "nextp".
2501 * Otherwise skip over white space.
2502 */
2503 static char_u *
2504may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2505{
2506 char_u *p = skipwhite(arg);
2507
2508 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002509 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002510 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002511 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002512 if (*nextp != NULL)
2513 return *nextp;
2514 }
2515 return p;
2516}
2517
2518/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002519 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002520 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002521 * Returns NULL when at the end.
2522 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002523 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002524next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002525{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002526 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002527
2528 do
2529 {
2530 ++cctx->ctx_lnum;
2531 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002532 {
2533 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002534 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002535 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002536 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002537 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002538 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002539 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002540 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002541 return line;
2542}
2543
2544/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002545 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002546 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002547 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2548 */
2549 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002550may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002551{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002552 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002553 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002554 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002555
2556 if (next == NULL)
2557 return FAIL;
2558 *arg = skipwhite(next);
2559 }
2560 return OK;
2561}
2562
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002563/*
2564 * Idem, and give an error when failed.
2565 */
2566 static int
2567may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2568{
2569 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2570 {
2571 emsg(_("E1097: line incomplete"));
2572 return FAIL;
2573 }
2574 return OK;
2575}
2576
2577
Bram Moolenaara5565e42020-05-09 15:44:01 +02002578// Structure passed between the compile_expr* functions to keep track of
2579// constants that have been parsed but for which no code was produced yet. If
2580// possible expressions on these constants are applied at compile time. If
2581// that is not possible, the code to push the constants needs to be generated
2582// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002583// Using 50 should be more than enough of 5 levels of ().
2584#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002585typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002586 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002587 int pp_used; // active entries in pp_tv[]
2588} ppconst_T;
2589
Bram Moolenaar1c747212020-05-09 18:28:34 +02002590static int compile_expr0(char_u **arg, cctx_T *cctx);
2591static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2592
Bram Moolenaara5565e42020-05-09 15:44:01 +02002593/*
2594 * Generate a PUSH instruction for "tv".
2595 * "tv" will be consumed or cleared.
2596 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2597 */
2598 static int
2599generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2600{
2601 if (tv != NULL)
2602 {
2603 switch (tv->v_type)
2604 {
2605 case VAR_UNKNOWN:
2606 break;
2607 case VAR_BOOL:
2608 generate_PUSHBOOL(cctx, tv->vval.v_number);
2609 break;
2610 case VAR_SPECIAL:
2611 generate_PUSHSPEC(cctx, tv->vval.v_number);
2612 break;
2613 case VAR_NUMBER:
2614 generate_PUSHNR(cctx, tv->vval.v_number);
2615 break;
2616#ifdef FEAT_FLOAT
2617 case VAR_FLOAT:
2618 generate_PUSHF(cctx, tv->vval.v_float);
2619 break;
2620#endif
2621 case VAR_BLOB:
2622 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2623 tv->vval.v_blob = NULL;
2624 break;
2625 case VAR_STRING:
2626 generate_PUSHS(cctx, tv->vval.v_string);
2627 tv->vval.v_string = NULL;
2628 break;
2629 default:
2630 iemsg("constant type not supported");
2631 clear_tv(tv);
2632 return FAIL;
2633 }
2634 tv->v_type = VAR_UNKNOWN;
2635 }
2636 return OK;
2637}
2638
2639/*
2640 * Generate code for any ppconst entries.
2641 */
2642 static int
2643generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2644{
2645 int i;
2646 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002647 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002648
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002649 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002650 for (i = 0; i < ppconst->pp_used; ++i)
2651 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2652 ret = FAIL;
2653 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002654 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002655 return ret;
2656}
2657
2658/*
2659 * Clear ppconst constants. Used when failing.
2660 */
2661 static void
2662clear_ppconst(ppconst_T *ppconst)
2663{
2664 int i;
2665
2666 for (i = 0; i < ppconst->pp_used; ++i)
2667 clear_tv(&ppconst->pp_tv[i]);
2668 ppconst->pp_used = 0;
2669}
2670
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002671/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002672 * Generate an instruction to load script-local variable "name", without the
2673 * leading "s:".
2674 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 */
2676 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002677compile_load_scriptvar(
2678 cctx_T *cctx,
2679 char_u *name, // variable NUL terminated
2680 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002681 char_u **end, // end of variable
2682 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002684 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2686 imported_T *import;
2687
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002688 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002690 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002691 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2692 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 }
2694 if (idx >= 0)
2695 {
2696 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2697
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002698 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 current_sctx.sc_sid, idx, sv->sv_type);
2700 return OK;
2701 }
2702
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002703 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 if (import != NULL)
2705 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002706 if (import->imp_all)
2707 {
2708 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002709 char_u *exp_name;
2710 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002711 ufunc_T *ufunc;
2712 type_T *type;
2713
2714 // Used "import * as Name", need to lookup the member.
2715 if (*p != '.')
2716 {
2717 semsg(_("E1060: expected dot after name: %s"), start);
2718 return FAIL;
2719 }
2720 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002721 if (VIM_ISWHITE(*p))
2722 {
2723 emsg(_("E1074: no white space allowed after dot"));
2724 return FAIL;
2725 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002726
Bram Moolenaar1c991142020-07-04 13:15:31 +02002727 // isolate one name
2728 exp_name = p;
2729 while (eval_isnamec(*p))
2730 ++p;
2731 cc = *p;
2732 *p = NUL;
2733
2734 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2735 *p = cc;
2736 p = skipwhite(p);
2737
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002738 // TODO: what if it is a function?
2739 if (idx < 0)
2740 return FAIL;
2741 *end = p;
2742
2743 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2744 import->imp_sid,
2745 idx,
2746 type);
2747 }
2748 else
2749 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002750 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002751 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2752 import->imp_sid,
2753 import->imp_var_vals_idx,
2754 import->imp_type);
2755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 return OK;
2757 }
2758
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002759 if (error)
2760 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 return FAIL;
2762}
2763
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002764 static int
2765generate_funcref(cctx_T *cctx, char_u *name)
2766{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002767 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002768
2769 if (ufunc == NULL)
2770 return FAIL;
2771
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002772 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2773 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002774}
2775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776/*
2777 * Compile a variable name into a load instruction.
2778 * "end" points to just after the name.
2779 * When "error" is FALSE do not give an error when not found.
2780 */
2781 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002782compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783{
2784 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002785 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002786 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002788 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789
2790 if (*(*arg + 1) == ':')
2791 {
2792 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002793 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002794 {
2795 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002797 switch (**arg)
2798 {
2799 case 'g': isn_type = ISN_LOADGDICT; break;
2800 case 'w': isn_type = ISN_LOADWDICT; break;
2801 case 't': isn_type = ISN_LOADTDICT; break;
2802 case 'b': isn_type = ISN_LOADBDICT; break;
2803 default:
2804 semsg(_(e_namespace), *arg);
2805 goto theend;
2806 }
2807 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2808 goto theend;
2809 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 else
2812 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002813 isntype_T isn_type = ISN_DROP;
2814
2815 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2816 if (name == NULL)
2817 return FAIL;
2818
2819 switch (**arg)
2820 {
2821 case 'v': res = generate_LOADV(cctx, name, error);
2822 break;
2823 case 's': res = compile_load_scriptvar(cctx, name,
2824 NULL, NULL, error);
2825 break;
2826 case 'g': isn_type = ISN_LOADG; break;
2827 case 'w': isn_type = ISN_LOADW; break;
2828 case 't': isn_type = ISN_LOADT; break;
2829 case 'b': isn_type = ISN_LOADB; break;
2830 default: semsg(_(e_namespace), *arg);
2831 goto theend;
2832 }
2833 if (isn_type != ISN_DROP)
2834 {
2835 // Global, Buffer-local, Window-local and Tabpage-local
2836 // variables can be defined later, thus we don't check if it
2837 // exists, give error at runtime.
2838 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 }
2841 }
2842 else
2843 {
2844 size_t len = end - *arg;
2845 int idx;
2846 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002847 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848
2849 name = vim_strnsave(*arg, end - *arg);
2850 if (name == NULL)
2851 return FAIL;
2852
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002853 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002855 if (!gen_load_outer)
2856 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 }
2858 else
2859 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002860 lvar_T *lvar = lookup_local(*arg, len, cctx);
2861
2862 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002864 type = lvar->lv_type;
2865 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002866 if (lvar->lv_from_outer)
2867 gen_load_outer = TRUE;
2868 else
2869 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 }
2871 else
2872 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002873 // "var" can be script-local even without using "s:" if it
2874 // already exists.
2875 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2876 == SCRIPT_VERSION_VIM9
2877 || lookup_script(*arg, len) == OK)
2878 res = compile_load_scriptvar(cctx, name, *arg, &end,
2879 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002880
Bram Moolenaara5565e42020-05-09 15:44:01 +02002881 // When the name starts with an uppercase letter or "x:" it
2882 // can be a user defined function.
2883 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2884 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 }
2886 }
2887 if (gen_load)
2888 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002889 if (gen_load_outer)
2890 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 }
2892
2893 *arg = end;
2894
2895theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002896 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 semsg(_(e_var_notfound), name);
2898 vim_free(name);
2899 return res;
2900}
2901
2902/*
2903 * Compile the argument expressions.
2904 * "arg" points to just after the "(" and is advanced to after the ")"
2905 */
2906 static int
2907compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2908{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002909 char_u *p = *arg;
2910 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911
Bram Moolenaare6085c52020-04-12 20:19:16 +02002912 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002914 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2915 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002916 if (*p == ')')
2917 {
2918 *arg = p + 1;
2919 return OK;
2920 }
2921
Bram Moolenaara5565e42020-05-09 15:44:01 +02002922 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 return FAIL;
2924 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002925
2926 if (*p != ',' && *skipwhite(p) == ',')
2927 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002928 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002929 p = skipwhite(p);
2930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002932 {
2933 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002934 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002935 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002936 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002937 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002938 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002940failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002941 emsg(_(e_missing_close));
2942 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943}
2944
2945/*
2946 * Compile a function call: name(arg1, arg2)
2947 * "arg" points to "name", "arg + varlen" to the "(".
2948 * "argcount_init" is 1 for "value->method()"
2949 * Instructions:
2950 * EVAL arg1
2951 * EVAL arg2
2952 * BCALL / DCALL / UCALL
2953 */
2954 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002955compile_call(
2956 char_u **arg,
2957 size_t varlen,
2958 cctx_T *cctx,
2959 ppconst_T *ppconst,
2960 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961{
2962 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002963 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 int argcount = argcount_init;
2965 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002966 char_u fname_buf[FLEN_FIXED + 1];
2967 char_u *tofree = NULL;
2968 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002970 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971
Bram Moolenaara5565e42020-05-09 15:44:01 +02002972 // we can evaluate "has('name')" at compile time
2973 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2974 {
2975 char_u *s = skipwhite(*arg + varlen + 1);
2976 typval_T argvars[2];
2977
2978 argvars[0].v_type = VAR_UNKNOWN;
2979 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002980 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002981 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002982 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002983 s = skipwhite(s);
2984 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2985 {
2986 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2987
2988 *arg = s + 1;
2989 argvars[1].v_type = VAR_UNKNOWN;
2990 tv->v_type = VAR_NUMBER;
2991 tv->vval.v_number = 0;
2992 f_has(argvars, tv);
2993 clear_tv(&argvars[0]);
2994 ++ppconst->pp_used;
2995 return OK;
2996 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002997 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002998 }
2999
3000 if (generate_ppconst(cctx, ppconst) == FAIL)
3001 return FAIL;
3002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 if (varlen >= sizeof(namebuf))
3004 {
3005 semsg(_("E1011: name too long: %s"), name);
3006 return FAIL;
3007 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003008 vim_strncpy(namebuf, *arg, varlen);
3009 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010
3011 *arg = skipwhite(*arg + varlen + 1);
3012 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003013 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003015 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 {
3017 int idx;
3018
3019 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003020 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003022 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003023 else
3024 semsg(_(e_unknownfunc), namebuf);
3025 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 }
3027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003029 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003031 {
3032 res = generate_CALL(cctx, ufunc, argcount);
3033 goto theend;
3034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035
3036 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003037 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003039 if (STRNCMP(namebuf, "g:", 2) != 0
3040 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003041 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003042 garray_T *stack = &cctx->ctx_type_stack;
3043 type_T *type;
3044
3045 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3046 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003047 goto theend;
3048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003050 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003051 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003052 if (STRNCMP(namebuf, "g:", 2) == 0)
3053 res = generate_UCALL(cctx, name, argcount);
3054 else
3055 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003056
3057theend:
3058 vim_free(tofree);
3059 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060}
3061
3062// like NAMESPACE_CHAR but with 'a' and 'l'.
3063#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3064
3065/*
3066 * Find the end of a variable or function name. Unlike find_name_end() this
3067 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003068 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 * Return a pointer to just after the name. Equal to "arg" if there is no
3070 * valid name.
3071 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003072 static char_u *
3073to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074{
3075 char_u *p;
3076
3077 // Quick check for valid starting character.
3078 if (!eval_isnamec1(*arg))
3079 return arg;
3080
3081 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3082 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3083 // and can be used in slice "[n:]".
3084 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003085 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3087 break;
3088 return p;
3089}
3090
3091/*
3092 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003093 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 */
3095 char_u *
3096to_name_const_end(char_u *arg)
3097{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003098 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 typval_T rettv;
3100
3101 if (p == arg && *arg == '[')
3102 {
3103
3104 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003105 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 p = arg;
3107 }
3108 else if (p == arg && *arg == '#' && arg[1] == '{')
3109 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003110 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003112 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 p = arg;
3114 }
3115 else if (p == arg && *arg == '{')
3116 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003117 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003119 // Can be "{x -> ret}()".
3120 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003122 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 if (ret != OK)
3124 p = arg;
3125 }
3126
3127 return p;
3128}
3129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130/*
3131 * parse a list: [expr, expr]
3132 * "*arg" points to the '['.
3133 */
3134 static int
3135compile_list(char_u **arg, cctx_T *cctx)
3136{
3137 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003138 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 int count = 0;
3140
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003141 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003143 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003144 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003145 semsg(_(e_list_end), *arg);
3146 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003147 }
3148 if (*p == ']')
3149 {
3150 ++p;
3151 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003152 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003153 p += STRLEN(p);
3154 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003155 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003156 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 break;
3158 ++count;
3159 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003160 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003162 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3163 {
3164 semsg(_(e_white_after), ",");
3165 return FAIL;
3166 }
3167 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003168 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 p = skipwhite(p);
3170 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003171 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172
3173 generate_NEWLIST(cctx, count);
3174 return OK;
3175}
3176
3177/*
3178 * parse a lambda: {arg, arg -> expr}
3179 * "*arg" points to the '{'.
3180 */
3181 static int
3182compile_lambda(char_u **arg, cctx_T *cctx)
3183{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 typval_T rettv;
3185 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003186 evalarg_T evalarg;
3187
3188 CLEAR_FIELD(evalarg);
3189 evalarg.eval_flags = EVAL_EVALUATE;
3190 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191
3192 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003193 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003197 ++ufunc->uf_refcount;
3198 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003199 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200
3201 // The function will have one line: "return {expr}".
3202 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003203 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003205 clear_evalarg(&evalarg, NULL);
3206
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003207 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003208 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003209
3210 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 return FAIL;
3212}
3213
3214/*
3215 * Compile a lamda call: expr->{lambda}(args)
3216 * "arg" points to the "{".
3217 */
3218 static int
3219compile_lambda_call(char_u **arg, cctx_T *cctx)
3220{
3221 ufunc_T *ufunc;
3222 typval_T rettv;
3223 int argcount = 1;
3224 int ret = FAIL;
3225
3226 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003227 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 return FAIL;
3229
3230 if (**arg != '(')
3231 {
3232 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003233 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 else
3235 semsg(_(e_missing_paren), "lambda");
3236 clear_tv(&rettv);
3237 return FAIL;
3238 }
3239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 ufunc = rettv.vval.v_partial->pt_func;
3241 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003242 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003243 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003244
3245 // The function will have one line: "return {expr}".
3246 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003247 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248
3249 // compile the arguments
3250 *arg = skipwhite(*arg + 1);
3251 if (compile_arguments(arg, cctx, &argcount) == OK)
3252 // call the compiled function
3253 ret = generate_CALL(cctx, ufunc, argcount);
3254
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003255 if (ret == FAIL)
3256 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 return ret;
3258}
3259
3260/*
3261 * parse a dict: {'key': val} or #{key: val}
3262 * "*arg" points to the '{'.
3263 */
3264 static int
3265compile_dict(char_u **arg, cctx_T *cctx, int literal)
3266{
3267 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003268 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 int count = 0;
3270 dict_T *d = dict_alloc();
3271 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003272 char_u *whitep = *arg;
3273 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274
3275 if (d == NULL)
3276 return FAIL;
3277 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003278 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 {
3280 char_u *key = NULL;
3281
Bram Moolenaar23c55272020-06-21 16:58:13 +02003282 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003283 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003284 *arg = NULL;
3285 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003286 }
3287
3288 if (**arg == '}')
3289 break;
3290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 if (literal)
3292 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003293 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294
Bram Moolenaar2c330432020-04-13 14:41:35 +02003295 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 {
3297 semsg(_("E1014: Invalid key: %s"), *arg);
3298 return FAIL;
3299 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003300 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 if (generate_PUSHS(cctx, key) == FAIL)
3302 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003303 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 }
3305 else
3306 {
3307 isn_T *isn;
3308
Bram Moolenaara5565e42020-05-09 15:44:01 +02003309 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3312 if (isn->isn_type == ISN_PUSHS)
3313 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003314 else
3315 {
3316 type_T *keytype = ((type_T **)stack->ga_data)
3317 [stack->ga_len - 1];
3318 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3319 return FAIL;
3320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 }
3322
3323 // Check for duplicate keys, if using string keys.
3324 if (key != NULL)
3325 {
3326 item = dict_find(d, key, -1);
3327 if (item != NULL)
3328 {
3329 semsg(_(e_duplicate_key), key);
3330 goto failret;
3331 }
3332 item = dictitem_alloc(key);
3333 if (item != NULL)
3334 {
3335 item->di_tv.v_type = VAR_UNKNOWN;
3336 item->di_tv.v_lock = 0;
3337 if (dict_add(d, item) == FAIL)
3338 dictitem_free(item);
3339 }
3340 }
3341
3342 *arg = skipwhite(*arg);
3343 if (**arg != ':')
3344 {
3345 semsg(_(e_missing_dict_colon), *arg);
3346 return FAIL;
3347 }
3348
Bram Moolenaar2c330432020-04-13 14:41:35 +02003349 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003351 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003352 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003353 *arg = NULL;
3354 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003355 }
3356
Bram Moolenaara5565e42020-05-09 15:44:01 +02003357 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 return FAIL;
3359 ++count;
3360
Bram Moolenaar2c330432020-04-13 14:41:35 +02003361 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003362 *arg = skipwhite(*arg);
3363 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003364 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003365 *arg = NULL;
3366 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 if (**arg == '}')
3369 break;
3370 if (**arg != ',')
3371 {
3372 semsg(_(e_missing_dict_comma), *arg);
3373 goto failret;
3374 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003375 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 *arg = skipwhite(*arg + 1);
3377 }
3378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 *arg = *arg + 1;
3380
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003381 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003382 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003383 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003384 *arg += STRLEN(*arg);
3385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 dict_unref(d);
3387 return generate_NEWDICT(cctx, count);
3388
3389failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003390 if (*arg == NULL)
3391 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 dict_unref(d);
3393 return FAIL;
3394}
3395
3396/*
3397 * Compile "&option".
3398 */
3399 static int
3400compile_get_option(char_u **arg, cctx_T *cctx)
3401{
3402 typval_T rettv;
3403 char_u *start = *arg;
3404 int ret;
3405
3406 // parse the option and get the current value to get the type.
3407 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003408 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 if (ret == OK)
3410 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003411 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 char_u *name = vim_strnsave(start, *arg - start);
3413 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3414
3415 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3416 vim_free(name);
3417 }
3418 clear_tv(&rettv);
3419
3420 return ret;
3421}
3422
3423/*
3424 * Compile "$VAR".
3425 */
3426 static int
3427compile_get_env(char_u **arg, cctx_T *cctx)
3428{
3429 char_u *start = *arg;
3430 int len;
3431 int ret;
3432 char_u *name;
3433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 ++*arg;
3435 len = get_env_len(arg);
3436 if (len == 0)
3437 {
3438 semsg(_(e_syntax_at), start - 1);
3439 return FAIL;
3440 }
3441
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003442 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 name = vim_strnsave(start, len + 1);
3444 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3445 vim_free(name);
3446 return ret;
3447}
3448
3449/*
3450 * Compile "@r".
3451 */
3452 static int
3453compile_get_register(char_u **arg, cctx_T *cctx)
3454{
3455 int ret;
3456
3457 ++*arg;
3458 if (**arg == NUL)
3459 {
3460 semsg(_(e_syntax_at), *arg - 1);
3461 return FAIL;
3462 }
3463 if (!valid_yank_reg(**arg, TRUE))
3464 {
3465 emsg_invreg(**arg);
3466 return FAIL;
3467 }
3468 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3469 ++*arg;
3470 return ret;
3471}
3472
3473/*
3474 * Apply leading '!', '-' and '+' to constant "rettv".
3475 */
3476 static int
3477apply_leader(typval_T *rettv, char_u *start, char_u *end)
3478{
3479 char_u *p = end;
3480
3481 // this works from end to start
3482 while (p > start)
3483 {
3484 --p;
3485 if (*p == '-' || *p == '+')
3486 {
3487 // only '-' has an effect, for '+' we only check the type
3488#ifdef FEAT_FLOAT
3489 if (rettv->v_type == VAR_FLOAT)
3490 {
3491 if (*p == '-')
3492 rettv->vval.v_float = -rettv->vval.v_float;
3493 }
3494 else
3495#endif
3496 {
3497 varnumber_T val;
3498 int error = FALSE;
3499
3500 // tv_get_number_chk() accepts a string, but we don't want that
3501 // here
3502 if (check_not_string(rettv) == FAIL)
3503 return FAIL;
3504 val = tv_get_number_chk(rettv, &error);
3505 clear_tv(rettv);
3506 if (error)
3507 return FAIL;
3508 if (*p == '-')
3509 val = -val;
3510 rettv->v_type = VAR_NUMBER;
3511 rettv->vval.v_number = val;
3512 }
3513 }
3514 else
3515 {
3516 int v = tv2bool(rettv);
3517
3518 // '!' is permissive in the type.
3519 clear_tv(rettv);
3520 rettv->v_type = VAR_BOOL;
3521 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3522 }
3523 }
3524 return OK;
3525}
3526
3527/*
3528 * Recognize v: variables that are constants and set "rettv".
3529 */
3530 static void
3531get_vim_constant(char_u **arg, typval_T *rettv)
3532{
3533 if (STRNCMP(*arg, "v:true", 6) == 0)
3534 {
3535 rettv->v_type = VAR_BOOL;
3536 rettv->vval.v_number = VVAL_TRUE;
3537 *arg += 6;
3538 }
3539 else if (STRNCMP(*arg, "v:false", 7) == 0)
3540 {
3541 rettv->v_type = VAR_BOOL;
3542 rettv->vval.v_number = VVAL_FALSE;
3543 *arg += 7;
3544 }
3545 else if (STRNCMP(*arg, "v:null", 6) == 0)
3546 {
3547 rettv->v_type = VAR_SPECIAL;
3548 rettv->vval.v_number = VVAL_NULL;
3549 *arg += 6;
3550 }
3551 else if (STRNCMP(*arg, "v:none", 6) == 0)
3552 {
3553 rettv->v_type = VAR_SPECIAL;
3554 rettv->vval.v_number = VVAL_NONE;
3555 *arg += 6;
3556 }
3557}
3558
Bram Moolenaar61a89812020-05-07 16:58:17 +02003559 static exptype_T
3560get_compare_type(char_u *p, int *len, int *type_is)
3561{
3562 exptype_T type = EXPR_UNKNOWN;
3563 int i;
3564
3565 switch (p[0])
3566 {
3567 case '=': if (p[1] == '=')
3568 type = EXPR_EQUAL;
3569 else if (p[1] == '~')
3570 type = EXPR_MATCH;
3571 break;
3572 case '!': if (p[1] == '=')
3573 type = EXPR_NEQUAL;
3574 else if (p[1] == '~')
3575 type = EXPR_NOMATCH;
3576 break;
3577 case '>': if (p[1] != '=')
3578 {
3579 type = EXPR_GREATER;
3580 *len = 1;
3581 }
3582 else
3583 type = EXPR_GEQUAL;
3584 break;
3585 case '<': if (p[1] != '=')
3586 {
3587 type = EXPR_SMALLER;
3588 *len = 1;
3589 }
3590 else
3591 type = EXPR_SEQUAL;
3592 break;
3593 case 'i': if (p[1] == 's')
3594 {
3595 // "is" and "isnot"; but not a prefix of a name
3596 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3597 *len = 5;
3598 i = p[*len];
3599 if (!isalnum(i) && i != '_')
3600 {
3601 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3602 *type_is = TRUE;
3603 }
3604 }
3605 break;
3606 }
3607 return type;
3608}
3609
3610/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 * Compile code to apply '-', '+' and '!'.
3612 */
3613 static int
3614compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3615{
3616 char_u *p = end;
3617
3618 // this works from end to start
3619 while (p > start)
3620 {
3621 --p;
3622 if (*p == '-' || *p == '+')
3623 {
3624 int negate = *p == '-';
3625 isn_T *isn;
3626
3627 // TODO: check type
3628 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3629 {
3630 --p;
3631 if (*p == '-')
3632 negate = !negate;
3633 }
3634 // only '-' has an effect, for '+' we only check the type
3635 if (negate)
3636 isn = generate_instr(cctx, ISN_NEGATENR);
3637 else
3638 isn = generate_instr(cctx, ISN_CHECKNR);
3639 if (isn == NULL)
3640 return FAIL;
3641 }
3642 else
3643 {
3644 int invert = TRUE;
3645
3646 while (p > start && p[-1] == '!')
3647 {
3648 --p;
3649 invert = !invert;
3650 }
3651 if (generate_2BOOL(cctx, invert) == FAIL)
3652 return FAIL;
3653 }
3654 }
3655 return OK;
3656}
3657
3658/*
3659 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003660 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 */
3662 static int
3663compile_subscript(
3664 char_u **arg,
3665 cctx_T *cctx,
3666 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003667 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003668 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669{
3670 for (;;)
3671 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003672 char_u *p = skipwhite(*arg);
3673
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003674 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003675 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003676 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003677
3678 // If a following line starts with "->{" or "->X" advance to that
3679 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003680 // Also if a following line starts with ".x".
3681 if (next != NULL &&
3682 ((next[0] == '-' && next[1] == '>'
3683 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3684 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003685 {
3686 next = next_line_from_context(cctx, TRUE);
3687 if (next == NULL)
3688 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003689 *arg = next;
3690 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003691 }
3692 }
3693
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003694 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003696 garray_T *stack = &cctx->ctx_type_stack;
3697 type_T *type;
3698 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003700 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003701 return FAIL;
3702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003704 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3705
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003706 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3708 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003709 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 return FAIL;
3711 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003712 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003714 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003715 return FAIL;
3716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 // something->method()
3718 // Apply the '!', '-' and '+' first:
3719 // -1.0->func() works like (-1.0)->func()
3720 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3721 return FAIL;
3722 *start_leader = end_leader; // don't apply again later
3723
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003724 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003725 *arg = skipwhite(p);
3726 if (may_get_next_line(p, arg, cctx) == FAIL)
3727 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 if (**arg == '{')
3729 {
3730 // lambda call: list->{lambda}
3731 if (compile_lambda_call(arg, cctx) == FAIL)
3732 return FAIL;
3733 }
3734 else
3735 {
3736 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003737 p = *arg;
3738 if (ASCII_ISALPHA(*p) && p[1] == ':')
3739 p += 2;
3740 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 ;
3742 if (*p != '(')
3743 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003744 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 return FAIL;
3746 }
3747 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003748 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 return FAIL;
3750 }
3751 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003752 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003754 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003755 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003756 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003757
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003758 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003759 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003760 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003761 // TODO: blob index
3762 // TODO: more arguments
3763 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003764 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003765 return FAIL;
3766
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003767 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003768 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003769 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003770 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003771 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 return FAIL;
3773
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003774 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3775 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 if (**arg != ']')
3777 {
3778 emsg(_(e_missbrac));
3779 return FAIL;
3780 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003781 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003783 // We can index a list and a dict. If we don't know the type
3784 // we can use the index value type.
3785 // TODO: If we don't know use an instruction to figure it out at
3786 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003787 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003788 vtype = (*typep)->tt_type;
3789 if (*typep == &t_any)
3790 {
3791 type_T *valtype = ((type_T **)stack->ga_data)
3792 [stack->ga_len - 1];
3793 if (valtype == &t_string)
3794 vtype = VAR_DICT;
3795 }
3796 if (vtype == VAR_DICT)
3797 {
3798 if ((*typep)->tt_type == VAR_DICT)
3799 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003800 else
3801 {
3802 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3803 return FAIL;
3804 *typep = &t_any;
3805 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003806 if (may_generate_2STRING(-1, cctx) == FAIL)
3807 return FAIL;
3808 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3809 return FAIL;
3810 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003811 else if (vtype == VAR_STRING)
3812 {
3813 *typep = &t_number;
3814 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3815 return FAIL;
3816 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003817 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003818 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003819 if ((*typep)->tt_type == VAR_LIST)
3820 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003821 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003822 return FAIL;
3823 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003824 else
3825 {
3826 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003827 return FAIL;
3828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003830 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003832 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003833 return FAIL;
3834
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003835 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003836 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3837 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003839 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 if (eval_isnamec1(*p))
3841 while (eval_isnamec(*p))
3842 MB_PTR_ADV(p);
3843 if (p == *arg)
3844 {
3845 semsg(_(e_syntax_at), *arg);
3846 return FAIL;
3847 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003848 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 return FAIL;
3850 *arg = p;
3851 }
3852 else
3853 break;
3854 }
3855
3856 // TODO - see handle_subscript():
3857 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3858 // Don't do this when "Func" is already a partial that was bound
3859 // explicitly (pt_auto is FALSE).
3860
3861 return OK;
3862}
3863
3864/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003865 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3866 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003868 * If the value is a constant "ppconst->pp_ret" will be set.
3869 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003870 *
3871 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 */
3873
3874/*
3875 * number number constant
3876 * 0zFFFFFFFF Blob constant
3877 * "string" string constant
3878 * 'string' literal string constant
3879 * &option-name option value
3880 * @r register contents
3881 * identifier variable value
3882 * function() function call
3883 * $VAR environment variable
3884 * (expression) nested expression
3885 * [expr, expr] List
3886 * {key: val, key: val} Dictionary
3887 * #{key: val, key: val} Dictionary with literal keys
3888 *
3889 * Also handle:
3890 * ! in front logical NOT
3891 * - in front unary minus
3892 * + in front unary plus (ignored)
3893 * trailing (arg) funcref/partial call
3894 * trailing [] subscript in String or List
3895 * trailing .name entry in Dictionary
3896 * trailing ->name() method call
3897 */
3898 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003899compile_expr7(
3900 char_u **arg,
3901 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003902 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 char_u *start_leader, *end_leader;
3905 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003906 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003907 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908
3909 /*
3910 * Skip '!', '-' and '+' characters. They are handled later.
3911 */
3912 start_leader = *arg;
3913 while (**arg == '!' || **arg == '-' || **arg == '+')
3914 *arg = skipwhite(*arg + 1);
3915 end_leader = *arg;
3916
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003917 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 switch (**arg)
3919 {
3920 /*
3921 * Number constant.
3922 */
3923 case '0': // also for blob starting with 0z
3924 case '1':
3925 case '2':
3926 case '3':
3927 case '4':
3928 case '5':
3929 case '6':
3930 case '7':
3931 case '8':
3932 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003933 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 return FAIL;
3935 break;
3936
3937 /*
3938 * String constant: "string".
3939 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003940 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 return FAIL;
3942 break;
3943
3944 /*
3945 * Literal string constant: 'str''ing'.
3946 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003947 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 return FAIL;
3949 break;
3950
3951 /*
3952 * Constant Vim variable.
3953 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003954 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 ret = NOTDONE;
3956 break;
3957
3958 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003959 * "true" constant
3960 */
3961 case 't': if (STRNCMP(*arg, "true", 4) == 0
3962 && !eval_isnamec((*arg)[4]))
3963 {
3964 *arg += 4;
3965 rettv->v_type = VAR_BOOL;
3966 rettv->vval.v_number = VVAL_TRUE;
3967 }
3968 else
3969 ret = NOTDONE;
3970 break;
3971
3972 /*
3973 * "false" constant
3974 */
3975 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3976 && !eval_isnamec((*arg)[5]))
3977 {
3978 *arg += 5;
3979 rettv->v_type = VAR_BOOL;
3980 rettv->vval.v_number = VVAL_FALSE;
3981 }
3982 else
3983 ret = NOTDONE;
3984 break;
3985
3986 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 * List: [expr, expr]
3988 */
3989 case '[': ret = compile_list(arg, cctx);
3990 break;
3991
3992 /*
3993 * Dictionary: #{key: val, key: val}
3994 */
3995 case '#': if ((*arg)[1] == '{')
3996 {
3997 ++*arg;
3998 ret = compile_dict(arg, cctx, TRUE);
3999 }
4000 else
4001 ret = NOTDONE;
4002 break;
4003
4004 /*
4005 * Lambda: {arg, arg -> expr}
4006 * Dictionary: {'key': val, 'key': val}
4007 */
4008 case '{': {
4009 char_u *start = skipwhite(*arg + 1);
4010
4011 // Find out what comes after the arguments.
4012 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004013 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 if (ret != FAIL && *start == '>')
4015 ret = compile_lambda(arg, cctx);
4016 else
4017 ret = compile_dict(arg, cctx, FALSE);
4018 }
4019 break;
4020
4021 /*
4022 * Option value: &name
4023 */
4024 case '&': ret = compile_get_option(arg, cctx);
4025 break;
4026
4027 /*
4028 * Environment variable: $VAR.
4029 */
4030 case '$': ret = compile_get_env(arg, cctx);
4031 break;
4032
4033 /*
4034 * Register contents: @r.
4035 */
4036 case '@': ret = compile_get_register(arg, cctx);
4037 break;
4038 /*
4039 * nested expression: (expression).
4040 */
4041 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004042
4043 // recursive!
4044 if (ppconst->pp_used <= PPSIZE - 10)
4045 {
4046 ret = compile_expr1(arg, cctx, ppconst);
4047 }
4048 else
4049 {
4050 // Not enough space in ppconst, flush constants.
4051 if (generate_ppconst(cctx, ppconst) == FAIL)
4052 return FAIL;
4053 ret = compile_expr0(arg, cctx);
4054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 *arg = skipwhite(*arg);
4056 if (**arg == ')')
4057 ++*arg;
4058 else if (ret == OK)
4059 {
4060 emsg(_(e_missing_close));
4061 ret = FAIL;
4062 }
4063 break;
4064
4065 default: ret = NOTDONE;
4066 break;
4067 }
4068 if (ret == FAIL)
4069 return FAIL;
4070
Bram Moolenaar1c747212020-05-09 18:28:34 +02004071 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 {
4073 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004074 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004076 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 return FAIL;
4078 }
4079 start_leader = end_leader; // don't apply again below
4080
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004081 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004082 clear_tv(rettv);
4083 else
4084 // A constant expression can possibly be handled compile time,
4085 // return the value instead of generating code.
4086 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 }
4088 else if (ret == NOTDONE)
4089 {
4090 char_u *p;
4091 int r;
4092
4093 if (!eval_isnamec1(**arg))
4094 {
4095 semsg(_("E1015: Name expected: %s"), *arg);
4096 return FAIL;
4097 }
4098
4099 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004100 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004102 {
4103 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004106 {
4107 if (generate_ppconst(cctx, ppconst) == FAIL)
4108 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 if (r == FAIL)
4112 return FAIL;
4113 }
4114
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004115 // Handle following "[]", ".member", etc.
4116 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004117 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004118 ppconst) == FAIL)
4119 return FAIL;
4120 if (ppconst->pp_used > 0)
4121 {
4122 // apply the '!', '-' and '+' before the constant
4123 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4124 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4125 return FAIL;
4126 return OK;
4127 }
4128 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004130 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131}
4132
4133/*
4134 * * number multiplication
4135 * / number division
4136 * % number modulo
4137 */
4138 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004139compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140{
4141 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004142 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004143 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004145 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004146 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 return FAIL;
4148
4149 /*
4150 * Repeat computing, until no "*", "/" or "%" is following.
4151 */
4152 for (;;)
4153 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004154 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 if (*op != '*' && *op != '/' && *op != '%')
4156 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004157 if (next != NULL)
4158 {
4159 *arg = next_line_from_context(cctx, TRUE);
4160 op = skipwhite(*arg);
4161 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004162
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004163 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 {
4165 char_u buf[3];
4166
4167 vim_strncpy(buf, op, 1);
4168 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004169 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 }
4171 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004172 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004173 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004175 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004176 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004178
4179 if (ppconst->pp_used == ppconst_used + 2
4180 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4181 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004182 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004183 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4184 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004185 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004187 // both are numbers: compute the result
4188 switch (*op)
4189 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004190 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004191 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004192 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004193 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004194 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004195 break;
4196 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004197 tv1->vval.v_number = res;
4198 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004199 }
4200 else
4201 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004202 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004203 generate_two_op(cctx, op);
4204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 }
4206
4207 return OK;
4208}
4209
4210/*
4211 * + number addition
4212 * - number subtraction
4213 * .. string concatenation
4214 */
4215 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004216compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217{
4218 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004219 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004221 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222
4223 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004224 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 return FAIL;
4226
4227 /*
4228 * Repeat computing, until no "+", "-" or ".." is following.
4229 */
4230 for (;;)
4231 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004232 op = may_peek_next_line(cctx, *arg, &next);
4233 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 break;
4235 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004236 if (next != NULL)
4237 {
4238 *arg = next_line_from_context(cctx, TRUE);
4239 op = skipwhite(*arg);
4240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004242 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 {
4244 char_u buf[3];
4245
4246 vim_strncpy(buf, op, oplen);
4247 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004248 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 }
4250
4251 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004252 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004253 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004255 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004256 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 return FAIL;
4258
Bram Moolenaara5565e42020-05-09 15:44:01 +02004259 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004260 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004261 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4262 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4263 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4264 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004266 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4267 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004268
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004269 // concat/subtract/add constant numbers
4270 if (*op == '+')
4271 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4272 else if (*op == '-')
4273 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4274 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004275 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004276 // concatenate constant strings
4277 char_u *s1 = tv1->vval.v_string;
4278 char_u *s2 = tv2->vval.v_string;
4279 size_t len1 = STRLEN(s1);
4280
4281 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4282 if (tv1->vval.v_string == NULL)
4283 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004284 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004285 return FAIL;
4286 }
4287 mch_memmove(tv1->vval.v_string, s1, len1);
4288 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004289 vim_free(s1);
4290 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004291 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004292 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 }
4294 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004295 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004296 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004297 if (*op == '.')
4298 {
4299 if (may_generate_2STRING(-2, cctx) == FAIL
4300 || may_generate_2STRING(-1, cctx) == FAIL)
4301 return FAIL;
4302 generate_instr_drop(cctx, ISN_CONCAT, 1);
4303 }
4304 else
4305 generate_two_op(cctx, op);
4306 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 }
4308
4309 return OK;
4310}
4311
4312/*
4313 * expr5a == expr5b
4314 * expr5a =~ expr5b
4315 * expr5a != expr5b
4316 * expr5a !~ expr5b
4317 * expr5a > expr5b
4318 * expr5a >= expr5b
4319 * expr5a < expr5b
4320 * expr5a <= expr5b
4321 * expr5a is expr5b
4322 * expr5a isnot expr5b
4323 *
4324 * Produces instructions:
4325 * EVAL expr5a Push result of "expr5a"
4326 * EVAL expr5b Push result of "expr5b"
4327 * COMPARE one of the compare instructions
4328 */
4329 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004330compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331{
4332 exptype_T type = EXPR_UNKNOWN;
4333 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004334 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004337 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338
4339 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004340 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 return FAIL;
4342
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004343 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004344 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345
4346 /*
4347 * If there is a comparative operator, use it.
4348 */
4349 if (type != EXPR_UNKNOWN)
4350 {
4351 int ic = FALSE; // Default: do not ignore case
4352
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004353 if (next != NULL)
4354 {
4355 *arg = next_line_from_context(cctx, TRUE);
4356 p = skipwhite(*arg);
4357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 if (type_is && (p[len] == '?' || p[len] == '#'))
4359 {
4360 semsg(_(e_invexpr2), *arg);
4361 return FAIL;
4362 }
4363 // extra question mark appended: ignore case
4364 if (p[len] == '?')
4365 {
4366 ic = TRUE;
4367 ++len;
4368 }
4369 // extra '#' appended: match case (ignored)
4370 else if (p[len] == '#')
4371 ++len;
4372 // nothing appended: match case
4373
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004374 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 {
4376 char_u buf[7];
4377
4378 vim_strncpy(buf, p, len);
4379 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004380 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 }
4382
4383 // get the second variable
4384 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004385 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004386 return FAIL;
4387
Bram Moolenaara5565e42020-05-09 15:44:01 +02004388 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 return FAIL;
4390
Bram Moolenaara5565e42020-05-09 15:44:01 +02004391 if (ppconst->pp_used == ppconst_used + 2)
4392 {
4393 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4394 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4395 int ret;
4396
4397 // Both sides are a constant, compute the result now.
4398 // First check for a valid combination of types, this is more
4399 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004400 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004401 ret = FAIL;
4402 else
4403 {
4404 ret = typval_compare(tv1, tv2, type, ic);
4405 tv1->v_type = VAR_BOOL;
4406 tv1->vval.v_number = tv1->vval.v_number
4407 ? VVAL_TRUE : VVAL_FALSE;
4408 clear_tv(tv2);
4409 --ppconst->pp_used;
4410 }
4411 return ret;
4412 }
4413
4414 generate_ppconst(cctx, ppconst);
4415 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 }
4417
4418 return OK;
4419}
4420
Bram Moolenaar7f141552020-05-09 17:35:53 +02004421static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423/*
4424 * Compile || or &&.
4425 */
4426 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004427compile_and_or(
4428 char_u **arg,
4429 cctx_T *cctx,
4430 char *op,
4431 ppconst_T *ppconst,
4432 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004434 char_u *next;
4435 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 int opchar = *op;
4437
4438 if (p[0] == opchar && p[1] == opchar)
4439 {
4440 garray_T *instr = &cctx->ctx_instr;
4441 garray_T end_ga;
4442
4443 /*
4444 * Repeat until there is no following "||" or "&&"
4445 */
4446 ga_init2(&end_ga, sizeof(int), 10);
4447 while (p[0] == opchar && p[1] == opchar)
4448 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004449 if (next != NULL)
4450 {
4451 *arg = next_line_from_context(cctx, TRUE);
4452 p = skipwhite(*arg);
4453 }
4454
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004455 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4456 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004458 return FAIL;
4459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460
Bram Moolenaara5565e42020-05-09 15:44:01 +02004461 // TODO: use ppconst if the value is a constant
4462 generate_ppconst(cctx, ppconst);
4463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 if (ga_grow(&end_ga, 1) == FAIL)
4465 {
4466 ga_clear(&end_ga);
4467 return FAIL;
4468 }
4469 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4470 ++end_ga.ga_len;
4471 generate_JUMP(cctx, opchar == '|'
4472 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4473
4474 // eval the next expression
4475 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004476 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004477 return FAIL;
4478
Bram Moolenaara5565e42020-05-09 15:44:01 +02004479 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4480 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 {
4482 ga_clear(&end_ga);
4483 return FAIL;
4484 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004485
4486 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004488 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489
4490 // Fill in the end label in all jumps.
4491 while (end_ga.ga_len > 0)
4492 {
4493 isn_T *isn;
4494
4495 --end_ga.ga_len;
4496 isn = ((isn_T *)instr->ga_data)
4497 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4498 isn->isn_arg.jump.jump_where = instr->ga_len;
4499 }
4500 ga_clear(&end_ga);
4501 }
4502
4503 return OK;
4504}
4505
4506/*
4507 * expr4a && expr4a && expr4a logical AND
4508 *
4509 * Produces instructions:
4510 * EVAL expr4a Push result of "expr4a"
4511 * JUMP_AND_KEEP_IF_FALSE end
4512 * EVAL expr4b Push result of "expr4b"
4513 * JUMP_AND_KEEP_IF_FALSE end
4514 * EVAL expr4c Push result of "expr4c"
4515 * end:
4516 */
4517 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004518compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004520 int ppconst_used = ppconst->pp_used;
4521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004523 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 return FAIL;
4525
4526 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004527 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528}
4529
4530/*
4531 * expr3a || expr3b || expr3c logical OR
4532 *
4533 * Produces instructions:
4534 * EVAL expr3a Push result of "expr3a"
4535 * JUMP_AND_KEEP_IF_TRUE end
4536 * EVAL expr3b Push result of "expr3b"
4537 * JUMP_AND_KEEP_IF_TRUE end
4538 * EVAL expr3c Push result of "expr3c"
4539 * end:
4540 */
4541 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004542compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004544 int ppconst_used = ppconst->pp_used;
4545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004547 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 return FAIL;
4549
4550 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004551 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552}
4553
4554/*
4555 * Toplevel expression: expr2 ? expr1a : expr1b
4556 *
4557 * Produces instructions:
4558 * EVAL expr2 Push result of "expr"
4559 * JUMP_IF_FALSE alt jump if false
4560 * EVAL expr1a
4561 * JUMP_ALWAYS end
4562 * alt: EVAL expr1b
4563 * end:
4564 */
4565 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004566compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567{
4568 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004570 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571
Bram Moolenaar61a89812020-05-07 16:58:17 +02004572 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004573 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 return FAIL;
4575
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004576 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 if (*p == '?')
4578 {
4579 garray_T *instr = &cctx->ctx_instr;
4580 garray_T *stack = &cctx->ctx_type_stack;
4581 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004582 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004584 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004586 int has_const_expr = FALSE;
4587 int const_value = FALSE;
4588 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004590 if (next != NULL)
4591 {
4592 *arg = next_line_from_context(cctx, TRUE);
4593 p = skipwhite(*arg);
4594 }
4595
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004596 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4597 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004599 return FAIL;
4600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601
Bram Moolenaara5565e42020-05-09 15:44:01 +02004602 if (ppconst->pp_used == ppconst_used + 1)
4603 {
4604 // the condition is a constant, we know whether the ? or the :
4605 // expression is to be evaluated.
4606 has_const_expr = TRUE;
4607 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4608 clear_tv(&ppconst->pp_tv[ppconst_used]);
4609 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004610 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4611 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004612 }
4613 else
4614 {
4615 generate_ppconst(cctx, ppconst);
4616 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618
4619 // evaluate the second expression; any type is accepted
4620 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004621 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004622 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004623 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004624 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625
Bram Moolenaara5565e42020-05-09 15:44:01 +02004626 if (!has_const_expr)
4627 {
4628 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629
Bram Moolenaara5565e42020-05-09 15:44:01 +02004630 // remember the type and drop it
4631 --stack->ga_len;
4632 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633
Bram Moolenaara5565e42020-05-09 15:44:01 +02004634 end_idx = instr->ga_len;
4635 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4636
4637 // jump here from JUMP_IF_FALSE
4638 isn = ((isn_T *)instr->ga_data) + alt_idx;
4639 isn->isn_arg.jump.jump_where = instr->ga_len;
4640 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641
4642 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004643 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 if (*p != ':')
4645 {
4646 emsg(_(e_missing_colon));
4647 return FAIL;
4648 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004649 if (next != NULL)
4650 {
4651 *arg = next_line_from_context(cctx, TRUE);
4652 p = skipwhite(*arg);
4653 }
4654
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004655 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4656 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004658 return FAIL;
4659 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660
4661 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004662 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004663 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4664 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004666 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004667 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004668 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004669 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670
Bram Moolenaara5565e42020-05-09 15:44:01 +02004671 if (!has_const_expr)
4672 {
4673 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674
Bram Moolenaara5565e42020-05-09 15:44:01 +02004675 // If the types differ, the result has a more generic type.
4676 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4677 common_type(type1, type2, &type2, cctx->ctx_type_list);
4678
4679 // jump here from JUMP_ALWAYS
4680 isn = ((isn_T *)instr->ga_data) + end_idx;
4681 isn->isn_arg.jump.jump_where = instr->ga_len;
4682 }
4683
4684 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 }
4686 return OK;
4687}
4688
4689/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004690 * Toplevel expression.
4691 */
4692 static int
4693compile_expr0(char_u **arg, cctx_T *cctx)
4694{
4695 ppconst_T ppconst;
4696
4697 CLEAR_FIELD(ppconst);
4698 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4699 {
4700 clear_ppconst(&ppconst);
4701 return FAIL;
4702 }
4703 if (generate_ppconst(cctx, &ppconst) == FAIL)
4704 return FAIL;
4705 return OK;
4706}
4707
4708/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 * compile "return [expr]"
4710 */
4711 static char_u *
4712compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4713{
4714 char_u *p = arg;
4715 garray_T *stack = &cctx->ctx_type_stack;
4716 type_T *stack_type;
4717
4718 if (*p != NUL && *p != '|' && *p != '\n')
4719 {
4720 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004721 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 return NULL;
4723
4724 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4725 if (set_return_type)
4726 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004727 else
4728 {
4729 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4730 && stack_type->tt_type != VAR_VOID
4731 && stack_type->tt_type != VAR_UNKNOWN)
4732 {
4733 emsg(_("E1096: Returning a value in a function without a return type"));
4734 return NULL;
4735 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004736 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4737 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004739 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 }
4741 else
4742 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004743 // "set_return_type" cannot be TRUE, only used for a lambda which
4744 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004745 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4746 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 {
4748 emsg(_("E1003: Missing return value"));
4749 return NULL;
4750 }
4751
4752 // No argument, return zero.
4753 generate_PUSHNR(cctx, 0);
4754 }
4755
4756 if (generate_instr(cctx, ISN_RETURN) == NULL)
4757 return NULL;
4758
4759 // "return val | endif" is possible
4760 return skipwhite(p);
4761}
4762
4763/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004764 * Get a line from the compilation context, compatible with exarg_T getline().
4765 * Return a pointer to the line in allocated memory.
4766 * Return NULL for end-of-file or some error.
4767 */
4768 static char_u *
4769exarg_getline(
4770 int c UNUSED,
4771 void *cookie,
4772 int indent UNUSED,
4773 int do_concat UNUSED)
4774{
4775 cctx_T *cctx = (cctx_T *)cookie;
4776
4777 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4778 {
4779 iemsg("Heredoc got to end");
4780 return NULL;
4781 }
4782 ++cctx->ctx_lnum;
4783 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4784 [cctx->ctx_lnum]);
4785}
4786
4787/*
4788 * Compile a nested :def command.
4789 */
4790 static char_u *
4791compile_nested_function(exarg_T *eap, cctx_T *cctx)
4792{
4793 char_u *name_start = eap->arg;
4794 char_u *name_end = to_name_end(eap->arg, FALSE);
4795 char_u *name = get_lambda_name();
4796 lvar_T *lvar;
4797 ufunc_T *ufunc;
4798
4799 eap->arg = name_end;
4800 eap->getline = exarg_getline;
4801 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004802 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004803 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004804 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004805
Bram Moolenaar822ba242020-05-24 23:00:18 +02004806 if (ufunc == NULL)
4807 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004808 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004809 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004810 return NULL;
4811
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004812 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004813 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004814 TRUE, ufunc->uf_func_type);
4815
4816 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4817 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4818 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004819
Bram Moolenaar61a89812020-05-07 16:58:17 +02004820 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004821 return (char_u *)"";
4822}
4823
4824/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 * Return the length of an assignment operator, or zero if there isn't one.
4826 */
4827 int
4828assignment_len(char_u *p, int *heredoc)
4829{
4830 if (*p == '=')
4831 {
4832 if (p[1] == '<' && p[2] == '<')
4833 {
4834 *heredoc = TRUE;
4835 return 3;
4836 }
4837 return 1;
4838 }
4839 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4840 return 2;
4841 if (STRNCMP(p, "..=", 3) == 0)
4842 return 3;
4843 return 0;
4844}
4845
4846// words that cannot be used as a variable
4847static char *reserved[] = {
4848 "true",
4849 "false",
4850 NULL
4851};
4852
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004853typedef enum {
4854 dest_local,
4855 dest_option,
4856 dest_env,
4857 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004858 dest_buffer,
4859 dest_window,
4860 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004861 dest_vimvar,
4862 dest_script,
4863 dest_reg,
4864} assign_dest_T;
4865
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004867 * Generate the load instruction for "name".
4868 */
4869 static void
4870generate_loadvar(
4871 cctx_T *cctx,
4872 assign_dest_T dest,
4873 char_u *name,
4874 lvar_T *lvar,
4875 type_T *type)
4876{
4877 switch (dest)
4878 {
4879 case dest_option:
4880 // TODO: check the option exists
4881 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4882 break;
4883 case dest_global:
4884 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4885 break;
4886 case dest_buffer:
4887 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4888 break;
4889 case dest_window:
4890 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4891 break;
4892 case dest_tab:
4893 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4894 break;
4895 case dest_script:
4896 compile_load_scriptvar(cctx,
4897 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4898 break;
4899 case dest_env:
4900 // Include $ in the name here
4901 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4902 break;
4903 case dest_reg:
4904 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4905 break;
4906 case dest_vimvar:
4907 generate_LOADV(cctx, name + 2, TRUE);
4908 break;
4909 case dest_local:
4910 if (lvar->lv_from_outer)
4911 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4912 NULL, type);
4913 else
4914 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4915 break;
4916 }
4917}
4918
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004919 void
4920vim9_declare_error(char_u *name)
4921{
4922 char *scope = "";
4923
4924 switch (*name)
4925 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004926 case 'g': scope = _("global"); break;
4927 case 'b': scope = _("buffer"); break;
4928 case 'w': scope = _("window"); break;
4929 case 't': scope = _("tab"); break;
4930 case 'v': scope = "v:"; break;
4931 case '$': semsg(_(e_declare_env_var), name); return;
4932 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004933 }
4934 semsg(_(e_declare_var), scope, name);
4935}
4936
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004937/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004938 * Compile declaration and assignment:
4939 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004941 * Return NULL for an error.
4942 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 */
4944 static char_u *
4945compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4946{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004947 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004949 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 char_u *ret = NULL;
4951 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004952 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004955 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957 int oplen = 0;
4958 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004959 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004960 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004961 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004965 // Skip over the "var" or "[var, var]" to get to any "=".
4966 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4967 if (p == NULL)
4968 return *arg == '[' ? arg : NULL;
4969
4970 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004972 // TODO: should we allow this, and figure out type inference from list
4973 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004974 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 return NULL;
4976 }
4977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978 sp = p;
4979 p = skipwhite(p);
4980 op = p;
4981 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004982
4983 if (var_count > 0 && oplen == 0)
4984 // can be something like "[1, 2]->func()"
4985 return arg;
4986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4988 {
4989 char_u buf[4];
4990
4991 vim_strncpy(buf, op, oplen);
4992 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004993 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004994 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 if (heredoc)
4997 {
4998 list_T *l;
4999 listitem_T *li;
5000
5001 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005002 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005004 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005
5006 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005007 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 {
5009 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5010 li->li_tv.vval.v_string = NULL;
5011 }
5012 generate_NEWLIST(cctx, l->lv_len);
5013 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005014 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 list_free(l);
5016 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005017 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005019 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005021 // for "[var, var] = expr" evaluate the expression here, loop over the
5022 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005023
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005024 p = skipwhite(op + oplen);
5025 if (compile_expr0(&p, cctx) == FAIL)
5026 return NULL;
5027 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005029 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005031 type_T *stacktype;
5032
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005033 stacktype = stack->ga_len == 0 ? &t_void
5034 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005035 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005037 emsg(_(e_cannot_use_void));
5038 goto theend;
5039 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005040 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005041 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005042 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005043 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5044 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005045 }
5046 }
5047
5048 /*
5049 * Loop over variables in "[var, var] = expr".
5050 * For "var = expr" and "let var: type" this is done only once.
5051 */
5052 if (var_count > 0)
5053 var_start = skipwhite(arg + 1); // skip over the "["
5054 else
5055 var_start = arg;
5056 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5057 {
5058 char_u *var_end = skip_var_one(var_start, FALSE);
5059 size_t varlen;
5060 int new_local = FALSE;
5061 int opt_type;
5062 int opt_flags = 0;
5063 assign_dest_T dest = dest_local;
5064 int vimvaridx = -1;
5065 lvar_T *lvar = NULL;
5066 lvar_T arg_lvar;
5067 int has_type = FALSE;
5068 int has_index = FALSE;
5069 int instr_count = -1;
5070
5071 p = (*var_start == '&' || *var_start == '$'
5072 || *var_start == '@') ? var_start + 1 : var_start;
5073 p = to_name_end(p, TRUE);
5074
5075 // "a: type" is declaring variable "a" with a type, not "a:".
5076 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5077 --var_end;
5078 if (is_decl && p == var_start + 2 && p[-1] == ':')
5079 --p;
5080
5081 varlen = p - var_start;
5082 vim_free(name);
5083 name = vim_strnsave(var_start, varlen);
5084 if (name == NULL)
5085 return NULL;
5086 if (!heredoc)
5087 type = &t_any;
5088
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005089 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005090 {
5091 if (*var_start == '&')
5092 {
5093 int cc;
5094 long numval;
5095
5096 dest = dest_option;
5097 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005099 emsg(_(e_const_option));
5100 goto theend;
5101 }
5102 if (is_decl)
5103 {
5104 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5105 goto theend;
5106 }
5107 p = var_start;
5108 p = find_option_end(&p, &opt_flags);
5109 if (p == NULL)
5110 {
5111 // cannot happen?
5112 emsg(_(e_letunexp));
5113 goto theend;
5114 }
5115 cc = *p;
5116 *p = NUL;
5117 opt_type = get_option_value(var_start + 1, &numval,
5118 NULL, opt_flags);
5119 *p = cc;
5120 if (opt_type == -3)
5121 {
5122 semsg(_(e_unknown_option), var_start);
5123 goto theend;
5124 }
5125 if (opt_type == -2 || opt_type == 0)
5126 type = &t_string;
5127 else
5128 type = &t_number; // both number and boolean option
5129 }
5130 else if (*var_start == '$')
5131 {
5132 dest = dest_env;
5133 type = &t_string;
5134 if (is_decl)
5135 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005136 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005137 goto theend;
5138 }
5139 }
5140 else if (*var_start == '@')
5141 {
5142 if (!valid_yank_reg(var_start[1], TRUE))
5143 {
5144 emsg_invreg(var_start[1]);
5145 goto theend;
5146 }
5147 dest = dest_reg;
5148 type = &t_string;
5149 if (is_decl)
5150 {
5151 semsg(_("E1066: Cannot declare a register: %s"), name);
5152 goto theend;
5153 }
5154 }
5155 else if (STRNCMP(var_start, "g:", 2) == 0)
5156 {
5157 dest = dest_global;
5158 if (is_decl)
5159 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005160 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005161 goto theend;
5162 }
5163 }
5164 else if (STRNCMP(var_start, "b:", 2) == 0)
5165 {
5166 dest = dest_buffer;
5167 if (is_decl)
5168 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005169 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005170 goto theend;
5171 }
5172 }
5173 else if (STRNCMP(var_start, "w:", 2) == 0)
5174 {
5175 dest = dest_window;
5176 if (is_decl)
5177 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005178 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005179 goto theend;
5180 }
5181 }
5182 else if (STRNCMP(var_start, "t:", 2) == 0)
5183 {
5184 dest = dest_tab;
5185 if (is_decl)
5186 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005187 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005188 goto theend;
5189 }
5190 }
5191 else if (STRNCMP(var_start, "v:", 2) == 0)
5192 {
5193 typval_T *vtv;
5194 int di_flags;
5195
5196 vimvaridx = find_vim_var(name + 2, &di_flags);
5197 if (vimvaridx < 0)
5198 {
5199 semsg(_(e_var_notfound), var_start);
5200 goto theend;
5201 }
5202 // We use the current value of "sandbox" here, is that OK?
5203 if (var_check_ro(di_flags, name, FALSE))
5204 goto theend;
5205 dest = dest_vimvar;
5206 vtv = get_vim_var_tv(vimvaridx);
5207 type = typval2type(vtv);
5208 if (is_decl)
5209 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005210 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005211 goto theend;
5212 }
5213 }
5214 else
5215 {
5216 int idx;
5217
5218 for (idx = 0; reserved[idx] != NULL; ++idx)
5219 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005220 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005221 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005222 goto theend;
5223 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005224
5225 lvar = lookup_local(var_start, varlen, cctx);
5226 if (lvar == NULL)
5227 {
5228 CLEAR_FIELD(arg_lvar);
5229 if (lookup_arg(var_start, varlen,
5230 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5231 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005232 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005233 if (is_decl)
5234 {
5235 semsg(_(e_used_as_arg), name);
5236 goto theend;
5237 }
5238 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005239 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005240 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005241 if (lvar != NULL)
5242 {
5243 if (is_decl)
5244 {
5245 semsg(_("E1017: Variable already declared: %s"), name);
5246 goto theend;
5247 }
5248 else if (lvar->lv_const)
5249 {
5250 semsg(_("E1018: Cannot assign to a constant: %s"),
5251 name);
5252 goto theend;
5253 }
5254 }
5255 else if (STRNCMP(var_start, "s:", 2) == 0
5256 || lookup_script(var_start, varlen) == OK
5257 || find_imported(var_start, varlen, cctx) != NULL)
5258 {
5259 dest = dest_script;
5260 if (is_decl)
5261 {
5262 semsg(_("E1054: Variable already declared in the script: %s"),
5263 name);
5264 goto theend;
5265 }
5266 }
5267 else if (name[1] == ':' && name[2] != NUL)
5268 {
5269 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5270 name);
5271 goto theend;
5272 }
5273 else if (!is_decl)
5274 {
5275 semsg(_("E1089: unknown variable: %s"), name);
5276 goto theend;
5277 }
5278 }
5279 }
5280
5281 // handle "a:name" as a name, not index "name" on "a"
5282 if (varlen > 1 || var_start[varlen] != ':')
5283 p = var_end;
5284
5285 if (dest != dest_option)
5286 {
5287 if (is_decl && *p == ':')
5288 {
5289 // parse optional type: "let var: type = expr"
5290 if (!VIM_ISWHITE(p[1]))
5291 {
5292 semsg(_(e_white_after), ":");
5293 goto theend;
5294 }
5295 p = skipwhite(p + 1);
5296 type = parse_type(&p, cctx->ctx_type_list);
5297 has_type = TRUE;
5298 }
5299 else if (lvar != NULL)
5300 type = lvar->lv_type;
5301 }
5302
5303 if (oplen == 3 && !heredoc && dest != dest_global
5304 && type->tt_type != VAR_STRING
5305 && type->tt_type != VAR_ANY)
5306 {
5307 emsg(_("E1019: Can only concatenate to string"));
5308 goto theend;
5309 }
5310
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005311 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005312 {
5313 if (oplen > 1 && !heredoc)
5314 {
5315 // +=, /=, etc. require an existing variable
5316 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5317 name);
5318 goto theend;
5319 }
5320
5321 // new local variable
5322 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5323 goto theend;
5324 lvar = reserve_local(cctx, var_start, varlen,
5325 cmdidx == CMD_const, type);
5326 if (lvar == NULL)
5327 goto theend;
5328 new_local = TRUE;
5329 }
5330
5331 member_type = type;
5332 if (var_end > var_start + varlen)
5333 {
5334 // Something follows after the variable: "var[idx]".
5335 if (is_decl)
5336 {
5337 emsg(_("E1087: cannot use an index when declaring a variable"));
5338 goto theend;
5339 }
5340
5341 if (var_start[varlen] == '[')
5342 {
5343 has_index = TRUE;
5344 if (type->tt_member == NULL)
5345 {
5346 semsg(_("E1088: cannot use an index on %s"), name);
5347 goto theend;
5348 }
5349 member_type = type->tt_member;
5350 }
5351 else
5352 {
5353 semsg("Not supported yet: %s", var_start);
5354 goto theend;
5355 }
5356 }
5357 else if (lvar == &arg_lvar)
5358 {
5359 semsg(_("E1090: Cannot assign to argument %s"), name);
5360 goto theend;
5361 }
5362
5363 if (!heredoc)
5364 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005365 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005366 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005367 if (oplen > 0 && var_count == 0)
5368 {
5369 // skip over the "=" and the expression
5370 p = skipwhite(op + oplen);
5371 compile_expr0(&p, cctx);
5372 }
5373 }
5374 else if (oplen > 0)
5375 {
5376 type_T *stacktype;
5377
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005378 // For "var = expr" evaluate the expression.
5379 if (var_count == 0)
5380 {
5381 int r;
5382
5383 // for "+=", "*=", "..=" etc. first load the current value
5384 if (*op != '=')
5385 {
5386 generate_loadvar(cctx, dest, name, lvar, type);
5387
5388 if (has_index)
5389 {
5390 // TODO: get member from list or dict
5391 emsg("Index with operation not supported yet");
5392 goto theend;
5393 }
5394 }
5395
5396 // Compile the expression. Temporarily hide the new local
5397 // variable here, it is not available to this expression.
5398 if (new_local)
5399 --cctx->ctx_locals.ga_len;
5400 instr_count = instr->ga_len;
5401 p = skipwhite(op + oplen);
5402 r = compile_expr0(&p, cctx);
5403 if (new_local)
5404 ++cctx->ctx_locals.ga_len;
5405 if (r == FAIL)
5406 goto theend;
5407 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005408 else if (semicolon && var_idx == var_count - 1)
5409 {
5410 // For "[var; var] = expr" get the rest of the list
5411 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5412 goto theend;
5413 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005414 else
5415 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005416 // For "[var, var] = expr" get the "var_idx" item from the
5417 // list.
5418 if (generate_GETITEM(cctx, var_idx) == FAIL)
5419 return FAIL;
5420 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005421
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005422 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005423 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005424 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005425 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005426 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005427 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005428 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005429 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005430 emsg(_(e_cannot_use_void));
5431 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005432 }
5433 else
5434 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005435 // An empty list or dict has a &t_void member,
5436 // for a variable that implies &t_any.
5437 if (stacktype == &t_list_empty)
5438 lvar->lv_type = &t_list_any;
5439 else if (stacktype == &t_dict_empty)
5440 lvar->lv_type = &t_dict_any;
5441 else
5442 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005443 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005444 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005445 else
5446 {
5447 type_T *use_type = lvar->lv_type;
5448
5449 if (has_index)
5450 {
5451 use_type = use_type->tt_member;
5452 if (use_type == NULL)
5453 use_type = &t_void;
5454 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005455 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005456 == FAIL)
5457 goto theend;
5458 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005459 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005460 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005461 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005462 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005465 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005466 emsg(_(e_const_req_value));
5467 goto theend;
5468 }
5469 else if (!has_type || dest == dest_option)
5470 {
5471 emsg(_(e_type_req));
5472 goto theend;
5473 }
5474 else
5475 {
5476 // variables are always initialized
5477 if (ga_grow(instr, 1) == FAIL)
5478 goto theend;
5479 switch (member_type->tt_type)
5480 {
5481 case VAR_BOOL:
5482 generate_PUSHBOOL(cctx, VVAL_FALSE);
5483 break;
5484 case VAR_FLOAT:
5485#ifdef FEAT_FLOAT
5486 generate_PUSHF(cctx, 0.0);
5487#endif
5488 break;
5489 case VAR_STRING:
5490 generate_PUSHS(cctx, NULL);
5491 break;
5492 case VAR_BLOB:
5493 generate_PUSHBLOB(cctx, NULL);
5494 break;
5495 case VAR_FUNC:
5496 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5497 break;
5498 case VAR_LIST:
5499 generate_NEWLIST(cctx, 0);
5500 break;
5501 case VAR_DICT:
5502 generate_NEWDICT(cctx, 0);
5503 break;
5504 case VAR_JOB:
5505 generate_PUSHJOB(cctx, NULL);
5506 break;
5507 case VAR_CHANNEL:
5508 generate_PUSHCHANNEL(cctx, NULL);
5509 break;
5510 case VAR_NUMBER:
5511 case VAR_UNKNOWN:
5512 case VAR_ANY:
5513 case VAR_PARTIAL:
5514 case VAR_VOID:
5515 case VAR_SPECIAL: // cannot happen
5516 generate_PUSHNR(cctx, 0);
5517 break;
5518 }
5519 }
5520 if (var_count == 0)
5521 end = p;
5522 }
5523
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005524 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005525 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005526 break;
5527
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005528 if (oplen > 0 && *op != '=')
5529 {
5530 type_T *expected = &t_number;
5531 type_T *stacktype;
5532
5533 // TODO: if type is known use float or any operation
5534
5535 if (*op == '.')
5536 expected = &t_string;
5537 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005538 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005539 goto theend;
5540
5541 if (*op == '.')
5542 generate_instr_drop(cctx, ISN_CONCAT, 1);
5543 else
5544 {
5545 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5546
5547 if (isn == NULL)
5548 goto theend;
5549 switch (*op)
5550 {
5551 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5552 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5553 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5554 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5555 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557 }
5558 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005559
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005560 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005561 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005562 int r;
5563
5564 // Compile the "idx" in "var[idx]".
5565 if (new_local)
5566 --cctx->ctx_locals.ga_len;
5567 p = skipwhite(var_start + varlen + 1);
5568 r = compile_expr0(&p, cctx);
5569 if (new_local)
5570 ++cctx->ctx_locals.ga_len;
5571 if (r == FAIL)
5572 goto theend;
5573 if (*skipwhite(p) != ']')
5574 {
5575 emsg(_(e_missbrac));
5576 goto theend;
5577 }
5578 if (type->tt_type == VAR_DICT
5579 && may_generate_2STRING(-1, cctx) == FAIL)
5580 goto theend;
5581 if (type->tt_type == VAR_LIST
5582 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005583 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005584 {
5585 emsg(_(e_number_exp));
5586 goto theend;
5587 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005588
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005589 // Load the dict or list. On the stack we then have:
5590 // - value
5591 // - index
5592 // - variable
5593 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005594
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005595 if (type->tt_type == VAR_LIST)
5596 {
5597 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5598 return FAIL;
5599 }
5600 else if (type->tt_type == VAR_DICT)
5601 {
5602 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5603 return FAIL;
5604 }
5605 else
5606 {
5607 emsg(_(e_listreq));
5608 goto theend;
5609 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005610 }
5611 else
5612 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005613 switch (dest)
5614 {
5615 case dest_option:
5616 generate_STOREOPT(cctx, name + 1, opt_flags);
5617 break;
5618 case dest_global:
5619 // include g: with the name, easier to execute that way
5620 generate_STORE(cctx, ISN_STOREG, 0, name);
5621 break;
5622 case dest_buffer:
5623 // include b: with the name, easier to execute that way
5624 generate_STORE(cctx, ISN_STOREB, 0, name);
5625 break;
5626 case dest_window:
5627 // include w: with the name, easier to execute that way
5628 generate_STORE(cctx, ISN_STOREW, 0, name);
5629 break;
5630 case dest_tab:
5631 // include t: with the name, easier to execute that way
5632 generate_STORE(cctx, ISN_STORET, 0, name);
5633 break;
5634 case dest_env:
5635 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5636 break;
5637 case dest_reg:
5638 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5639 break;
5640 case dest_vimvar:
5641 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5642 break;
5643 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005644 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005645 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5646 imported_T *import = NULL;
5647 int sid = current_sctx.sc_sid;
5648 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005649
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005650 if (name[1] != ':')
5651 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005652 import = find_imported(name, 0, cctx);
5653 if (import != NULL)
5654 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005655 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005656
5657 idx = get_script_item_idx(sid, rawname, TRUE);
5658 // TODO: specific type
5659 if (idx < 0)
5660 {
5661 char_u *name_s = name;
5662
5663 // Include s: in the name for store_var()
5664 if (name[1] != ':')
5665 {
5666 int len = (int)STRLEN(name) + 3;
5667
5668 name_s = alloc(len);
5669 if (name_s == NULL)
5670 name_s = name;
5671 else
5672 vim_snprintf((char *)name_s, len,
5673 "s:%s", name);
5674 }
5675 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005676 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005677 if (name_s != name)
5678 vim_free(name_s);
5679 }
5680 else
5681 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005682 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005683 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005684 break;
5685 case dest_local:
5686 if (lvar != NULL)
5687 {
5688 isn_T *isn = ((isn_T *)instr->ga_data)
5689 + instr->ga_len - 1;
5690
5691 // optimization: turn "var = 123" from ISN_PUSHNR +
5692 // ISN_STORE into ISN_STORENR
5693 if (!lvar->lv_from_outer
5694 && instr->ga_len == instr_count + 1
5695 && isn->isn_type == ISN_PUSHNR)
5696 {
5697 varnumber_T val = isn->isn_arg.number;
5698
5699 isn->isn_type = ISN_STORENR;
5700 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5701 isn->isn_arg.storenr.stnr_val = val;
5702 if (stack->ga_len > 0)
5703 --stack->ga_len;
5704 }
5705 else if (lvar->lv_from_outer)
5706 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005707 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005708 else
5709 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5710 }
5711 break;
5712 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005713 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005714
5715 if (var_idx + 1 < var_count)
5716 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005717 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005718
5719 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005720 if (var_count > 0 && !semicolon)
5721 {
5722 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5723 goto theend;
5724 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005725
Bram Moolenaarb2097502020-07-19 17:17:02 +02005726 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005727
5728theend:
5729 vim_free(name);
5730 return ret;
5731}
5732
5733/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005734 * Check if "name" can be "unlet".
5735 */
5736 int
5737check_vim9_unlet(char_u *name)
5738{
5739 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5740 {
5741 semsg(_("E1081: Cannot unlet %s"), name);
5742 return FAIL;
5743 }
5744 return OK;
5745}
5746
5747/*
5748 * Callback passed to ex_unletlock().
5749 */
5750 static int
5751compile_unlet(
5752 lval_T *lvp,
5753 char_u *name_end,
5754 exarg_T *eap,
5755 int deep UNUSED,
5756 void *coookie)
5757{
5758 cctx_T *cctx = coookie;
5759
5760 if (lvp->ll_tv == NULL)
5761 {
5762 char_u *p = lvp->ll_name;
5763 int cc = *name_end;
5764 int ret = OK;
5765
5766 // Normal name. Only supports g:, w:, t: and b: namespaces.
5767 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005768 if (*p == '$')
5769 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5770 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005771 ret = FAIL;
5772 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005773 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005774
5775 *name_end = cc;
5776 return ret;
5777 }
5778
5779 // TODO: unlet {list}[idx]
5780 // TODO: unlet {dict}[key]
5781 emsg("Sorry, :unlet not fully implemented yet");
5782 return FAIL;
5783}
5784
5785/*
5786 * compile "unlet var", "lock var" and "unlock var"
5787 * "arg" points to "var".
5788 */
5789 static char_u *
5790compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5791{
5792 char_u *p = arg;
5793
5794 if (eap->cmdidx != CMD_unlet)
5795 {
5796 emsg("Sorry, :lock and unlock not implemented yet");
5797 return NULL;
5798 }
5799
5800 if (*p == '!')
5801 {
5802 p = skipwhite(p + 1);
5803 eap->forceit = TRUE;
5804 }
5805
5806 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5807 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5808}
5809
5810/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005811 * Compile an :import command.
5812 */
5813 static char_u *
5814compile_import(char_u *arg, cctx_T *cctx)
5815{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005816 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005817}
5818
5819/*
5820 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5821 */
5822 static int
5823compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5824{
5825 garray_T *instr = &cctx->ctx_instr;
5826 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5827
5828 if (endlabel == NULL)
5829 return FAIL;
5830 endlabel->el_next = *el;
5831 *el = endlabel;
5832 endlabel->el_end_label = instr->ga_len;
5833
5834 generate_JUMP(cctx, when, 0);
5835 return OK;
5836}
5837
5838 static void
5839compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5840{
5841 garray_T *instr = &cctx->ctx_instr;
5842
5843 while (*el != NULL)
5844 {
5845 endlabel_T *cur = (*el);
5846 isn_T *isn;
5847
5848 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5849 isn->isn_arg.jump.jump_where = instr->ga_len;
5850 *el = cur->el_next;
5851 vim_free(cur);
5852 }
5853}
5854
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005855 static void
5856compile_free_jump_to_end(endlabel_T **el)
5857{
5858 while (*el != NULL)
5859 {
5860 endlabel_T *cur = (*el);
5861
5862 *el = cur->el_next;
5863 vim_free(cur);
5864 }
5865}
5866
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005867/*
5868 * Create a new scope and set up the generic items.
5869 */
5870 static scope_T *
5871new_scope(cctx_T *cctx, scopetype_T type)
5872{
5873 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5874
5875 if (scope == NULL)
5876 return NULL;
5877 scope->se_outer = cctx->ctx_scope;
5878 cctx->ctx_scope = scope;
5879 scope->se_type = type;
5880 scope->se_local_count = cctx->ctx_locals.ga_len;
5881 return scope;
5882}
5883
5884/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005885 * Free the current scope and go back to the outer scope.
5886 */
5887 static void
5888drop_scope(cctx_T *cctx)
5889{
5890 scope_T *scope = cctx->ctx_scope;
5891
5892 if (scope == NULL)
5893 {
5894 iemsg("calling drop_scope() without a scope");
5895 return;
5896 }
5897 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005898 switch (scope->se_type)
5899 {
5900 case IF_SCOPE:
5901 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5902 case FOR_SCOPE:
5903 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5904 case WHILE_SCOPE:
5905 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5906 case TRY_SCOPE:
5907 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5908 case NO_SCOPE:
5909 case BLOCK_SCOPE:
5910 break;
5911 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005912 vim_free(scope);
5913}
5914
5915/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916 * compile "if expr"
5917 *
5918 * "if expr" Produces instructions:
5919 * EVAL expr Push result of "expr"
5920 * JUMP_IF_FALSE end
5921 * ... body ...
5922 * end:
5923 *
5924 * "if expr | else" Produces instructions:
5925 * EVAL expr Push result of "expr"
5926 * JUMP_IF_FALSE else
5927 * ... body ...
5928 * JUMP_ALWAYS end
5929 * else:
5930 * ... body ...
5931 * end:
5932 *
5933 * "if expr1 | elseif expr2 | else" Produces instructions:
5934 * EVAL expr Push result of "expr"
5935 * JUMP_IF_FALSE elseif
5936 * ... body ...
5937 * JUMP_ALWAYS end
5938 * elseif:
5939 * EVAL expr Push result of "expr"
5940 * JUMP_IF_FALSE else
5941 * ... body ...
5942 * JUMP_ALWAYS end
5943 * else:
5944 * ... body ...
5945 * end:
5946 */
5947 static char_u *
5948compile_if(char_u *arg, cctx_T *cctx)
5949{
5950 char_u *p = arg;
5951 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005952 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005954 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005955 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956
Bram Moolenaara5565e42020-05-09 15:44:01 +02005957 CLEAR_FIELD(ppconst);
5958 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005959 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005960 clear_ppconst(&ppconst);
5961 return NULL;
5962 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005963 if (cctx->ctx_skip == SKIP_YES)
5964 clear_ppconst(&ppconst);
5965 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005966 {
5967 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005968 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005969 clear_ppconst(&ppconst);
5970 }
5971 else
5972 {
5973 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005974 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005975 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005976 return NULL;
5977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005978
5979 scope = new_scope(cctx, IF_SCOPE);
5980 if (scope == NULL)
5981 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005982 scope->se_skip_save = skip_save;
5983 // "is_had_return" will be reset if any block does not end in :return
5984 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005985
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005986 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005987 {
5988 // "where" is set when ":elseif", "else" or ":endif" is found
5989 scope->se_u.se_if.is_if_label = instr->ga_len;
5990 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5991 }
5992 else
5993 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994
5995 return p;
5996}
5997
5998 static char_u *
5999compile_elseif(char_u *arg, cctx_T *cctx)
6000{
6001 char_u *p = arg;
6002 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006003 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006004 isn_T *isn;
6005 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006006 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007
6008 if (scope == NULL || scope->se_type != IF_SCOPE)
6009 {
6010 emsg(_(e_elseif_without_if));
6011 return NULL;
6012 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006013 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006014 if (!cctx->ctx_had_return)
6015 scope->se_u.se_if.is_had_return = FALSE;
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 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006020 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006021 return NULL;
6022 // previous "if" or "elseif" jumps here
6023 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6024 isn->isn_arg.jump.jump_where = instr->ga_len;
6025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006027 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006028 CLEAR_FIELD(ppconst);
6029 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006030 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006031 clear_ppconst(&ppconst);
6032 return NULL;
6033 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006034 if (scope->se_skip_save == SKIP_YES)
6035 clear_ppconst(&ppconst);
6036 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006037 {
6038 // The expression results in a constant.
6039 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006040 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006041 clear_ppconst(&ppconst);
6042 scope->se_u.se_if.is_if_label = -1;
6043 }
6044 else
6045 {
6046 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006047 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006048 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006049 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006051 // "where" is set when ":elseif", "else" or ":endif" is found
6052 scope->se_u.se_if.is_if_label = instr->ga_len;
6053 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055
6056 return p;
6057}
6058
6059 static char_u *
6060compile_else(char_u *arg, cctx_T *cctx)
6061{
6062 char_u *p = arg;
6063 garray_T *instr = &cctx->ctx_instr;
6064 isn_T *isn;
6065 scope_T *scope = cctx->ctx_scope;
6066
6067 if (scope == NULL || scope->se_type != IF_SCOPE)
6068 {
6069 emsg(_(e_else_without_if));
6070 return NULL;
6071 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006072 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006073 if (!cctx->ctx_had_return)
6074 scope->se_u.se_if.is_had_return = FALSE;
6075 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076
Bram Moolenaarefd88552020-06-18 20:50:10 +02006077 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006078 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006079 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006080 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006081 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006082 if (!cctx->ctx_had_return
6083 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6084 JUMP_ALWAYS, cctx) == FAIL)
6085 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006086 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006087
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006088 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006089 {
6090 if (scope->se_u.se_if.is_if_label >= 0)
6091 {
6092 // previous "if" or "elseif" jumps here
6093 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6094 isn->isn_arg.jump.jump_where = instr->ga_len;
6095 scope->se_u.se_if.is_if_label = -1;
6096 }
6097 }
6098
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006099 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006100 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6101 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102
6103 return p;
6104}
6105
6106 static char_u *
6107compile_endif(char_u *arg, cctx_T *cctx)
6108{
6109 scope_T *scope = cctx->ctx_scope;
6110 ifscope_T *ifscope;
6111 garray_T *instr = &cctx->ctx_instr;
6112 isn_T *isn;
6113
6114 if (scope == NULL || scope->se_type != IF_SCOPE)
6115 {
6116 emsg(_(e_endif_without_if));
6117 return NULL;
6118 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006119 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006120 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006121 if (!cctx->ctx_had_return)
6122 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006123
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006124 if (scope->se_u.se_if.is_if_label >= 0)
6125 {
6126 // previous "if" or "elseif" jumps here
6127 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6128 isn->isn_arg.jump.jump_where = instr->ga_len;
6129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130 // Fill in the "end" label in jumps at the end of the blocks.
6131 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006132 cctx->ctx_skip = scope->se_skip_save;
6133
6134 // If all the blocks end in :return and there is an :else then the
6135 // had_return flag is set.
6136 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006138 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139 return arg;
6140}
6141
6142/*
6143 * compile "for var in expr"
6144 *
6145 * Produces instructions:
6146 * PUSHNR -1
6147 * STORE loop-idx Set index to -1
6148 * EVAL expr Push result of "expr"
6149 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6150 * - if beyond end, jump to "end"
6151 * - otherwise get item from list and push it
6152 * STORE var Store item in "var"
6153 * ... body ...
6154 * JUMP top Jump back to repeat
6155 * end: DROP Drop the result of "expr"
6156 *
6157 */
6158 static char_u *
6159compile_for(char_u *arg, cctx_T *cctx)
6160{
6161 char_u *p;
6162 size_t varlen;
6163 garray_T *instr = &cctx->ctx_instr;
6164 garray_T *stack = &cctx->ctx_type_stack;
6165 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006166 lvar_T *loop_lvar; // loop iteration variable
6167 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006168 type_T *vartype;
6169
6170 // TODO: list of variables: "for [key, value] in dict"
6171 // parse "var"
6172 for (p = arg; eval_isnamec1(*p); ++p)
6173 ;
6174 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006175 var_lvar = lookup_local(arg, varlen, cctx);
6176 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177 {
6178 semsg(_("E1023: variable already defined: %s"), arg);
6179 return NULL;
6180 }
6181
6182 // consume "in"
6183 p = skipwhite(p);
6184 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6185 {
6186 emsg(_(e_missing_in));
6187 return NULL;
6188 }
6189 p = skipwhite(p + 2);
6190
6191
6192 scope = new_scope(cctx, FOR_SCOPE);
6193 if (scope == NULL)
6194 return NULL;
6195
6196 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006197 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6198 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006199 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006200 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006201 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006202 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204
6205 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006206 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6207 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006208 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006209 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006210 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006213
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006214 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006215
6216 // compile "expr", it remains on the stack until "endfor"
6217 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006218 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006219 {
6220 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006222 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006223
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006224 // Now that we know the type of "var", check that it is a list, now or at
6225 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006227 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006228 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006229 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 return NULL;
6231 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006232 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006233 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006234
6235 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006236 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006238 generate_FOR(cctx, loop_lvar->lv_idx);
6239 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240
6241 return arg;
6242}
6243
6244/*
6245 * compile "endfor"
6246 */
6247 static char_u *
6248compile_endfor(char_u *arg, cctx_T *cctx)
6249{
6250 garray_T *instr = &cctx->ctx_instr;
6251 scope_T *scope = cctx->ctx_scope;
6252 forscope_T *forscope;
6253 isn_T *isn;
6254
6255 if (scope == NULL || scope->se_type != FOR_SCOPE)
6256 {
6257 emsg(_(e_for));
6258 return NULL;
6259 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006260 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006262 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263
6264 // At end of ":for" scope jump back to the FOR instruction.
6265 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6266
6267 // Fill in the "end" label in the FOR statement so it can jump here
6268 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6269 isn->isn_arg.forloop.for_end = instr->ga_len;
6270
6271 // Fill in the "end" label any BREAK statements
6272 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6273
6274 // Below the ":for" scope drop the "expr" list from the stack.
6275 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6276 return NULL;
6277
6278 vim_free(scope);
6279
6280 return arg;
6281}
6282
6283/*
6284 * compile "while expr"
6285 *
6286 * Produces instructions:
6287 * top: EVAL expr Push result of "expr"
6288 * JUMP_IF_FALSE end jump if false
6289 * ... body ...
6290 * JUMP top Jump back to repeat
6291 * end:
6292 *
6293 */
6294 static char_u *
6295compile_while(char_u *arg, cctx_T *cctx)
6296{
6297 char_u *p = arg;
6298 garray_T *instr = &cctx->ctx_instr;
6299 scope_T *scope;
6300
6301 scope = new_scope(cctx, WHILE_SCOPE);
6302 if (scope == NULL)
6303 return NULL;
6304
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006305 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306
6307 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006308 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 return NULL;
6310
6311 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006312 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 JUMP_IF_FALSE, cctx) == FAIL)
6314 return FAIL;
6315
6316 return p;
6317}
6318
6319/*
6320 * compile "endwhile"
6321 */
6322 static char_u *
6323compile_endwhile(char_u *arg, cctx_T *cctx)
6324{
6325 scope_T *scope = cctx->ctx_scope;
6326
6327 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6328 {
6329 emsg(_(e_while));
6330 return NULL;
6331 }
6332 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006333 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334
6335 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006336 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337
6338 // Fill in the "end" label in the WHILE statement so it can jump here.
6339 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006340 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006341
6342 vim_free(scope);
6343
6344 return arg;
6345}
6346
6347/*
6348 * compile "continue"
6349 */
6350 static char_u *
6351compile_continue(char_u *arg, cctx_T *cctx)
6352{
6353 scope_T *scope = cctx->ctx_scope;
6354
6355 for (;;)
6356 {
6357 if (scope == NULL)
6358 {
6359 emsg(_(e_continue));
6360 return NULL;
6361 }
6362 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6363 break;
6364 scope = scope->se_outer;
6365 }
6366
6367 // Jump back to the FOR or WHILE instruction.
6368 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006369 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6370 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006371 return arg;
6372}
6373
6374/*
6375 * compile "break"
6376 */
6377 static char_u *
6378compile_break(char_u *arg, cctx_T *cctx)
6379{
6380 scope_T *scope = cctx->ctx_scope;
6381 endlabel_T **el;
6382
6383 for (;;)
6384 {
6385 if (scope == NULL)
6386 {
6387 emsg(_(e_break));
6388 return NULL;
6389 }
6390 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6391 break;
6392 scope = scope->se_outer;
6393 }
6394
6395 // Jump to the end of the FOR or WHILE loop.
6396 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006397 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006399 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6401 return FAIL;
6402
6403 return arg;
6404}
6405
6406/*
6407 * compile "{" start of block
6408 */
6409 static char_u *
6410compile_block(char_u *arg, cctx_T *cctx)
6411{
6412 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6413 return NULL;
6414 return skipwhite(arg + 1);
6415}
6416
6417/*
6418 * compile end of block: drop one scope
6419 */
6420 static void
6421compile_endblock(cctx_T *cctx)
6422{
6423 scope_T *scope = cctx->ctx_scope;
6424
6425 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006426 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427 vim_free(scope);
6428}
6429
6430/*
6431 * compile "try"
6432 * Creates a new scope for the try-endtry, pointing to the first catch and
6433 * finally.
6434 * Creates another scope for the "try" block itself.
6435 * TRY instruction sets up exception handling at runtime.
6436 *
6437 * "try"
6438 * TRY -> catch1, -> finally push trystack entry
6439 * ... try block
6440 * "throw {exception}"
6441 * EVAL {exception}
6442 * THROW create exception
6443 * ... try block
6444 * " catch {expr}"
6445 * JUMP -> finally
6446 * catch1: PUSH exeception
6447 * EVAL {expr}
6448 * MATCH
6449 * JUMP nomatch -> catch2
6450 * CATCH remove exception
6451 * ... catch block
6452 * " catch"
6453 * JUMP -> finally
6454 * catch2: CATCH remove exception
6455 * ... catch block
6456 * " finally"
6457 * finally:
6458 * ... finally block
6459 * " endtry"
6460 * ENDTRY pop trystack entry, may rethrow
6461 */
6462 static char_u *
6463compile_try(char_u *arg, cctx_T *cctx)
6464{
6465 garray_T *instr = &cctx->ctx_instr;
6466 scope_T *try_scope;
6467 scope_T *scope;
6468
6469 // scope that holds the jumps that go to catch/finally/endtry
6470 try_scope = new_scope(cctx, TRY_SCOPE);
6471 if (try_scope == NULL)
6472 return NULL;
6473
6474 // "catch" is set when the first ":catch" is found.
6475 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006476 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477 if (generate_instr(cctx, ISN_TRY) == NULL)
6478 return NULL;
6479
6480 // scope for the try block itself
6481 scope = new_scope(cctx, BLOCK_SCOPE);
6482 if (scope == NULL)
6483 return NULL;
6484
6485 return arg;
6486}
6487
6488/*
6489 * compile "catch {expr}"
6490 */
6491 static char_u *
6492compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6493{
6494 scope_T *scope = cctx->ctx_scope;
6495 garray_T *instr = &cctx->ctx_instr;
6496 char_u *p;
6497 isn_T *isn;
6498
6499 // end block scope from :try or :catch
6500 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6501 compile_endblock(cctx);
6502 scope = cctx->ctx_scope;
6503
6504 // Error if not in a :try scope
6505 if (scope == NULL || scope->se_type != TRY_SCOPE)
6506 {
6507 emsg(_(e_catch));
6508 return NULL;
6509 }
6510
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006511 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006512 {
6513 emsg(_("E1033: catch unreachable after catch-all"));
6514 return NULL;
6515 }
6516
6517 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006518 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006519 JUMP_ALWAYS, cctx) == FAIL)
6520 return NULL;
6521
6522 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006523 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524 if (isn->isn_arg.try.try_catch == 0)
6525 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006526 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527 {
6528 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006529 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006530 isn->isn_arg.jump.jump_where = instr->ga_len;
6531 }
6532
6533 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006534 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006535 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006536 scope->se_u.se_try.ts_caught_all = TRUE;
6537 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538 }
6539 else
6540 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006541 char_u *end;
6542 char_u *pat;
6543 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006544 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006545 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 // Push v:exception, push {expr} and MATCH
6548 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6549
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006550 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006551 if (*end != *p)
6552 {
6553 semsg(_("E1067: Separator mismatch: %s"), p);
6554 vim_free(tofree);
6555 return FAIL;
6556 }
6557 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006558 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006559 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006560 len = (int)(end - tofree);
6561 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006562 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006563 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006564 if (pat == NULL)
6565 return FAIL;
6566 if (generate_PUSHS(cctx, pat) == FAIL)
6567 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006569 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6570 return NULL;
6571
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006572 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6574 return NULL;
6575 }
6576
6577 if (generate_instr(cctx, ISN_CATCH) == NULL)
6578 return NULL;
6579
6580 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6581 return NULL;
6582 return p;
6583}
6584
6585 static char_u *
6586compile_finally(char_u *arg, cctx_T *cctx)
6587{
6588 scope_T *scope = cctx->ctx_scope;
6589 garray_T *instr = &cctx->ctx_instr;
6590 isn_T *isn;
6591
6592 // end block scope from :try or :catch
6593 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6594 compile_endblock(cctx);
6595 scope = cctx->ctx_scope;
6596
6597 // Error if not in a :try scope
6598 if (scope == NULL || scope->se_type != TRY_SCOPE)
6599 {
6600 emsg(_(e_finally));
6601 return NULL;
6602 }
6603
6604 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006605 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 if (isn->isn_arg.try.try_finally != 0)
6607 {
6608 emsg(_(e_finally_dup));
6609 return NULL;
6610 }
6611
6612 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006613 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006614
Bram Moolenaar585fea72020-04-02 22:33:21 +02006615 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006616 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 {
6618 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006619 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006621 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 }
6623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006624 // TODO: set index in ts_finally_label jumps
6625
6626 return arg;
6627}
6628
6629 static char_u *
6630compile_endtry(char_u *arg, cctx_T *cctx)
6631{
6632 scope_T *scope = cctx->ctx_scope;
6633 garray_T *instr = &cctx->ctx_instr;
6634 isn_T *isn;
6635
6636 // end block scope from :catch or :finally
6637 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6638 compile_endblock(cctx);
6639 scope = cctx->ctx_scope;
6640
6641 // Error if not in a :try scope
6642 if (scope == NULL || scope->se_type != TRY_SCOPE)
6643 {
6644 if (scope == NULL)
6645 emsg(_(e_no_endtry));
6646 else if (scope->se_type == WHILE_SCOPE)
6647 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006648 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006649 emsg(_(e_endfor));
6650 else
6651 emsg(_(e_endif));
6652 return NULL;
6653 }
6654
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006655 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6657 {
6658 emsg(_("E1032: missing :catch or :finally"));
6659 return NULL;
6660 }
6661
6662 // Fill in the "end" label in jumps at the end of the blocks, if not done
6663 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006664 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665
6666 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006667 if (isn->isn_arg.try.try_catch == 0)
6668 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669 if (isn->isn_arg.try.try_finally == 0)
6670 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006671
6672 if (scope->se_u.se_try.ts_catch_label != 0)
6673 {
6674 // Last catch without match jumps here
6675 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6676 isn->isn_arg.jump.jump_where = instr->ga_len;
6677 }
6678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006679 compile_endblock(cctx);
6680
6681 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6682 return NULL;
6683 return arg;
6684}
6685
6686/*
6687 * compile "throw {expr}"
6688 */
6689 static char_u *
6690compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6691{
6692 char_u *p = skipwhite(arg);
6693
Bram Moolenaara5565e42020-05-09 15:44:01 +02006694 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695 return NULL;
6696 if (may_generate_2STRING(-1, cctx) == FAIL)
6697 return NULL;
6698 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6699 return NULL;
6700
6701 return p;
6702}
6703
6704/*
6705 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006706 * compile "echomsg expr"
6707 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006708 * compile "execute expr"
6709 */
6710 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006711compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006712{
6713 char_u *p = arg;
6714 int count = 0;
6715
6716 for (;;)
6717 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006718 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006719 return NULL;
6720 ++count;
6721 p = skipwhite(p);
6722 if (ends_excmd(*p))
6723 break;
6724 }
6725
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006726 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6727 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6728 else if (cmdidx == CMD_execute)
6729 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6730 else if (cmdidx == CMD_echomsg)
6731 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6732 else
6733 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 return p;
6735}
6736
6737/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006738 * A command that is not compiled, execute with legacy code.
6739 */
6740 static char_u *
6741compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6742{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006743 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006744 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006745 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006746
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006747 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006748 goto theend;
6749
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006750 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006751 {
6752 long argt = excmd_get_argt(eap->cmdidx);
6753 int usefilter = FALSE;
6754
6755 has_expr = argt & (EX_XFILE | EX_EXPAND);
6756
6757 // If the command can be followed by a bar, find the bar and truncate
6758 // it, so that the following command can be compiled.
6759 // The '|' is overwritten with a NUL, it is put back below.
6760 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6761 && *eap->arg == '!')
6762 // :w !filter or :r !filter or :r! filter
6763 usefilter = TRUE;
6764 if ((argt & EX_TRLBAR) && !usefilter)
6765 {
6766 separate_nextcmd(eap);
6767 if (eap->nextcmd != NULL)
6768 nextcmd = eap->nextcmd;
6769 }
6770 }
6771
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006772 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6773 {
6774 // expand filename in "syntax include [@group] filename"
6775 has_expr = TRUE;
6776 eap->arg = skipwhite(eap->arg + 7);
6777 if (*eap->arg == '@')
6778 eap->arg = skiptowhite(eap->arg);
6779 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006780
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006781 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006782 {
6783 int count = 0;
6784 char_u *start = skipwhite(line);
6785
6786 // :cmd xxx`=expr1`yyy`=expr2`zzz
6787 // PUSHS ":cmd xxx"
6788 // eval expr1
6789 // PUSHS "yyy"
6790 // eval expr2
6791 // PUSHS "zzz"
6792 // EXECCONCAT 5
6793 for (;;)
6794 {
6795 if (p > start)
6796 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006797 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006798 ++count;
6799 }
6800 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006801 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006802 return NULL;
6803 may_generate_2STRING(-1, cctx);
6804 ++count;
6805 p = skipwhite(p);
6806 if (*p != '`')
6807 {
6808 emsg(_("E1083: missing backtick"));
6809 return NULL;
6810 }
6811 start = p + 1;
6812
6813 p = (char_u *)strstr((char *)start, "`=");
6814 if (p == NULL)
6815 {
6816 if (*skipwhite(start) != NUL)
6817 {
6818 generate_PUSHS(cctx, vim_strsave(start));
6819 ++count;
6820 }
6821 break;
6822 }
6823 }
6824 generate_EXECCONCAT(cctx, count);
6825 }
6826 else
6827 generate_EXEC(cctx, line);
6828
6829theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006830 if (*nextcmd != NUL)
6831 {
6832 // the parser expects a pointer to the bar, put it back
6833 --nextcmd;
6834 *nextcmd = '|';
6835 }
6836
6837 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006838}
6839
6840/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006841 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006842 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006843 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006844 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006845add_def_function(ufunc_T *ufunc)
6846{
6847 dfunc_T *dfunc;
6848
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006849 if (def_functions.ga_len == 0)
6850 {
6851 // The first position is not used, so that a zero uf_dfunc_idx means it
6852 // wasn't set.
6853 if (ga_grow(&def_functions, 1) == FAIL)
6854 return FAIL;
6855 ++def_functions.ga_len;
6856 }
6857
Bram Moolenaar09689a02020-05-09 22:50:08 +02006858 // Add the function to "def_functions".
6859 if (ga_grow(&def_functions, 1) == FAIL)
6860 return FAIL;
6861 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6862 CLEAR_POINTER(dfunc);
6863 dfunc->df_idx = def_functions.ga_len;
6864 ufunc->uf_dfunc_idx = dfunc->df_idx;
6865 dfunc->df_ufunc = ufunc;
6866 ++def_functions.ga_len;
6867 return OK;
6868}
6869
6870/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006871 * After ex_function() has collected all the function lines: parse and compile
6872 * the lines into instructions.
6873 * Adds the function to "def_functions".
6874 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6875 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006876 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006877 * This can be used recursively through compile_lambda(), which may reallocate
6878 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006879 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006881 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006882compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006883{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884 char_u *line = NULL;
6885 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887 cctx_T cctx;
6888 garray_T *instr;
6889 int called_emsg_before = called_emsg;
6890 int ret = FAIL;
6891 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006892 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006893 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006894
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006895 // When using a function that was compiled before: Free old instructions.
6896 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006897 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006898 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006899 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6900 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006901 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006902 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006903 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006904 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006905
Bram Moolenaar985116a2020-07-12 17:31:09 +02006906 ufunc->uf_def_status = UF_COMPILING;
6907
Bram Moolenaara80faa82020-04-12 19:37:17 +02006908 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 cctx.ctx_ufunc = ufunc;
6910 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006911 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6913 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6914 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6915 cctx.ctx_type_list = &ufunc->uf_type_list;
6916 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6917 instr = &cctx.ctx_instr;
6918
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006919 // Set the context to the function, it may be compiled when called from
6920 // another script. Set the script version to the most modern one.
6921 // The line number will be set in next_line_from_context().
6922 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6924
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006925 // Make sure error messages are OK.
6926 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6927 if (do_estack_push)
6928 estack_push_ufunc(ufunc, 1);
6929
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006930 if (ufunc->uf_def_args.ga_len > 0)
6931 {
6932 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006933 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006934 int i;
6935 char_u *arg;
6936 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6937
6938 // Produce instructions for the default values of optional arguments.
6939 // Store the instruction index in uf_def_arg_idx[] so that we know
6940 // where to start when the function is called, depending on the number
6941 // of arguments.
6942 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6943 if (ufunc->uf_def_arg_idx == NULL)
6944 goto erret;
6945 for (i = 0; i < count; ++i)
6946 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006947 garray_T *stack = &cctx.ctx_type_stack;
6948 type_T *val_type;
6949 int arg_idx = first_def_arg + i;
6950
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006951 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6952 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006953 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006954 goto erret;
6955
6956 // If no type specified use the type of the default value.
6957 // Otherwise check that the default value type matches the
6958 // specified type.
6959 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6960 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6961 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006962 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006963 == FAIL)
6964 {
6965 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6966 arg_idx + 1);
6967 goto erret;
6968 }
6969
6970 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006971 goto erret;
6972 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006973 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6974 }
6975
6976 /*
6977 * Loop over all the lines of the function and generate instructions.
6978 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 for (;;)
6980 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006981 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006982 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006983 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006984 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006985
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006986 // Bail out on the first error to avoid a flood of errors and report
6987 // the right line number when inside try/catch.
6988 if (emsg_before != called_emsg)
6989 goto erret;
6990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991 if (line != NULL && *line == '|')
6992 // the line continues after a '|'
6993 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006994 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006995 && !(*line == '#' && (line == cctx.ctx_line_start
6996 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006998 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 goto erret;
7000 }
7001 else
7002 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007003 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007004 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007005 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007006 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007008 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009
Bram Moolenaara80faa82020-04-12 19:37:17 +02007010 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 ea.cmdlinep = &line;
7012 ea.cmd = skipwhite(line);
7013
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007014 // Some things can be recognized by the first character.
7015 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007016 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007017 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007018 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007019 if (ea.cmd[1] != '{')
7020 {
7021 line = (char_u *)"";
7022 continue;
7023 }
7024 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007026 case '}':
7027 {
7028 // "}" ends a block scope
7029 scopetype_T stype = cctx.ctx_scope == NULL
7030 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007032 if (stype == BLOCK_SCOPE)
7033 {
7034 compile_endblock(&cctx);
7035 line = ea.cmd;
7036 }
7037 else
7038 {
7039 emsg(_("E1025: using } outside of a block scope"));
7040 goto erret;
7041 }
7042 if (line != NULL)
7043 line = skipwhite(ea.cmd + 1);
7044 continue;
7045 }
7046
7047 case '{':
7048 // "{" starts a block scope
7049 // "{'a': 1}->func() is something else
7050 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7051 {
7052 line = compile_block(ea.cmd, &cctx);
7053 continue;
7054 }
7055 break;
7056
7057 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007058 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007059 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 }
7061
7062 /*
7063 * COMMAND MODIFIERS
7064 */
7065 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7066 {
7067 if (errormsg != NULL)
7068 goto erret;
7069 // empty line or comment
7070 line = (char_u *)"";
7071 continue;
7072 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007073 // TODO: use modifiers in the command
7074 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007075 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076
7077 // Skip ":call" to get to the function name.
7078 if (checkforcmd(&ea.cmd, "call", 3))
7079 ea.cmd = skipwhite(ea.cmd);
7080
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007081 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007083 char_u *pskip;
7084
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007085 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007086 // find what follows.
7087 // Skip over "var.member", "var[idx]" and the like.
7088 // Also "&opt = val", "$ENV = val" and "@r = val".
7089 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007090 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007091 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007092 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007094 char_u *var_end;
7095 int oplen;
7096 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007098 var_end = find_name_end(pskip, NULL, NULL,
7099 FNE_CHECK_START | FNE_INCL_BR);
7100 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007101 if (oplen > 0)
7102 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007103 size_t len = p - ea.cmd;
7104
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007105 // Recognize an assignment if we recognize the variable
7106 // name:
7107 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007108 // "local = expr" where "local" is a local var.
7109 // "script = expr" where "script" is a script-local var.
7110 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007111 // "&opt = expr"
7112 // "$ENV = expr"
7113 // "@r = expr"
7114 if (*ea.cmd == '&'
7115 || *ea.cmd == '$'
7116 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007117 || ((len) > 2 && ea.cmd[1] == ':')
7118 || lookup_local(ea.cmd, len, &cctx) != NULL
7119 || lookup_arg(ea.cmd, len, NULL, NULL,
7120 NULL, &cctx) == OK
7121 || lookup_script(ea.cmd, len) == OK
7122 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007123 {
7124 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007125 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007126 goto erret;
7127 continue;
7128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 }
7130 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007131
7132 if (*ea.cmd == '[')
7133 {
7134 // [var, var] = expr
7135 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7136 if (line == NULL)
7137 goto erret;
7138 if (line != ea.cmd)
7139 continue;
7140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141 }
7142
7143 /*
7144 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007145 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007147 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007148 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007149 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007150 ea.cmd = skip_range(ea.cmd, NULL);
7151 if (ea.cmd > cmd && !starts_with_colon)
7152 {
7153 emsg(_(e_colon_required));
7154 goto erret;
7155 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007156 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007157 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007158 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007159 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007160
7161 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7162 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007163 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007164 {
7165 line += STRLEN(line);
7166 continue;
7167 }
7168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007170 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007172 // CMD_let cannot happen, compile_assignment() above is used
7173 iemsg("Command from find_ex_command() not handled");
7174 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007176 }
7177
7178 p = skipwhite(p);
7179
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007180 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007181 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007182 && ea.cmdidx != CMD_elseif
7183 && ea.cmdidx != CMD_else
7184 && ea.cmdidx != CMD_endif)
7185 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007186 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007187 continue;
7188 }
7189
Bram Moolenaarefd88552020-06-18 20:50:10 +02007190 if (ea.cmdidx != CMD_elseif
7191 && ea.cmdidx != CMD_else
7192 && ea.cmdidx != CMD_endif
7193 && ea.cmdidx != CMD_endfor
7194 && ea.cmdidx != CMD_endwhile
7195 && ea.cmdidx != CMD_catch
7196 && ea.cmdidx != CMD_finally
7197 && ea.cmdidx != CMD_endtry)
7198 {
7199 if (cctx.ctx_had_return)
7200 {
7201 emsg(_("E1095: Unreachable code after :return"));
7202 goto erret;
7203 }
7204 }
7205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007206 switch (ea.cmdidx)
7207 {
7208 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007209 ea.arg = p;
7210 line = compile_nested_function(&ea, &cctx);
7211 break;
7212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007213 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007214 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215 goto erret;
7216
7217 case CMD_return:
7218 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007219 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007220 break;
7221
7222 case CMD_let:
7223 case CMD_const:
7224 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007225 if (line == p)
7226 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227 break;
7228
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007229 case CMD_unlet:
7230 case CMD_unlockvar:
7231 case CMD_lockvar:
7232 line = compile_unletlock(p, &ea, &cctx);
7233 break;
7234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007235 case CMD_import:
7236 line = compile_import(p, &cctx);
7237 break;
7238
7239 case CMD_if:
7240 line = compile_if(p, &cctx);
7241 break;
7242 case CMD_elseif:
7243 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007244 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007245 break;
7246 case CMD_else:
7247 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007248 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249 break;
7250 case CMD_endif:
7251 line = compile_endif(p, &cctx);
7252 break;
7253
7254 case CMD_while:
7255 line = compile_while(p, &cctx);
7256 break;
7257 case CMD_endwhile:
7258 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007259 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260 break;
7261
7262 case CMD_for:
7263 line = compile_for(p, &cctx);
7264 break;
7265 case CMD_endfor:
7266 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007267 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007268 break;
7269 case CMD_continue:
7270 line = compile_continue(p, &cctx);
7271 break;
7272 case CMD_break:
7273 line = compile_break(p, &cctx);
7274 break;
7275
7276 case CMD_try:
7277 line = compile_try(p, &cctx);
7278 break;
7279 case CMD_catch:
7280 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007281 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007282 break;
7283 case CMD_finally:
7284 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007285 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007286 break;
7287 case CMD_endtry:
7288 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007289 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290 break;
7291 case CMD_throw:
7292 line = compile_throw(p, &cctx);
7293 break;
7294
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007295 case CMD_eval:
7296 if (compile_expr0(&p, &cctx) == FAIL)
7297 goto erret;
7298
7299 // drop the return value
7300 generate_instr_drop(&cctx, ISN_DROP, 1);
7301
7302 line = skipwhite(p);
7303 break;
7304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007307 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007308 case CMD_echomsg:
7309 case CMD_echoerr:
7310 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007311 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007313 // TODO: other commands with an expression argument
7314
Bram Moolenaar002262f2020-07-08 17:47:57 +02007315 case CMD_SIZE:
7316 semsg(_("E476: Invalid command: %s"), ea.cmd);
7317 goto erret;
7318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007320 // Not recognized, execute with do_cmdline_cmd().
7321 ea.arg = p;
7322 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323 break;
7324 }
7325 if (line == NULL)
7326 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007327 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328
7329 if (cctx.ctx_type_stack.ga_len < 0)
7330 {
7331 iemsg("Type stack underflow");
7332 goto erret;
7333 }
7334 }
7335
7336 if (cctx.ctx_scope != NULL)
7337 {
7338 if (cctx.ctx_scope->se_type == IF_SCOPE)
7339 emsg(_(e_endif));
7340 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7341 emsg(_(e_endwhile));
7342 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7343 emsg(_(e_endfor));
7344 else
7345 emsg(_("E1026: Missing }"));
7346 goto erret;
7347 }
7348
Bram Moolenaarefd88552020-06-18 20:50:10 +02007349 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 {
7351 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7352 {
7353 emsg(_("E1027: Missing return statement"));
7354 goto erret;
7355 }
7356
7357 // Return zero if there is no return at the end.
7358 generate_PUSHNR(&cctx, 0);
7359 generate_instr(&cctx, ISN_RETURN);
7360 }
7361
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007362 {
7363 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7364 + ufunc->uf_dfunc_idx;
7365 dfunc->df_deleted = FALSE;
7366 dfunc->df_instr = instr->ga_data;
7367 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007368 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007369 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007370 if (cctx.ctx_outer_used)
7371 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007372 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007373 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007374
7375 ret = OK;
7376
7377erret:
7378 if (ret == FAIL)
7379 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007380 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007381 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7382 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007383
7384 for (idx = 0; idx < instr->ga_len; ++idx)
7385 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007387
Bram Moolenaar45a15082020-05-25 00:28:33 +02007388 // if using the last entry in the table we might as well remove it
7389 if (!dfunc->df_deleted
7390 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007391 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007392 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007393
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007394 while (cctx.ctx_scope != NULL)
7395 drop_scope(&cctx);
7396
Bram Moolenaar20431c92020-03-20 18:39:46 +01007397 // Don't execute this function body.
7398 ga_clear_strings(&ufunc->uf_lines);
7399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007400 if (errormsg != NULL)
7401 emsg(errormsg);
7402 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007403 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 }
7405
7406 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007407 if (do_estack_push)
7408 estack_pop();
7409
Bram Moolenaar20431c92020-03-20 18:39:46 +01007410 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007411 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007413 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414}
7415
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007416 void
7417set_function_type(ufunc_T *ufunc)
7418{
7419 int varargs = ufunc->uf_va_name != NULL;
7420 int argcount = ufunc->uf_args.ga_len;
7421
7422 // Create a type for the function, with the return type and any
7423 // argument types.
7424 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7425 // The type is included in "tt_args".
7426 if (argcount > 0 || varargs)
7427 {
7428 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7429 argcount, &ufunc->uf_type_list);
7430 // Add argument types to the function type.
7431 if (func_type_add_arg_types(ufunc->uf_func_type,
7432 argcount + varargs,
7433 &ufunc->uf_type_list) == FAIL)
7434 return;
7435 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7436 ufunc->uf_func_type->tt_min_argcount =
7437 argcount - ufunc->uf_def_args.ga_len;
7438 if (ufunc->uf_arg_types == NULL)
7439 {
7440 int i;
7441
7442 // lambda does not have argument types.
7443 for (i = 0; i < argcount; ++i)
7444 ufunc->uf_func_type->tt_args[i] = &t_any;
7445 }
7446 else
7447 mch_memmove(ufunc->uf_func_type->tt_args,
7448 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7449 if (varargs)
7450 {
7451 ufunc->uf_func_type->tt_args[argcount] =
7452 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7453 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7454 }
7455 }
7456 else
7457 // No arguments, can use a predefined type.
7458 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7459 argcount, &ufunc->uf_type_list);
7460}
7461
7462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463/*
7464 * Delete an instruction, free what it contains.
7465 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007466 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467delete_instr(isn_T *isn)
7468{
7469 switch (isn->isn_type)
7470 {
7471 case ISN_EXEC:
7472 case ISN_LOADENV:
7473 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007474 case ISN_LOADB:
7475 case ISN_LOADW:
7476 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007478 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 case ISN_PUSHEXC:
7480 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007481 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007482 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007483 case ISN_STOREB:
7484 case ISN_STOREW:
7485 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007486 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 vim_free(isn->isn_arg.string);
7488 break;
7489
7490 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007491 case ISN_STORES:
7492 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007493 break;
7494
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007495 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007496 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007497 vim_free(isn->isn_arg.unlet.ul_name);
7498 break;
7499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007500 case ISN_STOREOPT:
7501 vim_free(isn->isn_arg.storeopt.so_name);
7502 break;
7503
7504 case ISN_PUSHBLOB: // push blob isn_arg.blob
7505 blob_unref(isn->isn_arg.blob);
7506 break;
7507
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007508 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007509#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007510 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007511#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007512 break;
7513
7514 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007515#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007516 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007517#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007518 break;
7519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007520 case ISN_UCALL:
7521 vim_free(isn->isn_arg.ufunc.cuf_name);
7522 break;
7523
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007524 case ISN_FUNCREF:
7525 {
7526 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7527 + isn->isn_arg.funcref.fr_func;
7528 func_ptr_unref(dfunc->df_ufunc);
7529 }
7530 break;
7531
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532 case ISN_2BOOL:
7533 case ISN_2STRING:
7534 case ISN_ADDBLOB:
7535 case ISN_ADDLIST:
7536 case ISN_BCALL:
7537 case ISN_CATCH:
7538 case ISN_CHECKNR:
7539 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007540 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 case ISN_COMPAREANY:
7542 case ISN_COMPAREBLOB:
7543 case ISN_COMPAREBOOL:
7544 case ISN_COMPAREDICT:
7545 case ISN_COMPAREFLOAT:
7546 case ISN_COMPAREFUNC:
7547 case ISN_COMPARELIST:
7548 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549 case ISN_COMPARESPECIAL:
7550 case ISN_COMPARESTRING:
7551 case ISN_CONCAT:
7552 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007553 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007554 case ISN_DROP:
7555 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007556 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007557 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007558 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007559 case ISN_EXECCONCAT:
7560 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007561 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007562 case ISN_LISTINDEX:
7563 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007564 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007565 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007566 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567 case ISN_JUMP:
7568 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007569 case ISN_LOADBDICT:
7570 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007571 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007573 case ISN_LOADSCRIPT:
7574 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007575 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007576 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007577 case ISN_NEGATENR:
7578 case ISN_NEWDICT:
7579 case ISN_NEWLIST:
7580 case ISN_OPNR:
7581 case ISN_OPFLOAT:
7582 case ISN_OPANY:
7583 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007584 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585 case ISN_PUSHF:
7586 case ISN_PUSHNR:
7587 case ISN_PUSHBOOL:
7588 case ISN_PUSHSPEC:
7589 case ISN_RETURN:
7590 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007591 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007592 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007593 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007594 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007595 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007596 case ISN_STOREDICT:
7597 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007598 case ISN_THROW:
7599 case ISN_TRY:
7600 // nothing allocated
7601 break;
7602 }
7603}
7604
7605/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007606 * Free all instructions for "dfunc".
7607 */
7608 static void
7609delete_def_function_contents(dfunc_T *dfunc)
7610{
7611 int idx;
7612
7613 ga_clear(&dfunc->df_def_args_isn);
7614
7615 if (dfunc->df_instr != NULL)
7616 {
7617 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7618 delete_instr(dfunc->df_instr + idx);
7619 VIM_CLEAR(dfunc->df_instr);
7620 }
7621
7622 dfunc->df_deleted = TRUE;
7623}
7624
7625/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007626 * When a user function is deleted, clear the contents of any associated def
7627 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007628 */
7629 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007630clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007632 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007633 {
7634 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7635 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007636
Bram Moolenaar20431c92020-03-20 18:39:46 +01007637 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007638 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007639 }
7640}
7641
7642#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007643/*
7644 * Free all functions defined with ":def".
7645 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007646 void
7647free_def_functions(void)
7648{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007649 int idx;
7650
7651 for (idx = 0; idx < def_functions.ga_len; ++idx)
7652 {
7653 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7654
7655 delete_def_function_contents(dfunc);
7656 }
7657
7658 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007659}
7660#endif
7661
7662
7663#endif // FEAT_EVAL