blob: 55f08a73ea83e47a8429512b5317fde49aaad418 [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 Moolenaar2f1980f2020-07-22 19:30:06 +02001884 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
1885 && STRNCMP("func", start, 4) == 0)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001886 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001887 if (*p == '(')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001888 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001889 // handle func(args): type
1890 ++p;
1891 while (*p != ')' && *p != NUL)
1892 {
1893 char_u *sp = p;
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001894
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001895 p = skip_type(p);
1896 if (p == sp)
1897 return p; // syntax error
1898 if (*p == ',')
1899 p = skipwhite(p + 1);
1900 }
1901 if (*p == ')')
1902 {
1903 if (p[1] == ':')
1904 p = skip_type(skipwhite(p + 2));
1905 else
1906 p = skipwhite(p + 1);
1907 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001908 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001909 else
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001910 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001911 // handle func: return_type
1912 p = skip_type(skipwhite(p + 1));
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001913 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001914 }
1915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 return p;
1917}
1918
1919/*
1920 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001921 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 * Returns NULL in case of failure.
1923 */
1924 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001925parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926{
1927 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001928 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929
1930 if (**arg != '<')
1931 {
1932 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001933 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 else
1935 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001936 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 }
1938 *arg = skipwhite(*arg + 1);
1939
Bram Moolenaard77a8522020-04-03 21:59:57 +02001940 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941
1942 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001943 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 {
1945 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001946 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 }
1948 ++*arg;
1949
1950 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001951 return get_list_type(member_type, type_gap);
1952 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953}
1954
1955/*
1956 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001957 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 */
1959 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001960parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961{
1962 char_u *p = *arg;
1963 size_t len;
1964
1965 // skip over the first word
1966 while (ASCII_ISALNUM(*p) || *p == '_')
1967 ++p;
1968 len = p - *arg;
1969
1970 switch (**arg)
1971 {
1972 case 'a':
1973 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1974 {
1975 *arg += len;
1976 return &t_any;
1977 }
1978 break;
1979 case 'b':
1980 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1981 {
1982 *arg += len;
1983 return &t_bool;
1984 }
1985 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1986 {
1987 *arg += len;
1988 return &t_blob;
1989 }
1990 break;
1991 case 'c':
1992 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1993 {
1994 *arg += len;
1995 return &t_channel;
1996 }
1997 break;
1998 case 'd':
1999 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
2000 {
2001 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002002 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 }
2004 break;
2005 case 'f':
2006 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
2007 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01002008#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 *arg += len;
2010 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002011#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002012 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002013 return &t_any;
2014#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 }
2016 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2017 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002018 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002019 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002020 int argcount = -1;
2021 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002022 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002023 type_T *arg_type[MAX_FUNC_ARGS + 1];
2024
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002025 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002027 if (**arg == '(')
2028 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002029 // "func" may or may not return a value, "func()" does
2030 // not return a value.
2031 ret_type = &t_void;
2032
Bram Moolenaard77a8522020-04-03 21:59:57 +02002033 p = ++*arg;
2034 argcount = 0;
2035 while (*p != NUL && *p != ')')
2036 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002037 if (*p == '?')
2038 {
2039 if (first_optional == -1)
2040 first_optional = argcount;
2041 ++p;
2042 }
2043 else if (first_optional != -1)
2044 {
2045 emsg(_("E1007: mandatory argument after optional argument"));
2046 return &t_any;
2047 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002048 else if (STRNCMP(p, "...", 3) == 0)
2049 {
2050 flags |= TTFLAG_VARARGS;
2051 p += 3;
2052 }
2053
2054 arg_type[argcount++] = parse_type(&p, type_gap);
2055
2056 // Nothing comes after "...{type}".
2057 if (flags & TTFLAG_VARARGS)
2058 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002059
Bram Moolenaard77a8522020-04-03 21:59:57 +02002060 if (*p != ',' && *skipwhite(p) == ',')
2061 {
2062 semsg(_(e_no_white_before), ",");
2063 return &t_any;
2064 }
2065 if (*p == ',')
2066 {
2067 ++p;
2068 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002069 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002070 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002071 return &t_any;
2072 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002073 }
2074 p = skipwhite(p);
2075 if (argcount == MAX_FUNC_ARGS)
2076 {
2077 emsg(_("E740: Too many argument types"));
2078 return &t_any;
2079 }
2080 }
2081
2082 p = skipwhite(p);
2083 if (*p != ')')
2084 {
2085 emsg(_(e_missing_close));
2086 return &t_any;
2087 }
2088 *arg = p + 1;
2089 }
2090 if (**arg == ':')
2091 {
2092 // parse return type
2093 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002094 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002095 semsg(_(e_white_after), ":");
2096 *arg = skipwhite(*arg);
2097 ret_type = parse_type(arg, type_gap);
2098 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002099 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002100 type = get_func_type(ret_type, argcount, type_gap);
2101 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002102 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002103 type = alloc_func_type(ret_type, argcount, type_gap);
2104 type->tt_flags = flags;
2105 if (argcount > 0)
2106 {
2107 type->tt_argcount = argcount;
2108 type->tt_min_argcount = first_optional == -1
2109 ? argcount : first_optional;
2110 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002111 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002112 return &t_any;
2113 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002114 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002115 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002116 }
2117 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 }
2119 break;
2120 case 'j':
2121 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2122 {
2123 *arg += len;
2124 return &t_job;
2125 }
2126 break;
2127 case 'l':
2128 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2129 {
2130 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002131 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 }
2133 break;
2134 case 'n':
2135 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2136 {
2137 *arg += len;
2138 return &t_number;
2139 }
2140 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 case 's':
2142 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2143 {
2144 *arg += len;
2145 return &t_string;
2146 }
2147 break;
2148 case 'v':
2149 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2150 {
2151 *arg += len;
2152 return &t_void;
2153 }
2154 break;
2155 }
2156
2157 semsg(_("E1010: Type not recognized: %s"), *arg);
2158 return &t_any;
2159}
2160
2161/*
2162 * Check if "type1" and "type2" are exactly the same.
2163 */
2164 static int
2165equal_type(type_T *type1, type_T *type2)
2166{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002167 int i;
2168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 if (type1->tt_type != type2->tt_type)
2170 return FALSE;
2171 switch (type1->tt_type)
2172 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002174 case VAR_ANY:
2175 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 case VAR_SPECIAL:
2177 case VAR_BOOL:
2178 case VAR_NUMBER:
2179 case VAR_FLOAT:
2180 case VAR_STRING:
2181 case VAR_BLOB:
2182 case VAR_JOB:
2183 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002184 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 case VAR_LIST:
2186 case VAR_DICT:
2187 return equal_type(type1->tt_member, type2->tt_member);
2188 case VAR_FUNC:
2189 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002190 if (!equal_type(type1->tt_member, type2->tt_member)
2191 || type1->tt_argcount != type2->tt_argcount)
2192 return FALSE;
2193 if (type1->tt_argcount < 0
2194 || type1->tt_args == NULL || type2->tt_args == NULL)
2195 return TRUE;
2196 for (i = 0; i < type1->tt_argcount; ++i)
2197 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2198 return FALSE;
2199 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 }
2201 return TRUE;
2202}
2203
2204/*
2205 * Find the common type of "type1" and "type2" and put it in "dest".
2206 * "type2" and "dest" may be the same.
2207 */
2208 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002209common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210{
2211 if (equal_type(type1, type2))
2212 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002213 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 return;
2215 }
2216
2217 if (type1->tt_type == type2->tt_type)
2218 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2220 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002221 type_T *common;
2222
Bram Moolenaard77a8522020-04-03 21:59:57 +02002223 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002224 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002225 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002226 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002227 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 return;
2229 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002230 if (type1->tt_type == VAR_FUNC)
2231 {
2232 type_T *common;
2233
2234 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2235 if (type1->tt_argcount == type2->tt_argcount
2236 && type1->tt_argcount >= 0)
2237 {
2238 int argcount = type1->tt_argcount;
2239 int i;
2240
2241 *dest = alloc_func_type(common, argcount, type_gap);
2242 if (type1->tt_args != NULL && type2->tt_args != NULL)
2243 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002244 if (func_type_add_arg_types(*dest, argcount,
2245 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002246 for (i = 0; i < argcount; ++i)
2247 common_type(type1->tt_args[i], type2->tt_args[i],
2248 &(*dest)->tt_args[i], type_gap);
2249 }
2250 }
2251 else
2252 *dest = alloc_func_type(common, -1, type_gap);
2253 return;
2254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 }
2256
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002257 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258}
2259
2260 char *
2261vartype_name(vartype_T type)
2262{
2263 switch (type)
2264 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002265 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002266 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 case VAR_SPECIAL: return "special";
2269 case VAR_BOOL: return "bool";
2270 case VAR_NUMBER: return "number";
2271 case VAR_FLOAT: return "float";
2272 case VAR_STRING: return "string";
2273 case VAR_BLOB: return "blob";
2274 case VAR_JOB: return "job";
2275 case VAR_CHANNEL: return "channel";
2276 case VAR_LIST: return "list";
2277 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002278
2279 case VAR_FUNC:
2280 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002282 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283}
2284
2285/*
2286 * Return the name of a type.
2287 * The result may be in allocated memory, in which case "tofree" is set.
2288 */
2289 char *
2290type_name(type_T *type, char **tofree)
2291{
2292 char *name = vartype_name(type->tt_type);
2293
2294 *tofree = NULL;
2295 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2296 {
2297 char *member_free;
2298 char *member_name = type_name(type->tt_member, &member_free);
2299 size_t len;
2300
2301 len = STRLEN(name) + STRLEN(member_name) + 3;
2302 *tofree = alloc(len);
2303 if (*tofree != NULL)
2304 {
2305 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2306 vim_free(member_free);
2307 return *tofree;
2308 }
2309 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002310 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002311 {
2312 garray_T ga;
2313 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002314 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002315
2316 ga_init2(&ga, 1, 100);
2317 if (ga_grow(&ga, 20) == FAIL)
2318 return "[unknown]";
2319 *tofree = ga.ga_data;
2320 STRCPY(ga.ga_data, "func(");
2321 ga.ga_len += 5;
2322
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002323 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002324 {
2325 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002326 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002327 int len;
2328
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002329 if (type->tt_args == NULL)
2330 arg_type = "[unknown]";
2331 else
2332 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002333 if (i > 0)
2334 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002335 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002336 ga.ga_len += 2;
2337 }
2338 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002339 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002340 {
2341 vim_free(arg_free);
2342 return "[unknown]";
2343 }
2344 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002345 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002346 {
2347 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2348 ga.ga_len += 3;
2349 }
2350 else if (i >= type->tt_min_argcount)
2351 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002352 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002353 ga.ga_len += len;
2354 vim_free(arg_free);
2355 }
2356
2357 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002358 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002359 else
2360 {
2361 char *ret_free;
2362 char *ret_name = type_name(type->tt_member, &ret_free);
2363 int len;
2364
2365 len = (int)STRLEN(ret_name) + 4;
2366 if (ga_grow(&ga, len) == FAIL)
2367 {
2368 vim_free(ret_free);
2369 return "[unknown]";
2370 }
2371 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002372 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2373 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002374 vim_free(ret_free);
2375 }
2376 return ga.ga_data;
2377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378
2379 return name;
2380}
2381
2382/*
2383 * Find "name" in script-local items of script "sid".
2384 * Returns the index in "sn_var_vals" if found.
2385 * If found but not in "sn_var_vals" returns -1.
2386 * If not found returns -2.
2387 */
2388 int
2389get_script_item_idx(int sid, char_u *name, int check_writable)
2390{
2391 hashtab_T *ht;
2392 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002393 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 int idx;
2395
2396 // First look the name up in the hashtable.
2397 if (sid <= 0 || sid > script_items.ga_len)
2398 return -1;
2399 ht = &SCRIPT_VARS(sid);
2400 di = find_var_in_ht(ht, 0, name, TRUE);
2401 if (di == NULL)
2402 return -2;
2403
2404 // Now find the svar_T index in sn_var_vals.
2405 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2406 {
2407 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2408
2409 if (sv->sv_tv == &di->di_tv)
2410 {
2411 if (check_writable && sv->sv_const)
2412 semsg(_(e_readonlyvar), name);
2413 return idx;
2414 }
2415 }
2416 return -1;
2417}
2418
2419/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002420 * Find "name" in imported items of the current script or in "cctx" if not
2421 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 */
2423 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002424find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002426 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 int idx;
2428
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002429 if (current_sctx.sc_sid <= 0)
2430 return NULL;
2431 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 if (cctx != NULL)
2433 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2434 {
2435 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2436 + idx;
2437
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002438 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2439 : STRLEN(import->imp_name) == len
2440 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 return import;
2442 }
2443
2444 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2445 {
2446 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2447
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002448 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2449 : STRLEN(import->imp_name) == len
2450 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 return import;
2452 }
2453 return NULL;
2454}
2455
2456/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002457 * Free all imported variables.
2458 */
2459 static void
2460free_imported(cctx_T *cctx)
2461{
2462 int idx;
2463
2464 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2465 {
2466 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2467
2468 vim_free(import->imp_name);
2469 }
2470 ga_clear(&cctx->ctx_imports);
2471}
2472
2473/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002474 * Return TRUE if "p" points at a "#" but not at "#{".
2475 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002476 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002477vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002478{
2479 return p[0] == '#' && p[1] != '{';
2480}
2481
2482/*
2483 * Return a pointer to the next line that isn't empty or only contains a
2484 * comment. Skips over white space.
2485 * Returns NULL if there is none.
2486 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002487 char_u *
2488peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002489{
2490 int lnum = cctx->ctx_lnum;
2491
2492 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2493 {
2494 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002495 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002496
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002497 if (line == NULL)
2498 break;
2499 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002500 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002501 return p;
2502 }
2503 return NULL;
2504}
2505
2506/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002507 * Called when checking for a following operator at "arg". When the rest of
2508 * the line is empty or only a comment, peek the next line. If there is a next
2509 * line return a pointer to it and set "nextp".
2510 * Otherwise skip over white space.
2511 */
2512 static char_u *
2513may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2514{
2515 char_u *p = skipwhite(arg);
2516
2517 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002518 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002519 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002520 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002521 if (*nextp != NULL)
2522 return *nextp;
2523 }
2524 return p;
2525}
2526
2527/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002528 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002529 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002530 * Returns NULL when at the end.
2531 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002532 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002533next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002534{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002535 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002536
2537 do
2538 {
2539 ++cctx->ctx_lnum;
2540 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002541 {
2542 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002543 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002544 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002545 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002546 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002547 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002548 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002549 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002550 return line;
2551}
2552
2553/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002554 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002555 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002556 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2557 */
2558 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002559may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002560{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002561 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002562 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002563 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002564
2565 if (next == NULL)
2566 return FAIL;
2567 *arg = skipwhite(next);
2568 }
2569 return OK;
2570}
2571
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002572/*
2573 * Idem, and give an error when failed.
2574 */
2575 static int
2576may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2577{
2578 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2579 {
2580 emsg(_("E1097: line incomplete"));
2581 return FAIL;
2582 }
2583 return OK;
2584}
2585
2586
Bram Moolenaara5565e42020-05-09 15:44:01 +02002587// Structure passed between the compile_expr* functions to keep track of
2588// constants that have been parsed but for which no code was produced yet. If
2589// possible expressions on these constants are applied at compile time. If
2590// that is not possible, the code to push the constants needs to be generated
2591// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002592// Using 50 should be more than enough of 5 levels of ().
2593#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002594typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002595 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002596 int pp_used; // active entries in pp_tv[]
2597} ppconst_T;
2598
Bram Moolenaar1c747212020-05-09 18:28:34 +02002599static int compile_expr0(char_u **arg, cctx_T *cctx);
2600static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2601
Bram Moolenaara5565e42020-05-09 15:44:01 +02002602/*
2603 * Generate a PUSH instruction for "tv".
2604 * "tv" will be consumed or cleared.
2605 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2606 */
2607 static int
2608generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2609{
2610 if (tv != NULL)
2611 {
2612 switch (tv->v_type)
2613 {
2614 case VAR_UNKNOWN:
2615 break;
2616 case VAR_BOOL:
2617 generate_PUSHBOOL(cctx, tv->vval.v_number);
2618 break;
2619 case VAR_SPECIAL:
2620 generate_PUSHSPEC(cctx, tv->vval.v_number);
2621 break;
2622 case VAR_NUMBER:
2623 generate_PUSHNR(cctx, tv->vval.v_number);
2624 break;
2625#ifdef FEAT_FLOAT
2626 case VAR_FLOAT:
2627 generate_PUSHF(cctx, tv->vval.v_float);
2628 break;
2629#endif
2630 case VAR_BLOB:
2631 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2632 tv->vval.v_blob = NULL;
2633 break;
2634 case VAR_STRING:
2635 generate_PUSHS(cctx, tv->vval.v_string);
2636 tv->vval.v_string = NULL;
2637 break;
2638 default:
2639 iemsg("constant type not supported");
2640 clear_tv(tv);
2641 return FAIL;
2642 }
2643 tv->v_type = VAR_UNKNOWN;
2644 }
2645 return OK;
2646}
2647
2648/*
2649 * Generate code for any ppconst entries.
2650 */
2651 static int
2652generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2653{
2654 int i;
2655 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002656 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002657
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002658 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002659 for (i = 0; i < ppconst->pp_used; ++i)
2660 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2661 ret = FAIL;
2662 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002663 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002664 return ret;
2665}
2666
2667/*
2668 * Clear ppconst constants. Used when failing.
2669 */
2670 static void
2671clear_ppconst(ppconst_T *ppconst)
2672{
2673 int i;
2674
2675 for (i = 0; i < ppconst->pp_used; ++i)
2676 clear_tv(&ppconst->pp_tv[i]);
2677 ppconst->pp_used = 0;
2678}
2679
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002680/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002681 * Generate an instruction to load script-local variable "name", without the
2682 * leading "s:".
2683 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 */
2685 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002686compile_load_scriptvar(
2687 cctx_T *cctx,
2688 char_u *name, // variable NUL terminated
2689 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002690 char_u **end, // end of variable
2691 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002693 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2695 imported_T *import;
2696
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002697 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002699 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002700 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2701 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 }
2703 if (idx >= 0)
2704 {
2705 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2706
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002707 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 current_sctx.sc_sid, idx, sv->sv_type);
2709 return OK;
2710 }
2711
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002712 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 if (import != NULL)
2714 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002715 if (import->imp_all)
2716 {
2717 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002718 char_u *exp_name;
2719 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002720 ufunc_T *ufunc;
2721 type_T *type;
2722
2723 // Used "import * as Name", need to lookup the member.
2724 if (*p != '.')
2725 {
2726 semsg(_("E1060: expected dot after name: %s"), start);
2727 return FAIL;
2728 }
2729 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002730 if (VIM_ISWHITE(*p))
2731 {
2732 emsg(_("E1074: no white space allowed after dot"));
2733 return FAIL;
2734 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002735
Bram Moolenaar1c991142020-07-04 13:15:31 +02002736 // isolate one name
2737 exp_name = p;
2738 while (eval_isnamec(*p))
2739 ++p;
2740 cc = *p;
2741 *p = NUL;
2742
2743 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2744 *p = cc;
2745 p = skipwhite(p);
2746
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002747 // TODO: what if it is a function?
2748 if (idx < 0)
2749 return FAIL;
2750 *end = p;
2751
2752 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2753 import->imp_sid,
2754 idx,
2755 type);
2756 }
2757 else
2758 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002759 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002760 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2761 import->imp_sid,
2762 import->imp_var_vals_idx,
2763 import->imp_type);
2764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 return OK;
2766 }
2767
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002768 if (error)
2769 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 return FAIL;
2771}
2772
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002773 static int
2774generate_funcref(cctx_T *cctx, char_u *name)
2775{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002776 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002777
2778 if (ufunc == NULL)
2779 return FAIL;
2780
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002781 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2782 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002783}
2784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785/*
2786 * Compile a variable name into a load instruction.
2787 * "end" points to just after the name.
2788 * When "error" is FALSE do not give an error when not found.
2789 */
2790 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002791compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792{
2793 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002794 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002795 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002797 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798
2799 if (*(*arg + 1) == ':')
2800 {
2801 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002802 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002803 {
2804 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002806 switch (**arg)
2807 {
2808 case 'g': isn_type = ISN_LOADGDICT; break;
2809 case 'w': isn_type = ISN_LOADWDICT; break;
2810 case 't': isn_type = ISN_LOADTDICT; break;
2811 case 'b': isn_type = ISN_LOADBDICT; break;
2812 default:
2813 semsg(_(e_namespace), *arg);
2814 goto theend;
2815 }
2816 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2817 goto theend;
2818 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002819 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 else
2821 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002822 isntype_T isn_type = ISN_DROP;
2823
2824 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2825 if (name == NULL)
2826 return FAIL;
2827
2828 switch (**arg)
2829 {
2830 case 'v': res = generate_LOADV(cctx, name, error);
2831 break;
2832 case 's': res = compile_load_scriptvar(cctx, name,
2833 NULL, NULL, error);
2834 break;
2835 case 'g': isn_type = ISN_LOADG; break;
2836 case 'w': isn_type = ISN_LOADW; break;
2837 case 't': isn_type = ISN_LOADT; break;
2838 case 'b': isn_type = ISN_LOADB; break;
2839 default: semsg(_(e_namespace), *arg);
2840 goto theend;
2841 }
2842 if (isn_type != ISN_DROP)
2843 {
2844 // Global, Buffer-local, Window-local and Tabpage-local
2845 // variables can be defined later, thus we don't check if it
2846 // exists, give error at runtime.
2847 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 }
2850 }
2851 else
2852 {
2853 size_t len = end - *arg;
2854 int idx;
2855 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002856 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857
2858 name = vim_strnsave(*arg, end - *arg);
2859 if (name == NULL)
2860 return FAIL;
2861
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002862 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002864 if (!gen_load_outer)
2865 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 }
2867 else
2868 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002869 lvar_T *lvar = lookup_local(*arg, len, cctx);
2870
2871 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002873 type = lvar->lv_type;
2874 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002875 if (lvar->lv_from_outer)
2876 gen_load_outer = TRUE;
2877 else
2878 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 }
2880 else
2881 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002882 // "var" can be script-local even without using "s:" if it
2883 // already exists.
2884 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2885 == SCRIPT_VERSION_VIM9
2886 || lookup_script(*arg, len) == OK)
2887 res = compile_load_scriptvar(cctx, name, *arg, &end,
2888 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002889
Bram Moolenaara5565e42020-05-09 15:44:01 +02002890 // When the name starts with an uppercase letter or "x:" it
2891 // can be a user defined function.
2892 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2893 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 }
2895 }
2896 if (gen_load)
2897 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002898 if (gen_load_outer)
2899 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 }
2901
2902 *arg = end;
2903
2904theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002905 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 semsg(_(e_var_notfound), name);
2907 vim_free(name);
2908 return res;
2909}
2910
2911/*
2912 * Compile the argument expressions.
2913 * "arg" points to just after the "(" and is advanced to after the ")"
2914 */
2915 static int
2916compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2917{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002918 char_u *p = *arg;
2919 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920
Bram Moolenaare6085c52020-04-12 20:19:16 +02002921 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002923 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2924 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002925 if (*p == ')')
2926 {
2927 *arg = p + 1;
2928 return OK;
2929 }
2930
Bram Moolenaara5565e42020-05-09 15:44:01 +02002931 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 return FAIL;
2933 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002934
2935 if (*p != ',' && *skipwhite(p) == ',')
2936 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002937 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002938 p = skipwhite(p);
2939 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002941 {
2942 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002943 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002944 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002945 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002946 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002947 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002949failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002950 emsg(_(e_missing_close));
2951 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952}
2953
2954/*
2955 * Compile a function call: name(arg1, arg2)
2956 * "arg" points to "name", "arg + varlen" to the "(".
2957 * "argcount_init" is 1 for "value->method()"
2958 * Instructions:
2959 * EVAL arg1
2960 * EVAL arg2
2961 * BCALL / DCALL / UCALL
2962 */
2963 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002964compile_call(
2965 char_u **arg,
2966 size_t varlen,
2967 cctx_T *cctx,
2968 ppconst_T *ppconst,
2969 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970{
2971 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002972 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 int argcount = argcount_init;
2974 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002975 char_u fname_buf[FLEN_FIXED + 1];
2976 char_u *tofree = NULL;
2977 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002979 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980
Bram Moolenaara5565e42020-05-09 15:44:01 +02002981 // we can evaluate "has('name')" at compile time
2982 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2983 {
2984 char_u *s = skipwhite(*arg + varlen + 1);
2985 typval_T argvars[2];
2986
2987 argvars[0].v_type = VAR_UNKNOWN;
2988 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002989 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002990 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002991 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002992 s = skipwhite(s);
2993 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2994 {
2995 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2996
2997 *arg = s + 1;
2998 argvars[1].v_type = VAR_UNKNOWN;
2999 tv->v_type = VAR_NUMBER;
3000 tv->vval.v_number = 0;
3001 f_has(argvars, tv);
3002 clear_tv(&argvars[0]);
3003 ++ppconst->pp_used;
3004 return OK;
3005 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003006 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003007 }
3008
3009 if (generate_ppconst(cctx, ppconst) == FAIL)
3010 return FAIL;
3011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 if (varlen >= sizeof(namebuf))
3013 {
3014 semsg(_("E1011: name too long: %s"), name);
3015 return FAIL;
3016 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003017 vim_strncpy(namebuf, *arg, varlen);
3018 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019
3020 *arg = skipwhite(*arg + varlen + 1);
3021 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003022 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003024 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 {
3026 int idx;
3027
3028 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003029 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003031 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003032 else
3033 semsg(_(e_unknownfunc), namebuf);
3034 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 }
3036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003038 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003040 {
3041 res = generate_CALL(cctx, ufunc, argcount);
3042 goto theend;
3043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044
3045 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003046 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003048 if (STRNCMP(namebuf, "g:", 2) != 0
3049 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003050 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003051 garray_T *stack = &cctx->ctx_type_stack;
3052 type_T *type;
3053
3054 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3055 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003056 goto theend;
3057 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003059 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003060 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003061 if (STRNCMP(namebuf, "g:", 2) == 0)
3062 res = generate_UCALL(cctx, name, argcount);
3063 else
3064 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003065
3066theend:
3067 vim_free(tofree);
3068 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069}
3070
3071// like NAMESPACE_CHAR but with 'a' and 'l'.
3072#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3073
3074/*
3075 * Find the end of a variable or function name. Unlike find_name_end() this
3076 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003077 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 * Return a pointer to just after the name. Equal to "arg" if there is no
3079 * valid name.
3080 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003081 static char_u *
3082to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083{
3084 char_u *p;
3085
3086 // Quick check for valid starting character.
3087 if (!eval_isnamec1(*arg))
3088 return arg;
3089
3090 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3091 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3092 // and can be used in slice "[n:]".
3093 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003094 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3096 break;
3097 return p;
3098}
3099
3100/*
3101 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003102 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 */
3104 char_u *
3105to_name_const_end(char_u *arg)
3106{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003107 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 typval_T rettv;
3109
3110 if (p == arg && *arg == '[')
3111 {
3112
3113 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003114 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 p = arg;
3116 }
3117 else if (p == arg && *arg == '#' && arg[1] == '{')
3118 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003119 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003121 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 p = arg;
3123 }
3124 else if (p == arg && *arg == '{')
3125 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003126 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003128 // Can be "{x -> ret}()".
3129 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003131 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 if (ret != OK)
3133 p = arg;
3134 }
3135
3136 return p;
3137}
3138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139/*
3140 * parse a list: [expr, expr]
3141 * "*arg" points to the '['.
3142 */
3143 static int
3144compile_list(char_u **arg, cctx_T *cctx)
3145{
3146 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003147 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 int count = 0;
3149
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003150 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003152 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003153 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003154 semsg(_(e_list_end), *arg);
3155 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003156 }
3157 if (*p == ']')
3158 {
3159 ++p;
3160 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003161 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003162 p += STRLEN(p);
3163 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003164 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003165 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 break;
3167 ++count;
3168 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003169 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003171 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3172 {
3173 semsg(_(e_white_after), ",");
3174 return FAIL;
3175 }
3176 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003177 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 p = skipwhite(p);
3179 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003180 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181
3182 generate_NEWLIST(cctx, count);
3183 return OK;
3184}
3185
3186/*
3187 * parse a lambda: {arg, arg -> expr}
3188 * "*arg" points to the '{'.
3189 */
3190 static int
3191compile_lambda(char_u **arg, cctx_T *cctx)
3192{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 typval_T rettv;
3194 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003195 evalarg_T evalarg;
3196
3197 CLEAR_FIELD(evalarg);
3198 evalarg.eval_flags = EVAL_EVALUATE;
3199 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200
3201 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003202 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003206 ++ufunc->uf_refcount;
3207 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003208 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209
3210 // The function will have one line: "return {expr}".
3211 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003212 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003214 clear_evalarg(&evalarg, NULL);
3215
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003216 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003217 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003218
3219 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 return FAIL;
3221}
3222
3223/*
3224 * Compile a lamda call: expr->{lambda}(args)
3225 * "arg" points to the "{".
3226 */
3227 static int
3228compile_lambda_call(char_u **arg, cctx_T *cctx)
3229{
3230 ufunc_T *ufunc;
3231 typval_T rettv;
3232 int argcount = 1;
3233 int ret = FAIL;
3234
3235 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003236 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 return FAIL;
3238
3239 if (**arg != '(')
3240 {
3241 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003242 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 else
3244 semsg(_(e_missing_paren), "lambda");
3245 clear_tv(&rettv);
3246 return FAIL;
3247 }
3248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 ufunc = rettv.vval.v_partial->pt_func;
3250 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003251 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003252 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003253
3254 // The function will have one line: "return {expr}".
3255 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003256 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257
3258 // compile the arguments
3259 *arg = skipwhite(*arg + 1);
3260 if (compile_arguments(arg, cctx, &argcount) == OK)
3261 // call the compiled function
3262 ret = generate_CALL(cctx, ufunc, argcount);
3263
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003264 if (ret == FAIL)
3265 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 return ret;
3267}
3268
3269/*
3270 * parse a dict: {'key': val} or #{key: val}
3271 * "*arg" points to the '{'.
3272 */
3273 static int
3274compile_dict(char_u **arg, cctx_T *cctx, int literal)
3275{
3276 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003277 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 int count = 0;
3279 dict_T *d = dict_alloc();
3280 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003281 char_u *whitep = *arg;
3282 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283
3284 if (d == NULL)
3285 return FAIL;
3286 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003287 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 {
3289 char_u *key = NULL;
3290
Bram Moolenaar23c55272020-06-21 16:58:13 +02003291 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003292 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003293 *arg = NULL;
3294 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003295 }
3296
3297 if (**arg == '}')
3298 break;
3299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 if (literal)
3301 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003302 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303
Bram Moolenaar2c330432020-04-13 14:41:35 +02003304 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 {
3306 semsg(_("E1014: Invalid key: %s"), *arg);
3307 return FAIL;
3308 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003309 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 if (generate_PUSHS(cctx, key) == FAIL)
3311 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003312 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 }
3314 else
3315 {
3316 isn_T *isn;
3317
Bram Moolenaara5565e42020-05-09 15:44:01 +02003318 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3321 if (isn->isn_type == ISN_PUSHS)
3322 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003323 else
3324 {
3325 type_T *keytype = ((type_T **)stack->ga_data)
3326 [stack->ga_len - 1];
3327 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3328 return FAIL;
3329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 }
3331
3332 // Check for duplicate keys, if using string keys.
3333 if (key != NULL)
3334 {
3335 item = dict_find(d, key, -1);
3336 if (item != NULL)
3337 {
3338 semsg(_(e_duplicate_key), key);
3339 goto failret;
3340 }
3341 item = dictitem_alloc(key);
3342 if (item != NULL)
3343 {
3344 item->di_tv.v_type = VAR_UNKNOWN;
3345 item->di_tv.v_lock = 0;
3346 if (dict_add(d, item) == FAIL)
3347 dictitem_free(item);
3348 }
3349 }
3350
3351 *arg = skipwhite(*arg);
3352 if (**arg != ':')
3353 {
3354 semsg(_(e_missing_dict_colon), *arg);
3355 return FAIL;
3356 }
3357
Bram Moolenaar2c330432020-04-13 14:41:35 +02003358 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003360 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003361 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003362 *arg = NULL;
3363 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003364 }
3365
Bram Moolenaara5565e42020-05-09 15:44:01 +02003366 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 return FAIL;
3368 ++count;
3369
Bram Moolenaar2c330432020-04-13 14:41:35 +02003370 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003371 *arg = skipwhite(*arg);
3372 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003373 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003374 *arg = NULL;
3375 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 if (**arg == '}')
3378 break;
3379 if (**arg != ',')
3380 {
3381 semsg(_(e_missing_dict_comma), *arg);
3382 goto failret;
3383 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003384 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 *arg = skipwhite(*arg + 1);
3386 }
3387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 *arg = *arg + 1;
3389
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003390 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003391 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003392 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003393 *arg += STRLEN(*arg);
3394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 dict_unref(d);
3396 return generate_NEWDICT(cctx, count);
3397
3398failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003399 if (*arg == NULL)
3400 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 dict_unref(d);
3402 return FAIL;
3403}
3404
3405/*
3406 * Compile "&option".
3407 */
3408 static int
3409compile_get_option(char_u **arg, cctx_T *cctx)
3410{
3411 typval_T rettv;
3412 char_u *start = *arg;
3413 int ret;
3414
3415 // parse the option and get the current value to get the type.
3416 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003417 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 if (ret == OK)
3419 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003420 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 char_u *name = vim_strnsave(start, *arg - start);
3422 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3423
3424 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3425 vim_free(name);
3426 }
3427 clear_tv(&rettv);
3428
3429 return ret;
3430}
3431
3432/*
3433 * Compile "$VAR".
3434 */
3435 static int
3436compile_get_env(char_u **arg, cctx_T *cctx)
3437{
3438 char_u *start = *arg;
3439 int len;
3440 int ret;
3441 char_u *name;
3442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 ++*arg;
3444 len = get_env_len(arg);
3445 if (len == 0)
3446 {
3447 semsg(_(e_syntax_at), start - 1);
3448 return FAIL;
3449 }
3450
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003451 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 name = vim_strnsave(start, len + 1);
3453 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3454 vim_free(name);
3455 return ret;
3456}
3457
3458/*
3459 * Compile "@r".
3460 */
3461 static int
3462compile_get_register(char_u **arg, cctx_T *cctx)
3463{
3464 int ret;
3465
3466 ++*arg;
3467 if (**arg == NUL)
3468 {
3469 semsg(_(e_syntax_at), *arg - 1);
3470 return FAIL;
3471 }
3472 if (!valid_yank_reg(**arg, TRUE))
3473 {
3474 emsg_invreg(**arg);
3475 return FAIL;
3476 }
3477 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3478 ++*arg;
3479 return ret;
3480}
3481
3482/*
3483 * Apply leading '!', '-' and '+' to constant "rettv".
3484 */
3485 static int
3486apply_leader(typval_T *rettv, char_u *start, char_u *end)
3487{
3488 char_u *p = end;
3489
3490 // this works from end to start
3491 while (p > start)
3492 {
3493 --p;
3494 if (*p == '-' || *p == '+')
3495 {
3496 // only '-' has an effect, for '+' we only check the type
3497#ifdef FEAT_FLOAT
3498 if (rettv->v_type == VAR_FLOAT)
3499 {
3500 if (*p == '-')
3501 rettv->vval.v_float = -rettv->vval.v_float;
3502 }
3503 else
3504#endif
3505 {
3506 varnumber_T val;
3507 int error = FALSE;
3508
3509 // tv_get_number_chk() accepts a string, but we don't want that
3510 // here
3511 if (check_not_string(rettv) == FAIL)
3512 return FAIL;
3513 val = tv_get_number_chk(rettv, &error);
3514 clear_tv(rettv);
3515 if (error)
3516 return FAIL;
3517 if (*p == '-')
3518 val = -val;
3519 rettv->v_type = VAR_NUMBER;
3520 rettv->vval.v_number = val;
3521 }
3522 }
3523 else
3524 {
3525 int v = tv2bool(rettv);
3526
3527 // '!' is permissive in the type.
3528 clear_tv(rettv);
3529 rettv->v_type = VAR_BOOL;
3530 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3531 }
3532 }
3533 return OK;
3534}
3535
3536/*
3537 * Recognize v: variables that are constants and set "rettv".
3538 */
3539 static void
3540get_vim_constant(char_u **arg, typval_T *rettv)
3541{
3542 if (STRNCMP(*arg, "v:true", 6) == 0)
3543 {
3544 rettv->v_type = VAR_BOOL;
3545 rettv->vval.v_number = VVAL_TRUE;
3546 *arg += 6;
3547 }
3548 else if (STRNCMP(*arg, "v:false", 7) == 0)
3549 {
3550 rettv->v_type = VAR_BOOL;
3551 rettv->vval.v_number = VVAL_FALSE;
3552 *arg += 7;
3553 }
3554 else if (STRNCMP(*arg, "v:null", 6) == 0)
3555 {
3556 rettv->v_type = VAR_SPECIAL;
3557 rettv->vval.v_number = VVAL_NULL;
3558 *arg += 6;
3559 }
3560 else if (STRNCMP(*arg, "v:none", 6) == 0)
3561 {
3562 rettv->v_type = VAR_SPECIAL;
3563 rettv->vval.v_number = VVAL_NONE;
3564 *arg += 6;
3565 }
3566}
3567
Bram Moolenaar61a89812020-05-07 16:58:17 +02003568 static exptype_T
3569get_compare_type(char_u *p, int *len, int *type_is)
3570{
3571 exptype_T type = EXPR_UNKNOWN;
3572 int i;
3573
3574 switch (p[0])
3575 {
3576 case '=': if (p[1] == '=')
3577 type = EXPR_EQUAL;
3578 else if (p[1] == '~')
3579 type = EXPR_MATCH;
3580 break;
3581 case '!': if (p[1] == '=')
3582 type = EXPR_NEQUAL;
3583 else if (p[1] == '~')
3584 type = EXPR_NOMATCH;
3585 break;
3586 case '>': if (p[1] != '=')
3587 {
3588 type = EXPR_GREATER;
3589 *len = 1;
3590 }
3591 else
3592 type = EXPR_GEQUAL;
3593 break;
3594 case '<': if (p[1] != '=')
3595 {
3596 type = EXPR_SMALLER;
3597 *len = 1;
3598 }
3599 else
3600 type = EXPR_SEQUAL;
3601 break;
3602 case 'i': if (p[1] == 's')
3603 {
3604 // "is" and "isnot"; but not a prefix of a name
3605 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3606 *len = 5;
3607 i = p[*len];
3608 if (!isalnum(i) && i != '_')
3609 {
3610 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3611 *type_is = TRUE;
3612 }
3613 }
3614 break;
3615 }
3616 return type;
3617}
3618
3619/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 * Compile code to apply '-', '+' and '!'.
3621 */
3622 static int
3623compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3624{
3625 char_u *p = end;
3626
3627 // this works from end to start
3628 while (p > start)
3629 {
3630 --p;
3631 if (*p == '-' || *p == '+')
3632 {
3633 int negate = *p == '-';
3634 isn_T *isn;
3635
3636 // TODO: check type
3637 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3638 {
3639 --p;
3640 if (*p == '-')
3641 negate = !negate;
3642 }
3643 // only '-' has an effect, for '+' we only check the type
3644 if (negate)
3645 isn = generate_instr(cctx, ISN_NEGATENR);
3646 else
3647 isn = generate_instr(cctx, ISN_CHECKNR);
3648 if (isn == NULL)
3649 return FAIL;
3650 }
3651 else
3652 {
3653 int invert = TRUE;
3654
3655 while (p > start && p[-1] == '!')
3656 {
3657 --p;
3658 invert = !invert;
3659 }
3660 if (generate_2BOOL(cctx, invert) == FAIL)
3661 return FAIL;
3662 }
3663 }
3664 return OK;
3665}
3666
3667/*
3668 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003669 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 */
3671 static int
3672compile_subscript(
3673 char_u **arg,
3674 cctx_T *cctx,
3675 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003676 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003677 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678{
3679 for (;;)
3680 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003681 char_u *p = skipwhite(*arg);
3682
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003683 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003684 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003685 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003686
3687 // If a following line starts with "->{" or "->X" advance to that
3688 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003689 // Also if a following line starts with ".x".
3690 if (next != NULL &&
3691 ((next[0] == '-' && next[1] == '>'
3692 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3693 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003694 {
3695 next = next_line_from_context(cctx, TRUE);
3696 if (next == NULL)
3697 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003698 *arg = next;
3699 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003700 }
3701 }
3702
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003703 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003705 garray_T *stack = &cctx->ctx_type_stack;
3706 type_T *type;
3707 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003709 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003710 return FAIL;
3711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003713 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3714
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003715 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3717 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003718 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 return FAIL;
3720 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003721 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003723 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003724 return FAIL;
3725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 // something->method()
3727 // Apply the '!', '-' and '+' first:
3728 // -1.0->func() works like (-1.0)->func()
3729 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3730 return FAIL;
3731 *start_leader = end_leader; // don't apply again later
3732
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003733 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003734 *arg = skipwhite(p);
3735 if (may_get_next_line(p, arg, cctx) == FAIL)
3736 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 if (**arg == '{')
3738 {
3739 // lambda call: list->{lambda}
3740 if (compile_lambda_call(arg, cctx) == FAIL)
3741 return FAIL;
3742 }
3743 else
3744 {
3745 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003746 p = *arg;
3747 if (ASCII_ISALPHA(*p) && p[1] == ':')
3748 p += 2;
3749 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 ;
3751 if (*p != '(')
3752 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003753 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 return FAIL;
3755 }
3756 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003757 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 return FAIL;
3759 }
3760 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003761 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003763 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003764 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003765 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003766
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003767 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003768 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003769 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003770 // TODO: blob index
3771 // TODO: more arguments
3772 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003773 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003774 return FAIL;
3775
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003776 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003777 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003778 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003779 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003780 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 return FAIL;
3782
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003783 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3784 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 if (**arg != ']')
3786 {
3787 emsg(_(e_missbrac));
3788 return FAIL;
3789 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003790 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003792 // We can index a list and a dict. If we don't know the type
3793 // we can use the index value type.
3794 // TODO: If we don't know use an instruction to figure it out at
3795 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003796 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003797 vtype = (*typep)->tt_type;
3798 if (*typep == &t_any)
3799 {
3800 type_T *valtype = ((type_T **)stack->ga_data)
3801 [stack->ga_len - 1];
3802 if (valtype == &t_string)
3803 vtype = VAR_DICT;
3804 }
3805 if (vtype == VAR_DICT)
3806 {
3807 if ((*typep)->tt_type == VAR_DICT)
3808 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003809 else
3810 {
3811 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3812 return FAIL;
3813 *typep = &t_any;
3814 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003815 if (may_generate_2STRING(-1, cctx) == FAIL)
3816 return FAIL;
3817 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3818 return FAIL;
3819 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003820 else if (vtype == VAR_STRING)
3821 {
3822 *typep = &t_number;
3823 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3824 return FAIL;
3825 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003826 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003827 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003828 if ((*typep)->tt_type == VAR_LIST)
3829 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003830 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003831 return FAIL;
3832 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003833 else
3834 {
3835 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003836 return FAIL;
3837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003839 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003841 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003842 return FAIL;
3843
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003844 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003845 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3846 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003848 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 if (eval_isnamec1(*p))
3850 while (eval_isnamec(*p))
3851 MB_PTR_ADV(p);
3852 if (p == *arg)
3853 {
3854 semsg(_(e_syntax_at), *arg);
3855 return FAIL;
3856 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003857 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 return FAIL;
3859 *arg = p;
3860 }
3861 else
3862 break;
3863 }
3864
3865 // TODO - see handle_subscript():
3866 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3867 // Don't do this when "Func" is already a partial that was bound
3868 // explicitly (pt_auto is FALSE).
3869
3870 return OK;
3871}
3872
3873/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003874 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3875 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003877 * If the value is a constant "ppconst->pp_ret" will be set.
3878 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003879 *
3880 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 */
3882
3883/*
3884 * number number constant
3885 * 0zFFFFFFFF Blob constant
3886 * "string" string constant
3887 * 'string' literal string constant
3888 * &option-name option value
3889 * @r register contents
3890 * identifier variable value
3891 * function() function call
3892 * $VAR environment variable
3893 * (expression) nested expression
3894 * [expr, expr] List
3895 * {key: val, key: val} Dictionary
3896 * #{key: val, key: val} Dictionary with literal keys
3897 *
3898 * Also handle:
3899 * ! in front logical NOT
3900 * - in front unary minus
3901 * + in front unary plus (ignored)
3902 * trailing (arg) funcref/partial call
3903 * trailing [] subscript in String or List
3904 * trailing .name entry in Dictionary
3905 * trailing ->name() method call
3906 */
3907 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003908compile_expr7(
3909 char_u **arg,
3910 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003911 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 char_u *start_leader, *end_leader;
3914 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003915 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003916 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917
3918 /*
3919 * Skip '!', '-' and '+' characters. They are handled later.
3920 */
3921 start_leader = *arg;
3922 while (**arg == '!' || **arg == '-' || **arg == '+')
3923 *arg = skipwhite(*arg + 1);
3924 end_leader = *arg;
3925
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003926 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 switch (**arg)
3928 {
3929 /*
3930 * Number constant.
3931 */
3932 case '0': // also for blob starting with 0z
3933 case '1':
3934 case '2':
3935 case '3':
3936 case '4':
3937 case '5':
3938 case '6':
3939 case '7':
3940 case '8':
3941 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003942 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 return FAIL;
3944 break;
3945
3946 /*
3947 * String constant: "string".
3948 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003949 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 return FAIL;
3951 break;
3952
3953 /*
3954 * Literal string constant: 'str''ing'.
3955 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003956 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 return FAIL;
3958 break;
3959
3960 /*
3961 * Constant Vim variable.
3962 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003963 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 ret = NOTDONE;
3965 break;
3966
3967 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003968 * "true" constant
3969 */
3970 case 't': if (STRNCMP(*arg, "true", 4) == 0
3971 && !eval_isnamec((*arg)[4]))
3972 {
3973 *arg += 4;
3974 rettv->v_type = VAR_BOOL;
3975 rettv->vval.v_number = VVAL_TRUE;
3976 }
3977 else
3978 ret = NOTDONE;
3979 break;
3980
3981 /*
3982 * "false" constant
3983 */
3984 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3985 && !eval_isnamec((*arg)[5]))
3986 {
3987 *arg += 5;
3988 rettv->v_type = VAR_BOOL;
3989 rettv->vval.v_number = VVAL_FALSE;
3990 }
3991 else
3992 ret = NOTDONE;
3993 break;
3994
3995 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 * List: [expr, expr]
3997 */
3998 case '[': ret = compile_list(arg, cctx);
3999 break;
4000
4001 /*
4002 * Dictionary: #{key: val, key: val}
4003 */
4004 case '#': if ((*arg)[1] == '{')
4005 {
4006 ++*arg;
4007 ret = compile_dict(arg, cctx, TRUE);
4008 }
4009 else
4010 ret = NOTDONE;
4011 break;
4012
4013 /*
4014 * Lambda: {arg, arg -> expr}
4015 * Dictionary: {'key': val, 'key': val}
4016 */
4017 case '{': {
4018 char_u *start = skipwhite(*arg + 1);
4019
4020 // Find out what comes after the arguments.
4021 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004022 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 if (ret != FAIL && *start == '>')
4024 ret = compile_lambda(arg, cctx);
4025 else
4026 ret = compile_dict(arg, cctx, FALSE);
4027 }
4028 break;
4029
4030 /*
4031 * Option value: &name
4032 */
4033 case '&': ret = compile_get_option(arg, cctx);
4034 break;
4035
4036 /*
4037 * Environment variable: $VAR.
4038 */
4039 case '$': ret = compile_get_env(arg, cctx);
4040 break;
4041
4042 /*
4043 * Register contents: @r.
4044 */
4045 case '@': ret = compile_get_register(arg, cctx);
4046 break;
4047 /*
4048 * nested expression: (expression).
4049 */
4050 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004051
4052 // recursive!
4053 if (ppconst->pp_used <= PPSIZE - 10)
4054 {
4055 ret = compile_expr1(arg, cctx, ppconst);
4056 }
4057 else
4058 {
4059 // Not enough space in ppconst, flush constants.
4060 if (generate_ppconst(cctx, ppconst) == FAIL)
4061 return FAIL;
4062 ret = compile_expr0(arg, cctx);
4063 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 *arg = skipwhite(*arg);
4065 if (**arg == ')')
4066 ++*arg;
4067 else if (ret == OK)
4068 {
4069 emsg(_(e_missing_close));
4070 ret = FAIL;
4071 }
4072 break;
4073
4074 default: ret = NOTDONE;
4075 break;
4076 }
4077 if (ret == FAIL)
4078 return FAIL;
4079
Bram Moolenaar1c747212020-05-09 18:28:34 +02004080 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 {
4082 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004083 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004085 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 return FAIL;
4087 }
4088 start_leader = end_leader; // don't apply again below
4089
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004090 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004091 clear_tv(rettv);
4092 else
4093 // A constant expression can possibly be handled compile time,
4094 // return the value instead of generating code.
4095 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 }
4097 else if (ret == NOTDONE)
4098 {
4099 char_u *p;
4100 int r;
4101
4102 if (!eval_isnamec1(**arg))
4103 {
4104 semsg(_("E1015: Name expected: %s"), *arg);
4105 return FAIL;
4106 }
4107
4108 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004109 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004111 {
4112 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004115 {
4116 if (generate_ppconst(cctx, ppconst) == FAIL)
4117 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 if (r == FAIL)
4121 return FAIL;
4122 }
4123
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004124 // Handle following "[]", ".member", etc.
4125 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004126 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004127 ppconst) == FAIL)
4128 return FAIL;
4129 if (ppconst->pp_used > 0)
4130 {
4131 // apply the '!', '-' and '+' before the constant
4132 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4133 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4134 return FAIL;
4135 return OK;
4136 }
4137 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004139 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140}
4141
4142/*
4143 * * number multiplication
4144 * / number division
4145 * % number modulo
4146 */
4147 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004148compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149{
4150 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004151 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004152 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004154 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004155 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 return FAIL;
4157
4158 /*
4159 * Repeat computing, until no "*", "/" or "%" is following.
4160 */
4161 for (;;)
4162 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004163 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 if (*op != '*' && *op != '/' && *op != '%')
4165 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004166 if (next != NULL)
4167 {
4168 *arg = next_line_from_context(cctx, TRUE);
4169 op = skipwhite(*arg);
4170 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004171
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004172 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 {
4174 char_u buf[3];
4175
4176 vim_strncpy(buf, op, 1);
4177 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004178 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 }
4180 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004181 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004182 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004184 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004185 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004187
4188 if (ppconst->pp_used == ppconst_used + 2
4189 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4190 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004191 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004192 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4193 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004194 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004196 // both are numbers: compute the result
4197 switch (*op)
4198 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004199 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004200 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004201 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004202 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004203 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004204 break;
4205 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004206 tv1->vval.v_number = res;
4207 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004208 }
4209 else
4210 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004211 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004212 generate_two_op(cctx, op);
4213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 }
4215
4216 return OK;
4217}
4218
4219/*
4220 * + number addition
4221 * - number subtraction
4222 * .. string concatenation
4223 */
4224 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004225compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226{
4227 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004228 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004230 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231
4232 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004233 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 return FAIL;
4235
4236 /*
4237 * Repeat computing, until no "+", "-" or ".." is following.
4238 */
4239 for (;;)
4240 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004241 op = may_peek_next_line(cctx, *arg, &next);
4242 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 break;
4244 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004245 if (next != NULL)
4246 {
4247 *arg = next_line_from_context(cctx, TRUE);
4248 op = skipwhite(*arg);
4249 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004251 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 {
4253 char_u buf[3];
4254
4255 vim_strncpy(buf, op, oplen);
4256 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004257 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 }
4259
4260 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004261 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004262 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004264 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004265 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 return FAIL;
4267
Bram Moolenaara5565e42020-05-09 15:44:01 +02004268 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004269 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004270 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4271 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4272 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4273 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004275 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4276 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004277
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004278 // concat/subtract/add constant numbers
4279 if (*op == '+')
4280 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4281 else if (*op == '-')
4282 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4283 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004284 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004285 // concatenate constant strings
4286 char_u *s1 = tv1->vval.v_string;
4287 char_u *s2 = tv2->vval.v_string;
4288 size_t len1 = STRLEN(s1);
4289
4290 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4291 if (tv1->vval.v_string == NULL)
4292 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004293 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004294 return FAIL;
4295 }
4296 mch_memmove(tv1->vval.v_string, s1, len1);
4297 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004298 vim_free(s1);
4299 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004300 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004301 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 }
4303 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004304 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004305 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004306 if (*op == '.')
4307 {
4308 if (may_generate_2STRING(-2, cctx) == FAIL
4309 || may_generate_2STRING(-1, cctx) == FAIL)
4310 return FAIL;
4311 generate_instr_drop(cctx, ISN_CONCAT, 1);
4312 }
4313 else
4314 generate_two_op(cctx, op);
4315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 }
4317
4318 return OK;
4319}
4320
4321/*
4322 * expr5a == expr5b
4323 * expr5a =~ expr5b
4324 * expr5a != expr5b
4325 * expr5a !~ expr5b
4326 * expr5a > expr5b
4327 * expr5a >= expr5b
4328 * expr5a < expr5b
4329 * expr5a <= expr5b
4330 * expr5a is expr5b
4331 * expr5a isnot expr5b
4332 *
4333 * Produces instructions:
4334 * EVAL expr5a Push result of "expr5a"
4335 * EVAL expr5b Push result of "expr5b"
4336 * COMPARE one of the compare instructions
4337 */
4338 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004339compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340{
4341 exptype_T type = EXPR_UNKNOWN;
4342 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004343 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004346 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347
4348 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004349 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 return FAIL;
4351
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004352 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004353 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354
4355 /*
4356 * If there is a comparative operator, use it.
4357 */
4358 if (type != EXPR_UNKNOWN)
4359 {
4360 int ic = FALSE; // Default: do not ignore case
4361
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004362 if (next != NULL)
4363 {
4364 *arg = next_line_from_context(cctx, TRUE);
4365 p = skipwhite(*arg);
4366 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 if (type_is && (p[len] == '?' || p[len] == '#'))
4368 {
4369 semsg(_(e_invexpr2), *arg);
4370 return FAIL;
4371 }
4372 // extra question mark appended: ignore case
4373 if (p[len] == '?')
4374 {
4375 ic = TRUE;
4376 ++len;
4377 }
4378 // extra '#' appended: match case (ignored)
4379 else if (p[len] == '#')
4380 ++len;
4381 // nothing appended: match case
4382
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004383 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 {
4385 char_u buf[7];
4386
4387 vim_strncpy(buf, p, len);
4388 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004389 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 }
4391
4392 // get the second variable
4393 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004394 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004395 return FAIL;
4396
Bram Moolenaara5565e42020-05-09 15:44:01 +02004397 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 return FAIL;
4399
Bram Moolenaara5565e42020-05-09 15:44:01 +02004400 if (ppconst->pp_used == ppconst_used + 2)
4401 {
4402 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4403 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4404 int ret;
4405
4406 // Both sides are a constant, compute the result now.
4407 // First check for a valid combination of types, this is more
4408 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004409 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004410 ret = FAIL;
4411 else
4412 {
4413 ret = typval_compare(tv1, tv2, type, ic);
4414 tv1->v_type = VAR_BOOL;
4415 tv1->vval.v_number = tv1->vval.v_number
4416 ? VVAL_TRUE : VVAL_FALSE;
4417 clear_tv(tv2);
4418 --ppconst->pp_used;
4419 }
4420 return ret;
4421 }
4422
4423 generate_ppconst(cctx, ppconst);
4424 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 }
4426
4427 return OK;
4428}
4429
Bram Moolenaar7f141552020-05-09 17:35:53 +02004430static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432/*
4433 * Compile || or &&.
4434 */
4435 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004436compile_and_or(
4437 char_u **arg,
4438 cctx_T *cctx,
4439 char *op,
4440 ppconst_T *ppconst,
4441 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004443 char_u *next;
4444 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 int opchar = *op;
4446
4447 if (p[0] == opchar && p[1] == opchar)
4448 {
4449 garray_T *instr = &cctx->ctx_instr;
4450 garray_T end_ga;
4451
4452 /*
4453 * Repeat until there is no following "||" or "&&"
4454 */
4455 ga_init2(&end_ga, sizeof(int), 10);
4456 while (p[0] == opchar && p[1] == opchar)
4457 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004458 if (next != NULL)
4459 {
4460 *arg = next_line_from_context(cctx, TRUE);
4461 p = skipwhite(*arg);
4462 }
4463
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004464 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4465 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004467 return FAIL;
4468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469
Bram Moolenaara5565e42020-05-09 15:44:01 +02004470 // TODO: use ppconst if the value is a constant
4471 generate_ppconst(cctx, ppconst);
4472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 if (ga_grow(&end_ga, 1) == FAIL)
4474 {
4475 ga_clear(&end_ga);
4476 return FAIL;
4477 }
4478 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4479 ++end_ga.ga_len;
4480 generate_JUMP(cctx, opchar == '|'
4481 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4482
4483 // eval the next expression
4484 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004485 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004486 return FAIL;
4487
Bram Moolenaara5565e42020-05-09 15:44:01 +02004488 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4489 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 {
4491 ga_clear(&end_ga);
4492 return FAIL;
4493 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004494
4495 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004497 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498
4499 // Fill in the end label in all jumps.
4500 while (end_ga.ga_len > 0)
4501 {
4502 isn_T *isn;
4503
4504 --end_ga.ga_len;
4505 isn = ((isn_T *)instr->ga_data)
4506 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4507 isn->isn_arg.jump.jump_where = instr->ga_len;
4508 }
4509 ga_clear(&end_ga);
4510 }
4511
4512 return OK;
4513}
4514
4515/*
4516 * expr4a && expr4a && expr4a logical AND
4517 *
4518 * Produces instructions:
4519 * EVAL expr4a Push result of "expr4a"
4520 * JUMP_AND_KEEP_IF_FALSE end
4521 * EVAL expr4b Push result of "expr4b"
4522 * JUMP_AND_KEEP_IF_FALSE end
4523 * EVAL expr4c Push result of "expr4c"
4524 * end:
4525 */
4526 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004527compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004529 int ppconst_used = ppconst->pp_used;
4530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004532 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 return FAIL;
4534
4535 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004536 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537}
4538
4539/*
4540 * expr3a || expr3b || expr3c logical OR
4541 *
4542 * Produces instructions:
4543 * EVAL expr3a Push result of "expr3a"
4544 * JUMP_AND_KEEP_IF_TRUE end
4545 * EVAL expr3b Push result of "expr3b"
4546 * JUMP_AND_KEEP_IF_TRUE end
4547 * EVAL expr3c Push result of "expr3c"
4548 * end:
4549 */
4550 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004551compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004553 int ppconst_used = ppconst->pp_used;
4554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004556 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 return FAIL;
4558
4559 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004560 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561}
4562
4563/*
4564 * Toplevel expression: expr2 ? expr1a : expr1b
4565 *
4566 * Produces instructions:
4567 * EVAL expr2 Push result of "expr"
4568 * JUMP_IF_FALSE alt jump if false
4569 * EVAL expr1a
4570 * JUMP_ALWAYS end
4571 * alt: EVAL expr1b
4572 * end:
4573 */
4574 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004575compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576{
4577 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004578 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004579 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580
Bram Moolenaar61a89812020-05-07 16:58:17 +02004581 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004582 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 return FAIL;
4584
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004585 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 if (*p == '?')
4587 {
4588 garray_T *instr = &cctx->ctx_instr;
4589 garray_T *stack = &cctx->ctx_type_stack;
4590 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004591 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004593 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004595 int has_const_expr = FALSE;
4596 int const_value = FALSE;
4597 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004599 if (next != NULL)
4600 {
4601 *arg = next_line_from_context(cctx, TRUE);
4602 p = skipwhite(*arg);
4603 }
4604
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004605 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4606 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004608 return FAIL;
4609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610
Bram Moolenaara5565e42020-05-09 15:44:01 +02004611 if (ppconst->pp_used == ppconst_used + 1)
4612 {
4613 // the condition is a constant, we know whether the ? or the :
4614 // expression is to be evaluated.
4615 has_const_expr = TRUE;
4616 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4617 clear_tv(&ppconst->pp_tv[ppconst_used]);
4618 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004619 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4620 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004621 }
4622 else
4623 {
4624 generate_ppconst(cctx, ppconst);
4625 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627
4628 // evaluate the second expression; any type is accepted
4629 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004630 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004631 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004632 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004633 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634
Bram Moolenaara5565e42020-05-09 15:44:01 +02004635 if (!has_const_expr)
4636 {
4637 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638
Bram Moolenaara5565e42020-05-09 15:44:01 +02004639 // remember the type and drop it
4640 --stack->ga_len;
4641 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642
Bram Moolenaara5565e42020-05-09 15:44:01 +02004643 end_idx = instr->ga_len;
4644 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4645
4646 // jump here from JUMP_IF_FALSE
4647 isn = ((isn_T *)instr->ga_data) + alt_idx;
4648 isn->isn_arg.jump.jump_where = instr->ga_len;
4649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650
4651 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004652 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 if (*p != ':')
4654 {
4655 emsg(_(e_missing_colon));
4656 return FAIL;
4657 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004658 if (next != NULL)
4659 {
4660 *arg = next_line_from_context(cctx, TRUE);
4661 p = skipwhite(*arg);
4662 }
4663
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004664 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4665 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004667 return FAIL;
4668 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669
4670 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004671 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004672 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4673 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004675 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004676 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004677 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004678 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679
Bram Moolenaara5565e42020-05-09 15:44:01 +02004680 if (!has_const_expr)
4681 {
4682 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683
Bram Moolenaara5565e42020-05-09 15:44:01 +02004684 // If the types differ, the result has a more generic type.
4685 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4686 common_type(type1, type2, &type2, cctx->ctx_type_list);
4687
4688 // jump here from JUMP_ALWAYS
4689 isn = ((isn_T *)instr->ga_data) + end_idx;
4690 isn->isn_arg.jump.jump_where = instr->ga_len;
4691 }
4692
4693 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 }
4695 return OK;
4696}
4697
4698/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004699 * Toplevel expression.
4700 */
4701 static int
4702compile_expr0(char_u **arg, cctx_T *cctx)
4703{
4704 ppconst_T ppconst;
4705
4706 CLEAR_FIELD(ppconst);
4707 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4708 {
4709 clear_ppconst(&ppconst);
4710 return FAIL;
4711 }
4712 if (generate_ppconst(cctx, &ppconst) == FAIL)
4713 return FAIL;
4714 return OK;
4715}
4716
4717/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 * compile "return [expr]"
4719 */
4720 static char_u *
4721compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4722{
4723 char_u *p = arg;
4724 garray_T *stack = &cctx->ctx_type_stack;
4725 type_T *stack_type;
4726
4727 if (*p != NUL && *p != '|' && *p != '\n')
4728 {
4729 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004730 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 return NULL;
4732
4733 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4734 if (set_return_type)
4735 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004736 else
4737 {
4738 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4739 && stack_type->tt_type != VAR_VOID
4740 && stack_type->tt_type != VAR_UNKNOWN)
4741 {
4742 emsg(_("E1096: Returning a value in a function without a return type"));
4743 return NULL;
4744 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004745 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4746 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 }
4750 else
4751 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004752 // "set_return_type" cannot be TRUE, only used for a lambda which
4753 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004754 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4755 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756 {
4757 emsg(_("E1003: Missing return value"));
4758 return NULL;
4759 }
4760
4761 // No argument, return zero.
4762 generate_PUSHNR(cctx, 0);
4763 }
4764
4765 if (generate_instr(cctx, ISN_RETURN) == NULL)
4766 return NULL;
4767
4768 // "return val | endif" is possible
4769 return skipwhite(p);
4770}
4771
4772/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004773 * Get a line from the compilation context, compatible with exarg_T getline().
4774 * Return a pointer to the line in allocated memory.
4775 * Return NULL for end-of-file or some error.
4776 */
4777 static char_u *
4778exarg_getline(
4779 int c UNUSED,
4780 void *cookie,
4781 int indent UNUSED,
4782 int do_concat UNUSED)
4783{
4784 cctx_T *cctx = (cctx_T *)cookie;
4785
4786 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4787 {
4788 iemsg("Heredoc got to end");
4789 return NULL;
4790 }
4791 ++cctx->ctx_lnum;
4792 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4793 [cctx->ctx_lnum]);
4794}
4795
4796/*
4797 * Compile a nested :def command.
4798 */
4799 static char_u *
4800compile_nested_function(exarg_T *eap, cctx_T *cctx)
4801{
4802 char_u *name_start = eap->arg;
4803 char_u *name_end = to_name_end(eap->arg, FALSE);
4804 char_u *name = get_lambda_name();
4805 lvar_T *lvar;
4806 ufunc_T *ufunc;
4807
4808 eap->arg = name_end;
4809 eap->getline = exarg_getline;
4810 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004811 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004812 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004813 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004814
Bram Moolenaar822ba242020-05-24 23:00:18 +02004815 if (ufunc == NULL)
4816 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004817 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004818 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004819 return NULL;
4820
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004821 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004822 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004823 TRUE, ufunc->uf_func_type);
4824
4825 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4826 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4827 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004828
Bram Moolenaar61a89812020-05-07 16:58:17 +02004829 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004830 return (char_u *)"";
4831}
4832
4833/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 * Return the length of an assignment operator, or zero if there isn't one.
4835 */
4836 int
4837assignment_len(char_u *p, int *heredoc)
4838{
4839 if (*p == '=')
4840 {
4841 if (p[1] == '<' && p[2] == '<')
4842 {
4843 *heredoc = TRUE;
4844 return 3;
4845 }
4846 return 1;
4847 }
4848 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4849 return 2;
4850 if (STRNCMP(p, "..=", 3) == 0)
4851 return 3;
4852 return 0;
4853}
4854
4855// words that cannot be used as a variable
4856static char *reserved[] = {
4857 "true",
4858 "false",
4859 NULL
4860};
4861
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004862typedef enum {
4863 dest_local,
4864 dest_option,
4865 dest_env,
4866 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004867 dest_buffer,
4868 dest_window,
4869 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004870 dest_vimvar,
4871 dest_script,
4872 dest_reg,
4873} assign_dest_T;
4874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004876 * Generate the load instruction for "name".
4877 */
4878 static void
4879generate_loadvar(
4880 cctx_T *cctx,
4881 assign_dest_T dest,
4882 char_u *name,
4883 lvar_T *lvar,
4884 type_T *type)
4885{
4886 switch (dest)
4887 {
4888 case dest_option:
4889 // TODO: check the option exists
4890 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4891 break;
4892 case dest_global:
4893 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4894 break;
4895 case dest_buffer:
4896 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4897 break;
4898 case dest_window:
4899 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4900 break;
4901 case dest_tab:
4902 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4903 break;
4904 case dest_script:
4905 compile_load_scriptvar(cctx,
4906 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4907 break;
4908 case dest_env:
4909 // Include $ in the name here
4910 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4911 break;
4912 case dest_reg:
4913 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4914 break;
4915 case dest_vimvar:
4916 generate_LOADV(cctx, name + 2, TRUE);
4917 break;
4918 case dest_local:
4919 if (lvar->lv_from_outer)
4920 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4921 NULL, type);
4922 else
4923 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4924 break;
4925 }
4926}
4927
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004928 void
4929vim9_declare_error(char_u *name)
4930{
4931 char *scope = "";
4932
4933 switch (*name)
4934 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004935 case 'g': scope = _("global"); break;
4936 case 'b': scope = _("buffer"); break;
4937 case 'w': scope = _("window"); break;
4938 case 't': scope = _("tab"); break;
4939 case 'v': scope = "v:"; break;
4940 case '$': semsg(_(e_declare_env_var), name); return;
4941 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004942 }
4943 semsg(_(e_declare_var), scope, name);
4944}
4945
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004946/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004947 * Compile declaration and assignment:
4948 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004950 * Return NULL for an error.
4951 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952 */
4953 static char_u *
4954compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4955{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004956 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004958 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 char_u *ret = NULL;
4960 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004961 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004964 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 int oplen = 0;
4967 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004968 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004969 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004970 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004974 // Skip over the "var" or "[var, var]" to get to any "=".
4975 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4976 if (p == NULL)
4977 return *arg == '[' ? arg : NULL;
4978
4979 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004981 // TODO: should we allow this, and figure out type inference from list
4982 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004983 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 return NULL;
4985 }
4986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 sp = p;
4988 p = skipwhite(p);
4989 op = p;
4990 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004991
4992 if (var_count > 0 && oplen == 0)
4993 // can be something like "[1, 2]->func()"
4994 return arg;
4995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4997 {
4998 char_u buf[4];
4999
5000 vim_strncpy(buf, op, oplen);
5001 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005002 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005003 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 if (heredoc)
5006 {
5007 list_T *l;
5008 listitem_T *li;
5009
5010 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005011 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005013 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014
5015 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005016 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 {
5018 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5019 li->li_tv.vval.v_string = NULL;
5020 }
5021 generate_NEWLIST(cctx, l->lv_len);
5022 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005023 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024 list_free(l);
5025 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005026 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005028 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005030 // for "[var, var] = expr" evaluate the expression here, loop over the
5031 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005032
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005033 p = skipwhite(op + oplen);
5034 if (compile_expr0(&p, cctx) == FAIL)
5035 return NULL;
5036 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005037
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005038 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005040 type_T *stacktype;
5041
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005042 stacktype = stack->ga_len == 0 ? &t_void
5043 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005044 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005046 emsg(_(e_cannot_use_void));
5047 goto theend;
5048 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005049 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005050 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005051 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005052 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5053 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005054 }
5055 }
5056
5057 /*
5058 * Loop over variables in "[var, var] = expr".
5059 * For "var = expr" and "let var: type" this is done only once.
5060 */
5061 if (var_count > 0)
5062 var_start = skipwhite(arg + 1); // skip over the "["
5063 else
5064 var_start = arg;
5065 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5066 {
5067 char_u *var_end = skip_var_one(var_start, FALSE);
5068 size_t varlen;
5069 int new_local = FALSE;
5070 int opt_type;
5071 int opt_flags = 0;
5072 assign_dest_T dest = dest_local;
5073 int vimvaridx = -1;
5074 lvar_T *lvar = NULL;
5075 lvar_T arg_lvar;
5076 int has_type = FALSE;
5077 int has_index = FALSE;
5078 int instr_count = -1;
5079
5080 p = (*var_start == '&' || *var_start == '$'
5081 || *var_start == '@') ? var_start + 1 : var_start;
5082 p = to_name_end(p, TRUE);
5083
5084 // "a: type" is declaring variable "a" with a type, not "a:".
5085 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5086 --var_end;
5087 if (is_decl && p == var_start + 2 && p[-1] == ':')
5088 --p;
5089
5090 varlen = p - var_start;
5091 vim_free(name);
5092 name = vim_strnsave(var_start, varlen);
5093 if (name == NULL)
5094 return NULL;
5095 if (!heredoc)
5096 type = &t_any;
5097
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005098 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005099 {
5100 if (*var_start == '&')
5101 {
5102 int cc;
5103 long numval;
5104
5105 dest = dest_option;
5106 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005108 emsg(_(e_const_option));
5109 goto theend;
5110 }
5111 if (is_decl)
5112 {
5113 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5114 goto theend;
5115 }
5116 p = var_start;
5117 p = find_option_end(&p, &opt_flags);
5118 if (p == NULL)
5119 {
5120 // cannot happen?
5121 emsg(_(e_letunexp));
5122 goto theend;
5123 }
5124 cc = *p;
5125 *p = NUL;
5126 opt_type = get_option_value(var_start + 1, &numval,
5127 NULL, opt_flags);
5128 *p = cc;
5129 if (opt_type == -3)
5130 {
5131 semsg(_(e_unknown_option), var_start);
5132 goto theend;
5133 }
5134 if (opt_type == -2 || opt_type == 0)
5135 type = &t_string;
5136 else
5137 type = &t_number; // both number and boolean option
5138 }
5139 else if (*var_start == '$')
5140 {
5141 dest = dest_env;
5142 type = &t_string;
5143 if (is_decl)
5144 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005145 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005146 goto theend;
5147 }
5148 }
5149 else if (*var_start == '@')
5150 {
5151 if (!valid_yank_reg(var_start[1], TRUE))
5152 {
5153 emsg_invreg(var_start[1]);
5154 goto theend;
5155 }
5156 dest = dest_reg;
5157 type = &t_string;
5158 if (is_decl)
5159 {
5160 semsg(_("E1066: Cannot declare a register: %s"), name);
5161 goto theend;
5162 }
5163 }
5164 else if (STRNCMP(var_start, "g:", 2) == 0)
5165 {
5166 dest = dest_global;
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, "b:", 2) == 0)
5174 {
5175 dest = dest_buffer;
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, "w:", 2) == 0)
5183 {
5184 dest = dest_window;
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, "t:", 2) == 0)
5192 {
5193 dest = dest_tab;
5194 if (is_decl)
5195 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005196 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005197 goto theend;
5198 }
5199 }
5200 else if (STRNCMP(var_start, "v:", 2) == 0)
5201 {
5202 typval_T *vtv;
5203 int di_flags;
5204
5205 vimvaridx = find_vim_var(name + 2, &di_flags);
5206 if (vimvaridx < 0)
5207 {
5208 semsg(_(e_var_notfound), var_start);
5209 goto theend;
5210 }
5211 // We use the current value of "sandbox" here, is that OK?
5212 if (var_check_ro(di_flags, name, FALSE))
5213 goto theend;
5214 dest = dest_vimvar;
5215 vtv = get_vim_var_tv(vimvaridx);
5216 type = typval2type(vtv);
5217 if (is_decl)
5218 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005219 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005220 goto theend;
5221 }
5222 }
5223 else
5224 {
5225 int idx;
5226
5227 for (idx = 0; reserved[idx] != NULL; ++idx)
5228 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005229 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005230 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005231 goto theend;
5232 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005233
5234 lvar = lookup_local(var_start, varlen, cctx);
5235 if (lvar == NULL)
5236 {
5237 CLEAR_FIELD(arg_lvar);
5238 if (lookup_arg(var_start, varlen,
5239 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5240 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005241 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005242 if (is_decl)
5243 {
5244 semsg(_(e_used_as_arg), name);
5245 goto theend;
5246 }
5247 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005248 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005249 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005250 if (lvar != NULL)
5251 {
5252 if (is_decl)
5253 {
5254 semsg(_("E1017: Variable already declared: %s"), name);
5255 goto theend;
5256 }
5257 else if (lvar->lv_const)
5258 {
5259 semsg(_("E1018: Cannot assign to a constant: %s"),
5260 name);
5261 goto theend;
5262 }
5263 }
5264 else if (STRNCMP(var_start, "s:", 2) == 0
5265 || lookup_script(var_start, varlen) == OK
5266 || find_imported(var_start, varlen, cctx) != NULL)
5267 {
5268 dest = dest_script;
5269 if (is_decl)
5270 {
5271 semsg(_("E1054: Variable already declared in the script: %s"),
5272 name);
5273 goto theend;
5274 }
5275 }
5276 else if (name[1] == ':' && name[2] != NUL)
5277 {
5278 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5279 name);
5280 goto theend;
5281 }
5282 else if (!is_decl)
5283 {
5284 semsg(_("E1089: unknown variable: %s"), name);
5285 goto theend;
5286 }
5287 }
5288 }
5289
5290 // handle "a:name" as a name, not index "name" on "a"
5291 if (varlen > 1 || var_start[varlen] != ':')
5292 p = var_end;
5293
5294 if (dest != dest_option)
5295 {
5296 if (is_decl && *p == ':')
5297 {
5298 // parse optional type: "let var: type = expr"
5299 if (!VIM_ISWHITE(p[1]))
5300 {
5301 semsg(_(e_white_after), ":");
5302 goto theend;
5303 }
5304 p = skipwhite(p + 1);
5305 type = parse_type(&p, cctx->ctx_type_list);
5306 has_type = TRUE;
5307 }
5308 else if (lvar != NULL)
5309 type = lvar->lv_type;
5310 }
5311
5312 if (oplen == 3 && !heredoc && dest != dest_global
5313 && type->tt_type != VAR_STRING
5314 && type->tt_type != VAR_ANY)
5315 {
5316 emsg(_("E1019: Can only concatenate to string"));
5317 goto theend;
5318 }
5319
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005320 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005321 {
5322 if (oplen > 1 && !heredoc)
5323 {
5324 // +=, /=, etc. require an existing variable
5325 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5326 name);
5327 goto theend;
5328 }
5329
5330 // new local variable
5331 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5332 goto theend;
5333 lvar = reserve_local(cctx, var_start, varlen,
5334 cmdidx == CMD_const, type);
5335 if (lvar == NULL)
5336 goto theend;
5337 new_local = TRUE;
5338 }
5339
5340 member_type = type;
5341 if (var_end > var_start + varlen)
5342 {
5343 // Something follows after the variable: "var[idx]".
5344 if (is_decl)
5345 {
5346 emsg(_("E1087: cannot use an index when declaring a variable"));
5347 goto theend;
5348 }
5349
5350 if (var_start[varlen] == '[')
5351 {
5352 has_index = TRUE;
5353 if (type->tt_member == NULL)
5354 {
5355 semsg(_("E1088: cannot use an index on %s"), name);
5356 goto theend;
5357 }
5358 member_type = type->tt_member;
5359 }
5360 else
5361 {
5362 semsg("Not supported yet: %s", var_start);
5363 goto theend;
5364 }
5365 }
5366 else if (lvar == &arg_lvar)
5367 {
5368 semsg(_("E1090: Cannot assign to argument %s"), name);
5369 goto theend;
5370 }
5371
5372 if (!heredoc)
5373 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005374 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005375 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005376 if (oplen > 0 && var_count == 0)
5377 {
5378 // skip over the "=" and the expression
5379 p = skipwhite(op + oplen);
5380 compile_expr0(&p, cctx);
5381 }
5382 }
5383 else if (oplen > 0)
5384 {
5385 type_T *stacktype;
5386
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005387 // For "var = expr" evaluate the expression.
5388 if (var_count == 0)
5389 {
5390 int r;
5391
5392 // for "+=", "*=", "..=" etc. first load the current value
5393 if (*op != '=')
5394 {
5395 generate_loadvar(cctx, dest, name, lvar, type);
5396
5397 if (has_index)
5398 {
5399 // TODO: get member from list or dict
5400 emsg("Index with operation not supported yet");
5401 goto theend;
5402 }
5403 }
5404
5405 // Compile the expression. Temporarily hide the new local
5406 // variable here, it is not available to this expression.
5407 if (new_local)
5408 --cctx->ctx_locals.ga_len;
5409 instr_count = instr->ga_len;
5410 p = skipwhite(op + oplen);
5411 r = compile_expr0(&p, cctx);
5412 if (new_local)
5413 ++cctx->ctx_locals.ga_len;
5414 if (r == FAIL)
5415 goto theend;
5416 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005417 else if (semicolon && var_idx == var_count - 1)
5418 {
5419 // For "[var; var] = expr" get the rest of the list
5420 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5421 goto theend;
5422 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005423 else
5424 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005425 // For "[var, var] = expr" get the "var_idx" item from the
5426 // list.
5427 if (generate_GETITEM(cctx, var_idx) == FAIL)
5428 return FAIL;
5429 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005430
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005431 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005432 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005433 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005434 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005435 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005436 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005437 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005438 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005439 emsg(_(e_cannot_use_void));
5440 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005441 }
5442 else
5443 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005444 // An empty list or dict has a &t_void member,
5445 // for a variable that implies &t_any.
5446 if (stacktype == &t_list_empty)
5447 lvar->lv_type = &t_list_any;
5448 else if (stacktype == &t_dict_empty)
5449 lvar->lv_type = &t_dict_any;
5450 else
5451 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005452 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005453 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005454 else
5455 {
5456 type_T *use_type = lvar->lv_type;
5457
5458 if (has_index)
5459 {
5460 use_type = use_type->tt_member;
5461 if (use_type == NULL)
5462 use_type = &t_void;
5463 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005464 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005465 == FAIL)
5466 goto theend;
5467 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005468 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005469 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005470 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005471 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005472 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005473 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005475 emsg(_(e_const_req_value));
5476 goto theend;
5477 }
5478 else if (!has_type || dest == dest_option)
5479 {
5480 emsg(_(e_type_req));
5481 goto theend;
5482 }
5483 else
5484 {
5485 // variables are always initialized
5486 if (ga_grow(instr, 1) == FAIL)
5487 goto theend;
5488 switch (member_type->tt_type)
5489 {
5490 case VAR_BOOL:
5491 generate_PUSHBOOL(cctx, VVAL_FALSE);
5492 break;
5493 case VAR_FLOAT:
5494#ifdef FEAT_FLOAT
5495 generate_PUSHF(cctx, 0.0);
5496#endif
5497 break;
5498 case VAR_STRING:
5499 generate_PUSHS(cctx, NULL);
5500 break;
5501 case VAR_BLOB:
5502 generate_PUSHBLOB(cctx, NULL);
5503 break;
5504 case VAR_FUNC:
5505 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5506 break;
5507 case VAR_LIST:
5508 generate_NEWLIST(cctx, 0);
5509 break;
5510 case VAR_DICT:
5511 generate_NEWDICT(cctx, 0);
5512 break;
5513 case VAR_JOB:
5514 generate_PUSHJOB(cctx, NULL);
5515 break;
5516 case VAR_CHANNEL:
5517 generate_PUSHCHANNEL(cctx, NULL);
5518 break;
5519 case VAR_NUMBER:
5520 case VAR_UNKNOWN:
5521 case VAR_ANY:
5522 case VAR_PARTIAL:
5523 case VAR_VOID:
5524 case VAR_SPECIAL: // cannot happen
5525 generate_PUSHNR(cctx, 0);
5526 break;
5527 }
5528 }
5529 if (var_count == 0)
5530 end = p;
5531 }
5532
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005533 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005534 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005535 break;
5536
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005537 if (oplen > 0 && *op != '=')
5538 {
5539 type_T *expected = &t_number;
5540 type_T *stacktype;
5541
5542 // TODO: if type is known use float or any operation
5543
5544 if (*op == '.')
5545 expected = &t_string;
5546 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005547 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005548 goto theend;
5549
5550 if (*op == '.')
5551 generate_instr_drop(cctx, ISN_CONCAT, 1);
5552 else
5553 {
5554 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5555
5556 if (isn == NULL)
5557 goto theend;
5558 switch (*op)
5559 {
5560 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5561 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5562 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5563 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5564 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5565 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005566 }
5567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005568
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005569 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005570 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005571 int r;
5572
5573 // Compile the "idx" in "var[idx]".
5574 if (new_local)
5575 --cctx->ctx_locals.ga_len;
5576 p = skipwhite(var_start + varlen + 1);
5577 r = compile_expr0(&p, cctx);
5578 if (new_local)
5579 ++cctx->ctx_locals.ga_len;
5580 if (r == FAIL)
5581 goto theend;
5582 if (*skipwhite(p) != ']')
5583 {
5584 emsg(_(e_missbrac));
5585 goto theend;
5586 }
5587 if (type->tt_type == VAR_DICT
5588 && may_generate_2STRING(-1, cctx) == FAIL)
5589 goto theend;
5590 if (type->tt_type == VAR_LIST
5591 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005592 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005593 {
5594 emsg(_(e_number_exp));
5595 goto theend;
5596 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005597
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005598 // Load the dict or list. On the stack we then have:
5599 // - value
5600 // - index
5601 // - variable
5602 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005603
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005604 if (type->tt_type == VAR_LIST)
5605 {
5606 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5607 return FAIL;
5608 }
5609 else if (type->tt_type == VAR_DICT)
5610 {
5611 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5612 return FAIL;
5613 }
5614 else
5615 {
5616 emsg(_(e_listreq));
5617 goto theend;
5618 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005619 }
5620 else
5621 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005622 switch (dest)
5623 {
5624 case dest_option:
5625 generate_STOREOPT(cctx, name + 1, opt_flags);
5626 break;
5627 case dest_global:
5628 // include g: with the name, easier to execute that way
5629 generate_STORE(cctx, ISN_STOREG, 0, name);
5630 break;
5631 case dest_buffer:
5632 // include b: with the name, easier to execute that way
5633 generate_STORE(cctx, ISN_STOREB, 0, name);
5634 break;
5635 case dest_window:
5636 // include w: with the name, easier to execute that way
5637 generate_STORE(cctx, ISN_STOREW, 0, name);
5638 break;
5639 case dest_tab:
5640 // include t: with the name, easier to execute that way
5641 generate_STORE(cctx, ISN_STORET, 0, name);
5642 break;
5643 case dest_env:
5644 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5645 break;
5646 case dest_reg:
5647 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5648 break;
5649 case dest_vimvar:
5650 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5651 break;
5652 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005653 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005654 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5655 imported_T *import = NULL;
5656 int sid = current_sctx.sc_sid;
5657 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005658
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005659 if (name[1] != ':')
5660 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005661 import = find_imported(name, 0, cctx);
5662 if (import != NULL)
5663 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005664 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005665
5666 idx = get_script_item_idx(sid, rawname, TRUE);
5667 // TODO: specific type
5668 if (idx < 0)
5669 {
5670 char_u *name_s = name;
5671
5672 // Include s: in the name for store_var()
5673 if (name[1] != ':')
5674 {
5675 int len = (int)STRLEN(name) + 3;
5676
5677 name_s = alloc(len);
5678 if (name_s == NULL)
5679 name_s = name;
5680 else
5681 vim_snprintf((char *)name_s, len,
5682 "s:%s", name);
5683 }
5684 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005685 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005686 if (name_s != name)
5687 vim_free(name_s);
5688 }
5689 else
5690 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005691 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005692 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005693 break;
5694 case dest_local:
5695 if (lvar != NULL)
5696 {
5697 isn_T *isn = ((isn_T *)instr->ga_data)
5698 + instr->ga_len - 1;
5699
5700 // optimization: turn "var = 123" from ISN_PUSHNR +
5701 // ISN_STORE into ISN_STORENR
5702 if (!lvar->lv_from_outer
5703 && instr->ga_len == instr_count + 1
5704 && isn->isn_type == ISN_PUSHNR)
5705 {
5706 varnumber_T val = isn->isn_arg.number;
5707
5708 isn->isn_type = ISN_STORENR;
5709 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5710 isn->isn_arg.storenr.stnr_val = val;
5711 if (stack->ga_len > 0)
5712 --stack->ga_len;
5713 }
5714 else if (lvar->lv_from_outer)
5715 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005716 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005717 else
5718 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5719 }
5720 break;
5721 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005722 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005723
5724 if (var_idx + 1 < var_count)
5725 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005726 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005727
5728 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005729 if (var_count > 0 && !semicolon)
5730 {
5731 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5732 goto theend;
5733 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005734
Bram Moolenaarb2097502020-07-19 17:17:02 +02005735 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005736
5737theend:
5738 vim_free(name);
5739 return ret;
5740}
5741
5742/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005743 * Check if "name" can be "unlet".
5744 */
5745 int
5746check_vim9_unlet(char_u *name)
5747{
5748 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5749 {
5750 semsg(_("E1081: Cannot unlet %s"), name);
5751 return FAIL;
5752 }
5753 return OK;
5754}
5755
5756/*
5757 * Callback passed to ex_unletlock().
5758 */
5759 static int
5760compile_unlet(
5761 lval_T *lvp,
5762 char_u *name_end,
5763 exarg_T *eap,
5764 int deep UNUSED,
5765 void *coookie)
5766{
5767 cctx_T *cctx = coookie;
5768
5769 if (lvp->ll_tv == NULL)
5770 {
5771 char_u *p = lvp->ll_name;
5772 int cc = *name_end;
5773 int ret = OK;
5774
5775 // Normal name. Only supports g:, w:, t: and b: namespaces.
5776 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005777 if (*p == '$')
5778 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5779 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005780 ret = FAIL;
5781 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005782 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005783
5784 *name_end = cc;
5785 return ret;
5786 }
5787
5788 // TODO: unlet {list}[idx]
5789 // TODO: unlet {dict}[key]
5790 emsg("Sorry, :unlet not fully implemented yet");
5791 return FAIL;
5792}
5793
5794/*
5795 * compile "unlet var", "lock var" and "unlock var"
5796 * "arg" points to "var".
5797 */
5798 static char_u *
5799compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5800{
5801 char_u *p = arg;
5802
5803 if (eap->cmdidx != CMD_unlet)
5804 {
5805 emsg("Sorry, :lock and unlock not implemented yet");
5806 return NULL;
5807 }
5808
5809 if (*p == '!')
5810 {
5811 p = skipwhite(p + 1);
5812 eap->forceit = TRUE;
5813 }
5814
5815 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5816 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5817}
5818
5819/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820 * Compile an :import command.
5821 */
5822 static char_u *
5823compile_import(char_u *arg, cctx_T *cctx)
5824{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005825 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005826}
5827
5828/*
5829 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5830 */
5831 static int
5832compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5833{
5834 garray_T *instr = &cctx->ctx_instr;
5835 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5836
5837 if (endlabel == NULL)
5838 return FAIL;
5839 endlabel->el_next = *el;
5840 *el = endlabel;
5841 endlabel->el_end_label = instr->ga_len;
5842
5843 generate_JUMP(cctx, when, 0);
5844 return OK;
5845}
5846
5847 static void
5848compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5849{
5850 garray_T *instr = &cctx->ctx_instr;
5851
5852 while (*el != NULL)
5853 {
5854 endlabel_T *cur = (*el);
5855 isn_T *isn;
5856
5857 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5858 isn->isn_arg.jump.jump_where = instr->ga_len;
5859 *el = cur->el_next;
5860 vim_free(cur);
5861 }
5862}
5863
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005864 static void
5865compile_free_jump_to_end(endlabel_T **el)
5866{
5867 while (*el != NULL)
5868 {
5869 endlabel_T *cur = (*el);
5870
5871 *el = cur->el_next;
5872 vim_free(cur);
5873 }
5874}
5875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005876/*
5877 * Create a new scope and set up the generic items.
5878 */
5879 static scope_T *
5880new_scope(cctx_T *cctx, scopetype_T type)
5881{
5882 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5883
5884 if (scope == NULL)
5885 return NULL;
5886 scope->se_outer = cctx->ctx_scope;
5887 cctx->ctx_scope = scope;
5888 scope->se_type = type;
5889 scope->se_local_count = cctx->ctx_locals.ga_len;
5890 return scope;
5891}
5892
5893/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005894 * Free the current scope and go back to the outer scope.
5895 */
5896 static void
5897drop_scope(cctx_T *cctx)
5898{
5899 scope_T *scope = cctx->ctx_scope;
5900
5901 if (scope == NULL)
5902 {
5903 iemsg("calling drop_scope() without a scope");
5904 return;
5905 }
5906 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005907 switch (scope->se_type)
5908 {
5909 case IF_SCOPE:
5910 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5911 case FOR_SCOPE:
5912 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5913 case WHILE_SCOPE:
5914 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5915 case TRY_SCOPE:
5916 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5917 case NO_SCOPE:
5918 case BLOCK_SCOPE:
5919 break;
5920 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005921 vim_free(scope);
5922}
5923
5924/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005925 * compile "if expr"
5926 *
5927 * "if expr" Produces instructions:
5928 * EVAL expr Push result of "expr"
5929 * JUMP_IF_FALSE end
5930 * ... body ...
5931 * end:
5932 *
5933 * "if expr | else" Produces instructions:
5934 * EVAL expr Push result of "expr"
5935 * JUMP_IF_FALSE else
5936 * ... body ...
5937 * JUMP_ALWAYS end
5938 * else:
5939 * ... body ...
5940 * end:
5941 *
5942 * "if expr1 | elseif expr2 | else" Produces instructions:
5943 * EVAL expr Push result of "expr"
5944 * JUMP_IF_FALSE elseif
5945 * ... body ...
5946 * JUMP_ALWAYS end
5947 * elseif:
5948 * EVAL expr Push result of "expr"
5949 * JUMP_IF_FALSE else
5950 * ... body ...
5951 * JUMP_ALWAYS end
5952 * else:
5953 * ... body ...
5954 * end:
5955 */
5956 static char_u *
5957compile_if(char_u *arg, cctx_T *cctx)
5958{
5959 char_u *p = arg;
5960 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005961 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005962 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005963 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005964 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005965
Bram Moolenaara5565e42020-05-09 15:44:01 +02005966 CLEAR_FIELD(ppconst);
5967 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005968 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005969 clear_ppconst(&ppconst);
5970 return NULL;
5971 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005972 if (cctx->ctx_skip == SKIP_YES)
5973 clear_ppconst(&ppconst);
5974 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005975 {
5976 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005977 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005978 clear_ppconst(&ppconst);
5979 }
5980 else
5981 {
5982 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005983 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005984 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005985 return NULL;
5986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987
5988 scope = new_scope(cctx, IF_SCOPE);
5989 if (scope == NULL)
5990 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005991 scope->se_skip_save = skip_save;
5992 // "is_had_return" will be reset if any block does not end in :return
5993 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005995 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005996 {
5997 // "where" is set when ":elseif", "else" or ":endif" is found
5998 scope->se_u.se_if.is_if_label = instr->ga_len;
5999 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6000 }
6001 else
6002 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003
6004 return p;
6005}
6006
6007 static char_u *
6008compile_elseif(char_u *arg, cctx_T *cctx)
6009{
6010 char_u *p = arg;
6011 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006012 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006013 isn_T *isn;
6014 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006015 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016
6017 if (scope == NULL || scope->se_type != IF_SCOPE)
6018 {
6019 emsg(_(e_elseif_without_if));
6020 return NULL;
6021 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006022 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006023 if (!cctx->ctx_had_return)
6024 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006025
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006026 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006027 {
6028 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006030 return NULL;
6031 // previous "if" or "elseif" jumps here
6032 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6033 isn->isn_arg.jump.jump_where = instr->ga_len;
6034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006036 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006037 CLEAR_FIELD(ppconst);
6038 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006039 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006040 clear_ppconst(&ppconst);
6041 return NULL;
6042 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006043 if (scope->se_skip_save == SKIP_YES)
6044 clear_ppconst(&ppconst);
6045 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006046 {
6047 // The expression results in a constant.
6048 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006049 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006050 clear_ppconst(&ppconst);
6051 scope->se_u.se_if.is_if_label = -1;
6052 }
6053 else
6054 {
6055 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006056 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006057 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006058 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006060 // "where" is set when ":elseif", "else" or ":endif" is found
6061 scope->se_u.se_if.is_if_label = instr->ga_len;
6062 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6063 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064
6065 return p;
6066}
6067
6068 static char_u *
6069compile_else(char_u *arg, cctx_T *cctx)
6070{
6071 char_u *p = arg;
6072 garray_T *instr = &cctx->ctx_instr;
6073 isn_T *isn;
6074 scope_T *scope = cctx->ctx_scope;
6075
6076 if (scope == NULL || scope->se_type != IF_SCOPE)
6077 {
6078 emsg(_(e_else_without_if));
6079 return NULL;
6080 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006081 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006082 if (!cctx->ctx_had_return)
6083 scope->se_u.se_if.is_had_return = FALSE;
6084 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006085
Bram Moolenaarefd88552020-06-18 20:50:10 +02006086 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006087 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006088 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006089 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006090 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006091 if (!cctx->ctx_had_return
6092 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6093 JUMP_ALWAYS, cctx) == FAIL)
6094 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006095 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006096
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006097 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006098 {
6099 if (scope->se_u.se_if.is_if_label >= 0)
6100 {
6101 // previous "if" or "elseif" jumps here
6102 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6103 isn->isn_arg.jump.jump_where = instr->ga_len;
6104 scope->se_u.se_if.is_if_label = -1;
6105 }
6106 }
6107
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006108 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006109 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111
6112 return p;
6113}
6114
6115 static char_u *
6116compile_endif(char_u *arg, cctx_T *cctx)
6117{
6118 scope_T *scope = cctx->ctx_scope;
6119 ifscope_T *ifscope;
6120 garray_T *instr = &cctx->ctx_instr;
6121 isn_T *isn;
6122
6123 if (scope == NULL || scope->se_type != IF_SCOPE)
6124 {
6125 emsg(_(e_endif_without_if));
6126 return NULL;
6127 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006128 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006129 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006130 if (!cctx->ctx_had_return)
6131 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006132
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006133 if (scope->se_u.se_if.is_if_label >= 0)
6134 {
6135 // previous "if" or "elseif" jumps here
6136 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6137 isn->isn_arg.jump.jump_where = instr->ga_len;
6138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139 // Fill in the "end" label in jumps at the end of the blocks.
6140 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006141 cctx->ctx_skip = scope->se_skip_save;
6142
6143 // If all the blocks end in :return and there is an :else then the
6144 // had_return flag is set.
6145 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006147 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006148 return arg;
6149}
6150
6151/*
6152 * compile "for var in expr"
6153 *
6154 * Produces instructions:
6155 * PUSHNR -1
6156 * STORE loop-idx Set index to -1
6157 * EVAL expr Push result of "expr"
6158 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6159 * - if beyond end, jump to "end"
6160 * - otherwise get item from list and push it
6161 * STORE var Store item in "var"
6162 * ... body ...
6163 * JUMP top Jump back to repeat
6164 * end: DROP Drop the result of "expr"
6165 *
6166 */
6167 static char_u *
6168compile_for(char_u *arg, cctx_T *cctx)
6169{
6170 char_u *p;
6171 size_t varlen;
6172 garray_T *instr = &cctx->ctx_instr;
6173 garray_T *stack = &cctx->ctx_type_stack;
6174 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006175 lvar_T *loop_lvar; // loop iteration variable
6176 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177 type_T *vartype;
6178
6179 // TODO: list of variables: "for [key, value] in dict"
6180 // parse "var"
6181 for (p = arg; eval_isnamec1(*p); ++p)
6182 ;
6183 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006184 var_lvar = lookup_local(arg, varlen, cctx);
6185 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006186 {
6187 semsg(_("E1023: variable already defined: %s"), arg);
6188 return NULL;
6189 }
6190
6191 // consume "in"
6192 p = skipwhite(p);
6193 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6194 {
6195 emsg(_(e_missing_in));
6196 return NULL;
6197 }
6198 p = skipwhite(p + 2);
6199
6200
6201 scope = new_scope(cctx, FOR_SCOPE);
6202 if (scope == NULL)
6203 return NULL;
6204
6205 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006206 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6207 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006208 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006209 // out of memory
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
6214 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006215 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6216 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006217 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006218 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006219 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006220 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006223 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224
6225 // compile "expr", it remains on the stack until "endfor"
6226 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006227 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006228 {
6229 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006233 // Now that we know the type of "var", check that it is a list, now or at
6234 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006236 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006238 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006239 return NULL;
6240 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006241 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006242 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243
6244 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006245 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006247 generate_FOR(cctx, loop_lvar->lv_idx);
6248 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006249
6250 return arg;
6251}
6252
6253/*
6254 * compile "endfor"
6255 */
6256 static char_u *
6257compile_endfor(char_u *arg, cctx_T *cctx)
6258{
6259 garray_T *instr = &cctx->ctx_instr;
6260 scope_T *scope = cctx->ctx_scope;
6261 forscope_T *forscope;
6262 isn_T *isn;
6263
6264 if (scope == NULL || scope->se_type != FOR_SCOPE)
6265 {
6266 emsg(_(e_for));
6267 return NULL;
6268 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006269 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006271 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272
6273 // At end of ":for" scope jump back to the FOR instruction.
6274 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6275
6276 // Fill in the "end" label in the FOR statement so it can jump here
6277 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6278 isn->isn_arg.forloop.for_end = instr->ga_len;
6279
6280 // Fill in the "end" label any BREAK statements
6281 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6282
6283 // Below the ":for" scope drop the "expr" list from the stack.
6284 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6285 return NULL;
6286
6287 vim_free(scope);
6288
6289 return arg;
6290}
6291
6292/*
6293 * compile "while expr"
6294 *
6295 * Produces instructions:
6296 * top: EVAL expr Push result of "expr"
6297 * JUMP_IF_FALSE end jump if false
6298 * ... body ...
6299 * JUMP top Jump back to repeat
6300 * end:
6301 *
6302 */
6303 static char_u *
6304compile_while(char_u *arg, cctx_T *cctx)
6305{
6306 char_u *p = arg;
6307 garray_T *instr = &cctx->ctx_instr;
6308 scope_T *scope;
6309
6310 scope = new_scope(cctx, WHILE_SCOPE);
6311 if (scope == NULL)
6312 return NULL;
6313
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006314 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315
6316 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006317 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 return NULL;
6319
6320 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006321 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 JUMP_IF_FALSE, cctx) == FAIL)
6323 return FAIL;
6324
6325 return p;
6326}
6327
6328/*
6329 * compile "endwhile"
6330 */
6331 static char_u *
6332compile_endwhile(char_u *arg, cctx_T *cctx)
6333{
6334 scope_T *scope = cctx->ctx_scope;
6335
6336 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6337 {
6338 emsg(_(e_while));
6339 return NULL;
6340 }
6341 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006342 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343
6344 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006345 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346
6347 // Fill in the "end" label in the WHILE statement so it can jump here.
6348 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006349 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350
6351 vim_free(scope);
6352
6353 return arg;
6354}
6355
6356/*
6357 * compile "continue"
6358 */
6359 static char_u *
6360compile_continue(char_u *arg, cctx_T *cctx)
6361{
6362 scope_T *scope = cctx->ctx_scope;
6363
6364 for (;;)
6365 {
6366 if (scope == NULL)
6367 {
6368 emsg(_(e_continue));
6369 return NULL;
6370 }
6371 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6372 break;
6373 scope = scope->se_outer;
6374 }
6375
6376 // Jump back to the FOR or WHILE instruction.
6377 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006378 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6379 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380 return arg;
6381}
6382
6383/*
6384 * compile "break"
6385 */
6386 static char_u *
6387compile_break(char_u *arg, cctx_T *cctx)
6388{
6389 scope_T *scope = cctx->ctx_scope;
6390 endlabel_T **el;
6391
6392 for (;;)
6393 {
6394 if (scope == NULL)
6395 {
6396 emsg(_(e_break));
6397 return NULL;
6398 }
6399 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6400 break;
6401 scope = scope->se_outer;
6402 }
6403
6404 // Jump to the end of the FOR or WHILE loop.
6405 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006406 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006407 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006408 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6410 return FAIL;
6411
6412 return arg;
6413}
6414
6415/*
6416 * compile "{" start of block
6417 */
6418 static char_u *
6419compile_block(char_u *arg, cctx_T *cctx)
6420{
6421 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6422 return NULL;
6423 return skipwhite(arg + 1);
6424}
6425
6426/*
6427 * compile end of block: drop one scope
6428 */
6429 static void
6430compile_endblock(cctx_T *cctx)
6431{
6432 scope_T *scope = cctx->ctx_scope;
6433
6434 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006435 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436 vim_free(scope);
6437}
6438
6439/*
6440 * compile "try"
6441 * Creates a new scope for the try-endtry, pointing to the first catch and
6442 * finally.
6443 * Creates another scope for the "try" block itself.
6444 * TRY instruction sets up exception handling at runtime.
6445 *
6446 * "try"
6447 * TRY -> catch1, -> finally push trystack entry
6448 * ... try block
6449 * "throw {exception}"
6450 * EVAL {exception}
6451 * THROW create exception
6452 * ... try block
6453 * " catch {expr}"
6454 * JUMP -> finally
6455 * catch1: PUSH exeception
6456 * EVAL {expr}
6457 * MATCH
6458 * JUMP nomatch -> catch2
6459 * CATCH remove exception
6460 * ... catch block
6461 * " catch"
6462 * JUMP -> finally
6463 * catch2: CATCH remove exception
6464 * ... catch block
6465 * " finally"
6466 * finally:
6467 * ... finally block
6468 * " endtry"
6469 * ENDTRY pop trystack entry, may rethrow
6470 */
6471 static char_u *
6472compile_try(char_u *arg, cctx_T *cctx)
6473{
6474 garray_T *instr = &cctx->ctx_instr;
6475 scope_T *try_scope;
6476 scope_T *scope;
6477
6478 // scope that holds the jumps that go to catch/finally/endtry
6479 try_scope = new_scope(cctx, TRY_SCOPE);
6480 if (try_scope == NULL)
6481 return NULL;
6482
6483 // "catch" is set when the first ":catch" is found.
6484 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006485 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 if (generate_instr(cctx, ISN_TRY) == NULL)
6487 return NULL;
6488
6489 // scope for the try block itself
6490 scope = new_scope(cctx, BLOCK_SCOPE);
6491 if (scope == NULL)
6492 return NULL;
6493
6494 return arg;
6495}
6496
6497/*
6498 * compile "catch {expr}"
6499 */
6500 static char_u *
6501compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6502{
6503 scope_T *scope = cctx->ctx_scope;
6504 garray_T *instr = &cctx->ctx_instr;
6505 char_u *p;
6506 isn_T *isn;
6507
6508 // end block scope from :try or :catch
6509 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6510 compile_endblock(cctx);
6511 scope = cctx->ctx_scope;
6512
6513 // Error if not in a :try scope
6514 if (scope == NULL || scope->se_type != TRY_SCOPE)
6515 {
6516 emsg(_(e_catch));
6517 return NULL;
6518 }
6519
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006520 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521 {
6522 emsg(_("E1033: catch unreachable after catch-all"));
6523 return NULL;
6524 }
6525
6526 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006527 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 JUMP_ALWAYS, cctx) == FAIL)
6529 return NULL;
6530
6531 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006532 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 if (isn->isn_arg.try.try_catch == 0)
6534 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006535 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006536 {
6537 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006538 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 isn->isn_arg.jump.jump_where = instr->ga_len;
6540 }
6541
6542 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006543 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006545 scope->se_u.se_try.ts_caught_all = TRUE;
6546 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 }
6548 else
6549 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006550 char_u *end;
6551 char_u *pat;
6552 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006553 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006554 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556 // Push v:exception, push {expr} and MATCH
6557 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6558
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006559 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006560 if (*end != *p)
6561 {
6562 semsg(_("E1067: Separator mismatch: %s"), p);
6563 vim_free(tofree);
6564 return FAIL;
6565 }
6566 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006567 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006568 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006569 len = (int)(end - tofree);
6570 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006571 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006572 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006573 if (pat == NULL)
6574 return FAIL;
6575 if (generate_PUSHS(cctx, pat) == FAIL)
6576 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6579 return NULL;
6580
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006581 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6583 return NULL;
6584 }
6585
6586 if (generate_instr(cctx, ISN_CATCH) == NULL)
6587 return NULL;
6588
6589 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6590 return NULL;
6591 return p;
6592}
6593
6594 static char_u *
6595compile_finally(char_u *arg, cctx_T *cctx)
6596{
6597 scope_T *scope = cctx->ctx_scope;
6598 garray_T *instr = &cctx->ctx_instr;
6599 isn_T *isn;
6600
6601 // end block scope from :try or :catch
6602 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6603 compile_endblock(cctx);
6604 scope = cctx->ctx_scope;
6605
6606 // Error if not in a :try scope
6607 if (scope == NULL || scope->se_type != TRY_SCOPE)
6608 {
6609 emsg(_(e_finally));
6610 return NULL;
6611 }
6612
6613 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006614 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 if (isn->isn_arg.try.try_finally != 0)
6616 {
6617 emsg(_(e_finally_dup));
6618 return NULL;
6619 }
6620
6621 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006622 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006623
Bram Moolenaar585fea72020-04-02 22:33:21 +02006624 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006625 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626 {
6627 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006628 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006630 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631 }
6632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633 // TODO: set index in ts_finally_label jumps
6634
6635 return arg;
6636}
6637
6638 static char_u *
6639compile_endtry(char_u *arg, cctx_T *cctx)
6640{
6641 scope_T *scope = cctx->ctx_scope;
6642 garray_T *instr = &cctx->ctx_instr;
6643 isn_T *isn;
6644
6645 // end block scope from :catch or :finally
6646 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6647 compile_endblock(cctx);
6648 scope = cctx->ctx_scope;
6649
6650 // Error if not in a :try scope
6651 if (scope == NULL || scope->se_type != TRY_SCOPE)
6652 {
6653 if (scope == NULL)
6654 emsg(_(e_no_endtry));
6655 else if (scope->se_type == WHILE_SCOPE)
6656 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006657 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006658 emsg(_(e_endfor));
6659 else
6660 emsg(_(e_endif));
6661 return NULL;
6662 }
6663
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006664 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6666 {
6667 emsg(_("E1032: missing :catch or :finally"));
6668 return NULL;
6669 }
6670
6671 // Fill in the "end" label in jumps at the end of the blocks, if not done
6672 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006673 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674
6675 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006676 if (isn->isn_arg.try.try_catch == 0)
6677 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 if (isn->isn_arg.try.try_finally == 0)
6679 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006680
6681 if (scope->se_u.se_try.ts_catch_label != 0)
6682 {
6683 // Last catch without match jumps here
6684 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6685 isn->isn_arg.jump.jump_where = instr->ga_len;
6686 }
6687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 compile_endblock(cctx);
6689
6690 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6691 return NULL;
6692 return arg;
6693}
6694
6695/*
6696 * compile "throw {expr}"
6697 */
6698 static char_u *
6699compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6700{
6701 char_u *p = skipwhite(arg);
6702
Bram Moolenaara5565e42020-05-09 15:44:01 +02006703 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 return NULL;
6705 if (may_generate_2STRING(-1, cctx) == FAIL)
6706 return NULL;
6707 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6708 return NULL;
6709
6710 return p;
6711}
6712
6713/*
6714 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006715 * compile "echomsg expr"
6716 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006717 * compile "execute expr"
6718 */
6719 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006720compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006721{
6722 char_u *p = arg;
6723 int count = 0;
6724
6725 for (;;)
6726 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006727 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006728 return NULL;
6729 ++count;
6730 p = skipwhite(p);
6731 if (ends_excmd(*p))
6732 break;
6733 }
6734
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006735 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6736 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6737 else if (cmdidx == CMD_execute)
6738 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6739 else if (cmdidx == CMD_echomsg)
6740 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6741 else
6742 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006743 return p;
6744}
6745
6746/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006747 * A command that is not compiled, execute with legacy code.
6748 */
6749 static char_u *
6750compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6751{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006752 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006753 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006754 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006755
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006756 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006757 goto theend;
6758
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006759 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006760 {
6761 long argt = excmd_get_argt(eap->cmdidx);
6762 int usefilter = FALSE;
6763
6764 has_expr = argt & (EX_XFILE | EX_EXPAND);
6765
6766 // If the command can be followed by a bar, find the bar and truncate
6767 // it, so that the following command can be compiled.
6768 // The '|' is overwritten with a NUL, it is put back below.
6769 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6770 && *eap->arg == '!')
6771 // :w !filter or :r !filter or :r! filter
6772 usefilter = TRUE;
6773 if ((argt & EX_TRLBAR) && !usefilter)
6774 {
6775 separate_nextcmd(eap);
6776 if (eap->nextcmd != NULL)
6777 nextcmd = eap->nextcmd;
6778 }
6779 }
6780
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006781 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6782 {
6783 // expand filename in "syntax include [@group] filename"
6784 has_expr = TRUE;
6785 eap->arg = skipwhite(eap->arg + 7);
6786 if (*eap->arg == '@')
6787 eap->arg = skiptowhite(eap->arg);
6788 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006789
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006790 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006791 {
6792 int count = 0;
6793 char_u *start = skipwhite(line);
6794
6795 // :cmd xxx`=expr1`yyy`=expr2`zzz
6796 // PUSHS ":cmd xxx"
6797 // eval expr1
6798 // PUSHS "yyy"
6799 // eval expr2
6800 // PUSHS "zzz"
6801 // EXECCONCAT 5
6802 for (;;)
6803 {
6804 if (p > start)
6805 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006806 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006807 ++count;
6808 }
6809 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006810 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006811 return NULL;
6812 may_generate_2STRING(-1, cctx);
6813 ++count;
6814 p = skipwhite(p);
6815 if (*p != '`')
6816 {
6817 emsg(_("E1083: missing backtick"));
6818 return NULL;
6819 }
6820 start = p + 1;
6821
6822 p = (char_u *)strstr((char *)start, "`=");
6823 if (p == NULL)
6824 {
6825 if (*skipwhite(start) != NUL)
6826 {
6827 generate_PUSHS(cctx, vim_strsave(start));
6828 ++count;
6829 }
6830 break;
6831 }
6832 }
6833 generate_EXECCONCAT(cctx, count);
6834 }
6835 else
6836 generate_EXEC(cctx, line);
6837
6838theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006839 if (*nextcmd != NUL)
6840 {
6841 // the parser expects a pointer to the bar, put it back
6842 --nextcmd;
6843 *nextcmd = '|';
6844 }
6845
6846 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006847}
6848
6849/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006850 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006851 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006852 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006853 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006854add_def_function(ufunc_T *ufunc)
6855{
6856 dfunc_T *dfunc;
6857
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006858 if (def_functions.ga_len == 0)
6859 {
6860 // The first position is not used, so that a zero uf_dfunc_idx means it
6861 // wasn't set.
6862 if (ga_grow(&def_functions, 1) == FAIL)
6863 return FAIL;
6864 ++def_functions.ga_len;
6865 }
6866
Bram Moolenaar09689a02020-05-09 22:50:08 +02006867 // Add the function to "def_functions".
6868 if (ga_grow(&def_functions, 1) == FAIL)
6869 return FAIL;
6870 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6871 CLEAR_POINTER(dfunc);
6872 dfunc->df_idx = def_functions.ga_len;
6873 ufunc->uf_dfunc_idx = dfunc->df_idx;
6874 dfunc->df_ufunc = ufunc;
6875 ++def_functions.ga_len;
6876 return OK;
6877}
6878
6879/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 * After ex_function() has collected all the function lines: parse and compile
6881 * the lines into instructions.
6882 * Adds the function to "def_functions".
6883 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6884 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006885 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006886 * This can be used recursively through compile_lambda(), which may reallocate
6887 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006888 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006890 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006891compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006893 char_u *line = NULL;
6894 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006896 cctx_T cctx;
6897 garray_T *instr;
6898 int called_emsg_before = called_emsg;
6899 int ret = FAIL;
6900 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006901 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006902 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006904 // When using a function that was compiled before: Free old instructions.
6905 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006906 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006907 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006908 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6909 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006910 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006911 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006912 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006913 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914
Bram Moolenaar985116a2020-07-12 17:31:09 +02006915 ufunc->uf_def_status = UF_COMPILING;
6916
Bram Moolenaara80faa82020-04-12 19:37:17 +02006917 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918 cctx.ctx_ufunc = ufunc;
6919 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006920 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6922 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6923 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6924 cctx.ctx_type_list = &ufunc->uf_type_list;
6925 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6926 instr = &cctx.ctx_instr;
6927
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006928 // Set the context to the function, it may be compiled when called from
6929 // another script. Set the script version to the most modern one.
6930 // The line number will be set in next_line_from_context().
6931 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6933
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006934 // Make sure error messages are OK.
6935 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6936 if (do_estack_push)
6937 estack_push_ufunc(ufunc, 1);
6938
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006939 if (ufunc->uf_def_args.ga_len > 0)
6940 {
6941 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006942 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006943 int i;
6944 char_u *arg;
6945 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6946
6947 // Produce instructions for the default values of optional arguments.
6948 // Store the instruction index in uf_def_arg_idx[] so that we know
6949 // where to start when the function is called, depending on the number
6950 // of arguments.
6951 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6952 if (ufunc->uf_def_arg_idx == NULL)
6953 goto erret;
6954 for (i = 0; i < count; ++i)
6955 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006956 garray_T *stack = &cctx.ctx_type_stack;
6957 type_T *val_type;
6958 int arg_idx = first_def_arg + i;
6959
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006960 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6961 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006962 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006963 goto erret;
6964
6965 // If no type specified use the type of the default value.
6966 // Otherwise check that the default value type matches the
6967 // specified type.
6968 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6969 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6970 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006971 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006972 == FAIL)
6973 {
6974 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6975 arg_idx + 1);
6976 goto erret;
6977 }
6978
6979 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006980 goto erret;
6981 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006982 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6983 }
6984
6985 /*
6986 * Loop over all the lines of the function and generate instructions.
6987 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988 for (;;)
6989 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006990 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006991 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006992 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006993 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006994
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006995 // Bail out on the first error to avoid a flood of errors and report
6996 // the right line number when inside try/catch.
6997 if (emsg_before != called_emsg)
6998 goto erret;
6999
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 if (line != NULL && *line == '|')
7001 // the line continues after a '|'
7002 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007003 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007004 && !(*line == '#' && (line == cctx.ctx_line_start
7005 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007006 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007007 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007008 goto erret;
7009 }
7010 else
7011 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007012 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007013 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007014 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007016 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007017 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018
Bram Moolenaara80faa82020-04-12 19:37:17 +02007019 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020 ea.cmdlinep = &line;
7021 ea.cmd = skipwhite(line);
7022
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007023 // Some things can be recognized by the first character.
7024 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007026 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007027 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007028 if (ea.cmd[1] != '{')
7029 {
7030 line = (char_u *)"";
7031 continue;
7032 }
7033 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007035 case '}':
7036 {
7037 // "}" ends a block scope
7038 scopetype_T stype = cctx.ctx_scope == NULL
7039 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007041 if (stype == BLOCK_SCOPE)
7042 {
7043 compile_endblock(&cctx);
7044 line = ea.cmd;
7045 }
7046 else
7047 {
7048 emsg(_("E1025: using } outside of a block scope"));
7049 goto erret;
7050 }
7051 if (line != NULL)
7052 line = skipwhite(ea.cmd + 1);
7053 continue;
7054 }
7055
7056 case '{':
7057 // "{" starts a block scope
7058 // "{'a': 1}->func() is something else
7059 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7060 {
7061 line = compile_block(ea.cmd, &cctx);
7062 continue;
7063 }
7064 break;
7065
7066 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007067 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007068 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 }
7070
7071 /*
7072 * COMMAND MODIFIERS
7073 */
7074 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7075 {
7076 if (errormsg != NULL)
7077 goto erret;
7078 // empty line or comment
7079 line = (char_u *)"";
7080 continue;
7081 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007082 // TODO: use modifiers in the command
7083 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007084 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007085
7086 // Skip ":call" to get to the function name.
7087 if (checkforcmd(&ea.cmd, "call", 3))
7088 ea.cmd = skipwhite(ea.cmd);
7089
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007090 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007091 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007092 char_u *pskip;
7093
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007094 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007095 // find what follows.
7096 // Skip over "var.member", "var[idx]" and the like.
7097 // Also "&opt = val", "$ENV = val" and "@r = val".
7098 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007099 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007100 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007101 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007103 char_u *var_end;
7104 int oplen;
7105 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007106
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007107 var_end = find_name_end(pskip, NULL, NULL,
7108 FNE_CHECK_START | FNE_INCL_BR);
7109 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007110 if (oplen > 0)
7111 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007112 size_t len = p - ea.cmd;
7113
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007114 // Recognize an assignment if we recognize the variable
7115 // name:
7116 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007117 // "local = expr" where "local" is a local var.
7118 // "script = expr" where "script" is a script-local var.
7119 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007120 // "&opt = expr"
7121 // "$ENV = expr"
7122 // "@r = expr"
7123 if (*ea.cmd == '&'
7124 || *ea.cmd == '$'
7125 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007126 || ((len) > 2 && ea.cmd[1] == ':')
7127 || lookup_local(ea.cmd, len, &cctx) != NULL
7128 || lookup_arg(ea.cmd, len, NULL, NULL,
7129 NULL, &cctx) == OK
7130 || lookup_script(ea.cmd, len) == OK
7131 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007132 {
7133 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007134 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007135 goto erret;
7136 continue;
7137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 }
7139 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007140
7141 if (*ea.cmd == '[')
7142 {
7143 // [var, var] = expr
7144 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7145 if (line == NULL)
7146 goto erret;
7147 if (line != ea.cmd)
7148 continue;
7149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007150 }
7151
7152 /*
7153 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007154 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007156 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007157 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007158 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007159 ea.cmd = skip_range(ea.cmd, NULL);
7160 if (ea.cmd > cmd && !starts_with_colon)
7161 {
7162 emsg(_(e_colon_required));
7163 goto erret;
7164 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007165 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007166 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007167 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007168 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169
7170 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7171 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007172 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007173 {
7174 line += STRLEN(line);
7175 continue;
7176 }
7177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007179 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007181 // CMD_let cannot happen, compile_assignment() above is used
7182 iemsg("Command from find_ex_command() not handled");
7183 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185 }
7186
7187 p = skipwhite(p);
7188
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007189 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007190 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007191 && ea.cmdidx != CMD_elseif
7192 && ea.cmdidx != CMD_else
7193 && ea.cmdidx != CMD_endif)
7194 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007195 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007196 continue;
7197 }
7198
Bram Moolenaarefd88552020-06-18 20:50:10 +02007199 if (ea.cmdidx != CMD_elseif
7200 && ea.cmdidx != CMD_else
7201 && ea.cmdidx != CMD_endif
7202 && ea.cmdidx != CMD_endfor
7203 && ea.cmdidx != CMD_endwhile
7204 && ea.cmdidx != CMD_catch
7205 && ea.cmdidx != CMD_finally
7206 && ea.cmdidx != CMD_endtry)
7207 {
7208 if (cctx.ctx_had_return)
7209 {
7210 emsg(_("E1095: Unreachable code after :return"));
7211 goto erret;
7212 }
7213 }
7214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215 switch (ea.cmdidx)
7216 {
7217 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007218 ea.arg = p;
7219 line = compile_nested_function(&ea, &cctx);
7220 break;
7221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007223 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007224 goto erret;
7225
7226 case CMD_return:
7227 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007228 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007229 break;
7230
7231 case CMD_let:
7232 case CMD_const:
7233 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007234 if (line == p)
7235 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236 break;
7237
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007238 case CMD_unlet:
7239 case CMD_unlockvar:
7240 case CMD_lockvar:
7241 line = compile_unletlock(p, &ea, &cctx);
7242 break;
7243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244 case CMD_import:
7245 line = compile_import(p, &cctx);
7246 break;
7247
7248 case CMD_if:
7249 line = compile_if(p, &cctx);
7250 break;
7251 case CMD_elseif:
7252 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007253 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254 break;
7255 case CMD_else:
7256 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007257 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 break;
7259 case CMD_endif:
7260 line = compile_endif(p, &cctx);
7261 break;
7262
7263 case CMD_while:
7264 line = compile_while(p, &cctx);
7265 break;
7266 case CMD_endwhile:
7267 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007268 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269 break;
7270
7271 case CMD_for:
7272 line = compile_for(p, &cctx);
7273 break;
7274 case CMD_endfor:
7275 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007276 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 break;
7278 case CMD_continue:
7279 line = compile_continue(p, &cctx);
7280 break;
7281 case CMD_break:
7282 line = compile_break(p, &cctx);
7283 break;
7284
7285 case CMD_try:
7286 line = compile_try(p, &cctx);
7287 break;
7288 case CMD_catch:
7289 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007290 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007291 break;
7292 case CMD_finally:
7293 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007294 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295 break;
7296 case CMD_endtry:
7297 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007298 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007299 break;
7300 case CMD_throw:
7301 line = compile_throw(p, &cctx);
7302 break;
7303
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007304 case CMD_eval:
7305 if (compile_expr0(&p, &cctx) == FAIL)
7306 goto erret;
7307
7308 // drop the return value
7309 generate_instr_drop(&cctx, ISN_DROP, 1);
7310
7311 line = skipwhite(p);
7312 break;
7313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007316 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007317 case CMD_echomsg:
7318 case CMD_echoerr:
7319 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007320 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007322 // TODO: other commands with an expression argument
7323
Bram Moolenaar002262f2020-07-08 17:47:57 +02007324 case CMD_SIZE:
7325 semsg(_("E476: Invalid command: %s"), ea.cmd);
7326 goto erret;
7327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007329 // Not recognized, execute with do_cmdline_cmd().
7330 ea.arg = p;
7331 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332 break;
7333 }
7334 if (line == NULL)
7335 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007336 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337
7338 if (cctx.ctx_type_stack.ga_len < 0)
7339 {
7340 iemsg("Type stack underflow");
7341 goto erret;
7342 }
7343 }
7344
7345 if (cctx.ctx_scope != NULL)
7346 {
7347 if (cctx.ctx_scope->se_type == IF_SCOPE)
7348 emsg(_(e_endif));
7349 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7350 emsg(_(e_endwhile));
7351 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7352 emsg(_(e_endfor));
7353 else
7354 emsg(_("E1026: Missing }"));
7355 goto erret;
7356 }
7357
Bram Moolenaarefd88552020-06-18 20:50:10 +02007358 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007359 {
7360 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7361 {
7362 emsg(_("E1027: Missing return statement"));
7363 goto erret;
7364 }
7365
7366 // Return zero if there is no return at the end.
7367 generate_PUSHNR(&cctx, 0);
7368 generate_instr(&cctx, ISN_RETURN);
7369 }
7370
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007371 {
7372 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7373 + ufunc->uf_dfunc_idx;
7374 dfunc->df_deleted = FALSE;
7375 dfunc->df_instr = instr->ga_data;
7376 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007377 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007378 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007379 if (cctx.ctx_outer_used)
7380 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007381 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007382 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383
7384 ret = OK;
7385
7386erret:
7387 if (ret == FAIL)
7388 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007389 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007390 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7391 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007392
7393 for (idx = 0; idx < instr->ga_len; ++idx)
7394 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007396
Bram Moolenaar45a15082020-05-25 00:28:33 +02007397 // if using the last entry in the table we might as well remove it
7398 if (!dfunc->df_deleted
7399 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007400 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007401 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007402
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007403 while (cctx.ctx_scope != NULL)
7404 drop_scope(&cctx);
7405
Bram Moolenaar20431c92020-03-20 18:39:46 +01007406 // Don't execute this function body.
7407 ga_clear_strings(&ufunc->uf_lines);
7408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409 if (errormsg != NULL)
7410 emsg(errormsg);
7411 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007412 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413 }
7414
7415 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007416 if (do_estack_push)
7417 estack_pop();
7418
Bram Moolenaar20431c92020-03-20 18:39:46 +01007419 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007420 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007422 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423}
7424
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007425 void
7426set_function_type(ufunc_T *ufunc)
7427{
7428 int varargs = ufunc->uf_va_name != NULL;
7429 int argcount = ufunc->uf_args.ga_len;
7430
7431 // Create a type for the function, with the return type and any
7432 // argument types.
7433 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7434 // The type is included in "tt_args".
7435 if (argcount > 0 || varargs)
7436 {
7437 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7438 argcount, &ufunc->uf_type_list);
7439 // Add argument types to the function type.
7440 if (func_type_add_arg_types(ufunc->uf_func_type,
7441 argcount + varargs,
7442 &ufunc->uf_type_list) == FAIL)
7443 return;
7444 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7445 ufunc->uf_func_type->tt_min_argcount =
7446 argcount - ufunc->uf_def_args.ga_len;
7447 if (ufunc->uf_arg_types == NULL)
7448 {
7449 int i;
7450
7451 // lambda does not have argument types.
7452 for (i = 0; i < argcount; ++i)
7453 ufunc->uf_func_type->tt_args[i] = &t_any;
7454 }
7455 else
7456 mch_memmove(ufunc->uf_func_type->tt_args,
7457 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7458 if (varargs)
7459 {
7460 ufunc->uf_func_type->tt_args[argcount] =
7461 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7462 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7463 }
7464 }
7465 else
7466 // No arguments, can use a predefined type.
7467 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7468 argcount, &ufunc->uf_type_list);
7469}
7470
7471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472/*
7473 * Delete an instruction, free what it contains.
7474 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007475 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476delete_instr(isn_T *isn)
7477{
7478 switch (isn->isn_type)
7479 {
7480 case ISN_EXEC:
7481 case ISN_LOADENV:
7482 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007483 case ISN_LOADB:
7484 case ISN_LOADW:
7485 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007487 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 case ISN_PUSHEXC:
7489 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007490 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007491 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007492 case ISN_STOREB:
7493 case ISN_STOREW:
7494 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007495 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 vim_free(isn->isn_arg.string);
7497 break;
7498
7499 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007500 case ISN_STORES:
7501 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502 break;
7503
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007504 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007505 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007506 vim_free(isn->isn_arg.unlet.ul_name);
7507 break;
7508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509 case ISN_STOREOPT:
7510 vim_free(isn->isn_arg.storeopt.so_name);
7511 break;
7512
7513 case ISN_PUSHBLOB: // push blob isn_arg.blob
7514 blob_unref(isn->isn_arg.blob);
7515 break;
7516
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007517 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007518#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007519 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007520#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007521 break;
7522
7523 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007524#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007525 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007526#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007527 break;
7528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529 case ISN_UCALL:
7530 vim_free(isn->isn_arg.ufunc.cuf_name);
7531 break;
7532
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007533 case ISN_FUNCREF:
7534 {
7535 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7536 + isn->isn_arg.funcref.fr_func;
7537 func_ptr_unref(dfunc->df_ufunc);
7538 }
7539 break;
7540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 case ISN_2BOOL:
7542 case ISN_2STRING:
7543 case ISN_ADDBLOB:
7544 case ISN_ADDLIST:
7545 case ISN_BCALL:
7546 case ISN_CATCH:
7547 case ISN_CHECKNR:
7548 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007549 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007550 case ISN_COMPAREANY:
7551 case ISN_COMPAREBLOB:
7552 case ISN_COMPAREBOOL:
7553 case ISN_COMPAREDICT:
7554 case ISN_COMPAREFLOAT:
7555 case ISN_COMPAREFUNC:
7556 case ISN_COMPARELIST:
7557 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007558 case ISN_COMPARESPECIAL:
7559 case ISN_COMPARESTRING:
7560 case ISN_CONCAT:
7561 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007562 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563 case ISN_DROP:
7564 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007565 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007566 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007568 case ISN_EXECCONCAT:
7569 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007570 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007571 case ISN_LISTINDEX:
7572 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007573 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007574 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007575 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576 case ISN_JUMP:
7577 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007578 case ISN_LOADBDICT:
7579 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007580 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007581 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007582 case ISN_LOADSCRIPT:
7583 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007584 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007585 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007586 case ISN_NEGATENR:
7587 case ISN_NEWDICT:
7588 case ISN_NEWLIST:
7589 case ISN_OPNR:
7590 case ISN_OPFLOAT:
7591 case ISN_OPANY:
7592 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007593 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594 case ISN_PUSHF:
7595 case ISN_PUSHNR:
7596 case ISN_PUSHBOOL:
7597 case ISN_PUSHSPEC:
7598 case ISN_RETURN:
7599 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007600 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007601 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007603 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007605 case ISN_STOREDICT:
7606 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607 case ISN_THROW:
7608 case ISN_TRY:
7609 // nothing allocated
7610 break;
7611 }
7612}
7613
7614/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007615 * Free all instructions for "dfunc".
7616 */
7617 static void
7618delete_def_function_contents(dfunc_T *dfunc)
7619{
7620 int idx;
7621
7622 ga_clear(&dfunc->df_def_args_isn);
7623
7624 if (dfunc->df_instr != NULL)
7625 {
7626 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7627 delete_instr(dfunc->df_instr + idx);
7628 VIM_CLEAR(dfunc->df_instr);
7629 }
7630
7631 dfunc->df_deleted = TRUE;
7632}
7633
7634/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007635 * When a user function is deleted, clear the contents of any associated def
7636 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007637 */
7638 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007639clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007641 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 {
7643 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7644 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645
Bram Moolenaar20431c92020-03-20 18:39:46 +01007646 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007647 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007648 }
7649}
7650
7651#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007652/*
7653 * Free all functions defined with ":def".
7654 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007655 void
7656free_def_functions(void)
7657{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007658 int idx;
7659
7660 for (idx = 0; idx < def_functions.ga_len; ++idx)
7661 {
7662 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7663
7664 delete_def_function_contents(dfunc);
7665 }
7666
7667 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007668}
7669#endif
7670
7671
7672#endif // FEAT_EVAL