blob: 3b021afdcb4eb4b9bc73e75ea7ce31d3c236c747 [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 Moolenaarb283a8a2020-02-02 22:24:04 +0100646 emsg(_("E1035: 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 Moolenaare6085c52020-04-12 20:19:16 +02002384 * Get the next line of the function from "cctx".
2385 * Returns NULL when at the end.
2386 */
2387 static char_u *
2388next_line_from_context(cctx_T *cctx)
2389{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002390 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002391
2392 do
2393 {
2394 ++cctx->ctx_lnum;
2395 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002396 {
2397 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002398 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002399 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002400 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002401 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002402 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002403 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002404 return line;
2405}
2406
2407/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002408 * Return TRUE if "p" points at a "#" but not at "#{".
2409 */
2410 static int
2411comment_start(char_u *p)
2412{
2413 return p[0] == '#' && p[1] != '{';
2414}
2415
2416/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002417 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002418 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002419 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2420 */
2421 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002422may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002423{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002424 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002425 {
2426 char_u *next = next_line_from_context(cctx);
2427
2428 if (next == NULL)
2429 return FAIL;
2430 *arg = skipwhite(next);
2431 }
2432 return OK;
2433}
2434
Bram Moolenaara5565e42020-05-09 15:44:01 +02002435// Structure passed between the compile_expr* functions to keep track of
2436// constants that have been parsed but for which no code was produced yet. If
2437// possible expressions on these constants are applied at compile time. If
2438// that is not possible, the code to push the constants needs to be generated
2439// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002440// Using 50 should be more than enough of 5 levels of ().
2441#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002442typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002443 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002444 int pp_used; // active entries in pp_tv[]
2445} ppconst_T;
2446
Bram Moolenaar1c747212020-05-09 18:28:34 +02002447static int compile_expr0(char_u **arg, cctx_T *cctx);
2448static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2449
Bram Moolenaara5565e42020-05-09 15:44:01 +02002450/*
2451 * Generate a PUSH instruction for "tv".
2452 * "tv" will be consumed or cleared.
2453 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2454 */
2455 static int
2456generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2457{
2458 if (tv != NULL)
2459 {
2460 switch (tv->v_type)
2461 {
2462 case VAR_UNKNOWN:
2463 break;
2464 case VAR_BOOL:
2465 generate_PUSHBOOL(cctx, tv->vval.v_number);
2466 break;
2467 case VAR_SPECIAL:
2468 generate_PUSHSPEC(cctx, tv->vval.v_number);
2469 break;
2470 case VAR_NUMBER:
2471 generate_PUSHNR(cctx, tv->vval.v_number);
2472 break;
2473#ifdef FEAT_FLOAT
2474 case VAR_FLOAT:
2475 generate_PUSHF(cctx, tv->vval.v_float);
2476 break;
2477#endif
2478 case VAR_BLOB:
2479 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2480 tv->vval.v_blob = NULL;
2481 break;
2482 case VAR_STRING:
2483 generate_PUSHS(cctx, tv->vval.v_string);
2484 tv->vval.v_string = NULL;
2485 break;
2486 default:
2487 iemsg("constant type not supported");
2488 clear_tv(tv);
2489 return FAIL;
2490 }
2491 tv->v_type = VAR_UNKNOWN;
2492 }
2493 return OK;
2494}
2495
2496/*
2497 * Generate code for any ppconst entries.
2498 */
2499 static int
2500generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2501{
2502 int i;
2503 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002504 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002505
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002506 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002507 for (i = 0; i < ppconst->pp_used; ++i)
2508 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2509 ret = FAIL;
2510 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002511 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002512 return ret;
2513}
2514
2515/*
2516 * Clear ppconst constants. Used when failing.
2517 */
2518 static void
2519clear_ppconst(ppconst_T *ppconst)
2520{
2521 int i;
2522
2523 for (i = 0; i < ppconst->pp_used; ++i)
2524 clear_tv(&ppconst->pp_tv[i]);
2525 ppconst->pp_used = 0;
2526}
2527
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002528/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002529 * Generate an instruction to load script-local variable "name", without the
2530 * leading "s:".
2531 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 */
2533 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002534compile_load_scriptvar(
2535 cctx_T *cctx,
2536 char_u *name, // variable NUL terminated
2537 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002538 char_u **end, // end of variable
2539 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002541 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2543 imported_T *import;
2544
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002545 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002547 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002548 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2549 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 }
2551 if (idx >= 0)
2552 {
2553 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2554
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002555 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 current_sctx.sc_sid, idx, sv->sv_type);
2557 return OK;
2558 }
2559
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002560 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 if (import != NULL)
2562 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002563 if (import->imp_all)
2564 {
2565 char_u *p = skipwhite(*end);
2566 int name_len;
2567 ufunc_T *ufunc;
2568 type_T *type;
2569
2570 // Used "import * as Name", need to lookup the member.
2571 if (*p != '.')
2572 {
2573 semsg(_("E1060: expected dot after name: %s"), start);
2574 return FAIL;
2575 }
2576 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002577 if (VIM_ISWHITE(*p))
2578 {
2579 emsg(_("E1074: no white space allowed after dot"));
2580 return FAIL;
2581 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002582
2583 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2584 // TODO: what if it is a function?
2585 if (idx < 0)
2586 return FAIL;
2587 *end = p;
2588
2589 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2590 import->imp_sid,
2591 idx,
2592 type);
2593 }
2594 else
2595 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002596 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002597 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2598 import->imp_sid,
2599 import->imp_var_vals_idx,
2600 import->imp_type);
2601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 return OK;
2603 }
2604
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002605 if (error)
2606 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 return FAIL;
2608}
2609
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002610 static int
2611generate_funcref(cctx_T *cctx, char_u *name)
2612{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002613 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002614
2615 if (ufunc == NULL)
2616 return FAIL;
2617
2618 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2619}
2620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621/*
2622 * Compile a variable name into a load instruction.
2623 * "end" points to just after the name.
2624 * When "error" is FALSE do not give an error when not found.
2625 */
2626 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002627compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628{
2629 type_T *type;
2630 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002631 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002633 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634
2635 if (*(*arg + 1) == ':')
2636 {
2637 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002638 if (end <= *arg + 2)
2639 name = vim_strsave((char_u *)"[empty]");
2640 else
2641 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 if (name == NULL)
2643 return FAIL;
2644
2645 if (**arg == 'v')
2646 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002647 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 }
2649 else if (**arg == 'g')
2650 {
2651 // Global variables can be defined later, thus we don't check if it
2652 // exists, give error at runtime.
2653 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2654 }
2655 else if (**arg == 's')
2656 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002657 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002659 else if (**arg == 'b')
2660 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002661 // Buffer-local variables can be defined later, thus we don't check
2662 // if it exists, give error at runtime.
2663 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002664 }
2665 else if (**arg == 'w')
2666 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002667 // Window-local variables can be defined later, thus we don't check
2668 // if it exists, give error at runtime.
2669 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002670 }
2671 else if (**arg == 't')
2672 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002673 // Tabpage-local variables can be defined later, thus we don't
2674 // check if it exists, give error at runtime.
2675 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 else
2678 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002679 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 goto theend;
2681 }
2682 }
2683 else
2684 {
2685 size_t len = end - *arg;
2686 int idx;
2687 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002688 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689
2690 name = vim_strnsave(*arg, end - *arg);
2691 if (name == NULL)
2692 return FAIL;
2693
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002694 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002696 if (!gen_load_outer)
2697 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 }
2699 else
2700 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002701 lvar_T *lvar = lookup_local(*arg, len, cctx);
2702
2703 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002705 type = lvar->lv_type;
2706 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002707 if (lvar->lv_from_outer)
2708 gen_load_outer = TRUE;
2709 else
2710 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 }
2712 else
2713 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002714 // "var" can be script-local even without using "s:" if it
2715 // already exists.
2716 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2717 == SCRIPT_VERSION_VIM9
2718 || lookup_script(*arg, len) == OK)
2719 res = compile_load_scriptvar(cctx, name, *arg, &end,
2720 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002721
Bram Moolenaara5565e42020-05-09 15:44:01 +02002722 // When the name starts with an uppercase letter or "x:" it
2723 // can be a user defined function.
2724 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2725 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 }
2727 }
2728 if (gen_load)
2729 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002730 if (gen_load_outer)
2731 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 }
2733
2734 *arg = end;
2735
2736theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002737 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 semsg(_(e_var_notfound), name);
2739 vim_free(name);
2740 return res;
2741}
2742
2743/*
2744 * Compile the argument expressions.
2745 * "arg" points to just after the "(" and is advanced to after the ")"
2746 */
2747 static int
2748compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2749{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002750 char_u *p = *arg;
2751 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752
Bram Moolenaare6085c52020-04-12 20:19:16 +02002753 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002755 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002756 {
2757 p = next_line_from_context(cctx);
2758 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002759 goto failret;
2760 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002761 p = skipwhite(p);
2762 }
2763 if (*p == ')')
2764 {
2765 *arg = p + 1;
2766 return OK;
2767 }
2768
Bram Moolenaara5565e42020-05-09 15:44:01 +02002769 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 return FAIL;
2771 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002772
2773 if (*p != ',' && *skipwhite(p) == ',')
2774 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002775 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002776 p = skipwhite(p);
2777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002779 {
2780 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002781 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002782 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002783 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002784 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002785 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002787failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002788 emsg(_(e_missing_close));
2789 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790}
2791
2792/*
2793 * Compile a function call: name(arg1, arg2)
2794 * "arg" points to "name", "arg + varlen" to the "(".
2795 * "argcount_init" is 1 for "value->method()"
2796 * Instructions:
2797 * EVAL arg1
2798 * EVAL arg2
2799 * BCALL / DCALL / UCALL
2800 */
2801 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002802compile_call(
2803 char_u **arg,
2804 size_t varlen,
2805 cctx_T *cctx,
2806 ppconst_T *ppconst,
2807 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808{
2809 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002810 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 int argcount = argcount_init;
2812 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002813 char_u fname_buf[FLEN_FIXED + 1];
2814 char_u *tofree = NULL;
2815 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002817 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818
Bram Moolenaara5565e42020-05-09 15:44:01 +02002819 // we can evaluate "has('name')" at compile time
2820 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2821 {
2822 char_u *s = skipwhite(*arg + varlen + 1);
2823 typval_T argvars[2];
2824
2825 argvars[0].v_type = VAR_UNKNOWN;
2826 if (*s == '"')
2827 (void)get_string_tv(&s, &argvars[0], TRUE);
2828 else if (*s == '\'')
2829 (void)get_lit_string_tv(&s, &argvars[0], TRUE);
2830 s = skipwhite(s);
2831 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2832 {
2833 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2834
2835 *arg = s + 1;
2836 argvars[1].v_type = VAR_UNKNOWN;
2837 tv->v_type = VAR_NUMBER;
2838 tv->vval.v_number = 0;
2839 f_has(argvars, tv);
2840 clear_tv(&argvars[0]);
2841 ++ppconst->pp_used;
2842 return OK;
2843 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002844 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002845 }
2846
2847 if (generate_ppconst(cctx, ppconst) == FAIL)
2848 return FAIL;
2849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 if (varlen >= sizeof(namebuf))
2851 {
2852 semsg(_("E1011: name too long: %s"), name);
2853 return FAIL;
2854 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002855 vim_strncpy(namebuf, *arg, varlen);
2856 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857
2858 *arg = skipwhite(*arg + varlen + 1);
2859 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002860 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002862 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 {
2864 int idx;
2865
2866 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002867 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002869 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002870 else
2871 semsg(_(e_unknownfunc), namebuf);
2872 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 }
2874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002876 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002878 {
2879 res = generate_CALL(cctx, ufunc, argcount);
2880 goto theend;
2881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882
2883 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002884 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002886 if (STRNCMP(namebuf, "g:", 2) != 0
2887 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002888 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002889 garray_T *stack = &cctx->ctx_type_stack;
2890 type_T *type;
2891
2892 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2893 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002894 goto theend;
2895 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002897 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002898 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002899 if (STRNCMP(namebuf, "g:", 2) == 0)
2900 res = generate_UCALL(cctx, name, argcount);
2901 else
2902 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002903
2904theend:
2905 vim_free(tofree);
2906 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907}
2908
2909// like NAMESPACE_CHAR but with 'a' and 'l'.
2910#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2911
2912/*
2913 * Find the end of a variable or function name. Unlike find_name_end() this
2914 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002915 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 * Return a pointer to just after the name. Equal to "arg" if there is no
2917 * valid name.
2918 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002919 static char_u *
2920to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921{
2922 char_u *p;
2923
2924 // Quick check for valid starting character.
2925 if (!eval_isnamec1(*arg))
2926 return arg;
2927
2928 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2929 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2930 // and can be used in slice "[n:]".
2931 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002932 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2934 break;
2935 return p;
2936}
2937
2938/*
2939 * Like to_name_end() but also skip over a list or dict constant.
2940 */
2941 char_u *
2942to_name_const_end(char_u *arg)
2943{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002944 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 typval_T rettv;
2946
2947 if (p == arg && *arg == '[')
2948 {
2949
2950 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar32e35112020-05-14 22:41:15 +02002951 if (get_list_tv(&p, &rettv, 0, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 p = arg;
2953 }
2954 else if (p == arg && *arg == '#' && arg[1] == '{')
2955 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002956 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 ++p;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002958 if (eval_dict(&p, &rettv, 0, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 p = arg;
2960 }
2961 else if (p == arg && *arg == '{')
2962 {
2963 int ret = get_lambda_tv(&p, &rettv, FALSE);
2964
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002965 // Can be "{x -> ret}()".
2966 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 if (ret == NOTDONE)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002968 ret = eval_dict(&p, &rettv, 0, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 if (ret != OK)
2970 p = arg;
2971 }
2972
2973 return p;
2974}
2975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976/*
2977 * parse a list: [expr, expr]
2978 * "*arg" points to the '['.
2979 */
2980 static int
2981compile_list(char_u **arg, cctx_T *cctx)
2982{
2983 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002984 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 int count = 0;
2986
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002987 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002989 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002990 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002991 p = next_line_from_context(cctx);
2992 if (p == NULL)
2993 {
2994 semsg(_(e_list_end), *arg);
2995 return FAIL;
2996 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002997 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002998 p = skipwhite(p);
2999 }
3000 if (*p == ']')
3001 {
3002 ++p;
3003 // Allow for following comment, after at least one space.
3004 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
3005 p += STRLEN(p);
3006 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003007 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003008 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 break;
3010 ++count;
3011 if (*p == ',')
3012 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003013 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 p = skipwhite(p);
3015 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003016 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017
3018 generate_NEWLIST(cctx, count);
3019 return OK;
3020}
3021
3022/*
3023 * parse a lambda: {arg, arg -> expr}
3024 * "*arg" points to the '{'.
3025 */
3026 static int
3027compile_lambda(char_u **arg, cctx_T *cctx)
3028{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 typval_T rettv;
3030 ufunc_T *ufunc;
3031
3032 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01003033 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003037 ++ufunc->uf_refcount;
3038 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003039 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040
3041 // The function will have one line: "return {expr}".
3042 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003043 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003045 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003046 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 return FAIL;
3048}
3049
3050/*
3051 * Compile a lamda call: expr->{lambda}(args)
3052 * "arg" points to the "{".
3053 */
3054 static int
3055compile_lambda_call(char_u **arg, cctx_T *cctx)
3056{
3057 ufunc_T *ufunc;
3058 typval_T rettv;
3059 int argcount = 1;
3060 int ret = FAIL;
3061
3062 // Get the funcref in "rettv".
3063 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
3064 return FAIL;
3065
3066 if (**arg != '(')
3067 {
3068 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003069 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 else
3071 semsg(_(e_missing_paren), "lambda");
3072 clear_tv(&rettv);
3073 return FAIL;
3074 }
3075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 ufunc = rettv.vval.v_partial->pt_func;
3077 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003078 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003079 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003080
3081 // The function will have one line: "return {expr}".
3082 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003083 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084
3085 // compile the arguments
3086 *arg = skipwhite(*arg + 1);
3087 if (compile_arguments(arg, cctx, &argcount) == OK)
3088 // call the compiled function
3089 ret = generate_CALL(cctx, ufunc, argcount);
3090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 return ret;
3092}
3093
3094/*
3095 * parse a dict: {'key': val} or #{key: val}
3096 * "*arg" points to the '{'.
3097 */
3098 static int
3099compile_dict(char_u **arg, cctx_T *cctx, int literal)
3100{
3101 garray_T *instr = &cctx->ctx_instr;
3102 int count = 0;
3103 dict_T *d = dict_alloc();
3104 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003105 char_u *whitep = *arg;
3106 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107
3108 if (d == NULL)
3109 return FAIL;
3110 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003111 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 {
3113 char_u *key = NULL;
3114
Bram Moolenaar2c330432020-04-13 14:41:35 +02003115 while (**arg == NUL || (literal && **arg == '"')
3116 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003117 {
3118 *arg = next_line_from_context(cctx);
3119 if (*arg == NULL)
3120 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003121 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003122 *arg = skipwhite(*arg);
3123 }
3124
3125 if (**arg == '}')
3126 break;
3127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 if (literal)
3129 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003130 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131
Bram Moolenaar2c330432020-04-13 14:41:35 +02003132 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 {
3134 semsg(_("E1014: Invalid key: %s"), *arg);
3135 return FAIL;
3136 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003137 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 if (generate_PUSHS(cctx, key) == FAIL)
3139 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003140 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 }
3142 else
3143 {
3144 isn_T *isn;
3145
Bram Moolenaara5565e42020-05-09 15:44:01 +02003146 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 return FAIL;
3148 // TODO: check type is string
3149 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3150 if (isn->isn_type == ISN_PUSHS)
3151 key = isn->isn_arg.string;
3152 }
3153
3154 // Check for duplicate keys, if using string keys.
3155 if (key != NULL)
3156 {
3157 item = dict_find(d, key, -1);
3158 if (item != NULL)
3159 {
3160 semsg(_(e_duplicate_key), key);
3161 goto failret;
3162 }
3163 item = dictitem_alloc(key);
3164 if (item != NULL)
3165 {
3166 item->di_tv.v_type = VAR_UNKNOWN;
3167 item->di_tv.v_lock = 0;
3168 if (dict_add(d, item) == FAIL)
3169 dictitem_free(item);
3170 }
3171 }
3172
3173 *arg = skipwhite(*arg);
3174 if (**arg != ':')
3175 {
3176 semsg(_(e_missing_dict_colon), *arg);
3177 return FAIL;
3178 }
3179
Bram Moolenaar2c330432020-04-13 14:41:35 +02003180 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003182 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003183 {
3184 *arg = next_line_from_context(cctx);
3185 if (*arg == NULL)
3186 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003187 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003188 *arg = skipwhite(*arg);
3189 }
3190
Bram Moolenaara5565e42020-05-09 15:44:01 +02003191 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 return FAIL;
3193 ++count;
3194
Bram Moolenaar2c330432020-04-13 14:41:35 +02003195 whitep = *arg;
3196 p = skipwhite(*arg);
3197 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003198 {
3199 *arg = next_line_from_context(cctx);
3200 if (*arg == NULL)
3201 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003202 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003203 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003204 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 if (**arg == '}')
3207 break;
3208 if (**arg != ',')
3209 {
3210 semsg(_(e_missing_dict_comma), *arg);
3211 goto failret;
3212 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003213 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 *arg = skipwhite(*arg + 1);
3215 }
3216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 *arg = *arg + 1;
3218
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003219 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003220 p = skipwhite(*arg);
3221 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003222 *arg += STRLEN(*arg);
3223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 dict_unref(d);
3225 return generate_NEWDICT(cctx, count);
3226
3227failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003228 if (*arg == NULL)
3229 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 dict_unref(d);
3231 return FAIL;
3232}
3233
3234/*
3235 * Compile "&option".
3236 */
3237 static int
3238compile_get_option(char_u **arg, cctx_T *cctx)
3239{
3240 typval_T rettv;
3241 char_u *start = *arg;
3242 int ret;
3243
3244 // parse the option and get the current value to get the type.
3245 rettv.v_type = VAR_UNKNOWN;
3246 ret = get_option_tv(arg, &rettv, TRUE);
3247 if (ret == OK)
3248 {
3249 // include the '&' in the name, get_option_tv() expects it.
3250 char_u *name = vim_strnsave(start, *arg - start);
3251 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3252
3253 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3254 vim_free(name);
3255 }
3256 clear_tv(&rettv);
3257
3258 return ret;
3259}
3260
3261/*
3262 * Compile "$VAR".
3263 */
3264 static int
3265compile_get_env(char_u **arg, cctx_T *cctx)
3266{
3267 char_u *start = *arg;
3268 int len;
3269 int ret;
3270 char_u *name;
3271
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 ++*arg;
3273 len = get_env_len(arg);
3274 if (len == 0)
3275 {
3276 semsg(_(e_syntax_at), start - 1);
3277 return FAIL;
3278 }
3279
3280 // include the '$' in the name, get_env_tv() expects it.
3281 name = vim_strnsave(start, len + 1);
3282 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3283 vim_free(name);
3284 return ret;
3285}
3286
3287/*
3288 * Compile "@r".
3289 */
3290 static int
3291compile_get_register(char_u **arg, cctx_T *cctx)
3292{
3293 int ret;
3294
3295 ++*arg;
3296 if (**arg == NUL)
3297 {
3298 semsg(_(e_syntax_at), *arg - 1);
3299 return FAIL;
3300 }
3301 if (!valid_yank_reg(**arg, TRUE))
3302 {
3303 emsg_invreg(**arg);
3304 return FAIL;
3305 }
3306 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3307 ++*arg;
3308 return ret;
3309}
3310
3311/*
3312 * Apply leading '!', '-' and '+' to constant "rettv".
3313 */
3314 static int
3315apply_leader(typval_T *rettv, char_u *start, char_u *end)
3316{
3317 char_u *p = end;
3318
3319 // this works from end to start
3320 while (p > start)
3321 {
3322 --p;
3323 if (*p == '-' || *p == '+')
3324 {
3325 // only '-' has an effect, for '+' we only check the type
3326#ifdef FEAT_FLOAT
3327 if (rettv->v_type == VAR_FLOAT)
3328 {
3329 if (*p == '-')
3330 rettv->vval.v_float = -rettv->vval.v_float;
3331 }
3332 else
3333#endif
3334 {
3335 varnumber_T val;
3336 int error = FALSE;
3337
3338 // tv_get_number_chk() accepts a string, but we don't want that
3339 // here
3340 if (check_not_string(rettv) == FAIL)
3341 return FAIL;
3342 val = tv_get_number_chk(rettv, &error);
3343 clear_tv(rettv);
3344 if (error)
3345 return FAIL;
3346 if (*p == '-')
3347 val = -val;
3348 rettv->v_type = VAR_NUMBER;
3349 rettv->vval.v_number = val;
3350 }
3351 }
3352 else
3353 {
3354 int v = tv2bool(rettv);
3355
3356 // '!' is permissive in the type.
3357 clear_tv(rettv);
3358 rettv->v_type = VAR_BOOL;
3359 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3360 }
3361 }
3362 return OK;
3363}
3364
3365/*
3366 * Recognize v: variables that are constants and set "rettv".
3367 */
3368 static void
3369get_vim_constant(char_u **arg, typval_T *rettv)
3370{
3371 if (STRNCMP(*arg, "v:true", 6) == 0)
3372 {
3373 rettv->v_type = VAR_BOOL;
3374 rettv->vval.v_number = VVAL_TRUE;
3375 *arg += 6;
3376 }
3377 else if (STRNCMP(*arg, "v:false", 7) == 0)
3378 {
3379 rettv->v_type = VAR_BOOL;
3380 rettv->vval.v_number = VVAL_FALSE;
3381 *arg += 7;
3382 }
3383 else if (STRNCMP(*arg, "v:null", 6) == 0)
3384 {
3385 rettv->v_type = VAR_SPECIAL;
3386 rettv->vval.v_number = VVAL_NULL;
3387 *arg += 6;
3388 }
3389 else if (STRNCMP(*arg, "v:none", 6) == 0)
3390 {
3391 rettv->v_type = VAR_SPECIAL;
3392 rettv->vval.v_number = VVAL_NONE;
3393 *arg += 6;
3394 }
3395}
3396
Bram Moolenaar61a89812020-05-07 16:58:17 +02003397 static exptype_T
3398get_compare_type(char_u *p, int *len, int *type_is)
3399{
3400 exptype_T type = EXPR_UNKNOWN;
3401 int i;
3402
3403 switch (p[0])
3404 {
3405 case '=': if (p[1] == '=')
3406 type = EXPR_EQUAL;
3407 else if (p[1] == '~')
3408 type = EXPR_MATCH;
3409 break;
3410 case '!': if (p[1] == '=')
3411 type = EXPR_NEQUAL;
3412 else if (p[1] == '~')
3413 type = EXPR_NOMATCH;
3414 break;
3415 case '>': if (p[1] != '=')
3416 {
3417 type = EXPR_GREATER;
3418 *len = 1;
3419 }
3420 else
3421 type = EXPR_GEQUAL;
3422 break;
3423 case '<': if (p[1] != '=')
3424 {
3425 type = EXPR_SMALLER;
3426 *len = 1;
3427 }
3428 else
3429 type = EXPR_SEQUAL;
3430 break;
3431 case 'i': if (p[1] == 's')
3432 {
3433 // "is" and "isnot"; but not a prefix of a name
3434 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3435 *len = 5;
3436 i = p[*len];
3437 if (!isalnum(i) && i != '_')
3438 {
3439 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3440 *type_is = TRUE;
3441 }
3442 }
3443 break;
3444 }
3445 return type;
3446}
3447
3448/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 * Compile code to apply '-', '+' and '!'.
3450 */
3451 static int
3452compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3453{
3454 char_u *p = end;
3455
3456 // this works from end to start
3457 while (p > start)
3458 {
3459 --p;
3460 if (*p == '-' || *p == '+')
3461 {
3462 int negate = *p == '-';
3463 isn_T *isn;
3464
3465 // TODO: check type
3466 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3467 {
3468 --p;
3469 if (*p == '-')
3470 negate = !negate;
3471 }
3472 // only '-' has an effect, for '+' we only check the type
3473 if (negate)
3474 isn = generate_instr(cctx, ISN_NEGATENR);
3475 else
3476 isn = generate_instr(cctx, ISN_CHECKNR);
3477 if (isn == NULL)
3478 return FAIL;
3479 }
3480 else
3481 {
3482 int invert = TRUE;
3483
3484 while (p > start && p[-1] == '!')
3485 {
3486 --p;
3487 invert = !invert;
3488 }
3489 if (generate_2BOOL(cctx, invert) == FAIL)
3490 return FAIL;
3491 }
3492 }
3493 return OK;
3494}
3495
3496/*
3497 * Compile whatever comes after "name" or "name()".
3498 */
3499 static int
3500compile_subscript(
3501 char_u **arg,
3502 cctx_T *cctx,
3503 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003504 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003505 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506{
3507 for (;;)
3508 {
3509 if (**arg == '(')
3510 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003511 garray_T *stack = &cctx->ctx_type_stack;
3512 type_T *type;
3513 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003515 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003516 return FAIL;
3517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003519 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 *arg = skipwhite(*arg + 1);
3522 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3523 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003524 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 return FAIL;
3526 }
3527 else if (**arg == '-' && (*arg)[1] == '>')
3528 {
3529 char_u *p;
3530
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003531 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003532 return FAIL;
3533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 // something->method()
3535 // Apply the '!', '-' and '+' first:
3536 // -1.0->func() works like (-1.0)->func()
3537 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3538 return FAIL;
3539 *start_leader = end_leader; // don't apply again later
3540
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003541 p = *arg + 2;
3542 *arg = skipwhite(p);
3543 if (may_get_next_line(p, arg, cctx) == FAIL)
3544 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 if (**arg == '{')
3546 {
3547 // lambda call: list->{lambda}
3548 if (compile_lambda_call(arg, cctx) == FAIL)
3549 return FAIL;
3550 }
3551 else
3552 {
3553 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003554 p = *arg;
3555 if (ASCII_ISALPHA(*p) && p[1] == ':')
3556 p += 2;
3557 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 ;
3559 if (*p != '(')
3560 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003561 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 return FAIL;
3563 }
3564 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003565 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 return FAIL;
3567 }
3568 }
3569 else if (**arg == '[')
3570 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003571 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003572 type_T **typep;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003573 char_u *p;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003574
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003575 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003576 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003577 // TODO: blob index
3578 // TODO: more arguments
3579 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003580 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003581 return FAIL;
3582
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003583 p = *arg + 1;
3584 *arg = skipwhite(p);
3585 if (may_get_next_line(p, arg, cctx) == FAIL)
3586 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003587 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 return FAIL;
3589
3590 if (**arg != ']')
3591 {
3592 emsg(_(e_missbrac));
3593 return FAIL;
3594 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003595 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003597 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3598 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003599 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003600 if ((*typep)->tt_type == VAR_LIST)
3601 *typep = (*typep)->tt_member;
3602 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3603 return FAIL;
3604 }
3605 else if ((*typep)->tt_type == VAR_DICT)
3606 {
3607 *typep = (*typep)->tt_member;
3608 if (may_generate_2STRING(-1, cctx) == FAIL)
3609 return FAIL;
3610 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3611 return FAIL;
3612 }
3613 else
3614 {
3615 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003616 return FAIL;
3617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 }
3619 else if (**arg == '.' && (*arg)[1] != '.')
3620 {
3621 char_u *p;
3622
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003623 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003624 return FAIL;
3625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 ++*arg;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003627 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3628 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003630 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 if (eval_isnamec1(*p))
3632 while (eval_isnamec(*p))
3633 MB_PTR_ADV(p);
3634 if (p == *arg)
3635 {
3636 semsg(_(e_syntax_at), *arg);
3637 return FAIL;
3638 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003639 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 return FAIL;
3641 *arg = p;
3642 }
3643 else
3644 break;
3645 }
3646
3647 // TODO - see handle_subscript():
3648 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3649 // Don't do this when "Func" is already a partial that was bound
3650 // explicitly (pt_auto is FALSE).
3651
3652 return OK;
3653}
3654
3655/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003656 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3657 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003659 * If the value is a constant "ppconst->pp_ret" will be set.
3660 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003661 *
3662 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 */
3664
3665/*
3666 * number number constant
3667 * 0zFFFFFFFF Blob constant
3668 * "string" string constant
3669 * 'string' literal string constant
3670 * &option-name option value
3671 * @r register contents
3672 * identifier variable value
3673 * function() function call
3674 * $VAR environment variable
3675 * (expression) nested expression
3676 * [expr, expr] List
3677 * {key: val, key: val} Dictionary
3678 * #{key: val, key: val} Dictionary with literal keys
3679 *
3680 * Also handle:
3681 * ! in front logical NOT
3682 * - in front unary minus
3683 * + in front unary plus (ignored)
3684 * trailing (arg) funcref/partial call
3685 * trailing [] subscript in String or List
3686 * trailing .name entry in Dictionary
3687 * trailing ->name() method call
3688 */
3689 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003690compile_expr7(
3691 char_u **arg,
3692 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003693 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 char_u *start_leader, *end_leader;
3696 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003697 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003698 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699
3700 /*
3701 * Skip '!', '-' and '+' characters. They are handled later.
3702 */
3703 start_leader = *arg;
3704 while (**arg == '!' || **arg == '-' || **arg == '+')
3705 *arg = skipwhite(*arg + 1);
3706 end_leader = *arg;
3707
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003708 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 switch (**arg)
3710 {
3711 /*
3712 * Number constant.
3713 */
3714 case '0': // also for blob starting with 0z
3715 case '1':
3716 case '2':
3717 case '3':
3718 case '4':
3719 case '5':
3720 case '6':
3721 case '7':
3722 case '8':
3723 case '9':
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003724 case '.': if (get_number_tv(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 return FAIL;
3726 break;
3727
3728 /*
3729 * String constant: "string".
3730 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003731 case '"': if (get_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 return FAIL;
3733 break;
3734
3735 /*
3736 * Literal string constant: 'str''ing'.
3737 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003738 case '\'': if (get_lit_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 return FAIL;
3740 break;
3741
3742 /*
3743 * Constant Vim variable.
3744 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003745 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 ret = NOTDONE;
3747 break;
3748
3749 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003750 * "true" constant
3751 */
3752 case 't': if (STRNCMP(*arg, "true", 4) == 0
3753 && !eval_isnamec((*arg)[4]))
3754 {
3755 *arg += 4;
3756 rettv->v_type = VAR_BOOL;
3757 rettv->vval.v_number = VVAL_TRUE;
3758 }
3759 else
3760 ret = NOTDONE;
3761 break;
3762
3763 /*
3764 * "false" constant
3765 */
3766 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3767 && !eval_isnamec((*arg)[5]))
3768 {
3769 *arg += 5;
3770 rettv->v_type = VAR_BOOL;
3771 rettv->vval.v_number = VVAL_FALSE;
3772 }
3773 else
3774 ret = NOTDONE;
3775 break;
3776
3777 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 * List: [expr, expr]
3779 */
3780 case '[': ret = compile_list(arg, cctx);
3781 break;
3782
3783 /*
3784 * Dictionary: #{key: val, key: val}
3785 */
3786 case '#': if ((*arg)[1] == '{')
3787 {
3788 ++*arg;
3789 ret = compile_dict(arg, cctx, TRUE);
3790 }
3791 else
3792 ret = NOTDONE;
3793 break;
3794
3795 /*
3796 * Lambda: {arg, arg -> expr}
3797 * Dictionary: {'key': val, 'key': val}
3798 */
3799 case '{': {
3800 char_u *start = skipwhite(*arg + 1);
3801
3802 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003803 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003805 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 if (ret != FAIL && *start == '>')
3807 ret = compile_lambda(arg, cctx);
3808 else
3809 ret = compile_dict(arg, cctx, FALSE);
3810 }
3811 break;
3812
3813 /*
3814 * Option value: &name
3815 */
3816 case '&': ret = compile_get_option(arg, cctx);
3817 break;
3818
3819 /*
3820 * Environment variable: $VAR.
3821 */
3822 case '$': ret = compile_get_env(arg, cctx);
3823 break;
3824
3825 /*
3826 * Register contents: @r.
3827 */
3828 case '@': ret = compile_get_register(arg, cctx);
3829 break;
3830 /*
3831 * nested expression: (expression).
3832 */
3833 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003834
3835 // recursive!
3836 if (ppconst->pp_used <= PPSIZE - 10)
3837 {
3838 ret = compile_expr1(arg, cctx, ppconst);
3839 }
3840 else
3841 {
3842 // Not enough space in ppconst, flush constants.
3843 if (generate_ppconst(cctx, ppconst) == FAIL)
3844 return FAIL;
3845 ret = compile_expr0(arg, cctx);
3846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 *arg = skipwhite(*arg);
3848 if (**arg == ')')
3849 ++*arg;
3850 else if (ret == OK)
3851 {
3852 emsg(_(e_missing_close));
3853 ret = FAIL;
3854 }
3855 break;
3856
3857 default: ret = NOTDONE;
3858 break;
3859 }
3860 if (ret == FAIL)
3861 return FAIL;
3862
Bram Moolenaar1c747212020-05-09 18:28:34 +02003863 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 {
3865 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003866 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003868 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 return FAIL;
3870 }
3871 start_leader = end_leader; // don't apply again below
3872
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003873 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003874 clear_tv(rettv);
3875 else
3876 // A constant expression can possibly be handled compile time,
3877 // return the value instead of generating code.
3878 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 }
3880 else if (ret == NOTDONE)
3881 {
3882 char_u *p;
3883 int r;
3884
3885 if (!eval_isnamec1(**arg))
3886 {
3887 semsg(_("E1015: Name expected: %s"), *arg);
3888 return FAIL;
3889 }
3890
3891 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003892 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003894 {
3895 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003898 {
3899 if (generate_ppconst(cctx, ppconst) == FAIL)
3900 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003902 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 if (r == FAIL)
3904 return FAIL;
3905 }
3906
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003907 // Handle following "[]", ".member", etc.
3908 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003909 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003910 ppconst) == FAIL)
3911 return FAIL;
3912 if (ppconst->pp_used > 0)
3913 {
3914 // apply the '!', '-' and '+' before the constant
3915 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3916 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3917 return FAIL;
3918 return OK;
3919 }
3920 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003922 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923}
3924
3925/*
3926 * * number multiplication
3927 * / number division
3928 * % number modulo
3929 */
3930 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003931compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932{
3933 char_u *op;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003934 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003936 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003937 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 return FAIL;
3939
3940 /*
3941 * Repeat computing, until no "*", "/" or "%" is following.
3942 */
3943 for (;;)
3944 {
3945 op = skipwhite(*arg);
3946 if (*op != '*' && *op != '/' && *op != '%')
3947 break;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003948
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003949 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 {
3951 char_u buf[3];
3952
3953 vim_strncpy(buf, op, 1);
3954 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003955 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 }
3957 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003958 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003959 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003961 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003962 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003964
3965 if (ppconst->pp_used == ppconst_used + 2
3966 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3967 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003968 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003969 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3970 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003971 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003973 // both are numbers: compute the result
3974 switch (*op)
3975 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003976 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003977 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003978 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003979 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003980 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003981 break;
3982 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003983 tv1->vval.v_number = res;
3984 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003985 }
3986 else
3987 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003988 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003989 generate_two_op(cctx, op);
3990 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 }
3992
3993 return OK;
3994}
3995
3996/*
3997 * + number addition
3998 * - number subtraction
3999 * .. string concatenation
4000 */
4001 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004002compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003{
4004 char_u *op;
4005 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004006 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007
4008 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004009 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 return FAIL;
4011
4012 /*
4013 * Repeat computing, until no "+", "-" or ".." is following.
4014 */
4015 for (;;)
4016 {
4017 op = skipwhite(*arg);
4018 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
4019 break;
4020 oplen = (*op == '.' ? 2 : 1);
4021
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004022 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 {
4024 char_u buf[3];
4025
4026 vim_strncpy(buf, op, oplen);
4027 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004028 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 }
4030
4031 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004032 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004033 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004035 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004036 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 return FAIL;
4038
Bram Moolenaara5565e42020-05-09 15:44:01 +02004039 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004040 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004041 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4042 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4043 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4044 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004046 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4047 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004048
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004049 // concat/subtract/add constant numbers
4050 if (*op == '+')
4051 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4052 else if (*op == '-')
4053 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4054 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004055 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004056 // concatenate constant strings
4057 char_u *s1 = tv1->vval.v_string;
4058 char_u *s2 = tv2->vval.v_string;
4059 size_t len1 = STRLEN(s1);
4060
4061 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4062 if (tv1->vval.v_string == NULL)
4063 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004064 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004065 return FAIL;
4066 }
4067 mch_memmove(tv1->vval.v_string, s1, len1);
4068 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004069 vim_free(s1);
4070 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004071 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004072 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 }
4074 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004075 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004076 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004077 if (*op == '.')
4078 {
4079 if (may_generate_2STRING(-2, cctx) == FAIL
4080 || may_generate_2STRING(-1, cctx) == FAIL)
4081 return FAIL;
4082 generate_instr_drop(cctx, ISN_CONCAT, 1);
4083 }
4084 else
4085 generate_two_op(cctx, op);
4086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 }
4088
4089 return OK;
4090}
4091
4092/*
4093 * expr5a == expr5b
4094 * expr5a =~ expr5b
4095 * expr5a != expr5b
4096 * expr5a !~ expr5b
4097 * expr5a > expr5b
4098 * expr5a >= expr5b
4099 * expr5a < expr5b
4100 * expr5a <= expr5b
4101 * expr5a is expr5b
4102 * expr5a isnot expr5b
4103 *
4104 * Produces instructions:
4105 * EVAL expr5a Push result of "expr5a"
4106 * EVAL expr5b Push result of "expr5b"
4107 * COMPARE one of the compare instructions
4108 */
4109 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004110compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111{
4112 exptype_T type = EXPR_UNKNOWN;
4113 char_u *p;
4114 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004116 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117
4118 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004119 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 return FAIL;
4121
4122 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004123 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124
4125 /*
4126 * If there is a comparative operator, use it.
4127 */
4128 if (type != EXPR_UNKNOWN)
4129 {
4130 int ic = FALSE; // Default: do not ignore case
4131
4132 if (type_is && (p[len] == '?' || p[len] == '#'))
4133 {
4134 semsg(_(e_invexpr2), *arg);
4135 return FAIL;
4136 }
4137 // extra question mark appended: ignore case
4138 if (p[len] == '?')
4139 {
4140 ic = TRUE;
4141 ++len;
4142 }
4143 // extra '#' appended: match case (ignored)
4144 else if (p[len] == '#')
4145 ++len;
4146 // nothing appended: match case
4147
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004148 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 {
4150 char_u buf[7];
4151
4152 vim_strncpy(buf, p, len);
4153 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004154 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 }
4156
4157 // get the second variable
4158 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004159 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004160 return FAIL;
4161
Bram Moolenaara5565e42020-05-09 15:44:01 +02004162 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 return FAIL;
4164
Bram Moolenaara5565e42020-05-09 15:44:01 +02004165 if (ppconst->pp_used == ppconst_used + 2)
4166 {
4167 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4168 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4169 int ret;
4170
4171 // Both sides are a constant, compute the result now.
4172 // First check for a valid combination of types, this is more
4173 // strict than typval_compare().
4174 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4175 ret = FAIL;
4176 else
4177 {
4178 ret = typval_compare(tv1, tv2, type, ic);
4179 tv1->v_type = VAR_BOOL;
4180 tv1->vval.v_number = tv1->vval.v_number
4181 ? VVAL_TRUE : VVAL_FALSE;
4182 clear_tv(tv2);
4183 --ppconst->pp_used;
4184 }
4185 return ret;
4186 }
4187
4188 generate_ppconst(cctx, ppconst);
4189 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 }
4191
4192 return OK;
4193}
4194
Bram Moolenaar7f141552020-05-09 17:35:53 +02004195static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197/*
4198 * Compile || or &&.
4199 */
4200 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004201compile_and_or(
4202 char_u **arg,
4203 cctx_T *cctx,
4204 char *op,
4205 ppconst_T *ppconst,
4206 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207{
4208 char_u *p = skipwhite(*arg);
4209 int opchar = *op;
4210
4211 if (p[0] == opchar && p[1] == opchar)
4212 {
4213 garray_T *instr = &cctx->ctx_instr;
4214 garray_T end_ga;
4215
4216 /*
4217 * Repeat until there is no following "||" or "&&"
4218 */
4219 ga_init2(&end_ga, sizeof(int), 10);
4220 while (p[0] == opchar && p[1] == opchar)
4221 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004222 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4223 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004225 return FAIL;
4226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227
Bram Moolenaara5565e42020-05-09 15:44:01 +02004228 // TODO: use ppconst if the value is a constant
4229 generate_ppconst(cctx, ppconst);
4230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 if (ga_grow(&end_ga, 1) == FAIL)
4232 {
4233 ga_clear(&end_ga);
4234 return FAIL;
4235 }
4236 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4237 ++end_ga.ga_len;
4238 generate_JUMP(cctx, opchar == '|'
4239 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4240
4241 // eval the next expression
4242 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004243 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004244 return FAIL;
4245
Bram Moolenaara5565e42020-05-09 15:44:01 +02004246 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4247 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 {
4249 ga_clear(&end_ga);
4250 return FAIL;
4251 }
4252 p = skipwhite(*arg);
4253 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004254 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255
4256 // Fill in the end label in all jumps.
4257 while (end_ga.ga_len > 0)
4258 {
4259 isn_T *isn;
4260
4261 --end_ga.ga_len;
4262 isn = ((isn_T *)instr->ga_data)
4263 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4264 isn->isn_arg.jump.jump_where = instr->ga_len;
4265 }
4266 ga_clear(&end_ga);
4267 }
4268
4269 return OK;
4270}
4271
4272/*
4273 * expr4a && expr4a && expr4a logical AND
4274 *
4275 * Produces instructions:
4276 * EVAL expr4a Push result of "expr4a"
4277 * JUMP_AND_KEEP_IF_FALSE end
4278 * EVAL expr4b Push result of "expr4b"
4279 * JUMP_AND_KEEP_IF_FALSE end
4280 * EVAL expr4c Push result of "expr4c"
4281 * end:
4282 */
4283 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004284compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004286 int ppconst_used = ppconst->pp_used;
4287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004289 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 return FAIL;
4291
4292 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004293 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294}
4295
4296/*
4297 * expr3a || expr3b || expr3c logical OR
4298 *
4299 * Produces instructions:
4300 * EVAL expr3a Push result of "expr3a"
4301 * JUMP_AND_KEEP_IF_TRUE end
4302 * EVAL expr3b Push result of "expr3b"
4303 * JUMP_AND_KEEP_IF_TRUE end
4304 * EVAL expr3c Push result of "expr3c"
4305 * end:
4306 */
4307 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004308compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004310 int ppconst_used = ppconst->pp_used;
4311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004313 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 return FAIL;
4315
4316 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004317 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318}
4319
4320/*
4321 * Toplevel expression: expr2 ? expr1a : expr1b
4322 *
4323 * Produces instructions:
4324 * EVAL expr2 Push result of "expr"
4325 * JUMP_IF_FALSE alt jump if false
4326 * EVAL expr1a
4327 * JUMP_ALWAYS end
4328 * alt: EVAL expr1b
4329 * end:
4330 */
4331 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004332compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333{
4334 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004335 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336
Bram Moolenaar61a89812020-05-07 16:58:17 +02004337 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004338 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 return FAIL;
4340
4341 p = skipwhite(*arg);
4342 if (*p == '?')
4343 {
4344 garray_T *instr = &cctx->ctx_instr;
4345 garray_T *stack = &cctx->ctx_type_stack;
4346 int alt_idx = instr->ga_len;
4347 int end_idx;
4348 isn_T *isn;
4349 type_T *type1;
4350 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004351 int has_const_expr = FALSE;
4352 int const_value = FALSE;
4353 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004355 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4356 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004358 return FAIL;
4359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360
Bram Moolenaara5565e42020-05-09 15:44:01 +02004361 if (ppconst->pp_used == ppconst_used + 1)
4362 {
4363 // the condition is a constant, we know whether the ? or the :
4364 // expression is to be evaluated.
4365 has_const_expr = TRUE;
4366 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4367 clear_tv(&ppconst->pp_tv[ppconst_used]);
4368 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004369 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4370 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004371 }
4372 else
4373 {
4374 generate_ppconst(cctx, ppconst);
4375 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377
4378 // evaluate the second expression; any type is accepted
4379 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004380 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004381 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004382 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004383 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384
Bram Moolenaara5565e42020-05-09 15:44:01 +02004385 if (!has_const_expr)
4386 {
4387 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388
Bram Moolenaara5565e42020-05-09 15:44:01 +02004389 // remember the type and drop it
4390 --stack->ga_len;
4391 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392
Bram Moolenaara5565e42020-05-09 15:44:01 +02004393 end_idx = instr->ga_len;
4394 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4395
4396 // jump here from JUMP_IF_FALSE
4397 isn = ((isn_T *)instr->ga_data) + alt_idx;
4398 isn->isn_arg.jump.jump_where = instr->ga_len;
4399 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400
4401 // Check for the ":".
4402 p = skipwhite(*arg);
4403 if (*p != ':')
4404 {
4405 emsg(_(e_missing_colon));
4406 return FAIL;
4407 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004408 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4409 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004411 return FAIL;
4412 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413
4414 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004415 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004416 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4417 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004419 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004420 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004421 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004422 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423
Bram Moolenaara5565e42020-05-09 15:44:01 +02004424 if (!has_const_expr)
4425 {
4426 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427
Bram Moolenaara5565e42020-05-09 15:44:01 +02004428 // If the types differ, the result has a more generic type.
4429 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4430 common_type(type1, type2, &type2, cctx->ctx_type_list);
4431
4432 // jump here from JUMP_ALWAYS
4433 isn = ((isn_T *)instr->ga_data) + end_idx;
4434 isn->isn_arg.jump.jump_where = instr->ga_len;
4435 }
4436
4437 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 }
4439 return OK;
4440}
4441
4442/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004443 * Toplevel expression.
4444 */
4445 static int
4446compile_expr0(char_u **arg, cctx_T *cctx)
4447{
4448 ppconst_T ppconst;
4449
4450 CLEAR_FIELD(ppconst);
4451 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4452 {
4453 clear_ppconst(&ppconst);
4454 return FAIL;
4455 }
4456 if (generate_ppconst(cctx, &ppconst) == FAIL)
4457 return FAIL;
4458 return OK;
4459}
4460
4461/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 * compile "return [expr]"
4463 */
4464 static char_u *
4465compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4466{
4467 char_u *p = arg;
4468 garray_T *stack = &cctx->ctx_type_stack;
4469 type_T *stack_type;
4470
4471 if (*p != NUL && *p != '|' && *p != '\n')
4472 {
4473 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004474 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 return NULL;
4476
4477 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4478 if (set_return_type)
4479 cctx->ctx_ufunc->uf_ret_type = stack_type;
4480 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4481 == FAIL)
4482 return NULL;
4483 }
4484 else
4485 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004486 // "set_return_type" cannot be TRUE, only used for a lambda which
4487 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004488 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4489 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 {
4491 emsg(_("E1003: Missing return value"));
4492 return NULL;
4493 }
4494
4495 // No argument, return zero.
4496 generate_PUSHNR(cctx, 0);
4497 }
4498
4499 if (generate_instr(cctx, ISN_RETURN) == NULL)
4500 return NULL;
4501
4502 // "return val | endif" is possible
4503 return skipwhite(p);
4504}
4505
4506/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004507 * Get a line from the compilation context, compatible with exarg_T getline().
4508 * Return a pointer to the line in allocated memory.
4509 * Return NULL for end-of-file or some error.
4510 */
4511 static char_u *
4512exarg_getline(
4513 int c UNUSED,
4514 void *cookie,
4515 int indent UNUSED,
4516 int do_concat UNUSED)
4517{
4518 cctx_T *cctx = (cctx_T *)cookie;
4519
4520 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4521 {
4522 iemsg("Heredoc got to end");
4523 return NULL;
4524 }
4525 ++cctx->ctx_lnum;
4526 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4527 [cctx->ctx_lnum]);
4528}
4529
4530/*
4531 * Compile a nested :def command.
4532 */
4533 static char_u *
4534compile_nested_function(exarg_T *eap, cctx_T *cctx)
4535{
4536 char_u *name_start = eap->arg;
4537 char_u *name_end = to_name_end(eap->arg, FALSE);
4538 char_u *name = get_lambda_name();
4539 lvar_T *lvar;
4540 ufunc_T *ufunc;
4541
4542 eap->arg = name_end;
4543 eap->getline = exarg_getline;
4544 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004545 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004546 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004547 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004548
Bram Moolenaar822ba242020-05-24 23:00:18 +02004549 if (ufunc == NULL)
4550 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004551 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004552 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004553 return NULL;
4554
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004555 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004556 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004557 TRUE, ufunc->uf_func_type);
4558
4559 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4560 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4561 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004562
Bram Moolenaar61a89812020-05-07 16:58:17 +02004563 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004564 return (char_u *)"";
4565}
4566
4567/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 * Return the length of an assignment operator, or zero if there isn't one.
4569 */
4570 int
4571assignment_len(char_u *p, int *heredoc)
4572{
4573 if (*p == '=')
4574 {
4575 if (p[1] == '<' && p[2] == '<')
4576 {
4577 *heredoc = TRUE;
4578 return 3;
4579 }
4580 return 1;
4581 }
4582 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4583 return 2;
4584 if (STRNCMP(p, "..=", 3) == 0)
4585 return 3;
4586 return 0;
4587}
4588
4589// words that cannot be used as a variable
4590static char *reserved[] = {
4591 "true",
4592 "false",
4593 NULL
4594};
4595
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004596typedef enum {
4597 dest_local,
4598 dest_option,
4599 dest_env,
4600 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004601 dest_buffer,
4602 dest_window,
4603 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004604 dest_vimvar,
4605 dest_script,
4606 dest_reg,
4607} assign_dest_T;
4608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004610 * Generate the load instruction for "name".
4611 */
4612 static void
4613generate_loadvar(
4614 cctx_T *cctx,
4615 assign_dest_T dest,
4616 char_u *name,
4617 lvar_T *lvar,
4618 type_T *type)
4619{
4620 switch (dest)
4621 {
4622 case dest_option:
4623 // TODO: check the option exists
4624 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4625 break;
4626 case dest_global:
4627 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4628 break;
4629 case dest_buffer:
4630 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4631 break;
4632 case dest_window:
4633 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4634 break;
4635 case dest_tab:
4636 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4637 break;
4638 case dest_script:
4639 compile_load_scriptvar(cctx,
4640 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4641 break;
4642 case dest_env:
4643 // Include $ in the name here
4644 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4645 break;
4646 case dest_reg:
4647 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4648 break;
4649 case dest_vimvar:
4650 generate_LOADV(cctx, name + 2, TRUE);
4651 break;
4652 case dest_local:
4653 if (lvar->lv_from_outer)
4654 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4655 NULL, type);
4656 else
4657 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4658 break;
4659 }
4660}
4661
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004662 void
4663vim9_declare_error(char_u *name)
4664{
4665 char *scope = "";
4666
4667 switch (*name)
4668 {
4669 case 'g': scope = " global"; break;
4670 case 'b': scope = " buffer"; break;
4671 case 'w': scope = " window"; break;
4672 case 't': scope = " tab"; break;
4673 case 'v': scope = " v:"; break;
4674 case '$': scope = "n environment"; break;
4675 }
4676 semsg(_(e_declare_var), scope, name);
4677}
4678
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004679/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004680 * Compile declaration and assignment:
4681 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004683 * Return NULL for an error.
4684 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 */
4686 static char_u *
4687compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4688{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004689 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004691 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 char_u *ret = NULL;
4693 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004694 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004697 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 int oplen = 0;
4700 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004701 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004702 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004703 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004707 // Skip over the "var" or "[var, var]" to get to any "=".
4708 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4709 if (p == NULL)
4710 return *arg == '[' ? arg : NULL;
4711
4712 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004714 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 return NULL;
4716 }
4717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 sp = p;
4719 p = skipwhite(p);
4720 op = p;
4721 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004722
4723 if (var_count > 0 && oplen == 0)
4724 // can be something like "[1, 2]->func()"
4725 return arg;
4726
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4728 {
4729 char_u buf[4];
4730
4731 vim_strncpy(buf, op, oplen);
4732 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004733 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004734 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 if (heredoc)
4737 {
4738 list_T *l;
4739 listitem_T *li;
4740
4741 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004742 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004744 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745
4746 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004747 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 {
4749 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4750 li->li_tv.vval.v_string = NULL;
4751 }
4752 generate_NEWLIST(cctx, l->lv_len);
4753 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004754 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 list_free(l);
4756 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004757 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004759 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004761 // for "[var, var] = expr" evaluate the expression here, loop over the
4762 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004763
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004764 p = skipwhite(op + oplen);
4765 if (compile_expr0(&p, cctx) == FAIL)
4766 return NULL;
4767 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004769 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004771 type_T *stacktype;
4772
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004773 stacktype = stack->ga_len == 0 ? &t_void
4774 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004775 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004777 emsg(_(e_cannot_use_void));
4778 goto theend;
4779 }
4780 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4781 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004782 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4783 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004784 }
4785 }
4786
4787 /*
4788 * Loop over variables in "[var, var] = expr".
4789 * For "var = expr" and "let var: type" this is done only once.
4790 */
4791 if (var_count > 0)
4792 var_start = skipwhite(arg + 1); // skip over the "["
4793 else
4794 var_start = arg;
4795 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4796 {
4797 char_u *var_end = skip_var_one(var_start, FALSE);
4798 size_t varlen;
4799 int new_local = FALSE;
4800 int opt_type;
4801 int opt_flags = 0;
4802 assign_dest_T dest = dest_local;
4803 int vimvaridx = -1;
4804 lvar_T *lvar = NULL;
4805 lvar_T arg_lvar;
4806 int has_type = FALSE;
4807 int has_index = FALSE;
4808 int instr_count = -1;
4809
4810 p = (*var_start == '&' || *var_start == '$'
4811 || *var_start == '@') ? var_start + 1 : var_start;
4812 p = to_name_end(p, TRUE);
4813
4814 // "a: type" is declaring variable "a" with a type, not "a:".
4815 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4816 --var_end;
4817 if (is_decl && p == var_start + 2 && p[-1] == ':')
4818 --p;
4819
4820 varlen = p - var_start;
4821 vim_free(name);
4822 name = vim_strnsave(var_start, varlen);
4823 if (name == NULL)
4824 return NULL;
4825 if (!heredoc)
4826 type = &t_any;
4827
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004828 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004829 {
4830 if (*var_start == '&')
4831 {
4832 int cc;
4833 long numval;
4834
4835 dest = dest_option;
4836 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004838 emsg(_(e_const_option));
4839 goto theend;
4840 }
4841 if (is_decl)
4842 {
4843 semsg(_("E1052: Cannot declare an option: %s"), var_start);
4844 goto theend;
4845 }
4846 p = var_start;
4847 p = find_option_end(&p, &opt_flags);
4848 if (p == NULL)
4849 {
4850 // cannot happen?
4851 emsg(_(e_letunexp));
4852 goto theend;
4853 }
4854 cc = *p;
4855 *p = NUL;
4856 opt_type = get_option_value(var_start + 1, &numval,
4857 NULL, opt_flags);
4858 *p = cc;
4859 if (opt_type == -3)
4860 {
4861 semsg(_(e_unknown_option), var_start);
4862 goto theend;
4863 }
4864 if (opt_type == -2 || opt_type == 0)
4865 type = &t_string;
4866 else
4867 type = &t_number; // both number and boolean option
4868 }
4869 else if (*var_start == '$')
4870 {
4871 dest = dest_env;
4872 type = &t_string;
4873 if (is_decl)
4874 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004875 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004876 goto theend;
4877 }
4878 }
4879 else if (*var_start == '@')
4880 {
4881 if (!valid_yank_reg(var_start[1], TRUE))
4882 {
4883 emsg_invreg(var_start[1]);
4884 goto theend;
4885 }
4886 dest = dest_reg;
4887 type = &t_string;
4888 if (is_decl)
4889 {
4890 semsg(_("E1066: Cannot declare a register: %s"), name);
4891 goto theend;
4892 }
4893 }
4894 else if (STRNCMP(var_start, "g:", 2) == 0)
4895 {
4896 dest = dest_global;
4897 if (is_decl)
4898 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004899 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004900 goto theend;
4901 }
4902 }
4903 else if (STRNCMP(var_start, "b:", 2) == 0)
4904 {
4905 dest = dest_buffer;
4906 if (is_decl)
4907 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004908 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004909 goto theend;
4910 }
4911 }
4912 else if (STRNCMP(var_start, "w:", 2) == 0)
4913 {
4914 dest = dest_window;
4915 if (is_decl)
4916 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004917 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004918 goto theend;
4919 }
4920 }
4921 else if (STRNCMP(var_start, "t:", 2) == 0)
4922 {
4923 dest = dest_tab;
4924 if (is_decl)
4925 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004926 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004927 goto theend;
4928 }
4929 }
4930 else if (STRNCMP(var_start, "v:", 2) == 0)
4931 {
4932 typval_T *vtv;
4933 int di_flags;
4934
4935 vimvaridx = find_vim_var(name + 2, &di_flags);
4936 if (vimvaridx < 0)
4937 {
4938 semsg(_(e_var_notfound), var_start);
4939 goto theend;
4940 }
4941 // We use the current value of "sandbox" here, is that OK?
4942 if (var_check_ro(di_flags, name, FALSE))
4943 goto theend;
4944 dest = dest_vimvar;
4945 vtv = get_vim_var_tv(vimvaridx);
4946 type = typval2type(vtv);
4947 if (is_decl)
4948 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004949 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004950 goto theend;
4951 }
4952 }
4953 else
4954 {
4955 int idx;
4956
4957 for (idx = 0; reserved[idx] != NULL; ++idx)
4958 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004959 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004960 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004961 goto theend;
4962 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004963
4964 lvar = lookup_local(var_start, varlen, cctx);
4965 if (lvar == NULL)
4966 {
4967 CLEAR_FIELD(arg_lvar);
4968 if (lookup_arg(var_start, varlen,
4969 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4970 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004971 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004972 if (is_decl)
4973 {
4974 semsg(_(e_used_as_arg), name);
4975 goto theend;
4976 }
4977 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004978 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004979 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004980 if (lvar != NULL)
4981 {
4982 if (is_decl)
4983 {
4984 semsg(_("E1017: Variable already declared: %s"), name);
4985 goto theend;
4986 }
4987 else if (lvar->lv_const)
4988 {
4989 semsg(_("E1018: Cannot assign to a constant: %s"),
4990 name);
4991 goto theend;
4992 }
4993 }
4994 else if (STRNCMP(var_start, "s:", 2) == 0
4995 || lookup_script(var_start, varlen) == OK
4996 || find_imported(var_start, varlen, cctx) != NULL)
4997 {
4998 dest = dest_script;
4999 if (is_decl)
5000 {
5001 semsg(_("E1054: Variable already declared in the script: %s"),
5002 name);
5003 goto theend;
5004 }
5005 }
5006 else if (name[1] == ':' && name[2] != NUL)
5007 {
5008 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5009 name);
5010 goto theend;
5011 }
5012 else if (!is_decl)
5013 {
5014 semsg(_("E1089: unknown variable: %s"), name);
5015 goto theend;
5016 }
5017 }
5018 }
5019
5020 // handle "a:name" as a name, not index "name" on "a"
5021 if (varlen > 1 || var_start[varlen] != ':')
5022 p = var_end;
5023
5024 if (dest != dest_option)
5025 {
5026 if (is_decl && *p == ':')
5027 {
5028 // parse optional type: "let var: type = expr"
5029 if (!VIM_ISWHITE(p[1]))
5030 {
5031 semsg(_(e_white_after), ":");
5032 goto theend;
5033 }
5034 p = skipwhite(p + 1);
5035 type = parse_type(&p, cctx->ctx_type_list);
5036 has_type = TRUE;
5037 }
5038 else if (lvar != NULL)
5039 type = lvar->lv_type;
5040 }
5041
5042 if (oplen == 3 && !heredoc && dest != dest_global
5043 && type->tt_type != VAR_STRING
5044 && type->tt_type != VAR_ANY)
5045 {
5046 emsg(_("E1019: Can only concatenate to string"));
5047 goto theend;
5048 }
5049
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005050 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005051 {
5052 if (oplen > 1 && !heredoc)
5053 {
5054 // +=, /=, etc. require an existing variable
5055 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5056 name);
5057 goto theend;
5058 }
5059
5060 // new local variable
5061 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5062 goto theend;
5063 lvar = reserve_local(cctx, var_start, varlen,
5064 cmdidx == CMD_const, type);
5065 if (lvar == NULL)
5066 goto theend;
5067 new_local = TRUE;
5068 }
5069
5070 member_type = type;
5071 if (var_end > var_start + varlen)
5072 {
5073 // Something follows after the variable: "var[idx]".
5074 if (is_decl)
5075 {
5076 emsg(_("E1087: cannot use an index when declaring a variable"));
5077 goto theend;
5078 }
5079
5080 if (var_start[varlen] == '[')
5081 {
5082 has_index = TRUE;
5083 if (type->tt_member == NULL)
5084 {
5085 semsg(_("E1088: cannot use an index on %s"), name);
5086 goto theend;
5087 }
5088 member_type = type->tt_member;
5089 }
5090 else
5091 {
5092 semsg("Not supported yet: %s", var_start);
5093 goto theend;
5094 }
5095 }
5096 else if (lvar == &arg_lvar)
5097 {
5098 semsg(_("E1090: Cannot assign to argument %s"), name);
5099 goto theend;
5100 }
5101
5102 if (!heredoc)
5103 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005104 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005105 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005106 if (oplen > 0 && var_count == 0)
5107 {
5108 // skip over the "=" and the expression
5109 p = skipwhite(op + oplen);
5110 compile_expr0(&p, cctx);
5111 }
5112 }
5113 else if (oplen > 0)
5114 {
5115 type_T *stacktype;
5116
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005117 // For "var = expr" evaluate the expression.
5118 if (var_count == 0)
5119 {
5120 int r;
5121
5122 // for "+=", "*=", "..=" etc. first load the current value
5123 if (*op != '=')
5124 {
5125 generate_loadvar(cctx, dest, name, lvar, type);
5126
5127 if (has_index)
5128 {
5129 // TODO: get member from list or dict
5130 emsg("Index with operation not supported yet");
5131 goto theend;
5132 }
5133 }
5134
5135 // Compile the expression. Temporarily hide the new local
5136 // variable here, it is not available to this expression.
5137 if (new_local)
5138 --cctx->ctx_locals.ga_len;
5139 instr_count = instr->ga_len;
5140 p = skipwhite(op + oplen);
5141 r = compile_expr0(&p, cctx);
5142 if (new_local)
5143 ++cctx->ctx_locals.ga_len;
5144 if (r == FAIL)
5145 goto theend;
5146 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005147 else if (semicolon && var_idx == var_count - 1)
5148 {
5149 // For "[var; var] = expr" get the rest of the list
5150 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5151 goto theend;
5152 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005153 else
5154 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005155 // For "[var, var] = expr" get the "var_idx" item from the
5156 // list.
5157 if (generate_GETITEM(cctx, var_idx) == FAIL)
5158 return FAIL;
5159 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005160
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005161 stacktype = stack->ga_len == 0 ? &t_void
5162 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5163 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005164 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005165 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005166 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005167 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005168 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005169 emsg(_(e_cannot_use_void));
5170 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005171 }
5172 else
5173 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005174 // An empty list or dict has a &t_void member,
5175 // for a variable that implies &t_any.
5176 if (stacktype == &t_list_empty)
5177 lvar->lv_type = &t_list_any;
5178 else if (stacktype == &t_dict_empty)
5179 lvar->lv_type = &t_dict_any;
5180 else
5181 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005182 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005183 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005184 else
5185 {
5186 type_T *use_type = lvar->lv_type;
5187
5188 if (has_index)
5189 {
5190 use_type = use_type->tt_member;
5191 if (use_type == NULL)
5192 use_type = &t_void;
5193 }
5194 if (need_type(stacktype, use_type, -1, cctx)
5195 == FAIL)
5196 goto theend;
5197 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005198 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005199 else if (*p != '=' && need_type(stacktype, member_type, -1,
5200 cctx) == FAIL)
5201 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005203 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005205 emsg(_(e_const_req_value));
5206 goto theend;
5207 }
5208 else if (!has_type || dest == dest_option)
5209 {
5210 emsg(_(e_type_req));
5211 goto theend;
5212 }
5213 else
5214 {
5215 // variables are always initialized
5216 if (ga_grow(instr, 1) == FAIL)
5217 goto theend;
5218 switch (member_type->tt_type)
5219 {
5220 case VAR_BOOL:
5221 generate_PUSHBOOL(cctx, VVAL_FALSE);
5222 break;
5223 case VAR_FLOAT:
5224#ifdef FEAT_FLOAT
5225 generate_PUSHF(cctx, 0.0);
5226#endif
5227 break;
5228 case VAR_STRING:
5229 generate_PUSHS(cctx, NULL);
5230 break;
5231 case VAR_BLOB:
5232 generate_PUSHBLOB(cctx, NULL);
5233 break;
5234 case VAR_FUNC:
5235 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5236 break;
5237 case VAR_LIST:
5238 generate_NEWLIST(cctx, 0);
5239 break;
5240 case VAR_DICT:
5241 generate_NEWDICT(cctx, 0);
5242 break;
5243 case VAR_JOB:
5244 generate_PUSHJOB(cctx, NULL);
5245 break;
5246 case VAR_CHANNEL:
5247 generate_PUSHCHANNEL(cctx, NULL);
5248 break;
5249 case VAR_NUMBER:
5250 case VAR_UNKNOWN:
5251 case VAR_ANY:
5252 case VAR_PARTIAL:
5253 case VAR_VOID:
5254 case VAR_SPECIAL: // cannot happen
5255 generate_PUSHNR(cctx, 0);
5256 break;
5257 }
5258 }
5259 if (var_count == 0)
5260 end = p;
5261 }
5262
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005263 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005264 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005265 break;
5266
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005267 if (oplen > 0 && *op != '=')
5268 {
5269 type_T *expected = &t_number;
5270 type_T *stacktype;
5271
5272 // TODO: if type is known use float or any operation
5273
5274 if (*op == '.')
5275 expected = &t_string;
5276 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5277 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5278 goto theend;
5279
5280 if (*op == '.')
5281 generate_instr_drop(cctx, ISN_CONCAT, 1);
5282 else
5283 {
5284 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5285
5286 if (isn == NULL)
5287 goto theend;
5288 switch (*op)
5289 {
5290 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5291 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5292 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5293 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5294 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296 }
5297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005298
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005299 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005300 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005301 int r;
5302
5303 // Compile the "idx" in "var[idx]".
5304 if (new_local)
5305 --cctx->ctx_locals.ga_len;
5306 p = skipwhite(var_start + varlen + 1);
5307 r = compile_expr0(&p, cctx);
5308 if (new_local)
5309 ++cctx->ctx_locals.ga_len;
5310 if (r == FAIL)
5311 goto theend;
5312 if (*skipwhite(p) != ']')
5313 {
5314 emsg(_(e_missbrac));
5315 goto theend;
5316 }
5317 if (type->tt_type == VAR_DICT
5318 && may_generate_2STRING(-1, cctx) == FAIL)
5319 goto theend;
5320 if (type->tt_type == VAR_LIST
5321 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005322 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005323 {
5324 emsg(_(e_number_exp));
5325 goto theend;
5326 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005327
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005328 // Load the dict or list. On the stack we then have:
5329 // - value
5330 // - index
5331 // - variable
5332 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005333
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005334 if (type->tt_type == VAR_LIST)
5335 {
5336 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5337 return FAIL;
5338 }
5339 else if (type->tt_type == VAR_DICT)
5340 {
5341 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5342 return FAIL;
5343 }
5344 else
5345 {
5346 emsg(_(e_listreq));
5347 goto theend;
5348 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005349 }
5350 else
5351 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005352 switch (dest)
5353 {
5354 case dest_option:
5355 generate_STOREOPT(cctx, name + 1, opt_flags);
5356 break;
5357 case dest_global:
5358 // include g: with the name, easier to execute that way
5359 generate_STORE(cctx, ISN_STOREG, 0, name);
5360 break;
5361 case dest_buffer:
5362 // include b: with the name, easier to execute that way
5363 generate_STORE(cctx, ISN_STOREB, 0, name);
5364 break;
5365 case dest_window:
5366 // include w: with the name, easier to execute that way
5367 generate_STORE(cctx, ISN_STOREW, 0, name);
5368 break;
5369 case dest_tab:
5370 // include t: with the name, easier to execute that way
5371 generate_STORE(cctx, ISN_STORET, 0, name);
5372 break;
5373 case dest_env:
5374 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5375 break;
5376 case dest_reg:
5377 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5378 break;
5379 case dest_vimvar:
5380 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5381 break;
5382 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005383 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005384 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5385 imported_T *import = NULL;
5386 int sid = current_sctx.sc_sid;
5387 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005388
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005389 if (name[1] != ':')
5390 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005391 import = find_imported(name, 0, cctx);
5392 if (import != NULL)
5393 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005394 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005395
5396 idx = get_script_item_idx(sid, rawname, TRUE);
5397 // TODO: specific type
5398 if (idx < 0)
5399 {
5400 char_u *name_s = name;
5401
5402 // Include s: in the name for store_var()
5403 if (name[1] != ':')
5404 {
5405 int len = (int)STRLEN(name) + 3;
5406
5407 name_s = alloc(len);
5408 if (name_s == NULL)
5409 name_s = name;
5410 else
5411 vim_snprintf((char *)name_s, len,
5412 "s:%s", name);
5413 }
5414 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005415 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005416 if (name_s != name)
5417 vim_free(name_s);
5418 }
5419 else
5420 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005421 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005422 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005423 break;
5424 case dest_local:
5425 if (lvar != NULL)
5426 {
5427 isn_T *isn = ((isn_T *)instr->ga_data)
5428 + instr->ga_len - 1;
5429
5430 // optimization: turn "var = 123" from ISN_PUSHNR +
5431 // ISN_STORE into ISN_STORENR
5432 if (!lvar->lv_from_outer
5433 && instr->ga_len == instr_count + 1
5434 && isn->isn_type == ISN_PUSHNR)
5435 {
5436 varnumber_T val = isn->isn_arg.number;
5437
5438 isn->isn_type = ISN_STORENR;
5439 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5440 isn->isn_arg.storenr.stnr_val = val;
5441 if (stack->ga_len > 0)
5442 --stack->ga_len;
5443 }
5444 else if (lvar->lv_from_outer)
5445 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005446 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005447 else
5448 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5449 }
5450 break;
5451 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005452 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005453
5454 if (var_idx + 1 < var_count)
5455 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005457
5458 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005459 if (var_count > 0 && !semicolon)
5460 {
5461 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5462 goto theend;
5463 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005465 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466
5467theend:
5468 vim_free(name);
5469 return ret;
5470}
5471
5472/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005473 * Check if "name" can be "unlet".
5474 */
5475 int
5476check_vim9_unlet(char_u *name)
5477{
5478 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5479 {
5480 semsg(_("E1081: Cannot unlet %s"), name);
5481 return FAIL;
5482 }
5483 return OK;
5484}
5485
5486/*
5487 * Callback passed to ex_unletlock().
5488 */
5489 static int
5490compile_unlet(
5491 lval_T *lvp,
5492 char_u *name_end,
5493 exarg_T *eap,
5494 int deep UNUSED,
5495 void *coookie)
5496{
5497 cctx_T *cctx = coookie;
5498
5499 if (lvp->ll_tv == NULL)
5500 {
5501 char_u *p = lvp->ll_name;
5502 int cc = *name_end;
5503 int ret = OK;
5504
5505 // Normal name. Only supports g:, w:, t: and b: namespaces.
5506 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005507 if (*p == '$')
5508 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5509 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005510 ret = FAIL;
5511 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005512 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005513
5514 *name_end = cc;
5515 return ret;
5516 }
5517
5518 // TODO: unlet {list}[idx]
5519 // TODO: unlet {dict}[key]
5520 emsg("Sorry, :unlet not fully implemented yet");
5521 return FAIL;
5522}
5523
5524/*
5525 * compile "unlet var", "lock var" and "unlock var"
5526 * "arg" points to "var".
5527 */
5528 static char_u *
5529compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5530{
5531 char_u *p = arg;
5532
5533 if (eap->cmdidx != CMD_unlet)
5534 {
5535 emsg("Sorry, :lock and unlock not implemented yet");
5536 return NULL;
5537 }
5538
5539 if (*p == '!')
5540 {
5541 p = skipwhite(p + 1);
5542 eap->forceit = TRUE;
5543 }
5544
5545 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5546 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5547}
5548
5549/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550 * Compile an :import command.
5551 */
5552 static char_u *
5553compile_import(char_u *arg, cctx_T *cctx)
5554{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005555 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556}
5557
5558/*
5559 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5560 */
5561 static int
5562compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5563{
5564 garray_T *instr = &cctx->ctx_instr;
5565 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5566
5567 if (endlabel == NULL)
5568 return FAIL;
5569 endlabel->el_next = *el;
5570 *el = endlabel;
5571 endlabel->el_end_label = instr->ga_len;
5572
5573 generate_JUMP(cctx, when, 0);
5574 return OK;
5575}
5576
5577 static void
5578compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5579{
5580 garray_T *instr = &cctx->ctx_instr;
5581
5582 while (*el != NULL)
5583 {
5584 endlabel_T *cur = (*el);
5585 isn_T *isn;
5586
5587 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5588 isn->isn_arg.jump.jump_where = instr->ga_len;
5589 *el = cur->el_next;
5590 vim_free(cur);
5591 }
5592}
5593
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005594 static void
5595compile_free_jump_to_end(endlabel_T **el)
5596{
5597 while (*el != NULL)
5598 {
5599 endlabel_T *cur = (*el);
5600
5601 *el = cur->el_next;
5602 vim_free(cur);
5603 }
5604}
5605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005606/*
5607 * Create a new scope and set up the generic items.
5608 */
5609 static scope_T *
5610new_scope(cctx_T *cctx, scopetype_T type)
5611{
5612 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5613
5614 if (scope == NULL)
5615 return NULL;
5616 scope->se_outer = cctx->ctx_scope;
5617 cctx->ctx_scope = scope;
5618 scope->se_type = type;
5619 scope->se_local_count = cctx->ctx_locals.ga_len;
5620 return scope;
5621}
5622
5623/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005624 * Free the current scope and go back to the outer scope.
5625 */
5626 static void
5627drop_scope(cctx_T *cctx)
5628{
5629 scope_T *scope = cctx->ctx_scope;
5630
5631 if (scope == NULL)
5632 {
5633 iemsg("calling drop_scope() without a scope");
5634 return;
5635 }
5636 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005637 switch (scope->se_type)
5638 {
5639 case IF_SCOPE:
5640 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5641 case FOR_SCOPE:
5642 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5643 case WHILE_SCOPE:
5644 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5645 case TRY_SCOPE:
5646 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5647 case NO_SCOPE:
5648 case BLOCK_SCOPE:
5649 break;
5650 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005651 vim_free(scope);
5652}
5653
5654/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005655 * compile "if expr"
5656 *
5657 * "if expr" Produces instructions:
5658 * EVAL expr Push result of "expr"
5659 * JUMP_IF_FALSE end
5660 * ... body ...
5661 * end:
5662 *
5663 * "if expr | else" Produces instructions:
5664 * EVAL expr Push result of "expr"
5665 * JUMP_IF_FALSE else
5666 * ... body ...
5667 * JUMP_ALWAYS end
5668 * else:
5669 * ... body ...
5670 * end:
5671 *
5672 * "if expr1 | elseif expr2 | else" Produces instructions:
5673 * EVAL expr Push result of "expr"
5674 * JUMP_IF_FALSE elseif
5675 * ... body ...
5676 * JUMP_ALWAYS end
5677 * elseif:
5678 * EVAL expr Push result of "expr"
5679 * JUMP_IF_FALSE else
5680 * ... body ...
5681 * JUMP_ALWAYS end
5682 * else:
5683 * ... body ...
5684 * end:
5685 */
5686 static char_u *
5687compile_if(char_u *arg, cctx_T *cctx)
5688{
5689 char_u *p = arg;
5690 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005691 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005692 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005693 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005694 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005695
Bram Moolenaara5565e42020-05-09 15:44:01 +02005696 CLEAR_FIELD(ppconst);
5697 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005698 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005699 clear_ppconst(&ppconst);
5700 return NULL;
5701 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005702 if (cctx->ctx_skip == SKIP_YES)
5703 clear_ppconst(&ppconst);
5704 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005705 {
5706 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005707 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005708 clear_ppconst(&ppconst);
5709 }
5710 else
5711 {
5712 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005713 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005714 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005715 return NULL;
5716 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005717
5718 scope = new_scope(cctx, IF_SCOPE);
5719 if (scope == NULL)
5720 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005721 scope->se_skip_save = skip_save;
5722 // "is_had_return" will be reset if any block does not end in :return
5723 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005724
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005725 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005726 {
5727 // "where" is set when ":elseif", "else" or ":endif" is found
5728 scope->se_u.se_if.is_if_label = instr->ga_len;
5729 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5730 }
5731 else
5732 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005733
5734 return p;
5735}
5736
5737 static char_u *
5738compile_elseif(char_u *arg, cctx_T *cctx)
5739{
5740 char_u *p = arg;
5741 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005742 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005743 isn_T *isn;
5744 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005745 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746
5747 if (scope == NULL || scope->se_type != IF_SCOPE)
5748 {
5749 emsg(_(e_elseif_without_if));
5750 return NULL;
5751 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005752 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005753 if (!cctx->ctx_had_return)
5754 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005755
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005756 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005757 {
5758 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005760 return NULL;
5761 // previous "if" or "elseif" jumps here
5762 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5763 isn->isn_arg.jump.jump_where = instr->ga_len;
5764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005765
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005766 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005767 CLEAR_FIELD(ppconst);
5768 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005769 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005770 clear_ppconst(&ppconst);
5771 return NULL;
5772 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005773 if (scope->se_skip_save == SKIP_YES)
5774 clear_ppconst(&ppconst);
5775 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005776 {
5777 // The expression results in a constant.
5778 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005779 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005780 clear_ppconst(&ppconst);
5781 scope->se_u.se_if.is_if_label = -1;
5782 }
5783 else
5784 {
5785 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005786 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005787 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005788 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005789
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005790 // "where" is set when ":elseif", "else" or ":endif" is found
5791 scope->se_u.se_if.is_if_label = instr->ga_len;
5792 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005794
5795 return p;
5796}
5797
5798 static char_u *
5799compile_else(char_u *arg, cctx_T *cctx)
5800{
5801 char_u *p = arg;
5802 garray_T *instr = &cctx->ctx_instr;
5803 isn_T *isn;
5804 scope_T *scope = cctx->ctx_scope;
5805
5806 if (scope == NULL || scope->se_type != IF_SCOPE)
5807 {
5808 emsg(_(e_else_without_if));
5809 return NULL;
5810 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005811 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005812 if (!cctx->ctx_had_return)
5813 scope->se_u.se_if.is_had_return = FALSE;
5814 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005815
Bram Moolenaarefd88552020-06-18 20:50:10 +02005816 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005817 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005818 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005819 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005820 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005821 if (!cctx->ctx_had_return
5822 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5823 JUMP_ALWAYS, cctx) == FAIL)
5824 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005825 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005826
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005827 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005828 {
5829 if (scope->se_u.se_if.is_if_label >= 0)
5830 {
5831 // previous "if" or "elseif" jumps here
5832 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5833 isn->isn_arg.jump.jump_where = instr->ga_len;
5834 scope->se_u.se_if.is_if_label = -1;
5835 }
5836 }
5837
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005838 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005839 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005841
5842 return p;
5843}
5844
5845 static char_u *
5846compile_endif(char_u *arg, cctx_T *cctx)
5847{
5848 scope_T *scope = cctx->ctx_scope;
5849 ifscope_T *ifscope;
5850 garray_T *instr = &cctx->ctx_instr;
5851 isn_T *isn;
5852
5853 if (scope == NULL || scope->se_type != IF_SCOPE)
5854 {
5855 emsg(_(e_endif_without_if));
5856 return NULL;
5857 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005858 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005859 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005860 if (!cctx->ctx_had_return)
5861 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005862
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005863 if (scope->se_u.se_if.is_if_label >= 0)
5864 {
5865 // previous "if" or "elseif" jumps here
5866 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5867 isn->isn_arg.jump.jump_where = instr->ga_len;
5868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869 // Fill in the "end" label in jumps at the end of the blocks.
5870 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005871 cctx->ctx_skip = scope->se_skip_save;
5872
5873 // If all the blocks end in :return and there is an :else then the
5874 // had_return flag is set.
5875 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005876
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005877 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005878 return arg;
5879}
5880
5881/*
5882 * compile "for var in expr"
5883 *
5884 * Produces instructions:
5885 * PUSHNR -1
5886 * STORE loop-idx Set index to -1
5887 * EVAL expr Push result of "expr"
5888 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5889 * - if beyond end, jump to "end"
5890 * - otherwise get item from list and push it
5891 * STORE var Store item in "var"
5892 * ... body ...
5893 * JUMP top Jump back to repeat
5894 * end: DROP Drop the result of "expr"
5895 *
5896 */
5897 static char_u *
5898compile_for(char_u *arg, cctx_T *cctx)
5899{
5900 char_u *p;
5901 size_t varlen;
5902 garray_T *instr = &cctx->ctx_instr;
5903 garray_T *stack = &cctx->ctx_type_stack;
5904 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005905 lvar_T *loop_lvar; // loop iteration variable
5906 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005907 type_T *vartype;
5908
5909 // TODO: list of variables: "for [key, value] in dict"
5910 // parse "var"
5911 for (p = arg; eval_isnamec1(*p); ++p)
5912 ;
5913 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005914 var_lvar = lookup_local(arg, varlen, cctx);
5915 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916 {
5917 semsg(_("E1023: variable already defined: %s"), arg);
5918 return NULL;
5919 }
5920
5921 // consume "in"
5922 p = skipwhite(p);
5923 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5924 {
5925 emsg(_(e_missing_in));
5926 return NULL;
5927 }
5928 p = skipwhite(p + 2);
5929
5930
5931 scope = new_scope(cctx, FOR_SCOPE);
5932 if (scope == NULL)
5933 return NULL;
5934
5935 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005936 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5937 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005938 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005939 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005940 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005943
5944 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005945 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5946 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005947 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005948 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005949 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005952
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005953 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005954
5955 // compile "expr", it remains on the stack until "endfor"
5956 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005957 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005958 {
5959 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005960 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005962
5963 // now we know the type of "var"
5964 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5965 if (vartype->tt_type != VAR_LIST)
5966 {
5967 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005968 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969 return NULL;
5970 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005971 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005972 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973
5974 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005975 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005976
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005977 generate_FOR(cctx, loop_lvar->lv_idx);
5978 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979
5980 return arg;
5981}
5982
5983/*
5984 * compile "endfor"
5985 */
5986 static char_u *
5987compile_endfor(char_u *arg, cctx_T *cctx)
5988{
5989 garray_T *instr = &cctx->ctx_instr;
5990 scope_T *scope = cctx->ctx_scope;
5991 forscope_T *forscope;
5992 isn_T *isn;
5993
5994 if (scope == NULL || scope->se_type != FOR_SCOPE)
5995 {
5996 emsg(_(e_for));
5997 return NULL;
5998 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005999 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006001 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002
6003 // At end of ":for" scope jump back to the FOR instruction.
6004 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6005
6006 // Fill in the "end" label in the FOR statement so it can jump here
6007 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6008 isn->isn_arg.forloop.for_end = instr->ga_len;
6009
6010 // Fill in the "end" label any BREAK statements
6011 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6012
6013 // Below the ":for" scope drop the "expr" list from the stack.
6014 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6015 return NULL;
6016
6017 vim_free(scope);
6018
6019 return arg;
6020}
6021
6022/*
6023 * compile "while expr"
6024 *
6025 * Produces instructions:
6026 * top: EVAL expr Push result of "expr"
6027 * JUMP_IF_FALSE end jump if false
6028 * ... body ...
6029 * JUMP top Jump back to repeat
6030 * end:
6031 *
6032 */
6033 static char_u *
6034compile_while(char_u *arg, cctx_T *cctx)
6035{
6036 char_u *p = arg;
6037 garray_T *instr = &cctx->ctx_instr;
6038 scope_T *scope;
6039
6040 scope = new_scope(cctx, WHILE_SCOPE);
6041 if (scope == NULL)
6042 return NULL;
6043
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006044 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006045
6046 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006047 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048 return NULL;
6049
6050 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006051 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052 JUMP_IF_FALSE, cctx) == FAIL)
6053 return FAIL;
6054
6055 return p;
6056}
6057
6058/*
6059 * compile "endwhile"
6060 */
6061 static char_u *
6062compile_endwhile(char_u *arg, cctx_T *cctx)
6063{
6064 scope_T *scope = cctx->ctx_scope;
6065
6066 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6067 {
6068 emsg(_(e_while));
6069 return NULL;
6070 }
6071 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006072 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006073
6074 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006075 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076
6077 // Fill in the "end" label in the WHILE statement so it can jump here.
6078 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006079 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080
6081 vim_free(scope);
6082
6083 return arg;
6084}
6085
6086/*
6087 * compile "continue"
6088 */
6089 static char_u *
6090compile_continue(char_u *arg, cctx_T *cctx)
6091{
6092 scope_T *scope = cctx->ctx_scope;
6093
6094 for (;;)
6095 {
6096 if (scope == NULL)
6097 {
6098 emsg(_(e_continue));
6099 return NULL;
6100 }
6101 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6102 break;
6103 scope = scope->se_outer;
6104 }
6105
6106 // Jump back to the FOR or WHILE instruction.
6107 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006108 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6109 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006110 return arg;
6111}
6112
6113/*
6114 * compile "break"
6115 */
6116 static char_u *
6117compile_break(char_u *arg, cctx_T *cctx)
6118{
6119 scope_T *scope = cctx->ctx_scope;
6120 endlabel_T **el;
6121
6122 for (;;)
6123 {
6124 if (scope == NULL)
6125 {
6126 emsg(_(e_break));
6127 return NULL;
6128 }
6129 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6130 break;
6131 scope = scope->se_outer;
6132 }
6133
6134 // Jump to the end of the FOR or WHILE loop.
6135 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006136 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006138 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6140 return FAIL;
6141
6142 return arg;
6143}
6144
6145/*
6146 * compile "{" start of block
6147 */
6148 static char_u *
6149compile_block(char_u *arg, cctx_T *cctx)
6150{
6151 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6152 return NULL;
6153 return skipwhite(arg + 1);
6154}
6155
6156/*
6157 * compile end of block: drop one scope
6158 */
6159 static void
6160compile_endblock(cctx_T *cctx)
6161{
6162 scope_T *scope = cctx->ctx_scope;
6163
6164 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006165 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006166 vim_free(scope);
6167}
6168
6169/*
6170 * compile "try"
6171 * Creates a new scope for the try-endtry, pointing to the first catch and
6172 * finally.
6173 * Creates another scope for the "try" block itself.
6174 * TRY instruction sets up exception handling at runtime.
6175 *
6176 * "try"
6177 * TRY -> catch1, -> finally push trystack entry
6178 * ... try block
6179 * "throw {exception}"
6180 * EVAL {exception}
6181 * THROW create exception
6182 * ... try block
6183 * " catch {expr}"
6184 * JUMP -> finally
6185 * catch1: PUSH exeception
6186 * EVAL {expr}
6187 * MATCH
6188 * JUMP nomatch -> catch2
6189 * CATCH remove exception
6190 * ... catch block
6191 * " catch"
6192 * JUMP -> finally
6193 * catch2: CATCH remove exception
6194 * ... catch block
6195 * " finally"
6196 * finally:
6197 * ... finally block
6198 * " endtry"
6199 * ENDTRY pop trystack entry, may rethrow
6200 */
6201 static char_u *
6202compile_try(char_u *arg, cctx_T *cctx)
6203{
6204 garray_T *instr = &cctx->ctx_instr;
6205 scope_T *try_scope;
6206 scope_T *scope;
6207
6208 // scope that holds the jumps that go to catch/finally/endtry
6209 try_scope = new_scope(cctx, TRY_SCOPE);
6210 if (try_scope == NULL)
6211 return NULL;
6212
6213 // "catch" is set when the first ":catch" is found.
6214 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006215 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216 if (generate_instr(cctx, ISN_TRY) == NULL)
6217 return NULL;
6218
6219 // scope for the try block itself
6220 scope = new_scope(cctx, BLOCK_SCOPE);
6221 if (scope == NULL)
6222 return NULL;
6223
6224 return arg;
6225}
6226
6227/*
6228 * compile "catch {expr}"
6229 */
6230 static char_u *
6231compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6232{
6233 scope_T *scope = cctx->ctx_scope;
6234 garray_T *instr = &cctx->ctx_instr;
6235 char_u *p;
6236 isn_T *isn;
6237
6238 // end block scope from :try or :catch
6239 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6240 compile_endblock(cctx);
6241 scope = cctx->ctx_scope;
6242
6243 // Error if not in a :try scope
6244 if (scope == NULL || scope->se_type != TRY_SCOPE)
6245 {
6246 emsg(_(e_catch));
6247 return NULL;
6248 }
6249
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006250 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 {
6252 emsg(_("E1033: catch unreachable after catch-all"));
6253 return NULL;
6254 }
6255
6256 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006257 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006258 JUMP_ALWAYS, cctx) == FAIL)
6259 return NULL;
6260
6261 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006262 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263 if (isn->isn_arg.try.try_catch == 0)
6264 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006265 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006266 {
6267 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006268 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006269 isn->isn_arg.jump.jump_where = instr->ga_len;
6270 }
6271
6272 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006273 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006275 scope->se_u.se_try.ts_caught_all = TRUE;
6276 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277 }
6278 else
6279 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006280 char_u *end;
6281 char_u *pat;
6282 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006283 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006284 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 // Push v:exception, push {expr} and MATCH
6287 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6288
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006289 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006290 if (*end != *p)
6291 {
6292 semsg(_("E1067: Separator mismatch: %s"), p);
6293 vim_free(tofree);
6294 return FAIL;
6295 }
6296 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006297 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006298 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006299 len = (int)(end - tofree);
6300 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006301 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006302 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006303 if (pat == NULL)
6304 return FAIL;
6305 if (generate_PUSHS(cctx, pat) == FAIL)
6306 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6309 return NULL;
6310
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006311 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6313 return NULL;
6314 }
6315
6316 if (generate_instr(cctx, ISN_CATCH) == NULL)
6317 return NULL;
6318
6319 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6320 return NULL;
6321 return p;
6322}
6323
6324 static char_u *
6325compile_finally(char_u *arg, cctx_T *cctx)
6326{
6327 scope_T *scope = cctx->ctx_scope;
6328 garray_T *instr = &cctx->ctx_instr;
6329 isn_T *isn;
6330
6331 // end block scope from :try or :catch
6332 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6333 compile_endblock(cctx);
6334 scope = cctx->ctx_scope;
6335
6336 // Error if not in a :try scope
6337 if (scope == NULL || scope->se_type != TRY_SCOPE)
6338 {
6339 emsg(_(e_finally));
6340 return NULL;
6341 }
6342
6343 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006344 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345 if (isn->isn_arg.try.try_finally != 0)
6346 {
6347 emsg(_(e_finally_dup));
6348 return NULL;
6349 }
6350
6351 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006352 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353
Bram Moolenaar585fea72020-04-02 22:33:21 +02006354 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006355 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356 {
6357 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006358 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006359 isn->isn_arg.jump.jump_where = instr->ga_len;
6360 }
6361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006362 // TODO: set index in ts_finally_label jumps
6363
6364 return arg;
6365}
6366
6367 static char_u *
6368compile_endtry(char_u *arg, cctx_T *cctx)
6369{
6370 scope_T *scope = cctx->ctx_scope;
6371 garray_T *instr = &cctx->ctx_instr;
6372 isn_T *isn;
6373
6374 // end block scope from :catch or :finally
6375 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6376 compile_endblock(cctx);
6377 scope = cctx->ctx_scope;
6378
6379 // Error if not in a :try scope
6380 if (scope == NULL || scope->se_type != TRY_SCOPE)
6381 {
6382 if (scope == NULL)
6383 emsg(_(e_no_endtry));
6384 else if (scope->se_type == WHILE_SCOPE)
6385 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006386 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387 emsg(_(e_endfor));
6388 else
6389 emsg(_(e_endif));
6390 return NULL;
6391 }
6392
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006393 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6395 {
6396 emsg(_("E1032: missing :catch or :finally"));
6397 return NULL;
6398 }
6399
6400 // Fill in the "end" label in jumps at the end of the blocks, if not done
6401 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006402 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403
6404 // End :catch or :finally scope: set value in ISN_TRY instruction
6405 if (isn->isn_arg.try.try_finally == 0)
6406 isn->isn_arg.try.try_finally = instr->ga_len;
6407 compile_endblock(cctx);
6408
6409 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6410 return NULL;
6411 return arg;
6412}
6413
6414/*
6415 * compile "throw {expr}"
6416 */
6417 static char_u *
6418compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6419{
6420 char_u *p = skipwhite(arg);
6421
Bram Moolenaara5565e42020-05-09 15:44:01 +02006422 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423 return NULL;
6424 if (may_generate_2STRING(-1, cctx) == FAIL)
6425 return NULL;
6426 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6427 return NULL;
6428
6429 return p;
6430}
6431
6432/*
6433 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006434 * compile "echomsg expr"
6435 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006436 * compile "execute expr"
6437 */
6438 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006439compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006440{
6441 char_u *p = arg;
6442 int count = 0;
6443
6444 for (;;)
6445 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006446 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006447 return NULL;
6448 ++count;
6449 p = skipwhite(p);
6450 if (ends_excmd(*p))
6451 break;
6452 }
6453
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006454 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6455 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6456 else if (cmdidx == CMD_execute)
6457 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6458 else if (cmdidx == CMD_echomsg)
6459 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6460 else
6461 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006462 return p;
6463}
6464
6465/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006466 * A command that is not compiled, execute with legacy code.
6467 */
6468 static char_u *
6469compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6470{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006471 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006472 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006473
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006474 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006475 goto theend;
6476
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006477 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6478 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006479 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6480 {
6481 // expand filename in "syntax include [@group] filename"
6482 has_expr = TRUE;
6483 eap->arg = skipwhite(eap->arg + 7);
6484 if (*eap->arg == '@')
6485 eap->arg = skiptowhite(eap->arg);
6486 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006487
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006488 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006489 {
6490 int count = 0;
6491 char_u *start = skipwhite(line);
6492
6493 // :cmd xxx`=expr1`yyy`=expr2`zzz
6494 // PUSHS ":cmd xxx"
6495 // eval expr1
6496 // PUSHS "yyy"
6497 // eval expr2
6498 // PUSHS "zzz"
6499 // EXECCONCAT 5
6500 for (;;)
6501 {
6502 if (p > start)
6503 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006504 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006505 ++count;
6506 }
6507 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006508 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006509 return NULL;
6510 may_generate_2STRING(-1, cctx);
6511 ++count;
6512 p = skipwhite(p);
6513 if (*p != '`')
6514 {
6515 emsg(_("E1083: missing backtick"));
6516 return NULL;
6517 }
6518 start = p + 1;
6519
6520 p = (char_u *)strstr((char *)start, "`=");
6521 if (p == NULL)
6522 {
6523 if (*skipwhite(start) != NUL)
6524 {
6525 generate_PUSHS(cctx, vim_strsave(start));
6526 ++count;
6527 }
6528 break;
6529 }
6530 }
6531 generate_EXECCONCAT(cctx, count);
6532 }
6533 else
6534 generate_EXEC(cctx, line);
6535
6536theend:
6537 return (char_u *)"";
6538}
6539
6540/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006541 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006542 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006543 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006544 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006545add_def_function(ufunc_T *ufunc)
6546{
6547 dfunc_T *dfunc;
6548
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006549 if (def_functions.ga_len == 0)
6550 {
6551 // The first position is not used, so that a zero uf_dfunc_idx means it
6552 // wasn't set.
6553 if (ga_grow(&def_functions, 1) == FAIL)
6554 return FAIL;
6555 ++def_functions.ga_len;
6556 }
6557
Bram Moolenaar09689a02020-05-09 22:50:08 +02006558 // Add the function to "def_functions".
6559 if (ga_grow(&def_functions, 1) == FAIL)
6560 return FAIL;
6561 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6562 CLEAR_POINTER(dfunc);
6563 dfunc->df_idx = def_functions.ga_len;
6564 ufunc->uf_dfunc_idx = dfunc->df_idx;
6565 dfunc->df_ufunc = ufunc;
6566 ++def_functions.ga_len;
6567 return OK;
6568}
6569
6570/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571 * After ex_function() has collected all the function lines: parse and compile
6572 * the lines into instructions.
6573 * Adds the function to "def_functions".
6574 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6575 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006576 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006577 * This can be used recursively through compile_lambda(), which may reallocate
6578 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006579 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006581 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006582compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006584 char_u *line = NULL;
6585 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 cctx_T cctx;
6588 garray_T *instr;
6589 int called_emsg_before = called_emsg;
6590 int ret = FAIL;
6591 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006592 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006593 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006595 // When using a function that was compiled before: Free old instructions.
6596 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006597 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006599 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6600 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006601 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006603 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006604 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006605
Bram Moolenaara80faa82020-04-12 19:37:17 +02006606 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 cctx.ctx_ufunc = ufunc;
6608 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006609 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006610 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6611 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6612 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6613 cctx.ctx_type_list = &ufunc->uf_type_list;
6614 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6615 instr = &cctx.ctx_instr;
6616
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006617 // Set the context to the function, it may be compiled when called from
6618 // another script. Set the script version to the most modern one.
6619 // The line number will be set in next_line_from_context().
6620 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6622
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006623 // Make sure error messages are OK.
6624 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6625 if (do_estack_push)
6626 estack_push_ufunc(ufunc, 1);
6627
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006628 if (ufunc->uf_def_args.ga_len > 0)
6629 {
6630 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006631 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006632 int i;
6633 char_u *arg;
6634 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6635
6636 // Produce instructions for the default values of optional arguments.
6637 // Store the instruction index in uf_def_arg_idx[] so that we know
6638 // where to start when the function is called, depending on the number
6639 // of arguments.
6640 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6641 if (ufunc->uf_def_arg_idx == NULL)
6642 goto erret;
6643 for (i = 0; i < count; ++i)
6644 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006645 garray_T *stack = &cctx.ctx_type_stack;
6646 type_T *val_type;
6647 int arg_idx = first_def_arg + i;
6648
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006649 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6650 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006651 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006652 goto erret;
6653
6654 // If no type specified use the type of the default value.
6655 // Otherwise check that the default value type matches the
6656 // specified type.
6657 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6658 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6659 ufunc->uf_arg_types[arg_idx] = val_type;
6660 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6661 == FAIL)
6662 {
6663 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6664 arg_idx + 1);
6665 goto erret;
6666 }
6667
6668 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006669 goto erret;
6670 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006671 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6672 }
6673
6674 /*
6675 * Loop over all the lines of the function and generate instructions.
6676 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006677 for (;;)
6678 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006679 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006680 int starts_with_colon = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006681
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006682 // Bail out on the first error to avoid a flood of errors and report
6683 // the right line number when inside try/catch.
6684 if (emsg_before != called_emsg)
6685 goto erret;
6686
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687 if (line != NULL && *line == '|')
6688 // the line continues after a '|'
6689 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006690 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006691 && !(*line == '#' && (line == cctx.ctx_line_start
6692 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006694 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695 goto erret;
6696 }
6697 else
6698 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006699 line = next_line_from_context(&cctx);
6700 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006701 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006704 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006705
Bram Moolenaara80faa82020-04-12 19:37:17 +02006706 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006707 ea.cmdlinep = &line;
6708 ea.cmd = skipwhite(line);
6709
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006710 // Some things can be recognized by the first character.
6711 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006713 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006714 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006715 if (ea.cmd[1] != '{')
6716 {
6717 line = (char_u *)"";
6718 continue;
6719 }
6720 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006722 case '}':
6723 {
6724 // "}" ends a block scope
6725 scopetype_T stype = cctx.ctx_scope == NULL
6726 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006727
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006728 if (stype == BLOCK_SCOPE)
6729 {
6730 compile_endblock(&cctx);
6731 line = ea.cmd;
6732 }
6733 else
6734 {
6735 emsg(_("E1025: using } outside of a block scope"));
6736 goto erret;
6737 }
6738 if (line != NULL)
6739 line = skipwhite(ea.cmd + 1);
6740 continue;
6741 }
6742
6743 case '{':
6744 // "{" starts a block scope
6745 // "{'a': 1}->func() is something else
6746 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6747 {
6748 line = compile_block(ea.cmd, &cctx);
6749 continue;
6750 }
6751 break;
6752
6753 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006754 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006755 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756 }
6757
6758 /*
6759 * COMMAND MODIFIERS
6760 */
6761 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6762 {
6763 if (errormsg != NULL)
6764 goto erret;
6765 // empty line or comment
6766 line = (char_u *)"";
6767 continue;
6768 }
6769
6770 // Skip ":call" to get to the function name.
6771 if (checkforcmd(&ea.cmd, "call", 3))
6772 ea.cmd = skipwhite(ea.cmd);
6773
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006774 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006776 char_u *pskip;
6777
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006778 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006779 // find what follows.
6780 // Skip over "var.member", "var[idx]" and the like.
6781 // Also "&opt = val", "$ENV = val" and "@r = val".
6782 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006783 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006784 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006785 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006787 char_u *var_end;
6788 int oplen;
6789 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006791 var_end = find_name_end(pskip, NULL, NULL,
6792 FNE_CHECK_START | FNE_INCL_BR);
6793 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006794 if (oplen > 0)
6795 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006796 size_t len = p - ea.cmd;
6797
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006798 // Recognize an assignment if we recognize the variable
6799 // name:
6800 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006801 // "local = expr" where "local" is a local var.
6802 // "script = expr" where "script" is a script-local var.
6803 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006804 // "&opt = expr"
6805 // "$ENV = expr"
6806 // "@r = expr"
6807 if (*ea.cmd == '&'
6808 || *ea.cmd == '$'
6809 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006810 || ((len) > 2 && ea.cmd[1] == ':')
6811 || lookup_local(ea.cmd, len, &cctx) != NULL
6812 || lookup_arg(ea.cmd, len, NULL, NULL,
6813 NULL, &cctx) == OK
6814 || lookup_script(ea.cmd, len) == OK
6815 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006816 {
6817 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006818 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006819 goto erret;
6820 continue;
6821 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006822 }
6823 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006824
6825 if (*ea.cmd == '[')
6826 {
6827 // [var, var] = expr
6828 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6829 if (line == NULL)
6830 goto erret;
6831 if (line != ea.cmd)
6832 continue;
6833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006834 }
6835
6836 /*
6837 * COMMAND after range
6838 */
6839 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006840 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006841 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006842 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843
6844 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6845 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006846 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006847 {
6848 line += STRLEN(line);
6849 continue;
6850 }
6851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006852 // Expression or function call.
6853 if (ea.cmdidx == CMD_eval)
6854 {
6855 p = ea.cmd;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006856 if (compile_expr0(&p, &cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857 goto erret;
6858
6859 // drop the return value
6860 generate_instr_drop(&cctx, ISN_DROP, 1);
6861 line = p;
6862 continue;
6863 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006864 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006865 iemsg("Command from find_ex_command() not handled");
6866 goto erret;
6867 }
6868
6869 p = skipwhite(p);
6870
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006871 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006872 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006873 && ea.cmdidx != CMD_elseif
6874 && ea.cmdidx != CMD_else
6875 && ea.cmdidx != CMD_endif)
6876 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006877 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006878 continue;
6879 }
6880
Bram Moolenaarefd88552020-06-18 20:50:10 +02006881 if (ea.cmdidx != CMD_elseif
6882 && ea.cmdidx != CMD_else
6883 && ea.cmdidx != CMD_endif
6884 && ea.cmdidx != CMD_endfor
6885 && ea.cmdidx != CMD_endwhile
6886 && ea.cmdidx != CMD_catch
6887 && ea.cmdidx != CMD_finally
6888 && ea.cmdidx != CMD_endtry)
6889 {
6890 if (cctx.ctx_had_return)
6891 {
6892 emsg(_("E1095: Unreachable code after :return"));
6893 goto erret;
6894 }
6895 }
6896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 switch (ea.cmdidx)
6898 {
6899 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006900 ea.arg = p;
6901 line = compile_nested_function(&ea, &cctx);
6902 break;
6903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006904 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006905 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906 goto erret;
6907
6908 case CMD_return:
6909 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006910 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006911 break;
6912
6913 case CMD_let:
6914 case CMD_const:
6915 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006916 if (line == p)
6917 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918 break;
6919
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006920 case CMD_unlet:
6921 case CMD_unlockvar:
6922 case CMD_lockvar:
6923 line = compile_unletlock(p, &ea, &cctx);
6924 break;
6925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006926 case CMD_import:
6927 line = compile_import(p, &cctx);
6928 break;
6929
6930 case CMD_if:
6931 line = compile_if(p, &cctx);
6932 break;
6933 case CMD_elseif:
6934 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006935 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 break;
6937 case CMD_else:
6938 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006939 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940 break;
6941 case CMD_endif:
6942 line = compile_endif(p, &cctx);
6943 break;
6944
6945 case CMD_while:
6946 line = compile_while(p, &cctx);
6947 break;
6948 case CMD_endwhile:
6949 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006950 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006951 break;
6952
6953 case CMD_for:
6954 line = compile_for(p, &cctx);
6955 break;
6956 case CMD_endfor:
6957 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006958 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959 break;
6960 case CMD_continue:
6961 line = compile_continue(p, &cctx);
6962 break;
6963 case CMD_break:
6964 line = compile_break(p, &cctx);
6965 break;
6966
6967 case CMD_try:
6968 line = compile_try(p, &cctx);
6969 break;
6970 case CMD_catch:
6971 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006972 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 break;
6974 case CMD_finally:
6975 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006976 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 break;
6978 case CMD_endtry:
6979 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006980 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006981 break;
6982 case CMD_throw:
6983 line = compile_throw(p, &cctx);
6984 break;
6985
6986 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006988 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006989 case CMD_echomsg:
6990 case CMD_echoerr:
6991 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006992 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993
6994 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006995 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006996 // Not recognized, execute with do_cmdline_cmd().
6997 ea.arg = p;
6998 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 break;
7000 }
7001 if (line == NULL)
7002 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007003 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007004
7005 if (cctx.ctx_type_stack.ga_len < 0)
7006 {
7007 iemsg("Type stack underflow");
7008 goto erret;
7009 }
7010 }
7011
7012 if (cctx.ctx_scope != NULL)
7013 {
7014 if (cctx.ctx_scope->se_type == IF_SCOPE)
7015 emsg(_(e_endif));
7016 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7017 emsg(_(e_endwhile));
7018 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7019 emsg(_(e_endfor));
7020 else
7021 emsg(_("E1026: Missing }"));
7022 goto erret;
7023 }
7024
Bram Moolenaarefd88552020-06-18 20:50:10 +02007025 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026 {
7027 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7028 {
7029 emsg(_("E1027: Missing return statement"));
7030 goto erret;
7031 }
7032
7033 // Return zero if there is no return at the end.
7034 generate_PUSHNR(&cctx, 0);
7035 generate_instr(&cctx, ISN_RETURN);
7036 }
7037
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007038 {
7039 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7040 + ufunc->uf_dfunc_idx;
7041 dfunc->df_deleted = FALSE;
7042 dfunc->df_instr = instr->ga_data;
7043 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007044 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007045 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007046 if (cctx.ctx_outer_used)
7047 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007048 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007049 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050
7051 ret = OK;
7052
7053erret:
7054 if (ret == FAIL)
7055 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007056 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007057 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7058 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007059
7060 for (idx = 0; idx < instr->ga_len; ++idx)
7061 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007063
Bram Moolenaar45a15082020-05-25 00:28:33 +02007064 // if using the last entry in the table we might as well remove it
7065 if (!dfunc->df_deleted
7066 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007067 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007068 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007069
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007070 while (cctx.ctx_scope != NULL)
7071 drop_scope(&cctx);
7072
Bram Moolenaar20431c92020-03-20 18:39:46 +01007073 // Don't execute this function body.
7074 ga_clear_strings(&ufunc->uf_lines);
7075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 if (errormsg != NULL)
7077 emsg(errormsg);
7078 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007079 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 }
7081
7082 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007083 if (do_estack_push)
7084 estack_pop();
7085
Bram Moolenaar20431c92020-03-20 18:39:46 +01007086 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007087 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007089 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090}
7091
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007092 void
7093set_function_type(ufunc_T *ufunc)
7094{
7095 int varargs = ufunc->uf_va_name != NULL;
7096 int argcount = ufunc->uf_args.ga_len;
7097
7098 // Create a type for the function, with the return type and any
7099 // argument types.
7100 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7101 // The type is included in "tt_args".
7102 if (argcount > 0 || varargs)
7103 {
7104 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7105 argcount, &ufunc->uf_type_list);
7106 // Add argument types to the function type.
7107 if (func_type_add_arg_types(ufunc->uf_func_type,
7108 argcount + varargs,
7109 &ufunc->uf_type_list) == FAIL)
7110 return;
7111 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7112 ufunc->uf_func_type->tt_min_argcount =
7113 argcount - ufunc->uf_def_args.ga_len;
7114 if (ufunc->uf_arg_types == NULL)
7115 {
7116 int i;
7117
7118 // lambda does not have argument types.
7119 for (i = 0; i < argcount; ++i)
7120 ufunc->uf_func_type->tt_args[i] = &t_any;
7121 }
7122 else
7123 mch_memmove(ufunc->uf_func_type->tt_args,
7124 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7125 if (varargs)
7126 {
7127 ufunc->uf_func_type->tt_args[argcount] =
7128 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7129 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7130 }
7131 }
7132 else
7133 // No arguments, can use a predefined type.
7134 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7135 argcount, &ufunc->uf_type_list);
7136}
7137
7138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139/*
7140 * Delete an instruction, free what it contains.
7141 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007142 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143delete_instr(isn_T *isn)
7144{
7145 switch (isn->isn_type)
7146 {
7147 case ISN_EXEC:
7148 case ISN_LOADENV:
7149 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007150 case ISN_LOADB:
7151 case ISN_LOADW:
7152 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007153 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007154 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 case ISN_PUSHEXC:
7156 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007157 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007159 case ISN_STOREB:
7160 case ISN_STOREW:
7161 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007162 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 vim_free(isn->isn_arg.string);
7164 break;
7165
7166 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007167 case ISN_STORES:
7168 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169 break;
7170
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007171 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007172 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007173 vim_free(isn->isn_arg.unlet.ul_name);
7174 break;
7175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007176 case ISN_STOREOPT:
7177 vim_free(isn->isn_arg.storeopt.so_name);
7178 break;
7179
7180 case ISN_PUSHBLOB: // push blob isn_arg.blob
7181 blob_unref(isn->isn_arg.blob);
7182 break;
7183
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007184 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007185#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007186 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007187#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007188 break;
7189
7190 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007191#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007192 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007193#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007194 break;
7195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196 case ISN_UCALL:
7197 vim_free(isn->isn_arg.ufunc.cuf_name);
7198 break;
7199
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007200 case ISN_FUNCREF:
7201 {
7202 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7203 + isn->isn_arg.funcref.fr_func;
7204 func_ptr_unref(dfunc->df_ufunc);
7205 }
7206 break;
7207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007208 case ISN_2BOOL:
7209 case ISN_2STRING:
7210 case ISN_ADDBLOB:
7211 case ISN_ADDLIST:
7212 case ISN_BCALL:
7213 case ISN_CATCH:
7214 case ISN_CHECKNR:
7215 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007216 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217 case ISN_COMPAREANY:
7218 case ISN_COMPAREBLOB:
7219 case ISN_COMPAREBOOL:
7220 case ISN_COMPAREDICT:
7221 case ISN_COMPAREFLOAT:
7222 case ISN_COMPAREFUNC:
7223 case ISN_COMPARELIST:
7224 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225 case ISN_COMPARESPECIAL:
7226 case ISN_COMPARESTRING:
7227 case ISN_CONCAT:
7228 case ISN_DCALL:
7229 case ISN_DROP:
7230 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007231 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007232 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007233 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007234 case ISN_EXECCONCAT:
7235 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007237 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007238 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007239 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007240 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241 case ISN_JUMP:
7242 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007243 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244 case ISN_LOADSCRIPT:
7245 case ISN_LOADREG:
7246 case ISN_LOADV:
7247 case ISN_NEGATENR:
7248 case ISN_NEWDICT:
7249 case ISN_NEWLIST:
7250 case ISN_OPNR:
7251 case ISN_OPFLOAT:
7252 case ISN_OPANY:
7253 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007254 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007255 case ISN_PUSHF:
7256 case ISN_PUSHNR:
7257 case ISN_PUSHBOOL:
7258 case ISN_PUSHSPEC:
7259 case ISN_RETURN:
7260 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007261 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007262 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007264 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007265 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007266 case ISN_STOREDICT:
7267 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007268 case ISN_THROW:
7269 case ISN_TRY:
7270 // nothing allocated
7271 break;
7272 }
7273}
7274
7275/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007276 * Free all instructions for "dfunc".
7277 */
7278 static void
7279delete_def_function_contents(dfunc_T *dfunc)
7280{
7281 int idx;
7282
7283 ga_clear(&dfunc->df_def_args_isn);
7284
7285 if (dfunc->df_instr != NULL)
7286 {
7287 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7288 delete_instr(dfunc->df_instr + idx);
7289 VIM_CLEAR(dfunc->df_instr);
7290 }
7291
7292 dfunc->df_deleted = TRUE;
7293}
7294
7295/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007296 * When a user function is deleted, clear the contents of any associated def
7297 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298 */
7299 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007300clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007302 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007303 {
7304 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7305 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306
Bram Moolenaar20431c92020-03-20 18:39:46 +01007307 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007308 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007309 }
7310}
7311
7312#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007313/*
7314 * Free all functions defined with ":def".
7315 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316 void
7317free_def_functions(void)
7318{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007319 int idx;
7320
7321 for (idx = 0; idx < def_functions.ga_len; ++idx)
7322 {
7323 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7324
7325 delete_def_function_contents(dfunc);
7326 }
7327
7328 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007329}
7330#endif
7331
7332
7333#endif // FEAT_EVAL