blob: fa7171c8a4ed6b8218e3dbd299cfd2b19f06b3b1 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
146static char e_var_notfound[] = N_("E1001: variable not found: %s");
147static char e_syntax_at[] = N_("E1002: Syntax error at %s");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200148static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200149static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +0200150static char e_namespace[] = N_("E1075: Namespace not supported: %s");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151
Bram Moolenaar20431c92020-03-20 18:39:46 +0100152static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200153static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154
155/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200156 * Lookup variable "name" in the local scope and return it.
157 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160lookup_local(char_u *name, size_t len, cctx_T *cctx)
161{
162 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200163 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200166 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100172 if (STRNCMP(name, lvar->lv_name, len) == 0
173 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
175 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200176 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100178 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200179
180 // Find local in outer function scope.
181 if (cctx->ctx_outer != NULL)
182 {
183 lvar = lookup_local(name, len, cctx->ctx_outer);
184 if (lvar != NULL)
185 {
186 // TODO: are there situations we should not mark the outer scope as
187 // used?
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 return lvar;
191 }
192 }
193
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195}
196
197/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198 * Lookup an argument in the current function and an enclosing function.
199 * Returns the argument index in "idxp"
200 * Returns the argument type in "type"
201 * Sets "gen_load_outer" to TRUE if found in outer scope.
202 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203 */
204 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200205lookup_arg(
206 char_u *name,
207 size_t len,
208 int *idxp,
209 type_T **type,
210 int *gen_load_outer,
211 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212{
213 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200214 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100216 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
219 {
220 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
221
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200222 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
223 {
224 if (idxp != NULL)
225 {
226 // Arguments are located above the frame pointer. One further
227 // if there is a vararg argument
228 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
229 + STACK_FRAME_SIZE)
230 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
231
232 if (cctx->ctx_ufunc->uf_arg_types != NULL)
233 *type = cctx->ctx_ufunc->uf_arg_types[idx];
234 else
235 *type = &t_any;
236 }
237 return OK;
238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200241 va_name = cctx->ctx_ufunc->uf_va_name;
242 if (va_name != NULL
243 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
244 {
245 if (idxp != NULL)
246 {
247 // varargs is always the last argument
248 *idxp = -STACK_FRAME_SIZE - 1;
249 *type = cctx->ctx_ufunc->uf_va_type;
250 }
251 return OK;
252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200254 if (cctx->ctx_outer != NULL)
255 {
256 // Lookup the name for an argument of the outer function.
257 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
258 == OK)
259 {
260 *gen_load_outer = TRUE;
261 return OK;
262 }
263 }
264
265 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266}
267
268/*
269 * Lookup a variable in the current script.
270 * Returns OK or FAIL.
271 */
272 static int
273lookup_script(char_u *name, size_t len)
274{
275 int cc;
276 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
277 dictitem_T *di;
278
279 cc = name[len];
280 name[len] = NUL;
281 di = find_var_in_ht(ht, 0, name, TRUE);
282 name[len] = cc;
283 return di == NULL ? FAIL: OK;
284}
285
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100286/*
287 * Check if "p[len]" is already defined, either in script "import_sid" or in
288 * compilation context "cctx".
289 * Return FAIL and give an error if it defined.
290 */
291 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200292check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100293{
294 if (lookup_script(p, len) == OK
295 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200296 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100297 || find_imported(p, len, cctx) != NULL)))
298 {
299 semsg("E1073: imported name already defined: %s", p);
300 return FAIL;
301 }
302 return OK;
303}
304
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200305/*
306 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
307 * be freed later.
308 */
309 static type_T *
310alloc_type(garray_T *type_gap)
311{
312 type_T *type;
313
314 if (ga_grow(type_gap, 1) == FAIL)
315 return NULL;
316 type = ALLOC_CLEAR_ONE(type_T);
317 if (type != NULL)
318 {
319 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
320 ++type_gap->ga_len;
321 }
322 return type;
323}
324
Bram Moolenaar6110e792020-07-08 19:35:21 +0200325 void
326clear_type_list(garray_T *gap)
327{
328 while (gap->ga_len > 0)
329 vim_free(((type_T **)gap->ga_data)[--gap->ga_len]);
330 ga_clear(gap);
331}
332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100333 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200334get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335{
336 type_T *type;
337
338 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200339 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200341 if (member_type->tt_type == VAR_VOID
342 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100343 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100344 if (member_type->tt_type == VAR_BOOL)
345 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 if (member_type->tt_type == VAR_NUMBER)
347 return &t_list_number;
348 if (member_type->tt_type == VAR_STRING)
349 return &t_list_string;
350
351 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200352 type = alloc_type(type_gap);
353 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100354 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355 type->tt_type = VAR_LIST;
356 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200357 type->tt_argcount = 0;
358 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100359 return type;
360}
361
362 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200363get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364{
365 type_T *type;
366
367 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200368 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200370 if (member_type->tt_type == VAR_VOID
371 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100372 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100373 if (member_type->tt_type == VAR_BOOL)
374 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375 if (member_type->tt_type == VAR_NUMBER)
376 return &t_dict_number;
377 if (member_type->tt_type == VAR_STRING)
378 return &t_dict_string;
379
380 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200381 type = alloc_type(type_gap);
382 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100383 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 type->tt_type = VAR_DICT;
385 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200386 type->tt_argcount = 0;
387 type->tt_args = NULL;
388 return type;
389}
390
391/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200392 * Allocate a new type for a function.
393 */
394 static type_T *
395alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
396{
397 type_T *type = alloc_type(type_gap);
398
399 if (type == NULL)
400 return &t_any;
401 type->tt_type = VAR_FUNC;
402 type->tt_member = ret_type;
403 type->tt_argcount = argcount;
404 type->tt_args = NULL;
405 return type;
406}
407
408/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200409 * Get a function type, based on the return type "ret_type".
410 * If "argcount" is -1 or 0 a predefined type can be used.
411 * If "argcount" > 0 always create a new type, so that arguments can be added.
412 */
413 static type_T *
414get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
415{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200416 // recognize commonly used types
417 if (argcount <= 0)
418 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200419 if (ret_type == &t_unknown)
420 {
421 // (argcount == 0) is not possible
422 return &t_func_unknown;
423 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200424 if (ret_type == &t_void)
425 {
426 if (argcount == 0)
427 return &t_func_0_void;
428 else
429 return &t_func_void;
430 }
431 if (ret_type == &t_any)
432 {
433 if (argcount == 0)
434 return &t_func_0_any;
435 else
436 return &t_func_any;
437 }
438 if (ret_type == &t_number)
439 {
440 if (argcount == 0)
441 return &t_func_0_number;
442 else
443 return &t_func_number;
444 }
445 if (ret_type == &t_string)
446 {
447 if (argcount == 0)
448 return &t_func_0_string;
449 else
450 return &t_func_string;
451 }
452 }
453
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200454 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455}
456
Bram Moolenaara8c17702020-04-01 21:17:24 +0200457/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200458 * For a function type, reserve space for "argcount" argument types (including
459 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200460 */
461 static int
462func_type_add_arg_types(
463 type_T *functype,
464 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200465 garray_T *type_gap)
466{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200467 // To make it easy to free the space needed for the argument types, add the
468 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200469 if (ga_grow(type_gap, 1) == FAIL)
470 return FAIL;
471 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
472 if (functype->tt_args == NULL)
473 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200474 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
475 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200477 return OK;
478}
479
480/*
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200481 * Get a type_T for a typval_T.
482 * "type_list" is used to temporarily create types in.
Bram Moolenaara8c17702020-04-01 21:17:24 +0200483 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200484 type_T *
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200485typval2type(typval_T *tv, garray_T *type_gap)
Bram Moolenaara8c17702020-04-01 21:17:24 +0200486{
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200487 type_T *actual;
488 type_T *member_type;
489
Bram Moolenaara8c17702020-04-01 21:17:24 +0200490 if (tv->v_type == VAR_NUMBER)
491 return &t_number;
492 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200493 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200494 if (tv->v_type == VAR_STRING)
495 return &t_string;
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200496
497 if (tv->v_type == VAR_LIST
498 && tv->vval.v_list != NULL
499 && tv->vval.v_list->lv_first != NULL)
500 {
501 // Use the type of the first member, it is the most specific.
502 member_type = typval2type(&tv->vval.v_list->lv_first->li_tv, type_gap);
503 return get_list_type(member_type, type_gap);
504 }
505
506 if (tv->v_type == VAR_DICT
507 && tv->vval.v_dict != NULL
508 && tv->vval.v_dict->dv_hashtab.ht_used > 0)
509 {
510 dict_iterator_T iter;
511 typval_T *value;
512
513 // Use the type of the first value, it is the most specific.
514 dict_iterate_start(tv, &iter);
515 dict_iterate_next(&iter, &value);
516 member_type = typval2type(value, type_gap);
517 return get_dict_type(member_type, type_gap);
518 }
519
520 if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
521 {
522 char_u *name = NULL;
523 ufunc_T *ufunc = NULL;
524
525 if (tv->v_type == VAR_PARTIAL)
526 {
527 if (tv->vval.v_partial->pt_func != NULL)
528 ufunc = tv->vval.v_partial->pt_func;
529 else
530 name = tv->vval.v_partial->pt_name;
531 }
532 else
533 name = tv->vval.v_string;
534 if (name != NULL)
535 // TODO: how about a builtin function?
536 ufunc = find_func(name, FALSE, NULL);
537 if (ufunc != NULL && ufunc->uf_func_type != NULL)
538 return ufunc->uf_func_type;
539 }
540
541 actual = alloc_type(type_gap);
542 if (actual == NULL)
543 return NULL;
544 actual->tt_type = tv->v_type;
545 actual->tt_member = &t_any;
546
547 return actual;
548}
549
550/*
551 * Get a type_T for a typval_T, used for v: variables.
552 * "type_list" is used to temporarily create types in.
553 */
554 type_T *
555typval2type_vimvar(typval_T *tv, garray_T *type_gap)
556{
Bram Moolenaara8c17702020-04-01 21:17:24 +0200557 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
558 return &t_list_string;
559 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
560 return &t_dict_any;
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200561 return typval2type(tv, type_gap);
562}
563
564
565/*
566 * Return FAIL if "expected" and "actual" don't match.
567 */
568 int
569check_typval_type(type_T *expected, typval_T *actual_tv)
570{
571 garray_T type_list;
572 type_T *actual_type;
573 int res = FAIL;
574
575 ga_init2(&type_list, sizeof(type_T *), 10);
576 actual_type = typval2type(actual_tv, &type_list);
577 if (actual_type != NULL)
578 res = check_type(expected, actual_type, TRUE);
579 clear_type_list(&type_list);
580 return res;
Bram Moolenaara8c17702020-04-01 21:17:24 +0200581}
582
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200583 static void
584type_mismatch(type_T *expected, type_T *actual)
585{
586 char *tofree1, *tofree2;
587
588 semsg(_("E1013: type mismatch, expected %s but got %s"),
589 type_name(expected, &tofree1), type_name(actual, &tofree2));
590 vim_free(tofree1);
591 vim_free(tofree2);
592}
593
594 static void
595arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
596{
597 char *tofree1, *tofree2;
598
599 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
600 argidx,
601 type_name(expected, &tofree1), type_name(actual, &tofree2));
602 vim_free(tofree1);
603 vim_free(tofree2);
604}
605
606/*
607 * Check if the expected and actual types match.
608 * Does not allow for assigning "any" to a specific type.
609 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200610 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200611check_type(type_T *expected, type_T *actual, int give_msg)
612{
613 int ret = OK;
614
615 // When expected is "unknown" we accept any actual type.
616 // When expected is "any" we accept any actual type except "void".
617 if (expected->tt_type != VAR_UNKNOWN
618 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
619
620 {
621 if (expected->tt_type != actual->tt_type)
622 {
623 if (give_msg)
624 type_mismatch(expected, actual);
625 return FAIL;
626 }
627 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
628 {
629 // "unknown" is used for an empty list or dict
630 if (actual->tt_member != &t_unknown)
631 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
632 }
633 else if (expected->tt_type == VAR_FUNC)
634 {
635 if (expected->tt_member != &t_unknown)
636 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
637 if (ret == OK && expected->tt_argcount != -1
638 && (actual->tt_argcount < expected->tt_min_argcount
639 || actual->tt_argcount > expected->tt_argcount))
640 ret = FAIL;
641 }
642 if (ret == FAIL && give_msg)
643 type_mismatch(expected, actual);
644 }
645 return ret;
646}
647
Bram Moolenaar65b95452020-07-19 14:03:09 +0200648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649/////////////////////////////////////////////////////////////////////
650// Following generate_ functions expect the caller to call ga_grow().
651
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200652#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
653#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655/*
656 * Generate an instruction without arguments.
657 * Returns a pointer to the new instruction, NULL if failed.
658 */
659 static isn_T *
660generate_instr(cctx_T *cctx, isntype_T isn_type)
661{
662 garray_T *instr = &cctx->ctx_instr;
663 isn_T *isn;
664
Bram Moolenaar080457c2020-03-03 21:53:32 +0100665 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 if (ga_grow(instr, 1) == FAIL)
667 return NULL;
668 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
669 isn->isn_type = isn_type;
670 isn->isn_lnum = cctx->ctx_lnum + 1;
671 ++instr->ga_len;
672
673 return isn;
674}
675
676/*
677 * Generate an instruction without arguments.
678 * "drop" will be removed from the stack.
679 * Returns a pointer to the new instruction, NULL if failed.
680 */
681 static isn_T *
682generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
683{
684 garray_T *stack = &cctx->ctx_type_stack;
685
Bram Moolenaar080457c2020-03-03 21:53:32 +0100686 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100687 stack->ga_len -= drop;
688 return generate_instr(cctx, isn_type);
689}
690
691/*
692 * Generate instruction "isn_type" and put "type" on the type stack.
693 */
694 static isn_T *
695generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
696{
697 isn_T *isn;
698 garray_T *stack = &cctx->ctx_type_stack;
699
700 if ((isn = generate_instr(cctx, isn_type)) == NULL)
701 return NULL;
702
703 if (ga_grow(stack, 1) == FAIL)
704 return NULL;
705 ((type_T **)stack->ga_data)[stack->ga_len] = type;
706 ++stack->ga_len;
707
708 return isn;
709}
710
711/*
712 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
713 */
714 static int
715may_generate_2STRING(int offset, cctx_T *cctx)
716{
717 isn_T *isn;
718 garray_T *stack = &cctx->ctx_type_stack;
719 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
720
721 if ((*type)->tt_type == VAR_STRING)
722 return OK;
723 *type = &t_string;
724
725 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
726 return FAIL;
727 isn->isn_arg.number = offset;
728
729 return OK;
730}
731
732 static int
733check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
734{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200735 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200737 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 {
739 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200740 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 else
742 semsg(_("E1036: %c requires number or float arguments"), *op);
743 return FAIL;
744 }
745 return OK;
746}
747
748/*
749 * Generate an instruction with two arguments. The instruction depends on the
750 * type of the arguments.
751 */
752 static int
753generate_two_op(cctx_T *cctx, char_u *op)
754{
755 garray_T *stack = &cctx->ctx_type_stack;
756 type_T *type1;
757 type_T *type2;
758 vartype_T vartype;
759 isn_T *isn;
760
Bram Moolenaar080457c2020-03-03 21:53:32 +0100761 RETURN_OK_IF_SKIP(cctx);
762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763 // Get the known type of the two items on the stack. If they are matching
764 // use a type-specific instruction. Otherwise fall back to runtime type
765 // checking.
766 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
767 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200768 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 if (type1->tt_type == type2->tt_type
770 && (type1->tt_type == VAR_NUMBER
771 || type1->tt_type == VAR_LIST
772#ifdef FEAT_FLOAT
773 || type1->tt_type == VAR_FLOAT
774#endif
775 || type1->tt_type == VAR_BLOB))
776 vartype = type1->tt_type;
777
778 switch (*op)
779 {
780 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200781 && type1->tt_type != VAR_ANY
782 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 && check_number_or_float(
784 type1->tt_type, type2->tt_type, op) == FAIL)
785 return FAIL;
786 isn = generate_instr_drop(cctx,
787 vartype == VAR_NUMBER ? ISN_OPNR
788 : vartype == VAR_LIST ? ISN_ADDLIST
789 : vartype == VAR_BLOB ? ISN_ADDBLOB
790#ifdef FEAT_FLOAT
791 : vartype == VAR_FLOAT ? ISN_OPFLOAT
792#endif
793 : ISN_OPANY, 1);
794 if (isn != NULL)
795 isn->isn_arg.op.op_type = EXPR_ADD;
796 break;
797
798 case '-':
799 case '*':
800 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
801 op) == FAIL)
802 return FAIL;
803 if (vartype == VAR_NUMBER)
804 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
805#ifdef FEAT_FLOAT
806 else if (vartype == VAR_FLOAT)
807 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
808#endif
809 else
810 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
811 if (isn != NULL)
812 isn->isn_arg.op.op_type = *op == '*'
813 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
814 break;
815
Bram Moolenaar4c683752020-04-05 21:38:23 +0200816 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200818 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 && type2->tt_type != VAR_NUMBER))
820 {
821 emsg(_("E1035: % requires number arguments"));
822 return FAIL;
823 }
824 isn = generate_instr_drop(cctx,
825 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
826 if (isn != NULL)
827 isn->isn_arg.op.op_type = EXPR_REM;
828 break;
829 }
830
831 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200832 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 {
834 type_T *type = &t_any;
835
836#ifdef FEAT_FLOAT
837 // float+number and number+float results in float
838 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
839 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
840 type = &t_float;
841#endif
842 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
843 }
844
845 return OK;
846}
847
848/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200849 * Get the instruction to use for comparing "type1" with "type2"
850 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200852 static isntype_T
853get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854{
855 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856
Bram Moolenaar4c683752020-04-05 21:38:23 +0200857 if (type1 == VAR_UNKNOWN)
858 type1 = VAR_ANY;
859 if (type2 == VAR_UNKNOWN)
860 type2 = VAR_ANY;
861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 if (type1 == type2)
863 {
864 switch (type1)
865 {
866 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
867 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
868 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
869 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
870 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
871 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
872 case VAR_LIST: isntype = ISN_COMPARELIST; break;
873 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
874 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875 default: isntype = ISN_COMPAREANY; break;
876 }
877 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200878 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
880 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
881 isntype = ISN_COMPAREANY;
882
883 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
884 && (isntype == ISN_COMPAREBOOL
885 || isntype == ISN_COMPARESPECIAL
886 || isntype == ISN_COMPARENR
887 || isntype == ISN_COMPAREFLOAT))
888 {
889 semsg(_("E1037: Cannot use \"%s\" with %s"),
890 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200891 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 }
893 if (isntype == ISN_DROP
894 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
895 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
896 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
897 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
898 && exptype != EXPR_IS && exptype != EXPR_ISNOT
899 && (type1 == VAR_BLOB || type2 == VAR_BLOB
900 || type1 == VAR_LIST || type2 == VAR_LIST))))
901 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100902 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200904 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200906 return isntype;
907}
908
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200909 int
910check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
911{
912 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
913 return FAIL;
914 return OK;
915}
916
Bram Moolenaara5565e42020-05-09 15:44:01 +0200917/*
918 * Generate an ISN_COMPARE* instruction with a boolean result.
919 */
920 static int
921generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
922{
923 isntype_T isntype;
924 isn_T *isn;
925 garray_T *stack = &cctx->ctx_type_stack;
926 vartype_T type1;
927 vartype_T type2;
928
929 RETURN_OK_IF_SKIP(cctx);
930
931 // Get the known type of the two items on the stack. If they are matching
932 // use a type-specific instruction. Otherwise fall back to runtime type
933 // checking.
934 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
935 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
936 isntype = get_compare_isn(exptype, type1, type2);
937 if (isntype == ISN_DROP)
938 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939
940 if ((isn = generate_instr(cctx, isntype)) == NULL)
941 return FAIL;
942 isn->isn_arg.op.op_type = exptype;
943 isn->isn_arg.op.op_ic = ic;
944
945 // takes two arguments, puts one bool back
946 if (stack->ga_len >= 2)
947 {
948 --stack->ga_len;
949 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
950 }
951
952 return OK;
953}
954
955/*
956 * Generate an ISN_2BOOL instruction.
957 */
958 static int
959generate_2BOOL(cctx_T *cctx, int invert)
960{
961 isn_T *isn;
962 garray_T *stack = &cctx->ctx_type_stack;
963
Bram Moolenaar080457c2020-03-03 21:53:32 +0100964 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
966 return FAIL;
967 isn->isn_arg.number = invert;
968
969 // type becomes bool
970 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
971
972 return OK;
973}
974
975 static int
976generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
977{
978 isn_T *isn;
979 garray_T *stack = &cctx->ctx_type_stack;
980
Bram Moolenaar080457c2020-03-03 21:53:32 +0100981 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
983 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200984 // TODO: whole type, e.g. for a function also arg and return types
985 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 isn->isn_arg.type.ct_off = offset;
987
988 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200989 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990
991 return OK;
992}
993
994/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200995 * Check that
996 * - "actual" is "expected" type or
997 * - "actual" is a type that can be "expected" type: add a runtime check; or
998 * - return FAIL.
999 */
1000 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001001need_type(
1002 type_T *actual,
1003 type_T *expected,
1004 int offset,
1005 cctx_T *cctx,
1006 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001007{
1008 if (check_type(expected, actual, FALSE) == OK)
1009 return OK;
1010 if (actual->tt_type != VAR_ANY
1011 && actual->tt_type != VAR_UNKNOWN
1012 && !(actual->tt_type == VAR_FUNC
1013 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
1014 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001015 if (!silent)
1016 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001017 return FAIL;
1018 }
1019 generate_TYPECHECK(cctx, expected, offset);
1020 return OK;
1021}
1022
1023/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 * Generate an ISN_PUSHNR instruction.
1025 */
1026 static int
1027generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1028{
1029 isn_T *isn;
1030
Bram Moolenaar080457c2020-03-03 21:53:32 +01001031 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1033 return FAIL;
1034 isn->isn_arg.number = number;
1035
1036 return OK;
1037}
1038
1039/*
1040 * Generate an ISN_PUSHBOOL instruction.
1041 */
1042 static int
1043generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1044{
1045 isn_T *isn;
1046
Bram Moolenaar080457c2020-03-03 21:53:32 +01001047 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1049 return FAIL;
1050 isn->isn_arg.number = number;
1051
1052 return OK;
1053}
1054
1055/*
1056 * Generate an ISN_PUSHSPEC instruction.
1057 */
1058 static int
1059generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1060{
1061 isn_T *isn;
1062
Bram Moolenaar080457c2020-03-03 21:53:32 +01001063 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1065 return FAIL;
1066 isn->isn_arg.number = number;
1067
1068 return OK;
1069}
1070
1071#ifdef FEAT_FLOAT
1072/*
1073 * Generate an ISN_PUSHF instruction.
1074 */
1075 static int
1076generate_PUSHF(cctx_T *cctx, float_T fnumber)
1077{
1078 isn_T *isn;
1079
Bram Moolenaar080457c2020-03-03 21:53:32 +01001080 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1082 return FAIL;
1083 isn->isn_arg.fnumber = fnumber;
1084
1085 return OK;
1086}
1087#endif
1088
1089/*
1090 * Generate an ISN_PUSHS instruction.
1091 * Consumes "str".
1092 */
1093 static int
1094generate_PUSHS(cctx_T *cctx, char_u *str)
1095{
1096 isn_T *isn;
1097
Bram Moolenaar080457c2020-03-03 21:53:32 +01001098 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1100 return FAIL;
1101 isn->isn_arg.string = str;
1102
1103 return OK;
1104}
1105
1106/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001107 * Generate an ISN_PUSHCHANNEL instruction.
1108 * Consumes "channel".
1109 */
1110 static int
1111generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1112{
1113 isn_T *isn;
1114
Bram Moolenaar080457c2020-03-03 21:53:32 +01001115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001116 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1117 return FAIL;
1118 isn->isn_arg.channel = channel;
1119
1120 return OK;
1121}
1122
1123/*
1124 * Generate an ISN_PUSHJOB instruction.
1125 * Consumes "job".
1126 */
1127 static int
1128generate_PUSHJOB(cctx_T *cctx, job_T *job)
1129{
1130 isn_T *isn;
1131
Bram Moolenaar080457c2020-03-03 21:53:32 +01001132 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001133 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001134 return FAIL;
1135 isn->isn_arg.job = job;
1136
1137 return OK;
1138}
1139
1140/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 * Generate an ISN_PUSHBLOB instruction.
1142 * Consumes "blob".
1143 */
1144 static int
1145generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1146{
1147 isn_T *isn;
1148
Bram Moolenaar080457c2020-03-03 21:53:32 +01001149 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1151 return FAIL;
1152 isn->isn_arg.blob = blob;
1153
1154 return OK;
1155}
1156
1157/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001158 * Generate an ISN_PUSHFUNC instruction with name "name".
1159 * Consumes "name".
1160 */
1161 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001162generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001163{
1164 isn_T *isn;
1165
Bram Moolenaar080457c2020-03-03 21:53:32 +01001166 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001167 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001168 return FAIL;
1169 isn->isn_arg.string = name;
1170
1171 return OK;
1172}
1173
1174/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001175 * Generate an ISN_GETITEM instruction with "index".
1176 */
1177 static int
1178generate_GETITEM(cctx_T *cctx, int index)
1179{
1180 isn_T *isn;
1181 garray_T *stack = &cctx->ctx_type_stack;
1182 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1183 type_T *item_type = &t_any;
1184
1185 RETURN_OK_IF_SKIP(cctx);
1186
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001187 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001188 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001189 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001190 emsg(_(e_listreq));
1191 return FAIL;
1192 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001193 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001194 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1195 return FAIL;
1196 isn->isn_arg.number = index;
1197
1198 // add the item type to the type stack
1199 if (ga_grow(stack, 1) == FAIL)
1200 return FAIL;
1201 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1202 ++stack->ga_len;
1203 return OK;
1204}
1205
1206/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001207 * Generate an ISN_SLICE instruction with "count".
1208 */
1209 static int
1210generate_SLICE(cctx_T *cctx, int count)
1211{
1212 isn_T *isn;
1213
1214 RETURN_OK_IF_SKIP(cctx);
1215 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1216 return FAIL;
1217 isn->isn_arg.number = count;
1218 return OK;
1219}
1220
1221/*
1222 * Generate an ISN_CHECKLEN instruction with "min_len".
1223 */
1224 static int
1225generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1226{
1227 isn_T *isn;
1228
1229 RETURN_OK_IF_SKIP(cctx);
1230
1231 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1232 return FAIL;
1233 isn->isn_arg.checklen.cl_min_len = min_len;
1234 isn->isn_arg.checklen.cl_more_OK = more_OK;
1235
1236 return OK;
1237}
1238
1239/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 * Generate an ISN_STORE instruction.
1241 */
1242 static int
1243generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1244{
1245 isn_T *isn;
1246
Bram Moolenaar080457c2020-03-03 21:53:32 +01001247 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1249 return FAIL;
1250 if (name != NULL)
1251 isn->isn_arg.string = vim_strsave(name);
1252 else
1253 isn->isn_arg.number = idx;
1254
1255 return OK;
1256}
1257
1258/*
1259 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1260 */
1261 static int
1262generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1263{
1264 isn_T *isn;
1265
Bram Moolenaar080457c2020-03-03 21:53:32 +01001266 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1268 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001269 isn->isn_arg.storenr.stnr_idx = idx;
1270 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271
1272 return OK;
1273}
1274
1275/*
1276 * Generate an ISN_STOREOPT instruction
1277 */
1278 static int
1279generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1280{
1281 isn_T *isn;
1282
Bram Moolenaar080457c2020-03-03 21:53:32 +01001283 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1285 return FAIL;
1286 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1287 isn->isn_arg.storeopt.so_flags = opt_flags;
1288
1289 return OK;
1290}
1291
1292/*
1293 * Generate an ISN_LOAD or similar instruction.
1294 */
1295 static int
1296generate_LOAD(
1297 cctx_T *cctx,
1298 isntype_T isn_type,
1299 int idx,
1300 char_u *name,
1301 type_T *type)
1302{
1303 isn_T *isn;
1304
Bram Moolenaar080457c2020-03-03 21:53:32 +01001305 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1307 return FAIL;
1308 if (name != NULL)
1309 isn->isn_arg.string = vim_strsave(name);
1310 else
1311 isn->isn_arg.number = idx;
1312
1313 return OK;
1314}
1315
1316/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001317 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001318 */
1319 static int
1320generate_LOADV(
1321 cctx_T *cctx,
1322 char_u *name,
1323 int error)
1324{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001325 int di_flags;
1326 int vidx = find_vim_var(name, &di_flags);
1327 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001328
Bram Moolenaar080457c2020-03-03 21:53:32 +01001329 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001330 if (vidx < 0)
1331 {
1332 if (error)
1333 semsg(_(e_var_notfound), name);
1334 return FAIL;
1335 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001336 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001337
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001338 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001339}
1340
1341/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001342 * Generate an ISN_UNLET instruction.
1343 */
1344 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001345generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001346{
1347 isn_T *isn;
1348
1349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001350 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001351 return FAIL;
1352 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1353 isn->isn_arg.unlet.ul_forceit = forceit;
1354
1355 return OK;
1356}
1357
1358/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 * Generate an ISN_LOADS instruction.
1360 */
1361 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001362generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001364 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001366 int sid,
1367 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368{
1369 isn_T *isn;
1370
Bram Moolenaar080457c2020-03-03 21:53:32 +01001371 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001372 if (isn_type == ISN_LOADS)
1373 isn = generate_instr_type(cctx, isn_type, type);
1374 else
1375 isn = generate_instr_drop(cctx, isn_type, 1);
1376 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001378 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1379 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380
1381 return OK;
1382}
1383
1384/*
1385 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1386 */
1387 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001388generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 cctx_T *cctx,
1390 isntype_T isn_type,
1391 int sid,
1392 int idx,
1393 type_T *type)
1394{
1395 isn_T *isn;
1396
Bram Moolenaar080457c2020-03-03 21:53:32 +01001397 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 if (isn_type == ISN_LOADSCRIPT)
1399 isn = generate_instr_type(cctx, isn_type, type);
1400 else
1401 isn = generate_instr_drop(cctx, isn_type, 1);
1402 if (isn == NULL)
1403 return FAIL;
1404 isn->isn_arg.script.script_sid = sid;
1405 isn->isn_arg.script.script_idx = idx;
1406 return OK;
1407}
1408
1409/*
1410 * Generate an ISN_NEWLIST instruction.
1411 */
1412 static int
1413generate_NEWLIST(cctx_T *cctx, int count)
1414{
1415 isn_T *isn;
1416 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 type_T *type;
1418 type_T *member;
1419
Bram Moolenaar080457c2020-03-03 21:53:32 +01001420 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1422 return FAIL;
1423 isn->isn_arg.number = count;
1424
1425 // drop the value types
1426 stack->ga_len -= count;
1427
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001428 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001429 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 if (count > 0)
1431 member = ((type_T **)stack->ga_data)[stack->ga_len];
1432 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001433 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001434 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435
1436 // add the list type to the type stack
1437 if (ga_grow(stack, 1) == FAIL)
1438 return FAIL;
1439 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1440 ++stack->ga_len;
1441
1442 return OK;
1443}
1444
1445/*
1446 * Generate an ISN_NEWDICT instruction.
1447 */
1448 static int
1449generate_NEWDICT(cctx_T *cctx, int count)
1450{
1451 isn_T *isn;
1452 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 type_T *type;
1454 type_T *member;
1455
Bram Moolenaar080457c2020-03-03 21:53:32 +01001456 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1458 return FAIL;
1459 isn->isn_arg.number = count;
1460
1461 // drop the key and value types
1462 stack->ga_len -= 2 * count;
1463
Bram Moolenaar436472f2020-02-20 22:54:43 +01001464 // Use the first value type for the list member type. Use "void" for an
1465 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 if (count > 0)
1467 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1468 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001469 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001470 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471
1472 // add the dict type to the type stack
1473 if (ga_grow(stack, 1) == FAIL)
1474 return FAIL;
1475 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1476 ++stack->ga_len;
1477
1478 return OK;
1479}
1480
1481/*
1482 * Generate an ISN_FUNCREF instruction.
1483 */
1484 static int
1485generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1486{
1487 isn_T *isn;
1488 garray_T *stack = &cctx->ctx_type_stack;
1489
Bram Moolenaar080457c2020-03-03 21:53:32 +01001490 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1492 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001493 isn->isn_arg.funcref.fr_func = dfunc_idx;
1494 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495
1496 if (ga_grow(stack, 1) == FAIL)
1497 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001498 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 // TODO: argument and return types
1500 ++stack->ga_len;
1501
1502 return OK;
1503}
1504
1505/*
1506 * Generate an ISN_JUMP instruction.
1507 */
1508 static int
1509generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1510{
1511 isn_T *isn;
1512 garray_T *stack = &cctx->ctx_type_stack;
1513
Bram Moolenaar080457c2020-03-03 21:53:32 +01001514 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1516 return FAIL;
1517 isn->isn_arg.jump.jump_when = when;
1518 isn->isn_arg.jump.jump_where = where;
1519
1520 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1521 --stack->ga_len;
1522
1523 return OK;
1524}
1525
1526 static int
1527generate_FOR(cctx_T *cctx, int loop_idx)
1528{
1529 isn_T *isn;
1530 garray_T *stack = &cctx->ctx_type_stack;
1531
Bram Moolenaar080457c2020-03-03 21:53:32 +01001532 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1534 return FAIL;
1535 isn->isn_arg.forloop.for_idx = loop_idx;
1536
1537 if (ga_grow(stack, 1) == FAIL)
1538 return FAIL;
1539 // type doesn't matter, will be stored next
1540 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1541 ++stack->ga_len;
1542
1543 return OK;
1544}
1545
1546/*
1547 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001548 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 * Return FAIL if the number of arguments is wrong.
1550 */
1551 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001552generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553{
1554 isn_T *isn;
1555 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001556 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001557 type_T *argtypes[MAX_FUNC_ARGS];
1558 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559
Bram Moolenaar080457c2020-03-03 21:53:32 +01001560 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001561 argoff = check_internal_func(func_idx, argcount);
1562 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 return FAIL;
1564
Bram Moolenaar389df252020-07-09 21:20:47 +02001565 if (method_call && argoff > 1)
1566 {
1567 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1568 return FAIL;
1569 isn->isn_arg.shuffle.shfl_item = argcount;
1570 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1571 }
1572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1574 return FAIL;
1575 isn->isn_arg.bfunc.cbf_idx = func_idx;
1576 isn->isn_arg.bfunc.cbf_argcount = argcount;
1577
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001578 for (i = 0; i < argcount; ++i)
1579 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 stack->ga_len -= argcount; // drop the arguments
1582 if (ga_grow(stack, 1) == FAIL)
1583 return FAIL;
1584 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001585 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 ++stack->ga_len; // add return value
1587
1588 return OK;
1589}
1590
1591/*
1592 * Generate an ISN_DCALL or ISN_UCALL instruction.
1593 * Return FAIL if the number of arguments is wrong.
1594 */
1595 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001596generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597{
1598 isn_T *isn;
1599 garray_T *stack = &cctx->ctx_type_stack;
1600 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001601 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602
Bram Moolenaar080457c2020-03-03 21:53:32 +01001603 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 if (argcount > regular_args && !has_varargs(ufunc))
1605 {
1606 semsg(_(e_toomanyarg), ufunc->uf_name);
1607 return FAIL;
1608 }
1609 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1610 {
1611 semsg(_(e_toofewarg), ufunc->uf_name);
1612 return FAIL;
1613 }
1614
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001615 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001616 {
1617 int i;
1618
1619 for (i = 0; i < argcount; ++i)
1620 {
1621 type_T *expected;
1622 type_T *actual;
1623
1624 if (i < regular_args)
1625 {
1626 if (ufunc->uf_arg_types == NULL)
1627 continue;
1628 expected = ufunc->uf_arg_types[i];
1629 }
1630 else
1631 expected = ufunc->uf_va_type->tt_member;
1632 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001633 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001634 {
1635 arg_type_mismatch(expected, actual, i + 1);
1636 return FAIL;
1637 }
1638 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001639 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001640 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001641 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001642 }
1643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001645 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001646 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001648 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 {
1650 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1651 isn->isn_arg.dfunc.cdf_argcount = argcount;
1652 }
1653 else
1654 {
1655 // A user function may be deleted and redefined later, can't use the
1656 // ufunc pointer, need to look it up again at runtime.
1657 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1658 isn->isn_arg.ufunc.cuf_argcount = argcount;
1659 }
1660
1661 stack->ga_len -= argcount; // drop the arguments
1662 if (ga_grow(stack, 1) == FAIL)
1663 return FAIL;
1664 // add return value
1665 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1666 ++stack->ga_len;
1667
1668 return OK;
1669}
1670
1671/*
1672 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1673 */
1674 static int
1675generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1676{
1677 isn_T *isn;
1678 garray_T *stack = &cctx->ctx_type_stack;
1679
Bram Moolenaar080457c2020-03-03 21:53:32 +01001680 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1682 return FAIL;
1683 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1684 isn->isn_arg.ufunc.cuf_argcount = argcount;
1685
1686 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001687 if (ga_grow(stack, 1) == FAIL)
1688 return FAIL;
1689 // add return value
1690 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1691 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692
1693 return OK;
1694}
1695
1696/*
1697 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001698 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 */
1700 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001701generate_PCALL(
1702 cctx_T *cctx,
1703 int argcount,
1704 char_u *name,
1705 type_T *type,
1706 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707{
1708 isn_T *isn;
1709 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001710 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711
Bram Moolenaar080457c2020-03-03 21:53:32 +01001712 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001713
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001714 if (type->tt_type == VAR_ANY)
1715 ret_type = &t_any;
1716 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001717 {
1718 if (type->tt_argcount != -1)
1719 {
1720 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1721
1722 if (argcount < type->tt_min_argcount - varargs)
1723 {
1724 semsg(_(e_toofewarg), "[reference]");
1725 return FAIL;
1726 }
1727 if (!varargs && argcount > type->tt_argcount)
1728 {
1729 semsg(_(e_toomanyarg), "[reference]");
1730 return FAIL;
1731 }
1732 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001733 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001734 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001735 else
1736 {
1737 semsg(_("E1085: Not a callable type: %s"), name);
1738 return FAIL;
1739 }
1740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1742 return FAIL;
1743 isn->isn_arg.pfunc.cpf_top = at_top;
1744 isn->isn_arg.pfunc.cpf_argcount = argcount;
1745
1746 stack->ga_len -= argcount; // drop the arguments
1747
1748 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001749 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001751 // If partial is above the arguments it must be cleared and replaced with
1752 // the return value.
1753 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1754 return FAIL;
1755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 return OK;
1757}
1758
1759/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001760 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 */
1762 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001763generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764{
1765 isn_T *isn;
1766 garray_T *stack = &cctx->ctx_type_stack;
1767 type_T *type;
1768
Bram Moolenaar080457c2020-03-03 21:53:32 +01001769 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001770 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001772 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001774 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001776 if (type->tt_type != VAR_DICT && type != &t_any)
1777 {
1778 emsg(_(e_dictreq));
1779 return FAIL;
1780 }
1781 // change dict type to dict member type
1782 if (type->tt_type == VAR_DICT)
1783 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784
1785 return OK;
1786}
1787
1788/*
1789 * Generate an ISN_ECHO instruction.
1790 */
1791 static int
1792generate_ECHO(cctx_T *cctx, int with_white, int count)
1793{
1794 isn_T *isn;
1795
Bram Moolenaar080457c2020-03-03 21:53:32 +01001796 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1798 return FAIL;
1799 isn->isn_arg.echo.echo_with_white = with_white;
1800 isn->isn_arg.echo.echo_count = count;
1801
1802 return OK;
1803}
1804
Bram Moolenaarad39c092020-02-26 18:23:43 +01001805/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001806 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001807 */
1808 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001809generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001810{
1811 isn_T *isn;
1812
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001813 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001814 return FAIL;
1815 isn->isn_arg.number = count;
1816
1817 return OK;
1818}
1819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 static int
1821generate_EXEC(cctx_T *cctx, char_u *line)
1822{
1823 isn_T *isn;
1824
Bram Moolenaar080457c2020-03-03 21:53:32 +01001825 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1827 return FAIL;
1828 isn->isn_arg.string = vim_strsave(line);
1829 return OK;
1830}
1831
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001832 static int
1833generate_EXECCONCAT(cctx_T *cctx, int count)
1834{
1835 isn_T *isn;
1836
1837 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1838 return FAIL;
1839 isn->isn_arg.number = count;
1840 return OK;
1841}
1842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843/*
1844 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001845 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001847 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1849{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 lvar_T *lvar;
1851
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001852 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001854 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001855 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 }
1857
1858 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001859 return NULL;
1860 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001862 // Every local variable uses the next entry on the stack. We could re-use
1863 // the last ones when leaving a scope, but then variables used in a closure
1864 // might get overwritten. To keep things simple do not re-use stack
1865 // entries. This is less efficient, but memory is cheap these days.
1866 lvar->lv_idx = cctx->ctx_locals_count++;
1867
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001868 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 lvar->lv_const = isConst;
1870 lvar->lv_type = type;
1871
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001872 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873}
1874
1875/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001876 * Remove local variables above "new_top".
1877 */
1878 static void
1879unwind_locals(cctx_T *cctx, int new_top)
1880{
1881 if (cctx->ctx_locals.ga_len > new_top)
1882 {
1883 int idx;
1884 lvar_T *lvar;
1885
1886 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1887 {
1888 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1889 vim_free(lvar->lv_name);
1890 }
1891 }
1892 cctx->ctx_locals.ga_len = new_top;
1893}
1894
1895/*
1896 * Free all local variables.
1897 */
1898 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001899free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001900{
1901 unwind_locals(cctx, 0);
1902 ga_clear(&cctx->ctx_locals);
1903}
1904
1905/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 * Skip over a type definition and return a pointer to just after it.
1907 */
1908 char_u *
1909skip_type(char_u *start)
1910{
1911 char_u *p = start;
1912
1913 while (ASCII_ISALNUM(*p) || *p == '_')
1914 ++p;
1915
1916 // Skip over "<type>"; this is permissive about white space.
1917 if (*skipwhite(p) == '<')
1918 {
1919 p = skipwhite(p);
1920 p = skip_type(skipwhite(p + 1));
1921 p = skipwhite(p);
1922 if (*p == '>')
1923 ++p;
1924 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001925 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
1926 && STRNCMP("func", start, 4) == 0)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001927 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001928 if (*p == '(')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001929 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001930 // handle func(args): type
1931 ++p;
1932 while (*p != ')' && *p != NUL)
1933 {
1934 char_u *sp = p;
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001935
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001936 p = skip_type(p);
1937 if (p == sp)
1938 return p; // syntax error
1939 if (*p == ',')
1940 p = skipwhite(p + 1);
1941 }
1942 if (*p == ')')
1943 {
1944 if (p[1] == ':')
1945 p = skip_type(skipwhite(p + 2));
1946 else
1947 p = skipwhite(p + 1);
1948 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001949 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001950 else
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001951 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001952 // handle func: return_type
1953 p = skip_type(skipwhite(p + 1));
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001954 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001955 }
1956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 return p;
1958}
1959
1960/*
1961 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001962 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 * Returns NULL in case of failure.
1964 */
1965 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001966parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967{
1968 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001969 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970
1971 if (**arg != '<')
1972 {
1973 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001974 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 else
1976 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001977 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 }
1979 *arg = skipwhite(*arg + 1);
1980
Bram Moolenaard77a8522020-04-03 21:59:57 +02001981 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982
1983 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001984 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 {
1986 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001987 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 }
1989 ++*arg;
1990
1991 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001992 return get_list_type(member_type, type_gap);
1993 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994}
1995
1996/*
1997 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001998 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 */
2000 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002001parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002{
2003 char_u *p = *arg;
2004 size_t len;
2005
2006 // skip over the first word
2007 while (ASCII_ISALNUM(*p) || *p == '_')
2008 ++p;
2009 len = p - *arg;
2010
2011 switch (**arg)
2012 {
2013 case 'a':
2014 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
2015 {
2016 *arg += len;
2017 return &t_any;
2018 }
2019 break;
2020 case 'b':
2021 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
2022 {
2023 *arg += len;
2024 return &t_bool;
2025 }
2026 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
2027 {
2028 *arg += len;
2029 return &t_blob;
2030 }
2031 break;
2032 case 'c':
2033 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
2034 {
2035 *arg += len;
2036 return &t_channel;
2037 }
2038 break;
2039 case 'd':
2040 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
2041 {
2042 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002043 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 }
2045 break;
2046 case 'f':
2047 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
2048 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01002049#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 *arg += len;
2051 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002052#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002053 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002054 return &t_any;
2055#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 }
2057 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2058 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002059 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002060 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002061 int argcount = -1;
2062 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002063 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002064 type_T *arg_type[MAX_FUNC_ARGS + 1];
2065
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002066 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002068 if (**arg == '(')
2069 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002070 // "func" may or may not return a value, "func()" does
2071 // not return a value.
2072 ret_type = &t_void;
2073
Bram Moolenaard77a8522020-04-03 21:59:57 +02002074 p = ++*arg;
2075 argcount = 0;
2076 while (*p != NUL && *p != ')')
2077 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002078 if (*p == '?')
2079 {
2080 if (first_optional == -1)
2081 first_optional = argcount;
2082 ++p;
2083 }
2084 else if (first_optional != -1)
2085 {
2086 emsg(_("E1007: mandatory argument after optional argument"));
2087 return &t_any;
2088 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002089 else if (STRNCMP(p, "...", 3) == 0)
2090 {
2091 flags |= TTFLAG_VARARGS;
2092 p += 3;
2093 }
2094
2095 arg_type[argcount++] = parse_type(&p, type_gap);
2096
2097 // Nothing comes after "...{type}".
2098 if (flags & TTFLAG_VARARGS)
2099 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002100
Bram Moolenaard77a8522020-04-03 21:59:57 +02002101 if (*p != ',' && *skipwhite(p) == ',')
2102 {
2103 semsg(_(e_no_white_before), ",");
2104 return &t_any;
2105 }
2106 if (*p == ',')
2107 {
2108 ++p;
2109 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002110 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002111 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002112 return &t_any;
2113 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002114 }
2115 p = skipwhite(p);
2116 if (argcount == MAX_FUNC_ARGS)
2117 {
2118 emsg(_("E740: Too many argument types"));
2119 return &t_any;
2120 }
2121 }
2122
2123 p = skipwhite(p);
2124 if (*p != ')')
2125 {
2126 emsg(_(e_missing_close));
2127 return &t_any;
2128 }
2129 *arg = p + 1;
2130 }
2131 if (**arg == ':')
2132 {
2133 // parse return type
2134 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002135 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002136 semsg(_(e_white_after), ":");
2137 *arg = skipwhite(*arg);
2138 ret_type = parse_type(arg, type_gap);
2139 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002140 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002141 type = get_func_type(ret_type, argcount, type_gap);
2142 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002143 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002144 type = alloc_func_type(ret_type, argcount, type_gap);
2145 type->tt_flags = flags;
2146 if (argcount > 0)
2147 {
2148 type->tt_argcount = argcount;
2149 type->tt_min_argcount = first_optional == -1
2150 ? argcount : first_optional;
2151 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002152 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002153 return &t_any;
2154 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002155 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002156 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002157 }
2158 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 }
2160 break;
2161 case 'j':
2162 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2163 {
2164 *arg += len;
2165 return &t_job;
2166 }
2167 break;
2168 case 'l':
2169 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2170 {
2171 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002172 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 }
2174 break;
2175 case 'n':
2176 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2177 {
2178 *arg += len;
2179 return &t_number;
2180 }
2181 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 case 's':
2183 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2184 {
2185 *arg += len;
2186 return &t_string;
2187 }
2188 break;
2189 case 'v':
2190 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2191 {
2192 *arg += len;
2193 return &t_void;
2194 }
2195 break;
2196 }
2197
2198 semsg(_("E1010: Type not recognized: %s"), *arg);
2199 return &t_any;
2200}
2201
2202/*
2203 * Check if "type1" and "type2" are exactly the same.
2204 */
2205 static int
2206equal_type(type_T *type1, type_T *type2)
2207{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002208 int i;
2209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 if (type1->tt_type != type2->tt_type)
2211 return FALSE;
2212 switch (type1->tt_type)
2213 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002215 case VAR_ANY:
2216 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 case VAR_SPECIAL:
2218 case VAR_BOOL:
2219 case VAR_NUMBER:
2220 case VAR_FLOAT:
2221 case VAR_STRING:
2222 case VAR_BLOB:
2223 case VAR_JOB:
2224 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002225 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 case VAR_LIST:
2227 case VAR_DICT:
2228 return equal_type(type1->tt_member, type2->tt_member);
2229 case VAR_FUNC:
2230 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002231 if (!equal_type(type1->tt_member, type2->tt_member)
2232 || type1->tt_argcount != type2->tt_argcount)
2233 return FALSE;
2234 if (type1->tt_argcount < 0
2235 || type1->tt_args == NULL || type2->tt_args == NULL)
2236 return TRUE;
2237 for (i = 0; i < type1->tt_argcount; ++i)
2238 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2239 return FALSE;
2240 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 }
2242 return TRUE;
2243}
2244
2245/*
2246 * Find the common type of "type1" and "type2" and put it in "dest".
2247 * "type2" and "dest" may be the same.
2248 */
2249 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002250common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251{
2252 if (equal_type(type1, type2))
2253 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002254 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 return;
2256 }
2257
2258 if (type1->tt_type == type2->tt_type)
2259 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2261 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002262 type_T *common;
2263
Bram Moolenaard77a8522020-04-03 21:59:57 +02002264 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002265 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002266 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002267 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002268 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 return;
2270 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002271 if (type1->tt_type == VAR_FUNC)
2272 {
2273 type_T *common;
2274
2275 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2276 if (type1->tt_argcount == type2->tt_argcount
2277 && type1->tt_argcount >= 0)
2278 {
2279 int argcount = type1->tt_argcount;
2280 int i;
2281
2282 *dest = alloc_func_type(common, argcount, type_gap);
2283 if (type1->tt_args != NULL && type2->tt_args != NULL)
2284 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002285 if (func_type_add_arg_types(*dest, argcount,
2286 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002287 for (i = 0; i < argcount; ++i)
2288 common_type(type1->tt_args[i], type2->tt_args[i],
2289 &(*dest)->tt_args[i], type_gap);
2290 }
2291 }
2292 else
2293 *dest = alloc_func_type(common, -1, type_gap);
2294 return;
2295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 }
2297
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002298 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299}
2300
2301 char *
2302vartype_name(vartype_T type)
2303{
2304 switch (type)
2305 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002306 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002307 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 case VAR_SPECIAL: return "special";
2310 case VAR_BOOL: return "bool";
2311 case VAR_NUMBER: return "number";
2312 case VAR_FLOAT: return "float";
2313 case VAR_STRING: return "string";
2314 case VAR_BLOB: return "blob";
2315 case VAR_JOB: return "job";
2316 case VAR_CHANNEL: return "channel";
2317 case VAR_LIST: return "list";
2318 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002319
2320 case VAR_FUNC:
2321 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002323 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324}
2325
2326/*
2327 * Return the name of a type.
2328 * The result may be in allocated memory, in which case "tofree" is set.
2329 */
2330 char *
2331type_name(type_T *type, char **tofree)
2332{
2333 char *name = vartype_name(type->tt_type);
2334
2335 *tofree = NULL;
2336 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2337 {
2338 char *member_free;
2339 char *member_name = type_name(type->tt_member, &member_free);
2340 size_t len;
2341
2342 len = STRLEN(name) + STRLEN(member_name) + 3;
2343 *tofree = alloc(len);
2344 if (*tofree != NULL)
2345 {
2346 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2347 vim_free(member_free);
2348 return *tofree;
2349 }
2350 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002351 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002352 {
2353 garray_T ga;
2354 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002355 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002356
2357 ga_init2(&ga, 1, 100);
2358 if (ga_grow(&ga, 20) == FAIL)
2359 return "[unknown]";
2360 *tofree = ga.ga_data;
2361 STRCPY(ga.ga_data, "func(");
2362 ga.ga_len += 5;
2363
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002364 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002365 {
2366 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002367 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002368 int len;
2369
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002370 if (type->tt_args == NULL)
2371 arg_type = "[unknown]";
2372 else
2373 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002374 if (i > 0)
2375 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002376 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002377 ga.ga_len += 2;
2378 }
2379 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002380 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002381 {
2382 vim_free(arg_free);
2383 return "[unknown]";
2384 }
2385 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002386 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002387 {
2388 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2389 ga.ga_len += 3;
2390 }
2391 else if (i >= type->tt_min_argcount)
2392 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002393 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002394 ga.ga_len += len;
2395 vim_free(arg_free);
2396 }
2397
2398 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002399 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002400 else
2401 {
2402 char *ret_free;
2403 char *ret_name = type_name(type->tt_member, &ret_free);
2404 int len;
2405
2406 len = (int)STRLEN(ret_name) + 4;
2407 if (ga_grow(&ga, len) == FAIL)
2408 {
2409 vim_free(ret_free);
2410 return "[unknown]";
2411 }
2412 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002413 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2414 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002415 vim_free(ret_free);
2416 }
2417 return ga.ga_data;
2418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419
2420 return name;
2421}
2422
2423/*
2424 * Find "name" in script-local items of script "sid".
2425 * Returns the index in "sn_var_vals" if found.
2426 * If found but not in "sn_var_vals" returns -1.
2427 * If not found returns -2.
2428 */
2429 int
2430get_script_item_idx(int sid, char_u *name, int check_writable)
2431{
2432 hashtab_T *ht;
2433 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002434 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 int idx;
2436
2437 // First look the name up in the hashtable.
2438 if (sid <= 0 || sid > script_items.ga_len)
2439 return -1;
2440 ht = &SCRIPT_VARS(sid);
2441 di = find_var_in_ht(ht, 0, name, TRUE);
2442 if (di == NULL)
2443 return -2;
2444
2445 // Now find the svar_T index in sn_var_vals.
2446 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2447 {
2448 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2449
2450 if (sv->sv_tv == &di->di_tv)
2451 {
2452 if (check_writable && sv->sv_const)
2453 semsg(_(e_readonlyvar), name);
2454 return idx;
2455 }
2456 }
2457 return -1;
2458}
2459
2460/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002461 * Find "name" in imported items of the current script or in "cctx" if not
2462 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 */
2464 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002465find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002467 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 int idx;
2469
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002470 if (current_sctx.sc_sid <= 0)
2471 return NULL;
2472 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 if (cctx != NULL)
2474 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2475 {
2476 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2477 + idx;
2478
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002479 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2480 : STRLEN(import->imp_name) == len
2481 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 return import;
2483 }
2484
2485 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2486 {
2487 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2488
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002489 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2490 : STRLEN(import->imp_name) == len
2491 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 return import;
2493 }
2494 return NULL;
2495}
2496
2497/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002498 * Free all imported variables.
2499 */
2500 static void
2501free_imported(cctx_T *cctx)
2502{
2503 int idx;
2504
2505 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2506 {
2507 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2508
2509 vim_free(import->imp_name);
2510 }
2511 ga_clear(&cctx->ctx_imports);
2512}
2513
2514/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002515 * Return TRUE if "p" points at a "#" but not at "#{".
2516 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002517 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002518vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002519{
2520 return p[0] == '#' && p[1] != '{';
2521}
2522
2523/*
2524 * Return a pointer to the next line that isn't empty or only contains a
2525 * comment. Skips over white space.
2526 * Returns NULL if there is none.
2527 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002528 char_u *
2529peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002530{
2531 int lnum = cctx->ctx_lnum;
2532
2533 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2534 {
2535 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002536 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002537
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002538 if (line == NULL)
2539 break;
2540 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002541 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002542 return p;
2543 }
2544 return NULL;
2545}
2546
2547/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002548 * Called when checking for a following operator at "arg". When the rest of
2549 * the line is empty or only a comment, peek the next line. If there is a next
2550 * line return a pointer to it and set "nextp".
2551 * Otherwise skip over white space.
2552 */
2553 static char_u *
2554may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2555{
2556 char_u *p = skipwhite(arg);
2557
2558 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002559 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002560 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002561 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002562 if (*nextp != NULL)
2563 return *nextp;
2564 }
2565 return p;
2566}
2567
2568/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002569 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002570 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002571 * Returns NULL when at the end.
2572 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002573 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002574next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002575{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002576 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002577
2578 do
2579 {
2580 ++cctx->ctx_lnum;
2581 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002582 {
2583 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002584 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002585 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002586 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002587 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002588 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002589 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002590 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002591 return line;
2592}
2593
2594/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002595 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002596 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002597 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2598 */
2599 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002600may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002601{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002602 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002603 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002604 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002605
2606 if (next == NULL)
2607 return FAIL;
2608 *arg = skipwhite(next);
2609 }
2610 return OK;
2611}
2612
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002613/*
2614 * Idem, and give an error when failed.
2615 */
2616 static int
2617may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2618{
2619 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2620 {
2621 emsg(_("E1097: line incomplete"));
2622 return FAIL;
2623 }
2624 return OK;
2625}
2626
2627
Bram Moolenaara5565e42020-05-09 15:44:01 +02002628// Structure passed between the compile_expr* functions to keep track of
2629// constants that have been parsed but for which no code was produced yet. If
2630// possible expressions on these constants are applied at compile time. If
2631// that is not possible, the code to push the constants needs to be generated
2632// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002633// Using 50 should be more than enough of 5 levels of ().
2634#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002635typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002636 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002637 int pp_used; // active entries in pp_tv[]
2638} ppconst_T;
2639
Bram Moolenaar1c747212020-05-09 18:28:34 +02002640static int compile_expr0(char_u **arg, cctx_T *cctx);
2641static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2642
Bram Moolenaara5565e42020-05-09 15:44:01 +02002643/*
2644 * Generate a PUSH instruction for "tv".
2645 * "tv" will be consumed or cleared.
2646 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2647 */
2648 static int
2649generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2650{
2651 if (tv != NULL)
2652 {
2653 switch (tv->v_type)
2654 {
2655 case VAR_UNKNOWN:
2656 break;
2657 case VAR_BOOL:
2658 generate_PUSHBOOL(cctx, tv->vval.v_number);
2659 break;
2660 case VAR_SPECIAL:
2661 generate_PUSHSPEC(cctx, tv->vval.v_number);
2662 break;
2663 case VAR_NUMBER:
2664 generate_PUSHNR(cctx, tv->vval.v_number);
2665 break;
2666#ifdef FEAT_FLOAT
2667 case VAR_FLOAT:
2668 generate_PUSHF(cctx, tv->vval.v_float);
2669 break;
2670#endif
2671 case VAR_BLOB:
2672 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2673 tv->vval.v_blob = NULL;
2674 break;
2675 case VAR_STRING:
2676 generate_PUSHS(cctx, tv->vval.v_string);
2677 tv->vval.v_string = NULL;
2678 break;
2679 default:
2680 iemsg("constant type not supported");
2681 clear_tv(tv);
2682 return FAIL;
2683 }
2684 tv->v_type = VAR_UNKNOWN;
2685 }
2686 return OK;
2687}
2688
2689/*
2690 * Generate code for any ppconst entries.
2691 */
2692 static int
2693generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2694{
2695 int i;
2696 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002697 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002698
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002699 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002700 for (i = 0; i < ppconst->pp_used; ++i)
2701 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2702 ret = FAIL;
2703 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002704 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002705 return ret;
2706}
2707
2708/*
2709 * Clear ppconst constants. Used when failing.
2710 */
2711 static void
2712clear_ppconst(ppconst_T *ppconst)
2713{
2714 int i;
2715
2716 for (i = 0; i < ppconst->pp_used; ++i)
2717 clear_tv(&ppconst->pp_tv[i]);
2718 ppconst->pp_used = 0;
2719}
2720
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002721/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002722 * Generate an instruction to load script-local variable "name", without the
2723 * leading "s:".
2724 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 */
2726 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002727compile_load_scriptvar(
2728 cctx_T *cctx,
2729 char_u *name, // variable NUL terminated
2730 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002731 char_u **end, // end of variable
2732 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002734 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2736 imported_T *import;
2737
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002738 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002740 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002741 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2742 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 }
2744 if (idx >= 0)
2745 {
2746 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2747
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002748 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 current_sctx.sc_sid, idx, sv->sv_type);
2750 return OK;
2751 }
2752
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002753 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 if (import != NULL)
2755 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002756 if (import->imp_all)
2757 {
2758 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002759 char_u *exp_name;
2760 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002761 ufunc_T *ufunc;
2762 type_T *type;
2763
2764 // Used "import * as Name", need to lookup the member.
2765 if (*p != '.')
2766 {
2767 semsg(_("E1060: expected dot after name: %s"), start);
2768 return FAIL;
2769 }
2770 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002771 if (VIM_ISWHITE(*p))
2772 {
2773 emsg(_("E1074: no white space allowed after dot"));
2774 return FAIL;
2775 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002776
Bram Moolenaar1c991142020-07-04 13:15:31 +02002777 // isolate one name
2778 exp_name = p;
2779 while (eval_isnamec(*p))
2780 ++p;
2781 cc = *p;
2782 *p = NUL;
2783
2784 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2785 *p = cc;
2786 p = skipwhite(p);
2787
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002788 // TODO: what if it is a function?
2789 if (idx < 0)
2790 return FAIL;
2791 *end = p;
2792
2793 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2794 import->imp_sid,
2795 idx,
2796 type);
2797 }
2798 else
2799 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002800 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002801 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2802 import->imp_sid,
2803 import->imp_var_vals_idx,
2804 import->imp_type);
2805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 return OK;
2807 }
2808
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002809 if (error)
2810 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 return FAIL;
2812}
2813
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002814 static int
2815generate_funcref(cctx_T *cctx, char_u *name)
2816{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002817 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002818
2819 if (ufunc == NULL)
2820 return FAIL;
2821
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002822 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2823 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002824}
2825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826/*
2827 * Compile a variable name into a load instruction.
2828 * "end" points to just after the name.
2829 * When "error" is FALSE do not give an error when not found.
2830 */
2831 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002832compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833{
2834 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002835 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002836 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002838 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839
2840 if (*(*arg + 1) == ':')
2841 {
2842 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002843 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002844 {
2845 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002847 switch (**arg)
2848 {
2849 case 'g': isn_type = ISN_LOADGDICT; break;
2850 case 'w': isn_type = ISN_LOADWDICT; break;
2851 case 't': isn_type = ISN_LOADTDICT; break;
2852 case 'b': isn_type = ISN_LOADBDICT; break;
2853 default:
2854 semsg(_(e_namespace), *arg);
2855 goto theend;
2856 }
2857 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2858 goto theend;
2859 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002860 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 else
2862 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002863 isntype_T isn_type = ISN_DROP;
2864
2865 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2866 if (name == NULL)
2867 return FAIL;
2868
2869 switch (**arg)
2870 {
2871 case 'v': res = generate_LOADV(cctx, name, error);
2872 break;
2873 case 's': res = compile_load_scriptvar(cctx, name,
2874 NULL, NULL, error);
2875 break;
2876 case 'g': isn_type = ISN_LOADG; break;
2877 case 'w': isn_type = ISN_LOADW; break;
2878 case 't': isn_type = ISN_LOADT; break;
2879 case 'b': isn_type = ISN_LOADB; break;
2880 default: semsg(_(e_namespace), *arg);
2881 goto theend;
2882 }
2883 if (isn_type != ISN_DROP)
2884 {
2885 // Global, Buffer-local, Window-local and Tabpage-local
2886 // variables can be defined later, thus we don't check if it
2887 // exists, give error at runtime.
2888 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2889 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 }
2891 }
2892 else
2893 {
2894 size_t len = end - *arg;
2895 int idx;
2896 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002897 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898
2899 name = vim_strnsave(*arg, end - *arg);
2900 if (name == NULL)
2901 return FAIL;
2902
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002903 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002905 if (!gen_load_outer)
2906 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 }
2908 else
2909 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002910 lvar_T *lvar = lookup_local(*arg, len, cctx);
2911
2912 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002914 type = lvar->lv_type;
2915 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002916 if (lvar->lv_from_outer)
2917 gen_load_outer = TRUE;
2918 else
2919 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 }
2921 else
2922 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002923 // "var" can be script-local even without using "s:" if it
2924 // already exists.
2925 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2926 == SCRIPT_VERSION_VIM9
2927 || lookup_script(*arg, len) == OK)
2928 res = compile_load_scriptvar(cctx, name, *arg, &end,
2929 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002930
Bram Moolenaara5565e42020-05-09 15:44:01 +02002931 // When the name starts with an uppercase letter or "x:" it
2932 // can be a user defined function.
2933 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2934 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 }
2936 }
2937 if (gen_load)
2938 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002939 if (gen_load_outer)
2940 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 }
2942
2943 *arg = end;
2944
2945theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002946 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 semsg(_(e_var_notfound), name);
2948 vim_free(name);
2949 return res;
2950}
2951
2952/*
2953 * Compile the argument expressions.
2954 * "arg" points to just after the "(" and is advanced to after the ")"
2955 */
2956 static int
2957compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2958{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002959 char_u *p = *arg;
2960 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961
Bram Moolenaare6085c52020-04-12 20:19:16 +02002962 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002964 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2965 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002966 if (*p == ')')
2967 {
2968 *arg = p + 1;
2969 return OK;
2970 }
2971
Bram Moolenaara5565e42020-05-09 15:44:01 +02002972 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 return FAIL;
2974 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002975
2976 if (*p != ',' && *skipwhite(p) == ',')
2977 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002978 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002979 p = skipwhite(p);
2980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002982 {
2983 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002984 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002985 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002986 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002987 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002988 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002990failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002991 emsg(_(e_missing_close));
2992 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993}
2994
2995/*
2996 * Compile a function call: name(arg1, arg2)
2997 * "arg" points to "name", "arg + varlen" to the "(".
2998 * "argcount_init" is 1 for "value->method()"
2999 * Instructions:
3000 * EVAL arg1
3001 * EVAL arg2
3002 * BCALL / DCALL / UCALL
3003 */
3004 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003005compile_call(
3006 char_u **arg,
3007 size_t varlen,
3008 cctx_T *cctx,
3009 ppconst_T *ppconst,
3010 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011{
3012 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003013 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 int argcount = argcount_init;
3015 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003016 char_u fname_buf[FLEN_FIXED + 1];
3017 char_u *tofree = NULL;
3018 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003020 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021
Bram Moolenaara5565e42020-05-09 15:44:01 +02003022 // we can evaluate "has('name')" at compile time
3023 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3024 {
3025 char_u *s = skipwhite(*arg + varlen + 1);
3026 typval_T argvars[2];
3027
3028 argvars[0].v_type = VAR_UNKNOWN;
3029 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003030 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003031 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003032 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003033 s = skipwhite(s);
3034 if (*s == ')' && argvars[0].v_type == VAR_STRING)
3035 {
3036 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3037
3038 *arg = s + 1;
3039 argvars[1].v_type = VAR_UNKNOWN;
3040 tv->v_type = VAR_NUMBER;
3041 tv->vval.v_number = 0;
3042 f_has(argvars, tv);
3043 clear_tv(&argvars[0]);
3044 ++ppconst->pp_used;
3045 return OK;
3046 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003047 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003048 }
3049
3050 if (generate_ppconst(cctx, ppconst) == FAIL)
3051 return FAIL;
3052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 if (varlen >= sizeof(namebuf))
3054 {
3055 semsg(_("E1011: name too long: %s"), name);
3056 return FAIL;
3057 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003058 vim_strncpy(namebuf, *arg, varlen);
3059 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060
3061 *arg = skipwhite(*arg + varlen + 1);
3062 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003063 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003065 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 {
3067 int idx;
3068
3069 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003070 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003072 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003073 else
3074 semsg(_(e_unknownfunc), namebuf);
3075 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 }
3077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003079 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003081 {
3082 res = generate_CALL(cctx, ufunc, argcount);
3083 goto theend;
3084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085
3086 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003087 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003089 if (STRNCMP(namebuf, "g:", 2) != 0
3090 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003091 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003092 garray_T *stack = &cctx->ctx_type_stack;
3093 type_T *type;
3094
3095 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3096 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003097 goto theend;
3098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003100 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003101 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003102 if (STRNCMP(namebuf, "g:", 2) == 0)
3103 res = generate_UCALL(cctx, name, argcount);
3104 else
3105 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003106
3107theend:
3108 vim_free(tofree);
3109 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110}
3111
3112// like NAMESPACE_CHAR but with 'a' and 'l'.
3113#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3114
3115/*
3116 * Find the end of a variable or function name. Unlike find_name_end() this
3117 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003118 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 * Return a pointer to just after the name. Equal to "arg" if there is no
3120 * valid name.
3121 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003122 static char_u *
3123to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124{
3125 char_u *p;
3126
3127 // Quick check for valid starting character.
3128 if (!eval_isnamec1(*arg))
3129 return arg;
3130
3131 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3132 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3133 // and can be used in slice "[n:]".
3134 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003135 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3137 break;
3138 return p;
3139}
3140
3141/*
3142 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003143 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 */
3145 char_u *
3146to_name_const_end(char_u *arg)
3147{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003148 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 typval_T rettv;
3150
3151 if (p == arg && *arg == '[')
3152 {
3153
3154 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003155 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 p = arg;
3157 }
3158 else if (p == arg && *arg == '#' && arg[1] == '{')
3159 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003160 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003162 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 p = arg;
3164 }
3165 else if (p == arg && *arg == '{')
3166 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003167 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003169 // Can be "{x -> ret}()".
3170 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003172 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 if (ret != OK)
3174 p = arg;
3175 }
3176
3177 return p;
3178}
3179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180/*
3181 * parse a list: [expr, expr]
3182 * "*arg" points to the '['.
3183 */
3184 static int
3185compile_list(char_u **arg, cctx_T *cctx)
3186{
3187 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003188 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 int count = 0;
3190
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003191 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003193 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003194 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003195 semsg(_(e_list_end), *arg);
3196 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003197 }
3198 if (*p == ']')
3199 {
3200 ++p;
3201 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003202 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003203 p += STRLEN(p);
3204 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003205 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003206 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 break;
3208 ++count;
3209 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003210 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003212 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3213 {
3214 semsg(_(e_white_after), ",");
3215 return FAIL;
3216 }
3217 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003218 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 p = skipwhite(p);
3220 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003221 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222
3223 generate_NEWLIST(cctx, count);
3224 return OK;
3225}
3226
3227/*
3228 * parse a lambda: {arg, arg -> expr}
3229 * "*arg" points to the '{'.
3230 */
3231 static int
3232compile_lambda(char_u **arg, cctx_T *cctx)
3233{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 typval_T rettv;
3235 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003236 evalarg_T evalarg;
3237
3238 CLEAR_FIELD(evalarg);
3239 evalarg.eval_flags = EVAL_EVALUATE;
3240 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241
3242 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003243 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003247 ++ufunc->uf_refcount;
3248 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003249 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250
3251 // The function will have one line: "return {expr}".
3252 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003253 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003255 clear_evalarg(&evalarg, NULL);
3256
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003257 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003258 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003259
3260 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 return FAIL;
3262}
3263
3264/*
3265 * Compile a lamda call: expr->{lambda}(args)
3266 * "arg" points to the "{".
3267 */
3268 static int
3269compile_lambda_call(char_u **arg, cctx_T *cctx)
3270{
3271 ufunc_T *ufunc;
3272 typval_T rettv;
3273 int argcount = 1;
3274 int ret = FAIL;
3275
3276 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003277 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 return FAIL;
3279
3280 if (**arg != '(')
3281 {
3282 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003283 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 else
3285 semsg(_(e_missing_paren), "lambda");
3286 clear_tv(&rettv);
3287 return FAIL;
3288 }
3289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 ufunc = rettv.vval.v_partial->pt_func;
3291 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003292 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003293 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003294
3295 // The function will have one line: "return {expr}".
3296 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003297 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298
3299 // compile the arguments
3300 *arg = skipwhite(*arg + 1);
3301 if (compile_arguments(arg, cctx, &argcount) == OK)
3302 // call the compiled function
3303 ret = generate_CALL(cctx, ufunc, argcount);
3304
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003305 if (ret == FAIL)
3306 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 return ret;
3308}
3309
3310/*
3311 * parse a dict: {'key': val} or #{key: val}
3312 * "*arg" points to the '{'.
3313 */
3314 static int
3315compile_dict(char_u **arg, cctx_T *cctx, int literal)
3316{
3317 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003318 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 int count = 0;
3320 dict_T *d = dict_alloc();
3321 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003322 char_u *whitep = *arg;
3323 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324
3325 if (d == NULL)
3326 return FAIL;
3327 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003328 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 {
3330 char_u *key = NULL;
3331
Bram Moolenaar23c55272020-06-21 16:58:13 +02003332 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003333 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003334 *arg = NULL;
3335 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003336 }
3337
3338 if (**arg == '}')
3339 break;
3340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 if (literal)
3342 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003343 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344
Bram Moolenaar2c330432020-04-13 14:41:35 +02003345 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 {
3347 semsg(_("E1014: Invalid key: %s"), *arg);
3348 return FAIL;
3349 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003350 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 if (generate_PUSHS(cctx, key) == FAIL)
3352 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003353 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 }
3355 else
3356 {
3357 isn_T *isn;
3358
Bram Moolenaara5565e42020-05-09 15:44:01 +02003359 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3362 if (isn->isn_type == ISN_PUSHS)
3363 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003364 else
3365 {
3366 type_T *keytype = ((type_T **)stack->ga_data)
3367 [stack->ga_len - 1];
3368 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3369 return FAIL;
3370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 }
3372
3373 // Check for duplicate keys, if using string keys.
3374 if (key != NULL)
3375 {
3376 item = dict_find(d, key, -1);
3377 if (item != NULL)
3378 {
3379 semsg(_(e_duplicate_key), key);
3380 goto failret;
3381 }
3382 item = dictitem_alloc(key);
3383 if (item != NULL)
3384 {
3385 item->di_tv.v_type = VAR_UNKNOWN;
3386 item->di_tv.v_lock = 0;
3387 if (dict_add(d, item) == FAIL)
3388 dictitem_free(item);
3389 }
3390 }
3391
3392 *arg = skipwhite(*arg);
3393 if (**arg != ':')
3394 {
3395 semsg(_(e_missing_dict_colon), *arg);
3396 return FAIL;
3397 }
3398
Bram Moolenaar2c330432020-04-13 14:41:35 +02003399 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003401 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003402 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003403 *arg = NULL;
3404 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003405 }
3406
Bram Moolenaara5565e42020-05-09 15:44:01 +02003407 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 return FAIL;
3409 ++count;
3410
Bram Moolenaar2c330432020-04-13 14:41:35 +02003411 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003412 *arg = skipwhite(*arg);
3413 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003414 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003415 *arg = NULL;
3416 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003417 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 if (**arg == '}')
3419 break;
3420 if (**arg != ',')
3421 {
3422 semsg(_(e_missing_dict_comma), *arg);
3423 goto failret;
3424 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003425 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 *arg = skipwhite(*arg + 1);
3427 }
3428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 *arg = *arg + 1;
3430
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003431 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003432 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003433 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003434 *arg += STRLEN(*arg);
3435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 dict_unref(d);
3437 return generate_NEWDICT(cctx, count);
3438
3439failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003440 if (*arg == NULL)
3441 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 dict_unref(d);
3443 return FAIL;
3444}
3445
3446/*
3447 * Compile "&option".
3448 */
3449 static int
3450compile_get_option(char_u **arg, cctx_T *cctx)
3451{
3452 typval_T rettv;
3453 char_u *start = *arg;
3454 int ret;
3455
3456 // parse the option and get the current value to get the type.
3457 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003458 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 if (ret == OK)
3460 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003461 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 char_u *name = vim_strnsave(start, *arg - start);
3463 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3464
3465 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3466 vim_free(name);
3467 }
3468 clear_tv(&rettv);
3469
3470 return ret;
3471}
3472
3473/*
3474 * Compile "$VAR".
3475 */
3476 static int
3477compile_get_env(char_u **arg, cctx_T *cctx)
3478{
3479 char_u *start = *arg;
3480 int len;
3481 int ret;
3482 char_u *name;
3483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 ++*arg;
3485 len = get_env_len(arg);
3486 if (len == 0)
3487 {
3488 semsg(_(e_syntax_at), start - 1);
3489 return FAIL;
3490 }
3491
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003492 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 name = vim_strnsave(start, len + 1);
3494 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3495 vim_free(name);
3496 return ret;
3497}
3498
3499/*
3500 * Compile "@r".
3501 */
3502 static int
3503compile_get_register(char_u **arg, cctx_T *cctx)
3504{
3505 int ret;
3506
3507 ++*arg;
3508 if (**arg == NUL)
3509 {
3510 semsg(_(e_syntax_at), *arg - 1);
3511 return FAIL;
3512 }
3513 if (!valid_yank_reg(**arg, TRUE))
3514 {
3515 emsg_invreg(**arg);
3516 return FAIL;
3517 }
3518 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3519 ++*arg;
3520 return ret;
3521}
3522
3523/*
3524 * Apply leading '!', '-' and '+' to constant "rettv".
3525 */
3526 static int
3527apply_leader(typval_T *rettv, char_u *start, char_u *end)
3528{
3529 char_u *p = end;
3530
3531 // this works from end to start
3532 while (p > start)
3533 {
3534 --p;
3535 if (*p == '-' || *p == '+')
3536 {
3537 // only '-' has an effect, for '+' we only check the type
3538#ifdef FEAT_FLOAT
3539 if (rettv->v_type == VAR_FLOAT)
3540 {
3541 if (*p == '-')
3542 rettv->vval.v_float = -rettv->vval.v_float;
3543 }
3544 else
3545#endif
3546 {
3547 varnumber_T val;
3548 int error = FALSE;
3549
3550 // tv_get_number_chk() accepts a string, but we don't want that
3551 // here
3552 if (check_not_string(rettv) == FAIL)
3553 return FAIL;
3554 val = tv_get_number_chk(rettv, &error);
3555 clear_tv(rettv);
3556 if (error)
3557 return FAIL;
3558 if (*p == '-')
3559 val = -val;
3560 rettv->v_type = VAR_NUMBER;
3561 rettv->vval.v_number = val;
3562 }
3563 }
3564 else
3565 {
3566 int v = tv2bool(rettv);
3567
3568 // '!' is permissive in the type.
3569 clear_tv(rettv);
3570 rettv->v_type = VAR_BOOL;
3571 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3572 }
3573 }
3574 return OK;
3575}
3576
3577/*
3578 * Recognize v: variables that are constants and set "rettv".
3579 */
3580 static void
3581get_vim_constant(char_u **arg, typval_T *rettv)
3582{
3583 if (STRNCMP(*arg, "v:true", 6) == 0)
3584 {
3585 rettv->v_type = VAR_BOOL;
3586 rettv->vval.v_number = VVAL_TRUE;
3587 *arg += 6;
3588 }
3589 else if (STRNCMP(*arg, "v:false", 7) == 0)
3590 {
3591 rettv->v_type = VAR_BOOL;
3592 rettv->vval.v_number = VVAL_FALSE;
3593 *arg += 7;
3594 }
3595 else if (STRNCMP(*arg, "v:null", 6) == 0)
3596 {
3597 rettv->v_type = VAR_SPECIAL;
3598 rettv->vval.v_number = VVAL_NULL;
3599 *arg += 6;
3600 }
3601 else if (STRNCMP(*arg, "v:none", 6) == 0)
3602 {
3603 rettv->v_type = VAR_SPECIAL;
3604 rettv->vval.v_number = VVAL_NONE;
3605 *arg += 6;
3606 }
3607}
3608
Bram Moolenaar61a89812020-05-07 16:58:17 +02003609 static exptype_T
3610get_compare_type(char_u *p, int *len, int *type_is)
3611{
3612 exptype_T type = EXPR_UNKNOWN;
3613 int i;
3614
3615 switch (p[0])
3616 {
3617 case '=': if (p[1] == '=')
3618 type = EXPR_EQUAL;
3619 else if (p[1] == '~')
3620 type = EXPR_MATCH;
3621 break;
3622 case '!': if (p[1] == '=')
3623 type = EXPR_NEQUAL;
3624 else if (p[1] == '~')
3625 type = EXPR_NOMATCH;
3626 break;
3627 case '>': if (p[1] != '=')
3628 {
3629 type = EXPR_GREATER;
3630 *len = 1;
3631 }
3632 else
3633 type = EXPR_GEQUAL;
3634 break;
3635 case '<': if (p[1] != '=')
3636 {
3637 type = EXPR_SMALLER;
3638 *len = 1;
3639 }
3640 else
3641 type = EXPR_SEQUAL;
3642 break;
3643 case 'i': if (p[1] == 's')
3644 {
3645 // "is" and "isnot"; but not a prefix of a name
3646 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3647 *len = 5;
3648 i = p[*len];
3649 if (!isalnum(i) && i != '_')
3650 {
3651 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3652 *type_is = TRUE;
3653 }
3654 }
3655 break;
3656 }
3657 return type;
3658}
3659
3660/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 * Compile code to apply '-', '+' and '!'.
3662 */
3663 static int
3664compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3665{
3666 char_u *p = end;
3667
3668 // this works from end to start
3669 while (p > start)
3670 {
3671 --p;
3672 if (*p == '-' || *p == '+')
3673 {
3674 int negate = *p == '-';
3675 isn_T *isn;
3676
3677 // TODO: check type
3678 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3679 {
3680 --p;
3681 if (*p == '-')
3682 negate = !negate;
3683 }
3684 // only '-' has an effect, for '+' we only check the type
3685 if (negate)
3686 isn = generate_instr(cctx, ISN_NEGATENR);
3687 else
3688 isn = generate_instr(cctx, ISN_CHECKNR);
3689 if (isn == NULL)
3690 return FAIL;
3691 }
3692 else
3693 {
3694 int invert = TRUE;
3695
3696 while (p > start && p[-1] == '!')
3697 {
3698 --p;
3699 invert = !invert;
3700 }
3701 if (generate_2BOOL(cctx, invert) == FAIL)
3702 return FAIL;
3703 }
3704 }
3705 return OK;
3706}
3707
3708/*
3709 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003710 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 */
3712 static int
3713compile_subscript(
3714 char_u **arg,
3715 cctx_T *cctx,
3716 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003717 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003718 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719{
3720 for (;;)
3721 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003722 char_u *p = skipwhite(*arg);
3723
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003724 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003725 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003726 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003727
3728 // If a following line starts with "->{" or "->X" advance to that
3729 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003730 // Also if a following line starts with ".x".
3731 if (next != NULL &&
3732 ((next[0] == '-' && next[1] == '>'
3733 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3734 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003735 {
3736 next = next_line_from_context(cctx, TRUE);
3737 if (next == NULL)
3738 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003739 *arg = next;
3740 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003741 }
3742 }
3743
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003744 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003746 garray_T *stack = &cctx->ctx_type_stack;
3747 type_T *type;
3748 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003750 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003751 return FAIL;
3752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003754 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3755
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003756 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3758 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003759 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 return FAIL;
3761 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003762 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 {
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 Moolenaar8a7d6542020-01-26 15:56:19 +01003767 // something->method()
3768 // Apply the '!', '-' and '+' first:
3769 // -1.0->func() works like (-1.0)->func()
3770 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3771 return FAIL;
3772 *start_leader = end_leader; // don't apply again later
3773
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003774 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003775 *arg = skipwhite(p);
3776 if (may_get_next_line(p, arg, cctx) == FAIL)
3777 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 if (**arg == '{')
3779 {
3780 // lambda call: list->{lambda}
3781 if (compile_lambda_call(arg, cctx) == FAIL)
3782 return FAIL;
3783 }
3784 else
3785 {
3786 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003787 p = *arg;
3788 if (ASCII_ISALPHA(*p) && p[1] == ':')
3789 p += 2;
3790 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 ;
3792 if (*p != '(')
3793 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003794 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 return FAIL;
3796 }
3797 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003798 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 return FAIL;
3800 }
3801 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003802 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003804 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003805 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003806 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003807
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003808 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003809 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003810 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003811 // TODO: blob index
3812 // TODO: more arguments
3813 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003814 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003815 return FAIL;
3816
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003817 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003818 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003819 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003820 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003821 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 return FAIL;
3823
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003824 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3825 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 if (**arg != ']')
3827 {
3828 emsg(_(e_missbrac));
3829 return FAIL;
3830 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003831 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003833 // We can index a list and a dict. If we don't know the type
3834 // we can use the index value type.
3835 // TODO: If we don't know use an instruction to figure it out at
3836 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003837 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003838 vtype = (*typep)->tt_type;
3839 if (*typep == &t_any)
3840 {
3841 type_T *valtype = ((type_T **)stack->ga_data)
3842 [stack->ga_len - 1];
3843 if (valtype == &t_string)
3844 vtype = VAR_DICT;
3845 }
3846 if (vtype == VAR_DICT)
3847 {
3848 if ((*typep)->tt_type == VAR_DICT)
3849 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003850 else
3851 {
3852 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3853 return FAIL;
3854 *typep = &t_any;
3855 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003856 if (may_generate_2STRING(-1, cctx) == FAIL)
3857 return FAIL;
3858 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3859 return FAIL;
3860 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003861 else if (vtype == VAR_STRING)
3862 {
3863 *typep = &t_number;
3864 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3865 return FAIL;
3866 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003867 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003868 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003869 if ((*typep)->tt_type == VAR_LIST)
3870 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003871 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003872 return FAIL;
3873 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003874 else
3875 {
3876 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003877 return FAIL;
3878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003880 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003882 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003883 return FAIL;
3884
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003885 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003886 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3887 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003889 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 if (eval_isnamec1(*p))
3891 while (eval_isnamec(*p))
3892 MB_PTR_ADV(p);
3893 if (p == *arg)
3894 {
3895 semsg(_(e_syntax_at), *arg);
3896 return FAIL;
3897 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003898 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 return FAIL;
3900 *arg = p;
3901 }
3902 else
3903 break;
3904 }
3905
3906 // TODO - see handle_subscript():
3907 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3908 // Don't do this when "Func" is already a partial that was bound
3909 // explicitly (pt_auto is FALSE).
3910
3911 return OK;
3912}
3913
3914/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003915 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3916 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003918 * If the value is a constant "ppconst->pp_ret" will be set.
3919 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003920 *
3921 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 */
3923
3924/*
3925 * number number constant
3926 * 0zFFFFFFFF Blob constant
3927 * "string" string constant
3928 * 'string' literal string constant
3929 * &option-name option value
3930 * @r register contents
3931 * identifier variable value
3932 * function() function call
3933 * $VAR environment variable
3934 * (expression) nested expression
3935 * [expr, expr] List
3936 * {key: val, key: val} Dictionary
3937 * #{key: val, key: val} Dictionary with literal keys
3938 *
3939 * Also handle:
3940 * ! in front logical NOT
3941 * - in front unary minus
3942 * + in front unary plus (ignored)
3943 * trailing (arg) funcref/partial call
3944 * trailing [] subscript in String or List
3945 * trailing .name entry in Dictionary
3946 * trailing ->name() method call
3947 */
3948 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003949compile_expr7(
3950 char_u **arg,
3951 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003952 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 char_u *start_leader, *end_leader;
3955 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003956 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003957 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958
3959 /*
3960 * Skip '!', '-' and '+' characters. They are handled later.
3961 */
3962 start_leader = *arg;
3963 while (**arg == '!' || **arg == '-' || **arg == '+')
3964 *arg = skipwhite(*arg + 1);
3965 end_leader = *arg;
3966
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003967 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 switch (**arg)
3969 {
3970 /*
3971 * Number constant.
3972 */
3973 case '0': // also for blob starting with 0z
3974 case '1':
3975 case '2':
3976 case '3':
3977 case '4':
3978 case '5':
3979 case '6':
3980 case '7':
3981 case '8':
3982 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003983 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 return FAIL;
3985 break;
3986
3987 /*
3988 * String constant: "string".
3989 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003990 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 return FAIL;
3992 break;
3993
3994 /*
3995 * Literal string constant: 'str''ing'.
3996 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003997 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 return FAIL;
3999 break;
4000
4001 /*
4002 * Constant Vim variable.
4003 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004004 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 ret = NOTDONE;
4006 break;
4007
4008 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004009 * "true" constant
4010 */
4011 case 't': if (STRNCMP(*arg, "true", 4) == 0
4012 && !eval_isnamec((*arg)[4]))
4013 {
4014 *arg += 4;
4015 rettv->v_type = VAR_BOOL;
4016 rettv->vval.v_number = VVAL_TRUE;
4017 }
4018 else
4019 ret = NOTDONE;
4020 break;
4021
4022 /*
4023 * "false" constant
4024 */
4025 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4026 && !eval_isnamec((*arg)[5]))
4027 {
4028 *arg += 5;
4029 rettv->v_type = VAR_BOOL;
4030 rettv->vval.v_number = VVAL_FALSE;
4031 }
4032 else
4033 ret = NOTDONE;
4034 break;
4035
4036 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 * List: [expr, expr]
4038 */
4039 case '[': ret = compile_list(arg, cctx);
4040 break;
4041
4042 /*
4043 * Dictionary: #{key: val, key: val}
4044 */
4045 case '#': if ((*arg)[1] == '{')
4046 {
4047 ++*arg;
4048 ret = compile_dict(arg, cctx, TRUE);
4049 }
4050 else
4051 ret = NOTDONE;
4052 break;
4053
4054 /*
4055 * Lambda: {arg, arg -> expr}
4056 * Dictionary: {'key': val, 'key': val}
4057 */
4058 case '{': {
4059 char_u *start = skipwhite(*arg + 1);
4060
4061 // Find out what comes after the arguments.
4062 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004063 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 if (ret != FAIL && *start == '>')
4065 ret = compile_lambda(arg, cctx);
4066 else
4067 ret = compile_dict(arg, cctx, FALSE);
4068 }
4069 break;
4070
4071 /*
4072 * Option value: &name
4073 */
4074 case '&': ret = compile_get_option(arg, cctx);
4075 break;
4076
4077 /*
4078 * Environment variable: $VAR.
4079 */
4080 case '$': ret = compile_get_env(arg, cctx);
4081 break;
4082
4083 /*
4084 * Register contents: @r.
4085 */
4086 case '@': ret = compile_get_register(arg, cctx);
4087 break;
4088 /*
4089 * nested expression: (expression).
4090 */
4091 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004092
4093 // recursive!
4094 if (ppconst->pp_used <= PPSIZE - 10)
4095 {
4096 ret = compile_expr1(arg, cctx, ppconst);
4097 }
4098 else
4099 {
4100 // Not enough space in ppconst, flush constants.
4101 if (generate_ppconst(cctx, ppconst) == FAIL)
4102 return FAIL;
4103 ret = compile_expr0(arg, cctx);
4104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 *arg = skipwhite(*arg);
4106 if (**arg == ')')
4107 ++*arg;
4108 else if (ret == OK)
4109 {
4110 emsg(_(e_missing_close));
4111 ret = FAIL;
4112 }
4113 break;
4114
4115 default: ret = NOTDONE;
4116 break;
4117 }
4118 if (ret == FAIL)
4119 return FAIL;
4120
Bram Moolenaar1c747212020-05-09 18:28:34 +02004121 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 {
4123 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004124 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004126 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 return FAIL;
4128 }
4129 start_leader = end_leader; // don't apply again below
4130
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004131 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004132 clear_tv(rettv);
4133 else
4134 // A constant expression can possibly be handled compile time,
4135 // return the value instead of generating code.
4136 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 }
4138 else if (ret == NOTDONE)
4139 {
4140 char_u *p;
4141 int r;
4142
4143 if (!eval_isnamec1(**arg))
4144 {
4145 semsg(_("E1015: Name expected: %s"), *arg);
4146 return FAIL;
4147 }
4148
4149 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004150 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004152 {
4153 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4154 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004156 {
4157 if (generate_ppconst(cctx, ppconst) == FAIL)
4158 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 if (r == FAIL)
4162 return FAIL;
4163 }
4164
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004165 // Handle following "[]", ".member", etc.
4166 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004167 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004168 ppconst) == FAIL)
4169 return FAIL;
4170 if (ppconst->pp_used > 0)
4171 {
4172 // apply the '!', '-' and '+' before the constant
4173 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4174 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4175 return FAIL;
4176 return OK;
4177 }
4178 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004180 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181}
4182
4183/*
4184 * * number multiplication
4185 * / number division
4186 * % number modulo
4187 */
4188 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004189compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190{
4191 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004192 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004193 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004195 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004196 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 return FAIL;
4198
4199 /*
4200 * Repeat computing, until no "*", "/" or "%" is following.
4201 */
4202 for (;;)
4203 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004204 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 if (*op != '*' && *op != '/' && *op != '%')
4206 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004207 if (next != NULL)
4208 {
4209 *arg = next_line_from_context(cctx, TRUE);
4210 op = skipwhite(*arg);
4211 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004212
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004213 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 {
4215 char_u buf[3];
4216
4217 vim_strncpy(buf, op, 1);
4218 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004219 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 }
4221 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004222 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004223 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004225 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004226 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004228
4229 if (ppconst->pp_used == ppconst_used + 2
4230 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4231 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004232 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004233 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4234 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004235 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004237 // both are numbers: compute the result
4238 switch (*op)
4239 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004240 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004241 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004242 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004243 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004244 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004245 break;
4246 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004247 tv1->vval.v_number = res;
4248 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004249 }
4250 else
4251 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004252 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004253 generate_two_op(cctx, op);
4254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 }
4256
4257 return OK;
4258}
4259
4260/*
4261 * + number addition
4262 * - number subtraction
4263 * .. string concatenation
4264 */
4265 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004266compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267{
4268 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004269 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004271 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272
4273 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004274 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 return FAIL;
4276
4277 /*
4278 * Repeat computing, until no "+", "-" or ".." is following.
4279 */
4280 for (;;)
4281 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004282 op = may_peek_next_line(cctx, *arg, &next);
4283 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 break;
4285 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004286 if (next != NULL)
4287 {
4288 *arg = next_line_from_context(cctx, TRUE);
4289 op = skipwhite(*arg);
4290 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004292 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 {
4294 char_u buf[3];
4295
4296 vim_strncpy(buf, op, oplen);
4297 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004298 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 }
4300
4301 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004302 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004303 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004305 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004306 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 return FAIL;
4308
Bram Moolenaara5565e42020-05-09 15:44:01 +02004309 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004310 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004311 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4312 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4313 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4314 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4317 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004318
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004319 // concat/subtract/add constant numbers
4320 if (*op == '+')
4321 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4322 else if (*op == '-')
4323 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4324 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004325 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004326 // concatenate constant strings
4327 char_u *s1 = tv1->vval.v_string;
4328 char_u *s2 = tv2->vval.v_string;
4329 size_t len1 = STRLEN(s1);
4330
4331 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4332 if (tv1->vval.v_string == NULL)
4333 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004334 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004335 return FAIL;
4336 }
4337 mch_memmove(tv1->vval.v_string, s1, len1);
4338 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004339 vim_free(s1);
4340 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004341 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004342 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 }
4344 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004345 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004346 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004347 if (*op == '.')
4348 {
4349 if (may_generate_2STRING(-2, cctx) == FAIL
4350 || may_generate_2STRING(-1, cctx) == FAIL)
4351 return FAIL;
4352 generate_instr_drop(cctx, ISN_CONCAT, 1);
4353 }
4354 else
4355 generate_two_op(cctx, op);
4356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 }
4358
4359 return OK;
4360}
4361
4362/*
4363 * expr5a == expr5b
4364 * expr5a =~ expr5b
4365 * expr5a != expr5b
4366 * expr5a !~ expr5b
4367 * expr5a > expr5b
4368 * expr5a >= expr5b
4369 * expr5a < expr5b
4370 * expr5a <= expr5b
4371 * expr5a is expr5b
4372 * expr5a isnot expr5b
4373 *
4374 * Produces instructions:
4375 * EVAL expr5a Push result of "expr5a"
4376 * EVAL expr5b Push result of "expr5b"
4377 * COMPARE one of the compare instructions
4378 */
4379 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004380compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381{
4382 exptype_T type = EXPR_UNKNOWN;
4383 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004384 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004387 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388
4389 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004390 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 return FAIL;
4392
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004393 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004394 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395
4396 /*
4397 * If there is a comparative operator, use it.
4398 */
4399 if (type != EXPR_UNKNOWN)
4400 {
4401 int ic = FALSE; // Default: do not ignore case
4402
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004403 if (next != NULL)
4404 {
4405 *arg = next_line_from_context(cctx, TRUE);
4406 p = skipwhite(*arg);
4407 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 if (type_is && (p[len] == '?' || p[len] == '#'))
4409 {
4410 semsg(_(e_invexpr2), *arg);
4411 return FAIL;
4412 }
4413 // extra question mark appended: ignore case
4414 if (p[len] == '?')
4415 {
4416 ic = TRUE;
4417 ++len;
4418 }
4419 // extra '#' appended: match case (ignored)
4420 else if (p[len] == '#')
4421 ++len;
4422 // nothing appended: match case
4423
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004424 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 {
4426 char_u buf[7];
4427
4428 vim_strncpy(buf, p, len);
4429 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004430 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 }
4432
4433 // get the second variable
4434 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004435 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004436 return FAIL;
4437
Bram Moolenaara5565e42020-05-09 15:44:01 +02004438 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 return FAIL;
4440
Bram Moolenaara5565e42020-05-09 15:44:01 +02004441 if (ppconst->pp_used == ppconst_used + 2)
4442 {
4443 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4444 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4445 int ret;
4446
4447 // Both sides are a constant, compute the result now.
4448 // First check for a valid combination of types, this is more
4449 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004450 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004451 ret = FAIL;
4452 else
4453 {
4454 ret = typval_compare(tv1, tv2, type, ic);
4455 tv1->v_type = VAR_BOOL;
4456 tv1->vval.v_number = tv1->vval.v_number
4457 ? VVAL_TRUE : VVAL_FALSE;
4458 clear_tv(tv2);
4459 --ppconst->pp_used;
4460 }
4461 return ret;
4462 }
4463
4464 generate_ppconst(cctx, ppconst);
4465 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 }
4467
4468 return OK;
4469}
4470
Bram Moolenaar7f141552020-05-09 17:35:53 +02004471static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473/*
4474 * Compile || or &&.
4475 */
4476 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004477compile_and_or(
4478 char_u **arg,
4479 cctx_T *cctx,
4480 char *op,
4481 ppconst_T *ppconst,
4482 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004484 char_u *next;
4485 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 int opchar = *op;
4487
4488 if (p[0] == opchar && p[1] == opchar)
4489 {
4490 garray_T *instr = &cctx->ctx_instr;
4491 garray_T end_ga;
4492
4493 /*
4494 * Repeat until there is no following "||" or "&&"
4495 */
4496 ga_init2(&end_ga, sizeof(int), 10);
4497 while (p[0] == opchar && p[1] == opchar)
4498 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004499 if (next != NULL)
4500 {
4501 *arg = next_line_from_context(cctx, TRUE);
4502 p = skipwhite(*arg);
4503 }
4504
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004505 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4506 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004508 return FAIL;
4509 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510
Bram Moolenaara5565e42020-05-09 15:44:01 +02004511 // TODO: use ppconst if the value is a constant
4512 generate_ppconst(cctx, ppconst);
4513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 if (ga_grow(&end_ga, 1) == FAIL)
4515 {
4516 ga_clear(&end_ga);
4517 return FAIL;
4518 }
4519 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4520 ++end_ga.ga_len;
4521 generate_JUMP(cctx, opchar == '|'
4522 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4523
4524 // eval the next expression
4525 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004526 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004527 return FAIL;
4528
Bram Moolenaara5565e42020-05-09 15:44:01 +02004529 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4530 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 {
4532 ga_clear(&end_ga);
4533 return FAIL;
4534 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004535
4536 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004538 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539
4540 // Fill in the end label in all jumps.
4541 while (end_ga.ga_len > 0)
4542 {
4543 isn_T *isn;
4544
4545 --end_ga.ga_len;
4546 isn = ((isn_T *)instr->ga_data)
4547 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4548 isn->isn_arg.jump.jump_where = instr->ga_len;
4549 }
4550 ga_clear(&end_ga);
4551 }
4552
4553 return OK;
4554}
4555
4556/*
4557 * expr4a && expr4a && expr4a logical AND
4558 *
4559 * Produces instructions:
4560 * EVAL expr4a Push result of "expr4a"
4561 * JUMP_AND_KEEP_IF_FALSE end
4562 * EVAL expr4b Push result of "expr4b"
4563 * JUMP_AND_KEEP_IF_FALSE end
4564 * EVAL expr4c Push result of "expr4c"
4565 * end:
4566 */
4567 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004568compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004570 int ppconst_used = ppconst->pp_used;
4571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004573 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 return FAIL;
4575
4576 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004577 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578}
4579
4580/*
4581 * expr3a || expr3b || expr3c logical OR
4582 *
4583 * Produces instructions:
4584 * EVAL expr3a Push result of "expr3a"
4585 * JUMP_AND_KEEP_IF_TRUE end
4586 * EVAL expr3b Push result of "expr3b"
4587 * JUMP_AND_KEEP_IF_TRUE end
4588 * EVAL expr3c Push result of "expr3c"
4589 * end:
4590 */
4591 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004592compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004594 int ppconst_used = ppconst->pp_used;
4595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 return FAIL;
4599
4600 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004601 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602}
4603
4604/*
4605 * Toplevel expression: expr2 ? expr1a : expr1b
4606 *
4607 * Produces instructions:
4608 * EVAL expr2 Push result of "expr"
4609 * JUMP_IF_FALSE alt jump if false
4610 * EVAL expr1a
4611 * JUMP_ALWAYS end
4612 * alt: EVAL expr1b
4613 * end:
4614 */
4615 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004616compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617{
4618 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004619 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004620 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621
Bram Moolenaar61a89812020-05-07 16:58:17 +02004622 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004623 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 return FAIL;
4625
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004626 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 if (*p == '?')
4628 {
4629 garray_T *instr = &cctx->ctx_instr;
4630 garray_T *stack = &cctx->ctx_type_stack;
4631 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004632 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004634 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004636 int has_const_expr = FALSE;
4637 int const_value = FALSE;
4638 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004640 if (next != NULL)
4641 {
4642 *arg = next_line_from_context(cctx, TRUE);
4643 p = skipwhite(*arg);
4644 }
4645
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004646 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4647 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004649 return FAIL;
4650 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651
Bram Moolenaara5565e42020-05-09 15:44:01 +02004652 if (ppconst->pp_used == ppconst_used + 1)
4653 {
4654 // the condition is a constant, we know whether the ? or the :
4655 // expression is to be evaluated.
4656 has_const_expr = TRUE;
4657 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4658 clear_tv(&ppconst->pp_tv[ppconst_used]);
4659 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004660 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4661 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004662 }
4663 else
4664 {
4665 generate_ppconst(cctx, ppconst);
4666 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4667 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668
4669 // evaluate the second expression; any type is accepted
4670 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004671 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004672 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004673 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004674 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675
Bram Moolenaara5565e42020-05-09 15:44:01 +02004676 if (!has_const_expr)
4677 {
4678 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679
Bram Moolenaara5565e42020-05-09 15:44:01 +02004680 // remember the type and drop it
4681 --stack->ga_len;
4682 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683
Bram Moolenaara5565e42020-05-09 15:44:01 +02004684 end_idx = instr->ga_len;
4685 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4686
4687 // jump here from JUMP_IF_FALSE
4688 isn = ((isn_T *)instr->ga_data) + alt_idx;
4689 isn->isn_arg.jump.jump_where = instr->ga_len;
4690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691
4692 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004693 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 if (*p != ':')
4695 {
4696 emsg(_(e_missing_colon));
4697 return FAIL;
4698 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004699 if (next != NULL)
4700 {
4701 *arg = next_line_from_context(cctx, TRUE);
4702 p = skipwhite(*arg);
4703 }
4704
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004705 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4706 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004708 return FAIL;
4709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710
4711 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004712 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004713 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4714 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004716 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004717 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004719 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720
Bram Moolenaara5565e42020-05-09 15:44:01 +02004721 if (!has_const_expr)
4722 {
4723 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724
Bram Moolenaara5565e42020-05-09 15:44:01 +02004725 // If the types differ, the result has a more generic type.
4726 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4727 common_type(type1, type2, &type2, cctx->ctx_type_list);
4728
4729 // jump here from JUMP_ALWAYS
4730 isn = ((isn_T *)instr->ga_data) + end_idx;
4731 isn->isn_arg.jump.jump_where = instr->ga_len;
4732 }
4733
4734 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 }
4736 return OK;
4737}
4738
4739/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004740 * Toplevel expression.
4741 */
4742 static int
4743compile_expr0(char_u **arg, cctx_T *cctx)
4744{
4745 ppconst_T ppconst;
4746
4747 CLEAR_FIELD(ppconst);
4748 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4749 {
4750 clear_ppconst(&ppconst);
4751 return FAIL;
4752 }
4753 if (generate_ppconst(cctx, &ppconst) == FAIL)
4754 return FAIL;
4755 return OK;
4756}
4757
4758/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759 * compile "return [expr]"
4760 */
4761 static char_u *
4762compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4763{
4764 char_u *p = arg;
4765 garray_T *stack = &cctx->ctx_type_stack;
4766 type_T *stack_type;
4767
4768 if (*p != NUL && *p != '|' && *p != '\n')
4769 {
4770 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004771 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 return NULL;
4773
4774 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4775 if (set_return_type)
4776 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004777 else
4778 {
4779 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4780 && stack_type->tt_type != VAR_VOID
4781 && stack_type->tt_type != VAR_UNKNOWN)
4782 {
4783 emsg(_("E1096: Returning a value in a function without a return type"));
4784 return NULL;
4785 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004786 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4787 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004789 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 }
4791 else
4792 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004793 // "set_return_type" cannot be TRUE, only used for a lambda which
4794 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004795 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4796 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 {
4798 emsg(_("E1003: Missing return value"));
4799 return NULL;
4800 }
4801
4802 // No argument, return zero.
4803 generate_PUSHNR(cctx, 0);
4804 }
4805
4806 if (generate_instr(cctx, ISN_RETURN) == NULL)
4807 return NULL;
4808
4809 // "return val | endif" is possible
4810 return skipwhite(p);
4811}
4812
4813/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004814 * Get a line from the compilation context, compatible with exarg_T getline().
4815 * Return a pointer to the line in allocated memory.
4816 * Return NULL for end-of-file or some error.
4817 */
4818 static char_u *
4819exarg_getline(
4820 int c UNUSED,
4821 void *cookie,
4822 int indent UNUSED,
4823 int do_concat UNUSED)
4824{
4825 cctx_T *cctx = (cctx_T *)cookie;
4826
4827 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4828 {
4829 iemsg("Heredoc got to end");
4830 return NULL;
4831 }
4832 ++cctx->ctx_lnum;
4833 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4834 [cctx->ctx_lnum]);
4835}
4836
4837/*
4838 * Compile a nested :def command.
4839 */
4840 static char_u *
4841compile_nested_function(exarg_T *eap, cctx_T *cctx)
4842{
4843 char_u *name_start = eap->arg;
4844 char_u *name_end = to_name_end(eap->arg, FALSE);
4845 char_u *name = get_lambda_name();
4846 lvar_T *lvar;
4847 ufunc_T *ufunc;
4848
4849 eap->arg = name_end;
4850 eap->getline = exarg_getline;
4851 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004852 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004853 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004854 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004855
Bram Moolenaar822ba242020-05-24 23:00:18 +02004856 if (ufunc == NULL)
4857 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004858 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004859 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004860 return NULL;
4861
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004862 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004863 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004864 TRUE, ufunc->uf_func_type);
4865
4866 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4867 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4868 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004869
Bram Moolenaar61a89812020-05-07 16:58:17 +02004870 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004871 return (char_u *)"";
4872}
4873
4874/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 * Return the length of an assignment operator, or zero if there isn't one.
4876 */
4877 int
4878assignment_len(char_u *p, int *heredoc)
4879{
4880 if (*p == '=')
4881 {
4882 if (p[1] == '<' && p[2] == '<')
4883 {
4884 *heredoc = TRUE;
4885 return 3;
4886 }
4887 return 1;
4888 }
4889 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4890 return 2;
4891 if (STRNCMP(p, "..=", 3) == 0)
4892 return 3;
4893 return 0;
4894}
4895
4896// words that cannot be used as a variable
4897static char *reserved[] = {
4898 "true",
4899 "false",
4900 NULL
4901};
4902
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004903typedef enum {
4904 dest_local,
4905 dest_option,
4906 dest_env,
4907 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004908 dest_buffer,
4909 dest_window,
4910 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004911 dest_vimvar,
4912 dest_script,
4913 dest_reg,
4914} assign_dest_T;
4915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004917 * Generate the load instruction for "name".
4918 */
4919 static void
4920generate_loadvar(
4921 cctx_T *cctx,
4922 assign_dest_T dest,
4923 char_u *name,
4924 lvar_T *lvar,
4925 type_T *type)
4926{
4927 switch (dest)
4928 {
4929 case dest_option:
4930 // TODO: check the option exists
4931 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4932 break;
4933 case dest_global:
4934 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4935 break;
4936 case dest_buffer:
4937 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4938 break;
4939 case dest_window:
4940 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4941 break;
4942 case dest_tab:
4943 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4944 break;
4945 case dest_script:
4946 compile_load_scriptvar(cctx,
4947 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4948 break;
4949 case dest_env:
4950 // Include $ in the name here
4951 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4952 break;
4953 case dest_reg:
4954 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4955 break;
4956 case dest_vimvar:
4957 generate_LOADV(cctx, name + 2, TRUE);
4958 break;
4959 case dest_local:
4960 if (lvar->lv_from_outer)
4961 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4962 NULL, type);
4963 else
4964 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4965 break;
4966 }
4967}
4968
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004969 void
4970vim9_declare_error(char_u *name)
4971{
4972 char *scope = "";
4973
4974 switch (*name)
4975 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004976 case 'g': scope = _("global"); break;
4977 case 'b': scope = _("buffer"); break;
4978 case 'w': scope = _("window"); break;
4979 case 't': scope = _("tab"); break;
4980 case 'v': scope = "v:"; break;
4981 case '$': semsg(_(e_declare_env_var), name); return;
4982 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004983 }
4984 semsg(_(e_declare_var), scope, name);
4985}
4986
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004987/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004988 * Compile declaration and assignment:
4989 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004991 * Return NULL for an error.
4992 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 */
4994 static char_u *
4995compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4996{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004997 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004999 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 char_u *ret = NULL;
5001 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005002 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005005 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007 int oplen = 0;
5008 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005009 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005010 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005011 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005015 // Skip over the "var" or "[var, var]" to get to any "=".
5016 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5017 if (p == NULL)
5018 return *arg == '[' ? arg : NULL;
5019
5020 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005022 // TODO: should we allow this, and figure out type inference from list
5023 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005024 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 return NULL;
5026 }
5027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 sp = p;
5029 p = skipwhite(p);
5030 op = p;
5031 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005032
5033 if (var_count > 0 && oplen == 0)
5034 // can be something like "[1, 2]->func()"
5035 return arg;
5036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005037 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5038 {
5039 char_u buf[4];
5040
5041 vim_strncpy(buf, op, oplen);
5042 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005043 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005044 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 if (heredoc)
5047 {
5048 list_T *l;
5049 listitem_T *li;
5050
5051 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005052 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005053 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005054 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055
5056 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005057 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 {
5059 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5060 li->li_tv.vval.v_string = NULL;
5061 }
5062 generate_NEWLIST(cctx, l->lv_len);
5063 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005064 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 list_free(l);
5066 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005067 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005069 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005071 // for "[var, var] = expr" evaluate the expression here, loop over the
5072 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005073
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005074 p = skipwhite(op + oplen);
5075 if (compile_expr0(&p, cctx) == FAIL)
5076 return NULL;
5077 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005079 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005081 type_T *stacktype;
5082
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005083 stacktype = stack->ga_len == 0 ? &t_void
5084 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005085 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005087 emsg(_(e_cannot_use_void));
5088 goto theend;
5089 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005090 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005091 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005092 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005093 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5094 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005095 }
5096 }
5097
5098 /*
5099 * Loop over variables in "[var, var] = expr".
5100 * For "var = expr" and "let var: type" this is done only once.
5101 */
5102 if (var_count > 0)
5103 var_start = skipwhite(arg + 1); // skip over the "["
5104 else
5105 var_start = arg;
5106 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5107 {
5108 char_u *var_end = skip_var_one(var_start, FALSE);
5109 size_t varlen;
5110 int new_local = FALSE;
5111 int opt_type;
5112 int opt_flags = 0;
5113 assign_dest_T dest = dest_local;
5114 int vimvaridx = -1;
5115 lvar_T *lvar = NULL;
5116 lvar_T arg_lvar;
5117 int has_type = FALSE;
5118 int has_index = FALSE;
5119 int instr_count = -1;
5120
5121 p = (*var_start == '&' || *var_start == '$'
5122 || *var_start == '@') ? var_start + 1 : var_start;
5123 p = to_name_end(p, TRUE);
5124
5125 // "a: type" is declaring variable "a" with a type, not "a:".
5126 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5127 --var_end;
5128 if (is_decl && p == var_start + 2 && p[-1] == ':')
5129 --p;
5130
5131 varlen = p - var_start;
5132 vim_free(name);
5133 name = vim_strnsave(var_start, varlen);
5134 if (name == NULL)
5135 return NULL;
5136 if (!heredoc)
5137 type = &t_any;
5138
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005139 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005140 {
5141 if (*var_start == '&')
5142 {
5143 int cc;
5144 long numval;
5145
5146 dest = dest_option;
5147 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005149 emsg(_(e_const_option));
5150 goto theend;
5151 }
5152 if (is_decl)
5153 {
5154 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5155 goto theend;
5156 }
5157 p = var_start;
5158 p = find_option_end(&p, &opt_flags);
5159 if (p == NULL)
5160 {
5161 // cannot happen?
5162 emsg(_(e_letunexp));
5163 goto theend;
5164 }
5165 cc = *p;
5166 *p = NUL;
5167 opt_type = get_option_value(var_start + 1, &numval,
5168 NULL, opt_flags);
5169 *p = cc;
5170 if (opt_type == -3)
5171 {
5172 semsg(_(e_unknown_option), var_start);
5173 goto theend;
5174 }
5175 if (opt_type == -2 || opt_type == 0)
5176 type = &t_string;
5177 else
5178 type = &t_number; // both number and boolean option
5179 }
5180 else if (*var_start == '$')
5181 {
5182 dest = dest_env;
5183 type = &t_string;
5184 if (is_decl)
5185 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005186 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005187 goto theend;
5188 }
5189 }
5190 else if (*var_start == '@')
5191 {
5192 if (!valid_yank_reg(var_start[1], TRUE))
5193 {
5194 emsg_invreg(var_start[1]);
5195 goto theend;
5196 }
5197 dest = dest_reg;
5198 type = &t_string;
5199 if (is_decl)
5200 {
5201 semsg(_("E1066: Cannot declare a register: %s"), name);
5202 goto theend;
5203 }
5204 }
5205 else if (STRNCMP(var_start, "g:", 2) == 0)
5206 {
5207 dest = dest_global;
5208 if (is_decl)
5209 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005210 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005211 goto theend;
5212 }
5213 }
5214 else if (STRNCMP(var_start, "b:", 2) == 0)
5215 {
5216 dest = dest_buffer;
5217 if (is_decl)
5218 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005219 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005220 goto theend;
5221 }
5222 }
5223 else if (STRNCMP(var_start, "w:", 2) == 0)
5224 {
5225 dest = dest_window;
5226 if (is_decl)
5227 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005228 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005229 goto theend;
5230 }
5231 }
5232 else if (STRNCMP(var_start, "t:", 2) == 0)
5233 {
5234 dest = dest_tab;
5235 if (is_decl)
5236 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005237 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005238 goto theend;
5239 }
5240 }
5241 else if (STRNCMP(var_start, "v:", 2) == 0)
5242 {
5243 typval_T *vtv;
5244 int di_flags;
5245
5246 vimvaridx = find_vim_var(name + 2, &di_flags);
5247 if (vimvaridx < 0)
5248 {
5249 semsg(_(e_var_notfound), var_start);
5250 goto theend;
5251 }
5252 // We use the current value of "sandbox" here, is that OK?
5253 if (var_check_ro(di_flags, name, FALSE))
5254 goto theend;
5255 dest = dest_vimvar;
5256 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005257 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005258 if (is_decl)
5259 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005260 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005261 goto theend;
5262 }
5263 }
5264 else
5265 {
5266 int idx;
5267
5268 for (idx = 0; reserved[idx] != NULL; ++idx)
5269 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005270 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005271 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005272 goto theend;
5273 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005274
5275 lvar = lookup_local(var_start, varlen, cctx);
5276 if (lvar == NULL)
5277 {
5278 CLEAR_FIELD(arg_lvar);
5279 if (lookup_arg(var_start, varlen,
5280 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5281 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005282 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005283 if (is_decl)
5284 {
5285 semsg(_(e_used_as_arg), name);
5286 goto theend;
5287 }
5288 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005289 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005290 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005291 if (lvar != NULL)
5292 {
5293 if (is_decl)
5294 {
5295 semsg(_("E1017: Variable already declared: %s"), name);
5296 goto theend;
5297 }
5298 else if (lvar->lv_const)
5299 {
5300 semsg(_("E1018: Cannot assign to a constant: %s"),
5301 name);
5302 goto theend;
5303 }
5304 }
5305 else if (STRNCMP(var_start, "s:", 2) == 0
5306 || lookup_script(var_start, varlen) == OK
5307 || find_imported(var_start, varlen, cctx) != NULL)
5308 {
5309 dest = dest_script;
5310 if (is_decl)
5311 {
5312 semsg(_("E1054: Variable already declared in the script: %s"),
5313 name);
5314 goto theend;
5315 }
5316 }
5317 else if (name[1] == ':' && name[2] != NUL)
5318 {
5319 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5320 name);
5321 goto theend;
5322 }
5323 else if (!is_decl)
5324 {
5325 semsg(_("E1089: unknown variable: %s"), name);
5326 goto theend;
5327 }
5328 }
5329 }
5330
5331 // handle "a:name" as a name, not index "name" on "a"
5332 if (varlen > 1 || var_start[varlen] != ':')
5333 p = var_end;
5334
5335 if (dest != dest_option)
5336 {
5337 if (is_decl && *p == ':')
5338 {
5339 // parse optional type: "let var: type = expr"
5340 if (!VIM_ISWHITE(p[1]))
5341 {
5342 semsg(_(e_white_after), ":");
5343 goto theend;
5344 }
5345 p = skipwhite(p + 1);
5346 type = parse_type(&p, cctx->ctx_type_list);
5347 has_type = TRUE;
5348 }
5349 else if (lvar != NULL)
5350 type = lvar->lv_type;
5351 }
5352
5353 if (oplen == 3 && !heredoc && dest != dest_global
5354 && type->tt_type != VAR_STRING
5355 && type->tt_type != VAR_ANY)
5356 {
5357 emsg(_("E1019: Can only concatenate to string"));
5358 goto theend;
5359 }
5360
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005361 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005362 {
5363 if (oplen > 1 && !heredoc)
5364 {
5365 // +=, /=, etc. require an existing variable
5366 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5367 name);
5368 goto theend;
5369 }
5370
5371 // new local variable
5372 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5373 goto theend;
5374 lvar = reserve_local(cctx, var_start, varlen,
5375 cmdidx == CMD_const, type);
5376 if (lvar == NULL)
5377 goto theend;
5378 new_local = TRUE;
5379 }
5380
5381 member_type = type;
5382 if (var_end > var_start + varlen)
5383 {
5384 // Something follows after the variable: "var[idx]".
5385 if (is_decl)
5386 {
5387 emsg(_("E1087: cannot use an index when declaring a variable"));
5388 goto theend;
5389 }
5390
5391 if (var_start[varlen] == '[')
5392 {
5393 has_index = TRUE;
5394 if (type->tt_member == NULL)
5395 {
5396 semsg(_("E1088: cannot use an index on %s"), name);
5397 goto theend;
5398 }
5399 member_type = type->tt_member;
5400 }
5401 else
5402 {
5403 semsg("Not supported yet: %s", var_start);
5404 goto theend;
5405 }
5406 }
5407 else if (lvar == &arg_lvar)
5408 {
5409 semsg(_("E1090: Cannot assign to argument %s"), name);
5410 goto theend;
5411 }
5412
5413 if (!heredoc)
5414 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005415 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005416 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005417 if (oplen > 0 && var_count == 0)
5418 {
5419 // skip over the "=" and the expression
5420 p = skipwhite(op + oplen);
5421 compile_expr0(&p, cctx);
5422 }
5423 }
5424 else if (oplen > 0)
5425 {
5426 type_T *stacktype;
5427
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005428 // For "var = expr" evaluate the expression.
5429 if (var_count == 0)
5430 {
5431 int r;
5432
5433 // for "+=", "*=", "..=" etc. first load the current value
5434 if (*op != '=')
5435 {
5436 generate_loadvar(cctx, dest, name, lvar, type);
5437
5438 if (has_index)
5439 {
5440 // TODO: get member from list or dict
5441 emsg("Index with operation not supported yet");
5442 goto theend;
5443 }
5444 }
5445
5446 // Compile the expression. Temporarily hide the new local
5447 // variable here, it is not available to this expression.
5448 if (new_local)
5449 --cctx->ctx_locals.ga_len;
5450 instr_count = instr->ga_len;
5451 p = skipwhite(op + oplen);
5452 r = compile_expr0(&p, cctx);
5453 if (new_local)
5454 ++cctx->ctx_locals.ga_len;
5455 if (r == FAIL)
5456 goto theend;
5457 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005458 else if (semicolon && var_idx == var_count - 1)
5459 {
5460 // For "[var; var] = expr" get the rest of the list
5461 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5462 goto theend;
5463 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005464 else
5465 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005466 // For "[var, var] = expr" get the "var_idx" item from the
5467 // list.
5468 if (generate_GETITEM(cctx, var_idx) == FAIL)
5469 return FAIL;
5470 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005471
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005472 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005473 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005474 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005475 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005476 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005477 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005478 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005479 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005480 emsg(_(e_cannot_use_void));
5481 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005482 }
5483 else
5484 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005485 // An empty list or dict has a &t_void member,
5486 // for a variable that implies &t_any.
5487 if (stacktype == &t_list_empty)
5488 lvar->lv_type = &t_list_any;
5489 else if (stacktype == &t_dict_empty)
5490 lvar->lv_type = &t_dict_any;
5491 else
5492 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005493 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005494 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005495 else
5496 {
5497 type_T *use_type = lvar->lv_type;
5498
5499 if (has_index)
5500 {
5501 use_type = use_type->tt_member;
5502 if (use_type == NULL)
5503 use_type = &t_void;
5504 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005505 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005506 == FAIL)
5507 goto theend;
5508 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005509 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005510 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005511 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005512 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005514 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005516 emsg(_(e_const_req_value));
5517 goto theend;
5518 }
5519 else if (!has_type || dest == dest_option)
5520 {
5521 emsg(_(e_type_req));
5522 goto theend;
5523 }
5524 else
5525 {
5526 // variables are always initialized
5527 if (ga_grow(instr, 1) == FAIL)
5528 goto theend;
5529 switch (member_type->tt_type)
5530 {
5531 case VAR_BOOL:
5532 generate_PUSHBOOL(cctx, VVAL_FALSE);
5533 break;
5534 case VAR_FLOAT:
5535#ifdef FEAT_FLOAT
5536 generate_PUSHF(cctx, 0.0);
5537#endif
5538 break;
5539 case VAR_STRING:
5540 generate_PUSHS(cctx, NULL);
5541 break;
5542 case VAR_BLOB:
5543 generate_PUSHBLOB(cctx, NULL);
5544 break;
5545 case VAR_FUNC:
5546 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5547 break;
5548 case VAR_LIST:
5549 generate_NEWLIST(cctx, 0);
5550 break;
5551 case VAR_DICT:
5552 generate_NEWDICT(cctx, 0);
5553 break;
5554 case VAR_JOB:
5555 generate_PUSHJOB(cctx, NULL);
5556 break;
5557 case VAR_CHANNEL:
5558 generate_PUSHCHANNEL(cctx, NULL);
5559 break;
5560 case VAR_NUMBER:
5561 case VAR_UNKNOWN:
5562 case VAR_ANY:
5563 case VAR_PARTIAL:
5564 case VAR_VOID:
5565 case VAR_SPECIAL: // cannot happen
5566 generate_PUSHNR(cctx, 0);
5567 break;
5568 }
5569 }
5570 if (var_count == 0)
5571 end = p;
5572 }
5573
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005574 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005575 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005576 break;
5577
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005578 if (oplen > 0 && *op != '=')
5579 {
5580 type_T *expected = &t_number;
5581 type_T *stacktype;
5582
5583 // TODO: if type is known use float or any operation
5584
5585 if (*op == '.')
5586 expected = &t_string;
5587 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005588 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005589 goto theend;
5590
5591 if (*op == '.')
5592 generate_instr_drop(cctx, ISN_CONCAT, 1);
5593 else
5594 {
5595 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5596
5597 if (isn == NULL)
5598 goto theend;
5599 switch (*op)
5600 {
5601 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5602 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5603 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5604 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5605 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607 }
5608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005610 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005611 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005612 int r;
5613
5614 // Compile the "idx" in "var[idx]".
5615 if (new_local)
5616 --cctx->ctx_locals.ga_len;
5617 p = skipwhite(var_start + varlen + 1);
5618 r = compile_expr0(&p, cctx);
5619 if (new_local)
5620 ++cctx->ctx_locals.ga_len;
5621 if (r == FAIL)
5622 goto theend;
5623 if (*skipwhite(p) != ']')
5624 {
5625 emsg(_(e_missbrac));
5626 goto theend;
5627 }
5628 if (type->tt_type == VAR_DICT
5629 && may_generate_2STRING(-1, cctx) == FAIL)
5630 goto theend;
5631 if (type->tt_type == VAR_LIST
5632 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005633 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005634 {
5635 emsg(_(e_number_exp));
5636 goto theend;
5637 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005638
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005639 // Load the dict or list. On the stack we then have:
5640 // - value
5641 // - index
5642 // - variable
5643 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005644
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005645 if (type->tt_type == VAR_LIST)
5646 {
5647 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5648 return FAIL;
5649 }
5650 else if (type->tt_type == VAR_DICT)
5651 {
5652 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5653 return FAIL;
5654 }
5655 else
5656 {
5657 emsg(_(e_listreq));
5658 goto theend;
5659 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005660 }
5661 else
5662 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005663 switch (dest)
5664 {
5665 case dest_option:
5666 generate_STOREOPT(cctx, name + 1, opt_flags);
5667 break;
5668 case dest_global:
5669 // include g: with the name, easier to execute that way
5670 generate_STORE(cctx, ISN_STOREG, 0, name);
5671 break;
5672 case dest_buffer:
5673 // include b: with the name, easier to execute that way
5674 generate_STORE(cctx, ISN_STOREB, 0, name);
5675 break;
5676 case dest_window:
5677 // include w: with the name, easier to execute that way
5678 generate_STORE(cctx, ISN_STOREW, 0, name);
5679 break;
5680 case dest_tab:
5681 // include t: with the name, easier to execute that way
5682 generate_STORE(cctx, ISN_STORET, 0, name);
5683 break;
5684 case dest_env:
5685 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5686 break;
5687 case dest_reg:
5688 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5689 break;
5690 case dest_vimvar:
5691 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5692 break;
5693 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005694 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005695 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5696 imported_T *import = NULL;
5697 int sid = current_sctx.sc_sid;
5698 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005699
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005700 if (name[1] != ':')
5701 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005702 import = find_imported(name, 0, cctx);
5703 if (import != NULL)
5704 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005705 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005706
5707 idx = get_script_item_idx(sid, rawname, TRUE);
5708 // TODO: specific type
5709 if (idx < 0)
5710 {
5711 char_u *name_s = name;
5712
5713 // Include s: in the name for store_var()
5714 if (name[1] != ':')
5715 {
5716 int len = (int)STRLEN(name) + 3;
5717
5718 name_s = alloc(len);
5719 if (name_s == NULL)
5720 name_s = name;
5721 else
5722 vim_snprintf((char *)name_s, len,
5723 "s:%s", name);
5724 }
5725 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005726 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005727 if (name_s != name)
5728 vim_free(name_s);
5729 }
5730 else
5731 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005732 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005733 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005734 break;
5735 case dest_local:
5736 if (lvar != NULL)
5737 {
5738 isn_T *isn = ((isn_T *)instr->ga_data)
5739 + instr->ga_len - 1;
5740
5741 // optimization: turn "var = 123" from ISN_PUSHNR +
5742 // ISN_STORE into ISN_STORENR
5743 if (!lvar->lv_from_outer
5744 && instr->ga_len == instr_count + 1
5745 && isn->isn_type == ISN_PUSHNR)
5746 {
5747 varnumber_T val = isn->isn_arg.number;
5748
5749 isn->isn_type = ISN_STORENR;
5750 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5751 isn->isn_arg.storenr.stnr_val = val;
5752 if (stack->ga_len > 0)
5753 --stack->ga_len;
5754 }
5755 else if (lvar->lv_from_outer)
5756 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005757 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005758 else
5759 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5760 }
5761 break;
5762 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005763 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005764
5765 if (var_idx + 1 < var_count)
5766 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005767 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005768
5769 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005770 if (var_count > 0 && !semicolon)
5771 {
5772 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5773 goto theend;
5774 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005775
Bram Moolenaarb2097502020-07-19 17:17:02 +02005776 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777
5778theend:
5779 vim_free(name);
5780 return ret;
5781}
5782
5783/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005784 * Check if "name" can be "unlet".
5785 */
5786 int
5787check_vim9_unlet(char_u *name)
5788{
5789 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5790 {
5791 semsg(_("E1081: Cannot unlet %s"), name);
5792 return FAIL;
5793 }
5794 return OK;
5795}
5796
5797/*
5798 * Callback passed to ex_unletlock().
5799 */
5800 static int
5801compile_unlet(
5802 lval_T *lvp,
5803 char_u *name_end,
5804 exarg_T *eap,
5805 int deep UNUSED,
5806 void *coookie)
5807{
5808 cctx_T *cctx = coookie;
5809
5810 if (lvp->ll_tv == NULL)
5811 {
5812 char_u *p = lvp->ll_name;
5813 int cc = *name_end;
5814 int ret = OK;
5815
5816 // Normal name. Only supports g:, w:, t: and b: namespaces.
5817 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005818 if (*p == '$')
5819 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5820 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005821 ret = FAIL;
5822 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005823 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005824
5825 *name_end = cc;
5826 return ret;
5827 }
5828
5829 // TODO: unlet {list}[idx]
5830 // TODO: unlet {dict}[key]
5831 emsg("Sorry, :unlet not fully implemented yet");
5832 return FAIL;
5833}
5834
5835/*
5836 * compile "unlet var", "lock var" and "unlock var"
5837 * "arg" points to "var".
5838 */
5839 static char_u *
5840compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5841{
5842 char_u *p = arg;
5843
5844 if (eap->cmdidx != CMD_unlet)
5845 {
5846 emsg("Sorry, :lock and unlock not implemented yet");
5847 return NULL;
5848 }
5849
5850 if (*p == '!')
5851 {
5852 p = skipwhite(p + 1);
5853 eap->forceit = TRUE;
5854 }
5855
5856 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5857 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5858}
5859
5860/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861 * Compile an :import command.
5862 */
5863 static char_u *
5864compile_import(char_u *arg, cctx_T *cctx)
5865{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005866 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005867}
5868
5869/*
5870 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5871 */
5872 static int
5873compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5874{
5875 garray_T *instr = &cctx->ctx_instr;
5876 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5877
5878 if (endlabel == NULL)
5879 return FAIL;
5880 endlabel->el_next = *el;
5881 *el = endlabel;
5882 endlabel->el_end_label = instr->ga_len;
5883
5884 generate_JUMP(cctx, when, 0);
5885 return OK;
5886}
5887
5888 static void
5889compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5890{
5891 garray_T *instr = &cctx->ctx_instr;
5892
5893 while (*el != NULL)
5894 {
5895 endlabel_T *cur = (*el);
5896 isn_T *isn;
5897
5898 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5899 isn->isn_arg.jump.jump_where = instr->ga_len;
5900 *el = cur->el_next;
5901 vim_free(cur);
5902 }
5903}
5904
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005905 static void
5906compile_free_jump_to_end(endlabel_T **el)
5907{
5908 while (*el != NULL)
5909 {
5910 endlabel_T *cur = (*el);
5911
5912 *el = cur->el_next;
5913 vim_free(cur);
5914 }
5915}
5916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005917/*
5918 * Create a new scope and set up the generic items.
5919 */
5920 static scope_T *
5921new_scope(cctx_T *cctx, scopetype_T type)
5922{
5923 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5924
5925 if (scope == NULL)
5926 return NULL;
5927 scope->se_outer = cctx->ctx_scope;
5928 cctx->ctx_scope = scope;
5929 scope->se_type = type;
5930 scope->se_local_count = cctx->ctx_locals.ga_len;
5931 return scope;
5932}
5933
5934/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005935 * Free the current scope and go back to the outer scope.
5936 */
5937 static void
5938drop_scope(cctx_T *cctx)
5939{
5940 scope_T *scope = cctx->ctx_scope;
5941
5942 if (scope == NULL)
5943 {
5944 iemsg("calling drop_scope() without a scope");
5945 return;
5946 }
5947 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005948 switch (scope->se_type)
5949 {
5950 case IF_SCOPE:
5951 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5952 case FOR_SCOPE:
5953 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5954 case WHILE_SCOPE:
5955 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5956 case TRY_SCOPE:
5957 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5958 case NO_SCOPE:
5959 case BLOCK_SCOPE:
5960 break;
5961 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005962 vim_free(scope);
5963}
5964
5965/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005966 * compile "if expr"
5967 *
5968 * "if expr" Produces instructions:
5969 * EVAL expr Push result of "expr"
5970 * JUMP_IF_FALSE end
5971 * ... body ...
5972 * end:
5973 *
5974 * "if expr | else" Produces instructions:
5975 * EVAL expr Push result of "expr"
5976 * JUMP_IF_FALSE else
5977 * ... body ...
5978 * JUMP_ALWAYS end
5979 * else:
5980 * ... body ...
5981 * end:
5982 *
5983 * "if expr1 | elseif expr2 | else" Produces instructions:
5984 * EVAL expr Push result of "expr"
5985 * JUMP_IF_FALSE elseif
5986 * ... body ...
5987 * JUMP_ALWAYS end
5988 * elseif:
5989 * EVAL expr Push result of "expr"
5990 * JUMP_IF_FALSE else
5991 * ... body ...
5992 * JUMP_ALWAYS end
5993 * else:
5994 * ... body ...
5995 * end:
5996 */
5997 static char_u *
5998compile_if(char_u *arg, cctx_T *cctx)
5999{
6000 char_u *p = arg;
6001 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006002 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006004 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006005 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006006
Bram Moolenaara5565e42020-05-09 15:44:01 +02006007 CLEAR_FIELD(ppconst);
6008 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006009 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006010 clear_ppconst(&ppconst);
6011 return NULL;
6012 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006013 if (cctx->ctx_skip == SKIP_YES)
6014 clear_ppconst(&ppconst);
6015 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006016 {
6017 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006018 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006019 clear_ppconst(&ppconst);
6020 }
6021 else
6022 {
6023 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006024 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006025 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006026 return NULL;
6027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006028
6029 scope = new_scope(cctx, IF_SCOPE);
6030 if (scope == NULL)
6031 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006032 scope->se_skip_save = skip_save;
6033 // "is_had_return" will be reset if any block does not end in :return
6034 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006036 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006037 {
6038 // "where" is set when ":elseif", "else" or ":endif" is found
6039 scope->se_u.se_if.is_if_label = instr->ga_len;
6040 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6041 }
6042 else
6043 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006044
6045 return p;
6046}
6047
6048 static char_u *
6049compile_elseif(char_u *arg, cctx_T *cctx)
6050{
6051 char_u *p = arg;
6052 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006053 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006054 isn_T *isn;
6055 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006056 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006057
6058 if (scope == NULL || scope->se_type != IF_SCOPE)
6059 {
6060 emsg(_(e_elseif_without_if));
6061 return NULL;
6062 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006063 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006064 if (!cctx->ctx_had_return)
6065 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006067 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006068 {
6069 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006070 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006071 return NULL;
6072 // previous "if" or "elseif" jumps here
6073 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6074 isn->isn_arg.jump.jump_where = instr->ga_len;
6075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006077 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006078 CLEAR_FIELD(ppconst);
6079 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006080 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006081 clear_ppconst(&ppconst);
6082 return NULL;
6083 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006084 if (scope->se_skip_save == SKIP_YES)
6085 clear_ppconst(&ppconst);
6086 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006087 {
6088 // The expression results in a constant.
6089 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006090 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006091 clear_ppconst(&ppconst);
6092 scope->se_u.se_if.is_if_label = -1;
6093 }
6094 else
6095 {
6096 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006097 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006098 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006099 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006100
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006101 // "where" is set when ":elseif", "else" or ":endif" is found
6102 scope->se_u.se_if.is_if_label = instr->ga_len;
6103 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006105
6106 return p;
6107}
6108
6109 static char_u *
6110compile_else(char_u *arg, cctx_T *cctx)
6111{
6112 char_u *p = arg;
6113 garray_T *instr = &cctx->ctx_instr;
6114 isn_T *isn;
6115 scope_T *scope = cctx->ctx_scope;
6116
6117 if (scope == NULL || scope->se_type != IF_SCOPE)
6118 {
6119 emsg(_(e_else_without_if));
6120 return NULL;
6121 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006122 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006123 if (!cctx->ctx_had_return)
6124 scope->se_u.se_if.is_had_return = FALSE;
6125 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006126
Bram Moolenaarefd88552020-06-18 20:50:10 +02006127 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006128 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006129 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006130 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006131 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006132 if (!cctx->ctx_had_return
6133 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6134 JUMP_ALWAYS, cctx) == FAIL)
6135 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006136 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006137
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006138 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006139 {
6140 if (scope->se_u.se_if.is_if_label >= 0)
6141 {
6142 // previous "if" or "elseif" jumps here
6143 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6144 isn->isn_arg.jump.jump_where = instr->ga_len;
6145 scope->se_u.se_if.is_if_label = -1;
6146 }
6147 }
6148
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006149 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006150 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006152
6153 return p;
6154}
6155
6156 static char_u *
6157compile_endif(char_u *arg, cctx_T *cctx)
6158{
6159 scope_T *scope = cctx->ctx_scope;
6160 ifscope_T *ifscope;
6161 garray_T *instr = &cctx->ctx_instr;
6162 isn_T *isn;
6163
6164 if (scope == NULL || scope->se_type != IF_SCOPE)
6165 {
6166 emsg(_(e_endif_without_if));
6167 return NULL;
6168 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006169 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006170 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006171 if (!cctx->ctx_had_return)
6172 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006173
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006174 if (scope->se_u.se_if.is_if_label >= 0)
6175 {
6176 // previous "if" or "elseif" jumps here
6177 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6178 isn->isn_arg.jump.jump_where = instr->ga_len;
6179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006180 // Fill in the "end" label in jumps at the end of the blocks.
6181 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006182 cctx->ctx_skip = scope->se_skip_save;
6183
6184 // If all the blocks end in :return and there is an :else then the
6185 // had_return flag is set.
6186 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006188 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006189 return arg;
6190}
6191
6192/*
6193 * compile "for var in expr"
6194 *
6195 * Produces instructions:
6196 * PUSHNR -1
6197 * STORE loop-idx Set index to -1
6198 * EVAL expr Push result of "expr"
6199 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6200 * - if beyond end, jump to "end"
6201 * - otherwise get item from list and push it
6202 * STORE var Store item in "var"
6203 * ... body ...
6204 * JUMP top Jump back to repeat
6205 * end: DROP Drop the result of "expr"
6206 *
6207 */
6208 static char_u *
6209compile_for(char_u *arg, cctx_T *cctx)
6210{
6211 char_u *p;
6212 size_t varlen;
6213 garray_T *instr = &cctx->ctx_instr;
6214 garray_T *stack = &cctx->ctx_type_stack;
6215 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006216 lvar_T *loop_lvar; // loop iteration variable
6217 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218 type_T *vartype;
6219
6220 // TODO: list of variables: "for [key, value] in dict"
6221 // parse "var"
6222 for (p = arg; eval_isnamec1(*p); ++p)
6223 ;
6224 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006225 var_lvar = lookup_local(arg, varlen, cctx);
6226 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006227 {
6228 semsg(_("E1023: variable already defined: %s"), arg);
6229 return NULL;
6230 }
6231
6232 // consume "in"
6233 p = skipwhite(p);
6234 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6235 {
6236 emsg(_(e_missing_in));
6237 return NULL;
6238 }
6239 p = skipwhite(p + 2);
6240
6241
6242 scope = new_scope(cctx, FOR_SCOPE);
6243 if (scope == NULL)
6244 return NULL;
6245
6246 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006247 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6248 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006249 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006250 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006251 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006252 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254
6255 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006256 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6257 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006258 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006259 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006260 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006262 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006264 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265
6266 // compile "expr", it remains on the stack until "endfor"
6267 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006268 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006269 {
6270 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006273
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006274 // Now that we know the type of "var", check that it is a list, now or at
6275 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006276 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006277 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006279 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006280 return NULL;
6281 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006282 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006283 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284
6285 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006286 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006288 generate_FOR(cctx, loop_lvar->lv_idx);
6289 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290
6291 return arg;
6292}
6293
6294/*
6295 * compile "endfor"
6296 */
6297 static char_u *
6298compile_endfor(char_u *arg, cctx_T *cctx)
6299{
6300 garray_T *instr = &cctx->ctx_instr;
6301 scope_T *scope = cctx->ctx_scope;
6302 forscope_T *forscope;
6303 isn_T *isn;
6304
6305 if (scope == NULL || scope->se_type != FOR_SCOPE)
6306 {
6307 emsg(_(e_for));
6308 return NULL;
6309 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006310 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006312 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313
6314 // At end of ":for" scope jump back to the FOR instruction.
6315 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6316
6317 // Fill in the "end" label in the FOR statement so it can jump here
6318 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6319 isn->isn_arg.forloop.for_end = instr->ga_len;
6320
6321 // Fill in the "end" label any BREAK statements
6322 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6323
6324 // Below the ":for" scope drop the "expr" list from the stack.
6325 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6326 return NULL;
6327
6328 vim_free(scope);
6329
6330 return arg;
6331}
6332
6333/*
6334 * compile "while expr"
6335 *
6336 * Produces instructions:
6337 * top: EVAL expr Push result of "expr"
6338 * JUMP_IF_FALSE end jump if false
6339 * ... body ...
6340 * JUMP top Jump back to repeat
6341 * end:
6342 *
6343 */
6344 static char_u *
6345compile_while(char_u *arg, cctx_T *cctx)
6346{
6347 char_u *p = arg;
6348 garray_T *instr = &cctx->ctx_instr;
6349 scope_T *scope;
6350
6351 scope = new_scope(cctx, WHILE_SCOPE);
6352 if (scope == NULL)
6353 return NULL;
6354
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006355 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356
6357 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006358 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006359 return NULL;
6360
6361 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006362 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006363 JUMP_IF_FALSE, cctx) == FAIL)
6364 return FAIL;
6365
6366 return p;
6367}
6368
6369/*
6370 * compile "endwhile"
6371 */
6372 static char_u *
6373compile_endwhile(char_u *arg, cctx_T *cctx)
6374{
6375 scope_T *scope = cctx->ctx_scope;
6376
6377 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6378 {
6379 emsg(_(e_while));
6380 return NULL;
6381 }
6382 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006383 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006384
6385 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006386 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387
6388 // Fill in the "end" label in the WHILE statement so it can jump here.
6389 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006390 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006391
6392 vim_free(scope);
6393
6394 return arg;
6395}
6396
6397/*
6398 * compile "continue"
6399 */
6400 static char_u *
6401compile_continue(char_u *arg, cctx_T *cctx)
6402{
6403 scope_T *scope = cctx->ctx_scope;
6404
6405 for (;;)
6406 {
6407 if (scope == NULL)
6408 {
6409 emsg(_(e_continue));
6410 return NULL;
6411 }
6412 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6413 break;
6414 scope = scope->se_outer;
6415 }
6416
6417 // Jump back to the FOR or WHILE instruction.
6418 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006419 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6420 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006421 return arg;
6422}
6423
6424/*
6425 * compile "break"
6426 */
6427 static char_u *
6428compile_break(char_u *arg, cctx_T *cctx)
6429{
6430 scope_T *scope = cctx->ctx_scope;
6431 endlabel_T **el;
6432
6433 for (;;)
6434 {
6435 if (scope == NULL)
6436 {
6437 emsg(_(e_break));
6438 return NULL;
6439 }
6440 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6441 break;
6442 scope = scope->se_outer;
6443 }
6444
6445 // Jump to the end of the FOR or WHILE loop.
6446 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006447 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006448 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006449 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006450 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6451 return FAIL;
6452
6453 return arg;
6454}
6455
6456/*
6457 * compile "{" start of block
6458 */
6459 static char_u *
6460compile_block(char_u *arg, cctx_T *cctx)
6461{
6462 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6463 return NULL;
6464 return skipwhite(arg + 1);
6465}
6466
6467/*
6468 * compile end of block: drop one scope
6469 */
6470 static void
6471compile_endblock(cctx_T *cctx)
6472{
6473 scope_T *scope = cctx->ctx_scope;
6474
6475 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006476 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477 vim_free(scope);
6478}
6479
6480/*
6481 * compile "try"
6482 * Creates a new scope for the try-endtry, pointing to the first catch and
6483 * finally.
6484 * Creates another scope for the "try" block itself.
6485 * TRY instruction sets up exception handling at runtime.
6486 *
6487 * "try"
6488 * TRY -> catch1, -> finally push trystack entry
6489 * ... try block
6490 * "throw {exception}"
6491 * EVAL {exception}
6492 * THROW create exception
6493 * ... try block
6494 * " catch {expr}"
6495 * JUMP -> finally
6496 * catch1: PUSH exeception
6497 * EVAL {expr}
6498 * MATCH
6499 * JUMP nomatch -> catch2
6500 * CATCH remove exception
6501 * ... catch block
6502 * " catch"
6503 * JUMP -> finally
6504 * catch2: CATCH remove exception
6505 * ... catch block
6506 * " finally"
6507 * finally:
6508 * ... finally block
6509 * " endtry"
6510 * ENDTRY pop trystack entry, may rethrow
6511 */
6512 static char_u *
6513compile_try(char_u *arg, cctx_T *cctx)
6514{
6515 garray_T *instr = &cctx->ctx_instr;
6516 scope_T *try_scope;
6517 scope_T *scope;
6518
6519 // scope that holds the jumps that go to catch/finally/endtry
6520 try_scope = new_scope(cctx, TRY_SCOPE);
6521 if (try_scope == NULL)
6522 return NULL;
6523
6524 // "catch" is set when the first ":catch" is found.
6525 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006526 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527 if (generate_instr(cctx, ISN_TRY) == NULL)
6528 return NULL;
6529
6530 // scope for the try block itself
6531 scope = new_scope(cctx, BLOCK_SCOPE);
6532 if (scope == NULL)
6533 return NULL;
6534
6535 return arg;
6536}
6537
6538/*
6539 * compile "catch {expr}"
6540 */
6541 static char_u *
6542compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6543{
6544 scope_T *scope = cctx->ctx_scope;
6545 garray_T *instr = &cctx->ctx_instr;
6546 char_u *p;
6547 isn_T *isn;
6548
6549 // end block scope from :try or :catch
6550 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6551 compile_endblock(cctx);
6552 scope = cctx->ctx_scope;
6553
6554 // Error if not in a :try scope
6555 if (scope == NULL || scope->se_type != TRY_SCOPE)
6556 {
6557 emsg(_(e_catch));
6558 return NULL;
6559 }
6560
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006561 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 {
6563 emsg(_("E1033: catch unreachable after catch-all"));
6564 return NULL;
6565 }
6566
6567 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006568 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006569 JUMP_ALWAYS, cctx) == FAIL)
6570 return NULL;
6571
6572 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006573 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006574 if (isn->isn_arg.try.try_catch == 0)
6575 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006576 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006577 {
6578 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006579 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 isn->isn_arg.jump.jump_where = instr->ga_len;
6581 }
6582
6583 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006584 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006585 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006586 scope->se_u.se_try.ts_caught_all = TRUE;
6587 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588 }
6589 else
6590 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006591 char_u *end;
6592 char_u *pat;
6593 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006594 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006595 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 // Push v:exception, push {expr} and MATCH
6598 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6599
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006600 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006601 if (*end != *p)
6602 {
6603 semsg(_("E1067: Separator mismatch: %s"), p);
6604 vim_free(tofree);
6605 return FAIL;
6606 }
6607 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006608 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006609 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006610 len = (int)(end - tofree);
6611 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006612 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006613 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006614 if (pat == NULL)
6615 return FAIL;
6616 if (generate_PUSHS(cctx, pat) == FAIL)
6617 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6620 return NULL;
6621
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006622 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006623 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6624 return NULL;
6625 }
6626
6627 if (generate_instr(cctx, ISN_CATCH) == NULL)
6628 return NULL;
6629
6630 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6631 return NULL;
6632 return p;
6633}
6634
6635 static char_u *
6636compile_finally(char_u *arg, cctx_T *cctx)
6637{
6638 scope_T *scope = cctx->ctx_scope;
6639 garray_T *instr = &cctx->ctx_instr;
6640 isn_T *isn;
6641
6642 // end block scope from :try or :catch
6643 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6644 compile_endblock(cctx);
6645 scope = cctx->ctx_scope;
6646
6647 // Error if not in a :try scope
6648 if (scope == NULL || scope->se_type != TRY_SCOPE)
6649 {
6650 emsg(_(e_finally));
6651 return NULL;
6652 }
6653
6654 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006655 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 if (isn->isn_arg.try.try_finally != 0)
6657 {
6658 emsg(_(e_finally_dup));
6659 return NULL;
6660 }
6661
6662 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006663 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664
Bram Moolenaar585fea72020-04-02 22:33:21 +02006665 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006666 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006667 {
6668 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006669 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006670 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006671 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006672 }
6673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 // TODO: set index in ts_finally_label jumps
6675
6676 return arg;
6677}
6678
6679 static char_u *
6680compile_endtry(char_u *arg, cctx_T *cctx)
6681{
6682 scope_T *scope = cctx->ctx_scope;
6683 garray_T *instr = &cctx->ctx_instr;
6684 isn_T *isn;
6685
6686 // end block scope from :catch or :finally
6687 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6688 compile_endblock(cctx);
6689 scope = cctx->ctx_scope;
6690
6691 // Error if not in a :try scope
6692 if (scope == NULL || scope->se_type != TRY_SCOPE)
6693 {
6694 if (scope == NULL)
6695 emsg(_(e_no_endtry));
6696 else if (scope->se_type == WHILE_SCOPE)
6697 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006698 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699 emsg(_(e_endfor));
6700 else
6701 emsg(_(e_endif));
6702 return NULL;
6703 }
6704
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006705 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6707 {
6708 emsg(_("E1032: missing :catch or :finally"));
6709 return NULL;
6710 }
6711
6712 // Fill in the "end" label in jumps at the end of the blocks, if not done
6713 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006714 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715
6716 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006717 if (isn->isn_arg.try.try_catch == 0)
6718 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 if (isn->isn_arg.try.try_finally == 0)
6720 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006721
6722 if (scope->se_u.se_try.ts_catch_label != 0)
6723 {
6724 // Last catch without match jumps here
6725 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6726 isn->isn_arg.jump.jump_where = instr->ga_len;
6727 }
6728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729 compile_endblock(cctx);
6730
6731 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6732 return NULL;
6733 return arg;
6734}
6735
6736/*
6737 * compile "throw {expr}"
6738 */
6739 static char_u *
6740compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6741{
6742 char_u *p = skipwhite(arg);
6743
Bram Moolenaara5565e42020-05-09 15:44:01 +02006744 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 return NULL;
6746 if (may_generate_2STRING(-1, cctx) == FAIL)
6747 return NULL;
6748 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6749 return NULL;
6750
6751 return p;
6752}
6753
6754/*
6755 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006756 * compile "echomsg expr"
6757 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006758 * compile "execute expr"
6759 */
6760 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006761compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006762{
6763 char_u *p = arg;
6764 int count = 0;
6765
6766 for (;;)
6767 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006768 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006769 return NULL;
6770 ++count;
6771 p = skipwhite(p);
6772 if (ends_excmd(*p))
6773 break;
6774 }
6775
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006776 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6777 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6778 else if (cmdidx == CMD_execute)
6779 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6780 else if (cmdidx == CMD_echomsg)
6781 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6782 else
6783 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006784 return p;
6785}
6786
6787/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006788 * A command that is not compiled, execute with legacy code.
6789 */
6790 static char_u *
6791compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6792{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006793 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006794 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006795 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006796
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006797 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006798 goto theend;
6799
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006800 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006801 {
6802 long argt = excmd_get_argt(eap->cmdidx);
6803 int usefilter = FALSE;
6804
6805 has_expr = argt & (EX_XFILE | EX_EXPAND);
6806
6807 // If the command can be followed by a bar, find the bar and truncate
6808 // it, so that the following command can be compiled.
6809 // The '|' is overwritten with a NUL, it is put back below.
6810 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6811 && *eap->arg == '!')
6812 // :w !filter or :r !filter or :r! filter
6813 usefilter = TRUE;
6814 if ((argt & EX_TRLBAR) && !usefilter)
6815 {
6816 separate_nextcmd(eap);
6817 if (eap->nextcmd != NULL)
6818 nextcmd = eap->nextcmd;
6819 }
6820 }
6821
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006822 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6823 {
6824 // expand filename in "syntax include [@group] filename"
6825 has_expr = TRUE;
6826 eap->arg = skipwhite(eap->arg + 7);
6827 if (*eap->arg == '@')
6828 eap->arg = skiptowhite(eap->arg);
6829 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006830
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006831 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006832 {
6833 int count = 0;
6834 char_u *start = skipwhite(line);
6835
6836 // :cmd xxx`=expr1`yyy`=expr2`zzz
6837 // PUSHS ":cmd xxx"
6838 // eval expr1
6839 // PUSHS "yyy"
6840 // eval expr2
6841 // PUSHS "zzz"
6842 // EXECCONCAT 5
6843 for (;;)
6844 {
6845 if (p > start)
6846 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006847 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006848 ++count;
6849 }
6850 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006851 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006852 return NULL;
6853 may_generate_2STRING(-1, cctx);
6854 ++count;
6855 p = skipwhite(p);
6856 if (*p != '`')
6857 {
6858 emsg(_("E1083: missing backtick"));
6859 return NULL;
6860 }
6861 start = p + 1;
6862
6863 p = (char_u *)strstr((char *)start, "`=");
6864 if (p == NULL)
6865 {
6866 if (*skipwhite(start) != NUL)
6867 {
6868 generate_PUSHS(cctx, vim_strsave(start));
6869 ++count;
6870 }
6871 break;
6872 }
6873 }
6874 generate_EXECCONCAT(cctx, count);
6875 }
6876 else
6877 generate_EXEC(cctx, line);
6878
6879theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006880 if (*nextcmd != NUL)
6881 {
6882 // the parser expects a pointer to the bar, put it back
6883 --nextcmd;
6884 *nextcmd = '|';
6885 }
6886
6887 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006888}
6889
6890/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006891 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006892 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006893 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006894 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006895add_def_function(ufunc_T *ufunc)
6896{
6897 dfunc_T *dfunc;
6898
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006899 if (def_functions.ga_len == 0)
6900 {
6901 // The first position is not used, so that a zero uf_dfunc_idx means it
6902 // wasn't set.
6903 if (ga_grow(&def_functions, 1) == FAIL)
6904 return FAIL;
6905 ++def_functions.ga_len;
6906 }
6907
Bram Moolenaar09689a02020-05-09 22:50:08 +02006908 // Add the function to "def_functions".
6909 if (ga_grow(&def_functions, 1) == FAIL)
6910 return FAIL;
6911 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6912 CLEAR_POINTER(dfunc);
6913 dfunc->df_idx = def_functions.ga_len;
6914 ufunc->uf_dfunc_idx = dfunc->df_idx;
6915 dfunc->df_ufunc = ufunc;
6916 ++def_functions.ga_len;
6917 return OK;
6918}
6919
6920/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921 * After ex_function() has collected all the function lines: parse and compile
6922 * the lines into instructions.
6923 * Adds the function to "def_functions".
6924 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6925 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006926 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006927 * This can be used recursively through compile_lambda(), which may reallocate
6928 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006929 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006931 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006932compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 char_u *line = NULL;
6935 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937 cctx_T cctx;
6938 garray_T *instr;
6939 int called_emsg_before = called_emsg;
6940 int ret = FAIL;
6941 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006942 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006943 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006945 // When using a function that was compiled before: Free old instructions.
6946 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006947 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006949 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6950 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006951 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006953 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006954 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955
Bram Moolenaar985116a2020-07-12 17:31:09 +02006956 ufunc->uf_def_status = UF_COMPILING;
6957
Bram Moolenaara80faa82020-04-12 19:37:17 +02006958 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959 cctx.ctx_ufunc = ufunc;
6960 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006961 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006962 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6963 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6964 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6965 cctx.ctx_type_list = &ufunc->uf_type_list;
6966 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6967 instr = &cctx.ctx_instr;
6968
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006969 // Set the context to the function, it may be compiled when called from
6970 // another script. Set the script version to the most modern one.
6971 // The line number will be set in next_line_from_context().
6972 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6974
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006975 // Make sure error messages are OK.
6976 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6977 if (do_estack_push)
6978 estack_push_ufunc(ufunc, 1);
6979
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006980 if (ufunc->uf_def_args.ga_len > 0)
6981 {
6982 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006983 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006984 int i;
6985 char_u *arg;
6986 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6987
6988 // Produce instructions for the default values of optional arguments.
6989 // Store the instruction index in uf_def_arg_idx[] so that we know
6990 // where to start when the function is called, depending on the number
6991 // of arguments.
6992 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6993 if (ufunc->uf_def_arg_idx == NULL)
6994 goto erret;
6995 for (i = 0; i < count; ++i)
6996 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006997 garray_T *stack = &cctx.ctx_type_stack;
6998 type_T *val_type;
6999 int arg_idx = first_def_arg + i;
7000
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007001 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7002 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007003 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007004 goto erret;
7005
7006 // If no type specified use the type of the default value.
7007 // Otherwise check that the default value type matches the
7008 // specified type.
7009 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7010 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
7011 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaare30f64b2020-07-15 19:48:20 +02007012 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007013 == FAIL)
7014 {
7015 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
7016 arg_idx + 1);
7017 goto erret;
7018 }
7019
7020 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007021 goto erret;
7022 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007023 ufunc->uf_def_arg_idx[count] = instr->ga_len;
7024 }
7025
7026 /*
7027 * Loop over all the lines of the function and generate instructions.
7028 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 for (;;)
7030 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007031 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007032 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007033 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007034 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007035
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007036 // Bail out on the first error to avoid a flood of errors and report
7037 // the right line number when inside try/catch.
7038 if (emsg_before != called_emsg)
7039 goto erret;
7040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 if (line != NULL && *line == '|')
7042 // the line continues after a '|'
7043 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007044 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007045 && !(*line == '#' && (line == cctx.ctx_line_start
7046 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007048 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049 goto erret;
7050 }
7051 else
7052 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007053 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007054 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007055 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007058 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059
Bram Moolenaara80faa82020-04-12 19:37:17 +02007060 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061 ea.cmdlinep = &line;
7062 ea.cmd = skipwhite(line);
7063
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007064 // Some things can be recognized by the first character.
7065 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007067 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007068 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007069 if (ea.cmd[1] != '{')
7070 {
7071 line = (char_u *)"";
7072 continue;
7073 }
7074 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007076 case '}':
7077 {
7078 // "}" ends a block scope
7079 scopetype_T stype = cctx.ctx_scope == NULL
7080 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007082 if (stype == BLOCK_SCOPE)
7083 {
7084 compile_endblock(&cctx);
7085 line = ea.cmd;
7086 }
7087 else
7088 {
7089 emsg(_("E1025: using } outside of a block scope"));
7090 goto erret;
7091 }
7092 if (line != NULL)
7093 line = skipwhite(ea.cmd + 1);
7094 continue;
7095 }
7096
7097 case '{':
7098 // "{" starts a block scope
7099 // "{'a': 1}->func() is something else
7100 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7101 {
7102 line = compile_block(ea.cmd, &cctx);
7103 continue;
7104 }
7105 break;
7106
7107 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007108 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007109 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110 }
7111
7112 /*
7113 * COMMAND MODIFIERS
7114 */
7115 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7116 {
7117 if (errormsg != NULL)
7118 goto erret;
7119 // empty line or comment
7120 line = (char_u *)"";
7121 continue;
7122 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007123 // TODO: use modifiers in the command
7124 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007125 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126
7127 // Skip ":call" to get to the function name.
7128 if (checkforcmd(&ea.cmd, "call", 3))
7129 ea.cmd = skipwhite(ea.cmd);
7130
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007131 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007133 char_u *pskip;
7134
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007135 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007136 // find what follows.
7137 // Skip over "var.member", "var[idx]" and the like.
7138 // Also "&opt = val", "$ENV = val" and "@r = val".
7139 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007140 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007141 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007142 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007144 char_u *var_end;
7145 int oplen;
7146 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007148 var_end = find_name_end(pskip, NULL, NULL,
7149 FNE_CHECK_START | FNE_INCL_BR);
7150 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007151 if (oplen > 0)
7152 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007153 size_t len = p - ea.cmd;
7154
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007155 // Recognize an assignment if we recognize the variable
7156 // name:
7157 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007158 // "local = expr" where "local" is a local var.
7159 // "script = expr" where "script" is a script-local var.
7160 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007161 // "&opt = expr"
7162 // "$ENV = expr"
7163 // "@r = expr"
7164 if (*ea.cmd == '&'
7165 || *ea.cmd == '$'
7166 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007167 || ((len) > 2 && ea.cmd[1] == ':')
7168 || lookup_local(ea.cmd, len, &cctx) != NULL
7169 || lookup_arg(ea.cmd, len, NULL, NULL,
7170 NULL, &cctx) == OK
7171 || lookup_script(ea.cmd, len) == OK
7172 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007173 {
7174 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007175 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007176 goto erret;
7177 continue;
7178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007179 }
7180 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007181
7182 if (*ea.cmd == '[')
7183 {
7184 // [var, var] = expr
7185 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7186 if (line == NULL)
7187 goto erret;
7188 if (line != ea.cmd)
7189 continue;
7190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007191 }
7192
7193 /*
7194 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007195 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007197 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007198 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007199 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007200 ea.cmd = skip_range(ea.cmd, NULL);
7201 if (ea.cmd > cmd && !starts_with_colon)
7202 {
7203 emsg(_(e_colon_required));
7204 goto erret;
7205 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007206 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007207 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007208 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007209 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007210
7211 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7212 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007213 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007214 {
7215 line += STRLEN(line);
7216 continue;
7217 }
7218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007219 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007220 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007221 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007222 // CMD_let cannot happen, compile_assignment() above is used
7223 iemsg("Command from find_ex_command() not handled");
7224 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007226 }
7227
7228 p = skipwhite(p);
7229
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007230 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007231 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007232 && ea.cmdidx != CMD_elseif
7233 && ea.cmdidx != CMD_else
7234 && ea.cmdidx != CMD_endif)
7235 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007236 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007237 continue;
7238 }
7239
Bram Moolenaarefd88552020-06-18 20:50:10 +02007240 if (ea.cmdidx != CMD_elseif
7241 && ea.cmdidx != CMD_else
7242 && ea.cmdidx != CMD_endif
7243 && ea.cmdidx != CMD_endfor
7244 && ea.cmdidx != CMD_endwhile
7245 && ea.cmdidx != CMD_catch
7246 && ea.cmdidx != CMD_finally
7247 && ea.cmdidx != CMD_endtry)
7248 {
7249 if (cctx.ctx_had_return)
7250 {
7251 emsg(_("E1095: Unreachable code after :return"));
7252 goto erret;
7253 }
7254 }
7255
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256 switch (ea.cmdidx)
7257 {
7258 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007259 ea.arg = p;
7260 line = compile_nested_function(&ea, &cctx);
7261 break;
7262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007264 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007265 goto erret;
7266
7267 case CMD_return:
7268 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007269 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007270 break;
7271
7272 case CMD_let:
7273 case CMD_const:
7274 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007275 if (line == p)
7276 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 break;
7278
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007279 case CMD_unlet:
7280 case CMD_unlockvar:
7281 case CMD_lockvar:
7282 line = compile_unletlock(p, &ea, &cctx);
7283 break;
7284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007285 case CMD_import:
7286 line = compile_import(p, &cctx);
7287 break;
7288
7289 case CMD_if:
7290 line = compile_if(p, &cctx);
7291 break;
7292 case CMD_elseif:
7293 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007294 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295 break;
7296 case CMD_else:
7297 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007298 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007299 break;
7300 case CMD_endif:
7301 line = compile_endif(p, &cctx);
7302 break;
7303
7304 case CMD_while:
7305 line = compile_while(p, &cctx);
7306 break;
7307 case CMD_endwhile:
7308 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007309 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 break;
7311
7312 case CMD_for:
7313 line = compile_for(p, &cctx);
7314 break;
7315 case CMD_endfor:
7316 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007317 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007318 break;
7319 case CMD_continue:
7320 line = compile_continue(p, &cctx);
7321 break;
7322 case CMD_break:
7323 line = compile_break(p, &cctx);
7324 break;
7325
7326 case CMD_try:
7327 line = compile_try(p, &cctx);
7328 break;
7329 case CMD_catch:
7330 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007331 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332 break;
7333 case CMD_finally:
7334 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007335 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336 break;
7337 case CMD_endtry:
7338 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007339 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 break;
7341 case CMD_throw:
7342 line = compile_throw(p, &cctx);
7343 break;
7344
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007345 case CMD_eval:
7346 if (compile_expr0(&p, &cctx) == FAIL)
7347 goto erret;
7348
7349 // drop the return value
7350 generate_instr_drop(&cctx, ISN_DROP, 1);
7351
7352 line = skipwhite(p);
7353 break;
7354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007357 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007358 case CMD_echomsg:
7359 case CMD_echoerr:
7360 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007361 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007363 // TODO: other commands with an expression argument
7364
Bram Moolenaar002262f2020-07-08 17:47:57 +02007365 case CMD_SIZE:
7366 semsg(_("E476: Invalid command: %s"), ea.cmd);
7367 goto erret;
7368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007370 // Not recognized, execute with do_cmdline_cmd().
7371 ea.arg = p;
7372 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373 break;
7374 }
7375 if (line == NULL)
7376 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007377 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378
7379 if (cctx.ctx_type_stack.ga_len < 0)
7380 {
7381 iemsg("Type stack underflow");
7382 goto erret;
7383 }
7384 }
7385
7386 if (cctx.ctx_scope != NULL)
7387 {
7388 if (cctx.ctx_scope->se_type == IF_SCOPE)
7389 emsg(_(e_endif));
7390 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7391 emsg(_(e_endwhile));
7392 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7393 emsg(_(e_endfor));
7394 else
7395 emsg(_("E1026: Missing }"));
7396 goto erret;
7397 }
7398
Bram Moolenaarefd88552020-06-18 20:50:10 +02007399 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007400 {
7401 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7402 {
7403 emsg(_("E1027: Missing return statement"));
7404 goto erret;
7405 }
7406
7407 // Return zero if there is no return at the end.
7408 generate_PUSHNR(&cctx, 0);
7409 generate_instr(&cctx, ISN_RETURN);
7410 }
7411
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007412 {
7413 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7414 + ufunc->uf_dfunc_idx;
7415 dfunc->df_deleted = FALSE;
7416 dfunc->df_instr = instr->ga_data;
7417 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007418 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007419 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007420 if (cctx.ctx_outer_used)
7421 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007422 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007423 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424
7425 ret = OK;
7426
7427erret:
7428 if (ret == FAIL)
7429 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007430 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007431 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7432 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007433
7434 for (idx = 0; idx < instr->ga_len; ++idx)
7435 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007436 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007437
Bram Moolenaar45a15082020-05-25 00:28:33 +02007438 // if using the last entry in the table we might as well remove it
7439 if (!dfunc->df_deleted
7440 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007441 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007442 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007443
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007444 while (cctx.ctx_scope != NULL)
7445 drop_scope(&cctx);
7446
Bram Moolenaar20431c92020-03-20 18:39:46 +01007447 // Don't execute this function body.
7448 ga_clear_strings(&ufunc->uf_lines);
7449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 if (errormsg != NULL)
7451 emsg(errormsg);
7452 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007453 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007454 }
7455
7456 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007457 if (do_estack_push)
7458 estack_pop();
7459
Bram Moolenaar20431c92020-03-20 18:39:46 +01007460 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007461 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007463 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464}
7465
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007466 void
7467set_function_type(ufunc_T *ufunc)
7468{
7469 int varargs = ufunc->uf_va_name != NULL;
7470 int argcount = ufunc->uf_args.ga_len;
7471
7472 // Create a type for the function, with the return type and any
7473 // argument types.
7474 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7475 // The type is included in "tt_args".
7476 if (argcount > 0 || varargs)
7477 {
7478 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7479 argcount, &ufunc->uf_type_list);
7480 // Add argument types to the function type.
7481 if (func_type_add_arg_types(ufunc->uf_func_type,
7482 argcount + varargs,
7483 &ufunc->uf_type_list) == FAIL)
7484 return;
7485 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7486 ufunc->uf_func_type->tt_min_argcount =
7487 argcount - ufunc->uf_def_args.ga_len;
7488 if (ufunc->uf_arg_types == NULL)
7489 {
7490 int i;
7491
7492 // lambda does not have argument types.
7493 for (i = 0; i < argcount; ++i)
7494 ufunc->uf_func_type->tt_args[i] = &t_any;
7495 }
7496 else
7497 mch_memmove(ufunc->uf_func_type->tt_args,
7498 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7499 if (varargs)
7500 {
7501 ufunc->uf_func_type->tt_args[argcount] =
7502 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7503 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7504 }
7505 }
7506 else
7507 // No arguments, can use a predefined type.
7508 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7509 argcount, &ufunc->uf_type_list);
7510}
7511
7512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513/*
7514 * Delete an instruction, free what it contains.
7515 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007516 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517delete_instr(isn_T *isn)
7518{
7519 switch (isn->isn_type)
7520 {
7521 case ISN_EXEC:
7522 case ISN_LOADENV:
7523 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007524 case ISN_LOADB:
7525 case ISN_LOADW:
7526 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007527 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007528 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529 case ISN_PUSHEXC:
7530 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007531 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007533 case ISN_STOREB:
7534 case ISN_STOREW:
7535 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007536 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537 vim_free(isn->isn_arg.string);
7538 break;
7539
7540 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007541 case ISN_STORES:
7542 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007543 break;
7544
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007545 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007546 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007547 vim_free(isn->isn_arg.unlet.ul_name);
7548 break;
7549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007550 case ISN_STOREOPT:
7551 vim_free(isn->isn_arg.storeopt.so_name);
7552 break;
7553
7554 case ISN_PUSHBLOB: // push blob isn_arg.blob
7555 blob_unref(isn->isn_arg.blob);
7556 break;
7557
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007558 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007559#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007560 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007561#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007562 break;
7563
7564 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007565#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007566 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007567#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007568 break;
7569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007570 case ISN_UCALL:
7571 vim_free(isn->isn_arg.ufunc.cuf_name);
7572 break;
7573
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007574 case ISN_FUNCREF:
7575 {
7576 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7577 + isn->isn_arg.funcref.fr_func;
7578 func_ptr_unref(dfunc->df_ufunc);
7579 }
7580 break;
7581
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007582 case ISN_2BOOL:
7583 case ISN_2STRING:
7584 case ISN_ADDBLOB:
7585 case ISN_ADDLIST:
7586 case ISN_BCALL:
7587 case ISN_CATCH:
7588 case ISN_CHECKNR:
7589 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007590 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007591 case ISN_COMPAREANY:
7592 case ISN_COMPAREBLOB:
7593 case ISN_COMPAREBOOL:
7594 case ISN_COMPAREDICT:
7595 case ISN_COMPAREFLOAT:
7596 case ISN_COMPAREFUNC:
7597 case ISN_COMPARELIST:
7598 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007599 case ISN_COMPARESPECIAL:
7600 case ISN_COMPARESTRING:
7601 case ISN_CONCAT:
7602 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007603 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604 case ISN_DROP:
7605 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007606 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007607 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007608 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007609 case ISN_EXECCONCAT:
7610 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007611 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007612 case ISN_LISTINDEX:
7613 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007614 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007615 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007616 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617 case ISN_JUMP:
7618 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007619 case ISN_LOADBDICT:
7620 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007621 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007622 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007623 case ISN_LOADSCRIPT:
7624 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007625 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007626 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627 case ISN_NEGATENR:
7628 case ISN_NEWDICT:
7629 case ISN_NEWLIST:
7630 case ISN_OPNR:
7631 case ISN_OPFLOAT:
7632 case ISN_OPANY:
7633 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007634 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635 case ISN_PUSHF:
7636 case ISN_PUSHNR:
7637 case ISN_PUSHBOOL:
7638 case ISN_PUSHSPEC:
7639 case ISN_RETURN:
7640 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007641 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007642 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007643 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007644 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007646 case ISN_STOREDICT:
7647 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007648 case ISN_THROW:
7649 case ISN_TRY:
7650 // nothing allocated
7651 break;
7652 }
7653}
7654
7655/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007656 * Free all instructions for "dfunc".
7657 */
7658 static void
7659delete_def_function_contents(dfunc_T *dfunc)
7660{
7661 int idx;
7662
7663 ga_clear(&dfunc->df_def_args_isn);
7664
7665 if (dfunc->df_instr != NULL)
7666 {
7667 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7668 delete_instr(dfunc->df_instr + idx);
7669 VIM_CLEAR(dfunc->df_instr);
7670 }
7671
7672 dfunc->df_deleted = TRUE;
7673}
7674
7675/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007676 * When a user function is deleted, clear the contents of any associated def
7677 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007678 */
7679 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007680clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007682 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683 {
7684 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7685 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007686
Bram Moolenaar20431c92020-03-20 18:39:46 +01007687 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007688 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007689 }
7690}
7691
7692#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007693/*
7694 * Free all functions defined with ":def".
7695 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007696 void
7697free_def_functions(void)
7698{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007699 int idx;
7700
7701 for (idx = 0; idx < def_functions.ga_len; ++idx)
7702 {
7703 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7704
7705 delete_def_function_contents(dfunc);
7706 }
7707
7708 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007709}
7710#endif
7711
7712
7713#endif // FEAT_EVAL