blob: 39ef5f6cda5ca09e7e3f5454572ef62d4ee7da05 [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100150
Bram Moolenaar20431c92020-03-20 18:39:46 +0100151static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200152static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153
154/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200155 * Lookup variable "name" in the local scope and return it.
156 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200158 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159lookup_local(char_u *name, size_t len, cctx_T *cctx)
160{
161 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100164 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200165 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166
167 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
169 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 if (STRNCMP(name, lvar->lv_name, len) == 0
172 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200173 {
174 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200175 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200178
179 // Find local in outer function scope.
180 if (cctx->ctx_outer != NULL)
181 {
182 lvar = lookup_local(name, len, cctx->ctx_outer);
183 if (lvar != NULL)
184 {
185 // TODO: are there situations we should not mark the outer scope as
186 // used?
187 cctx->ctx_outer_used = TRUE;
188 lvar->lv_from_outer = TRUE;
189 return lvar;
190 }
191 }
192
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100194}
195
196/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200197 * Lookup an argument in the current function and an enclosing function.
198 * Returns the argument index in "idxp"
199 * Returns the argument type in "type"
200 * Sets "gen_load_outer" to TRUE if found in outer scope.
201 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202 */
203 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200204lookup_arg(
205 char_u *name,
206 size_t len,
207 int *idxp,
208 type_T **type,
209 int *gen_load_outer,
210 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211{
212 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200213 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100215 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200216 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
218 {
219 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
220
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200221 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
222 {
223 if (idxp != NULL)
224 {
225 // Arguments are located above the frame pointer. One further
226 // if there is a vararg argument
227 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
228 + STACK_FRAME_SIZE)
229 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
230
231 if (cctx->ctx_ufunc->uf_arg_types != NULL)
232 *type = cctx->ctx_ufunc->uf_arg_types[idx];
233 else
234 *type = &t_any;
235 }
236 return OK;
237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200240 va_name = cctx->ctx_ufunc->uf_va_name;
241 if (va_name != NULL
242 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
243 {
244 if (idxp != NULL)
245 {
246 // varargs is always the last argument
247 *idxp = -STACK_FRAME_SIZE - 1;
248 *type = cctx->ctx_ufunc->uf_va_type;
249 }
250 return OK;
251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200253 if (cctx->ctx_outer != NULL)
254 {
255 // Lookup the name for an argument of the outer function.
256 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
257 == OK)
258 {
259 *gen_load_outer = TRUE;
260 return OK;
261 }
262 }
263
264 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265}
266
267/*
268 * Lookup a variable in the current script.
269 * Returns OK or FAIL.
270 */
271 static int
272lookup_script(char_u *name, size_t len)
273{
274 int cc;
275 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
276 dictitem_T *di;
277
278 cc = name[len];
279 name[len] = NUL;
280 di = find_var_in_ht(ht, 0, name, TRUE);
281 name[len] = cc;
282 return di == NULL ? FAIL: OK;
283}
284
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100285/*
286 * Check if "p[len]" is already defined, either in script "import_sid" or in
287 * compilation context "cctx".
288 * Return FAIL and give an error if it defined.
289 */
290 int
291check_defined(char_u *p, int len, cctx_T *cctx)
292{
293 if (lookup_script(p, len) == OK
294 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200295 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100296 || find_imported(p, len, cctx) != NULL)))
297 {
298 semsg("E1073: imported name already defined: %s", p);
299 return FAIL;
300 }
301 return OK;
302}
303
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200304/*
305 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
306 * be freed later.
307 */
308 static type_T *
309alloc_type(garray_T *type_gap)
310{
311 type_T *type;
312
313 if (ga_grow(type_gap, 1) == FAIL)
314 return NULL;
315 type = ALLOC_CLEAR_ONE(type_T);
316 if (type != NULL)
317 {
318 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
319 ++type_gap->ga_len;
320 }
321 return type;
322}
323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100324 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200325get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326{
327 type_T *type;
328
329 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200330 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100331 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200332 if (member_type->tt_type == VAR_VOID
333 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100334 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100335 if (member_type->tt_type == VAR_BOOL)
336 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100337 if (member_type->tt_type == VAR_NUMBER)
338 return &t_list_number;
339 if (member_type->tt_type == VAR_STRING)
340 return &t_list_string;
341
342 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200343 type = alloc_type(type_gap);
344 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100345 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 type->tt_type = VAR_LIST;
347 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200348 type->tt_argcount = 0;
349 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350 return type;
351}
352
353 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200354get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355{
356 type_T *type;
357
358 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200359 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100360 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200361 if (member_type->tt_type == VAR_VOID
362 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100363 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100364 if (member_type->tt_type == VAR_BOOL)
365 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366 if (member_type->tt_type == VAR_NUMBER)
367 return &t_dict_number;
368 if (member_type->tt_type == VAR_STRING)
369 return &t_dict_string;
370
371 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200372 type = alloc_type(type_gap);
373 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100374 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375 type->tt_type = VAR_DICT;
376 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200377 type->tt_argcount = 0;
378 type->tt_args = NULL;
379 return type;
380}
381
382/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200383 * Allocate a new type for a function.
384 */
385 static type_T *
386alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
387{
388 type_T *type = alloc_type(type_gap);
389
390 if (type == NULL)
391 return &t_any;
392 type->tt_type = VAR_FUNC;
393 type->tt_member = ret_type;
394 type->tt_argcount = argcount;
395 type->tt_args = NULL;
396 return type;
397}
398
399/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200400 * Get a function type, based on the return type "ret_type".
401 * If "argcount" is -1 or 0 a predefined type can be used.
402 * If "argcount" > 0 always create a new type, so that arguments can be added.
403 */
404 static type_T *
405get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
406{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200407 // recognize commonly used types
408 if (argcount <= 0)
409 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200410 if (ret_type == &t_unknown)
411 {
412 // (argcount == 0) is not possible
413 return &t_func_unknown;
414 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200415 if (ret_type == &t_void)
416 {
417 if (argcount == 0)
418 return &t_func_0_void;
419 else
420 return &t_func_void;
421 }
422 if (ret_type == &t_any)
423 {
424 if (argcount == 0)
425 return &t_func_0_any;
426 else
427 return &t_func_any;
428 }
429 if (ret_type == &t_number)
430 {
431 if (argcount == 0)
432 return &t_func_0_number;
433 else
434 return &t_func_number;
435 }
436 if (ret_type == &t_string)
437 {
438 if (argcount == 0)
439 return &t_func_0_string;
440 else
441 return &t_func_string;
442 }
443 }
444
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200445 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446}
447
Bram Moolenaara8c17702020-04-01 21:17:24 +0200448/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200449 * For a function type, reserve space for "argcount" argument types (including
450 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200451 */
452 static int
453func_type_add_arg_types(
454 type_T *functype,
455 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200456 garray_T *type_gap)
457{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200458 // To make it easy to free the space needed for the argument types, add the
459 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200460 if (ga_grow(type_gap, 1) == FAIL)
461 return FAIL;
462 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
463 if (functype->tt_args == NULL)
464 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200465 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
466 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200467 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200468 return OK;
469}
470
471/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200472 * Return the type_T for a typval. Only for primitive types.
473 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200474 type_T *
Bram Moolenaara8c17702020-04-01 21:17:24 +0200475typval2type(typval_T *tv)
476{
477 if (tv->v_type == VAR_NUMBER)
478 return &t_number;
479 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200480 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200481 if (tv->v_type == VAR_STRING)
482 return &t_string;
483 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
484 return &t_list_string;
485 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
486 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200487 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200488}
489
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200490 static void
491type_mismatch(type_T *expected, type_T *actual)
492{
493 char *tofree1, *tofree2;
494
495 semsg(_("E1013: type mismatch, expected %s but got %s"),
496 type_name(expected, &tofree1), type_name(actual, &tofree2));
497 vim_free(tofree1);
498 vim_free(tofree2);
499}
500
501 static void
502arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
503{
504 char *tofree1, *tofree2;
505
506 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
507 argidx,
508 type_name(expected, &tofree1), type_name(actual, &tofree2));
509 vim_free(tofree1);
510 vim_free(tofree2);
511}
512
513/*
514 * Check if the expected and actual types match.
515 * Does not allow for assigning "any" to a specific type.
516 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200517 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200518check_type(type_T *expected, type_T *actual, int give_msg)
519{
520 int ret = OK;
521
522 // When expected is "unknown" we accept any actual type.
523 // When expected is "any" we accept any actual type except "void".
524 if (expected->tt_type != VAR_UNKNOWN
525 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
526
527 {
528 if (expected->tt_type != actual->tt_type)
529 {
530 if (give_msg)
531 type_mismatch(expected, actual);
532 return FAIL;
533 }
534 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
535 {
536 // "unknown" is used for an empty list or dict
537 if (actual->tt_member != &t_unknown)
538 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
539 }
540 else if (expected->tt_type == VAR_FUNC)
541 {
542 if (expected->tt_member != &t_unknown)
543 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
544 if (ret == OK && expected->tt_argcount != -1
545 && (actual->tt_argcount < expected->tt_min_argcount
546 || actual->tt_argcount > expected->tt_argcount))
547 ret = FAIL;
548 }
549 if (ret == FAIL && give_msg)
550 type_mismatch(expected, actual);
551 }
552 return ret;
553}
554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555/////////////////////////////////////////////////////////////////////
556// Following generate_ functions expect the caller to call ga_grow().
557
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200558#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
559#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100561/*
562 * Generate an instruction without arguments.
563 * Returns a pointer to the new instruction, NULL if failed.
564 */
565 static isn_T *
566generate_instr(cctx_T *cctx, isntype_T isn_type)
567{
568 garray_T *instr = &cctx->ctx_instr;
569 isn_T *isn;
570
Bram Moolenaar080457c2020-03-03 21:53:32 +0100571 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 if (ga_grow(instr, 1) == FAIL)
573 return NULL;
574 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
575 isn->isn_type = isn_type;
576 isn->isn_lnum = cctx->ctx_lnum + 1;
577 ++instr->ga_len;
578
579 return isn;
580}
581
582/*
583 * Generate an instruction without arguments.
584 * "drop" will be removed from the stack.
585 * Returns a pointer to the new instruction, NULL if failed.
586 */
587 static isn_T *
588generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
589{
590 garray_T *stack = &cctx->ctx_type_stack;
591
Bram Moolenaar080457c2020-03-03 21:53:32 +0100592 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 stack->ga_len -= drop;
594 return generate_instr(cctx, isn_type);
595}
596
597/*
598 * Generate instruction "isn_type" and put "type" on the type stack.
599 */
600 static isn_T *
601generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
602{
603 isn_T *isn;
604 garray_T *stack = &cctx->ctx_type_stack;
605
606 if ((isn = generate_instr(cctx, isn_type)) == NULL)
607 return NULL;
608
609 if (ga_grow(stack, 1) == FAIL)
610 return NULL;
611 ((type_T **)stack->ga_data)[stack->ga_len] = type;
612 ++stack->ga_len;
613
614 return isn;
615}
616
617/*
618 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
619 */
620 static int
621may_generate_2STRING(int offset, cctx_T *cctx)
622{
623 isn_T *isn;
624 garray_T *stack = &cctx->ctx_type_stack;
625 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
626
627 if ((*type)->tt_type == VAR_STRING)
628 return OK;
629 *type = &t_string;
630
631 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
632 return FAIL;
633 isn->isn_arg.number = offset;
634
635 return OK;
636}
637
638 static int
639check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
640{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200641 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200643 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 {
645 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200646 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647 else
648 semsg(_("E1036: %c requires number or float arguments"), *op);
649 return FAIL;
650 }
651 return OK;
652}
653
654/*
655 * Generate an instruction with two arguments. The instruction depends on the
656 * type of the arguments.
657 */
658 static int
659generate_two_op(cctx_T *cctx, char_u *op)
660{
661 garray_T *stack = &cctx->ctx_type_stack;
662 type_T *type1;
663 type_T *type2;
664 vartype_T vartype;
665 isn_T *isn;
666
Bram Moolenaar080457c2020-03-03 21:53:32 +0100667 RETURN_OK_IF_SKIP(cctx);
668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 // Get the known type of the two items on the stack. If they are matching
670 // use a type-specific instruction. Otherwise fall back to runtime type
671 // checking.
672 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
673 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200674 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675 if (type1->tt_type == type2->tt_type
676 && (type1->tt_type == VAR_NUMBER
677 || type1->tt_type == VAR_LIST
678#ifdef FEAT_FLOAT
679 || type1->tt_type == VAR_FLOAT
680#endif
681 || type1->tt_type == VAR_BLOB))
682 vartype = type1->tt_type;
683
684 switch (*op)
685 {
686 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200687 && type1->tt_type != VAR_ANY
688 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 && check_number_or_float(
690 type1->tt_type, type2->tt_type, op) == FAIL)
691 return FAIL;
692 isn = generate_instr_drop(cctx,
693 vartype == VAR_NUMBER ? ISN_OPNR
694 : vartype == VAR_LIST ? ISN_ADDLIST
695 : vartype == VAR_BLOB ? ISN_ADDBLOB
696#ifdef FEAT_FLOAT
697 : vartype == VAR_FLOAT ? ISN_OPFLOAT
698#endif
699 : ISN_OPANY, 1);
700 if (isn != NULL)
701 isn->isn_arg.op.op_type = EXPR_ADD;
702 break;
703
704 case '-':
705 case '*':
706 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
707 op) == FAIL)
708 return FAIL;
709 if (vartype == VAR_NUMBER)
710 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
711#ifdef FEAT_FLOAT
712 else if (vartype == VAR_FLOAT)
713 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
714#endif
715 else
716 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
717 if (isn != NULL)
718 isn->isn_arg.op.op_type = *op == '*'
719 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
720 break;
721
Bram Moolenaar4c683752020-04-05 21:38:23 +0200722 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200724 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 && type2->tt_type != VAR_NUMBER))
726 {
727 emsg(_("E1035: % requires number arguments"));
728 return FAIL;
729 }
730 isn = generate_instr_drop(cctx,
731 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
732 if (isn != NULL)
733 isn->isn_arg.op.op_type = EXPR_REM;
734 break;
735 }
736
737 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200738 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100739 {
740 type_T *type = &t_any;
741
742#ifdef FEAT_FLOAT
743 // float+number and number+float results in float
744 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
745 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
746 type = &t_float;
747#endif
748 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
749 }
750
751 return OK;
752}
753
754/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200755 * Get the instruction to use for comparing "type1" with "type2"
756 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200758 static isntype_T
759get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760{
761 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762
Bram Moolenaar4c683752020-04-05 21:38:23 +0200763 if (type1 == VAR_UNKNOWN)
764 type1 = VAR_ANY;
765 if (type2 == VAR_UNKNOWN)
766 type2 = VAR_ANY;
767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 if (type1 == type2)
769 {
770 switch (type1)
771 {
772 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
773 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
774 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
775 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
776 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
777 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
778 case VAR_LIST: isntype = ISN_COMPARELIST; break;
779 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
780 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 default: isntype = ISN_COMPAREANY; break;
782 }
783 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200784 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
786 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
787 isntype = ISN_COMPAREANY;
788
789 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
790 && (isntype == ISN_COMPAREBOOL
791 || isntype == ISN_COMPARESPECIAL
792 || isntype == ISN_COMPARENR
793 || isntype == ISN_COMPAREFLOAT))
794 {
795 semsg(_("E1037: Cannot use \"%s\" with %s"),
796 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200797 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 }
799 if (isntype == ISN_DROP
800 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
801 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
802 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
803 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
804 && exptype != EXPR_IS && exptype != EXPR_ISNOT
805 && (type1 == VAR_BLOB || type2 == VAR_BLOB
806 || type1 == VAR_LIST || type2 == VAR_LIST))))
807 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100808 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200810 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200812 return isntype;
813}
814
815/*
816 * Generate an ISN_COMPARE* instruction with a boolean result.
817 */
818 static int
819generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
820{
821 isntype_T isntype;
822 isn_T *isn;
823 garray_T *stack = &cctx->ctx_type_stack;
824 vartype_T type1;
825 vartype_T type2;
826
827 RETURN_OK_IF_SKIP(cctx);
828
829 // Get the known type of the two items on the stack. If they are matching
830 // use a type-specific instruction. Otherwise fall back to runtime type
831 // checking.
832 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
833 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
834 isntype = get_compare_isn(exptype, type1, type2);
835 if (isntype == ISN_DROP)
836 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837
838 if ((isn = generate_instr(cctx, isntype)) == NULL)
839 return FAIL;
840 isn->isn_arg.op.op_type = exptype;
841 isn->isn_arg.op.op_ic = ic;
842
843 // takes two arguments, puts one bool back
844 if (stack->ga_len >= 2)
845 {
846 --stack->ga_len;
847 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
848 }
849
850 return OK;
851}
852
853/*
854 * Generate an ISN_2BOOL instruction.
855 */
856 static int
857generate_2BOOL(cctx_T *cctx, int invert)
858{
859 isn_T *isn;
860 garray_T *stack = &cctx->ctx_type_stack;
861
Bram Moolenaar080457c2020-03-03 21:53:32 +0100862 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100863 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
864 return FAIL;
865 isn->isn_arg.number = invert;
866
867 // type becomes bool
868 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
869
870 return OK;
871}
872
873 static int
874generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
875{
876 isn_T *isn;
877 garray_T *stack = &cctx->ctx_type_stack;
878
Bram Moolenaar080457c2020-03-03 21:53:32 +0100879 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
881 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200882 // TODO: whole type, e.g. for a function also arg and return types
883 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884 isn->isn_arg.type.ct_off = offset;
885
886 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200887 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888
889 return OK;
890}
891
892/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200893 * Check that
894 * - "actual" is "expected" type or
895 * - "actual" is a type that can be "expected" type: add a runtime check; or
896 * - return FAIL.
897 */
898 static int
899need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
900{
901 if (check_type(expected, actual, FALSE) == OK)
902 return OK;
903 if (actual->tt_type != VAR_ANY
904 && actual->tt_type != VAR_UNKNOWN
905 && !(actual->tt_type == VAR_FUNC
906 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
907 {
908 type_mismatch(expected, actual);
909 return FAIL;
910 }
911 generate_TYPECHECK(cctx, expected, offset);
912 return OK;
913}
914
915/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916 * Generate an ISN_PUSHNR instruction.
917 */
918 static int
919generate_PUSHNR(cctx_T *cctx, varnumber_T number)
920{
921 isn_T *isn;
922
Bram Moolenaar080457c2020-03-03 21:53:32 +0100923 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
925 return FAIL;
926 isn->isn_arg.number = number;
927
928 return OK;
929}
930
931/*
932 * Generate an ISN_PUSHBOOL instruction.
933 */
934 static int
935generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
936{
937 isn_T *isn;
938
Bram Moolenaar080457c2020-03-03 21:53:32 +0100939 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
941 return FAIL;
942 isn->isn_arg.number = number;
943
944 return OK;
945}
946
947/*
948 * Generate an ISN_PUSHSPEC instruction.
949 */
950 static int
951generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
952{
953 isn_T *isn;
954
Bram Moolenaar080457c2020-03-03 21:53:32 +0100955 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
957 return FAIL;
958 isn->isn_arg.number = number;
959
960 return OK;
961}
962
963#ifdef FEAT_FLOAT
964/*
965 * Generate an ISN_PUSHF instruction.
966 */
967 static int
968generate_PUSHF(cctx_T *cctx, float_T fnumber)
969{
970 isn_T *isn;
971
Bram Moolenaar080457c2020-03-03 21:53:32 +0100972 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
974 return FAIL;
975 isn->isn_arg.fnumber = fnumber;
976
977 return OK;
978}
979#endif
980
981/*
982 * Generate an ISN_PUSHS instruction.
983 * Consumes "str".
984 */
985 static int
986generate_PUSHS(cctx_T *cctx, char_u *str)
987{
988 isn_T *isn;
989
Bram Moolenaar080457c2020-03-03 21:53:32 +0100990 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
992 return FAIL;
993 isn->isn_arg.string = str;
994
995 return OK;
996}
997
998/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100999 * Generate an ISN_PUSHCHANNEL instruction.
1000 * Consumes "channel".
1001 */
1002 static int
1003generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1004{
1005 isn_T *isn;
1006
Bram Moolenaar080457c2020-03-03 21:53:32 +01001007 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001008 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1009 return FAIL;
1010 isn->isn_arg.channel = channel;
1011
1012 return OK;
1013}
1014
1015/*
1016 * Generate an ISN_PUSHJOB instruction.
1017 * Consumes "job".
1018 */
1019 static int
1020generate_PUSHJOB(cctx_T *cctx, job_T *job)
1021{
1022 isn_T *isn;
1023
Bram Moolenaar080457c2020-03-03 21:53:32 +01001024 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001025 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001026 return FAIL;
1027 isn->isn_arg.job = job;
1028
1029 return OK;
1030}
1031
1032/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 * Generate an ISN_PUSHBLOB instruction.
1034 * Consumes "blob".
1035 */
1036 static int
1037generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1038{
1039 isn_T *isn;
1040
Bram Moolenaar080457c2020-03-03 21:53:32 +01001041 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1043 return FAIL;
1044 isn->isn_arg.blob = blob;
1045
1046 return OK;
1047}
1048
1049/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001050 * Generate an ISN_PUSHFUNC instruction with name "name".
1051 * Consumes "name".
1052 */
1053 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001054generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001055{
1056 isn_T *isn;
1057
Bram Moolenaar080457c2020-03-03 21:53:32 +01001058 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001059 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001060 return FAIL;
1061 isn->isn_arg.string = name;
1062
1063 return OK;
1064}
1065
1066/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001067 * Generate an ISN_GETITEM instruction with "index".
1068 */
1069 static int
1070generate_GETITEM(cctx_T *cctx, int index)
1071{
1072 isn_T *isn;
1073 garray_T *stack = &cctx->ctx_type_stack;
1074 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1075 type_T *item_type = &t_any;
1076
1077 RETURN_OK_IF_SKIP(cctx);
1078
1079 if (type->tt_type == VAR_LIST)
1080 item_type = type->tt_member;
1081 else if (type->tt_type != VAR_ANY)
1082 {
1083 emsg(_(e_listreq));
1084 return FAIL;
1085 }
1086 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1087 return FAIL;
1088 isn->isn_arg.number = index;
1089
1090 // add the item type to the type stack
1091 if (ga_grow(stack, 1) == FAIL)
1092 return FAIL;
1093 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1094 ++stack->ga_len;
1095 return OK;
1096}
1097
1098/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001099 * Generate an ISN_SLICE instruction with "count".
1100 */
1101 static int
1102generate_SLICE(cctx_T *cctx, int count)
1103{
1104 isn_T *isn;
1105
1106 RETURN_OK_IF_SKIP(cctx);
1107 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1108 return FAIL;
1109 isn->isn_arg.number = count;
1110 return OK;
1111}
1112
1113/*
1114 * Generate an ISN_CHECKLEN instruction with "min_len".
1115 */
1116 static int
1117generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1118{
1119 isn_T *isn;
1120
1121 RETURN_OK_IF_SKIP(cctx);
1122
1123 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1124 return FAIL;
1125 isn->isn_arg.checklen.cl_min_len = min_len;
1126 isn->isn_arg.checklen.cl_more_OK = more_OK;
1127
1128 return OK;
1129}
1130
1131/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 * Generate an ISN_STORE instruction.
1133 */
1134 static int
1135generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1136{
1137 isn_T *isn;
1138
Bram Moolenaar080457c2020-03-03 21:53:32 +01001139 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1141 return FAIL;
1142 if (name != NULL)
1143 isn->isn_arg.string = vim_strsave(name);
1144 else
1145 isn->isn_arg.number = idx;
1146
1147 return OK;
1148}
1149
1150/*
1151 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1152 */
1153 static int
1154generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1155{
1156 isn_T *isn;
1157
Bram Moolenaar080457c2020-03-03 21:53:32 +01001158 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1160 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001161 isn->isn_arg.storenr.stnr_idx = idx;
1162 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163
1164 return OK;
1165}
1166
1167/*
1168 * Generate an ISN_STOREOPT instruction
1169 */
1170 static int
1171generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1172{
1173 isn_T *isn;
1174
Bram Moolenaar080457c2020-03-03 21:53:32 +01001175 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1177 return FAIL;
1178 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1179 isn->isn_arg.storeopt.so_flags = opt_flags;
1180
1181 return OK;
1182}
1183
1184/*
1185 * Generate an ISN_LOAD or similar instruction.
1186 */
1187 static int
1188generate_LOAD(
1189 cctx_T *cctx,
1190 isntype_T isn_type,
1191 int idx,
1192 char_u *name,
1193 type_T *type)
1194{
1195 isn_T *isn;
1196
Bram Moolenaar080457c2020-03-03 21:53:32 +01001197 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1199 return FAIL;
1200 if (name != NULL)
1201 isn->isn_arg.string = vim_strsave(name);
1202 else
1203 isn->isn_arg.number = idx;
1204
1205 return OK;
1206}
1207
1208/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001209 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001210 */
1211 static int
1212generate_LOADV(
1213 cctx_T *cctx,
1214 char_u *name,
1215 int error)
1216{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001217 int di_flags;
1218 int vidx = find_vim_var(name, &di_flags);
1219 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001220
Bram Moolenaar080457c2020-03-03 21:53:32 +01001221 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001222 if (vidx < 0)
1223 {
1224 if (error)
1225 semsg(_(e_var_notfound), name);
1226 return FAIL;
1227 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001228 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001229
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001230 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001231}
1232
1233/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001234 * Generate an ISN_UNLET instruction.
1235 */
1236 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001237generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001238{
1239 isn_T *isn;
1240
1241 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001242 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001243 return FAIL;
1244 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1245 isn->isn_arg.unlet.ul_forceit = forceit;
1246
1247 return OK;
1248}
1249
1250/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 * Generate an ISN_LOADS instruction.
1252 */
1253 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001254generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001256 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001258 int sid,
1259 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260{
1261 isn_T *isn;
1262
Bram Moolenaar080457c2020-03-03 21:53:32 +01001263 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001264 if (isn_type == ISN_LOADS)
1265 isn = generate_instr_type(cctx, isn_type, type);
1266 else
1267 isn = generate_instr_drop(cctx, isn_type, 1);
1268 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001270 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1271 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272
1273 return OK;
1274}
1275
1276/*
1277 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1278 */
1279 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 cctx_T *cctx,
1282 isntype_T isn_type,
1283 int sid,
1284 int idx,
1285 type_T *type)
1286{
1287 isn_T *isn;
1288
Bram Moolenaar080457c2020-03-03 21:53:32 +01001289 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 if (isn_type == ISN_LOADSCRIPT)
1291 isn = generate_instr_type(cctx, isn_type, type);
1292 else
1293 isn = generate_instr_drop(cctx, isn_type, 1);
1294 if (isn == NULL)
1295 return FAIL;
1296 isn->isn_arg.script.script_sid = sid;
1297 isn->isn_arg.script.script_idx = idx;
1298 return OK;
1299}
1300
1301/*
1302 * Generate an ISN_NEWLIST instruction.
1303 */
1304 static int
1305generate_NEWLIST(cctx_T *cctx, int count)
1306{
1307 isn_T *isn;
1308 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 type_T *type;
1310 type_T *member;
1311
Bram Moolenaar080457c2020-03-03 21:53:32 +01001312 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1314 return FAIL;
1315 isn->isn_arg.number = count;
1316
1317 // drop the value types
1318 stack->ga_len -= count;
1319
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001320 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001321 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 if (count > 0)
1323 member = ((type_T **)stack->ga_data)[stack->ga_len];
1324 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001325 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001326 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
1328 // add the list type to the type stack
1329 if (ga_grow(stack, 1) == FAIL)
1330 return FAIL;
1331 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1332 ++stack->ga_len;
1333
1334 return OK;
1335}
1336
1337/*
1338 * Generate an ISN_NEWDICT instruction.
1339 */
1340 static int
1341generate_NEWDICT(cctx_T *cctx, int count)
1342{
1343 isn_T *isn;
1344 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 type_T *type;
1346 type_T *member;
1347
Bram Moolenaar080457c2020-03-03 21:53:32 +01001348 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1350 return FAIL;
1351 isn->isn_arg.number = count;
1352
1353 // drop the key and value types
1354 stack->ga_len -= 2 * count;
1355
Bram Moolenaar436472f2020-02-20 22:54:43 +01001356 // Use the first value type for the list member type. Use "void" for an
1357 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 if (count > 0)
1359 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1360 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001361 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001362 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363
1364 // add the dict type to the type stack
1365 if (ga_grow(stack, 1) == FAIL)
1366 return FAIL;
1367 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1368 ++stack->ga_len;
1369
1370 return OK;
1371}
1372
1373/*
1374 * Generate an ISN_FUNCREF instruction.
1375 */
1376 static int
1377generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1378{
1379 isn_T *isn;
1380 garray_T *stack = &cctx->ctx_type_stack;
1381
Bram Moolenaar080457c2020-03-03 21:53:32 +01001382 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1384 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001385 isn->isn_arg.funcref.fr_func = dfunc_idx;
1386 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387
1388 if (ga_grow(stack, 1) == FAIL)
1389 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001390 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 // TODO: argument and return types
1392 ++stack->ga_len;
1393
1394 return OK;
1395}
1396
1397/*
1398 * Generate an ISN_JUMP instruction.
1399 */
1400 static int
1401generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1402{
1403 isn_T *isn;
1404 garray_T *stack = &cctx->ctx_type_stack;
1405
Bram Moolenaar080457c2020-03-03 21:53:32 +01001406 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1408 return FAIL;
1409 isn->isn_arg.jump.jump_when = when;
1410 isn->isn_arg.jump.jump_where = where;
1411
1412 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1413 --stack->ga_len;
1414
1415 return OK;
1416}
1417
1418 static int
1419generate_FOR(cctx_T *cctx, int loop_idx)
1420{
1421 isn_T *isn;
1422 garray_T *stack = &cctx->ctx_type_stack;
1423
Bram Moolenaar080457c2020-03-03 21:53:32 +01001424 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1426 return FAIL;
1427 isn->isn_arg.forloop.for_idx = loop_idx;
1428
1429 if (ga_grow(stack, 1) == FAIL)
1430 return FAIL;
1431 // type doesn't matter, will be stored next
1432 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1433 ++stack->ga_len;
1434
1435 return OK;
1436}
1437
1438/*
1439 * Generate an ISN_BCALL instruction.
1440 * Return FAIL if the number of arguments is wrong.
1441 */
1442 static int
1443generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1444{
1445 isn_T *isn;
1446 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001447 type_T *argtypes[MAX_FUNC_ARGS];
1448 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449
Bram Moolenaar080457c2020-03-03 21:53:32 +01001450 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 if (check_internal_func(func_idx, argcount) == FAIL)
1452 return FAIL;
1453
1454 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1455 return FAIL;
1456 isn->isn_arg.bfunc.cbf_idx = func_idx;
1457 isn->isn_arg.bfunc.cbf_argcount = argcount;
1458
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001459 for (i = 0; i < argcount; ++i)
1460 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 stack->ga_len -= argcount; // drop the arguments
1463 if (ga_grow(stack, 1) == FAIL)
1464 return FAIL;
1465 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001466 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 ++stack->ga_len; // add return value
1468
1469 return OK;
1470}
1471
1472/*
1473 * Generate an ISN_DCALL or ISN_UCALL instruction.
1474 * Return FAIL if the number of arguments is wrong.
1475 */
1476 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001477generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478{
1479 isn_T *isn;
1480 garray_T *stack = &cctx->ctx_type_stack;
1481 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001482 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483
Bram Moolenaar080457c2020-03-03 21:53:32 +01001484 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 if (argcount > regular_args && !has_varargs(ufunc))
1486 {
1487 semsg(_(e_toomanyarg), ufunc->uf_name);
1488 return FAIL;
1489 }
1490 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1491 {
1492 semsg(_(e_toofewarg), ufunc->uf_name);
1493 return FAIL;
1494 }
1495
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001496 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001497 {
1498 int i;
1499
1500 for (i = 0; i < argcount; ++i)
1501 {
1502 type_T *expected;
1503 type_T *actual;
1504
1505 if (i < regular_args)
1506 {
1507 if (ufunc->uf_arg_types == NULL)
1508 continue;
1509 expected = ufunc->uf_arg_types[i];
1510 }
1511 else
1512 expected = ufunc->uf_va_type->tt_member;
1513 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001514 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001515 {
1516 arg_type_mismatch(expected, actual, i + 1);
1517 return FAIL;
1518 }
1519 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001520 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001521 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001522 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001523 }
1524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001526 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001527 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001529 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 {
1531 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1532 isn->isn_arg.dfunc.cdf_argcount = argcount;
1533 }
1534 else
1535 {
1536 // A user function may be deleted and redefined later, can't use the
1537 // ufunc pointer, need to look it up again at runtime.
1538 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1539 isn->isn_arg.ufunc.cuf_argcount = argcount;
1540 }
1541
1542 stack->ga_len -= argcount; // drop the arguments
1543 if (ga_grow(stack, 1) == FAIL)
1544 return FAIL;
1545 // add return value
1546 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1547 ++stack->ga_len;
1548
1549 return OK;
1550}
1551
1552/*
1553 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1554 */
1555 static int
1556generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1557{
1558 isn_T *isn;
1559 garray_T *stack = &cctx->ctx_type_stack;
1560
Bram Moolenaar080457c2020-03-03 21:53:32 +01001561 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1563 return FAIL;
1564 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1565 isn->isn_arg.ufunc.cuf_argcount = argcount;
1566
1567 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001568 if (ga_grow(stack, 1) == FAIL)
1569 return FAIL;
1570 // add return value
1571 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1572 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573
1574 return OK;
1575}
1576
1577/*
1578 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001579 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 */
1581 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001582generate_PCALL(
1583 cctx_T *cctx,
1584 int argcount,
1585 char_u *name,
1586 type_T *type,
1587 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588{
1589 isn_T *isn;
1590 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001591 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592
Bram Moolenaar080457c2020-03-03 21:53:32 +01001593 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001594
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001595 if (type->tt_type == VAR_ANY)
1596 ret_type = &t_any;
1597 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001598 {
1599 if (type->tt_argcount != -1)
1600 {
1601 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1602
1603 if (argcount < type->tt_min_argcount - varargs)
1604 {
1605 semsg(_(e_toofewarg), "[reference]");
1606 return FAIL;
1607 }
1608 if (!varargs && argcount > type->tt_argcount)
1609 {
1610 semsg(_(e_toomanyarg), "[reference]");
1611 return FAIL;
1612 }
1613 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001614 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001615 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001616 else
1617 {
1618 semsg(_("E1085: Not a callable type: %s"), name);
1619 return FAIL;
1620 }
1621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1623 return FAIL;
1624 isn->isn_arg.pfunc.cpf_top = at_top;
1625 isn->isn_arg.pfunc.cpf_argcount = argcount;
1626
1627 stack->ga_len -= argcount; // drop the arguments
1628
1629 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001630 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001632 // If partial is above the arguments it must be cleared and replaced with
1633 // the return value.
1634 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1635 return FAIL;
1636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 return OK;
1638}
1639
1640/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001641 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 */
1643 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001644generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645{
1646 isn_T *isn;
1647 garray_T *stack = &cctx->ctx_type_stack;
1648 type_T *type;
1649
Bram Moolenaar080457c2020-03-03 21:53:32 +01001650 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001651 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001653 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001655 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001657 if (type->tt_type != VAR_DICT && type != &t_any)
1658 {
1659 emsg(_(e_dictreq));
1660 return FAIL;
1661 }
1662 // change dict type to dict member type
1663 if (type->tt_type == VAR_DICT)
1664 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665
1666 return OK;
1667}
1668
1669/*
1670 * Generate an ISN_ECHO instruction.
1671 */
1672 static int
1673generate_ECHO(cctx_T *cctx, int with_white, int count)
1674{
1675 isn_T *isn;
1676
Bram Moolenaar080457c2020-03-03 21:53:32 +01001677 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1679 return FAIL;
1680 isn->isn_arg.echo.echo_with_white = with_white;
1681 isn->isn_arg.echo.echo_count = count;
1682
1683 return OK;
1684}
1685
Bram Moolenaarad39c092020-02-26 18:23:43 +01001686/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001687 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001688 */
1689 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001690generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001691{
1692 isn_T *isn;
1693
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001694 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001695 return FAIL;
1696 isn->isn_arg.number = count;
1697
1698 return OK;
1699}
1700
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 static int
1702generate_EXEC(cctx_T *cctx, char_u *line)
1703{
1704 isn_T *isn;
1705
Bram Moolenaar080457c2020-03-03 21:53:32 +01001706 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1708 return FAIL;
1709 isn->isn_arg.string = vim_strsave(line);
1710 return OK;
1711}
1712
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001713 static int
1714generate_EXECCONCAT(cctx_T *cctx, int count)
1715{
1716 isn_T *isn;
1717
1718 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1719 return FAIL;
1720 isn->isn_arg.number = count;
1721 return OK;
1722}
1723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724/*
1725 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001726 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001728 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1730{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 lvar_T *lvar;
1732
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001733 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001735 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001736 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 }
1738
1739 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001740 return NULL;
1741 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001743 // Every local variable uses the next entry on the stack. We could re-use
1744 // the last ones when leaving a scope, but then variables used in a closure
1745 // might get overwritten. To keep things simple do not re-use stack
1746 // entries. This is less efficient, but memory is cheap these days.
1747 lvar->lv_idx = cctx->ctx_locals_count++;
1748
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001749 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 lvar->lv_const = isConst;
1751 lvar->lv_type = type;
1752
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001753 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754}
1755
1756/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001757 * Remove local variables above "new_top".
1758 */
1759 static void
1760unwind_locals(cctx_T *cctx, int new_top)
1761{
1762 if (cctx->ctx_locals.ga_len > new_top)
1763 {
1764 int idx;
1765 lvar_T *lvar;
1766
1767 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1768 {
1769 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1770 vim_free(lvar->lv_name);
1771 }
1772 }
1773 cctx->ctx_locals.ga_len = new_top;
1774}
1775
1776/*
1777 * Free all local variables.
1778 */
1779 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001780free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001781{
1782 unwind_locals(cctx, 0);
1783 ga_clear(&cctx->ctx_locals);
1784}
1785
1786/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 * Skip over a type definition and return a pointer to just after it.
1788 */
1789 char_u *
1790skip_type(char_u *start)
1791{
1792 char_u *p = start;
1793
1794 while (ASCII_ISALNUM(*p) || *p == '_')
1795 ++p;
1796
1797 // Skip over "<type>"; this is permissive about white space.
1798 if (*skipwhite(p) == '<')
1799 {
1800 p = skipwhite(p);
1801 p = skip_type(skipwhite(p + 1));
1802 p = skipwhite(p);
1803 if (*p == '>')
1804 ++p;
1805 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001806 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1807 {
1808 // handle func(args): type
1809 ++p;
1810 while (*p != ')' && *p != NUL)
1811 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001812 char_u *sp = p;
1813
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001814 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001815 if (p == sp)
1816 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001817 if (*p == ',')
1818 p = skipwhite(p + 1);
1819 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001820 if (*p == ')')
1821 {
1822 if (p[1] == ':')
1823 p = skip_type(skipwhite(p + 2));
1824 else
1825 p = skipwhite(p + 1);
1826 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001827 }
1828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 return p;
1830}
1831
1832/*
1833 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001834 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 * Returns NULL in case of failure.
1836 */
1837 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001838parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839{
1840 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001841 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842
1843 if (**arg != '<')
1844 {
1845 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001846 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 else
1848 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001849 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 }
1851 *arg = skipwhite(*arg + 1);
1852
Bram Moolenaard77a8522020-04-03 21:59:57 +02001853 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854
1855 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001856 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 {
1858 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001859 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 }
1861 ++*arg;
1862
1863 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001864 return get_list_type(member_type, type_gap);
1865 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866}
1867
1868/*
1869 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001870 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 */
1872 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001873parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874{
1875 char_u *p = *arg;
1876 size_t len;
1877
1878 // skip over the first word
1879 while (ASCII_ISALNUM(*p) || *p == '_')
1880 ++p;
1881 len = p - *arg;
1882
1883 switch (**arg)
1884 {
1885 case 'a':
1886 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1887 {
1888 *arg += len;
1889 return &t_any;
1890 }
1891 break;
1892 case 'b':
1893 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1894 {
1895 *arg += len;
1896 return &t_bool;
1897 }
1898 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1899 {
1900 *arg += len;
1901 return &t_blob;
1902 }
1903 break;
1904 case 'c':
1905 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1906 {
1907 *arg += len;
1908 return &t_channel;
1909 }
1910 break;
1911 case 'd':
1912 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1913 {
1914 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001915 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 }
1917 break;
1918 case 'f':
1919 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1920 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001921#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 *arg += len;
1923 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001924#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001925 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001926 return &t_any;
1927#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 }
1929 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1930 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001931 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001932 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001933 int argcount = -1;
1934 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001935 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001936 type_T *arg_type[MAX_FUNC_ARGS + 1];
1937
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001938 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001940 if (**arg == '(')
1941 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001942 // "func" may or may not return a value, "func()" does
1943 // not return a value.
1944 ret_type = &t_void;
1945
Bram Moolenaard77a8522020-04-03 21:59:57 +02001946 p = ++*arg;
1947 argcount = 0;
1948 while (*p != NUL && *p != ')')
1949 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001950 if (*p == '?')
1951 {
1952 if (first_optional == -1)
1953 first_optional = argcount;
1954 ++p;
1955 }
1956 else if (first_optional != -1)
1957 {
1958 emsg(_("E1007: mandatory argument after optional argument"));
1959 return &t_any;
1960 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001961 else if (STRNCMP(p, "...", 3) == 0)
1962 {
1963 flags |= TTFLAG_VARARGS;
1964 p += 3;
1965 }
1966
1967 arg_type[argcount++] = parse_type(&p, type_gap);
1968
1969 // Nothing comes after "...{type}".
1970 if (flags & TTFLAG_VARARGS)
1971 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001972
Bram Moolenaard77a8522020-04-03 21:59:57 +02001973 if (*p != ',' && *skipwhite(p) == ',')
1974 {
1975 semsg(_(e_no_white_before), ",");
1976 return &t_any;
1977 }
1978 if (*p == ',')
1979 {
1980 ++p;
1981 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001982 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001983 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001984 return &t_any;
1985 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001986 }
1987 p = skipwhite(p);
1988 if (argcount == MAX_FUNC_ARGS)
1989 {
1990 emsg(_("E740: Too many argument types"));
1991 return &t_any;
1992 }
1993 }
1994
1995 p = skipwhite(p);
1996 if (*p != ')')
1997 {
1998 emsg(_(e_missing_close));
1999 return &t_any;
2000 }
2001 *arg = p + 1;
2002 }
2003 if (**arg == ':')
2004 {
2005 // parse return type
2006 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002007 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002008 semsg(_(e_white_after), ":");
2009 *arg = skipwhite(*arg);
2010 ret_type = parse_type(arg, type_gap);
2011 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002012 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002013 type = get_func_type(ret_type, argcount, type_gap);
2014 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002015 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002016 type = alloc_func_type(ret_type, argcount, type_gap);
2017 type->tt_flags = flags;
2018 if (argcount > 0)
2019 {
2020 type->tt_argcount = argcount;
2021 type->tt_min_argcount = first_optional == -1
2022 ? argcount : first_optional;
2023 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002024 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002025 return &t_any;
2026 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002027 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002028 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002029 }
2030 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 }
2032 break;
2033 case 'j':
2034 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2035 {
2036 *arg += len;
2037 return &t_job;
2038 }
2039 break;
2040 case 'l':
2041 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2042 {
2043 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002044 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 }
2046 break;
2047 case 'n':
2048 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2049 {
2050 *arg += len;
2051 return &t_number;
2052 }
2053 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 case 's':
2055 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2056 {
2057 *arg += len;
2058 return &t_string;
2059 }
2060 break;
2061 case 'v':
2062 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2063 {
2064 *arg += len;
2065 return &t_void;
2066 }
2067 break;
2068 }
2069
2070 semsg(_("E1010: Type not recognized: %s"), *arg);
2071 return &t_any;
2072}
2073
2074/*
2075 * Check if "type1" and "type2" are exactly the same.
2076 */
2077 static int
2078equal_type(type_T *type1, type_T *type2)
2079{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002080 int i;
2081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 if (type1->tt_type != type2->tt_type)
2083 return FALSE;
2084 switch (type1->tt_type)
2085 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002087 case VAR_ANY:
2088 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 case VAR_SPECIAL:
2090 case VAR_BOOL:
2091 case VAR_NUMBER:
2092 case VAR_FLOAT:
2093 case VAR_STRING:
2094 case VAR_BLOB:
2095 case VAR_JOB:
2096 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002097 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 case VAR_LIST:
2099 case VAR_DICT:
2100 return equal_type(type1->tt_member, type2->tt_member);
2101 case VAR_FUNC:
2102 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002103 if (!equal_type(type1->tt_member, type2->tt_member)
2104 || type1->tt_argcount != type2->tt_argcount)
2105 return FALSE;
2106 if (type1->tt_argcount < 0
2107 || type1->tt_args == NULL || type2->tt_args == NULL)
2108 return TRUE;
2109 for (i = 0; i < type1->tt_argcount; ++i)
2110 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2111 return FALSE;
2112 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 }
2114 return TRUE;
2115}
2116
2117/*
2118 * Find the common type of "type1" and "type2" and put it in "dest".
2119 * "type2" and "dest" may be the same.
2120 */
2121 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002122common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123{
2124 if (equal_type(type1, type2))
2125 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002126 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 return;
2128 }
2129
2130 if (type1->tt_type == type2->tt_type)
2131 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2133 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002134 type_T *common;
2135
Bram Moolenaard77a8522020-04-03 21:59:57 +02002136 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002137 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002138 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002139 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002140 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 return;
2142 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002143 if (type1->tt_type == VAR_FUNC)
2144 {
2145 type_T *common;
2146
2147 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2148 if (type1->tt_argcount == type2->tt_argcount
2149 && type1->tt_argcount >= 0)
2150 {
2151 int argcount = type1->tt_argcount;
2152 int i;
2153
2154 *dest = alloc_func_type(common, argcount, type_gap);
2155 if (type1->tt_args != NULL && type2->tt_args != NULL)
2156 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002157 if (func_type_add_arg_types(*dest, argcount,
2158 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002159 for (i = 0; i < argcount; ++i)
2160 common_type(type1->tt_args[i], type2->tt_args[i],
2161 &(*dest)->tt_args[i], type_gap);
2162 }
2163 }
2164 else
2165 *dest = alloc_func_type(common, -1, type_gap);
2166 return;
2167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 }
2169
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002170 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171}
2172
2173 char *
2174vartype_name(vartype_T type)
2175{
2176 switch (type)
2177 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002178 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002179 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 case VAR_SPECIAL: return "special";
2182 case VAR_BOOL: return "bool";
2183 case VAR_NUMBER: return "number";
2184 case VAR_FLOAT: return "float";
2185 case VAR_STRING: return "string";
2186 case VAR_BLOB: return "blob";
2187 case VAR_JOB: return "job";
2188 case VAR_CHANNEL: return "channel";
2189 case VAR_LIST: return "list";
2190 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002191
2192 case VAR_FUNC:
2193 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002195 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196}
2197
2198/*
2199 * Return the name of a type.
2200 * The result may be in allocated memory, in which case "tofree" is set.
2201 */
2202 char *
2203type_name(type_T *type, char **tofree)
2204{
2205 char *name = vartype_name(type->tt_type);
2206
2207 *tofree = NULL;
2208 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2209 {
2210 char *member_free;
2211 char *member_name = type_name(type->tt_member, &member_free);
2212 size_t len;
2213
2214 len = STRLEN(name) + STRLEN(member_name) + 3;
2215 *tofree = alloc(len);
2216 if (*tofree != NULL)
2217 {
2218 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2219 vim_free(member_free);
2220 return *tofree;
2221 }
2222 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002223 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002224 {
2225 garray_T ga;
2226 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002227 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002228
2229 ga_init2(&ga, 1, 100);
2230 if (ga_grow(&ga, 20) == FAIL)
2231 return "[unknown]";
2232 *tofree = ga.ga_data;
2233 STRCPY(ga.ga_data, "func(");
2234 ga.ga_len += 5;
2235
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002236 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002237 {
2238 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002239 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002240 int len;
2241
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002242 if (type->tt_args == NULL)
2243 arg_type = "[unknown]";
2244 else
2245 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002246 if (i > 0)
2247 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002248 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002249 ga.ga_len += 2;
2250 }
2251 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002252 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002253 {
2254 vim_free(arg_free);
2255 return "[unknown]";
2256 }
2257 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002258 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002259 {
2260 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2261 ga.ga_len += 3;
2262 }
2263 else if (i >= type->tt_min_argcount)
2264 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002265 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002266 ga.ga_len += len;
2267 vim_free(arg_free);
2268 }
2269
2270 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002271 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002272 else
2273 {
2274 char *ret_free;
2275 char *ret_name = type_name(type->tt_member, &ret_free);
2276 int len;
2277
2278 len = (int)STRLEN(ret_name) + 4;
2279 if (ga_grow(&ga, len) == FAIL)
2280 {
2281 vim_free(ret_free);
2282 return "[unknown]";
2283 }
2284 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002285 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2286 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002287 vim_free(ret_free);
2288 }
2289 return ga.ga_data;
2290 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291
2292 return name;
2293}
2294
2295/*
2296 * Find "name" in script-local items of script "sid".
2297 * Returns the index in "sn_var_vals" if found.
2298 * If found but not in "sn_var_vals" returns -1.
2299 * If not found returns -2.
2300 */
2301 int
2302get_script_item_idx(int sid, char_u *name, int check_writable)
2303{
2304 hashtab_T *ht;
2305 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002306 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 int idx;
2308
2309 // First look the name up in the hashtable.
2310 if (sid <= 0 || sid > script_items.ga_len)
2311 return -1;
2312 ht = &SCRIPT_VARS(sid);
2313 di = find_var_in_ht(ht, 0, name, TRUE);
2314 if (di == NULL)
2315 return -2;
2316
2317 // Now find the svar_T index in sn_var_vals.
2318 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2319 {
2320 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2321
2322 if (sv->sv_tv == &di->di_tv)
2323 {
2324 if (check_writable && sv->sv_const)
2325 semsg(_(e_readonlyvar), name);
2326 return idx;
2327 }
2328 }
2329 return -1;
2330}
2331
2332/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002333 * Find "name" in imported items of the current script or in "cctx" if not
2334 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 */
2336 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002337find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002339 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 int idx;
2341
2342 if (cctx != NULL)
2343 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2344 {
2345 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2346 + idx;
2347
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002348 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2349 : STRLEN(import->imp_name) == len
2350 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 return import;
2352 }
2353
2354 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2355 {
2356 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2357
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002358 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2359 : STRLEN(import->imp_name) == len
2360 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 return import;
2362 }
2363 return NULL;
2364}
2365
2366/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002367 * Free all imported variables.
2368 */
2369 static void
2370free_imported(cctx_T *cctx)
2371{
2372 int idx;
2373
2374 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2375 {
2376 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2377
2378 vim_free(import->imp_name);
2379 }
2380 ga_clear(&cctx->ctx_imports);
2381}
2382
2383/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002384 * Return TRUE if "p" points at a "#" but not at "#{".
2385 */
2386 static int
2387comment_start(char_u *p)
2388{
2389 return p[0] == '#' && p[1] != '{';
2390}
2391
2392/*
2393 * Return a pointer to the next line that isn't empty or only contains a
2394 * comment. Skips over white space.
2395 * Returns NULL if there is none.
2396 */
2397 static char_u *
2398peek_next_line(cctx_T *cctx)
2399{
2400 int lnum = cctx->ctx_lnum;
2401
2402 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2403 {
2404 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002405 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002406
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002407 if (line == NULL)
2408 break;
2409 p = skipwhite(line);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002410 if (*p != NUL && !comment_start(p))
2411 return p;
2412 }
2413 return NULL;
2414}
2415
2416/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002417 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002418 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002419 * Returns NULL when at the end.
2420 */
2421 static char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002422next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002423{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002424 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002425
2426 do
2427 {
2428 ++cctx->ctx_lnum;
2429 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002430 {
2431 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002432 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002433 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002434 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002435 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002436 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002437 } while (line == NULL || *skipwhite(line) == NUL
2438 || (skip_comment && comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002439 return line;
2440}
2441
2442/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002443 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002444 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002445 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2446 */
2447 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002448may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002449{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002450 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002451 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002452 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002453
2454 if (next == NULL)
2455 return FAIL;
2456 *arg = skipwhite(next);
2457 }
2458 return OK;
2459}
2460
Bram Moolenaara5565e42020-05-09 15:44:01 +02002461// Structure passed between the compile_expr* functions to keep track of
2462// constants that have been parsed but for which no code was produced yet. If
2463// possible expressions on these constants are applied at compile time. If
2464// that is not possible, the code to push the constants needs to be generated
2465// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002466// Using 50 should be more than enough of 5 levels of ().
2467#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002468typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002469 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002470 int pp_used; // active entries in pp_tv[]
2471} ppconst_T;
2472
Bram Moolenaar1c747212020-05-09 18:28:34 +02002473static int compile_expr0(char_u **arg, cctx_T *cctx);
2474static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2475
Bram Moolenaara5565e42020-05-09 15:44:01 +02002476/*
2477 * Generate a PUSH instruction for "tv".
2478 * "tv" will be consumed or cleared.
2479 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2480 */
2481 static int
2482generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2483{
2484 if (tv != NULL)
2485 {
2486 switch (tv->v_type)
2487 {
2488 case VAR_UNKNOWN:
2489 break;
2490 case VAR_BOOL:
2491 generate_PUSHBOOL(cctx, tv->vval.v_number);
2492 break;
2493 case VAR_SPECIAL:
2494 generate_PUSHSPEC(cctx, tv->vval.v_number);
2495 break;
2496 case VAR_NUMBER:
2497 generate_PUSHNR(cctx, tv->vval.v_number);
2498 break;
2499#ifdef FEAT_FLOAT
2500 case VAR_FLOAT:
2501 generate_PUSHF(cctx, tv->vval.v_float);
2502 break;
2503#endif
2504 case VAR_BLOB:
2505 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2506 tv->vval.v_blob = NULL;
2507 break;
2508 case VAR_STRING:
2509 generate_PUSHS(cctx, tv->vval.v_string);
2510 tv->vval.v_string = NULL;
2511 break;
2512 default:
2513 iemsg("constant type not supported");
2514 clear_tv(tv);
2515 return FAIL;
2516 }
2517 tv->v_type = VAR_UNKNOWN;
2518 }
2519 return OK;
2520}
2521
2522/*
2523 * Generate code for any ppconst entries.
2524 */
2525 static int
2526generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2527{
2528 int i;
2529 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002530 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002531
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002532 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002533 for (i = 0; i < ppconst->pp_used; ++i)
2534 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2535 ret = FAIL;
2536 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002537 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002538 return ret;
2539}
2540
2541/*
2542 * Clear ppconst constants. Used when failing.
2543 */
2544 static void
2545clear_ppconst(ppconst_T *ppconst)
2546{
2547 int i;
2548
2549 for (i = 0; i < ppconst->pp_used; ++i)
2550 clear_tv(&ppconst->pp_tv[i]);
2551 ppconst->pp_used = 0;
2552}
2553
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002554/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002555 * Generate an instruction to load script-local variable "name", without the
2556 * leading "s:".
2557 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 */
2559 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002560compile_load_scriptvar(
2561 cctx_T *cctx,
2562 char_u *name, // variable NUL terminated
2563 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002564 char_u **end, // end of variable
2565 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002567 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2569 imported_T *import;
2570
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002571 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002573 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002574 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2575 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 }
2577 if (idx >= 0)
2578 {
2579 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2580
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002581 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 current_sctx.sc_sid, idx, sv->sv_type);
2583 return OK;
2584 }
2585
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002586 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 if (import != NULL)
2588 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002589 if (import->imp_all)
2590 {
2591 char_u *p = skipwhite(*end);
2592 int name_len;
2593 ufunc_T *ufunc;
2594 type_T *type;
2595
2596 // Used "import * as Name", need to lookup the member.
2597 if (*p != '.')
2598 {
2599 semsg(_("E1060: expected dot after name: %s"), start);
2600 return FAIL;
2601 }
2602 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002603 if (VIM_ISWHITE(*p))
2604 {
2605 emsg(_("E1074: no white space allowed after dot"));
2606 return FAIL;
2607 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002608
2609 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2610 // TODO: what if it is a function?
2611 if (idx < 0)
2612 return FAIL;
2613 *end = p;
2614
2615 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2616 import->imp_sid,
2617 idx,
2618 type);
2619 }
2620 else
2621 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002622 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002623 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2624 import->imp_sid,
2625 import->imp_var_vals_idx,
2626 import->imp_type);
2627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 return OK;
2629 }
2630
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002631 if (error)
2632 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 return FAIL;
2634}
2635
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002636 static int
2637generate_funcref(cctx_T *cctx, char_u *name)
2638{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002639 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002640
2641 if (ufunc == NULL)
2642 return FAIL;
2643
2644 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2645}
2646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647/*
2648 * Compile a variable name into a load instruction.
2649 * "end" points to just after the name.
2650 * When "error" is FALSE do not give an error when not found.
2651 */
2652 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002653compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654{
2655 type_T *type;
2656 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002657 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002659 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660
2661 if (*(*arg + 1) == ':')
2662 {
2663 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002664 if (end <= *arg + 2)
2665 name = vim_strsave((char_u *)"[empty]");
2666 else
2667 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 if (name == NULL)
2669 return FAIL;
2670
2671 if (**arg == 'v')
2672 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002673 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 }
2675 else if (**arg == 'g')
2676 {
2677 // Global variables can be defined later, thus we don't check if it
2678 // exists, give error at runtime.
2679 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2680 }
2681 else if (**arg == 's')
2682 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002683 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002685 else if (**arg == 'b')
2686 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002687 // Buffer-local variables can be defined later, thus we don't check
2688 // if it exists, give error at runtime.
2689 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002690 }
2691 else if (**arg == 'w')
2692 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002693 // Window-local variables can be defined later, thus we don't check
2694 // if it exists, give error at runtime.
2695 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002696 }
2697 else if (**arg == 't')
2698 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002699 // Tabpage-local variables can be defined later, thus we don't
2700 // check if it exists, give error at runtime.
2701 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002702 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 else
2704 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002705 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 goto theend;
2707 }
2708 }
2709 else
2710 {
2711 size_t len = end - *arg;
2712 int idx;
2713 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002714 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715
2716 name = vim_strnsave(*arg, end - *arg);
2717 if (name == NULL)
2718 return FAIL;
2719
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002720 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002722 if (!gen_load_outer)
2723 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 }
2725 else
2726 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002727 lvar_T *lvar = lookup_local(*arg, len, cctx);
2728
2729 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002731 type = lvar->lv_type;
2732 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002733 if (lvar->lv_from_outer)
2734 gen_load_outer = TRUE;
2735 else
2736 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 }
2738 else
2739 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002740 // "var" can be script-local even without using "s:" if it
2741 // already exists.
2742 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2743 == SCRIPT_VERSION_VIM9
2744 || lookup_script(*arg, len) == OK)
2745 res = compile_load_scriptvar(cctx, name, *arg, &end,
2746 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002747
Bram Moolenaara5565e42020-05-09 15:44:01 +02002748 // When the name starts with an uppercase letter or "x:" it
2749 // can be a user defined function.
2750 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2751 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 }
2753 }
2754 if (gen_load)
2755 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002756 if (gen_load_outer)
2757 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 }
2759
2760 *arg = end;
2761
2762theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002763 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 semsg(_(e_var_notfound), name);
2765 vim_free(name);
2766 return res;
2767}
2768
2769/*
2770 * Compile the argument expressions.
2771 * "arg" points to just after the "(" and is advanced to after the ")"
2772 */
2773 static int
2774compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2775{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002776 char_u *p = *arg;
2777 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778
Bram Moolenaare6085c52020-04-12 20:19:16 +02002779 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002781 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2782 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002783 if (*p == ')')
2784 {
2785 *arg = p + 1;
2786 return OK;
2787 }
2788
Bram Moolenaara5565e42020-05-09 15:44:01 +02002789 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 return FAIL;
2791 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002792
2793 if (*p != ',' && *skipwhite(p) == ',')
2794 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002795 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002796 p = skipwhite(p);
2797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002799 {
2800 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002801 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002802 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002803 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002804 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002805 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002807failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002808 emsg(_(e_missing_close));
2809 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810}
2811
2812/*
2813 * Compile a function call: name(arg1, arg2)
2814 * "arg" points to "name", "arg + varlen" to the "(".
2815 * "argcount_init" is 1 for "value->method()"
2816 * Instructions:
2817 * EVAL arg1
2818 * EVAL arg2
2819 * BCALL / DCALL / UCALL
2820 */
2821 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002822compile_call(
2823 char_u **arg,
2824 size_t varlen,
2825 cctx_T *cctx,
2826 ppconst_T *ppconst,
2827 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828{
2829 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002830 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 int argcount = argcount_init;
2832 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002833 char_u fname_buf[FLEN_FIXED + 1];
2834 char_u *tofree = NULL;
2835 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002837 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838
Bram Moolenaara5565e42020-05-09 15:44:01 +02002839 // we can evaluate "has('name')" at compile time
2840 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2841 {
2842 char_u *s = skipwhite(*arg + varlen + 1);
2843 typval_T argvars[2];
2844
2845 argvars[0].v_type = VAR_UNKNOWN;
2846 if (*s == '"')
2847 (void)get_string_tv(&s, &argvars[0], TRUE);
2848 else if (*s == '\'')
2849 (void)get_lit_string_tv(&s, &argvars[0], TRUE);
2850 s = skipwhite(s);
2851 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2852 {
2853 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2854
2855 *arg = s + 1;
2856 argvars[1].v_type = VAR_UNKNOWN;
2857 tv->v_type = VAR_NUMBER;
2858 tv->vval.v_number = 0;
2859 f_has(argvars, tv);
2860 clear_tv(&argvars[0]);
2861 ++ppconst->pp_used;
2862 return OK;
2863 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002864 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002865 }
2866
2867 if (generate_ppconst(cctx, ppconst) == FAIL)
2868 return FAIL;
2869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 if (varlen >= sizeof(namebuf))
2871 {
2872 semsg(_("E1011: name too long: %s"), name);
2873 return FAIL;
2874 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002875 vim_strncpy(namebuf, *arg, varlen);
2876 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877
2878 *arg = skipwhite(*arg + varlen + 1);
2879 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002880 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002882 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 {
2884 int idx;
2885
2886 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002887 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002889 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002890 else
2891 semsg(_(e_unknownfunc), namebuf);
2892 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 }
2894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002896 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002898 {
2899 res = generate_CALL(cctx, ufunc, argcount);
2900 goto theend;
2901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902
2903 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002904 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002906 if (STRNCMP(namebuf, "g:", 2) != 0
2907 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002908 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002909 garray_T *stack = &cctx->ctx_type_stack;
2910 type_T *type;
2911
2912 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2913 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002914 goto theend;
2915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002917 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002918 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002919 if (STRNCMP(namebuf, "g:", 2) == 0)
2920 res = generate_UCALL(cctx, name, argcount);
2921 else
2922 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002923
2924theend:
2925 vim_free(tofree);
2926 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927}
2928
2929// like NAMESPACE_CHAR but with 'a' and 'l'.
2930#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2931
2932/*
2933 * Find the end of a variable or function name. Unlike find_name_end() this
2934 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002935 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 * Return a pointer to just after the name. Equal to "arg" if there is no
2937 * valid name.
2938 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002939 static char_u *
2940to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941{
2942 char_u *p;
2943
2944 // Quick check for valid starting character.
2945 if (!eval_isnamec1(*arg))
2946 return arg;
2947
2948 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2949 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2950 // and can be used in slice "[n:]".
2951 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002952 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2954 break;
2955 return p;
2956}
2957
2958/*
2959 * Like to_name_end() but also skip over a list or dict constant.
2960 */
2961 char_u *
2962to_name_const_end(char_u *arg)
2963{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002964 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 typval_T rettv;
2966
2967 if (p == arg && *arg == '[')
2968 {
2969
2970 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar32e35112020-05-14 22:41:15 +02002971 if (get_list_tv(&p, &rettv, 0, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 p = arg;
2973 }
2974 else if (p == arg && *arg == '#' && arg[1] == '{')
2975 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002976 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 ++p;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002978 if (eval_dict(&p, &rettv, 0, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 p = arg;
2980 }
2981 else if (p == arg && *arg == '{')
2982 {
2983 int ret = get_lambda_tv(&p, &rettv, FALSE);
2984
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002985 // Can be "{x -> ret}()".
2986 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 if (ret == NOTDONE)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002988 ret = eval_dict(&p, &rettv, 0, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 if (ret != OK)
2990 p = arg;
2991 }
2992
2993 return p;
2994}
2995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996/*
2997 * parse a list: [expr, expr]
2998 * "*arg" points to the '['.
2999 */
3000 static int
3001compile_list(char_u **arg, cctx_T *cctx)
3002{
3003 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003004 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 int count = 0;
3006
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003007 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003009 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003010 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003011 semsg(_(e_list_end), *arg);
3012 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003013 }
3014 if (*p == ']')
3015 {
3016 ++p;
3017 // Allow for following comment, after at least one space.
3018 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
3019 p += STRLEN(p);
3020 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003021 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003022 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 break;
3024 ++count;
3025 if (*p == ',')
3026 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003027 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 p = skipwhite(p);
3029 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003030 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031
3032 generate_NEWLIST(cctx, count);
3033 return OK;
3034}
3035
3036/*
3037 * parse a lambda: {arg, arg -> expr}
3038 * "*arg" points to the '{'.
3039 */
3040 static int
3041compile_lambda(char_u **arg, cctx_T *cctx)
3042{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 typval_T rettv;
3044 ufunc_T *ufunc;
3045
3046 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01003047 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003051 ++ufunc->uf_refcount;
3052 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003053 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054
3055 // The function will have one line: "return {expr}".
3056 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003057 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003059 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003060 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 return FAIL;
3062}
3063
3064/*
3065 * Compile a lamda call: expr->{lambda}(args)
3066 * "arg" points to the "{".
3067 */
3068 static int
3069compile_lambda_call(char_u **arg, cctx_T *cctx)
3070{
3071 ufunc_T *ufunc;
3072 typval_T rettv;
3073 int argcount = 1;
3074 int ret = FAIL;
3075
3076 // Get the funcref in "rettv".
3077 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
3078 return FAIL;
3079
3080 if (**arg != '(')
3081 {
3082 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003083 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 else
3085 semsg(_(e_missing_paren), "lambda");
3086 clear_tv(&rettv);
3087 return FAIL;
3088 }
3089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 ufunc = rettv.vval.v_partial->pt_func;
3091 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003092 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003093 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003094
3095 // The function will have one line: "return {expr}".
3096 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003097 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098
3099 // compile the arguments
3100 *arg = skipwhite(*arg + 1);
3101 if (compile_arguments(arg, cctx, &argcount) == OK)
3102 // call the compiled function
3103 ret = generate_CALL(cctx, ufunc, argcount);
3104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 return ret;
3106}
3107
3108/*
3109 * parse a dict: {'key': val} or #{key: val}
3110 * "*arg" points to the '{'.
3111 */
3112 static int
3113compile_dict(char_u **arg, cctx_T *cctx, int literal)
3114{
3115 garray_T *instr = &cctx->ctx_instr;
3116 int count = 0;
3117 dict_T *d = dict_alloc();
3118 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003119 char_u *whitep = *arg;
3120 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121
3122 if (d == NULL)
3123 return FAIL;
3124 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003125 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 {
3127 char_u *key = NULL;
3128
Bram Moolenaar23c55272020-06-21 16:58:13 +02003129 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003130 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003131 *arg = NULL;
3132 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003133 }
3134
3135 if (**arg == '}')
3136 break;
3137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 if (literal)
3139 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003140 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141
Bram Moolenaar2c330432020-04-13 14:41:35 +02003142 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 {
3144 semsg(_("E1014: Invalid key: %s"), *arg);
3145 return FAIL;
3146 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003147 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 if (generate_PUSHS(cctx, key) == FAIL)
3149 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003150 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 }
3152 else
3153 {
3154 isn_T *isn;
3155
Bram Moolenaara5565e42020-05-09 15:44:01 +02003156 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 return FAIL;
3158 // TODO: check type is string
3159 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3160 if (isn->isn_type == ISN_PUSHS)
3161 key = isn->isn_arg.string;
3162 }
3163
3164 // Check for duplicate keys, if using string keys.
3165 if (key != NULL)
3166 {
3167 item = dict_find(d, key, -1);
3168 if (item != NULL)
3169 {
3170 semsg(_(e_duplicate_key), key);
3171 goto failret;
3172 }
3173 item = dictitem_alloc(key);
3174 if (item != NULL)
3175 {
3176 item->di_tv.v_type = VAR_UNKNOWN;
3177 item->di_tv.v_lock = 0;
3178 if (dict_add(d, item) == FAIL)
3179 dictitem_free(item);
3180 }
3181 }
3182
3183 *arg = skipwhite(*arg);
3184 if (**arg != ':')
3185 {
3186 semsg(_(e_missing_dict_colon), *arg);
3187 return FAIL;
3188 }
3189
Bram Moolenaar2c330432020-04-13 14:41:35 +02003190 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003192 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003193 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003194 *arg = NULL;
3195 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003196 }
3197
Bram Moolenaara5565e42020-05-09 15:44:01 +02003198 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 return FAIL;
3200 ++count;
3201
Bram Moolenaar2c330432020-04-13 14:41:35 +02003202 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003203 *arg = skipwhite(*arg);
3204 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003205 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003206 *arg = NULL;
3207 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 if (**arg == '}')
3210 break;
3211 if (**arg != ',')
3212 {
3213 semsg(_(e_missing_dict_comma), *arg);
3214 goto failret;
3215 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003216 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 *arg = skipwhite(*arg + 1);
3218 }
3219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 *arg = *arg + 1;
3221
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003222 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003223 p = skipwhite(*arg);
3224 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003225 *arg += STRLEN(*arg);
3226
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 dict_unref(d);
3228 return generate_NEWDICT(cctx, count);
3229
3230failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003231 if (*arg == NULL)
3232 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 dict_unref(d);
3234 return FAIL;
3235}
3236
3237/*
3238 * Compile "&option".
3239 */
3240 static int
3241compile_get_option(char_u **arg, cctx_T *cctx)
3242{
3243 typval_T rettv;
3244 char_u *start = *arg;
3245 int ret;
3246
3247 // parse the option and get the current value to get the type.
3248 rettv.v_type = VAR_UNKNOWN;
3249 ret = get_option_tv(arg, &rettv, TRUE);
3250 if (ret == OK)
3251 {
3252 // include the '&' in the name, get_option_tv() expects it.
3253 char_u *name = vim_strnsave(start, *arg - start);
3254 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3255
3256 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3257 vim_free(name);
3258 }
3259 clear_tv(&rettv);
3260
3261 return ret;
3262}
3263
3264/*
3265 * Compile "$VAR".
3266 */
3267 static int
3268compile_get_env(char_u **arg, cctx_T *cctx)
3269{
3270 char_u *start = *arg;
3271 int len;
3272 int ret;
3273 char_u *name;
3274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 ++*arg;
3276 len = get_env_len(arg);
3277 if (len == 0)
3278 {
3279 semsg(_(e_syntax_at), start - 1);
3280 return FAIL;
3281 }
3282
3283 // include the '$' in the name, get_env_tv() expects it.
3284 name = vim_strnsave(start, len + 1);
3285 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3286 vim_free(name);
3287 return ret;
3288}
3289
3290/*
3291 * Compile "@r".
3292 */
3293 static int
3294compile_get_register(char_u **arg, cctx_T *cctx)
3295{
3296 int ret;
3297
3298 ++*arg;
3299 if (**arg == NUL)
3300 {
3301 semsg(_(e_syntax_at), *arg - 1);
3302 return FAIL;
3303 }
3304 if (!valid_yank_reg(**arg, TRUE))
3305 {
3306 emsg_invreg(**arg);
3307 return FAIL;
3308 }
3309 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3310 ++*arg;
3311 return ret;
3312}
3313
3314/*
3315 * Apply leading '!', '-' and '+' to constant "rettv".
3316 */
3317 static int
3318apply_leader(typval_T *rettv, char_u *start, char_u *end)
3319{
3320 char_u *p = end;
3321
3322 // this works from end to start
3323 while (p > start)
3324 {
3325 --p;
3326 if (*p == '-' || *p == '+')
3327 {
3328 // only '-' has an effect, for '+' we only check the type
3329#ifdef FEAT_FLOAT
3330 if (rettv->v_type == VAR_FLOAT)
3331 {
3332 if (*p == '-')
3333 rettv->vval.v_float = -rettv->vval.v_float;
3334 }
3335 else
3336#endif
3337 {
3338 varnumber_T val;
3339 int error = FALSE;
3340
3341 // tv_get_number_chk() accepts a string, but we don't want that
3342 // here
3343 if (check_not_string(rettv) == FAIL)
3344 return FAIL;
3345 val = tv_get_number_chk(rettv, &error);
3346 clear_tv(rettv);
3347 if (error)
3348 return FAIL;
3349 if (*p == '-')
3350 val = -val;
3351 rettv->v_type = VAR_NUMBER;
3352 rettv->vval.v_number = val;
3353 }
3354 }
3355 else
3356 {
3357 int v = tv2bool(rettv);
3358
3359 // '!' is permissive in the type.
3360 clear_tv(rettv);
3361 rettv->v_type = VAR_BOOL;
3362 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3363 }
3364 }
3365 return OK;
3366}
3367
3368/*
3369 * Recognize v: variables that are constants and set "rettv".
3370 */
3371 static void
3372get_vim_constant(char_u **arg, typval_T *rettv)
3373{
3374 if (STRNCMP(*arg, "v:true", 6) == 0)
3375 {
3376 rettv->v_type = VAR_BOOL;
3377 rettv->vval.v_number = VVAL_TRUE;
3378 *arg += 6;
3379 }
3380 else if (STRNCMP(*arg, "v:false", 7) == 0)
3381 {
3382 rettv->v_type = VAR_BOOL;
3383 rettv->vval.v_number = VVAL_FALSE;
3384 *arg += 7;
3385 }
3386 else if (STRNCMP(*arg, "v:null", 6) == 0)
3387 {
3388 rettv->v_type = VAR_SPECIAL;
3389 rettv->vval.v_number = VVAL_NULL;
3390 *arg += 6;
3391 }
3392 else if (STRNCMP(*arg, "v:none", 6) == 0)
3393 {
3394 rettv->v_type = VAR_SPECIAL;
3395 rettv->vval.v_number = VVAL_NONE;
3396 *arg += 6;
3397 }
3398}
3399
Bram Moolenaar61a89812020-05-07 16:58:17 +02003400 static exptype_T
3401get_compare_type(char_u *p, int *len, int *type_is)
3402{
3403 exptype_T type = EXPR_UNKNOWN;
3404 int i;
3405
3406 switch (p[0])
3407 {
3408 case '=': if (p[1] == '=')
3409 type = EXPR_EQUAL;
3410 else if (p[1] == '~')
3411 type = EXPR_MATCH;
3412 break;
3413 case '!': if (p[1] == '=')
3414 type = EXPR_NEQUAL;
3415 else if (p[1] == '~')
3416 type = EXPR_NOMATCH;
3417 break;
3418 case '>': if (p[1] != '=')
3419 {
3420 type = EXPR_GREATER;
3421 *len = 1;
3422 }
3423 else
3424 type = EXPR_GEQUAL;
3425 break;
3426 case '<': if (p[1] != '=')
3427 {
3428 type = EXPR_SMALLER;
3429 *len = 1;
3430 }
3431 else
3432 type = EXPR_SEQUAL;
3433 break;
3434 case 'i': if (p[1] == 's')
3435 {
3436 // "is" and "isnot"; but not a prefix of a name
3437 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3438 *len = 5;
3439 i = p[*len];
3440 if (!isalnum(i) && i != '_')
3441 {
3442 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3443 *type_is = TRUE;
3444 }
3445 }
3446 break;
3447 }
3448 return type;
3449}
3450
3451/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 * Compile code to apply '-', '+' and '!'.
3453 */
3454 static int
3455compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3456{
3457 char_u *p = end;
3458
3459 // this works from end to start
3460 while (p > start)
3461 {
3462 --p;
3463 if (*p == '-' || *p == '+')
3464 {
3465 int negate = *p == '-';
3466 isn_T *isn;
3467
3468 // TODO: check type
3469 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3470 {
3471 --p;
3472 if (*p == '-')
3473 negate = !negate;
3474 }
3475 // only '-' has an effect, for '+' we only check the type
3476 if (negate)
3477 isn = generate_instr(cctx, ISN_NEGATENR);
3478 else
3479 isn = generate_instr(cctx, ISN_CHECKNR);
3480 if (isn == NULL)
3481 return FAIL;
3482 }
3483 else
3484 {
3485 int invert = TRUE;
3486
3487 while (p > start && p[-1] == '!')
3488 {
3489 --p;
3490 invert = !invert;
3491 }
3492 if (generate_2BOOL(cctx, invert) == FAIL)
3493 return FAIL;
3494 }
3495 }
3496 return OK;
3497}
3498
3499/*
3500 * Compile whatever comes after "name" or "name()".
3501 */
3502 static int
3503compile_subscript(
3504 char_u **arg,
3505 cctx_T *cctx,
3506 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003507 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003508 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509{
3510 for (;;)
3511 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003512 char_u *p = skipwhite(*arg);
3513
3514 if (*p == NUL || (VIM_ISWHITE(**arg) && comment_start(p)))
3515 {
3516 char_u *next = peek_next_line(cctx);
3517
3518 // If a following line starts with "->{" or "->X" advance to that
3519 // line, so that a line break before "->" is allowed.
3520 if (next != NULL && next[0] == '-' && next[1] == '>'
3521 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3522 {
3523 next = next_line_from_context(cctx, TRUE);
3524 if (next == NULL)
3525 return FAIL;
3526 *arg = skipwhite(next);
3527 }
3528 }
3529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 if (**arg == '(')
3531 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003532 garray_T *stack = &cctx->ctx_type_stack;
3533 type_T *type;
3534 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003536 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003537 return FAIL;
3538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003540 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 *arg = skipwhite(*arg + 1);
3543 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3544 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003545 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 return FAIL;
3547 }
3548 else if (**arg == '-' && (*arg)[1] == '>')
3549 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003550 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003551 return FAIL;
3552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 // something->method()
3554 // Apply the '!', '-' and '+' first:
3555 // -1.0->func() works like (-1.0)->func()
3556 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3557 return FAIL;
3558 *start_leader = end_leader; // don't apply again later
3559
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003560 p = *arg + 2;
3561 *arg = skipwhite(p);
3562 if (may_get_next_line(p, arg, cctx) == FAIL)
3563 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 if (**arg == '{')
3565 {
3566 // lambda call: list->{lambda}
3567 if (compile_lambda_call(arg, cctx) == FAIL)
3568 return FAIL;
3569 }
3570 else
3571 {
3572 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003573 p = *arg;
3574 if (ASCII_ISALPHA(*p) && p[1] == ':')
3575 p += 2;
3576 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 ;
3578 if (*p != '(')
3579 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003580 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 return FAIL;
3582 }
3583 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003584 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 return FAIL;
3586 }
3587 }
3588 else if (**arg == '[')
3589 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003590 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003591 type_T **typep;
3592
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003593 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003594 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003595 // TODO: blob index
3596 // TODO: more arguments
3597 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003598 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003599 return FAIL;
3600
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003601 p = *arg + 1;
3602 *arg = skipwhite(p);
3603 if (may_get_next_line(p, arg, cctx) == FAIL)
3604 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003605 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 return FAIL;
3607
3608 if (**arg != ']')
3609 {
3610 emsg(_(e_missbrac));
3611 return FAIL;
3612 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003613 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003615 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3616 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003617 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003618 if ((*typep)->tt_type == VAR_LIST)
3619 *typep = (*typep)->tt_member;
3620 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3621 return FAIL;
3622 }
3623 else if ((*typep)->tt_type == VAR_DICT)
3624 {
3625 *typep = (*typep)->tt_member;
3626 if (may_generate_2STRING(-1, cctx) == FAIL)
3627 return FAIL;
3628 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3629 return FAIL;
3630 }
3631 else
3632 {
3633 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003634 return FAIL;
3635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 }
3637 else if (**arg == '.' && (*arg)[1] != '.')
3638 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003639 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003640 return FAIL;
3641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 ++*arg;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003643 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3644 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003646 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 if (eval_isnamec1(*p))
3648 while (eval_isnamec(*p))
3649 MB_PTR_ADV(p);
3650 if (p == *arg)
3651 {
3652 semsg(_(e_syntax_at), *arg);
3653 return FAIL;
3654 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003655 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 return FAIL;
3657 *arg = p;
3658 }
3659 else
3660 break;
3661 }
3662
3663 // TODO - see handle_subscript():
3664 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3665 // Don't do this when "Func" is already a partial that was bound
3666 // explicitly (pt_auto is FALSE).
3667
3668 return OK;
3669}
3670
3671/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003672 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3673 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003675 * If the value is a constant "ppconst->pp_ret" will be set.
3676 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003677 *
3678 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 */
3680
3681/*
3682 * number number constant
3683 * 0zFFFFFFFF Blob constant
3684 * "string" string constant
3685 * 'string' literal string constant
3686 * &option-name option value
3687 * @r register contents
3688 * identifier variable value
3689 * function() function call
3690 * $VAR environment variable
3691 * (expression) nested expression
3692 * [expr, expr] List
3693 * {key: val, key: val} Dictionary
3694 * #{key: val, key: val} Dictionary with literal keys
3695 *
3696 * Also handle:
3697 * ! in front logical NOT
3698 * - in front unary minus
3699 * + in front unary plus (ignored)
3700 * trailing (arg) funcref/partial call
3701 * trailing [] subscript in String or List
3702 * trailing .name entry in Dictionary
3703 * trailing ->name() method call
3704 */
3705 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003706compile_expr7(
3707 char_u **arg,
3708 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003709 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 char_u *start_leader, *end_leader;
3712 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003713 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003714 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715
3716 /*
3717 * Skip '!', '-' and '+' characters. They are handled later.
3718 */
3719 start_leader = *arg;
3720 while (**arg == '!' || **arg == '-' || **arg == '+')
3721 *arg = skipwhite(*arg + 1);
3722 end_leader = *arg;
3723
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003724 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 switch (**arg)
3726 {
3727 /*
3728 * Number constant.
3729 */
3730 case '0': // also for blob starting with 0z
3731 case '1':
3732 case '2':
3733 case '3':
3734 case '4':
3735 case '5':
3736 case '6':
3737 case '7':
3738 case '8':
3739 case '9':
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003740 case '.': if (get_number_tv(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 return FAIL;
3742 break;
3743
3744 /*
3745 * String constant: "string".
3746 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003747 case '"': if (get_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 return FAIL;
3749 break;
3750
3751 /*
3752 * Literal string constant: 'str''ing'.
3753 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003754 case '\'': if (get_lit_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 return FAIL;
3756 break;
3757
3758 /*
3759 * Constant Vim variable.
3760 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003761 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 ret = NOTDONE;
3763 break;
3764
3765 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003766 * "true" constant
3767 */
3768 case 't': if (STRNCMP(*arg, "true", 4) == 0
3769 && !eval_isnamec((*arg)[4]))
3770 {
3771 *arg += 4;
3772 rettv->v_type = VAR_BOOL;
3773 rettv->vval.v_number = VVAL_TRUE;
3774 }
3775 else
3776 ret = NOTDONE;
3777 break;
3778
3779 /*
3780 * "false" constant
3781 */
3782 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3783 && !eval_isnamec((*arg)[5]))
3784 {
3785 *arg += 5;
3786 rettv->v_type = VAR_BOOL;
3787 rettv->vval.v_number = VVAL_FALSE;
3788 }
3789 else
3790 ret = NOTDONE;
3791 break;
3792
3793 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 * List: [expr, expr]
3795 */
3796 case '[': ret = compile_list(arg, cctx);
3797 break;
3798
3799 /*
3800 * Dictionary: #{key: val, key: val}
3801 */
3802 case '#': if ((*arg)[1] == '{')
3803 {
3804 ++*arg;
3805 ret = compile_dict(arg, cctx, TRUE);
3806 }
3807 else
3808 ret = NOTDONE;
3809 break;
3810
3811 /*
3812 * Lambda: {arg, arg -> expr}
3813 * Dictionary: {'key': val, 'key': val}
3814 */
3815 case '{': {
3816 char_u *start = skipwhite(*arg + 1);
3817
3818 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003819 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003821 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 if (ret != FAIL && *start == '>')
3823 ret = compile_lambda(arg, cctx);
3824 else
3825 ret = compile_dict(arg, cctx, FALSE);
3826 }
3827 break;
3828
3829 /*
3830 * Option value: &name
3831 */
3832 case '&': ret = compile_get_option(arg, cctx);
3833 break;
3834
3835 /*
3836 * Environment variable: $VAR.
3837 */
3838 case '$': ret = compile_get_env(arg, cctx);
3839 break;
3840
3841 /*
3842 * Register contents: @r.
3843 */
3844 case '@': ret = compile_get_register(arg, cctx);
3845 break;
3846 /*
3847 * nested expression: (expression).
3848 */
3849 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003850
3851 // recursive!
3852 if (ppconst->pp_used <= PPSIZE - 10)
3853 {
3854 ret = compile_expr1(arg, cctx, ppconst);
3855 }
3856 else
3857 {
3858 // Not enough space in ppconst, flush constants.
3859 if (generate_ppconst(cctx, ppconst) == FAIL)
3860 return FAIL;
3861 ret = compile_expr0(arg, cctx);
3862 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 *arg = skipwhite(*arg);
3864 if (**arg == ')')
3865 ++*arg;
3866 else if (ret == OK)
3867 {
3868 emsg(_(e_missing_close));
3869 ret = FAIL;
3870 }
3871 break;
3872
3873 default: ret = NOTDONE;
3874 break;
3875 }
3876 if (ret == FAIL)
3877 return FAIL;
3878
Bram Moolenaar1c747212020-05-09 18:28:34 +02003879 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 {
3881 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003882 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003884 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 return FAIL;
3886 }
3887 start_leader = end_leader; // don't apply again below
3888
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003889 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003890 clear_tv(rettv);
3891 else
3892 // A constant expression can possibly be handled compile time,
3893 // return the value instead of generating code.
3894 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 }
3896 else if (ret == NOTDONE)
3897 {
3898 char_u *p;
3899 int r;
3900
3901 if (!eval_isnamec1(**arg))
3902 {
3903 semsg(_("E1015: Name expected: %s"), *arg);
3904 return FAIL;
3905 }
3906
3907 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003908 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003910 {
3911 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3912 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003914 {
3915 if (generate_ppconst(cctx, ppconst) == FAIL)
3916 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 if (r == FAIL)
3920 return FAIL;
3921 }
3922
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003923 // Handle following "[]", ".member", etc.
3924 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003925 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003926 ppconst) == FAIL)
3927 return FAIL;
3928 if (ppconst->pp_used > 0)
3929 {
3930 // apply the '!', '-' and '+' before the constant
3931 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3932 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3933 return FAIL;
3934 return OK;
3935 }
3936 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003938 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939}
3940
3941/*
3942 * * number multiplication
3943 * / number division
3944 * % number modulo
3945 */
3946 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003947compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948{
3949 char_u *op;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003950 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003952 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003953 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 return FAIL;
3955
3956 /*
3957 * Repeat computing, until no "*", "/" or "%" is following.
3958 */
3959 for (;;)
3960 {
3961 op = skipwhite(*arg);
3962 if (*op != '*' && *op != '/' && *op != '%')
3963 break;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003964
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003965 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 {
3967 char_u buf[3];
3968
3969 vim_strncpy(buf, op, 1);
3970 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003971 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 }
3973 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003974 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003975 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003977 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003978 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003980
3981 if (ppconst->pp_used == ppconst_used + 2
3982 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3983 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003984 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003985 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3986 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003987 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003989 // both are numbers: compute the result
3990 switch (*op)
3991 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003992 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003993 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003994 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003995 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003996 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003997 break;
3998 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003999 tv1->vval.v_number = res;
4000 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004001 }
4002 else
4003 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004004 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004005 generate_two_op(cctx, op);
4006 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 }
4008
4009 return OK;
4010}
4011
4012/*
4013 * + number addition
4014 * - number subtraction
4015 * .. string concatenation
4016 */
4017 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004018compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019{
4020 char_u *op;
4021 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004022 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023
4024 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004025 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 return FAIL;
4027
4028 /*
4029 * Repeat computing, until no "+", "-" or ".." is following.
4030 */
4031 for (;;)
4032 {
4033 op = skipwhite(*arg);
4034 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
4035 break;
4036 oplen = (*op == '.' ? 2 : 1);
4037
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004038 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 {
4040 char_u buf[3];
4041
4042 vim_strncpy(buf, op, oplen);
4043 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004044 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 }
4046
4047 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004048 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004049 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004051 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004052 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 return FAIL;
4054
Bram Moolenaara5565e42020-05-09 15:44:01 +02004055 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004056 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004057 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4058 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4059 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4060 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004062 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4063 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004064
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004065 // concat/subtract/add constant numbers
4066 if (*op == '+')
4067 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4068 else if (*op == '-')
4069 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4070 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004071 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004072 // concatenate constant strings
4073 char_u *s1 = tv1->vval.v_string;
4074 char_u *s2 = tv2->vval.v_string;
4075 size_t len1 = STRLEN(s1);
4076
4077 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4078 if (tv1->vval.v_string == NULL)
4079 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004080 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004081 return FAIL;
4082 }
4083 mch_memmove(tv1->vval.v_string, s1, len1);
4084 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004085 vim_free(s1);
4086 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004087 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004088 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 }
4090 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004091 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004092 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004093 if (*op == '.')
4094 {
4095 if (may_generate_2STRING(-2, cctx) == FAIL
4096 || may_generate_2STRING(-1, cctx) == FAIL)
4097 return FAIL;
4098 generate_instr_drop(cctx, ISN_CONCAT, 1);
4099 }
4100 else
4101 generate_two_op(cctx, op);
4102 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 }
4104
4105 return OK;
4106}
4107
4108/*
4109 * expr5a == expr5b
4110 * expr5a =~ expr5b
4111 * expr5a != expr5b
4112 * expr5a !~ expr5b
4113 * expr5a > expr5b
4114 * expr5a >= expr5b
4115 * expr5a < expr5b
4116 * expr5a <= expr5b
4117 * expr5a is expr5b
4118 * expr5a isnot expr5b
4119 *
4120 * Produces instructions:
4121 * EVAL expr5a Push result of "expr5a"
4122 * EVAL expr5b Push result of "expr5b"
4123 * COMPARE one of the compare instructions
4124 */
4125 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004126compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127{
4128 exptype_T type = EXPR_UNKNOWN;
4129 char_u *p;
4130 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004132 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133
4134 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004135 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 return FAIL;
4137
4138 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004139 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140
4141 /*
4142 * If there is a comparative operator, use it.
4143 */
4144 if (type != EXPR_UNKNOWN)
4145 {
4146 int ic = FALSE; // Default: do not ignore case
4147
4148 if (type_is && (p[len] == '?' || p[len] == '#'))
4149 {
4150 semsg(_(e_invexpr2), *arg);
4151 return FAIL;
4152 }
4153 // extra question mark appended: ignore case
4154 if (p[len] == '?')
4155 {
4156 ic = TRUE;
4157 ++len;
4158 }
4159 // extra '#' appended: match case (ignored)
4160 else if (p[len] == '#')
4161 ++len;
4162 // nothing appended: match case
4163
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004164 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 {
4166 char_u buf[7];
4167
4168 vim_strncpy(buf, p, len);
4169 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004170 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 }
4172
4173 // get the second variable
4174 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004175 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004176 return FAIL;
4177
Bram Moolenaara5565e42020-05-09 15:44:01 +02004178 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 return FAIL;
4180
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 if (ppconst->pp_used == ppconst_used + 2)
4182 {
4183 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4184 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4185 int ret;
4186
4187 // Both sides are a constant, compute the result now.
4188 // First check for a valid combination of types, this is more
4189 // strict than typval_compare().
4190 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4191 ret = FAIL;
4192 else
4193 {
4194 ret = typval_compare(tv1, tv2, type, ic);
4195 tv1->v_type = VAR_BOOL;
4196 tv1->vval.v_number = tv1->vval.v_number
4197 ? VVAL_TRUE : VVAL_FALSE;
4198 clear_tv(tv2);
4199 --ppconst->pp_used;
4200 }
4201 return ret;
4202 }
4203
4204 generate_ppconst(cctx, ppconst);
4205 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 }
4207
4208 return OK;
4209}
4210
Bram Moolenaar7f141552020-05-09 17:35:53 +02004211static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213/*
4214 * Compile || or &&.
4215 */
4216 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004217compile_and_or(
4218 char_u **arg,
4219 cctx_T *cctx,
4220 char *op,
4221 ppconst_T *ppconst,
4222 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223{
4224 char_u *p = skipwhite(*arg);
4225 int opchar = *op;
4226
4227 if (p[0] == opchar && p[1] == opchar)
4228 {
4229 garray_T *instr = &cctx->ctx_instr;
4230 garray_T end_ga;
4231
4232 /*
4233 * Repeat until there is no following "||" or "&&"
4234 */
4235 ga_init2(&end_ga, sizeof(int), 10);
4236 while (p[0] == opchar && p[1] == opchar)
4237 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004238 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4239 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004241 return FAIL;
4242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243
Bram Moolenaara5565e42020-05-09 15:44:01 +02004244 // TODO: use ppconst if the value is a constant
4245 generate_ppconst(cctx, ppconst);
4246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 if (ga_grow(&end_ga, 1) == FAIL)
4248 {
4249 ga_clear(&end_ga);
4250 return FAIL;
4251 }
4252 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4253 ++end_ga.ga_len;
4254 generate_JUMP(cctx, opchar == '|'
4255 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4256
4257 // eval the next expression
4258 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004259 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004260 return FAIL;
4261
Bram Moolenaara5565e42020-05-09 15:44:01 +02004262 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4263 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 {
4265 ga_clear(&end_ga);
4266 return FAIL;
4267 }
4268 p = skipwhite(*arg);
4269 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004270 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271
4272 // Fill in the end label in all jumps.
4273 while (end_ga.ga_len > 0)
4274 {
4275 isn_T *isn;
4276
4277 --end_ga.ga_len;
4278 isn = ((isn_T *)instr->ga_data)
4279 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4280 isn->isn_arg.jump.jump_where = instr->ga_len;
4281 }
4282 ga_clear(&end_ga);
4283 }
4284
4285 return OK;
4286}
4287
4288/*
4289 * expr4a && expr4a && expr4a logical AND
4290 *
4291 * Produces instructions:
4292 * EVAL expr4a Push result of "expr4a"
4293 * JUMP_AND_KEEP_IF_FALSE end
4294 * EVAL expr4b Push result of "expr4b"
4295 * JUMP_AND_KEEP_IF_FALSE end
4296 * EVAL expr4c Push result of "expr4c"
4297 * end:
4298 */
4299 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004300compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004302 int ppconst_used = ppconst->pp_used;
4303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004305 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 return FAIL;
4307
4308 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004309 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310}
4311
4312/*
4313 * expr3a || expr3b || expr3c logical OR
4314 *
4315 * Produces instructions:
4316 * EVAL expr3a Push result of "expr3a"
4317 * JUMP_AND_KEEP_IF_TRUE end
4318 * EVAL expr3b Push result of "expr3b"
4319 * JUMP_AND_KEEP_IF_TRUE end
4320 * EVAL expr3c Push result of "expr3c"
4321 * end:
4322 */
4323 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004324compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004326 int ppconst_used = ppconst->pp_used;
4327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004329 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 return FAIL;
4331
4332 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004333 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334}
4335
4336/*
4337 * Toplevel expression: expr2 ? expr1a : expr1b
4338 *
4339 * Produces instructions:
4340 * EVAL expr2 Push result of "expr"
4341 * JUMP_IF_FALSE alt jump if false
4342 * EVAL expr1a
4343 * JUMP_ALWAYS end
4344 * alt: EVAL expr1b
4345 * end:
4346 */
4347 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004348compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349{
4350 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004351 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352
Bram Moolenaar61a89812020-05-07 16:58:17 +02004353 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004354 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 return FAIL;
4356
4357 p = skipwhite(*arg);
4358 if (*p == '?')
4359 {
4360 garray_T *instr = &cctx->ctx_instr;
4361 garray_T *stack = &cctx->ctx_type_stack;
4362 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004363 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004365 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004367 int has_const_expr = FALSE;
4368 int const_value = FALSE;
4369 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004371 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4372 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004374 return FAIL;
4375 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376
Bram Moolenaara5565e42020-05-09 15:44:01 +02004377 if (ppconst->pp_used == ppconst_used + 1)
4378 {
4379 // the condition is a constant, we know whether the ? or the :
4380 // expression is to be evaluated.
4381 has_const_expr = TRUE;
4382 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4383 clear_tv(&ppconst->pp_tv[ppconst_used]);
4384 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004385 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4386 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004387 }
4388 else
4389 {
4390 generate_ppconst(cctx, ppconst);
4391 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4392 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393
4394 // evaluate the second expression; any type is accepted
4395 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004396 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004397 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004398 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004399 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400
Bram Moolenaara5565e42020-05-09 15:44:01 +02004401 if (!has_const_expr)
4402 {
4403 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404
Bram Moolenaara5565e42020-05-09 15:44:01 +02004405 // remember the type and drop it
4406 --stack->ga_len;
4407 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408
Bram Moolenaara5565e42020-05-09 15:44:01 +02004409 end_idx = instr->ga_len;
4410 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4411
4412 // jump here from JUMP_IF_FALSE
4413 isn = ((isn_T *)instr->ga_data) + alt_idx;
4414 isn->isn_arg.jump.jump_where = instr->ga_len;
4415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416
4417 // Check for the ":".
4418 p = skipwhite(*arg);
4419 if (*p != ':')
4420 {
4421 emsg(_(e_missing_colon));
4422 return FAIL;
4423 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004424 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4425 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004427 return FAIL;
4428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429
4430 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004431 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004432 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4433 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004435 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004436 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004437 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004438 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439
Bram Moolenaara5565e42020-05-09 15:44:01 +02004440 if (!has_const_expr)
4441 {
4442 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443
Bram Moolenaara5565e42020-05-09 15:44:01 +02004444 // If the types differ, the result has a more generic type.
4445 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4446 common_type(type1, type2, &type2, cctx->ctx_type_list);
4447
4448 // jump here from JUMP_ALWAYS
4449 isn = ((isn_T *)instr->ga_data) + end_idx;
4450 isn->isn_arg.jump.jump_where = instr->ga_len;
4451 }
4452
4453 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 }
4455 return OK;
4456}
4457
4458/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004459 * Toplevel expression.
4460 */
4461 static int
4462compile_expr0(char_u **arg, cctx_T *cctx)
4463{
4464 ppconst_T ppconst;
4465
4466 CLEAR_FIELD(ppconst);
4467 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4468 {
4469 clear_ppconst(&ppconst);
4470 return FAIL;
4471 }
4472 if (generate_ppconst(cctx, &ppconst) == FAIL)
4473 return FAIL;
4474 return OK;
4475}
4476
4477/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 * compile "return [expr]"
4479 */
4480 static char_u *
4481compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4482{
4483 char_u *p = arg;
4484 garray_T *stack = &cctx->ctx_type_stack;
4485 type_T *stack_type;
4486
4487 if (*p != NUL && *p != '|' && *p != '\n')
4488 {
4489 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004490 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 return NULL;
4492
4493 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4494 if (set_return_type)
4495 cctx->ctx_ufunc->uf_ret_type = stack_type;
4496 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4497 == FAIL)
4498 return NULL;
4499 }
4500 else
4501 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004502 // "set_return_type" cannot be TRUE, only used for a lambda which
4503 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004504 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4505 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 {
4507 emsg(_("E1003: Missing return value"));
4508 return NULL;
4509 }
4510
4511 // No argument, return zero.
4512 generate_PUSHNR(cctx, 0);
4513 }
4514
4515 if (generate_instr(cctx, ISN_RETURN) == NULL)
4516 return NULL;
4517
4518 // "return val | endif" is possible
4519 return skipwhite(p);
4520}
4521
4522/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004523 * Get a line from the compilation context, compatible with exarg_T getline().
4524 * Return a pointer to the line in allocated memory.
4525 * Return NULL for end-of-file or some error.
4526 */
4527 static char_u *
4528exarg_getline(
4529 int c UNUSED,
4530 void *cookie,
4531 int indent UNUSED,
4532 int do_concat UNUSED)
4533{
4534 cctx_T *cctx = (cctx_T *)cookie;
4535
4536 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4537 {
4538 iemsg("Heredoc got to end");
4539 return NULL;
4540 }
4541 ++cctx->ctx_lnum;
4542 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4543 [cctx->ctx_lnum]);
4544}
4545
4546/*
4547 * Compile a nested :def command.
4548 */
4549 static char_u *
4550compile_nested_function(exarg_T *eap, cctx_T *cctx)
4551{
4552 char_u *name_start = eap->arg;
4553 char_u *name_end = to_name_end(eap->arg, FALSE);
4554 char_u *name = get_lambda_name();
4555 lvar_T *lvar;
4556 ufunc_T *ufunc;
4557
4558 eap->arg = name_end;
4559 eap->getline = exarg_getline;
4560 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004561 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004562 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004563 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004564
Bram Moolenaar822ba242020-05-24 23:00:18 +02004565 if (ufunc == NULL)
4566 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004567 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004568 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004569 return NULL;
4570
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004571 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004572 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004573 TRUE, ufunc->uf_func_type);
4574
4575 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4576 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4577 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004578
Bram Moolenaar61a89812020-05-07 16:58:17 +02004579 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004580 return (char_u *)"";
4581}
4582
4583/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 * Return the length of an assignment operator, or zero if there isn't one.
4585 */
4586 int
4587assignment_len(char_u *p, int *heredoc)
4588{
4589 if (*p == '=')
4590 {
4591 if (p[1] == '<' && p[2] == '<')
4592 {
4593 *heredoc = TRUE;
4594 return 3;
4595 }
4596 return 1;
4597 }
4598 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4599 return 2;
4600 if (STRNCMP(p, "..=", 3) == 0)
4601 return 3;
4602 return 0;
4603}
4604
4605// words that cannot be used as a variable
4606static char *reserved[] = {
4607 "true",
4608 "false",
4609 NULL
4610};
4611
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004612typedef enum {
4613 dest_local,
4614 dest_option,
4615 dest_env,
4616 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004617 dest_buffer,
4618 dest_window,
4619 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004620 dest_vimvar,
4621 dest_script,
4622 dest_reg,
4623} assign_dest_T;
4624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004626 * Generate the load instruction for "name".
4627 */
4628 static void
4629generate_loadvar(
4630 cctx_T *cctx,
4631 assign_dest_T dest,
4632 char_u *name,
4633 lvar_T *lvar,
4634 type_T *type)
4635{
4636 switch (dest)
4637 {
4638 case dest_option:
4639 // TODO: check the option exists
4640 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4641 break;
4642 case dest_global:
4643 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4644 break;
4645 case dest_buffer:
4646 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4647 break;
4648 case dest_window:
4649 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4650 break;
4651 case dest_tab:
4652 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4653 break;
4654 case dest_script:
4655 compile_load_scriptvar(cctx,
4656 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4657 break;
4658 case dest_env:
4659 // Include $ in the name here
4660 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4661 break;
4662 case dest_reg:
4663 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4664 break;
4665 case dest_vimvar:
4666 generate_LOADV(cctx, name + 2, TRUE);
4667 break;
4668 case dest_local:
4669 if (lvar->lv_from_outer)
4670 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4671 NULL, type);
4672 else
4673 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4674 break;
4675 }
4676}
4677
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004678 void
4679vim9_declare_error(char_u *name)
4680{
4681 char *scope = "";
4682
4683 switch (*name)
4684 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004685 case 'g': scope = _("global"); break;
4686 case 'b': scope = _("buffer"); break;
4687 case 'w': scope = _("window"); break;
4688 case 't': scope = _("tab"); break;
4689 case 'v': scope = "v:"; break;
4690 case '$': semsg(_(e_declare_env_var), name); return;
4691 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004692 }
4693 semsg(_(e_declare_var), scope, name);
4694}
4695
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004696/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004697 * Compile declaration and assignment:
4698 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004700 * Return NULL for an error.
4701 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 */
4703 static char_u *
4704compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4705{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004706 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004708 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 char_u *ret = NULL;
4710 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004711 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004714 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 int oplen = 0;
4717 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004718 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004719 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004720 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004724 // Skip over the "var" or "[var, var]" to get to any "=".
4725 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4726 if (p == NULL)
4727 return *arg == '[' ? arg : NULL;
4728
4729 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004731 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 return NULL;
4733 }
4734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 sp = p;
4736 p = skipwhite(p);
4737 op = p;
4738 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004739
4740 if (var_count > 0 && oplen == 0)
4741 // can be something like "[1, 2]->func()"
4742 return arg;
4743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4745 {
4746 char_u buf[4];
4747
4748 vim_strncpy(buf, op, oplen);
4749 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004750 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004751 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 if (heredoc)
4754 {
4755 list_T *l;
4756 listitem_T *li;
4757
4758 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004759 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004761 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762
4763 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004764 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 {
4766 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4767 li->li_tv.vval.v_string = NULL;
4768 }
4769 generate_NEWLIST(cctx, l->lv_len);
4770 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004771 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 list_free(l);
4773 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004774 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004776 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004778 // for "[var, var] = expr" evaluate the expression here, loop over the
4779 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004780
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004781 p = skipwhite(op + oplen);
4782 if (compile_expr0(&p, cctx) == FAIL)
4783 return NULL;
4784 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004786 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004788 type_T *stacktype;
4789
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004790 stacktype = stack->ga_len == 0 ? &t_void
4791 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004792 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004794 emsg(_(e_cannot_use_void));
4795 goto theend;
4796 }
4797 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4798 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004799 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4800 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004801 }
4802 }
4803
4804 /*
4805 * Loop over variables in "[var, var] = expr".
4806 * For "var = expr" and "let var: type" this is done only once.
4807 */
4808 if (var_count > 0)
4809 var_start = skipwhite(arg + 1); // skip over the "["
4810 else
4811 var_start = arg;
4812 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4813 {
4814 char_u *var_end = skip_var_one(var_start, FALSE);
4815 size_t varlen;
4816 int new_local = FALSE;
4817 int opt_type;
4818 int opt_flags = 0;
4819 assign_dest_T dest = dest_local;
4820 int vimvaridx = -1;
4821 lvar_T *lvar = NULL;
4822 lvar_T arg_lvar;
4823 int has_type = FALSE;
4824 int has_index = FALSE;
4825 int instr_count = -1;
4826
4827 p = (*var_start == '&' || *var_start == '$'
4828 || *var_start == '@') ? var_start + 1 : var_start;
4829 p = to_name_end(p, TRUE);
4830
4831 // "a: type" is declaring variable "a" with a type, not "a:".
4832 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4833 --var_end;
4834 if (is_decl && p == var_start + 2 && p[-1] == ':')
4835 --p;
4836
4837 varlen = p - var_start;
4838 vim_free(name);
4839 name = vim_strnsave(var_start, varlen);
4840 if (name == NULL)
4841 return NULL;
4842 if (!heredoc)
4843 type = &t_any;
4844
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004845 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004846 {
4847 if (*var_start == '&')
4848 {
4849 int cc;
4850 long numval;
4851
4852 dest = dest_option;
4853 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004855 emsg(_(e_const_option));
4856 goto theend;
4857 }
4858 if (is_decl)
4859 {
4860 semsg(_("E1052: Cannot declare an option: %s"), var_start);
4861 goto theend;
4862 }
4863 p = var_start;
4864 p = find_option_end(&p, &opt_flags);
4865 if (p == NULL)
4866 {
4867 // cannot happen?
4868 emsg(_(e_letunexp));
4869 goto theend;
4870 }
4871 cc = *p;
4872 *p = NUL;
4873 opt_type = get_option_value(var_start + 1, &numval,
4874 NULL, opt_flags);
4875 *p = cc;
4876 if (opt_type == -3)
4877 {
4878 semsg(_(e_unknown_option), var_start);
4879 goto theend;
4880 }
4881 if (opt_type == -2 || opt_type == 0)
4882 type = &t_string;
4883 else
4884 type = &t_number; // both number and boolean option
4885 }
4886 else if (*var_start == '$')
4887 {
4888 dest = dest_env;
4889 type = &t_string;
4890 if (is_decl)
4891 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004892 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004893 goto theend;
4894 }
4895 }
4896 else if (*var_start == '@')
4897 {
4898 if (!valid_yank_reg(var_start[1], TRUE))
4899 {
4900 emsg_invreg(var_start[1]);
4901 goto theend;
4902 }
4903 dest = dest_reg;
4904 type = &t_string;
4905 if (is_decl)
4906 {
4907 semsg(_("E1066: Cannot declare a register: %s"), name);
4908 goto theend;
4909 }
4910 }
4911 else if (STRNCMP(var_start, "g:", 2) == 0)
4912 {
4913 dest = dest_global;
4914 if (is_decl)
4915 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004916 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004917 goto theend;
4918 }
4919 }
4920 else if (STRNCMP(var_start, "b:", 2) == 0)
4921 {
4922 dest = dest_buffer;
4923 if (is_decl)
4924 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004925 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004926 goto theend;
4927 }
4928 }
4929 else if (STRNCMP(var_start, "w:", 2) == 0)
4930 {
4931 dest = dest_window;
4932 if (is_decl)
4933 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004934 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004935 goto theend;
4936 }
4937 }
4938 else if (STRNCMP(var_start, "t:", 2) == 0)
4939 {
4940 dest = dest_tab;
4941 if (is_decl)
4942 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004943 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004944 goto theend;
4945 }
4946 }
4947 else if (STRNCMP(var_start, "v:", 2) == 0)
4948 {
4949 typval_T *vtv;
4950 int di_flags;
4951
4952 vimvaridx = find_vim_var(name + 2, &di_flags);
4953 if (vimvaridx < 0)
4954 {
4955 semsg(_(e_var_notfound), var_start);
4956 goto theend;
4957 }
4958 // We use the current value of "sandbox" here, is that OK?
4959 if (var_check_ro(di_flags, name, FALSE))
4960 goto theend;
4961 dest = dest_vimvar;
4962 vtv = get_vim_var_tv(vimvaridx);
4963 type = typval2type(vtv);
4964 if (is_decl)
4965 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004966 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004967 goto theend;
4968 }
4969 }
4970 else
4971 {
4972 int idx;
4973
4974 for (idx = 0; reserved[idx] != NULL; ++idx)
4975 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004976 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004977 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004978 goto theend;
4979 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004980
4981 lvar = lookup_local(var_start, varlen, cctx);
4982 if (lvar == NULL)
4983 {
4984 CLEAR_FIELD(arg_lvar);
4985 if (lookup_arg(var_start, varlen,
4986 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4987 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004988 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004989 if (is_decl)
4990 {
4991 semsg(_(e_used_as_arg), name);
4992 goto theend;
4993 }
4994 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004995 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004996 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004997 if (lvar != NULL)
4998 {
4999 if (is_decl)
5000 {
5001 semsg(_("E1017: Variable already declared: %s"), name);
5002 goto theend;
5003 }
5004 else if (lvar->lv_const)
5005 {
5006 semsg(_("E1018: Cannot assign to a constant: %s"),
5007 name);
5008 goto theend;
5009 }
5010 }
5011 else if (STRNCMP(var_start, "s:", 2) == 0
5012 || lookup_script(var_start, varlen) == OK
5013 || find_imported(var_start, varlen, cctx) != NULL)
5014 {
5015 dest = dest_script;
5016 if (is_decl)
5017 {
5018 semsg(_("E1054: Variable already declared in the script: %s"),
5019 name);
5020 goto theend;
5021 }
5022 }
5023 else if (name[1] == ':' && name[2] != NUL)
5024 {
5025 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5026 name);
5027 goto theend;
5028 }
5029 else if (!is_decl)
5030 {
5031 semsg(_("E1089: unknown variable: %s"), name);
5032 goto theend;
5033 }
5034 }
5035 }
5036
5037 // handle "a:name" as a name, not index "name" on "a"
5038 if (varlen > 1 || var_start[varlen] != ':')
5039 p = var_end;
5040
5041 if (dest != dest_option)
5042 {
5043 if (is_decl && *p == ':')
5044 {
5045 // parse optional type: "let var: type = expr"
5046 if (!VIM_ISWHITE(p[1]))
5047 {
5048 semsg(_(e_white_after), ":");
5049 goto theend;
5050 }
5051 p = skipwhite(p + 1);
5052 type = parse_type(&p, cctx->ctx_type_list);
5053 has_type = TRUE;
5054 }
5055 else if (lvar != NULL)
5056 type = lvar->lv_type;
5057 }
5058
5059 if (oplen == 3 && !heredoc && dest != dest_global
5060 && type->tt_type != VAR_STRING
5061 && type->tt_type != VAR_ANY)
5062 {
5063 emsg(_("E1019: Can only concatenate to string"));
5064 goto theend;
5065 }
5066
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005067 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005068 {
5069 if (oplen > 1 && !heredoc)
5070 {
5071 // +=, /=, etc. require an existing variable
5072 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5073 name);
5074 goto theend;
5075 }
5076
5077 // new local variable
5078 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5079 goto theend;
5080 lvar = reserve_local(cctx, var_start, varlen,
5081 cmdidx == CMD_const, type);
5082 if (lvar == NULL)
5083 goto theend;
5084 new_local = TRUE;
5085 }
5086
5087 member_type = type;
5088 if (var_end > var_start + varlen)
5089 {
5090 // Something follows after the variable: "var[idx]".
5091 if (is_decl)
5092 {
5093 emsg(_("E1087: cannot use an index when declaring a variable"));
5094 goto theend;
5095 }
5096
5097 if (var_start[varlen] == '[')
5098 {
5099 has_index = TRUE;
5100 if (type->tt_member == NULL)
5101 {
5102 semsg(_("E1088: cannot use an index on %s"), name);
5103 goto theend;
5104 }
5105 member_type = type->tt_member;
5106 }
5107 else
5108 {
5109 semsg("Not supported yet: %s", var_start);
5110 goto theend;
5111 }
5112 }
5113 else if (lvar == &arg_lvar)
5114 {
5115 semsg(_("E1090: Cannot assign to argument %s"), name);
5116 goto theend;
5117 }
5118
5119 if (!heredoc)
5120 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005121 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005122 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005123 if (oplen > 0 && var_count == 0)
5124 {
5125 // skip over the "=" and the expression
5126 p = skipwhite(op + oplen);
5127 compile_expr0(&p, cctx);
5128 }
5129 }
5130 else if (oplen > 0)
5131 {
5132 type_T *stacktype;
5133
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005134 // For "var = expr" evaluate the expression.
5135 if (var_count == 0)
5136 {
5137 int r;
5138
5139 // for "+=", "*=", "..=" etc. first load the current value
5140 if (*op != '=')
5141 {
5142 generate_loadvar(cctx, dest, name, lvar, type);
5143
5144 if (has_index)
5145 {
5146 // TODO: get member from list or dict
5147 emsg("Index with operation not supported yet");
5148 goto theend;
5149 }
5150 }
5151
5152 // Compile the expression. Temporarily hide the new local
5153 // variable here, it is not available to this expression.
5154 if (new_local)
5155 --cctx->ctx_locals.ga_len;
5156 instr_count = instr->ga_len;
5157 p = skipwhite(op + oplen);
5158 r = compile_expr0(&p, cctx);
5159 if (new_local)
5160 ++cctx->ctx_locals.ga_len;
5161 if (r == FAIL)
5162 goto theend;
5163 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005164 else if (semicolon && var_idx == var_count - 1)
5165 {
5166 // For "[var; var] = expr" get the rest of the list
5167 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5168 goto theend;
5169 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005170 else
5171 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005172 // For "[var, var] = expr" get the "var_idx" item from the
5173 // list.
5174 if (generate_GETITEM(cctx, var_idx) == FAIL)
5175 return FAIL;
5176 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005177
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005178 stacktype = stack->ga_len == 0 ? &t_void
5179 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5180 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005181 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005182 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005183 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005184 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005185 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005186 emsg(_(e_cannot_use_void));
5187 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005188 }
5189 else
5190 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005191 // An empty list or dict has a &t_void member,
5192 // for a variable that implies &t_any.
5193 if (stacktype == &t_list_empty)
5194 lvar->lv_type = &t_list_any;
5195 else if (stacktype == &t_dict_empty)
5196 lvar->lv_type = &t_dict_any;
5197 else
5198 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005199 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005200 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005201 else
5202 {
5203 type_T *use_type = lvar->lv_type;
5204
5205 if (has_index)
5206 {
5207 use_type = use_type->tt_member;
5208 if (use_type == NULL)
5209 use_type = &t_void;
5210 }
5211 if (need_type(stacktype, use_type, -1, cctx)
5212 == FAIL)
5213 goto theend;
5214 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005215 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005216 else if (*p != '=' && need_type(stacktype, member_type, -1,
5217 cctx) == FAIL)
5218 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005220 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005222 emsg(_(e_const_req_value));
5223 goto theend;
5224 }
5225 else if (!has_type || dest == dest_option)
5226 {
5227 emsg(_(e_type_req));
5228 goto theend;
5229 }
5230 else
5231 {
5232 // variables are always initialized
5233 if (ga_grow(instr, 1) == FAIL)
5234 goto theend;
5235 switch (member_type->tt_type)
5236 {
5237 case VAR_BOOL:
5238 generate_PUSHBOOL(cctx, VVAL_FALSE);
5239 break;
5240 case VAR_FLOAT:
5241#ifdef FEAT_FLOAT
5242 generate_PUSHF(cctx, 0.0);
5243#endif
5244 break;
5245 case VAR_STRING:
5246 generate_PUSHS(cctx, NULL);
5247 break;
5248 case VAR_BLOB:
5249 generate_PUSHBLOB(cctx, NULL);
5250 break;
5251 case VAR_FUNC:
5252 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5253 break;
5254 case VAR_LIST:
5255 generate_NEWLIST(cctx, 0);
5256 break;
5257 case VAR_DICT:
5258 generate_NEWDICT(cctx, 0);
5259 break;
5260 case VAR_JOB:
5261 generate_PUSHJOB(cctx, NULL);
5262 break;
5263 case VAR_CHANNEL:
5264 generate_PUSHCHANNEL(cctx, NULL);
5265 break;
5266 case VAR_NUMBER:
5267 case VAR_UNKNOWN:
5268 case VAR_ANY:
5269 case VAR_PARTIAL:
5270 case VAR_VOID:
5271 case VAR_SPECIAL: // cannot happen
5272 generate_PUSHNR(cctx, 0);
5273 break;
5274 }
5275 }
5276 if (var_count == 0)
5277 end = p;
5278 }
5279
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005280 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005281 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005282 break;
5283
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005284 if (oplen > 0 && *op != '=')
5285 {
5286 type_T *expected = &t_number;
5287 type_T *stacktype;
5288
5289 // TODO: if type is known use float or any operation
5290
5291 if (*op == '.')
5292 expected = &t_string;
5293 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5294 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5295 goto theend;
5296
5297 if (*op == '.')
5298 generate_instr_drop(cctx, ISN_CONCAT, 1);
5299 else
5300 {
5301 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5302
5303 if (isn == NULL)
5304 goto theend;
5305 switch (*op)
5306 {
5307 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5308 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5309 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5310 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5311 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005313 }
5314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005316 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005317 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005318 int r;
5319
5320 // Compile the "idx" in "var[idx]".
5321 if (new_local)
5322 --cctx->ctx_locals.ga_len;
5323 p = skipwhite(var_start + varlen + 1);
5324 r = compile_expr0(&p, cctx);
5325 if (new_local)
5326 ++cctx->ctx_locals.ga_len;
5327 if (r == FAIL)
5328 goto theend;
5329 if (*skipwhite(p) != ']')
5330 {
5331 emsg(_(e_missbrac));
5332 goto theend;
5333 }
5334 if (type->tt_type == VAR_DICT
5335 && may_generate_2STRING(-1, cctx) == FAIL)
5336 goto theend;
5337 if (type->tt_type == VAR_LIST
5338 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005339 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005340 {
5341 emsg(_(e_number_exp));
5342 goto theend;
5343 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005344
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005345 // Load the dict or list. On the stack we then have:
5346 // - value
5347 // - index
5348 // - variable
5349 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005350
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005351 if (type->tt_type == VAR_LIST)
5352 {
5353 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5354 return FAIL;
5355 }
5356 else if (type->tt_type == VAR_DICT)
5357 {
5358 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5359 return FAIL;
5360 }
5361 else
5362 {
5363 emsg(_(e_listreq));
5364 goto theend;
5365 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005366 }
5367 else
5368 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005369 switch (dest)
5370 {
5371 case dest_option:
5372 generate_STOREOPT(cctx, name + 1, opt_flags);
5373 break;
5374 case dest_global:
5375 // include g: with the name, easier to execute that way
5376 generate_STORE(cctx, ISN_STOREG, 0, name);
5377 break;
5378 case dest_buffer:
5379 // include b: with the name, easier to execute that way
5380 generate_STORE(cctx, ISN_STOREB, 0, name);
5381 break;
5382 case dest_window:
5383 // include w: with the name, easier to execute that way
5384 generate_STORE(cctx, ISN_STOREW, 0, name);
5385 break;
5386 case dest_tab:
5387 // include t: with the name, easier to execute that way
5388 generate_STORE(cctx, ISN_STORET, 0, name);
5389 break;
5390 case dest_env:
5391 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5392 break;
5393 case dest_reg:
5394 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5395 break;
5396 case dest_vimvar:
5397 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5398 break;
5399 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005400 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005401 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5402 imported_T *import = NULL;
5403 int sid = current_sctx.sc_sid;
5404 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005405
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005406 if (name[1] != ':')
5407 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005408 import = find_imported(name, 0, cctx);
5409 if (import != NULL)
5410 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005411 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005412
5413 idx = get_script_item_idx(sid, rawname, TRUE);
5414 // TODO: specific type
5415 if (idx < 0)
5416 {
5417 char_u *name_s = name;
5418
5419 // Include s: in the name for store_var()
5420 if (name[1] != ':')
5421 {
5422 int len = (int)STRLEN(name) + 3;
5423
5424 name_s = alloc(len);
5425 if (name_s == NULL)
5426 name_s = name;
5427 else
5428 vim_snprintf((char *)name_s, len,
5429 "s:%s", name);
5430 }
5431 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005432 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005433 if (name_s != name)
5434 vim_free(name_s);
5435 }
5436 else
5437 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005438 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005439 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005440 break;
5441 case dest_local:
5442 if (lvar != NULL)
5443 {
5444 isn_T *isn = ((isn_T *)instr->ga_data)
5445 + instr->ga_len - 1;
5446
5447 // optimization: turn "var = 123" from ISN_PUSHNR +
5448 // ISN_STORE into ISN_STORENR
5449 if (!lvar->lv_from_outer
5450 && instr->ga_len == instr_count + 1
5451 && isn->isn_type == ISN_PUSHNR)
5452 {
5453 varnumber_T val = isn->isn_arg.number;
5454
5455 isn->isn_type = ISN_STORENR;
5456 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5457 isn->isn_arg.storenr.stnr_val = val;
5458 if (stack->ga_len > 0)
5459 --stack->ga_len;
5460 }
5461 else if (lvar->lv_from_outer)
5462 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005463 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464 else
5465 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5466 }
5467 break;
5468 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005469 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005470
5471 if (var_idx + 1 < var_count)
5472 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005473 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005474
5475 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005476 if (var_count > 0 && !semicolon)
5477 {
5478 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5479 goto theend;
5480 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005481
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005482 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005483
5484theend:
5485 vim_free(name);
5486 return ret;
5487}
5488
5489/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005490 * Check if "name" can be "unlet".
5491 */
5492 int
5493check_vim9_unlet(char_u *name)
5494{
5495 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5496 {
5497 semsg(_("E1081: Cannot unlet %s"), name);
5498 return FAIL;
5499 }
5500 return OK;
5501}
5502
5503/*
5504 * Callback passed to ex_unletlock().
5505 */
5506 static int
5507compile_unlet(
5508 lval_T *lvp,
5509 char_u *name_end,
5510 exarg_T *eap,
5511 int deep UNUSED,
5512 void *coookie)
5513{
5514 cctx_T *cctx = coookie;
5515
5516 if (lvp->ll_tv == NULL)
5517 {
5518 char_u *p = lvp->ll_name;
5519 int cc = *name_end;
5520 int ret = OK;
5521
5522 // Normal name. Only supports g:, w:, t: and b: namespaces.
5523 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005524 if (*p == '$')
5525 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5526 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005527 ret = FAIL;
5528 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005529 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005530
5531 *name_end = cc;
5532 return ret;
5533 }
5534
5535 // TODO: unlet {list}[idx]
5536 // TODO: unlet {dict}[key]
5537 emsg("Sorry, :unlet not fully implemented yet");
5538 return FAIL;
5539}
5540
5541/*
5542 * compile "unlet var", "lock var" and "unlock var"
5543 * "arg" points to "var".
5544 */
5545 static char_u *
5546compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5547{
5548 char_u *p = arg;
5549
5550 if (eap->cmdidx != CMD_unlet)
5551 {
5552 emsg("Sorry, :lock and unlock not implemented yet");
5553 return NULL;
5554 }
5555
5556 if (*p == '!')
5557 {
5558 p = skipwhite(p + 1);
5559 eap->forceit = TRUE;
5560 }
5561
5562 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5563 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5564}
5565
5566/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005567 * Compile an :import command.
5568 */
5569 static char_u *
5570compile_import(char_u *arg, cctx_T *cctx)
5571{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005572 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005573}
5574
5575/*
5576 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5577 */
5578 static int
5579compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5580{
5581 garray_T *instr = &cctx->ctx_instr;
5582 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5583
5584 if (endlabel == NULL)
5585 return FAIL;
5586 endlabel->el_next = *el;
5587 *el = endlabel;
5588 endlabel->el_end_label = instr->ga_len;
5589
5590 generate_JUMP(cctx, when, 0);
5591 return OK;
5592}
5593
5594 static void
5595compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5596{
5597 garray_T *instr = &cctx->ctx_instr;
5598
5599 while (*el != NULL)
5600 {
5601 endlabel_T *cur = (*el);
5602 isn_T *isn;
5603
5604 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5605 isn->isn_arg.jump.jump_where = instr->ga_len;
5606 *el = cur->el_next;
5607 vim_free(cur);
5608 }
5609}
5610
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005611 static void
5612compile_free_jump_to_end(endlabel_T **el)
5613{
5614 while (*el != NULL)
5615 {
5616 endlabel_T *cur = (*el);
5617
5618 *el = cur->el_next;
5619 vim_free(cur);
5620 }
5621}
5622
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005623/*
5624 * Create a new scope and set up the generic items.
5625 */
5626 static scope_T *
5627new_scope(cctx_T *cctx, scopetype_T type)
5628{
5629 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5630
5631 if (scope == NULL)
5632 return NULL;
5633 scope->se_outer = cctx->ctx_scope;
5634 cctx->ctx_scope = scope;
5635 scope->se_type = type;
5636 scope->se_local_count = cctx->ctx_locals.ga_len;
5637 return scope;
5638}
5639
5640/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005641 * Free the current scope and go back to the outer scope.
5642 */
5643 static void
5644drop_scope(cctx_T *cctx)
5645{
5646 scope_T *scope = cctx->ctx_scope;
5647
5648 if (scope == NULL)
5649 {
5650 iemsg("calling drop_scope() without a scope");
5651 return;
5652 }
5653 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005654 switch (scope->se_type)
5655 {
5656 case IF_SCOPE:
5657 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5658 case FOR_SCOPE:
5659 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5660 case WHILE_SCOPE:
5661 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5662 case TRY_SCOPE:
5663 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5664 case NO_SCOPE:
5665 case BLOCK_SCOPE:
5666 break;
5667 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005668 vim_free(scope);
5669}
5670
5671/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672 * compile "if expr"
5673 *
5674 * "if expr" Produces instructions:
5675 * EVAL expr Push result of "expr"
5676 * JUMP_IF_FALSE end
5677 * ... body ...
5678 * end:
5679 *
5680 * "if expr | else" Produces instructions:
5681 * EVAL expr Push result of "expr"
5682 * JUMP_IF_FALSE else
5683 * ... body ...
5684 * JUMP_ALWAYS end
5685 * else:
5686 * ... body ...
5687 * end:
5688 *
5689 * "if expr1 | elseif expr2 | else" Produces instructions:
5690 * EVAL expr Push result of "expr"
5691 * JUMP_IF_FALSE elseif
5692 * ... body ...
5693 * JUMP_ALWAYS end
5694 * elseif:
5695 * EVAL expr Push result of "expr"
5696 * JUMP_IF_FALSE else
5697 * ... body ...
5698 * JUMP_ALWAYS end
5699 * else:
5700 * ... body ...
5701 * end:
5702 */
5703 static char_u *
5704compile_if(char_u *arg, cctx_T *cctx)
5705{
5706 char_u *p = arg;
5707 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005708 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005709 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005710 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005711 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005712
Bram Moolenaara5565e42020-05-09 15:44:01 +02005713 CLEAR_FIELD(ppconst);
5714 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005715 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005716 clear_ppconst(&ppconst);
5717 return NULL;
5718 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005719 if (cctx->ctx_skip == SKIP_YES)
5720 clear_ppconst(&ppconst);
5721 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005722 {
5723 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005724 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005725 clear_ppconst(&ppconst);
5726 }
5727 else
5728 {
5729 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005730 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005731 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005732 return NULL;
5733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005734
5735 scope = new_scope(cctx, IF_SCOPE);
5736 if (scope == NULL)
5737 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005738 scope->se_skip_save = skip_save;
5739 // "is_had_return" will be reset if any block does not end in :return
5740 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005741
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005742 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005743 {
5744 // "where" is set when ":elseif", "else" or ":endif" is found
5745 scope->se_u.se_if.is_if_label = instr->ga_len;
5746 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5747 }
5748 else
5749 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005750
5751 return p;
5752}
5753
5754 static char_u *
5755compile_elseif(char_u *arg, cctx_T *cctx)
5756{
5757 char_u *p = arg;
5758 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005759 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005760 isn_T *isn;
5761 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005762 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763
5764 if (scope == NULL || scope->se_type != IF_SCOPE)
5765 {
5766 emsg(_(e_elseif_without_if));
5767 return NULL;
5768 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005769 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005770 if (!cctx->ctx_had_return)
5771 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005772
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005773 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005774 {
5775 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005776 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005777 return NULL;
5778 // previous "if" or "elseif" jumps here
5779 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5780 isn->isn_arg.jump.jump_where = instr->ga_len;
5781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005782
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005783 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005784 CLEAR_FIELD(ppconst);
5785 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005786 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005787 clear_ppconst(&ppconst);
5788 return NULL;
5789 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005790 if (scope->se_skip_save == SKIP_YES)
5791 clear_ppconst(&ppconst);
5792 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005793 {
5794 // The expression results in a constant.
5795 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005796 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005797 clear_ppconst(&ppconst);
5798 scope->se_u.se_if.is_if_label = -1;
5799 }
5800 else
5801 {
5802 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005803 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005804 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005805 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005806
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005807 // "where" is set when ":elseif", "else" or ":endif" is found
5808 scope->se_u.se_if.is_if_label = instr->ga_len;
5809 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005811
5812 return p;
5813}
5814
5815 static char_u *
5816compile_else(char_u *arg, cctx_T *cctx)
5817{
5818 char_u *p = arg;
5819 garray_T *instr = &cctx->ctx_instr;
5820 isn_T *isn;
5821 scope_T *scope = cctx->ctx_scope;
5822
5823 if (scope == NULL || scope->se_type != IF_SCOPE)
5824 {
5825 emsg(_(e_else_without_if));
5826 return NULL;
5827 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005828 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005829 if (!cctx->ctx_had_return)
5830 scope->se_u.se_if.is_had_return = FALSE;
5831 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005832
Bram Moolenaarefd88552020-06-18 20:50:10 +02005833 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005834 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005835 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005836 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005837 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005838 if (!cctx->ctx_had_return
5839 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5840 JUMP_ALWAYS, cctx) == FAIL)
5841 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005842 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005843
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005844 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005845 {
5846 if (scope->se_u.se_if.is_if_label >= 0)
5847 {
5848 // previous "if" or "elseif" jumps here
5849 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5850 isn->isn_arg.jump.jump_where = instr->ga_len;
5851 scope->se_u.se_if.is_if_label = -1;
5852 }
5853 }
5854
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005855 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005856 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5857 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005858
5859 return p;
5860}
5861
5862 static char_u *
5863compile_endif(char_u *arg, cctx_T *cctx)
5864{
5865 scope_T *scope = cctx->ctx_scope;
5866 ifscope_T *ifscope;
5867 garray_T *instr = &cctx->ctx_instr;
5868 isn_T *isn;
5869
5870 if (scope == NULL || scope->se_type != IF_SCOPE)
5871 {
5872 emsg(_(e_endif_without_if));
5873 return NULL;
5874 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005875 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005876 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005877 if (!cctx->ctx_had_return)
5878 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005879
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005880 if (scope->se_u.se_if.is_if_label >= 0)
5881 {
5882 // previous "if" or "elseif" jumps here
5883 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5884 isn->isn_arg.jump.jump_where = instr->ga_len;
5885 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886 // Fill in the "end" label in jumps at the end of the blocks.
5887 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005888 cctx->ctx_skip = scope->se_skip_save;
5889
5890 // If all the blocks end in :return and there is an :else then the
5891 // had_return flag is set.
5892 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005893
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005894 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895 return arg;
5896}
5897
5898/*
5899 * compile "for var in expr"
5900 *
5901 * Produces instructions:
5902 * PUSHNR -1
5903 * STORE loop-idx Set index to -1
5904 * EVAL expr Push result of "expr"
5905 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5906 * - if beyond end, jump to "end"
5907 * - otherwise get item from list and push it
5908 * STORE var Store item in "var"
5909 * ... body ...
5910 * JUMP top Jump back to repeat
5911 * end: DROP Drop the result of "expr"
5912 *
5913 */
5914 static char_u *
5915compile_for(char_u *arg, cctx_T *cctx)
5916{
5917 char_u *p;
5918 size_t varlen;
5919 garray_T *instr = &cctx->ctx_instr;
5920 garray_T *stack = &cctx->ctx_type_stack;
5921 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005922 lvar_T *loop_lvar; // loop iteration variable
5923 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005924 type_T *vartype;
5925
5926 // TODO: list of variables: "for [key, value] in dict"
5927 // parse "var"
5928 for (p = arg; eval_isnamec1(*p); ++p)
5929 ;
5930 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005931 var_lvar = lookup_local(arg, varlen, cctx);
5932 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933 {
5934 semsg(_("E1023: variable already defined: %s"), arg);
5935 return NULL;
5936 }
5937
5938 // consume "in"
5939 p = skipwhite(p);
5940 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5941 {
5942 emsg(_(e_missing_in));
5943 return NULL;
5944 }
5945 p = skipwhite(p + 2);
5946
5947
5948 scope = new_scope(cctx, FOR_SCOPE);
5949 if (scope == NULL)
5950 return NULL;
5951
5952 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005953 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5954 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005955 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005956 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005957 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005958 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005959 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005960
5961 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005962 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5963 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005964 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005965 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005966 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005967 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005970 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005971
5972 // compile "expr", it remains on the stack until "endfor"
5973 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005974 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005975 {
5976 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979
5980 // now we know the type of "var"
5981 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5982 if (vartype->tt_type != VAR_LIST)
5983 {
5984 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005985 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986 return NULL;
5987 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005988 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005989 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990
5991 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005992 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005994 generate_FOR(cctx, loop_lvar->lv_idx);
5995 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005996
5997 return arg;
5998}
5999
6000/*
6001 * compile "endfor"
6002 */
6003 static char_u *
6004compile_endfor(char_u *arg, cctx_T *cctx)
6005{
6006 garray_T *instr = &cctx->ctx_instr;
6007 scope_T *scope = cctx->ctx_scope;
6008 forscope_T *forscope;
6009 isn_T *isn;
6010
6011 if (scope == NULL || scope->se_type != FOR_SCOPE)
6012 {
6013 emsg(_(e_for));
6014 return NULL;
6015 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006016 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006017 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006018 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006019
6020 // At end of ":for" scope jump back to the FOR instruction.
6021 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6022
6023 // Fill in the "end" label in the FOR statement so it can jump here
6024 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6025 isn->isn_arg.forloop.for_end = instr->ga_len;
6026
6027 // Fill in the "end" label any BREAK statements
6028 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6029
6030 // Below the ":for" scope drop the "expr" list from the stack.
6031 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6032 return NULL;
6033
6034 vim_free(scope);
6035
6036 return arg;
6037}
6038
6039/*
6040 * compile "while expr"
6041 *
6042 * Produces instructions:
6043 * top: EVAL expr Push result of "expr"
6044 * JUMP_IF_FALSE end jump if false
6045 * ... body ...
6046 * JUMP top Jump back to repeat
6047 * end:
6048 *
6049 */
6050 static char_u *
6051compile_while(char_u *arg, cctx_T *cctx)
6052{
6053 char_u *p = arg;
6054 garray_T *instr = &cctx->ctx_instr;
6055 scope_T *scope;
6056
6057 scope = new_scope(cctx, WHILE_SCOPE);
6058 if (scope == NULL)
6059 return NULL;
6060
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006061 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062
6063 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006064 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006065 return NULL;
6066
6067 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006068 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006069 JUMP_IF_FALSE, cctx) == FAIL)
6070 return FAIL;
6071
6072 return p;
6073}
6074
6075/*
6076 * compile "endwhile"
6077 */
6078 static char_u *
6079compile_endwhile(char_u *arg, cctx_T *cctx)
6080{
6081 scope_T *scope = cctx->ctx_scope;
6082
6083 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6084 {
6085 emsg(_(e_while));
6086 return NULL;
6087 }
6088 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006089 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006090
6091 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006092 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006093
6094 // Fill in the "end" label in the WHILE statement so it can jump here.
6095 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006096 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006097
6098 vim_free(scope);
6099
6100 return arg;
6101}
6102
6103/*
6104 * compile "continue"
6105 */
6106 static char_u *
6107compile_continue(char_u *arg, cctx_T *cctx)
6108{
6109 scope_T *scope = cctx->ctx_scope;
6110
6111 for (;;)
6112 {
6113 if (scope == NULL)
6114 {
6115 emsg(_(e_continue));
6116 return NULL;
6117 }
6118 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6119 break;
6120 scope = scope->se_outer;
6121 }
6122
6123 // Jump back to the FOR or WHILE instruction.
6124 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006125 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6126 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127 return arg;
6128}
6129
6130/*
6131 * compile "break"
6132 */
6133 static char_u *
6134compile_break(char_u *arg, cctx_T *cctx)
6135{
6136 scope_T *scope = cctx->ctx_scope;
6137 endlabel_T **el;
6138
6139 for (;;)
6140 {
6141 if (scope == NULL)
6142 {
6143 emsg(_(e_break));
6144 return NULL;
6145 }
6146 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6147 break;
6148 scope = scope->se_outer;
6149 }
6150
6151 // Jump to the end of the FOR or WHILE loop.
6152 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006153 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006154 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006155 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6157 return FAIL;
6158
6159 return arg;
6160}
6161
6162/*
6163 * compile "{" start of block
6164 */
6165 static char_u *
6166compile_block(char_u *arg, cctx_T *cctx)
6167{
6168 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6169 return NULL;
6170 return skipwhite(arg + 1);
6171}
6172
6173/*
6174 * compile end of block: drop one scope
6175 */
6176 static void
6177compile_endblock(cctx_T *cctx)
6178{
6179 scope_T *scope = cctx->ctx_scope;
6180
6181 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006182 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183 vim_free(scope);
6184}
6185
6186/*
6187 * compile "try"
6188 * Creates a new scope for the try-endtry, pointing to the first catch and
6189 * finally.
6190 * Creates another scope for the "try" block itself.
6191 * TRY instruction sets up exception handling at runtime.
6192 *
6193 * "try"
6194 * TRY -> catch1, -> finally push trystack entry
6195 * ... try block
6196 * "throw {exception}"
6197 * EVAL {exception}
6198 * THROW create exception
6199 * ... try block
6200 * " catch {expr}"
6201 * JUMP -> finally
6202 * catch1: PUSH exeception
6203 * EVAL {expr}
6204 * MATCH
6205 * JUMP nomatch -> catch2
6206 * CATCH remove exception
6207 * ... catch block
6208 * " catch"
6209 * JUMP -> finally
6210 * catch2: CATCH remove exception
6211 * ... catch block
6212 * " finally"
6213 * finally:
6214 * ... finally block
6215 * " endtry"
6216 * ENDTRY pop trystack entry, may rethrow
6217 */
6218 static char_u *
6219compile_try(char_u *arg, cctx_T *cctx)
6220{
6221 garray_T *instr = &cctx->ctx_instr;
6222 scope_T *try_scope;
6223 scope_T *scope;
6224
6225 // scope that holds the jumps that go to catch/finally/endtry
6226 try_scope = new_scope(cctx, TRY_SCOPE);
6227 if (try_scope == NULL)
6228 return NULL;
6229
6230 // "catch" is set when the first ":catch" is found.
6231 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006232 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 if (generate_instr(cctx, ISN_TRY) == NULL)
6234 return NULL;
6235
6236 // scope for the try block itself
6237 scope = new_scope(cctx, BLOCK_SCOPE);
6238 if (scope == NULL)
6239 return NULL;
6240
6241 return arg;
6242}
6243
6244/*
6245 * compile "catch {expr}"
6246 */
6247 static char_u *
6248compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6249{
6250 scope_T *scope = cctx->ctx_scope;
6251 garray_T *instr = &cctx->ctx_instr;
6252 char_u *p;
6253 isn_T *isn;
6254
6255 // end block scope from :try or :catch
6256 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6257 compile_endblock(cctx);
6258 scope = cctx->ctx_scope;
6259
6260 // Error if not in a :try scope
6261 if (scope == NULL || scope->se_type != TRY_SCOPE)
6262 {
6263 emsg(_(e_catch));
6264 return NULL;
6265 }
6266
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006267 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006268 {
6269 emsg(_("E1033: catch unreachable after catch-all"));
6270 return NULL;
6271 }
6272
6273 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006274 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 JUMP_ALWAYS, cctx) == FAIL)
6276 return NULL;
6277
6278 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006279 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006280 if (isn->isn_arg.try.try_catch == 0)
6281 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006282 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006283 {
6284 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006285 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 isn->isn_arg.jump.jump_where = instr->ga_len;
6287 }
6288
6289 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006290 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006292 scope->se_u.se_try.ts_caught_all = TRUE;
6293 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294 }
6295 else
6296 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006297 char_u *end;
6298 char_u *pat;
6299 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006300 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006301 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 // Push v:exception, push {expr} and MATCH
6304 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6305
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006306 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006307 if (*end != *p)
6308 {
6309 semsg(_("E1067: Separator mismatch: %s"), p);
6310 vim_free(tofree);
6311 return FAIL;
6312 }
6313 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006314 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006315 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006316 len = (int)(end - tofree);
6317 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006318 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006319 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006320 if (pat == NULL)
6321 return FAIL;
6322 if (generate_PUSHS(cctx, pat) == FAIL)
6323 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6326 return NULL;
6327
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006328 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6330 return NULL;
6331 }
6332
6333 if (generate_instr(cctx, ISN_CATCH) == NULL)
6334 return NULL;
6335
6336 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6337 return NULL;
6338 return p;
6339}
6340
6341 static char_u *
6342compile_finally(char_u *arg, cctx_T *cctx)
6343{
6344 scope_T *scope = cctx->ctx_scope;
6345 garray_T *instr = &cctx->ctx_instr;
6346 isn_T *isn;
6347
6348 // end block scope from :try or :catch
6349 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6350 compile_endblock(cctx);
6351 scope = cctx->ctx_scope;
6352
6353 // Error if not in a :try scope
6354 if (scope == NULL || scope->se_type != TRY_SCOPE)
6355 {
6356 emsg(_(e_finally));
6357 return NULL;
6358 }
6359
6360 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006361 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006362 if (isn->isn_arg.try.try_finally != 0)
6363 {
6364 emsg(_(e_finally_dup));
6365 return NULL;
6366 }
6367
6368 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006369 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006370
Bram Moolenaar585fea72020-04-02 22:33:21 +02006371 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006372 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006373 {
6374 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006375 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006376 isn->isn_arg.jump.jump_where = instr->ga_len;
6377 }
6378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006379 // TODO: set index in ts_finally_label jumps
6380
6381 return arg;
6382}
6383
6384 static char_u *
6385compile_endtry(char_u *arg, cctx_T *cctx)
6386{
6387 scope_T *scope = cctx->ctx_scope;
6388 garray_T *instr = &cctx->ctx_instr;
6389 isn_T *isn;
6390
6391 // end block scope from :catch or :finally
6392 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6393 compile_endblock(cctx);
6394 scope = cctx->ctx_scope;
6395
6396 // Error if not in a :try scope
6397 if (scope == NULL || scope->se_type != TRY_SCOPE)
6398 {
6399 if (scope == NULL)
6400 emsg(_(e_no_endtry));
6401 else if (scope->se_type == WHILE_SCOPE)
6402 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006403 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006404 emsg(_(e_endfor));
6405 else
6406 emsg(_(e_endif));
6407 return NULL;
6408 }
6409
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006410 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006411 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6412 {
6413 emsg(_("E1032: missing :catch or :finally"));
6414 return NULL;
6415 }
6416
6417 // Fill in the "end" label in jumps at the end of the blocks, if not done
6418 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006419 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006420
6421 // End :catch or :finally scope: set value in ISN_TRY instruction
6422 if (isn->isn_arg.try.try_finally == 0)
6423 isn->isn_arg.try.try_finally = instr->ga_len;
6424 compile_endblock(cctx);
6425
6426 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6427 return NULL;
6428 return arg;
6429}
6430
6431/*
6432 * compile "throw {expr}"
6433 */
6434 static char_u *
6435compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6436{
6437 char_u *p = skipwhite(arg);
6438
Bram Moolenaara5565e42020-05-09 15:44:01 +02006439 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006440 return NULL;
6441 if (may_generate_2STRING(-1, cctx) == FAIL)
6442 return NULL;
6443 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6444 return NULL;
6445
6446 return p;
6447}
6448
6449/*
6450 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006451 * compile "echomsg expr"
6452 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006453 * compile "execute expr"
6454 */
6455 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006456compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006457{
6458 char_u *p = arg;
6459 int count = 0;
6460
6461 for (;;)
6462 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006463 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006464 return NULL;
6465 ++count;
6466 p = skipwhite(p);
6467 if (ends_excmd(*p))
6468 break;
6469 }
6470
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006471 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6472 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6473 else if (cmdidx == CMD_execute)
6474 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6475 else if (cmdidx == CMD_echomsg)
6476 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6477 else
6478 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006479 return p;
6480}
6481
6482/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006483 * A command that is not compiled, execute with legacy code.
6484 */
6485 static char_u *
6486compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6487{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006488 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006489 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006490
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006491 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006492 goto theend;
6493
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006494 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6495 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006496 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6497 {
6498 // expand filename in "syntax include [@group] filename"
6499 has_expr = TRUE;
6500 eap->arg = skipwhite(eap->arg + 7);
6501 if (*eap->arg == '@')
6502 eap->arg = skiptowhite(eap->arg);
6503 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006504
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006505 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006506 {
6507 int count = 0;
6508 char_u *start = skipwhite(line);
6509
6510 // :cmd xxx`=expr1`yyy`=expr2`zzz
6511 // PUSHS ":cmd xxx"
6512 // eval expr1
6513 // PUSHS "yyy"
6514 // eval expr2
6515 // PUSHS "zzz"
6516 // EXECCONCAT 5
6517 for (;;)
6518 {
6519 if (p > start)
6520 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006521 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006522 ++count;
6523 }
6524 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006525 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006526 return NULL;
6527 may_generate_2STRING(-1, cctx);
6528 ++count;
6529 p = skipwhite(p);
6530 if (*p != '`')
6531 {
6532 emsg(_("E1083: missing backtick"));
6533 return NULL;
6534 }
6535 start = p + 1;
6536
6537 p = (char_u *)strstr((char *)start, "`=");
6538 if (p == NULL)
6539 {
6540 if (*skipwhite(start) != NUL)
6541 {
6542 generate_PUSHS(cctx, vim_strsave(start));
6543 ++count;
6544 }
6545 break;
6546 }
6547 }
6548 generate_EXECCONCAT(cctx, count);
6549 }
6550 else
6551 generate_EXEC(cctx, line);
6552
6553theend:
6554 return (char_u *)"";
6555}
6556
6557/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006558 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006559 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006560 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006561 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006562add_def_function(ufunc_T *ufunc)
6563{
6564 dfunc_T *dfunc;
6565
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006566 if (def_functions.ga_len == 0)
6567 {
6568 // The first position is not used, so that a zero uf_dfunc_idx means it
6569 // wasn't set.
6570 if (ga_grow(&def_functions, 1) == FAIL)
6571 return FAIL;
6572 ++def_functions.ga_len;
6573 }
6574
Bram Moolenaar09689a02020-05-09 22:50:08 +02006575 // Add the function to "def_functions".
6576 if (ga_grow(&def_functions, 1) == FAIL)
6577 return FAIL;
6578 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6579 CLEAR_POINTER(dfunc);
6580 dfunc->df_idx = def_functions.ga_len;
6581 ufunc->uf_dfunc_idx = dfunc->df_idx;
6582 dfunc->df_ufunc = ufunc;
6583 ++def_functions.ga_len;
6584 return OK;
6585}
6586
6587/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588 * After ex_function() has collected all the function lines: parse and compile
6589 * the lines into instructions.
6590 * Adds the function to "def_functions".
6591 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6592 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006593 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006594 * This can be used recursively through compile_lambda(), which may reallocate
6595 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006596 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006598 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006599compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 char_u *line = NULL;
6602 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 cctx_T cctx;
6605 garray_T *instr;
6606 int called_emsg_before = called_emsg;
6607 int ret = FAIL;
6608 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006609 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006610 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006612 // When using a function that was compiled before: Free old instructions.
6613 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006614 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006616 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6617 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006618 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006620 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006621 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622
Bram Moolenaara80faa82020-04-12 19:37:17 +02006623 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006624 cctx.ctx_ufunc = ufunc;
6625 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006626 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006627 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6628 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6629 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6630 cctx.ctx_type_list = &ufunc->uf_type_list;
6631 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6632 instr = &cctx.ctx_instr;
6633
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006634 // Set the context to the function, it may be compiled when called from
6635 // another script. Set the script version to the most modern one.
6636 // The line number will be set in next_line_from_context().
6637 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006638 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6639
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006640 // Make sure error messages are OK.
6641 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6642 if (do_estack_push)
6643 estack_push_ufunc(ufunc, 1);
6644
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006645 if (ufunc->uf_def_args.ga_len > 0)
6646 {
6647 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006648 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006649 int i;
6650 char_u *arg;
6651 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6652
6653 // Produce instructions for the default values of optional arguments.
6654 // Store the instruction index in uf_def_arg_idx[] so that we know
6655 // where to start when the function is called, depending on the number
6656 // of arguments.
6657 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6658 if (ufunc->uf_def_arg_idx == NULL)
6659 goto erret;
6660 for (i = 0; i < count; ++i)
6661 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006662 garray_T *stack = &cctx.ctx_type_stack;
6663 type_T *val_type;
6664 int arg_idx = first_def_arg + i;
6665
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006666 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6667 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006668 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006669 goto erret;
6670
6671 // If no type specified use the type of the default value.
6672 // Otherwise check that the default value type matches the
6673 // specified type.
6674 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6675 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6676 ufunc->uf_arg_types[arg_idx] = val_type;
6677 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6678 == FAIL)
6679 {
6680 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6681 arg_idx + 1);
6682 goto erret;
6683 }
6684
6685 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006686 goto erret;
6687 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006688 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6689 }
6690
6691 /*
6692 * Loop over all the lines of the function and generate instructions.
6693 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 for (;;)
6695 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006696 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006697 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006698 char_u *cmd;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006699
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006700 // Bail out on the first error to avoid a flood of errors and report
6701 // the right line number when inside try/catch.
6702 if (emsg_before != called_emsg)
6703 goto erret;
6704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006705 if (line != NULL && *line == '|')
6706 // the line continues after a '|'
6707 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006708 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006709 && !(*line == '#' && (line == cctx.ctx_line_start
6710 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006711 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006712 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 goto erret;
6714 }
6715 else
6716 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006717 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006718 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006719 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006720 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006722 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723
Bram Moolenaara80faa82020-04-12 19:37:17 +02006724 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725 ea.cmdlinep = &line;
6726 ea.cmd = skipwhite(line);
6727
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006728 // Some things can be recognized by the first character.
6729 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006730 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006731 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006732 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006733 if (ea.cmd[1] != '{')
6734 {
6735 line = (char_u *)"";
6736 continue;
6737 }
6738 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006740 case '}':
6741 {
6742 // "}" ends a block scope
6743 scopetype_T stype = cctx.ctx_scope == NULL
6744 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006746 if (stype == BLOCK_SCOPE)
6747 {
6748 compile_endblock(&cctx);
6749 line = ea.cmd;
6750 }
6751 else
6752 {
6753 emsg(_("E1025: using } outside of a block scope"));
6754 goto erret;
6755 }
6756 if (line != NULL)
6757 line = skipwhite(ea.cmd + 1);
6758 continue;
6759 }
6760
6761 case '{':
6762 // "{" starts a block scope
6763 // "{'a': 1}->func() is something else
6764 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6765 {
6766 line = compile_block(ea.cmd, &cctx);
6767 continue;
6768 }
6769 break;
6770
6771 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006772 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006773 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006774 }
6775
6776 /*
6777 * COMMAND MODIFIERS
6778 */
6779 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6780 {
6781 if (errormsg != NULL)
6782 goto erret;
6783 // empty line or comment
6784 line = (char_u *)"";
6785 continue;
6786 }
6787
6788 // Skip ":call" to get to the function name.
6789 if (checkforcmd(&ea.cmd, "call", 3))
6790 ea.cmd = skipwhite(ea.cmd);
6791
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006792 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006794 char_u *pskip;
6795
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006796 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006797 // find what follows.
6798 // Skip over "var.member", "var[idx]" and the like.
6799 // Also "&opt = val", "$ENV = val" and "@r = val".
6800 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006801 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006802 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006803 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006805 char_u *var_end;
6806 int oplen;
6807 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006809 var_end = find_name_end(pskip, NULL, NULL,
6810 FNE_CHECK_START | FNE_INCL_BR);
6811 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006812 if (oplen > 0)
6813 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006814 size_t len = p - ea.cmd;
6815
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006816 // Recognize an assignment if we recognize the variable
6817 // name:
6818 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006819 // "local = expr" where "local" is a local var.
6820 // "script = expr" where "script" is a script-local var.
6821 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006822 // "&opt = expr"
6823 // "$ENV = expr"
6824 // "@r = expr"
6825 if (*ea.cmd == '&'
6826 || *ea.cmd == '$'
6827 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006828 || ((len) > 2 && ea.cmd[1] == ':')
6829 || lookup_local(ea.cmd, len, &cctx) != NULL
6830 || lookup_arg(ea.cmd, len, NULL, NULL,
6831 NULL, &cctx) == OK
6832 || lookup_script(ea.cmd, len) == OK
6833 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006834 {
6835 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006836 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006837 goto erret;
6838 continue;
6839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 }
6841 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006842
6843 if (*ea.cmd == '[')
6844 {
6845 // [var, var] = expr
6846 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6847 if (line == NULL)
6848 goto erret;
6849 if (line != ea.cmd)
6850 continue;
6851 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006852 }
6853
6854 /*
6855 * COMMAND after range
6856 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006857 cmd = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006858 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006859 if (ea.cmd > cmd && !starts_with_colon)
6860 {
6861 emsg(_(e_colon_required));
6862 goto erret;
6863 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006864 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006865 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006866 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006867
6868 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6869 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006870 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006871 {
6872 line += STRLEN(line);
6873 continue;
6874 }
6875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006876 // Expression or function call.
6877 if (ea.cmdidx == CMD_eval)
6878 {
6879 p = ea.cmd;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006880 if (compile_expr0(&p, &cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 goto erret;
6882
6883 // drop the return value
6884 generate_instr_drop(&cctx, ISN_DROP, 1);
6885 line = p;
6886 continue;
6887 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006888 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889 iemsg("Command from find_ex_command() not handled");
6890 goto erret;
6891 }
6892
6893 p = skipwhite(p);
6894
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006895 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006896 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006897 && ea.cmdidx != CMD_elseif
6898 && ea.cmdidx != CMD_else
6899 && ea.cmdidx != CMD_endif)
6900 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006901 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006902 continue;
6903 }
6904
Bram Moolenaarefd88552020-06-18 20:50:10 +02006905 if (ea.cmdidx != CMD_elseif
6906 && ea.cmdidx != CMD_else
6907 && ea.cmdidx != CMD_endif
6908 && ea.cmdidx != CMD_endfor
6909 && ea.cmdidx != CMD_endwhile
6910 && ea.cmdidx != CMD_catch
6911 && ea.cmdidx != CMD_finally
6912 && ea.cmdidx != CMD_endtry)
6913 {
6914 if (cctx.ctx_had_return)
6915 {
6916 emsg(_("E1095: Unreachable code after :return"));
6917 goto erret;
6918 }
6919 }
6920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921 switch (ea.cmdidx)
6922 {
6923 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006924 ea.arg = p;
6925 line = compile_nested_function(&ea, &cctx);
6926 break;
6927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006928 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006929 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 goto erret;
6931
6932 case CMD_return:
6933 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006934 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 break;
6936
6937 case CMD_let:
6938 case CMD_const:
6939 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006940 if (line == p)
6941 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942 break;
6943
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006944 case CMD_unlet:
6945 case CMD_unlockvar:
6946 case CMD_lockvar:
6947 line = compile_unletlock(p, &ea, &cctx);
6948 break;
6949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 case CMD_import:
6951 line = compile_import(p, &cctx);
6952 break;
6953
6954 case CMD_if:
6955 line = compile_if(p, &cctx);
6956 break;
6957 case CMD_elseif:
6958 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006959 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006960 break;
6961 case CMD_else:
6962 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006963 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006964 break;
6965 case CMD_endif:
6966 line = compile_endif(p, &cctx);
6967 break;
6968
6969 case CMD_while:
6970 line = compile_while(p, &cctx);
6971 break;
6972 case CMD_endwhile:
6973 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006974 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975 break;
6976
6977 case CMD_for:
6978 line = compile_for(p, &cctx);
6979 break;
6980 case CMD_endfor:
6981 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006982 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983 break;
6984 case CMD_continue:
6985 line = compile_continue(p, &cctx);
6986 break;
6987 case CMD_break:
6988 line = compile_break(p, &cctx);
6989 break;
6990
6991 case CMD_try:
6992 line = compile_try(p, &cctx);
6993 break;
6994 case CMD_catch:
6995 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006996 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997 break;
6998 case CMD_finally:
6999 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007000 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001 break;
7002 case CMD_endtry:
7003 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007004 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005 break;
7006 case CMD_throw:
7007 line = compile_throw(p, &cctx);
7008 break;
7009
7010 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007012 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007013 case CMD_echomsg:
7014 case CMD_echoerr:
7015 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007016 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007018 // TODO: other commands with an expression argument
7019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007021 // Not recognized, execute with do_cmdline_cmd().
7022 ea.arg = p;
7023 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024 break;
7025 }
7026 if (line == NULL)
7027 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007028 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029
7030 if (cctx.ctx_type_stack.ga_len < 0)
7031 {
7032 iemsg("Type stack underflow");
7033 goto erret;
7034 }
7035 }
7036
7037 if (cctx.ctx_scope != NULL)
7038 {
7039 if (cctx.ctx_scope->se_type == IF_SCOPE)
7040 emsg(_(e_endif));
7041 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7042 emsg(_(e_endwhile));
7043 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7044 emsg(_(e_endfor));
7045 else
7046 emsg(_("E1026: Missing }"));
7047 goto erret;
7048 }
7049
Bram Moolenaarefd88552020-06-18 20:50:10 +02007050 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051 {
7052 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7053 {
7054 emsg(_("E1027: Missing return statement"));
7055 goto erret;
7056 }
7057
7058 // Return zero if there is no return at the end.
7059 generate_PUSHNR(&cctx, 0);
7060 generate_instr(&cctx, ISN_RETURN);
7061 }
7062
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007063 {
7064 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7065 + ufunc->uf_dfunc_idx;
7066 dfunc->df_deleted = FALSE;
7067 dfunc->df_instr = instr->ga_data;
7068 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007069 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007070 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007071 if (cctx.ctx_outer_used)
7072 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007073 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075
7076 ret = OK;
7077
7078erret:
7079 if (ret == FAIL)
7080 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007081 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007082 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7083 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007084
7085 for (idx = 0; idx < instr->ga_len; ++idx)
7086 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007087 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007088
Bram Moolenaar45a15082020-05-25 00:28:33 +02007089 // if using the last entry in the table we might as well remove it
7090 if (!dfunc->df_deleted
7091 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007092 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007093 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007094
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007095 while (cctx.ctx_scope != NULL)
7096 drop_scope(&cctx);
7097
Bram Moolenaar20431c92020-03-20 18:39:46 +01007098 // Don't execute this function body.
7099 ga_clear_strings(&ufunc->uf_lines);
7100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101 if (errormsg != NULL)
7102 emsg(errormsg);
7103 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007104 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105 }
7106
7107 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007108 if (do_estack_push)
7109 estack_pop();
7110
Bram Moolenaar20431c92020-03-20 18:39:46 +01007111 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007112 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007114 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007115}
7116
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007117 void
7118set_function_type(ufunc_T *ufunc)
7119{
7120 int varargs = ufunc->uf_va_name != NULL;
7121 int argcount = ufunc->uf_args.ga_len;
7122
7123 // Create a type for the function, with the return type and any
7124 // argument types.
7125 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7126 // The type is included in "tt_args".
7127 if (argcount > 0 || varargs)
7128 {
7129 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7130 argcount, &ufunc->uf_type_list);
7131 // Add argument types to the function type.
7132 if (func_type_add_arg_types(ufunc->uf_func_type,
7133 argcount + varargs,
7134 &ufunc->uf_type_list) == FAIL)
7135 return;
7136 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7137 ufunc->uf_func_type->tt_min_argcount =
7138 argcount - ufunc->uf_def_args.ga_len;
7139 if (ufunc->uf_arg_types == NULL)
7140 {
7141 int i;
7142
7143 // lambda does not have argument types.
7144 for (i = 0; i < argcount; ++i)
7145 ufunc->uf_func_type->tt_args[i] = &t_any;
7146 }
7147 else
7148 mch_memmove(ufunc->uf_func_type->tt_args,
7149 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7150 if (varargs)
7151 {
7152 ufunc->uf_func_type->tt_args[argcount] =
7153 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7154 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7155 }
7156 }
7157 else
7158 // No arguments, can use a predefined type.
7159 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7160 argcount, &ufunc->uf_type_list);
7161}
7162
7163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164/*
7165 * Delete an instruction, free what it contains.
7166 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007167 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168delete_instr(isn_T *isn)
7169{
7170 switch (isn->isn_type)
7171 {
7172 case ISN_EXEC:
7173 case ISN_LOADENV:
7174 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007175 case ISN_LOADB:
7176 case ISN_LOADW:
7177 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007179 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180 case ISN_PUSHEXC:
7181 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007182 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007184 case ISN_STOREB:
7185 case ISN_STOREW:
7186 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007187 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188 vim_free(isn->isn_arg.string);
7189 break;
7190
7191 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007192 case ISN_STORES:
7193 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194 break;
7195
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007196 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007197 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007198 vim_free(isn->isn_arg.unlet.ul_name);
7199 break;
7200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007201 case ISN_STOREOPT:
7202 vim_free(isn->isn_arg.storeopt.so_name);
7203 break;
7204
7205 case ISN_PUSHBLOB: // push blob isn_arg.blob
7206 blob_unref(isn->isn_arg.blob);
7207 break;
7208
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007209 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007210#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007211 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007212#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007213 break;
7214
7215 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007216#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007217 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007218#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007219 break;
7220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007221 case ISN_UCALL:
7222 vim_free(isn->isn_arg.ufunc.cuf_name);
7223 break;
7224
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007225 case ISN_FUNCREF:
7226 {
7227 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7228 + isn->isn_arg.funcref.fr_func;
7229 func_ptr_unref(dfunc->df_ufunc);
7230 }
7231 break;
7232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007233 case ISN_2BOOL:
7234 case ISN_2STRING:
7235 case ISN_ADDBLOB:
7236 case ISN_ADDLIST:
7237 case ISN_BCALL:
7238 case ISN_CATCH:
7239 case ISN_CHECKNR:
7240 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007241 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007242 case ISN_COMPAREANY:
7243 case ISN_COMPAREBLOB:
7244 case ISN_COMPAREBOOL:
7245 case ISN_COMPAREDICT:
7246 case ISN_COMPAREFLOAT:
7247 case ISN_COMPAREFUNC:
7248 case ISN_COMPARELIST:
7249 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007250 case ISN_COMPARESPECIAL:
7251 case ISN_COMPARESTRING:
7252 case ISN_CONCAT:
7253 case ISN_DCALL:
7254 case ISN_DROP:
7255 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007256 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007257 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007259 case ISN_EXECCONCAT:
7260 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007262 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007263 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007264 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007265 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007266 case ISN_JUMP:
7267 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007268 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269 case ISN_LOADSCRIPT:
7270 case ISN_LOADREG:
7271 case ISN_LOADV:
7272 case ISN_NEGATENR:
7273 case ISN_NEWDICT:
7274 case ISN_NEWLIST:
7275 case ISN_OPNR:
7276 case ISN_OPFLOAT:
7277 case ISN_OPANY:
7278 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007279 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007280 case ISN_PUSHF:
7281 case ISN_PUSHNR:
7282 case ISN_PUSHBOOL:
7283 case ISN_PUSHSPEC:
7284 case ISN_RETURN:
7285 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007286 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007287 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007288 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007289 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007291 case ISN_STOREDICT:
7292 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007293 case ISN_THROW:
7294 case ISN_TRY:
7295 // nothing allocated
7296 break;
7297 }
7298}
7299
7300/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007301 * Free all instructions for "dfunc".
7302 */
7303 static void
7304delete_def_function_contents(dfunc_T *dfunc)
7305{
7306 int idx;
7307
7308 ga_clear(&dfunc->df_def_args_isn);
7309
7310 if (dfunc->df_instr != NULL)
7311 {
7312 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7313 delete_instr(dfunc->df_instr + idx);
7314 VIM_CLEAR(dfunc->df_instr);
7315 }
7316
7317 dfunc->df_deleted = TRUE;
7318}
7319
7320/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007321 * When a user function is deleted, clear the contents of any associated def
7322 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323 */
7324 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007325clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007327 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328 {
7329 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7330 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331
Bram Moolenaar20431c92020-03-20 18:39:46 +01007332 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007333 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 }
7335}
7336
7337#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007338/*
7339 * Free all functions defined with ":def".
7340 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341 void
7342free_def_functions(void)
7343{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007344 int idx;
7345
7346 for (idx = 0; idx < def_functions.ga_len; ++idx)
7347 {
7348 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7349
7350 delete_def_function_contents(dfunc);
7351 }
7352
7353 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354}
7355#endif
7356
7357
7358#endif // FEAT_EVAL