blob: 1354a38b2c79a18ead95b37505c81fef918be96c [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
146static char e_var_notfound[] = N_("E1001: variable not found: %s");
147static char e_syntax_at[] = N_("E1002: Syntax error at %s");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200148static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200149static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +0200150static char e_namespace[] = N_("E1075: Namespace not supported: %s");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151
Bram Moolenaar20431c92020-03-20 18:39:46 +0100152static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200153static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154
155/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200156 * Lookup variable "name" in the local scope and return it.
157 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160lookup_local(char_u *name, size_t len, cctx_T *cctx)
161{
162 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200163 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200166 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100172 if (STRNCMP(name, lvar->lv_name, len) == 0
173 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
175 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200176 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100178 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200179
180 // Find local in outer function scope.
181 if (cctx->ctx_outer != NULL)
182 {
183 lvar = lookup_local(name, len, cctx->ctx_outer);
184 if (lvar != NULL)
185 {
186 // TODO: are there situations we should not mark the outer scope as
187 // used?
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 return lvar;
191 }
192 }
193
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195}
196
197/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198 * Lookup an argument in the current function and an enclosing function.
199 * Returns the argument index in "idxp"
200 * Returns the argument type in "type"
201 * Sets "gen_load_outer" to TRUE if found in outer scope.
202 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203 */
204 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200205lookup_arg(
206 char_u *name,
207 size_t len,
208 int *idxp,
209 type_T **type,
210 int *gen_load_outer,
211 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212{
213 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200214 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100216 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
219 {
220 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
221
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200222 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
223 {
224 if (idxp != NULL)
225 {
226 // Arguments are located above the frame pointer. One further
227 // if there is a vararg argument
228 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
229 + STACK_FRAME_SIZE)
230 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
231
232 if (cctx->ctx_ufunc->uf_arg_types != NULL)
233 *type = cctx->ctx_ufunc->uf_arg_types[idx];
234 else
235 *type = &t_any;
236 }
237 return OK;
238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200241 va_name = cctx->ctx_ufunc->uf_va_name;
242 if (va_name != NULL
243 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
244 {
245 if (idxp != NULL)
246 {
247 // varargs is always the last argument
248 *idxp = -STACK_FRAME_SIZE - 1;
249 *type = cctx->ctx_ufunc->uf_va_type;
250 }
251 return OK;
252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200254 if (cctx->ctx_outer != NULL)
255 {
256 // Lookup the name for an argument of the outer function.
257 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
258 == OK)
259 {
260 *gen_load_outer = TRUE;
261 return OK;
262 }
263 }
264
265 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266}
267
268/*
269 * Lookup a variable in the current script.
270 * Returns OK or FAIL.
271 */
272 static int
273lookup_script(char_u *name, size_t len)
274{
275 int cc;
276 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
277 dictitem_T *di;
278
279 cc = name[len];
280 name[len] = NUL;
281 di = find_var_in_ht(ht, 0, name, TRUE);
282 name[len] = cc;
283 return di == NULL ? FAIL: OK;
284}
285
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100286/*
287 * Check if "p[len]" is already defined, either in script "import_sid" or in
288 * compilation context "cctx".
289 * Return FAIL and give an error if it defined.
290 */
291 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200292check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100293{
294 if (lookup_script(p, len) == OK
295 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200296 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100297 || find_imported(p, len, cctx) != NULL)))
298 {
299 semsg("E1073: imported name already defined: %s", p);
300 return FAIL;
301 }
302 return OK;
303}
304
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200305/*
306 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
307 * be freed later.
308 */
309 static type_T *
310alloc_type(garray_T *type_gap)
311{
312 type_T *type;
313
314 if (ga_grow(type_gap, 1) == FAIL)
315 return NULL;
316 type = ALLOC_CLEAR_ONE(type_T);
317 if (type != NULL)
318 {
319 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
320 ++type_gap->ga_len;
321 }
322 return type;
323}
324
Bram Moolenaar6110e792020-07-08 19:35:21 +0200325 void
326clear_type_list(garray_T *gap)
327{
328 while (gap->ga_len > 0)
329 vim_free(((type_T **)gap->ga_data)[--gap->ga_len]);
330 ga_clear(gap);
331}
332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100333 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200334get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335{
336 type_T *type;
337
338 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200339 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200341 if (member_type->tt_type == VAR_VOID
342 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100343 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100344 if (member_type->tt_type == VAR_BOOL)
345 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 if (member_type->tt_type == VAR_NUMBER)
347 return &t_list_number;
348 if (member_type->tt_type == VAR_STRING)
349 return &t_list_string;
350
351 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200352 type = alloc_type(type_gap);
353 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100354 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355 type->tt_type = VAR_LIST;
356 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200357 type->tt_argcount = 0;
358 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100359 return type;
360}
361
362 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200363get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364{
365 type_T *type;
366
367 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200368 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200370 if (member_type->tt_type == VAR_VOID
371 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100372 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100373 if (member_type->tt_type == VAR_BOOL)
374 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375 if (member_type->tt_type == VAR_NUMBER)
376 return &t_dict_number;
377 if (member_type->tt_type == VAR_STRING)
378 return &t_dict_string;
379
380 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200381 type = alloc_type(type_gap);
382 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100383 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 type->tt_type = VAR_DICT;
385 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200386 type->tt_argcount = 0;
387 type->tt_args = NULL;
388 return type;
389}
390
391/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200392 * Allocate a new type for a function.
393 */
394 static type_T *
395alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
396{
397 type_T *type = alloc_type(type_gap);
398
399 if (type == NULL)
400 return &t_any;
401 type->tt_type = VAR_FUNC;
402 type->tt_member = ret_type;
403 type->tt_argcount = argcount;
404 type->tt_args = NULL;
405 return type;
406}
407
408/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200409 * Get a function type, based on the return type "ret_type".
410 * If "argcount" is -1 or 0 a predefined type can be used.
411 * If "argcount" > 0 always create a new type, so that arguments can be added.
412 */
413 static type_T *
414get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
415{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200416 // recognize commonly used types
417 if (argcount <= 0)
418 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200419 if (ret_type == &t_unknown)
420 {
421 // (argcount == 0) is not possible
422 return &t_func_unknown;
423 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200424 if (ret_type == &t_void)
425 {
426 if (argcount == 0)
427 return &t_func_0_void;
428 else
429 return &t_func_void;
430 }
431 if (ret_type == &t_any)
432 {
433 if (argcount == 0)
434 return &t_func_0_any;
435 else
436 return &t_func_any;
437 }
438 if (ret_type == &t_number)
439 {
440 if (argcount == 0)
441 return &t_func_0_number;
442 else
443 return &t_func_number;
444 }
445 if (ret_type == &t_string)
446 {
447 if (argcount == 0)
448 return &t_func_0_string;
449 else
450 return &t_func_string;
451 }
452 }
453
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200454 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455}
456
Bram Moolenaara8c17702020-04-01 21:17:24 +0200457/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200458 * For a function type, reserve space for "argcount" argument types (including
459 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200460 */
461 static int
462func_type_add_arg_types(
463 type_T *functype,
464 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200465 garray_T *type_gap)
466{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200467 // To make it easy to free the space needed for the argument types, add the
468 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200469 if (ga_grow(type_gap, 1) == FAIL)
470 return FAIL;
471 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
472 if (functype->tt_args == NULL)
473 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200474 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
475 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200477 return OK;
478}
479
480/*
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200481 * Get a type_T for a typval_T.
482 * "type_list" is used to temporarily create types in.
Bram Moolenaara8c17702020-04-01 21:17:24 +0200483 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200484 type_T *
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200485typval2type(typval_T *tv, garray_T *type_gap)
Bram Moolenaara8c17702020-04-01 21:17:24 +0200486{
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200487 type_T *actual;
488 type_T *member_type;
489
Bram Moolenaara8c17702020-04-01 21:17:24 +0200490 if (tv->v_type == VAR_NUMBER)
491 return &t_number;
492 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200493 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200494 if (tv->v_type == VAR_STRING)
495 return &t_string;
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200496
497 if (tv->v_type == VAR_LIST
498 && tv->vval.v_list != NULL
499 && tv->vval.v_list->lv_first != NULL)
500 {
501 // Use the type of the first member, it is the most specific.
502 member_type = typval2type(&tv->vval.v_list->lv_first->li_tv, type_gap);
503 return get_list_type(member_type, type_gap);
504 }
505
506 if (tv->v_type == VAR_DICT
507 && tv->vval.v_dict != NULL
508 && tv->vval.v_dict->dv_hashtab.ht_used > 0)
509 {
510 dict_iterator_T iter;
511 typval_T *value;
512
513 // Use the type of the first value, it is the most specific.
514 dict_iterate_start(tv, &iter);
515 dict_iterate_next(&iter, &value);
516 member_type = typval2type(value, type_gap);
517 return get_dict_type(member_type, type_gap);
518 }
519
520 if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
521 {
522 char_u *name = NULL;
523 ufunc_T *ufunc = NULL;
524
525 if (tv->v_type == VAR_PARTIAL)
526 {
527 if (tv->vval.v_partial->pt_func != NULL)
528 ufunc = tv->vval.v_partial->pt_func;
529 else
530 name = tv->vval.v_partial->pt_name;
531 }
532 else
533 name = tv->vval.v_string;
534 if (name != NULL)
535 // TODO: how about a builtin function?
536 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200537 if (ufunc != NULL)
538 {
539 // May need to get the argument types from default values by
540 // compiling the function.
541 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
542 && compile_def_function(ufunc, TRUE, NULL) == FAIL)
543 return NULL;
544 if (ufunc->uf_func_type != NULL)
545 return ufunc->uf_func_type;
546 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200547 }
548
549 actual = alloc_type(type_gap);
550 if (actual == NULL)
551 return NULL;
552 actual->tt_type = tv->v_type;
553 actual->tt_member = &t_any;
554
555 return actual;
556}
557
558/*
559 * Get a type_T for a typval_T, used for v: variables.
560 * "type_list" is used to temporarily create types in.
561 */
562 type_T *
563typval2type_vimvar(typval_T *tv, garray_T *type_gap)
564{
Bram Moolenaara8c17702020-04-01 21:17:24 +0200565 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
566 return &t_list_string;
567 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
568 return &t_dict_any;
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200569 return typval2type(tv, type_gap);
570}
571
572
573/*
574 * Return FAIL if "expected" and "actual" don't match.
575 */
576 int
577check_typval_type(type_T *expected, typval_T *actual_tv)
578{
579 garray_T type_list;
580 type_T *actual_type;
581 int res = FAIL;
582
583 ga_init2(&type_list, sizeof(type_T *), 10);
584 actual_type = typval2type(actual_tv, &type_list);
585 if (actual_type != NULL)
586 res = check_type(expected, actual_type, TRUE);
587 clear_type_list(&type_list);
588 return res;
Bram Moolenaara8c17702020-04-01 21:17:24 +0200589}
590
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200591 static void
592type_mismatch(type_T *expected, type_T *actual)
593{
594 char *tofree1, *tofree2;
595
596 semsg(_("E1013: type mismatch, expected %s but got %s"),
597 type_name(expected, &tofree1), type_name(actual, &tofree2));
598 vim_free(tofree1);
599 vim_free(tofree2);
600}
601
602 static void
603arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
604{
605 char *tofree1, *tofree2;
606
607 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
608 argidx,
609 type_name(expected, &tofree1), type_name(actual, &tofree2));
610 vim_free(tofree1);
611 vim_free(tofree2);
612}
613
614/*
615 * Check if the expected and actual types match.
616 * Does not allow for assigning "any" to a specific type.
617 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200618 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200619check_type(type_T *expected, type_T *actual, int give_msg)
620{
621 int ret = OK;
622
623 // When expected is "unknown" we accept any actual type.
624 // When expected is "any" we accept any actual type except "void".
625 if (expected->tt_type != VAR_UNKNOWN
626 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
627
628 {
629 if (expected->tt_type != actual->tt_type)
630 {
631 if (give_msg)
632 type_mismatch(expected, actual);
633 return FAIL;
634 }
635 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
636 {
637 // "unknown" is used for an empty list or dict
638 if (actual->tt_member != &t_unknown)
639 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
640 }
641 else if (expected->tt_type == VAR_FUNC)
642 {
643 if (expected->tt_member != &t_unknown)
644 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
645 if (ret == OK && expected->tt_argcount != -1
646 && (actual->tt_argcount < expected->tt_min_argcount
647 || actual->tt_argcount > expected->tt_argcount))
648 ret = FAIL;
Bram Moolenaarb8070e32020-07-23 20:56:04 +0200649 if (expected->tt_args != NULL && actual->tt_args != NULL)
650 {
651 int i;
652
653 for (i = 0; i < expected->tt_argcount; ++i)
654 if (check_type(expected->tt_args[i], actual->tt_args[i],
655 FALSE) == FAIL)
656 {
657 ret = FAIL;
658 break;
659 }
660 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200661 }
662 if (ret == FAIL && give_msg)
663 type_mismatch(expected, actual);
664 }
665 return ret;
666}
667
Bram Moolenaar65b95452020-07-19 14:03:09 +0200668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669/////////////////////////////////////////////////////////////////////
670// Following generate_ functions expect the caller to call ga_grow().
671
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200672#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
673#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675/*
676 * Generate an instruction without arguments.
677 * Returns a pointer to the new instruction, NULL if failed.
678 */
679 static isn_T *
680generate_instr(cctx_T *cctx, isntype_T isn_type)
681{
682 garray_T *instr = &cctx->ctx_instr;
683 isn_T *isn;
684
Bram Moolenaar080457c2020-03-03 21:53:32 +0100685 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686 if (ga_grow(instr, 1) == FAIL)
687 return NULL;
688 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
689 isn->isn_type = isn_type;
690 isn->isn_lnum = cctx->ctx_lnum + 1;
691 ++instr->ga_len;
692
693 return isn;
694}
695
696/*
697 * Generate an instruction without arguments.
698 * "drop" will be removed from the stack.
699 * Returns a pointer to the new instruction, NULL if failed.
700 */
701 static isn_T *
702generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
703{
704 garray_T *stack = &cctx->ctx_type_stack;
705
Bram Moolenaar080457c2020-03-03 21:53:32 +0100706 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 stack->ga_len -= drop;
708 return generate_instr(cctx, isn_type);
709}
710
711/*
712 * Generate instruction "isn_type" and put "type" on the type stack.
713 */
714 static isn_T *
715generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
716{
717 isn_T *isn;
718 garray_T *stack = &cctx->ctx_type_stack;
719
720 if ((isn = generate_instr(cctx, isn_type)) == NULL)
721 return NULL;
722
723 if (ga_grow(stack, 1) == FAIL)
724 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200725 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726 ++stack->ga_len;
727
728 return isn;
729}
730
731/*
732 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
733 */
734 static int
735may_generate_2STRING(int offset, cctx_T *cctx)
736{
737 isn_T *isn;
738 garray_T *stack = &cctx->ctx_type_stack;
739 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
740
741 if ((*type)->tt_type == VAR_STRING)
742 return OK;
743 *type = &t_string;
744
745 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
746 return FAIL;
747 isn->isn_arg.number = offset;
748
749 return OK;
750}
751
752 static int
753check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
754{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200755 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200757 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 {
759 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200760 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 else
762 semsg(_("E1036: %c requires number or float arguments"), *op);
763 return FAIL;
764 }
765 return OK;
766}
767
768/*
769 * Generate an instruction with two arguments. The instruction depends on the
770 * type of the arguments.
771 */
772 static int
773generate_two_op(cctx_T *cctx, char_u *op)
774{
775 garray_T *stack = &cctx->ctx_type_stack;
776 type_T *type1;
777 type_T *type2;
778 vartype_T vartype;
779 isn_T *isn;
780
Bram Moolenaar080457c2020-03-03 21:53:32 +0100781 RETURN_OK_IF_SKIP(cctx);
782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 // Get the known type of the two items on the stack. If they are matching
784 // use a type-specific instruction. Otherwise fall back to runtime type
785 // checking.
786 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
787 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200788 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 if (type1->tt_type == type2->tt_type
790 && (type1->tt_type == VAR_NUMBER
791 || type1->tt_type == VAR_LIST
792#ifdef FEAT_FLOAT
793 || type1->tt_type == VAR_FLOAT
794#endif
795 || type1->tt_type == VAR_BLOB))
796 vartype = type1->tt_type;
797
798 switch (*op)
799 {
800 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200801 && type1->tt_type != VAR_ANY
802 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803 && check_number_or_float(
804 type1->tt_type, type2->tt_type, op) == FAIL)
805 return FAIL;
806 isn = generate_instr_drop(cctx,
807 vartype == VAR_NUMBER ? ISN_OPNR
808 : vartype == VAR_LIST ? ISN_ADDLIST
809 : vartype == VAR_BLOB ? ISN_ADDBLOB
810#ifdef FEAT_FLOAT
811 : vartype == VAR_FLOAT ? ISN_OPFLOAT
812#endif
813 : ISN_OPANY, 1);
814 if (isn != NULL)
815 isn->isn_arg.op.op_type = EXPR_ADD;
816 break;
817
818 case '-':
819 case '*':
820 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
821 op) == FAIL)
822 return FAIL;
823 if (vartype == VAR_NUMBER)
824 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
825#ifdef FEAT_FLOAT
826 else if (vartype == VAR_FLOAT)
827 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
828#endif
829 else
830 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
831 if (isn != NULL)
832 isn->isn_arg.op.op_type = *op == '*'
833 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
834 break;
835
Bram Moolenaar4c683752020-04-05 21:38:23 +0200836 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200838 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 && type2->tt_type != VAR_NUMBER))
840 {
841 emsg(_("E1035: % requires number arguments"));
842 return FAIL;
843 }
844 isn = generate_instr_drop(cctx,
845 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
846 if (isn != NULL)
847 isn->isn_arg.op.op_type = EXPR_REM;
848 break;
849 }
850
851 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200852 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 {
854 type_T *type = &t_any;
855
856#ifdef FEAT_FLOAT
857 // float+number and number+float results in float
858 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
859 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
860 type = &t_float;
861#endif
862 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
863 }
864
865 return OK;
866}
867
868/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200869 * Get the instruction to use for comparing "type1" with "type2"
870 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200872 static isntype_T
873get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874{
875 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876
Bram Moolenaar4c683752020-04-05 21:38:23 +0200877 if (type1 == VAR_UNKNOWN)
878 type1 = VAR_ANY;
879 if (type2 == VAR_UNKNOWN)
880 type2 = VAR_ANY;
881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 if (type1 == type2)
883 {
884 switch (type1)
885 {
886 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
887 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
888 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
889 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
890 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
891 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
892 case VAR_LIST: isntype = ISN_COMPARELIST; break;
893 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
894 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 default: isntype = ISN_COMPAREANY; break;
896 }
897 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200898 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
900 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
901 isntype = ISN_COMPAREANY;
902
903 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
904 && (isntype == ISN_COMPAREBOOL
905 || isntype == ISN_COMPARESPECIAL
906 || isntype == ISN_COMPARENR
907 || isntype == ISN_COMPAREFLOAT))
908 {
909 semsg(_("E1037: Cannot use \"%s\" with %s"),
910 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200911 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912 }
913 if (isntype == ISN_DROP
914 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
915 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
916 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
917 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
918 && exptype != EXPR_IS && exptype != EXPR_ISNOT
919 && (type1 == VAR_BLOB || type2 == VAR_BLOB
920 || type1 == VAR_LIST || type2 == VAR_LIST))))
921 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100922 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200924 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200926 return isntype;
927}
928
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200929 int
930check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
931{
932 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
933 return FAIL;
934 return OK;
935}
936
Bram Moolenaara5565e42020-05-09 15:44:01 +0200937/*
938 * Generate an ISN_COMPARE* instruction with a boolean result.
939 */
940 static int
941generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
942{
943 isntype_T isntype;
944 isn_T *isn;
945 garray_T *stack = &cctx->ctx_type_stack;
946 vartype_T type1;
947 vartype_T type2;
948
949 RETURN_OK_IF_SKIP(cctx);
950
951 // Get the known type of the two items on the stack. If they are matching
952 // use a type-specific instruction. Otherwise fall back to runtime type
953 // checking.
954 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
955 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
956 isntype = get_compare_isn(exptype, type1, type2);
957 if (isntype == ISN_DROP)
958 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959
960 if ((isn = generate_instr(cctx, isntype)) == NULL)
961 return FAIL;
962 isn->isn_arg.op.op_type = exptype;
963 isn->isn_arg.op.op_ic = ic;
964
965 // takes two arguments, puts one bool back
966 if (stack->ga_len >= 2)
967 {
968 --stack->ga_len;
969 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
970 }
971
972 return OK;
973}
974
975/*
976 * Generate an ISN_2BOOL instruction.
977 */
978 static int
979generate_2BOOL(cctx_T *cctx, int invert)
980{
981 isn_T *isn;
982 garray_T *stack = &cctx->ctx_type_stack;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
986 return FAIL;
987 isn->isn_arg.number = invert;
988
989 // type becomes bool
990 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
991
992 return OK;
993}
994
995 static int
996generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
997{
998 isn_T *isn;
999 garray_T *stack = &cctx->ctx_type_stack;
1000
Bram Moolenaar080457c2020-03-03 21:53:32 +01001001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
1003 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +02001004 // TODO: whole type, e.g. for a function also arg and return types
1005 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 isn->isn_arg.type.ct_off = offset;
1007
1008 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001009 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010
1011 return OK;
1012}
1013
1014/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001015 * Check that
1016 * - "actual" is "expected" type or
1017 * - "actual" is a type that can be "expected" type: add a runtime check; or
1018 * - return FAIL.
1019 */
1020 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001021need_type(
1022 type_T *actual,
1023 type_T *expected,
1024 int offset,
1025 cctx_T *cctx,
1026 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001027{
1028 if (check_type(expected, actual, FALSE) == OK)
1029 return OK;
1030 if (actual->tt_type != VAR_ANY
1031 && actual->tt_type != VAR_UNKNOWN
1032 && !(actual->tt_type == VAR_FUNC
1033 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
1034 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001035 if (!silent)
1036 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001037 return FAIL;
1038 }
1039 generate_TYPECHECK(cctx, expected, offset);
1040 return OK;
1041}
1042
1043/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 * Generate an ISN_PUSHNR instruction.
1045 */
1046 static int
1047generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1048{
1049 isn_T *isn;
1050
Bram Moolenaar080457c2020-03-03 21:53:32 +01001051 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1053 return FAIL;
1054 isn->isn_arg.number = number;
1055
1056 return OK;
1057}
1058
1059/*
1060 * Generate an ISN_PUSHBOOL instruction.
1061 */
1062 static int
1063generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1064{
1065 isn_T *isn;
1066
Bram Moolenaar080457c2020-03-03 21:53:32 +01001067 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1069 return FAIL;
1070 isn->isn_arg.number = number;
1071
1072 return OK;
1073}
1074
1075/*
1076 * Generate an ISN_PUSHSPEC instruction.
1077 */
1078 static int
1079generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1080{
1081 isn_T *isn;
1082
Bram Moolenaar080457c2020-03-03 21:53:32 +01001083 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1085 return FAIL;
1086 isn->isn_arg.number = number;
1087
1088 return OK;
1089}
1090
1091#ifdef FEAT_FLOAT
1092/*
1093 * Generate an ISN_PUSHF instruction.
1094 */
1095 static int
1096generate_PUSHF(cctx_T *cctx, float_T fnumber)
1097{
1098 isn_T *isn;
1099
Bram Moolenaar080457c2020-03-03 21:53:32 +01001100 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1102 return FAIL;
1103 isn->isn_arg.fnumber = fnumber;
1104
1105 return OK;
1106}
1107#endif
1108
1109/*
1110 * Generate an ISN_PUSHS instruction.
1111 * Consumes "str".
1112 */
1113 static int
1114generate_PUSHS(cctx_T *cctx, char_u *str)
1115{
1116 isn_T *isn;
1117
Bram Moolenaar080457c2020-03-03 21:53:32 +01001118 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1120 return FAIL;
1121 isn->isn_arg.string = str;
1122
1123 return OK;
1124}
1125
1126/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001127 * Generate an ISN_PUSHCHANNEL instruction.
1128 * Consumes "channel".
1129 */
1130 static int
1131generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1132{
1133 isn_T *isn;
1134
Bram Moolenaar080457c2020-03-03 21:53:32 +01001135 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001136 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1137 return FAIL;
1138 isn->isn_arg.channel = channel;
1139
1140 return OK;
1141}
1142
1143/*
1144 * Generate an ISN_PUSHJOB instruction.
1145 * Consumes "job".
1146 */
1147 static int
1148generate_PUSHJOB(cctx_T *cctx, job_T *job)
1149{
1150 isn_T *isn;
1151
Bram Moolenaar080457c2020-03-03 21:53:32 +01001152 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001153 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001154 return FAIL;
1155 isn->isn_arg.job = job;
1156
1157 return OK;
1158}
1159
1160/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 * Generate an ISN_PUSHBLOB instruction.
1162 * Consumes "blob".
1163 */
1164 static int
1165generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1166{
1167 isn_T *isn;
1168
Bram Moolenaar080457c2020-03-03 21:53:32 +01001169 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1171 return FAIL;
1172 isn->isn_arg.blob = blob;
1173
1174 return OK;
1175}
1176
1177/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001178 * Generate an ISN_PUSHFUNC instruction with name "name".
1179 * Consumes "name".
1180 */
1181 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001182generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001183{
1184 isn_T *isn;
1185
Bram Moolenaar080457c2020-03-03 21:53:32 +01001186 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001187 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001188 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001189 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001190
1191 return OK;
1192}
1193
1194/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001195 * Generate an ISN_GETITEM instruction with "index".
1196 */
1197 static int
1198generate_GETITEM(cctx_T *cctx, int index)
1199{
1200 isn_T *isn;
1201 garray_T *stack = &cctx->ctx_type_stack;
1202 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1203 type_T *item_type = &t_any;
1204
1205 RETURN_OK_IF_SKIP(cctx);
1206
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001207 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001208 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001209 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001210 emsg(_(e_listreq));
1211 return FAIL;
1212 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001213 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001214 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1215 return FAIL;
1216 isn->isn_arg.number = index;
1217
1218 // add the item type to the type stack
1219 if (ga_grow(stack, 1) == FAIL)
1220 return FAIL;
1221 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1222 ++stack->ga_len;
1223 return OK;
1224}
1225
1226/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001227 * Generate an ISN_SLICE instruction with "count".
1228 */
1229 static int
1230generate_SLICE(cctx_T *cctx, int count)
1231{
1232 isn_T *isn;
1233
1234 RETURN_OK_IF_SKIP(cctx);
1235 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1236 return FAIL;
1237 isn->isn_arg.number = count;
1238 return OK;
1239}
1240
1241/*
1242 * Generate an ISN_CHECKLEN instruction with "min_len".
1243 */
1244 static int
1245generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1246{
1247 isn_T *isn;
1248
1249 RETURN_OK_IF_SKIP(cctx);
1250
1251 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1252 return FAIL;
1253 isn->isn_arg.checklen.cl_min_len = min_len;
1254 isn->isn_arg.checklen.cl_more_OK = more_OK;
1255
1256 return OK;
1257}
1258
1259/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 * Generate an ISN_STORE instruction.
1261 */
1262 static int
1263generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1264{
1265 isn_T *isn;
1266
Bram Moolenaar080457c2020-03-03 21:53:32 +01001267 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1269 return FAIL;
1270 if (name != NULL)
1271 isn->isn_arg.string = vim_strsave(name);
1272 else
1273 isn->isn_arg.number = idx;
1274
1275 return OK;
1276}
1277
1278/*
1279 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1280 */
1281 static int
1282generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1283{
1284 isn_T *isn;
1285
Bram Moolenaar080457c2020-03-03 21:53:32 +01001286 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1288 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001289 isn->isn_arg.storenr.stnr_idx = idx;
1290 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291
1292 return OK;
1293}
1294
1295/*
1296 * Generate an ISN_STOREOPT instruction
1297 */
1298 static int
1299generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1300{
1301 isn_T *isn;
1302
Bram Moolenaar080457c2020-03-03 21:53:32 +01001303 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1305 return FAIL;
1306 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1307 isn->isn_arg.storeopt.so_flags = opt_flags;
1308
1309 return OK;
1310}
1311
1312/*
1313 * Generate an ISN_LOAD or similar instruction.
1314 */
1315 static int
1316generate_LOAD(
1317 cctx_T *cctx,
1318 isntype_T isn_type,
1319 int idx,
1320 char_u *name,
1321 type_T *type)
1322{
1323 isn_T *isn;
1324
Bram Moolenaar080457c2020-03-03 21:53:32 +01001325 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1327 return FAIL;
1328 if (name != NULL)
1329 isn->isn_arg.string = vim_strsave(name);
1330 else
1331 isn->isn_arg.number = idx;
1332
1333 return OK;
1334}
1335
1336/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001337 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001338 */
1339 static int
1340generate_LOADV(
1341 cctx_T *cctx,
1342 char_u *name,
1343 int error)
1344{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001345 int di_flags;
1346 int vidx = find_vim_var(name, &di_flags);
1347 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001348
Bram Moolenaar080457c2020-03-03 21:53:32 +01001349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001350 if (vidx < 0)
1351 {
1352 if (error)
1353 semsg(_(e_var_notfound), name);
1354 return FAIL;
1355 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001356 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001358 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001359}
1360
1361/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001362 * Generate an ISN_UNLET instruction.
1363 */
1364 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001365generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001366{
1367 isn_T *isn;
1368
1369 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001370 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001371 return FAIL;
1372 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1373 isn->isn_arg.unlet.ul_forceit = forceit;
1374
1375 return OK;
1376}
1377
1378/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 * Generate an ISN_LOADS instruction.
1380 */
1381 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001382generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001384 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001386 int sid,
1387 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388{
1389 isn_T *isn;
1390
Bram Moolenaar080457c2020-03-03 21:53:32 +01001391 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001392 if (isn_type == ISN_LOADS)
1393 isn = generate_instr_type(cctx, isn_type, type);
1394 else
1395 isn = generate_instr_drop(cctx, isn_type, 1);
1396 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001398 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1399 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400
1401 return OK;
1402}
1403
1404/*
1405 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1406 */
1407 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001408generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 cctx_T *cctx,
1410 isntype_T isn_type,
1411 int sid,
1412 int idx,
1413 type_T *type)
1414{
1415 isn_T *isn;
1416
Bram Moolenaar080457c2020-03-03 21:53:32 +01001417 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 if (isn_type == ISN_LOADSCRIPT)
1419 isn = generate_instr_type(cctx, isn_type, type);
1420 else
1421 isn = generate_instr_drop(cctx, isn_type, 1);
1422 if (isn == NULL)
1423 return FAIL;
1424 isn->isn_arg.script.script_sid = sid;
1425 isn->isn_arg.script.script_idx = idx;
1426 return OK;
1427}
1428
1429/*
1430 * Generate an ISN_NEWLIST instruction.
1431 */
1432 static int
1433generate_NEWLIST(cctx_T *cctx, int count)
1434{
1435 isn_T *isn;
1436 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 type_T *type;
1438 type_T *member;
1439
Bram Moolenaar080457c2020-03-03 21:53:32 +01001440 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1442 return FAIL;
1443 isn->isn_arg.number = count;
1444
1445 // drop the value types
1446 stack->ga_len -= count;
1447
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001448 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001449 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 if (count > 0)
1451 member = ((type_T **)stack->ga_data)[stack->ga_len];
1452 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001453 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001454 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455
1456 // add the list type to the type stack
1457 if (ga_grow(stack, 1) == FAIL)
1458 return FAIL;
1459 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1460 ++stack->ga_len;
1461
1462 return OK;
1463}
1464
1465/*
1466 * Generate an ISN_NEWDICT instruction.
1467 */
1468 static int
1469generate_NEWDICT(cctx_T *cctx, int count)
1470{
1471 isn_T *isn;
1472 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 type_T *type;
1474 type_T *member;
1475
Bram Moolenaar080457c2020-03-03 21:53:32 +01001476 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1478 return FAIL;
1479 isn->isn_arg.number = count;
1480
1481 // drop the key and value types
1482 stack->ga_len -= 2 * count;
1483
Bram Moolenaar436472f2020-02-20 22:54:43 +01001484 // Use the first value type for the list member type. Use "void" for an
1485 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 if (count > 0)
1487 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1488 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001489 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001490 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491
1492 // add the dict type to the type stack
1493 if (ga_grow(stack, 1) == FAIL)
1494 return FAIL;
1495 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1496 ++stack->ga_len;
1497
1498 return OK;
1499}
1500
1501/*
1502 * Generate an ISN_FUNCREF instruction.
1503 */
1504 static int
1505generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1506{
1507 isn_T *isn;
1508 garray_T *stack = &cctx->ctx_type_stack;
1509
Bram Moolenaar080457c2020-03-03 21:53:32 +01001510 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1512 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001513 isn->isn_arg.funcref.fr_func = dfunc_idx;
1514 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515
1516 if (ga_grow(stack, 1) == FAIL)
1517 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001518 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 // TODO: argument and return types
1520 ++stack->ga_len;
1521
1522 return OK;
1523}
1524
1525/*
1526 * Generate an ISN_JUMP instruction.
1527 */
1528 static int
1529generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1530{
1531 isn_T *isn;
1532 garray_T *stack = &cctx->ctx_type_stack;
1533
Bram Moolenaar080457c2020-03-03 21:53:32 +01001534 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1536 return FAIL;
1537 isn->isn_arg.jump.jump_when = when;
1538 isn->isn_arg.jump.jump_where = where;
1539
1540 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1541 --stack->ga_len;
1542
1543 return OK;
1544}
1545
1546 static int
1547generate_FOR(cctx_T *cctx, int loop_idx)
1548{
1549 isn_T *isn;
1550 garray_T *stack = &cctx->ctx_type_stack;
1551
Bram Moolenaar080457c2020-03-03 21:53:32 +01001552 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1554 return FAIL;
1555 isn->isn_arg.forloop.for_idx = loop_idx;
1556
1557 if (ga_grow(stack, 1) == FAIL)
1558 return FAIL;
1559 // type doesn't matter, will be stored next
1560 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1561 ++stack->ga_len;
1562
1563 return OK;
1564}
1565
1566/*
1567 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001568 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 * Return FAIL if the number of arguments is wrong.
1570 */
1571 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001572generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573{
1574 isn_T *isn;
1575 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001576 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001577 type_T *argtypes[MAX_FUNC_ARGS];
1578 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579
Bram Moolenaar080457c2020-03-03 21:53:32 +01001580 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001581 argoff = check_internal_func(func_idx, argcount);
1582 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 return FAIL;
1584
Bram Moolenaar389df252020-07-09 21:20:47 +02001585 if (method_call && argoff > 1)
1586 {
1587 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1588 return FAIL;
1589 isn->isn_arg.shuffle.shfl_item = argcount;
1590 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1591 }
1592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1594 return FAIL;
1595 isn->isn_arg.bfunc.cbf_idx = func_idx;
1596 isn->isn_arg.bfunc.cbf_argcount = argcount;
1597
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001598 for (i = 0; i < argcount; ++i)
1599 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 stack->ga_len -= argcount; // drop the arguments
1602 if (ga_grow(stack, 1) == FAIL)
1603 return FAIL;
1604 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001605 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 ++stack->ga_len; // add return value
1607
1608 return OK;
1609}
1610
1611/*
1612 * Generate an ISN_DCALL or ISN_UCALL instruction.
1613 * Return FAIL if the number of arguments is wrong.
1614 */
1615 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001616generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617{
1618 isn_T *isn;
1619 garray_T *stack = &cctx->ctx_type_stack;
1620 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001621 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622
Bram Moolenaar080457c2020-03-03 21:53:32 +01001623 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 if (argcount > regular_args && !has_varargs(ufunc))
1625 {
1626 semsg(_(e_toomanyarg), ufunc->uf_name);
1627 return FAIL;
1628 }
1629 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1630 {
1631 semsg(_(e_toofewarg), ufunc->uf_name);
1632 return FAIL;
1633 }
1634
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001635 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001636 {
1637 int i;
1638
1639 for (i = 0; i < argcount; ++i)
1640 {
1641 type_T *expected;
1642 type_T *actual;
1643
1644 if (i < regular_args)
1645 {
1646 if (ufunc->uf_arg_types == NULL)
1647 continue;
1648 expected = ufunc->uf_arg_types[i];
1649 }
1650 else
1651 expected = ufunc->uf_va_type->tt_member;
1652 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001653 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001654 {
1655 arg_type_mismatch(expected, actual, i + 1);
1656 return FAIL;
1657 }
1658 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001659 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001660 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001661 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001662 }
1663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001665 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001666 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001668 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 {
1670 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1671 isn->isn_arg.dfunc.cdf_argcount = argcount;
1672 }
1673 else
1674 {
1675 // A user function may be deleted and redefined later, can't use the
1676 // ufunc pointer, need to look it up again at runtime.
1677 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1678 isn->isn_arg.ufunc.cuf_argcount = argcount;
1679 }
1680
1681 stack->ga_len -= argcount; // drop the arguments
1682 if (ga_grow(stack, 1) == FAIL)
1683 return FAIL;
1684 // add return value
1685 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1686 ++stack->ga_len;
1687
1688 return OK;
1689}
1690
1691/*
1692 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1693 */
1694 static int
1695generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1696{
1697 isn_T *isn;
1698 garray_T *stack = &cctx->ctx_type_stack;
1699
Bram Moolenaar080457c2020-03-03 21:53:32 +01001700 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1702 return FAIL;
1703 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1704 isn->isn_arg.ufunc.cuf_argcount = argcount;
1705
1706 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001707 if (ga_grow(stack, 1) == FAIL)
1708 return FAIL;
1709 // add return value
1710 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1711 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712
1713 return OK;
1714}
1715
1716/*
1717 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001718 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 */
1720 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001721generate_PCALL(
1722 cctx_T *cctx,
1723 int argcount,
1724 char_u *name,
1725 type_T *type,
1726 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727{
1728 isn_T *isn;
1729 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001730 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731
Bram Moolenaar080457c2020-03-03 21:53:32 +01001732 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001733
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001734 if (type->tt_type == VAR_ANY)
1735 ret_type = &t_any;
1736 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001737 {
1738 if (type->tt_argcount != -1)
1739 {
1740 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1741
1742 if (argcount < type->tt_min_argcount - varargs)
1743 {
1744 semsg(_(e_toofewarg), "[reference]");
1745 return FAIL;
1746 }
1747 if (!varargs && argcount > type->tt_argcount)
1748 {
1749 semsg(_(e_toomanyarg), "[reference]");
1750 return FAIL;
1751 }
1752 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001753 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001754 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001755 else
1756 {
1757 semsg(_("E1085: Not a callable type: %s"), name);
1758 return FAIL;
1759 }
1760
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1762 return FAIL;
1763 isn->isn_arg.pfunc.cpf_top = at_top;
1764 isn->isn_arg.pfunc.cpf_argcount = argcount;
1765
1766 stack->ga_len -= argcount; // drop the arguments
1767
1768 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001769 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001771 // If partial is above the arguments it must be cleared and replaced with
1772 // the return value.
1773 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1774 return FAIL;
1775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 return OK;
1777}
1778
1779/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001780 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 */
1782 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001783generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784{
1785 isn_T *isn;
1786 garray_T *stack = &cctx->ctx_type_stack;
1787 type_T *type;
1788
Bram Moolenaar080457c2020-03-03 21:53:32 +01001789 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001790 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001792 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001794 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001796 if (type->tt_type != VAR_DICT && type != &t_any)
1797 {
1798 emsg(_(e_dictreq));
1799 return FAIL;
1800 }
1801 // change dict type to dict member type
1802 if (type->tt_type == VAR_DICT)
1803 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804
1805 return OK;
1806}
1807
1808/*
1809 * Generate an ISN_ECHO instruction.
1810 */
1811 static int
1812generate_ECHO(cctx_T *cctx, int with_white, int count)
1813{
1814 isn_T *isn;
1815
Bram Moolenaar080457c2020-03-03 21:53:32 +01001816 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1818 return FAIL;
1819 isn->isn_arg.echo.echo_with_white = with_white;
1820 isn->isn_arg.echo.echo_count = count;
1821
1822 return OK;
1823}
1824
Bram Moolenaarad39c092020-02-26 18:23:43 +01001825/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001826 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001827 */
1828 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001829generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001830{
1831 isn_T *isn;
1832
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001833 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001834 return FAIL;
1835 isn->isn_arg.number = count;
1836
1837 return OK;
1838}
1839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 static int
1841generate_EXEC(cctx_T *cctx, char_u *line)
1842{
1843 isn_T *isn;
1844
Bram Moolenaar080457c2020-03-03 21:53:32 +01001845 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1847 return FAIL;
1848 isn->isn_arg.string = vim_strsave(line);
1849 return OK;
1850}
1851
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001852 static int
1853generate_EXECCONCAT(cctx_T *cctx, int count)
1854{
1855 isn_T *isn;
1856
1857 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1858 return FAIL;
1859 isn->isn_arg.number = count;
1860 return OK;
1861}
1862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863/*
1864 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001865 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001867 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1869{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 lvar_T *lvar;
1871
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001872 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001874 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001875 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 }
1877
1878 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001879 return NULL;
1880 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001882 // Every local variable uses the next entry on the stack. We could re-use
1883 // the last ones when leaving a scope, but then variables used in a closure
1884 // might get overwritten. To keep things simple do not re-use stack
1885 // entries. This is less efficient, but memory is cheap these days.
1886 lvar->lv_idx = cctx->ctx_locals_count++;
1887
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001888 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 lvar->lv_const = isConst;
1890 lvar->lv_type = type;
1891
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001892 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893}
1894
1895/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001896 * Remove local variables above "new_top".
1897 */
1898 static void
1899unwind_locals(cctx_T *cctx, int new_top)
1900{
1901 if (cctx->ctx_locals.ga_len > new_top)
1902 {
1903 int idx;
1904 lvar_T *lvar;
1905
1906 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1907 {
1908 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1909 vim_free(lvar->lv_name);
1910 }
1911 }
1912 cctx->ctx_locals.ga_len = new_top;
1913}
1914
1915/*
1916 * Free all local variables.
1917 */
1918 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001919free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001920{
1921 unwind_locals(cctx, 0);
1922 ga_clear(&cctx->ctx_locals);
1923}
1924
1925/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 * Skip over a type definition and return a pointer to just after it.
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001927 * When "optional" is TRUE then a leading "?" is accepted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 */
1929 char_u *
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001930skip_type(char_u *start, int optional)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931{
1932 char_u *p = start;
1933
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001934 if (optional && *p == '?')
1935 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936 while (ASCII_ISALNUM(*p) || *p == '_')
1937 ++p;
1938
1939 // Skip over "<type>"; this is permissive about white space.
1940 if (*skipwhite(p) == '<')
1941 {
1942 p = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001943 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 p = skipwhite(p);
1945 if (*p == '>')
1946 ++p;
1947 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001948 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
1949 && STRNCMP("func", start, 4) == 0)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001950 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001951 if (*p == '(')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001952 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001953 // handle func(args): type
1954 ++p;
1955 while (*p != ')' && *p != NUL)
1956 {
1957 char_u *sp = p;
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001958
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001959 p = skip_type(p, TRUE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001960 if (p == sp)
1961 return p; // syntax error
1962 if (*p == ',')
1963 p = skipwhite(p + 1);
1964 }
1965 if (*p == ')')
1966 {
1967 if (p[1] == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001968 p = skip_type(skipwhite(p + 2), FALSE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001969 else
Bram Moolenaarbfba8652020-07-23 20:09:10 +02001970 ++p;
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001971 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001972 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001973 else
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001974 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001975 // handle func: return_type
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001976 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001977 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001978 }
1979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 return p;
1981}
1982
1983/*
1984 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001985 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 * Returns NULL in case of failure.
1987 */
1988 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001989parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990{
1991 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001992 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993
1994 if (**arg != '<')
1995 {
1996 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001997 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 else
1999 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002000 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 }
2002 *arg = skipwhite(*arg + 1);
2003
Bram Moolenaard77a8522020-04-03 21:59:57 +02002004 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005
2006 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002007 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 {
2009 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002010 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 }
2012 ++*arg;
2013
2014 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002015 return get_list_type(member_type, type_gap);
2016 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017}
2018
2019/*
2020 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02002021 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 */
2023 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002024parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025{
2026 char_u *p = *arg;
2027 size_t len;
2028
2029 // skip over the first word
2030 while (ASCII_ISALNUM(*p) || *p == '_')
2031 ++p;
2032 len = p - *arg;
2033
2034 switch (**arg)
2035 {
2036 case 'a':
2037 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
2038 {
2039 *arg += len;
2040 return &t_any;
2041 }
2042 break;
2043 case 'b':
2044 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
2045 {
2046 *arg += len;
2047 return &t_bool;
2048 }
2049 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
2050 {
2051 *arg += len;
2052 return &t_blob;
2053 }
2054 break;
2055 case 'c':
2056 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
2057 {
2058 *arg += len;
2059 return &t_channel;
2060 }
2061 break;
2062 case 'd':
2063 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
2064 {
2065 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002066 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 }
2068 break;
2069 case 'f':
2070 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
2071 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01002072#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 *arg += len;
2074 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002075#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002076 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002077 return &t_any;
2078#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 }
2080 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2081 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002082 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002083 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002084 int argcount = -1;
2085 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002086 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002087 type_T *arg_type[MAX_FUNC_ARGS + 1];
2088
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002089 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002091 if (**arg == '(')
2092 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002093 // "func" may or may not return a value, "func()" does
2094 // not return a value.
2095 ret_type = &t_void;
2096
Bram Moolenaard77a8522020-04-03 21:59:57 +02002097 p = ++*arg;
2098 argcount = 0;
2099 while (*p != NUL && *p != ')')
2100 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002101 if (*p == '?')
2102 {
2103 if (first_optional == -1)
2104 first_optional = argcount;
2105 ++p;
2106 }
2107 else if (first_optional != -1)
2108 {
2109 emsg(_("E1007: mandatory argument after optional argument"));
2110 return &t_any;
2111 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002112 else if (STRNCMP(p, "...", 3) == 0)
2113 {
2114 flags |= TTFLAG_VARARGS;
2115 p += 3;
2116 }
2117
2118 arg_type[argcount++] = parse_type(&p, type_gap);
2119
2120 // Nothing comes after "...{type}".
2121 if (flags & TTFLAG_VARARGS)
2122 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002123
Bram Moolenaard77a8522020-04-03 21:59:57 +02002124 if (*p != ',' && *skipwhite(p) == ',')
2125 {
2126 semsg(_(e_no_white_before), ",");
2127 return &t_any;
2128 }
2129 if (*p == ',')
2130 {
2131 ++p;
2132 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002133 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002134 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002135 return &t_any;
2136 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002137 }
2138 p = skipwhite(p);
2139 if (argcount == MAX_FUNC_ARGS)
2140 {
2141 emsg(_("E740: Too many argument types"));
2142 return &t_any;
2143 }
2144 }
2145
2146 p = skipwhite(p);
2147 if (*p != ')')
2148 {
2149 emsg(_(e_missing_close));
2150 return &t_any;
2151 }
2152 *arg = p + 1;
2153 }
2154 if (**arg == ':')
2155 {
2156 // parse return type
2157 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002158 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002159 semsg(_(e_white_after), ":");
2160 *arg = skipwhite(*arg);
2161 ret_type = parse_type(arg, type_gap);
2162 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002163 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002164 type = get_func_type(ret_type, argcount, type_gap);
2165 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002166 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002167 type = alloc_func_type(ret_type, argcount, type_gap);
2168 type->tt_flags = flags;
2169 if (argcount > 0)
2170 {
2171 type->tt_argcount = argcount;
2172 type->tt_min_argcount = first_optional == -1
2173 ? argcount : first_optional;
2174 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002175 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002176 return &t_any;
2177 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002178 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002179 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002180 }
2181 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 }
2183 break;
2184 case 'j':
2185 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2186 {
2187 *arg += len;
2188 return &t_job;
2189 }
2190 break;
2191 case 'l':
2192 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2193 {
2194 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002195 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 }
2197 break;
2198 case 'n':
2199 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2200 {
2201 *arg += len;
2202 return &t_number;
2203 }
2204 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 case 's':
2206 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2207 {
2208 *arg += len;
2209 return &t_string;
2210 }
2211 break;
2212 case 'v':
2213 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2214 {
2215 *arg += len;
2216 return &t_void;
2217 }
2218 break;
2219 }
2220
2221 semsg(_("E1010: Type not recognized: %s"), *arg);
2222 return &t_any;
2223}
2224
2225/*
2226 * Check if "type1" and "type2" are exactly the same.
2227 */
2228 static int
2229equal_type(type_T *type1, type_T *type2)
2230{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002231 int i;
2232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 if (type1->tt_type != type2->tt_type)
2234 return FALSE;
2235 switch (type1->tt_type)
2236 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002238 case VAR_ANY:
2239 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240 case VAR_SPECIAL:
2241 case VAR_BOOL:
2242 case VAR_NUMBER:
2243 case VAR_FLOAT:
2244 case VAR_STRING:
2245 case VAR_BLOB:
2246 case VAR_JOB:
2247 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002248 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 case VAR_LIST:
2250 case VAR_DICT:
2251 return equal_type(type1->tt_member, type2->tt_member);
2252 case VAR_FUNC:
2253 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002254 if (!equal_type(type1->tt_member, type2->tt_member)
2255 || type1->tt_argcount != type2->tt_argcount)
2256 return FALSE;
2257 if (type1->tt_argcount < 0
2258 || type1->tt_args == NULL || type2->tt_args == NULL)
2259 return TRUE;
2260 for (i = 0; i < type1->tt_argcount; ++i)
2261 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2262 return FALSE;
2263 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 }
2265 return TRUE;
2266}
2267
2268/*
2269 * Find the common type of "type1" and "type2" and put it in "dest".
2270 * "type2" and "dest" may be the same.
2271 */
2272 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002273common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274{
2275 if (equal_type(type1, type2))
2276 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002277 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278 return;
2279 }
2280
2281 if (type1->tt_type == type2->tt_type)
2282 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2284 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002285 type_T *common;
2286
Bram Moolenaard77a8522020-04-03 21:59:57 +02002287 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002288 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002289 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002290 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002291 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 return;
2293 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002294 if (type1->tt_type == VAR_FUNC)
2295 {
2296 type_T *common;
2297
2298 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2299 if (type1->tt_argcount == type2->tt_argcount
2300 && type1->tt_argcount >= 0)
2301 {
2302 int argcount = type1->tt_argcount;
2303 int i;
2304
2305 *dest = alloc_func_type(common, argcount, type_gap);
2306 if (type1->tt_args != NULL && type2->tt_args != NULL)
2307 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002308 if (func_type_add_arg_types(*dest, argcount,
2309 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002310 for (i = 0; i < argcount; ++i)
2311 common_type(type1->tt_args[i], type2->tt_args[i],
2312 &(*dest)->tt_args[i], type_gap);
2313 }
2314 }
2315 else
2316 *dest = alloc_func_type(common, -1, type_gap);
2317 return;
2318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 }
2320
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002321 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322}
2323
2324 char *
2325vartype_name(vartype_T type)
2326{
2327 switch (type)
2328 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002329 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002330 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002331 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 case VAR_SPECIAL: return "special";
2333 case VAR_BOOL: return "bool";
2334 case VAR_NUMBER: return "number";
2335 case VAR_FLOAT: return "float";
2336 case VAR_STRING: return "string";
2337 case VAR_BLOB: return "blob";
2338 case VAR_JOB: return "job";
2339 case VAR_CHANNEL: return "channel";
2340 case VAR_LIST: return "list";
2341 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002342
2343 case VAR_FUNC:
2344 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002346 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347}
2348
2349/*
2350 * Return the name of a type.
2351 * The result may be in allocated memory, in which case "tofree" is set.
2352 */
2353 char *
2354type_name(type_T *type, char **tofree)
2355{
2356 char *name = vartype_name(type->tt_type);
2357
2358 *tofree = NULL;
2359 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2360 {
2361 char *member_free;
2362 char *member_name = type_name(type->tt_member, &member_free);
2363 size_t len;
2364
2365 len = STRLEN(name) + STRLEN(member_name) + 3;
2366 *tofree = alloc(len);
2367 if (*tofree != NULL)
2368 {
2369 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2370 vim_free(member_free);
2371 return *tofree;
2372 }
2373 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002374 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002375 {
2376 garray_T ga;
2377 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002378 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002379
2380 ga_init2(&ga, 1, 100);
2381 if (ga_grow(&ga, 20) == FAIL)
2382 return "[unknown]";
2383 *tofree = ga.ga_data;
2384 STRCPY(ga.ga_data, "func(");
2385 ga.ga_len += 5;
2386
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002387 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002388 {
2389 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002390 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002391 int len;
2392
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002393 if (type->tt_args == NULL)
2394 arg_type = "[unknown]";
2395 else
2396 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002397 if (i > 0)
2398 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002399 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002400 ga.ga_len += 2;
2401 }
2402 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002403 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002404 {
2405 vim_free(arg_free);
2406 return "[unknown]";
2407 }
2408 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002409 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002410 {
2411 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2412 ga.ga_len += 3;
2413 }
2414 else if (i >= type->tt_min_argcount)
2415 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002416 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002417 ga.ga_len += len;
2418 vim_free(arg_free);
2419 }
2420
2421 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002422 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002423 else
2424 {
2425 char *ret_free;
2426 char *ret_name = type_name(type->tt_member, &ret_free);
2427 int len;
2428
2429 len = (int)STRLEN(ret_name) + 4;
2430 if (ga_grow(&ga, len) == FAIL)
2431 {
2432 vim_free(ret_free);
2433 return "[unknown]";
2434 }
2435 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002436 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2437 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002438 vim_free(ret_free);
2439 }
2440 return ga.ga_data;
2441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442
2443 return name;
2444}
2445
2446/*
2447 * Find "name" in script-local items of script "sid".
2448 * Returns the index in "sn_var_vals" if found.
2449 * If found but not in "sn_var_vals" returns -1.
2450 * If not found returns -2.
2451 */
2452 int
2453get_script_item_idx(int sid, char_u *name, int check_writable)
2454{
2455 hashtab_T *ht;
2456 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002457 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 int idx;
2459
2460 // First look the name up in the hashtable.
2461 if (sid <= 0 || sid > script_items.ga_len)
2462 return -1;
2463 ht = &SCRIPT_VARS(sid);
2464 di = find_var_in_ht(ht, 0, name, TRUE);
2465 if (di == NULL)
2466 return -2;
2467
2468 // Now find the svar_T index in sn_var_vals.
2469 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2470 {
2471 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2472
2473 if (sv->sv_tv == &di->di_tv)
2474 {
2475 if (check_writable && sv->sv_const)
2476 semsg(_(e_readonlyvar), name);
2477 return idx;
2478 }
2479 }
2480 return -1;
2481}
2482
2483/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002484 * Find "name" in imported items of the current script or in "cctx" if not
2485 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 */
2487 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002488find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002490 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 int idx;
2492
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002493 if (current_sctx.sc_sid <= 0)
2494 return NULL;
2495 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 if (cctx != NULL)
2497 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2498 {
2499 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2500 + idx;
2501
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002502 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2503 : STRLEN(import->imp_name) == len
2504 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 return import;
2506 }
2507
2508 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2509 {
2510 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2511
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002512 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2513 : STRLEN(import->imp_name) == len
2514 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 return import;
2516 }
2517 return NULL;
2518}
2519
2520/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002521 * Free all imported variables.
2522 */
2523 static void
2524free_imported(cctx_T *cctx)
2525{
2526 int idx;
2527
2528 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2529 {
2530 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2531
2532 vim_free(import->imp_name);
2533 }
2534 ga_clear(&cctx->ctx_imports);
2535}
2536
2537/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002538 * Return TRUE if "p" points at a "#" but not at "#{".
2539 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002540 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002541vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002542{
2543 return p[0] == '#' && p[1] != '{';
2544}
2545
2546/*
2547 * Return a pointer to the next line that isn't empty or only contains a
2548 * comment. Skips over white space.
2549 * Returns NULL if there is none.
2550 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002551 char_u *
2552peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002553{
2554 int lnum = cctx->ctx_lnum;
2555
2556 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2557 {
2558 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002559 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002560
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002561 if (line == NULL)
2562 break;
2563 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002564 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002565 return p;
2566 }
2567 return NULL;
2568}
2569
2570/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002571 * Called when checking for a following operator at "arg". When the rest of
2572 * the line is empty or only a comment, peek the next line. If there is a next
2573 * line return a pointer to it and set "nextp".
2574 * Otherwise skip over white space.
2575 */
2576 static char_u *
2577may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2578{
2579 char_u *p = skipwhite(arg);
2580
2581 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002582 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002583 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002584 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002585 if (*nextp != NULL)
2586 return *nextp;
2587 }
2588 return p;
2589}
2590
2591/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002592 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002593 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002594 * Returns NULL when at the end.
2595 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002596 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002597next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002598{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002599 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002600
2601 do
2602 {
2603 ++cctx->ctx_lnum;
2604 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002605 {
2606 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002607 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002608 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002609 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002610 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002611 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002612 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002613 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002614 return line;
2615}
2616
2617/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002618 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002619 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002620 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2621 */
2622 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002623may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002624{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002625 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002626 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002627 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002628
2629 if (next == NULL)
2630 return FAIL;
2631 *arg = skipwhite(next);
2632 }
2633 return OK;
2634}
2635
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002636/*
2637 * Idem, and give an error when failed.
2638 */
2639 static int
2640may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2641{
2642 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2643 {
2644 emsg(_("E1097: line incomplete"));
2645 return FAIL;
2646 }
2647 return OK;
2648}
2649
2650
Bram Moolenaara5565e42020-05-09 15:44:01 +02002651// Structure passed between the compile_expr* functions to keep track of
2652// constants that have been parsed but for which no code was produced yet. If
2653// possible expressions on these constants are applied at compile time. If
2654// that is not possible, the code to push the constants needs to be generated
2655// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002656// Using 50 should be more than enough of 5 levels of ().
2657#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002658typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002659 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002660 int pp_used; // active entries in pp_tv[]
2661} ppconst_T;
2662
Bram Moolenaar1c747212020-05-09 18:28:34 +02002663static int compile_expr0(char_u **arg, cctx_T *cctx);
2664static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2665
Bram Moolenaara5565e42020-05-09 15:44:01 +02002666/*
2667 * Generate a PUSH instruction for "tv".
2668 * "tv" will be consumed or cleared.
2669 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2670 */
2671 static int
2672generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2673{
2674 if (tv != NULL)
2675 {
2676 switch (tv->v_type)
2677 {
2678 case VAR_UNKNOWN:
2679 break;
2680 case VAR_BOOL:
2681 generate_PUSHBOOL(cctx, tv->vval.v_number);
2682 break;
2683 case VAR_SPECIAL:
2684 generate_PUSHSPEC(cctx, tv->vval.v_number);
2685 break;
2686 case VAR_NUMBER:
2687 generate_PUSHNR(cctx, tv->vval.v_number);
2688 break;
2689#ifdef FEAT_FLOAT
2690 case VAR_FLOAT:
2691 generate_PUSHF(cctx, tv->vval.v_float);
2692 break;
2693#endif
2694 case VAR_BLOB:
2695 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2696 tv->vval.v_blob = NULL;
2697 break;
2698 case VAR_STRING:
2699 generate_PUSHS(cctx, tv->vval.v_string);
2700 tv->vval.v_string = NULL;
2701 break;
2702 default:
2703 iemsg("constant type not supported");
2704 clear_tv(tv);
2705 return FAIL;
2706 }
2707 tv->v_type = VAR_UNKNOWN;
2708 }
2709 return OK;
2710}
2711
2712/*
2713 * Generate code for any ppconst entries.
2714 */
2715 static int
2716generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2717{
2718 int i;
2719 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002720 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002721
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002722 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002723 for (i = 0; i < ppconst->pp_used; ++i)
2724 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2725 ret = FAIL;
2726 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002727 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002728 return ret;
2729}
2730
2731/*
2732 * Clear ppconst constants. Used when failing.
2733 */
2734 static void
2735clear_ppconst(ppconst_T *ppconst)
2736{
2737 int i;
2738
2739 for (i = 0; i < ppconst->pp_used; ++i)
2740 clear_tv(&ppconst->pp_tv[i]);
2741 ppconst->pp_used = 0;
2742}
2743
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002744/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002745 * Generate an instruction to load script-local variable "name", without the
2746 * leading "s:".
2747 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 */
2749 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002750compile_load_scriptvar(
2751 cctx_T *cctx,
2752 char_u *name, // variable NUL terminated
2753 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002754 char_u **end, // end of variable
2755 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002757 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2759 imported_T *import;
2760
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002761 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002763 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002764 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2765 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 }
2767 if (idx >= 0)
2768 {
2769 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2770
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002771 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 current_sctx.sc_sid, idx, sv->sv_type);
2773 return OK;
2774 }
2775
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002776 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 if (import != NULL)
2778 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002779 if (import->imp_all)
2780 {
2781 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002782 char_u *exp_name;
2783 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002784 ufunc_T *ufunc;
2785 type_T *type;
2786
2787 // Used "import * as Name", need to lookup the member.
2788 if (*p != '.')
2789 {
2790 semsg(_("E1060: expected dot after name: %s"), start);
2791 return FAIL;
2792 }
2793 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002794 if (VIM_ISWHITE(*p))
2795 {
2796 emsg(_("E1074: no white space allowed after dot"));
2797 return FAIL;
2798 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002799
Bram Moolenaar1c991142020-07-04 13:15:31 +02002800 // isolate one name
2801 exp_name = p;
2802 while (eval_isnamec(*p))
2803 ++p;
2804 cc = *p;
2805 *p = NUL;
2806
2807 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2808 *p = cc;
2809 p = skipwhite(p);
2810
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002811 // TODO: what if it is a function?
2812 if (idx < 0)
2813 return FAIL;
2814 *end = p;
2815
2816 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2817 import->imp_sid,
2818 idx,
2819 type);
2820 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002821 else if (import->imp_funcname != NULL)
2822 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002823 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002824 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2825 import->imp_sid,
2826 import->imp_var_vals_idx,
2827 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 return OK;
2829 }
2830
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002831 if (error)
2832 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 return FAIL;
2834}
2835
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002836 static int
2837generate_funcref(cctx_T *cctx, char_u *name)
2838{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002839 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002840
2841 if (ufunc == NULL)
2842 return FAIL;
2843
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002844 // Need to compile any default values to get the argument types.
2845 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2846 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2847 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002848 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002849}
2850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851/*
2852 * Compile a variable name into a load instruction.
2853 * "end" points to just after the name.
2854 * When "error" is FALSE do not give an error when not found.
2855 */
2856 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002857compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858{
2859 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002860 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002861 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002863 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864
2865 if (*(*arg + 1) == ':')
2866 {
2867 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002868 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002869 {
2870 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002872 switch (**arg)
2873 {
2874 case 'g': isn_type = ISN_LOADGDICT; break;
2875 case 'w': isn_type = ISN_LOADWDICT; break;
2876 case 't': isn_type = ISN_LOADTDICT; break;
2877 case 'b': isn_type = ISN_LOADBDICT; break;
2878 default:
2879 semsg(_(e_namespace), *arg);
2880 goto theend;
2881 }
2882 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2883 goto theend;
2884 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002885 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 else
2887 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002888 isntype_T isn_type = ISN_DROP;
2889
2890 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2891 if (name == NULL)
2892 return FAIL;
2893
2894 switch (**arg)
2895 {
2896 case 'v': res = generate_LOADV(cctx, name, error);
2897 break;
2898 case 's': res = compile_load_scriptvar(cctx, name,
2899 NULL, NULL, error);
2900 break;
2901 case 'g': isn_type = ISN_LOADG; break;
2902 case 'w': isn_type = ISN_LOADW; break;
2903 case 't': isn_type = ISN_LOADT; break;
2904 case 'b': isn_type = ISN_LOADB; break;
2905 default: semsg(_(e_namespace), *arg);
2906 goto theend;
2907 }
2908 if (isn_type != ISN_DROP)
2909 {
2910 // Global, Buffer-local, Window-local and Tabpage-local
2911 // variables can be defined later, thus we don't check if it
2912 // exists, give error at runtime.
2913 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 }
2916 }
2917 else
2918 {
2919 size_t len = end - *arg;
2920 int idx;
2921 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002922 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923
2924 name = vim_strnsave(*arg, end - *arg);
2925 if (name == NULL)
2926 return FAIL;
2927
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002928 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002930 if (!gen_load_outer)
2931 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 }
2933 else
2934 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002935 lvar_T *lvar = lookup_local(*arg, len, cctx);
2936
2937 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002939 type = lvar->lv_type;
2940 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002941 if (lvar->lv_from_outer)
2942 gen_load_outer = TRUE;
2943 else
2944 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 }
2946 else
2947 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002948 // "var" can be script-local even without using "s:" if it
2949 // already exists.
2950 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2951 == SCRIPT_VERSION_VIM9
2952 || lookup_script(*arg, len) == OK)
2953 res = compile_load_scriptvar(cctx, name, *arg, &end,
2954 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002955
Bram Moolenaara5565e42020-05-09 15:44:01 +02002956 // When the name starts with an uppercase letter or "x:" it
2957 // can be a user defined function.
2958 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2959 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 }
2961 }
2962 if (gen_load)
2963 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002964 if (gen_load_outer)
2965 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 }
2967
2968 *arg = end;
2969
2970theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002971 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 semsg(_(e_var_notfound), name);
2973 vim_free(name);
2974 return res;
2975}
2976
2977/*
2978 * Compile the argument expressions.
2979 * "arg" points to just after the "(" and is advanced to after the ")"
2980 */
2981 static int
2982compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2983{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002984 char_u *p = *arg;
2985 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986
Bram Moolenaare6085c52020-04-12 20:19:16 +02002987 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002989 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2990 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002991 if (*p == ')')
2992 {
2993 *arg = p + 1;
2994 return OK;
2995 }
2996
Bram Moolenaara5565e42020-05-09 15:44:01 +02002997 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 return FAIL;
2999 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003000
3001 if (*p != ',' && *skipwhite(p) == ',')
3002 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02003003 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003004 p = skipwhite(p);
3005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003007 {
3008 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003009 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02003010 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003011 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003012 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003013 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003015failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003016 emsg(_(e_missing_close));
3017 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018}
3019
3020/*
3021 * Compile a function call: name(arg1, arg2)
3022 * "arg" points to "name", "arg + varlen" to the "(".
3023 * "argcount_init" is 1 for "value->method()"
3024 * Instructions:
3025 * EVAL arg1
3026 * EVAL arg2
3027 * BCALL / DCALL / UCALL
3028 */
3029 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003030compile_call(
3031 char_u **arg,
3032 size_t varlen,
3033 cctx_T *cctx,
3034 ppconst_T *ppconst,
3035 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036{
3037 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003038 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 int argcount = argcount_init;
3040 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003041 char_u fname_buf[FLEN_FIXED + 1];
3042 char_u *tofree = NULL;
3043 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003045 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046
Bram Moolenaara5565e42020-05-09 15:44:01 +02003047 // we can evaluate "has('name')" at compile time
3048 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3049 {
3050 char_u *s = skipwhite(*arg + varlen + 1);
3051 typval_T argvars[2];
3052
3053 argvars[0].v_type = VAR_UNKNOWN;
3054 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003055 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003056 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003057 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003058 s = skipwhite(s);
3059 if (*s == ')' && argvars[0].v_type == VAR_STRING)
3060 {
3061 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3062
3063 *arg = s + 1;
3064 argvars[1].v_type = VAR_UNKNOWN;
3065 tv->v_type = VAR_NUMBER;
3066 tv->vval.v_number = 0;
3067 f_has(argvars, tv);
3068 clear_tv(&argvars[0]);
3069 ++ppconst->pp_used;
3070 return OK;
3071 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003072 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003073 }
3074
3075 if (generate_ppconst(cctx, ppconst) == FAIL)
3076 return FAIL;
3077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 if (varlen >= sizeof(namebuf))
3079 {
3080 semsg(_("E1011: name too long: %s"), name);
3081 return FAIL;
3082 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003083 vim_strncpy(namebuf, *arg, varlen);
3084 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085
3086 *arg = skipwhite(*arg + varlen + 1);
3087 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003088 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003090 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 {
3092 int idx;
3093
3094 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003095 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003097 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003098 else
3099 semsg(_(e_unknownfunc), namebuf);
3100 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 }
3102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003104 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003106 {
3107 res = generate_CALL(cctx, ufunc, argcount);
3108 goto theend;
3109 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110
3111 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003112 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003114 if (STRNCMP(namebuf, "g:", 2) != 0
3115 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003116 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003117 garray_T *stack = &cctx->ctx_type_stack;
3118 type_T *type;
3119
3120 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3121 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003122 goto theend;
3123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003125 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003126 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003127 if (STRNCMP(namebuf, "g:", 2) == 0)
3128 res = generate_UCALL(cctx, name, argcount);
3129 else
3130 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003131
3132theend:
3133 vim_free(tofree);
3134 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135}
3136
3137// like NAMESPACE_CHAR but with 'a' and 'l'.
3138#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3139
3140/*
3141 * Find the end of a variable or function name. Unlike find_name_end() this
3142 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003143 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 * Return a pointer to just after the name. Equal to "arg" if there is no
3145 * valid name.
3146 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003147 static char_u *
3148to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149{
3150 char_u *p;
3151
3152 // Quick check for valid starting character.
3153 if (!eval_isnamec1(*arg))
3154 return arg;
3155
3156 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3157 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3158 // and can be used in slice "[n:]".
3159 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003160 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3162 break;
3163 return p;
3164}
3165
3166/*
3167 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003168 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 */
3170 char_u *
3171to_name_const_end(char_u *arg)
3172{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003173 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 typval_T rettv;
3175
3176 if (p == arg && *arg == '[')
3177 {
3178
3179 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003180 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 p = arg;
3182 }
3183 else if (p == arg && *arg == '#' && arg[1] == '{')
3184 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003185 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003187 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 p = arg;
3189 }
3190 else if (p == arg && *arg == '{')
3191 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003192 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003194 // Can be "{x -> ret}()".
3195 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003197 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 if (ret != OK)
3199 p = arg;
3200 }
3201
3202 return p;
3203}
3204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205/*
3206 * parse a list: [expr, expr]
3207 * "*arg" points to the '['.
3208 */
3209 static int
3210compile_list(char_u **arg, cctx_T *cctx)
3211{
3212 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003213 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 int count = 0;
3215
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003216 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003218 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003219 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003220 semsg(_(e_list_end), *arg);
3221 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003222 }
3223 if (*p == ']')
3224 {
3225 ++p;
3226 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003227 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003228 p += STRLEN(p);
3229 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003230 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003231 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 break;
3233 ++count;
3234 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003235 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003237 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3238 {
3239 semsg(_(e_white_after), ",");
3240 return FAIL;
3241 }
3242 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003243 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 p = skipwhite(p);
3245 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003246 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247
3248 generate_NEWLIST(cctx, count);
3249 return OK;
3250}
3251
3252/*
3253 * parse a lambda: {arg, arg -> expr}
3254 * "*arg" points to the '{'.
3255 */
3256 static int
3257compile_lambda(char_u **arg, cctx_T *cctx)
3258{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 typval_T rettv;
3260 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003261 evalarg_T evalarg;
3262
3263 CLEAR_FIELD(evalarg);
3264 evalarg.eval_flags = EVAL_EVALUATE;
3265 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266
3267 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003268 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003272 ++ufunc->uf_refcount;
3273 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003274 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275
3276 // The function will have one line: "return {expr}".
3277 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003278 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003280 clear_evalarg(&evalarg, NULL);
3281
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003282 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003283 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003284
3285 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 return FAIL;
3287}
3288
3289/*
3290 * Compile a lamda call: expr->{lambda}(args)
3291 * "arg" points to the "{".
3292 */
3293 static int
3294compile_lambda_call(char_u **arg, cctx_T *cctx)
3295{
3296 ufunc_T *ufunc;
3297 typval_T rettv;
3298 int argcount = 1;
3299 int ret = FAIL;
3300
3301 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003302 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 return FAIL;
3304
3305 if (**arg != '(')
3306 {
3307 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003308 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 else
3310 semsg(_(e_missing_paren), "lambda");
3311 clear_tv(&rettv);
3312 return FAIL;
3313 }
3314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 ufunc = rettv.vval.v_partial->pt_func;
3316 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003317 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003318 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003319
3320 // The function will have one line: "return {expr}".
3321 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003322 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323
3324 // compile the arguments
3325 *arg = skipwhite(*arg + 1);
3326 if (compile_arguments(arg, cctx, &argcount) == OK)
3327 // call the compiled function
3328 ret = generate_CALL(cctx, ufunc, argcount);
3329
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003330 if (ret == FAIL)
3331 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 return ret;
3333}
3334
3335/*
3336 * parse a dict: {'key': val} or #{key: val}
3337 * "*arg" points to the '{'.
3338 */
3339 static int
3340compile_dict(char_u **arg, cctx_T *cctx, int literal)
3341{
3342 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003343 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 int count = 0;
3345 dict_T *d = dict_alloc();
3346 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003347 char_u *whitep = *arg;
3348 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349
3350 if (d == NULL)
3351 return FAIL;
3352 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003353 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 {
3355 char_u *key = NULL;
3356
Bram Moolenaar23c55272020-06-21 16:58:13 +02003357 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003358 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003359 *arg = NULL;
3360 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003361 }
3362
3363 if (**arg == '}')
3364 break;
3365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 if (literal)
3367 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003368 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369
Bram Moolenaar2c330432020-04-13 14:41:35 +02003370 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 {
3372 semsg(_("E1014: Invalid key: %s"), *arg);
3373 return FAIL;
3374 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003375 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 if (generate_PUSHS(cctx, key) == FAIL)
3377 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003378 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 }
3380 else
3381 {
3382 isn_T *isn;
3383
Bram Moolenaara5565e42020-05-09 15:44:01 +02003384 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3387 if (isn->isn_type == ISN_PUSHS)
3388 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003389 else
3390 {
3391 type_T *keytype = ((type_T **)stack->ga_data)
3392 [stack->ga_len - 1];
3393 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3394 return FAIL;
3395 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 }
3397
3398 // Check for duplicate keys, if using string keys.
3399 if (key != NULL)
3400 {
3401 item = dict_find(d, key, -1);
3402 if (item != NULL)
3403 {
3404 semsg(_(e_duplicate_key), key);
3405 goto failret;
3406 }
3407 item = dictitem_alloc(key);
3408 if (item != NULL)
3409 {
3410 item->di_tv.v_type = VAR_UNKNOWN;
3411 item->di_tv.v_lock = 0;
3412 if (dict_add(d, item) == FAIL)
3413 dictitem_free(item);
3414 }
3415 }
3416
3417 *arg = skipwhite(*arg);
3418 if (**arg != ':')
3419 {
3420 semsg(_(e_missing_dict_colon), *arg);
3421 return FAIL;
3422 }
3423
Bram Moolenaar2c330432020-04-13 14:41:35 +02003424 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003426 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003427 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003428 *arg = NULL;
3429 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003430 }
3431
Bram Moolenaara5565e42020-05-09 15:44:01 +02003432 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 return FAIL;
3434 ++count;
3435
Bram Moolenaar2c330432020-04-13 14:41:35 +02003436 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003437 *arg = skipwhite(*arg);
3438 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003439 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003440 *arg = NULL;
3441 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003442 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 if (**arg == '}')
3444 break;
3445 if (**arg != ',')
3446 {
3447 semsg(_(e_missing_dict_comma), *arg);
3448 goto failret;
3449 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003450 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 *arg = skipwhite(*arg + 1);
3452 }
3453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 *arg = *arg + 1;
3455
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003456 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003457 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003458 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003459 *arg += STRLEN(*arg);
3460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 dict_unref(d);
3462 return generate_NEWDICT(cctx, count);
3463
3464failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003465 if (*arg == NULL)
3466 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 dict_unref(d);
3468 return FAIL;
3469}
3470
3471/*
3472 * Compile "&option".
3473 */
3474 static int
3475compile_get_option(char_u **arg, cctx_T *cctx)
3476{
3477 typval_T rettv;
3478 char_u *start = *arg;
3479 int ret;
3480
3481 // parse the option and get the current value to get the type.
3482 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003483 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 if (ret == OK)
3485 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003486 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 char_u *name = vim_strnsave(start, *arg - start);
3488 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3489
3490 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3491 vim_free(name);
3492 }
3493 clear_tv(&rettv);
3494
3495 return ret;
3496}
3497
3498/*
3499 * Compile "$VAR".
3500 */
3501 static int
3502compile_get_env(char_u **arg, cctx_T *cctx)
3503{
3504 char_u *start = *arg;
3505 int len;
3506 int ret;
3507 char_u *name;
3508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 ++*arg;
3510 len = get_env_len(arg);
3511 if (len == 0)
3512 {
3513 semsg(_(e_syntax_at), start - 1);
3514 return FAIL;
3515 }
3516
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003517 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 name = vim_strnsave(start, len + 1);
3519 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3520 vim_free(name);
3521 return ret;
3522}
3523
3524/*
3525 * Compile "@r".
3526 */
3527 static int
3528compile_get_register(char_u **arg, cctx_T *cctx)
3529{
3530 int ret;
3531
3532 ++*arg;
3533 if (**arg == NUL)
3534 {
3535 semsg(_(e_syntax_at), *arg - 1);
3536 return FAIL;
3537 }
3538 if (!valid_yank_reg(**arg, TRUE))
3539 {
3540 emsg_invreg(**arg);
3541 return FAIL;
3542 }
3543 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3544 ++*arg;
3545 return ret;
3546}
3547
3548/*
3549 * Apply leading '!', '-' and '+' to constant "rettv".
3550 */
3551 static int
3552apply_leader(typval_T *rettv, char_u *start, char_u *end)
3553{
3554 char_u *p = end;
3555
3556 // this works from end to start
3557 while (p > start)
3558 {
3559 --p;
3560 if (*p == '-' || *p == '+')
3561 {
3562 // only '-' has an effect, for '+' we only check the type
3563#ifdef FEAT_FLOAT
3564 if (rettv->v_type == VAR_FLOAT)
3565 {
3566 if (*p == '-')
3567 rettv->vval.v_float = -rettv->vval.v_float;
3568 }
3569 else
3570#endif
3571 {
3572 varnumber_T val;
3573 int error = FALSE;
3574
3575 // tv_get_number_chk() accepts a string, but we don't want that
3576 // here
3577 if (check_not_string(rettv) == FAIL)
3578 return FAIL;
3579 val = tv_get_number_chk(rettv, &error);
3580 clear_tv(rettv);
3581 if (error)
3582 return FAIL;
3583 if (*p == '-')
3584 val = -val;
3585 rettv->v_type = VAR_NUMBER;
3586 rettv->vval.v_number = val;
3587 }
3588 }
3589 else
3590 {
3591 int v = tv2bool(rettv);
3592
3593 // '!' is permissive in the type.
3594 clear_tv(rettv);
3595 rettv->v_type = VAR_BOOL;
3596 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3597 }
3598 }
3599 return OK;
3600}
3601
3602/*
3603 * Recognize v: variables that are constants and set "rettv".
3604 */
3605 static void
3606get_vim_constant(char_u **arg, typval_T *rettv)
3607{
3608 if (STRNCMP(*arg, "v:true", 6) == 0)
3609 {
3610 rettv->v_type = VAR_BOOL;
3611 rettv->vval.v_number = VVAL_TRUE;
3612 *arg += 6;
3613 }
3614 else if (STRNCMP(*arg, "v:false", 7) == 0)
3615 {
3616 rettv->v_type = VAR_BOOL;
3617 rettv->vval.v_number = VVAL_FALSE;
3618 *arg += 7;
3619 }
3620 else if (STRNCMP(*arg, "v:null", 6) == 0)
3621 {
3622 rettv->v_type = VAR_SPECIAL;
3623 rettv->vval.v_number = VVAL_NULL;
3624 *arg += 6;
3625 }
3626 else if (STRNCMP(*arg, "v:none", 6) == 0)
3627 {
3628 rettv->v_type = VAR_SPECIAL;
3629 rettv->vval.v_number = VVAL_NONE;
3630 *arg += 6;
3631 }
3632}
3633
Bram Moolenaar61a89812020-05-07 16:58:17 +02003634 static exptype_T
3635get_compare_type(char_u *p, int *len, int *type_is)
3636{
3637 exptype_T type = EXPR_UNKNOWN;
3638 int i;
3639
3640 switch (p[0])
3641 {
3642 case '=': if (p[1] == '=')
3643 type = EXPR_EQUAL;
3644 else if (p[1] == '~')
3645 type = EXPR_MATCH;
3646 break;
3647 case '!': if (p[1] == '=')
3648 type = EXPR_NEQUAL;
3649 else if (p[1] == '~')
3650 type = EXPR_NOMATCH;
3651 break;
3652 case '>': if (p[1] != '=')
3653 {
3654 type = EXPR_GREATER;
3655 *len = 1;
3656 }
3657 else
3658 type = EXPR_GEQUAL;
3659 break;
3660 case '<': if (p[1] != '=')
3661 {
3662 type = EXPR_SMALLER;
3663 *len = 1;
3664 }
3665 else
3666 type = EXPR_SEQUAL;
3667 break;
3668 case 'i': if (p[1] == 's')
3669 {
3670 // "is" and "isnot"; but not a prefix of a name
3671 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3672 *len = 5;
3673 i = p[*len];
3674 if (!isalnum(i) && i != '_')
3675 {
3676 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3677 *type_is = TRUE;
3678 }
3679 }
3680 break;
3681 }
3682 return type;
3683}
3684
3685/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 * Compile code to apply '-', '+' and '!'.
3687 */
3688 static int
3689compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3690{
3691 char_u *p = end;
3692
3693 // this works from end to start
3694 while (p > start)
3695 {
3696 --p;
3697 if (*p == '-' || *p == '+')
3698 {
3699 int negate = *p == '-';
3700 isn_T *isn;
3701
3702 // TODO: check type
3703 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3704 {
3705 --p;
3706 if (*p == '-')
3707 negate = !negate;
3708 }
3709 // only '-' has an effect, for '+' we only check the type
3710 if (negate)
3711 isn = generate_instr(cctx, ISN_NEGATENR);
3712 else
3713 isn = generate_instr(cctx, ISN_CHECKNR);
3714 if (isn == NULL)
3715 return FAIL;
3716 }
3717 else
3718 {
3719 int invert = TRUE;
3720
3721 while (p > start && p[-1] == '!')
3722 {
3723 --p;
3724 invert = !invert;
3725 }
3726 if (generate_2BOOL(cctx, invert) == FAIL)
3727 return FAIL;
3728 }
3729 }
3730 return OK;
3731}
3732
3733/*
3734 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003735 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 */
3737 static int
3738compile_subscript(
3739 char_u **arg,
3740 cctx_T *cctx,
3741 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003742 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003743 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744{
3745 for (;;)
3746 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003747 char_u *p = skipwhite(*arg);
3748
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003749 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003750 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003751 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003752
3753 // If a following line starts with "->{" or "->X" advance to that
3754 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003755 // Also if a following line starts with ".x".
3756 if (next != NULL &&
3757 ((next[0] == '-' && next[1] == '>'
3758 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3759 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003760 {
3761 next = next_line_from_context(cctx, TRUE);
3762 if (next == NULL)
3763 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003764 *arg = next;
3765 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003766 }
3767 }
3768
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003769 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3770 // not a function call.
3771 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003773 garray_T *stack = &cctx->ctx_type_stack;
3774 type_T *type;
3775 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003777 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003778 return FAIL;
3779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003781 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3782
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003783 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3785 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003786 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 return FAIL;
3788 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003789 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003791 char_u *pstart = p;
3792
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003793 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003794 return FAIL;
3795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 // something->method()
3797 // Apply the '!', '-' and '+' first:
3798 // -1.0->func() works like (-1.0)->func()
3799 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3800 return FAIL;
3801 *start_leader = end_leader; // don't apply again later
3802
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003803 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003804 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003805 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 if (**arg == '{')
3807 {
3808 // lambda call: list->{lambda}
3809 if (compile_lambda_call(arg, cctx) == FAIL)
3810 return FAIL;
3811 }
3812 else
3813 {
3814 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003815 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003816 if (!eval_isnamec1(*p))
3817 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003818 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003819 return FAIL;
3820 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003821 if (ASCII_ISALPHA(*p) && p[1] == ':')
3822 p += 2;
3823 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 ;
3825 if (*p != '(')
3826 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003827 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 return FAIL;
3829 }
3830 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003831 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 return FAIL;
3833 }
3834 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003835 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003837 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003838 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003839 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003840
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003841 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003842 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003843 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003844 // TODO: blob index
3845 // TODO: more arguments
3846 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003847 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003848 return FAIL;
3849
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003850 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003851 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003852 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003853 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003854 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 return FAIL;
3856
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003857 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3858 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 if (**arg != ']')
3860 {
3861 emsg(_(e_missbrac));
3862 return FAIL;
3863 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003864 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003866 // We can index a list and a dict. If we don't know the type
3867 // we can use the index value type.
3868 // TODO: If we don't know use an instruction to figure it out at
3869 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003870 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003871 vtype = (*typep)->tt_type;
3872 if (*typep == &t_any)
3873 {
3874 type_T *valtype = ((type_T **)stack->ga_data)
3875 [stack->ga_len - 1];
3876 if (valtype == &t_string)
3877 vtype = VAR_DICT;
3878 }
3879 if (vtype == VAR_DICT)
3880 {
3881 if ((*typep)->tt_type == VAR_DICT)
3882 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003883 else
3884 {
3885 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3886 return FAIL;
3887 *typep = &t_any;
3888 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003889 if (may_generate_2STRING(-1, cctx) == FAIL)
3890 return FAIL;
3891 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3892 return FAIL;
3893 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003894 else if (vtype == VAR_STRING)
3895 {
3896 *typep = &t_number;
3897 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3898 return FAIL;
3899 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003900 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003901 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003902 if ((*typep)->tt_type == VAR_LIST)
3903 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003904 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003905 return FAIL;
3906 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003907 else
3908 {
3909 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003910 return FAIL;
3911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003913 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003915 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003916 return FAIL;
3917
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003918 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003919 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3920 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003922 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 if (eval_isnamec1(*p))
3924 while (eval_isnamec(*p))
3925 MB_PTR_ADV(p);
3926 if (p == *arg)
3927 {
3928 semsg(_(e_syntax_at), *arg);
3929 return FAIL;
3930 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003931 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 return FAIL;
3933 *arg = p;
3934 }
3935 else
3936 break;
3937 }
3938
3939 // TODO - see handle_subscript():
3940 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3941 // Don't do this when "Func" is already a partial that was bound
3942 // explicitly (pt_auto is FALSE).
3943
3944 return OK;
3945}
3946
3947/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003948 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3949 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003951 * If the value is a constant "ppconst->pp_ret" will be set.
3952 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003953 *
3954 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 */
3956
3957/*
3958 * number number constant
3959 * 0zFFFFFFFF Blob constant
3960 * "string" string constant
3961 * 'string' literal string constant
3962 * &option-name option value
3963 * @r register contents
3964 * identifier variable value
3965 * function() function call
3966 * $VAR environment variable
3967 * (expression) nested expression
3968 * [expr, expr] List
3969 * {key: val, key: val} Dictionary
3970 * #{key: val, key: val} Dictionary with literal keys
3971 *
3972 * Also handle:
3973 * ! in front logical NOT
3974 * - in front unary minus
3975 * + in front unary plus (ignored)
3976 * trailing (arg) funcref/partial call
3977 * trailing [] subscript in String or List
3978 * trailing .name entry in Dictionary
3979 * trailing ->name() method call
3980 */
3981 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003982compile_expr7(
3983 char_u **arg,
3984 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003985 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 char_u *start_leader, *end_leader;
3988 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003989 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003990 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991
3992 /*
3993 * Skip '!', '-' and '+' characters. They are handled later.
3994 */
3995 start_leader = *arg;
3996 while (**arg == '!' || **arg == '-' || **arg == '+')
3997 *arg = skipwhite(*arg + 1);
3998 end_leader = *arg;
3999
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004000 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 switch (**arg)
4002 {
4003 /*
4004 * Number constant.
4005 */
4006 case '0': // also for blob starting with 0z
4007 case '1':
4008 case '2':
4009 case '3':
4010 case '4':
4011 case '5':
4012 case '6':
4013 case '7':
4014 case '8':
4015 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004016 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 return FAIL;
4018 break;
4019
4020 /*
4021 * String constant: "string".
4022 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004023 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 return FAIL;
4025 break;
4026
4027 /*
4028 * Literal string constant: 'str''ing'.
4029 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004030 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 return FAIL;
4032 break;
4033
4034 /*
4035 * Constant Vim variable.
4036 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004037 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 ret = NOTDONE;
4039 break;
4040
4041 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004042 * "true" constant
4043 */
4044 case 't': if (STRNCMP(*arg, "true", 4) == 0
4045 && !eval_isnamec((*arg)[4]))
4046 {
4047 *arg += 4;
4048 rettv->v_type = VAR_BOOL;
4049 rettv->vval.v_number = VVAL_TRUE;
4050 }
4051 else
4052 ret = NOTDONE;
4053 break;
4054
4055 /*
4056 * "false" constant
4057 */
4058 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4059 && !eval_isnamec((*arg)[5]))
4060 {
4061 *arg += 5;
4062 rettv->v_type = VAR_BOOL;
4063 rettv->vval.v_number = VVAL_FALSE;
4064 }
4065 else
4066 ret = NOTDONE;
4067 break;
4068
4069 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 * List: [expr, expr]
4071 */
4072 case '[': ret = compile_list(arg, cctx);
4073 break;
4074
4075 /*
4076 * Dictionary: #{key: val, key: val}
4077 */
4078 case '#': if ((*arg)[1] == '{')
4079 {
4080 ++*arg;
4081 ret = compile_dict(arg, cctx, TRUE);
4082 }
4083 else
4084 ret = NOTDONE;
4085 break;
4086
4087 /*
4088 * Lambda: {arg, arg -> expr}
4089 * Dictionary: {'key': val, 'key': val}
4090 */
4091 case '{': {
4092 char_u *start = skipwhite(*arg + 1);
4093
4094 // Find out what comes after the arguments.
4095 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004096 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 if (ret != FAIL && *start == '>')
4098 ret = compile_lambda(arg, cctx);
4099 else
4100 ret = compile_dict(arg, cctx, FALSE);
4101 }
4102 break;
4103
4104 /*
4105 * Option value: &name
4106 */
4107 case '&': ret = compile_get_option(arg, cctx);
4108 break;
4109
4110 /*
4111 * Environment variable: $VAR.
4112 */
4113 case '$': ret = compile_get_env(arg, cctx);
4114 break;
4115
4116 /*
4117 * Register contents: @r.
4118 */
4119 case '@': ret = compile_get_register(arg, cctx);
4120 break;
4121 /*
4122 * nested expression: (expression).
4123 */
4124 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004125
4126 // recursive!
4127 if (ppconst->pp_used <= PPSIZE - 10)
4128 {
4129 ret = compile_expr1(arg, cctx, ppconst);
4130 }
4131 else
4132 {
4133 // Not enough space in ppconst, flush constants.
4134 if (generate_ppconst(cctx, ppconst) == FAIL)
4135 return FAIL;
4136 ret = compile_expr0(arg, cctx);
4137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 *arg = skipwhite(*arg);
4139 if (**arg == ')')
4140 ++*arg;
4141 else if (ret == OK)
4142 {
4143 emsg(_(e_missing_close));
4144 ret = FAIL;
4145 }
4146 break;
4147
4148 default: ret = NOTDONE;
4149 break;
4150 }
4151 if (ret == FAIL)
4152 return FAIL;
4153
Bram Moolenaar1c747212020-05-09 18:28:34 +02004154 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 {
4156 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004157 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004159 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 return FAIL;
4161 }
4162 start_leader = end_leader; // don't apply again below
4163
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004164 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004165 clear_tv(rettv);
4166 else
4167 // A constant expression can possibly be handled compile time,
4168 // return the value instead of generating code.
4169 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 }
4171 else if (ret == NOTDONE)
4172 {
4173 char_u *p;
4174 int r;
4175
4176 if (!eval_isnamec1(**arg))
4177 {
4178 semsg(_("E1015: Name expected: %s"), *arg);
4179 return FAIL;
4180 }
4181
4182 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004183 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004185 {
4186 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4187 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004189 {
4190 if (generate_ppconst(cctx, ppconst) == FAIL)
4191 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004193 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 if (r == FAIL)
4195 return FAIL;
4196 }
4197
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004198 // Handle following "[]", ".member", etc.
4199 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004200 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004201 ppconst) == FAIL)
4202 return FAIL;
4203 if (ppconst->pp_used > 0)
4204 {
4205 // apply the '!', '-' and '+' before the constant
4206 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4207 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4208 return FAIL;
4209 return OK;
4210 }
4211 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004213 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214}
4215
4216/*
4217 * * number multiplication
4218 * / number division
4219 * % number modulo
4220 */
4221 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004222compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223{
4224 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004225 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004226 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004228 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004229 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 return FAIL;
4231
4232 /*
4233 * Repeat computing, until no "*", "/" or "%" is following.
4234 */
4235 for (;;)
4236 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004237 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 if (*op != '*' && *op != '/' && *op != '%')
4239 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004240 if (next != NULL)
4241 {
4242 *arg = next_line_from_context(cctx, TRUE);
4243 op = skipwhite(*arg);
4244 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004245
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004246 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 {
4248 char_u buf[3];
4249
4250 vim_strncpy(buf, op, 1);
4251 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004252 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 }
4254 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004255 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004256 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004258 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004259 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004261
4262 if (ppconst->pp_used == ppconst_used + 2
4263 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4264 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004265 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004266 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4267 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004268 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004270 // both are numbers: compute the result
4271 switch (*op)
4272 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004273 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004274 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004275 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004276 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004277 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004278 break;
4279 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004280 tv1->vval.v_number = res;
4281 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004282 }
4283 else
4284 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004285 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004286 generate_two_op(cctx, op);
4287 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 }
4289
4290 return OK;
4291}
4292
4293/*
4294 * + number addition
4295 * - number subtraction
4296 * .. string concatenation
4297 */
4298 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004299compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300{
4301 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004302 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004304 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305
4306 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004307 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 return FAIL;
4309
4310 /*
4311 * Repeat computing, until no "+", "-" or ".." is following.
4312 */
4313 for (;;)
4314 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004315 op = may_peek_next_line(cctx, *arg, &next);
4316 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 break;
4318 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004319 if (next != NULL)
4320 {
4321 *arg = next_line_from_context(cctx, TRUE);
4322 op = skipwhite(*arg);
4323 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004325 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 {
4327 char_u buf[3];
4328
4329 vim_strncpy(buf, op, oplen);
4330 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004331 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 }
4333
4334 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004335 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004336 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004338 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004339 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 return FAIL;
4341
Bram Moolenaara5565e42020-05-09 15:44:01 +02004342 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004343 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004344 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4345 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4346 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4347 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004349 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4350 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004351
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004352 // concat/subtract/add constant numbers
4353 if (*op == '+')
4354 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4355 else if (*op == '-')
4356 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4357 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004358 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004359 // concatenate constant strings
4360 char_u *s1 = tv1->vval.v_string;
4361 char_u *s2 = tv2->vval.v_string;
4362 size_t len1 = STRLEN(s1);
4363
4364 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4365 if (tv1->vval.v_string == NULL)
4366 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004367 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004368 return FAIL;
4369 }
4370 mch_memmove(tv1->vval.v_string, s1, len1);
4371 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004372 vim_free(s1);
4373 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004374 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004375 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 }
4377 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004378 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004379 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004380 if (*op == '.')
4381 {
4382 if (may_generate_2STRING(-2, cctx) == FAIL
4383 || may_generate_2STRING(-1, cctx) == FAIL)
4384 return FAIL;
4385 generate_instr_drop(cctx, ISN_CONCAT, 1);
4386 }
4387 else
4388 generate_two_op(cctx, op);
4389 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 }
4391
4392 return OK;
4393}
4394
4395/*
4396 * expr5a == expr5b
4397 * expr5a =~ expr5b
4398 * expr5a != expr5b
4399 * expr5a !~ expr5b
4400 * expr5a > expr5b
4401 * expr5a >= expr5b
4402 * expr5a < expr5b
4403 * expr5a <= expr5b
4404 * expr5a is expr5b
4405 * expr5a isnot expr5b
4406 *
4407 * Produces instructions:
4408 * EVAL expr5a Push result of "expr5a"
4409 * EVAL expr5b Push result of "expr5b"
4410 * COMPARE one of the compare instructions
4411 */
4412 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004413compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414{
4415 exptype_T type = EXPR_UNKNOWN;
4416 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004417 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004420 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421
4422 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004423 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 return FAIL;
4425
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004426 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004427 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428
4429 /*
4430 * If there is a comparative operator, use it.
4431 */
4432 if (type != EXPR_UNKNOWN)
4433 {
4434 int ic = FALSE; // Default: do not ignore case
4435
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004436 if (next != NULL)
4437 {
4438 *arg = next_line_from_context(cctx, TRUE);
4439 p = skipwhite(*arg);
4440 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 if (type_is && (p[len] == '?' || p[len] == '#'))
4442 {
4443 semsg(_(e_invexpr2), *arg);
4444 return FAIL;
4445 }
4446 // extra question mark appended: ignore case
4447 if (p[len] == '?')
4448 {
4449 ic = TRUE;
4450 ++len;
4451 }
4452 // extra '#' appended: match case (ignored)
4453 else if (p[len] == '#')
4454 ++len;
4455 // nothing appended: match case
4456
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004457 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 {
4459 char_u buf[7];
4460
4461 vim_strncpy(buf, p, len);
4462 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004463 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 }
4465
4466 // get the second variable
4467 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004468 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004469 return FAIL;
4470
Bram Moolenaara5565e42020-05-09 15:44:01 +02004471 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 return FAIL;
4473
Bram Moolenaara5565e42020-05-09 15:44:01 +02004474 if (ppconst->pp_used == ppconst_used + 2)
4475 {
4476 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4477 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4478 int ret;
4479
4480 // Both sides are a constant, compute the result now.
4481 // First check for a valid combination of types, this is more
4482 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004483 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004484 ret = FAIL;
4485 else
4486 {
4487 ret = typval_compare(tv1, tv2, type, ic);
4488 tv1->v_type = VAR_BOOL;
4489 tv1->vval.v_number = tv1->vval.v_number
4490 ? VVAL_TRUE : VVAL_FALSE;
4491 clear_tv(tv2);
4492 --ppconst->pp_used;
4493 }
4494 return ret;
4495 }
4496
4497 generate_ppconst(cctx, ppconst);
4498 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 }
4500
4501 return OK;
4502}
4503
Bram Moolenaar7f141552020-05-09 17:35:53 +02004504static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506/*
4507 * Compile || or &&.
4508 */
4509 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004510compile_and_or(
4511 char_u **arg,
4512 cctx_T *cctx,
4513 char *op,
4514 ppconst_T *ppconst,
4515 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004517 char_u *next;
4518 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 int opchar = *op;
4520
4521 if (p[0] == opchar && p[1] == opchar)
4522 {
4523 garray_T *instr = &cctx->ctx_instr;
4524 garray_T end_ga;
4525
4526 /*
4527 * Repeat until there is no following "||" or "&&"
4528 */
4529 ga_init2(&end_ga, sizeof(int), 10);
4530 while (p[0] == opchar && p[1] == opchar)
4531 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004532 if (next != NULL)
4533 {
4534 *arg = next_line_from_context(cctx, TRUE);
4535 p = skipwhite(*arg);
4536 }
4537
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004538 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4539 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004541 return FAIL;
4542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543
Bram Moolenaara5565e42020-05-09 15:44:01 +02004544 // TODO: use ppconst if the value is a constant
4545 generate_ppconst(cctx, ppconst);
4546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 if (ga_grow(&end_ga, 1) == FAIL)
4548 {
4549 ga_clear(&end_ga);
4550 return FAIL;
4551 }
4552 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4553 ++end_ga.ga_len;
4554 generate_JUMP(cctx, opchar == '|'
4555 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4556
4557 // eval the next expression
4558 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004559 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004560 return FAIL;
4561
Bram Moolenaara5565e42020-05-09 15:44:01 +02004562 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4563 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 {
4565 ga_clear(&end_ga);
4566 return FAIL;
4567 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004568
4569 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004571 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572
4573 // Fill in the end label in all jumps.
4574 while (end_ga.ga_len > 0)
4575 {
4576 isn_T *isn;
4577
4578 --end_ga.ga_len;
4579 isn = ((isn_T *)instr->ga_data)
4580 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4581 isn->isn_arg.jump.jump_where = instr->ga_len;
4582 }
4583 ga_clear(&end_ga);
4584 }
4585
4586 return OK;
4587}
4588
4589/*
4590 * expr4a && expr4a && expr4a logical AND
4591 *
4592 * Produces instructions:
4593 * EVAL expr4a Push result of "expr4a"
4594 * JUMP_AND_KEEP_IF_FALSE end
4595 * EVAL expr4b Push result of "expr4b"
4596 * JUMP_AND_KEEP_IF_FALSE end
4597 * EVAL expr4c Push result of "expr4c"
4598 * end:
4599 */
4600 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004601compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004603 int ppconst_used = ppconst->pp_used;
4604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004606 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 return FAIL;
4608
4609 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004610 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611}
4612
4613/*
4614 * expr3a || expr3b || expr3c logical OR
4615 *
4616 * Produces instructions:
4617 * EVAL expr3a Push result of "expr3a"
4618 * JUMP_AND_KEEP_IF_TRUE end
4619 * EVAL expr3b Push result of "expr3b"
4620 * JUMP_AND_KEEP_IF_TRUE end
4621 * EVAL expr3c Push result of "expr3c"
4622 * end:
4623 */
4624 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004625compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004627 int ppconst_used = ppconst->pp_used;
4628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004630 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 return FAIL;
4632
4633 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004634 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635}
4636
4637/*
4638 * Toplevel expression: expr2 ? expr1a : expr1b
4639 *
4640 * Produces instructions:
4641 * EVAL expr2 Push result of "expr"
4642 * JUMP_IF_FALSE alt jump if false
4643 * EVAL expr1a
4644 * JUMP_ALWAYS end
4645 * alt: EVAL expr1b
4646 * end:
4647 */
4648 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004649compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650{
4651 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004652 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004653 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654
Bram Moolenaar61a89812020-05-07 16:58:17 +02004655 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004656 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 return FAIL;
4658
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004659 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 if (*p == '?')
4661 {
4662 garray_T *instr = &cctx->ctx_instr;
4663 garray_T *stack = &cctx->ctx_type_stack;
4664 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004665 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004667 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 int has_const_expr = FALSE;
4670 int const_value = FALSE;
4671 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004673 if (next != NULL)
4674 {
4675 *arg = next_line_from_context(cctx, TRUE);
4676 p = skipwhite(*arg);
4677 }
4678
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004679 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4680 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004682 return FAIL;
4683 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684
Bram Moolenaara5565e42020-05-09 15:44:01 +02004685 if (ppconst->pp_used == ppconst_used + 1)
4686 {
4687 // the condition is a constant, we know whether the ? or the :
4688 // expression is to be evaluated.
4689 has_const_expr = TRUE;
4690 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4691 clear_tv(&ppconst->pp_tv[ppconst_used]);
4692 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004693 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4694 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004695 }
4696 else
4697 {
4698 generate_ppconst(cctx, ppconst);
4699 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4700 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701
4702 // evaluate the second expression; any type is accepted
4703 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004704 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004705 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004706 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004707 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708
Bram Moolenaara5565e42020-05-09 15:44:01 +02004709 if (!has_const_expr)
4710 {
4711 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712
Bram Moolenaara5565e42020-05-09 15:44:01 +02004713 // remember the type and drop it
4714 --stack->ga_len;
4715 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716
Bram Moolenaara5565e42020-05-09 15:44:01 +02004717 end_idx = instr->ga_len;
4718 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4719
4720 // jump here from JUMP_IF_FALSE
4721 isn = ((isn_T *)instr->ga_data) + alt_idx;
4722 isn->isn_arg.jump.jump_where = instr->ga_len;
4723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724
4725 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004726 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 if (*p != ':')
4728 {
4729 emsg(_(e_missing_colon));
4730 return FAIL;
4731 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004732 if (next != NULL)
4733 {
4734 *arg = next_line_from_context(cctx, TRUE);
4735 p = skipwhite(*arg);
4736 }
4737
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004738 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4739 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004741 return FAIL;
4742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743
4744 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004745 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004746 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4747 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004749 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004750 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004751 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004752 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753
Bram Moolenaara5565e42020-05-09 15:44:01 +02004754 if (!has_const_expr)
4755 {
4756 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757
Bram Moolenaara5565e42020-05-09 15:44:01 +02004758 // If the types differ, the result has a more generic type.
4759 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4760 common_type(type1, type2, &type2, cctx->ctx_type_list);
4761
4762 // jump here from JUMP_ALWAYS
4763 isn = ((isn_T *)instr->ga_data) + end_idx;
4764 isn->isn_arg.jump.jump_where = instr->ga_len;
4765 }
4766
4767 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768 }
4769 return OK;
4770}
4771
4772/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004773 * Toplevel expression.
4774 */
4775 static int
4776compile_expr0(char_u **arg, cctx_T *cctx)
4777{
4778 ppconst_T ppconst;
4779
4780 CLEAR_FIELD(ppconst);
4781 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4782 {
4783 clear_ppconst(&ppconst);
4784 return FAIL;
4785 }
4786 if (generate_ppconst(cctx, &ppconst) == FAIL)
4787 return FAIL;
4788 return OK;
4789}
4790
4791/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 * compile "return [expr]"
4793 */
4794 static char_u *
4795compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4796{
4797 char_u *p = arg;
4798 garray_T *stack = &cctx->ctx_type_stack;
4799 type_T *stack_type;
4800
4801 if (*p != NUL && *p != '|' && *p != '\n')
4802 {
4803 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004804 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 return NULL;
4806
4807 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4808 if (set_return_type)
4809 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004810 else
4811 {
4812 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4813 && stack_type->tt_type != VAR_VOID
4814 && stack_type->tt_type != VAR_UNKNOWN)
4815 {
4816 emsg(_("E1096: Returning a value in a function without a return type"));
4817 return NULL;
4818 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004819 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4820 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 }
4824 else
4825 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004826 // "set_return_type" cannot be TRUE, only used for a lambda which
4827 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004828 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4829 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 {
4831 emsg(_("E1003: Missing return value"));
4832 return NULL;
4833 }
4834
4835 // No argument, return zero.
4836 generate_PUSHNR(cctx, 0);
4837 }
4838
4839 if (generate_instr(cctx, ISN_RETURN) == NULL)
4840 return NULL;
4841
4842 // "return val | endif" is possible
4843 return skipwhite(p);
4844}
4845
4846/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004847 * Get a line from the compilation context, compatible with exarg_T getline().
4848 * Return a pointer to the line in allocated memory.
4849 * Return NULL for end-of-file or some error.
4850 */
4851 static char_u *
4852exarg_getline(
4853 int c UNUSED,
4854 void *cookie,
4855 int indent UNUSED,
4856 int do_concat UNUSED)
4857{
4858 cctx_T *cctx = (cctx_T *)cookie;
4859
4860 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4861 {
4862 iemsg("Heredoc got to end");
4863 return NULL;
4864 }
4865 ++cctx->ctx_lnum;
4866 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4867 [cctx->ctx_lnum]);
4868}
4869
4870/*
4871 * Compile a nested :def command.
4872 */
4873 static char_u *
4874compile_nested_function(exarg_T *eap, cctx_T *cctx)
4875{
4876 char_u *name_start = eap->arg;
4877 char_u *name_end = to_name_end(eap->arg, FALSE);
4878 char_u *name = get_lambda_name();
4879 lvar_T *lvar;
4880 ufunc_T *ufunc;
4881
4882 eap->arg = name_end;
4883 eap->getline = exarg_getline;
4884 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004885 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004886 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004887 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004888
Bram Moolenaar822ba242020-05-24 23:00:18 +02004889 if (ufunc == NULL)
4890 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004891 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004892 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004893 return NULL;
4894
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004895 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004896 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004897 TRUE, ufunc->uf_func_type);
4898
4899 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4900 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4901 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004902
Bram Moolenaar61a89812020-05-07 16:58:17 +02004903 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004904 return (char_u *)"";
4905}
4906
4907/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 * Return the length of an assignment operator, or zero if there isn't one.
4909 */
4910 int
4911assignment_len(char_u *p, int *heredoc)
4912{
4913 if (*p == '=')
4914 {
4915 if (p[1] == '<' && p[2] == '<')
4916 {
4917 *heredoc = TRUE;
4918 return 3;
4919 }
4920 return 1;
4921 }
4922 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4923 return 2;
4924 if (STRNCMP(p, "..=", 3) == 0)
4925 return 3;
4926 return 0;
4927}
4928
4929// words that cannot be used as a variable
4930static char *reserved[] = {
4931 "true",
4932 "false",
4933 NULL
4934};
4935
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004936typedef enum {
4937 dest_local,
4938 dest_option,
4939 dest_env,
4940 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004941 dest_buffer,
4942 dest_window,
4943 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004944 dest_vimvar,
4945 dest_script,
4946 dest_reg,
4947} assign_dest_T;
4948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004950 * Generate the load instruction for "name".
4951 */
4952 static void
4953generate_loadvar(
4954 cctx_T *cctx,
4955 assign_dest_T dest,
4956 char_u *name,
4957 lvar_T *lvar,
4958 type_T *type)
4959{
4960 switch (dest)
4961 {
4962 case dest_option:
4963 // TODO: check the option exists
4964 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4965 break;
4966 case dest_global:
4967 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4968 break;
4969 case dest_buffer:
4970 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4971 break;
4972 case dest_window:
4973 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4974 break;
4975 case dest_tab:
4976 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4977 break;
4978 case dest_script:
4979 compile_load_scriptvar(cctx,
4980 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4981 break;
4982 case dest_env:
4983 // Include $ in the name here
4984 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4985 break;
4986 case dest_reg:
4987 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4988 break;
4989 case dest_vimvar:
4990 generate_LOADV(cctx, name + 2, TRUE);
4991 break;
4992 case dest_local:
4993 if (lvar->lv_from_outer)
4994 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4995 NULL, type);
4996 else
4997 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4998 break;
4999 }
5000}
5001
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005002 void
5003vim9_declare_error(char_u *name)
5004{
5005 char *scope = "";
5006
5007 switch (*name)
5008 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005009 case 'g': scope = _("global"); break;
5010 case 'b': scope = _("buffer"); break;
5011 case 'w': scope = _("window"); break;
5012 case 't': scope = _("tab"); break;
5013 case 'v': scope = "v:"; break;
5014 case '$': semsg(_(e_declare_env_var), name); return;
5015 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005016 }
5017 semsg(_(e_declare_var), scope, name);
5018}
5019
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005020/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005021 * Compile declaration and assignment:
5022 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005024 * Return NULL for an error.
5025 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026 */
5027 static char_u *
5028compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5029{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005030 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005032 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 char_u *ret = NULL;
5034 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005035 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005037 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005038 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040 int oplen = 0;
5041 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005042 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005043 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005044 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005048 // Skip over the "var" or "[var, var]" to get to any "=".
5049 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5050 if (p == NULL)
5051 return *arg == '[' ? arg : NULL;
5052
5053 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005055 // TODO: should we allow this, and figure out type inference from list
5056 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005057 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 return NULL;
5059 }
5060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061 sp = p;
5062 p = skipwhite(p);
5063 op = p;
5064 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005065
5066 if (var_count > 0 && oplen == 0)
5067 // can be something like "[1, 2]->func()"
5068 return arg;
5069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5071 {
5072 char_u buf[4];
5073
5074 vim_strncpy(buf, op, oplen);
5075 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005076 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005077 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 if (heredoc)
5080 {
5081 list_T *l;
5082 listitem_T *li;
5083
5084 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005085 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005087 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088
5089 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005090 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 {
5092 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5093 li->li_tv.vval.v_string = NULL;
5094 }
5095 generate_NEWLIST(cctx, l->lv_len);
5096 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005097 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098 list_free(l);
5099 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005100 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005102 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005104 // for "[var, var] = expr" evaluate the expression here, loop over the
5105 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005106
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005107 p = skipwhite(op + oplen);
5108 if (compile_expr0(&p, cctx) == FAIL)
5109 return NULL;
5110 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005112 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005114 type_T *stacktype;
5115
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005116 stacktype = stack->ga_len == 0 ? &t_void
5117 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005118 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005120 emsg(_(e_cannot_use_void));
5121 goto theend;
5122 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005123 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005124 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005125 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005126 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5127 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005128 }
5129 }
5130
5131 /*
5132 * Loop over variables in "[var, var] = expr".
5133 * For "var = expr" and "let var: type" this is done only once.
5134 */
5135 if (var_count > 0)
5136 var_start = skipwhite(arg + 1); // skip over the "["
5137 else
5138 var_start = arg;
5139 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5140 {
5141 char_u *var_end = skip_var_one(var_start, FALSE);
5142 size_t varlen;
5143 int new_local = FALSE;
5144 int opt_type;
5145 int opt_flags = 0;
5146 assign_dest_T dest = dest_local;
5147 int vimvaridx = -1;
5148 lvar_T *lvar = NULL;
5149 lvar_T arg_lvar;
5150 int has_type = FALSE;
5151 int has_index = FALSE;
5152 int instr_count = -1;
5153
5154 p = (*var_start == '&' || *var_start == '$'
5155 || *var_start == '@') ? var_start + 1 : var_start;
5156 p = to_name_end(p, TRUE);
5157
5158 // "a: type" is declaring variable "a" with a type, not "a:".
5159 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5160 --var_end;
5161 if (is_decl && p == var_start + 2 && p[-1] == ':')
5162 --p;
5163
5164 varlen = p - var_start;
5165 vim_free(name);
5166 name = vim_strnsave(var_start, varlen);
5167 if (name == NULL)
5168 return NULL;
5169 if (!heredoc)
5170 type = &t_any;
5171
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005172 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005173 {
5174 if (*var_start == '&')
5175 {
5176 int cc;
5177 long numval;
5178
5179 dest = dest_option;
5180 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005182 emsg(_(e_const_option));
5183 goto theend;
5184 }
5185 if (is_decl)
5186 {
5187 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5188 goto theend;
5189 }
5190 p = var_start;
5191 p = find_option_end(&p, &opt_flags);
5192 if (p == NULL)
5193 {
5194 // cannot happen?
5195 emsg(_(e_letunexp));
5196 goto theend;
5197 }
5198 cc = *p;
5199 *p = NUL;
5200 opt_type = get_option_value(var_start + 1, &numval,
5201 NULL, opt_flags);
5202 *p = cc;
5203 if (opt_type == -3)
5204 {
5205 semsg(_(e_unknown_option), var_start);
5206 goto theend;
5207 }
5208 if (opt_type == -2 || opt_type == 0)
5209 type = &t_string;
5210 else
5211 type = &t_number; // both number and boolean option
5212 }
5213 else if (*var_start == '$')
5214 {
5215 dest = dest_env;
5216 type = &t_string;
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 if (*var_start == '@')
5224 {
5225 if (!valid_yank_reg(var_start[1], TRUE))
5226 {
5227 emsg_invreg(var_start[1]);
5228 goto theend;
5229 }
5230 dest = dest_reg;
5231 type = &t_string;
5232 if (is_decl)
5233 {
5234 semsg(_("E1066: Cannot declare a register: %s"), name);
5235 goto theend;
5236 }
5237 }
5238 else if (STRNCMP(var_start, "g:", 2) == 0)
5239 {
5240 dest = dest_global;
5241 if (is_decl)
5242 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005243 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005244 goto theend;
5245 }
5246 }
5247 else if (STRNCMP(var_start, "b:", 2) == 0)
5248 {
5249 dest = dest_buffer;
5250 if (is_decl)
5251 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005252 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005253 goto theend;
5254 }
5255 }
5256 else if (STRNCMP(var_start, "w:", 2) == 0)
5257 {
5258 dest = dest_window;
5259 if (is_decl)
5260 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005261 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 goto theend;
5263 }
5264 }
5265 else if (STRNCMP(var_start, "t:", 2) == 0)
5266 {
5267 dest = dest_tab;
5268 if (is_decl)
5269 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005270 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005271 goto theend;
5272 }
5273 }
5274 else if (STRNCMP(var_start, "v:", 2) == 0)
5275 {
5276 typval_T *vtv;
5277 int di_flags;
5278
5279 vimvaridx = find_vim_var(name + 2, &di_flags);
5280 if (vimvaridx < 0)
5281 {
5282 semsg(_(e_var_notfound), var_start);
5283 goto theend;
5284 }
5285 // We use the current value of "sandbox" here, is that OK?
5286 if (var_check_ro(di_flags, name, FALSE))
5287 goto theend;
5288 dest = dest_vimvar;
5289 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005290 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005291 if (is_decl)
5292 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005293 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005294 goto theend;
5295 }
5296 }
5297 else
5298 {
5299 int idx;
5300
5301 for (idx = 0; reserved[idx] != NULL; ++idx)
5302 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005303 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005304 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005305 goto theend;
5306 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005307
5308 lvar = lookup_local(var_start, varlen, cctx);
5309 if (lvar == NULL)
5310 {
5311 CLEAR_FIELD(arg_lvar);
5312 if (lookup_arg(var_start, varlen,
5313 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5314 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005315 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005316 if (is_decl)
5317 {
5318 semsg(_(e_used_as_arg), name);
5319 goto theend;
5320 }
5321 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005322 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005323 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005324 if (lvar != NULL)
5325 {
5326 if (is_decl)
5327 {
5328 semsg(_("E1017: Variable already declared: %s"), name);
5329 goto theend;
5330 }
5331 else if (lvar->lv_const)
5332 {
5333 semsg(_("E1018: Cannot assign to a constant: %s"),
5334 name);
5335 goto theend;
5336 }
5337 }
5338 else if (STRNCMP(var_start, "s:", 2) == 0
5339 || lookup_script(var_start, varlen) == OK
5340 || find_imported(var_start, varlen, cctx) != NULL)
5341 {
5342 dest = dest_script;
5343 if (is_decl)
5344 {
5345 semsg(_("E1054: Variable already declared in the script: %s"),
5346 name);
5347 goto theend;
5348 }
5349 }
5350 else if (name[1] == ':' && name[2] != NUL)
5351 {
5352 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5353 name);
5354 goto theend;
5355 }
5356 else if (!is_decl)
5357 {
5358 semsg(_("E1089: unknown variable: %s"), name);
5359 goto theend;
5360 }
5361 }
5362 }
5363
5364 // handle "a:name" as a name, not index "name" on "a"
5365 if (varlen > 1 || var_start[varlen] != ':')
5366 p = var_end;
5367
5368 if (dest != dest_option)
5369 {
5370 if (is_decl && *p == ':')
5371 {
5372 // parse optional type: "let var: type = expr"
5373 if (!VIM_ISWHITE(p[1]))
5374 {
5375 semsg(_(e_white_after), ":");
5376 goto theend;
5377 }
5378 p = skipwhite(p + 1);
5379 type = parse_type(&p, cctx->ctx_type_list);
5380 has_type = TRUE;
5381 }
5382 else if (lvar != NULL)
5383 type = lvar->lv_type;
5384 }
5385
5386 if (oplen == 3 && !heredoc && dest != dest_global
5387 && type->tt_type != VAR_STRING
5388 && type->tt_type != VAR_ANY)
5389 {
5390 emsg(_("E1019: Can only concatenate to string"));
5391 goto theend;
5392 }
5393
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005394 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005395 {
5396 if (oplen > 1 && !heredoc)
5397 {
5398 // +=, /=, etc. require an existing variable
5399 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5400 name);
5401 goto theend;
5402 }
5403
5404 // new local variable
5405 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5406 goto theend;
5407 lvar = reserve_local(cctx, var_start, varlen,
5408 cmdidx == CMD_const, type);
5409 if (lvar == NULL)
5410 goto theend;
5411 new_local = TRUE;
5412 }
5413
5414 member_type = type;
5415 if (var_end > var_start + varlen)
5416 {
5417 // Something follows after the variable: "var[idx]".
5418 if (is_decl)
5419 {
5420 emsg(_("E1087: cannot use an index when declaring a variable"));
5421 goto theend;
5422 }
5423
5424 if (var_start[varlen] == '[')
5425 {
5426 has_index = TRUE;
5427 if (type->tt_member == NULL)
5428 {
5429 semsg(_("E1088: cannot use an index on %s"), name);
5430 goto theend;
5431 }
5432 member_type = type->tt_member;
5433 }
5434 else
5435 {
5436 semsg("Not supported yet: %s", var_start);
5437 goto theend;
5438 }
5439 }
5440 else if (lvar == &arg_lvar)
5441 {
5442 semsg(_("E1090: Cannot assign to argument %s"), name);
5443 goto theend;
5444 }
5445
5446 if (!heredoc)
5447 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005448 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005449 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005450 if (oplen > 0 && var_count == 0)
5451 {
5452 // skip over the "=" and the expression
5453 p = skipwhite(op + oplen);
5454 compile_expr0(&p, cctx);
5455 }
5456 }
5457 else if (oplen > 0)
5458 {
5459 type_T *stacktype;
5460
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005461 // For "var = expr" evaluate the expression.
5462 if (var_count == 0)
5463 {
5464 int r;
5465
5466 // for "+=", "*=", "..=" etc. first load the current value
5467 if (*op != '=')
5468 {
5469 generate_loadvar(cctx, dest, name, lvar, type);
5470
5471 if (has_index)
5472 {
5473 // TODO: get member from list or dict
5474 emsg("Index with operation not supported yet");
5475 goto theend;
5476 }
5477 }
5478
5479 // Compile the expression. Temporarily hide the new local
5480 // variable here, it is not available to this expression.
5481 if (new_local)
5482 --cctx->ctx_locals.ga_len;
5483 instr_count = instr->ga_len;
5484 p = skipwhite(op + oplen);
5485 r = compile_expr0(&p, cctx);
5486 if (new_local)
5487 ++cctx->ctx_locals.ga_len;
5488 if (r == FAIL)
5489 goto theend;
5490 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005491 else if (semicolon && var_idx == var_count - 1)
5492 {
5493 // For "[var; var] = expr" get the rest of the list
5494 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5495 goto theend;
5496 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005497 else
5498 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005499 // For "[var, var] = expr" get the "var_idx" item from the
5500 // list.
5501 if (generate_GETITEM(cctx, var_idx) == FAIL)
5502 return FAIL;
5503 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005504
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005505 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005506 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005507 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005508 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005509 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005510 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005511 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005512 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005513 emsg(_(e_cannot_use_void));
5514 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005515 }
5516 else
5517 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005518 // An empty list or dict has a &t_void member,
5519 // for a variable that implies &t_any.
5520 if (stacktype == &t_list_empty)
5521 lvar->lv_type = &t_list_any;
5522 else if (stacktype == &t_dict_empty)
5523 lvar->lv_type = &t_dict_any;
5524 else
5525 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005526 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005527 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005528 else
5529 {
5530 type_T *use_type = lvar->lv_type;
5531
5532 if (has_index)
5533 {
5534 use_type = use_type->tt_member;
5535 if (use_type == NULL)
5536 use_type = &t_void;
5537 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005538 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005539 == FAIL)
5540 goto theend;
5541 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005542 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005543 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005544 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005545 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005547 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005549 emsg(_(e_const_req_value));
5550 goto theend;
5551 }
5552 else if (!has_type || dest == dest_option)
5553 {
5554 emsg(_(e_type_req));
5555 goto theend;
5556 }
5557 else
5558 {
5559 // variables are always initialized
5560 if (ga_grow(instr, 1) == FAIL)
5561 goto theend;
5562 switch (member_type->tt_type)
5563 {
5564 case VAR_BOOL:
5565 generate_PUSHBOOL(cctx, VVAL_FALSE);
5566 break;
5567 case VAR_FLOAT:
5568#ifdef FEAT_FLOAT
5569 generate_PUSHF(cctx, 0.0);
5570#endif
5571 break;
5572 case VAR_STRING:
5573 generate_PUSHS(cctx, NULL);
5574 break;
5575 case VAR_BLOB:
5576 generate_PUSHBLOB(cctx, NULL);
5577 break;
5578 case VAR_FUNC:
5579 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5580 break;
5581 case VAR_LIST:
5582 generate_NEWLIST(cctx, 0);
5583 break;
5584 case VAR_DICT:
5585 generate_NEWDICT(cctx, 0);
5586 break;
5587 case VAR_JOB:
5588 generate_PUSHJOB(cctx, NULL);
5589 break;
5590 case VAR_CHANNEL:
5591 generate_PUSHCHANNEL(cctx, NULL);
5592 break;
5593 case VAR_NUMBER:
5594 case VAR_UNKNOWN:
5595 case VAR_ANY:
5596 case VAR_PARTIAL:
5597 case VAR_VOID:
5598 case VAR_SPECIAL: // cannot happen
5599 generate_PUSHNR(cctx, 0);
5600 break;
5601 }
5602 }
5603 if (var_count == 0)
5604 end = p;
5605 }
5606
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005607 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005608 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005609 break;
5610
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005611 if (oplen > 0 && *op != '=')
5612 {
5613 type_T *expected = &t_number;
5614 type_T *stacktype;
5615
5616 // TODO: if type is known use float or any operation
5617
5618 if (*op == '.')
5619 expected = &t_string;
5620 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005621 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005622 goto theend;
5623
5624 if (*op == '.')
5625 generate_instr_drop(cctx, ISN_CONCAT, 1);
5626 else
5627 {
5628 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5629
5630 if (isn == NULL)
5631 goto theend;
5632 switch (*op)
5633 {
5634 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5635 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5636 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5637 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5638 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640 }
5641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005643 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005644 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005645 int r;
5646
5647 // Compile the "idx" in "var[idx]".
5648 if (new_local)
5649 --cctx->ctx_locals.ga_len;
5650 p = skipwhite(var_start + varlen + 1);
5651 r = compile_expr0(&p, cctx);
5652 if (new_local)
5653 ++cctx->ctx_locals.ga_len;
5654 if (r == FAIL)
5655 goto theend;
5656 if (*skipwhite(p) != ']')
5657 {
5658 emsg(_(e_missbrac));
5659 goto theend;
5660 }
5661 if (type->tt_type == VAR_DICT
5662 && may_generate_2STRING(-1, cctx) == FAIL)
5663 goto theend;
5664 if (type->tt_type == VAR_LIST
5665 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005666 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005667 {
5668 emsg(_(e_number_exp));
5669 goto theend;
5670 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005671
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005672 // Load the dict or list. On the stack we then have:
5673 // - value
5674 // - index
5675 // - variable
5676 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005677
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005678 if (type->tt_type == VAR_LIST)
5679 {
5680 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5681 return FAIL;
5682 }
5683 else if (type->tt_type == VAR_DICT)
5684 {
5685 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5686 return FAIL;
5687 }
5688 else
5689 {
5690 emsg(_(e_listreq));
5691 goto theend;
5692 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005693 }
5694 else
5695 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005696 switch (dest)
5697 {
5698 case dest_option:
5699 generate_STOREOPT(cctx, name + 1, opt_flags);
5700 break;
5701 case dest_global:
5702 // include g: with the name, easier to execute that way
5703 generate_STORE(cctx, ISN_STOREG, 0, name);
5704 break;
5705 case dest_buffer:
5706 // include b: with the name, easier to execute that way
5707 generate_STORE(cctx, ISN_STOREB, 0, name);
5708 break;
5709 case dest_window:
5710 // include w: with the name, easier to execute that way
5711 generate_STORE(cctx, ISN_STOREW, 0, name);
5712 break;
5713 case dest_tab:
5714 // include t: with the name, easier to execute that way
5715 generate_STORE(cctx, ISN_STORET, 0, name);
5716 break;
5717 case dest_env:
5718 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5719 break;
5720 case dest_reg:
5721 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5722 break;
5723 case dest_vimvar:
5724 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5725 break;
5726 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005727 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005728 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5729 imported_T *import = NULL;
5730 int sid = current_sctx.sc_sid;
5731 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005732
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005733 if (name[1] != ':')
5734 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005735 import = find_imported(name, 0, cctx);
5736 if (import != NULL)
5737 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005738 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005739
5740 idx = get_script_item_idx(sid, rawname, TRUE);
5741 // TODO: specific type
5742 if (idx < 0)
5743 {
5744 char_u *name_s = name;
5745
5746 // Include s: in the name for store_var()
5747 if (name[1] != ':')
5748 {
5749 int len = (int)STRLEN(name) + 3;
5750
5751 name_s = alloc(len);
5752 if (name_s == NULL)
5753 name_s = name;
5754 else
5755 vim_snprintf((char *)name_s, len,
5756 "s:%s", name);
5757 }
5758 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005759 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005760 if (name_s != name)
5761 vim_free(name_s);
5762 }
5763 else
5764 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005765 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005766 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005767 break;
5768 case dest_local:
5769 if (lvar != NULL)
5770 {
5771 isn_T *isn = ((isn_T *)instr->ga_data)
5772 + instr->ga_len - 1;
5773
5774 // optimization: turn "var = 123" from ISN_PUSHNR +
5775 // ISN_STORE into ISN_STORENR
5776 if (!lvar->lv_from_outer
5777 && instr->ga_len == instr_count + 1
5778 && isn->isn_type == ISN_PUSHNR)
5779 {
5780 varnumber_T val = isn->isn_arg.number;
5781
5782 isn->isn_type = ISN_STORENR;
5783 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5784 isn->isn_arg.storenr.stnr_val = val;
5785 if (stack->ga_len > 0)
5786 --stack->ga_len;
5787 }
5788 else if (lvar->lv_from_outer)
5789 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005790 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005791 else
5792 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5793 }
5794 break;
5795 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005796 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005797
5798 if (var_idx + 1 < var_count)
5799 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005800 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005801
5802 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005803 if (var_count > 0 && !semicolon)
5804 {
5805 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5806 goto theend;
5807 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005808
Bram Moolenaarb2097502020-07-19 17:17:02 +02005809 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005810
5811theend:
5812 vim_free(name);
5813 return ret;
5814}
5815
5816/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005817 * Check if "name" can be "unlet".
5818 */
5819 int
5820check_vim9_unlet(char_u *name)
5821{
5822 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5823 {
5824 semsg(_("E1081: Cannot unlet %s"), name);
5825 return FAIL;
5826 }
5827 return OK;
5828}
5829
5830/*
5831 * Callback passed to ex_unletlock().
5832 */
5833 static int
5834compile_unlet(
5835 lval_T *lvp,
5836 char_u *name_end,
5837 exarg_T *eap,
5838 int deep UNUSED,
5839 void *coookie)
5840{
5841 cctx_T *cctx = coookie;
5842
5843 if (lvp->ll_tv == NULL)
5844 {
5845 char_u *p = lvp->ll_name;
5846 int cc = *name_end;
5847 int ret = OK;
5848
5849 // Normal name. Only supports g:, w:, t: and b: namespaces.
5850 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005851 if (*p == '$')
5852 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5853 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005854 ret = FAIL;
5855 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005856 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005857
5858 *name_end = cc;
5859 return ret;
5860 }
5861
5862 // TODO: unlet {list}[idx]
5863 // TODO: unlet {dict}[key]
5864 emsg("Sorry, :unlet not fully implemented yet");
5865 return FAIL;
5866}
5867
5868/*
5869 * compile "unlet var", "lock var" and "unlock var"
5870 * "arg" points to "var".
5871 */
5872 static char_u *
5873compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5874{
5875 char_u *p = arg;
5876
5877 if (eap->cmdidx != CMD_unlet)
5878 {
5879 emsg("Sorry, :lock and unlock not implemented yet");
5880 return NULL;
5881 }
5882
5883 if (*p == '!')
5884 {
5885 p = skipwhite(p + 1);
5886 eap->forceit = TRUE;
5887 }
5888
5889 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5890 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5891}
5892
5893/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005894 * Compile an :import command.
5895 */
5896 static char_u *
5897compile_import(char_u *arg, cctx_T *cctx)
5898{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005899 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005900}
5901
5902/*
5903 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5904 */
5905 static int
5906compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5907{
5908 garray_T *instr = &cctx->ctx_instr;
5909 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5910
5911 if (endlabel == NULL)
5912 return FAIL;
5913 endlabel->el_next = *el;
5914 *el = endlabel;
5915 endlabel->el_end_label = instr->ga_len;
5916
5917 generate_JUMP(cctx, when, 0);
5918 return OK;
5919}
5920
5921 static void
5922compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5923{
5924 garray_T *instr = &cctx->ctx_instr;
5925
5926 while (*el != NULL)
5927 {
5928 endlabel_T *cur = (*el);
5929 isn_T *isn;
5930
5931 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5932 isn->isn_arg.jump.jump_where = instr->ga_len;
5933 *el = cur->el_next;
5934 vim_free(cur);
5935 }
5936}
5937
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005938 static void
5939compile_free_jump_to_end(endlabel_T **el)
5940{
5941 while (*el != NULL)
5942 {
5943 endlabel_T *cur = (*el);
5944
5945 *el = cur->el_next;
5946 vim_free(cur);
5947 }
5948}
5949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950/*
5951 * Create a new scope and set up the generic items.
5952 */
5953 static scope_T *
5954new_scope(cctx_T *cctx, scopetype_T type)
5955{
5956 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5957
5958 if (scope == NULL)
5959 return NULL;
5960 scope->se_outer = cctx->ctx_scope;
5961 cctx->ctx_scope = scope;
5962 scope->se_type = type;
5963 scope->se_local_count = cctx->ctx_locals.ga_len;
5964 return scope;
5965}
5966
5967/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005968 * Free the current scope and go back to the outer scope.
5969 */
5970 static void
5971drop_scope(cctx_T *cctx)
5972{
5973 scope_T *scope = cctx->ctx_scope;
5974
5975 if (scope == NULL)
5976 {
5977 iemsg("calling drop_scope() without a scope");
5978 return;
5979 }
5980 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005981 switch (scope->se_type)
5982 {
5983 case IF_SCOPE:
5984 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5985 case FOR_SCOPE:
5986 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5987 case WHILE_SCOPE:
5988 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5989 case TRY_SCOPE:
5990 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5991 case NO_SCOPE:
5992 case BLOCK_SCOPE:
5993 break;
5994 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005995 vim_free(scope);
5996}
5997
5998/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999 * compile "if expr"
6000 *
6001 * "if expr" Produces instructions:
6002 * EVAL expr Push result of "expr"
6003 * JUMP_IF_FALSE end
6004 * ... body ...
6005 * end:
6006 *
6007 * "if expr | else" Produces instructions:
6008 * EVAL expr Push result of "expr"
6009 * JUMP_IF_FALSE else
6010 * ... body ...
6011 * JUMP_ALWAYS end
6012 * else:
6013 * ... body ...
6014 * end:
6015 *
6016 * "if expr1 | elseif expr2 | else" Produces instructions:
6017 * EVAL expr Push result of "expr"
6018 * JUMP_IF_FALSE elseif
6019 * ... body ...
6020 * JUMP_ALWAYS end
6021 * elseif:
6022 * EVAL expr Push result of "expr"
6023 * JUMP_IF_FALSE else
6024 * ... body ...
6025 * JUMP_ALWAYS end
6026 * else:
6027 * ... body ...
6028 * end:
6029 */
6030 static char_u *
6031compile_if(char_u *arg, cctx_T *cctx)
6032{
6033 char_u *p = arg;
6034 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006035 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006037 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006038 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006039
Bram Moolenaara5565e42020-05-09 15:44:01 +02006040 CLEAR_FIELD(ppconst);
6041 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006042 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006043 clear_ppconst(&ppconst);
6044 return NULL;
6045 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006046 if (cctx->ctx_skip == SKIP_YES)
6047 clear_ppconst(&ppconst);
6048 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006049 {
6050 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006051 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006052 clear_ppconst(&ppconst);
6053 }
6054 else
6055 {
6056 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006057 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006058 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006059 return NULL;
6060 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061
6062 scope = new_scope(cctx, IF_SCOPE);
6063 if (scope == NULL)
6064 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006065 scope->se_skip_save = skip_save;
6066 // "is_had_return" will be reset if any block does not end in :return
6067 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006068
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006069 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006070 {
6071 // "where" is set when ":elseif", "else" or ":endif" is found
6072 scope->se_u.se_if.is_if_label = instr->ga_len;
6073 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6074 }
6075 else
6076 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006077
6078 return p;
6079}
6080
6081 static char_u *
6082compile_elseif(char_u *arg, cctx_T *cctx)
6083{
6084 char_u *p = arg;
6085 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006086 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006087 isn_T *isn;
6088 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006089 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006090
6091 if (scope == NULL || scope->se_type != IF_SCOPE)
6092 {
6093 emsg(_(e_elseif_without_if));
6094 return NULL;
6095 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006096 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006097 if (!cctx->ctx_had_return)
6098 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006099
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006100 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006101 {
6102 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006103 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006104 return NULL;
6105 // previous "if" or "elseif" jumps here
6106 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6107 isn->isn_arg.jump.jump_where = instr->ga_len;
6108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006110 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006111 CLEAR_FIELD(ppconst);
6112 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006113 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006114 clear_ppconst(&ppconst);
6115 return NULL;
6116 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006117 if (scope->se_skip_save == SKIP_YES)
6118 clear_ppconst(&ppconst);
6119 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006120 {
6121 // The expression results in a constant.
6122 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006123 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006124 clear_ppconst(&ppconst);
6125 scope->se_u.se_if.is_if_label = -1;
6126 }
6127 else
6128 {
6129 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006130 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006131 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006132 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006133
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006134 // "where" is set when ":elseif", "else" or ":endif" is found
6135 scope->se_u.se_if.is_if_label = instr->ga_len;
6136 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006138
6139 return p;
6140}
6141
6142 static char_u *
6143compile_else(char_u *arg, cctx_T *cctx)
6144{
6145 char_u *p = arg;
6146 garray_T *instr = &cctx->ctx_instr;
6147 isn_T *isn;
6148 scope_T *scope = cctx->ctx_scope;
6149
6150 if (scope == NULL || scope->se_type != IF_SCOPE)
6151 {
6152 emsg(_(e_else_without_if));
6153 return NULL;
6154 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006155 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006156 if (!cctx->ctx_had_return)
6157 scope->se_u.se_if.is_had_return = FALSE;
6158 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006159
Bram Moolenaarefd88552020-06-18 20:50:10 +02006160 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006161 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006162 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006163 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006164 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006165 if (!cctx->ctx_had_return
6166 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6167 JUMP_ALWAYS, cctx) == FAIL)
6168 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006169 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006170
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006171 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006172 {
6173 if (scope->se_u.se_if.is_if_label >= 0)
6174 {
6175 // previous "if" or "elseif" jumps here
6176 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6177 isn->isn_arg.jump.jump_where = instr->ga_len;
6178 scope->se_u.se_if.is_if_label = -1;
6179 }
6180 }
6181
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006182 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006183 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6184 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185
6186 return p;
6187}
6188
6189 static char_u *
6190compile_endif(char_u *arg, cctx_T *cctx)
6191{
6192 scope_T *scope = cctx->ctx_scope;
6193 ifscope_T *ifscope;
6194 garray_T *instr = &cctx->ctx_instr;
6195 isn_T *isn;
6196
6197 if (scope == NULL || scope->se_type != IF_SCOPE)
6198 {
6199 emsg(_(e_endif_without_if));
6200 return NULL;
6201 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006202 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006203 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006204 if (!cctx->ctx_had_return)
6205 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006206
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006207 if (scope->se_u.se_if.is_if_label >= 0)
6208 {
6209 // previous "if" or "elseif" jumps here
6210 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6211 isn->isn_arg.jump.jump_where = instr->ga_len;
6212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006213 // Fill in the "end" label in jumps at the end of the blocks.
6214 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006215 cctx->ctx_skip = scope->se_skip_save;
6216
6217 // If all the blocks end in :return and there is an :else then the
6218 // had_return flag is set.
6219 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006220
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006221 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 return arg;
6223}
6224
6225/*
6226 * compile "for var in expr"
6227 *
6228 * Produces instructions:
6229 * PUSHNR -1
6230 * STORE loop-idx Set index to -1
6231 * EVAL expr Push result of "expr"
6232 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6233 * - if beyond end, jump to "end"
6234 * - otherwise get item from list and push it
6235 * STORE var Store item in "var"
6236 * ... body ...
6237 * JUMP top Jump back to repeat
6238 * end: DROP Drop the result of "expr"
6239 *
6240 */
6241 static char_u *
6242compile_for(char_u *arg, cctx_T *cctx)
6243{
6244 char_u *p;
6245 size_t varlen;
6246 garray_T *instr = &cctx->ctx_instr;
6247 garray_T *stack = &cctx->ctx_type_stack;
6248 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006249 lvar_T *loop_lvar; // loop iteration variable
6250 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 type_T *vartype;
6252
6253 // TODO: list of variables: "for [key, value] in dict"
6254 // parse "var"
6255 for (p = arg; eval_isnamec1(*p); ++p)
6256 ;
6257 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006258 var_lvar = lookup_local(arg, varlen, cctx);
6259 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260 {
6261 semsg(_("E1023: variable already defined: %s"), arg);
6262 return NULL;
6263 }
6264
6265 // consume "in"
6266 p = skipwhite(p);
6267 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6268 {
6269 emsg(_(e_missing_in));
6270 return NULL;
6271 }
6272 p = skipwhite(p + 2);
6273
6274
6275 scope = new_scope(cctx, FOR_SCOPE);
6276 if (scope == NULL)
6277 return NULL;
6278
6279 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006280 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6281 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006282 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006283 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006284 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006285 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006286 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287
6288 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006289 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6290 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006291 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006292 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006293 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006297 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298
6299 // compile "expr", it remains on the stack until "endfor"
6300 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006301 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006302 {
6303 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006305 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006307 // Now that we know the type of "var", check that it is a list, now or at
6308 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006310 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006312 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 return NULL;
6314 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006315 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006316 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317
6318 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006319 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006321 generate_FOR(cctx, loop_lvar->lv_idx);
6322 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323
6324 return arg;
6325}
6326
6327/*
6328 * compile "endfor"
6329 */
6330 static char_u *
6331compile_endfor(char_u *arg, cctx_T *cctx)
6332{
6333 garray_T *instr = &cctx->ctx_instr;
6334 scope_T *scope = cctx->ctx_scope;
6335 forscope_T *forscope;
6336 isn_T *isn;
6337
6338 if (scope == NULL || scope->se_type != FOR_SCOPE)
6339 {
6340 emsg(_(e_for));
6341 return NULL;
6342 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006343 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006345 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346
6347 // At end of ":for" scope jump back to the FOR instruction.
6348 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6349
6350 // Fill in the "end" label in the FOR statement so it can jump here
6351 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6352 isn->isn_arg.forloop.for_end = instr->ga_len;
6353
6354 // Fill in the "end" label any BREAK statements
6355 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6356
6357 // Below the ":for" scope drop the "expr" list from the stack.
6358 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6359 return NULL;
6360
6361 vim_free(scope);
6362
6363 return arg;
6364}
6365
6366/*
6367 * compile "while expr"
6368 *
6369 * Produces instructions:
6370 * top: EVAL expr Push result of "expr"
6371 * JUMP_IF_FALSE end jump if false
6372 * ... body ...
6373 * JUMP top Jump back to repeat
6374 * end:
6375 *
6376 */
6377 static char_u *
6378compile_while(char_u *arg, cctx_T *cctx)
6379{
6380 char_u *p = arg;
6381 garray_T *instr = &cctx->ctx_instr;
6382 scope_T *scope;
6383
6384 scope = new_scope(cctx, WHILE_SCOPE);
6385 if (scope == NULL)
6386 return NULL;
6387
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006388 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006389
6390 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006391 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392 return NULL;
6393
6394 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006395 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006396 JUMP_IF_FALSE, cctx) == FAIL)
6397 return FAIL;
6398
6399 return p;
6400}
6401
6402/*
6403 * compile "endwhile"
6404 */
6405 static char_u *
6406compile_endwhile(char_u *arg, cctx_T *cctx)
6407{
6408 scope_T *scope = cctx->ctx_scope;
6409
6410 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6411 {
6412 emsg(_(e_while));
6413 return NULL;
6414 }
6415 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006416 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006417
6418 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006419 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006420
6421 // Fill in the "end" label in the WHILE statement so it can jump here.
6422 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006423 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006424
6425 vim_free(scope);
6426
6427 return arg;
6428}
6429
6430/*
6431 * compile "continue"
6432 */
6433 static char_u *
6434compile_continue(char_u *arg, cctx_T *cctx)
6435{
6436 scope_T *scope = cctx->ctx_scope;
6437
6438 for (;;)
6439 {
6440 if (scope == NULL)
6441 {
6442 emsg(_(e_continue));
6443 return NULL;
6444 }
6445 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6446 break;
6447 scope = scope->se_outer;
6448 }
6449
6450 // Jump back to the FOR or WHILE instruction.
6451 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006452 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6453 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454 return arg;
6455}
6456
6457/*
6458 * compile "break"
6459 */
6460 static char_u *
6461compile_break(char_u *arg, cctx_T *cctx)
6462{
6463 scope_T *scope = cctx->ctx_scope;
6464 endlabel_T **el;
6465
6466 for (;;)
6467 {
6468 if (scope == NULL)
6469 {
6470 emsg(_(e_break));
6471 return NULL;
6472 }
6473 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6474 break;
6475 scope = scope->se_outer;
6476 }
6477
6478 // Jump to the end of the FOR or WHILE loop.
6479 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006480 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006481 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006482 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6484 return FAIL;
6485
6486 return arg;
6487}
6488
6489/*
6490 * compile "{" start of block
6491 */
6492 static char_u *
6493compile_block(char_u *arg, cctx_T *cctx)
6494{
6495 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6496 return NULL;
6497 return skipwhite(arg + 1);
6498}
6499
6500/*
6501 * compile end of block: drop one scope
6502 */
6503 static void
6504compile_endblock(cctx_T *cctx)
6505{
6506 scope_T *scope = cctx->ctx_scope;
6507
6508 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006509 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006510 vim_free(scope);
6511}
6512
6513/*
6514 * compile "try"
6515 * Creates a new scope for the try-endtry, pointing to the first catch and
6516 * finally.
6517 * Creates another scope for the "try" block itself.
6518 * TRY instruction sets up exception handling at runtime.
6519 *
6520 * "try"
6521 * TRY -> catch1, -> finally push trystack entry
6522 * ... try block
6523 * "throw {exception}"
6524 * EVAL {exception}
6525 * THROW create exception
6526 * ... try block
6527 * " catch {expr}"
6528 * JUMP -> finally
6529 * catch1: PUSH exeception
6530 * EVAL {expr}
6531 * MATCH
6532 * JUMP nomatch -> catch2
6533 * CATCH remove exception
6534 * ... catch block
6535 * " catch"
6536 * JUMP -> finally
6537 * catch2: CATCH remove exception
6538 * ... catch block
6539 * " finally"
6540 * finally:
6541 * ... finally block
6542 * " endtry"
6543 * ENDTRY pop trystack entry, may rethrow
6544 */
6545 static char_u *
6546compile_try(char_u *arg, cctx_T *cctx)
6547{
6548 garray_T *instr = &cctx->ctx_instr;
6549 scope_T *try_scope;
6550 scope_T *scope;
6551
6552 // scope that holds the jumps that go to catch/finally/endtry
6553 try_scope = new_scope(cctx, TRY_SCOPE);
6554 if (try_scope == NULL)
6555 return NULL;
6556
6557 // "catch" is set when the first ":catch" is found.
6558 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006559 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560 if (generate_instr(cctx, ISN_TRY) == NULL)
6561 return NULL;
6562
6563 // scope for the try block itself
6564 scope = new_scope(cctx, BLOCK_SCOPE);
6565 if (scope == NULL)
6566 return NULL;
6567
6568 return arg;
6569}
6570
6571/*
6572 * compile "catch {expr}"
6573 */
6574 static char_u *
6575compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6576{
6577 scope_T *scope = cctx->ctx_scope;
6578 garray_T *instr = &cctx->ctx_instr;
6579 char_u *p;
6580 isn_T *isn;
6581
6582 // end block scope from :try or :catch
6583 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6584 compile_endblock(cctx);
6585 scope = cctx->ctx_scope;
6586
6587 // Error if not in a :try scope
6588 if (scope == NULL || scope->se_type != TRY_SCOPE)
6589 {
6590 emsg(_(e_catch));
6591 return NULL;
6592 }
6593
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006594 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 {
6596 emsg(_("E1033: catch unreachable after catch-all"));
6597 return NULL;
6598 }
6599
6600 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006601 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 JUMP_ALWAYS, cctx) == FAIL)
6603 return NULL;
6604
6605 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006606 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 if (isn->isn_arg.try.try_catch == 0)
6608 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006609 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006610 {
6611 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006612 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 isn->isn_arg.jump.jump_where = instr->ga_len;
6614 }
6615
6616 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006617 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006618 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006619 scope->se_u.se_try.ts_caught_all = TRUE;
6620 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621 }
6622 else
6623 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006624 char_u *end;
6625 char_u *pat;
6626 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006627 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006628 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006630 // Push v:exception, push {expr} and MATCH
6631 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6632
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006633 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006634 if (*end != *p)
6635 {
6636 semsg(_("E1067: Separator mismatch: %s"), p);
6637 vim_free(tofree);
6638 return FAIL;
6639 }
6640 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006641 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006642 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006643 len = (int)(end - tofree);
6644 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006645 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006646 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006647 if (pat == NULL)
6648 return FAIL;
6649 if (generate_PUSHS(cctx, pat) == FAIL)
6650 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006652 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6653 return NULL;
6654
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006655 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6657 return NULL;
6658 }
6659
6660 if (generate_instr(cctx, ISN_CATCH) == NULL)
6661 return NULL;
6662
6663 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6664 return NULL;
6665 return p;
6666}
6667
6668 static char_u *
6669compile_finally(char_u *arg, cctx_T *cctx)
6670{
6671 scope_T *scope = cctx->ctx_scope;
6672 garray_T *instr = &cctx->ctx_instr;
6673 isn_T *isn;
6674
6675 // end block scope from :try or :catch
6676 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6677 compile_endblock(cctx);
6678 scope = cctx->ctx_scope;
6679
6680 // Error if not in a :try scope
6681 if (scope == NULL || scope->se_type != TRY_SCOPE)
6682 {
6683 emsg(_(e_finally));
6684 return NULL;
6685 }
6686
6687 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006688 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006689 if (isn->isn_arg.try.try_finally != 0)
6690 {
6691 emsg(_(e_finally_dup));
6692 return NULL;
6693 }
6694
6695 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006696 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697
Bram Moolenaar585fea72020-04-02 22:33:21 +02006698 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006699 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 {
6701 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006702 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006704 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006705 }
6706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006707 // TODO: set index in ts_finally_label jumps
6708
6709 return arg;
6710}
6711
6712 static char_u *
6713compile_endtry(char_u *arg, cctx_T *cctx)
6714{
6715 scope_T *scope = cctx->ctx_scope;
6716 garray_T *instr = &cctx->ctx_instr;
6717 isn_T *isn;
6718
6719 // end block scope from :catch or :finally
6720 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6721 compile_endblock(cctx);
6722 scope = cctx->ctx_scope;
6723
6724 // Error if not in a :try scope
6725 if (scope == NULL || scope->se_type != TRY_SCOPE)
6726 {
6727 if (scope == NULL)
6728 emsg(_(e_no_endtry));
6729 else if (scope->se_type == WHILE_SCOPE)
6730 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006731 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732 emsg(_(e_endfor));
6733 else
6734 emsg(_(e_endif));
6735 return NULL;
6736 }
6737
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006738 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6740 {
6741 emsg(_("E1032: missing :catch or :finally"));
6742 return NULL;
6743 }
6744
6745 // Fill in the "end" label in jumps at the end of the blocks, if not done
6746 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006747 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006748
6749 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006750 if (isn->isn_arg.try.try_catch == 0)
6751 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 if (isn->isn_arg.try.try_finally == 0)
6753 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006754
6755 if (scope->se_u.se_try.ts_catch_label != 0)
6756 {
6757 // Last catch without match jumps here
6758 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6759 isn->isn_arg.jump.jump_where = instr->ga_len;
6760 }
6761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 compile_endblock(cctx);
6763
6764 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6765 return NULL;
6766 return arg;
6767}
6768
6769/*
6770 * compile "throw {expr}"
6771 */
6772 static char_u *
6773compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6774{
6775 char_u *p = skipwhite(arg);
6776
Bram Moolenaara5565e42020-05-09 15:44:01 +02006777 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778 return NULL;
6779 if (may_generate_2STRING(-1, cctx) == FAIL)
6780 return NULL;
6781 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6782 return NULL;
6783
6784 return p;
6785}
6786
6787/*
6788 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006789 * compile "echomsg expr"
6790 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006791 * compile "execute expr"
6792 */
6793 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006794compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006795{
6796 char_u *p = arg;
6797 int count = 0;
6798
6799 for (;;)
6800 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006801 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006802 return NULL;
6803 ++count;
6804 p = skipwhite(p);
6805 if (ends_excmd(*p))
6806 break;
6807 }
6808
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006809 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6810 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6811 else if (cmdidx == CMD_execute)
6812 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6813 else if (cmdidx == CMD_echomsg)
6814 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6815 else
6816 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006817 return p;
6818}
6819
6820/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006821 * A command that is not compiled, execute with legacy code.
6822 */
6823 static char_u *
6824compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6825{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006826 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006827 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006828 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006829
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006830 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006831 goto theend;
6832
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006833 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006834 {
6835 long argt = excmd_get_argt(eap->cmdidx);
6836 int usefilter = FALSE;
6837
6838 has_expr = argt & (EX_XFILE | EX_EXPAND);
6839
6840 // If the command can be followed by a bar, find the bar and truncate
6841 // it, so that the following command can be compiled.
6842 // The '|' is overwritten with a NUL, it is put back below.
6843 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6844 && *eap->arg == '!')
6845 // :w !filter or :r !filter or :r! filter
6846 usefilter = TRUE;
6847 if ((argt & EX_TRLBAR) && !usefilter)
6848 {
6849 separate_nextcmd(eap);
6850 if (eap->nextcmd != NULL)
6851 nextcmd = eap->nextcmd;
6852 }
6853 }
6854
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006855 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6856 {
6857 // expand filename in "syntax include [@group] filename"
6858 has_expr = TRUE;
6859 eap->arg = skipwhite(eap->arg + 7);
6860 if (*eap->arg == '@')
6861 eap->arg = skiptowhite(eap->arg);
6862 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006863
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006864 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006865 {
6866 int count = 0;
6867 char_u *start = skipwhite(line);
6868
6869 // :cmd xxx`=expr1`yyy`=expr2`zzz
6870 // PUSHS ":cmd xxx"
6871 // eval expr1
6872 // PUSHS "yyy"
6873 // eval expr2
6874 // PUSHS "zzz"
6875 // EXECCONCAT 5
6876 for (;;)
6877 {
6878 if (p > start)
6879 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006880 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006881 ++count;
6882 }
6883 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006884 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006885 return NULL;
6886 may_generate_2STRING(-1, cctx);
6887 ++count;
6888 p = skipwhite(p);
6889 if (*p != '`')
6890 {
6891 emsg(_("E1083: missing backtick"));
6892 return NULL;
6893 }
6894 start = p + 1;
6895
6896 p = (char_u *)strstr((char *)start, "`=");
6897 if (p == NULL)
6898 {
6899 if (*skipwhite(start) != NUL)
6900 {
6901 generate_PUSHS(cctx, vim_strsave(start));
6902 ++count;
6903 }
6904 break;
6905 }
6906 }
6907 generate_EXECCONCAT(cctx, count);
6908 }
6909 else
6910 generate_EXEC(cctx, line);
6911
6912theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006913 if (*nextcmd != NUL)
6914 {
6915 // the parser expects a pointer to the bar, put it back
6916 --nextcmd;
6917 *nextcmd = '|';
6918 }
6919
6920 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006921}
6922
6923/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006924 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006925 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006926 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006927 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006928add_def_function(ufunc_T *ufunc)
6929{
6930 dfunc_T *dfunc;
6931
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006932 if (def_functions.ga_len == 0)
6933 {
6934 // The first position is not used, so that a zero uf_dfunc_idx means it
6935 // wasn't set.
6936 if (ga_grow(&def_functions, 1) == FAIL)
6937 return FAIL;
6938 ++def_functions.ga_len;
6939 }
6940
Bram Moolenaar09689a02020-05-09 22:50:08 +02006941 // Add the function to "def_functions".
6942 if (ga_grow(&def_functions, 1) == FAIL)
6943 return FAIL;
6944 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6945 CLEAR_POINTER(dfunc);
6946 dfunc->df_idx = def_functions.ga_len;
6947 ufunc->uf_dfunc_idx = dfunc->df_idx;
6948 dfunc->df_ufunc = ufunc;
6949 ++def_functions.ga_len;
6950 return OK;
6951}
6952
6953/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954 * After ex_function() has collected all the function lines: parse and compile
6955 * the lines into instructions.
6956 * Adds the function to "def_functions".
6957 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6958 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006959 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006960 * This can be used recursively through compile_lambda(), which may reallocate
6961 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006962 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006964 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006965compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967 char_u *line = NULL;
6968 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970 cctx_T cctx;
6971 garray_T *instr;
6972 int called_emsg_before = called_emsg;
6973 int ret = FAIL;
6974 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006975 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006976 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006977 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006979 // When using a function that was compiled before: Free old instructions.
6980 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006981 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006983 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6984 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006985 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006986 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006987 else
6988 {
6989 if (add_def_function(ufunc) == FAIL)
6990 return FAIL;
6991 new_def_function = TRUE;
6992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993
Bram Moolenaar985116a2020-07-12 17:31:09 +02006994 ufunc->uf_def_status = UF_COMPILING;
6995
Bram Moolenaara80faa82020-04-12 19:37:17 +02006996 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997 cctx.ctx_ufunc = ufunc;
6998 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006999 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7001 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7002 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7003 cctx.ctx_type_list = &ufunc->uf_type_list;
7004 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7005 instr = &cctx.ctx_instr;
7006
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007007 // Set the context to the function, it may be compiled when called from
7008 // another script. Set the script version to the most modern one.
7009 // The line number will be set in next_line_from_context().
7010 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7012
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007013 // Make sure error messages are OK.
7014 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7015 if (do_estack_push)
7016 estack_push_ufunc(ufunc, 1);
7017
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007018 if (ufunc->uf_def_args.ga_len > 0)
7019 {
7020 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007021 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007022 int i;
7023 char_u *arg;
7024 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007025 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007026
7027 // Produce instructions for the default values of optional arguments.
7028 // Store the instruction index in uf_def_arg_idx[] so that we know
7029 // where to start when the function is called, depending on the number
7030 // of arguments.
7031 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7032 if (ufunc->uf_def_arg_idx == NULL)
7033 goto erret;
7034 for (i = 0; i < count; ++i)
7035 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007036 garray_T *stack = &cctx.ctx_type_stack;
7037 type_T *val_type;
7038 int arg_idx = first_def_arg + i;
7039
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007040 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7041 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007042 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007043 goto erret;
7044
7045 // If no type specified use the type of the default value.
7046 // Otherwise check that the default value type matches the
7047 // specified type.
7048 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7049 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007050 {
7051 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007052 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007053 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02007054 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007055 == FAIL)
7056 {
7057 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
7058 arg_idx + 1);
7059 goto erret;
7060 }
7061
7062 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007063 goto erret;
7064 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007065 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007066
7067 if (did_set_arg_type)
7068 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007069 }
7070
7071 /*
7072 * Loop over all the lines of the function and generate instructions.
7073 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074 for (;;)
7075 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007076 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007077 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007078 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007079 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007080
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007081 // Bail out on the first error to avoid a flood of errors and report
7082 // the right line number when inside try/catch.
7083 if (emsg_before != called_emsg)
7084 goto erret;
7085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007086 if (line != NULL && *line == '|')
7087 // the line continues after a '|'
7088 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007089 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007090 && !(*line == '#' && (line == cctx.ctx_line_start
7091 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007093 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094 goto erret;
7095 }
7096 else
7097 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007098 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007099 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007100 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007103 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007104
Bram Moolenaara80faa82020-04-12 19:37:17 +02007105 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007106 ea.cmdlinep = &line;
7107 ea.cmd = skipwhite(line);
7108
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007109 // Some things can be recognized by the first character.
7110 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007111 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007112 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007113 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007114 if (ea.cmd[1] != '{')
7115 {
7116 line = (char_u *)"";
7117 continue;
7118 }
7119 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007121 case '}':
7122 {
7123 // "}" ends a block scope
7124 scopetype_T stype = cctx.ctx_scope == NULL
7125 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007127 if (stype == BLOCK_SCOPE)
7128 {
7129 compile_endblock(&cctx);
7130 line = ea.cmd;
7131 }
7132 else
7133 {
7134 emsg(_("E1025: using } outside of a block scope"));
7135 goto erret;
7136 }
7137 if (line != NULL)
7138 line = skipwhite(ea.cmd + 1);
7139 continue;
7140 }
7141
7142 case '{':
7143 // "{" starts a block scope
7144 // "{'a': 1}->func() is something else
7145 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7146 {
7147 line = compile_block(ea.cmd, &cctx);
7148 continue;
7149 }
7150 break;
7151
7152 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007153 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007154 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 }
7156
7157 /*
7158 * COMMAND MODIFIERS
7159 */
7160 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7161 {
7162 if (errormsg != NULL)
7163 goto erret;
7164 // empty line or comment
7165 line = (char_u *)"";
7166 continue;
7167 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007168 // TODO: use modifiers in the command
7169 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007170 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171
7172 // Skip ":call" to get to the function name.
7173 if (checkforcmd(&ea.cmd, "call", 3))
7174 ea.cmd = skipwhite(ea.cmd);
7175
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007176 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007178 char_u *pskip;
7179
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007180 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007181 // find what follows.
7182 // Skip over "var.member", "var[idx]" and the like.
7183 // Also "&opt = val", "$ENV = val" and "@r = val".
7184 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007185 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007186 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007187 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007189 char_u *var_end;
7190 int oplen;
7191 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007193 var_end = find_name_end(pskip, NULL, NULL,
7194 FNE_CHECK_START | FNE_INCL_BR);
7195 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007196 if (oplen > 0)
7197 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007198 size_t len = p - ea.cmd;
7199
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007200 // Recognize an assignment if we recognize the variable
7201 // name:
7202 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007203 // "local = expr" where "local" is a local var.
7204 // "script = expr" where "script" is a script-local var.
7205 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007206 // "&opt = expr"
7207 // "$ENV = expr"
7208 // "@r = expr"
7209 if (*ea.cmd == '&'
7210 || *ea.cmd == '$'
7211 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007212 || ((len) > 2 && ea.cmd[1] == ':')
7213 || lookup_local(ea.cmd, len, &cctx) != NULL
7214 || lookup_arg(ea.cmd, len, NULL, NULL,
7215 NULL, &cctx) == OK
7216 || lookup_script(ea.cmd, len) == OK
7217 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007218 {
7219 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007220 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007221 goto erret;
7222 continue;
7223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007224 }
7225 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007226
7227 if (*ea.cmd == '[')
7228 {
7229 // [var, var] = expr
7230 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7231 if (line == NULL)
7232 goto erret;
7233 if (line != ea.cmd)
7234 continue;
7235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236 }
7237
7238 /*
7239 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007240 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007242 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007243 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007244 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007245 ea.cmd = skip_range(ea.cmd, NULL);
7246 if (ea.cmd > cmd && !starts_with_colon)
7247 {
7248 emsg(_(e_colon_required));
7249 goto erret;
7250 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007251 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007252 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007253 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007254 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007255
7256 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7257 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007258 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007259 {
7260 line += STRLEN(line);
7261 continue;
7262 }
7263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007264 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007265 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007266 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007267 // CMD_let cannot happen, compile_assignment() above is used
7268 iemsg("Command from find_ex_command() not handled");
7269 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007270 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271 }
7272
7273 p = skipwhite(p);
7274
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007275 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007276 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007277 && ea.cmdidx != CMD_elseif
7278 && ea.cmdidx != CMD_else
7279 && ea.cmdidx != CMD_endif)
7280 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007281 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007282 continue;
7283 }
7284
Bram Moolenaarefd88552020-06-18 20:50:10 +02007285 if (ea.cmdidx != CMD_elseif
7286 && ea.cmdidx != CMD_else
7287 && ea.cmdidx != CMD_endif
7288 && ea.cmdidx != CMD_endfor
7289 && ea.cmdidx != CMD_endwhile
7290 && ea.cmdidx != CMD_catch
7291 && ea.cmdidx != CMD_finally
7292 && ea.cmdidx != CMD_endtry)
7293 {
7294 if (cctx.ctx_had_return)
7295 {
7296 emsg(_("E1095: Unreachable code after :return"));
7297 goto erret;
7298 }
7299 }
7300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301 switch (ea.cmdidx)
7302 {
7303 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007304 ea.arg = p;
7305 line = compile_nested_function(&ea, &cctx);
7306 break;
7307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007309 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 goto erret;
7311
7312 case CMD_return:
7313 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007314 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315 break;
7316
7317 case CMD_let:
7318 case CMD_const:
7319 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007320 if (line == p)
7321 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 break;
7323
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007324 case CMD_unlet:
7325 case CMD_unlockvar:
7326 case CMD_lockvar:
7327 line = compile_unletlock(p, &ea, &cctx);
7328 break;
7329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007330 case CMD_import:
7331 line = compile_import(p, &cctx);
7332 break;
7333
7334 case CMD_if:
7335 line = compile_if(p, &cctx);
7336 break;
7337 case CMD_elseif:
7338 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007339 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 break;
7341 case CMD_else:
7342 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007343 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 break;
7345 case CMD_endif:
7346 line = compile_endif(p, &cctx);
7347 break;
7348
7349 case CMD_while:
7350 line = compile_while(p, &cctx);
7351 break;
7352 case CMD_endwhile:
7353 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007354 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355 break;
7356
7357 case CMD_for:
7358 line = compile_for(p, &cctx);
7359 break;
7360 case CMD_endfor:
7361 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007362 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 break;
7364 case CMD_continue:
7365 line = compile_continue(p, &cctx);
7366 break;
7367 case CMD_break:
7368 line = compile_break(p, &cctx);
7369 break;
7370
7371 case CMD_try:
7372 line = compile_try(p, &cctx);
7373 break;
7374 case CMD_catch:
7375 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007376 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 break;
7378 case CMD_finally:
7379 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007380 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 break;
7382 case CMD_endtry:
7383 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007384 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385 break;
7386 case CMD_throw:
7387 line = compile_throw(p, &cctx);
7388 break;
7389
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007390 case CMD_eval:
7391 if (compile_expr0(&p, &cctx) == FAIL)
7392 goto erret;
7393
7394 // drop the return value
7395 generate_instr_drop(&cctx, ISN_DROP, 1);
7396
7397 line = skipwhite(p);
7398 break;
7399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007400 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007402 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007403 case CMD_echomsg:
7404 case CMD_echoerr:
7405 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007406 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007408 // TODO: other commands with an expression argument
7409
Bram Moolenaar002262f2020-07-08 17:47:57 +02007410 case CMD_SIZE:
7411 semsg(_("E476: Invalid command: %s"), ea.cmd);
7412 goto erret;
7413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007415 // Not recognized, execute with do_cmdline_cmd().
7416 ea.arg = p;
7417 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007418 break;
7419 }
7420 if (line == NULL)
7421 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007422 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423
7424 if (cctx.ctx_type_stack.ga_len < 0)
7425 {
7426 iemsg("Type stack underflow");
7427 goto erret;
7428 }
7429 }
7430
7431 if (cctx.ctx_scope != NULL)
7432 {
7433 if (cctx.ctx_scope->se_type == IF_SCOPE)
7434 emsg(_(e_endif));
7435 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7436 emsg(_(e_endwhile));
7437 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7438 emsg(_(e_endfor));
7439 else
7440 emsg(_("E1026: Missing }"));
7441 goto erret;
7442 }
7443
Bram Moolenaarefd88552020-06-18 20:50:10 +02007444 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445 {
7446 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7447 {
7448 emsg(_("E1027: Missing return statement"));
7449 goto erret;
7450 }
7451
7452 // Return zero if there is no return at the end.
7453 generate_PUSHNR(&cctx, 0);
7454 generate_instr(&cctx, ISN_RETURN);
7455 }
7456
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007457 {
7458 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7459 + ufunc->uf_dfunc_idx;
7460 dfunc->df_deleted = FALSE;
7461 dfunc->df_instr = instr->ga_data;
7462 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007463 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007464 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007465 if (cctx.ctx_outer_used)
7466 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007467 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469
7470 ret = OK;
7471
7472erret:
7473 if (ret == FAIL)
7474 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007475 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007476 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7477 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007478
7479 for (idx = 0; idx < instr->ga_len; ++idx)
7480 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007482
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007483 // If using the last entry in the table and it was added above, we
7484 // might as well remove it.
7485 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007486 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007487 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007488 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007489 ufunc->uf_dfunc_idx = 0;
7490 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007491 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007492
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007493 while (cctx.ctx_scope != NULL)
7494 drop_scope(&cctx);
7495
Bram Moolenaar20431c92020-03-20 18:39:46 +01007496 // Don't execute this function body.
7497 ga_clear_strings(&ufunc->uf_lines);
7498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007499 if (errormsg != NULL)
7500 emsg(errormsg);
7501 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007502 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007503 }
7504
7505 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007506 if (do_estack_push)
7507 estack_pop();
7508
Bram Moolenaar20431c92020-03-20 18:39:46 +01007509 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007510 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007512 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513}
7514
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007515 void
7516set_function_type(ufunc_T *ufunc)
7517{
7518 int varargs = ufunc->uf_va_name != NULL;
7519 int argcount = ufunc->uf_args.ga_len;
7520
7521 // Create a type for the function, with the return type and any
7522 // argument types.
7523 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7524 // The type is included in "tt_args".
7525 if (argcount > 0 || varargs)
7526 {
7527 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7528 argcount, &ufunc->uf_type_list);
7529 // Add argument types to the function type.
7530 if (func_type_add_arg_types(ufunc->uf_func_type,
7531 argcount + varargs,
7532 &ufunc->uf_type_list) == FAIL)
7533 return;
7534 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7535 ufunc->uf_func_type->tt_min_argcount =
7536 argcount - ufunc->uf_def_args.ga_len;
7537 if (ufunc->uf_arg_types == NULL)
7538 {
7539 int i;
7540
7541 // lambda does not have argument types.
7542 for (i = 0; i < argcount; ++i)
7543 ufunc->uf_func_type->tt_args[i] = &t_any;
7544 }
7545 else
7546 mch_memmove(ufunc->uf_func_type->tt_args,
7547 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7548 if (varargs)
7549 {
7550 ufunc->uf_func_type->tt_args[argcount] =
7551 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7552 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7553 }
7554 }
7555 else
7556 // No arguments, can use a predefined type.
7557 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7558 argcount, &ufunc->uf_type_list);
7559}
7560
7561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562/*
7563 * Delete an instruction, free what it contains.
7564 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007565 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007566delete_instr(isn_T *isn)
7567{
7568 switch (isn->isn_type)
7569 {
7570 case ISN_EXEC:
7571 case ISN_LOADENV:
7572 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007573 case ISN_LOADB:
7574 case ISN_LOADW:
7575 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007577 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007578 case ISN_PUSHEXC:
7579 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007580 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007581 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007582 case ISN_STOREB:
7583 case ISN_STOREW:
7584 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007585 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007586 vim_free(isn->isn_arg.string);
7587 break;
7588
7589 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007590 case ISN_STORES:
7591 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007592 break;
7593
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007594 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007595 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007596 vim_free(isn->isn_arg.unlet.ul_name);
7597 break;
7598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007599 case ISN_STOREOPT:
7600 vim_free(isn->isn_arg.storeopt.so_name);
7601 break;
7602
7603 case ISN_PUSHBLOB: // push blob isn_arg.blob
7604 blob_unref(isn->isn_arg.blob);
7605 break;
7606
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007607 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007608#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007609 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007610#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007611 break;
7612
7613 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007614#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007615 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007616#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007617 break;
7618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007619 case ISN_UCALL:
7620 vim_free(isn->isn_arg.ufunc.cuf_name);
7621 break;
7622
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007623 case ISN_FUNCREF:
7624 {
7625 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7626 + isn->isn_arg.funcref.fr_func;
7627 func_ptr_unref(dfunc->df_ufunc);
7628 }
7629 break;
7630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631 case ISN_2BOOL:
7632 case ISN_2STRING:
7633 case ISN_ADDBLOB:
7634 case ISN_ADDLIST:
7635 case ISN_BCALL:
7636 case ISN_CATCH:
7637 case ISN_CHECKNR:
7638 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007639 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640 case ISN_COMPAREANY:
7641 case ISN_COMPAREBLOB:
7642 case ISN_COMPAREBOOL:
7643 case ISN_COMPAREDICT:
7644 case ISN_COMPAREFLOAT:
7645 case ISN_COMPAREFUNC:
7646 case ISN_COMPARELIST:
7647 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007648 case ISN_COMPARESPECIAL:
7649 case ISN_COMPARESTRING:
7650 case ISN_CONCAT:
7651 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007652 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007653 case ISN_DROP:
7654 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007655 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007656 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007657 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007658 case ISN_EXECCONCAT:
7659 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007661 case ISN_LISTINDEX:
7662 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007663 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007664 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007665 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007666 case ISN_JUMP:
7667 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007668 case ISN_LOADBDICT:
7669 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007670 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007671 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007672 case ISN_LOADSCRIPT:
7673 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007674 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007675 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007676 case ISN_NEGATENR:
7677 case ISN_NEWDICT:
7678 case ISN_NEWLIST:
7679 case ISN_OPNR:
7680 case ISN_OPFLOAT:
7681 case ISN_OPANY:
7682 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007683 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684 case ISN_PUSHF:
7685 case ISN_PUSHNR:
7686 case ISN_PUSHBOOL:
7687 case ISN_PUSHSPEC:
7688 case ISN_RETURN:
7689 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007690 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007691 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007692 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007693 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007695 case ISN_STOREDICT:
7696 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007697 case ISN_THROW:
7698 case ISN_TRY:
7699 // nothing allocated
7700 break;
7701 }
7702}
7703
7704/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007705 * Free all instructions for "dfunc".
7706 */
7707 static void
7708delete_def_function_contents(dfunc_T *dfunc)
7709{
7710 int idx;
7711
7712 ga_clear(&dfunc->df_def_args_isn);
7713
7714 if (dfunc->df_instr != NULL)
7715 {
7716 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7717 delete_instr(dfunc->df_instr + idx);
7718 VIM_CLEAR(dfunc->df_instr);
7719 }
7720
7721 dfunc->df_deleted = TRUE;
7722}
7723
7724/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007725 * When a user function is deleted, clear the contents of any associated def
7726 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727 */
7728 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007729clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007730{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007731 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007732 {
7733 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7734 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735
Bram Moolenaar20431c92020-03-20 18:39:46 +01007736 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007737 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007738 }
7739}
7740
7741#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007742/*
7743 * Free all functions defined with ":def".
7744 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745 void
7746free_def_functions(void)
7747{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007748 int idx;
7749
7750 for (idx = 0; idx < def_functions.ga_len; ++idx)
7751 {
7752 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7753
7754 delete_def_function_contents(dfunc);
7755 }
7756
7757 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758}
7759#endif
7760
7761
7762#endif // FEAT_EVAL