blob: 93a4d896c5acf017c2afd180b53909b206b65053 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
146static char e_var_notfound[] = N_("E1001: variable not found: %s");
147static char e_syntax_at[] = N_("E1002: Syntax error at %s");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200148static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200149static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +0200150static char e_namespace[] = N_("E1075: Namespace not supported: %s");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151
Bram Moolenaar20431c92020-03-20 18:39:46 +0100152static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200153static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154
155/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200156 * Lookup variable "name" in the local scope and return it.
157 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160lookup_local(char_u *name, size_t len, cctx_T *cctx)
161{
162 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200163 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200166 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100172 if (STRNCMP(name, lvar->lv_name, len) == 0
173 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
175 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200176 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100178 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200179
180 // Find local in outer function scope.
181 if (cctx->ctx_outer != NULL)
182 {
183 lvar = lookup_local(name, len, cctx->ctx_outer);
184 if (lvar != NULL)
185 {
186 // TODO: are there situations we should not mark the outer scope as
187 // used?
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 return lvar;
191 }
192 }
193
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195}
196
197/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198 * Lookup an argument in the current function and an enclosing function.
199 * Returns the argument index in "idxp"
200 * Returns the argument type in "type"
201 * Sets "gen_load_outer" to TRUE if found in outer scope.
202 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203 */
204 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200205lookup_arg(
206 char_u *name,
207 size_t len,
208 int *idxp,
209 type_T **type,
210 int *gen_load_outer,
211 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212{
213 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200214 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100216 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
219 {
220 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
221
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200222 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
223 {
224 if (idxp != NULL)
225 {
226 // Arguments are located above the frame pointer. One further
227 // if there is a vararg argument
228 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
229 + STACK_FRAME_SIZE)
230 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
231
232 if (cctx->ctx_ufunc->uf_arg_types != NULL)
233 *type = cctx->ctx_ufunc->uf_arg_types[idx];
234 else
235 *type = &t_any;
236 }
237 return OK;
238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200241 va_name = cctx->ctx_ufunc->uf_va_name;
242 if (va_name != NULL
243 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
244 {
245 if (idxp != NULL)
246 {
247 // varargs is always the last argument
248 *idxp = -STACK_FRAME_SIZE - 1;
249 *type = cctx->ctx_ufunc->uf_va_type;
250 }
251 return OK;
252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200254 if (cctx->ctx_outer != NULL)
255 {
256 // Lookup the name for an argument of the outer function.
257 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
258 == OK)
259 {
260 *gen_load_outer = TRUE;
261 return OK;
262 }
263 }
264
265 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266}
267
268/*
269 * Lookup a variable in the current script.
270 * Returns OK or FAIL.
271 */
272 static int
273lookup_script(char_u *name, size_t len)
274{
275 int cc;
276 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
277 dictitem_T *di;
278
279 cc = name[len];
280 name[len] = NUL;
281 di = find_var_in_ht(ht, 0, name, TRUE);
282 name[len] = cc;
283 return di == NULL ? FAIL: OK;
284}
285
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100286/*
287 * Check if "p[len]" is already defined, either in script "import_sid" or in
288 * compilation context "cctx".
289 * Return FAIL and give an error if it defined.
290 */
291 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200292check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100293{
294 if (lookup_script(p, len) == OK
295 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200296 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100297 || find_imported(p, len, cctx) != NULL)))
298 {
299 semsg("E1073: imported name already defined: %s", p);
300 return FAIL;
301 }
302 return OK;
303}
304
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200305/*
306 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
307 * be freed later.
308 */
309 static type_T *
310alloc_type(garray_T *type_gap)
311{
312 type_T *type;
313
314 if (ga_grow(type_gap, 1) == FAIL)
315 return NULL;
316 type = ALLOC_CLEAR_ONE(type_T);
317 if (type != NULL)
318 {
319 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
320 ++type_gap->ga_len;
321 }
322 return type;
323}
324
Bram Moolenaar6110e792020-07-08 19:35:21 +0200325 void
326clear_type_list(garray_T *gap)
327{
328 while (gap->ga_len > 0)
329 vim_free(((type_T **)gap->ga_data)[--gap->ga_len]);
330 ga_clear(gap);
331}
332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100333 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200334get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335{
336 type_T *type;
337
338 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200339 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200341 if (member_type->tt_type == VAR_VOID
342 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100343 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100344 if (member_type->tt_type == VAR_BOOL)
345 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 if (member_type->tt_type == VAR_NUMBER)
347 return &t_list_number;
348 if (member_type->tt_type == VAR_STRING)
349 return &t_list_string;
350
351 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200352 type = alloc_type(type_gap);
353 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100354 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355 type->tt_type = VAR_LIST;
356 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200357 type->tt_argcount = 0;
358 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100359 return type;
360}
361
362 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200363get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364{
365 type_T *type;
366
367 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200368 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200370 if (member_type->tt_type == VAR_VOID
371 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100372 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100373 if (member_type->tt_type == VAR_BOOL)
374 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375 if (member_type->tt_type == VAR_NUMBER)
376 return &t_dict_number;
377 if (member_type->tt_type == VAR_STRING)
378 return &t_dict_string;
379
380 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200381 type = alloc_type(type_gap);
382 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100383 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 type->tt_type = VAR_DICT;
385 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200386 type->tt_argcount = 0;
387 type->tt_args = NULL;
388 return type;
389}
390
391/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200392 * Allocate a new type for a function.
393 */
394 static type_T *
395alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
396{
397 type_T *type = alloc_type(type_gap);
398
399 if (type == NULL)
400 return &t_any;
401 type->tt_type = VAR_FUNC;
402 type->tt_member = ret_type;
403 type->tt_argcount = argcount;
404 type->tt_args = NULL;
405 return type;
406}
407
408/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200409 * Get a function type, based on the return type "ret_type".
410 * If "argcount" is -1 or 0 a predefined type can be used.
411 * If "argcount" > 0 always create a new type, so that arguments can be added.
412 */
413 static type_T *
414get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
415{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200416 // recognize commonly used types
417 if (argcount <= 0)
418 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200419 if (ret_type == &t_unknown)
420 {
421 // (argcount == 0) is not possible
422 return &t_func_unknown;
423 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200424 if (ret_type == &t_void)
425 {
426 if (argcount == 0)
427 return &t_func_0_void;
428 else
429 return &t_func_void;
430 }
431 if (ret_type == &t_any)
432 {
433 if (argcount == 0)
434 return &t_func_0_any;
435 else
436 return &t_func_any;
437 }
438 if (ret_type == &t_number)
439 {
440 if (argcount == 0)
441 return &t_func_0_number;
442 else
443 return &t_func_number;
444 }
445 if (ret_type == &t_string)
446 {
447 if (argcount == 0)
448 return &t_func_0_string;
449 else
450 return &t_func_string;
451 }
452 }
453
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200454 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455}
456
Bram Moolenaara8c17702020-04-01 21:17:24 +0200457/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200458 * For a function type, reserve space for "argcount" argument types (including
459 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200460 */
461 static int
462func_type_add_arg_types(
463 type_T *functype,
464 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200465 garray_T *type_gap)
466{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200467 // To make it easy to free the space needed for the argument types, add the
468 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200469 if (ga_grow(type_gap, 1) == FAIL)
470 return FAIL;
471 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
472 if (functype->tt_args == NULL)
473 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200474 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
475 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200477 return OK;
478}
479
480/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200481 * Return the type_T for a typval. Only for primitive types.
482 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200483 type_T *
Bram Moolenaara8c17702020-04-01 21:17:24 +0200484typval2type(typval_T *tv)
485{
486 if (tv->v_type == VAR_NUMBER)
487 return &t_number;
488 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200489 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200490 if (tv->v_type == VAR_STRING)
491 return &t_string;
492 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
493 return &t_list_string;
494 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
495 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200496 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200497}
498
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200499 static void
500type_mismatch(type_T *expected, type_T *actual)
501{
502 char *tofree1, *tofree2;
503
504 semsg(_("E1013: type mismatch, expected %s but got %s"),
505 type_name(expected, &tofree1), type_name(actual, &tofree2));
506 vim_free(tofree1);
507 vim_free(tofree2);
508}
509
510 static void
511arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
512{
513 char *tofree1, *tofree2;
514
515 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
516 argidx,
517 type_name(expected, &tofree1), type_name(actual, &tofree2));
518 vim_free(tofree1);
519 vim_free(tofree2);
520}
521
522/*
523 * Check if the expected and actual types match.
524 * Does not allow for assigning "any" to a specific type.
525 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200526 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200527check_type(type_T *expected, type_T *actual, int give_msg)
528{
529 int ret = OK;
530
531 // When expected is "unknown" we accept any actual type.
532 // When expected is "any" we accept any actual type except "void".
533 if (expected->tt_type != VAR_UNKNOWN
534 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
535
536 {
537 if (expected->tt_type != actual->tt_type)
538 {
539 if (give_msg)
540 type_mismatch(expected, actual);
541 return FAIL;
542 }
543 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
544 {
545 // "unknown" is used for an empty list or dict
546 if (actual->tt_member != &t_unknown)
547 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
548 }
549 else if (expected->tt_type == VAR_FUNC)
550 {
551 if (expected->tt_member != &t_unknown)
552 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
553 if (ret == OK && expected->tt_argcount != -1
554 && (actual->tt_argcount < expected->tt_min_argcount
555 || actual->tt_argcount > expected->tt_argcount))
556 ret = FAIL;
557 }
558 if (ret == FAIL && give_msg)
559 type_mismatch(expected, actual);
560 }
561 return ret;
562}
563
Bram Moolenaar65b95452020-07-19 14:03:09 +0200564/*
565 * Return FAIl if "expected" and "actual" don't match.
566 * TODO: better type comparison
567 */
568 int
569check_argtype(type_T *expected, typval_T *actual_tv)
570{
571 type_T actual;
572 type_T member;
573
574 // TODO: should should be done with more levels
575 CLEAR_FIELD(actual);
576 actual.tt_type = actual_tv->v_type;
577 if (actual_tv->v_type == VAR_LIST
578 && actual_tv->vval.v_list != NULL
579 && actual_tv->vval.v_list->lv_first != NULL)
580 {
581 // Use the type of the first member, it is the most specific.
582 CLEAR_FIELD(member);
583 member.tt_type = actual_tv->vval.v_list->lv_first->li_tv.v_type;
584 member.tt_member = &t_any;
585 actual.tt_member = &member;
586 }
587 else if (actual_tv->v_type == VAR_DICT
588 && actual_tv->vval.v_dict != NULL
589 && actual_tv->vval.v_dict->dv_hashtab.ht_used > 0)
590 {
591 dict_iterator_T iter;
592 typval_T *value;
593
594 // Use the type of the first value, it is the most specific.
595 dict_iterate_start(actual_tv, &iter);
596 dict_iterate_next(&iter, &value);
597 CLEAR_FIELD(member);
598 member.tt_type = value->v_type;
599 member.tt_member = &t_any;
600 actual.tt_member = &member;
601 }
602 else
603 actual.tt_member = &t_any;
604 return check_type(expected, &actual, TRUE);
605}
606
607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608/////////////////////////////////////////////////////////////////////
609// Following generate_ functions expect the caller to call ga_grow().
610
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200611#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
612#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614/*
615 * Generate an instruction without arguments.
616 * Returns a pointer to the new instruction, NULL if failed.
617 */
618 static isn_T *
619generate_instr(cctx_T *cctx, isntype_T isn_type)
620{
621 garray_T *instr = &cctx->ctx_instr;
622 isn_T *isn;
623
Bram Moolenaar080457c2020-03-03 21:53:32 +0100624 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 if (ga_grow(instr, 1) == FAIL)
626 return NULL;
627 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
628 isn->isn_type = isn_type;
629 isn->isn_lnum = cctx->ctx_lnum + 1;
630 ++instr->ga_len;
631
632 return isn;
633}
634
635/*
636 * Generate an instruction without arguments.
637 * "drop" will be removed from the stack.
638 * Returns a pointer to the new instruction, NULL if failed.
639 */
640 static isn_T *
641generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
642{
643 garray_T *stack = &cctx->ctx_type_stack;
644
Bram Moolenaar080457c2020-03-03 21:53:32 +0100645 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646 stack->ga_len -= drop;
647 return generate_instr(cctx, isn_type);
648}
649
650/*
651 * Generate instruction "isn_type" and put "type" on the type stack.
652 */
653 static isn_T *
654generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
655{
656 isn_T *isn;
657 garray_T *stack = &cctx->ctx_type_stack;
658
659 if ((isn = generate_instr(cctx, isn_type)) == NULL)
660 return NULL;
661
662 if (ga_grow(stack, 1) == FAIL)
663 return NULL;
664 ((type_T **)stack->ga_data)[stack->ga_len] = type;
665 ++stack->ga_len;
666
667 return isn;
668}
669
670/*
671 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
672 */
673 static int
674may_generate_2STRING(int offset, cctx_T *cctx)
675{
676 isn_T *isn;
677 garray_T *stack = &cctx->ctx_type_stack;
678 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
679
680 if ((*type)->tt_type == VAR_STRING)
681 return OK;
682 *type = &t_string;
683
684 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
685 return FAIL;
686 isn->isn_arg.number = offset;
687
688 return OK;
689}
690
691 static int
692check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
693{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200694 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200696 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 {
698 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200699 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 else
701 semsg(_("E1036: %c requires number or float arguments"), *op);
702 return FAIL;
703 }
704 return OK;
705}
706
707/*
708 * Generate an instruction with two arguments. The instruction depends on the
709 * type of the arguments.
710 */
711 static int
712generate_two_op(cctx_T *cctx, char_u *op)
713{
714 garray_T *stack = &cctx->ctx_type_stack;
715 type_T *type1;
716 type_T *type2;
717 vartype_T vartype;
718 isn_T *isn;
719
Bram Moolenaar080457c2020-03-03 21:53:32 +0100720 RETURN_OK_IF_SKIP(cctx);
721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 // Get the known type of the two items on the stack. If they are matching
723 // use a type-specific instruction. Otherwise fall back to runtime type
724 // checking.
725 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
726 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200727 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 if (type1->tt_type == type2->tt_type
729 && (type1->tt_type == VAR_NUMBER
730 || type1->tt_type == VAR_LIST
731#ifdef FEAT_FLOAT
732 || type1->tt_type == VAR_FLOAT
733#endif
734 || type1->tt_type == VAR_BLOB))
735 vartype = type1->tt_type;
736
737 switch (*op)
738 {
739 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200740 && type1->tt_type != VAR_ANY
741 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742 && check_number_or_float(
743 type1->tt_type, type2->tt_type, op) == FAIL)
744 return FAIL;
745 isn = generate_instr_drop(cctx,
746 vartype == VAR_NUMBER ? ISN_OPNR
747 : vartype == VAR_LIST ? ISN_ADDLIST
748 : vartype == VAR_BLOB ? ISN_ADDBLOB
749#ifdef FEAT_FLOAT
750 : vartype == VAR_FLOAT ? ISN_OPFLOAT
751#endif
752 : ISN_OPANY, 1);
753 if (isn != NULL)
754 isn->isn_arg.op.op_type = EXPR_ADD;
755 break;
756
757 case '-':
758 case '*':
759 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
760 op) == FAIL)
761 return FAIL;
762 if (vartype == VAR_NUMBER)
763 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
764#ifdef FEAT_FLOAT
765 else if (vartype == VAR_FLOAT)
766 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
767#endif
768 else
769 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
770 if (isn != NULL)
771 isn->isn_arg.op.op_type = *op == '*'
772 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
773 break;
774
Bram Moolenaar4c683752020-04-05 21:38:23 +0200775 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200777 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 && type2->tt_type != VAR_NUMBER))
779 {
780 emsg(_("E1035: % requires number arguments"));
781 return FAIL;
782 }
783 isn = generate_instr_drop(cctx,
784 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
785 if (isn != NULL)
786 isn->isn_arg.op.op_type = EXPR_REM;
787 break;
788 }
789
790 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200791 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792 {
793 type_T *type = &t_any;
794
795#ifdef FEAT_FLOAT
796 // float+number and number+float results in float
797 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
798 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
799 type = &t_float;
800#endif
801 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
802 }
803
804 return OK;
805}
806
807/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200808 * Get the instruction to use for comparing "type1" with "type2"
809 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200811 static isntype_T
812get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813{
814 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815
Bram Moolenaar4c683752020-04-05 21:38:23 +0200816 if (type1 == VAR_UNKNOWN)
817 type1 = VAR_ANY;
818 if (type2 == VAR_UNKNOWN)
819 type2 = VAR_ANY;
820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 if (type1 == type2)
822 {
823 switch (type1)
824 {
825 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
826 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
827 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
828 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
829 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
830 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
831 case VAR_LIST: isntype = ISN_COMPARELIST; break;
832 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
833 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834 default: isntype = ISN_COMPAREANY; break;
835 }
836 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200837 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
839 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
840 isntype = ISN_COMPAREANY;
841
842 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
843 && (isntype == ISN_COMPAREBOOL
844 || isntype == ISN_COMPARESPECIAL
845 || isntype == ISN_COMPARENR
846 || isntype == ISN_COMPAREFLOAT))
847 {
848 semsg(_("E1037: Cannot use \"%s\" with %s"),
849 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200850 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 }
852 if (isntype == ISN_DROP
853 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
854 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
855 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
856 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
857 && exptype != EXPR_IS && exptype != EXPR_ISNOT
858 && (type1 == VAR_BLOB || type2 == VAR_BLOB
859 || type1 == VAR_LIST || type2 == VAR_LIST))))
860 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100861 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200863 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200865 return isntype;
866}
867
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200868 int
869check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
870{
871 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
872 return FAIL;
873 return OK;
874}
875
Bram Moolenaara5565e42020-05-09 15:44:01 +0200876/*
877 * Generate an ISN_COMPARE* instruction with a boolean result.
878 */
879 static int
880generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
881{
882 isntype_T isntype;
883 isn_T *isn;
884 garray_T *stack = &cctx->ctx_type_stack;
885 vartype_T type1;
886 vartype_T type2;
887
888 RETURN_OK_IF_SKIP(cctx);
889
890 // Get the known type of the two items on the stack. If they are matching
891 // use a type-specific instruction. Otherwise fall back to runtime type
892 // checking.
893 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
894 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
895 isntype = get_compare_isn(exptype, type1, type2);
896 if (isntype == ISN_DROP)
897 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898
899 if ((isn = generate_instr(cctx, isntype)) == NULL)
900 return FAIL;
901 isn->isn_arg.op.op_type = exptype;
902 isn->isn_arg.op.op_ic = ic;
903
904 // takes two arguments, puts one bool back
905 if (stack->ga_len >= 2)
906 {
907 --stack->ga_len;
908 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
909 }
910
911 return OK;
912}
913
914/*
915 * Generate an ISN_2BOOL instruction.
916 */
917 static int
918generate_2BOOL(cctx_T *cctx, int invert)
919{
920 isn_T *isn;
921 garray_T *stack = &cctx->ctx_type_stack;
922
Bram Moolenaar080457c2020-03-03 21:53:32 +0100923 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
925 return FAIL;
926 isn->isn_arg.number = invert;
927
928 // type becomes bool
929 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
930
931 return OK;
932}
933
934 static int
935generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
936{
937 isn_T *isn;
938 garray_T *stack = &cctx->ctx_type_stack;
939
Bram Moolenaar080457c2020-03-03 21:53:32 +0100940 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
942 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200943 // TODO: whole type, e.g. for a function also arg and return types
944 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 isn->isn_arg.type.ct_off = offset;
946
947 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200948 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949
950 return OK;
951}
952
953/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200954 * Check that
955 * - "actual" is "expected" type or
956 * - "actual" is a type that can be "expected" type: add a runtime check; or
957 * - return FAIL.
958 */
959 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200960need_type(
961 type_T *actual,
962 type_T *expected,
963 int offset,
964 cctx_T *cctx,
965 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200966{
967 if (check_type(expected, actual, FALSE) == OK)
968 return OK;
969 if (actual->tt_type != VAR_ANY
970 && actual->tt_type != VAR_UNKNOWN
971 && !(actual->tt_type == VAR_FUNC
972 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
973 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200974 if (!silent)
975 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200976 return FAIL;
977 }
978 generate_TYPECHECK(cctx, expected, offset);
979 return OK;
980}
981
982/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 * Generate an ISN_PUSHNR instruction.
984 */
985 static int
986generate_PUSHNR(cctx_T *cctx, varnumber_T number)
987{
988 isn_T *isn;
989
Bram Moolenaar080457c2020-03-03 21:53:32 +0100990 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
992 return FAIL;
993 isn->isn_arg.number = number;
994
995 return OK;
996}
997
998/*
999 * Generate an ISN_PUSHBOOL instruction.
1000 */
1001 static int
1002generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1003{
1004 isn_T *isn;
1005
Bram Moolenaar080457c2020-03-03 21:53:32 +01001006 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1008 return FAIL;
1009 isn->isn_arg.number = number;
1010
1011 return OK;
1012}
1013
1014/*
1015 * Generate an ISN_PUSHSPEC instruction.
1016 */
1017 static int
1018generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1019{
1020 isn_T *isn;
1021
Bram Moolenaar080457c2020-03-03 21:53:32 +01001022 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1024 return FAIL;
1025 isn->isn_arg.number = number;
1026
1027 return OK;
1028}
1029
1030#ifdef FEAT_FLOAT
1031/*
1032 * Generate an ISN_PUSHF instruction.
1033 */
1034 static int
1035generate_PUSHF(cctx_T *cctx, float_T fnumber)
1036{
1037 isn_T *isn;
1038
Bram Moolenaar080457c2020-03-03 21:53:32 +01001039 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1041 return FAIL;
1042 isn->isn_arg.fnumber = fnumber;
1043
1044 return OK;
1045}
1046#endif
1047
1048/*
1049 * Generate an ISN_PUSHS instruction.
1050 * Consumes "str".
1051 */
1052 static int
1053generate_PUSHS(cctx_T *cctx, char_u *str)
1054{
1055 isn_T *isn;
1056
Bram Moolenaar080457c2020-03-03 21:53:32 +01001057 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1059 return FAIL;
1060 isn->isn_arg.string = str;
1061
1062 return OK;
1063}
1064
1065/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001066 * Generate an ISN_PUSHCHANNEL instruction.
1067 * Consumes "channel".
1068 */
1069 static int
1070generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1071{
1072 isn_T *isn;
1073
Bram Moolenaar080457c2020-03-03 21:53:32 +01001074 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001075 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1076 return FAIL;
1077 isn->isn_arg.channel = channel;
1078
1079 return OK;
1080}
1081
1082/*
1083 * Generate an ISN_PUSHJOB instruction.
1084 * Consumes "job".
1085 */
1086 static int
1087generate_PUSHJOB(cctx_T *cctx, job_T *job)
1088{
1089 isn_T *isn;
1090
Bram Moolenaar080457c2020-03-03 21:53:32 +01001091 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001092 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001093 return FAIL;
1094 isn->isn_arg.job = job;
1095
1096 return OK;
1097}
1098
1099/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 * Generate an ISN_PUSHBLOB instruction.
1101 * Consumes "blob".
1102 */
1103 static int
1104generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1105{
1106 isn_T *isn;
1107
Bram Moolenaar080457c2020-03-03 21:53:32 +01001108 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1110 return FAIL;
1111 isn->isn_arg.blob = blob;
1112
1113 return OK;
1114}
1115
1116/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001117 * Generate an ISN_PUSHFUNC instruction with name "name".
1118 * Consumes "name".
1119 */
1120 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001121generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001122{
1123 isn_T *isn;
1124
Bram Moolenaar080457c2020-03-03 21:53:32 +01001125 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001126 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001127 return FAIL;
1128 isn->isn_arg.string = name;
1129
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001134 * Generate an ISN_GETITEM instruction with "index".
1135 */
1136 static int
1137generate_GETITEM(cctx_T *cctx, int index)
1138{
1139 isn_T *isn;
1140 garray_T *stack = &cctx->ctx_type_stack;
1141 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1142 type_T *item_type = &t_any;
1143
1144 RETURN_OK_IF_SKIP(cctx);
1145
1146 if (type->tt_type == VAR_LIST)
1147 item_type = type->tt_member;
1148 else if (type->tt_type != VAR_ANY)
1149 {
1150 emsg(_(e_listreq));
1151 return FAIL;
1152 }
1153 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1154 return FAIL;
1155 isn->isn_arg.number = index;
1156
1157 // add the item type to the type stack
1158 if (ga_grow(stack, 1) == FAIL)
1159 return FAIL;
1160 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1161 ++stack->ga_len;
1162 return OK;
1163}
1164
1165/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001166 * Generate an ISN_SLICE instruction with "count".
1167 */
1168 static int
1169generate_SLICE(cctx_T *cctx, int count)
1170{
1171 isn_T *isn;
1172
1173 RETURN_OK_IF_SKIP(cctx);
1174 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1175 return FAIL;
1176 isn->isn_arg.number = count;
1177 return OK;
1178}
1179
1180/*
1181 * Generate an ISN_CHECKLEN instruction with "min_len".
1182 */
1183 static int
1184generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1185{
1186 isn_T *isn;
1187
1188 RETURN_OK_IF_SKIP(cctx);
1189
1190 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1191 return FAIL;
1192 isn->isn_arg.checklen.cl_min_len = min_len;
1193 isn->isn_arg.checklen.cl_more_OK = more_OK;
1194
1195 return OK;
1196}
1197
1198/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 * Generate an ISN_STORE instruction.
1200 */
1201 static int
1202generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1203{
1204 isn_T *isn;
1205
Bram Moolenaar080457c2020-03-03 21:53:32 +01001206 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1208 return FAIL;
1209 if (name != NULL)
1210 isn->isn_arg.string = vim_strsave(name);
1211 else
1212 isn->isn_arg.number = idx;
1213
1214 return OK;
1215}
1216
1217/*
1218 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1219 */
1220 static int
1221generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1222{
1223 isn_T *isn;
1224
Bram Moolenaar080457c2020-03-03 21:53:32 +01001225 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1227 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001228 isn->isn_arg.storenr.stnr_idx = idx;
1229 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230
1231 return OK;
1232}
1233
1234/*
1235 * Generate an ISN_STOREOPT instruction
1236 */
1237 static int
1238generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1239{
1240 isn_T *isn;
1241
Bram Moolenaar080457c2020-03-03 21:53:32 +01001242 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1244 return FAIL;
1245 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1246 isn->isn_arg.storeopt.so_flags = opt_flags;
1247
1248 return OK;
1249}
1250
1251/*
1252 * Generate an ISN_LOAD or similar instruction.
1253 */
1254 static int
1255generate_LOAD(
1256 cctx_T *cctx,
1257 isntype_T isn_type,
1258 int idx,
1259 char_u *name,
1260 type_T *type)
1261{
1262 isn_T *isn;
1263
Bram Moolenaar080457c2020-03-03 21:53:32 +01001264 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1266 return FAIL;
1267 if (name != NULL)
1268 isn->isn_arg.string = vim_strsave(name);
1269 else
1270 isn->isn_arg.number = idx;
1271
1272 return OK;
1273}
1274
1275/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001276 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001277 */
1278 static int
1279generate_LOADV(
1280 cctx_T *cctx,
1281 char_u *name,
1282 int error)
1283{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001284 int di_flags;
1285 int vidx = find_vim_var(name, &di_flags);
1286 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001287
Bram Moolenaar080457c2020-03-03 21:53:32 +01001288 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001289 if (vidx < 0)
1290 {
1291 if (error)
1292 semsg(_(e_var_notfound), name);
1293 return FAIL;
1294 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001295 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001296
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001297 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001298}
1299
1300/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001301 * Generate an ISN_UNLET instruction.
1302 */
1303 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001304generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001305{
1306 isn_T *isn;
1307
1308 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001309 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001310 return FAIL;
1311 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1312 isn->isn_arg.unlet.ul_forceit = forceit;
1313
1314 return OK;
1315}
1316
1317/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 * Generate an ISN_LOADS instruction.
1319 */
1320 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001321generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001323 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001325 int sid,
1326 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327{
1328 isn_T *isn;
1329
Bram Moolenaar080457c2020-03-03 21:53:32 +01001330 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001331 if (isn_type == ISN_LOADS)
1332 isn = generate_instr_type(cctx, isn_type, type);
1333 else
1334 isn = generate_instr_drop(cctx, isn_type, 1);
1335 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001337 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1338 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339
1340 return OK;
1341}
1342
1343/*
1344 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1345 */
1346 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001347generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 cctx_T *cctx,
1349 isntype_T isn_type,
1350 int sid,
1351 int idx,
1352 type_T *type)
1353{
1354 isn_T *isn;
1355
Bram Moolenaar080457c2020-03-03 21:53:32 +01001356 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 if (isn_type == ISN_LOADSCRIPT)
1358 isn = generate_instr_type(cctx, isn_type, type);
1359 else
1360 isn = generate_instr_drop(cctx, isn_type, 1);
1361 if (isn == NULL)
1362 return FAIL;
1363 isn->isn_arg.script.script_sid = sid;
1364 isn->isn_arg.script.script_idx = idx;
1365 return OK;
1366}
1367
1368/*
1369 * Generate an ISN_NEWLIST instruction.
1370 */
1371 static int
1372generate_NEWLIST(cctx_T *cctx, int count)
1373{
1374 isn_T *isn;
1375 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 type_T *type;
1377 type_T *member;
1378
Bram Moolenaar080457c2020-03-03 21:53:32 +01001379 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1381 return FAIL;
1382 isn->isn_arg.number = count;
1383
1384 // drop the value types
1385 stack->ga_len -= count;
1386
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001387 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001388 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 if (count > 0)
1390 member = ((type_T **)stack->ga_data)[stack->ga_len];
1391 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001392 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001393 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394
1395 // add the list type to the type stack
1396 if (ga_grow(stack, 1) == FAIL)
1397 return FAIL;
1398 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1399 ++stack->ga_len;
1400
1401 return OK;
1402}
1403
1404/*
1405 * Generate an ISN_NEWDICT instruction.
1406 */
1407 static int
1408generate_NEWDICT(cctx_T *cctx, int count)
1409{
1410 isn_T *isn;
1411 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 type_T *type;
1413 type_T *member;
1414
Bram Moolenaar080457c2020-03-03 21:53:32 +01001415 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1417 return FAIL;
1418 isn->isn_arg.number = count;
1419
1420 // drop the key and value types
1421 stack->ga_len -= 2 * count;
1422
Bram Moolenaar436472f2020-02-20 22:54:43 +01001423 // Use the first value type for the list member type. Use "void" for an
1424 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 if (count > 0)
1426 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1427 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001428 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001429 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430
1431 // add the dict type to the type stack
1432 if (ga_grow(stack, 1) == FAIL)
1433 return FAIL;
1434 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1435 ++stack->ga_len;
1436
1437 return OK;
1438}
1439
1440/*
1441 * Generate an ISN_FUNCREF instruction.
1442 */
1443 static int
1444generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1445{
1446 isn_T *isn;
1447 garray_T *stack = &cctx->ctx_type_stack;
1448
Bram Moolenaar080457c2020-03-03 21:53:32 +01001449 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1451 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001452 isn->isn_arg.funcref.fr_func = dfunc_idx;
1453 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454
1455 if (ga_grow(stack, 1) == FAIL)
1456 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001457 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 // TODO: argument and return types
1459 ++stack->ga_len;
1460
1461 return OK;
1462}
1463
1464/*
1465 * Generate an ISN_JUMP instruction.
1466 */
1467 static int
1468generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1469{
1470 isn_T *isn;
1471 garray_T *stack = &cctx->ctx_type_stack;
1472
Bram Moolenaar080457c2020-03-03 21:53:32 +01001473 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1475 return FAIL;
1476 isn->isn_arg.jump.jump_when = when;
1477 isn->isn_arg.jump.jump_where = where;
1478
1479 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1480 --stack->ga_len;
1481
1482 return OK;
1483}
1484
1485 static int
1486generate_FOR(cctx_T *cctx, int loop_idx)
1487{
1488 isn_T *isn;
1489 garray_T *stack = &cctx->ctx_type_stack;
1490
Bram Moolenaar080457c2020-03-03 21:53:32 +01001491 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1493 return FAIL;
1494 isn->isn_arg.forloop.for_idx = loop_idx;
1495
1496 if (ga_grow(stack, 1) == FAIL)
1497 return FAIL;
1498 // type doesn't matter, will be stored next
1499 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1500 ++stack->ga_len;
1501
1502 return OK;
1503}
1504
1505/*
1506 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001507 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 * Return FAIL if the number of arguments is wrong.
1509 */
1510 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001511generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512{
1513 isn_T *isn;
1514 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001515 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001516 type_T *argtypes[MAX_FUNC_ARGS];
1517 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518
Bram Moolenaar080457c2020-03-03 21:53:32 +01001519 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001520 argoff = check_internal_func(func_idx, argcount);
1521 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 return FAIL;
1523
Bram Moolenaar389df252020-07-09 21:20:47 +02001524 if (method_call && argoff > 1)
1525 {
1526 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1527 return FAIL;
1528 isn->isn_arg.shuffle.shfl_item = argcount;
1529 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1530 }
1531
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1533 return FAIL;
1534 isn->isn_arg.bfunc.cbf_idx = func_idx;
1535 isn->isn_arg.bfunc.cbf_argcount = argcount;
1536
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001537 for (i = 0; i < argcount; ++i)
1538 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 stack->ga_len -= argcount; // drop the arguments
1541 if (ga_grow(stack, 1) == FAIL)
1542 return FAIL;
1543 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001544 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 ++stack->ga_len; // add return value
1546
1547 return OK;
1548}
1549
1550/*
1551 * Generate an ISN_DCALL or ISN_UCALL instruction.
1552 * Return FAIL if the number of arguments is wrong.
1553 */
1554 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001555generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556{
1557 isn_T *isn;
1558 garray_T *stack = &cctx->ctx_type_stack;
1559 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001560 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561
Bram Moolenaar080457c2020-03-03 21:53:32 +01001562 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 if (argcount > regular_args && !has_varargs(ufunc))
1564 {
1565 semsg(_(e_toomanyarg), ufunc->uf_name);
1566 return FAIL;
1567 }
1568 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1569 {
1570 semsg(_(e_toofewarg), ufunc->uf_name);
1571 return FAIL;
1572 }
1573
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001574 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001575 {
1576 int i;
1577
1578 for (i = 0; i < argcount; ++i)
1579 {
1580 type_T *expected;
1581 type_T *actual;
1582
1583 if (i < regular_args)
1584 {
1585 if (ufunc->uf_arg_types == NULL)
1586 continue;
1587 expected = ufunc->uf_arg_types[i];
1588 }
1589 else
1590 expected = ufunc->uf_va_type->tt_member;
1591 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001592 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001593 {
1594 arg_type_mismatch(expected, actual, i + 1);
1595 return FAIL;
1596 }
1597 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001598 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001599 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001600 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001601 }
1602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001604 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001605 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001607 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 {
1609 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1610 isn->isn_arg.dfunc.cdf_argcount = argcount;
1611 }
1612 else
1613 {
1614 // A user function may be deleted and redefined later, can't use the
1615 // ufunc pointer, need to look it up again at runtime.
1616 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1617 isn->isn_arg.ufunc.cuf_argcount = argcount;
1618 }
1619
1620 stack->ga_len -= argcount; // drop the arguments
1621 if (ga_grow(stack, 1) == FAIL)
1622 return FAIL;
1623 // add return value
1624 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1625 ++stack->ga_len;
1626
1627 return OK;
1628}
1629
1630/*
1631 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1632 */
1633 static int
1634generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1635{
1636 isn_T *isn;
1637 garray_T *stack = &cctx->ctx_type_stack;
1638
Bram Moolenaar080457c2020-03-03 21:53:32 +01001639 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1641 return FAIL;
1642 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1643 isn->isn_arg.ufunc.cuf_argcount = argcount;
1644
1645 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001646 if (ga_grow(stack, 1) == FAIL)
1647 return FAIL;
1648 // add return value
1649 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1650 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651
1652 return OK;
1653}
1654
1655/*
1656 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001657 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 */
1659 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001660generate_PCALL(
1661 cctx_T *cctx,
1662 int argcount,
1663 char_u *name,
1664 type_T *type,
1665 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666{
1667 isn_T *isn;
1668 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001669 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670
Bram Moolenaar080457c2020-03-03 21:53:32 +01001671 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001672
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001673 if (type->tt_type == VAR_ANY)
1674 ret_type = &t_any;
1675 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001676 {
1677 if (type->tt_argcount != -1)
1678 {
1679 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1680
1681 if (argcount < type->tt_min_argcount - varargs)
1682 {
1683 semsg(_(e_toofewarg), "[reference]");
1684 return FAIL;
1685 }
1686 if (!varargs && argcount > type->tt_argcount)
1687 {
1688 semsg(_(e_toomanyarg), "[reference]");
1689 return FAIL;
1690 }
1691 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001692 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001693 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001694 else
1695 {
1696 semsg(_("E1085: Not a callable type: %s"), name);
1697 return FAIL;
1698 }
1699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1701 return FAIL;
1702 isn->isn_arg.pfunc.cpf_top = at_top;
1703 isn->isn_arg.pfunc.cpf_argcount = argcount;
1704
1705 stack->ga_len -= argcount; // drop the arguments
1706
1707 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001708 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001710 // If partial is above the arguments it must be cleared and replaced with
1711 // the return value.
1712 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1713 return FAIL;
1714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715 return OK;
1716}
1717
1718/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001719 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 */
1721 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001722generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723{
1724 isn_T *isn;
1725 garray_T *stack = &cctx->ctx_type_stack;
1726 type_T *type;
1727
Bram Moolenaar080457c2020-03-03 21:53:32 +01001728 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001729 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001731 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001733 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001735 if (type->tt_type != VAR_DICT && type != &t_any)
1736 {
1737 emsg(_(e_dictreq));
1738 return FAIL;
1739 }
1740 // change dict type to dict member type
1741 if (type->tt_type == VAR_DICT)
1742 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743
1744 return OK;
1745}
1746
1747/*
1748 * Generate an ISN_ECHO instruction.
1749 */
1750 static int
1751generate_ECHO(cctx_T *cctx, int with_white, int count)
1752{
1753 isn_T *isn;
1754
Bram Moolenaar080457c2020-03-03 21:53:32 +01001755 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1757 return FAIL;
1758 isn->isn_arg.echo.echo_with_white = with_white;
1759 isn->isn_arg.echo.echo_count = count;
1760
1761 return OK;
1762}
1763
Bram Moolenaarad39c092020-02-26 18:23:43 +01001764/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001765 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001766 */
1767 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001768generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001769{
1770 isn_T *isn;
1771
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001772 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001773 return FAIL;
1774 isn->isn_arg.number = count;
1775
1776 return OK;
1777}
1778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 static int
1780generate_EXEC(cctx_T *cctx, char_u *line)
1781{
1782 isn_T *isn;
1783
Bram Moolenaar080457c2020-03-03 21:53:32 +01001784 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1786 return FAIL;
1787 isn->isn_arg.string = vim_strsave(line);
1788 return OK;
1789}
1790
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001791 static int
1792generate_EXECCONCAT(cctx_T *cctx, int count)
1793{
1794 isn_T *isn;
1795
1796 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1797 return FAIL;
1798 isn->isn_arg.number = count;
1799 return OK;
1800}
1801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802/*
1803 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001804 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001806 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1808{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 lvar_T *lvar;
1810
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001811 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001813 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001814 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 }
1816
1817 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001818 return NULL;
1819 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001821 // Every local variable uses the next entry on the stack. We could re-use
1822 // the last ones when leaving a scope, but then variables used in a closure
1823 // might get overwritten. To keep things simple do not re-use stack
1824 // entries. This is less efficient, but memory is cheap these days.
1825 lvar->lv_idx = cctx->ctx_locals_count++;
1826
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001827 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 lvar->lv_const = isConst;
1829 lvar->lv_type = type;
1830
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001831 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832}
1833
1834/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001835 * Remove local variables above "new_top".
1836 */
1837 static void
1838unwind_locals(cctx_T *cctx, int new_top)
1839{
1840 if (cctx->ctx_locals.ga_len > new_top)
1841 {
1842 int idx;
1843 lvar_T *lvar;
1844
1845 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1846 {
1847 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1848 vim_free(lvar->lv_name);
1849 }
1850 }
1851 cctx->ctx_locals.ga_len = new_top;
1852}
1853
1854/*
1855 * Free all local variables.
1856 */
1857 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001858free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001859{
1860 unwind_locals(cctx, 0);
1861 ga_clear(&cctx->ctx_locals);
1862}
1863
1864/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 * Skip over a type definition and return a pointer to just after it.
1866 */
1867 char_u *
1868skip_type(char_u *start)
1869{
1870 char_u *p = start;
1871
1872 while (ASCII_ISALNUM(*p) || *p == '_')
1873 ++p;
1874
1875 // Skip over "<type>"; this is permissive about white space.
1876 if (*skipwhite(p) == '<')
1877 {
1878 p = skipwhite(p);
1879 p = skip_type(skipwhite(p + 1));
1880 p = skipwhite(p);
1881 if (*p == '>')
1882 ++p;
1883 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001884 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1885 {
1886 // handle func(args): type
1887 ++p;
1888 while (*p != ')' && *p != NUL)
1889 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001890 char_u *sp = p;
1891
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001892 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001893 if (p == sp)
1894 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001895 if (*p == ',')
1896 p = skipwhite(p + 1);
1897 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001898 if (*p == ')')
1899 {
1900 if (p[1] == ':')
1901 p = skip_type(skipwhite(p + 2));
1902 else
1903 p = skipwhite(p + 1);
1904 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001905 }
1906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 return p;
1908}
1909
1910/*
1911 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001912 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 * Returns NULL in case of failure.
1914 */
1915 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001916parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917{
1918 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001919 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920
1921 if (**arg != '<')
1922 {
1923 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001924 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 else
1926 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001927 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 }
1929 *arg = skipwhite(*arg + 1);
1930
Bram Moolenaard77a8522020-04-03 21:59:57 +02001931 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932
1933 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001934 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 {
1936 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001937 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 }
1939 ++*arg;
1940
1941 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001942 return get_list_type(member_type, type_gap);
1943 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944}
1945
1946/*
1947 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001948 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 */
1950 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001951parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952{
1953 char_u *p = *arg;
1954 size_t len;
1955
1956 // skip over the first word
1957 while (ASCII_ISALNUM(*p) || *p == '_')
1958 ++p;
1959 len = p - *arg;
1960
1961 switch (**arg)
1962 {
1963 case 'a':
1964 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1965 {
1966 *arg += len;
1967 return &t_any;
1968 }
1969 break;
1970 case 'b':
1971 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1972 {
1973 *arg += len;
1974 return &t_bool;
1975 }
1976 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1977 {
1978 *arg += len;
1979 return &t_blob;
1980 }
1981 break;
1982 case 'c':
1983 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1984 {
1985 *arg += len;
1986 return &t_channel;
1987 }
1988 break;
1989 case 'd':
1990 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1991 {
1992 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001993 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 }
1995 break;
1996 case 'f':
1997 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1998 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001999#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 *arg += len;
2001 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002002#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002003 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002004 return &t_any;
2005#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 }
2007 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2008 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002009 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002010 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002011 int argcount = -1;
2012 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002013 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002014 type_T *arg_type[MAX_FUNC_ARGS + 1];
2015
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002016 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002018 if (**arg == '(')
2019 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002020 // "func" may or may not return a value, "func()" does
2021 // not return a value.
2022 ret_type = &t_void;
2023
Bram Moolenaard77a8522020-04-03 21:59:57 +02002024 p = ++*arg;
2025 argcount = 0;
2026 while (*p != NUL && *p != ')')
2027 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002028 if (*p == '?')
2029 {
2030 if (first_optional == -1)
2031 first_optional = argcount;
2032 ++p;
2033 }
2034 else if (first_optional != -1)
2035 {
2036 emsg(_("E1007: mandatory argument after optional argument"));
2037 return &t_any;
2038 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002039 else if (STRNCMP(p, "...", 3) == 0)
2040 {
2041 flags |= TTFLAG_VARARGS;
2042 p += 3;
2043 }
2044
2045 arg_type[argcount++] = parse_type(&p, type_gap);
2046
2047 // Nothing comes after "...{type}".
2048 if (flags & TTFLAG_VARARGS)
2049 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002050
Bram Moolenaard77a8522020-04-03 21:59:57 +02002051 if (*p != ',' && *skipwhite(p) == ',')
2052 {
2053 semsg(_(e_no_white_before), ",");
2054 return &t_any;
2055 }
2056 if (*p == ',')
2057 {
2058 ++p;
2059 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002060 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002061 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002062 return &t_any;
2063 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002064 }
2065 p = skipwhite(p);
2066 if (argcount == MAX_FUNC_ARGS)
2067 {
2068 emsg(_("E740: Too many argument types"));
2069 return &t_any;
2070 }
2071 }
2072
2073 p = skipwhite(p);
2074 if (*p != ')')
2075 {
2076 emsg(_(e_missing_close));
2077 return &t_any;
2078 }
2079 *arg = p + 1;
2080 }
2081 if (**arg == ':')
2082 {
2083 // parse return type
2084 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002085 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002086 semsg(_(e_white_after), ":");
2087 *arg = skipwhite(*arg);
2088 ret_type = parse_type(arg, type_gap);
2089 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002090 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002091 type = get_func_type(ret_type, argcount, type_gap);
2092 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002093 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002094 type = alloc_func_type(ret_type, argcount, type_gap);
2095 type->tt_flags = flags;
2096 if (argcount > 0)
2097 {
2098 type->tt_argcount = argcount;
2099 type->tt_min_argcount = first_optional == -1
2100 ? argcount : first_optional;
2101 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002102 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002103 return &t_any;
2104 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002105 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002106 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002107 }
2108 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 }
2110 break;
2111 case 'j':
2112 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2113 {
2114 *arg += len;
2115 return &t_job;
2116 }
2117 break;
2118 case 'l':
2119 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2120 {
2121 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002122 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 }
2124 break;
2125 case 'n':
2126 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2127 {
2128 *arg += len;
2129 return &t_number;
2130 }
2131 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 case 's':
2133 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2134 {
2135 *arg += len;
2136 return &t_string;
2137 }
2138 break;
2139 case 'v':
2140 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2141 {
2142 *arg += len;
2143 return &t_void;
2144 }
2145 break;
2146 }
2147
2148 semsg(_("E1010: Type not recognized: %s"), *arg);
2149 return &t_any;
2150}
2151
2152/*
2153 * Check if "type1" and "type2" are exactly the same.
2154 */
2155 static int
2156equal_type(type_T *type1, type_T *type2)
2157{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002158 int i;
2159
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 if (type1->tt_type != type2->tt_type)
2161 return FALSE;
2162 switch (type1->tt_type)
2163 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002165 case VAR_ANY:
2166 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 case VAR_SPECIAL:
2168 case VAR_BOOL:
2169 case VAR_NUMBER:
2170 case VAR_FLOAT:
2171 case VAR_STRING:
2172 case VAR_BLOB:
2173 case VAR_JOB:
2174 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002175 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 case VAR_LIST:
2177 case VAR_DICT:
2178 return equal_type(type1->tt_member, type2->tt_member);
2179 case VAR_FUNC:
2180 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002181 if (!equal_type(type1->tt_member, type2->tt_member)
2182 || type1->tt_argcount != type2->tt_argcount)
2183 return FALSE;
2184 if (type1->tt_argcount < 0
2185 || type1->tt_args == NULL || type2->tt_args == NULL)
2186 return TRUE;
2187 for (i = 0; i < type1->tt_argcount; ++i)
2188 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2189 return FALSE;
2190 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 }
2192 return TRUE;
2193}
2194
2195/*
2196 * Find the common type of "type1" and "type2" and put it in "dest".
2197 * "type2" and "dest" may be the same.
2198 */
2199 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002200common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201{
2202 if (equal_type(type1, type2))
2203 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002204 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 return;
2206 }
2207
2208 if (type1->tt_type == type2->tt_type)
2209 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2211 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002212 type_T *common;
2213
Bram Moolenaard77a8522020-04-03 21:59:57 +02002214 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002215 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002216 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002217 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002218 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 return;
2220 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002221 if (type1->tt_type == VAR_FUNC)
2222 {
2223 type_T *common;
2224
2225 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2226 if (type1->tt_argcount == type2->tt_argcount
2227 && type1->tt_argcount >= 0)
2228 {
2229 int argcount = type1->tt_argcount;
2230 int i;
2231
2232 *dest = alloc_func_type(common, argcount, type_gap);
2233 if (type1->tt_args != NULL && type2->tt_args != NULL)
2234 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002235 if (func_type_add_arg_types(*dest, argcount,
2236 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002237 for (i = 0; i < argcount; ++i)
2238 common_type(type1->tt_args[i], type2->tt_args[i],
2239 &(*dest)->tt_args[i], type_gap);
2240 }
2241 }
2242 else
2243 *dest = alloc_func_type(common, -1, type_gap);
2244 return;
2245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 }
2247
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002248 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249}
2250
2251 char *
2252vartype_name(vartype_T type)
2253{
2254 switch (type)
2255 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002256 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002257 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 case VAR_SPECIAL: return "special";
2260 case VAR_BOOL: return "bool";
2261 case VAR_NUMBER: return "number";
2262 case VAR_FLOAT: return "float";
2263 case VAR_STRING: return "string";
2264 case VAR_BLOB: return "blob";
2265 case VAR_JOB: return "job";
2266 case VAR_CHANNEL: return "channel";
2267 case VAR_LIST: return "list";
2268 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002269
2270 case VAR_FUNC:
2271 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002273 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274}
2275
2276/*
2277 * Return the name of a type.
2278 * The result may be in allocated memory, in which case "tofree" is set.
2279 */
2280 char *
2281type_name(type_T *type, char **tofree)
2282{
2283 char *name = vartype_name(type->tt_type);
2284
2285 *tofree = NULL;
2286 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2287 {
2288 char *member_free;
2289 char *member_name = type_name(type->tt_member, &member_free);
2290 size_t len;
2291
2292 len = STRLEN(name) + STRLEN(member_name) + 3;
2293 *tofree = alloc(len);
2294 if (*tofree != NULL)
2295 {
2296 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2297 vim_free(member_free);
2298 return *tofree;
2299 }
2300 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002301 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002302 {
2303 garray_T ga;
2304 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002305 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002306
2307 ga_init2(&ga, 1, 100);
2308 if (ga_grow(&ga, 20) == FAIL)
2309 return "[unknown]";
2310 *tofree = ga.ga_data;
2311 STRCPY(ga.ga_data, "func(");
2312 ga.ga_len += 5;
2313
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002314 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002315 {
2316 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002317 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002318 int len;
2319
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002320 if (type->tt_args == NULL)
2321 arg_type = "[unknown]";
2322 else
2323 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002324 if (i > 0)
2325 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002326 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002327 ga.ga_len += 2;
2328 }
2329 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002330 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002331 {
2332 vim_free(arg_free);
2333 return "[unknown]";
2334 }
2335 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002336 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002337 {
2338 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2339 ga.ga_len += 3;
2340 }
2341 else if (i >= type->tt_min_argcount)
2342 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002343 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002344 ga.ga_len += len;
2345 vim_free(arg_free);
2346 }
2347
2348 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002349 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002350 else
2351 {
2352 char *ret_free;
2353 char *ret_name = type_name(type->tt_member, &ret_free);
2354 int len;
2355
2356 len = (int)STRLEN(ret_name) + 4;
2357 if (ga_grow(&ga, len) == FAIL)
2358 {
2359 vim_free(ret_free);
2360 return "[unknown]";
2361 }
2362 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002363 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2364 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002365 vim_free(ret_free);
2366 }
2367 return ga.ga_data;
2368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369
2370 return name;
2371}
2372
2373/*
2374 * Find "name" in script-local items of script "sid".
2375 * Returns the index in "sn_var_vals" if found.
2376 * If found but not in "sn_var_vals" returns -1.
2377 * If not found returns -2.
2378 */
2379 int
2380get_script_item_idx(int sid, char_u *name, int check_writable)
2381{
2382 hashtab_T *ht;
2383 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002384 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 int idx;
2386
2387 // First look the name up in the hashtable.
2388 if (sid <= 0 || sid > script_items.ga_len)
2389 return -1;
2390 ht = &SCRIPT_VARS(sid);
2391 di = find_var_in_ht(ht, 0, name, TRUE);
2392 if (di == NULL)
2393 return -2;
2394
2395 // Now find the svar_T index in sn_var_vals.
2396 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2397 {
2398 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2399
2400 if (sv->sv_tv == &di->di_tv)
2401 {
2402 if (check_writable && sv->sv_const)
2403 semsg(_(e_readonlyvar), name);
2404 return idx;
2405 }
2406 }
2407 return -1;
2408}
2409
2410/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002411 * Find "name" in imported items of the current script or in "cctx" if not
2412 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 */
2414 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002415find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002417 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 int idx;
2419
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002420 if (current_sctx.sc_sid <= 0)
2421 return NULL;
2422 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 if (cctx != NULL)
2424 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2425 {
2426 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2427 + idx;
2428
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002429 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2430 : STRLEN(import->imp_name) == len
2431 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 return import;
2433 }
2434
2435 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2436 {
2437 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2438
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002439 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2440 : STRLEN(import->imp_name) == len
2441 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 return import;
2443 }
2444 return NULL;
2445}
2446
2447/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002448 * Free all imported variables.
2449 */
2450 static void
2451free_imported(cctx_T *cctx)
2452{
2453 int idx;
2454
2455 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2456 {
2457 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2458
2459 vim_free(import->imp_name);
2460 }
2461 ga_clear(&cctx->ctx_imports);
2462}
2463
2464/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002465 * Return TRUE if "p" points at a "#" but not at "#{".
2466 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002467 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002468vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002469{
2470 return p[0] == '#' && p[1] != '{';
2471}
2472
2473/*
2474 * Return a pointer to the next line that isn't empty or only contains a
2475 * comment. Skips over white space.
2476 * Returns NULL if there is none.
2477 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002478 char_u *
2479peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002480{
2481 int lnum = cctx->ctx_lnum;
2482
2483 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2484 {
2485 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002486 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002487
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002488 if (line == NULL)
2489 break;
2490 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002491 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002492 return p;
2493 }
2494 return NULL;
2495}
2496
2497/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002498 * Called when checking for a following operator at "arg". When the rest of
2499 * the line is empty or only a comment, peek the next line. If there is a next
2500 * line return a pointer to it and set "nextp".
2501 * Otherwise skip over white space.
2502 */
2503 static char_u *
2504may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2505{
2506 char_u *p = skipwhite(arg);
2507
2508 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002509 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002510 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002511 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002512 if (*nextp != NULL)
2513 return *nextp;
2514 }
2515 return p;
2516}
2517
2518/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002519 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002520 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002521 * Returns NULL when at the end.
2522 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002523 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002524next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002525{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002526 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002527
2528 do
2529 {
2530 ++cctx->ctx_lnum;
2531 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002532 {
2533 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002534 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002535 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002536 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002537 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002538 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002539 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002540 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002541 return line;
2542}
2543
2544/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002545 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002546 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002547 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2548 */
2549 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002550may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002551{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002552 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002553 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002554 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002555
2556 if (next == NULL)
2557 return FAIL;
2558 *arg = skipwhite(next);
2559 }
2560 return OK;
2561}
2562
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002563/*
2564 * Idem, and give an error when failed.
2565 */
2566 static int
2567may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2568{
2569 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2570 {
2571 emsg(_("E1097: line incomplete"));
2572 return FAIL;
2573 }
2574 return OK;
2575}
2576
2577
Bram Moolenaara5565e42020-05-09 15:44:01 +02002578// Structure passed between the compile_expr* functions to keep track of
2579// constants that have been parsed but for which no code was produced yet. If
2580// possible expressions on these constants are applied at compile time. If
2581// that is not possible, the code to push the constants needs to be generated
2582// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002583// Using 50 should be more than enough of 5 levels of ().
2584#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002585typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002586 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002587 int pp_used; // active entries in pp_tv[]
2588} ppconst_T;
2589
Bram Moolenaar1c747212020-05-09 18:28:34 +02002590static int compile_expr0(char_u **arg, cctx_T *cctx);
2591static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2592
Bram Moolenaara5565e42020-05-09 15:44:01 +02002593/*
2594 * Generate a PUSH instruction for "tv".
2595 * "tv" will be consumed or cleared.
2596 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2597 */
2598 static int
2599generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2600{
2601 if (tv != NULL)
2602 {
2603 switch (tv->v_type)
2604 {
2605 case VAR_UNKNOWN:
2606 break;
2607 case VAR_BOOL:
2608 generate_PUSHBOOL(cctx, tv->vval.v_number);
2609 break;
2610 case VAR_SPECIAL:
2611 generate_PUSHSPEC(cctx, tv->vval.v_number);
2612 break;
2613 case VAR_NUMBER:
2614 generate_PUSHNR(cctx, tv->vval.v_number);
2615 break;
2616#ifdef FEAT_FLOAT
2617 case VAR_FLOAT:
2618 generate_PUSHF(cctx, tv->vval.v_float);
2619 break;
2620#endif
2621 case VAR_BLOB:
2622 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2623 tv->vval.v_blob = NULL;
2624 break;
2625 case VAR_STRING:
2626 generate_PUSHS(cctx, tv->vval.v_string);
2627 tv->vval.v_string = NULL;
2628 break;
2629 default:
2630 iemsg("constant type not supported");
2631 clear_tv(tv);
2632 return FAIL;
2633 }
2634 tv->v_type = VAR_UNKNOWN;
2635 }
2636 return OK;
2637}
2638
2639/*
2640 * Generate code for any ppconst entries.
2641 */
2642 static int
2643generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2644{
2645 int i;
2646 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002647 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002648
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002649 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002650 for (i = 0; i < ppconst->pp_used; ++i)
2651 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2652 ret = FAIL;
2653 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002654 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002655 return ret;
2656}
2657
2658/*
2659 * Clear ppconst constants. Used when failing.
2660 */
2661 static void
2662clear_ppconst(ppconst_T *ppconst)
2663{
2664 int i;
2665
2666 for (i = 0; i < ppconst->pp_used; ++i)
2667 clear_tv(&ppconst->pp_tv[i]);
2668 ppconst->pp_used = 0;
2669}
2670
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002671/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002672 * Generate an instruction to load script-local variable "name", without the
2673 * leading "s:".
2674 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 */
2676 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002677compile_load_scriptvar(
2678 cctx_T *cctx,
2679 char_u *name, // variable NUL terminated
2680 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002681 char_u **end, // end of variable
2682 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002684 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2686 imported_T *import;
2687
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002688 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002690 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002691 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2692 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 }
2694 if (idx >= 0)
2695 {
2696 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2697
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002698 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 current_sctx.sc_sid, idx, sv->sv_type);
2700 return OK;
2701 }
2702
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002703 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 if (import != NULL)
2705 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002706 if (import->imp_all)
2707 {
2708 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002709 char_u *exp_name;
2710 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002711 ufunc_T *ufunc;
2712 type_T *type;
2713
2714 // Used "import * as Name", need to lookup the member.
2715 if (*p != '.')
2716 {
2717 semsg(_("E1060: expected dot after name: %s"), start);
2718 return FAIL;
2719 }
2720 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002721 if (VIM_ISWHITE(*p))
2722 {
2723 emsg(_("E1074: no white space allowed after dot"));
2724 return FAIL;
2725 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002726
Bram Moolenaar1c991142020-07-04 13:15:31 +02002727 // isolate one name
2728 exp_name = p;
2729 while (eval_isnamec(*p))
2730 ++p;
2731 cc = *p;
2732 *p = NUL;
2733
2734 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2735 *p = cc;
2736 p = skipwhite(p);
2737
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002738 // TODO: what if it is a function?
2739 if (idx < 0)
2740 return FAIL;
2741 *end = p;
2742
2743 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2744 import->imp_sid,
2745 idx,
2746 type);
2747 }
2748 else
2749 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002750 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002751 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2752 import->imp_sid,
2753 import->imp_var_vals_idx,
2754 import->imp_type);
2755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 return OK;
2757 }
2758
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002759 if (error)
2760 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 return FAIL;
2762}
2763
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002764 static int
2765generate_funcref(cctx_T *cctx, char_u *name)
2766{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002767 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002768
2769 if (ufunc == NULL)
2770 return FAIL;
2771
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002772 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2773 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002774}
2775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776/*
2777 * Compile a variable name into a load instruction.
2778 * "end" points to just after the name.
2779 * When "error" is FALSE do not give an error when not found.
2780 */
2781 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002782compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783{
2784 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002785 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002786 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002788 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789
2790 if (*(*arg + 1) == ':')
2791 {
2792 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002793 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002794 {
2795 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002797 switch (**arg)
2798 {
2799 case 'g': isn_type = ISN_LOADGDICT; break;
2800 case 'w': isn_type = ISN_LOADWDICT; break;
2801 case 't': isn_type = ISN_LOADTDICT; break;
2802 case 'b': isn_type = ISN_LOADBDICT; break;
2803 default:
2804 semsg(_(e_namespace), *arg);
2805 goto theend;
2806 }
2807 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2808 goto theend;
2809 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 else
2812 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002813 isntype_T isn_type = ISN_DROP;
2814
2815 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2816 if (name == NULL)
2817 return FAIL;
2818
2819 switch (**arg)
2820 {
2821 case 'v': res = generate_LOADV(cctx, name, error);
2822 break;
2823 case 's': res = compile_load_scriptvar(cctx, name,
2824 NULL, NULL, error);
2825 break;
2826 case 'g': isn_type = ISN_LOADG; break;
2827 case 'w': isn_type = ISN_LOADW; break;
2828 case 't': isn_type = ISN_LOADT; break;
2829 case 'b': isn_type = ISN_LOADB; break;
2830 default: semsg(_(e_namespace), *arg);
2831 goto theend;
2832 }
2833 if (isn_type != ISN_DROP)
2834 {
2835 // Global, Buffer-local, Window-local and Tabpage-local
2836 // variables can be defined later, thus we don't check if it
2837 // exists, give error at runtime.
2838 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 }
2841 }
2842 else
2843 {
2844 size_t len = end - *arg;
2845 int idx;
2846 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002847 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848
2849 name = vim_strnsave(*arg, end - *arg);
2850 if (name == NULL)
2851 return FAIL;
2852
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002853 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002855 if (!gen_load_outer)
2856 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 }
2858 else
2859 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002860 lvar_T *lvar = lookup_local(*arg, len, cctx);
2861
2862 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002864 type = lvar->lv_type;
2865 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002866 if (lvar->lv_from_outer)
2867 gen_load_outer = TRUE;
2868 else
2869 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 }
2871 else
2872 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002873 // "var" can be script-local even without using "s:" if it
2874 // already exists.
2875 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2876 == SCRIPT_VERSION_VIM9
2877 || lookup_script(*arg, len) == OK)
2878 res = compile_load_scriptvar(cctx, name, *arg, &end,
2879 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002880
Bram Moolenaara5565e42020-05-09 15:44:01 +02002881 // When the name starts with an uppercase letter or "x:" it
2882 // can be a user defined function.
2883 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2884 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 }
2886 }
2887 if (gen_load)
2888 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002889 if (gen_load_outer)
2890 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 }
2892
2893 *arg = end;
2894
2895theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002896 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 semsg(_(e_var_notfound), name);
2898 vim_free(name);
2899 return res;
2900}
2901
2902/*
2903 * Compile the argument expressions.
2904 * "arg" points to just after the "(" and is advanced to after the ")"
2905 */
2906 static int
2907compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2908{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002909 char_u *p = *arg;
2910 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911
Bram Moolenaare6085c52020-04-12 20:19:16 +02002912 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002914 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2915 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002916 if (*p == ')')
2917 {
2918 *arg = p + 1;
2919 return OK;
2920 }
2921
Bram Moolenaara5565e42020-05-09 15:44:01 +02002922 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 return FAIL;
2924 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002925
2926 if (*p != ',' && *skipwhite(p) == ',')
2927 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002928 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002929 p = skipwhite(p);
2930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002932 {
2933 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002934 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002935 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002936 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002937 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002938 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002940failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002941 emsg(_(e_missing_close));
2942 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943}
2944
2945/*
2946 * Compile a function call: name(arg1, arg2)
2947 * "arg" points to "name", "arg + varlen" to the "(".
2948 * "argcount_init" is 1 for "value->method()"
2949 * Instructions:
2950 * EVAL arg1
2951 * EVAL arg2
2952 * BCALL / DCALL / UCALL
2953 */
2954 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002955compile_call(
2956 char_u **arg,
2957 size_t varlen,
2958 cctx_T *cctx,
2959 ppconst_T *ppconst,
2960 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961{
2962 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002963 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 int argcount = argcount_init;
2965 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002966 char_u fname_buf[FLEN_FIXED + 1];
2967 char_u *tofree = NULL;
2968 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002970 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971
Bram Moolenaara5565e42020-05-09 15:44:01 +02002972 // we can evaluate "has('name')" at compile time
2973 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2974 {
2975 char_u *s = skipwhite(*arg + varlen + 1);
2976 typval_T argvars[2];
2977
2978 argvars[0].v_type = VAR_UNKNOWN;
2979 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002980 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002981 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002982 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002983 s = skipwhite(s);
2984 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2985 {
2986 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2987
2988 *arg = s + 1;
2989 argvars[1].v_type = VAR_UNKNOWN;
2990 tv->v_type = VAR_NUMBER;
2991 tv->vval.v_number = 0;
2992 f_has(argvars, tv);
2993 clear_tv(&argvars[0]);
2994 ++ppconst->pp_used;
2995 return OK;
2996 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002997 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002998 }
2999
3000 if (generate_ppconst(cctx, ppconst) == FAIL)
3001 return FAIL;
3002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 if (varlen >= sizeof(namebuf))
3004 {
3005 semsg(_("E1011: name too long: %s"), name);
3006 return FAIL;
3007 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003008 vim_strncpy(namebuf, *arg, varlen);
3009 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010
3011 *arg = skipwhite(*arg + varlen + 1);
3012 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003013 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003015 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 {
3017 int idx;
3018
3019 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003020 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003022 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003023 else
3024 semsg(_(e_unknownfunc), namebuf);
3025 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 }
3027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003029 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003031 {
3032 res = generate_CALL(cctx, ufunc, argcount);
3033 goto theend;
3034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035
3036 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003037 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003039 if (STRNCMP(namebuf, "g:", 2) != 0
3040 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003041 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003042 garray_T *stack = &cctx->ctx_type_stack;
3043 type_T *type;
3044
3045 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3046 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003047 goto theend;
3048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003050 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003051 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003052 if (STRNCMP(namebuf, "g:", 2) == 0)
3053 res = generate_UCALL(cctx, name, argcount);
3054 else
3055 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003056
3057theend:
3058 vim_free(tofree);
3059 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060}
3061
3062// like NAMESPACE_CHAR but with 'a' and 'l'.
3063#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3064
3065/*
3066 * Find the end of a variable or function name. Unlike find_name_end() this
3067 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003068 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 * Return a pointer to just after the name. Equal to "arg" if there is no
3070 * valid name.
3071 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003072 static char_u *
3073to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074{
3075 char_u *p;
3076
3077 // Quick check for valid starting character.
3078 if (!eval_isnamec1(*arg))
3079 return arg;
3080
3081 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3082 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3083 // and can be used in slice "[n:]".
3084 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003085 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3087 break;
3088 return p;
3089}
3090
3091/*
3092 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003093 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 */
3095 char_u *
3096to_name_const_end(char_u *arg)
3097{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003098 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 typval_T rettv;
3100
3101 if (p == arg && *arg == '[')
3102 {
3103
3104 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003105 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 p = arg;
3107 }
3108 else if (p == arg && *arg == '#' && arg[1] == '{')
3109 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003110 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003112 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 p = arg;
3114 }
3115 else if (p == arg && *arg == '{')
3116 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003117 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003119 // Can be "{x -> ret}()".
3120 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003122 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 if (ret != OK)
3124 p = arg;
3125 }
3126
3127 return p;
3128}
3129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130/*
3131 * parse a list: [expr, expr]
3132 * "*arg" points to the '['.
3133 */
3134 static int
3135compile_list(char_u **arg, cctx_T *cctx)
3136{
3137 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003138 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 int count = 0;
3140
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003141 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003143 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003144 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003145 semsg(_(e_list_end), *arg);
3146 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003147 }
3148 if (*p == ']')
3149 {
3150 ++p;
3151 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003152 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003153 p += STRLEN(p);
3154 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003155 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003156 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 break;
3158 ++count;
3159 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003160 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003162 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3163 {
3164 semsg(_(e_white_after), ",");
3165 return FAIL;
3166 }
3167 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003168 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 p = skipwhite(p);
3170 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003171 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172
3173 generate_NEWLIST(cctx, count);
3174 return OK;
3175}
3176
3177/*
3178 * parse a lambda: {arg, arg -> expr}
3179 * "*arg" points to the '{'.
3180 */
3181 static int
3182compile_lambda(char_u **arg, cctx_T *cctx)
3183{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 typval_T rettv;
3185 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003186 evalarg_T evalarg;
3187
3188 CLEAR_FIELD(evalarg);
3189 evalarg.eval_flags = EVAL_EVALUATE;
3190 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191
3192 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003193 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003197 ++ufunc->uf_refcount;
3198 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003199 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200
3201 // The function will have one line: "return {expr}".
3202 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003203 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003205 clear_evalarg(&evalarg, NULL);
3206
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003207 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003208 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003209
3210 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 return FAIL;
3212}
3213
3214/*
3215 * Compile a lamda call: expr->{lambda}(args)
3216 * "arg" points to the "{".
3217 */
3218 static int
3219compile_lambda_call(char_u **arg, cctx_T *cctx)
3220{
3221 ufunc_T *ufunc;
3222 typval_T rettv;
3223 int argcount = 1;
3224 int ret = FAIL;
3225
3226 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003227 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 return FAIL;
3229
3230 if (**arg != '(')
3231 {
3232 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003233 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 else
3235 semsg(_(e_missing_paren), "lambda");
3236 clear_tv(&rettv);
3237 return FAIL;
3238 }
3239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 ufunc = rettv.vval.v_partial->pt_func;
3241 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003242 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003243 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003244
3245 // The function will have one line: "return {expr}".
3246 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003247 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248
3249 // compile the arguments
3250 *arg = skipwhite(*arg + 1);
3251 if (compile_arguments(arg, cctx, &argcount) == OK)
3252 // call the compiled function
3253 ret = generate_CALL(cctx, ufunc, argcount);
3254
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003255 if (ret == FAIL)
3256 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 return ret;
3258}
3259
3260/*
3261 * parse a dict: {'key': val} or #{key: val}
3262 * "*arg" points to the '{'.
3263 */
3264 static int
3265compile_dict(char_u **arg, cctx_T *cctx, int literal)
3266{
3267 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003268 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 int count = 0;
3270 dict_T *d = dict_alloc();
3271 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003272 char_u *whitep = *arg;
3273 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274
3275 if (d == NULL)
3276 return FAIL;
3277 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003278 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 {
3280 char_u *key = NULL;
3281
Bram Moolenaar23c55272020-06-21 16:58:13 +02003282 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003283 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003284 *arg = NULL;
3285 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003286 }
3287
3288 if (**arg == '}')
3289 break;
3290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 if (literal)
3292 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003293 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294
Bram Moolenaar2c330432020-04-13 14:41:35 +02003295 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 {
3297 semsg(_("E1014: Invalid key: %s"), *arg);
3298 return FAIL;
3299 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003300 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 if (generate_PUSHS(cctx, key) == FAIL)
3302 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003303 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 }
3305 else
3306 {
3307 isn_T *isn;
3308
Bram Moolenaara5565e42020-05-09 15:44:01 +02003309 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3312 if (isn->isn_type == ISN_PUSHS)
3313 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003314 else
3315 {
3316 type_T *keytype = ((type_T **)stack->ga_data)
3317 [stack->ga_len - 1];
3318 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3319 return FAIL;
3320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 }
3322
3323 // Check for duplicate keys, if using string keys.
3324 if (key != NULL)
3325 {
3326 item = dict_find(d, key, -1);
3327 if (item != NULL)
3328 {
3329 semsg(_(e_duplicate_key), key);
3330 goto failret;
3331 }
3332 item = dictitem_alloc(key);
3333 if (item != NULL)
3334 {
3335 item->di_tv.v_type = VAR_UNKNOWN;
3336 item->di_tv.v_lock = 0;
3337 if (dict_add(d, item) == FAIL)
3338 dictitem_free(item);
3339 }
3340 }
3341
3342 *arg = skipwhite(*arg);
3343 if (**arg != ':')
3344 {
3345 semsg(_(e_missing_dict_colon), *arg);
3346 return FAIL;
3347 }
3348
Bram Moolenaar2c330432020-04-13 14:41:35 +02003349 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003351 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003352 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003353 *arg = NULL;
3354 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003355 }
3356
Bram Moolenaara5565e42020-05-09 15:44:01 +02003357 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 return FAIL;
3359 ++count;
3360
Bram Moolenaar2c330432020-04-13 14:41:35 +02003361 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003362 *arg = skipwhite(*arg);
3363 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003364 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003365 *arg = NULL;
3366 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 if (**arg == '}')
3369 break;
3370 if (**arg != ',')
3371 {
3372 semsg(_(e_missing_dict_comma), *arg);
3373 goto failret;
3374 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003375 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 *arg = skipwhite(*arg + 1);
3377 }
3378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 *arg = *arg + 1;
3380
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003381 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003382 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003383 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003384 *arg += STRLEN(*arg);
3385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 dict_unref(d);
3387 return generate_NEWDICT(cctx, count);
3388
3389failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003390 if (*arg == NULL)
3391 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 dict_unref(d);
3393 return FAIL;
3394}
3395
3396/*
3397 * Compile "&option".
3398 */
3399 static int
3400compile_get_option(char_u **arg, cctx_T *cctx)
3401{
3402 typval_T rettv;
3403 char_u *start = *arg;
3404 int ret;
3405
3406 // parse the option and get the current value to get the type.
3407 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003408 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 if (ret == OK)
3410 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003411 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 char_u *name = vim_strnsave(start, *arg - start);
3413 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3414
3415 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3416 vim_free(name);
3417 }
3418 clear_tv(&rettv);
3419
3420 return ret;
3421}
3422
3423/*
3424 * Compile "$VAR".
3425 */
3426 static int
3427compile_get_env(char_u **arg, cctx_T *cctx)
3428{
3429 char_u *start = *arg;
3430 int len;
3431 int ret;
3432 char_u *name;
3433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 ++*arg;
3435 len = get_env_len(arg);
3436 if (len == 0)
3437 {
3438 semsg(_(e_syntax_at), start - 1);
3439 return FAIL;
3440 }
3441
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003442 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 name = vim_strnsave(start, len + 1);
3444 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3445 vim_free(name);
3446 return ret;
3447}
3448
3449/*
3450 * Compile "@r".
3451 */
3452 static int
3453compile_get_register(char_u **arg, cctx_T *cctx)
3454{
3455 int ret;
3456
3457 ++*arg;
3458 if (**arg == NUL)
3459 {
3460 semsg(_(e_syntax_at), *arg - 1);
3461 return FAIL;
3462 }
3463 if (!valid_yank_reg(**arg, TRUE))
3464 {
3465 emsg_invreg(**arg);
3466 return FAIL;
3467 }
3468 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3469 ++*arg;
3470 return ret;
3471}
3472
3473/*
3474 * Apply leading '!', '-' and '+' to constant "rettv".
3475 */
3476 static int
3477apply_leader(typval_T *rettv, char_u *start, char_u *end)
3478{
3479 char_u *p = end;
3480
3481 // this works from end to start
3482 while (p > start)
3483 {
3484 --p;
3485 if (*p == '-' || *p == '+')
3486 {
3487 // only '-' has an effect, for '+' we only check the type
3488#ifdef FEAT_FLOAT
3489 if (rettv->v_type == VAR_FLOAT)
3490 {
3491 if (*p == '-')
3492 rettv->vval.v_float = -rettv->vval.v_float;
3493 }
3494 else
3495#endif
3496 {
3497 varnumber_T val;
3498 int error = FALSE;
3499
3500 // tv_get_number_chk() accepts a string, but we don't want that
3501 // here
3502 if (check_not_string(rettv) == FAIL)
3503 return FAIL;
3504 val = tv_get_number_chk(rettv, &error);
3505 clear_tv(rettv);
3506 if (error)
3507 return FAIL;
3508 if (*p == '-')
3509 val = -val;
3510 rettv->v_type = VAR_NUMBER;
3511 rettv->vval.v_number = val;
3512 }
3513 }
3514 else
3515 {
3516 int v = tv2bool(rettv);
3517
3518 // '!' is permissive in the type.
3519 clear_tv(rettv);
3520 rettv->v_type = VAR_BOOL;
3521 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3522 }
3523 }
3524 return OK;
3525}
3526
3527/*
3528 * Recognize v: variables that are constants and set "rettv".
3529 */
3530 static void
3531get_vim_constant(char_u **arg, typval_T *rettv)
3532{
3533 if (STRNCMP(*arg, "v:true", 6) == 0)
3534 {
3535 rettv->v_type = VAR_BOOL;
3536 rettv->vval.v_number = VVAL_TRUE;
3537 *arg += 6;
3538 }
3539 else if (STRNCMP(*arg, "v:false", 7) == 0)
3540 {
3541 rettv->v_type = VAR_BOOL;
3542 rettv->vval.v_number = VVAL_FALSE;
3543 *arg += 7;
3544 }
3545 else if (STRNCMP(*arg, "v:null", 6) == 0)
3546 {
3547 rettv->v_type = VAR_SPECIAL;
3548 rettv->vval.v_number = VVAL_NULL;
3549 *arg += 6;
3550 }
3551 else if (STRNCMP(*arg, "v:none", 6) == 0)
3552 {
3553 rettv->v_type = VAR_SPECIAL;
3554 rettv->vval.v_number = VVAL_NONE;
3555 *arg += 6;
3556 }
3557}
3558
Bram Moolenaar61a89812020-05-07 16:58:17 +02003559 static exptype_T
3560get_compare_type(char_u *p, int *len, int *type_is)
3561{
3562 exptype_T type = EXPR_UNKNOWN;
3563 int i;
3564
3565 switch (p[0])
3566 {
3567 case '=': if (p[1] == '=')
3568 type = EXPR_EQUAL;
3569 else if (p[1] == '~')
3570 type = EXPR_MATCH;
3571 break;
3572 case '!': if (p[1] == '=')
3573 type = EXPR_NEQUAL;
3574 else if (p[1] == '~')
3575 type = EXPR_NOMATCH;
3576 break;
3577 case '>': if (p[1] != '=')
3578 {
3579 type = EXPR_GREATER;
3580 *len = 1;
3581 }
3582 else
3583 type = EXPR_GEQUAL;
3584 break;
3585 case '<': if (p[1] != '=')
3586 {
3587 type = EXPR_SMALLER;
3588 *len = 1;
3589 }
3590 else
3591 type = EXPR_SEQUAL;
3592 break;
3593 case 'i': if (p[1] == 's')
3594 {
3595 // "is" and "isnot"; but not a prefix of a name
3596 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3597 *len = 5;
3598 i = p[*len];
3599 if (!isalnum(i) && i != '_')
3600 {
3601 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3602 *type_is = TRUE;
3603 }
3604 }
3605 break;
3606 }
3607 return type;
3608}
3609
3610/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 * Compile code to apply '-', '+' and '!'.
3612 */
3613 static int
3614compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3615{
3616 char_u *p = end;
3617
3618 // this works from end to start
3619 while (p > start)
3620 {
3621 --p;
3622 if (*p == '-' || *p == '+')
3623 {
3624 int negate = *p == '-';
3625 isn_T *isn;
3626
3627 // TODO: check type
3628 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3629 {
3630 --p;
3631 if (*p == '-')
3632 negate = !negate;
3633 }
3634 // only '-' has an effect, for '+' we only check the type
3635 if (negate)
3636 isn = generate_instr(cctx, ISN_NEGATENR);
3637 else
3638 isn = generate_instr(cctx, ISN_CHECKNR);
3639 if (isn == NULL)
3640 return FAIL;
3641 }
3642 else
3643 {
3644 int invert = TRUE;
3645
3646 while (p > start && p[-1] == '!')
3647 {
3648 --p;
3649 invert = !invert;
3650 }
3651 if (generate_2BOOL(cctx, invert) == FAIL)
3652 return FAIL;
3653 }
3654 }
3655 return OK;
3656}
3657
3658/*
3659 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003660 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 */
3662 static int
3663compile_subscript(
3664 char_u **arg,
3665 cctx_T *cctx,
3666 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003667 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003668 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669{
3670 for (;;)
3671 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003672 char_u *p = skipwhite(*arg);
3673
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003674 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003675 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003676 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003677
3678 // If a following line starts with "->{" or "->X" advance to that
3679 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003680 // Also if a following line starts with ".x".
3681 if (next != NULL &&
3682 ((next[0] == '-' && next[1] == '>'
3683 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3684 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003685 {
3686 next = next_line_from_context(cctx, TRUE);
3687 if (next == NULL)
3688 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003689 *arg = next;
3690 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003691 }
3692 }
3693
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003694 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003696 garray_T *stack = &cctx->ctx_type_stack;
3697 type_T *type;
3698 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003700 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003701 return FAIL;
3702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003704 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3705
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003706 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3708 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003709 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 return FAIL;
3711 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003712 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003714 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003715 return FAIL;
3716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 // something->method()
3718 // Apply the '!', '-' and '+' first:
3719 // -1.0->func() works like (-1.0)->func()
3720 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3721 return FAIL;
3722 *start_leader = end_leader; // don't apply again later
3723
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003724 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003725 *arg = skipwhite(p);
3726 if (may_get_next_line(p, arg, cctx) == FAIL)
3727 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 if (**arg == '{')
3729 {
3730 // lambda call: list->{lambda}
3731 if (compile_lambda_call(arg, cctx) == FAIL)
3732 return FAIL;
3733 }
3734 else
3735 {
3736 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003737 p = *arg;
3738 if (ASCII_ISALPHA(*p) && p[1] == ':')
3739 p += 2;
3740 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 ;
3742 if (*p != '(')
3743 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003744 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 return FAIL;
3746 }
3747 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003748 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 return FAIL;
3750 }
3751 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003752 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003754 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003755 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003756 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003757
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003758 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003759 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003760 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003761 // TODO: blob index
3762 // TODO: more arguments
3763 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003764 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003765 return FAIL;
3766
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003767 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003768 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003769 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003770 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003771 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 return FAIL;
3773
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003774 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3775 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 if (**arg != ']')
3777 {
3778 emsg(_(e_missbrac));
3779 return FAIL;
3780 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003781 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003783 // We can index a list and a dict. If we don't know the type
3784 // we can use the index value type.
3785 // TODO: If we don't know use an instruction to figure it out at
3786 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003787 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003788 vtype = (*typep)->tt_type;
3789 if (*typep == &t_any)
3790 {
3791 type_T *valtype = ((type_T **)stack->ga_data)
3792 [stack->ga_len - 1];
3793 if (valtype == &t_string)
3794 vtype = VAR_DICT;
3795 }
3796 if (vtype == VAR_DICT)
3797 {
3798 if ((*typep)->tt_type == VAR_DICT)
3799 *typep = (*typep)->tt_member;
3800 else if (need_type(*typep, &t_dict_any, -2, cctx, FALSE)
3801 == FAIL)
3802 return FAIL;
3803 if (may_generate_2STRING(-1, cctx) == FAIL)
3804 return FAIL;
3805 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3806 return FAIL;
3807 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003808 else if (vtype == VAR_STRING)
3809 {
3810 *typep = &t_number;
3811 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3812 return FAIL;
3813 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003814 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003815 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003816 if ((*typep)->tt_type == VAR_LIST)
3817 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003818 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003819 return FAIL;
3820 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003821 else
3822 {
3823 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003824 return FAIL;
3825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003827 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003829 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003830 return FAIL;
3831
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003832 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003833 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3834 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003836 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 if (eval_isnamec1(*p))
3838 while (eval_isnamec(*p))
3839 MB_PTR_ADV(p);
3840 if (p == *arg)
3841 {
3842 semsg(_(e_syntax_at), *arg);
3843 return FAIL;
3844 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003845 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 return FAIL;
3847 *arg = p;
3848 }
3849 else
3850 break;
3851 }
3852
3853 // TODO - see handle_subscript():
3854 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3855 // Don't do this when "Func" is already a partial that was bound
3856 // explicitly (pt_auto is FALSE).
3857
3858 return OK;
3859}
3860
3861/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003862 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3863 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003865 * If the value is a constant "ppconst->pp_ret" will be set.
3866 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003867 *
3868 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 */
3870
3871/*
3872 * number number constant
3873 * 0zFFFFFFFF Blob constant
3874 * "string" string constant
3875 * 'string' literal string constant
3876 * &option-name option value
3877 * @r register contents
3878 * identifier variable value
3879 * function() function call
3880 * $VAR environment variable
3881 * (expression) nested expression
3882 * [expr, expr] List
3883 * {key: val, key: val} Dictionary
3884 * #{key: val, key: val} Dictionary with literal keys
3885 *
3886 * Also handle:
3887 * ! in front logical NOT
3888 * - in front unary minus
3889 * + in front unary plus (ignored)
3890 * trailing (arg) funcref/partial call
3891 * trailing [] subscript in String or List
3892 * trailing .name entry in Dictionary
3893 * trailing ->name() method call
3894 */
3895 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003896compile_expr7(
3897 char_u **arg,
3898 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003899 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 char_u *start_leader, *end_leader;
3902 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003903 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003904 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905
3906 /*
3907 * Skip '!', '-' and '+' characters. They are handled later.
3908 */
3909 start_leader = *arg;
3910 while (**arg == '!' || **arg == '-' || **arg == '+')
3911 *arg = skipwhite(*arg + 1);
3912 end_leader = *arg;
3913
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003914 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 switch (**arg)
3916 {
3917 /*
3918 * Number constant.
3919 */
3920 case '0': // also for blob starting with 0z
3921 case '1':
3922 case '2':
3923 case '3':
3924 case '4':
3925 case '5':
3926 case '6':
3927 case '7':
3928 case '8':
3929 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003930 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 return FAIL;
3932 break;
3933
3934 /*
3935 * String constant: "string".
3936 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003937 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 return FAIL;
3939 break;
3940
3941 /*
3942 * Literal string constant: 'str''ing'.
3943 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003944 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 return FAIL;
3946 break;
3947
3948 /*
3949 * Constant Vim variable.
3950 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003951 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 ret = NOTDONE;
3953 break;
3954
3955 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003956 * "true" constant
3957 */
3958 case 't': if (STRNCMP(*arg, "true", 4) == 0
3959 && !eval_isnamec((*arg)[4]))
3960 {
3961 *arg += 4;
3962 rettv->v_type = VAR_BOOL;
3963 rettv->vval.v_number = VVAL_TRUE;
3964 }
3965 else
3966 ret = NOTDONE;
3967 break;
3968
3969 /*
3970 * "false" constant
3971 */
3972 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3973 && !eval_isnamec((*arg)[5]))
3974 {
3975 *arg += 5;
3976 rettv->v_type = VAR_BOOL;
3977 rettv->vval.v_number = VVAL_FALSE;
3978 }
3979 else
3980 ret = NOTDONE;
3981 break;
3982
3983 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 * List: [expr, expr]
3985 */
3986 case '[': ret = compile_list(arg, cctx);
3987 break;
3988
3989 /*
3990 * Dictionary: #{key: val, key: val}
3991 */
3992 case '#': if ((*arg)[1] == '{')
3993 {
3994 ++*arg;
3995 ret = compile_dict(arg, cctx, TRUE);
3996 }
3997 else
3998 ret = NOTDONE;
3999 break;
4000
4001 /*
4002 * Lambda: {arg, arg -> expr}
4003 * Dictionary: {'key': val, 'key': val}
4004 */
4005 case '{': {
4006 char_u *start = skipwhite(*arg + 1);
4007
4008 // Find out what comes after the arguments.
4009 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004010 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 if (ret != FAIL && *start == '>')
4012 ret = compile_lambda(arg, cctx);
4013 else
4014 ret = compile_dict(arg, cctx, FALSE);
4015 }
4016 break;
4017
4018 /*
4019 * Option value: &name
4020 */
4021 case '&': ret = compile_get_option(arg, cctx);
4022 break;
4023
4024 /*
4025 * Environment variable: $VAR.
4026 */
4027 case '$': ret = compile_get_env(arg, cctx);
4028 break;
4029
4030 /*
4031 * Register contents: @r.
4032 */
4033 case '@': ret = compile_get_register(arg, cctx);
4034 break;
4035 /*
4036 * nested expression: (expression).
4037 */
4038 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004039
4040 // recursive!
4041 if (ppconst->pp_used <= PPSIZE - 10)
4042 {
4043 ret = compile_expr1(arg, cctx, ppconst);
4044 }
4045 else
4046 {
4047 // Not enough space in ppconst, flush constants.
4048 if (generate_ppconst(cctx, ppconst) == FAIL)
4049 return FAIL;
4050 ret = compile_expr0(arg, cctx);
4051 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 *arg = skipwhite(*arg);
4053 if (**arg == ')')
4054 ++*arg;
4055 else if (ret == OK)
4056 {
4057 emsg(_(e_missing_close));
4058 ret = FAIL;
4059 }
4060 break;
4061
4062 default: ret = NOTDONE;
4063 break;
4064 }
4065 if (ret == FAIL)
4066 return FAIL;
4067
Bram Moolenaar1c747212020-05-09 18:28:34 +02004068 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 {
4070 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004071 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004073 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 return FAIL;
4075 }
4076 start_leader = end_leader; // don't apply again below
4077
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004078 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004079 clear_tv(rettv);
4080 else
4081 // A constant expression can possibly be handled compile time,
4082 // return the value instead of generating code.
4083 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 }
4085 else if (ret == NOTDONE)
4086 {
4087 char_u *p;
4088 int r;
4089
4090 if (!eval_isnamec1(**arg))
4091 {
4092 semsg(_("E1015: Name expected: %s"), *arg);
4093 return FAIL;
4094 }
4095
4096 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004097 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004099 {
4100 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4101 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004103 {
4104 if (generate_ppconst(cctx, ppconst) == FAIL)
4105 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 if (r == FAIL)
4109 return FAIL;
4110 }
4111
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004112 // Handle following "[]", ".member", etc.
4113 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004114 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004115 ppconst) == FAIL)
4116 return FAIL;
4117 if (ppconst->pp_used > 0)
4118 {
4119 // apply the '!', '-' and '+' before the constant
4120 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4121 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4122 return FAIL;
4123 return OK;
4124 }
4125 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004127 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128}
4129
4130/*
4131 * * number multiplication
4132 * / number division
4133 * % number modulo
4134 */
4135 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004136compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137{
4138 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004139 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004140 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004142 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004143 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 return FAIL;
4145
4146 /*
4147 * Repeat computing, until no "*", "/" or "%" is following.
4148 */
4149 for (;;)
4150 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004151 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 if (*op != '*' && *op != '/' && *op != '%')
4153 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004154 if (next != NULL)
4155 {
4156 *arg = next_line_from_context(cctx, TRUE);
4157 op = skipwhite(*arg);
4158 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004159
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004160 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 {
4162 char_u buf[3];
4163
4164 vim_strncpy(buf, op, 1);
4165 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004166 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 }
4168 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004169 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004170 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004172 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004173 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004175
4176 if (ppconst->pp_used == ppconst_used + 2
4177 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4178 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004179 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004180 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4181 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004182 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004184 // both are numbers: compute the result
4185 switch (*op)
4186 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004187 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004188 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004189 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004190 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004191 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004192 break;
4193 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004194 tv1->vval.v_number = res;
4195 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004196 }
4197 else
4198 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004199 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004200 generate_two_op(cctx, op);
4201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 }
4203
4204 return OK;
4205}
4206
4207/*
4208 * + number addition
4209 * - number subtraction
4210 * .. string concatenation
4211 */
4212 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004213compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214{
4215 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004216 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004218 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219
4220 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004221 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 return FAIL;
4223
4224 /*
4225 * Repeat computing, until no "+", "-" or ".." is following.
4226 */
4227 for (;;)
4228 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004229 op = may_peek_next_line(cctx, *arg, &next);
4230 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 break;
4232 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004233 if (next != NULL)
4234 {
4235 *arg = next_line_from_context(cctx, TRUE);
4236 op = skipwhite(*arg);
4237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004239 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 {
4241 char_u buf[3];
4242
4243 vim_strncpy(buf, op, oplen);
4244 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004245 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 }
4247
4248 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004249 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004250 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004252 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004253 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 return FAIL;
4255
Bram Moolenaara5565e42020-05-09 15:44:01 +02004256 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004257 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004258 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4259 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4260 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4261 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004263 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4264 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004265
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004266 // concat/subtract/add constant numbers
4267 if (*op == '+')
4268 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4269 else if (*op == '-')
4270 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4271 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004272 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004273 // concatenate constant strings
4274 char_u *s1 = tv1->vval.v_string;
4275 char_u *s2 = tv2->vval.v_string;
4276 size_t len1 = STRLEN(s1);
4277
4278 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4279 if (tv1->vval.v_string == NULL)
4280 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004281 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004282 return FAIL;
4283 }
4284 mch_memmove(tv1->vval.v_string, s1, len1);
4285 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004286 vim_free(s1);
4287 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004288 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004289 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 }
4291 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004292 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004293 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004294 if (*op == '.')
4295 {
4296 if (may_generate_2STRING(-2, cctx) == FAIL
4297 || may_generate_2STRING(-1, cctx) == FAIL)
4298 return FAIL;
4299 generate_instr_drop(cctx, ISN_CONCAT, 1);
4300 }
4301 else
4302 generate_two_op(cctx, op);
4303 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 }
4305
4306 return OK;
4307}
4308
4309/*
4310 * expr5a == expr5b
4311 * expr5a =~ expr5b
4312 * expr5a != expr5b
4313 * expr5a !~ expr5b
4314 * expr5a > expr5b
4315 * expr5a >= expr5b
4316 * expr5a < expr5b
4317 * expr5a <= expr5b
4318 * expr5a is expr5b
4319 * expr5a isnot expr5b
4320 *
4321 * Produces instructions:
4322 * EVAL expr5a Push result of "expr5a"
4323 * EVAL expr5b Push result of "expr5b"
4324 * COMPARE one of the compare instructions
4325 */
4326 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004327compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328{
4329 exptype_T type = EXPR_UNKNOWN;
4330 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004331 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004334 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335
4336 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004337 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 return FAIL;
4339
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004340 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004341 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342
4343 /*
4344 * If there is a comparative operator, use it.
4345 */
4346 if (type != EXPR_UNKNOWN)
4347 {
4348 int ic = FALSE; // Default: do not ignore case
4349
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004350 if (next != NULL)
4351 {
4352 *arg = next_line_from_context(cctx, TRUE);
4353 p = skipwhite(*arg);
4354 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 if (type_is && (p[len] == '?' || p[len] == '#'))
4356 {
4357 semsg(_(e_invexpr2), *arg);
4358 return FAIL;
4359 }
4360 // extra question mark appended: ignore case
4361 if (p[len] == '?')
4362 {
4363 ic = TRUE;
4364 ++len;
4365 }
4366 // extra '#' appended: match case (ignored)
4367 else if (p[len] == '#')
4368 ++len;
4369 // nothing appended: match case
4370
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004371 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 {
4373 char_u buf[7];
4374
4375 vim_strncpy(buf, p, len);
4376 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004377 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 }
4379
4380 // get the second variable
4381 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004382 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004383 return FAIL;
4384
Bram Moolenaara5565e42020-05-09 15:44:01 +02004385 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 return FAIL;
4387
Bram Moolenaara5565e42020-05-09 15:44:01 +02004388 if (ppconst->pp_used == ppconst_used + 2)
4389 {
4390 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4391 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4392 int ret;
4393
4394 // Both sides are a constant, compute the result now.
4395 // First check for a valid combination of types, this is more
4396 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004397 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004398 ret = FAIL;
4399 else
4400 {
4401 ret = typval_compare(tv1, tv2, type, ic);
4402 tv1->v_type = VAR_BOOL;
4403 tv1->vval.v_number = tv1->vval.v_number
4404 ? VVAL_TRUE : VVAL_FALSE;
4405 clear_tv(tv2);
4406 --ppconst->pp_used;
4407 }
4408 return ret;
4409 }
4410
4411 generate_ppconst(cctx, ppconst);
4412 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 }
4414
4415 return OK;
4416}
4417
Bram Moolenaar7f141552020-05-09 17:35:53 +02004418static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420/*
4421 * Compile || or &&.
4422 */
4423 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004424compile_and_or(
4425 char_u **arg,
4426 cctx_T *cctx,
4427 char *op,
4428 ppconst_T *ppconst,
4429 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004431 char_u *next;
4432 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 int opchar = *op;
4434
4435 if (p[0] == opchar && p[1] == opchar)
4436 {
4437 garray_T *instr = &cctx->ctx_instr;
4438 garray_T end_ga;
4439
4440 /*
4441 * Repeat until there is no following "||" or "&&"
4442 */
4443 ga_init2(&end_ga, sizeof(int), 10);
4444 while (p[0] == opchar && p[1] == opchar)
4445 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004446 if (next != NULL)
4447 {
4448 *arg = next_line_from_context(cctx, TRUE);
4449 p = skipwhite(*arg);
4450 }
4451
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004452 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4453 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004455 return FAIL;
4456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457
Bram Moolenaara5565e42020-05-09 15:44:01 +02004458 // TODO: use ppconst if the value is a constant
4459 generate_ppconst(cctx, ppconst);
4460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 if (ga_grow(&end_ga, 1) == FAIL)
4462 {
4463 ga_clear(&end_ga);
4464 return FAIL;
4465 }
4466 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4467 ++end_ga.ga_len;
4468 generate_JUMP(cctx, opchar == '|'
4469 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4470
4471 // eval the next expression
4472 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004473 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004474 return FAIL;
4475
Bram Moolenaara5565e42020-05-09 15:44:01 +02004476 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4477 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 {
4479 ga_clear(&end_ga);
4480 return FAIL;
4481 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004482
4483 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004485 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486
4487 // Fill in the end label in all jumps.
4488 while (end_ga.ga_len > 0)
4489 {
4490 isn_T *isn;
4491
4492 --end_ga.ga_len;
4493 isn = ((isn_T *)instr->ga_data)
4494 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4495 isn->isn_arg.jump.jump_where = instr->ga_len;
4496 }
4497 ga_clear(&end_ga);
4498 }
4499
4500 return OK;
4501}
4502
4503/*
4504 * expr4a && expr4a && expr4a logical AND
4505 *
4506 * Produces instructions:
4507 * EVAL expr4a Push result of "expr4a"
4508 * JUMP_AND_KEEP_IF_FALSE end
4509 * EVAL expr4b Push result of "expr4b"
4510 * JUMP_AND_KEEP_IF_FALSE end
4511 * EVAL expr4c Push result of "expr4c"
4512 * end:
4513 */
4514 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004515compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004517 int ppconst_used = ppconst->pp_used;
4518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004520 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 return FAIL;
4522
4523 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004524 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525}
4526
4527/*
4528 * expr3a || expr3b || expr3c logical OR
4529 *
4530 * Produces instructions:
4531 * EVAL expr3a Push result of "expr3a"
4532 * JUMP_AND_KEEP_IF_TRUE end
4533 * EVAL expr3b Push result of "expr3b"
4534 * JUMP_AND_KEEP_IF_TRUE end
4535 * EVAL expr3c Push result of "expr3c"
4536 * end:
4537 */
4538 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004539compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004541 int ppconst_used = ppconst->pp_used;
4542
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004544 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 return FAIL;
4546
4547 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004548 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549}
4550
4551/*
4552 * Toplevel expression: expr2 ? expr1a : expr1b
4553 *
4554 * Produces instructions:
4555 * EVAL expr2 Push result of "expr"
4556 * JUMP_IF_FALSE alt jump if false
4557 * EVAL expr1a
4558 * JUMP_ALWAYS end
4559 * alt: EVAL expr1b
4560 * end:
4561 */
4562 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004563compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564{
4565 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004566 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004567 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568
Bram Moolenaar61a89812020-05-07 16:58:17 +02004569 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004570 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 return FAIL;
4572
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004573 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 if (*p == '?')
4575 {
4576 garray_T *instr = &cctx->ctx_instr;
4577 garray_T *stack = &cctx->ctx_type_stack;
4578 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004579 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004581 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004583 int has_const_expr = FALSE;
4584 int const_value = FALSE;
4585 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004587 if (next != NULL)
4588 {
4589 *arg = next_line_from_context(cctx, TRUE);
4590 p = skipwhite(*arg);
4591 }
4592
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004593 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4594 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004596 return FAIL;
4597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598
Bram Moolenaara5565e42020-05-09 15:44:01 +02004599 if (ppconst->pp_used == ppconst_used + 1)
4600 {
4601 // the condition is a constant, we know whether the ? or the :
4602 // expression is to be evaluated.
4603 has_const_expr = TRUE;
4604 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4605 clear_tv(&ppconst->pp_tv[ppconst_used]);
4606 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004607 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4608 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004609 }
4610 else
4611 {
4612 generate_ppconst(cctx, ppconst);
4613 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615
4616 // evaluate the second expression; any type is accepted
4617 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004618 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004619 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004620 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004621 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622
Bram Moolenaara5565e42020-05-09 15:44:01 +02004623 if (!has_const_expr)
4624 {
4625 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626
Bram Moolenaara5565e42020-05-09 15:44:01 +02004627 // remember the type and drop it
4628 --stack->ga_len;
4629 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630
Bram Moolenaara5565e42020-05-09 15:44:01 +02004631 end_idx = instr->ga_len;
4632 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4633
4634 // jump here from JUMP_IF_FALSE
4635 isn = ((isn_T *)instr->ga_data) + alt_idx;
4636 isn->isn_arg.jump.jump_where = instr->ga_len;
4637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638
4639 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004640 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 if (*p != ':')
4642 {
4643 emsg(_(e_missing_colon));
4644 return FAIL;
4645 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004646 if (next != NULL)
4647 {
4648 *arg = next_line_from_context(cctx, TRUE);
4649 p = skipwhite(*arg);
4650 }
4651
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004652 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4653 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004655 return FAIL;
4656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657
4658 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004659 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004660 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4661 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004663 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004664 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004665 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004666 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667
Bram Moolenaara5565e42020-05-09 15:44:01 +02004668 if (!has_const_expr)
4669 {
4670 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671
Bram Moolenaara5565e42020-05-09 15:44:01 +02004672 // If the types differ, the result has a more generic type.
4673 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4674 common_type(type1, type2, &type2, cctx->ctx_type_list);
4675
4676 // jump here from JUMP_ALWAYS
4677 isn = ((isn_T *)instr->ga_data) + end_idx;
4678 isn->isn_arg.jump.jump_where = instr->ga_len;
4679 }
4680
4681 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 }
4683 return OK;
4684}
4685
4686/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004687 * Toplevel expression.
4688 */
4689 static int
4690compile_expr0(char_u **arg, cctx_T *cctx)
4691{
4692 ppconst_T ppconst;
4693
4694 CLEAR_FIELD(ppconst);
4695 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4696 {
4697 clear_ppconst(&ppconst);
4698 return FAIL;
4699 }
4700 if (generate_ppconst(cctx, &ppconst) == FAIL)
4701 return FAIL;
4702 return OK;
4703}
4704
4705/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 * compile "return [expr]"
4707 */
4708 static char_u *
4709compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4710{
4711 char_u *p = arg;
4712 garray_T *stack = &cctx->ctx_type_stack;
4713 type_T *stack_type;
4714
4715 if (*p != NUL && *p != '|' && *p != '\n')
4716 {
4717 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719 return NULL;
4720
4721 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4722 if (set_return_type)
4723 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004724 else
4725 {
4726 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4727 && stack_type->tt_type != VAR_VOID
4728 && stack_type->tt_type != VAR_UNKNOWN)
4729 {
4730 emsg(_("E1096: Returning a value in a function without a return type"));
4731 return NULL;
4732 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004733 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4734 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 }
4738 else
4739 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004740 // "set_return_type" cannot be TRUE, only used for a lambda which
4741 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004742 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4743 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 {
4745 emsg(_("E1003: Missing return value"));
4746 return NULL;
4747 }
4748
4749 // No argument, return zero.
4750 generate_PUSHNR(cctx, 0);
4751 }
4752
4753 if (generate_instr(cctx, ISN_RETURN) == NULL)
4754 return NULL;
4755
4756 // "return val | endif" is possible
4757 return skipwhite(p);
4758}
4759
4760/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004761 * Get a line from the compilation context, compatible with exarg_T getline().
4762 * Return a pointer to the line in allocated memory.
4763 * Return NULL for end-of-file or some error.
4764 */
4765 static char_u *
4766exarg_getline(
4767 int c UNUSED,
4768 void *cookie,
4769 int indent UNUSED,
4770 int do_concat UNUSED)
4771{
4772 cctx_T *cctx = (cctx_T *)cookie;
4773
4774 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4775 {
4776 iemsg("Heredoc got to end");
4777 return NULL;
4778 }
4779 ++cctx->ctx_lnum;
4780 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4781 [cctx->ctx_lnum]);
4782}
4783
4784/*
4785 * Compile a nested :def command.
4786 */
4787 static char_u *
4788compile_nested_function(exarg_T *eap, cctx_T *cctx)
4789{
4790 char_u *name_start = eap->arg;
4791 char_u *name_end = to_name_end(eap->arg, FALSE);
4792 char_u *name = get_lambda_name();
4793 lvar_T *lvar;
4794 ufunc_T *ufunc;
4795
4796 eap->arg = name_end;
4797 eap->getline = exarg_getline;
4798 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004799 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004800 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004801 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004802
Bram Moolenaar822ba242020-05-24 23:00:18 +02004803 if (ufunc == NULL)
4804 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004805 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004806 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004807 return NULL;
4808
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004809 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004810 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004811 TRUE, ufunc->uf_func_type);
4812
4813 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4814 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4815 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004816
Bram Moolenaar61a89812020-05-07 16:58:17 +02004817 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004818 return (char_u *)"";
4819}
4820
4821/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 * Return the length of an assignment operator, or zero if there isn't one.
4823 */
4824 int
4825assignment_len(char_u *p, int *heredoc)
4826{
4827 if (*p == '=')
4828 {
4829 if (p[1] == '<' && p[2] == '<')
4830 {
4831 *heredoc = TRUE;
4832 return 3;
4833 }
4834 return 1;
4835 }
4836 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4837 return 2;
4838 if (STRNCMP(p, "..=", 3) == 0)
4839 return 3;
4840 return 0;
4841}
4842
4843// words that cannot be used as a variable
4844static char *reserved[] = {
4845 "true",
4846 "false",
4847 NULL
4848};
4849
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004850typedef enum {
4851 dest_local,
4852 dest_option,
4853 dest_env,
4854 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004855 dest_buffer,
4856 dest_window,
4857 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004858 dest_vimvar,
4859 dest_script,
4860 dest_reg,
4861} assign_dest_T;
4862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004864 * Generate the load instruction for "name".
4865 */
4866 static void
4867generate_loadvar(
4868 cctx_T *cctx,
4869 assign_dest_T dest,
4870 char_u *name,
4871 lvar_T *lvar,
4872 type_T *type)
4873{
4874 switch (dest)
4875 {
4876 case dest_option:
4877 // TODO: check the option exists
4878 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4879 break;
4880 case dest_global:
4881 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4882 break;
4883 case dest_buffer:
4884 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4885 break;
4886 case dest_window:
4887 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4888 break;
4889 case dest_tab:
4890 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4891 break;
4892 case dest_script:
4893 compile_load_scriptvar(cctx,
4894 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4895 break;
4896 case dest_env:
4897 // Include $ in the name here
4898 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4899 break;
4900 case dest_reg:
4901 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4902 break;
4903 case dest_vimvar:
4904 generate_LOADV(cctx, name + 2, TRUE);
4905 break;
4906 case dest_local:
4907 if (lvar->lv_from_outer)
4908 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4909 NULL, type);
4910 else
4911 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4912 break;
4913 }
4914}
4915
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004916 void
4917vim9_declare_error(char_u *name)
4918{
4919 char *scope = "";
4920
4921 switch (*name)
4922 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004923 case 'g': scope = _("global"); break;
4924 case 'b': scope = _("buffer"); break;
4925 case 'w': scope = _("window"); break;
4926 case 't': scope = _("tab"); break;
4927 case 'v': scope = "v:"; break;
4928 case '$': semsg(_(e_declare_env_var), name); return;
4929 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004930 }
4931 semsg(_(e_declare_var), scope, name);
4932}
4933
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004934/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004935 * Compile declaration and assignment:
4936 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004938 * Return NULL for an error.
4939 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 */
4941 static char_u *
4942compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4943{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004944 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004946 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 char_u *ret = NULL;
4948 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004949 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004952 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 int oplen = 0;
4955 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004956 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004957 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004958 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004962 // Skip over the "var" or "[var, var]" to get to any "=".
4963 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4964 if (p == NULL)
4965 return *arg == '[' ? arg : NULL;
4966
4967 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004969 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 return NULL;
4971 }
4972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 sp = p;
4974 p = skipwhite(p);
4975 op = p;
4976 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004977
4978 if (var_count > 0 && oplen == 0)
4979 // can be something like "[1, 2]->func()"
4980 return arg;
4981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4983 {
4984 char_u buf[4];
4985
4986 vim_strncpy(buf, op, oplen);
4987 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004988 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004989 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 if (heredoc)
4992 {
4993 list_T *l;
4994 listitem_T *li;
4995
4996 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004997 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004999 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000
5001 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005002 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 {
5004 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5005 li->li_tv.vval.v_string = NULL;
5006 }
5007 generate_NEWLIST(cctx, l->lv_len);
5008 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005009 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 list_free(l);
5011 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005012 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005014 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005016 // for "[var, var] = expr" evaluate the expression here, loop over the
5017 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005018
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005019 p = skipwhite(op + oplen);
5020 if (compile_expr0(&p, cctx) == FAIL)
5021 return NULL;
5022 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005024 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005026 type_T *stacktype;
5027
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005028 stacktype = stack->ga_len == 0 ? &t_void
5029 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005030 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005032 emsg(_(e_cannot_use_void));
5033 goto theend;
5034 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005035 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005036 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005037 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005038 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5039 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005040 }
5041 }
5042
5043 /*
5044 * Loop over variables in "[var, var] = expr".
5045 * For "var = expr" and "let var: type" this is done only once.
5046 */
5047 if (var_count > 0)
5048 var_start = skipwhite(arg + 1); // skip over the "["
5049 else
5050 var_start = arg;
5051 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5052 {
5053 char_u *var_end = skip_var_one(var_start, FALSE);
5054 size_t varlen;
5055 int new_local = FALSE;
5056 int opt_type;
5057 int opt_flags = 0;
5058 assign_dest_T dest = dest_local;
5059 int vimvaridx = -1;
5060 lvar_T *lvar = NULL;
5061 lvar_T arg_lvar;
5062 int has_type = FALSE;
5063 int has_index = FALSE;
5064 int instr_count = -1;
5065
5066 p = (*var_start == '&' || *var_start == '$'
5067 || *var_start == '@') ? var_start + 1 : var_start;
5068 p = to_name_end(p, TRUE);
5069
5070 // "a: type" is declaring variable "a" with a type, not "a:".
5071 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5072 --var_end;
5073 if (is_decl && p == var_start + 2 && p[-1] == ':')
5074 --p;
5075
5076 varlen = p - var_start;
5077 vim_free(name);
5078 name = vim_strnsave(var_start, varlen);
5079 if (name == NULL)
5080 return NULL;
5081 if (!heredoc)
5082 type = &t_any;
5083
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005084 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005085 {
5086 if (*var_start == '&')
5087 {
5088 int cc;
5089 long numval;
5090
5091 dest = dest_option;
5092 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005094 emsg(_(e_const_option));
5095 goto theend;
5096 }
5097 if (is_decl)
5098 {
5099 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5100 goto theend;
5101 }
5102 p = var_start;
5103 p = find_option_end(&p, &opt_flags);
5104 if (p == NULL)
5105 {
5106 // cannot happen?
5107 emsg(_(e_letunexp));
5108 goto theend;
5109 }
5110 cc = *p;
5111 *p = NUL;
5112 opt_type = get_option_value(var_start + 1, &numval,
5113 NULL, opt_flags);
5114 *p = cc;
5115 if (opt_type == -3)
5116 {
5117 semsg(_(e_unknown_option), var_start);
5118 goto theend;
5119 }
5120 if (opt_type == -2 || opt_type == 0)
5121 type = &t_string;
5122 else
5123 type = &t_number; // both number and boolean option
5124 }
5125 else if (*var_start == '$')
5126 {
5127 dest = dest_env;
5128 type = &t_string;
5129 if (is_decl)
5130 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005131 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005132 goto theend;
5133 }
5134 }
5135 else if (*var_start == '@')
5136 {
5137 if (!valid_yank_reg(var_start[1], TRUE))
5138 {
5139 emsg_invreg(var_start[1]);
5140 goto theend;
5141 }
5142 dest = dest_reg;
5143 type = &t_string;
5144 if (is_decl)
5145 {
5146 semsg(_("E1066: Cannot declare a register: %s"), name);
5147 goto theend;
5148 }
5149 }
5150 else if (STRNCMP(var_start, "g:", 2) == 0)
5151 {
5152 dest = dest_global;
5153 if (is_decl)
5154 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005155 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005156 goto theend;
5157 }
5158 }
5159 else if (STRNCMP(var_start, "b:", 2) == 0)
5160 {
5161 dest = dest_buffer;
5162 if (is_decl)
5163 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005164 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005165 goto theend;
5166 }
5167 }
5168 else if (STRNCMP(var_start, "w:", 2) == 0)
5169 {
5170 dest = dest_window;
5171 if (is_decl)
5172 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005173 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005174 goto theend;
5175 }
5176 }
5177 else if (STRNCMP(var_start, "t:", 2) == 0)
5178 {
5179 dest = dest_tab;
5180 if (is_decl)
5181 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005182 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005183 goto theend;
5184 }
5185 }
5186 else if (STRNCMP(var_start, "v:", 2) == 0)
5187 {
5188 typval_T *vtv;
5189 int di_flags;
5190
5191 vimvaridx = find_vim_var(name + 2, &di_flags);
5192 if (vimvaridx < 0)
5193 {
5194 semsg(_(e_var_notfound), var_start);
5195 goto theend;
5196 }
5197 // We use the current value of "sandbox" here, is that OK?
5198 if (var_check_ro(di_flags, name, FALSE))
5199 goto theend;
5200 dest = dest_vimvar;
5201 vtv = get_vim_var_tv(vimvaridx);
5202 type = typval2type(vtv);
5203 if (is_decl)
5204 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005205 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005206 goto theend;
5207 }
5208 }
5209 else
5210 {
5211 int idx;
5212
5213 for (idx = 0; reserved[idx] != NULL; ++idx)
5214 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005215 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005216 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005217 goto theend;
5218 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005219
5220 lvar = lookup_local(var_start, varlen, cctx);
5221 if (lvar == NULL)
5222 {
5223 CLEAR_FIELD(arg_lvar);
5224 if (lookup_arg(var_start, varlen,
5225 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5226 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005227 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005228 if (is_decl)
5229 {
5230 semsg(_(e_used_as_arg), name);
5231 goto theend;
5232 }
5233 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005234 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005235 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005236 if (lvar != NULL)
5237 {
5238 if (is_decl)
5239 {
5240 semsg(_("E1017: Variable already declared: %s"), name);
5241 goto theend;
5242 }
5243 else if (lvar->lv_const)
5244 {
5245 semsg(_("E1018: Cannot assign to a constant: %s"),
5246 name);
5247 goto theend;
5248 }
5249 }
5250 else if (STRNCMP(var_start, "s:", 2) == 0
5251 || lookup_script(var_start, varlen) == OK
5252 || find_imported(var_start, varlen, cctx) != NULL)
5253 {
5254 dest = dest_script;
5255 if (is_decl)
5256 {
5257 semsg(_("E1054: Variable already declared in the script: %s"),
5258 name);
5259 goto theend;
5260 }
5261 }
5262 else if (name[1] == ':' && name[2] != NUL)
5263 {
5264 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5265 name);
5266 goto theend;
5267 }
5268 else if (!is_decl)
5269 {
5270 semsg(_("E1089: unknown variable: %s"), name);
5271 goto theend;
5272 }
5273 }
5274 }
5275
5276 // handle "a:name" as a name, not index "name" on "a"
5277 if (varlen > 1 || var_start[varlen] != ':')
5278 p = var_end;
5279
5280 if (dest != dest_option)
5281 {
5282 if (is_decl && *p == ':')
5283 {
5284 // parse optional type: "let var: type = expr"
5285 if (!VIM_ISWHITE(p[1]))
5286 {
5287 semsg(_(e_white_after), ":");
5288 goto theend;
5289 }
5290 p = skipwhite(p + 1);
5291 type = parse_type(&p, cctx->ctx_type_list);
5292 has_type = TRUE;
5293 }
5294 else if (lvar != NULL)
5295 type = lvar->lv_type;
5296 }
5297
5298 if (oplen == 3 && !heredoc && dest != dest_global
5299 && type->tt_type != VAR_STRING
5300 && type->tt_type != VAR_ANY)
5301 {
5302 emsg(_("E1019: Can only concatenate to string"));
5303 goto theend;
5304 }
5305
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005306 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005307 {
5308 if (oplen > 1 && !heredoc)
5309 {
5310 // +=, /=, etc. require an existing variable
5311 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5312 name);
5313 goto theend;
5314 }
5315
5316 // new local variable
5317 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5318 goto theend;
5319 lvar = reserve_local(cctx, var_start, varlen,
5320 cmdidx == CMD_const, type);
5321 if (lvar == NULL)
5322 goto theend;
5323 new_local = TRUE;
5324 }
5325
5326 member_type = type;
5327 if (var_end > var_start + varlen)
5328 {
5329 // Something follows after the variable: "var[idx]".
5330 if (is_decl)
5331 {
5332 emsg(_("E1087: cannot use an index when declaring a variable"));
5333 goto theend;
5334 }
5335
5336 if (var_start[varlen] == '[')
5337 {
5338 has_index = TRUE;
5339 if (type->tt_member == NULL)
5340 {
5341 semsg(_("E1088: cannot use an index on %s"), name);
5342 goto theend;
5343 }
5344 member_type = type->tt_member;
5345 }
5346 else
5347 {
5348 semsg("Not supported yet: %s", var_start);
5349 goto theend;
5350 }
5351 }
5352 else if (lvar == &arg_lvar)
5353 {
5354 semsg(_("E1090: Cannot assign to argument %s"), name);
5355 goto theend;
5356 }
5357
5358 if (!heredoc)
5359 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005360 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005361 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005362 if (oplen > 0 && var_count == 0)
5363 {
5364 // skip over the "=" and the expression
5365 p = skipwhite(op + oplen);
5366 compile_expr0(&p, cctx);
5367 }
5368 }
5369 else if (oplen > 0)
5370 {
5371 type_T *stacktype;
5372
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005373 // For "var = expr" evaluate the expression.
5374 if (var_count == 0)
5375 {
5376 int r;
5377
5378 // for "+=", "*=", "..=" etc. first load the current value
5379 if (*op != '=')
5380 {
5381 generate_loadvar(cctx, dest, name, lvar, type);
5382
5383 if (has_index)
5384 {
5385 // TODO: get member from list or dict
5386 emsg("Index with operation not supported yet");
5387 goto theend;
5388 }
5389 }
5390
5391 // Compile the expression. Temporarily hide the new local
5392 // variable here, it is not available to this expression.
5393 if (new_local)
5394 --cctx->ctx_locals.ga_len;
5395 instr_count = instr->ga_len;
5396 p = skipwhite(op + oplen);
5397 r = compile_expr0(&p, cctx);
5398 if (new_local)
5399 ++cctx->ctx_locals.ga_len;
5400 if (r == FAIL)
5401 goto theend;
5402 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005403 else if (semicolon && var_idx == var_count - 1)
5404 {
5405 // For "[var; var] = expr" get the rest of the list
5406 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5407 goto theend;
5408 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005409 else
5410 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005411 // For "[var, var] = expr" get the "var_idx" item from the
5412 // list.
5413 if (generate_GETITEM(cctx, var_idx) == FAIL)
5414 return FAIL;
5415 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005416
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005417 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005418 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005419 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005420 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005421 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005422 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005423 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005424 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005425 emsg(_(e_cannot_use_void));
5426 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005427 }
5428 else
5429 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005430 // An empty list or dict has a &t_void member,
5431 // for a variable that implies &t_any.
5432 if (stacktype == &t_list_empty)
5433 lvar->lv_type = &t_list_any;
5434 else if (stacktype == &t_dict_empty)
5435 lvar->lv_type = &t_dict_any;
5436 else
5437 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005438 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005439 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005440 else
5441 {
5442 type_T *use_type = lvar->lv_type;
5443
5444 if (has_index)
5445 {
5446 use_type = use_type->tt_member;
5447 if (use_type == NULL)
5448 use_type = &t_void;
5449 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005450 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005451 == FAIL)
5452 goto theend;
5453 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005454 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005455 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005456 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005457 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005459 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005460 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005461 emsg(_(e_const_req_value));
5462 goto theend;
5463 }
5464 else if (!has_type || dest == dest_option)
5465 {
5466 emsg(_(e_type_req));
5467 goto theend;
5468 }
5469 else
5470 {
5471 // variables are always initialized
5472 if (ga_grow(instr, 1) == FAIL)
5473 goto theend;
5474 switch (member_type->tt_type)
5475 {
5476 case VAR_BOOL:
5477 generate_PUSHBOOL(cctx, VVAL_FALSE);
5478 break;
5479 case VAR_FLOAT:
5480#ifdef FEAT_FLOAT
5481 generate_PUSHF(cctx, 0.0);
5482#endif
5483 break;
5484 case VAR_STRING:
5485 generate_PUSHS(cctx, NULL);
5486 break;
5487 case VAR_BLOB:
5488 generate_PUSHBLOB(cctx, NULL);
5489 break;
5490 case VAR_FUNC:
5491 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5492 break;
5493 case VAR_LIST:
5494 generate_NEWLIST(cctx, 0);
5495 break;
5496 case VAR_DICT:
5497 generate_NEWDICT(cctx, 0);
5498 break;
5499 case VAR_JOB:
5500 generate_PUSHJOB(cctx, NULL);
5501 break;
5502 case VAR_CHANNEL:
5503 generate_PUSHCHANNEL(cctx, NULL);
5504 break;
5505 case VAR_NUMBER:
5506 case VAR_UNKNOWN:
5507 case VAR_ANY:
5508 case VAR_PARTIAL:
5509 case VAR_VOID:
5510 case VAR_SPECIAL: // cannot happen
5511 generate_PUSHNR(cctx, 0);
5512 break;
5513 }
5514 }
5515 if (var_count == 0)
5516 end = p;
5517 }
5518
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005519 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005520 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005521 break;
5522
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005523 if (oplen > 0 && *op != '=')
5524 {
5525 type_T *expected = &t_number;
5526 type_T *stacktype;
5527
5528 // TODO: if type is known use float or any operation
5529
5530 if (*op == '.')
5531 expected = &t_string;
5532 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005533 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005534 goto theend;
5535
5536 if (*op == '.')
5537 generate_instr_drop(cctx, ISN_CONCAT, 1);
5538 else
5539 {
5540 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5541
5542 if (isn == NULL)
5543 goto theend;
5544 switch (*op)
5545 {
5546 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5547 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5548 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5549 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5550 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5551 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005552 }
5553 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005554
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005555 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005556 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005557 int r;
5558
5559 // Compile the "idx" in "var[idx]".
5560 if (new_local)
5561 --cctx->ctx_locals.ga_len;
5562 p = skipwhite(var_start + varlen + 1);
5563 r = compile_expr0(&p, cctx);
5564 if (new_local)
5565 ++cctx->ctx_locals.ga_len;
5566 if (r == FAIL)
5567 goto theend;
5568 if (*skipwhite(p) != ']')
5569 {
5570 emsg(_(e_missbrac));
5571 goto theend;
5572 }
5573 if (type->tt_type == VAR_DICT
5574 && may_generate_2STRING(-1, cctx) == FAIL)
5575 goto theend;
5576 if (type->tt_type == VAR_LIST
5577 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005578 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005579 {
5580 emsg(_(e_number_exp));
5581 goto theend;
5582 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005583
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005584 // Load the dict or list. On the stack we then have:
5585 // - value
5586 // - index
5587 // - variable
5588 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005589
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005590 if (type->tt_type == VAR_LIST)
5591 {
5592 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5593 return FAIL;
5594 }
5595 else if (type->tt_type == VAR_DICT)
5596 {
5597 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5598 return FAIL;
5599 }
5600 else
5601 {
5602 emsg(_(e_listreq));
5603 goto theend;
5604 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005605 }
5606 else
5607 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005608 switch (dest)
5609 {
5610 case dest_option:
5611 generate_STOREOPT(cctx, name + 1, opt_flags);
5612 break;
5613 case dest_global:
5614 // include g: with the name, easier to execute that way
5615 generate_STORE(cctx, ISN_STOREG, 0, name);
5616 break;
5617 case dest_buffer:
5618 // include b: with the name, easier to execute that way
5619 generate_STORE(cctx, ISN_STOREB, 0, name);
5620 break;
5621 case dest_window:
5622 // include w: with the name, easier to execute that way
5623 generate_STORE(cctx, ISN_STOREW, 0, name);
5624 break;
5625 case dest_tab:
5626 // include t: with the name, easier to execute that way
5627 generate_STORE(cctx, ISN_STORET, 0, name);
5628 break;
5629 case dest_env:
5630 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5631 break;
5632 case dest_reg:
5633 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5634 break;
5635 case dest_vimvar:
5636 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5637 break;
5638 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005639 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005640 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5641 imported_T *import = NULL;
5642 int sid = current_sctx.sc_sid;
5643 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005644
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005645 if (name[1] != ':')
5646 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005647 import = find_imported(name, 0, cctx);
5648 if (import != NULL)
5649 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005650 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005651
5652 idx = get_script_item_idx(sid, rawname, TRUE);
5653 // TODO: specific type
5654 if (idx < 0)
5655 {
5656 char_u *name_s = name;
5657
5658 // Include s: in the name for store_var()
5659 if (name[1] != ':')
5660 {
5661 int len = (int)STRLEN(name) + 3;
5662
5663 name_s = alloc(len);
5664 if (name_s == NULL)
5665 name_s = name;
5666 else
5667 vim_snprintf((char *)name_s, len,
5668 "s:%s", name);
5669 }
5670 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005671 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005672 if (name_s != name)
5673 vim_free(name_s);
5674 }
5675 else
5676 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005677 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005678 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005679 break;
5680 case dest_local:
5681 if (lvar != NULL)
5682 {
5683 isn_T *isn = ((isn_T *)instr->ga_data)
5684 + instr->ga_len - 1;
5685
5686 // optimization: turn "var = 123" from ISN_PUSHNR +
5687 // ISN_STORE into ISN_STORENR
5688 if (!lvar->lv_from_outer
5689 && instr->ga_len == instr_count + 1
5690 && isn->isn_type == ISN_PUSHNR)
5691 {
5692 varnumber_T val = isn->isn_arg.number;
5693
5694 isn->isn_type = ISN_STORENR;
5695 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5696 isn->isn_arg.storenr.stnr_val = val;
5697 if (stack->ga_len > 0)
5698 --stack->ga_len;
5699 }
5700 else if (lvar->lv_from_outer)
5701 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005702 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005703 else
5704 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5705 }
5706 break;
5707 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005708 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005709
5710 if (var_idx + 1 < var_count)
5711 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005712 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005713
5714 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005715 if (var_count > 0 && !semicolon)
5716 {
5717 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5718 goto theend;
5719 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005720
Bram Moolenaarb2097502020-07-19 17:17:02 +02005721 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005722
5723theend:
5724 vim_free(name);
5725 return ret;
5726}
5727
5728/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005729 * Check if "name" can be "unlet".
5730 */
5731 int
5732check_vim9_unlet(char_u *name)
5733{
5734 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5735 {
5736 semsg(_("E1081: Cannot unlet %s"), name);
5737 return FAIL;
5738 }
5739 return OK;
5740}
5741
5742/*
5743 * Callback passed to ex_unletlock().
5744 */
5745 static int
5746compile_unlet(
5747 lval_T *lvp,
5748 char_u *name_end,
5749 exarg_T *eap,
5750 int deep UNUSED,
5751 void *coookie)
5752{
5753 cctx_T *cctx = coookie;
5754
5755 if (lvp->ll_tv == NULL)
5756 {
5757 char_u *p = lvp->ll_name;
5758 int cc = *name_end;
5759 int ret = OK;
5760
5761 // Normal name. Only supports g:, w:, t: and b: namespaces.
5762 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005763 if (*p == '$')
5764 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5765 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005766 ret = FAIL;
5767 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005768 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005769
5770 *name_end = cc;
5771 return ret;
5772 }
5773
5774 // TODO: unlet {list}[idx]
5775 // TODO: unlet {dict}[key]
5776 emsg("Sorry, :unlet not fully implemented yet");
5777 return FAIL;
5778}
5779
5780/*
5781 * compile "unlet var", "lock var" and "unlock var"
5782 * "arg" points to "var".
5783 */
5784 static char_u *
5785compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5786{
5787 char_u *p = arg;
5788
5789 if (eap->cmdidx != CMD_unlet)
5790 {
5791 emsg("Sorry, :lock and unlock not implemented yet");
5792 return NULL;
5793 }
5794
5795 if (*p == '!')
5796 {
5797 p = skipwhite(p + 1);
5798 eap->forceit = TRUE;
5799 }
5800
5801 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5802 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5803}
5804
5805/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005806 * Compile an :import command.
5807 */
5808 static char_u *
5809compile_import(char_u *arg, cctx_T *cctx)
5810{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005811 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005812}
5813
5814/*
5815 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5816 */
5817 static int
5818compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5819{
5820 garray_T *instr = &cctx->ctx_instr;
5821 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5822
5823 if (endlabel == NULL)
5824 return FAIL;
5825 endlabel->el_next = *el;
5826 *el = endlabel;
5827 endlabel->el_end_label = instr->ga_len;
5828
5829 generate_JUMP(cctx, when, 0);
5830 return OK;
5831}
5832
5833 static void
5834compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5835{
5836 garray_T *instr = &cctx->ctx_instr;
5837
5838 while (*el != NULL)
5839 {
5840 endlabel_T *cur = (*el);
5841 isn_T *isn;
5842
5843 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5844 isn->isn_arg.jump.jump_where = instr->ga_len;
5845 *el = cur->el_next;
5846 vim_free(cur);
5847 }
5848}
5849
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005850 static void
5851compile_free_jump_to_end(endlabel_T **el)
5852{
5853 while (*el != NULL)
5854 {
5855 endlabel_T *cur = (*el);
5856
5857 *el = cur->el_next;
5858 vim_free(cur);
5859 }
5860}
5861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005862/*
5863 * Create a new scope and set up the generic items.
5864 */
5865 static scope_T *
5866new_scope(cctx_T *cctx, scopetype_T type)
5867{
5868 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5869
5870 if (scope == NULL)
5871 return NULL;
5872 scope->se_outer = cctx->ctx_scope;
5873 cctx->ctx_scope = scope;
5874 scope->se_type = type;
5875 scope->se_local_count = cctx->ctx_locals.ga_len;
5876 return scope;
5877}
5878
5879/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005880 * Free the current scope and go back to the outer scope.
5881 */
5882 static void
5883drop_scope(cctx_T *cctx)
5884{
5885 scope_T *scope = cctx->ctx_scope;
5886
5887 if (scope == NULL)
5888 {
5889 iemsg("calling drop_scope() without a scope");
5890 return;
5891 }
5892 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005893 switch (scope->se_type)
5894 {
5895 case IF_SCOPE:
5896 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5897 case FOR_SCOPE:
5898 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5899 case WHILE_SCOPE:
5900 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5901 case TRY_SCOPE:
5902 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5903 case NO_SCOPE:
5904 case BLOCK_SCOPE:
5905 break;
5906 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005907 vim_free(scope);
5908}
5909
5910/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005911 * compile "if expr"
5912 *
5913 * "if expr" Produces instructions:
5914 * EVAL expr Push result of "expr"
5915 * JUMP_IF_FALSE end
5916 * ... body ...
5917 * end:
5918 *
5919 * "if expr | else" Produces instructions:
5920 * EVAL expr Push result of "expr"
5921 * JUMP_IF_FALSE else
5922 * ... body ...
5923 * JUMP_ALWAYS end
5924 * else:
5925 * ... body ...
5926 * end:
5927 *
5928 * "if expr1 | elseif expr2 | else" Produces instructions:
5929 * EVAL expr Push result of "expr"
5930 * JUMP_IF_FALSE elseif
5931 * ... body ...
5932 * JUMP_ALWAYS end
5933 * elseif:
5934 * EVAL expr Push result of "expr"
5935 * JUMP_IF_FALSE else
5936 * ... body ...
5937 * JUMP_ALWAYS end
5938 * else:
5939 * ... body ...
5940 * end:
5941 */
5942 static char_u *
5943compile_if(char_u *arg, cctx_T *cctx)
5944{
5945 char_u *p = arg;
5946 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005947 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005948 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005949 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005950 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005951
Bram Moolenaara5565e42020-05-09 15:44:01 +02005952 CLEAR_FIELD(ppconst);
5953 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005954 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005955 clear_ppconst(&ppconst);
5956 return NULL;
5957 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005958 if (cctx->ctx_skip == SKIP_YES)
5959 clear_ppconst(&ppconst);
5960 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005961 {
5962 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005963 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005964 clear_ppconst(&ppconst);
5965 }
5966 else
5967 {
5968 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005969 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005970 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005971 return NULL;
5972 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973
5974 scope = new_scope(cctx, IF_SCOPE);
5975 if (scope == NULL)
5976 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005977 scope->se_skip_save = skip_save;
5978 // "is_had_return" will be reset if any block does not end in :return
5979 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005981 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005982 {
5983 // "where" is set when ":elseif", "else" or ":endif" is found
5984 scope->se_u.se_if.is_if_label = instr->ga_len;
5985 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5986 }
5987 else
5988 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005989
5990 return p;
5991}
5992
5993 static char_u *
5994compile_elseif(char_u *arg, cctx_T *cctx)
5995{
5996 char_u *p = arg;
5997 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005998 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999 isn_T *isn;
6000 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006001 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002
6003 if (scope == NULL || scope->se_type != IF_SCOPE)
6004 {
6005 emsg(_(e_elseif_without_if));
6006 return NULL;
6007 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006008 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006009 if (!cctx->ctx_had_return)
6010 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006012 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006013 {
6014 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006016 return NULL;
6017 // previous "if" or "elseif" jumps here
6018 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6019 isn->isn_arg.jump.jump_where = instr->ga_len;
6020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006022 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006023 CLEAR_FIELD(ppconst);
6024 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006025 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006026 clear_ppconst(&ppconst);
6027 return NULL;
6028 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006029 if (scope->se_skip_save == SKIP_YES)
6030 clear_ppconst(&ppconst);
6031 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006032 {
6033 // The expression results in a constant.
6034 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006035 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006036 clear_ppconst(&ppconst);
6037 scope->se_u.se_if.is_if_label = -1;
6038 }
6039 else
6040 {
6041 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006042 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006043 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006044 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006045
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006046 // "where" is set when ":elseif", "else" or ":endif" is found
6047 scope->se_u.se_if.is_if_label = instr->ga_len;
6048 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6049 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050
6051 return p;
6052}
6053
6054 static char_u *
6055compile_else(char_u *arg, cctx_T *cctx)
6056{
6057 char_u *p = arg;
6058 garray_T *instr = &cctx->ctx_instr;
6059 isn_T *isn;
6060 scope_T *scope = cctx->ctx_scope;
6061
6062 if (scope == NULL || scope->se_type != IF_SCOPE)
6063 {
6064 emsg(_(e_else_without_if));
6065 return NULL;
6066 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006067 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006068 if (!cctx->ctx_had_return)
6069 scope->se_u.se_if.is_had_return = FALSE;
6070 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006071
Bram Moolenaarefd88552020-06-18 20:50:10 +02006072 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006073 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006074 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006075 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006076 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006077 if (!cctx->ctx_had_return
6078 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6079 JUMP_ALWAYS, cctx) == FAIL)
6080 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006081 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006082
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006083 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006084 {
6085 if (scope->se_u.se_if.is_if_label >= 0)
6086 {
6087 // previous "if" or "elseif" jumps here
6088 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6089 isn->isn_arg.jump.jump_where = instr->ga_len;
6090 scope->se_u.se_if.is_if_label = -1;
6091 }
6092 }
6093
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006094 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006095 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006097
6098 return p;
6099}
6100
6101 static char_u *
6102compile_endif(char_u *arg, cctx_T *cctx)
6103{
6104 scope_T *scope = cctx->ctx_scope;
6105 ifscope_T *ifscope;
6106 garray_T *instr = &cctx->ctx_instr;
6107 isn_T *isn;
6108
6109 if (scope == NULL || scope->se_type != IF_SCOPE)
6110 {
6111 emsg(_(e_endif_without_if));
6112 return NULL;
6113 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006114 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006115 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006116 if (!cctx->ctx_had_return)
6117 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006119 if (scope->se_u.se_if.is_if_label >= 0)
6120 {
6121 // previous "if" or "elseif" jumps here
6122 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6123 isn->isn_arg.jump.jump_where = instr->ga_len;
6124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125 // Fill in the "end" label in jumps at the end of the blocks.
6126 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006127 cctx->ctx_skip = scope->se_skip_save;
6128
6129 // If all the blocks end in :return and there is an :else then the
6130 // had_return flag is set.
6131 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006132
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006133 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134 return arg;
6135}
6136
6137/*
6138 * compile "for var in expr"
6139 *
6140 * Produces instructions:
6141 * PUSHNR -1
6142 * STORE loop-idx Set index to -1
6143 * EVAL expr Push result of "expr"
6144 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6145 * - if beyond end, jump to "end"
6146 * - otherwise get item from list and push it
6147 * STORE var Store item in "var"
6148 * ... body ...
6149 * JUMP top Jump back to repeat
6150 * end: DROP Drop the result of "expr"
6151 *
6152 */
6153 static char_u *
6154compile_for(char_u *arg, cctx_T *cctx)
6155{
6156 char_u *p;
6157 size_t varlen;
6158 garray_T *instr = &cctx->ctx_instr;
6159 garray_T *stack = &cctx->ctx_type_stack;
6160 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006161 lvar_T *loop_lvar; // loop iteration variable
6162 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006163 type_T *vartype;
6164
6165 // TODO: list of variables: "for [key, value] in dict"
6166 // parse "var"
6167 for (p = arg; eval_isnamec1(*p); ++p)
6168 ;
6169 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006170 var_lvar = lookup_local(arg, varlen, cctx);
6171 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172 {
6173 semsg(_("E1023: variable already defined: %s"), arg);
6174 return NULL;
6175 }
6176
6177 // consume "in"
6178 p = skipwhite(p);
6179 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6180 {
6181 emsg(_(e_missing_in));
6182 return NULL;
6183 }
6184 p = skipwhite(p + 2);
6185
6186
6187 scope = new_scope(cctx, FOR_SCOPE);
6188 if (scope == NULL)
6189 return NULL;
6190
6191 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006192 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6193 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006194 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006195 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006196 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006197 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006199
6200 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006201 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6202 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006203 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006204 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006205 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006206 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006208
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006209 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006210
6211 // compile "expr", it remains on the stack until "endfor"
6212 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006213 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006214 {
6215 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006219 // Now that we know the type of "var", check that it is a list, now or at
6220 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006222 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006223 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006224 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006225 return NULL;
6226 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006227 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006228 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229
6230 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006231 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006233 generate_FOR(cctx, loop_lvar->lv_idx);
6234 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235
6236 return arg;
6237}
6238
6239/*
6240 * compile "endfor"
6241 */
6242 static char_u *
6243compile_endfor(char_u *arg, cctx_T *cctx)
6244{
6245 garray_T *instr = &cctx->ctx_instr;
6246 scope_T *scope = cctx->ctx_scope;
6247 forscope_T *forscope;
6248 isn_T *isn;
6249
6250 if (scope == NULL || scope->se_type != FOR_SCOPE)
6251 {
6252 emsg(_(e_for));
6253 return NULL;
6254 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006255 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006257 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006258
6259 // At end of ":for" scope jump back to the FOR instruction.
6260 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6261
6262 // Fill in the "end" label in the FOR statement so it can jump here
6263 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6264 isn->isn_arg.forloop.for_end = instr->ga_len;
6265
6266 // Fill in the "end" label any BREAK statements
6267 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6268
6269 // Below the ":for" scope drop the "expr" list from the stack.
6270 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6271 return NULL;
6272
6273 vim_free(scope);
6274
6275 return arg;
6276}
6277
6278/*
6279 * compile "while expr"
6280 *
6281 * Produces instructions:
6282 * top: EVAL expr Push result of "expr"
6283 * JUMP_IF_FALSE end jump if false
6284 * ... body ...
6285 * JUMP top Jump back to repeat
6286 * end:
6287 *
6288 */
6289 static char_u *
6290compile_while(char_u *arg, cctx_T *cctx)
6291{
6292 char_u *p = arg;
6293 garray_T *instr = &cctx->ctx_instr;
6294 scope_T *scope;
6295
6296 scope = new_scope(cctx, WHILE_SCOPE);
6297 if (scope == NULL)
6298 return NULL;
6299
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006300 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301
6302 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006303 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 return NULL;
6305
6306 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006307 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 JUMP_IF_FALSE, cctx) == FAIL)
6309 return FAIL;
6310
6311 return p;
6312}
6313
6314/*
6315 * compile "endwhile"
6316 */
6317 static char_u *
6318compile_endwhile(char_u *arg, cctx_T *cctx)
6319{
6320 scope_T *scope = cctx->ctx_scope;
6321
6322 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6323 {
6324 emsg(_(e_while));
6325 return NULL;
6326 }
6327 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006328 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329
6330 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006331 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332
6333 // Fill in the "end" label in the WHILE statement so it can jump here.
6334 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006335 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006336
6337 vim_free(scope);
6338
6339 return arg;
6340}
6341
6342/*
6343 * compile "continue"
6344 */
6345 static char_u *
6346compile_continue(char_u *arg, cctx_T *cctx)
6347{
6348 scope_T *scope = cctx->ctx_scope;
6349
6350 for (;;)
6351 {
6352 if (scope == NULL)
6353 {
6354 emsg(_(e_continue));
6355 return NULL;
6356 }
6357 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6358 break;
6359 scope = scope->se_outer;
6360 }
6361
6362 // Jump back to the FOR or WHILE instruction.
6363 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006364 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6365 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366 return arg;
6367}
6368
6369/*
6370 * compile "break"
6371 */
6372 static char_u *
6373compile_break(char_u *arg, cctx_T *cctx)
6374{
6375 scope_T *scope = cctx->ctx_scope;
6376 endlabel_T **el;
6377
6378 for (;;)
6379 {
6380 if (scope == NULL)
6381 {
6382 emsg(_(e_break));
6383 return NULL;
6384 }
6385 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6386 break;
6387 scope = scope->se_outer;
6388 }
6389
6390 // Jump to the end of the FOR or WHILE loop.
6391 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006392 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006393 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006394 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006395 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6396 return FAIL;
6397
6398 return arg;
6399}
6400
6401/*
6402 * compile "{" start of block
6403 */
6404 static char_u *
6405compile_block(char_u *arg, cctx_T *cctx)
6406{
6407 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6408 return NULL;
6409 return skipwhite(arg + 1);
6410}
6411
6412/*
6413 * compile end of block: drop one scope
6414 */
6415 static void
6416compile_endblock(cctx_T *cctx)
6417{
6418 scope_T *scope = cctx->ctx_scope;
6419
6420 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006421 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006422 vim_free(scope);
6423}
6424
6425/*
6426 * compile "try"
6427 * Creates a new scope for the try-endtry, pointing to the first catch and
6428 * finally.
6429 * Creates another scope for the "try" block itself.
6430 * TRY instruction sets up exception handling at runtime.
6431 *
6432 * "try"
6433 * TRY -> catch1, -> finally push trystack entry
6434 * ... try block
6435 * "throw {exception}"
6436 * EVAL {exception}
6437 * THROW create exception
6438 * ... try block
6439 * " catch {expr}"
6440 * JUMP -> finally
6441 * catch1: PUSH exeception
6442 * EVAL {expr}
6443 * MATCH
6444 * JUMP nomatch -> catch2
6445 * CATCH remove exception
6446 * ... catch block
6447 * " catch"
6448 * JUMP -> finally
6449 * catch2: CATCH remove exception
6450 * ... catch block
6451 * " finally"
6452 * finally:
6453 * ... finally block
6454 * " endtry"
6455 * ENDTRY pop trystack entry, may rethrow
6456 */
6457 static char_u *
6458compile_try(char_u *arg, cctx_T *cctx)
6459{
6460 garray_T *instr = &cctx->ctx_instr;
6461 scope_T *try_scope;
6462 scope_T *scope;
6463
6464 // scope that holds the jumps that go to catch/finally/endtry
6465 try_scope = new_scope(cctx, TRY_SCOPE);
6466 if (try_scope == NULL)
6467 return NULL;
6468
6469 // "catch" is set when the first ":catch" is found.
6470 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006471 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006472 if (generate_instr(cctx, ISN_TRY) == NULL)
6473 return NULL;
6474
6475 // scope for the try block itself
6476 scope = new_scope(cctx, BLOCK_SCOPE);
6477 if (scope == NULL)
6478 return NULL;
6479
6480 return arg;
6481}
6482
6483/*
6484 * compile "catch {expr}"
6485 */
6486 static char_u *
6487compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6488{
6489 scope_T *scope = cctx->ctx_scope;
6490 garray_T *instr = &cctx->ctx_instr;
6491 char_u *p;
6492 isn_T *isn;
6493
6494 // end block scope from :try or :catch
6495 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6496 compile_endblock(cctx);
6497 scope = cctx->ctx_scope;
6498
6499 // Error if not in a :try scope
6500 if (scope == NULL || scope->se_type != TRY_SCOPE)
6501 {
6502 emsg(_(e_catch));
6503 return NULL;
6504 }
6505
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006506 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006507 {
6508 emsg(_("E1033: catch unreachable after catch-all"));
6509 return NULL;
6510 }
6511
6512 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006513 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006514 JUMP_ALWAYS, cctx) == FAIL)
6515 return NULL;
6516
6517 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006518 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006519 if (isn->isn_arg.try.try_catch == 0)
6520 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006521 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 {
6523 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006524 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525 isn->isn_arg.jump.jump_where = instr->ga_len;
6526 }
6527
6528 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006529 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006530 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006531 scope->se_u.se_try.ts_caught_all = TRUE;
6532 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 }
6534 else
6535 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006536 char_u *end;
6537 char_u *pat;
6538 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006539 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006540 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 // Push v:exception, push {expr} and MATCH
6543 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6544
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006545 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006546 if (*end != *p)
6547 {
6548 semsg(_("E1067: Separator mismatch: %s"), p);
6549 vim_free(tofree);
6550 return FAIL;
6551 }
6552 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006553 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006554 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006555 len = (int)(end - tofree);
6556 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006557 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006558 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006559 if (pat == NULL)
6560 return FAIL;
6561 if (generate_PUSHS(cctx, pat) == FAIL)
6562 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6565 return NULL;
6566
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006567 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006568 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6569 return NULL;
6570 }
6571
6572 if (generate_instr(cctx, ISN_CATCH) == NULL)
6573 return NULL;
6574
6575 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6576 return NULL;
6577 return p;
6578}
6579
6580 static char_u *
6581compile_finally(char_u *arg, cctx_T *cctx)
6582{
6583 scope_T *scope = cctx->ctx_scope;
6584 garray_T *instr = &cctx->ctx_instr;
6585 isn_T *isn;
6586
6587 // end block scope from :try or :catch
6588 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6589 compile_endblock(cctx);
6590 scope = cctx->ctx_scope;
6591
6592 // Error if not in a :try scope
6593 if (scope == NULL || scope->se_type != TRY_SCOPE)
6594 {
6595 emsg(_(e_finally));
6596 return NULL;
6597 }
6598
6599 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006600 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 if (isn->isn_arg.try.try_finally != 0)
6602 {
6603 emsg(_(e_finally_dup));
6604 return NULL;
6605 }
6606
6607 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006608 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006609
Bram Moolenaar585fea72020-04-02 22:33:21 +02006610 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006611 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006612 {
6613 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006614 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006616 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 }
6618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619 // TODO: set index in ts_finally_label jumps
6620
6621 return arg;
6622}
6623
6624 static char_u *
6625compile_endtry(char_u *arg, cctx_T *cctx)
6626{
6627 scope_T *scope = cctx->ctx_scope;
6628 garray_T *instr = &cctx->ctx_instr;
6629 isn_T *isn;
6630
6631 // end block scope from :catch or :finally
6632 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6633 compile_endblock(cctx);
6634 scope = cctx->ctx_scope;
6635
6636 // Error if not in a :try scope
6637 if (scope == NULL || scope->se_type != TRY_SCOPE)
6638 {
6639 if (scope == NULL)
6640 emsg(_(e_no_endtry));
6641 else if (scope->se_type == WHILE_SCOPE)
6642 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006643 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006644 emsg(_(e_endfor));
6645 else
6646 emsg(_(e_endif));
6647 return NULL;
6648 }
6649
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006650 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6652 {
6653 emsg(_("E1032: missing :catch or :finally"));
6654 return NULL;
6655 }
6656
6657 // Fill in the "end" label in jumps at the end of the blocks, if not done
6658 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006659 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660
6661 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006662 if (isn->isn_arg.try.try_catch == 0)
6663 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 if (isn->isn_arg.try.try_finally == 0)
6665 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006666
6667 if (scope->se_u.se_try.ts_catch_label != 0)
6668 {
6669 // Last catch without match jumps here
6670 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6671 isn->isn_arg.jump.jump_where = instr->ga_len;
6672 }
6673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 compile_endblock(cctx);
6675
6676 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6677 return NULL;
6678 return arg;
6679}
6680
6681/*
6682 * compile "throw {expr}"
6683 */
6684 static char_u *
6685compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6686{
6687 char_u *p = skipwhite(arg);
6688
Bram Moolenaara5565e42020-05-09 15:44:01 +02006689 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690 return NULL;
6691 if (may_generate_2STRING(-1, cctx) == FAIL)
6692 return NULL;
6693 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6694 return NULL;
6695
6696 return p;
6697}
6698
6699/*
6700 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006701 * compile "echomsg expr"
6702 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006703 * compile "execute expr"
6704 */
6705 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006706compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006707{
6708 char_u *p = arg;
6709 int count = 0;
6710
6711 for (;;)
6712 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006713 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006714 return NULL;
6715 ++count;
6716 p = skipwhite(p);
6717 if (ends_excmd(*p))
6718 break;
6719 }
6720
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006721 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6722 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6723 else if (cmdidx == CMD_execute)
6724 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6725 else if (cmdidx == CMD_echomsg)
6726 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6727 else
6728 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729 return p;
6730}
6731
6732/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006733 * A command that is not compiled, execute with legacy code.
6734 */
6735 static char_u *
6736compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6737{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006738 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006739 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006740 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006741
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006742 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006743 goto theend;
6744
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006745 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006746 {
6747 long argt = excmd_get_argt(eap->cmdidx);
6748 int usefilter = FALSE;
6749
6750 has_expr = argt & (EX_XFILE | EX_EXPAND);
6751
6752 // If the command can be followed by a bar, find the bar and truncate
6753 // it, so that the following command can be compiled.
6754 // The '|' is overwritten with a NUL, it is put back below.
6755 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6756 && *eap->arg == '!')
6757 // :w !filter or :r !filter or :r! filter
6758 usefilter = TRUE;
6759 if ((argt & EX_TRLBAR) && !usefilter)
6760 {
6761 separate_nextcmd(eap);
6762 if (eap->nextcmd != NULL)
6763 nextcmd = eap->nextcmd;
6764 }
6765 }
6766
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006767 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6768 {
6769 // expand filename in "syntax include [@group] filename"
6770 has_expr = TRUE;
6771 eap->arg = skipwhite(eap->arg + 7);
6772 if (*eap->arg == '@')
6773 eap->arg = skiptowhite(eap->arg);
6774 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006775
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006776 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006777 {
6778 int count = 0;
6779 char_u *start = skipwhite(line);
6780
6781 // :cmd xxx`=expr1`yyy`=expr2`zzz
6782 // PUSHS ":cmd xxx"
6783 // eval expr1
6784 // PUSHS "yyy"
6785 // eval expr2
6786 // PUSHS "zzz"
6787 // EXECCONCAT 5
6788 for (;;)
6789 {
6790 if (p > start)
6791 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006792 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006793 ++count;
6794 }
6795 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006796 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006797 return NULL;
6798 may_generate_2STRING(-1, cctx);
6799 ++count;
6800 p = skipwhite(p);
6801 if (*p != '`')
6802 {
6803 emsg(_("E1083: missing backtick"));
6804 return NULL;
6805 }
6806 start = p + 1;
6807
6808 p = (char_u *)strstr((char *)start, "`=");
6809 if (p == NULL)
6810 {
6811 if (*skipwhite(start) != NUL)
6812 {
6813 generate_PUSHS(cctx, vim_strsave(start));
6814 ++count;
6815 }
6816 break;
6817 }
6818 }
6819 generate_EXECCONCAT(cctx, count);
6820 }
6821 else
6822 generate_EXEC(cctx, line);
6823
6824theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006825 if (*nextcmd != NUL)
6826 {
6827 // the parser expects a pointer to the bar, put it back
6828 --nextcmd;
6829 *nextcmd = '|';
6830 }
6831
6832 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006833}
6834
6835/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006836 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006837 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006838 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006839 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006840add_def_function(ufunc_T *ufunc)
6841{
6842 dfunc_T *dfunc;
6843
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006844 if (def_functions.ga_len == 0)
6845 {
6846 // The first position is not used, so that a zero uf_dfunc_idx means it
6847 // wasn't set.
6848 if (ga_grow(&def_functions, 1) == FAIL)
6849 return FAIL;
6850 ++def_functions.ga_len;
6851 }
6852
Bram Moolenaar09689a02020-05-09 22:50:08 +02006853 // Add the function to "def_functions".
6854 if (ga_grow(&def_functions, 1) == FAIL)
6855 return FAIL;
6856 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6857 CLEAR_POINTER(dfunc);
6858 dfunc->df_idx = def_functions.ga_len;
6859 ufunc->uf_dfunc_idx = dfunc->df_idx;
6860 dfunc->df_ufunc = ufunc;
6861 ++def_functions.ga_len;
6862 return OK;
6863}
6864
6865/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006866 * After ex_function() has collected all the function lines: parse and compile
6867 * the lines into instructions.
6868 * Adds the function to "def_functions".
6869 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6870 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006871 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006872 * This can be used recursively through compile_lambda(), which may reallocate
6873 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006874 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006876 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006877compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 char_u *line = NULL;
6880 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 cctx_T cctx;
6883 garray_T *instr;
6884 int called_emsg_before = called_emsg;
6885 int ret = FAIL;
6886 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006887 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006888 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006890 // When using a function that was compiled before: Free old instructions.
6891 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006892 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006893 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006894 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6895 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006896 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006898 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006899 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900
Bram Moolenaar985116a2020-07-12 17:31:09 +02006901 ufunc->uf_def_status = UF_COMPILING;
6902
Bram Moolenaara80faa82020-04-12 19:37:17 +02006903 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006904 cctx.ctx_ufunc = ufunc;
6905 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006906 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006907 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6908 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6909 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6910 cctx.ctx_type_list = &ufunc->uf_type_list;
6911 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6912 instr = &cctx.ctx_instr;
6913
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006914 // Set the context to the function, it may be compiled when called from
6915 // another script. Set the script version to the most modern one.
6916 // The line number will be set in next_line_from_context().
6917 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6919
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006920 // Make sure error messages are OK.
6921 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6922 if (do_estack_push)
6923 estack_push_ufunc(ufunc, 1);
6924
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006925 if (ufunc->uf_def_args.ga_len > 0)
6926 {
6927 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006928 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006929 int i;
6930 char_u *arg;
6931 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6932
6933 // Produce instructions for the default values of optional arguments.
6934 // Store the instruction index in uf_def_arg_idx[] so that we know
6935 // where to start when the function is called, depending on the number
6936 // of arguments.
6937 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6938 if (ufunc->uf_def_arg_idx == NULL)
6939 goto erret;
6940 for (i = 0; i < count; ++i)
6941 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006942 garray_T *stack = &cctx.ctx_type_stack;
6943 type_T *val_type;
6944 int arg_idx = first_def_arg + i;
6945
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006946 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6947 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006948 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006949 goto erret;
6950
6951 // If no type specified use the type of the default value.
6952 // Otherwise check that the default value type matches the
6953 // specified type.
6954 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6955 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6956 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006957 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006958 == FAIL)
6959 {
6960 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6961 arg_idx + 1);
6962 goto erret;
6963 }
6964
6965 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006966 goto erret;
6967 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006968 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6969 }
6970
6971 /*
6972 * Loop over all the lines of the function and generate instructions.
6973 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974 for (;;)
6975 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006976 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006977 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006978 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006979 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006980
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006981 // Bail out on the first error to avoid a flood of errors and report
6982 // the right line number when inside try/catch.
6983 if (emsg_before != called_emsg)
6984 goto erret;
6985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006986 if (line != NULL && *line == '|')
6987 // the line continues after a '|'
6988 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006989 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006990 && !(*line == '#' && (line == cctx.ctx_line_start
6991 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006992 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006993 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006994 goto erret;
6995 }
6996 else
6997 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006998 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006999 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007000 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007003 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007004
Bram Moolenaara80faa82020-04-12 19:37:17 +02007005 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007006 ea.cmdlinep = &line;
7007 ea.cmd = skipwhite(line);
7008
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007009 // Some things can be recognized by the first character.
7010 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007012 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007013 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007014 if (ea.cmd[1] != '{')
7015 {
7016 line = (char_u *)"";
7017 continue;
7018 }
7019 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007021 case '}':
7022 {
7023 // "}" ends a block scope
7024 scopetype_T stype = cctx.ctx_scope == NULL
7025 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007027 if (stype == BLOCK_SCOPE)
7028 {
7029 compile_endblock(&cctx);
7030 line = ea.cmd;
7031 }
7032 else
7033 {
7034 emsg(_("E1025: using } outside of a block scope"));
7035 goto erret;
7036 }
7037 if (line != NULL)
7038 line = skipwhite(ea.cmd + 1);
7039 continue;
7040 }
7041
7042 case '{':
7043 // "{" starts a block scope
7044 // "{'a': 1}->func() is something else
7045 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7046 {
7047 line = compile_block(ea.cmd, &cctx);
7048 continue;
7049 }
7050 break;
7051
7052 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007053 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007054 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055 }
7056
7057 /*
7058 * COMMAND MODIFIERS
7059 */
7060 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7061 {
7062 if (errormsg != NULL)
7063 goto erret;
7064 // empty line or comment
7065 line = (char_u *)"";
7066 continue;
7067 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007068 // TODO: use modifiers in the command
7069 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007070 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071
7072 // Skip ":call" to get to the function name.
7073 if (checkforcmd(&ea.cmd, "call", 3))
7074 ea.cmd = skipwhite(ea.cmd);
7075
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007076 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007078 char_u *pskip;
7079
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007080 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007081 // find what follows.
7082 // Skip over "var.member", "var[idx]" and the like.
7083 // Also "&opt = val", "$ENV = val" and "@r = val".
7084 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007085 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007086 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007087 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007089 char_u *var_end;
7090 int oplen;
7091 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007093 var_end = find_name_end(pskip, NULL, NULL,
7094 FNE_CHECK_START | FNE_INCL_BR);
7095 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007096 if (oplen > 0)
7097 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007098 size_t len = p - ea.cmd;
7099
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007100 // Recognize an assignment if we recognize the variable
7101 // name:
7102 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007103 // "local = expr" where "local" is a local var.
7104 // "script = expr" where "script" is a script-local var.
7105 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007106 // "&opt = expr"
7107 // "$ENV = expr"
7108 // "@r = expr"
7109 if (*ea.cmd == '&'
7110 || *ea.cmd == '$'
7111 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007112 || ((len) > 2 && ea.cmd[1] == ':')
7113 || lookup_local(ea.cmd, len, &cctx) != NULL
7114 || lookup_arg(ea.cmd, len, NULL, NULL,
7115 NULL, &cctx) == OK
7116 || lookup_script(ea.cmd, len) == OK
7117 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007118 {
7119 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007120 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007121 goto erret;
7122 continue;
7123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007124 }
7125 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007126
7127 if (*ea.cmd == '[')
7128 {
7129 // [var, var] = expr
7130 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7131 if (line == NULL)
7132 goto erret;
7133 if (line != ea.cmd)
7134 continue;
7135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136 }
7137
7138 /*
7139 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007140 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007142 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007143 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007144 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007145 ea.cmd = skip_range(ea.cmd, NULL);
7146 if (ea.cmd > cmd && !starts_with_colon)
7147 {
7148 emsg(_(e_colon_required));
7149 goto erret;
7150 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007151 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007152 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007153 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007154 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155
7156 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7157 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007158 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007159 {
7160 line += STRLEN(line);
7161 continue;
7162 }
7163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007165 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007167 // CMD_let cannot happen, compile_assignment() above is used
7168 iemsg("Command from find_ex_command() not handled");
7169 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171 }
7172
7173 p = skipwhite(p);
7174
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007175 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007176 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007177 && ea.cmdidx != CMD_elseif
7178 && ea.cmdidx != CMD_else
7179 && ea.cmdidx != CMD_endif)
7180 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007181 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007182 continue;
7183 }
7184
Bram Moolenaarefd88552020-06-18 20:50:10 +02007185 if (ea.cmdidx != CMD_elseif
7186 && ea.cmdidx != CMD_else
7187 && ea.cmdidx != CMD_endif
7188 && ea.cmdidx != CMD_endfor
7189 && ea.cmdidx != CMD_endwhile
7190 && ea.cmdidx != CMD_catch
7191 && ea.cmdidx != CMD_finally
7192 && ea.cmdidx != CMD_endtry)
7193 {
7194 if (cctx.ctx_had_return)
7195 {
7196 emsg(_("E1095: Unreachable code after :return"));
7197 goto erret;
7198 }
7199 }
7200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007201 switch (ea.cmdidx)
7202 {
7203 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007204 ea.arg = p;
7205 line = compile_nested_function(&ea, &cctx);
7206 break;
7207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007208 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007209 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007210 goto erret;
7211
7212 case CMD_return:
7213 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007214 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215 break;
7216
7217 case CMD_let:
7218 case CMD_const:
7219 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007220 if (line == p)
7221 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222 break;
7223
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007224 case CMD_unlet:
7225 case CMD_unlockvar:
7226 case CMD_lockvar:
7227 line = compile_unletlock(p, &ea, &cctx);
7228 break;
7229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007230 case CMD_import:
7231 line = compile_import(p, &cctx);
7232 break;
7233
7234 case CMD_if:
7235 line = compile_if(p, &cctx);
7236 break;
7237 case CMD_elseif:
7238 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007239 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007240 break;
7241 case CMD_else:
7242 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007243 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244 break;
7245 case CMD_endif:
7246 line = compile_endif(p, &cctx);
7247 break;
7248
7249 case CMD_while:
7250 line = compile_while(p, &cctx);
7251 break;
7252 case CMD_endwhile:
7253 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007254 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007255 break;
7256
7257 case CMD_for:
7258 line = compile_for(p, &cctx);
7259 break;
7260 case CMD_endfor:
7261 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007262 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263 break;
7264 case CMD_continue:
7265 line = compile_continue(p, &cctx);
7266 break;
7267 case CMD_break:
7268 line = compile_break(p, &cctx);
7269 break;
7270
7271 case CMD_try:
7272 line = compile_try(p, &cctx);
7273 break;
7274 case CMD_catch:
7275 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007276 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 break;
7278 case CMD_finally:
7279 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007280 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 break;
7282 case CMD_endtry:
7283 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007284 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007285 break;
7286 case CMD_throw:
7287 line = compile_throw(p, &cctx);
7288 break;
7289
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007290 case CMD_eval:
7291 if (compile_expr0(&p, &cctx) == FAIL)
7292 goto erret;
7293
7294 // drop the return value
7295 generate_instr_drop(&cctx, ISN_DROP, 1);
7296
7297 line = skipwhite(p);
7298 break;
7299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007300 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007302 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007303 case CMD_echomsg:
7304 case CMD_echoerr:
7305 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007306 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007307
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007308 // TODO: other commands with an expression argument
7309
Bram Moolenaar002262f2020-07-08 17:47:57 +02007310 case CMD_SIZE:
7311 semsg(_("E476: Invalid command: %s"), ea.cmd);
7312 goto erret;
7313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007315 // Not recognized, execute with do_cmdline_cmd().
7316 ea.arg = p;
7317 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007318 break;
7319 }
7320 if (line == NULL)
7321 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007322 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323
7324 if (cctx.ctx_type_stack.ga_len < 0)
7325 {
7326 iemsg("Type stack underflow");
7327 goto erret;
7328 }
7329 }
7330
7331 if (cctx.ctx_scope != NULL)
7332 {
7333 if (cctx.ctx_scope->se_type == IF_SCOPE)
7334 emsg(_(e_endif));
7335 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7336 emsg(_(e_endwhile));
7337 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7338 emsg(_(e_endfor));
7339 else
7340 emsg(_("E1026: Missing }"));
7341 goto erret;
7342 }
7343
Bram Moolenaarefd88552020-06-18 20:50:10 +02007344 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345 {
7346 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7347 {
7348 emsg(_("E1027: Missing return statement"));
7349 goto erret;
7350 }
7351
7352 // Return zero if there is no return at the end.
7353 generate_PUSHNR(&cctx, 0);
7354 generate_instr(&cctx, ISN_RETURN);
7355 }
7356
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007357 {
7358 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7359 + ufunc->uf_dfunc_idx;
7360 dfunc->df_deleted = FALSE;
7361 dfunc->df_instr = instr->ga_data;
7362 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007363 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007364 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007365 if (cctx.ctx_outer_used)
7366 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007367 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369
7370 ret = OK;
7371
7372erret:
7373 if (ret == FAIL)
7374 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007375 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007376 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7377 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007378
7379 for (idx = 0; idx < instr->ga_len; ++idx)
7380 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007382
Bram Moolenaar45a15082020-05-25 00:28:33 +02007383 // if using the last entry in the table we might as well remove it
7384 if (!dfunc->df_deleted
7385 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007386 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007387 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007388
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007389 while (cctx.ctx_scope != NULL)
7390 drop_scope(&cctx);
7391
Bram Moolenaar20431c92020-03-20 18:39:46 +01007392 // Don't execute this function body.
7393 ga_clear_strings(&ufunc->uf_lines);
7394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 if (errormsg != NULL)
7396 emsg(errormsg);
7397 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007398 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 }
7400
7401 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007402 if (do_estack_push)
7403 estack_pop();
7404
Bram Moolenaar20431c92020-03-20 18:39:46 +01007405 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007406 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007408 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409}
7410
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007411 void
7412set_function_type(ufunc_T *ufunc)
7413{
7414 int varargs = ufunc->uf_va_name != NULL;
7415 int argcount = ufunc->uf_args.ga_len;
7416
7417 // Create a type for the function, with the return type and any
7418 // argument types.
7419 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7420 // The type is included in "tt_args".
7421 if (argcount > 0 || varargs)
7422 {
7423 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7424 argcount, &ufunc->uf_type_list);
7425 // Add argument types to the function type.
7426 if (func_type_add_arg_types(ufunc->uf_func_type,
7427 argcount + varargs,
7428 &ufunc->uf_type_list) == FAIL)
7429 return;
7430 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7431 ufunc->uf_func_type->tt_min_argcount =
7432 argcount - ufunc->uf_def_args.ga_len;
7433 if (ufunc->uf_arg_types == NULL)
7434 {
7435 int i;
7436
7437 // lambda does not have argument types.
7438 for (i = 0; i < argcount; ++i)
7439 ufunc->uf_func_type->tt_args[i] = &t_any;
7440 }
7441 else
7442 mch_memmove(ufunc->uf_func_type->tt_args,
7443 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7444 if (varargs)
7445 {
7446 ufunc->uf_func_type->tt_args[argcount] =
7447 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7448 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7449 }
7450 }
7451 else
7452 // No arguments, can use a predefined type.
7453 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7454 argcount, &ufunc->uf_type_list);
7455}
7456
7457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458/*
7459 * Delete an instruction, free what it contains.
7460 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007461 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462delete_instr(isn_T *isn)
7463{
7464 switch (isn->isn_type)
7465 {
7466 case ISN_EXEC:
7467 case ISN_LOADENV:
7468 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007469 case ISN_LOADB:
7470 case ISN_LOADW:
7471 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007473 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474 case ISN_PUSHEXC:
7475 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007476 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007478 case ISN_STOREB:
7479 case ISN_STOREW:
7480 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007481 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007482 vim_free(isn->isn_arg.string);
7483 break;
7484
7485 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007486 case ISN_STORES:
7487 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 break;
7489
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007490 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007491 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007492 vim_free(isn->isn_arg.unlet.ul_name);
7493 break;
7494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007495 case ISN_STOREOPT:
7496 vim_free(isn->isn_arg.storeopt.so_name);
7497 break;
7498
7499 case ISN_PUSHBLOB: // push blob isn_arg.blob
7500 blob_unref(isn->isn_arg.blob);
7501 break;
7502
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007503 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007504#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007505 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007506#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007507 break;
7508
7509 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007510#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007511 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007512#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007513 break;
7514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515 case ISN_UCALL:
7516 vim_free(isn->isn_arg.ufunc.cuf_name);
7517 break;
7518
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007519 case ISN_FUNCREF:
7520 {
7521 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7522 + isn->isn_arg.funcref.fr_func;
7523 func_ptr_unref(dfunc->df_ufunc);
7524 }
7525 break;
7526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007527 case ISN_2BOOL:
7528 case ISN_2STRING:
7529 case ISN_ADDBLOB:
7530 case ISN_ADDLIST:
7531 case ISN_BCALL:
7532 case ISN_CATCH:
7533 case ISN_CHECKNR:
7534 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007535 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007536 case ISN_COMPAREANY:
7537 case ISN_COMPAREBLOB:
7538 case ISN_COMPAREBOOL:
7539 case ISN_COMPAREDICT:
7540 case ISN_COMPAREFLOAT:
7541 case ISN_COMPAREFUNC:
7542 case ISN_COMPARELIST:
7543 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007544 case ISN_COMPARESPECIAL:
7545 case ISN_COMPARESTRING:
7546 case ISN_CONCAT:
7547 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007548 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549 case ISN_DROP:
7550 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007551 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007552 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007553 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007554 case ISN_EXECCONCAT:
7555 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007557 case ISN_LISTINDEX:
7558 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007559 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007560 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007561 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562 case ISN_JUMP:
7563 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007564 case ISN_LOADBDICT:
7565 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007566 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007568 case ISN_LOADSCRIPT:
7569 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007570 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007571 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572 case ISN_NEGATENR:
7573 case ISN_NEWDICT:
7574 case ISN_NEWLIST:
7575 case ISN_OPNR:
7576 case ISN_OPFLOAT:
7577 case ISN_OPANY:
7578 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007579 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580 case ISN_PUSHF:
7581 case ISN_PUSHNR:
7582 case ISN_PUSHBOOL:
7583 case ISN_PUSHSPEC:
7584 case ISN_RETURN:
7585 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007586 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007587 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007588 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007589 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007590 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007591 case ISN_STOREDICT:
7592 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007593 case ISN_THROW:
7594 case ISN_TRY:
7595 // nothing allocated
7596 break;
7597 }
7598}
7599
7600/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007601 * Free all instructions for "dfunc".
7602 */
7603 static void
7604delete_def_function_contents(dfunc_T *dfunc)
7605{
7606 int idx;
7607
7608 ga_clear(&dfunc->df_def_args_isn);
7609
7610 if (dfunc->df_instr != NULL)
7611 {
7612 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7613 delete_instr(dfunc->df_instr + idx);
7614 VIM_CLEAR(dfunc->df_instr);
7615 }
7616
7617 dfunc->df_deleted = TRUE;
7618}
7619
7620/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007621 * When a user function is deleted, clear the contents of any associated def
7622 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007623 */
7624 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007625clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007627 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007628 {
7629 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7630 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631
Bram Moolenaar20431c92020-03-20 18:39:46 +01007632 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007633 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007634 }
7635}
7636
7637#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007638/*
7639 * Free all functions defined with ":def".
7640 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007641 void
7642free_def_functions(void)
7643{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007644 int idx;
7645
7646 for (idx = 0; idx < def_functions.ga_len; ++idx)
7647 {
7648 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7649
7650 delete_def_function_contents(dfunc);
7651 }
7652
7653 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654}
7655#endif
7656
7657
7658#endif // FEAT_EVAL