blob: 394433a1274c7802b956c8c8b943cd59a9a2ed2e [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);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003776 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 if (**arg == '{')
3778 {
3779 // lambda call: list->{lambda}
3780 if (compile_lambda_call(arg, cctx) == FAIL)
3781 return FAIL;
3782 }
3783 else
3784 {
3785 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003786 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003787 if (!eval_isnamec1(*p))
3788 {
3789 semsg(_(e_trailing_arg), p);
3790 return FAIL;
3791 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003792 if (ASCII_ISALPHA(*p) && p[1] == ':')
3793 p += 2;
3794 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 ;
3796 if (*p != '(')
3797 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003798 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 return FAIL;
3800 }
3801 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003802 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 return FAIL;
3804 }
3805 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003806 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003808 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003809 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003810 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003811
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003812 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003813 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003814 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003815 // TODO: blob index
3816 // TODO: more arguments
3817 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003818 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003819 return FAIL;
3820
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003821 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003822 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003823 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003824 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003825 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 return FAIL;
3827
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003828 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3829 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 if (**arg != ']')
3831 {
3832 emsg(_(e_missbrac));
3833 return FAIL;
3834 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003835 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003837 // We can index a list and a dict. If we don't know the type
3838 // we can use the index value type.
3839 // TODO: If we don't know use an instruction to figure it out at
3840 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003841 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003842 vtype = (*typep)->tt_type;
3843 if (*typep == &t_any)
3844 {
3845 type_T *valtype = ((type_T **)stack->ga_data)
3846 [stack->ga_len - 1];
3847 if (valtype == &t_string)
3848 vtype = VAR_DICT;
3849 }
3850 if (vtype == VAR_DICT)
3851 {
3852 if ((*typep)->tt_type == VAR_DICT)
3853 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003854 else
3855 {
3856 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3857 return FAIL;
3858 *typep = &t_any;
3859 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003860 if (may_generate_2STRING(-1, cctx) == FAIL)
3861 return FAIL;
3862 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3863 return FAIL;
3864 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003865 else if (vtype == VAR_STRING)
3866 {
3867 *typep = &t_number;
3868 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3869 return FAIL;
3870 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003871 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003872 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003873 if ((*typep)->tt_type == VAR_LIST)
3874 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003875 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003876 return FAIL;
3877 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003878 else
3879 {
3880 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003881 return FAIL;
3882 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003884 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003886 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003887 return FAIL;
3888
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003889 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003890 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3891 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003893 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 if (eval_isnamec1(*p))
3895 while (eval_isnamec(*p))
3896 MB_PTR_ADV(p);
3897 if (p == *arg)
3898 {
3899 semsg(_(e_syntax_at), *arg);
3900 return FAIL;
3901 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003902 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 return FAIL;
3904 *arg = p;
3905 }
3906 else
3907 break;
3908 }
3909
3910 // TODO - see handle_subscript():
3911 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3912 // Don't do this when "Func" is already a partial that was bound
3913 // explicitly (pt_auto is FALSE).
3914
3915 return OK;
3916}
3917
3918/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003919 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3920 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003922 * If the value is a constant "ppconst->pp_ret" will be set.
3923 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003924 *
3925 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 */
3927
3928/*
3929 * number number constant
3930 * 0zFFFFFFFF Blob constant
3931 * "string" string constant
3932 * 'string' literal string constant
3933 * &option-name option value
3934 * @r register contents
3935 * identifier variable value
3936 * function() function call
3937 * $VAR environment variable
3938 * (expression) nested expression
3939 * [expr, expr] List
3940 * {key: val, key: val} Dictionary
3941 * #{key: val, key: val} Dictionary with literal keys
3942 *
3943 * Also handle:
3944 * ! in front logical NOT
3945 * - in front unary minus
3946 * + in front unary plus (ignored)
3947 * trailing (arg) funcref/partial call
3948 * trailing [] subscript in String or List
3949 * trailing .name entry in Dictionary
3950 * trailing ->name() method call
3951 */
3952 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003953compile_expr7(
3954 char_u **arg,
3955 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003956 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 char_u *start_leader, *end_leader;
3959 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003960 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003961 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962
3963 /*
3964 * Skip '!', '-' and '+' characters. They are handled later.
3965 */
3966 start_leader = *arg;
3967 while (**arg == '!' || **arg == '-' || **arg == '+')
3968 *arg = skipwhite(*arg + 1);
3969 end_leader = *arg;
3970
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003971 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 switch (**arg)
3973 {
3974 /*
3975 * Number constant.
3976 */
3977 case '0': // also for blob starting with 0z
3978 case '1':
3979 case '2':
3980 case '3':
3981 case '4':
3982 case '5':
3983 case '6':
3984 case '7':
3985 case '8':
3986 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003987 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 return FAIL;
3989 break;
3990
3991 /*
3992 * String constant: "string".
3993 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003994 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 return FAIL;
3996 break;
3997
3998 /*
3999 * Literal string constant: 'str''ing'.
4000 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004001 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 return FAIL;
4003 break;
4004
4005 /*
4006 * Constant Vim variable.
4007 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004008 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 ret = NOTDONE;
4010 break;
4011
4012 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004013 * "true" constant
4014 */
4015 case 't': if (STRNCMP(*arg, "true", 4) == 0
4016 && !eval_isnamec((*arg)[4]))
4017 {
4018 *arg += 4;
4019 rettv->v_type = VAR_BOOL;
4020 rettv->vval.v_number = VVAL_TRUE;
4021 }
4022 else
4023 ret = NOTDONE;
4024 break;
4025
4026 /*
4027 * "false" constant
4028 */
4029 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4030 && !eval_isnamec((*arg)[5]))
4031 {
4032 *arg += 5;
4033 rettv->v_type = VAR_BOOL;
4034 rettv->vval.v_number = VVAL_FALSE;
4035 }
4036 else
4037 ret = NOTDONE;
4038 break;
4039
4040 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 * List: [expr, expr]
4042 */
4043 case '[': ret = compile_list(arg, cctx);
4044 break;
4045
4046 /*
4047 * Dictionary: #{key: val, key: val}
4048 */
4049 case '#': if ((*arg)[1] == '{')
4050 {
4051 ++*arg;
4052 ret = compile_dict(arg, cctx, TRUE);
4053 }
4054 else
4055 ret = NOTDONE;
4056 break;
4057
4058 /*
4059 * Lambda: {arg, arg -> expr}
4060 * Dictionary: {'key': val, 'key': val}
4061 */
4062 case '{': {
4063 char_u *start = skipwhite(*arg + 1);
4064
4065 // Find out what comes after the arguments.
4066 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004067 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 if (ret != FAIL && *start == '>')
4069 ret = compile_lambda(arg, cctx);
4070 else
4071 ret = compile_dict(arg, cctx, FALSE);
4072 }
4073 break;
4074
4075 /*
4076 * Option value: &name
4077 */
4078 case '&': ret = compile_get_option(arg, cctx);
4079 break;
4080
4081 /*
4082 * Environment variable: $VAR.
4083 */
4084 case '$': ret = compile_get_env(arg, cctx);
4085 break;
4086
4087 /*
4088 * Register contents: @r.
4089 */
4090 case '@': ret = compile_get_register(arg, cctx);
4091 break;
4092 /*
4093 * nested expression: (expression).
4094 */
4095 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004096
4097 // recursive!
4098 if (ppconst->pp_used <= PPSIZE - 10)
4099 {
4100 ret = compile_expr1(arg, cctx, ppconst);
4101 }
4102 else
4103 {
4104 // Not enough space in ppconst, flush constants.
4105 if (generate_ppconst(cctx, ppconst) == FAIL)
4106 return FAIL;
4107 ret = compile_expr0(arg, cctx);
4108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 *arg = skipwhite(*arg);
4110 if (**arg == ')')
4111 ++*arg;
4112 else if (ret == OK)
4113 {
4114 emsg(_(e_missing_close));
4115 ret = FAIL;
4116 }
4117 break;
4118
4119 default: ret = NOTDONE;
4120 break;
4121 }
4122 if (ret == FAIL)
4123 return FAIL;
4124
Bram Moolenaar1c747212020-05-09 18:28:34 +02004125 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 {
4127 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004128 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004130 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 return FAIL;
4132 }
4133 start_leader = end_leader; // don't apply again below
4134
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004135 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004136 clear_tv(rettv);
4137 else
4138 // A constant expression can possibly be handled compile time,
4139 // return the value instead of generating code.
4140 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 }
4142 else if (ret == NOTDONE)
4143 {
4144 char_u *p;
4145 int r;
4146
4147 if (!eval_isnamec1(**arg))
4148 {
4149 semsg(_("E1015: Name expected: %s"), *arg);
4150 return FAIL;
4151 }
4152
4153 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004154 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004156 {
4157 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004160 {
4161 if (generate_ppconst(cctx, ppconst) == FAIL)
4162 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 if (r == FAIL)
4166 return FAIL;
4167 }
4168
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004169 // Handle following "[]", ".member", etc.
4170 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004171 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004172 ppconst) == FAIL)
4173 return FAIL;
4174 if (ppconst->pp_used > 0)
4175 {
4176 // apply the '!', '-' and '+' before the constant
4177 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4178 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4179 return FAIL;
4180 return OK;
4181 }
4182 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004184 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185}
4186
4187/*
4188 * * number multiplication
4189 * / number division
4190 * % number modulo
4191 */
4192 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004193compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194{
4195 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004196 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004197 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004199 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004200 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 return FAIL;
4202
4203 /*
4204 * Repeat computing, until no "*", "/" or "%" is following.
4205 */
4206 for (;;)
4207 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004208 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 if (*op != '*' && *op != '/' && *op != '%')
4210 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004211 if (next != NULL)
4212 {
4213 *arg = next_line_from_context(cctx, TRUE);
4214 op = skipwhite(*arg);
4215 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004216
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004217 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 {
4219 char_u buf[3];
4220
4221 vim_strncpy(buf, op, 1);
4222 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004223 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 }
4225 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004226 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004227 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004229 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004230 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004232
4233 if (ppconst->pp_used == ppconst_used + 2
4234 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4235 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004236 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004237 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4238 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004239 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004241 // both are numbers: compute the result
4242 switch (*op)
4243 {
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;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004246 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004247 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004248 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004249 break;
4250 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004251 tv1->vval.v_number = res;
4252 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004253 }
4254 else
4255 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004256 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004257 generate_two_op(cctx, op);
4258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 }
4260
4261 return OK;
4262}
4263
4264/*
4265 * + number addition
4266 * - number subtraction
4267 * .. string concatenation
4268 */
4269 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004270compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271{
4272 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004273 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004275 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276
4277 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004278 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 return FAIL;
4280
4281 /*
4282 * Repeat computing, until no "+", "-" or ".." is following.
4283 */
4284 for (;;)
4285 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004286 op = may_peek_next_line(cctx, *arg, &next);
4287 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 break;
4289 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004290 if (next != NULL)
4291 {
4292 *arg = next_line_from_context(cctx, TRUE);
4293 op = skipwhite(*arg);
4294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004296 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 {
4298 char_u buf[3];
4299
4300 vim_strncpy(buf, op, oplen);
4301 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004302 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 }
4304
4305 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004306 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004307 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004309 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004310 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 return FAIL;
4312
Bram Moolenaara5565e42020-05-09 15:44:01 +02004313 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004314 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004315 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4316 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4317 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4318 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004320 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4321 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004322
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004323 // concat/subtract/add constant numbers
4324 if (*op == '+')
4325 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4326 else if (*op == '-')
4327 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4328 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004329 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004330 // concatenate constant strings
4331 char_u *s1 = tv1->vval.v_string;
4332 char_u *s2 = tv2->vval.v_string;
4333 size_t len1 = STRLEN(s1);
4334
4335 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4336 if (tv1->vval.v_string == NULL)
4337 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004338 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004339 return FAIL;
4340 }
4341 mch_memmove(tv1->vval.v_string, s1, len1);
4342 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004343 vim_free(s1);
4344 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004345 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004346 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 }
4348 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004349 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004350 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004351 if (*op == '.')
4352 {
4353 if (may_generate_2STRING(-2, cctx) == FAIL
4354 || may_generate_2STRING(-1, cctx) == FAIL)
4355 return FAIL;
4356 generate_instr_drop(cctx, ISN_CONCAT, 1);
4357 }
4358 else
4359 generate_two_op(cctx, op);
4360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 }
4362
4363 return OK;
4364}
4365
4366/*
4367 * expr5a == expr5b
4368 * expr5a =~ expr5b
4369 * expr5a != expr5b
4370 * expr5a !~ expr5b
4371 * expr5a > expr5b
4372 * expr5a >= expr5b
4373 * expr5a < expr5b
4374 * expr5a <= expr5b
4375 * expr5a is expr5b
4376 * expr5a isnot expr5b
4377 *
4378 * Produces instructions:
4379 * EVAL expr5a Push result of "expr5a"
4380 * EVAL expr5b Push result of "expr5b"
4381 * COMPARE one of the compare instructions
4382 */
4383 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004384compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385{
4386 exptype_T type = EXPR_UNKNOWN;
4387 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004388 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004391 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392
4393 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004394 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 return FAIL;
4396
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004397 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004398 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399
4400 /*
4401 * If there is a comparative operator, use it.
4402 */
4403 if (type != EXPR_UNKNOWN)
4404 {
4405 int ic = FALSE; // Default: do not ignore case
4406
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004407 if (next != NULL)
4408 {
4409 *arg = next_line_from_context(cctx, TRUE);
4410 p = skipwhite(*arg);
4411 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 if (type_is && (p[len] == '?' || p[len] == '#'))
4413 {
4414 semsg(_(e_invexpr2), *arg);
4415 return FAIL;
4416 }
4417 // extra question mark appended: ignore case
4418 if (p[len] == '?')
4419 {
4420 ic = TRUE;
4421 ++len;
4422 }
4423 // extra '#' appended: match case (ignored)
4424 else if (p[len] == '#')
4425 ++len;
4426 // nothing appended: match case
4427
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004428 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 {
4430 char_u buf[7];
4431
4432 vim_strncpy(buf, p, len);
4433 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004434 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 }
4436
4437 // get the second variable
4438 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004439 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004440 return FAIL;
4441
Bram Moolenaara5565e42020-05-09 15:44:01 +02004442 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 return FAIL;
4444
Bram Moolenaara5565e42020-05-09 15:44:01 +02004445 if (ppconst->pp_used == ppconst_used + 2)
4446 {
4447 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4448 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4449 int ret;
4450
4451 // Both sides are a constant, compute the result now.
4452 // First check for a valid combination of types, this is more
4453 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004454 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004455 ret = FAIL;
4456 else
4457 {
4458 ret = typval_compare(tv1, tv2, type, ic);
4459 tv1->v_type = VAR_BOOL;
4460 tv1->vval.v_number = tv1->vval.v_number
4461 ? VVAL_TRUE : VVAL_FALSE;
4462 clear_tv(tv2);
4463 --ppconst->pp_used;
4464 }
4465 return ret;
4466 }
4467
4468 generate_ppconst(cctx, ppconst);
4469 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 }
4471
4472 return OK;
4473}
4474
Bram Moolenaar7f141552020-05-09 17:35:53 +02004475static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477/*
4478 * Compile || or &&.
4479 */
4480 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004481compile_and_or(
4482 char_u **arg,
4483 cctx_T *cctx,
4484 char *op,
4485 ppconst_T *ppconst,
4486 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004488 char_u *next;
4489 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 int opchar = *op;
4491
4492 if (p[0] == opchar && p[1] == opchar)
4493 {
4494 garray_T *instr = &cctx->ctx_instr;
4495 garray_T end_ga;
4496
4497 /*
4498 * Repeat until there is no following "||" or "&&"
4499 */
4500 ga_init2(&end_ga, sizeof(int), 10);
4501 while (p[0] == opchar && p[1] == opchar)
4502 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004503 if (next != NULL)
4504 {
4505 *arg = next_line_from_context(cctx, TRUE);
4506 p = skipwhite(*arg);
4507 }
4508
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004509 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4510 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004512 return FAIL;
4513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514
Bram Moolenaara5565e42020-05-09 15:44:01 +02004515 // TODO: use ppconst if the value is a constant
4516 generate_ppconst(cctx, ppconst);
4517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 if (ga_grow(&end_ga, 1) == FAIL)
4519 {
4520 ga_clear(&end_ga);
4521 return FAIL;
4522 }
4523 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4524 ++end_ga.ga_len;
4525 generate_JUMP(cctx, opchar == '|'
4526 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4527
4528 // eval the next expression
4529 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004530 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004531 return FAIL;
4532
Bram Moolenaara5565e42020-05-09 15:44:01 +02004533 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4534 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 {
4536 ga_clear(&end_ga);
4537 return FAIL;
4538 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004539
4540 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004542 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543
4544 // Fill in the end label in all jumps.
4545 while (end_ga.ga_len > 0)
4546 {
4547 isn_T *isn;
4548
4549 --end_ga.ga_len;
4550 isn = ((isn_T *)instr->ga_data)
4551 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4552 isn->isn_arg.jump.jump_where = instr->ga_len;
4553 }
4554 ga_clear(&end_ga);
4555 }
4556
4557 return OK;
4558}
4559
4560/*
4561 * expr4a && expr4a && expr4a logical AND
4562 *
4563 * Produces instructions:
4564 * EVAL expr4a Push result of "expr4a"
4565 * JUMP_AND_KEEP_IF_FALSE end
4566 * EVAL expr4b Push result of "expr4b"
4567 * JUMP_AND_KEEP_IF_FALSE end
4568 * EVAL expr4c Push result of "expr4c"
4569 * end:
4570 */
4571 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004572compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004574 int ppconst_used = ppconst->pp_used;
4575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004577 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 return FAIL;
4579
4580 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004581 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582}
4583
4584/*
4585 * expr3a || expr3b || expr3c logical OR
4586 *
4587 * Produces instructions:
4588 * EVAL expr3a Push result of "expr3a"
4589 * JUMP_AND_KEEP_IF_TRUE end
4590 * EVAL expr3b Push result of "expr3b"
4591 * JUMP_AND_KEEP_IF_TRUE end
4592 * EVAL expr3c Push result of "expr3c"
4593 * end:
4594 */
4595 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004596compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004598 int ppconst_used = ppconst->pp_used;
4599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004601 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 return FAIL;
4603
4604 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004605 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606}
4607
4608/*
4609 * Toplevel expression: expr2 ? expr1a : expr1b
4610 *
4611 * Produces instructions:
4612 * EVAL expr2 Push result of "expr"
4613 * JUMP_IF_FALSE alt jump if false
4614 * EVAL expr1a
4615 * JUMP_ALWAYS end
4616 * alt: EVAL expr1b
4617 * end:
4618 */
4619 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004620compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621{
4622 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004623 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004624 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625
Bram Moolenaar61a89812020-05-07 16:58:17 +02004626 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004627 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 return FAIL;
4629
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004630 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 if (*p == '?')
4632 {
4633 garray_T *instr = &cctx->ctx_instr;
4634 garray_T *stack = &cctx->ctx_type_stack;
4635 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004636 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004638 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004640 int has_const_expr = FALSE;
4641 int const_value = FALSE;
4642 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004644 if (next != NULL)
4645 {
4646 *arg = next_line_from_context(cctx, TRUE);
4647 p = skipwhite(*arg);
4648 }
4649
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004650 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4651 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004653 return FAIL;
4654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655
Bram Moolenaara5565e42020-05-09 15:44:01 +02004656 if (ppconst->pp_used == ppconst_used + 1)
4657 {
4658 // the condition is a constant, we know whether the ? or the :
4659 // expression is to be evaluated.
4660 has_const_expr = TRUE;
4661 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4662 clear_tv(&ppconst->pp_tv[ppconst_used]);
4663 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004664 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4665 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004666 }
4667 else
4668 {
4669 generate_ppconst(cctx, ppconst);
4670 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4671 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672
4673 // evaluate the second expression; any type is accepted
4674 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004675 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004676 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004677 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004678 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679
Bram Moolenaara5565e42020-05-09 15:44:01 +02004680 if (!has_const_expr)
4681 {
4682 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683
Bram Moolenaara5565e42020-05-09 15:44:01 +02004684 // remember the type and drop it
4685 --stack->ga_len;
4686 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687
Bram Moolenaara5565e42020-05-09 15:44:01 +02004688 end_idx = instr->ga_len;
4689 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4690
4691 // jump here from JUMP_IF_FALSE
4692 isn = ((isn_T *)instr->ga_data) + alt_idx;
4693 isn->isn_arg.jump.jump_where = instr->ga_len;
4694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695
4696 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004697 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 if (*p != ':')
4699 {
4700 emsg(_(e_missing_colon));
4701 return FAIL;
4702 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004703 if (next != NULL)
4704 {
4705 *arg = next_line_from_context(cctx, TRUE);
4706 p = skipwhite(*arg);
4707 }
4708
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004709 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4710 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004712 return FAIL;
4713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714
4715 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004716 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004717 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4718 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004720 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004721 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004722 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004723 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724
Bram Moolenaara5565e42020-05-09 15:44:01 +02004725 if (!has_const_expr)
4726 {
4727 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728
Bram Moolenaara5565e42020-05-09 15:44:01 +02004729 // If the types differ, the result has a more generic type.
4730 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4731 common_type(type1, type2, &type2, cctx->ctx_type_list);
4732
4733 // jump here from JUMP_ALWAYS
4734 isn = ((isn_T *)instr->ga_data) + end_idx;
4735 isn->isn_arg.jump.jump_where = instr->ga_len;
4736 }
4737
4738 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 }
4740 return OK;
4741}
4742
4743/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004744 * Toplevel expression.
4745 */
4746 static int
4747compile_expr0(char_u **arg, cctx_T *cctx)
4748{
4749 ppconst_T ppconst;
4750
4751 CLEAR_FIELD(ppconst);
4752 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4753 {
4754 clear_ppconst(&ppconst);
4755 return FAIL;
4756 }
4757 if (generate_ppconst(cctx, &ppconst) == FAIL)
4758 return FAIL;
4759 return OK;
4760}
4761
4762/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 * compile "return [expr]"
4764 */
4765 static char_u *
4766compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4767{
4768 char_u *p = arg;
4769 garray_T *stack = &cctx->ctx_type_stack;
4770 type_T *stack_type;
4771
4772 if (*p != NUL && *p != '|' && *p != '\n')
4773 {
4774 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004775 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 return NULL;
4777
4778 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4779 if (set_return_type)
4780 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004781 else
4782 {
4783 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4784 && stack_type->tt_type != VAR_VOID
4785 && stack_type->tt_type != VAR_UNKNOWN)
4786 {
4787 emsg(_("E1096: Returning a value in a function without a return type"));
4788 return NULL;
4789 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004790 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4791 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 }
4795 else
4796 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004797 // "set_return_type" cannot be TRUE, only used for a lambda which
4798 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004799 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4800 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 {
4802 emsg(_("E1003: Missing return value"));
4803 return NULL;
4804 }
4805
4806 // No argument, return zero.
4807 generate_PUSHNR(cctx, 0);
4808 }
4809
4810 if (generate_instr(cctx, ISN_RETURN) == NULL)
4811 return NULL;
4812
4813 // "return val | endif" is possible
4814 return skipwhite(p);
4815}
4816
4817/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004818 * Get a line from the compilation context, compatible with exarg_T getline().
4819 * Return a pointer to the line in allocated memory.
4820 * Return NULL for end-of-file or some error.
4821 */
4822 static char_u *
4823exarg_getline(
4824 int c UNUSED,
4825 void *cookie,
4826 int indent UNUSED,
4827 int do_concat UNUSED)
4828{
4829 cctx_T *cctx = (cctx_T *)cookie;
4830
4831 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4832 {
4833 iemsg("Heredoc got to end");
4834 return NULL;
4835 }
4836 ++cctx->ctx_lnum;
4837 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4838 [cctx->ctx_lnum]);
4839}
4840
4841/*
4842 * Compile a nested :def command.
4843 */
4844 static char_u *
4845compile_nested_function(exarg_T *eap, cctx_T *cctx)
4846{
4847 char_u *name_start = eap->arg;
4848 char_u *name_end = to_name_end(eap->arg, FALSE);
4849 char_u *name = get_lambda_name();
4850 lvar_T *lvar;
4851 ufunc_T *ufunc;
4852
4853 eap->arg = name_end;
4854 eap->getline = exarg_getline;
4855 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004856 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004857 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004858 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004859
Bram Moolenaar822ba242020-05-24 23:00:18 +02004860 if (ufunc == NULL)
4861 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004862 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004863 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004864 return NULL;
4865
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004866 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004867 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004868 TRUE, ufunc->uf_func_type);
4869
4870 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4871 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4872 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004873
Bram Moolenaar61a89812020-05-07 16:58:17 +02004874 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004875 return (char_u *)"";
4876}
4877
4878/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879 * Return the length of an assignment operator, or zero if there isn't one.
4880 */
4881 int
4882assignment_len(char_u *p, int *heredoc)
4883{
4884 if (*p == '=')
4885 {
4886 if (p[1] == '<' && p[2] == '<')
4887 {
4888 *heredoc = TRUE;
4889 return 3;
4890 }
4891 return 1;
4892 }
4893 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4894 return 2;
4895 if (STRNCMP(p, "..=", 3) == 0)
4896 return 3;
4897 return 0;
4898}
4899
4900// words that cannot be used as a variable
4901static char *reserved[] = {
4902 "true",
4903 "false",
4904 NULL
4905};
4906
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004907typedef enum {
4908 dest_local,
4909 dest_option,
4910 dest_env,
4911 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004912 dest_buffer,
4913 dest_window,
4914 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004915 dest_vimvar,
4916 dest_script,
4917 dest_reg,
4918} assign_dest_T;
4919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004921 * Generate the load instruction for "name".
4922 */
4923 static void
4924generate_loadvar(
4925 cctx_T *cctx,
4926 assign_dest_T dest,
4927 char_u *name,
4928 lvar_T *lvar,
4929 type_T *type)
4930{
4931 switch (dest)
4932 {
4933 case dest_option:
4934 // TODO: check the option exists
4935 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4936 break;
4937 case dest_global:
4938 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4939 break;
4940 case dest_buffer:
4941 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4942 break;
4943 case dest_window:
4944 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4945 break;
4946 case dest_tab:
4947 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4948 break;
4949 case dest_script:
4950 compile_load_scriptvar(cctx,
4951 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4952 break;
4953 case dest_env:
4954 // Include $ in the name here
4955 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4956 break;
4957 case dest_reg:
4958 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4959 break;
4960 case dest_vimvar:
4961 generate_LOADV(cctx, name + 2, TRUE);
4962 break;
4963 case dest_local:
4964 if (lvar->lv_from_outer)
4965 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4966 NULL, type);
4967 else
4968 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4969 break;
4970 }
4971}
4972
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004973 void
4974vim9_declare_error(char_u *name)
4975{
4976 char *scope = "";
4977
4978 switch (*name)
4979 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004980 case 'g': scope = _("global"); break;
4981 case 'b': scope = _("buffer"); break;
4982 case 'w': scope = _("window"); break;
4983 case 't': scope = _("tab"); break;
4984 case 'v': scope = "v:"; break;
4985 case '$': semsg(_(e_declare_env_var), name); return;
4986 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004987 }
4988 semsg(_(e_declare_var), scope, name);
4989}
4990
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004991/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004992 * Compile declaration and assignment:
4993 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004995 * Return NULL for an error.
4996 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997 */
4998 static char_u *
4999compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5000{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005001 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005003 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 char_u *ret = NULL;
5005 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005006 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005009 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 int oplen = 0;
5012 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005013 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005014 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005015 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005019 // Skip over the "var" or "[var, var]" to get to any "=".
5020 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5021 if (p == NULL)
5022 return *arg == '[' ? arg : NULL;
5023
5024 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005026 // TODO: should we allow this, and figure out type inference from list
5027 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005028 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029 return NULL;
5030 }
5031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 sp = p;
5033 p = skipwhite(p);
5034 op = p;
5035 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005036
5037 if (var_count > 0 && oplen == 0)
5038 // can be something like "[1, 2]->func()"
5039 return arg;
5040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5042 {
5043 char_u buf[4];
5044
5045 vim_strncpy(buf, op, oplen);
5046 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005047 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005048 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 if (heredoc)
5051 {
5052 list_T *l;
5053 listitem_T *li;
5054
5055 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005056 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005058 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059
5060 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005061 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 {
5063 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5064 li->li_tv.vval.v_string = NULL;
5065 }
5066 generate_NEWLIST(cctx, l->lv_len);
5067 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005068 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 list_free(l);
5070 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005071 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005073 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005074 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005075 // for "[var, var] = expr" evaluate the expression here, loop over the
5076 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005077
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005078 p = skipwhite(op + oplen);
5079 if (compile_expr0(&p, cctx) == FAIL)
5080 return NULL;
5081 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005083 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005085 type_T *stacktype;
5086
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005087 stacktype = stack->ga_len == 0 ? &t_void
5088 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005091 emsg(_(e_cannot_use_void));
5092 goto theend;
5093 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005094 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005095 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005096 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005097 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5098 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005099 }
5100 }
5101
5102 /*
5103 * Loop over variables in "[var, var] = expr".
5104 * For "var = expr" and "let var: type" this is done only once.
5105 */
5106 if (var_count > 0)
5107 var_start = skipwhite(arg + 1); // skip over the "["
5108 else
5109 var_start = arg;
5110 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5111 {
5112 char_u *var_end = skip_var_one(var_start, FALSE);
5113 size_t varlen;
5114 int new_local = FALSE;
5115 int opt_type;
5116 int opt_flags = 0;
5117 assign_dest_T dest = dest_local;
5118 int vimvaridx = -1;
5119 lvar_T *lvar = NULL;
5120 lvar_T arg_lvar;
5121 int has_type = FALSE;
5122 int has_index = FALSE;
5123 int instr_count = -1;
5124
5125 p = (*var_start == '&' || *var_start == '$'
5126 || *var_start == '@') ? var_start + 1 : var_start;
5127 p = to_name_end(p, TRUE);
5128
5129 // "a: type" is declaring variable "a" with a type, not "a:".
5130 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5131 --var_end;
5132 if (is_decl && p == var_start + 2 && p[-1] == ':')
5133 --p;
5134
5135 varlen = p - var_start;
5136 vim_free(name);
5137 name = vim_strnsave(var_start, varlen);
5138 if (name == NULL)
5139 return NULL;
5140 if (!heredoc)
5141 type = &t_any;
5142
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005143 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005144 {
5145 if (*var_start == '&')
5146 {
5147 int cc;
5148 long numval;
5149
5150 dest = dest_option;
5151 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005153 emsg(_(e_const_option));
5154 goto theend;
5155 }
5156 if (is_decl)
5157 {
5158 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5159 goto theend;
5160 }
5161 p = var_start;
5162 p = find_option_end(&p, &opt_flags);
5163 if (p == NULL)
5164 {
5165 // cannot happen?
5166 emsg(_(e_letunexp));
5167 goto theend;
5168 }
5169 cc = *p;
5170 *p = NUL;
5171 opt_type = get_option_value(var_start + 1, &numval,
5172 NULL, opt_flags);
5173 *p = cc;
5174 if (opt_type == -3)
5175 {
5176 semsg(_(e_unknown_option), var_start);
5177 goto theend;
5178 }
5179 if (opt_type == -2 || opt_type == 0)
5180 type = &t_string;
5181 else
5182 type = &t_number; // both number and boolean option
5183 }
5184 else if (*var_start == '$')
5185 {
5186 dest = dest_env;
5187 type = &t_string;
5188 if (is_decl)
5189 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005190 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005191 goto theend;
5192 }
5193 }
5194 else if (*var_start == '@')
5195 {
5196 if (!valid_yank_reg(var_start[1], TRUE))
5197 {
5198 emsg_invreg(var_start[1]);
5199 goto theend;
5200 }
5201 dest = dest_reg;
5202 type = &t_string;
5203 if (is_decl)
5204 {
5205 semsg(_("E1066: Cannot declare a register: %s"), name);
5206 goto theend;
5207 }
5208 }
5209 else if (STRNCMP(var_start, "g:", 2) == 0)
5210 {
5211 dest = dest_global;
5212 if (is_decl)
5213 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005214 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005215 goto theend;
5216 }
5217 }
5218 else if (STRNCMP(var_start, "b:", 2) == 0)
5219 {
5220 dest = dest_buffer;
5221 if (is_decl)
5222 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005223 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005224 goto theend;
5225 }
5226 }
5227 else if (STRNCMP(var_start, "w:", 2) == 0)
5228 {
5229 dest = dest_window;
5230 if (is_decl)
5231 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005232 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005233 goto theend;
5234 }
5235 }
5236 else if (STRNCMP(var_start, "t:", 2) == 0)
5237 {
5238 dest = dest_tab;
5239 if (is_decl)
5240 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005241 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005242 goto theend;
5243 }
5244 }
5245 else if (STRNCMP(var_start, "v:", 2) == 0)
5246 {
5247 typval_T *vtv;
5248 int di_flags;
5249
5250 vimvaridx = find_vim_var(name + 2, &di_flags);
5251 if (vimvaridx < 0)
5252 {
5253 semsg(_(e_var_notfound), var_start);
5254 goto theend;
5255 }
5256 // We use the current value of "sandbox" here, is that OK?
5257 if (var_check_ro(di_flags, name, FALSE))
5258 goto theend;
5259 dest = dest_vimvar;
5260 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005261 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 if (is_decl)
5263 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005264 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005265 goto theend;
5266 }
5267 }
5268 else
5269 {
5270 int idx;
5271
5272 for (idx = 0; reserved[idx] != NULL; ++idx)
5273 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005274 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005275 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005276 goto theend;
5277 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005278
5279 lvar = lookup_local(var_start, varlen, cctx);
5280 if (lvar == NULL)
5281 {
5282 CLEAR_FIELD(arg_lvar);
5283 if (lookup_arg(var_start, varlen,
5284 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5285 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005286 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005287 if (is_decl)
5288 {
5289 semsg(_(e_used_as_arg), name);
5290 goto theend;
5291 }
5292 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005293 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005294 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005295 if (lvar != NULL)
5296 {
5297 if (is_decl)
5298 {
5299 semsg(_("E1017: Variable already declared: %s"), name);
5300 goto theend;
5301 }
5302 else if (lvar->lv_const)
5303 {
5304 semsg(_("E1018: Cannot assign to a constant: %s"),
5305 name);
5306 goto theend;
5307 }
5308 }
5309 else if (STRNCMP(var_start, "s:", 2) == 0
5310 || lookup_script(var_start, varlen) == OK
5311 || find_imported(var_start, varlen, cctx) != NULL)
5312 {
5313 dest = dest_script;
5314 if (is_decl)
5315 {
5316 semsg(_("E1054: Variable already declared in the script: %s"),
5317 name);
5318 goto theend;
5319 }
5320 }
5321 else if (name[1] == ':' && name[2] != NUL)
5322 {
5323 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5324 name);
5325 goto theend;
5326 }
5327 else if (!is_decl)
5328 {
5329 semsg(_("E1089: unknown variable: %s"), name);
5330 goto theend;
5331 }
5332 }
5333 }
5334
5335 // handle "a:name" as a name, not index "name" on "a"
5336 if (varlen > 1 || var_start[varlen] != ':')
5337 p = var_end;
5338
5339 if (dest != dest_option)
5340 {
5341 if (is_decl && *p == ':')
5342 {
5343 // parse optional type: "let var: type = expr"
5344 if (!VIM_ISWHITE(p[1]))
5345 {
5346 semsg(_(e_white_after), ":");
5347 goto theend;
5348 }
5349 p = skipwhite(p + 1);
5350 type = parse_type(&p, cctx->ctx_type_list);
5351 has_type = TRUE;
5352 }
5353 else if (lvar != NULL)
5354 type = lvar->lv_type;
5355 }
5356
5357 if (oplen == 3 && !heredoc && dest != dest_global
5358 && type->tt_type != VAR_STRING
5359 && type->tt_type != VAR_ANY)
5360 {
5361 emsg(_("E1019: Can only concatenate to string"));
5362 goto theend;
5363 }
5364
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005365 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005366 {
5367 if (oplen > 1 && !heredoc)
5368 {
5369 // +=, /=, etc. require an existing variable
5370 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5371 name);
5372 goto theend;
5373 }
5374
5375 // new local variable
5376 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5377 goto theend;
5378 lvar = reserve_local(cctx, var_start, varlen,
5379 cmdidx == CMD_const, type);
5380 if (lvar == NULL)
5381 goto theend;
5382 new_local = TRUE;
5383 }
5384
5385 member_type = type;
5386 if (var_end > var_start + varlen)
5387 {
5388 // Something follows after the variable: "var[idx]".
5389 if (is_decl)
5390 {
5391 emsg(_("E1087: cannot use an index when declaring a variable"));
5392 goto theend;
5393 }
5394
5395 if (var_start[varlen] == '[')
5396 {
5397 has_index = TRUE;
5398 if (type->tt_member == NULL)
5399 {
5400 semsg(_("E1088: cannot use an index on %s"), name);
5401 goto theend;
5402 }
5403 member_type = type->tt_member;
5404 }
5405 else
5406 {
5407 semsg("Not supported yet: %s", var_start);
5408 goto theend;
5409 }
5410 }
5411 else if (lvar == &arg_lvar)
5412 {
5413 semsg(_("E1090: Cannot assign to argument %s"), name);
5414 goto theend;
5415 }
5416
5417 if (!heredoc)
5418 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005419 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005420 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005421 if (oplen > 0 && var_count == 0)
5422 {
5423 // skip over the "=" and the expression
5424 p = skipwhite(op + oplen);
5425 compile_expr0(&p, cctx);
5426 }
5427 }
5428 else if (oplen > 0)
5429 {
5430 type_T *stacktype;
5431
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005432 // For "var = expr" evaluate the expression.
5433 if (var_count == 0)
5434 {
5435 int r;
5436
5437 // for "+=", "*=", "..=" etc. first load the current value
5438 if (*op != '=')
5439 {
5440 generate_loadvar(cctx, dest, name, lvar, type);
5441
5442 if (has_index)
5443 {
5444 // TODO: get member from list or dict
5445 emsg("Index with operation not supported yet");
5446 goto theend;
5447 }
5448 }
5449
5450 // Compile the expression. Temporarily hide the new local
5451 // variable here, it is not available to this expression.
5452 if (new_local)
5453 --cctx->ctx_locals.ga_len;
5454 instr_count = instr->ga_len;
5455 p = skipwhite(op + oplen);
5456 r = compile_expr0(&p, cctx);
5457 if (new_local)
5458 ++cctx->ctx_locals.ga_len;
5459 if (r == FAIL)
5460 goto theend;
5461 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005462 else if (semicolon && var_idx == var_count - 1)
5463 {
5464 // For "[var; var] = expr" get the rest of the list
5465 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5466 goto theend;
5467 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005468 else
5469 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005470 // For "[var, var] = expr" get the "var_idx" item from the
5471 // list.
5472 if (generate_GETITEM(cctx, var_idx) == FAIL)
5473 return FAIL;
5474 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005475
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005476 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005477 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005478 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005479 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005480 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005481 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005482 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005483 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005484 emsg(_(e_cannot_use_void));
5485 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005486 }
5487 else
5488 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005489 // An empty list or dict has a &t_void member,
5490 // for a variable that implies &t_any.
5491 if (stacktype == &t_list_empty)
5492 lvar->lv_type = &t_list_any;
5493 else if (stacktype == &t_dict_empty)
5494 lvar->lv_type = &t_dict_any;
5495 else
5496 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005497 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005498 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005499 else
5500 {
5501 type_T *use_type = lvar->lv_type;
5502
5503 if (has_index)
5504 {
5505 use_type = use_type->tt_member;
5506 if (use_type == NULL)
5507 use_type = &t_void;
5508 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005509 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005510 == FAIL)
5511 goto theend;
5512 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005513 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005514 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005515 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005516 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005517 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005518 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005520 emsg(_(e_const_req_value));
5521 goto theend;
5522 }
5523 else if (!has_type || dest == dest_option)
5524 {
5525 emsg(_(e_type_req));
5526 goto theend;
5527 }
5528 else
5529 {
5530 // variables are always initialized
5531 if (ga_grow(instr, 1) == FAIL)
5532 goto theend;
5533 switch (member_type->tt_type)
5534 {
5535 case VAR_BOOL:
5536 generate_PUSHBOOL(cctx, VVAL_FALSE);
5537 break;
5538 case VAR_FLOAT:
5539#ifdef FEAT_FLOAT
5540 generate_PUSHF(cctx, 0.0);
5541#endif
5542 break;
5543 case VAR_STRING:
5544 generate_PUSHS(cctx, NULL);
5545 break;
5546 case VAR_BLOB:
5547 generate_PUSHBLOB(cctx, NULL);
5548 break;
5549 case VAR_FUNC:
5550 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5551 break;
5552 case VAR_LIST:
5553 generate_NEWLIST(cctx, 0);
5554 break;
5555 case VAR_DICT:
5556 generate_NEWDICT(cctx, 0);
5557 break;
5558 case VAR_JOB:
5559 generate_PUSHJOB(cctx, NULL);
5560 break;
5561 case VAR_CHANNEL:
5562 generate_PUSHCHANNEL(cctx, NULL);
5563 break;
5564 case VAR_NUMBER:
5565 case VAR_UNKNOWN:
5566 case VAR_ANY:
5567 case VAR_PARTIAL:
5568 case VAR_VOID:
5569 case VAR_SPECIAL: // cannot happen
5570 generate_PUSHNR(cctx, 0);
5571 break;
5572 }
5573 }
5574 if (var_count == 0)
5575 end = p;
5576 }
5577
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005578 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005579 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005580 break;
5581
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005582 if (oplen > 0 && *op != '=')
5583 {
5584 type_T *expected = &t_number;
5585 type_T *stacktype;
5586
5587 // TODO: if type is known use float or any operation
5588
5589 if (*op == '.')
5590 expected = &t_string;
5591 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005592 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005593 goto theend;
5594
5595 if (*op == '.')
5596 generate_instr_drop(cctx, ISN_CONCAT, 1);
5597 else
5598 {
5599 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5600
5601 if (isn == NULL)
5602 goto theend;
5603 switch (*op)
5604 {
5605 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5606 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5607 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5608 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5609 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5610 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005611 }
5612 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005614 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005615 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005616 int r;
5617
5618 // Compile the "idx" in "var[idx]".
5619 if (new_local)
5620 --cctx->ctx_locals.ga_len;
5621 p = skipwhite(var_start + varlen + 1);
5622 r = compile_expr0(&p, cctx);
5623 if (new_local)
5624 ++cctx->ctx_locals.ga_len;
5625 if (r == FAIL)
5626 goto theend;
5627 if (*skipwhite(p) != ']')
5628 {
5629 emsg(_(e_missbrac));
5630 goto theend;
5631 }
5632 if (type->tt_type == VAR_DICT
5633 && may_generate_2STRING(-1, cctx) == FAIL)
5634 goto theend;
5635 if (type->tt_type == VAR_LIST
5636 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005637 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005638 {
5639 emsg(_(e_number_exp));
5640 goto theend;
5641 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005642
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005643 // Load the dict or list. On the stack we then have:
5644 // - value
5645 // - index
5646 // - variable
5647 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005648
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005649 if (type->tt_type == VAR_LIST)
5650 {
5651 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5652 return FAIL;
5653 }
5654 else if (type->tt_type == VAR_DICT)
5655 {
5656 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5657 return FAIL;
5658 }
5659 else
5660 {
5661 emsg(_(e_listreq));
5662 goto theend;
5663 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005664 }
5665 else
5666 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005667 switch (dest)
5668 {
5669 case dest_option:
5670 generate_STOREOPT(cctx, name + 1, opt_flags);
5671 break;
5672 case dest_global:
5673 // include g: with the name, easier to execute that way
5674 generate_STORE(cctx, ISN_STOREG, 0, name);
5675 break;
5676 case dest_buffer:
5677 // include b: with the name, easier to execute that way
5678 generate_STORE(cctx, ISN_STOREB, 0, name);
5679 break;
5680 case dest_window:
5681 // include w: with the name, easier to execute that way
5682 generate_STORE(cctx, ISN_STOREW, 0, name);
5683 break;
5684 case dest_tab:
5685 // include t: with the name, easier to execute that way
5686 generate_STORE(cctx, ISN_STORET, 0, name);
5687 break;
5688 case dest_env:
5689 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5690 break;
5691 case dest_reg:
5692 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5693 break;
5694 case dest_vimvar:
5695 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5696 break;
5697 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005698 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005699 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5700 imported_T *import = NULL;
5701 int sid = current_sctx.sc_sid;
5702 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005703
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005704 if (name[1] != ':')
5705 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005706 import = find_imported(name, 0, cctx);
5707 if (import != NULL)
5708 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005709 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005710
5711 idx = get_script_item_idx(sid, rawname, TRUE);
5712 // TODO: specific type
5713 if (idx < 0)
5714 {
5715 char_u *name_s = name;
5716
5717 // Include s: in the name for store_var()
5718 if (name[1] != ':')
5719 {
5720 int len = (int)STRLEN(name) + 3;
5721
5722 name_s = alloc(len);
5723 if (name_s == NULL)
5724 name_s = name;
5725 else
5726 vim_snprintf((char *)name_s, len,
5727 "s:%s", name);
5728 }
5729 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005730 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005731 if (name_s != name)
5732 vim_free(name_s);
5733 }
5734 else
5735 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005736 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005737 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005738 break;
5739 case dest_local:
5740 if (lvar != NULL)
5741 {
5742 isn_T *isn = ((isn_T *)instr->ga_data)
5743 + instr->ga_len - 1;
5744
5745 // optimization: turn "var = 123" from ISN_PUSHNR +
5746 // ISN_STORE into ISN_STORENR
5747 if (!lvar->lv_from_outer
5748 && instr->ga_len == instr_count + 1
5749 && isn->isn_type == ISN_PUSHNR)
5750 {
5751 varnumber_T val = isn->isn_arg.number;
5752
5753 isn->isn_type = ISN_STORENR;
5754 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5755 isn->isn_arg.storenr.stnr_val = val;
5756 if (stack->ga_len > 0)
5757 --stack->ga_len;
5758 }
5759 else if (lvar->lv_from_outer)
5760 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005761 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005762 else
5763 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5764 }
5765 break;
5766 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005767 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005768
5769 if (var_idx + 1 < var_count)
5770 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005772
5773 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005774 if (var_count > 0 && !semicolon)
5775 {
5776 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5777 goto theend;
5778 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005779
Bram Moolenaarb2097502020-07-19 17:17:02 +02005780 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005781
5782theend:
5783 vim_free(name);
5784 return ret;
5785}
5786
5787/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005788 * Check if "name" can be "unlet".
5789 */
5790 int
5791check_vim9_unlet(char_u *name)
5792{
5793 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5794 {
5795 semsg(_("E1081: Cannot unlet %s"), name);
5796 return FAIL;
5797 }
5798 return OK;
5799}
5800
5801/*
5802 * Callback passed to ex_unletlock().
5803 */
5804 static int
5805compile_unlet(
5806 lval_T *lvp,
5807 char_u *name_end,
5808 exarg_T *eap,
5809 int deep UNUSED,
5810 void *coookie)
5811{
5812 cctx_T *cctx = coookie;
5813
5814 if (lvp->ll_tv == NULL)
5815 {
5816 char_u *p = lvp->ll_name;
5817 int cc = *name_end;
5818 int ret = OK;
5819
5820 // Normal name. Only supports g:, w:, t: and b: namespaces.
5821 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005822 if (*p == '$')
5823 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5824 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005825 ret = FAIL;
5826 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005827 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005828
5829 *name_end = cc;
5830 return ret;
5831 }
5832
5833 // TODO: unlet {list}[idx]
5834 // TODO: unlet {dict}[key]
5835 emsg("Sorry, :unlet not fully implemented yet");
5836 return FAIL;
5837}
5838
5839/*
5840 * compile "unlet var", "lock var" and "unlock var"
5841 * "arg" points to "var".
5842 */
5843 static char_u *
5844compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5845{
5846 char_u *p = arg;
5847
5848 if (eap->cmdidx != CMD_unlet)
5849 {
5850 emsg("Sorry, :lock and unlock not implemented yet");
5851 return NULL;
5852 }
5853
5854 if (*p == '!')
5855 {
5856 p = skipwhite(p + 1);
5857 eap->forceit = TRUE;
5858 }
5859
5860 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5861 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5862}
5863
5864/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005865 * Compile an :import command.
5866 */
5867 static char_u *
5868compile_import(char_u *arg, cctx_T *cctx)
5869{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005870 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005871}
5872
5873/*
5874 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5875 */
5876 static int
5877compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5878{
5879 garray_T *instr = &cctx->ctx_instr;
5880 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5881
5882 if (endlabel == NULL)
5883 return FAIL;
5884 endlabel->el_next = *el;
5885 *el = endlabel;
5886 endlabel->el_end_label = instr->ga_len;
5887
5888 generate_JUMP(cctx, when, 0);
5889 return OK;
5890}
5891
5892 static void
5893compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5894{
5895 garray_T *instr = &cctx->ctx_instr;
5896
5897 while (*el != NULL)
5898 {
5899 endlabel_T *cur = (*el);
5900 isn_T *isn;
5901
5902 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5903 isn->isn_arg.jump.jump_where = instr->ga_len;
5904 *el = cur->el_next;
5905 vim_free(cur);
5906 }
5907}
5908
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005909 static void
5910compile_free_jump_to_end(endlabel_T **el)
5911{
5912 while (*el != NULL)
5913 {
5914 endlabel_T *cur = (*el);
5915
5916 *el = cur->el_next;
5917 vim_free(cur);
5918 }
5919}
5920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005921/*
5922 * Create a new scope and set up the generic items.
5923 */
5924 static scope_T *
5925new_scope(cctx_T *cctx, scopetype_T type)
5926{
5927 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5928
5929 if (scope == NULL)
5930 return NULL;
5931 scope->se_outer = cctx->ctx_scope;
5932 cctx->ctx_scope = scope;
5933 scope->se_type = type;
5934 scope->se_local_count = cctx->ctx_locals.ga_len;
5935 return scope;
5936}
5937
5938/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005939 * Free the current scope and go back to the outer scope.
5940 */
5941 static void
5942drop_scope(cctx_T *cctx)
5943{
5944 scope_T *scope = cctx->ctx_scope;
5945
5946 if (scope == NULL)
5947 {
5948 iemsg("calling drop_scope() without a scope");
5949 return;
5950 }
5951 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005952 switch (scope->se_type)
5953 {
5954 case IF_SCOPE:
5955 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5956 case FOR_SCOPE:
5957 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5958 case WHILE_SCOPE:
5959 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5960 case TRY_SCOPE:
5961 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5962 case NO_SCOPE:
5963 case BLOCK_SCOPE:
5964 break;
5965 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005966 vim_free(scope);
5967}
5968
5969/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 * compile "if expr"
5971 *
5972 * "if expr" Produces instructions:
5973 * EVAL expr Push result of "expr"
5974 * JUMP_IF_FALSE end
5975 * ... body ...
5976 * end:
5977 *
5978 * "if expr | else" Produces instructions:
5979 * EVAL expr Push result of "expr"
5980 * JUMP_IF_FALSE else
5981 * ... body ...
5982 * JUMP_ALWAYS end
5983 * else:
5984 * ... body ...
5985 * end:
5986 *
5987 * "if expr1 | elseif expr2 | else" Produces instructions:
5988 * EVAL expr Push result of "expr"
5989 * JUMP_IF_FALSE elseif
5990 * ... body ...
5991 * JUMP_ALWAYS end
5992 * elseif:
5993 * EVAL expr Push result of "expr"
5994 * JUMP_IF_FALSE else
5995 * ... body ...
5996 * JUMP_ALWAYS end
5997 * else:
5998 * ... body ...
5999 * end:
6000 */
6001 static char_u *
6002compile_if(char_u *arg, cctx_T *cctx)
6003{
6004 char_u *p = arg;
6005 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006006 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006008 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006009 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006010
Bram Moolenaara5565e42020-05-09 15:44:01 +02006011 CLEAR_FIELD(ppconst);
6012 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006013 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006014 clear_ppconst(&ppconst);
6015 return NULL;
6016 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006017 if (cctx->ctx_skip == SKIP_YES)
6018 clear_ppconst(&ppconst);
6019 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006020 {
6021 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006022 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006023 clear_ppconst(&ppconst);
6024 }
6025 else
6026 {
6027 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006028 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006029 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006030 return NULL;
6031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006032
6033 scope = new_scope(cctx, IF_SCOPE);
6034 if (scope == NULL)
6035 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006036 scope->se_skip_save = skip_save;
6037 // "is_had_return" will be reset if any block does not end in :return
6038 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006039
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006040 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006041 {
6042 // "where" is set when ":elseif", "else" or ":endif" is found
6043 scope->se_u.se_if.is_if_label = instr->ga_len;
6044 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6045 }
6046 else
6047 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048
6049 return p;
6050}
6051
6052 static char_u *
6053compile_elseif(char_u *arg, cctx_T *cctx)
6054{
6055 char_u *p = arg;
6056 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006057 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058 isn_T *isn;
6059 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006060 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061
6062 if (scope == NULL || scope->se_type != IF_SCOPE)
6063 {
6064 emsg(_(e_elseif_without_if));
6065 return NULL;
6066 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006067 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006068 if (!cctx->ctx_had_return)
6069 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006070
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006071 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006072 {
6073 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006075 return NULL;
6076 // previous "if" or "elseif" jumps here
6077 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6078 isn->isn_arg.jump.jump_where = instr->ga_len;
6079 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006081 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006082 CLEAR_FIELD(ppconst);
6083 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006084 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006085 clear_ppconst(&ppconst);
6086 return NULL;
6087 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006088 if (scope->se_skip_save == SKIP_YES)
6089 clear_ppconst(&ppconst);
6090 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006091 {
6092 // The expression results in a constant.
6093 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006094 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006095 clear_ppconst(&ppconst);
6096 scope->se_u.se_if.is_if_label = -1;
6097 }
6098 else
6099 {
6100 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006101 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006102 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006103 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006104
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006105 // "where" is set when ":elseif", "else" or ":endif" is found
6106 scope->se_u.se_if.is_if_label = instr->ga_len;
6107 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109
6110 return p;
6111}
6112
6113 static char_u *
6114compile_else(char_u *arg, cctx_T *cctx)
6115{
6116 char_u *p = arg;
6117 garray_T *instr = &cctx->ctx_instr;
6118 isn_T *isn;
6119 scope_T *scope = cctx->ctx_scope;
6120
6121 if (scope == NULL || scope->se_type != IF_SCOPE)
6122 {
6123 emsg(_(e_else_without_if));
6124 return NULL;
6125 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006126 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006127 if (!cctx->ctx_had_return)
6128 scope->se_u.se_if.is_had_return = FALSE;
6129 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130
Bram Moolenaarefd88552020-06-18 20:50:10 +02006131 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006132 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006133 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006134 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006135 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006136 if (!cctx->ctx_had_return
6137 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6138 JUMP_ALWAYS, cctx) == FAIL)
6139 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006140 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006141
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006142 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006143 {
6144 if (scope->se_u.se_if.is_if_label >= 0)
6145 {
6146 // previous "if" or "elseif" jumps here
6147 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6148 isn->isn_arg.jump.jump_where = instr->ga_len;
6149 scope->se_u.se_if.is_if_label = -1;
6150 }
6151 }
6152
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006153 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006154 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156
6157 return p;
6158}
6159
6160 static char_u *
6161compile_endif(char_u *arg, cctx_T *cctx)
6162{
6163 scope_T *scope = cctx->ctx_scope;
6164 ifscope_T *ifscope;
6165 garray_T *instr = &cctx->ctx_instr;
6166 isn_T *isn;
6167
6168 if (scope == NULL || scope->se_type != IF_SCOPE)
6169 {
6170 emsg(_(e_endif_without_if));
6171 return NULL;
6172 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006173 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006174 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006175 if (!cctx->ctx_had_return)
6176 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006178 if (scope->se_u.se_if.is_if_label >= 0)
6179 {
6180 // previous "if" or "elseif" jumps here
6181 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6182 isn->isn_arg.jump.jump_where = instr->ga_len;
6183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006184 // Fill in the "end" label in jumps at the end of the blocks.
6185 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006186 cctx->ctx_skip = scope->se_skip_save;
6187
6188 // If all the blocks end in :return and there is an :else then the
6189 // had_return flag is set.
6190 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006191
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006192 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193 return arg;
6194}
6195
6196/*
6197 * compile "for var in expr"
6198 *
6199 * Produces instructions:
6200 * PUSHNR -1
6201 * STORE loop-idx Set index to -1
6202 * EVAL expr Push result of "expr"
6203 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6204 * - if beyond end, jump to "end"
6205 * - otherwise get item from list and push it
6206 * STORE var Store item in "var"
6207 * ... body ...
6208 * JUMP top Jump back to repeat
6209 * end: DROP Drop the result of "expr"
6210 *
6211 */
6212 static char_u *
6213compile_for(char_u *arg, cctx_T *cctx)
6214{
6215 char_u *p;
6216 size_t varlen;
6217 garray_T *instr = &cctx->ctx_instr;
6218 garray_T *stack = &cctx->ctx_type_stack;
6219 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006220 lvar_T *loop_lvar; // loop iteration variable
6221 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 type_T *vartype;
6223
6224 // TODO: list of variables: "for [key, value] in dict"
6225 // parse "var"
6226 for (p = arg; eval_isnamec1(*p); ++p)
6227 ;
6228 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006229 var_lvar = lookup_local(arg, varlen, cctx);
6230 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006231 {
6232 semsg(_("E1023: variable already defined: %s"), arg);
6233 return NULL;
6234 }
6235
6236 // consume "in"
6237 p = skipwhite(p);
6238 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6239 {
6240 emsg(_(e_missing_in));
6241 return NULL;
6242 }
6243 p = skipwhite(p + 2);
6244
6245
6246 scope = new_scope(cctx, FOR_SCOPE);
6247 if (scope == NULL)
6248 return NULL;
6249
6250 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006251 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6252 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006253 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006254 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006255 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006258
6259 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006260 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6261 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006262 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006263 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006264 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006266 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006268 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006269
6270 // compile "expr", it remains on the stack until "endfor"
6271 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006272 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006273 {
6274 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006276 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006278 // Now that we know the type of "var", check that it is a list, now or at
6279 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006280 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006281 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006283 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284 return NULL;
6285 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006286 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006287 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006288
6289 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006290 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006292 generate_FOR(cctx, loop_lvar->lv_idx);
6293 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294
6295 return arg;
6296}
6297
6298/*
6299 * compile "endfor"
6300 */
6301 static char_u *
6302compile_endfor(char_u *arg, cctx_T *cctx)
6303{
6304 garray_T *instr = &cctx->ctx_instr;
6305 scope_T *scope = cctx->ctx_scope;
6306 forscope_T *forscope;
6307 isn_T *isn;
6308
6309 if (scope == NULL || scope->se_type != FOR_SCOPE)
6310 {
6311 emsg(_(e_for));
6312 return NULL;
6313 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006314 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006316 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317
6318 // At end of ":for" scope jump back to the FOR instruction.
6319 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6320
6321 // Fill in the "end" label in the FOR statement so it can jump here
6322 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6323 isn->isn_arg.forloop.for_end = instr->ga_len;
6324
6325 // Fill in the "end" label any BREAK statements
6326 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6327
6328 // Below the ":for" scope drop the "expr" list from the stack.
6329 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6330 return NULL;
6331
6332 vim_free(scope);
6333
6334 return arg;
6335}
6336
6337/*
6338 * compile "while expr"
6339 *
6340 * Produces instructions:
6341 * top: EVAL expr Push result of "expr"
6342 * JUMP_IF_FALSE end jump if false
6343 * ... body ...
6344 * JUMP top Jump back to repeat
6345 * end:
6346 *
6347 */
6348 static char_u *
6349compile_while(char_u *arg, cctx_T *cctx)
6350{
6351 char_u *p = arg;
6352 garray_T *instr = &cctx->ctx_instr;
6353 scope_T *scope;
6354
6355 scope = new_scope(cctx, WHILE_SCOPE);
6356 if (scope == NULL)
6357 return NULL;
6358
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006359 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006360
6361 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006362 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006363 return NULL;
6364
6365 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006366 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006367 JUMP_IF_FALSE, cctx) == FAIL)
6368 return FAIL;
6369
6370 return p;
6371}
6372
6373/*
6374 * compile "endwhile"
6375 */
6376 static char_u *
6377compile_endwhile(char_u *arg, cctx_T *cctx)
6378{
6379 scope_T *scope = cctx->ctx_scope;
6380
6381 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6382 {
6383 emsg(_(e_while));
6384 return NULL;
6385 }
6386 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006387 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006388
6389 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006390 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006391
6392 // Fill in the "end" label in the WHILE statement so it can jump here.
6393 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006394 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006395
6396 vim_free(scope);
6397
6398 return arg;
6399}
6400
6401/*
6402 * compile "continue"
6403 */
6404 static char_u *
6405compile_continue(char_u *arg, cctx_T *cctx)
6406{
6407 scope_T *scope = cctx->ctx_scope;
6408
6409 for (;;)
6410 {
6411 if (scope == NULL)
6412 {
6413 emsg(_(e_continue));
6414 return NULL;
6415 }
6416 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6417 break;
6418 scope = scope->se_outer;
6419 }
6420
6421 // Jump back to the FOR or WHILE instruction.
6422 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006423 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6424 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006425 return arg;
6426}
6427
6428/*
6429 * compile "break"
6430 */
6431 static char_u *
6432compile_break(char_u *arg, cctx_T *cctx)
6433{
6434 scope_T *scope = cctx->ctx_scope;
6435 endlabel_T **el;
6436
6437 for (;;)
6438 {
6439 if (scope == NULL)
6440 {
6441 emsg(_(e_break));
6442 return NULL;
6443 }
6444 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6445 break;
6446 scope = scope->se_outer;
6447 }
6448
6449 // Jump to the end of the FOR or WHILE loop.
6450 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006451 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006452 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006453 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6455 return FAIL;
6456
6457 return arg;
6458}
6459
6460/*
6461 * compile "{" start of block
6462 */
6463 static char_u *
6464compile_block(char_u *arg, cctx_T *cctx)
6465{
6466 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6467 return NULL;
6468 return skipwhite(arg + 1);
6469}
6470
6471/*
6472 * compile end of block: drop one scope
6473 */
6474 static void
6475compile_endblock(cctx_T *cctx)
6476{
6477 scope_T *scope = cctx->ctx_scope;
6478
6479 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006480 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006481 vim_free(scope);
6482}
6483
6484/*
6485 * compile "try"
6486 * Creates a new scope for the try-endtry, pointing to the first catch and
6487 * finally.
6488 * Creates another scope for the "try" block itself.
6489 * TRY instruction sets up exception handling at runtime.
6490 *
6491 * "try"
6492 * TRY -> catch1, -> finally push trystack entry
6493 * ... try block
6494 * "throw {exception}"
6495 * EVAL {exception}
6496 * THROW create exception
6497 * ... try block
6498 * " catch {expr}"
6499 * JUMP -> finally
6500 * catch1: PUSH exeception
6501 * EVAL {expr}
6502 * MATCH
6503 * JUMP nomatch -> catch2
6504 * CATCH remove exception
6505 * ... catch block
6506 * " catch"
6507 * JUMP -> finally
6508 * catch2: CATCH remove exception
6509 * ... catch block
6510 * " finally"
6511 * finally:
6512 * ... finally block
6513 * " endtry"
6514 * ENDTRY pop trystack entry, may rethrow
6515 */
6516 static char_u *
6517compile_try(char_u *arg, cctx_T *cctx)
6518{
6519 garray_T *instr = &cctx->ctx_instr;
6520 scope_T *try_scope;
6521 scope_T *scope;
6522
6523 // scope that holds the jumps that go to catch/finally/endtry
6524 try_scope = new_scope(cctx, TRY_SCOPE);
6525 if (try_scope == NULL)
6526 return NULL;
6527
6528 // "catch" is set when the first ":catch" is found.
6529 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006530 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006531 if (generate_instr(cctx, ISN_TRY) == NULL)
6532 return NULL;
6533
6534 // scope for the try block itself
6535 scope = new_scope(cctx, BLOCK_SCOPE);
6536 if (scope == NULL)
6537 return NULL;
6538
6539 return arg;
6540}
6541
6542/*
6543 * compile "catch {expr}"
6544 */
6545 static char_u *
6546compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6547{
6548 scope_T *scope = cctx->ctx_scope;
6549 garray_T *instr = &cctx->ctx_instr;
6550 char_u *p;
6551 isn_T *isn;
6552
6553 // end block scope from :try or :catch
6554 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6555 compile_endblock(cctx);
6556 scope = cctx->ctx_scope;
6557
6558 // Error if not in a :try scope
6559 if (scope == NULL || scope->se_type != TRY_SCOPE)
6560 {
6561 emsg(_(e_catch));
6562 return NULL;
6563 }
6564
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006565 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 {
6567 emsg(_("E1033: catch unreachable after catch-all"));
6568 return NULL;
6569 }
6570
6571 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006572 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 JUMP_ALWAYS, cctx) == FAIL)
6574 return NULL;
6575
6576 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006577 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 if (isn->isn_arg.try.try_catch == 0)
6579 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006580 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006581 {
6582 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006583 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006584 isn->isn_arg.jump.jump_where = instr->ga_len;
6585 }
6586
6587 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006588 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006590 scope->se_u.se_try.ts_caught_all = TRUE;
6591 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592 }
6593 else
6594 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006595 char_u *end;
6596 char_u *pat;
6597 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006598 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006599 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 // Push v:exception, push {expr} and MATCH
6602 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6603
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006604 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006605 if (*end != *p)
6606 {
6607 semsg(_("E1067: Separator mismatch: %s"), p);
6608 vim_free(tofree);
6609 return FAIL;
6610 }
6611 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006612 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006613 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006614 len = (int)(end - tofree);
6615 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006616 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006617 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006618 if (pat == NULL)
6619 return FAIL;
6620 if (generate_PUSHS(cctx, pat) == FAIL)
6621 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006623 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6624 return NULL;
6625
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006626 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006627 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6628 return NULL;
6629 }
6630
6631 if (generate_instr(cctx, ISN_CATCH) == NULL)
6632 return NULL;
6633
6634 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6635 return NULL;
6636 return p;
6637}
6638
6639 static char_u *
6640compile_finally(char_u *arg, cctx_T *cctx)
6641{
6642 scope_T *scope = cctx->ctx_scope;
6643 garray_T *instr = &cctx->ctx_instr;
6644 isn_T *isn;
6645
6646 // end block scope from :try or :catch
6647 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6648 compile_endblock(cctx);
6649 scope = cctx->ctx_scope;
6650
6651 // Error if not in a :try scope
6652 if (scope == NULL || scope->se_type != TRY_SCOPE)
6653 {
6654 emsg(_(e_finally));
6655 return NULL;
6656 }
6657
6658 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006659 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660 if (isn->isn_arg.try.try_finally != 0)
6661 {
6662 emsg(_(e_finally_dup));
6663 return NULL;
6664 }
6665
6666 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006667 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006668
Bram Moolenaar585fea72020-04-02 22:33:21 +02006669 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006670 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006671 {
6672 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006673 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006675 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676 }
6677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 // TODO: set index in ts_finally_label jumps
6679
6680 return arg;
6681}
6682
6683 static char_u *
6684compile_endtry(char_u *arg, cctx_T *cctx)
6685{
6686 scope_T *scope = cctx->ctx_scope;
6687 garray_T *instr = &cctx->ctx_instr;
6688 isn_T *isn;
6689
6690 // end block scope from :catch or :finally
6691 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6692 compile_endblock(cctx);
6693 scope = cctx->ctx_scope;
6694
6695 // Error if not in a :try scope
6696 if (scope == NULL || scope->se_type != TRY_SCOPE)
6697 {
6698 if (scope == NULL)
6699 emsg(_(e_no_endtry));
6700 else if (scope->se_type == WHILE_SCOPE)
6701 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006702 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703 emsg(_(e_endfor));
6704 else
6705 emsg(_(e_endif));
6706 return NULL;
6707 }
6708
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006709 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6711 {
6712 emsg(_("E1032: missing :catch or :finally"));
6713 return NULL;
6714 }
6715
6716 // Fill in the "end" label in jumps at the end of the blocks, if not done
6717 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006718 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719
6720 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006721 if (isn->isn_arg.try.try_catch == 0)
6722 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723 if (isn->isn_arg.try.try_finally == 0)
6724 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006725
6726 if (scope->se_u.se_try.ts_catch_label != 0)
6727 {
6728 // Last catch without match jumps here
6729 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6730 isn->isn_arg.jump.jump_where = instr->ga_len;
6731 }
6732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006733 compile_endblock(cctx);
6734
6735 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6736 return NULL;
6737 return arg;
6738}
6739
6740/*
6741 * compile "throw {expr}"
6742 */
6743 static char_u *
6744compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6745{
6746 char_u *p = skipwhite(arg);
6747
Bram Moolenaara5565e42020-05-09 15:44:01 +02006748 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749 return NULL;
6750 if (may_generate_2STRING(-1, cctx) == FAIL)
6751 return NULL;
6752 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6753 return NULL;
6754
6755 return p;
6756}
6757
6758/*
6759 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006760 * compile "echomsg expr"
6761 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006762 * compile "execute expr"
6763 */
6764 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006765compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006766{
6767 char_u *p = arg;
6768 int count = 0;
6769
6770 for (;;)
6771 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006772 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006773 return NULL;
6774 ++count;
6775 p = skipwhite(p);
6776 if (ends_excmd(*p))
6777 break;
6778 }
6779
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006780 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6781 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6782 else if (cmdidx == CMD_execute)
6783 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6784 else if (cmdidx == CMD_echomsg)
6785 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6786 else
6787 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006788 return p;
6789}
6790
6791/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006792 * A command that is not compiled, execute with legacy code.
6793 */
6794 static char_u *
6795compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6796{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006797 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006798 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006799 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006800
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006801 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006802 goto theend;
6803
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006804 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006805 {
6806 long argt = excmd_get_argt(eap->cmdidx);
6807 int usefilter = FALSE;
6808
6809 has_expr = argt & (EX_XFILE | EX_EXPAND);
6810
6811 // If the command can be followed by a bar, find the bar and truncate
6812 // it, so that the following command can be compiled.
6813 // The '|' is overwritten with a NUL, it is put back below.
6814 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6815 && *eap->arg == '!')
6816 // :w !filter or :r !filter or :r! filter
6817 usefilter = TRUE;
6818 if ((argt & EX_TRLBAR) && !usefilter)
6819 {
6820 separate_nextcmd(eap);
6821 if (eap->nextcmd != NULL)
6822 nextcmd = eap->nextcmd;
6823 }
6824 }
6825
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006826 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6827 {
6828 // expand filename in "syntax include [@group] filename"
6829 has_expr = TRUE;
6830 eap->arg = skipwhite(eap->arg + 7);
6831 if (*eap->arg == '@')
6832 eap->arg = skiptowhite(eap->arg);
6833 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006834
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006835 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006836 {
6837 int count = 0;
6838 char_u *start = skipwhite(line);
6839
6840 // :cmd xxx`=expr1`yyy`=expr2`zzz
6841 // PUSHS ":cmd xxx"
6842 // eval expr1
6843 // PUSHS "yyy"
6844 // eval expr2
6845 // PUSHS "zzz"
6846 // EXECCONCAT 5
6847 for (;;)
6848 {
6849 if (p > start)
6850 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006851 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006852 ++count;
6853 }
6854 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006855 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006856 return NULL;
6857 may_generate_2STRING(-1, cctx);
6858 ++count;
6859 p = skipwhite(p);
6860 if (*p != '`')
6861 {
6862 emsg(_("E1083: missing backtick"));
6863 return NULL;
6864 }
6865 start = p + 1;
6866
6867 p = (char_u *)strstr((char *)start, "`=");
6868 if (p == NULL)
6869 {
6870 if (*skipwhite(start) != NUL)
6871 {
6872 generate_PUSHS(cctx, vim_strsave(start));
6873 ++count;
6874 }
6875 break;
6876 }
6877 }
6878 generate_EXECCONCAT(cctx, count);
6879 }
6880 else
6881 generate_EXEC(cctx, line);
6882
6883theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006884 if (*nextcmd != NUL)
6885 {
6886 // the parser expects a pointer to the bar, put it back
6887 --nextcmd;
6888 *nextcmd = '|';
6889 }
6890
6891 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006892}
6893
6894/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006895 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006896 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006897 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006898 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006899add_def_function(ufunc_T *ufunc)
6900{
6901 dfunc_T *dfunc;
6902
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006903 if (def_functions.ga_len == 0)
6904 {
6905 // The first position is not used, so that a zero uf_dfunc_idx means it
6906 // wasn't set.
6907 if (ga_grow(&def_functions, 1) == FAIL)
6908 return FAIL;
6909 ++def_functions.ga_len;
6910 }
6911
Bram Moolenaar09689a02020-05-09 22:50:08 +02006912 // Add the function to "def_functions".
6913 if (ga_grow(&def_functions, 1) == FAIL)
6914 return FAIL;
6915 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6916 CLEAR_POINTER(dfunc);
6917 dfunc->df_idx = def_functions.ga_len;
6918 ufunc->uf_dfunc_idx = dfunc->df_idx;
6919 dfunc->df_ufunc = ufunc;
6920 ++def_functions.ga_len;
6921 return OK;
6922}
6923
6924/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006925 * After ex_function() has collected all the function lines: parse and compile
6926 * the lines into instructions.
6927 * Adds the function to "def_functions".
6928 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6929 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006930 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006931 * This can be used recursively through compile_lambda(), which may reallocate
6932 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006933 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006935 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006936compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 char_u *line = NULL;
6939 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 cctx_T cctx;
6942 garray_T *instr;
6943 int called_emsg_before = called_emsg;
6944 int ret = FAIL;
6945 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006946 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006947 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006949 // When using a function that was compiled before: Free old instructions.
6950 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006951 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006953 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6954 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006955 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006957 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006958 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959
Bram Moolenaar985116a2020-07-12 17:31:09 +02006960 ufunc->uf_def_status = UF_COMPILING;
6961
Bram Moolenaara80faa82020-04-12 19:37:17 +02006962 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963 cctx.ctx_ufunc = ufunc;
6964 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006965 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6967 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6968 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6969 cctx.ctx_type_list = &ufunc->uf_type_list;
6970 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6971 instr = &cctx.ctx_instr;
6972
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006973 // Set the context to the function, it may be compiled when called from
6974 // another script. Set the script version to the most modern one.
6975 // The line number will be set in next_line_from_context().
6976 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6978
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006979 // Make sure error messages are OK.
6980 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6981 if (do_estack_push)
6982 estack_push_ufunc(ufunc, 1);
6983
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006984 if (ufunc->uf_def_args.ga_len > 0)
6985 {
6986 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006987 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006988 int i;
6989 char_u *arg;
6990 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6991
6992 // Produce instructions for the default values of optional arguments.
6993 // Store the instruction index in uf_def_arg_idx[] so that we know
6994 // where to start when the function is called, depending on the number
6995 // of arguments.
6996 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6997 if (ufunc->uf_def_arg_idx == NULL)
6998 goto erret;
6999 for (i = 0; i < count; ++i)
7000 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007001 garray_T *stack = &cctx.ctx_type_stack;
7002 type_T *val_type;
7003 int arg_idx = first_def_arg + i;
7004
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007005 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7006 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007007 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007008 goto erret;
7009
7010 // If no type specified use the type of the default value.
7011 // Otherwise check that the default value type matches the
7012 // specified type.
7013 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7014 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
7015 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaare30f64b2020-07-15 19:48:20 +02007016 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007017 == FAIL)
7018 {
7019 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
7020 arg_idx + 1);
7021 goto erret;
7022 }
7023
7024 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007025 goto erret;
7026 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007027 ufunc->uf_def_arg_idx[count] = instr->ga_len;
7028 }
7029
7030 /*
7031 * Loop over all the lines of the function and generate instructions.
7032 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033 for (;;)
7034 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007035 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007036 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007037 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007038 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007039
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007040 // Bail out on the first error to avoid a flood of errors and report
7041 // the right line number when inside try/catch.
7042 if (emsg_before != called_emsg)
7043 goto erret;
7044
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045 if (line != NULL && *line == '|')
7046 // the line continues after a '|'
7047 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007048 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007049 && !(*line == '#' && (line == cctx.ctx_line_start
7050 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007052 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 goto erret;
7054 }
7055 else
7056 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007057 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007058 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007059 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007062 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063
Bram Moolenaara80faa82020-04-12 19:37:17 +02007064 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007065 ea.cmdlinep = &line;
7066 ea.cmd = skipwhite(line);
7067
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007068 // Some things can be recognized by the first character.
7069 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007070 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007071 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007072 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007073 if (ea.cmd[1] != '{')
7074 {
7075 line = (char_u *)"";
7076 continue;
7077 }
7078 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007080 case '}':
7081 {
7082 // "}" ends a block scope
7083 scopetype_T stype = cctx.ctx_scope == NULL
7084 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007085
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007086 if (stype == BLOCK_SCOPE)
7087 {
7088 compile_endblock(&cctx);
7089 line = ea.cmd;
7090 }
7091 else
7092 {
7093 emsg(_("E1025: using } outside of a block scope"));
7094 goto erret;
7095 }
7096 if (line != NULL)
7097 line = skipwhite(ea.cmd + 1);
7098 continue;
7099 }
7100
7101 case '{':
7102 // "{" starts a block scope
7103 // "{'a': 1}->func() is something else
7104 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7105 {
7106 line = compile_block(ea.cmd, &cctx);
7107 continue;
7108 }
7109 break;
7110
7111 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007112 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007113 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114 }
7115
7116 /*
7117 * COMMAND MODIFIERS
7118 */
7119 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7120 {
7121 if (errormsg != NULL)
7122 goto erret;
7123 // empty line or comment
7124 line = (char_u *)"";
7125 continue;
7126 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007127 // TODO: use modifiers in the command
7128 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007129 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130
7131 // Skip ":call" to get to the function name.
7132 if (checkforcmd(&ea.cmd, "call", 3))
7133 ea.cmd = skipwhite(ea.cmd);
7134
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007135 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007137 char_u *pskip;
7138
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007139 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007140 // find what follows.
7141 // Skip over "var.member", "var[idx]" and the like.
7142 // Also "&opt = val", "$ENV = val" and "@r = val".
7143 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007144 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007145 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007146 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007148 char_u *var_end;
7149 int oplen;
7150 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007152 var_end = find_name_end(pskip, NULL, NULL,
7153 FNE_CHECK_START | FNE_INCL_BR);
7154 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007155 if (oplen > 0)
7156 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007157 size_t len = p - ea.cmd;
7158
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007159 // Recognize an assignment if we recognize the variable
7160 // name:
7161 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007162 // "local = expr" where "local" is a local var.
7163 // "script = expr" where "script" is a script-local var.
7164 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007165 // "&opt = expr"
7166 // "$ENV = expr"
7167 // "@r = expr"
7168 if (*ea.cmd == '&'
7169 || *ea.cmd == '$'
7170 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007171 || ((len) > 2 && ea.cmd[1] == ':')
7172 || lookup_local(ea.cmd, len, &cctx) != NULL
7173 || lookup_arg(ea.cmd, len, NULL, NULL,
7174 NULL, &cctx) == OK
7175 || lookup_script(ea.cmd, len) == OK
7176 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007177 {
7178 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007179 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007180 goto erret;
7181 continue;
7182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183 }
7184 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007185
7186 if (*ea.cmd == '[')
7187 {
7188 // [var, var] = expr
7189 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7190 if (line == NULL)
7191 goto erret;
7192 if (line != ea.cmd)
7193 continue;
7194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 }
7196
7197 /*
7198 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007199 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007200 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007201 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007202 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007203 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007204 ea.cmd = skip_range(ea.cmd, NULL);
7205 if (ea.cmd > cmd && !starts_with_colon)
7206 {
7207 emsg(_(e_colon_required));
7208 goto erret;
7209 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007210 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007211 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007212 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007213 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214
7215 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7216 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007217 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007218 {
7219 line += STRLEN(line);
7220 continue;
7221 }
7222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007223 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007224 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007226 // CMD_let cannot happen, compile_assignment() above is used
7227 iemsg("Command from find_ex_command() not handled");
7228 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007229 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007230 }
7231
7232 p = skipwhite(p);
7233
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007234 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007235 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007236 && ea.cmdidx != CMD_elseif
7237 && ea.cmdidx != CMD_else
7238 && ea.cmdidx != CMD_endif)
7239 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007240 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007241 continue;
7242 }
7243
Bram Moolenaarefd88552020-06-18 20:50:10 +02007244 if (ea.cmdidx != CMD_elseif
7245 && ea.cmdidx != CMD_else
7246 && ea.cmdidx != CMD_endif
7247 && ea.cmdidx != CMD_endfor
7248 && ea.cmdidx != CMD_endwhile
7249 && ea.cmdidx != CMD_catch
7250 && ea.cmdidx != CMD_finally
7251 && ea.cmdidx != CMD_endtry)
7252 {
7253 if (cctx.ctx_had_return)
7254 {
7255 emsg(_("E1095: Unreachable code after :return"));
7256 goto erret;
7257 }
7258 }
7259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260 switch (ea.cmdidx)
7261 {
7262 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007263 ea.arg = p;
7264 line = compile_nested_function(&ea, &cctx);
7265 break;
7266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007267 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007268 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269 goto erret;
7270
7271 case CMD_return:
7272 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007273 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 break;
7275
7276 case CMD_let:
7277 case CMD_const:
7278 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007279 if (line == p)
7280 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 break;
7282
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007283 case CMD_unlet:
7284 case CMD_unlockvar:
7285 case CMD_lockvar:
7286 line = compile_unletlock(p, &ea, &cctx);
7287 break;
7288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007289 case CMD_import:
7290 line = compile_import(p, &cctx);
7291 break;
7292
7293 case CMD_if:
7294 line = compile_if(p, &cctx);
7295 break;
7296 case CMD_elseif:
7297 line = compile_elseif(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_else:
7301 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007302 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007303 break;
7304 case CMD_endif:
7305 line = compile_endif(p, &cctx);
7306 break;
7307
7308 case CMD_while:
7309 line = compile_while(p, &cctx);
7310 break;
7311 case CMD_endwhile:
7312 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007313 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 break;
7315
7316 case CMD_for:
7317 line = compile_for(p, &cctx);
7318 break;
7319 case CMD_endfor:
7320 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007321 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 break;
7323 case CMD_continue:
7324 line = compile_continue(p, &cctx);
7325 break;
7326 case CMD_break:
7327 line = compile_break(p, &cctx);
7328 break;
7329
7330 case CMD_try:
7331 line = compile_try(p, &cctx);
7332 break;
7333 case CMD_catch:
7334 line = compile_catch(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_finally:
7338 line = compile_finally(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_endtry:
7342 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007343 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 break;
7345 case CMD_throw:
7346 line = compile_throw(p, &cctx);
7347 break;
7348
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007349 case CMD_eval:
7350 if (compile_expr0(&p, &cctx) == FAIL)
7351 goto erret;
7352
7353 // drop the return value
7354 generate_instr_drop(&cctx, ISN_DROP, 1);
7355
7356 line = skipwhite(p);
7357 break;
7358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007359 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007361 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007362 case CMD_echomsg:
7363 case CMD_echoerr:
7364 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007365 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007367 // TODO: other commands with an expression argument
7368
Bram Moolenaar002262f2020-07-08 17:47:57 +02007369 case CMD_SIZE:
7370 semsg(_("E476: Invalid command: %s"), ea.cmd);
7371 goto erret;
7372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007374 // Not recognized, execute with do_cmdline_cmd().
7375 ea.arg = p;
7376 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 break;
7378 }
7379 if (line == NULL)
7380 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007381 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382
7383 if (cctx.ctx_type_stack.ga_len < 0)
7384 {
7385 iemsg("Type stack underflow");
7386 goto erret;
7387 }
7388 }
7389
7390 if (cctx.ctx_scope != NULL)
7391 {
7392 if (cctx.ctx_scope->se_type == IF_SCOPE)
7393 emsg(_(e_endif));
7394 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7395 emsg(_(e_endwhile));
7396 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7397 emsg(_(e_endfor));
7398 else
7399 emsg(_("E1026: Missing }"));
7400 goto erret;
7401 }
7402
Bram Moolenaarefd88552020-06-18 20:50:10 +02007403 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 {
7405 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7406 {
7407 emsg(_("E1027: Missing return statement"));
7408 goto erret;
7409 }
7410
7411 // Return zero if there is no return at the end.
7412 generate_PUSHNR(&cctx, 0);
7413 generate_instr(&cctx, ISN_RETURN);
7414 }
7415
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007416 {
7417 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7418 + ufunc->uf_dfunc_idx;
7419 dfunc->df_deleted = FALSE;
7420 dfunc->df_instr = instr->ga_data;
7421 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007422 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007423 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007424 if (cctx.ctx_outer_used)
7425 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007426 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428
7429 ret = OK;
7430
7431erret:
7432 if (ret == FAIL)
7433 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007434 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007435 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7436 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007437
7438 for (idx = 0; idx < instr->ga_len; ++idx)
7439 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007440 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007441
Bram Moolenaar45a15082020-05-25 00:28:33 +02007442 // if using the last entry in the table we might as well remove it
7443 if (!dfunc->df_deleted
7444 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007445 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007446 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007447
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007448 while (cctx.ctx_scope != NULL)
7449 drop_scope(&cctx);
7450
Bram Moolenaar20431c92020-03-20 18:39:46 +01007451 // Don't execute this function body.
7452 ga_clear_strings(&ufunc->uf_lines);
7453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007454 if (errormsg != NULL)
7455 emsg(errormsg);
7456 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007457 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458 }
7459
7460 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007461 if (do_estack_push)
7462 estack_pop();
7463
Bram Moolenaar20431c92020-03-20 18:39:46 +01007464 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007465 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007467 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468}
7469
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007470 void
7471set_function_type(ufunc_T *ufunc)
7472{
7473 int varargs = ufunc->uf_va_name != NULL;
7474 int argcount = ufunc->uf_args.ga_len;
7475
7476 // Create a type for the function, with the return type and any
7477 // argument types.
7478 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7479 // The type is included in "tt_args".
7480 if (argcount > 0 || varargs)
7481 {
7482 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7483 argcount, &ufunc->uf_type_list);
7484 // Add argument types to the function type.
7485 if (func_type_add_arg_types(ufunc->uf_func_type,
7486 argcount + varargs,
7487 &ufunc->uf_type_list) == FAIL)
7488 return;
7489 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7490 ufunc->uf_func_type->tt_min_argcount =
7491 argcount - ufunc->uf_def_args.ga_len;
7492 if (ufunc->uf_arg_types == NULL)
7493 {
7494 int i;
7495
7496 // lambda does not have argument types.
7497 for (i = 0; i < argcount; ++i)
7498 ufunc->uf_func_type->tt_args[i] = &t_any;
7499 }
7500 else
7501 mch_memmove(ufunc->uf_func_type->tt_args,
7502 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7503 if (varargs)
7504 {
7505 ufunc->uf_func_type->tt_args[argcount] =
7506 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7507 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7508 }
7509 }
7510 else
7511 // No arguments, can use a predefined type.
7512 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7513 argcount, &ufunc->uf_type_list);
7514}
7515
7516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517/*
7518 * Delete an instruction, free what it contains.
7519 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007520 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521delete_instr(isn_T *isn)
7522{
7523 switch (isn->isn_type)
7524 {
7525 case ISN_EXEC:
7526 case ISN_LOADENV:
7527 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007528 case ISN_LOADB:
7529 case ISN_LOADW:
7530 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007531 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007532 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007533 case ISN_PUSHEXC:
7534 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007535 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007536 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007537 case ISN_STOREB:
7538 case ISN_STOREW:
7539 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007540 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 vim_free(isn->isn_arg.string);
7542 break;
7543
7544 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007545 case ISN_STORES:
7546 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007547 break;
7548
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007549 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007550 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007551 vim_free(isn->isn_arg.unlet.ul_name);
7552 break;
7553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007554 case ISN_STOREOPT:
7555 vim_free(isn->isn_arg.storeopt.so_name);
7556 break;
7557
7558 case ISN_PUSHBLOB: // push blob isn_arg.blob
7559 blob_unref(isn->isn_arg.blob);
7560 break;
7561
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007562 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007563#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007564 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007565#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007566 break;
7567
7568 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007569#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007570 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007571#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007572 break;
7573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574 case ISN_UCALL:
7575 vim_free(isn->isn_arg.ufunc.cuf_name);
7576 break;
7577
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007578 case ISN_FUNCREF:
7579 {
7580 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7581 + isn->isn_arg.funcref.fr_func;
7582 func_ptr_unref(dfunc->df_ufunc);
7583 }
7584 break;
7585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007586 case ISN_2BOOL:
7587 case ISN_2STRING:
7588 case ISN_ADDBLOB:
7589 case ISN_ADDLIST:
7590 case ISN_BCALL:
7591 case ISN_CATCH:
7592 case ISN_CHECKNR:
7593 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007594 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007595 case ISN_COMPAREANY:
7596 case ISN_COMPAREBLOB:
7597 case ISN_COMPAREBOOL:
7598 case ISN_COMPAREDICT:
7599 case ISN_COMPAREFLOAT:
7600 case ISN_COMPAREFUNC:
7601 case ISN_COMPARELIST:
7602 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007603 case ISN_COMPARESPECIAL:
7604 case ISN_COMPARESTRING:
7605 case ISN_CONCAT:
7606 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007607 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007608 case ISN_DROP:
7609 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007610 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007611 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007612 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007613 case ISN_EXECCONCAT:
7614 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007615 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007616 case ISN_LISTINDEX:
7617 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007618 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007619 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007620 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007621 case ISN_JUMP:
7622 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007623 case ISN_LOADBDICT:
7624 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007625 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007627 case ISN_LOADSCRIPT:
7628 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007630 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631 case ISN_NEGATENR:
7632 case ISN_NEWDICT:
7633 case ISN_NEWLIST:
7634 case ISN_OPNR:
7635 case ISN_OPFLOAT:
7636 case ISN_OPANY:
7637 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007638 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007639 case ISN_PUSHF:
7640 case ISN_PUSHNR:
7641 case ISN_PUSHBOOL:
7642 case ISN_PUSHSPEC:
7643 case ISN_RETURN:
7644 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007645 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007646 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007647 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007648 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007650 case ISN_STOREDICT:
7651 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007652 case ISN_THROW:
7653 case ISN_TRY:
7654 // nothing allocated
7655 break;
7656 }
7657}
7658
7659/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007660 * Free all instructions for "dfunc".
7661 */
7662 static void
7663delete_def_function_contents(dfunc_T *dfunc)
7664{
7665 int idx;
7666
7667 ga_clear(&dfunc->df_def_args_isn);
7668
7669 if (dfunc->df_instr != NULL)
7670 {
7671 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7672 delete_instr(dfunc->df_instr + idx);
7673 VIM_CLEAR(dfunc->df_instr);
7674 }
7675
7676 dfunc->df_deleted = TRUE;
7677}
7678
7679/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007680 * When a user function is deleted, clear the contents of any associated def
7681 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007682 */
7683 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007684clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007685{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007686 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007687 {
7688 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7689 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007690
Bram Moolenaar20431c92020-03-20 18:39:46 +01007691 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007692 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007693 }
7694}
7695
7696#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007697/*
7698 * Free all functions defined with ":def".
7699 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700 void
7701free_def_functions(void)
7702{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007703 int idx;
7704
7705 for (idx = 0; idx < def_functions.ga_len; ++idx)
7706 {
7707 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7708
7709 delete_def_function_contents(dfunc);
7710 }
7711
7712 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713}
7714#endif
7715
7716
7717#endif // FEAT_EVAL