blob: 941309ffbd2c0c96b15025b0b33db47ae6b35fba [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
823/*
824 * Generate an ISN_COMPARE* instruction with a boolean result.
825 */
826 static int
827generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
828{
829 isntype_T isntype;
830 isn_T *isn;
831 garray_T *stack = &cctx->ctx_type_stack;
832 vartype_T type1;
833 vartype_T type2;
834
835 RETURN_OK_IF_SKIP(cctx);
836
837 // Get the known type of the two items on the stack. If they are matching
838 // use a type-specific instruction. Otherwise fall back to runtime type
839 // checking.
840 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
841 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
842 isntype = get_compare_isn(exptype, type1, type2);
843 if (isntype == ISN_DROP)
844 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845
846 if ((isn = generate_instr(cctx, isntype)) == NULL)
847 return FAIL;
848 isn->isn_arg.op.op_type = exptype;
849 isn->isn_arg.op.op_ic = ic;
850
851 // takes two arguments, puts one bool back
852 if (stack->ga_len >= 2)
853 {
854 --stack->ga_len;
855 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
856 }
857
858 return OK;
859}
860
861/*
862 * Generate an ISN_2BOOL instruction.
863 */
864 static int
865generate_2BOOL(cctx_T *cctx, int invert)
866{
867 isn_T *isn;
868 garray_T *stack = &cctx->ctx_type_stack;
869
Bram Moolenaar080457c2020-03-03 21:53:32 +0100870 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
872 return FAIL;
873 isn->isn_arg.number = invert;
874
875 // type becomes bool
876 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
877
878 return OK;
879}
880
881 static int
882generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
883{
884 isn_T *isn;
885 garray_T *stack = &cctx->ctx_type_stack;
886
Bram Moolenaar080457c2020-03-03 21:53:32 +0100887 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
889 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200890 // TODO: whole type, e.g. for a function also arg and return types
891 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 isn->isn_arg.type.ct_off = offset;
893
894 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200895 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896
897 return OK;
898}
899
900/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200901 * Check that
902 * - "actual" is "expected" type or
903 * - "actual" is a type that can be "expected" type: add a runtime check; or
904 * - return FAIL.
905 */
906 static int
907need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
908{
909 if (check_type(expected, actual, FALSE) == OK)
910 return OK;
911 if (actual->tt_type != VAR_ANY
912 && actual->tt_type != VAR_UNKNOWN
913 && !(actual->tt_type == VAR_FUNC
914 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
915 {
916 type_mismatch(expected, actual);
917 return FAIL;
918 }
919 generate_TYPECHECK(cctx, expected, offset);
920 return OK;
921}
922
923/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 * Generate an ISN_PUSHNR instruction.
925 */
926 static int
927generate_PUSHNR(cctx_T *cctx, varnumber_T number)
928{
929 isn_T *isn;
930
Bram Moolenaar080457c2020-03-03 21:53:32 +0100931 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
933 return FAIL;
934 isn->isn_arg.number = number;
935
936 return OK;
937}
938
939/*
940 * Generate an ISN_PUSHBOOL instruction.
941 */
942 static int
943generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
944{
945 isn_T *isn;
946
Bram Moolenaar080457c2020-03-03 21:53:32 +0100947 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
949 return FAIL;
950 isn->isn_arg.number = number;
951
952 return OK;
953}
954
955/*
956 * Generate an ISN_PUSHSPEC instruction.
957 */
958 static int
959generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
960{
961 isn_T *isn;
962
Bram Moolenaar080457c2020-03-03 21:53:32 +0100963 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
965 return FAIL;
966 isn->isn_arg.number = number;
967
968 return OK;
969}
970
971#ifdef FEAT_FLOAT
972/*
973 * Generate an ISN_PUSHF instruction.
974 */
975 static int
976generate_PUSHF(cctx_T *cctx, float_T fnumber)
977{
978 isn_T *isn;
979
Bram Moolenaar080457c2020-03-03 21:53:32 +0100980 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
982 return FAIL;
983 isn->isn_arg.fnumber = fnumber;
984
985 return OK;
986}
987#endif
988
989/*
990 * Generate an ISN_PUSHS instruction.
991 * Consumes "str".
992 */
993 static int
994generate_PUSHS(cctx_T *cctx, char_u *str)
995{
996 isn_T *isn;
997
Bram Moolenaar080457c2020-03-03 21:53:32 +0100998 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1000 return FAIL;
1001 isn->isn_arg.string = str;
1002
1003 return OK;
1004}
1005
1006/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001007 * Generate an ISN_PUSHCHANNEL instruction.
1008 * Consumes "channel".
1009 */
1010 static int
1011generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1012{
1013 isn_T *isn;
1014
Bram Moolenaar080457c2020-03-03 21:53:32 +01001015 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001016 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1017 return FAIL;
1018 isn->isn_arg.channel = channel;
1019
1020 return OK;
1021}
1022
1023/*
1024 * Generate an ISN_PUSHJOB instruction.
1025 * Consumes "job".
1026 */
1027 static int
1028generate_PUSHJOB(cctx_T *cctx, job_T *job)
1029{
1030 isn_T *isn;
1031
Bram Moolenaar080457c2020-03-03 21:53:32 +01001032 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001033 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001034 return FAIL;
1035 isn->isn_arg.job = job;
1036
1037 return OK;
1038}
1039
1040/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 * Generate an ISN_PUSHBLOB instruction.
1042 * Consumes "blob".
1043 */
1044 static int
1045generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1046{
1047 isn_T *isn;
1048
Bram Moolenaar080457c2020-03-03 21:53:32 +01001049 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1051 return FAIL;
1052 isn->isn_arg.blob = blob;
1053
1054 return OK;
1055}
1056
1057/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001058 * Generate an ISN_PUSHFUNC instruction with name "name".
1059 * Consumes "name".
1060 */
1061 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001062generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001063{
1064 isn_T *isn;
1065
Bram Moolenaar080457c2020-03-03 21:53:32 +01001066 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001067 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001068 return FAIL;
1069 isn->isn_arg.string = name;
1070
1071 return OK;
1072}
1073
1074/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001075 * Generate an ISN_GETITEM instruction with "index".
1076 */
1077 static int
1078generate_GETITEM(cctx_T *cctx, int index)
1079{
1080 isn_T *isn;
1081 garray_T *stack = &cctx->ctx_type_stack;
1082 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1083 type_T *item_type = &t_any;
1084
1085 RETURN_OK_IF_SKIP(cctx);
1086
1087 if (type->tt_type == VAR_LIST)
1088 item_type = type->tt_member;
1089 else if (type->tt_type != VAR_ANY)
1090 {
1091 emsg(_(e_listreq));
1092 return FAIL;
1093 }
1094 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1095 return FAIL;
1096 isn->isn_arg.number = index;
1097
1098 // add the item type to the type stack
1099 if (ga_grow(stack, 1) == FAIL)
1100 return FAIL;
1101 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1102 ++stack->ga_len;
1103 return OK;
1104}
1105
1106/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001107 * Generate an ISN_SLICE instruction with "count".
1108 */
1109 static int
1110generate_SLICE(cctx_T *cctx, int count)
1111{
1112 isn_T *isn;
1113
1114 RETURN_OK_IF_SKIP(cctx);
1115 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1116 return FAIL;
1117 isn->isn_arg.number = count;
1118 return OK;
1119}
1120
1121/*
1122 * Generate an ISN_CHECKLEN instruction with "min_len".
1123 */
1124 static int
1125generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1126{
1127 isn_T *isn;
1128
1129 RETURN_OK_IF_SKIP(cctx);
1130
1131 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1132 return FAIL;
1133 isn->isn_arg.checklen.cl_min_len = min_len;
1134 isn->isn_arg.checklen.cl_more_OK = more_OK;
1135
1136 return OK;
1137}
1138
1139/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 * Generate an ISN_STORE instruction.
1141 */
1142 static int
1143generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1144{
1145 isn_T *isn;
1146
Bram Moolenaar080457c2020-03-03 21:53:32 +01001147 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1149 return FAIL;
1150 if (name != NULL)
1151 isn->isn_arg.string = vim_strsave(name);
1152 else
1153 isn->isn_arg.number = idx;
1154
1155 return OK;
1156}
1157
1158/*
1159 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1160 */
1161 static int
1162generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1163{
1164 isn_T *isn;
1165
Bram Moolenaar080457c2020-03-03 21:53:32 +01001166 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1168 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001169 isn->isn_arg.storenr.stnr_idx = idx;
1170 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171
1172 return OK;
1173}
1174
1175/*
1176 * Generate an ISN_STOREOPT instruction
1177 */
1178 static int
1179generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1180{
1181 isn_T *isn;
1182
Bram Moolenaar080457c2020-03-03 21:53:32 +01001183 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1185 return FAIL;
1186 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1187 isn->isn_arg.storeopt.so_flags = opt_flags;
1188
1189 return OK;
1190}
1191
1192/*
1193 * Generate an ISN_LOAD or similar instruction.
1194 */
1195 static int
1196generate_LOAD(
1197 cctx_T *cctx,
1198 isntype_T isn_type,
1199 int idx,
1200 char_u *name,
1201 type_T *type)
1202{
1203 isn_T *isn;
1204
Bram Moolenaar080457c2020-03-03 21:53:32 +01001205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1207 return FAIL;
1208 if (name != NULL)
1209 isn->isn_arg.string = vim_strsave(name);
1210 else
1211 isn->isn_arg.number = idx;
1212
1213 return OK;
1214}
1215
1216/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001217 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001218 */
1219 static int
1220generate_LOADV(
1221 cctx_T *cctx,
1222 char_u *name,
1223 int error)
1224{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001225 int di_flags;
1226 int vidx = find_vim_var(name, &di_flags);
1227 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001228
Bram Moolenaar080457c2020-03-03 21:53:32 +01001229 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001230 if (vidx < 0)
1231 {
1232 if (error)
1233 semsg(_(e_var_notfound), name);
1234 return FAIL;
1235 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001236 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001237
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001238 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001239}
1240
1241/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001242 * Generate an ISN_UNLET instruction.
1243 */
1244 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001245generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001246{
1247 isn_T *isn;
1248
1249 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001250 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001251 return FAIL;
1252 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1253 isn->isn_arg.unlet.ul_forceit = forceit;
1254
1255 return OK;
1256}
1257
1258/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 * Generate an ISN_LOADS instruction.
1260 */
1261 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001262generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001264 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001266 int sid,
1267 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268{
1269 isn_T *isn;
1270
Bram Moolenaar080457c2020-03-03 21:53:32 +01001271 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001272 if (isn_type == ISN_LOADS)
1273 isn = generate_instr_type(cctx, isn_type, type);
1274 else
1275 isn = generate_instr_drop(cctx, isn_type, 1);
1276 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001278 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1279 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280
1281 return OK;
1282}
1283
1284/*
1285 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1286 */
1287 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001288generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289 cctx_T *cctx,
1290 isntype_T isn_type,
1291 int sid,
1292 int idx,
1293 type_T *type)
1294{
1295 isn_T *isn;
1296
Bram Moolenaar080457c2020-03-03 21:53:32 +01001297 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 if (isn_type == ISN_LOADSCRIPT)
1299 isn = generate_instr_type(cctx, isn_type, type);
1300 else
1301 isn = generate_instr_drop(cctx, isn_type, 1);
1302 if (isn == NULL)
1303 return FAIL;
1304 isn->isn_arg.script.script_sid = sid;
1305 isn->isn_arg.script.script_idx = idx;
1306 return OK;
1307}
1308
1309/*
1310 * Generate an ISN_NEWLIST instruction.
1311 */
1312 static int
1313generate_NEWLIST(cctx_T *cctx, int count)
1314{
1315 isn_T *isn;
1316 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 type_T *type;
1318 type_T *member;
1319
Bram Moolenaar080457c2020-03-03 21:53:32 +01001320 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1322 return FAIL;
1323 isn->isn_arg.number = count;
1324
1325 // drop the value types
1326 stack->ga_len -= count;
1327
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001328 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001329 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 if (count > 0)
1331 member = ((type_T **)stack->ga_data)[stack->ga_len];
1332 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001333 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001334 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335
1336 // add the list type to the type stack
1337 if (ga_grow(stack, 1) == FAIL)
1338 return FAIL;
1339 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1340 ++stack->ga_len;
1341
1342 return OK;
1343}
1344
1345/*
1346 * Generate an ISN_NEWDICT instruction.
1347 */
1348 static int
1349generate_NEWDICT(cctx_T *cctx, int count)
1350{
1351 isn_T *isn;
1352 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353 type_T *type;
1354 type_T *member;
1355
Bram Moolenaar080457c2020-03-03 21:53:32 +01001356 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1358 return FAIL;
1359 isn->isn_arg.number = count;
1360
1361 // drop the key and value types
1362 stack->ga_len -= 2 * count;
1363
Bram Moolenaar436472f2020-02-20 22:54:43 +01001364 // Use the first value type for the list member type. Use "void" for an
1365 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 if (count > 0)
1367 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1368 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001369 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001370 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371
1372 // add the dict type to the type stack
1373 if (ga_grow(stack, 1) == FAIL)
1374 return FAIL;
1375 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1376 ++stack->ga_len;
1377
1378 return OK;
1379}
1380
1381/*
1382 * Generate an ISN_FUNCREF instruction.
1383 */
1384 static int
1385generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1386{
1387 isn_T *isn;
1388 garray_T *stack = &cctx->ctx_type_stack;
1389
Bram Moolenaar080457c2020-03-03 21:53:32 +01001390 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1392 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001393 isn->isn_arg.funcref.fr_func = dfunc_idx;
1394 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395
1396 if (ga_grow(stack, 1) == FAIL)
1397 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001398 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 // TODO: argument and return types
1400 ++stack->ga_len;
1401
1402 return OK;
1403}
1404
1405/*
1406 * Generate an ISN_JUMP instruction.
1407 */
1408 static int
1409generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1410{
1411 isn_T *isn;
1412 garray_T *stack = &cctx->ctx_type_stack;
1413
Bram Moolenaar080457c2020-03-03 21:53:32 +01001414 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1416 return FAIL;
1417 isn->isn_arg.jump.jump_when = when;
1418 isn->isn_arg.jump.jump_where = where;
1419
1420 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1421 --stack->ga_len;
1422
1423 return OK;
1424}
1425
1426 static int
1427generate_FOR(cctx_T *cctx, int loop_idx)
1428{
1429 isn_T *isn;
1430 garray_T *stack = &cctx->ctx_type_stack;
1431
Bram Moolenaar080457c2020-03-03 21:53:32 +01001432 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1434 return FAIL;
1435 isn->isn_arg.forloop.for_idx = loop_idx;
1436
1437 if (ga_grow(stack, 1) == FAIL)
1438 return FAIL;
1439 // type doesn't matter, will be stored next
1440 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1441 ++stack->ga_len;
1442
1443 return OK;
1444}
1445
1446/*
1447 * Generate an ISN_BCALL instruction.
1448 * Return FAIL if the number of arguments is wrong.
1449 */
1450 static int
1451generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1452{
1453 isn_T *isn;
1454 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001455 type_T *argtypes[MAX_FUNC_ARGS];
1456 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457
Bram Moolenaar080457c2020-03-03 21:53:32 +01001458 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 if (check_internal_func(func_idx, argcount) == FAIL)
1460 return FAIL;
1461
1462 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1463 return FAIL;
1464 isn->isn_arg.bfunc.cbf_idx = func_idx;
1465 isn->isn_arg.bfunc.cbf_argcount = argcount;
1466
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001467 for (i = 0; i < argcount; ++i)
1468 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 stack->ga_len -= argcount; // drop the arguments
1471 if (ga_grow(stack, 1) == FAIL)
1472 return FAIL;
1473 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001474 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475 ++stack->ga_len; // add return value
1476
1477 return OK;
1478}
1479
1480/*
1481 * Generate an ISN_DCALL or ISN_UCALL instruction.
1482 * Return FAIL if the number of arguments is wrong.
1483 */
1484 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001485generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486{
1487 isn_T *isn;
1488 garray_T *stack = &cctx->ctx_type_stack;
1489 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001490 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491
Bram Moolenaar080457c2020-03-03 21:53:32 +01001492 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 if (argcount > regular_args && !has_varargs(ufunc))
1494 {
1495 semsg(_(e_toomanyarg), ufunc->uf_name);
1496 return FAIL;
1497 }
1498 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1499 {
1500 semsg(_(e_toofewarg), ufunc->uf_name);
1501 return FAIL;
1502 }
1503
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001504 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001505 {
1506 int i;
1507
1508 for (i = 0; i < argcount; ++i)
1509 {
1510 type_T *expected;
1511 type_T *actual;
1512
1513 if (i < regular_args)
1514 {
1515 if (ufunc->uf_arg_types == NULL)
1516 continue;
1517 expected = ufunc->uf_arg_types[i];
1518 }
1519 else
1520 expected = ufunc->uf_va_type->tt_member;
1521 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001522 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001523 {
1524 arg_type_mismatch(expected, actual, i + 1);
1525 return FAIL;
1526 }
1527 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001528 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001529 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001530 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001531 }
1532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001534 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001535 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001537 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 {
1539 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1540 isn->isn_arg.dfunc.cdf_argcount = argcount;
1541 }
1542 else
1543 {
1544 // A user function may be deleted and redefined later, can't use the
1545 // ufunc pointer, need to look it up again at runtime.
1546 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1547 isn->isn_arg.ufunc.cuf_argcount = argcount;
1548 }
1549
1550 stack->ga_len -= argcount; // drop the arguments
1551 if (ga_grow(stack, 1) == FAIL)
1552 return FAIL;
1553 // add return value
1554 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1555 ++stack->ga_len;
1556
1557 return OK;
1558}
1559
1560/*
1561 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1562 */
1563 static int
1564generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1565{
1566 isn_T *isn;
1567 garray_T *stack = &cctx->ctx_type_stack;
1568
Bram Moolenaar080457c2020-03-03 21:53:32 +01001569 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1571 return FAIL;
1572 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1573 isn->isn_arg.ufunc.cuf_argcount = argcount;
1574
1575 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001576 if (ga_grow(stack, 1) == FAIL)
1577 return FAIL;
1578 // add return value
1579 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1580 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581
1582 return OK;
1583}
1584
1585/*
1586 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001587 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 */
1589 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001590generate_PCALL(
1591 cctx_T *cctx,
1592 int argcount,
1593 char_u *name,
1594 type_T *type,
1595 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596{
1597 isn_T *isn;
1598 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001599 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600
Bram Moolenaar080457c2020-03-03 21:53:32 +01001601 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001602
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001603 if (type->tt_type == VAR_ANY)
1604 ret_type = &t_any;
1605 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001606 {
1607 if (type->tt_argcount != -1)
1608 {
1609 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1610
1611 if (argcount < type->tt_min_argcount - varargs)
1612 {
1613 semsg(_(e_toofewarg), "[reference]");
1614 return FAIL;
1615 }
1616 if (!varargs && argcount > type->tt_argcount)
1617 {
1618 semsg(_(e_toomanyarg), "[reference]");
1619 return FAIL;
1620 }
1621 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001622 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001623 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001624 else
1625 {
1626 semsg(_("E1085: Not a callable type: %s"), name);
1627 return FAIL;
1628 }
1629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1631 return FAIL;
1632 isn->isn_arg.pfunc.cpf_top = at_top;
1633 isn->isn_arg.pfunc.cpf_argcount = argcount;
1634
1635 stack->ga_len -= argcount; // drop the arguments
1636
1637 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001638 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001640 // If partial is above the arguments it must be cleared and replaced with
1641 // the return value.
1642 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1643 return FAIL;
1644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 return OK;
1646}
1647
1648/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001649 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 */
1651 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001652generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653{
1654 isn_T *isn;
1655 garray_T *stack = &cctx->ctx_type_stack;
1656 type_T *type;
1657
Bram Moolenaar080457c2020-03-03 21:53:32 +01001658 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001659 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001661 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001663 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001665 if (type->tt_type != VAR_DICT && type != &t_any)
1666 {
1667 emsg(_(e_dictreq));
1668 return FAIL;
1669 }
1670 // change dict type to dict member type
1671 if (type->tt_type == VAR_DICT)
1672 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673
1674 return OK;
1675}
1676
1677/*
1678 * Generate an ISN_ECHO instruction.
1679 */
1680 static int
1681generate_ECHO(cctx_T *cctx, int with_white, int count)
1682{
1683 isn_T *isn;
1684
Bram Moolenaar080457c2020-03-03 21:53:32 +01001685 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1687 return FAIL;
1688 isn->isn_arg.echo.echo_with_white = with_white;
1689 isn->isn_arg.echo.echo_count = count;
1690
1691 return OK;
1692}
1693
Bram Moolenaarad39c092020-02-26 18:23:43 +01001694/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001695 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001696 */
1697 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001698generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001699{
1700 isn_T *isn;
1701
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001702 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001703 return FAIL;
1704 isn->isn_arg.number = count;
1705
1706 return OK;
1707}
1708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 static int
1710generate_EXEC(cctx_T *cctx, char_u *line)
1711{
1712 isn_T *isn;
1713
Bram Moolenaar080457c2020-03-03 21:53:32 +01001714 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1716 return FAIL;
1717 isn->isn_arg.string = vim_strsave(line);
1718 return OK;
1719}
1720
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001721 static int
1722generate_EXECCONCAT(cctx_T *cctx, int count)
1723{
1724 isn_T *isn;
1725
1726 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1727 return FAIL;
1728 isn->isn_arg.number = count;
1729 return OK;
1730}
1731
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732/*
1733 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001734 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001736 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1738{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 lvar_T *lvar;
1740
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001741 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001743 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001744 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 }
1746
1747 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001748 return NULL;
1749 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001751 // Every local variable uses the next entry on the stack. We could re-use
1752 // the last ones when leaving a scope, but then variables used in a closure
1753 // might get overwritten. To keep things simple do not re-use stack
1754 // entries. This is less efficient, but memory is cheap these days.
1755 lvar->lv_idx = cctx->ctx_locals_count++;
1756
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001757 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 lvar->lv_const = isConst;
1759 lvar->lv_type = type;
1760
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001761 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762}
1763
1764/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001765 * Remove local variables above "new_top".
1766 */
1767 static void
1768unwind_locals(cctx_T *cctx, int new_top)
1769{
1770 if (cctx->ctx_locals.ga_len > new_top)
1771 {
1772 int idx;
1773 lvar_T *lvar;
1774
1775 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1776 {
1777 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1778 vim_free(lvar->lv_name);
1779 }
1780 }
1781 cctx->ctx_locals.ga_len = new_top;
1782}
1783
1784/*
1785 * Free all local variables.
1786 */
1787 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001788free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001789{
1790 unwind_locals(cctx, 0);
1791 ga_clear(&cctx->ctx_locals);
1792}
1793
1794/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 * Skip over a type definition and return a pointer to just after it.
1796 */
1797 char_u *
1798skip_type(char_u *start)
1799{
1800 char_u *p = start;
1801
1802 while (ASCII_ISALNUM(*p) || *p == '_')
1803 ++p;
1804
1805 // Skip over "<type>"; this is permissive about white space.
1806 if (*skipwhite(p) == '<')
1807 {
1808 p = skipwhite(p);
1809 p = skip_type(skipwhite(p + 1));
1810 p = skipwhite(p);
1811 if (*p == '>')
1812 ++p;
1813 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001814 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1815 {
1816 // handle func(args): type
1817 ++p;
1818 while (*p != ')' && *p != NUL)
1819 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001820 char_u *sp = p;
1821
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001822 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001823 if (p == sp)
1824 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001825 if (*p == ',')
1826 p = skipwhite(p + 1);
1827 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001828 if (*p == ')')
1829 {
1830 if (p[1] == ':')
1831 p = skip_type(skipwhite(p + 2));
1832 else
1833 p = skipwhite(p + 1);
1834 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001835 }
1836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 return p;
1838}
1839
1840/*
1841 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001842 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 * Returns NULL in case of failure.
1844 */
1845 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001846parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847{
1848 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001849 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850
1851 if (**arg != '<')
1852 {
1853 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001854 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 else
1856 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001857 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 }
1859 *arg = skipwhite(*arg + 1);
1860
Bram Moolenaard77a8522020-04-03 21:59:57 +02001861 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862
1863 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001864 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 {
1866 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001867 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 }
1869 ++*arg;
1870
1871 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001872 return get_list_type(member_type, type_gap);
1873 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874}
1875
1876/*
1877 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001878 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879 */
1880 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001881parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882{
1883 char_u *p = *arg;
1884 size_t len;
1885
1886 // skip over the first word
1887 while (ASCII_ISALNUM(*p) || *p == '_')
1888 ++p;
1889 len = p - *arg;
1890
1891 switch (**arg)
1892 {
1893 case 'a':
1894 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1895 {
1896 *arg += len;
1897 return &t_any;
1898 }
1899 break;
1900 case 'b':
1901 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1902 {
1903 *arg += len;
1904 return &t_bool;
1905 }
1906 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1907 {
1908 *arg += len;
1909 return &t_blob;
1910 }
1911 break;
1912 case 'c':
1913 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1914 {
1915 *arg += len;
1916 return &t_channel;
1917 }
1918 break;
1919 case 'd':
1920 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1921 {
1922 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001923 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 }
1925 break;
1926 case 'f':
1927 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1928 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001929#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 *arg += len;
1931 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001932#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001933 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001934 return &t_any;
1935#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936 }
1937 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1938 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001939 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001940 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001941 int argcount = -1;
1942 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001943 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001944 type_T *arg_type[MAX_FUNC_ARGS + 1];
1945
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001946 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001948 if (**arg == '(')
1949 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001950 // "func" may or may not return a value, "func()" does
1951 // not return a value.
1952 ret_type = &t_void;
1953
Bram Moolenaard77a8522020-04-03 21:59:57 +02001954 p = ++*arg;
1955 argcount = 0;
1956 while (*p != NUL && *p != ')')
1957 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001958 if (*p == '?')
1959 {
1960 if (first_optional == -1)
1961 first_optional = argcount;
1962 ++p;
1963 }
1964 else if (first_optional != -1)
1965 {
1966 emsg(_("E1007: mandatory argument after optional argument"));
1967 return &t_any;
1968 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001969 else if (STRNCMP(p, "...", 3) == 0)
1970 {
1971 flags |= TTFLAG_VARARGS;
1972 p += 3;
1973 }
1974
1975 arg_type[argcount++] = parse_type(&p, type_gap);
1976
1977 // Nothing comes after "...{type}".
1978 if (flags & TTFLAG_VARARGS)
1979 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001980
Bram Moolenaard77a8522020-04-03 21:59:57 +02001981 if (*p != ',' && *skipwhite(p) == ',')
1982 {
1983 semsg(_(e_no_white_before), ",");
1984 return &t_any;
1985 }
1986 if (*p == ',')
1987 {
1988 ++p;
1989 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001990 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001991 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001992 return &t_any;
1993 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001994 }
1995 p = skipwhite(p);
1996 if (argcount == MAX_FUNC_ARGS)
1997 {
1998 emsg(_("E740: Too many argument types"));
1999 return &t_any;
2000 }
2001 }
2002
2003 p = skipwhite(p);
2004 if (*p != ')')
2005 {
2006 emsg(_(e_missing_close));
2007 return &t_any;
2008 }
2009 *arg = p + 1;
2010 }
2011 if (**arg == ':')
2012 {
2013 // parse return type
2014 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002015 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002016 semsg(_(e_white_after), ":");
2017 *arg = skipwhite(*arg);
2018 ret_type = parse_type(arg, type_gap);
2019 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002020 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002021 type = get_func_type(ret_type, argcount, type_gap);
2022 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002023 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002024 type = alloc_func_type(ret_type, argcount, type_gap);
2025 type->tt_flags = flags;
2026 if (argcount > 0)
2027 {
2028 type->tt_argcount = argcount;
2029 type->tt_min_argcount = first_optional == -1
2030 ? argcount : first_optional;
2031 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002032 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002033 return &t_any;
2034 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002035 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002036 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002037 }
2038 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 }
2040 break;
2041 case 'j':
2042 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2043 {
2044 *arg += len;
2045 return &t_job;
2046 }
2047 break;
2048 case 'l':
2049 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2050 {
2051 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002052 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 }
2054 break;
2055 case 'n':
2056 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2057 {
2058 *arg += len;
2059 return &t_number;
2060 }
2061 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 case 's':
2063 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2064 {
2065 *arg += len;
2066 return &t_string;
2067 }
2068 break;
2069 case 'v':
2070 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2071 {
2072 *arg += len;
2073 return &t_void;
2074 }
2075 break;
2076 }
2077
2078 semsg(_("E1010: Type not recognized: %s"), *arg);
2079 return &t_any;
2080}
2081
2082/*
2083 * Check if "type1" and "type2" are exactly the same.
2084 */
2085 static int
2086equal_type(type_T *type1, type_T *type2)
2087{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002088 int i;
2089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 if (type1->tt_type != type2->tt_type)
2091 return FALSE;
2092 switch (type1->tt_type)
2093 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002095 case VAR_ANY:
2096 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 case VAR_SPECIAL:
2098 case VAR_BOOL:
2099 case VAR_NUMBER:
2100 case VAR_FLOAT:
2101 case VAR_STRING:
2102 case VAR_BLOB:
2103 case VAR_JOB:
2104 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002105 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 case VAR_LIST:
2107 case VAR_DICT:
2108 return equal_type(type1->tt_member, type2->tt_member);
2109 case VAR_FUNC:
2110 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002111 if (!equal_type(type1->tt_member, type2->tt_member)
2112 || type1->tt_argcount != type2->tt_argcount)
2113 return FALSE;
2114 if (type1->tt_argcount < 0
2115 || type1->tt_args == NULL || type2->tt_args == NULL)
2116 return TRUE;
2117 for (i = 0; i < type1->tt_argcount; ++i)
2118 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2119 return FALSE;
2120 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 }
2122 return TRUE;
2123}
2124
2125/*
2126 * Find the common type of "type1" and "type2" and put it in "dest".
2127 * "type2" and "dest" may be the same.
2128 */
2129 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002130common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131{
2132 if (equal_type(type1, type2))
2133 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002134 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 return;
2136 }
2137
2138 if (type1->tt_type == type2->tt_type)
2139 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2141 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002142 type_T *common;
2143
Bram Moolenaard77a8522020-04-03 21:59:57 +02002144 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002145 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002146 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002147 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002148 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 return;
2150 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002151 if (type1->tt_type == VAR_FUNC)
2152 {
2153 type_T *common;
2154
2155 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2156 if (type1->tt_argcount == type2->tt_argcount
2157 && type1->tt_argcount >= 0)
2158 {
2159 int argcount = type1->tt_argcount;
2160 int i;
2161
2162 *dest = alloc_func_type(common, argcount, type_gap);
2163 if (type1->tt_args != NULL && type2->tt_args != NULL)
2164 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002165 if (func_type_add_arg_types(*dest, argcount,
2166 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002167 for (i = 0; i < argcount; ++i)
2168 common_type(type1->tt_args[i], type2->tt_args[i],
2169 &(*dest)->tt_args[i], type_gap);
2170 }
2171 }
2172 else
2173 *dest = alloc_func_type(common, -1, type_gap);
2174 return;
2175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 }
2177
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002178 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179}
2180
2181 char *
2182vartype_name(vartype_T type)
2183{
2184 switch (type)
2185 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002186 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002187 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189 case VAR_SPECIAL: return "special";
2190 case VAR_BOOL: return "bool";
2191 case VAR_NUMBER: return "number";
2192 case VAR_FLOAT: return "float";
2193 case VAR_STRING: return "string";
2194 case VAR_BLOB: return "blob";
2195 case VAR_JOB: return "job";
2196 case VAR_CHANNEL: return "channel";
2197 case VAR_LIST: return "list";
2198 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002199
2200 case VAR_FUNC:
2201 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002203 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204}
2205
2206/*
2207 * Return the name of a type.
2208 * The result may be in allocated memory, in which case "tofree" is set.
2209 */
2210 char *
2211type_name(type_T *type, char **tofree)
2212{
2213 char *name = vartype_name(type->tt_type);
2214
2215 *tofree = NULL;
2216 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2217 {
2218 char *member_free;
2219 char *member_name = type_name(type->tt_member, &member_free);
2220 size_t len;
2221
2222 len = STRLEN(name) + STRLEN(member_name) + 3;
2223 *tofree = alloc(len);
2224 if (*tofree != NULL)
2225 {
2226 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2227 vim_free(member_free);
2228 return *tofree;
2229 }
2230 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002231 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002232 {
2233 garray_T ga;
2234 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002235 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002236
2237 ga_init2(&ga, 1, 100);
2238 if (ga_grow(&ga, 20) == FAIL)
2239 return "[unknown]";
2240 *tofree = ga.ga_data;
2241 STRCPY(ga.ga_data, "func(");
2242 ga.ga_len += 5;
2243
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002244 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002245 {
2246 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002247 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002248 int len;
2249
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002250 if (type->tt_args == NULL)
2251 arg_type = "[unknown]";
2252 else
2253 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002254 if (i > 0)
2255 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002256 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002257 ga.ga_len += 2;
2258 }
2259 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002260 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002261 {
2262 vim_free(arg_free);
2263 return "[unknown]";
2264 }
2265 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002266 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002267 {
2268 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2269 ga.ga_len += 3;
2270 }
2271 else if (i >= type->tt_min_argcount)
2272 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002273 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002274 ga.ga_len += len;
2275 vim_free(arg_free);
2276 }
2277
2278 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002279 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002280 else
2281 {
2282 char *ret_free;
2283 char *ret_name = type_name(type->tt_member, &ret_free);
2284 int len;
2285
2286 len = (int)STRLEN(ret_name) + 4;
2287 if (ga_grow(&ga, len) == FAIL)
2288 {
2289 vim_free(ret_free);
2290 return "[unknown]";
2291 }
2292 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002293 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2294 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002295 vim_free(ret_free);
2296 }
2297 return ga.ga_data;
2298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299
2300 return name;
2301}
2302
2303/*
2304 * Find "name" in script-local items of script "sid".
2305 * Returns the index in "sn_var_vals" if found.
2306 * If found but not in "sn_var_vals" returns -1.
2307 * If not found returns -2.
2308 */
2309 int
2310get_script_item_idx(int sid, char_u *name, int check_writable)
2311{
2312 hashtab_T *ht;
2313 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002314 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 int idx;
2316
2317 // First look the name up in the hashtable.
2318 if (sid <= 0 || sid > script_items.ga_len)
2319 return -1;
2320 ht = &SCRIPT_VARS(sid);
2321 di = find_var_in_ht(ht, 0, name, TRUE);
2322 if (di == NULL)
2323 return -2;
2324
2325 // Now find the svar_T index in sn_var_vals.
2326 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2327 {
2328 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2329
2330 if (sv->sv_tv == &di->di_tv)
2331 {
2332 if (check_writable && sv->sv_const)
2333 semsg(_(e_readonlyvar), name);
2334 return idx;
2335 }
2336 }
2337 return -1;
2338}
2339
2340/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002341 * Find "name" in imported items of the current script or in "cctx" if not
2342 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 */
2344 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002345find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002347 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 int idx;
2349
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002350 if (current_sctx.sc_sid <= 0)
2351 return NULL;
2352 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 if (cctx != NULL)
2354 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2355 {
2356 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2357 + idx;
2358
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002359 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2360 : STRLEN(import->imp_name) == len
2361 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 return import;
2363 }
2364
2365 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2366 {
2367 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2368
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002369 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2370 : STRLEN(import->imp_name) == len
2371 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 return import;
2373 }
2374 return NULL;
2375}
2376
2377/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002378 * Free all imported variables.
2379 */
2380 static void
2381free_imported(cctx_T *cctx)
2382{
2383 int idx;
2384
2385 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2386 {
2387 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2388
2389 vim_free(import->imp_name);
2390 }
2391 ga_clear(&cctx->ctx_imports);
2392}
2393
2394/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002395 * Return TRUE if "p" points at a "#" but not at "#{".
2396 */
2397 static int
2398comment_start(char_u *p)
2399{
2400 return p[0] == '#' && p[1] != '{';
2401}
2402
2403/*
2404 * Return a pointer to the next line that isn't empty or only contains a
2405 * comment. Skips over white space.
2406 * Returns NULL if there is none.
2407 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002408 char_u *
2409peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002410{
2411 int lnum = cctx->ctx_lnum;
2412
2413 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2414 {
2415 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002416 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002417
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002418 if (line == NULL)
2419 break;
2420 p = skipwhite(line);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002421 if (*p != NUL && !comment_start(p))
2422 return p;
2423 }
2424 return NULL;
2425}
2426
2427/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002428 * Called when checking for a following operator at "arg". When the rest of
2429 * the line is empty or only a comment, peek the next line. If there is a next
2430 * line return a pointer to it and set "nextp".
2431 * Otherwise skip over white space.
2432 */
2433 static char_u *
2434may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2435{
2436 char_u *p = skipwhite(arg);
2437
2438 *nextp = NULL;
2439 if (*p == NUL || (VIM_ISWHITE(*arg) && comment_start(p)))
2440 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002441 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002442 if (*nextp != NULL)
2443 return *nextp;
2444 }
2445 return p;
2446}
2447
2448/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002449 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002450 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002451 * Returns NULL when at the end.
2452 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002453 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002454next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002455{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002456 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002457
2458 do
2459 {
2460 ++cctx->ctx_lnum;
2461 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002462 {
2463 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002464 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002465 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002466 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002467 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002468 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002469 } while (line == NULL || *skipwhite(line) == NUL
2470 || (skip_comment && comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002471 return line;
2472}
2473
2474/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002475 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002476 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002477 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2478 */
2479 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002480may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002481{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002482 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002483 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002484 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002485
2486 if (next == NULL)
2487 return FAIL;
2488 *arg = skipwhite(next);
2489 }
2490 return OK;
2491}
2492
Bram Moolenaara5565e42020-05-09 15:44:01 +02002493// Structure passed between the compile_expr* functions to keep track of
2494// constants that have been parsed but for which no code was produced yet. If
2495// possible expressions on these constants are applied at compile time. If
2496// that is not possible, the code to push the constants needs to be generated
2497// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002498// Using 50 should be more than enough of 5 levels of ().
2499#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002500typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002501 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002502 int pp_used; // active entries in pp_tv[]
2503} ppconst_T;
2504
Bram Moolenaar1c747212020-05-09 18:28:34 +02002505static int compile_expr0(char_u **arg, cctx_T *cctx);
2506static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2507
Bram Moolenaara5565e42020-05-09 15:44:01 +02002508/*
2509 * Generate a PUSH instruction for "tv".
2510 * "tv" will be consumed or cleared.
2511 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2512 */
2513 static int
2514generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2515{
2516 if (tv != NULL)
2517 {
2518 switch (tv->v_type)
2519 {
2520 case VAR_UNKNOWN:
2521 break;
2522 case VAR_BOOL:
2523 generate_PUSHBOOL(cctx, tv->vval.v_number);
2524 break;
2525 case VAR_SPECIAL:
2526 generate_PUSHSPEC(cctx, tv->vval.v_number);
2527 break;
2528 case VAR_NUMBER:
2529 generate_PUSHNR(cctx, tv->vval.v_number);
2530 break;
2531#ifdef FEAT_FLOAT
2532 case VAR_FLOAT:
2533 generate_PUSHF(cctx, tv->vval.v_float);
2534 break;
2535#endif
2536 case VAR_BLOB:
2537 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2538 tv->vval.v_blob = NULL;
2539 break;
2540 case VAR_STRING:
2541 generate_PUSHS(cctx, tv->vval.v_string);
2542 tv->vval.v_string = NULL;
2543 break;
2544 default:
2545 iemsg("constant type not supported");
2546 clear_tv(tv);
2547 return FAIL;
2548 }
2549 tv->v_type = VAR_UNKNOWN;
2550 }
2551 return OK;
2552}
2553
2554/*
2555 * Generate code for any ppconst entries.
2556 */
2557 static int
2558generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2559{
2560 int i;
2561 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002562 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002563
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002564 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002565 for (i = 0; i < ppconst->pp_used; ++i)
2566 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2567 ret = FAIL;
2568 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002569 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002570 return ret;
2571}
2572
2573/*
2574 * Clear ppconst constants. Used when failing.
2575 */
2576 static void
2577clear_ppconst(ppconst_T *ppconst)
2578{
2579 int i;
2580
2581 for (i = 0; i < ppconst->pp_used; ++i)
2582 clear_tv(&ppconst->pp_tv[i]);
2583 ppconst->pp_used = 0;
2584}
2585
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002586/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002587 * Generate an instruction to load script-local variable "name", without the
2588 * leading "s:".
2589 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 */
2591 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002592compile_load_scriptvar(
2593 cctx_T *cctx,
2594 char_u *name, // variable NUL terminated
2595 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002596 char_u **end, // end of variable
2597 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002599 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2601 imported_T *import;
2602
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002603 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002605 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002606 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2607 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 }
2609 if (idx >= 0)
2610 {
2611 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2612
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002613 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 current_sctx.sc_sid, idx, sv->sv_type);
2615 return OK;
2616 }
2617
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002618 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 if (import != NULL)
2620 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002621 if (import->imp_all)
2622 {
2623 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002624 char_u *exp_name;
2625 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002626 ufunc_T *ufunc;
2627 type_T *type;
2628
2629 // Used "import * as Name", need to lookup the member.
2630 if (*p != '.')
2631 {
2632 semsg(_("E1060: expected dot after name: %s"), start);
2633 return FAIL;
2634 }
2635 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002636 if (VIM_ISWHITE(*p))
2637 {
2638 emsg(_("E1074: no white space allowed after dot"));
2639 return FAIL;
2640 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002641
Bram Moolenaar1c991142020-07-04 13:15:31 +02002642 // isolate one name
2643 exp_name = p;
2644 while (eval_isnamec(*p))
2645 ++p;
2646 cc = *p;
2647 *p = NUL;
2648
2649 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2650 *p = cc;
2651 p = skipwhite(p);
2652
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002653 // TODO: what if it is a function?
2654 if (idx < 0)
2655 return FAIL;
2656 *end = p;
2657
2658 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2659 import->imp_sid,
2660 idx,
2661 type);
2662 }
2663 else
2664 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002665 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002666 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2667 import->imp_sid,
2668 import->imp_var_vals_idx,
2669 import->imp_type);
2670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 return OK;
2672 }
2673
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002674 if (error)
2675 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 return FAIL;
2677}
2678
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002679 static int
2680generate_funcref(cctx_T *cctx, char_u *name)
2681{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002682 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002683
2684 if (ufunc == NULL)
2685 return FAIL;
2686
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002687 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2688 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002689}
2690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691/*
2692 * Compile a variable name into a load instruction.
2693 * "end" points to just after the name.
2694 * When "error" is FALSE do not give an error when not found.
2695 */
2696 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002697compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698{
2699 type_T *type;
2700 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002701 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002703 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704
2705 if (*(*arg + 1) == ':')
2706 {
2707 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002708 if (end <= *arg + 2)
2709 name = vim_strsave((char_u *)"[empty]");
2710 else
2711 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 if (name == NULL)
2713 return FAIL;
2714
2715 if (**arg == 'v')
2716 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002717 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 }
2719 else if (**arg == 'g')
2720 {
2721 // Global variables can be defined later, thus we don't check if it
2722 // exists, give error at runtime.
2723 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2724 }
2725 else if (**arg == 's')
2726 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002727 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002729 else if (**arg == 'b')
2730 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002731 // Buffer-local variables can be defined later, thus we don't check
2732 // if it exists, give error at runtime.
2733 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002734 }
2735 else if (**arg == 'w')
2736 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002737 // Window-local variables can be defined later, thus we don't check
2738 // if it exists, give error at runtime.
2739 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002740 }
2741 else if (**arg == 't')
2742 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002743 // Tabpage-local variables can be defined later, thus we don't
2744 // check if it exists, give error at runtime.
2745 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 else
2748 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002749 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 goto theend;
2751 }
2752 }
2753 else
2754 {
2755 size_t len = end - *arg;
2756 int idx;
2757 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002758 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759
2760 name = vim_strnsave(*arg, end - *arg);
2761 if (name == NULL)
2762 return FAIL;
2763
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002764 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002766 if (!gen_load_outer)
2767 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 }
2769 else
2770 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002771 lvar_T *lvar = lookup_local(*arg, len, cctx);
2772
2773 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002775 type = lvar->lv_type;
2776 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002777 if (lvar->lv_from_outer)
2778 gen_load_outer = TRUE;
2779 else
2780 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 }
2782 else
2783 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002784 // "var" can be script-local even without using "s:" if it
2785 // already exists.
2786 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2787 == SCRIPT_VERSION_VIM9
2788 || lookup_script(*arg, len) == OK)
2789 res = compile_load_scriptvar(cctx, name, *arg, &end,
2790 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002791
Bram Moolenaara5565e42020-05-09 15:44:01 +02002792 // When the name starts with an uppercase letter or "x:" it
2793 // can be a user defined function.
2794 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2795 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 }
2797 }
2798 if (gen_load)
2799 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002800 if (gen_load_outer)
2801 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 }
2803
2804 *arg = end;
2805
2806theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002807 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 semsg(_(e_var_notfound), name);
2809 vim_free(name);
2810 return res;
2811}
2812
2813/*
2814 * Compile the argument expressions.
2815 * "arg" points to just after the "(" and is advanced to after the ")"
2816 */
2817 static int
2818compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2819{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002820 char_u *p = *arg;
2821 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822
Bram Moolenaare6085c52020-04-12 20:19:16 +02002823 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002825 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2826 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002827 if (*p == ')')
2828 {
2829 *arg = p + 1;
2830 return OK;
2831 }
2832
Bram Moolenaara5565e42020-05-09 15:44:01 +02002833 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 return FAIL;
2835 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002836
2837 if (*p != ',' && *skipwhite(p) == ',')
2838 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002839 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002840 p = skipwhite(p);
2841 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002843 {
2844 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002845 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002846 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002847 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002848 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002849 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002851failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002852 emsg(_(e_missing_close));
2853 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854}
2855
2856/*
2857 * Compile a function call: name(arg1, arg2)
2858 * "arg" points to "name", "arg + varlen" to the "(".
2859 * "argcount_init" is 1 for "value->method()"
2860 * Instructions:
2861 * EVAL arg1
2862 * EVAL arg2
2863 * BCALL / DCALL / UCALL
2864 */
2865 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002866compile_call(
2867 char_u **arg,
2868 size_t varlen,
2869 cctx_T *cctx,
2870 ppconst_T *ppconst,
2871 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872{
2873 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002874 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 int argcount = argcount_init;
2876 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002877 char_u fname_buf[FLEN_FIXED + 1];
2878 char_u *tofree = NULL;
2879 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002881 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882
Bram Moolenaara5565e42020-05-09 15:44:01 +02002883 // we can evaluate "has('name')" at compile time
2884 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2885 {
2886 char_u *s = skipwhite(*arg + varlen + 1);
2887 typval_T argvars[2];
2888
2889 argvars[0].v_type = VAR_UNKNOWN;
2890 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002891 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002892 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002893 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002894 s = skipwhite(s);
2895 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2896 {
2897 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2898
2899 *arg = s + 1;
2900 argvars[1].v_type = VAR_UNKNOWN;
2901 tv->v_type = VAR_NUMBER;
2902 tv->vval.v_number = 0;
2903 f_has(argvars, tv);
2904 clear_tv(&argvars[0]);
2905 ++ppconst->pp_used;
2906 return OK;
2907 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002908 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002909 }
2910
2911 if (generate_ppconst(cctx, ppconst) == FAIL)
2912 return FAIL;
2913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 if (varlen >= sizeof(namebuf))
2915 {
2916 semsg(_("E1011: name too long: %s"), name);
2917 return FAIL;
2918 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002919 vim_strncpy(namebuf, *arg, varlen);
2920 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921
2922 *arg = skipwhite(*arg + varlen + 1);
2923 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002924 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002926 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 {
2928 int idx;
2929
2930 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002931 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002933 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002934 else
2935 semsg(_(e_unknownfunc), namebuf);
2936 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 }
2938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002940 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002942 {
2943 res = generate_CALL(cctx, ufunc, argcount);
2944 goto theend;
2945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946
2947 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002948 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002950 if (STRNCMP(namebuf, "g:", 2) != 0
2951 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002952 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002953 garray_T *stack = &cctx->ctx_type_stack;
2954 type_T *type;
2955
2956 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2957 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002958 goto theend;
2959 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002961 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002962 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002963 if (STRNCMP(namebuf, "g:", 2) == 0)
2964 res = generate_UCALL(cctx, name, argcount);
2965 else
2966 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002967
2968theend:
2969 vim_free(tofree);
2970 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971}
2972
2973// like NAMESPACE_CHAR but with 'a' and 'l'.
2974#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2975
2976/*
2977 * Find the end of a variable or function name. Unlike find_name_end() this
2978 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002979 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 * Return a pointer to just after the name. Equal to "arg" if there is no
2981 * valid name.
2982 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002983 static char_u *
2984to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985{
2986 char_u *p;
2987
2988 // Quick check for valid starting character.
2989 if (!eval_isnamec1(*arg))
2990 return arg;
2991
2992 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2993 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2994 // and can be used in slice "[n:]".
2995 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002996 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2998 break;
2999 return p;
3000}
3001
3002/*
3003 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003004 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 */
3006 char_u *
3007to_name_const_end(char_u *arg)
3008{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003009 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 typval_T rettv;
3011
3012 if (p == arg && *arg == '[')
3013 {
3014
3015 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003016 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 p = arg;
3018 }
3019 else if (p == arg && *arg == '#' && arg[1] == '{')
3020 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003021 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003023 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 p = arg;
3025 }
3026 else if (p == arg && *arg == '{')
3027 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003028 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003030 // Can be "{x -> ret}()".
3031 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003033 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 if (ret != OK)
3035 p = arg;
3036 }
3037
3038 return p;
3039}
3040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041/*
3042 * parse a list: [expr, expr]
3043 * "*arg" points to the '['.
3044 */
3045 static int
3046compile_list(char_u **arg, cctx_T *cctx)
3047{
3048 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003049 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 int count = 0;
3051
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003052 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003054 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003055 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003056 semsg(_(e_list_end), *arg);
3057 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003058 }
3059 if (*p == ']')
3060 {
3061 ++p;
3062 // Allow for following comment, after at least one space.
3063 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
3064 p += STRLEN(p);
3065 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003066 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003067 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 break;
3069 ++count;
3070 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003071 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003073 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3074 {
3075 semsg(_(e_white_after), ",");
3076 return FAIL;
3077 }
3078 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003079 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 p = skipwhite(p);
3081 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003082 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083
3084 generate_NEWLIST(cctx, count);
3085 return OK;
3086}
3087
3088/*
3089 * parse a lambda: {arg, arg -> expr}
3090 * "*arg" points to the '{'.
3091 */
3092 static int
3093compile_lambda(char_u **arg, cctx_T *cctx)
3094{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 typval_T rettv;
3096 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003097 evalarg_T evalarg;
3098
3099 CLEAR_FIELD(evalarg);
3100 evalarg.eval_flags = EVAL_EVALUATE;
3101 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102
3103 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003104 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003108 ++ufunc->uf_refcount;
3109 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003110 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111
3112 // The function will have one line: "return {expr}".
3113 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003114 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003116 clear_evalarg(&evalarg, NULL);
3117
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003118 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003119 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 return FAIL;
3121}
3122
3123/*
3124 * Compile a lamda call: expr->{lambda}(args)
3125 * "arg" points to the "{".
3126 */
3127 static int
3128compile_lambda_call(char_u **arg, cctx_T *cctx)
3129{
3130 ufunc_T *ufunc;
3131 typval_T rettv;
3132 int argcount = 1;
3133 int ret = FAIL;
3134
3135 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003136 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 return FAIL;
3138
3139 if (**arg != '(')
3140 {
3141 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003142 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 else
3144 semsg(_(e_missing_paren), "lambda");
3145 clear_tv(&rettv);
3146 return FAIL;
3147 }
3148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 ufunc = rettv.vval.v_partial->pt_func;
3150 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003151 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003152 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003153
3154 // The function will have one line: "return {expr}".
3155 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003156 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157
3158 // compile the arguments
3159 *arg = skipwhite(*arg + 1);
3160 if (compile_arguments(arg, cctx, &argcount) == OK)
3161 // call the compiled function
3162 ret = generate_CALL(cctx, ufunc, argcount);
3163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 return ret;
3165}
3166
3167/*
3168 * parse a dict: {'key': val} or #{key: val}
3169 * "*arg" points to the '{'.
3170 */
3171 static int
3172compile_dict(char_u **arg, cctx_T *cctx, int literal)
3173{
3174 garray_T *instr = &cctx->ctx_instr;
3175 int count = 0;
3176 dict_T *d = dict_alloc();
3177 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003178 char_u *whitep = *arg;
3179 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180
3181 if (d == NULL)
3182 return FAIL;
3183 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003184 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 {
3186 char_u *key = NULL;
3187
Bram Moolenaar23c55272020-06-21 16:58:13 +02003188 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003189 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003190 *arg = NULL;
3191 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003192 }
3193
3194 if (**arg == '}')
3195 break;
3196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 if (literal)
3198 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003199 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200
Bram Moolenaar2c330432020-04-13 14:41:35 +02003201 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 {
3203 semsg(_("E1014: Invalid key: %s"), *arg);
3204 return FAIL;
3205 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003206 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 if (generate_PUSHS(cctx, key) == FAIL)
3208 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003209 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 }
3211 else
3212 {
3213 isn_T *isn;
3214
Bram Moolenaara5565e42020-05-09 15:44:01 +02003215 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 return FAIL;
3217 // TODO: check type is string
3218 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3219 if (isn->isn_type == ISN_PUSHS)
3220 key = isn->isn_arg.string;
3221 }
3222
3223 // Check for duplicate keys, if using string keys.
3224 if (key != NULL)
3225 {
3226 item = dict_find(d, key, -1);
3227 if (item != NULL)
3228 {
3229 semsg(_(e_duplicate_key), key);
3230 goto failret;
3231 }
3232 item = dictitem_alloc(key);
3233 if (item != NULL)
3234 {
3235 item->di_tv.v_type = VAR_UNKNOWN;
3236 item->di_tv.v_lock = 0;
3237 if (dict_add(d, item) == FAIL)
3238 dictitem_free(item);
3239 }
3240 }
3241
3242 *arg = skipwhite(*arg);
3243 if (**arg != ':')
3244 {
3245 semsg(_(e_missing_dict_colon), *arg);
3246 return FAIL;
3247 }
3248
Bram Moolenaar2c330432020-04-13 14:41:35 +02003249 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003251 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003252 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003253 *arg = NULL;
3254 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003255 }
3256
Bram Moolenaara5565e42020-05-09 15:44:01 +02003257 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 return FAIL;
3259 ++count;
3260
Bram Moolenaar2c330432020-04-13 14:41:35 +02003261 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003262 *arg = skipwhite(*arg);
3263 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003264 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003265 *arg = NULL;
3266 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003267 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 if (**arg == '}')
3269 break;
3270 if (**arg != ',')
3271 {
3272 semsg(_(e_missing_dict_comma), *arg);
3273 goto failret;
3274 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003275 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 *arg = skipwhite(*arg + 1);
3277 }
3278
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 *arg = *arg + 1;
3280
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003281 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003282 p = skipwhite(*arg);
3283 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003284 *arg += STRLEN(*arg);
3285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 dict_unref(d);
3287 return generate_NEWDICT(cctx, count);
3288
3289failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003290 if (*arg == NULL)
3291 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 dict_unref(d);
3293 return FAIL;
3294}
3295
3296/*
3297 * Compile "&option".
3298 */
3299 static int
3300compile_get_option(char_u **arg, cctx_T *cctx)
3301{
3302 typval_T rettv;
3303 char_u *start = *arg;
3304 int ret;
3305
3306 // parse the option and get the current value to get the type.
3307 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003308 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 if (ret == OK)
3310 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003311 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 char_u *name = vim_strnsave(start, *arg - start);
3313 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3314
3315 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3316 vim_free(name);
3317 }
3318 clear_tv(&rettv);
3319
3320 return ret;
3321}
3322
3323/*
3324 * Compile "$VAR".
3325 */
3326 static int
3327compile_get_env(char_u **arg, cctx_T *cctx)
3328{
3329 char_u *start = *arg;
3330 int len;
3331 int ret;
3332 char_u *name;
3333
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 ++*arg;
3335 len = get_env_len(arg);
3336 if (len == 0)
3337 {
3338 semsg(_(e_syntax_at), start - 1);
3339 return FAIL;
3340 }
3341
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003342 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 name = vim_strnsave(start, len + 1);
3344 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3345 vim_free(name);
3346 return ret;
3347}
3348
3349/*
3350 * Compile "@r".
3351 */
3352 static int
3353compile_get_register(char_u **arg, cctx_T *cctx)
3354{
3355 int ret;
3356
3357 ++*arg;
3358 if (**arg == NUL)
3359 {
3360 semsg(_(e_syntax_at), *arg - 1);
3361 return FAIL;
3362 }
3363 if (!valid_yank_reg(**arg, TRUE))
3364 {
3365 emsg_invreg(**arg);
3366 return FAIL;
3367 }
3368 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3369 ++*arg;
3370 return ret;
3371}
3372
3373/*
3374 * Apply leading '!', '-' and '+' to constant "rettv".
3375 */
3376 static int
3377apply_leader(typval_T *rettv, char_u *start, char_u *end)
3378{
3379 char_u *p = end;
3380
3381 // this works from end to start
3382 while (p > start)
3383 {
3384 --p;
3385 if (*p == '-' || *p == '+')
3386 {
3387 // only '-' has an effect, for '+' we only check the type
3388#ifdef FEAT_FLOAT
3389 if (rettv->v_type == VAR_FLOAT)
3390 {
3391 if (*p == '-')
3392 rettv->vval.v_float = -rettv->vval.v_float;
3393 }
3394 else
3395#endif
3396 {
3397 varnumber_T val;
3398 int error = FALSE;
3399
3400 // tv_get_number_chk() accepts a string, but we don't want that
3401 // here
3402 if (check_not_string(rettv) == FAIL)
3403 return FAIL;
3404 val = tv_get_number_chk(rettv, &error);
3405 clear_tv(rettv);
3406 if (error)
3407 return FAIL;
3408 if (*p == '-')
3409 val = -val;
3410 rettv->v_type = VAR_NUMBER;
3411 rettv->vval.v_number = val;
3412 }
3413 }
3414 else
3415 {
3416 int v = tv2bool(rettv);
3417
3418 // '!' is permissive in the type.
3419 clear_tv(rettv);
3420 rettv->v_type = VAR_BOOL;
3421 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3422 }
3423 }
3424 return OK;
3425}
3426
3427/*
3428 * Recognize v: variables that are constants and set "rettv".
3429 */
3430 static void
3431get_vim_constant(char_u **arg, typval_T *rettv)
3432{
3433 if (STRNCMP(*arg, "v:true", 6) == 0)
3434 {
3435 rettv->v_type = VAR_BOOL;
3436 rettv->vval.v_number = VVAL_TRUE;
3437 *arg += 6;
3438 }
3439 else if (STRNCMP(*arg, "v:false", 7) == 0)
3440 {
3441 rettv->v_type = VAR_BOOL;
3442 rettv->vval.v_number = VVAL_FALSE;
3443 *arg += 7;
3444 }
3445 else if (STRNCMP(*arg, "v:null", 6) == 0)
3446 {
3447 rettv->v_type = VAR_SPECIAL;
3448 rettv->vval.v_number = VVAL_NULL;
3449 *arg += 6;
3450 }
3451 else if (STRNCMP(*arg, "v:none", 6) == 0)
3452 {
3453 rettv->v_type = VAR_SPECIAL;
3454 rettv->vval.v_number = VVAL_NONE;
3455 *arg += 6;
3456 }
3457}
3458
Bram Moolenaar61a89812020-05-07 16:58:17 +02003459 static exptype_T
3460get_compare_type(char_u *p, int *len, int *type_is)
3461{
3462 exptype_T type = EXPR_UNKNOWN;
3463 int i;
3464
3465 switch (p[0])
3466 {
3467 case '=': if (p[1] == '=')
3468 type = EXPR_EQUAL;
3469 else if (p[1] == '~')
3470 type = EXPR_MATCH;
3471 break;
3472 case '!': if (p[1] == '=')
3473 type = EXPR_NEQUAL;
3474 else if (p[1] == '~')
3475 type = EXPR_NOMATCH;
3476 break;
3477 case '>': if (p[1] != '=')
3478 {
3479 type = EXPR_GREATER;
3480 *len = 1;
3481 }
3482 else
3483 type = EXPR_GEQUAL;
3484 break;
3485 case '<': if (p[1] != '=')
3486 {
3487 type = EXPR_SMALLER;
3488 *len = 1;
3489 }
3490 else
3491 type = EXPR_SEQUAL;
3492 break;
3493 case 'i': if (p[1] == 's')
3494 {
3495 // "is" and "isnot"; but not a prefix of a name
3496 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3497 *len = 5;
3498 i = p[*len];
3499 if (!isalnum(i) && i != '_')
3500 {
3501 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3502 *type_is = TRUE;
3503 }
3504 }
3505 break;
3506 }
3507 return type;
3508}
3509
3510/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 * Compile code to apply '-', '+' and '!'.
3512 */
3513 static int
3514compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3515{
3516 char_u *p = end;
3517
3518 // this works from end to start
3519 while (p > start)
3520 {
3521 --p;
3522 if (*p == '-' || *p == '+')
3523 {
3524 int negate = *p == '-';
3525 isn_T *isn;
3526
3527 // TODO: check type
3528 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3529 {
3530 --p;
3531 if (*p == '-')
3532 negate = !negate;
3533 }
3534 // only '-' has an effect, for '+' we only check the type
3535 if (negate)
3536 isn = generate_instr(cctx, ISN_NEGATENR);
3537 else
3538 isn = generate_instr(cctx, ISN_CHECKNR);
3539 if (isn == NULL)
3540 return FAIL;
3541 }
3542 else
3543 {
3544 int invert = TRUE;
3545
3546 while (p > start && p[-1] == '!')
3547 {
3548 --p;
3549 invert = !invert;
3550 }
3551 if (generate_2BOOL(cctx, invert) == FAIL)
3552 return FAIL;
3553 }
3554 }
3555 return OK;
3556}
3557
3558/*
3559 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003560 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 */
3562 static int
3563compile_subscript(
3564 char_u **arg,
3565 cctx_T *cctx,
3566 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003567 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003568 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569{
3570 for (;;)
3571 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003572 char_u *p = skipwhite(*arg);
3573
3574 if (*p == NUL || (VIM_ISWHITE(**arg) && comment_start(p)))
3575 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003576 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003577
3578 // If a following line starts with "->{" or "->X" advance to that
3579 // line, so that a line break before "->" is allowed.
3580 if (next != NULL && next[0] == '-' && next[1] == '>'
3581 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3582 {
3583 next = next_line_from_context(cctx, TRUE);
3584 if (next == NULL)
3585 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003586 *arg = next;
3587 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003588 }
3589 }
3590
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003591 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003593 garray_T *stack = &cctx->ctx_type_stack;
3594 type_T *type;
3595 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003597 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003598 return FAIL;
3599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003601 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3602
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003603 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3605 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003606 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 return FAIL;
3608 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003609 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003611 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003612 return FAIL;
3613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 // something->method()
3615 // Apply the '!', '-' and '+' first:
3616 // -1.0->func() works like (-1.0)->func()
3617 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3618 return FAIL;
3619 *start_leader = end_leader; // don't apply again later
3620
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003621 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003622 *arg = skipwhite(p);
3623 if (may_get_next_line(p, arg, cctx) == FAIL)
3624 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 if (**arg == '{')
3626 {
3627 // lambda call: list->{lambda}
3628 if (compile_lambda_call(arg, cctx) == FAIL)
3629 return FAIL;
3630 }
3631 else
3632 {
3633 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003634 p = *arg;
3635 if (ASCII_ISALPHA(*p) && p[1] == ':')
3636 p += 2;
3637 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 ;
3639 if (*p != '(')
3640 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003641 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 return FAIL;
3643 }
3644 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003645 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 return FAIL;
3647 }
3648 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003649 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003651 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003652 type_T **typep;
3653
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003654 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003655 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003656 // TODO: blob index
3657 // TODO: more arguments
3658 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003659 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003660 return FAIL;
3661
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003662 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003663 *arg = skipwhite(p);
3664 if (may_get_next_line(p, arg, cctx) == FAIL)
3665 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003666 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 return FAIL;
3668
3669 if (**arg != ']')
3670 {
3671 emsg(_(e_missbrac));
3672 return FAIL;
3673 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003674 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003676 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3677 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003678 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003679 if ((*typep)->tt_type == VAR_LIST)
3680 *typep = (*typep)->tt_member;
3681 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3682 return FAIL;
3683 }
3684 else if ((*typep)->tt_type == VAR_DICT)
3685 {
3686 *typep = (*typep)->tt_member;
3687 if (may_generate_2STRING(-1, cctx) == FAIL)
3688 return FAIL;
3689 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3690 return FAIL;
3691 }
3692 else
3693 {
3694 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003695 return FAIL;
3696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003698 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003700 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003701 return FAIL;
3702
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003703 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003704 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3705 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003707 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 if (eval_isnamec1(*p))
3709 while (eval_isnamec(*p))
3710 MB_PTR_ADV(p);
3711 if (p == *arg)
3712 {
3713 semsg(_(e_syntax_at), *arg);
3714 return FAIL;
3715 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003716 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 return FAIL;
3718 *arg = p;
3719 }
3720 else
3721 break;
3722 }
3723
3724 // TODO - see handle_subscript():
3725 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3726 // Don't do this when "Func" is already a partial that was bound
3727 // explicitly (pt_auto is FALSE).
3728
3729 return OK;
3730}
3731
3732/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003733 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3734 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003736 * If the value is a constant "ppconst->pp_ret" will be set.
3737 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003738 *
3739 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 */
3741
3742/*
3743 * number number constant
3744 * 0zFFFFFFFF Blob constant
3745 * "string" string constant
3746 * 'string' literal string constant
3747 * &option-name option value
3748 * @r register contents
3749 * identifier variable value
3750 * function() function call
3751 * $VAR environment variable
3752 * (expression) nested expression
3753 * [expr, expr] List
3754 * {key: val, key: val} Dictionary
3755 * #{key: val, key: val} Dictionary with literal keys
3756 *
3757 * Also handle:
3758 * ! in front logical NOT
3759 * - in front unary minus
3760 * + in front unary plus (ignored)
3761 * trailing (arg) funcref/partial call
3762 * trailing [] subscript in String or List
3763 * trailing .name entry in Dictionary
3764 * trailing ->name() method call
3765 */
3766 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003767compile_expr7(
3768 char_u **arg,
3769 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003770 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 char_u *start_leader, *end_leader;
3773 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003774 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003775 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776
3777 /*
3778 * Skip '!', '-' and '+' characters. They are handled later.
3779 */
3780 start_leader = *arg;
3781 while (**arg == '!' || **arg == '-' || **arg == '+')
3782 *arg = skipwhite(*arg + 1);
3783 end_leader = *arg;
3784
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003785 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 switch (**arg)
3787 {
3788 /*
3789 * Number constant.
3790 */
3791 case '0': // also for blob starting with 0z
3792 case '1':
3793 case '2':
3794 case '3':
3795 case '4':
3796 case '5':
3797 case '6':
3798 case '7':
3799 case '8':
3800 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003801 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 return FAIL;
3803 break;
3804
3805 /*
3806 * String constant: "string".
3807 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003808 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 return FAIL;
3810 break;
3811
3812 /*
3813 * Literal string constant: 'str''ing'.
3814 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003815 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 return FAIL;
3817 break;
3818
3819 /*
3820 * Constant Vim variable.
3821 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003822 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 ret = NOTDONE;
3824 break;
3825
3826 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003827 * "true" constant
3828 */
3829 case 't': if (STRNCMP(*arg, "true", 4) == 0
3830 && !eval_isnamec((*arg)[4]))
3831 {
3832 *arg += 4;
3833 rettv->v_type = VAR_BOOL;
3834 rettv->vval.v_number = VVAL_TRUE;
3835 }
3836 else
3837 ret = NOTDONE;
3838 break;
3839
3840 /*
3841 * "false" constant
3842 */
3843 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3844 && !eval_isnamec((*arg)[5]))
3845 {
3846 *arg += 5;
3847 rettv->v_type = VAR_BOOL;
3848 rettv->vval.v_number = VVAL_FALSE;
3849 }
3850 else
3851 ret = NOTDONE;
3852 break;
3853
3854 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 * List: [expr, expr]
3856 */
3857 case '[': ret = compile_list(arg, cctx);
3858 break;
3859
3860 /*
3861 * Dictionary: #{key: val, key: val}
3862 */
3863 case '#': if ((*arg)[1] == '{')
3864 {
3865 ++*arg;
3866 ret = compile_dict(arg, cctx, TRUE);
3867 }
3868 else
3869 ret = NOTDONE;
3870 break;
3871
3872 /*
3873 * Lambda: {arg, arg -> expr}
3874 * Dictionary: {'key': val, 'key': val}
3875 */
3876 case '{': {
3877 char_u *start = skipwhite(*arg + 1);
3878
3879 // Find out what comes after the arguments.
3880 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003881 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 if (ret != FAIL && *start == '>')
3883 ret = compile_lambda(arg, cctx);
3884 else
3885 ret = compile_dict(arg, cctx, FALSE);
3886 }
3887 break;
3888
3889 /*
3890 * Option value: &name
3891 */
3892 case '&': ret = compile_get_option(arg, cctx);
3893 break;
3894
3895 /*
3896 * Environment variable: $VAR.
3897 */
3898 case '$': ret = compile_get_env(arg, cctx);
3899 break;
3900
3901 /*
3902 * Register contents: @r.
3903 */
3904 case '@': ret = compile_get_register(arg, cctx);
3905 break;
3906 /*
3907 * nested expression: (expression).
3908 */
3909 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003910
3911 // recursive!
3912 if (ppconst->pp_used <= PPSIZE - 10)
3913 {
3914 ret = compile_expr1(arg, cctx, ppconst);
3915 }
3916 else
3917 {
3918 // Not enough space in ppconst, flush constants.
3919 if (generate_ppconst(cctx, ppconst) == FAIL)
3920 return FAIL;
3921 ret = compile_expr0(arg, cctx);
3922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 *arg = skipwhite(*arg);
3924 if (**arg == ')')
3925 ++*arg;
3926 else if (ret == OK)
3927 {
3928 emsg(_(e_missing_close));
3929 ret = FAIL;
3930 }
3931 break;
3932
3933 default: ret = NOTDONE;
3934 break;
3935 }
3936 if (ret == FAIL)
3937 return FAIL;
3938
Bram Moolenaar1c747212020-05-09 18:28:34 +02003939 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 {
3941 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003942 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003944 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 return FAIL;
3946 }
3947 start_leader = end_leader; // don't apply again below
3948
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003949 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003950 clear_tv(rettv);
3951 else
3952 // A constant expression can possibly be handled compile time,
3953 // return the value instead of generating code.
3954 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 }
3956 else if (ret == NOTDONE)
3957 {
3958 char_u *p;
3959 int r;
3960
3961 if (!eval_isnamec1(**arg))
3962 {
3963 semsg(_("E1015: Name expected: %s"), *arg);
3964 return FAIL;
3965 }
3966
3967 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003968 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003970 {
3971 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3972 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003974 {
3975 if (generate_ppconst(cctx, ppconst) == FAIL)
3976 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 if (r == FAIL)
3980 return FAIL;
3981 }
3982
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003983 // Handle following "[]", ".member", etc.
3984 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003985 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003986 ppconst) == FAIL)
3987 return FAIL;
3988 if (ppconst->pp_used > 0)
3989 {
3990 // apply the '!', '-' and '+' before the constant
3991 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3992 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3993 return FAIL;
3994 return OK;
3995 }
3996 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003998 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999}
4000
4001/*
4002 * * number multiplication
4003 * / number division
4004 * % number modulo
4005 */
4006 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004007compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008{
4009 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004010 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004011 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004013 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004014 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 return FAIL;
4016
4017 /*
4018 * Repeat computing, until no "*", "/" or "%" is following.
4019 */
4020 for (;;)
4021 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004022 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 if (*op != '*' && *op != '/' && *op != '%')
4024 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004025 if (next != NULL)
4026 {
4027 *arg = next_line_from_context(cctx, TRUE);
4028 op = skipwhite(*arg);
4029 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004030
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004031 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 {
4033 char_u buf[3];
4034
4035 vim_strncpy(buf, op, 1);
4036 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004037 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 }
4039 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004040 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004041 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004043 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004044 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004046
4047 if (ppconst->pp_used == ppconst_used + 2
4048 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4049 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004050 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004051 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4052 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004053 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004055 // both are numbers: compute the result
4056 switch (*op)
4057 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004058 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004059 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004060 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004061 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004062 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004063 break;
4064 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004065 tv1->vval.v_number = res;
4066 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004067 }
4068 else
4069 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004070 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004071 generate_two_op(cctx, op);
4072 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 }
4074
4075 return OK;
4076}
4077
4078/*
4079 * + number addition
4080 * - number subtraction
4081 * .. string concatenation
4082 */
4083 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004084compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085{
4086 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004087 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004089 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090
4091 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004092 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 return FAIL;
4094
4095 /*
4096 * Repeat computing, until no "+", "-" or ".." is following.
4097 */
4098 for (;;)
4099 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004100 op = may_peek_next_line(cctx, *arg, &next);
4101 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 break;
4103 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004104 if (next != NULL)
4105 {
4106 *arg = next_line_from_context(cctx, TRUE);
4107 op = skipwhite(*arg);
4108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004110 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 {
4112 char_u buf[3];
4113
4114 vim_strncpy(buf, op, oplen);
4115 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004116 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 }
4118
4119 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004120 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004121 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004123 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004124 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 return FAIL;
4126
Bram Moolenaara5565e42020-05-09 15:44:01 +02004127 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004128 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004129 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4130 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4131 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4132 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004134 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4135 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004136
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004137 // concat/subtract/add constant numbers
4138 if (*op == '+')
4139 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4140 else if (*op == '-')
4141 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4142 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004143 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004144 // concatenate constant strings
4145 char_u *s1 = tv1->vval.v_string;
4146 char_u *s2 = tv2->vval.v_string;
4147 size_t len1 = STRLEN(s1);
4148
4149 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4150 if (tv1->vval.v_string == NULL)
4151 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004152 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004153 return FAIL;
4154 }
4155 mch_memmove(tv1->vval.v_string, s1, len1);
4156 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004157 vim_free(s1);
4158 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004159 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004160 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 }
4162 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004163 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004164 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004165 if (*op == '.')
4166 {
4167 if (may_generate_2STRING(-2, cctx) == FAIL
4168 || may_generate_2STRING(-1, cctx) == FAIL)
4169 return FAIL;
4170 generate_instr_drop(cctx, ISN_CONCAT, 1);
4171 }
4172 else
4173 generate_two_op(cctx, op);
4174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 }
4176
4177 return OK;
4178}
4179
4180/*
4181 * expr5a == expr5b
4182 * expr5a =~ expr5b
4183 * expr5a != expr5b
4184 * expr5a !~ expr5b
4185 * expr5a > expr5b
4186 * expr5a >= expr5b
4187 * expr5a < expr5b
4188 * expr5a <= expr5b
4189 * expr5a is expr5b
4190 * expr5a isnot expr5b
4191 *
4192 * Produces instructions:
4193 * EVAL expr5a Push result of "expr5a"
4194 * EVAL expr5b Push result of "expr5b"
4195 * COMPARE one of the compare instructions
4196 */
4197 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004198compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199{
4200 exptype_T type = EXPR_UNKNOWN;
4201 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004202 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004205 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206
4207 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004208 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 return FAIL;
4210
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004211 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004212 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213
4214 /*
4215 * If there is a comparative operator, use it.
4216 */
4217 if (type != EXPR_UNKNOWN)
4218 {
4219 int ic = FALSE; // Default: do not ignore case
4220
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004221 if (next != NULL)
4222 {
4223 *arg = next_line_from_context(cctx, TRUE);
4224 p = skipwhite(*arg);
4225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 if (type_is && (p[len] == '?' || p[len] == '#'))
4227 {
4228 semsg(_(e_invexpr2), *arg);
4229 return FAIL;
4230 }
4231 // extra question mark appended: ignore case
4232 if (p[len] == '?')
4233 {
4234 ic = TRUE;
4235 ++len;
4236 }
4237 // extra '#' appended: match case (ignored)
4238 else if (p[len] == '#')
4239 ++len;
4240 // nothing appended: match case
4241
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004242 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 {
4244 char_u buf[7];
4245
4246 vim_strncpy(buf, p, len);
4247 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004248 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 }
4250
4251 // get the second variable
4252 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004253 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004254 return FAIL;
4255
Bram Moolenaara5565e42020-05-09 15:44:01 +02004256 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 return FAIL;
4258
Bram Moolenaara5565e42020-05-09 15:44:01 +02004259 if (ppconst->pp_used == ppconst_used + 2)
4260 {
4261 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4262 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4263 int ret;
4264
4265 // Both sides are a constant, compute the result now.
4266 // First check for a valid combination of types, this is more
4267 // strict than typval_compare().
4268 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4269 ret = FAIL;
4270 else
4271 {
4272 ret = typval_compare(tv1, tv2, type, ic);
4273 tv1->v_type = VAR_BOOL;
4274 tv1->vval.v_number = tv1->vval.v_number
4275 ? VVAL_TRUE : VVAL_FALSE;
4276 clear_tv(tv2);
4277 --ppconst->pp_used;
4278 }
4279 return ret;
4280 }
4281
4282 generate_ppconst(cctx, ppconst);
4283 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 }
4285
4286 return OK;
4287}
4288
Bram Moolenaar7f141552020-05-09 17:35:53 +02004289static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291/*
4292 * Compile || or &&.
4293 */
4294 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004295compile_and_or(
4296 char_u **arg,
4297 cctx_T *cctx,
4298 char *op,
4299 ppconst_T *ppconst,
4300 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004302 char_u *next;
4303 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 int opchar = *op;
4305
4306 if (p[0] == opchar && p[1] == opchar)
4307 {
4308 garray_T *instr = &cctx->ctx_instr;
4309 garray_T end_ga;
4310
4311 /*
4312 * Repeat until there is no following "||" or "&&"
4313 */
4314 ga_init2(&end_ga, sizeof(int), 10);
4315 while (p[0] == opchar && p[1] == opchar)
4316 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004317 if (next != NULL)
4318 {
4319 *arg = next_line_from_context(cctx, TRUE);
4320 p = skipwhite(*arg);
4321 }
4322
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004323 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4324 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004326 return FAIL;
4327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328
Bram Moolenaara5565e42020-05-09 15:44:01 +02004329 // TODO: use ppconst if the value is a constant
4330 generate_ppconst(cctx, ppconst);
4331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 if (ga_grow(&end_ga, 1) == FAIL)
4333 {
4334 ga_clear(&end_ga);
4335 return FAIL;
4336 }
4337 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4338 ++end_ga.ga_len;
4339 generate_JUMP(cctx, opchar == '|'
4340 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4341
4342 // eval the next expression
4343 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004344 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004345 return FAIL;
4346
Bram Moolenaara5565e42020-05-09 15:44:01 +02004347 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4348 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 {
4350 ga_clear(&end_ga);
4351 return FAIL;
4352 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004353
4354 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004356 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357
4358 // Fill in the end label in all jumps.
4359 while (end_ga.ga_len > 0)
4360 {
4361 isn_T *isn;
4362
4363 --end_ga.ga_len;
4364 isn = ((isn_T *)instr->ga_data)
4365 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4366 isn->isn_arg.jump.jump_where = instr->ga_len;
4367 }
4368 ga_clear(&end_ga);
4369 }
4370
4371 return OK;
4372}
4373
4374/*
4375 * expr4a && expr4a && expr4a logical AND
4376 *
4377 * Produces instructions:
4378 * EVAL expr4a Push result of "expr4a"
4379 * JUMP_AND_KEEP_IF_FALSE end
4380 * EVAL expr4b Push result of "expr4b"
4381 * JUMP_AND_KEEP_IF_FALSE end
4382 * EVAL expr4c Push result of "expr4c"
4383 * end:
4384 */
4385 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004386compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004388 int ppconst_used = ppconst->pp_used;
4389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004391 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 return FAIL;
4393
4394 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004395 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396}
4397
4398/*
4399 * expr3a || expr3b || expr3c logical OR
4400 *
4401 * Produces instructions:
4402 * EVAL expr3a Push result of "expr3a"
4403 * JUMP_AND_KEEP_IF_TRUE end
4404 * EVAL expr3b Push result of "expr3b"
4405 * JUMP_AND_KEEP_IF_TRUE end
4406 * EVAL expr3c Push result of "expr3c"
4407 * end:
4408 */
4409 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004410compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004412 int ppconst_used = ppconst->pp_used;
4413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004415 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 return FAIL;
4417
4418 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004419 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420}
4421
4422/*
4423 * Toplevel expression: expr2 ? expr1a : expr1b
4424 *
4425 * Produces instructions:
4426 * EVAL expr2 Push result of "expr"
4427 * JUMP_IF_FALSE alt jump if false
4428 * EVAL expr1a
4429 * JUMP_ALWAYS end
4430 * alt: EVAL expr1b
4431 * end:
4432 */
4433 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004434compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435{
4436 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004437 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004438 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439
Bram Moolenaar61a89812020-05-07 16:58:17 +02004440 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004441 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 return FAIL;
4443
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004444 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 if (*p == '?')
4446 {
4447 garray_T *instr = &cctx->ctx_instr;
4448 garray_T *stack = &cctx->ctx_type_stack;
4449 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004450 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004452 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004454 int has_const_expr = FALSE;
4455 int const_value = FALSE;
4456 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004458 if (next != NULL)
4459 {
4460 *arg = next_line_from_context(cctx, TRUE);
4461 p = skipwhite(*arg);
4462 }
4463
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004464 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4465 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004467 return FAIL;
4468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469
Bram Moolenaara5565e42020-05-09 15:44:01 +02004470 if (ppconst->pp_used == ppconst_used + 1)
4471 {
4472 // the condition is a constant, we know whether the ? or the :
4473 // expression is to be evaluated.
4474 has_const_expr = TRUE;
4475 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4476 clear_tv(&ppconst->pp_tv[ppconst_used]);
4477 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004478 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4479 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004480 }
4481 else
4482 {
4483 generate_ppconst(cctx, ppconst);
4484 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4485 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486
4487 // evaluate the second expression; any type is accepted
4488 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004489 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004490 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004491 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004492 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493
Bram Moolenaara5565e42020-05-09 15:44:01 +02004494 if (!has_const_expr)
4495 {
4496 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497
Bram Moolenaara5565e42020-05-09 15:44:01 +02004498 // remember the type and drop it
4499 --stack->ga_len;
4500 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501
Bram Moolenaara5565e42020-05-09 15:44:01 +02004502 end_idx = instr->ga_len;
4503 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4504
4505 // jump here from JUMP_IF_FALSE
4506 isn = ((isn_T *)instr->ga_data) + alt_idx;
4507 isn->isn_arg.jump.jump_where = instr->ga_len;
4508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509
4510 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004511 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 if (*p != ':')
4513 {
4514 emsg(_(e_missing_colon));
4515 return FAIL;
4516 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004517 if (next != NULL)
4518 {
4519 *arg = next_line_from_context(cctx, TRUE);
4520 p = skipwhite(*arg);
4521 }
4522
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004523 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4524 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004526 return FAIL;
4527 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528
4529 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004530 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004531 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4532 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 *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 // If the types differ, the result has a more generic type.
4544 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4545 common_type(type1, type2, &type2, cctx->ctx_type_list);
4546
4547 // jump here from JUMP_ALWAYS
4548 isn = ((isn_T *)instr->ga_data) + end_idx;
4549 isn->isn_arg.jump.jump_where = instr->ga_len;
4550 }
4551
4552 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 }
4554 return OK;
4555}
4556
4557/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004558 * Toplevel expression.
4559 */
4560 static int
4561compile_expr0(char_u **arg, cctx_T *cctx)
4562{
4563 ppconst_T ppconst;
4564
4565 CLEAR_FIELD(ppconst);
4566 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4567 {
4568 clear_ppconst(&ppconst);
4569 return FAIL;
4570 }
4571 if (generate_ppconst(cctx, &ppconst) == FAIL)
4572 return FAIL;
4573 return OK;
4574}
4575
4576/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 * compile "return [expr]"
4578 */
4579 static char_u *
4580compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4581{
4582 char_u *p = arg;
4583 garray_T *stack = &cctx->ctx_type_stack;
4584 type_T *stack_type;
4585
4586 if (*p != NUL && *p != '|' && *p != '\n')
4587 {
4588 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004589 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 return NULL;
4591
4592 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4593 if (set_return_type)
4594 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004595 else
4596 {
4597 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4598 && stack_type->tt_type != VAR_VOID
4599 && stack_type->tt_type != VAR_UNKNOWN)
4600 {
4601 emsg(_("E1096: Returning a value in a function without a return type"));
4602 return NULL;
4603 }
4604 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 == FAIL)
4606 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 }
4609 else
4610 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004611 // "set_return_type" cannot be TRUE, only used for a lambda which
4612 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004613 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4614 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615 {
4616 emsg(_("E1003: Missing return value"));
4617 return NULL;
4618 }
4619
4620 // No argument, return zero.
4621 generate_PUSHNR(cctx, 0);
4622 }
4623
4624 if (generate_instr(cctx, ISN_RETURN) == NULL)
4625 return NULL;
4626
4627 // "return val | endif" is possible
4628 return skipwhite(p);
4629}
4630
4631/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004632 * Get a line from the compilation context, compatible with exarg_T getline().
4633 * Return a pointer to the line in allocated memory.
4634 * Return NULL for end-of-file or some error.
4635 */
4636 static char_u *
4637exarg_getline(
4638 int c UNUSED,
4639 void *cookie,
4640 int indent UNUSED,
4641 int do_concat UNUSED)
4642{
4643 cctx_T *cctx = (cctx_T *)cookie;
4644
4645 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4646 {
4647 iemsg("Heredoc got to end");
4648 return NULL;
4649 }
4650 ++cctx->ctx_lnum;
4651 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4652 [cctx->ctx_lnum]);
4653}
4654
4655/*
4656 * Compile a nested :def command.
4657 */
4658 static char_u *
4659compile_nested_function(exarg_T *eap, cctx_T *cctx)
4660{
4661 char_u *name_start = eap->arg;
4662 char_u *name_end = to_name_end(eap->arg, FALSE);
4663 char_u *name = get_lambda_name();
4664 lvar_T *lvar;
4665 ufunc_T *ufunc;
4666
4667 eap->arg = name_end;
4668 eap->getline = exarg_getline;
4669 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004670 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004671 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004672 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004673
Bram Moolenaar822ba242020-05-24 23:00:18 +02004674 if (ufunc == NULL)
4675 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004676 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004677 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004678 return NULL;
4679
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004680 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004681 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004682 TRUE, ufunc->uf_func_type);
4683
4684 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4685 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4686 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004687
Bram Moolenaar61a89812020-05-07 16:58:17 +02004688 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004689 return (char_u *)"";
4690}
4691
4692/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 * Return the length of an assignment operator, or zero if there isn't one.
4694 */
4695 int
4696assignment_len(char_u *p, int *heredoc)
4697{
4698 if (*p == '=')
4699 {
4700 if (p[1] == '<' && p[2] == '<')
4701 {
4702 *heredoc = TRUE;
4703 return 3;
4704 }
4705 return 1;
4706 }
4707 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4708 return 2;
4709 if (STRNCMP(p, "..=", 3) == 0)
4710 return 3;
4711 return 0;
4712}
4713
4714// words that cannot be used as a variable
4715static char *reserved[] = {
4716 "true",
4717 "false",
4718 NULL
4719};
4720
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004721typedef enum {
4722 dest_local,
4723 dest_option,
4724 dest_env,
4725 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004726 dest_buffer,
4727 dest_window,
4728 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004729 dest_vimvar,
4730 dest_script,
4731 dest_reg,
4732} assign_dest_T;
4733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004735 * Generate the load instruction for "name".
4736 */
4737 static void
4738generate_loadvar(
4739 cctx_T *cctx,
4740 assign_dest_T dest,
4741 char_u *name,
4742 lvar_T *lvar,
4743 type_T *type)
4744{
4745 switch (dest)
4746 {
4747 case dest_option:
4748 // TODO: check the option exists
4749 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4750 break;
4751 case dest_global:
4752 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4753 break;
4754 case dest_buffer:
4755 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4756 break;
4757 case dest_window:
4758 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4759 break;
4760 case dest_tab:
4761 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4762 break;
4763 case dest_script:
4764 compile_load_scriptvar(cctx,
4765 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4766 break;
4767 case dest_env:
4768 // Include $ in the name here
4769 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4770 break;
4771 case dest_reg:
4772 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4773 break;
4774 case dest_vimvar:
4775 generate_LOADV(cctx, name + 2, TRUE);
4776 break;
4777 case dest_local:
4778 if (lvar->lv_from_outer)
4779 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4780 NULL, type);
4781 else
4782 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4783 break;
4784 }
4785}
4786
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004787 void
4788vim9_declare_error(char_u *name)
4789{
4790 char *scope = "";
4791
4792 switch (*name)
4793 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004794 case 'g': scope = _("global"); break;
4795 case 'b': scope = _("buffer"); break;
4796 case 'w': scope = _("window"); break;
4797 case 't': scope = _("tab"); break;
4798 case 'v': scope = "v:"; break;
4799 case '$': semsg(_(e_declare_env_var), name); return;
4800 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004801 }
4802 semsg(_(e_declare_var), scope, name);
4803}
4804
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004805/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004806 * Compile declaration and assignment:
4807 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004809 * Return NULL for an error.
4810 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 */
4812 static char_u *
4813compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4814{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004815 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004817 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004818 char_u *ret = NULL;
4819 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004820 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004823 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 int oplen = 0;
4826 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004827 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004828 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004829 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004833 // Skip over the "var" or "[var, var]" to get to any "=".
4834 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4835 if (p == NULL)
4836 return *arg == '[' ? arg : NULL;
4837
4838 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004840 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841 return NULL;
4842 }
4843
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 sp = p;
4845 p = skipwhite(p);
4846 op = p;
4847 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004848
4849 if (var_count > 0 && oplen == 0)
4850 // can be something like "[1, 2]->func()"
4851 return arg;
4852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4854 {
4855 char_u buf[4];
4856
4857 vim_strncpy(buf, op, oplen);
4858 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004859 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004860 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 if (heredoc)
4863 {
4864 list_T *l;
4865 listitem_T *li;
4866
4867 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004868 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004870 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871
4872 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004873 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 {
4875 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4876 li->li_tv.vval.v_string = NULL;
4877 }
4878 generate_NEWLIST(cctx, l->lv_len);
4879 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004880 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 list_free(l);
4882 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004883 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004885 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004887 // for "[var, var] = expr" evaluate the expression here, loop over the
4888 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004889
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004890 p = skipwhite(op + oplen);
4891 if (compile_expr0(&p, cctx) == FAIL)
4892 return NULL;
4893 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004895 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004897 type_T *stacktype;
4898
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004899 stacktype = stack->ga_len == 0 ? &t_void
4900 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004901 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004903 emsg(_(e_cannot_use_void));
4904 goto theend;
4905 }
4906 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4907 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004908 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4909 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004910 }
4911 }
4912
4913 /*
4914 * Loop over variables in "[var, var] = expr".
4915 * For "var = expr" and "let var: type" this is done only once.
4916 */
4917 if (var_count > 0)
4918 var_start = skipwhite(arg + 1); // skip over the "["
4919 else
4920 var_start = arg;
4921 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4922 {
4923 char_u *var_end = skip_var_one(var_start, FALSE);
4924 size_t varlen;
4925 int new_local = FALSE;
4926 int opt_type;
4927 int opt_flags = 0;
4928 assign_dest_T dest = dest_local;
4929 int vimvaridx = -1;
4930 lvar_T *lvar = NULL;
4931 lvar_T arg_lvar;
4932 int has_type = FALSE;
4933 int has_index = FALSE;
4934 int instr_count = -1;
4935
4936 p = (*var_start == '&' || *var_start == '$'
4937 || *var_start == '@') ? var_start + 1 : var_start;
4938 p = to_name_end(p, TRUE);
4939
4940 // "a: type" is declaring variable "a" with a type, not "a:".
4941 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4942 --var_end;
4943 if (is_decl && p == var_start + 2 && p[-1] == ':')
4944 --p;
4945
4946 varlen = p - var_start;
4947 vim_free(name);
4948 name = vim_strnsave(var_start, varlen);
4949 if (name == NULL)
4950 return NULL;
4951 if (!heredoc)
4952 type = &t_any;
4953
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004954 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004955 {
4956 if (*var_start == '&')
4957 {
4958 int cc;
4959 long numval;
4960
4961 dest = dest_option;
4962 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004964 emsg(_(e_const_option));
4965 goto theend;
4966 }
4967 if (is_decl)
4968 {
4969 semsg(_("E1052: Cannot declare an option: %s"), var_start);
4970 goto theend;
4971 }
4972 p = var_start;
4973 p = find_option_end(&p, &opt_flags);
4974 if (p == NULL)
4975 {
4976 // cannot happen?
4977 emsg(_(e_letunexp));
4978 goto theend;
4979 }
4980 cc = *p;
4981 *p = NUL;
4982 opt_type = get_option_value(var_start + 1, &numval,
4983 NULL, opt_flags);
4984 *p = cc;
4985 if (opt_type == -3)
4986 {
4987 semsg(_(e_unknown_option), var_start);
4988 goto theend;
4989 }
4990 if (opt_type == -2 || opt_type == 0)
4991 type = &t_string;
4992 else
4993 type = &t_number; // both number and boolean option
4994 }
4995 else if (*var_start == '$')
4996 {
4997 dest = dest_env;
4998 type = &t_string;
4999 if (is_decl)
5000 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005001 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005002 goto theend;
5003 }
5004 }
5005 else if (*var_start == '@')
5006 {
5007 if (!valid_yank_reg(var_start[1], TRUE))
5008 {
5009 emsg_invreg(var_start[1]);
5010 goto theend;
5011 }
5012 dest = dest_reg;
5013 type = &t_string;
5014 if (is_decl)
5015 {
5016 semsg(_("E1066: Cannot declare a register: %s"), name);
5017 goto theend;
5018 }
5019 }
5020 else if (STRNCMP(var_start, "g:", 2) == 0)
5021 {
5022 dest = dest_global;
5023 if (is_decl)
5024 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005025 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005026 goto theend;
5027 }
5028 }
5029 else if (STRNCMP(var_start, "b:", 2) == 0)
5030 {
5031 dest = dest_buffer;
5032 if (is_decl)
5033 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005034 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005035 goto theend;
5036 }
5037 }
5038 else if (STRNCMP(var_start, "w:", 2) == 0)
5039 {
5040 dest = dest_window;
5041 if (is_decl)
5042 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005043 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005044 goto theend;
5045 }
5046 }
5047 else if (STRNCMP(var_start, "t:", 2) == 0)
5048 {
5049 dest = dest_tab;
5050 if (is_decl)
5051 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005052 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005053 goto theend;
5054 }
5055 }
5056 else if (STRNCMP(var_start, "v:", 2) == 0)
5057 {
5058 typval_T *vtv;
5059 int di_flags;
5060
5061 vimvaridx = find_vim_var(name + 2, &di_flags);
5062 if (vimvaridx < 0)
5063 {
5064 semsg(_(e_var_notfound), var_start);
5065 goto theend;
5066 }
5067 // We use the current value of "sandbox" here, is that OK?
5068 if (var_check_ro(di_flags, name, FALSE))
5069 goto theend;
5070 dest = dest_vimvar;
5071 vtv = get_vim_var_tv(vimvaridx);
5072 type = typval2type(vtv);
5073 if (is_decl)
5074 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005075 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005076 goto theend;
5077 }
5078 }
5079 else
5080 {
5081 int idx;
5082
5083 for (idx = 0; reserved[idx] != NULL; ++idx)
5084 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005085 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005086 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005087 goto theend;
5088 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089
5090 lvar = lookup_local(var_start, varlen, cctx);
5091 if (lvar == NULL)
5092 {
5093 CLEAR_FIELD(arg_lvar);
5094 if (lookup_arg(var_start, varlen,
5095 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5096 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005097 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005098 if (is_decl)
5099 {
5100 semsg(_(e_used_as_arg), name);
5101 goto theend;
5102 }
5103 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005104 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005105 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005106 if (lvar != NULL)
5107 {
5108 if (is_decl)
5109 {
5110 semsg(_("E1017: Variable already declared: %s"), name);
5111 goto theend;
5112 }
5113 else if (lvar->lv_const)
5114 {
5115 semsg(_("E1018: Cannot assign to a constant: %s"),
5116 name);
5117 goto theend;
5118 }
5119 }
5120 else if (STRNCMP(var_start, "s:", 2) == 0
5121 || lookup_script(var_start, varlen) == OK
5122 || find_imported(var_start, varlen, cctx) != NULL)
5123 {
5124 dest = dest_script;
5125 if (is_decl)
5126 {
5127 semsg(_("E1054: Variable already declared in the script: %s"),
5128 name);
5129 goto theend;
5130 }
5131 }
5132 else if (name[1] == ':' && name[2] != NUL)
5133 {
5134 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5135 name);
5136 goto theend;
5137 }
5138 else if (!is_decl)
5139 {
5140 semsg(_("E1089: unknown variable: %s"), name);
5141 goto theend;
5142 }
5143 }
5144 }
5145
5146 // handle "a:name" as a name, not index "name" on "a"
5147 if (varlen > 1 || var_start[varlen] != ':')
5148 p = var_end;
5149
5150 if (dest != dest_option)
5151 {
5152 if (is_decl && *p == ':')
5153 {
5154 // parse optional type: "let var: type = expr"
5155 if (!VIM_ISWHITE(p[1]))
5156 {
5157 semsg(_(e_white_after), ":");
5158 goto theend;
5159 }
5160 p = skipwhite(p + 1);
5161 type = parse_type(&p, cctx->ctx_type_list);
5162 has_type = TRUE;
5163 }
5164 else if (lvar != NULL)
5165 type = lvar->lv_type;
5166 }
5167
5168 if (oplen == 3 && !heredoc && dest != dest_global
5169 && type->tt_type != VAR_STRING
5170 && type->tt_type != VAR_ANY)
5171 {
5172 emsg(_("E1019: Can only concatenate to string"));
5173 goto theend;
5174 }
5175
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005176 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005177 {
5178 if (oplen > 1 && !heredoc)
5179 {
5180 // +=, /=, etc. require an existing variable
5181 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5182 name);
5183 goto theend;
5184 }
5185
5186 // new local variable
5187 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5188 goto theend;
5189 lvar = reserve_local(cctx, var_start, varlen,
5190 cmdidx == CMD_const, type);
5191 if (lvar == NULL)
5192 goto theend;
5193 new_local = TRUE;
5194 }
5195
5196 member_type = type;
5197 if (var_end > var_start + varlen)
5198 {
5199 // Something follows after the variable: "var[idx]".
5200 if (is_decl)
5201 {
5202 emsg(_("E1087: cannot use an index when declaring a variable"));
5203 goto theend;
5204 }
5205
5206 if (var_start[varlen] == '[')
5207 {
5208 has_index = TRUE;
5209 if (type->tt_member == NULL)
5210 {
5211 semsg(_("E1088: cannot use an index on %s"), name);
5212 goto theend;
5213 }
5214 member_type = type->tt_member;
5215 }
5216 else
5217 {
5218 semsg("Not supported yet: %s", var_start);
5219 goto theend;
5220 }
5221 }
5222 else if (lvar == &arg_lvar)
5223 {
5224 semsg(_("E1090: Cannot assign to argument %s"), name);
5225 goto theend;
5226 }
5227
5228 if (!heredoc)
5229 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005230 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005231 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005232 if (oplen > 0 && var_count == 0)
5233 {
5234 // skip over the "=" and the expression
5235 p = skipwhite(op + oplen);
5236 compile_expr0(&p, cctx);
5237 }
5238 }
5239 else if (oplen > 0)
5240 {
5241 type_T *stacktype;
5242
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005243 // For "var = expr" evaluate the expression.
5244 if (var_count == 0)
5245 {
5246 int r;
5247
5248 // for "+=", "*=", "..=" etc. first load the current value
5249 if (*op != '=')
5250 {
5251 generate_loadvar(cctx, dest, name, lvar, type);
5252
5253 if (has_index)
5254 {
5255 // TODO: get member from list or dict
5256 emsg("Index with operation not supported yet");
5257 goto theend;
5258 }
5259 }
5260
5261 // Compile the expression. Temporarily hide the new local
5262 // variable here, it is not available to this expression.
5263 if (new_local)
5264 --cctx->ctx_locals.ga_len;
5265 instr_count = instr->ga_len;
5266 p = skipwhite(op + oplen);
5267 r = compile_expr0(&p, cctx);
5268 if (new_local)
5269 ++cctx->ctx_locals.ga_len;
5270 if (r == FAIL)
5271 goto theend;
5272 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005273 else if (semicolon && var_idx == var_count - 1)
5274 {
5275 // For "[var; var] = expr" get the rest of the list
5276 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5277 goto theend;
5278 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005279 else
5280 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005281 // For "[var, var] = expr" get the "var_idx" item from the
5282 // list.
5283 if (generate_GETITEM(cctx, var_idx) == FAIL)
5284 return FAIL;
5285 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005286
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005287 stacktype = stack->ga_len == 0 ? &t_void
5288 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5289 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005290 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005291 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005292 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005293 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005294 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005295 emsg(_(e_cannot_use_void));
5296 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005297 }
5298 else
5299 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005300 // An empty list or dict has a &t_void member,
5301 // for a variable that implies &t_any.
5302 if (stacktype == &t_list_empty)
5303 lvar->lv_type = &t_list_any;
5304 else if (stacktype == &t_dict_empty)
5305 lvar->lv_type = &t_dict_any;
5306 else
5307 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005308 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005309 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005310 else
5311 {
5312 type_T *use_type = lvar->lv_type;
5313
5314 if (has_index)
5315 {
5316 use_type = use_type->tt_member;
5317 if (use_type == NULL)
5318 use_type = &t_void;
5319 }
5320 if (need_type(stacktype, use_type, -1, cctx)
5321 == FAIL)
5322 goto theend;
5323 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005324 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005325 else if (*p != '=' && need_type(stacktype, member_type, -1,
5326 cctx) == FAIL)
5327 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005329 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005330 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005331 emsg(_(e_const_req_value));
5332 goto theend;
5333 }
5334 else if (!has_type || dest == dest_option)
5335 {
5336 emsg(_(e_type_req));
5337 goto theend;
5338 }
5339 else
5340 {
5341 // variables are always initialized
5342 if (ga_grow(instr, 1) == FAIL)
5343 goto theend;
5344 switch (member_type->tt_type)
5345 {
5346 case VAR_BOOL:
5347 generate_PUSHBOOL(cctx, VVAL_FALSE);
5348 break;
5349 case VAR_FLOAT:
5350#ifdef FEAT_FLOAT
5351 generate_PUSHF(cctx, 0.0);
5352#endif
5353 break;
5354 case VAR_STRING:
5355 generate_PUSHS(cctx, NULL);
5356 break;
5357 case VAR_BLOB:
5358 generate_PUSHBLOB(cctx, NULL);
5359 break;
5360 case VAR_FUNC:
5361 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5362 break;
5363 case VAR_LIST:
5364 generate_NEWLIST(cctx, 0);
5365 break;
5366 case VAR_DICT:
5367 generate_NEWDICT(cctx, 0);
5368 break;
5369 case VAR_JOB:
5370 generate_PUSHJOB(cctx, NULL);
5371 break;
5372 case VAR_CHANNEL:
5373 generate_PUSHCHANNEL(cctx, NULL);
5374 break;
5375 case VAR_NUMBER:
5376 case VAR_UNKNOWN:
5377 case VAR_ANY:
5378 case VAR_PARTIAL:
5379 case VAR_VOID:
5380 case VAR_SPECIAL: // cannot happen
5381 generate_PUSHNR(cctx, 0);
5382 break;
5383 }
5384 }
5385 if (var_count == 0)
5386 end = p;
5387 }
5388
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005389 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005390 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005391 break;
5392
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005393 if (oplen > 0 && *op != '=')
5394 {
5395 type_T *expected = &t_number;
5396 type_T *stacktype;
5397
5398 // TODO: if type is known use float or any operation
5399
5400 if (*op == '.')
5401 expected = &t_string;
5402 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5403 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5404 goto theend;
5405
5406 if (*op == '.')
5407 generate_instr_drop(cctx, ISN_CONCAT, 1);
5408 else
5409 {
5410 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5411
5412 if (isn == NULL)
5413 goto theend;
5414 switch (*op)
5415 {
5416 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5417 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5418 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5419 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5420 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422 }
5423 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005425 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005426 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005427 int r;
5428
5429 // Compile the "idx" in "var[idx]".
5430 if (new_local)
5431 --cctx->ctx_locals.ga_len;
5432 p = skipwhite(var_start + varlen + 1);
5433 r = compile_expr0(&p, cctx);
5434 if (new_local)
5435 ++cctx->ctx_locals.ga_len;
5436 if (r == FAIL)
5437 goto theend;
5438 if (*skipwhite(p) != ']')
5439 {
5440 emsg(_(e_missbrac));
5441 goto theend;
5442 }
5443 if (type->tt_type == VAR_DICT
5444 && may_generate_2STRING(-1, cctx) == FAIL)
5445 goto theend;
5446 if (type->tt_type == VAR_LIST
5447 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005448 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005449 {
5450 emsg(_(e_number_exp));
5451 goto theend;
5452 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005453
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005454 // Load the dict or list. On the stack we then have:
5455 // - value
5456 // - index
5457 // - variable
5458 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005459
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005460 if (type->tt_type == VAR_LIST)
5461 {
5462 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5463 return FAIL;
5464 }
5465 else if (type->tt_type == VAR_DICT)
5466 {
5467 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5468 return FAIL;
5469 }
5470 else
5471 {
5472 emsg(_(e_listreq));
5473 goto theend;
5474 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005475 }
5476 else
5477 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005478 switch (dest)
5479 {
5480 case dest_option:
5481 generate_STOREOPT(cctx, name + 1, opt_flags);
5482 break;
5483 case dest_global:
5484 // include g: with the name, easier to execute that way
5485 generate_STORE(cctx, ISN_STOREG, 0, name);
5486 break;
5487 case dest_buffer:
5488 // include b: with the name, easier to execute that way
5489 generate_STORE(cctx, ISN_STOREB, 0, name);
5490 break;
5491 case dest_window:
5492 // include w: with the name, easier to execute that way
5493 generate_STORE(cctx, ISN_STOREW, 0, name);
5494 break;
5495 case dest_tab:
5496 // include t: with the name, easier to execute that way
5497 generate_STORE(cctx, ISN_STORET, 0, name);
5498 break;
5499 case dest_env:
5500 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5501 break;
5502 case dest_reg:
5503 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5504 break;
5505 case dest_vimvar:
5506 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5507 break;
5508 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005509 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005510 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5511 imported_T *import = NULL;
5512 int sid = current_sctx.sc_sid;
5513 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005514
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005515 if (name[1] != ':')
5516 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005517 import = find_imported(name, 0, cctx);
5518 if (import != NULL)
5519 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005520 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005521
5522 idx = get_script_item_idx(sid, rawname, TRUE);
5523 // TODO: specific type
5524 if (idx < 0)
5525 {
5526 char_u *name_s = name;
5527
5528 // Include s: in the name for store_var()
5529 if (name[1] != ':')
5530 {
5531 int len = (int)STRLEN(name) + 3;
5532
5533 name_s = alloc(len);
5534 if (name_s == NULL)
5535 name_s = name;
5536 else
5537 vim_snprintf((char *)name_s, len,
5538 "s:%s", name);
5539 }
5540 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005541 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005542 if (name_s != name)
5543 vim_free(name_s);
5544 }
5545 else
5546 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005547 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005548 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005549 break;
5550 case dest_local:
5551 if (lvar != NULL)
5552 {
5553 isn_T *isn = ((isn_T *)instr->ga_data)
5554 + instr->ga_len - 1;
5555
5556 // optimization: turn "var = 123" from ISN_PUSHNR +
5557 // ISN_STORE into ISN_STORENR
5558 if (!lvar->lv_from_outer
5559 && instr->ga_len == instr_count + 1
5560 && isn->isn_type == ISN_PUSHNR)
5561 {
5562 varnumber_T val = isn->isn_arg.number;
5563
5564 isn->isn_type = ISN_STORENR;
5565 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5566 isn->isn_arg.storenr.stnr_val = val;
5567 if (stack->ga_len > 0)
5568 --stack->ga_len;
5569 }
5570 else if (lvar->lv_from_outer)
5571 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005572 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005573 else
5574 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5575 }
5576 break;
5577 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005578 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005579
5580 if (var_idx + 1 < var_count)
5581 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005582 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005583
5584 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005585 if (var_count > 0 && !semicolon)
5586 {
5587 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5588 goto theend;
5589 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005590
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005591 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592
5593theend:
5594 vim_free(name);
5595 return ret;
5596}
5597
5598/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005599 * Check if "name" can be "unlet".
5600 */
5601 int
5602check_vim9_unlet(char_u *name)
5603{
5604 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5605 {
5606 semsg(_("E1081: Cannot unlet %s"), name);
5607 return FAIL;
5608 }
5609 return OK;
5610}
5611
5612/*
5613 * Callback passed to ex_unletlock().
5614 */
5615 static int
5616compile_unlet(
5617 lval_T *lvp,
5618 char_u *name_end,
5619 exarg_T *eap,
5620 int deep UNUSED,
5621 void *coookie)
5622{
5623 cctx_T *cctx = coookie;
5624
5625 if (lvp->ll_tv == NULL)
5626 {
5627 char_u *p = lvp->ll_name;
5628 int cc = *name_end;
5629 int ret = OK;
5630
5631 // Normal name. Only supports g:, w:, t: and b: namespaces.
5632 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005633 if (*p == '$')
5634 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5635 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005636 ret = FAIL;
5637 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005638 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005639
5640 *name_end = cc;
5641 return ret;
5642 }
5643
5644 // TODO: unlet {list}[idx]
5645 // TODO: unlet {dict}[key]
5646 emsg("Sorry, :unlet not fully implemented yet");
5647 return FAIL;
5648}
5649
5650/*
5651 * compile "unlet var", "lock var" and "unlock var"
5652 * "arg" points to "var".
5653 */
5654 static char_u *
5655compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5656{
5657 char_u *p = arg;
5658
5659 if (eap->cmdidx != CMD_unlet)
5660 {
5661 emsg("Sorry, :lock and unlock not implemented yet");
5662 return NULL;
5663 }
5664
5665 if (*p == '!')
5666 {
5667 p = skipwhite(p + 1);
5668 eap->forceit = TRUE;
5669 }
5670
5671 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5672 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5673}
5674
5675/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005676 * Compile an :import command.
5677 */
5678 static char_u *
5679compile_import(char_u *arg, cctx_T *cctx)
5680{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005681 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005682}
5683
5684/*
5685 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5686 */
5687 static int
5688compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5689{
5690 garray_T *instr = &cctx->ctx_instr;
5691 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5692
5693 if (endlabel == NULL)
5694 return FAIL;
5695 endlabel->el_next = *el;
5696 *el = endlabel;
5697 endlabel->el_end_label = instr->ga_len;
5698
5699 generate_JUMP(cctx, when, 0);
5700 return OK;
5701}
5702
5703 static void
5704compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5705{
5706 garray_T *instr = &cctx->ctx_instr;
5707
5708 while (*el != NULL)
5709 {
5710 endlabel_T *cur = (*el);
5711 isn_T *isn;
5712
5713 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5714 isn->isn_arg.jump.jump_where = instr->ga_len;
5715 *el = cur->el_next;
5716 vim_free(cur);
5717 }
5718}
5719
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005720 static void
5721compile_free_jump_to_end(endlabel_T **el)
5722{
5723 while (*el != NULL)
5724 {
5725 endlabel_T *cur = (*el);
5726
5727 *el = cur->el_next;
5728 vim_free(cur);
5729 }
5730}
5731
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005732/*
5733 * Create a new scope and set up the generic items.
5734 */
5735 static scope_T *
5736new_scope(cctx_T *cctx, scopetype_T type)
5737{
5738 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5739
5740 if (scope == NULL)
5741 return NULL;
5742 scope->se_outer = cctx->ctx_scope;
5743 cctx->ctx_scope = scope;
5744 scope->se_type = type;
5745 scope->se_local_count = cctx->ctx_locals.ga_len;
5746 return scope;
5747}
5748
5749/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005750 * Free the current scope and go back to the outer scope.
5751 */
5752 static void
5753drop_scope(cctx_T *cctx)
5754{
5755 scope_T *scope = cctx->ctx_scope;
5756
5757 if (scope == NULL)
5758 {
5759 iemsg("calling drop_scope() without a scope");
5760 return;
5761 }
5762 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005763 switch (scope->se_type)
5764 {
5765 case IF_SCOPE:
5766 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5767 case FOR_SCOPE:
5768 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5769 case WHILE_SCOPE:
5770 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5771 case TRY_SCOPE:
5772 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5773 case NO_SCOPE:
5774 case BLOCK_SCOPE:
5775 break;
5776 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005777 vim_free(scope);
5778}
5779
5780/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005781 * compile "if expr"
5782 *
5783 * "if expr" Produces instructions:
5784 * EVAL expr Push result of "expr"
5785 * JUMP_IF_FALSE end
5786 * ... body ...
5787 * end:
5788 *
5789 * "if expr | else" Produces instructions:
5790 * EVAL expr Push result of "expr"
5791 * JUMP_IF_FALSE else
5792 * ... body ...
5793 * JUMP_ALWAYS end
5794 * else:
5795 * ... body ...
5796 * end:
5797 *
5798 * "if expr1 | elseif expr2 | else" Produces instructions:
5799 * EVAL expr Push result of "expr"
5800 * JUMP_IF_FALSE elseif
5801 * ... body ...
5802 * JUMP_ALWAYS end
5803 * elseif:
5804 * EVAL expr Push result of "expr"
5805 * JUMP_IF_FALSE else
5806 * ... body ...
5807 * JUMP_ALWAYS end
5808 * else:
5809 * ... body ...
5810 * end:
5811 */
5812 static char_u *
5813compile_if(char_u *arg, cctx_T *cctx)
5814{
5815 char_u *p = arg;
5816 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005817 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005818 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005819 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005820 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005821
Bram Moolenaara5565e42020-05-09 15:44:01 +02005822 CLEAR_FIELD(ppconst);
5823 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005824 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005825 clear_ppconst(&ppconst);
5826 return NULL;
5827 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005828 if (cctx->ctx_skip == SKIP_YES)
5829 clear_ppconst(&ppconst);
5830 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005831 {
5832 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005833 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005834 clear_ppconst(&ppconst);
5835 }
5836 else
5837 {
5838 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005839 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005840 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005841 return NULL;
5842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005843
5844 scope = new_scope(cctx, IF_SCOPE);
5845 if (scope == NULL)
5846 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005847 scope->se_skip_save = skip_save;
5848 // "is_had_return" will be reset if any block does not end in :return
5849 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005851 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005852 {
5853 // "where" is set when ":elseif", "else" or ":endif" is found
5854 scope->se_u.se_if.is_if_label = instr->ga_len;
5855 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5856 }
5857 else
5858 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005859
5860 return p;
5861}
5862
5863 static char_u *
5864compile_elseif(char_u *arg, cctx_T *cctx)
5865{
5866 char_u *p = arg;
5867 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005868 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869 isn_T *isn;
5870 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005871 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005872
5873 if (scope == NULL || scope->se_type != IF_SCOPE)
5874 {
5875 emsg(_(e_elseif_without_if));
5876 return NULL;
5877 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005878 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005879 if (!cctx->ctx_had_return)
5880 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005881
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005882 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005883 {
5884 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005885 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005886 return NULL;
5887 // previous "if" or "elseif" jumps here
5888 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5889 isn->isn_arg.jump.jump_where = instr->ga_len;
5890 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005891
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005892 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005893 CLEAR_FIELD(ppconst);
5894 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005895 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005896 clear_ppconst(&ppconst);
5897 return NULL;
5898 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005899 if (scope->se_skip_save == SKIP_YES)
5900 clear_ppconst(&ppconst);
5901 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005902 {
5903 // The expression results in a constant.
5904 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005905 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005906 clear_ppconst(&ppconst);
5907 scope->se_u.se_if.is_if_label = -1;
5908 }
5909 else
5910 {
5911 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005912 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005913 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005914 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005916 // "where" is set when ":elseif", "else" or ":endif" is found
5917 scope->se_u.se_if.is_if_label = instr->ga_len;
5918 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005920
5921 return p;
5922}
5923
5924 static char_u *
5925compile_else(char_u *arg, cctx_T *cctx)
5926{
5927 char_u *p = arg;
5928 garray_T *instr = &cctx->ctx_instr;
5929 isn_T *isn;
5930 scope_T *scope = cctx->ctx_scope;
5931
5932 if (scope == NULL || scope->se_type != IF_SCOPE)
5933 {
5934 emsg(_(e_else_without_if));
5935 return NULL;
5936 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005937 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005938 if (!cctx->ctx_had_return)
5939 scope->se_u.se_if.is_had_return = FALSE;
5940 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941
Bram Moolenaarefd88552020-06-18 20:50:10 +02005942 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005943 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005944 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005945 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005946 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005947 if (!cctx->ctx_had_return
5948 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5949 JUMP_ALWAYS, cctx) == FAIL)
5950 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005951 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005952
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005953 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005954 {
5955 if (scope->se_u.se_if.is_if_label >= 0)
5956 {
5957 // previous "if" or "elseif" jumps here
5958 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5959 isn->isn_arg.jump.jump_where = instr->ga_len;
5960 scope->se_u.se_if.is_if_label = -1;
5961 }
5962 }
5963
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005964 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005965 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5966 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005967
5968 return p;
5969}
5970
5971 static char_u *
5972compile_endif(char_u *arg, cctx_T *cctx)
5973{
5974 scope_T *scope = cctx->ctx_scope;
5975 ifscope_T *ifscope;
5976 garray_T *instr = &cctx->ctx_instr;
5977 isn_T *isn;
5978
5979 if (scope == NULL || scope->se_type != IF_SCOPE)
5980 {
5981 emsg(_(e_endif_without_if));
5982 return NULL;
5983 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005984 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005985 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005986 if (!cctx->ctx_had_return)
5987 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005989 if (scope->se_u.se_if.is_if_label >= 0)
5990 {
5991 // previous "if" or "elseif" jumps here
5992 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5993 isn->isn_arg.jump.jump_where = instr->ga_len;
5994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005995 // Fill in the "end" label in jumps at the end of the blocks.
5996 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005997 cctx->ctx_skip = scope->se_skip_save;
5998
5999 // If all the blocks end in :return and there is an :else then the
6000 // had_return flag is set.
6001 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006003 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006004 return arg;
6005}
6006
6007/*
6008 * compile "for var in expr"
6009 *
6010 * Produces instructions:
6011 * PUSHNR -1
6012 * STORE loop-idx Set index to -1
6013 * EVAL expr Push result of "expr"
6014 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6015 * - if beyond end, jump to "end"
6016 * - otherwise get item from list and push it
6017 * STORE var Store item in "var"
6018 * ... body ...
6019 * JUMP top Jump back to repeat
6020 * end: DROP Drop the result of "expr"
6021 *
6022 */
6023 static char_u *
6024compile_for(char_u *arg, cctx_T *cctx)
6025{
6026 char_u *p;
6027 size_t varlen;
6028 garray_T *instr = &cctx->ctx_instr;
6029 garray_T *stack = &cctx->ctx_type_stack;
6030 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006031 lvar_T *loop_lvar; // loop iteration variable
6032 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033 type_T *vartype;
6034
6035 // TODO: list of variables: "for [key, value] in dict"
6036 // parse "var"
6037 for (p = arg; eval_isnamec1(*p); ++p)
6038 ;
6039 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006040 var_lvar = lookup_local(arg, varlen, cctx);
6041 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 {
6043 semsg(_("E1023: variable already defined: %s"), arg);
6044 return NULL;
6045 }
6046
6047 // consume "in"
6048 p = skipwhite(p);
6049 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6050 {
6051 emsg(_(e_missing_in));
6052 return NULL;
6053 }
6054 p = skipwhite(p + 2);
6055
6056
6057 scope = new_scope(cctx, FOR_SCOPE);
6058 if (scope == NULL)
6059 return NULL;
6060
6061 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006062 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6063 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006064 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006065 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006066 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006067 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006069
6070 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006071 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6072 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006073 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006074 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006075 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006078
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006079 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080
6081 // compile "expr", it remains on the stack until "endfor"
6082 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006083 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006084 {
6085 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006088
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006089 // Now that we know the type of "var", check that it is a list, now or at
6090 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006091 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006092 if (need_type(vartype, &t_list_any, -1, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006093 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006094 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006095 return NULL;
6096 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006097 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006098 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006099
6100 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006101 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006103 generate_FOR(cctx, loop_lvar->lv_idx);
6104 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006105
6106 return arg;
6107}
6108
6109/*
6110 * compile "endfor"
6111 */
6112 static char_u *
6113compile_endfor(char_u *arg, cctx_T *cctx)
6114{
6115 garray_T *instr = &cctx->ctx_instr;
6116 scope_T *scope = cctx->ctx_scope;
6117 forscope_T *forscope;
6118 isn_T *isn;
6119
6120 if (scope == NULL || scope->se_type != FOR_SCOPE)
6121 {
6122 emsg(_(e_for));
6123 return NULL;
6124 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006125 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006126 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006127 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006128
6129 // At end of ":for" scope jump back to the FOR instruction.
6130 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6131
6132 // Fill in the "end" label in the FOR statement so it can jump here
6133 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6134 isn->isn_arg.forloop.for_end = instr->ga_len;
6135
6136 // Fill in the "end" label any BREAK statements
6137 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6138
6139 // Below the ":for" scope drop the "expr" list from the stack.
6140 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6141 return NULL;
6142
6143 vim_free(scope);
6144
6145 return arg;
6146}
6147
6148/*
6149 * compile "while expr"
6150 *
6151 * Produces instructions:
6152 * top: EVAL expr Push result of "expr"
6153 * JUMP_IF_FALSE end jump if false
6154 * ... body ...
6155 * JUMP top Jump back to repeat
6156 * end:
6157 *
6158 */
6159 static char_u *
6160compile_while(char_u *arg, cctx_T *cctx)
6161{
6162 char_u *p = arg;
6163 garray_T *instr = &cctx->ctx_instr;
6164 scope_T *scope;
6165
6166 scope = new_scope(cctx, WHILE_SCOPE);
6167 if (scope == NULL)
6168 return NULL;
6169
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006170 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006171
6172 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006173 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174 return NULL;
6175
6176 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006177 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006178 JUMP_IF_FALSE, cctx) == FAIL)
6179 return FAIL;
6180
6181 return p;
6182}
6183
6184/*
6185 * compile "endwhile"
6186 */
6187 static char_u *
6188compile_endwhile(char_u *arg, cctx_T *cctx)
6189{
6190 scope_T *scope = cctx->ctx_scope;
6191
6192 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6193 {
6194 emsg(_(e_while));
6195 return NULL;
6196 }
6197 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006198 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006199
6200 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006201 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006202
6203 // Fill in the "end" label in the WHILE statement so it can jump here.
6204 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006205 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006206
6207 vim_free(scope);
6208
6209 return arg;
6210}
6211
6212/*
6213 * compile "continue"
6214 */
6215 static char_u *
6216compile_continue(char_u *arg, cctx_T *cctx)
6217{
6218 scope_T *scope = cctx->ctx_scope;
6219
6220 for (;;)
6221 {
6222 if (scope == NULL)
6223 {
6224 emsg(_(e_continue));
6225 return NULL;
6226 }
6227 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6228 break;
6229 scope = scope->se_outer;
6230 }
6231
6232 // Jump back to the FOR or WHILE instruction.
6233 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006234 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6235 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236 return arg;
6237}
6238
6239/*
6240 * compile "break"
6241 */
6242 static char_u *
6243compile_break(char_u *arg, cctx_T *cctx)
6244{
6245 scope_T *scope = cctx->ctx_scope;
6246 endlabel_T **el;
6247
6248 for (;;)
6249 {
6250 if (scope == NULL)
6251 {
6252 emsg(_(e_break));
6253 return NULL;
6254 }
6255 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6256 break;
6257 scope = scope->se_outer;
6258 }
6259
6260 // Jump to the end of the FOR or WHILE loop.
6261 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006262 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006264 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6266 return FAIL;
6267
6268 return arg;
6269}
6270
6271/*
6272 * compile "{" start of block
6273 */
6274 static char_u *
6275compile_block(char_u *arg, cctx_T *cctx)
6276{
6277 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6278 return NULL;
6279 return skipwhite(arg + 1);
6280}
6281
6282/*
6283 * compile end of block: drop one scope
6284 */
6285 static void
6286compile_endblock(cctx_T *cctx)
6287{
6288 scope_T *scope = cctx->ctx_scope;
6289
6290 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006291 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 vim_free(scope);
6293}
6294
6295/*
6296 * compile "try"
6297 * Creates a new scope for the try-endtry, pointing to the first catch and
6298 * finally.
6299 * Creates another scope for the "try" block itself.
6300 * TRY instruction sets up exception handling at runtime.
6301 *
6302 * "try"
6303 * TRY -> catch1, -> finally push trystack entry
6304 * ... try block
6305 * "throw {exception}"
6306 * EVAL {exception}
6307 * THROW create exception
6308 * ... try block
6309 * " catch {expr}"
6310 * JUMP -> finally
6311 * catch1: PUSH exeception
6312 * EVAL {expr}
6313 * MATCH
6314 * JUMP nomatch -> catch2
6315 * CATCH remove exception
6316 * ... catch block
6317 * " catch"
6318 * JUMP -> finally
6319 * catch2: CATCH remove exception
6320 * ... catch block
6321 * " finally"
6322 * finally:
6323 * ... finally block
6324 * " endtry"
6325 * ENDTRY pop trystack entry, may rethrow
6326 */
6327 static char_u *
6328compile_try(char_u *arg, cctx_T *cctx)
6329{
6330 garray_T *instr = &cctx->ctx_instr;
6331 scope_T *try_scope;
6332 scope_T *scope;
6333
6334 // scope that holds the jumps that go to catch/finally/endtry
6335 try_scope = new_scope(cctx, TRY_SCOPE);
6336 if (try_scope == NULL)
6337 return NULL;
6338
6339 // "catch" is set when the first ":catch" is found.
6340 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006341 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006342 if (generate_instr(cctx, ISN_TRY) == NULL)
6343 return NULL;
6344
6345 // scope for the try block itself
6346 scope = new_scope(cctx, BLOCK_SCOPE);
6347 if (scope == NULL)
6348 return NULL;
6349
6350 return arg;
6351}
6352
6353/*
6354 * compile "catch {expr}"
6355 */
6356 static char_u *
6357compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6358{
6359 scope_T *scope = cctx->ctx_scope;
6360 garray_T *instr = &cctx->ctx_instr;
6361 char_u *p;
6362 isn_T *isn;
6363
6364 // end block scope from :try or :catch
6365 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6366 compile_endblock(cctx);
6367 scope = cctx->ctx_scope;
6368
6369 // Error if not in a :try scope
6370 if (scope == NULL || scope->se_type != TRY_SCOPE)
6371 {
6372 emsg(_(e_catch));
6373 return NULL;
6374 }
6375
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006376 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006377 {
6378 emsg(_("E1033: catch unreachable after catch-all"));
6379 return NULL;
6380 }
6381
6382 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006383 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006384 JUMP_ALWAYS, cctx) == FAIL)
6385 return NULL;
6386
6387 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006388 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006389 if (isn->isn_arg.try.try_catch == 0)
6390 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006391 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392 {
6393 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006394 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006395 isn->isn_arg.jump.jump_where = instr->ga_len;
6396 }
6397
6398 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006399 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006401 scope->se_u.se_try.ts_caught_all = TRUE;
6402 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403 }
6404 else
6405 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006406 char_u *end;
6407 char_u *pat;
6408 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006409 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006410 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006412 // Push v:exception, push {expr} and MATCH
6413 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6414
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006415 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006416 if (*end != *p)
6417 {
6418 semsg(_("E1067: Separator mismatch: %s"), p);
6419 vim_free(tofree);
6420 return FAIL;
6421 }
6422 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006423 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006424 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006425 len = (int)(end - tofree);
6426 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006427 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006428 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006429 if (pat == NULL)
6430 return FAIL;
6431 if (generate_PUSHS(cctx, pat) == FAIL)
6432 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6435 return NULL;
6436
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006437 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006438 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6439 return NULL;
6440 }
6441
6442 if (generate_instr(cctx, ISN_CATCH) == NULL)
6443 return NULL;
6444
6445 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6446 return NULL;
6447 return p;
6448}
6449
6450 static char_u *
6451compile_finally(char_u *arg, cctx_T *cctx)
6452{
6453 scope_T *scope = cctx->ctx_scope;
6454 garray_T *instr = &cctx->ctx_instr;
6455 isn_T *isn;
6456
6457 // end block scope from :try or :catch
6458 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6459 compile_endblock(cctx);
6460 scope = cctx->ctx_scope;
6461
6462 // Error if not in a :try scope
6463 if (scope == NULL || scope->se_type != TRY_SCOPE)
6464 {
6465 emsg(_(e_finally));
6466 return NULL;
6467 }
6468
6469 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006470 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006471 if (isn->isn_arg.try.try_finally != 0)
6472 {
6473 emsg(_(e_finally_dup));
6474 return NULL;
6475 }
6476
6477 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006478 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006479
Bram Moolenaar585fea72020-04-02 22:33:21 +02006480 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006481 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482 {
6483 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006484 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006485 isn->isn_arg.jump.jump_where = instr->ga_len;
6486 }
6487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488 // TODO: set index in ts_finally_label jumps
6489
6490 return arg;
6491}
6492
6493 static char_u *
6494compile_endtry(char_u *arg, cctx_T *cctx)
6495{
6496 scope_T *scope = cctx->ctx_scope;
6497 garray_T *instr = &cctx->ctx_instr;
6498 isn_T *isn;
6499
6500 // end block scope from :catch or :finally
6501 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6502 compile_endblock(cctx);
6503 scope = cctx->ctx_scope;
6504
6505 // Error if not in a :try scope
6506 if (scope == NULL || scope->se_type != TRY_SCOPE)
6507 {
6508 if (scope == NULL)
6509 emsg(_(e_no_endtry));
6510 else if (scope->se_type == WHILE_SCOPE)
6511 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006512 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 emsg(_(e_endfor));
6514 else
6515 emsg(_(e_endif));
6516 return NULL;
6517 }
6518
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006519 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6521 {
6522 emsg(_("E1032: missing :catch or :finally"));
6523 return NULL;
6524 }
6525
6526 // Fill in the "end" label in jumps at the end of the blocks, if not done
6527 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006528 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006529
6530 // End :catch or :finally scope: set value in ISN_TRY instruction
6531 if (isn->isn_arg.try.try_finally == 0)
6532 isn->isn_arg.try.try_finally = instr->ga_len;
6533 compile_endblock(cctx);
6534
6535 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6536 return NULL;
6537 return arg;
6538}
6539
6540/*
6541 * compile "throw {expr}"
6542 */
6543 static char_u *
6544compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6545{
6546 char_u *p = skipwhite(arg);
6547
Bram Moolenaara5565e42020-05-09 15:44:01 +02006548 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006549 return NULL;
6550 if (may_generate_2STRING(-1, cctx) == FAIL)
6551 return NULL;
6552 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6553 return NULL;
6554
6555 return p;
6556}
6557
6558/*
6559 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006560 * compile "echomsg expr"
6561 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006562 * compile "execute expr"
6563 */
6564 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006565compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006566{
6567 char_u *p = arg;
6568 int count = 0;
6569
6570 for (;;)
6571 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006572 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006573 return NULL;
6574 ++count;
6575 p = skipwhite(p);
6576 if (ends_excmd(*p))
6577 break;
6578 }
6579
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006580 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6581 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6582 else if (cmdidx == CMD_execute)
6583 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6584 else if (cmdidx == CMD_echomsg)
6585 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6586 else
6587 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588 return p;
6589}
6590
6591/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006592 * A command that is not compiled, execute with legacy code.
6593 */
6594 static char_u *
6595compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6596{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006597 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006598 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006599 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006600
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006601 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006602 goto theend;
6603
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006604 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006605 {
6606 long argt = excmd_get_argt(eap->cmdidx);
6607 int usefilter = FALSE;
6608
6609 has_expr = argt & (EX_XFILE | EX_EXPAND);
6610
6611 // If the command can be followed by a bar, find the bar and truncate
6612 // it, so that the following command can be compiled.
6613 // The '|' is overwritten with a NUL, it is put back below.
6614 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6615 && *eap->arg == '!')
6616 // :w !filter or :r !filter or :r! filter
6617 usefilter = TRUE;
6618 if ((argt & EX_TRLBAR) && !usefilter)
6619 {
6620 separate_nextcmd(eap);
6621 if (eap->nextcmd != NULL)
6622 nextcmd = eap->nextcmd;
6623 }
6624 }
6625
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006626 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6627 {
6628 // expand filename in "syntax include [@group] filename"
6629 has_expr = TRUE;
6630 eap->arg = skipwhite(eap->arg + 7);
6631 if (*eap->arg == '@')
6632 eap->arg = skiptowhite(eap->arg);
6633 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006634
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006635 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006636 {
6637 int count = 0;
6638 char_u *start = skipwhite(line);
6639
6640 // :cmd xxx`=expr1`yyy`=expr2`zzz
6641 // PUSHS ":cmd xxx"
6642 // eval expr1
6643 // PUSHS "yyy"
6644 // eval expr2
6645 // PUSHS "zzz"
6646 // EXECCONCAT 5
6647 for (;;)
6648 {
6649 if (p > start)
6650 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006651 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006652 ++count;
6653 }
6654 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006655 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006656 return NULL;
6657 may_generate_2STRING(-1, cctx);
6658 ++count;
6659 p = skipwhite(p);
6660 if (*p != '`')
6661 {
6662 emsg(_("E1083: missing backtick"));
6663 return NULL;
6664 }
6665 start = p + 1;
6666
6667 p = (char_u *)strstr((char *)start, "`=");
6668 if (p == NULL)
6669 {
6670 if (*skipwhite(start) != NUL)
6671 {
6672 generate_PUSHS(cctx, vim_strsave(start));
6673 ++count;
6674 }
6675 break;
6676 }
6677 }
6678 generate_EXECCONCAT(cctx, count);
6679 }
6680 else
6681 generate_EXEC(cctx, line);
6682
6683theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006684 if (*nextcmd != NUL)
6685 {
6686 // the parser expects a pointer to the bar, put it back
6687 --nextcmd;
6688 *nextcmd = '|';
6689 }
6690
6691 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006692}
6693
6694/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006695 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006696 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006697 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006698 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006699add_def_function(ufunc_T *ufunc)
6700{
6701 dfunc_T *dfunc;
6702
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006703 if (def_functions.ga_len == 0)
6704 {
6705 // The first position is not used, so that a zero uf_dfunc_idx means it
6706 // wasn't set.
6707 if (ga_grow(&def_functions, 1) == FAIL)
6708 return FAIL;
6709 ++def_functions.ga_len;
6710 }
6711
Bram Moolenaar09689a02020-05-09 22:50:08 +02006712 // Add the function to "def_functions".
6713 if (ga_grow(&def_functions, 1) == FAIL)
6714 return FAIL;
6715 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6716 CLEAR_POINTER(dfunc);
6717 dfunc->df_idx = def_functions.ga_len;
6718 ufunc->uf_dfunc_idx = dfunc->df_idx;
6719 dfunc->df_ufunc = ufunc;
6720 ++def_functions.ga_len;
6721 return OK;
6722}
6723
6724/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725 * After ex_function() has collected all the function lines: parse and compile
6726 * the lines into instructions.
6727 * Adds the function to "def_functions".
6728 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6729 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006730 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006731 * This can be used recursively through compile_lambda(), which may reallocate
6732 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006733 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006735 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006736compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006738 char_u *line = NULL;
6739 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006740 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006741 cctx_T cctx;
6742 garray_T *instr;
6743 int called_emsg_before = called_emsg;
6744 int ret = FAIL;
6745 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006746 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006747 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006748
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006749 // When using a function that was compiled before: Free old instructions.
6750 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006751 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006753 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6754 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006755 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006757 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006758 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006759
Bram Moolenaara80faa82020-04-12 19:37:17 +02006760 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006761 cctx.ctx_ufunc = ufunc;
6762 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006763 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6765 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6766 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6767 cctx.ctx_type_list = &ufunc->uf_type_list;
6768 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6769 instr = &cctx.ctx_instr;
6770
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006771 // Set the context to the function, it may be compiled when called from
6772 // another script. Set the script version to the most modern one.
6773 // The line number will be set in next_line_from_context().
6774 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6776
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006777 // Make sure error messages are OK.
6778 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6779 if (do_estack_push)
6780 estack_push_ufunc(ufunc, 1);
6781
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006782 if (ufunc->uf_def_args.ga_len > 0)
6783 {
6784 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006785 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006786 int i;
6787 char_u *arg;
6788 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6789
6790 // Produce instructions for the default values of optional arguments.
6791 // Store the instruction index in uf_def_arg_idx[] so that we know
6792 // where to start when the function is called, depending on the number
6793 // of arguments.
6794 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6795 if (ufunc->uf_def_arg_idx == NULL)
6796 goto erret;
6797 for (i = 0; i < count; ++i)
6798 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006799 garray_T *stack = &cctx.ctx_type_stack;
6800 type_T *val_type;
6801 int arg_idx = first_def_arg + i;
6802
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006803 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6804 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006805 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006806 goto erret;
6807
6808 // If no type specified use the type of the default value.
6809 // Otherwise check that the default value type matches the
6810 // specified type.
6811 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6812 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6813 ufunc->uf_arg_types[arg_idx] = val_type;
6814 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6815 == FAIL)
6816 {
6817 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6818 arg_idx + 1);
6819 goto erret;
6820 }
6821
6822 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006823 goto erret;
6824 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006825 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6826 }
6827
6828 /*
6829 * Loop over all the lines of the function and generate instructions.
6830 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831 for (;;)
6832 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006833 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006834 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006835 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006836 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006837
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006838 // Bail out on the first error to avoid a flood of errors and report
6839 // the right line number when inside try/catch.
6840 if (emsg_before != called_emsg)
6841 goto erret;
6842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843 if (line != NULL && *line == '|')
6844 // the line continues after a '|'
6845 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006846 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006847 && !(*line == '#' && (line == cctx.ctx_line_start
6848 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006849 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006850 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006851 goto erret;
6852 }
6853 else
6854 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006855 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006856 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006857 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006858 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006859 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006860 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006861
Bram Moolenaara80faa82020-04-12 19:37:17 +02006862 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006863 ea.cmdlinep = &line;
6864 ea.cmd = skipwhite(line);
6865
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006866 // Some things can be recognized by the first character.
6867 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006869 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006870 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006871 if (ea.cmd[1] != '{')
6872 {
6873 line = (char_u *)"";
6874 continue;
6875 }
6876 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006877
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006878 case '}':
6879 {
6880 // "}" ends a block scope
6881 scopetype_T stype = cctx.ctx_scope == NULL
6882 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006883
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006884 if (stype == BLOCK_SCOPE)
6885 {
6886 compile_endblock(&cctx);
6887 line = ea.cmd;
6888 }
6889 else
6890 {
6891 emsg(_("E1025: using } outside of a block scope"));
6892 goto erret;
6893 }
6894 if (line != NULL)
6895 line = skipwhite(ea.cmd + 1);
6896 continue;
6897 }
6898
6899 case '{':
6900 // "{" starts a block scope
6901 // "{'a': 1}->func() is something else
6902 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6903 {
6904 line = compile_block(ea.cmd, &cctx);
6905 continue;
6906 }
6907 break;
6908
6909 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006910 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006911 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912 }
6913
6914 /*
6915 * COMMAND MODIFIERS
6916 */
6917 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6918 {
6919 if (errormsg != NULL)
6920 goto erret;
6921 // empty line or comment
6922 line = (char_u *)"";
6923 continue;
6924 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006925 // TODO: use modifiers in the command
6926 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927
6928 // Skip ":call" to get to the function name.
6929 if (checkforcmd(&ea.cmd, "call", 3))
6930 ea.cmd = skipwhite(ea.cmd);
6931
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006932 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006934 char_u *pskip;
6935
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006936 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006937 // find what follows.
6938 // Skip over "var.member", "var[idx]" and the like.
6939 // Also "&opt = val", "$ENV = val" and "@r = val".
6940 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006941 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006942 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006943 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006945 char_u *var_end;
6946 int oplen;
6947 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006949 var_end = find_name_end(pskip, NULL, NULL,
6950 FNE_CHECK_START | FNE_INCL_BR);
6951 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006952 if (oplen > 0)
6953 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006954 size_t len = p - ea.cmd;
6955
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006956 // Recognize an assignment if we recognize the variable
6957 // name:
6958 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006959 // "local = expr" where "local" is a local var.
6960 // "script = expr" where "script" is a script-local var.
6961 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006962 // "&opt = expr"
6963 // "$ENV = expr"
6964 // "@r = expr"
6965 if (*ea.cmd == '&'
6966 || *ea.cmd == '$'
6967 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006968 || ((len) > 2 && ea.cmd[1] == ':')
6969 || lookup_local(ea.cmd, len, &cctx) != NULL
6970 || lookup_arg(ea.cmd, len, NULL, NULL,
6971 NULL, &cctx) == OK
6972 || lookup_script(ea.cmd, len) == OK
6973 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006974 {
6975 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006976 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006977 goto erret;
6978 continue;
6979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006980 }
6981 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006982
6983 if (*ea.cmd == '[')
6984 {
6985 // [var, var] = expr
6986 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6987 if (line == NULL)
6988 goto erret;
6989 if (line != ea.cmd)
6990 continue;
6991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006992 }
6993
6994 /*
6995 * COMMAND after range
6996 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006997 cmd = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006998 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006999 if (ea.cmd > cmd && !starts_with_colon)
7000 {
7001 emsg(_(e_colon_required));
7002 goto erret;
7003 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007004 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007005 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007006 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007
7008 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7009 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007010 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007011 {
7012 line += STRLEN(line);
7013 continue;
7014 }
7015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007016 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007017 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007019 // CMD_let cannot happen, compile_assignment() above is used
7020 iemsg("Command from find_ex_command() not handled");
7021 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007022 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 }
7024
7025 p = skipwhite(p);
7026
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007027 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007028 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007029 && ea.cmdidx != CMD_elseif
7030 && ea.cmdidx != CMD_else
7031 && ea.cmdidx != CMD_endif)
7032 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007033 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007034 continue;
7035 }
7036
Bram Moolenaarefd88552020-06-18 20:50:10 +02007037 if (ea.cmdidx != CMD_elseif
7038 && ea.cmdidx != CMD_else
7039 && ea.cmdidx != CMD_endif
7040 && ea.cmdidx != CMD_endfor
7041 && ea.cmdidx != CMD_endwhile
7042 && ea.cmdidx != CMD_catch
7043 && ea.cmdidx != CMD_finally
7044 && ea.cmdidx != CMD_endtry)
7045 {
7046 if (cctx.ctx_had_return)
7047 {
7048 emsg(_("E1095: Unreachable code after :return"));
7049 goto erret;
7050 }
7051 }
7052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 switch (ea.cmdidx)
7054 {
7055 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007056 ea.arg = p;
7057 line = compile_nested_function(&ea, &cctx);
7058 break;
7059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007061 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 goto erret;
7063
7064 case CMD_return:
7065 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007066 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007067 break;
7068
7069 case CMD_let:
7070 case CMD_const:
7071 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007072 if (line == p)
7073 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074 break;
7075
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007076 case CMD_unlet:
7077 case CMD_unlockvar:
7078 case CMD_lockvar:
7079 line = compile_unletlock(p, &ea, &cctx);
7080 break;
7081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082 case CMD_import:
7083 line = compile_import(p, &cctx);
7084 break;
7085
7086 case CMD_if:
7087 line = compile_if(p, &cctx);
7088 break;
7089 case CMD_elseif:
7090 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007091 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 break;
7093 case CMD_else:
7094 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007095 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007096 break;
7097 case CMD_endif:
7098 line = compile_endif(p, &cctx);
7099 break;
7100
7101 case CMD_while:
7102 line = compile_while(p, &cctx);
7103 break;
7104 case CMD_endwhile:
7105 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007106 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 break;
7108
7109 case CMD_for:
7110 line = compile_for(p, &cctx);
7111 break;
7112 case CMD_endfor:
7113 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007114 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007115 break;
7116 case CMD_continue:
7117 line = compile_continue(p, &cctx);
7118 break;
7119 case CMD_break:
7120 line = compile_break(p, &cctx);
7121 break;
7122
7123 case CMD_try:
7124 line = compile_try(p, &cctx);
7125 break;
7126 case CMD_catch:
7127 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007128 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 break;
7130 case CMD_finally:
7131 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007132 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 break;
7134 case CMD_endtry:
7135 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007136 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007137 break;
7138 case CMD_throw:
7139 line = compile_throw(p, &cctx);
7140 break;
7141
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007142 case CMD_eval:
7143 if (compile_expr0(&p, &cctx) == FAIL)
7144 goto erret;
7145
7146 // drop the return value
7147 generate_instr_drop(&cctx, ISN_DROP, 1);
7148
7149 line = skipwhite(p);
7150 break;
7151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007153 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007154 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007155 case CMD_echomsg:
7156 case CMD_echoerr:
7157 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007158 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007160 // TODO: other commands with an expression argument
7161
Bram Moolenaar002262f2020-07-08 17:47:57 +02007162 case CMD_SIZE:
7163 semsg(_("E476: Invalid command: %s"), ea.cmd);
7164 goto erret;
7165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007167 // Not recognized, execute with do_cmdline_cmd().
7168 ea.arg = p;
7169 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007170 break;
7171 }
7172 if (line == NULL)
7173 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007174 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175
7176 if (cctx.ctx_type_stack.ga_len < 0)
7177 {
7178 iemsg("Type stack underflow");
7179 goto erret;
7180 }
7181 }
7182
7183 if (cctx.ctx_scope != NULL)
7184 {
7185 if (cctx.ctx_scope->se_type == IF_SCOPE)
7186 emsg(_(e_endif));
7187 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7188 emsg(_(e_endwhile));
7189 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7190 emsg(_(e_endfor));
7191 else
7192 emsg(_("E1026: Missing }"));
7193 goto erret;
7194 }
7195
Bram Moolenaarefd88552020-06-18 20:50:10 +02007196 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007197 {
7198 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7199 {
7200 emsg(_("E1027: Missing return statement"));
7201 goto erret;
7202 }
7203
7204 // Return zero if there is no return at the end.
7205 generate_PUSHNR(&cctx, 0);
7206 generate_instr(&cctx, ISN_RETURN);
7207 }
7208
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007209 {
7210 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7211 + ufunc->uf_dfunc_idx;
7212 dfunc->df_deleted = FALSE;
7213 dfunc->df_instr = instr->ga_data;
7214 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007215 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007216 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007217 if (cctx.ctx_outer_used)
7218 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007219 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007221
7222 ret = OK;
7223
7224erret:
7225 if (ret == FAIL)
7226 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007227 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007228 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7229 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007230
7231 for (idx = 0; idx < instr->ga_len; ++idx)
7232 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007233 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007234
Bram Moolenaar45a15082020-05-25 00:28:33 +02007235 // if using the last entry in the table we might as well remove it
7236 if (!dfunc->df_deleted
7237 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007238 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007239 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007240
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007241 while (cctx.ctx_scope != NULL)
7242 drop_scope(&cctx);
7243
Bram Moolenaar20431c92020-03-20 18:39:46 +01007244 // Don't execute this function body.
7245 ga_clear_strings(&ufunc->uf_lines);
7246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007247 if (errormsg != NULL)
7248 emsg(errormsg);
7249 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007250 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007251 }
7252
7253 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007254 if (do_estack_push)
7255 estack_pop();
7256
Bram Moolenaar20431c92020-03-20 18:39:46 +01007257 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007258 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007259 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007260 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261}
7262
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007263 void
7264set_function_type(ufunc_T *ufunc)
7265{
7266 int varargs = ufunc->uf_va_name != NULL;
7267 int argcount = ufunc->uf_args.ga_len;
7268
7269 // Create a type for the function, with the return type and any
7270 // argument types.
7271 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7272 // The type is included in "tt_args".
7273 if (argcount > 0 || varargs)
7274 {
7275 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7276 argcount, &ufunc->uf_type_list);
7277 // Add argument types to the function type.
7278 if (func_type_add_arg_types(ufunc->uf_func_type,
7279 argcount + varargs,
7280 &ufunc->uf_type_list) == FAIL)
7281 return;
7282 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7283 ufunc->uf_func_type->tt_min_argcount =
7284 argcount - ufunc->uf_def_args.ga_len;
7285 if (ufunc->uf_arg_types == NULL)
7286 {
7287 int i;
7288
7289 // lambda does not have argument types.
7290 for (i = 0; i < argcount; ++i)
7291 ufunc->uf_func_type->tt_args[i] = &t_any;
7292 }
7293 else
7294 mch_memmove(ufunc->uf_func_type->tt_args,
7295 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7296 if (varargs)
7297 {
7298 ufunc->uf_func_type->tt_args[argcount] =
7299 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7300 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7301 }
7302 }
7303 else
7304 // No arguments, can use a predefined type.
7305 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7306 argcount, &ufunc->uf_type_list);
7307}
7308
7309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310/*
7311 * Delete an instruction, free what it contains.
7312 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007313 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314delete_instr(isn_T *isn)
7315{
7316 switch (isn->isn_type)
7317 {
7318 case ISN_EXEC:
7319 case ISN_LOADENV:
7320 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007321 case ISN_LOADB:
7322 case ISN_LOADW:
7323 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007325 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326 case ISN_PUSHEXC:
7327 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007328 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007329 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007330 case ISN_STOREB:
7331 case ISN_STOREW:
7332 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007333 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 vim_free(isn->isn_arg.string);
7335 break;
7336
7337 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007338 case ISN_STORES:
7339 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 break;
7341
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007342 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007343 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007344 vim_free(isn->isn_arg.unlet.ul_name);
7345 break;
7346
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347 case ISN_STOREOPT:
7348 vim_free(isn->isn_arg.storeopt.so_name);
7349 break;
7350
7351 case ISN_PUSHBLOB: // push blob isn_arg.blob
7352 blob_unref(isn->isn_arg.blob);
7353 break;
7354
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007355 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007356#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007357 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007358#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007359 break;
7360
7361 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007362#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007363 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007364#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007365 break;
7366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 case ISN_UCALL:
7368 vim_free(isn->isn_arg.ufunc.cuf_name);
7369 break;
7370
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007371 case ISN_FUNCREF:
7372 {
7373 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7374 + isn->isn_arg.funcref.fr_func;
7375 func_ptr_unref(dfunc->df_ufunc);
7376 }
7377 break;
7378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 case ISN_2BOOL:
7380 case ISN_2STRING:
7381 case ISN_ADDBLOB:
7382 case ISN_ADDLIST:
7383 case ISN_BCALL:
7384 case ISN_CATCH:
7385 case ISN_CHECKNR:
7386 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007387 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 case ISN_COMPAREANY:
7389 case ISN_COMPAREBLOB:
7390 case ISN_COMPAREBOOL:
7391 case ISN_COMPAREDICT:
7392 case ISN_COMPAREFLOAT:
7393 case ISN_COMPAREFUNC:
7394 case ISN_COMPARELIST:
7395 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007396 case ISN_COMPARESPECIAL:
7397 case ISN_COMPARESTRING:
7398 case ISN_CONCAT:
7399 case ISN_DCALL:
7400 case ISN_DROP:
7401 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007402 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007403 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007405 case ISN_EXECCONCAT:
7406 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007408 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007409 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007410 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007411 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 case ISN_JUMP:
7413 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007414 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007415 case ISN_LOADSCRIPT:
7416 case ISN_LOADREG:
7417 case ISN_LOADV:
7418 case ISN_NEGATENR:
7419 case ISN_NEWDICT:
7420 case ISN_NEWLIST:
7421 case ISN_OPNR:
7422 case ISN_OPFLOAT:
7423 case ISN_OPANY:
7424 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007425 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 case ISN_PUSHF:
7427 case ISN_PUSHNR:
7428 case ISN_PUSHBOOL:
7429 case ISN_PUSHSPEC:
7430 case ISN_RETURN:
7431 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007432 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007433 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007435 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007436 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007437 case ISN_STOREDICT:
7438 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 case ISN_THROW:
7440 case ISN_TRY:
7441 // nothing allocated
7442 break;
7443 }
7444}
7445
7446/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007447 * Free all instructions for "dfunc".
7448 */
7449 static void
7450delete_def_function_contents(dfunc_T *dfunc)
7451{
7452 int idx;
7453
7454 ga_clear(&dfunc->df_def_args_isn);
7455
7456 if (dfunc->df_instr != NULL)
7457 {
7458 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7459 delete_instr(dfunc->df_instr + idx);
7460 VIM_CLEAR(dfunc->df_instr);
7461 }
7462
7463 dfunc->df_deleted = TRUE;
7464}
7465
7466/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007467 * When a user function is deleted, clear the contents of any associated def
7468 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469 */
7470 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007471clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007473 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474 {
7475 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7476 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477
Bram Moolenaar20431c92020-03-20 18:39:46 +01007478 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007479 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 }
7481}
7482
7483#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007484/*
7485 * Free all functions defined with ":def".
7486 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 void
7488free_def_functions(void)
7489{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007490 int idx;
7491
7492 for (idx = 0; idx < def_functions.ga_len; ++idx)
7493 {
7494 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7495
7496 delete_def_function_contents(dfunc);
7497 }
7498
7499 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007500}
7501#endif
7502
7503
7504#endif // FEAT_EVAL