blob: 8d6c1398dad133bf15d608cc3cbd2cd2dd2a6d2c [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
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200291check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100292{
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 Moolenaar6110e792020-07-08 19:35:21 +0200324 void
325clear_type_list(garray_T *gap)
326{
327 while (gap->ga_len > 0)
328 vim_free(((type_T **)gap->ga_data)[--gap->ga_len]);
329 ga_clear(gap);
330}
331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200333get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334{
335 type_T *type;
336
337 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200338 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100339 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200340 if (member_type->tt_type == VAR_VOID
341 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100342 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100343 if (member_type->tt_type == VAR_BOOL)
344 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100345 if (member_type->tt_type == VAR_NUMBER)
346 return &t_list_number;
347 if (member_type->tt_type == VAR_STRING)
348 return &t_list_string;
349
350 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200351 type = alloc_type(type_gap);
352 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100353 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100354 type->tt_type = VAR_LIST;
355 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200356 type->tt_argcount = 0;
357 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358 return type;
359}
360
361 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200362get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363{
364 type_T *type;
365
366 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200367 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100368 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200369 if (member_type->tt_type == VAR_VOID
370 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100371 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100372 if (member_type->tt_type == VAR_BOOL)
373 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100374 if (member_type->tt_type == VAR_NUMBER)
375 return &t_dict_number;
376 if (member_type->tt_type == VAR_STRING)
377 return &t_dict_string;
378
379 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200380 type = alloc_type(type_gap);
381 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100382 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 type->tt_type = VAR_DICT;
384 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200385 type->tt_argcount = 0;
386 type->tt_args = NULL;
387 return type;
388}
389
390/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200391 * Allocate a new type for a function.
392 */
393 static type_T *
394alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
395{
396 type_T *type = alloc_type(type_gap);
397
398 if (type == NULL)
399 return &t_any;
400 type->tt_type = VAR_FUNC;
401 type->tt_member = ret_type;
402 type->tt_argcount = argcount;
403 type->tt_args = NULL;
404 return type;
405}
406
407/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200408 * Get a function type, based on the return type "ret_type".
409 * If "argcount" is -1 or 0 a predefined type can be used.
410 * If "argcount" > 0 always create a new type, so that arguments can be added.
411 */
412 static type_T *
413get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
414{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200415 // recognize commonly used types
416 if (argcount <= 0)
417 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200418 if (ret_type == &t_unknown)
419 {
420 // (argcount == 0) is not possible
421 return &t_func_unknown;
422 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200423 if (ret_type == &t_void)
424 {
425 if (argcount == 0)
426 return &t_func_0_void;
427 else
428 return &t_func_void;
429 }
430 if (ret_type == &t_any)
431 {
432 if (argcount == 0)
433 return &t_func_0_any;
434 else
435 return &t_func_any;
436 }
437 if (ret_type == &t_number)
438 {
439 if (argcount == 0)
440 return &t_func_0_number;
441 else
442 return &t_func_number;
443 }
444 if (ret_type == &t_string)
445 {
446 if (argcount == 0)
447 return &t_func_0_string;
448 else
449 return &t_func_string;
450 }
451 }
452
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200453 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454}
455
Bram Moolenaara8c17702020-04-01 21:17:24 +0200456/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200457 * For a function type, reserve space for "argcount" argument types (including
458 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200459 */
460 static int
461func_type_add_arg_types(
462 type_T *functype,
463 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200464 garray_T *type_gap)
465{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200466 // To make it easy to free the space needed for the argument types, add the
467 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200468 if (ga_grow(type_gap, 1) == FAIL)
469 return FAIL;
470 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
471 if (functype->tt_args == NULL)
472 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200473 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
474 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200475 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 return OK;
477}
478
479/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200480 * Return the type_T for a typval. Only for primitive types.
481 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200482 type_T *
Bram Moolenaara8c17702020-04-01 21:17:24 +0200483typval2type(typval_T *tv)
484{
485 if (tv->v_type == VAR_NUMBER)
486 return &t_number;
487 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200488 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200489 if (tv->v_type == VAR_STRING)
490 return &t_string;
491 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
492 return &t_list_string;
493 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
494 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200495 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200496}
497
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200498 static void
499type_mismatch(type_T *expected, type_T *actual)
500{
501 char *tofree1, *tofree2;
502
503 semsg(_("E1013: type mismatch, expected %s but got %s"),
504 type_name(expected, &tofree1), type_name(actual, &tofree2));
505 vim_free(tofree1);
506 vim_free(tofree2);
507}
508
509 static void
510arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
511{
512 char *tofree1, *tofree2;
513
514 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
515 argidx,
516 type_name(expected, &tofree1), type_name(actual, &tofree2));
517 vim_free(tofree1);
518 vim_free(tofree2);
519}
520
521/*
522 * Check if the expected and actual types match.
523 * Does not allow for assigning "any" to a specific type.
524 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200525 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200526check_type(type_T *expected, type_T *actual, int give_msg)
527{
528 int ret = OK;
529
530 // When expected is "unknown" we accept any actual type.
531 // When expected is "any" we accept any actual type except "void".
532 if (expected->tt_type != VAR_UNKNOWN
533 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
534
535 {
536 if (expected->tt_type != actual->tt_type)
537 {
538 if (give_msg)
539 type_mismatch(expected, actual);
540 return FAIL;
541 }
542 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
543 {
544 // "unknown" is used for an empty list or dict
545 if (actual->tt_member != &t_unknown)
546 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
547 }
548 else if (expected->tt_type == VAR_FUNC)
549 {
550 if (expected->tt_member != &t_unknown)
551 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
552 if (ret == OK && expected->tt_argcount != -1
553 && (actual->tt_argcount < expected->tt_min_argcount
554 || actual->tt_argcount > expected->tt_argcount))
555 ret = FAIL;
556 }
557 if (ret == FAIL && give_msg)
558 type_mismatch(expected, actual);
559 }
560 return ret;
561}
562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563/////////////////////////////////////////////////////////////////////
564// Following generate_ functions expect the caller to call ga_grow().
565
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200566#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
567#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569/*
570 * Generate an instruction without arguments.
571 * Returns a pointer to the new instruction, NULL if failed.
572 */
573 static isn_T *
574generate_instr(cctx_T *cctx, isntype_T isn_type)
575{
576 garray_T *instr = &cctx->ctx_instr;
577 isn_T *isn;
578
Bram Moolenaar080457c2020-03-03 21:53:32 +0100579 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 if (ga_grow(instr, 1) == FAIL)
581 return NULL;
582 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
583 isn->isn_type = isn_type;
584 isn->isn_lnum = cctx->ctx_lnum + 1;
585 ++instr->ga_len;
586
587 return isn;
588}
589
590/*
591 * Generate an instruction without arguments.
592 * "drop" will be removed from the stack.
593 * Returns a pointer to the new instruction, NULL if failed.
594 */
595 static isn_T *
596generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
597{
598 garray_T *stack = &cctx->ctx_type_stack;
599
Bram Moolenaar080457c2020-03-03 21:53:32 +0100600 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 stack->ga_len -= drop;
602 return generate_instr(cctx, isn_type);
603}
604
605/*
606 * Generate instruction "isn_type" and put "type" on the type stack.
607 */
608 static isn_T *
609generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
610{
611 isn_T *isn;
612 garray_T *stack = &cctx->ctx_type_stack;
613
614 if ((isn = generate_instr(cctx, isn_type)) == NULL)
615 return NULL;
616
617 if (ga_grow(stack, 1) == FAIL)
618 return NULL;
619 ((type_T **)stack->ga_data)[stack->ga_len] = type;
620 ++stack->ga_len;
621
622 return isn;
623}
624
625/*
626 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
627 */
628 static int
629may_generate_2STRING(int offset, cctx_T *cctx)
630{
631 isn_T *isn;
632 garray_T *stack = &cctx->ctx_type_stack;
633 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
634
635 if ((*type)->tt_type == VAR_STRING)
636 return OK;
637 *type = &t_string;
638
639 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
640 return FAIL;
641 isn->isn_arg.number = offset;
642
643 return OK;
644}
645
646 static int
647check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
648{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200649 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200651 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 {
653 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200654 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 else
656 semsg(_("E1036: %c requires number or float arguments"), *op);
657 return FAIL;
658 }
659 return OK;
660}
661
662/*
663 * Generate an instruction with two arguments. The instruction depends on the
664 * type of the arguments.
665 */
666 static int
667generate_two_op(cctx_T *cctx, char_u *op)
668{
669 garray_T *stack = &cctx->ctx_type_stack;
670 type_T *type1;
671 type_T *type2;
672 vartype_T vartype;
673 isn_T *isn;
674
Bram Moolenaar080457c2020-03-03 21:53:32 +0100675 RETURN_OK_IF_SKIP(cctx);
676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 // Get the known type of the two items on the stack. If they are matching
678 // use a type-specific instruction. Otherwise fall back to runtime type
679 // checking.
680 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
681 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 if (type1->tt_type == type2->tt_type
684 && (type1->tt_type == VAR_NUMBER
685 || type1->tt_type == VAR_LIST
686#ifdef FEAT_FLOAT
687 || type1->tt_type == VAR_FLOAT
688#endif
689 || type1->tt_type == VAR_BLOB))
690 vartype = type1->tt_type;
691
692 switch (*op)
693 {
694 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200695 && type1->tt_type != VAR_ANY
696 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 && check_number_or_float(
698 type1->tt_type, type2->tt_type, op) == FAIL)
699 return FAIL;
700 isn = generate_instr_drop(cctx,
701 vartype == VAR_NUMBER ? ISN_OPNR
702 : vartype == VAR_LIST ? ISN_ADDLIST
703 : vartype == VAR_BLOB ? ISN_ADDBLOB
704#ifdef FEAT_FLOAT
705 : vartype == VAR_FLOAT ? ISN_OPFLOAT
706#endif
707 : ISN_OPANY, 1);
708 if (isn != NULL)
709 isn->isn_arg.op.op_type = EXPR_ADD;
710 break;
711
712 case '-':
713 case '*':
714 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
715 op) == FAIL)
716 return FAIL;
717 if (vartype == VAR_NUMBER)
718 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
719#ifdef FEAT_FLOAT
720 else if (vartype == VAR_FLOAT)
721 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
722#endif
723 else
724 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
725 if (isn != NULL)
726 isn->isn_arg.op.op_type = *op == '*'
727 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
728 break;
729
Bram Moolenaar4c683752020-04-05 21:38:23 +0200730 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200732 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 && type2->tt_type != VAR_NUMBER))
734 {
735 emsg(_("E1035: % requires number arguments"));
736 return FAIL;
737 }
738 isn = generate_instr_drop(cctx,
739 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
740 if (isn != NULL)
741 isn->isn_arg.op.op_type = EXPR_REM;
742 break;
743 }
744
745 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200746 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 {
748 type_T *type = &t_any;
749
750#ifdef FEAT_FLOAT
751 // float+number and number+float results in float
752 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
753 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
754 type = &t_float;
755#endif
756 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
757 }
758
759 return OK;
760}
761
762/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200763 * Get the instruction to use for comparing "type1" with "type2"
764 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200766 static isntype_T
767get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768{
769 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770
Bram Moolenaar4c683752020-04-05 21:38:23 +0200771 if (type1 == VAR_UNKNOWN)
772 type1 = VAR_ANY;
773 if (type2 == VAR_UNKNOWN)
774 type2 = VAR_ANY;
775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 if (type1 == type2)
777 {
778 switch (type1)
779 {
780 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
781 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
782 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
783 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
784 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
785 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
786 case VAR_LIST: isntype = ISN_COMPARELIST; break;
787 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
788 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 default: isntype = ISN_COMPAREANY; break;
790 }
791 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200792 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
794 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
795 isntype = ISN_COMPAREANY;
796
797 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
798 && (isntype == ISN_COMPAREBOOL
799 || isntype == ISN_COMPARESPECIAL
800 || isntype == ISN_COMPARENR
801 || isntype == ISN_COMPAREFLOAT))
802 {
803 semsg(_("E1037: Cannot use \"%s\" with %s"),
804 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200805 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 }
807 if (isntype == ISN_DROP
808 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
809 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
810 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
811 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
812 && exptype != EXPR_IS && exptype != EXPR_ISNOT
813 && (type1 == VAR_BLOB || type2 == VAR_BLOB
814 || type1 == VAR_LIST || type2 == VAR_LIST))))
815 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100816 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200818 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200820 return isntype;
821}
822
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200823 int
824check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
825{
826 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
827 return FAIL;
828 return OK;
829}
830
Bram Moolenaara5565e42020-05-09 15:44:01 +0200831/*
832 * Generate an ISN_COMPARE* instruction with a boolean result.
833 */
834 static int
835generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
836{
837 isntype_T isntype;
838 isn_T *isn;
839 garray_T *stack = &cctx->ctx_type_stack;
840 vartype_T type1;
841 vartype_T type2;
842
843 RETURN_OK_IF_SKIP(cctx);
844
845 // Get the known type of the two items on the stack. If they are matching
846 // use a type-specific instruction. Otherwise fall back to runtime type
847 // checking.
848 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
849 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
850 isntype = get_compare_isn(exptype, type1, type2);
851 if (isntype == ISN_DROP)
852 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853
854 if ((isn = generate_instr(cctx, isntype)) == NULL)
855 return FAIL;
856 isn->isn_arg.op.op_type = exptype;
857 isn->isn_arg.op.op_ic = ic;
858
859 // takes two arguments, puts one bool back
860 if (stack->ga_len >= 2)
861 {
862 --stack->ga_len;
863 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
864 }
865
866 return OK;
867}
868
869/*
870 * Generate an ISN_2BOOL instruction.
871 */
872 static int
873generate_2BOOL(cctx_T *cctx, int invert)
874{
875 isn_T *isn;
876 garray_T *stack = &cctx->ctx_type_stack;
877
Bram Moolenaar080457c2020-03-03 21:53:32 +0100878 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
880 return FAIL;
881 isn->isn_arg.number = invert;
882
883 // type becomes bool
884 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
885
886 return OK;
887}
888
889 static int
890generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
891{
892 isn_T *isn;
893 garray_T *stack = &cctx->ctx_type_stack;
894
Bram Moolenaar080457c2020-03-03 21:53:32 +0100895 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
897 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200898 // TODO: whole type, e.g. for a function also arg and return types
899 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 isn->isn_arg.type.ct_off = offset;
901
902 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200903 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904
905 return OK;
906}
907
908/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200909 * Check that
910 * - "actual" is "expected" type or
911 * - "actual" is a type that can be "expected" type: add a runtime check; or
912 * - return FAIL.
913 */
914 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200915need_type(
916 type_T *actual,
917 type_T *expected,
918 int offset,
919 cctx_T *cctx,
920 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200921{
922 if (check_type(expected, actual, FALSE) == OK)
923 return OK;
924 if (actual->tt_type != VAR_ANY
925 && actual->tt_type != VAR_UNKNOWN
926 && !(actual->tt_type == VAR_FUNC
927 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
928 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200929 if (!silent)
930 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200931 return FAIL;
932 }
933 generate_TYPECHECK(cctx, expected, offset);
934 return OK;
935}
936
937/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 * Generate an ISN_PUSHNR instruction.
939 */
940 static int
941generate_PUSHNR(cctx_T *cctx, varnumber_T number)
942{
943 isn_T *isn;
944
Bram Moolenaar080457c2020-03-03 21:53:32 +0100945 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
947 return FAIL;
948 isn->isn_arg.number = number;
949
950 return OK;
951}
952
953/*
954 * Generate an ISN_PUSHBOOL instruction.
955 */
956 static int
957generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
958{
959 isn_T *isn;
960
Bram Moolenaar080457c2020-03-03 21:53:32 +0100961 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
963 return FAIL;
964 isn->isn_arg.number = number;
965
966 return OK;
967}
968
969/*
970 * Generate an ISN_PUSHSPEC instruction.
971 */
972 static int
973generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
974{
975 isn_T *isn;
976
Bram Moolenaar080457c2020-03-03 21:53:32 +0100977 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
979 return FAIL;
980 isn->isn_arg.number = number;
981
982 return OK;
983}
984
985#ifdef FEAT_FLOAT
986/*
987 * Generate an ISN_PUSHF instruction.
988 */
989 static int
990generate_PUSHF(cctx_T *cctx, float_T fnumber)
991{
992 isn_T *isn;
993
Bram Moolenaar080457c2020-03-03 21:53:32 +0100994 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
996 return FAIL;
997 isn->isn_arg.fnumber = fnumber;
998
999 return OK;
1000}
1001#endif
1002
1003/*
1004 * Generate an ISN_PUSHS instruction.
1005 * Consumes "str".
1006 */
1007 static int
1008generate_PUSHS(cctx_T *cctx, char_u *str)
1009{
1010 isn_T *isn;
1011
Bram Moolenaar080457c2020-03-03 21:53:32 +01001012 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1014 return FAIL;
1015 isn->isn_arg.string = str;
1016
1017 return OK;
1018}
1019
1020/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001021 * Generate an ISN_PUSHCHANNEL instruction.
1022 * Consumes "channel".
1023 */
1024 static int
1025generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1026{
1027 isn_T *isn;
1028
Bram Moolenaar080457c2020-03-03 21:53:32 +01001029 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001030 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1031 return FAIL;
1032 isn->isn_arg.channel = channel;
1033
1034 return OK;
1035}
1036
1037/*
1038 * Generate an ISN_PUSHJOB instruction.
1039 * Consumes "job".
1040 */
1041 static int
1042generate_PUSHJOB(cctx_T *cctx, job_T *job)
1043{
1044 isn_T *isn;
1045
Bram Moolenaar080457c2020-03-03 21:53:32 +01001046 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001047 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001048 return FAIL;
1049 isn->isn_arg.job = job;
1050
1051 return OK;
1052}
1053
1054/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 * Generate an ISN_PUSHBLOB instruction.
1056 * Consumes "blob".
1057 */
1058 static int
1059generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1060{
1061 isn_T *isn;
1062
Bram Moolenaar080457c2020-03-03 21:53:32 +01001063 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1065 return FAIL;
1066 isn->isn_arg.blob = blob;
1067
1068 return OK;
1069}
1070
1071/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001072 * Generate an ISN_PUSHFUNC instruction with name "name".
1073 * Consumes "name".
1074 */
1075 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001076generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001077{
1078 isn_T *isn;
1079
Bram Moolenaar080457c2020-03-03 21:53:32 +01001080 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001081 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001082 return FAIL;
1083 isn->isn_arg.string = name;
1084
1085 return OK;
1086}
1087
1088/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001089 * Generate an ISN_GETITEM instruction with "index".
1090 */
1091 static int
1092generate_GETITEM(cctx_T *cctx, int index)
1093{
1094 isn_T *isn;
1095 garray_T *stack = &cctx->ctx_type_stack;
1096 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1097 type_T *item_type = &t_any;
1098
1099 RETURN_OK_IF_SKIP(cctx);
1100
1101 if (type->tt_type == VAR_LIST)
1102 item_type = type->tt_member;
1103 else if (type->tt_type != VAR_ANY)
1104 {
1105 emsg(_(e_listreq));
1106 return FAIL;
1107 }
1108 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1109 return FAIL;
1110 isn->isn_arg.number = index;
1111
1112 // add the item type to the type stack
1113 if (ga_grow(stack, 1) == FAIL)
1114 return FAIL;
1115 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1116 ++stack->ga_len;
1117 return OK;
1118}
1119
1120/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001121 * Generate an ISN_SLICE instruction with "count".
1122 */
1123 static int
1124generate_SLICE(cctx_T *cctx, int count)
1125{
1126 isn_T *isn;
1127
1128 RETURN_OK_IF_SKIP(cctx);
1129 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1130 return FAIL;
1131 isn->isn_arg.number = count;
1132 return OK;
1133}
1134
1135/*
1136 * Generate an ISN_CHECKLEN instruction with "min_len".
1137 */
1138 static int
1139generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1140{
1141 isn_T *isn;
1142
1143 RETURN_OK_IF_SKIP(cctx);
1144
1145 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1146 return FAIL;
1147 isn->isn_arg.checklen.cl_min_len = min_len;
1148 isn->isn_arg.checklen.cl_more_OK = more_OK;
1149
1150 return OK;
1151}
1152
1153/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 * Generate an ISN_STORE instruction.
1155 */
1156 static int
1157generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1158{
1159 isn_T *isn;
1160
Bram Moolenaar080457c2020-03-03 21:53:32 +01001161 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1163 return FAIL;
1164 if (name != NULL)
1165 isn->isn_arg.string = vim_strsave(name);
1166 else
1167 isn->isn_arg.number = idx;
1168
1169 return OK;
1170}
1171
1172/*
1173 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1174 */
1175 static int
1176generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1177{
1178 isn_T *isn;
1179
Bram Moolenaar080457c2020-03-03 21:53:32 +01001180 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1182 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001183 isn->isn_arg.storenr.stnr_idx = idx;
1184 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185
1186 return OK;
1187}
1188
1189/*
1190 * Generate an ISN_STOREOPT instruction
1191 */
1192 static int
1193generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
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(cctx, ISN_STOREOPT)) == NULL)
1199 return FAIL;
1200 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1201 isn->isn_arg.storeopt.so_flags = opt_flags;
1202
1203 return OK;
1204}
1205
1206/*
1207 * Generate an ISN_LOAD or similar instruction.
1208 */
1209 static int
1210generate_LOAD(
1211 cctx_T *cctx,
1212 isntype_T isn_type,
1213 int idx,
1214 char_u *name,
1215 type_T *type)
1216{
1217 isn_T *isn;
1218
Bram Moolenaar080457c2020-03-03 21:53:32 +01001219 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1221 return FAIL;
1222 if (name != NULL)
1223 isn->isn_arg.string = vim_strsave(name);
1224 else
1225 isn->isn_arg.number = idx;
1226
1227 return OK;
1228}
1229
1230/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001231 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001232 */
1233 static int
1234generate_LOADV(
1235 cctx_T *cctx,
1236 char_u *name,
1237 int error)
1238{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001239 int di_flags;
1240 int vidx = find_vim_var(name, &di_flags);
1241 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001242
Bram Moolenaar080457c2020-03-03 21:53:32 +01001243 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001244 if (vidx < 0)
1245 {
1246 if (error)
1247 semsg(_(e_var_notfound), name);
1248 return FAIL;
1249 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001250 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001251
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001252 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001253}
1254
1255/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001256 * Generate an ISN_UNLET instruction.
1257 */
1258 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001259generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001260{
1261 isn_T *isn;
1262
1263 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001264 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001265 return FAIL;
1266 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1267 isn->isn_arg.unlet.ul_forceit = forceit;
1268
1269 return OK;
1270}
1271
1272/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 * Generate an ISN_LOADS instruction.
1274 */
1275 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001276generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001278 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280 int sid,
1281 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282{
1283 isn_T *isn;
1284
Bram Moolenaar080457c2020-03-03 21:53:32 +01001285 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286 if (isn_type == ISN_LOADS)
1287 isn = generate_instr_type(cctx, isn_type, type);
1288 else
1289 isn = generate_instr_drop(cctx, isn_type, 1);
1290 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001292 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1293 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001294
1295 return OK;
1296}
1297
1298/*
1299 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1300 */
1301 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001302generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 cctx_T *cctx,
1304 isntype_T isn_type,
1305 int sid,
1306 int idx,
1307 type_T *type)
1308{
1309 isn_T *isn;
1310
Bram Moolenaar080457c2020-03-03 21:53:32 +01001311 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 if (isn_type == ISN_LOADSCRIPT)
1313 isn = generate_instr_type(cctx, isn_type, type);
1314 else
1315 isn = generate_instr_drop(cctx, isn_type, 1);
1316 if (isn == NULL)
1317 return FAIL;
1318 isn->isn_arg.script.script_sid = sid;
1319 isn->isn_arg.script.script_idx = idx;
1320 return OK;
1321}
1322
1323/*
1324 * Generate an ISN_NEWLIST instruction.
1325 */
1326 static int
1327generate_NEWLIST(cctx_T *cctx, int count)
1328{
1329 isn_T *isn;
1330 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 type_T *type;
1332 type_T *member;
1333
Bram Moolenaar080457c2020-03-03 21:53:32 +01001334 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1336 return FAIL;
1337 isn->isn_arg.number = count;
1338
1339 // drop the value types
1340 stack->ga_len -= count;
1341
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001342 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001343 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if (count > 0)
1345 member = ((type_T **)stack->ga_data)[stack->ga_len];
1346 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001347 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001348 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349
1350 // add the list type to the type stack
1351 if (ga_grow(stack, 1) == FAIL)
1352 return FAIL;
1353 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1354 ++stack->ga_len;
1355
1356 return OK;
1357}
1358
1359/*
1360 * Generate an ISN_NEWDICT instruction.
1361 */
1362 static int
1363generate_NEWDICT(cctx_T *cctx, int count)
1364{
1365 isn_T *isn;
1366 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 type_T *type;
1368 type_T *member;
1369
Bram Moolenaar080457c2020-03-03 21:53:32 +01001370 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1372 return FAIL;
1373 isn->isn_arg.number = count;
1374
1375 // drop the key and value types
1376 stack->ga_len -= 2 * count;
1377
Bram Moolenaar436472f2020-02-20 22:54:43 +01001378 // Use the first value type for the list member type. Use "void" for an
1379 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 if (count > 0)
1381 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1382 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001383 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001384 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385
1386 // add the dict type to the type stack
1387 if (ga_grow(stack, 1) == FAIL)
1388 return FAIL;
1389 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1390 ++stack->ga_len;
1391
1392 return OK;
1393}
1394
1395/*
1396 * Generate an ISN_FUNCREF instruction.
1397 */
1398 static int
1399generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1400{
1401 isn_T *isn;
1402 garray_T *stack = &cctx->ctx_type_stack;
1403
Bram Moolenaar080457c2020-03-03 21:53:32 +01001404 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1406 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001407 isn->isn_arg.funcref.fr_func = dfunc_idx;
1408 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409
1410 if (ga_grow(stack, 1) == FAIL)
1411 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001412 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 // TODO: argument and return types
1414 ++stack->ga_len;
1415
1416 return OK;
1417}
1418
1419/*
1420 * Generate an ISN_JUMP instruction.
1421 */
1422 static int
1423generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1424{
1425 isn_T *isn;
1426 garray_T *stack = &cctx->ctx_type_stack;
1427
Bram Moolenaar080457c2020-03-03 21:53:32 +01001428 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1430 return FAIL;
1431 isn->isn_arg.jump.jump_when = when;
1432 isn->isn_arg.jump.jump_where = where;
1433
1434 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1435 --stack->ga_len;
1436
1437 return OK;
1438}
1439
1440 static int
1441generate_FOR(cctx_T *cctx, int loop_idx)
1442{
1443 isn_T *isn;
1444 garray_T *stack = &cctx->ctx_type_stack;
1445
Bram Moolenaar080457c2020-03-03 21:53:32 +01001446 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1448 return FAIL;
1449 isn->isn_arg.forloop.for_idx = loop_idx;
1450
1451 if (ga_grow(stack, 1) == FAIL)
1452 return FAIL;
1453 // type doesn't matter, will be stored next
1454 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1455 ++stack->ga_len;
1456
1457 return OK;
1458}
1459
1460/*
1461 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001462 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 * Return FAIL if the number of arguments is wrong.
1464 */
1465 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001466generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467{
1468 isn_T *isn;
1469 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001470 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001471 type_T *argtypes[MAX_FUNC_ARGS];
1472 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473
Bram Moolenaar080457c2020-03-03 21:53:32 +01001474 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001475 argoff = check_internal_func(func_idx, argcount);
1476 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477 return FAIL;
1478
Bram Moolenaar389df252020-07-09 21:20:47 +02001479 if (method_call && argoff > 1)
1480 {
1481 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1482 return FAIL;
1483 isn->isn_arg.shuffle.shfl_item = argcount;
1484 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1485 }
1486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1488 return FAIL;
1489 isn->isn_arg.bfunc.cbf_idx = func_idx;
1490 isn->isn_arg.bfunc.cbf_argcount = argcount;
1491
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001492 for (i = 0; i < argcount; ++i)
1493 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 stack->ga_len -= argcount; // drop the arguments
1496 if (ga_grow(stack, 1) == FAIL)
1497 return FAIL;
1498 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001499 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 ++stack->ga_len; // add return value
1501
1502 return OK;
1503}
1504
1505/*
1506 * Generate an ISN_DCALL or ISN_UCALL instruction.
1507 * Return FAIL if the number of arguments is wrong.
1508 */
1509 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001510generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511{
1512 isn_T *isn;
1513 garray_T *stack = &cctx->ctx_type_stack;
1514 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001515 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516
Bram Moolenaar080457c2020-03-03 21:53:32 +01001517 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 if (argcount > regular_args && !has_varargs(ufunc))
1519 {
1520 semsg(_(e_toomanyarg), ufunc->uf_name);
1521 return FAIL;
1522 }
1523 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1524 {
1525 semsg(_(e_toofewarg), ufunc->uf_name);
1526 return FAIL;
1527 }
1528
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001529 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001530 {
1531 int i;
1532
1533 for (i = 0; i < argcount; ++i)
1534 {
1535 type_T *expected;
1536 type_T *actual;
1537
1538 if (i < regular_args)
1539 {
1540 if (ufunc->uf_arg_types == NULL)
1541 continue;
1542 expected = ufunc->uf_arg_types[i];
1543 }
1544 else
1545 expected = ufunc->uf_va_type->tt_member;
1546 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001547 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001548 {
1549 arg_type_mismatch(expected, actual, i + 1);
1550 return FAIL;
1551 }
1552 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001553 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001554 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001555 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001556 }
1557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001559 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001560 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001562 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 {
1564 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1565 isn->isn_arg.dfunc.cdf_argcount = argcount;
1566 }
1567 else
1568 {
1569 // A user function may be deleted and redefined later, can't use the
1570 // ufunc pointer, need to look it up again at runtime.
1571 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1572 isn->isn_arg.ufunc.cuf_argcount = argcount;
1573 }
1574
1575 stack->ga_len -= argcount; // drop the arguments
1576 if (ga_grow(stack, 1) == FAIL)
1577 return FAIL;
1578 // add return value
1579 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1580 ++stack->ga_len;
1581
1582 return OK;
1583}
1584
1585/*
1586 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1587 */
1588 static int
1589generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1590{
1591 isn_T *isn;
1592 garray_T *stack = &cctx->ctx_type_stack;
1593
Bram Moolenaar080457c2020-03-03 21:53:32 +01001594 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1596 return FAIL;
1597 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1598 isn->isn_arg.ufunc.cuf_argcount = argcount;
1599
1600 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001601 if (ga_grow(stack, 1) == FAIL)
1602 return FAIL;
1603 // add return value
1604 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1605 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606
1607 return OK;
1608}
1609
1610/*
1611 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001612 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 */
1614 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001615generate_PCALL(
1616 cctx_T *cctx,
1617 int argcount,
1618 char_u *name,
1619 type_T *type,
1620 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621{
1622 isn_T *isn;
1623 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001624 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625
Bram Moolenaar080457c2020-03-03 21:53:32 +01001626 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001627
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001628 if (type->tt_type == VAR_ANY)
1629 ret_type = &t_any;
1630 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001631 {
1632 if (type->tt_argcount != -1)
1633 {
1634 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1635
1636 if (argcount < type->tt_min_argcount - varargs)
1637 {
1638 semsg(_(e_toofewarg), "[reference]");
1639 return FAIL;
1640 }
1641 if (!varargs && argcount > type->tt_argcount)
1642 {
1643 semsg(_(e_toomanyarg), "[reference]");
1644 return FAIL;
1645 }
1646 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001647 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001648 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001649 else
1650 {
1651 semsg(_("E1085: Not a callable type: %s"), name);
1652 return FAIL;
1653 }
1654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1656 return FAIL;
1657 isn->isn_arg.pfunc.cpf_top = at_top;
1658 isn->isn_arg.pfunc.cpf_argcount = argcount;
1659
1660 stack->ga_len -= argcount; // drop the arguments
1661
1662 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001663 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001665 // If partial is above the arguments it must be cleared and replaced with
1666 // the return value.
1667 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1668 return FAIL;
1669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 return OK;
1671}
1672
1673/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001674 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 */
1676 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001677generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678{
1679 isn_T *isn;
1680 garray_T *stack = &cctx->ctx_type_stack;
1681 type_T *type;
1682
Bram Moolenaar080457c2020-03-03 21:53:32 +01001683 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001684 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001686 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001688 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001690 if (type->tt_type != VAR_DICT && type != &t_any)
1691 {
1692 emsg(_(e_dictreq));
1693 return FAIL;
1694 }
1695 // change dict type to dict member type
1696 if (type->tt_type == VAR_DICT)
1697 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698
1699 return OK;
1700}
1701
1702/*
1703 * Generate an ISN_ECHO instruction.
1704 */
1705 static int
1706generate_ECHO(cctx_T *cctx, int with_white, int count)
1707{
1708 isn_T *isn;
1709
Bram Moolenaar080457c2020-03-03 21:53:32 +01001710 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1712 return FAIL;
1713 isn->isn_arg.echo.echo_with_white = with_white;
1714 isn->isn_arg.echo.echo_count = count;
1715
1716 return OK;
1717}
1718
Bram Moolenaarad39c092020-02-26 18:23:43 +01001719/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001720 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001721 */
1722 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001723generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001724{
1725 isn_T *isn;
1726
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001727 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001728 return FAIL;
1729 isn->isn_arg.number = count;
1730
1731 return OK;
1732}
1733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 static int
1735generate_EXEC(cctx_T *cctx, char_u *line)
1736{
1737 isn_T *isn;
1738
Bram Moolenaar080457c2020-03-03 21:53:32 +01001739 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1741 return FAIL;
1742 isn->isn_arg.string = vim_strsave(line);
1743 return OK;
1744}
1745
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001746 static int
1747generate_EXECCONCAT(cctx_T *cctx, int count)
1748{
1749 isn_T *isn;
1750
1751 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1752 return FAIL;
1753 isn->isn_arg.number = count;
1754 return OK;
1755}
1756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757/*
1758 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001759 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001761 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1763{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 lvar_T *lvar;
1765
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001766 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001768 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001769 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 }
1771
1772 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001773 return NULL;
1774 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001776 // Every local variable uses the next entry on the stack. We could re-use
1777 // the last ones when leaving a scope, but then variables used in a closure
1778 // might get overwritten. To keep things simple do not re-use stack
1779 // entries. This is less efficient, but memory is cheap these days.
1780 lvar->lv_idx = cctx->ctx_locals_count++;
1781
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001782 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 lvar->lv_const = isConst;
1784 lvar->lv_type = type;
1785
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001786 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787}
1788
1789/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001790 * Remove local variables above "new_top".
1791 */
1792 static void
1793unwind_locals(cctx_T *cctx, int new_top)
1794{
1795 if (cctx->ctx_locals.ga_len > new_top)
1796 {
1797 int idx;
1798 lvar_T *lvar;
1799
1800 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1801 {
1802 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1803 vim_free(lvar->lv_name);
1804 }
1805 }
1806 cctx->ctx_locals.ga_len = new_top;
1807}
1808
1809/*
1810 * Free all local variables.
1811 */
1812 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001813free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001814{
1815 unwind_locals(cctx, 0);
1816 ga_clear(&cctx->ctx_locals);
1817}
1818
1819/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 * Skip over a type definition and return a pointer to just after it.
1821 */
1822 char_u *
1823skip_type(char_u *start)
1824{
1825 char_u *p = start;
1826
1827 while (ASCII_ISALNUM(*p) || *p == '_')
1828 ++p;
1829
1830 // Skip over "<type>"; this is permissive about white space.
1831 if (*skipwhite(p) == '<')
1832 {
1833 p = skipwhite(p);
1834 p = skip_type(skipwhite(p + 1));
1835 p = skipwhite(p);
1836 if (*p == '>')
1837 ++p;
1838 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001839 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1840 {
1841 // handle func(args): type
1842 ++p;
1843 while (*p != ')' && *p != NUL)
1844 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001845 char_u *sp = p;
1846
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001847 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001848 if (p == sp)
1849 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001850 if (*p == ',')
1851 p = skipwhite(p + 1);
1852 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001853 if (*p == ')')
1854 {
1855 if (p[1] == ':')
1856 p = skip_type(skipwhite(p + 2));
1857 else
1858 p = skipwhite(p + 1);
1859 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001860 }
1861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 return p;
1863}
1864
1865/*
1866 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001867 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 * Returns NULL in case of failure.
1869 */
1870 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001871parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872{
1873 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001874 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875
1876 if (**arg != '<')
1877 {
1878 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001879 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 else
1881 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001882 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 }
1884 *arg = skipwhite(*arg + 1);
1885
Bram Moolenaard77a8522020-04-03 21:59:57 +02001886 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887
1888 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001889 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 {
1891 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001892 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 }
1894 ++*arg;
1895
1896 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001897 return get_list_type(member_type, type_gap);
1898 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899}
1900
1901/*
1902 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001903 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 */
1905 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001906parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907{
1908 char_u *p = *arg;
1909 size_t len;
1910
1911 // skip over the first word
1912 while (ASCII_ISALNUM(*p) || *p == '_')
1913 ++p;
1914 len = p - *arg;
1915
1916 switch (**arg)
1917 {
1918 case 'a':
1919 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1920 {
1921 *arg += len;
1922 return &t_any;
1923 }
1924 break;
1925 case 'b':
1926 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1927 {
1928 *arg += len;
1929 return &t_bool;
1930 }
1931 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1932 {
1933 *arg += len;
1934 return &t_blob;
1935 }
1936 break;
1937 case 'c':
1938 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1939 {
1940 *arg += len;
1941 return &t_channel;
1942 }
1943 break;
1944 case 'd':
1945 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1946 {
1947 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001948 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 }
1950 break;
1951 case 'f':
1952 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1953 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001954#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 *arg += len;
1956 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001957#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001958 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001959 return &t_any;
1960#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 }
1962 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1963 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001964 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001965 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001966 int argcount = -1;
1967 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001968 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001969 type_T *arg_type[MAX_FUNC_ARGS + 1];
1970
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001971 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001973 if (**arg == '(')
1974 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001975 // "func" may or may not return a value, "func()" does
1976 // not return a value.
1977 ret_type = &t_void;
1978
Bram Moolenaard77a8522020-04-03 21:59:57 +02001979 p = ++*arg;
1980 argcount = 0;
1981 while (*p != NUL && *p != ')')
1982 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001983 if (*p == '?')
1984 {
1985 if (first_optional == -1)
1986 first_optional = argcount;
1987 ++p;
1988 }
1989 else if (first_optional != -1)
1990 {
1991 emsg(_("E1007: mandatory argument after optional argument"));
1992 return &t_any;
1993 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001994 else if (STRNCMP(p, "...", 3) == 0)
1995 {
1996 flags |= TTFLAG_VARARGS;
1997 p += 3;
1998 }
1999
2000 arg_type[argcount++] = parse_type(&p, type_gap);
2001
2002 // Nothing comes after "...{type}".
2003 if (flags & TTFLAG_VARARGS)
2004 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002005
Bram Moolenaard77a8522020-04-03 21:59:57 +02002006 if (*p != ',' && *skipwhite(p) == ',')
2007 {
2008 semsg(_(e_no_white_before), ",");
2009 return &t_any;
2010 }
2011 if (*p == ',')
2012 {
2013 ++p;
2014 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002015 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002016 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002017 return &t_any;
2018 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002019 }
2020 p = skipwhite(p);
2021 if (argcount == MAX_FUNC_ARGS)
2022 {
2023 emsg(_("E740: Too many argument types"));
2024 return &t_any;
2025 }
2026 }
2027
2028 p = skipwhite(p);
2029 if (*p != ')')
2030 {
2031 emsg(_(e_missing_close));
2032 return &t_any;
2033 }
2034 *arg = p + 1;
2035 }
2036 if (**arg == ':')
2037 {
2038 // parse return type
2039 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002040 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002041 semsg(_(e_white_after), ":");
2042 *arg = skipwhite(*arg);
2043 ret_type = parse_type(arg, type_gap);
2044 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002045 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002046 type = get_func_type(ret_type, argcount, type_gap);
2047 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002048 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002049 type = alloc_func_type(ret_type, argcount, type_gap);
2050 type->tt_flags = flags;
2051 if (argcount > 0)
2052 {
2053 type->tt_argcount = argcount;
2054 type->tt_min_argcount = first_optional == -1
2055 ? argcount : first_optional;
2056 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002057 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002058 return &t_any;
2059 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002060 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002061 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002062 }
2063 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 }
2065 break;
2066 case 'j':
2067 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2068 {
2069 *arg += len;
2070 return &t_job;
2071 }
2072 break;
2073 case 'l':
2074 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2075 {
2076 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002077 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 }
2079 break;
2080 case 'n':
2081 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2082 {
2083 *arg += len;
2084 return &t_number;
2085 }
2086 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 case 's':
2088 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2089 {
2090 *arg += len;
2091 return &t_string;
2092 }
2093 break;
2094 case 'v':
2095 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2096 {
2097 *arg += len;
2098 return &t_void;
2099 }
2100 break;
2101 }
2102
2103 semsg(_("E1010: Type not recognized: %s"), *arg);
2104 return &t_any;
2105}
2106
2107/*
2108 * Check if "type1" and "type2" are exactly the same.
2109 */
2110 static int
2111equal_type(type_T *type1, type_T *type2)
2112{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002113 int i;
2114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 if (type1->tt_type != type2->tt_type)
2116 return FALSE;
2117 switch (type1->tt_type)
2118 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002120 case VAR_ANY:
2121 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 case VAR_SPECIAL:
2123 case VAR_BOOL:
2124 case VAR_NUMBER:
2125 case VAR_FLOAT:
2126 case VAR_STRING:
2127 case VAR_BLOB:
2128 case VAR_JOB:
2129 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002130 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 case VAR_LIST:
2132 case VAR_DICT:
2133 return equal_type(type1->tt_member, type2->tt_member);
2134 case VAR_FUNC:
2135 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002136 if (!equal_type(type1->tt_member, type2->tt_member)
2137 || type1->tt_argcount != type2->tt_argcount)
2138 return FALSE;
2139 if (type1->tt_argcount < 0
2140 || type1->tt_args == NULL || type2->tt_args == NULL)
2141 return TRUE;
2142 for (i = 0; i < type1->tt_argcount; ++i)
2143 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2144 return FALSE;
2145 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 }
2147 return TRUE;
2148}
2149
2150/*
2151 * Find the common type of "type1" and "type2" and put it in "dest".
2152 * "type2" and "dest" may be the same.
2153 */
2154 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002155common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156{
2157 if (equal_type(type1, type2))
2158 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002159 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 return;
2161 }
2162
2163 if (type1->tt_type == type2->tt_type)
2164 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2166 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002167 type_T *common;
2168
Bram Moolenaard77a8522020-04-03 21:59:57 +02002169 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002170 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002171 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002172 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002173 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 return;
2175 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002176 if (type1->tt_type == VAR_FUNC)
2177 {
2178 type_T *common;
2179
2180 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2181 if (type1->tt_argcount == type2->tt_argcount
2182 && type1->tt_argcount >= 0)
2183 {
2184 int argcount = type1->tt_argcount;
2185 int i;
2186
2187 *dest = alloc_func_type(common, argcount, type_gap);
2188 if (type1->tt_args != NULL && type2->tt_args != NULL)
2189 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002190 if (func_type_add_arg_types(*dest, argcount,
2191 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002192 for (i = 0; i < argcount; ++i)
2193 common_type(type1->tt_args[i], type2->tt_args[i],
2194 &(*dest)->tt_args[i], type_gap);
2195 }
2196 }
2197 else
2198 *dest = alloc_func_type(common, -1, type_gap);
2199 return;
2200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 }
2202
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002203 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204}
2205
2206 char *
2207vartype_name(vartype_T type)
2208{
2209 switch (type)
2210 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002211 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002212 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 case VAR_SPECIAL: return "special";
2215 case VAR_BOOL: return "bool";
2216 case VAR_NUMBER: return "number";
2217 case VAR_FLOAT: return "float";
2218 case VAR_STRING: return "string";
2219 case VAR_BLOB: return "blob";
2220 case VAR_JOB: return "job";
2221 case VAR_CHANNEL: return "channel";
2222 case VAR_LIST: return "list";
2223 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002224
2225 case VAR_FUNC:
2226 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002228 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229}
2230
2231/*
2232 * Return the name of a type.
2233 * The result may be in allocated memory, in which case "tofree" is set.
2234 */
2235 char *
2236type_name(type_T *type, char **tofree)
2237{
2238 char *name = vartype_name(type->tt_type);
2239
2240 *tofree = NULL;
2241 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2242 {
2243 char *member_free;
2244 char *member_name = type_name(type->tt_member, &member_free);
2245 size_t len;
2246
2247 len = STRLEN(name) + STRLEN(member_name) + 3;
2248 *tofree = alloc(len);
2249 if (*tofree != NULL)
2250 {
2251 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2252 vim_free(member_free);
2253 return *tofree;
2254 }
2255 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002256 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002257 {
2258 garray_T ga;
2259 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002260 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002261
2262 ga_init2(&ga, 1, 100);
2263 if (ga_grow(&ga, 20) == FAIL)
2264 return "[unknown]";
2265 *tofree = ga.ga_data;
2266 STRCPY(ga.ga_data, "func(");
2267 ga.ga_len += 5;
2268
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002269 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002270 {
2271 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002272 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002273 int len;
2274
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002275 if (type->tt_args == NULL)
2276 arg_type = "[unknown]";
2277 else
2278 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002279 if (i > 0)
2280 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002281 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002282 ga.ga_len += 2;
2283 }
2284 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002285 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002286 {
2287 vim_free(arg_free);
2288 return "[unknown]";
2289 }
2290 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002291 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002292 {
2293 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2294 ga.ga_len += 3;
2295 }
2296 else if (i >= type->tt_min_argcount)
2297 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002298 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002299 ga.ga_len += len;
2300 vim_free(arg_free);
2301 }
2302
2303 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002304 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002305 else
2306 {
2307 char *ret_free;
2308 char *ret_name = type_name(type->tt_member, &ret_free);
2309 int len;
2310
2311 len = (int)STRLEN(ret_name) + 4;
2312 if (ga_grow(&ga, len) == FAIL)
2313 {
2314 vim_free(ret_free);
2315 return "[unknown]";
2316 }
2317 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002318 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2319 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002320 vim_free(ret_free);
2321 }
2322 return ga.ga_data;
2323 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324
2325 return name;
2326}
2327
2328/*
2329 * Find "name" in script-local items of script "sid".
2330 * Returns the index in "sn_var_vals" if found.
2331 * If found but not in "sn_var_vals" returns -1.
2332 * If not found returns -2.
2333 */
2334 int
2335get_script_item_idx(int sid, char_u *name, int check_writable)
2336{
2337 hashtab_T *ht;
2338 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002339 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 int idx;
2341
2342 // First look the name up in the hashtable.
2343 if (sid <= 0 || sid > script_items.ga_len)
2344 return -1;
2345 ht = &SCRIPT_VARS(sid);
2346 di = find_var_in_ht(ht, 0, name, TRUE);
2347 if (di == NULL)
2348 return -2;
2349
2350 // Now find the svar_T index in sn_var_vals.
2351 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2352 {
2353 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2354
2355 if (sv->sv_tv == &di->di_tv)
2356 {
2357 if (check_writable && sv->sv_const)
2358 semsg(_(e_readonlyvar), name);
2359 return idx;
2360 }
2361 }
2362 return -1;
2363}
2364
2365/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002366 * Find "name" in imported items of the current script or in "cctx" if not
2367 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 */
2369 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002370find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002372 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 int idx;
2374
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002375 if (current_sctx.sc_sid <= 0)
2376 return NULL;
2377 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 if (cctx != NULL)
2379 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2380 {
2381 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2382 + idx;
2383
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002384 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2385 : STRLEN(import->imp_name) == len
2386 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 return import;
2388 }
2389
2390 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2391 {
2392 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2393
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002394 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2395 : STRLEN(import->imp_name) == len
2396 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 return import;
2398 }
2399 return NULL;
2400}
2401
2402/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002403 * Free all imported variables.
2404 */
2405 static void
2406free_imported(cctx_T *cctx)
2407{
2408 int idx;
2409
2410 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2411 {
2412 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2413
2414 vim_free(import->imp_name);
2415 }
2416 ga_clear(&cctx->ctx_imports);
2417}
2418
2419/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002420 * Return TRUE if "p" points at a "#" but not at "#{".
2421 */
2422 static int
2423comment_start(char_u *p)
2424{
2425 return p[0] == '#' && p[1] != '{';
2426}
2427
2428/*
2429 * Return a pointer to the next line that isn't empty or only contains a
2430 * comment. Skips over white space.
2431 * Returns NULL if there is none.
2432 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002433 char_u *
2434peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002435{
2436 int lnum = cctx->ctx_lnum;
2437
2438 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2439 {
2440 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002441 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002442
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002443 if (line == NULL)
2444 break;
2445 p = skipwhite(line);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002446 if (*p != NUL && !comment_start(p))
2447 return p;
2448 }
2449 return NULL;
2450}
2451
2452/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002453 * Called when checking for a following operator at "arg". When the rest of
2454 * the line is empty or only a comment, peek the next line. If there is a next
2455 * line return a pointer to it and set "nextp".
2456 * Otherwise skip over white space.
2457 */
2458 static char_u *
2459may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2460{
2461 char_u *p = skipwhite(arg);
2462
2463 *nextp = NULL;
2464 if (*p == NUL || (VIM_ISWHITE(*arg) && comment_start(p)))
2465 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002466 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002467 if (*nextp != NULL)
2468 return *nextp;
2469 }
2470 return p;
2471}
2472
2473/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002474 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002475 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002476 * Returns NULL when at the end.
2477 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002478 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002479next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002480{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002481 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002482
2483 do
2484 {
2485 ++cctx->ctx_lnum;
2486 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002487 {
2488 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002489 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002490 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002491 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002492 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002493 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002494 } while (line == NULL || *skipwhite(line) == NUL
2495 || (skip_comment && comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002496 return line;
2497}
2498
2499/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002500 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002501 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002502 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2503 */
2504 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002505may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002506{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002507 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002508 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002509 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002510
2511 if (next == NULL)
2512 return FAIL;
2513 *arg = skipwhite(next);
2514 }
2515 return OK;
2516}
2517
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002518/*
2519 * Idem, and give an error when failed.
2520 */
2521 static int
2522may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2523{
2524 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2525 {
2526 emsg(_("E1097: line incomplete"));
2527 return FAIL;
2528 }
2529 return OK;
2530}
2531
2532
Bram Moolenaara5565e42020-05-09 15:44:01 +02002533// Structure passed between the compile_expr* functions to keep track of
2534// constants that have been parsed but for which no code was produced yet. If
2535// possible expressions on these constants are applied at compile time. If
2536// that is not possible, the code to push the constants needs to be generated
2537// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002538// Using 50 should be more than enough of 5 levels of ().
2539#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002540typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002541 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002542 int pp_used; // active entries in pp_tv[]
2543} ppconst_T;
2544
Bram Moolenaar1c747212020-05-09 18:28:34 +02002545static int compile_expr0(char_u **arg, cctx_T *cctx);
2546static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2547
Bram Moolenaara5565e42020-05-09 15:44:01 +02002548/*
2549 * Generate a PUSH instruction for "tv".
2550 * "tv" will be consumed or cleared.
2551 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2552 */
2553 static int
2554generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2555{
2556 if (tv != NULL)
2557 {
2558 switch (tv->v_type)
2559 {
2560 case VAR_UNKNOWN:
2561 break;
2562 case VAR_BOOL:
2563 generate_PUSHBOOL(cctx, tv->vval.v_number);
2564 break;
2565 case VAR_SPECIAL:
2566 generate_PUSHSPEC(cctx, tv->vval.v_number);
2567 break;
2568 case VAR_NUMBER:
2569 generate_PUSHNR(cctx, tv->vval.v_number);
2570 break;
2571#ifdef FEAT_FLOAT
2572 case VAR_FLOAT:
2573 generate_PUSHF(cctx, tv->vval.v_float);
2574 break;
2575#endif
2576 case VAR_BLOB:
2577 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2578 tv->vval.v_blob = NULL;
2579 break;
2580 case VAR_STRING:
2581 generate_PUSHS(cctx, tv->vval.v_string);
2582 tv->vval.v_string = NULL;
2583 break;
2584 default:
2585 iemsg("constant type not supported");
2586 clear_tv(tv);
2587 return FAIL;
2588 }
2589 tv->v_type = VAR_UNKNOWN;
2590 }
2591 return OK;
2592}
2593
2594/*
2595 * Generate code for any ppconst entries.
2596 */
2597 static int
2598generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2599{
2600 int i;
2601 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002602 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002603
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002604 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002605 for (i = 0; i < ppconst->pp_used; ++i)
2606 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2607 ret = FAIL;
2608 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002609 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002610 return ret;
2611}
2612
2613/*
2614 * Clear ppconst constants. Used when failing.
2615 */
2616 static void
2617clear_ppconst(ppconst_T *ppconst)
2618{
2619 int i;
2620
2621 for (i = 0; i < ppconst->pp_used; ++i)
2622 clear_tv(&ppconst->pp_tv[i]);
2623 ppconst->pp_used = 0;
2624}
2625
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002626/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002627 * Generate an instruction to load script-local variable "name", without the
2628 * leading "s:".
2629 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 */
2631 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002632compile_load_scriptvar(
2633 cctx_T *cctx,
2634 char_u *name, // variable NUL terminated
2635 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002636 char_u **end, // end of variable
2637 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002639 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2641 imported_T *import;
2642
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002643 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002645 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002646 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2647 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 }
2649 if (idx >= 0)
2650 {
2651 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2652
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002653 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 current_sctx.sc_sid, idx, sv->sv_type);
2655 return OK;
2656 }
2657
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002658 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 if (import != NULL)
2660 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002661 if (import->imp_all)
2662 {
2663 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002664 char_u *exp_name;
2665 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002666 ufunc_T *ufunc;
2667 type_T *type;
2668
2669 // Used "import * as Name", need to lookup the member.
2670 if (*p != '.')
2671 {
2672 semsg(_("E1060: expected dot after name: %s"), start);
2673 return FAIL;
2674 }
2675 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002676 if (VIM_ISWHITE(*p))
2677 {
2678 emsg(_("E1074: no white space allowed after dot"));
2679 return FAIL;
2680 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002681
Bram Moolenaar1c991142020-07-04 13:15:31 +02002682 // isolate one name
2683 exp_name = p;
2684 while (eval_isnamec(*p))
2685 ++p;
2686 cc = *p;
2687 *p = NUL;
2688
2689 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2690 *p = cc;
2691 p = skipwhite(p);
2692
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002693 // TODO: what if it is a function?
2694 if (idx < 0)
2695 return FAIL;
2696 *end = p;
2697
2698 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2699 import->imp_sid,
2700 idx,
2701 type);
2702 }
2703 else
2704 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002705 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002706 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2707 import->imp_sid,
2708 import->imp_var_vals_idx,
2709 import->imp_type);
2710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 return OK;
2712 }
2713
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002714 if (error)
2715 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 return FAIL;
2717}
2718
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002719 static int
2720generate_funcref(cctx_T *cctx, char_u *name)
2721{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002722 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002723
2724 if (ufunc == NULL)
2725 return FAIL;
2726
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002727 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2728 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002729}
2730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731/*
2732 * Compile a variable name into a load instruction.
2733 * "end" points to just after the name.
2734 * When "error" is FALSE do not give an error when not found.
2735 */
2736 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002737compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738{
2739 type_T *type;
2740 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002741 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002743 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744
2745 if (*(*arg + 1) == ':')
2746 {
2747 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002748 if (end <= *arg + 2)
2749 name = vim_strsave((char_u *)"[empty]");
2750 else
2751 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 if (name == NULL)
2753 return FAIL;
2754
2755 if (**arg == 'v')
2756 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002757 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 }
2759 else if (**arg == 'g')
2760 {
2761 // Global variables can be defined later, thus we don't check if it
2762 // exists, give error at runtime.
2763 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2764 }
2765 else if (**arg == 's')
2766 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002767 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002769 else if (**arg == 'b')
2770 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002771 // Buffer-local variables can be defined later, thus we don't check
2772 // if it exists, give error at runtime.
2773 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002774 }
2775 else if (**arg == 'w')
2776 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002777 // Window-local variables can be defined later, thus we don't check
2778 // if it exists, give error at runtime.
2779 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002780 }
2781 else if (**arg == 't')
2782 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002783 // Tabpage-local variables can be defined later, thus we don't
2784 // check if it exists, give error at runtime.
2785 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 else
2788 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002789 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 goto theend;
2791 }
2792 }
2793 else
2794 {
2795 size_t len = end - *arg;
2796 int idx;
2797 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002798 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799
2800 name = vim_strnsave(*arg, end - *arg);
2801 if (name == NULL)
2802 return FAIL;
2803
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002804 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002806 if (!gen_load_outer)
2807 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 }
2809 else
2810 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002811 lvar_T *lvar = lookup_local(*arg, len, cctx);
2812
2813 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002815 type = lvar->lv_type;
2816 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002817 if (lvar->lv_from_outer)
2818 gen_load_outer = TRUE;
2819 else
2820 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 }
2822 else
2823 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002824 // "var" can be script-local even without using "s:" if it
2825 // already exists.
2826 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2827 == SCRIPT_VERSION_VIM9
2828 || lookup_script(*arg, len) == OK)
2829 res = compile_load_scriptvar(cctx, name, *arg, &end,
2830 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002831
Bram Moolenaara5565e42020-05-09 15:44:01 +02002832 // When the name starts with an uppercase letter or "x:" it
2833 // can be a user defined function.
2834 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2835 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 }
2837 }
2838 if (gen_load)
2839 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002840 if (gen_load_outer)
2841 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 }
2843
2844 *arg = end;
2845
2846theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002847 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 semsg(_(e_var_notfound), name);
2849 vim_free(name);
2850 return res;
2851}
2852
2853/*
2854 * Compile the argument expressions.
2855 * "arg" points to just after the "(" and is advanced to after the ")"
2856 */
2857 static int
2858compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2859{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002860 char_u *p = *arg;
2861 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862
Bram Moolenaare6085c52020-04-12 20:19:16 +02002863 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002865 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2866 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002867 if (*p == ')')
2868 {
2869 *arg = p + 1;
2870 return OK;
2871 }
2872
Bram Moolenaara5565e42020-05-09 15:44:01 +02002873 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 return FAIL;
2875 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002876
2877 if (*p != ',' && *skipwhite(p) == ',')
2878 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002879 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002880 p = skipwhite(p);
2881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002883 {
2884 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002885 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002886 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002887 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002888 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002889 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002891failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002892 emsg(_(e_missing_close));
2893 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894}
2895
2896/*
2897 * Compile a function call: name(arg1, arg2)
2898 * "arg" points to "name", "arg + varlen" to the "(".
2899 * "argcount_init" is 1 for "value->method()"
2900 * Instructions:
2901 * EVAL arg1
2902 * EVAL arg2
2903 * BCALL / DCALL / UCALL
2904 */
2905 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002906compile_call(
2907 char_u **arg,
2908 size_t varlen,
2909 cctx_T *cctx,
2910 ppconst_T *ppconst,
2911 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912{
2913 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002914 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 int argcount = argcount_init;
2916 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002917 char_u fname_buf[FLEN_FIXED + 1];
2918 char_u *tofree = NULL;
2919 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002921 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922
Bram Moolenaara5565e42020-05-09 15:44:01 +02002923 // we can evaluate "has('name')" at compile time
2924 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2925 {
2926 char_u *s = skipwhite(*arg + varlen + 1);
2927 typval_T argvars[2];
2928
2929 argvars[0].v_type = VAR_UNKNOWN;
2930 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002931 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002932 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002933 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002934 s = skipwhite(s);
2935 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2936 {
2937 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2938
2939 *arg = s + 1;
2940 argvars[1].v_type = VAR_UNKNOWN;
2941 tv->v_type = VAR_NUMBER;
2942 tv->vval.v_number = 0;
2943 f_has(argvars, tv);
2944 clear_tv(&argvars[0]);
2945 ++ppconst->pp_used;
2946 return OK;
2947 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002948 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002949 }
2950
2951 if (generate_ppconst(cctx, ppconst) == FAIL)
2952 return FAIL;
2953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 if (varlen >= sizeof(namebuf))
2955 {
2956 semsg(_("E1011: name too long: %s"), name);
2957 return FAIL;
2958 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002959 vim_strncpy(namebuf, *arg, varlen);
2960 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961
2962 *arg = skipwhite(*arg + varlen + 1);
2963 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002964 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002966 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 {
2968 int idx;
2969
2970 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002971 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002973 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002974 else
2975 semsg(_(e_unknownfunc), namebuf);
2976 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 }
2978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002980 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002982 {
2983 res = generate_CALL(cctx, ufunc, argcount);
2984 goto theend;
2985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986
2987 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002988 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002990 if (STRNCMP(namebuf, "g:", 2) != 0
2991 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002992 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002993 garray_T *stack = &cctx->ctx_type_stack;
2994 type_T *type;
2995
2996 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2997 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002998 goto theend;
2999 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003001 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003002 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003003 if (STRNCMP(namebuf, "g:", 2) == 0)
3004 res = generate_UCALL(cctx, name, argcount);
3005 else
3006 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003007
3008theend:
3009 vim_free(tofree);
3010 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011}
3012
3013// like NAMESPACE_CHAR but with 'a' and 'l'.
3014#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3015
3016/*
3017 * Find the end of a variable or function name. Unlike find_name_end() this
3018 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003019 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 * Return a pointer to just after the name. Equal to "arg" if there is no
3021 * valid name.
3022 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003023 static char_u *
3024to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025{
3026 char_u *p;
3027
3028 // Quick check for valid starting character.
3029 if (!eval_isnamec1(*arg))
3030 return arg;
3031
3032 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3033 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3034 // and can be used in slice "[n:]".
3035 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003036 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3038 break;
3039 return p;
3040}
3041
3042/*
3043 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003044 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 */
3046 char_u *
3047to_name_const_end(char_u *arg)
3048{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003049 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 typval_T rettv;
3051
3052 if (p == arg && *arg == '[')
3053 {
3054
3055 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003056 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 p = arg;
3058 }
3059 else if (p == arg && *arg == '#' && arg[1] == '{')
3060 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003061 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003063 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 p = arg;
3065 }
3066 else if (p == arg && *arg == '{')
3067 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003068 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003070 // Can be "{x -> ret}()".
3071 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003073 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 if (ret != OK)
3075 p = arg;
3076 }
3077
3078 return p;
3079}
3080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081/*
3082 * parse a list: [expr, expr]
3083 * "*arg" points to the '['.
3084 */
3085 static int
3086compile_list(char_u **arg, cctx_T *cctx)
3087{
3088 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003089 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 int count = 0;
3091
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003092 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003094 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003095 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003096 semsg(_(e_list_end), *arg);
3097 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003098 }
3099 if (*p == ']')
3100 {
3101 ++p;
3102 // Allow for following comment, after at least one space.
3103 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
3104 p += STRLEN(p);
3105 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003106 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003107 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 break;
3109 ++count;
3110 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003113 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3114 {
3115 semsg(_(e_white_after), ",");
3116 return FAIL;
3117 }
3118 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003119 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 p = skipwhite(p);
3121 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003122 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123
3124 generate_NEWLIST(cctx, count);
3125 return OK;
3126}
3127
3128/*
3129 * parse a lambda: {arg, arg -> expr}
3130 * "*arg" points to the '{'.
3131 */
3132 static int
3133compile_lambda(char_u **arg, cctx_T *cctx)
3134{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 typval_T rettv;
3136 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003137 evalarg_T evalarg;
3138
3139 CLEAR_FIELD(evalarg);
3140 evalarg.eval_flags = EVAL_EVALUATE;
3141 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142
3143 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003144 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003148 ++ufunc->uf_refcount;
3149 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003150 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151
3152 // The function will have one line: "return {expr}".
3153 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003154 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003156 clear_evalarg(&evalarg, NULL);
3157
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003158 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003159 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 return FAIL;
3161}
3162
3163/*
3164 * Compile a lamda call: expr->{lambda}(args)
3165 * "arg" points to the "{".
3166 */
3167 static int
3168compile_lambda_call(char_u **arg, cctx_T *cctx)
3169{
3170 ufunc_T *ufunc;
3171 typval_T rettv;
3172 int argcount = 1;
3173 int ret = FAIL;
3174
3175 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003176 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 return FAIL;
3178
3179 if (**arg != '(')
3180 {
3181 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003182 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 else
3184 semsg(_(e_missing_paren), "lambda");
3185 clear_tv(&rettv);
3186 return FAIL;
3187 }
3188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 ufunc = rettv.vval.v_partial->pt_func;
3190 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003191 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003192 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003193
3194 // The function will have one line: "return {expr}".
3195 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003196 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197
3198 // compile the arguments
3199 *arg = skipwhite(*arg + 1);
3200 if (compile_arguments(arg, cctx, &argcount) == OK)
3201 // call the compiled function
3202 ret = generate_CALL(cctx, ufunc, argcount);
3203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 return ret;
3205}
3206
3207/*
3208 * parse a dict: {'key': val} or #{key: val}
3209 * "*arg" points to the '{'.
3210 */
3211 static int
3212compile_dict(char_u **arg, cctx_T *cctx, int literal)
3213{
3214 garray_T *instr = &cctx->ctx_instr;
3215 int count = 0;
3216 dict_T *d = dict_alloc();
3217 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003218 char_u *whitep = *arg;
3219 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220
3221 if (d == NULL)
3222 return FAIL;
3223 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003224 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 {
3226 char_u *key = NULL;
3227
Bram Moolenaar23c55272020-06-21 16:58:13 +02003228 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003229 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003230 *arg = NULL;
3231 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003232 }
3233
3234 if (**arg == '}')
3235 break;
3236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 if (literal)
3238 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003239 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240
Bram Moolenaar2c330432020-04-13 14:41:35 +02003241 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 {
3243 semsg(_("E1014: Invalid key: %s"), *arg);
3244 return FAIL;
3245 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003246 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 if (generate_PUSHS(cctx, key) == FAIL)
3248 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003249 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 }
3251 else
3252 {
3253 isn_T *isn;
3254
Bram Moolenaara5565e42020-05-09 15:44:01 +02003255 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 return FAIL;
3257 // TODO: check type is string
3258 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3259 if (isn->isn_type == ISN_PUSHS)
3260 key = isn->isn_arg.string;
3261 }
3262
3263 // Check for duplicate keys, if using string keys.
3264 if (key != NULL)
3265 {
3266 item = dict_find(d, key, -1);
3267 if (item != NULL)
3268 {
3269 semsg(_(e_duplicate_key), key);
3270 goto failret;
3271 }
3272 item = dictitem_alloc(key);
3273 if (item != NULL)
3274 {
3275 item->di_tv.v_type = VAR_UNKNOWN;
3276 item->di_tv.v_lock = 0;
3277 if (dict_add(d, item) == FAIL)
3278 dictitem_free(item);
3279 }
3280 }
3281
3282 *arg = skipwhite(*arg);
3283 if (**arg != ':')
3284 {
3285 semsg(_(e_missing_dict_colon), *arg);
3286 return FAIL;
3287 }
3288
Bram Moolenaar2c330432020-04-13 14:41:35 +02003289 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003291 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003292 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003293 *arg = NULL;
3294 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003295 }
3296
Bram Moolenaara5565e42020-05-09 15:44:01 +02003297 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 return FAIL;
3299 ++count;
3300
Bram Moolenaar2c330432020-04-13 14:41:35 +02003301 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003302 *arg = skipwhite(*arg);
3303 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003304 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003305 *arg = NULL;
3306 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 if (**arg == '}')
3309 break;
3310 if (**arg != ',')
3311 {
3312 semsg(_(e_missing_dict_comma), *arg);
3313 goto failret;
3314 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003315 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 *arg = skipwhite(*arg + 1);
3317 }
3318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 *arg = *arg + 1;
3320
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003321 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003322 p = skipwhite(*arg);
3323 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003324 *arg += STRLEN(*arg);
3325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 dict_unref(d);
3327 return generate_NEWDICT(cctx, count);
3328
3329failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003330 if (*arg == NULL)
3331 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 dict_unref(d);
3333 return FAIL;
3334}
3335
3336/*
3337 * Compile "&option".
3338 */
3339 static int
3340compile_get_option(char_u **arg, cctx_T *cctx)
3341{
3342 typval_T rettv;
3343 char_u *start = *arg;
3344 int ret;
3345
3346 // parse the option and get the current value to get the type.
3347 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003348 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 if (ret == OK)
3350 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003351 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 char_u *name = vim_strnsave(start, *arg - start);
3353 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3354
3355 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3356 vim_free(name);
3357 }
3358 clear_tv(&rettv);
3359
3360 return ret;
3361}
3362
3363/*
3364 * Compile "$VAR".
3365 */
3366 static int
3367compile_get_env(char_u **arg, cctx_T *cctx)
3368{
3369 char_u *start = *arg;
3370 int len;
3371 int ret;
3372 char_u *name;
3373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 ++*arg;
3375 len = get_env_len(arg);
3376 if (len == 0)
3377 {
3378 semsg(_(e_syntax_at), start - 1);
3379 return FAIL;
3380 }
3381
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003382 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 name = vim_strnsave(start, len + 1);
3384 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3385 vim_free(name);
3386 return ret;
3387}
3388
3389/*
3390 * Compile "@r".
3391 */
3392 static int
3393compile_get_register(char_u **arg, cctx_T *cctx)
3394{
3395 int ret;
3396
3397 ++*arg;
3398 if (**arg == NUL)
3399 {
3400 semsg(_(e_syntax_at), *arg - 1);
3401 return FAIL;
3402 }
3403 if (!valid_yank_reg(**arg, TRUE))
3404 {
3405 emsg_invreg(**arg);
3406 return FAIL;
3407 }
3408 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3409 ++*arg;
3410 return ret;
3411}
3412
3413/*
3414 * Apply leading '!', '-' and '+' to constant "rettv".
3415 */
3416 static int
3417apply_leader(typval_T *rettv, char_u *start, char_u *end)
3418{
3419 char_u *p = end;
3420
3421 // this works from end to start
3422 while (p > start)
3423 {
3424 --p;
3425 if (*p == '-' || *p == '+')
3426 {
3427 // only '-' has an effect, for '+' we only check the type
3428#ifdef FEAT_FLOAT
3429 if (rettv->v_type == VAR_FLOAT)
3430 {
3431 if (*p == '-')
3432 rettv->vval.v_float = -rettv->vval.v_float;
3433 }
3434 else
3435#endif
3436 {
3437 varnumber_T val;
3438 int error = FALSE;
3439
3440 // tv_get_number_chk() accepts a string, but we don't want that
3441 // here
3442 if (check_not_string(rettv) == FAIL)
3443 return FAIL;
3444 val = tv_get_number_chk(rettv, &error);
3445 clear_tv(rettv);
3446 if (error)
3447 return FAIL;
3448 if (*p == '-')
3449 val = -val;
3450 rettv->v_type = VAR_NUMBER;
3451 rettv->vval.v_number = val;
3452 }
3453 }
3454 else
3455 {
3456 int v = tv2bool(rettv);
3457
3458 // '!' is permissive in the type.
3459 clear_tv(rettv);
3460 rettv->v_type = VAR_BOOL;
3461 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3462 }
3463 }
3464 return OK;
3465}
3466
3467/*
3468 * Recognize v: variables that are constants and set "rettv".
3469 */
3470 static void
3471get_vim_constant(char_u **arg, typval_T *rettv)
3472{
3473 if (STRNCMP(*arg, "v:true", 6) == 0)
3474 {
3475 rettv->v_type = VAR_BOOL;
3476 rettv->vval.v_number = VVAL_TRUE;
3477 *arg += 6;
3478 }
3479 else if (STRNCMP(*arg, "v:false", 7) == 0)
3480 {
3481 rettv->v_type = VAR_BOOL;
3482 rettv->vval.v_number = VVAL_FALSE;
3483 *arg += 7;
3484 }
3485 else if (STRNCMP(*arg, "v:null", 6) == 0)
3486 {
3487 rettv->v_type = VAR_SPECIAL;
3488 rettv->vval.v_number = VVAL_NULL;
3489 *arg += 6;
3490 }
3491 else if (STRNCMP(*arg, "v:none", 6) == 0)
3492 {
3493 rettv->v_type = VAR_SPECIAL;
3494 rettv->vval.v_number = VVAL_NONE;
3495 *arg += 6;
3496 }
3497}
3498
Bram Moolenaar61a89812020-05-07 16:58:17 +02003499 static exptype_T
3500get_compare_type(char_u *p, int *len, int *type_is)
3501{
3502 exptype_T type = EXPR_UNKNOWN;
3503 int i;
3504
3505 switch (p[0])
3506 {
3507 case '=': if (p[1] == '=')
3508 type = EXPR_EQUAL;
3509 else if (p[1] == '~')
3510 type = EXPR_MATCH;
3511 break;
3512 case '!': if (p[1] == '=')
3513 type = EXPR_NEQUAL;
3514 else if (p[1] == '~')
3515 type = EXPR_NOMATCH;
3516 break;
3517 case '>': if (p[1] != '=')
3518 {
3519 type = EXPR_GREATER;
3520 *len = 1;
3521 }
3522 else
3523 type = EXPR_GEQUAL;
3524 break;
3525 case '<': if (p[1] != '=')
3526 {
3527 type = EXPR_SMALLER;
3528 *len = 1;
3529 }
3530 else
3531 type = EXPR_SEQUAL;
3532 break;
3533 case 'i': if (p[1] == 's')
3534 {
3535 // "is" and "isnot"; but not a prefix of a name
3536 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3537 *len = 5;
3538 i = p[*len];
3539 if (!isalnum(i) && i != '_')
3540 {
3541 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3542 *type_is = TRUE;
3543 }
3544 }
3545 break;
3546 }
3547 return type;
3548}
3549
3550/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 * Compile code to apply '-', '+' and '!'.
3552 */
3553 static int
3554compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3555{
3556 char_u *p = end;
3557
3558 // this works from end to start
3559 while (p > start)
3560 {
3561 --p;
3562 if (*p == '-' || *p == '+')
3563 {
3564 int negate = *p == '-';
3565 isn_T *isn;
3566
3567 // TODO: check type
3568 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3569 {
3570 --p;
3571 if (*p == '-')
3572 negate = !negate;
3573 }
3574 // only '-' has an effect, for '+' we only check the type
3575 if (negate)
3576 isn = generate_instr(cctx, ISN_NEGATENR);
3577 else
3578 isn = generate_instr(cctx, ISN_CHECKNR);
3579 if (isn == NULL)
3580 return FAIL;
3581 }
3582 else
3583 {
3584 int invert = TRUE;
3585
3586 while (p > start && p[-1] == '!')
3587 {
3588 --p;
3589 invert = !invert;
3590 }
3591 if (generate_2BOOL(cctx, invert) == FAIL)
3592 return FAIL;
3593 }
3594 }
3595 return OK;
3596}
3597
3598/*
3599 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003600 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 */
3602 static int
3603compile_subscript(
3604 char_u **arg,
3605 cctx_T *cctx,
3606 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003607 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003608 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609{
3610 for (;;)
3611 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003612 char_u *p = skipwhite(*arg);
3613
3614 if (*p == NUL || (VIM_ISWHITE(**arg) && comment_start(p)))
3615 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003616 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003617
3618 // If a following line starts with "->{" or "->X" advance to that
3619 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003620 // Also if a following line starts with ".x".
3621 if (next != NULL &&
3622 ((next[0] == '-' && next[1] == '>'
3623 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3624 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003625 {
3626 next = next_line_from_context(cctx, TRUE);
3627 if (next == NULL)
3628 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003629 *arg = next;
3630 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003631 }
3632 }
3633
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003634 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003636 garray_T *stack = &cctx->ctx_type_stack;
3637 type_T *type;
3638 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003640 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003641 return FAIL;
3642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003644 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3645
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003646 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3648 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003649 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 return FAIL;
3651 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003652 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003654 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003655 return FAIL;
3656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 // something->method()
3658 // Apply the '!', '-' and '+' first:
3659 // -1.0->func() works like (-1.0)->func()
3660 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3661 return FAIL;
3662 *start_leader = end_leader; // don't apply again later
3663
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003664 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003665 *arg = skipwhite(p);
3666 if (may_get_next_line(p, arg, cctx) == FAIL)
3667 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 if (**arg == '{')
3669 {
3670 // lambda call: list->{lambda}
3671 if (compile_lambda_call(arg, cctx) == FAIL)
3672 return FAIL;
3673 }
3674 else
3675 {
3676 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003677 p = *arg;
3678 if (ASCII_ISALPHA(*p) && p[1] == ':')
3679 p += 2;
3680 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 ;
3682 if (*p != '(')
3683 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003684 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 return FAIL;
3686 }
3687 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003688 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 return FAIL;
3690 }
3691 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003692 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003694 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003695 type_T **typep;
3696
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003697 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003698 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003699 // TODO: blob index
3700 // TODO: more arguments
3701 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003702 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003703 return FAIL;
3704
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003705 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003706 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003707 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003708 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003709 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 return FAIL;
3711
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003712 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3713 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 if (**arg != ']')
3715 {
3716 emsg(_(e_missbrac));
3717 return FAIL;
3718 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003719 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003721 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3722 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003723 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003724 if ((*typep)->tt_type == VAR_LIST)
3725 *typep = (*typep)->tt_member;
3726 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3727 return FAIL;
3728 }
3729 else if ((*typep)->tt_type == VAR_DICT)
3730 {
3731 *typep = (*typep)->tt_member;
3732 if (may_generate_2STRING(-1, cctx) == FAIL)
3733 return FAIL;
3734 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3735 return FAIL;
3736 }
3737 else
3738 {
3739 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003740 return FAIL;
3741 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003743 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003745 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003746 return FAIL;
3747
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003748 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003749 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3750 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003752 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 if (eval_isnamec1(*p))
3754 while (eval_isnamec(*p))
3755 MB_PTR_ADV(p);
3756 if (p == *arg)
3757 {
3758 semsg(_(e_syntax_at), *arg);
3759 return FAIL;
3760 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003761 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 return FAIL;
3763 *arg = p;
3764 }
3765 else
3766 break;
3767 }
3768
3769 // TODO - see handle_subscript():
3770 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3771 // Don't do this when "Func" is already a partial that was bound
3772 // explicitly (pt_auto is FALSE).
3773
3774 return OK;
3775}
3776
3777/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003778 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3779 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003781 * If the value is a constant "ppconst->pp_ret" will be set.
3782 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003783 *
3784 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 */
3786
3787/*
3788 * number number constant
3789 * 0zFFFFFFFF Blob constant
3790 * "string" string constant
3791 * 'string' literal string constant
3792 * &option-name option value
3793 * @r register contents
3794 * identifier variable value
3795 * function() function call
3796 * $VAR environment variable
3797 * (expression) nested expression
3798 * [expr, expr] List
3799 * {key: val, key: val} Dictionary
3800 * #{key: val, key: val} Dictionary with literal keys
3801 *
3802 * Also handle:
3803 * ! in front logical NOT
3804 * - in front unary minus
3805 * + in front unary plus (ignored)
3806 * trailing (arg) funcref/partial call
3807 * trailing [] subscript in String or List
3808 * trailing .name entry in Dictionary
3809 * trailing ->name() method call
3810 */
3811 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003812compile_expr7(
3813 char_u **arg,
3814 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003815 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 char_u *start_leader, *end_leader;
3818 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003819 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003820 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821
3822 /*
3823 * Skip '!', '-' and '+' characters. They are handled later.
3824 */
3825 start_leader = *arg;
3826 while (**arg == '!' || **arg == '-' || **arg == '+')
3827 *arg = skipwhite(*arg + 1);
3828 end_leader = *arg;
3829
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003830 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 switch (**arg)
3832 {
3833 /*
3834 * Number constant.
3835 */
3836 case '0': // also for blob starting with 0z
3837 case '1':
3838 case '2':
3839 case '3':
3840 case '4':
3841 case '5':
3842 case '6':
3843 case '7':
3844 case '8':
3845 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003846 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 return FAIL;
3848 break;
3849
3850 /*
3851 * String constant: "string".
3852 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003853 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 return FAIL;
3855 break;
3856
3857 /*
3858 * Literal string constant: 'str''ing'.
3859 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003860 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 return FAIL;
3862 break;
3863
3864 /*
3865 * Constant Vim variable.
3866 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003867 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 ret = NOTDONE;
3869 break;
3870
3871 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003872 * "true" constant
3873 */
3874 case 't': if (STRNCMP(*arg, "true", 4) == 0
3875 && !eval_isnamec((*arg)[4]))
3876 {
3877 *arg += 4;
3878 rettv->v_type = VAR_BOOL;
3879 rettv->vval.v_number = VVAL_TRUE;
3880 }
3881 else
3882 ret = NOTDONE;
3883 break;
3884
3885 /*
3886 * "false" constant
3887 */
3888 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3889 && !eval_isnamec((*arg)[5]))
3890 {
3891 *arg += 5;
3892 rettv->v_type = VAR_BOOL;
3893 rettv->vval.v_number = VVAL_FALSE;
3894 }
3895 else
3896 ret = NOTDONE;
3897 break;
3898
3899 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 * List: [expr, expr]
3901 */
3902 case '[': ret = compile_list(arg, cctx);
3903 break;
3904
3905 /*
3906 * Dictionary: #{key: val, key: val}
3907 */
3908 case '#': if ((*arg)[1] == '{')
3909 {
3910 ++*arg;
3911 ret = compile_dict(arg, cctx, TRUE);
3912 }
3913 else
3914 ret = NOTDONE;
3915 break;
3916
3917 /*
3918 * Lambda: {arg, arg -> expr}
3919 * Dictionary: {'key': val, 'key': val}
3920 */
3921 case '{': {
3922 char_u *start = skipwhite(*arg + 1);
3923
3924 // Find out what comes after the arguments.
3925 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003926 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 if (ret != FAIL && *start == '>')
3928 ret = compile_lambda(arg, cctx);
3929 else
3930 ret = compile_dict(arg, cctx, FALSE);
3931 }
3932 break;
3933
3934 /*
3935 * Option value: &name
3936 */
3937 case '&': ret = compile_get_option(arg, cctx);
3938 break;
3939
3940 /*
3941 * Environment variable: $VAR.
3942 */
3943 case '$': ret = compile_get_env(arg, cctx);
3944 break;
3945
3946 /*
3947 * Register contents: @r.
3948 */
3949 case '@': ret = compile_get_register(arg, cctx);
3950 break;
3951 /*
3952 * nested expression: (expression).
3953 */
3954 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003955
3956 // recursive!
3957 if (ppconst->pp_used <= PPSIZE - 10)
3958 {
3959 ret = compile_expr1(arg, cctx, ppconst);
3960 }
3961 else
3962 {
3963 // Not enough space in ppconst, flush constants.
3964 if (generate_ppconst(cctx, ppconst) == FAIL)
3965 return FAIL;
3966 ret = compile_expr0(arg, cctx);
3967 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 *arg = skipwhite(*arg);
3969 if (**arg == ')')
3970 ++*arg;
3971 else if (ret == OK)
3972 {
3973 emsg(_(e_missing_close));
3974 ret = FAIL;
3975 }
3976 break;
3977
3978 default: ret = NOTDONE;
3979 break;
3980 }
3981 if (ret == FAIL)
3982 return FAIL;
3983
Bram Moolenaar1c747212020-05-09 18:28:34 +02003984 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 {
3986 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003987 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003989 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 return FAIL;
3991 }
3992 start_leader = end_leader; // don't apply again below
3993
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003994 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003995 clear_tv(rettv);
3996 else
3997 // A constant expression can possibly be handled compile time,
3998 // return the value instead of generating code.
3999 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 }
4001 else if (ret == NOTDONE)
4002 {
4003 char_u *p;
4004 int r;
4005
4006 if (!eval_isnamec1(**arg))
4007 {
4008 semsg(_("E1015: Name expected: %s"), *arg);
4009 return FAIL;
4010 }
4011
4012 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004013 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004015 {
4016 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4017 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004019 {
4020 if (generate_ppconst(cctx, ppconst) == FAIL)
4021 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 if (r == FAIL)
4025 return FAIL;
4026 }
4027
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004028 // Handle following "[]", ".member", etc.
4029 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004030 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004031 ppconst) == FAIL)
4032 return FAIL;
4033 if (ppconst->pp_used > 0)
4034 {
4035 // apply the '!', '-' and '+' before the constant
4036 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4037 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4038 return FAIL;
4039 return OK;
4040 }
4041 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004043 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044}
4045
4046/*
4047 * * number multiplication
4048 * / number division
4049 * % number modulo
4050 */
4051 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004052compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053{
4054 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004055 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004056 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004058 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004059 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 return FAIL;
4061
4062 /*
4063 * Repeat computing, until no "*", "/" or "%" is following.
4064 */
4065 for (;;)
4066 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004067 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 if (*op != '*' && *op != '/' && *op != '%')
4069 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004070 if (next != NULL)
4071 {
4072 *arg = next_line_from_context(cctx, TRUE);
4073 op = skipwhite(*arg);
4074 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004075
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004076 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 {
4078 char_u buf[3];
4079
4080 vim_strncpy(buf, op, 1);
4081 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004082 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 }
4084 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004085 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004086 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004088 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004089 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004091
4092 if (ppconst->pp_used == ppconst_used + 2
4093 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4094 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004095 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004096 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4097 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004098 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004100 // both are numbers: compute the result
4101 switch (*op)
4102 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004103 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004104 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004105 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004106 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004107 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004108 break;
4109 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004110 tv1->vval.v_number = res;
4111 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004112 }
4113 else
4114 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004115 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004116 generate_two_op(cctx, op);
4117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 }
4119
4120 return OK;
4121}
4122
4123/*
4124 * + number addition
4125 * - number subtraction
4126 * .. string concatenation
4127 */
4128 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004129compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130{
4131 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004132 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004134 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135
4136 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004137 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 return FAIL;
4139
4140 /*
4141 * Repeat computing, until no "+", "-" or ".." is following.
4142 */
4143 for (;;)
4144 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004145 op = may_peek_next_line(cctx, *arg, &next);
4146 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 break;
4148 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004149 if (next != NULL)
4150 {
4151 *arg = next_line_from_context(cctx, TRUE);
4152 op = skipwhite(*arg);
4153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004155 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 {
4157 char_u buf[3];
4158
4159 vim_strncpy(buf, op, oplen);
4160 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004161 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 }
4163
4164 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004165 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004166 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004168 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004169 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 return FAIL;
4171
Bram Moolenaara5565e42020-05-09 15:44:01 +02004172 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004173 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004174 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4175 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4176 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4177 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004179 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4180 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004181
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004182 // concat/subtract/add constant numbers
4183 if (*op == '+')
4184 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4185 else if (*op == '-')
4186 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4187 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004188 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004189 // concatenate constant strings
4190 char_u *s1 = tv1->vval.v_string;
4191 char_u *s2 = tv2->vval.v_string;
4192 size_t len1 = STRLEN(s1);
4193
4194 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4195 if (tv1->vval.v_string == NULL)
4196 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004197 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004198 return FAIL;
4199 }
4200 mch_memmove(tv1->vval.v_string, s1, len1);
4201 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004202 vim_free(s1);
4203 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004204 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004205 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 }
4207 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004208 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004209 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004210 if (*op == '.')
4211 {
4212 if (may_generate_2STRING(-2, cctx) == FAIL
4213 || may_generate_2STRING(-1, cctx) == FAIL)
4214 return FAIL;
4215 generate_instr_drop(cctx, ISN_CONCAT, 1);
4216 }
4217 else
4218 generate_two_op(cctx, op);
4219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 }
4221
4222 return OK;
4223}
4224
4225/*
4226 * expr5a == expr5b
4227 * expr5a =~ expr5b
4228 * expr5a != expr5b
4229 * expr5a !~ expr5b
4230 * expr5a > expr5b
4231 * expr5a >= expr5b
4232 * expr5a < expr5b
4233 * expr5a <= expr5b
4234 * expr5a is expr5b
4235 * expr5a isnot expr5b
4236 *
4237 * Produces instructions:
4238 * EVAL expr5a Push result of "expr5a"
4239 * EVAL expr5b Push result of "expr5b"
4240 * COMPARE one of the compare instructions
4241 */
4242 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004243compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244{
4245 exptype_T type = EXPR_UNKNOWN;
4246 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004247 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004250 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251
4252 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004253 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 return FAIL;
4255
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004256 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004257 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258
4259 /*
4260 * If there is a comparative operator, use it.
4261 */
4262 if (type != EXPR_UNKNOWN)
4263 {
4264 int ic = FALSE; // Default: do not ignore case
4265
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004266 if (next != NULL)
4267 {
4268 *arg = next_line_from_context(cctx, TRUE);
4269 p = skipwhite(*arg);
4270 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 if (type_is && (p[len] == '?' || p[len] == '#'))
4272 {
4273 semsg(_(e_invexpr2), *arg);
4274 return FAIL;
4275 }
4276 // extra question mark appended: ignore case
4277 if (p[len] == '?')
4278 {
4279 ic = TRUE;
4280 ++len;
4281 }
4282 // extra '#' appended: match case (ignored)
4283 else if (p[len] == '#')
4284 ++len;
4285 // nothing appended: match case
4286
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004287 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 {
4289 char_u buf[7];
4290
4291 vim_strncpy(buf, p, len);
4292 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004293 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 }
4295
4296 // get the second variable
4297 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004298 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004299 return FAIL;
4300
Bram Moolenaara5565e42020-05-09 15:44:01 +02004301 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 return FAIL;
4303
Bram Moolenaara5565e42020-05-09 15:44:01 +02004304 if (ppconst->pp_used == ppconst_used + 2)
4305 {
4306 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4307 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4308 int ret;
4309
4310 // Both sides are a constant, compute the result now.
4311 // First check for a valid combination of types, this is more
4312 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004313 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004314 ret = FAIL;
4315 else
4316 {
4317 ret = typval_compare(tv1, tv2, type, ic);
4318 tv1->v_type = VAR_BOOL;
4319 tv1->vval.v_number = tv1->vval.v_number
4320 ? VVAL_TRUE : VVAL_FALSE;
4321 clear_tv(tv2);
4322 --ppconst->pp_used;
4323 }
4324 return ret;
4325 }
4326
4327 generate_ppconst(cctx, ppconst);
4328 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 }
4330
4331 return OK;
4332}
4333
Bram Moolenaar7f141552020-05-09 17:35:53 +02004334static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336/*
4337 * Compile || or &&.
4338 */
4339 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004340compile_and_or(
4341 char_u **arg,
4342 cctx_T *cctx,
4343 char *op,
4344 ppconst_T *ppconst,
4345 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004347 char_u *next;
4348 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 int opchar = *op;
4350
4351 if (p[0] == opchar && p[1] == opchar)
4352 {
4353 garray_T *instr = &cctx->ctx_instr;
4354 garray_T end_ga;
4355
4356 /*
4357 * Repeat until there is no following "||" or "&&"
4358 */
4359 ga_init2(&end_ga, sizeof(int), 10);
4360 while (p[0] == opchar && p[1] == opchar)
4361 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004362 if (next != NULL)
4363 {
4364 *arg = next_line_from_context(cctx, TRUE);
4365 p = skipwhite(*arg);
4366 }
4367
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004368 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4369 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004371 return FAIL;
4372 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373
Bram Moolenaara5565e42020-05-09 15:44:01 +02004374 // TODO: use ppconst if the value is a constant
4375 generate_ppconst(cctx, ppconst);
4376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 if (ga_grow(&end_ga, 1) == FAIL)
4378 {
4379 ga_clear(&end_ga);
4380 return FAIL;
4381 }
4382 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4383 ++end_ga.ga_len;
4384 generate_JUMP(cctx, opchar == '|'
4385 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4386
4387 // eval the next expression
4388 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004389 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004390 return FAIL;
4391
Bram Moolenaara5565e42020-05-09 15:44:01 +02004392 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4393 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 {
4395 ga_clear(&end_ga);
4396 return FAIL;
4397 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004398
4399 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004401 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402
4403 // Fill in the end label in all jumps.
4404 while (end_ga.ga_len > 0)
4405 {
4406 isn_T *isn;
4407
4408 --end_ga.ga_len;
4409 isn = ((isn_T *)instr->ga_data)
4410 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4411 isn->isn_arg.jump.jump_where = instr->ga_len;
4412 }
4413 ga_clear(&end_ga);
4414 }
4415
4416 return OK;
4417}
4418
4419/*
4420 * expr4a && expr4a && expr4a logical AND
4421 *
4422 * Produces instructions:
4423 * EVAL expr4a Push result of "expr4a"
4424 * JUMP_AND_KEEP_IF_FALSE end
4425 * EVAL expr4b Push result of "expr4b"
4426 * JUMP_AND_KEEP_IF_FALSE end
4427 * EVAL expr4c Push result of "expr4c"
4428 * end:
4429 */
4430 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004431compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004433 int ppconst_used = ppconst->pp_used;
4434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004436 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 return FAIL;
4438
4439 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004440 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441}
4442
4443/*
4444 * expr3a || expr3b || expr3c logical OR
4445 *
4446 * Produces instructions:
4447 * EVAL expr3a Push result of "expr3a"
4448 * JUMP_AND_KEEP_IF_TRUE end
4449 * EVAL expr3b Push result of "expr3b"
4450 * JUMP_AND_KEEP_IF_TRUE end
4451 * EVAL expr3c Push result of "expr3c"
4452 * end:
4453 */
4454 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004455compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004457 int ppconst_used = ppconst->pp_used;
4458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004460 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 return FAIL;
4462
4463 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004464 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465}
4466
4467/*
4468 * Toplevel expression: expr2 ? expr1a : expr1b
4469 *
4470 * Produces instructions:
4471 * EVAL expr2 Push result of "expr"
4472 * JUMP_IF_FALSE alt jump if false
4473 * EVAL expr1a
4474 * JUMP_ALWAYS end
4475 * alt: EVAL expr1b
4476 * end:
4477 */
4478 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004479compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480{
4481 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004482 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004483 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484
Bram Moolenaar61a89812020-05-07 16:58:17 +02004485 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004486 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 return FAIL;
4488
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004489 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 if (*p == '?')
4491 {
4492 garray_T *instr = &cctx->ctx_instr;
4493 garray_T *stack = &cctx->ctx_type_stack;
4494 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004495 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004497 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004499 int has_const_expr = FALSE;
4500 int const_value = FALSE;
4501 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004503 if (next != NULL)
4504 {
4505 *arg = next_line_from_context(cctx, TRUE);
4506 p = skipwhite(*arg);
4507 }
4508
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004509 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4510 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004512 return FAIL;
4513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514
Bram Moolenaara5565e42020-05-09 15:44:01 +02004515 if (ppconst->pp_used == ppconst_used + 1)
4516 {
4517 // the condition is a constant, we know whether the ? or the :
4518 // expression is to be evaluated.
4519 has_const_expr = TRUE;
4520 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4521 clear_tv(&ppconst->pp_tv[ppconst_used]);
4522 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004523 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4524 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004525 }
4526 else
4527 {
4528 generate_ppconst(cctx, ppconst);
4529 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4530 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531
4532 // evaluate the second expression; any type is accepted
4533 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004534 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004535 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004536 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004537 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538
Bram Moolenaara5565e42020-05-09 15:44:01 +02004539 if (!has_const_expr)
4540 {
4541 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542
Bram Moolenaara5565e42020-05-09 15:44:01 +02004543 // remember the type and drop it
4544 --stack->ga_len;
4545 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546
Bram Moolenaara5565e42020-05-09 15:44:01 +02004547 end_idx = instr->ga_len;
4548 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4549
4550 // jump here from JUMP_IF_FALSE
4551 isn = ((isn_T *)instr->ga_data) + alt_idx;
4552 isn->isn_arg.jump.jump_where = instr->ga_len;
4553 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554
4555 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004556 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 if (*p != ':')
4558 {
4559 emsg(_(e_missing_colon));
4560 return FAIL;
4561 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004562 if (next != NULL)
4563 {
4564 *arg = next_line_from_context(cctx, TRUE);
4565 p = skipwhite(*arg);
4566 }
4567
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004568 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4569 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004571 return FAIL;
4572 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573
4574 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004575 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004576 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4577 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004579 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004580 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004581 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004582 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583
Bram Moolenaara5565e42020-05-09 15:44:01 +02004584 if (!has_const_expr)
4585 {
4586 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587
Bram Moolenaara5565e42020-05-09 15:44:01 +02004588 // If the types differ, the result has a more generic type.
4589 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4590 common_type(type1, type2, &type2, cctx->ctx_type_list);
4591
4592 // jump here from JUMP_ALWAYS
4593 isn = ((isn_T *)instr->ga_data) + end_idx;
4594 isn->isn_arg.jump.jump_where = instr->ga_len;
4595 }
4596
4597 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 }
4599 return OK;
4600}
4601
4602/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004603 * Toplevel expression.
4604 */
4605 static int
4606compile_expr0(char_u **arg, cctx_T *cctx)
4607{
4608 ppconst_T ppconst;
4609
4610 CLEAR_FIELD(ppconst);
4611 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4612 {
4613 clear_ppconst(&ppconst);
4614 return FAIL;
4615 }
4616 if (generate_ppconst(cctx, &ppconst) == FAIL)
4617 return FAIL;
4618 return OK;
4619}
4620
4621/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 * compile "return [expr]"
4623 */
4624 static char_u *
4625compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4626{
4627 char_u *p = arg;
4628 garray_T *stack = &cctx->ctx_type_stack;
4629 type_T *stack_type;
4630
4631 if (*p != NUL && *p != '|' && *p != '\n')
4632 {
4633 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004634 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 return NULL;
4636
4637 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4638 if (set_return_type)
4639 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004640 else
4641 {
4642 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4643 && stack_type->tt_type != VAR_VOID
4644 && stack_type->tt_type != VAR_UNKNOWN)
4645 {
4646 emsg(_("E1096: Returning a value in a function without a return type"));
4647 return NULL;
4648 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004649 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4650 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004652 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 }
4654 else
4655 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004656 // "set_return_type" cannot be TRUE, only used for a lambda which
4657 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004658 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4659 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 {
4661 emsg(_("E1003: Missing return value"));
4662 return NULL;
4663 }
4664
4665 // No argument, return zero.
4666 generate_PUSHNR(cctx, 0);
4667 }
4668
4669 if (generate_instr(cctx, ISN_RETURN) == NULL)
4670 return NULL;
4671
4672 // "return val | endif" is possible
4673 return skipwhite(p);
4674}
4675
4676/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004677 * Get a line from the compilation context, compatible with exarg_T getline().
4678 * Return a pointer to the line in allocated memory.
4679 * Return NULL for end-of-file or some error.
4680 */
4681 static char_u *
4682exarg_getline(
4683 int c UNUSED,
4684 void *cookie,
4685 int indent UNUSED,
4686 int do_concat UNUSED)
4687{
4688 cctx_T *cctx = (cctx_T *)cookie;
4689
4690 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4691 {
4692 iemsg("Heredoc got to end");
4693 return NULL;
4694 }
4695 ++cctx->ctx_lnum;
4696 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4697 [cctx->ctx_lnum]);
4698}
4699
4700/*
4701 * Compile a nested :def command.
4702 */
4703 static char_u *
4704compile_nested_function(exarg_T *eap, cctx_T *cctx)
4705{
4706 char_u *name_start = eap->arg;
4707 char_u *name_end = to_name_end(eap->arg, FALSE);
4708 char_u *name = get_lambda_name();
4709 lvar_T *lvar;
4710 ufunc_T *ufunc;
4711
4712 eap->arg = name_end;
4713 eap->getline = exarg_getline;
4714 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004715 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004716 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004717 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004718
Bram Moolenaar822ba242020-05-24 23:00:18 +02004719 if (ufunc == NULL)
4720 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004721 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004722 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004723 return NULL;
4724
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004725 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004726 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004727 TRUE, ufunc->uf_func_type);
4728
4729 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4730 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4731 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004732
Bram Moolenaar61a89812020-05-07 16:58:17 +02004733 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004734 return (char_u *)"";
4735}
4736
4737/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 * Return the length of an assignment operator, or zero if there isn't one.
4739 */
4740 int
4741assignment_len(char_u *p, int *heredoc)
4742{
4743 if (*p == '=')
4744 {
4745 if (p[1] == '<' && p[2] == '<')
4746 {
4747 *heredoc = TRUE;
4748 return 3;
4749 }
4750 return 1;
4751 }
4752 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4753 return 2;
4754 if (STRNCMP(p, "..=", 3) == 0)
4755 return 3;
4756 return 0;
4757}
4758
4759// words that cannot be used as a variable
4760static char *reserved[] = {
4761 "true",
4762 "false",
4763 NULL
4764};
4765
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004766typedef enum {
4767 dest_local,
4768 dest_option,
4769 dest_env,
4770 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004771 dest_buffer,
4772 dest_window,
4773 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004774 dest_vimvar,
4775 dest_script,
4776 dest_reg,
4777} assign_dest_T;
4778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004780 * Generate the load instruction for "name".
4781 */
4782 static void
4783generate_loadvar(
4784 cctx_T *cctx,
4785 assign_dest_T dest,
4786 char_u *name,
4787 lvar_T *lvar,
4788 type_T *type)
4789{
4790 switch (dest)
4791 {
4792 case dest_option:
4793 // TODO: check the option exists
4794 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4795 break;
4796 case dest_global:
4797 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4798 break;
4799 case dest_buffer:
4800 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4801 break;
4802 case dest_window:
4803 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4804 break;
4805 case dest_tab:
4806 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4807 break;
4808 case dest_script:
4809 compile_load_scriptvar(cctx,
4810 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4811 break;
4812 case dest_env:
4813 // Include $ in the name here
4814 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4815 break;
4816 case dest_reg:
4817 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4818 break;
4819 case dest_vimvar:
4820 generate_LOADV(cctx, name + 2, TRUE);
4821 break;
4822 case dest_local:
4823 if (lvar->lv_from_outer)
4824 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4825 NULL, type);
4826 else
4827 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4828 break;
4829 }
4830}
4831
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004832 void
4833vim9_declare_error(char_u *name)
4834{
4835 char *scope = "";
4836
4837 switch (*name)
4838 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004839 case 'g': scope = _("global"); break;
4840 case 'b': scope = _("buffer"); break;
4841 case 'w': scope = _("window"); break;
4842 case 't': scope = _("tab"); break;
4843 case 'v': scope = "v:"; break;
4844 case '$': semsg(_(e_declare_env_var), name); return;
4845 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004846 }
4847 semsg(_(e_declare_var), scope, name);
4848}
4849
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004850/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004851 * Compile declaration and assignment:
4852 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004854 * Return NULL for an error.
4855 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 */
4857 static char_u *
4858compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4859{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004860 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004862 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 char_u *ret = NULL;
4864 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004865 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004868 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 int oplen = 0;
4871 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004872 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004873 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004874 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004878 // Skip over the "var" or "[var, var]" to get to any "=".
4879 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4880 if (p == NULL)
4881 return *arg == '[' ? arg : NULL;
4882
4883 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004885 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 return NULL;
4887 }
4888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 sp = p;
4890 p = skipwhite(p);
4891 op = p;
4892 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004893
4894 if (var_count > 0 && oplen == 0)
4895 // can be something like "[1, 2]->func()"
4896 return arg;
4897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4899 {
4900 char_u buf[4];
4901
4902 vim_strncpy(buf, op, oplen);
4903 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004904 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004905 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 if (heredoc)
4908 {
4909 list_T *l;
4910 listitem_T *li;
4911
4912 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004913 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004915 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916
4917 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004918 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919 {
4920 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4921 li->li_tv.vval.v_string = NULL;
4922 }
4923 generate_NEWLIST(cctx, l->lv_len);
4924 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004925 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 list_free(l);
4927 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004928 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004930 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004932 // for "[var, var] = expr" evaluate the expression here, loop over the
4933 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004934
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004935 p = skipwhite(op + oplen);
4936 if (compile_expr0(&p, cctx) == FAIL)
4937 return NULL;
4938 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004940 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004942 type_T *stacktype;
4943
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004944 stacktype = stack->ga_len == 0 ? &t_void
4945 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004946 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004948 emsg(_(e_cannot_use_void));
4949 goto theend;
4950 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004951 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004952 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004953 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4954 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004955 }
4956 }
4957
4958 /*
4959 * Loop over variables in "[var, var] = expr".
4960 * For "var = expr" and "let var: type" this is done only once.
4961 */
4962 if (var_count > 0)
4963 var_start = skipwhite(arg + 1); // skip over the "["
4964 else
4965 var_start = arg;
4966 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4967 {
4968 char_u *var_end = skip_var_one(var_start, FALSE);
4969 size_t varlen;
4970 int new_local = FALSE;
4971 int opt_type;
4972 int opt_flags = 0;
4973 assign_dest_T dest = dest_local;
4974 int vimvaridx = -1;
4975 lvar_T *lvar = NULL;
4976 lvar_T arg_lvar;
4977 int has_type = FALSE;
4978 int has_index = FALSE;
4979 int instr_count = -1;
4980
4981 p = (*var_start == '&' || *var_start == '$'
4982 || *var_start == '@') ? var_start + 1 : var_start;
4983 p = to_name_end(p, TRUE);
4984
4985 // "a: type" is declaring variable "a" with a type, not "a:".
4986 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4987 --var_end;
4988 if (is_decl && p == var_start + 2 && p[-1] == ':')
4989 --p;
4990
4991 varlen = p - var_start;
4992 vim_free(name);
4993 name = vim_strnsave(var_start, varlen);
4994 if (name == NULL)
4995 return NULL;
4996 if (!heredoc)
4997 type = &t_any;
4998
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004999 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005000 {
5001 if (*var_start == '&')
5002 {
5003 int cc;
5004 long numval;
5005
5006 dest = dest_option;
5007 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005009 emsg(_(e_const_option));
5010 goto theend;
5011 }
5012 if (is_decl)
5013 {
5014 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5015 goto theend;
5016 }
5017 p = var_start;
5018 p = find_option_end(&p, &opt_flags);
5019 if (p == NULL)
5020 {
5021 // cannot happen?
5022 emsg(_(e_letunexp));
5023 goto theend;
5024 }
5025 cc = *p;
5026 *p = NUL;
5027 opt_type = get_option_value(var_start + 1, &numval,
5028 NULL, opt_flags);
5029 *p = cc;
5030 if (opt_type == -3)
5031 {
5032 semsg(_(e_unknown_option), var_start);
5033 goto theend;
5034 }
5035 if (opt_type == -2 || opt_type == 0)
5036 type = &t_string;
5037 else
5038 type = &t_number; // both number and boolean option
5039 }
5040 else if (*var_start == '$')
5041 {
5042 dest = dest_env;
5043 type = &t_string;
5044 if (is_decl)
5045 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005046 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005047 goto theend;
5048 }
5049 }
5050 else if (*var_start == '@')
5051 {
5052 if (!valid_yank_reg(var_start[1], TRUE))
5053 {
5054 emsg_invreg(var_start[1]);
5055 goto theend;
5056 }
5057 dest = dest_reg;
5058 type = &t_string;
5059 if (is_decl)
5060 {
5061 semsg(_("E1066: Cannot declare a register: %s"), name);
5062 goto theend;
5063 }
5064 }
5065 else if (STRNCMP(var_start, "g:", 2) == 0)
5066 {
5067 dest = dest_global;
5068 if (is_decl)
5069 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005070 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005071 goto theend;
5072 }
5073 }
5074 else if (STRNCMP(var_start, "b:", 2) == 0)
5075 {
5076 dest = dest_buffer;
5077 if (is_decl)
5078 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005079 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005080 goto theend;
5081 }
5082 }
5083 else if (STRNCMP(var_start, "w:", 2) == 0)
5084 {
5085 dest = dest_window;
5086 if (is_decl)
5087 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005088 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 goto theend;
5090 }
5091 }
5092 else if (STRNCMP(var_start, "t:", 2) == 0)
5093 {
5094 dest = dest_tab;
5095 if (is_decl)
5096 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005097 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005098 goto theend;
5099 }
5100 }
5101 else if (STRNCMP(var_start, "v:", 2) == 0)
5102 {
5103 typval_T *vtv;
5104 int di_flags;
5105
5106 vimvaridx = find_vim_var(name + 2, &di_flags);
5107 if (vimvaridx < 0)
5108 {
5109 semsg(_(e_var_notfound), var_start);
5110 goto theend;
5111 }
5112 // We use the current value of "sandbox" here, is that OK?
5113 if (var_check_ro(di_flags, name, FALSE))
5114 goto theend;
5115 dest = dest_vimvar;
5116 vtv = get_vim_var_tv(vimvaridx);
5117 type = typval2type(vtv);
5118 if (is_decl)
5119 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005120 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005121 goto theend;
5122 }
5123 }
5124 else
5125 {
5126 int idx;
5127
5128 for (idx = 0; reserved[idx] != NULL; ++idx)
5129 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005130 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005131 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005132 goto theend;
5133 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005134
5135 lvar = lookup_local(var_start, varlen, cctx);
5136 if (lvar == NULL)
5137 {
5138 CLEAR_FIELD(arg_lvar);
5139 if (lookup_arg(var_start, varlen,
5140 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5141 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005142 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005143 if (is_decl)
5144 {
5145 semsg(_(e_used_as_arg), name);
5146 goto theend;
5147 }
5148 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005149 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005150 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005151 if (lvar != NULL)
5152 {
5153 if (is_decl)
5154 {
5155 semsg(_("E1017: Variable already declared: %s"), name);
5156 goto theend;
5157 }
5158 else if (lvar->lv_const)
5159 {
5160 semsg(_("E1018: Cannot assign to a constant: %s"),
5161 name);
5162 goto theend;
5163 }
5164 }
5165 else if (STRNCMP(var_start, "s:", 2) == 0
5166 || lookup_script(var_start, varlen) == OK
5167 || find_imported(var_start, varlen, cctx) != NULL)
5168 {
5169 dest = dest_script;
5170 if (is_decl)
5171 {
5172 semsg(_("E1054: Variable already declared in the script: %s"),
5173 name);
5174 goto theend;
5175 }
5176 }
5177 else if (name[1] == ':' && name[2] != NUL)
5178 {
5179 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5180 name);
5181 goto theend;
5182 }
5183 else if (!is_decl)
5184 {
5185 semsg(_("E1089: unknown variable: %s"), name);
5186 goto theend;
5187 }
5188 }
5189 }
5190
5191 // handle "a:name" as a name, not index "name" on "a"
5192 if (varlen > 1 || var_start[varlen] != ':')
5193 p = var_end;
5194
5195 if (dest != dest_option)
5196 {
5197 if (is_decl && *p == ':')
5198 {
5199 // parse optional type: "let var: type = expr"
5200 if (!VIM_ISWHITE(p[1]))
5201 {
5202 semsg(_(e_white_after), ":");
5203 goto theend;
5204 }
5205 p = skipwhite(p + 1);
5206 type = parse_type(&p, cctx->ctx_type_list);
5207 has_type = TRUE;
5208 }
5209 else if (lvar != NULL)
5210 type = lvar->lv_type;
5211 }
5212
5213 if (oplen == 3 && !heredoc && dest != dest_global
5214 && type->tt_type != VAR_STRING
5215 && type->tt_type != VAR_ANY)
5216 {
5217 emsg(_("E1019: Can only concatenate to string"));
5218 goto theend;
5219 }
5220
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005221 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005222 {
5223 if (oplen > 1 && !heredoc)
5224 {
5225 // +=, /=, etc. require an existing variable
5226 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5227 name);
5228 goto theend;
5229 }
5230
5231 // new local variable
5232 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5233 goto theend;
5234 lvar = reserve_local(cctx, var_start, varlen,
5235 cmdidx == CMD_const, type);
5236 if (lvar == NULL)
5237 goto theend;
5238 new_local = TRUE;
5239 }
5240
5241 member_type = type;
5242 if (var_end > var_start + varlen)
5243 {
5244 // Something follows after the variable: "var[idx]".
5245 if (is_decl)
5246 {
5247 emsg(_("E1087: cannot use an index when declaring a variable"));
5248 goto theend;
5249 }
5250
5251 if (var_start[varlen] == '[')
5252 {
5253 has_index = TRUE;
5254 if (type->tt_member == NULL)
5255 {
5256 semsg(_("E1088: cannot use an index on %s"), name);
5257 goto theend;
5258 }
5259 member_type = type->tt_member;
5260 }
5261 else
5262 {
5263 semsg("Not supported yet: %s", var_start);
5264 goto theend;
5265 }
5266 }
5267 else if (lvar == &arg_lvar)
5268 {
5269 semsg(_("E1090: Cannot assign to argument %s"), name);
5270 goto theend;
5271 }
5272
5273 if (!heredoc)
5274 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005275 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005276 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005277 if (oplen > 0 && var_count == 0)
5278 {
5279 // skip over the "=" and the expression
5280 p = skipwhite(op + oplen);
5281 compile_expr0(&p, cctx);
5282 }
5283 }
5284 else if (oplen > 0)
5285 {
5286 type_T *stacktype;
5287
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 // For "var = expr" evaluate the expression.
5289 if (var_count == 0)
5290 {
5291 int r;
5292
5293 // for "+=", "*=", "..=" etc. first load the current value
5294 if (*op != '=')
5295 {
5296 generate_loadvar(cctx, dest, name, lvar, type);
5297
5298 if (has_index)
5299 {
5300 // TODO: get member from list or dict
5301 emsg("Index with operation not supported yet");
5302 goto theend;
5303 }
5304 }
5305
5306 // Compile the expression. Temporarily hide the new local
5307 // variable here, it is not available to this expression.
5308 if (new_local)
5309 --cctx->ctx_locals.ga_len;
5310 instr_count = instr->ga_len;
5311 p = skipwhite(op + oplen);
5312 r = compile_expr0(&p, cctx);
5313 if (new_local)
5314 ++cctx->ctx_locals.ga_len;
5315 if (r == FAIL)
5316 goto theend;
5317 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005318 else if (semicolon && var_idx == var_count - 1)
5319 {
5320 // For "[var; var] = expr" get the rest of the list
5321 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5322 goto theend;
5323 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005324 else
5325 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005326 // For "[var, var] = expr" get the "var_idx" item from the
5327 // list.
5328 if (generate_GETITEM(cctx, var_idx) == FAIL)
5329 return FAIL;
5330 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005331
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005332 stacktype = stack->ga_len == 0 ? &t_void
5333 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5334 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005335 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005336 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005337 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005338 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005339 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005340 emsg(_(e_cannot_use_void));
5341 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005342 }
5343 else
5344 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005345 // An empty list or dict has a &t_void member,
5346 // for a variable that implies &t_any.
5347 if (stacktype == &t_list_empty)
5348 lvar->lv_type = &t_list_any;
5349 else if (stacktype == &t_dict_empty)
5350 lvar->lv_type = &t_dict_any;
5351 else
5352 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005353 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005354 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005355 else
5356 {
5357 type_T *use_type = lvar->lv_type;
5358
5359 if (has_index)
5360 {
5361 use_type = use_type->tt_member;
5362 if (use_type == NULL)
5363 use_type = &t_void;
5364 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005365 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005366 == FAIL)
5367 goto theend;
5368 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005369 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005370 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005371 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005372 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005374 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005375 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005376 emsg(_(e_const_req_value));
5377 goto theend;
5378 }
5379 else if (!has_type || dest == dest_option)
5380 {
5381 emsg(_(e_type_req));
5382 goto theend;
5383 }
5384 else
5385 {
5386 // variables are always initialized
5387 if (ga_grow(instr, 1) == FAIL)
5388 goto theend;
5389 switch (member_type->tt_type)
5390 {
5391 case VAR_BOOL:
5392 generate_PUSHBOOL(cctx, VVAL_FALSE);
5393 break;
5394 case VAR_FLOAT:
5395#ifdef FEAT_FLOAT
5396 generate_PUSHF(cctx, 0.0);
5397#endif
5398 break;
5399 case VAR_STRING:
5400 generate_PUSHS(cctx, NULL);
5401 break;
5402 case VAR_BLOB:
5403 generate_PUSHBLOB(cctx, NULL);
5404 break;
5405 case VAR_FUNC:
5406 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5407 break;
5408 case VAR_LIST:
5409 generate_NEWLIST(cctx, 0);
5410 break;
5411 case VAR_DICT:
5412 generate_NEWDICT(cctx, 0);
5413 break;
5414 case VAR_JOB:
5415 generate_PUSHJOB(cctx, NULL);
5416 break;
5417 case VAR_CHANNEL:
5418 generate_PUSHCHANNEL(cctx, NULL);
5419 break;
5420 case VAR_NUMBER:
5421 case VAR_UNKNOWN:
5422 case VAR_ANY:
5423 case VAR_PARTIAL:
5424 case VAR_VOID:
5425 case VAR_SPECIAL: // cannot happen
5426 generate_PUSHNR(cctx, 0);
5427 break;
5428 }
5429 }
5430 if (var_count == 0)
5431 end = p;
5432 }
5433
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005434 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005435 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005436 break;
5437
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005438 if (oplen > 0 && *op != '=')
5439 {
5440 type_T *expected = &t_number;
5441 type_T *stacktype;
5442
5443 // TODO: if type is known use float or any operation
5444
5445 if (*op == '.')
5446 expected = &t_string;
5447 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005448 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005449 goto theend;
5450
5451 if (*op == '.')
5452 generate_instr_drop(cctx, ISN_CONCAT, 1);
5453 else
5454 {
5455 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5456
5457 if (isn == NULL)
5458 goto theend;
5459 switch (*op)
5460 {
5461 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5462 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5463 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5464 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5465 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005467 }
5468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005469
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005470 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005471 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005472 int r;
5473
5474 // Compile the "idx" in "var[idx]".
5475 if (new_local)
5476 --cctx->ctx_locals.ga_len;
5477 p = skipwhite(var_start + varlen + 1);
5478 r = compile_expr0(&p, cctx);
5479 if (new_local)
5480 ++cctx->ctx_locals.ga_len;
5481 if (r == FAIL)
5482 goto theend;
5483 if (*skipwhite(p) != ']')
5484 {
5485 emsg(_(e_missbrac));
5486 goto theend;
5487 }
5488 if (type->tt_type == VAR_DICT
5489 && may_generate_2STRING(-1, cctx) == FAIL)
5490 goto theend;
5491 if (type->tt_type == VAR_LIST
5492 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005493 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005494 {
5495 emsg(_(e_number_exp));
5496 goto theend;
5497 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005498
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005499 // Load the dict or list. On the stack we then have:
5500 // - value
5501 // - index
5502 // - variable
5503 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005504
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005505 if (type->tt_type == VAR_LIST)
5506 {
5507 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5508 return FAIL;
5509 }
5510 else if (type->tt_type == VAR_DICT)
5511 {
5512 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5513 return FAIL;
5514 }
5515 else
5516 {
5517 emsg(_(e_listreq));
5518 goto theend;
5519 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005520 }
5521 else
5522 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005523 switch (dest)
5524 {
5525 case dest_option:
5526 generate_STOREOPT(cctx, name + 1, opt_flags);
5527 break;
5528 case dest_global:
5529 // include g: with the name, easier to execute that way
5530 generate_STORE(cctx, ISN_STOREG, 0, name);
5531 break;
5532 case dest_buffer:
5533 // include b: with the name, easier to execute that way
5534 generate_STORE(cctx, ISN_STOREB, 0, name);
5535 break;
5536 case dest_window:
5537 // include w: with the name, easier to execute that way
5538 generate_STORE(cctx, ISN_STOREW, 0, name);
5539 break;
5540 case dest_tab:
5541 // include t: with the name, easier to execute that way
5542 generate_STORE(cctx, ISN_STORET, 0, name);
5543 break;
5544 case dest_env:
5545 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5546 break;
5547 case dest_reg:
5548 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5549 break;
5550 case dest_vimvar:
5551 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5552 break;
5553 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005554 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005555 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5556 imported_T *import = NULL;
5557 int sid = current_sctx.sc_sid;
5558 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005559
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005560 if (name[1] != ':')
5561 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005562 import = find_imported(name, 0, cctx);
5563 if (import != NULL)
5564 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005565 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005566
5567 idx = get_script_item_idx(sid, rawname, TRUE);
5568 // TODO: specific type
5569 if (idx < 0)
5570 {
5571 char_u *name_s = name;
5572
5573 // Include s: in the name for store_var()
5574 if (name[1] != ':')
5575 {
5576 int len = (int)STRLEN(name) + 3;
5577
5578 name_s = alloc(len);
5579 if (name_s == NULL)
5580 name_s = name;
5581 else
5582 vim_snprintf((char *)name_s, len,
5583 "s:%s", name);
5584 }
5585 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005586 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005587 if (name_s != name)
5588 vim_free(name_s);
5589 }
5590 else
5591 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005592 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005593 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005594 break;
5595 case dest_local:
5596 if (lvar != NULL)
5597 {
5598 isn_T *isn = ((isn_T *)instr->ga_data)
5599 + instr->ga_len - 1;
5600
5601 // optimization: turn "var = 123" from ISN_PUSHNR +
5602 // ISN_STORE into ISN_STORENR
5603 if (!lvar->lv_from_outer
5604 && instr->ga_len == instr_count + 1
5605 && isn->isn_type == ISN_PUSHNR)
5606 {
5607 varnumber_T val = isn->isn_arg.number;
5608
5609 isn->isn_type = ISN_STORENR;
5610 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5611 isn->isn_arg.storenr.stnr_val = val;
5612 if (stack->ga_len > 0)
5613 --stack->ga_len;
5614 }
5615 else if (lvar->lv_from_outer)
5616 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005617 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005618 else
5619 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5620 }
5621 break;
5622 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005623 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005624
5625 if (var_idx + 1 < var_count)
5626 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005627 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005628
5629 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005630 if (var_count > 0 && !semicolon)
5631 {
5632 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5633 goto theend;
5634 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005635
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005636 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005637
5638theend:
5639 vim_free(name);
5640 return ret;
5641}
5642
5643/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005644 * Check if "name" can be "unlet".
5645 */
5646 int
5647check_vim9_unlet(char_u *name)
5648{
5649 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5650 {
5651 semsg(_("E1081: Cannot unlet %s"), name);
5652 return FAIL;
5653 }
5654 return OK;
5655}
5656
5657/*
5658 * Callback passed to ex_unletlock().
5659 */
5660 static int
5661compile_unlet(
5662 lval_T *lvp,
5663 char_u *name_end,
5664 exarg_T *eap,
5665 int deep UNUSED,
5666 void *coookie)
5667{
5668 cctx_T *cctx = coookie;
5669
5670 if (lvp->ll_tv == NULL)
5671 {
5672 char_u *p = lvp->ll_name;
5673 int cc = *name_end;
5674 int ret = OK;
5675
5676 // Normal name. Only supports g:, w:, t: and b: namespaces.
5677 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005678 if (*p == '$')
5679 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5680 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005681 ret = FAIL;
5682 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005683 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005684
5685 *name_end = cc;
5686 return ret;
5687 }
5688
5689 // TODO: unlet {list}[idx]
5690 // TODO: unlet {dict}[key]
5691 emsg("Sorry, :unlet not fully implemented yet");
5692 return FAIL;
5693}
5694
5695/*
5696 * compile "unlet var", "lock var" and "unlock var"
5697 * "arg" points to "var".
5698 */
5699 static char_u *
5700compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5701{
5702 char_u *p = arg;
5703
5704 if (eap->cmdidx != CMD_unlet)
5705 {
5706 emsg("Sorry, :lock and unlock not implemented yet");
5707 return NULL;
5708 }
5709
5710 if (*p == '!')
5711 {
5712 p = skipwhite(p + 1);
5713 eap->forceit = TRUE;
5714 }
5715
5716 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5717 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5718}
5719
5720/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005721 * Compile an :import command.
5722 */
5723 static char_u *
5724compile_import(char_u *arg, cctx_T *cctx)
5725{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005726 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005727}
5728
5729/*
5730 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5731 */
5732 static int
5733compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5734{
5735 garray_T *instr = &cctx->ctx_instr;
5736 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5737
5738 if (endlabel == NULL)
5739 return FAIL;
5740 endlabel->el_next = *el;
5741 *el = endlabel;
5742 endlabel->el_end_label = instr->ga_len;
5743
5744 generate_JUMP(cctx, when, 0);
5745 return OK;
5746}
5747
5748 static void
5749compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5750{
5751 garray_T *instr = &cctx->ctx_instr;
5752
5753 while (*el != NULL)
5754 {
5755 endlabel_T *cur = (*el);
5756 isn_T *isn;
5757
5758 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5759 isn->isn_arg.jump.jump_where = instr->ga_len;
5760 *el = cur->el_next;
5761 vim_free(cur);
5762 }
5763}
5764
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005765 static void
5766compile_free_jump_to_end(endlabel_T **el)
5767{
5768 while (*el != NULL)
5769 {
5770 endlabel_T *cur = (*el);
5771
5772 *el = cur->el_next;
5773 vim_free(cur);
5774 }
5775}
5776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777/*
5778 * Create a new scope and set up the generic items.
5779 */
5780 static scope_T *
5781new_scope(cctx_T *cctx, scopetype_T type)
5782{
5783 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5784
5785 if (scope == NULL)
5786 return NULL;
5787 scope->se_outer = cctx->ctx_scope;
5788 cctx->ctx_scope = scope;
5789 scope->se_type = type;
5790 scope->se_local_count = cctx->ctx_locals.ga_len;
5791 return scope;
5792}
5793
5794/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005795 * Free the current scope and go back to the outer scope.
5796 */
5797 static void
5798drop_scope(cctx_T *cctx)
5799{
5800 scope_T *scope = cctx->ctx_scope;
5801
5802 if (scope == NULL)
5803 {
5804 iemsg("calling drop_scope() without a scope");
5805 return;
5806 }
5807 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005808 switch (scope->se_type)
5809 {
5810 case IF_SCOPE:
5811 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5812 case FOR_SCOPE:
5813 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5814 case WHILE_SCOPE:
5815 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5816 case TRY_SCOPE:
5817 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5818 case NO_SCOPE:
5819 case BLOCK_SCOPE:
5820 break;
5821 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005822 vim_free(scope);
5823}
5824
5825/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005826 * compile "if expr"
5827 *
5828 * "if expr" Produces instructions:
5829 * EVAL expr Push result of "expr"
5830 * JUMP_IF_FALSE end
5831 * ... body ...
5832 * end:
5833 *
5834 * "if expr | else" Produces instructions:
5835 * EVAL expr Push result of "expr"
5836 * JUMP_IF_FALSE else
5837 * ... body ...
5838 * JUMP_ALWAYS end
5839 * else:
5840 * ... body ...
5841 * end:
5842 *
5843 * "if expr1 | elseif expr2 | else" Produces instructions:
5844 * EVAL expr Push result of "expr"
5845 * JUMP_IF_FALSE elseif
5846 * ... body ...
5847 * JUMP_ALWAYS end
5848 * elseif:
5849 * EVAL expr Push result of "expr"
5850 * JUMP_IF_FALSE else
5851 * ... body ...
5852 * JUMP_ALWAYS end
5853 * else:
5854 * ... body ...
5855 * end:
5856 */
5857 static char_u *
5858compile_if(char_u *arg, cctx_T *cctx)
5859{
5860 char_u *p = arg;
5861 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005862 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005863 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005864 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005865 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866
Bram Moolenaara5565e42020-05-09 15:44:01 +02005867 CLEAR_FIELD(ppconst);
5868 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005869 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005870 clear_ppconst(&ppconst);
5871 return NULL;
5872 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005873 if (cctx->ctx_skip == SKIP_YES)
5874 clear_ppconst(&ppconst);
5875 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005876 {
5877 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005878 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005879 clear_ppconst(&ppconst);
5880 }
5881 else
5882 {
5883 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005884 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005885 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005886 return NULL;
5887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005888
5889 scope = new_scope(cctx, IF_SCOPE);
5890 if (scope == NULL)
5891 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005892 scope->se_skip_save = skip_save;
5893 // "is_had_return" will be reset if any block does not end in :return
5894 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005896 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005897 {
5898 // "where" is set when ":elseif", "else" or ":endif" is found
5899 scope->se_u.se_if.is_if_label = instr->ga_len;
5900 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5901 }
5902 else
5903 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005904
5905 return p;
5906}
5907
5908 static char_u *
5909compile_elseif(char_u *arg, cctx_T *cctx)
5910{
5911 char_u *p = arg;
5912 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005913 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005914 isn_T *isn;
5915 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005916 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005917
5918 if (scope == NULL || scope->se_type != IF_SCOPE)
5919 {
5920 emsg(_(e_elseif_without_if));
5921 return NULL;
5922 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005923 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005924 if (!cctx->ctx_had_return)
5925 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005926
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005927 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005928 {
5929 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005930 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005931 return NULL;
5932 // previous "if" or "elseif" jumps here
5933 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5934 isn->isn_arg.jump.jump_where = instr->ga_len;
5935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005937 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005938 CLEAR_FIELD(ppconst);
5939 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005940 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005941 clear_ppconst(&ppconst);
5942 return NULL;
5943 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005944 if (scope->se_skip_save == SKIP_YES)
5945 clear_ppconst(&ppconst);
5946 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005947 {
5948 // The expression results in a constant.
5949 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005950 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005951 clear_ppconst(&ppconst);
5952 scope->se_u.se_if.is_if_label = -1;
5953 }
5954 else
5955 {
5956 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005957 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005958 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005959 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005960
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005961 // "where" is set when ":elseif", "else" or ":endif" is found
5962 scope->se_u.se_if.is_if_label = instr->ga_len;
5963 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005965
5966 return p;
5967}
5968
5969 static char_u *
5970compile_else(char_u *arg, cctx_T *cctx)
5971{
5972 char_u *p = arg;
5973 garray_T *instr = &cctx->ctx_instr;
5974 isn_T *isn;
5975 scope_T *scope = cctx->ctx_scope;
5976
5977 if (scope == NULL || scope->se_type != IF_SCOPE)
5978 {
5979 emsg(_(e_else_without_if));
5980 return NULL;
5981 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005982 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005983 if (!cctx->ctx_had_return)
5984 scope->se_u.se_if.is_had_return = FALSE;
5985 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986
Bram Moolenaarefd88552020-06-18 20:50:10 +02005987 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005988 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005989 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005990 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005991 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005992 if (!cctx->ctx_had_return
5993 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5994 JUMP_ALWAYS, cctx) == FAIL)
5995 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005996 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005997
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005998 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005999 {
6000 if (scope->se_u.se_if.is_if_label >= 0)
6001 {
6002 // previous "if" or "elseif" jumps here
6003 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6004 isn->isn_arg.jump.jump_where = instr->ga_len;
6005 scope->se_u.se_if.is_if_label = -1;
6006 }
6007 }
6008
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006009 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006010 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012
6013 return p;
6014}
6015
6016 static char_u *
6017compile_endif(char_u *arg, cctx_T *cctx)
6018{
6019 scope_T *scope = cctx->ctx_scope;
6020 ifscope_T *ifscope;
6021 garray_T *instr = &cctx->ctx_instr;
6022 isn_T *isn;
6023
6024 if (scope == NULL || scope->se_type != IF_SCOPE)
6025 {
6026 emsg(_(e_endif_without_if));
6027 return NULL;
6028 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006029 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006030 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006031 if (!cctx->ctx_had_return)
6032 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006034 if (scope->se_u.se_if.is_if_label >= 0)
6035 {
6036 // previous "if" or "elseif" jumps here
6037 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6038 isn->isn_arg.jump.jump_where = instr->ga_len;
6039 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006040 // Fill in the "end" label in jumps at the end of the blocks.
6041 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006042 cctx->ctx_skip = scope->se_skip_save;
6043
6044 // If all the blocks end in :return and there is an :else then the
6045 // had_return flag is set.
6046 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006047
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006048 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006049 return arg;
6050}
6051
6052/*
6053 * compile "for var in expr"
6054 *
6055 * Produces instructions:
6056 * PUSHNR -1
6057 * STORE loop-idx Set index to -1
6058 * EVAL expr Push result of "expr"
6059 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6060 * - if beyond end, jump to "end"
6061 * - otherwise get item from list and push it
6062 * STORE var Store item in "var"
6063 * ... body ...
6064 * JUMP top Jump back to repeat
6065 * end: DROP Drop the result of "expr"
6066 *
6067 */
6068 static char_u *
6069compile_for(char_u *arg, cctx_T *cctx)
6070{
6071 char_u *p;
6072 size_t varlen;
6073 garray_T *instr = &cctx->ctx_instr;
6074 garray_T *stack = &cctx->ctx_type_stack;
6075 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006076 lvar_T *loop_lvar; // loop iteration variable
6077 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006078 type_T *vartype;
6079
6080 // TODO: list of variables: "for [key, value] in dict"
6081 // parse "var"
6082 for (p = arg; eval_isnamec1(*p); ++p)
6083 ;
6084 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006085 var_lvar = lookup_local(arg, varlen, cctx);
6086 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006087 {
6088 semsg(_("E1023: variable already defined: %s"), arg);
6089 return NULL;
6090 }
6091
6092 // consume "in"
6093 p = skipwhite(p);
6094 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6095 {
6096 emsg(_(e_missing_in));
6097 return NULL;
6098 }
6099 p = skipwhite(p + 2);
6100
6101
6102 scope = new_scope(cctx, FOR_SCOPE);
6103 if (scope == NULL)
6104 return NULL;
6105
6106 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006107 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6108 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006109 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006110 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006111 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006114
6115 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006116 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6117 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006118 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006119 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006120 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006121 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006123
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006124 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125
6126 // compile "expr", it remains on the stack until "endfor"
6127 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006128 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006129 {
6130 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006131 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006133
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006134 // Now that we know the type of "var", check that it is a list, now or at
6135 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006136 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006137 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006138 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006139 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006140 return NULL;
6141 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006142 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006143 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144
6145 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006146 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006147
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006148 generate_FOR(cctx, loop_lvar->lv_idx);
6149 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006150
6151 return arg;
6152}
6153
6154/*
6155 * compile "endfor"
6156 */
6157 static char_u *
6158compile_endfor(char_u *arg, cctx_T *cctx)
6159{
6160 garray_T *instr = &cctx->ctx_instr;
6161 scope_T *scope = cctx->ctx_scope;
6162 forscope_T *forscope;
6163 isn_T *isn;
6164
6165 if (scope == NULL || scope->se_type != FOR_SCOPE)
6166 {
6167 emsg(_(e_for));
6168 return NULL;
6169 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006170 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006171 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006172 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006173
6174 // At end of ":for" scope jump back to the FOR instruction.
6175 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6176
6177 // Fill in the "end" label in the FOR statement so it can jump here
6178 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6179 isn->isn_arg.forloop.for_end = instr->ga_len;
6180
6181 // Fill in the "end" label any BREAK statements
6182 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6183
6184 // Below the ":for" scope drop the "expr" list from the stack.
6185 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6186 return NULL;
6187
6188 vim_free(scope);
6189
6190 return arg;
6191}
6192
6193/*
6194 * compile "while expr"
6195 *
6196 * Produces instructions:
6197 * top: EVAL expr Push result of "expr"
6198 * JUMP_IF_FALSE end jump if false
6199 * ... body ...
6200 * JUMP top Jump back to repeat
6201 * end:
6202 *
6203 */
6204 static char_u *
6205compile_while(char_u *arg, cctx_T *cctx)
6206{
6207 char_u *p = arg;
6208 garray_T *instr = &cctx->ctx_instr;
6209 scope_T *scope;
6210
6211 scope = new_scope(cctx, WHILE_SCOPE);
6212 if (scope == NULL)
6213 return NULL;
6214
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006215 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216
6217 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006218 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219 return NULL;
6220
6221 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006222 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006223 JUMP_IF_FALSE, cctx) == FAIL)
6224 return FAIL;
6225
6226 return p;
6227}
6228
6229/*
6230 * compile "endwhile"
6231 */
6232 static char_u *
6233compile_endwhile(char_u *arg, cctx_T *cctx)
6234{
6235 scope_T *scope = cctx->ctx_scope;
6236
6237 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6238 {
6239 emsg(_(e_while));
6240 return NULL;
6241 }
6242 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006243 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244
6245 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006246 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247
6248 // Fill in the "end" label in the WHILE statement so it can jump here.
6249 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006250 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251
6252 vim_free(scope);
6253
6254 return arg;
6255}
6256
6257/*
6258 * compile "continue"
6259 */
6260 static char_u *
6261compile_continue(char_u *arg, cctx_T *cctx)
6262{
6263 scope_T *scope = cctx->ctx_scope;
6264
6265 for (;;)
6266 {
6267 if (scope == NULL)
6268 {
6269 emsg(_(e_continue));
6270 return NULL;
6271 }
6272 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6273 break;
6274 scope = scope->se_outer;
6275 }
6276
6277 // Jump back to the FOR or WHILE instruction.
6278 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006279 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6280 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281 return arg;
6282}
6283
6284/*
6285 * compile "break"
6286 */
6287 static char_u *
6288compile_break(char_u *arg, cctx_T *cctx)
6289{
6290 scope_T *scope = cctx->ctx_scope;
6291 endlabel_T **el;
6292
6293 for (;;)
6294 {
6295 if (scope == NULL)
6296 {
6297 emsg(_(e_break));
6298 return NULL;
6299 }
6300 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6301 break;
6302 scope = scope->se_outer;
6303 }
6304
6305 // Jump to the end of the FOR or WHILE loop.
6306 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006307 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006309 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6311 return FAIL;
6312
6313 return arg;
6314}
6315
6316/*
6317 * compile "{" start of block
6318 */
6319 static char_u *
6320compile_block(char_u *arg, cctx_T *cctx)
6321{
6322 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6323 return NULL;
6324 return skipwhite(arg + 1);
6325}
6326
6327/*
6328 * compile end of block: drop one scope
6329 */
6330 static void
6331compile_endblock(cctx_T *cctx)
6332{
6333 scope_T *scope = cctx->ctx_scope;
6334
6335 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006336 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337 vim_free(scope);
6338}
6339
6340/*
6341 * compile "try"
6342 * Creates a new scope for the try-endtry, pointing to the first catch and
6343 * finally.
6344 * Creates another scope for the "try" block itself.
6345 * TRY instruction sets up exception handling at runtime.
6346 *
6347 * "try"
6348 * TRY -> catch1, -> finally push trystack entry
6349 * ... try block
6350 * "throw {exception}"
6351 * EVAL {exception}
6352 * THROW create exception
6353 * ... try block
6354 * " catch {expr}"
6355 * JUMP -> finally
6356 * catch1: PUSH exeception
6357 * EVAL {expr}
6358 * MATCH
6359 * JUMP nomatch -> catch2
6360 * CATCH remove exception
6361 * ... catch block
6362 * " catch"
6363 * JUMP -> finally
6364 * catch2: CATCH remove exception
6365 * ... catch block
6366 * " finally"
6367 * finally:
6368 * ... finally block
6369 * " endtry"
6370 * ENDTRY pop trystack entry, may rethrow
6371 */
6372 static char_u *
6373compile_try(char_u *arg, cctx_T *cctx)
6374{
6375 garray_T *instr = &cctx->ctx_instr;
6376 scope_T *try_scope;
6377 scope_T *scope;
6378
6379 // scope that holds the jumps that go to catch/finally/endtry
6380 try_scope = new_scope(cctx, TRY_SCOPE);
6381 if (try_scope == NULL)
6382 return NULL;
6383
6384 // "catch" is set when the first ":catch" is found.
6385 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006386 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387 if (generate_instr(cctx, ISN_TRY) == NULL)
6388 return NULL;
6389
6390 // scope for the try block itself
6391 scope = new_scope(cctx, BLOCK_SCOPE);
6392 if (scope == NULL)
6393 return NULL;
6394
6395 return arg;
6396}
6397
6398/*
6399 * compile "catch {expr}"
6400 */
6401 static char_u *
6402compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6403{
6404 scope_T *scope = cctx->ctx_scope;
6405 garray_T *instr = &cctx->ctx_instr;
6406 char_u *p;
6407 isn_T *isn;
6408
6409 // end block scope from :try or :catch
6410 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6411 compile_endblock(cctx);
6412 scope = cctx->ctx_scope;
6413
6414 // Error if not in a :try scope
6415 if (scope == NULL || scope->se_type != TRY_SCOPE)
6416 {
6417 emsg(_(e_catch));
6418 return NULL;
6419 }
6420
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006421 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006422 {
6423 emsg(_("E1033: catch unreachable after catch-all"));
6424 return NULL;
6425 }
6426
6427 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006428 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006429 JUMP_ALWAYS, cctx) == FAIL)
6430 return NULL;
6431
6432 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006433 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434 if (isn->isn_arg.try.try_catch == 0)
6435 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006436 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006437 {
6438 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006439 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006440 isn->isn_arg.jump.jump_where = instr->ga_len;
6441 }
6442
6443 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006444 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006445 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006446 scope->se_u.se_try.ts_caught_all = TRUE;
6447 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006448 }
6449 else
6450 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006451 char_u *end;
6452 char_u *pat;
6453 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006454 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006455 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457 // Push v:exception, push {expr} and MATCH
6458 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6459
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006460 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006461 if (*end != *p)
6462 {
6463 semsg(_("E1067: Separator mismatch: %s"), p);
6464 vim_free(tofree);
6465 return FAIL;
6466 }
6467 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006468 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006469 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006470 len = (int)(end - tofree);
6471 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006472 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006473 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006474 if (pat == NULL)
6475 return FAIL;
6476 if (generate_PUSHS(cctx, pat) == FAIL)
6477 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006479 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6480 return NULL;
6481
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006482 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6484 return NULL;
6485 }
6486
6487 if (generate_instr(cctx, ISN_CATCH) == NULL)
6488 return NULL;
6489
6490 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6491 return NULL;
6492 return p;
6493}
6494
6495 static char_u *
6496compile_finally(char_u *arg, cctx_T *cctx)
6497{
6498 scope_T *scope = cctx->ctx_scope;
6499 garray_T *instr = &cctx->ctx_instr;
6500 isn_T *isn;
6501
6502 // end block scope from :try or :catch
6503 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6504 compile_endblock(cctx);
6505 scope = cctx->ctx_scope;
6506
6507 // Error if not in a :try scope
6508 if (scope == NULL || scope->se_type != TRY_SCOPE)
6509 {
6510 emsg(_(e_finally));
6511 return NULL;
6512 }
6513
6514 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006515 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516 if (isn->isn_arg.try.try_finally != 0)
6517 {
6518 emsg(_(e_finally_dup));
6519 return NULL;
6520 }
6521
6522 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006523 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524
Bram Moolenaar585fea72020-04-02 22:33:21 +02006525 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006526 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527 {
6528 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006529 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006530 isn->isn_arg.jump.jump_where = instr->ga_len;
6531 }
6532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 // TODO: set index in ts_finally_label jumps
6534
6535 return arg;
6536}
6537
6538 static char_u *
6539compile_endtry(char_u *arg, cctx_T *cctx)
6540{
6541 scope_T *scope = cctx->ctx_scope;
6542 garray_T *instr = &cctx->ctx_instr;
6543 isn_T *isn;
6544
6545 // end block scope from :catch or :finally
6546 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6547 compile_endblock(cctx);
6548 scope = cctx->ctx_scope;
6549
6550 // Error if not in a :try scope
6551 if (scope == NULL || scope->se_type != TRY_SCOPE)
6552 {
6553 if (scope == NULL)
6554 emsg(_(e_no_endtry));
6555 else if (scope->se_type == WHILE_SCOPE)
6556 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006557 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558 emsg(_(e_endfor));
6559 else
6560 emsg(_(e_endif));
6561 return NULL;
6562 }
6563
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006564 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006565 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6566 {
6567 emsg(_("E1032: missing :catch or :finally"));
6568 return NULL;
6569 }
6570
6571 // Fill in the "end" label in jumps at the end of the blocks, if not done
6572 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006573 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006574
6575 // End :catch or :finally scope: set value in ISN_TRY instruction
6576 if (isn->isn_arg.try.try_finally == 0)
6577 isn->isn_arg.try.try_finally = instr->ga_len;
6578 compile_endblock(cctx);
6579
6580 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6581 return NULL;
6582 return arg;
6583}
6584
6585/*
6586 * compile "throw {expr}"
6587 */
6588 static char_u *
6589compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6590{
6591 char_u *p = skipwhite(arg);
6592
Bram Moolenaara5565e42020-05-09 15:44:01 +02006593 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 return NULL;
6595 if (may_generate_2STRING(-1, cctx) == FAIL)
6596 return NULL;
6597 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6598 return NULL;
6599
6600 return p;
6601}
6602
6603/*
6604 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006605 * compile "echomsg expr"
6606 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006607 * compile "execute expr"
6608 */
6609 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006610compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006611{
6612 char_u *p = arg;
6613 int count = 0;
6614
6615 for (;;)
6616 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006617 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006618 return NULL;
6619 ++count;
6620 p = skipwhite(p);
6621 if (ends_excmd(*p))
6622 break;
6623 }
6624
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006625 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6626 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6627 else if (cmdidx == CMD_execute)
6628 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6629 else if (cmdidx == CMD_echomsg)
6630 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6631 else
6632 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633 return p;
6634}
6635
6636/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006637 * A command that is not compiled, execute with legacy code.
6638 */
6639 static char_u *
6640compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6641{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006642 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006643 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006644 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006645
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006646 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006647 goto theend;
6648
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006649 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006650 {
6651 long argt = excmd_get_argt(eap->cmdidx);
6652 int usefilter = FALSE;
6653
6654 has_expr = argt & (EX_XFILE | EX_EXPAND);
6655
6656 // If the command can be followed by a bar, find the bar and truncate
6657 // it, so that the following command can be compiled.
6658 // The '|' is overwritten with a NUL, it is put back below.
6659 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6660 && *eap->arg == '!')
6661 // :w !filter or :r !filter or :r! filter
6662 usefilter = TRUE;
6663 if ((argt & EX_TRLBAR) && !usefilter)
6664 {
6665 separate_nextcmd(eap);
6666 if (eap->nextcmd != NULL)
6667 nextcmd = eap->nextcmd;
6668 }
6669 }
6670
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006671 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6672 {
6673 // expand filename in "syntax include [@group] filename"
6674 has_expr = TRUE;
6675 eap->arg = skipwhite(eap->arg + 7);
6676 if (*eap->arg == '@')
6677 eap->arg = skiptowhite(eap->arg);
6678 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006679
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006680 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006681 {
6682 int count = 0;
6683 char_u *start = skipwhite(line);
6684
6685 // :cmd xxx`=expr1`yyy`=expr2`zzz
6686 // PUSHS ":cmd xxx"
6687 // eval expr1
6688 // PUSHS "yyy"
6689 // eval expr2
6690 // PUSHS "zzz"
6691 // EXECCONCAT 5
6692 for (;;)
6693 {
6694 if (p > start)
6695 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006696 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006697 ++count;
6698 }
6699 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006700 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006701 return NULL;
6702 may_generate_2STRING(-1, cctx);
6703 ++count;
6704 p = skipwhite(p);
6705 if (*p != '`')
6706 {
6707 emsg(_("E1083: missing backtick"));
6708 return NULL;
6709 }
6710 start = p + 1;
6711
6712 p = (char_u *)strstr((char *)start, "`=");
6713 if (p == NULL)
6714 {
6715 if (*skipwhite(start) != NUL)
6716 {
6717 generate_PUSHS(cctx, vim_strsave(start));
6718 ++count;
6719 }
6720 break;
6721 }
6722 }
6723 generate_EXECCONCAT(cctx, count);
6724 }
6725 else
6726 generate_EXEC(cctx, line);
6727
6728theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006729 if (*nextcmd != NUL)
6730 {
6731 // the parser expects a pointer to the bar, put it back
6732 --nextcmd;
6733 *nextcmd = '|';
6734 }
6735
6736 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006737}
6738
6739/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006740 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006741 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006742 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006743 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006744add_def_function(ufunc_T *ufunc)
6745{
6746 dfunc_T *dfunc;
6747
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006748 if (def_functions.ga_len == 0)
6749 {
6750 // The first position is not used, so that a zero uf_dfunc_idx means it
6751 // wasn't set.
6752 if (ga_grow(&def_functions, 1) == FAIL)
6753 return FAIL;
6754 ++def_functions.ga_len;
6755 }
6756
Bram Moolenaar09689a02020-05-09 22:50:08 +02006757 // Add the function to "def_functions".
6758 if (ga_grow(&def_functions, 1) == FAIL)
6759 return FAIL;
6760 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6761 CLEAR_POINTER(dfunc);
6762 dfunc->df_idx = def_functions.ga_len;
6763 ufunc->uf_dfunc_idx = dfunc->df_idx;
6764 dfunc->df_ufunc = ufunc;
6765 ++def_functions.ga_len;
6766 return OK;
6767}
6768
6769/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770 * After ex_function() has collected all the function lines: parse and compile
6771 * the lines into instructions.
6772 * Adds the function to "def_functions".
6773 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6774 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006775 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006776 * This can be used recursively through compile_lambda(), which may reallocate
6777 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006778 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006780 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006781compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006782{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006783 char_u *line = NULL;
6784 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006785 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 cctx_T cctx;
6787 garray_T *instr;
6788 int called_emsg_before = called_emsg;
6789 int ret = FAIL;
6790 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006791 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006792 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006794 // When using a function that was compiled before: Free old instructions.
6795 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006796 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006798 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6799 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006800 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006802 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006803 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804
Bram Moolenaar985116a2020-07-12 17:31:09 +02006805 ufunc->uf_def_status = UF_COMPILING;
6806
Bram Moolenaara80faa82020-04-12 19:37:17 +02006807 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 cctx.ctx_ufunc = ufunc;
6809 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006810 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006811 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6812 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6813 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6814 cctx.ctx_type_list = &ufunc->uf_type_list;
6815 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6816 instr = &cctx.ctx_instr;
6817
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006818 // Set the context to the function, it may be compiled when called from
6819 // another script. Set the script version to the most modern one.
6820 // The line number will be set in next_line_from_context().
6821 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006822 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6823
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006824 // Make sure error messages are OK.
6825 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6826 if (do_estack_push)
6827 estack_push_ufunc(ufunc, 1);
6828
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006829 if (ufunc->uf_def_args.ga_len > 0)
6830 {
6831 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006832 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006833 int i;
6834 char_u *arg;
6835 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6836
6837 // Produce instructions for the default values of optional arguments.
6838 // Store the instruction index in uf_def_arg_idx[] so that we know
6839 // where to start when the function is called, depending on the number
6840 // of arguments.
6841 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6842 if (ufunc->uf_def_arg_idx == NULL)
6843 goto erret;
6844 for (i = 0; i < count; ++i)
6845 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006846 garray_T *stack = &cctx.ctx_type_stack;
6847 type_T *val_type;
6848 int arg_idx = first_def_arg + i;
6849
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006850 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6851 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006852 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006853 goto erret;
6854
6855 // If no type specified use the type of the default value.
6856 // Otherwise check that the default value type matches the
6857 // specified type.
6858 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6859 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6860 ufunc->uf_arg_types[arg_idx] = val_type;
6861 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6862 == FAIL)
6863 {
6864 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6865 arg_idx + 1);
6866 goto erret;
6867 }
6868
6869 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006870 goto erret;
6871 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006872 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6873 }
6874
6875 /*
6876 * Loop over all the lines of the function and generate instructions.
6877 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 for (;;)
6879 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006880 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006881 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006882 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006883 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006884
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006885 // Bail out on the first error to avoid a flood of errors and report
6886 // the right line number when inside try/catch.
6887 if (emsg_before != called_emsg)
6888 goto erret;
6889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890 if (line != NULL && *line == '|')
6891 // the line continues after a '|'
6892 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006893 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006894 && !(*line == '#' && (line == cctx.ctx_line_start
6895 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006896 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006897 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006898 goto erret;
6899 }
6900 else
6901 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006902 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006903 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006904 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006905 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006907 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908
Bram Moolenaara80faa82020-04-12 19:37:17 +02006909 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006910 ea.cmdlinep = &line;
6911 ea.cmd = skipwhite(line);
6912
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006913 // Some things can be recognized by the first character.
6914 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006915 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006916 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006917 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006918 if (ea.cmd[1] != '{')
6919 {
6920 line = (char_u *)"";
6921 continue;
6922 }
6923 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006925 case '}':
6926 {
6927 // "}" ends a block scope
6928 scopetype_T stype = cctx.ctx_scope == NULL
6929 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006931 if (stype == BLOCK_SCOPE)
6932 {
6933 compile_endblock(&cctx);
6934 line = ea.cmd;
6935 }
6936 else
6937 {
6938 emsg(_("E1025: using } outside of a block scope"));
6939 goto erret;
6940 }
6941 if (line != NULL)
6942 line = skipwhite(ea.cmd + 1);
6943 continue;
6944 }
6945
6946 case '{':
6947 // "{" starts a block scope
6948 // "{'a': 1}->func() is something else
6949 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6950 {
6951 line = compile_block(ea.cmd, &cctx);
6952 continue;
6953 }
6954 break;
6955
6956 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006957 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006958 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959 }
6960
6961 /*
6962 * COMMAND MODIFIERS
6963 */
6964 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6965 {
6966 if (errormsg != NULL)
6967 goto erret;
6968 // empty line or comment
6969 line = (char_u *)"";
6970 continue;
6971 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006972 // TODO: use modifiers in the command
6973 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02006974 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975
6976 // Skip ":call" to get to the function name.
6977 if (checkforcmd(&ea.cmd, "call", 3))
6978 ea.cmd = skipwhite(ea.cmd);
6979
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006980 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006981 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006982 char_u *pskip;
6983
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006984 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006985 // find what follows.
6986 // Skip over "var.member", "var[idx]" and the like.
6987 // Also "&opt = val", "$ENV = val" and "@r = val".
6988 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006989 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006990 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006991 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006992 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006993 char_u *var_end;
6994 int oplen;
6995 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006997 var_end = find_name_end(pskip, NULL, NULL,
6998 FNE_CHECK_START | FNE_INCL_BR);
6999 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007000 if (oplen > 0)
7001 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007002 size_t len = p - ea.cmd;
7003
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007004 // Recognize an assignment if we recognize the variable
7005 // name:
7006 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007007 // "local = expr" where "local" is a local var.
7008 // "script = expr" where "script" is a script-local var.
7009 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007010 // "&opt = expr"
7011 // "$ENV = expr"
7012 // "@r = expr"
7013 if (*ea.cmd == '&'
7014 || *ea.cmd == '$'
7015 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007016 || ((len) > 2 && ea.cmd[1] == ':')
7017 || lookup_local(ea.cmd, len, &cctx) != NULL
7018 || lookup_arg(ea.cmd, len, NULL, NULL,
7019 NULL, &cctx) == OK
7020 || lookup_script(ea.cmd, len) == OK
7021 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007022 {
7023 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007024 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007025 goto erret;
7026 continue;
7027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028 }
7029 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007030
7031 if (*ea.cmd == '[')
7032 {
7033 // [var, var] = expr
7034 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7035 if (line == NULL)
7036 goto erret;
7037 if (line != ea.cmd)
7038 continue;
7039 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 }
7041
7042 /*
7043 * COMMAND after range
7044 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007045 cmd = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007047 if (ea.cmd > cmd && !starts_with_colon)
7048 {
7049 emsg(_(e_colon_required));
7050 goto erret;
7051 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007052 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007053 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007054 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055
7056 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7057 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007058 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007059 {
7060 line += STRLEN(line);
7061 continue;
7062 }
7063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007065 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007067 // CMD_let cannot happen, compile_assignment() above is used
7068 iemsg("Command from find_ex_command() not handled");
7069 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 }
7072
7073 p = skipwhite(p);
7074
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007075 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007076 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007077 && ea.cmdidx != CMD_elseif
7078 && ea.cmdidx != CMD_else
7079 && ea.cmdidx != CMD_endif)
7080 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007081 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007082 continue;
7083 }
7084
Bram Moolenaarefd88552020-06-18 20:50:10 +02007085 if (ea.cmdidx != CMD_elseif
7086 && ea.cmdidx != CMD_else
7087 && ea.cmdidx != CMD_endif
7088 && ea.cmdidx != CMD_endfor
7089 && ea.cmdidx != CMD_endwhile
7090 && ea.cmdidx != CMD_catch
7091 && ea.cmdidx != CMD_finally
7092 && ea.cmdidx != CMD_endtry)
7093 {
7094 if (cctx.ctx_had_return)
7095 {
7096 emsg(_("E1095: Unreachable code after :return"));
7097 goto erret;
7098 }
7099 }
7100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101 switch (ea.cmdidx)
7102 {
7103 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007104 ea.arg = p;
7105 line = compile_nested_function(&ea, &cctx);
7106 break;
7107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007108 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007109 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110 goto erret;
7111
7112 case CMD_return:
7113 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007114 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007115 break;
7116
7117 case CMD_let:
7118 case CMD_const:
7119 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007120 if (line == p)
7121 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007122 break;
7123
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007124 case CMD_unlet:
7125 case CMD_unlockvar:
7126 case CMD_lockvar:
7127 line = compile_unletlock(p, &ea, &cctx);
7128 break;
7129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130 case CMD_import:
7131 line = compile_import(p, &cctx);
7132 break;
7133
7134 case CMD_if:
7135 line = compile_if(p, &cctx);
7136 break;
7137 case CMD_elseif:
7138 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007139 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007140 break;
7141 case CMD_else:
7142 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007143 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 break;
7145 case CMD_endif:
7146 line = compile_endif(p, &cctx);
7147 break;
7148
7149 case CMD_while:
7150 line = compile_while(p, &cctx);
7151 break;
7152 case CMD_endwhile:
7153 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007154 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 break;
7156
7157 case CMD_for:
7158 line = compile_for(p, &cctx);
7159 break;
7160 case CMD_endfor:
7161 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007162 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 break;
7164 case CMD_continue:
7165 line = compile_continue(p, &cctx);
7166 break;
7167 case CMD_break:
7168 line = compile_break(p, &cctx);
7169 break;
7170
7171 case CMD_try:
7172 line = compile_try(p, &cctx);
7173 break;
7174 case CMD_catch:
7175 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007176 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 break;
7178 case CMD_finally:
7179 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007180 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007181 break;
7182 case CMD_endtry:
7183 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007184 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185 break;
7186 case CMD_throw:
7187 line = compile_throw(p, &cctx);
7188 break;
7189
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007190 case CMD_eval:
7191 if (compile_expr0(&p, &cctx) == FAIL)
7192 goto erret;
7193
7194 // drop the return value
7195 generate_instr_drop(&cctx, ISN_DROP, 1);
7196
7197 line = skipwhite(p);
7198 break;
7199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007200 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007201 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007202 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007203 case CMD_echomsg:
7204 case CMD_echoerr:
7205 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007206 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007207
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007208 // TODO: other commands with an expression argument
7209
Bram Moolenaar002262f2020-07-08 17:47:57 +02007210 case CMD_SIZE:
7211 semsg(_("E476: Invalid command: %s"), ea.cmd);
7212 goto erret;
7213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007215 // Not recognized, execute with do_cmdline_cmd().
7216 ea.arg = p;
7217 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007218 break;
7219 }
7220 if (line == NULL)
7221 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007222 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007223
7224 if (cctx.ctx_type_stack.ga_len < 0)
7225 {
7226 iemsg("Type stack underflow");
7227 goto erret;
7228 }
7229 }
7230
7231 if (cctx.ctx_scope != NULL)
7232 {
7233 if (cctx.ctx_scope->se_type == IF_SCOPE)
7234 emsg(_(e_endif));
7235 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7236 emsg(_(e_endwhile));
7237 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7238 emsg(_(e_endfor));
7239 else
7240 emsg(_("E1026: Missing }"));
7241 goto erret;
7242 }
7243
Bram Moolenaarefd88552020-06-18 20:50:10 +02007244 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007245 {
7246 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7247 {
7248 emsg(_("E1027: Missing return statement"));
7249 goto erret;
7250 }
7251
7252 // Return zero if there is no return at the end.
7253 generate_PUSHNR(&cctx, 0);
7254 generate_instr(&cctx, ISN_RETURN);
7255 }
7256
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007257 {
7258 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7259 + ufunc->uf_dfunc_idx;
7260 dfunc->df_deleted = FALSE;
7261 dfunc->df_instr = instr->ga_data;
7262 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007263 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007264 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007265 if (cctx.ctx_outer_used)
7266 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007267 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269
7270 ret = OK;
7271
7272erret:
7273 if (ret == FAIL)
7274 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007275 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007276 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7277 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007278
7279 for (idx = 0; idx < instr->ga_len; ++idx)
7280 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007282
Bram Moolenaar45a15082020-05-25 00:28:33 +02007283 // if using the last entry in the table we might as well remove it
7284 if (!dfunc->df_deleted
7285 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007286 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007287 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007288
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007289 while (cctx.ctx_scope != NULL)
7290 drop_scope(&cctx);
7291
Bram Moolenaar20431c92020-03-20 18:39:46 +01007292 // Don't execute this function body.
7293 ga_clear_strings(&ufunc->uf_lines);
7294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295 if (errormsg != NULL)
7296 emsg(errormsg);
7297 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007298 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007299 }
7300
7301 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007302 if (do_estack_push)
7303 estack_pop();
7304
Bram Moolenaar20431c92020-03-20 18:39:46 +01007305 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007306 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007307 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007308 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007309}
7310
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007311 void
7312set_function_type(ufunc_T *ufunc)
7313{
7314 int varargs = ufunc->uf_va_name != NULL;
7315 int argcount = ufunc->uf_args.ga_len;
7316
7317 // Create a type for the function, with the return type and any
7318 // argument types.
7319 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7320 // The type is included in "tt_args".
7321 if (argcount > 0 || varargs)
7322 {
7323 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7324 argcount, &ufunc->uf_type_list);
7325 // Add argument types to the function type.
7326 if (func_type_add_arg_types(ufunc->uf_func_type,
7327 argcount + varargs,
7328 &ufunc->uf_type_list) == FAIL)
7329 return;
7330 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7331 ufunc->uf_func_type->tt_min_argcount =
7332 argcount - ufunc->uf_def_args.ga_len;
7333 if (ufunc->uf_arg_types == NULL)
7334 {
7335 int i;
7336
7337 // lambda does not have argument types.
7338 for (i = 0; i < argcount; ++i)
7339 ufunc->uf_func_type->tt_args[i] = &t_any;
7340 }
7341 else
7342 mch_memmove(ufunc->uf_func_type->tt_args,
7343 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7344 if (varargs)
7345 {
7346 ufunc->uf_func_type->tt_args[argcount] =
7347 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7348 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7349 }
7350 }
7351 else
7352 // No arguments, can use a predefined type.
7353 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7354 argcount, &ufunc->uf_type_list);
7355}
7356
7357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007358/*
7359 * Delete an instruction, free what it contains.
7360 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007361 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362delete_instr(isn_T *isn)
7363{
7364 switch (isn->isn_type)
7365 {
7366 case ISN_EXEC:
7367 case ISN_LOADENV:
7368 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007369 case ISN_LOADB:
7370 case ISN_LOADW:
7371 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007373 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007374 case ISN_PUSHEXC:
7375 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007376 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007378 case ISN_STOREB:
7379 case ISN_STOREW:
7380 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007381 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382 vim_free(isn->isn_arg.string);
7383 break;
7384
7385 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007386 case ISN_STORES:
7387 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 break;
7389
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007390 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007391 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007392 vim_free(isn->isn_arg.unlet.ul_name);
7393 break;
7394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 case ISN_STOREOPT:
7396 vim_free(isn->isn_arg.storeopt.so_name);
7397 break;
7398
7399 case ISN_PUSHBLOB: // push blob isn_arg.blob
7400 blob_unref(isn->isn_arg.blob);
7401 break;
7402
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007403 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007404#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007405 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007406#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007407 break;
7408
7409 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007410#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007411 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007412#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007413 break;
7414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007415 case ISN_UCALL:
7416 vim_free(isn->isn_arg.ufunc.cuf_name);
7417 break;
7418
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007419 case ISN_FUNCREF:
7420 {
7421 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7422 + isn->isn_arg.funcref.fr_func;
7423 func_ptr_unref(dfunc->df_ufunc);
7424 }
7425 break;
7426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 case ISN_2BOOL:
7428 case ISN_2STRING:
7429 case ISN_ADDBLOB:
7430 case ISN_ADDLIST:
7431 case ISN_BCALL:
7432 case ISN_CATCH:
7433 case ISN_CHECKNR:
7434 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007435 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007436 case ISN_COMPAREANY:
7437 case ISN_COMPAREBLOB:
7438 case ISN_COMPAREBOOL:
7439 case ISN_COMPAREDICT:
7440 case ISN_COMPAREFLOAT:
7441 case ISN_COMPAREFUNC:
7442 case ISN_COMPARELIST:
7443 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 case ISN_COMPARESPECIAL:
7445 case ISN_COMPARESTRING:
7446 case ISN_CONCAT:
7447 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007448 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 case ISN_DROP:
7450 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007451 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007452 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007454 case ISN_EXECCONCAT:
7455 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007458 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007459 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007460 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461 case ISN_JUMP:
7462 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007463 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464 case ISN_LOADSCRIPT:
7465 case ISN_LOADREG:
7466 case ISN_LOADV:
7467 case ISN_NEGATENR:
7468 case ISN_NEWDICT:
7469 case ISN_NEWLIST:
7470 case ISN_OPNR:
7471 case ISN_OPFLOAT:
7472 case ISN_OPANY:
7473 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007474 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475 case ISN_PUSHF:
7476 case ISN_PUSHNR:
7477 case ISN_PUSHBOOL:
7478 case ISN_PUSHSPEC:
7479 case ISN_RETURN:
7480 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007481 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007482 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007484 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007485 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007486 case ISN_STOREDICT:
7487 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 case ISN_THROW:
7489 case ISN_TRY:
7490 // nothing allocated
7491 break;
7492 }
7493}
7494
7495/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007496 * Free all instructions for "dfunc".
7497 */
7498 static void
7499delete_def_function_contents(dfunc_T *dfunc)
7500{
7501 int idx;
7502
7503 ga_clear(&dfunc->df_def_args_isn);
7504
7505 if (dfunc->df_instr != NULL)
7506 {
7507 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7508 delete_instr(dfunc->df_instr + idx);
7509 VIM_CLEAR(dfunc->df_instr);
7510 }
7511
7512 dfunc->df_deleted = TRUE;
7513}
7514
7515/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007516 * When a user function is deleted, clear the contents of any associated def
7517 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 */
7519 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007520clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007522 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007523 {
7524 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7525 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007526
Bram Moolenaar20431c92020-03-20 18:39:46 +01007527 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007528 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529 }
7530}
7531
7532#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007533/*
7534 * Free all functions defined with ":def".
7535 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007536 void
7537free_def_functions(void)
7538{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007539 int idx;
7540
7541 for (idx = 0; idx < def_functions.ga_len; ++idx)
7542 {
7543 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7544
7545 delete_def_function_contents(dfunc);
7546 }
7547
7548 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549}
7550#endif
7551
7552
7553#endif // FEAT_EVAL