blob: c0e4674de421a1c4b1d6d2de36d82a697a25a1df [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 Moolenaar0cb5bcf2020-06-20 18:19:09 +02003116 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003117 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 return FAIL;
3119}
3120
3121/*
3122 * Compile a lamda call: expr->{lambda}(args)
3123 * "arg" points to the "{".
3124 */
3125 static int
3126compile_lambda_call(char_u **arg, cctx_T *cctx)
3127{
3128 ufunc_T *ufunc;
3129 typval_T rettv;
3130 int argcount = 1;
3131 int ret = FAIL;
3132
3133 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003134 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 return FAIL;
3136
3137 if (**arg != '(')
3138 {
3139 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003140 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 else
3142 semsg(_(e_missing_paren), "lambda");
3143 clear_tv(&rettv);
3144 return FAIL;
3145 }
3146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 ufunc = rettv.vval.v_partial->pt_func;
3148 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003149 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003150 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003151
3152 // The function will have one line: "return {expr}".
3153 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003154 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155
3156 // compile the arguments
3157 *arg = skipwhite(*arg + 1);
3158 if (compile_arguments(arg, cctx, &argcount) == OK)
3159 // call the compiled function
3160 ret = generate_CALL(cctx, ufunc, argcount);
3161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 return ret;
3163}
3164
3165/*
3166 * parse a dict: {'key': val} or #{key: val}
3167 * "*arg" points to the '{'.
3168 */
3169 static int
3170compile_dict(char_u **arg, cctx_T *cctx, int literal)
3171{
3172 garray_T *instr = &cctx->ctx_instr;
3173 int count = 0;
3174 dict_T *d = dict_alloc();
3175 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003176 char_u *whitep = *arg;
3177 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178
3179 if (d == NULL)
3180 return FAIL;
3181 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003182 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 {
3184 char_u *key = NULL;
3185
Bram Moolenaar23c55272020-06-21 16:58:13 +02003186 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003187 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003188 *arg = NULL;
3189 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003190 }
3191
3192 if (**arg == '}')
3193 break;
3194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 if (literal)
3196 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003197 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198
Bram Moolenaar2c330432020-04-13 14:41:35 +02003199 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 {
3201 semsg(_("E1014: Invalid key: %s"), *arg);
3202 return FAIL;
3203 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003204 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 if (generate_PUSHS(cctx, key) == FAIL)
3206 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003207 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 }
3209 else
3210 {
3211 isn_T *isn;
3212
Bram Moolenaara5565e42020-05-09 15:44:01 +02003213 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 return FAIL;
3215 // TODO: check type is string
3216 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3217 if (isn->isn_type == ISN_PUSHS)
3218 key = isn->isn_arg.string;
3219 }
3220
3221 // Check for duplicate keys, if using string keys.
3222 if (key != NULL)
3223 {
3224 item = dict_find(d, key, -1);
3225 if (item != NULL)
3226 {
3227 semsg(_(e_duplicate_key), key);
3228 goto failret;
3229 }
3230 item = dictitem_alloc(key);
3231 if (item != NULL)
3232 {
3233 item->di_tv.v_type = VAR_UNKNOWN;
3234 item->di_tv.v_lock = 0;
3235 if (dict_add(d, item) == FAIL)
3236 dictitem_free(item);
3237 }
3238 }
3239
3240 *arg = skipwhite(*arg);
3241 if (**arg != ':')
3242 {
3243 semsg(_(e_missing_dict_colon), *arg);
3244 return FAIL;
3245 }
3246
Bram Moolenaar2c330432020-04-13 14:41:35 +02003247 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003249 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003250 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003251 *arg = NULL;
3252 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003253 }
3254
Bram Moolenaara5565e42020-05-09 15:44:01 +02003255 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 return FAIL;
3257 ++count;
3258
Bram Moolenaar2c330432020-04-13 14:41:35 +02003259 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003260 *arg = skipwhite(*arg);
3261 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003262 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003263 *arg = NULL;
3264 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003265 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 if (**arg == '}')
3267 break;
3268 if (**arg != ',')
3269 {
3270 semsg(_(e_missing_dict_comma), *arg);
3271 goto failret;
3272 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003273 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 *arg = skipwhite(*arg + 1);
3275 }
3276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 *arg = *arg + 1;
3278
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003279 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003280 p = skipwhite(*arg);
3281 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003282 *arg += STRLEN(*arg);
3283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 dict_unref(d);
3285 return generate_NEWDICT(cctx, count);
3286
3287failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003288 if (*arg == NULL)
3289 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 dict_unref(d);
3291 return FAIL;
3292}
3293
3294/*
3295 * Compile "&option".
3296 */
3297 static int
3298compile_get_option(char_u **arg, cctx_T *cctx)
3299{
3300 typval_T rettv;
3301 char_u *start = *arg;
3302 int ret;
3303
3304 // parse the option and get the current value to get the type.
3305 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003306 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 if (ret == OK)
3308 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003309 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 char_u *name = vim_strnsave(start, *arg - start);
3311 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3312
3313 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3314 vim_free(name);
3315 }
3316 clear_tv(&rettv);
3317
3318 return ret;
3319}
3320
3321/*
3322 * Compile "$VAR".
3323 */
3324 static int
3325compile_get_env(char_u **arg, cctx_T *cctx)
3326{
3327 char_u *start = *arg;
3328 int len;
3329 int ret;
3330 char_u *name;
3331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 ++*arg;
3333 len = get_env_len(arg);
3334 if (len == 0)
3335 {
3336 semsg(_(e_syntax_at), start - 1);
3337 return FAIL;
3338 }
3339
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003340 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 name = vim_strnsave(start, len + 1);
3342 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3343 vim_free(name);
3344 return ret;
3345}
3346
3347/*
3348 * Compile "@r".
3349 */
3350 static int
3351compile_get_register(char_u **arg, cctx_T *cctx)
3352{
3353 int ret;
3354
3355 ++*arg;
3356 if (**arg == NUL)
3357 {
3358 semsg(_(e_syntax_at), *arg - 1);
3359 return FAIL;
3360 }
3361 if (!valid_yank_reg(**arg, TRUE))
3362 {
3363 emsg_invreg(**arg);
3364 return FAIL;
3365 }
3366 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3367 ++*arg;
3368 return ret;
3369}
3370
3371/*
3372 * Apply leading '!', '-' and '+' to constant "rettv".
3373 */
3374 static int
3375apply_leader(typval_T *rettv, char_u *start, char_u *end)
3376{
3377 char_u *p = end;
3378
3379 // this works from end to start
3380 while (p > start)
3381 {
3382 --p;
3383 if (*p == '-' || *p == '+')
3384 {
3385 // only '-' has an effect, for '+' we only check the type
3386#ifdef FEAT_FLOAT
3387 if (rettv->v_type == VAR_FLOAT)
3388 {
3389 if (*p == '-')
3390 rettv->vval.v_float = -rettv->vval.v_float;
3391 }
3392 else
3393#endif
3394 {
3395 varnumber_T val;
3396 int error = FALSE;
3397
3398 // tv_get_number_chk() accepts a string, but we don't want that
3399 // here
3400 if (check_not_string(rettv) == FAIL)
3401 return FAIL;
3402 val = tv_get_number_chk(rettv, &error);
3403 clear_tv(rettv);
3404 if (error)
3405 return FAIL;
3406 if (*p == '-')
3407 val = -val;
3408 rettv->v_type = VAR_NUMBER;
3409 rettv->vval.v_number = val;
3410 }
3411 }
3412 else
3413 {
3414 int v = tv2bool(rettv);
3415
3416 // '!' is permissive in the type.
3417 clear_tv(rettv);
3418 rettv->v_type = VAR_BOOL;
3419 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3420 }
3421 }
3422 return OK;
3423}
3424
3425/*
3426 * Recognize v: variables that are constants and set "rettv".
3427 */
3428 static void
3429get_vim_constant(char_u **arg, typval_T *rettv)
3430{
3431 if (STRNCMP(*arg, "v:true", 6) == 0)
3432 {
3433 rettv->v_type = VAR_BOOL;
3434 rettv->vval.v_number = VVAL_TRUE;
3435 *arg += 6;
3436 }
3437 else if (STRNCMP(*arg, "v:false", 7) == 0)
3438 {
3439 rettv->v_type = VAR_BOOL;
3440 rettv->vval.v_number = VVAL_FALSE;
3441 *arg += 7;
3442 }
3443 else if (STRNCMP(*arg, "v:null", 6) == 0)
3444 {
3445 rettv->v_type = VAR_SPECIAL;
3446 rettv->vval.v_number = VVAL_NULL;
3447 *arg += 6;
3448 }
3449 else if (STRNCMP(*arg, "v:none", 6) == 0)
3450 {
3451 rettv->v_type = VAR_SPECIAL;
3452 rettv->vval.v_number = VVAL_NONE;
3453 *arg += 6;
3454 }
3455}
3456
Bram Moolenaar61a89812020-05-07 16:58:17 +02003457 static exptype_T
3458get_compare_type(char_u *p, int *len, int *type_is)
3459{
3460 exptype_T type = EXPR_UNKNOWN;
3461 int i;
3462
3463 switch (p[0])
3464 {
3465 case '=': if (p[1] == '=')
3466 type = EXPR_EQUAL;
3467 else if (p[1] == '~')
3468 type = EXPR_MATCH;
3469 break;
3470 case '!': if (p[1] == '=')
3471 type = EXPR_NEQUAL;
3472 else if (p[1] == '~')
3473 type = EXPR_NOMATCH;
3474 break;
3475 case '>': if (p[1] != '=')
3476 {
3477 type = EXPR_GREATER;
3478 *len = 1;
3479 }
3480 else
3481 type = EXPR_GEQUAL;
3482 break;
3483 case '<': if (p[1] != '=')
3484 {
3485 type = EXPR_SMALLER;
3486 *len = 1;
3487 }
3488 else
3489 type = EXPR_SEQUAL;
3490 break;
3491 case 'i': if (p[1] == 's')
3492 {
3493 // "is" and "isnot"; but not a prefix of a name
3494 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3495 *len = 5;
3496 i = p[*len];
3497 if (!isalnum(i) && i != '_')
3498 {
3499 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3500 *type_is = TRUE;
3501 }
3502 }
3503 break;
3504 }
3505 return type;
3506}
3507
3508/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 * Compile code to apply '-', '+' and '!'.
3510 */
3511 static int
3512compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3513{
3514 char_u *p = end;
3515
3516 // this works from end to start
3517 while (p > start)
3518 {
3519 --p;
3520 if (*p == '-' || *p == '+')
3521 {
3522 int negate = *p == '-';
3523 isn_T *isn;
3524
3525 // TODO: check type
3526 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3527 {
3528 --p;
3529 if (*p == '-')
3530 negate = !negate;
3531 }
3532 // only '-' has an effect, for '+' we only check the type
3533 if (negate)
3534 isn = generate_instr(cctx, ISN_NEGATENR);
3535 else
3536 isn = generate_instr(cctx, ISN_CHECKNR);
3537 if (isn == NULL)
3538 return FAIL;
3539 }
3540 else
3541 {
3542 int invert = TRUE;
3543
3544 while (p > start && p[-1] == '!')
3545 {
3546 --p;
3547 invert = !invert;
3548 }
3549 if (generate_2BOOL(cctx, invert) == FAIL)
3550 return FAIL;
3551 }
3552 }
3553 return OK;
3554}
3555
3556/*
3557 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003558 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 */
3560 static int
3561compile_subscript(
3562 char_u **arg,
3563 cctx_T *cctx,
3564 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003565 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003566 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567{
3568 for (;;)
3569 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003570 char_u *p = skipwhite(*arg);
3571
3572 if (*p == NUL || (VIM_ISWHITE(**arg) && comment_start(p)))
3573 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003574 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003575
3576 // If a following line starts with "->{" or "->X" advance to that
3577 // line, so that a line break before "->" is allowed.
3578 if (next != NULL && next[0] == '-' && next[1] == '>'
3579 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3580 {
3581 next = next_line_from_context(cctx, TRUE);
3582 if (next == NULL)
3583 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003584 *arg = next;
3585 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003586 }
3587 }
3588
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003589 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003591 garray_T *stack = &cctx->ctx_type_stack;
3592 type_T *type;
3593 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003595 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003596 return FAIL;
3597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003599 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3600
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003601 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3603 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003604 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 return FAIL;
3606 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003607 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003609 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003610 return FAIL;
3611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 // something->method()
3613 // Apply the '!', '-' and '+' first:
3614 // -1.0->func() works like (-1.0)->func()
3615 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3616 return FAIL;
3617 *start_leader = end_leader; // don't apply again later
3618
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003619 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003620 *arg = skipwhite(p);
3621 if (may_get_next_line(p, arg, cctx) == FAIL)
3622 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 if (**arg == '{')
3624 {
3625 // lambda call: list->{lambda}
3626 if (compile_lambda_call(arg, cctx) == FAIL)
3627 return FAIL;
3628 }
3629 else
3630 {
3631 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003632 p = *arg;
3633 if (ASCII_ISALPHA(*p) && p[1] == ':')
3634 p += 2;
3635 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 ;
3637 if (*p != '(')
3638 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003639 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 return FAIL;
3641 }
3642 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003643 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 return FAIL;
3645 }
3646 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003647 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003649 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003650 type_T **typep;
3651
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003652 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003653 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003654 // TODO: blob index
3655 // TODO: more arguments
3656 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003657 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003658 return FAIL;
3659
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003660 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003661 *arg = skipwhite(p);
3662 if (may_get_next_line(p, arg, cctx) == FAIL)
3663 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003664 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 return FAIL;
3666
3667 if (**arg != ']')
3668 {
3669 emsg(_(e_missbrac));
3670 return FAIL;
3671 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003672 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003674 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3675 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003676 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003677 if ((*typep)->tt_type == VAR_LIST)
3678 *typep = (*typep)->tt_member;
3679 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3680 return FAIL;
3681 }
3682 else if ((*typep)->tt_type == VAR_DICT)
3683 {
3684 *typep = (*typep)->tt_member;
3685 if (may_generate_2STRING(-1, cctx) == FAIL)
3686 return FAIL;
3687 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3688 return FAIL;
3689 }
3690 else
3691 {
3692 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003693 return FAIL;
3694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003696 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003698 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003699 return FAIL;
3700
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003701 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003702 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3703 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003705 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 if (eval_isnamec1(*p))
3707 while (eval_isnamec(*p))
3708 MB_PTR_ADV(p);
3709 if (p == *arg)
3710 {
3711 semsg(_(e_syntax_at), *arg);
3712 return FAIL;
3713 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003714 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 return FAIL;
3716 *arg = p;
3717 }
3718 else
3719 break;
3720 }
3721
3722 // TODO - see handle_subscript():
3723 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3724 // Don't do this when "Func" is already a partial that was bound
3725 // explicitly (pt_auto is FALSE).
3726
3727 return OK;
3728}
3729
3730/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003731 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3732 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003734 * If the value is a constant "ppconst->pp_ret" will be set.
3735 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003736 *
3737 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 */
3739
3740/*
3741 * number number constant
3742 * 0zFFFFFFFF Blob constant
3743 * "string" string constant
3744 * 'string' literal string constant
3745 * &option-name option value
3746 * @r register contents
3747 * identifier variable value
3748 * function() function call
3749 * $VAR environment variable
3750 * (expression) nested expression
3751 * [expr, expr] List
3752 * {key: val, key: val} Dictionary
3753 * #{key: val, key: val} Dictionary with literal keys
3754 *
3755 * Also handle:
3756 * ! in front logical NOT
3757 * - in front unary minus
3758 * + in front unary plus (ignored)
3759 * trailing (arg) funcref/partial call
3760 * trailing [] subscript in String or List
3761 * trailing .name entry in Dictionary
3762 * trailing ->name() method call
3763 */
3764 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003765compile_expr7(
3766 char_u **arg,
3767 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003768 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 char_u *start_leader, *end_leader;
3771 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003772 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003773 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774
3775 /*
3776 * Skip '!', '-' and '+' characters. They are handled later.
3777 */
3778 start_leader = *arg;
3779 while (**arg == '!' || **arg == '-' || **arg == '+')
3780 *arg = skipwhite(*arg + 1);
3781 end_leader = *arg;
3782
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003783 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 switch (**arg)
3785 {
3786 /*
3787 * Number constant.
3788 */
3789 case '0': // also for blob starting with 0z
3790 case '1':
3791 case '2':
3792 case '3':
3793 case '4':
3794 case '5':
3795 case '6':
3796 case '7':
3797 case '8':
3798 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003799 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 return FAIL;
3801 break;
3802
3803 /*
3804 * String constant: "string".
3805 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003806 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 return FAIL;
3808 break;
3809
3810 /*
3811 * Literal string constant: 'str''ing'.
3812 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003813 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 return FAIL;
3815 break;
3816
3817 /*
3818 * Constant Vim variable.
3819 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003820 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 ret = NOTDONE;
3822 break;
3823
3824 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003825 * "true" constant
3826 */
3827 case 't': if (STRNCMP(*arg, "true", 4) == 0
3828 && !eval_isnamec((*arg)[4]))
3829 {
3830 *arg += 4;
3831 rettv->v_type = VAR_BOOL;
3832 rettv->vval.v_number = VVAL_TRUE;
3833 }
3834 else
3835 ret = NOTDONE;
3836 break;
3837
3838 /*
3839 * "false" constant
3840 */
3841 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3842 && !eval_isnamec((*arg)[5]))
3843 {
3844 *arg += 5;
3845 rettv->v_type = VAR_BOOL;
3846 rettv->vval.v_number = VVAL_FALSE;
3847 }
3848 else
3849 ret = NOTDONE;
3850 break;
3851
3852 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 * List: [expr, expr]
3854 */
3855 case '[': ret = compile_list(arg, cctx);
3856 break;
3857
3858 /*
3859 * Dictionary: #{key: val, key: val}
3860 */
3861 case '#': if ((*arg)[1] == '{')
3862 {
3863 ++*arg;
3864 ret = compile_dict(arg, cctx, TRUE);
3865 }
3866 else
3867 ret = NOTDONE;
3868 break;
3869
3870 /*
3871 * Lambda: {arg, arg -> expr}
3872 * Dictionary: {'key': val, 'key': val}
3873 */
3874 case '{': {
3875 char_u *start = skipwhite(*arg + 1);
3876
3877 // Find out what comes after the arguments.
3878 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003879 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 if (ret != FAIL && *start == '>')
3881 ret = compile_lambda(arg, cctx);
3882 else
3883 ret = compile_dict(arg, cctx, FALSE);
3884 }
3885 break;
3886
3887 /*
3888 * Option value: &name
3889 */
3890 case '&': ret = compile_get_option(arg, cctx);
3891 break;
3892
3893 /*
3894 * Environment variable: $VAR.
3895 */
3896 case '$': ret = compile_get_env(arg, cctx);
3897 break;
3898
3899 /*
3900 * Register contents: @r.
3901 */
3902 case '@': ret = compile_get_register(arg, cctx);
3903 break;
3904 /*
3905 * nested expression: (expression).
3906 */
3907 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003908
3909 // recursive!
3910 if (ppconst->pp_used <= PPSIZE - 10)
3911 {
3912 ret = compile_expr1(arg, cctx, ppconst);
3913 }
3914 else
3915 {
3916 // Not enough space in ppconst, flush constants.
3917 if (generate_ppconst(cctx, ppconst) == FAIL)
3918 return FAIL;
3919 ret = compile_expr0(arg, cctx);
3920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 *arg = skipwhite(*arg);
3922 if (**arg == ')')
3923 ++*arg;
3924 else if (ret == OK)
3925 {
3926 emsg(_(e_missing_close));
3927 ret = FAIL;
3928 }
3929 break;
3930
3931 default: ret = NOTDONE;
3932 break;
3933 }
3934 if (ret == FAIL)
3935 return FAIL;
3936
Bram Moolenaar1c747212020-05-09 18:28:34 +02003937 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 {
3939 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003940 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003942 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 return FAIL;
3944 }
3945 start_leader = end_leader; // don't apply again below
3946
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003947 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003948 clear_tv(rettv);
3949 else
3950 // A constant expression can possibly be handled compile time,
3951 // return the value instead of generating code.
3952 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 }
3954 else if (ret == NOTDONE)
3955 {
3956 char_u *p;
3957 int r;
3958
3959 if (!eval_isnamec1(**arg))
3960 {
3961 semsg(_("E1015: Name expected: %s"), *arg);
3962 return FAIL;
3963 }
3964
3965 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003966 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003968 {
3969 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3970 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003972 {
3973 if (generate_ppconst(cctx, ppconst) == FAIL)
3974 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003976 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 if (r == FAIL)
3978 return FAIL;
3979 }
3980
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003981 // Handle following "[]", ".member", etc.
3982 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003983 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003984 ppconst) == FAIL)
3985 return FAIL;
3986 if (ppconst->pp_used > 0)
3987 {
3988 // apply the '!', '-' and '+' before the constant
3989 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3990 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3991 return FAIL;
3992 return OK;
3993 }
3994 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003996 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997}
3998
3999/*
4000 * * number multiplication
4001 * / number division
4002 * % number modulo
4003 */
4004 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004005compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006{
4007 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004008 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004009 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004011 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004012 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 return FAIL;
4014
4015 /*
4016 * Repeat computing, until no "*", "/" or "%" is following.
4017 */
4018 for (;;)
4019 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004020 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 if (*op != '*' && *op != '/' && *op != '%')
4022 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004023 if (next != NULL)
4024 {
4025 *arg = next_line_from_context(cctx, TRUE);
4026 op = skipwhite(*arg);
4027 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004028
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004029 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 {
4031 char_u buf[3];
4032
4033 vim_strncpy(buf, op, 1);
4034 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004035 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 }
4037 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004038 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004039 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004041 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004042 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004044
4045 if (ppconst->pp_used == ppconst_used + 2
4046 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4047 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004048 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004049 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4050 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004051 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004053 // both are numbers: compute the result
4054 switch (*op)
4055 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004056 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004057 break;
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;
4062 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004063 tv1->vval.v_number = res;
4064 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004065 }
4066 else
4067 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004068 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004069 generate_two_op(cctx, op);
4070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 }
4072
4073 return OK;
4074}
4075
4076/*
4077 * + number addition
4078 * - number subtraction
4079 * .. string concatenation
4080 */
4081 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004082compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083{
4084 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004085 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004087 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088
4089 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004090 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 return FAIL;
4092
4093 /*
4094 * Repeat computing, until no "+", "-" or ".." is following.
4095 */
4096 for (;;)
4097 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004098 op = may_peek_next_line(cctx, *arg, &next);
4099 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 break;
4101 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004102 if (next != NULL)
4103 {
4104 *arg = next_line_from_context(cctx, TRUE);
4105 op = skipwhite(*arg);
4106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004108 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 {
4110 char_u buf[3];
4111
4112 vim_strncpy(buf, op, oplen);
4113 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004114 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 }
4116
4117 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004118 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004119 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004121 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004122 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 return FAIL;
4124
Bram Moolenaara5565e42020-05-09 15:44:01 +02004125 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004126 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004127 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4128 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4129 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4130 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004132 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4133 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004134
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004135 // concat/subtract/add constant numbers
4136 if (*op == '+')
4137 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4138 else if (*op == '-')
4139 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4140 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004141 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004142 // concatenate constant strings
4143 char_u *s1 = tv1->vval.v_string;
4144 char_u *s2 = tv2->vval.v_string;
4145 size_t len1 = STRLEN(s1);
4146
4147 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4148 if (tv1->vval.v_string == NULL)
4149 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004150 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004151 return FAIL;
4152 }
4153 mch_memmove(tv1->vval.v_string, s1, len1);
4154 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004155 vim_free(s1);
4156 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004157 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004158 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 }
4160 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004161 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004162 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004163 if (*op == '.')
4164 {
4165 if (may_generate_2STRING(-2, cctx) == FAIL
4166 || may_generate_2STRING(-1, cctx) == FAIL)
4167 return FAIL;
4168 generate_instr_drop(cctx, ISN_CONCAT, 1);
4169 }
4170 else
4171 generate_two_op(cctx, op);
4172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 }
4174
4175 return OK;
4176}
4177
4178/*
4179 * expr5a == expr5b
4180 * expr5a =~ expr5b
4181 * expr5a != expr5b
4182 * expr5a !~ expr5b
4183 * expr5a > expr5b
4184 * expr5a >= expr5b
4185 * expr5a < expr5b
4186 * expr5a <= expr5b
4187 * expr5a is expr5b
4188 * expr5a isnot expr5b
4189 *
4190 * Produces instructions:
4191 * EVAL expr5a Push result of "expr5a"
4192 * EVAL expr5b Push result of "expr5b"
4193 * COMPARE one of the compare instructions
4194 */
4195 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004196compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197{
4198 exptype_T type = EXPR_UNKNOWN;
4199 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004200 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004203 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204
4205 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004206 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 return FAIL;
4208
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004209 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004210 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211
4212 /*
4213 * If there is a comparative operator, use it.
4214 */
4215 if (type != EXPR_UNKNOWN)
4216 {
4217 int ic = FALSE; // Default: do not ignore case
4218
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004219 if (next != NULL)
4220 {
4221 *arg = next_line_from_context(cctx, TRUE);
4222 p = skipwhite(*arg);
4223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 if (type_is && (p[len] == '?' || p[len] == '#'))
4225 {
4226 semsg(_(e_invexpr2), *arg);
4227 return FAIL;
4228 }
4229 // extra question mark appended: ignore case
4230 if (p[len] == '?')
4231 {
4232 ic = TRUE;
4233 ++len;
4234 }
4235 // extra '#' appended: match case (ignored)
4236 else if (p[len] == '#')
4237 ++len;
4238 // nothing appended: match case
4239
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004240 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 {
4242 char_u buf[7];
4243
4244 vim_strncpy(buf, p, len);
4245 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004246 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 }
4248
4249 // get the second variable
4250 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004251 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004252 return FAIL;
4253
Bram Moolenaara5565e42020-05-09 15:44:01 +02004254 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 return FAIL;
4256
Bram Moolenaara5565e42020-05-09 15:44:01 +02004257 if (ppconst->pp_used == ppconst_used + 2)
4258 {
4259 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4260 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4261 int ret;
4262
4263 // Both sides are a constant, compute the result now.
4264 // First check for a valid combination of types, this is more
4265 // strict than typval_compare().
4266 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4267 ret = FAIL;
4268 else
4269 {
4270 ret = typval_compare(tv1, tv2, type, ic);
4271 tv1->v_type = VAR_BOOL;
4272 tv1->vval.v_number = tv1->vval.v_number
4273 ? VVAL_TRUE : VVAL_FALSE;
4274 clear_tv(tv2);
4275 --ppconst->pp_used;
4276 }
4277 return ret;
4278 }
4279
4280 generate_ppconst(cctx, ppconst);
4281 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 }
4283
4284 return OK;
4285}
4286
Bram Moolenaar7f141552020-05-09 17:35:53 +02004287static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289/*
4290 * Compile || or &&.
4291 */
4292 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004293compile_and_or(
4294 char_u **arg,
4295 cctx_T *cctx,
4296 char *op,
4297 ppconst_T *ppconst,
4298 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004300 char_u *next;
4301 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 int opchar = *op;
4303
4304 if (p[0] == opchar && p[1] == opchar)
4305 {
4306 garray_T *instr = &cctx->ctx_instr;
4307 garray_T end_ga;
4308
4309 /*
4310 * Repeat until there is no following "||" or "&&"
4311 */
4312 ga_init2(&end_ga, sizeof(int), 10);
4313 while (p[0] == opchar && p[1] == opchar)
4314 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004315 if (next != NULL)
4316 {
4317 *arg = next_line_from_context(cctx, TRUE);
4318 p = skipwhite(*arg);
4319 }
4320
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004321 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4322 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004324 return FAIL;
4325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326
Bram Moolenaara5565e42020-05-09 15:44:01 +02004327 // TODO: use ppconst if the value is a constant
4328 generate_ppconst(cctx, ppconst);
4329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 if (ga_grow(&end_ga, 1) == FAIL)
4331 {
4332 ga_clear(&end_ga);
4333 return FAIL;
4334 }
4335 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4336 ++end_ga.ga_len;
4337 generate_JUMP(cctx, opchar == '|'
4338 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4339
4340 // eval the next expression
4341 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004342 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004343 return FAIL;
4344
Bram Moolenaara5565e42020-05-09 15:44:01 +02004345 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4346 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 {
4348 ga_clear(&end_ga);
4349 return FAIL;
4350 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004351
4352 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004354 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355
4356 // Fill in the end label in all jumps.
4357 while (end_ga.ga_len > 0)
4358 {
4359 isn_T *isn;
4360
4361 --end_ga.ga_len;
4362 isn = ((isn_T *)instr->ga_data)
4363 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4364 isn->isn_arg.jump.jump_where = instr->ga_len;
4365 }
4366 ga_clear(&end_ga);
4367 }
4368
4369 return OK;
4370}
4371
4372/*
4373 * expr4a && expr4a && expr4a logical AND
4374 *
4375 * Produces instructions:
4376 * EVAL expr4a Push result of "expr4a"
4377 * JUMP_AND_KEEP_IF_FALSE end
4378 * EVAL expr4b Push result of "expr4b"
4379 * JUMP_AND_KEEP_IF_FALSE end
4380 * EVAL expr4c Push result of "expr4c"
4381 * end:
4382 */
4383 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004384compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004386 int ppconst_used = ppconst->pp_used;
4387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004389 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 return FAIL;
4391
4392 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004393 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394}
4395
4396/*
4397 * expr3a || expr3b || expr3c logical OR
4398 *
4399 * Produces instructions:
4400 * EVAL expr3a Push result of "expr3a"
4401 * JUMP_AND_KEEP_IF_TRUE end
4402 * EVAL expr3b Push result of "expr3b"
4403 * JUMP_AND_KEEP_IF_TRUE end
4404 * EVAL expr3c Push result of "expr3c"
4405 * end:
4406 */
4407 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004408compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004410 int ppconst_used = ppconst->pp_used;
4411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004413 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 return FAIL;
4415
4416 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004417 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418}
4419
4420/*
4421 * Toplevel expression: expr2 ? expr1a : expr1b
4422 *
4423 * Produces instructions:
4424 * EVAL expr2 Push result of "expr"
4425 * JUMP_IF_FALSE alt jump if false
4426 * EVAL expr1a
4427 * JUMP_ALWAYS end
4428 * alt: EVAL expr1b
4429 * end:
4430 */
4431 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004432compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433{
4434 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004435 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004436 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437
Bram Moolenaar61a89812020-05-07 16:58:17 +02004438 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004439 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 return FAIL;
4441
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004442 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 if (*p == '?')
4444 {
4445 garray_T *instr = &cctx->ctx_instr;
4446 garray_T *stack = &cctx->ctx_type_stack;
4447 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004448 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004450 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004452 int has_const_expr = FALSE;
4453 int const_value = FALSE;
4454 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004456 if (next != NULL)
4457 {
4458 *arg = next_line_from_context(cctx, TRUE);
4459 p = skipwhite(*arg);
4460 }
4461
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004462 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4463 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004465 return FAIL;
4466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467
Bram Moolenaara5565e42020-05-09 15:44:01 +02004468 if (ppconst->pp_used == ppconst_used + 1)
4469 {
4470 // the condition is a constant, we know whether the ? or the :
4471 // expression is to be evaluated.
4472 has_const_expr = TRUE;
4473 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4474 clear_tv(&ppconst->pp_tv[ppconst_used]);
4475 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004476 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4477 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004478 }
4479 else
4480 {
4481 generate_ppconst(cctx, ppconst);
4482 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484
4485 // evaluate the second expression; any type is accepted
4486 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004487 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004488 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004489 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004490 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491
Bram Moolenaara5565e42020-05-09 15:44:01 +02004492 if (!has_const_expr)
4493 {
4494 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495
Bram Moolenaara5565e42020-05-09 15:44:01 +02004496 // remember the type and drop it
4497 --stack->ga_len;
4498 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499
Bram Moolenaara5565e42020-05-09 15:44:01 +02004500 end_idx = instr->ga_len;
4501 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4502
4503 // jump here from JUMP_IF_FALSE
4504 isn = ((isn_T *)instr->ga_data) + alt_idx;
4505 isn->isn_arg.jump.jump_where = instr->ga_len;
4506 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507
4508 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004509 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 if (*p != ':')
4511 {
4512 emsg(_(e_missing_colon));
4513 return FAIL;
4514 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004515 if (next != NULL)
4516 {
4517 *arg = next_line_from_context(cctx, TRUE);
4518 p = skipwhite(*arg);
4519 }
4520
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004521 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4522 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004524 return FAIL;
4525 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526
4527 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004528 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004529 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4530 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004532 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004533 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004534 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004535 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536
Bram Moolenaara5565e42020-05-09 15:44:01 +02004537 if (!has_const_expr)
4538 {
4539 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540
Bram Moolenaara5565e42020-05-09 15:44:01 +02004541 // If the types differ, the result has a more generic type.
4542 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4543 common_type(type1, type2, &type2, cctx->ctx_type_list);
4544
4545 // jump here from JUMP_ALWAYS
4546 isn = ((isn_T *)instr->ga_data) + end_idx;
4547 isn->isn_arg.jump.jump_where = instr->ga_len;
4548 }
4549
4550 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 }
4552 return OK;
4553}
4554
4555/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004556 * Toplevel expression.
4557 */
4558 static int
4559compile_expr0(char_u **arg, cctx_T *cctx)
4560{
4561 ppconst_T ppconst;
4562
4563 CLEAR_FIELD(ppconst);
4564 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4565 {
4566 clear_ppconst(&ppconst);
4567 return FAIL;
4568 }
4569 if (generate_ppconst(cctx, &ppconst) == FAIL)
4570 return FAIL;
4571 return OK;
4572}
4573
4574/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 * compile "return [expr]"
4576 */
4577 static char_u *
4578compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4579{
4580 char_u *p = arg;
4581 garray_T *stack = &cctx->ctx_type_stack;
4582 type_T *stack_type;
4583
4584 if (*p != NUL && *p != '|' && *p != '\n')
4585 {
4586 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004587 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 return NULL;
4589
4590 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4591 if (set_return_type)
4592 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004593 else
4594 {
4595 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4596 && stack_type->tt_type != VAR_VOID
4597 && stack_type->tt_type != VAR_UNKNOWN)
4598 {
4599 emsg(_("E1096: Returning a value in a function without a return type"));
4600 return NULL;
4601 }
4602 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 == FAIL)
4604 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 }
4607 else
4608 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004609 // "set_return_type" cannot be TRUE, only used for a lambda which
4610 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004611 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4612 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 {
4614 emsg(_("E1003: Missing return value"));
4615 return NULL;
4616 }
4617
4618 // No argument, return zero.
4619 generate_PUSHNR(cctx, 0);
4620 }
4621
4622 if (generate_instr(cctx, ISN_RETURN) == NULL)
4623 return NULL;
4624
4625 // "return val | endif" is possible
4626 return skipwhite(p);
4627}
4628
4629/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004630 * Get a line from the compilation context, compatible with exarg_T getline().
4631 * Return a pointer to the line in allocated memory.
4632 * Return NULL for end-of-file or some error.
4633 */
4634 static char_u *
4635exarg_getline(
4636 int c UNUSED,
4637 void *cookie,
4638 int indent UNUSED,
4639 int do_concat UNUSED)
4640{
4641 cctx_T *cctx = (cctx_T *)cookie;
4642
4643 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4644 {
4645 iemsg("Heredoc got to end");
4646 return NULL;
4647 }
4648 ++cctx->ctx_lnum;
4649 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4650 [cctx->ctx_lnum]);
4651}
4652
4653/*
4654 * Compile a nested :def command.
4655 */
4656 static char_u *
4657compile_nested_function(exarg_T *eap, cctx_T *cctx)
4658{
4659 char_u *name_start = eap->arg;
4660 char_u *name_end = to_name_end(eap->arg, FALSE);
4661 char_u *name = get_lambda_name();
4662 lvar_T *lvar;
4663 ufunc_T *ufunc;
4664
4665 eap->arg = name_end;
4666 eap->getline = exarg_getline;
4667 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004668 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004669 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004670 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004671
Bram Moolenaar822ba242020-05-24 23:00:18 +02004672 if (ufunc == NULL)
4673 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004674 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004675 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004676 return NULL;
4677
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004678 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004679 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004680 TRUE, ufunc->uf_func_type);
4681
4682 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4683 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4684 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004685
Bram Moolenaar61a89812020-05-07 16:58:17 +02004686 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004687 return (char_u *)"";
4688}
4689
4690/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 * Return the length of an assignment operator, or zero if there isn't one.
4692 */
4693 int
4694assignment_len(char_u *p, int *heredoc)
4695{
4696 if (*p == '=')
4697 {
4698 if (p[1] == '<' && p[2] == '<')
4699 {
4700 *heredoc = TRUE;
4701 return 3;
4702 }
4703 return 1;
4704 }
4705 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4706 return 2;
4707 if (STRNCMP(p, "..=", 3) == 0)
4708 return 3;
4709 return 0;
4710}
4711
4712// words that cannot be used as a variable
4713static char *reserved[] = {
4714 "true",
4715 "false",
4716 NULL
4717};
4718
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004719typedef enum {
4720 dest_local,
4721 dest_option,
4722 dest_env,
4723 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004724 dest_buffer,
4725 dest_window,
4726 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004727 dest_vimvar,
4728 dest_script,
4729 dest_reg,
4730} assign_dest_T;
4731
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004733 * Generate the load instruction for "name".
4734 */
4735 static void
4736generate_loadvar(
4737 cctx_T *cctx,
4738 assign_dest_T dest,
4739 char_u *name,
4740 lvar_T *lvar,
4741 type_T *type)
4742{
4743 switch (dest)
4744 {
4745 case dest_option:
4746 // TODO: check the option exists
4747 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4748 break;
4749 case dest_global:
4750 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4751 break;
4752 case dest_buffer:
4753 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4754 break;
4755 case dest_window:
4756 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4757 break;
4758 case dest_tab:
4759 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4760 break;
4761 case dest_script:
4762 compile_load_scriptvar(cctx,
4763 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4764 break;
4765 case dest_env:
4766 // Include $ in the name here
4767 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4768 break;
4769 case dest_reg:
4770 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4771 break;
4772 case dest_vimvar:
4773 generate_LOADV(cctx, name + 2, TRUE);
4774 break;
4775 case dest_local:
4776 if (lvar->lv_from_outer)
4777 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4778 NULL, type);
4779 else
4780 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4781 break;
4782 }
4783}
4784
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004785 void
4786vim9_declare_error(char_u *name)
4787{
4788 char *scope = "";
4789
4790 switch (*name)
4791 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004792 case 'g': scope = _("global"); break;
4793 case 'b': scope = _("buffer"); break;
4794 case 'w': scope = _("window"); break;
4795 case 't': scope = _("tab"); break;
4796 case 'v': scope = "v:"; break;
4797 case '$': semsg(_(e_declare_env_var), name); return;
4798 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004799 }
4800 semsg(_(e_declare_var), scope, name);
4801}
4802
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004803/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004804 * Compile declaration and assignment:
4805 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004807 * Return NULL for an error.
4808 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 */
4810 static char_u *
4811compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4812{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004813 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004815 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 char_u *ret = NULL;
4817 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004818 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004821 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 int oplen = 0;
4824 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004825 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004826 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004827 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004831 // Skip over the "var" or "[var, var]" to get to any "=".
4832 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4833 if (p == NULL)
4834 return *arg == '[' ? arg : NULL;
4835
4836 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004838 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 return NULL;
4840 }
4841
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 sp = p;
4843 p = skipwhite(p);
4844 op = p;
4845 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004846
4847 if (var_count > 0 && oplen == 0)
4848 // can be something like "[1, 2]->func()"
4849 return arg;
4850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4852 {
4853 char_u buf[4];
4854
4855 vim_strncpy(buf, op, oplen);
4856 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004857 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004858 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 if (heredoc)
4861 {
4862 list_T *l;
4863 listitem_T *li;
4864
4865 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004866 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004868 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869
4870 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004871 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 {
4873 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4874 li->li_tv.vval.v_string = NULL;
4875 }
4876 generate_NEWLIST(cctx, l->lv_len);
4877 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004878 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879 list_free(l);
4880 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004881 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004883 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004885 // for "[var, var] = expr" evaluate the expression here, loop over the
4886 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004887
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004888 p = skipwhite(op + oplen);
4889 if (compile_expr0(&p, cctx) == FAIL)
4890 return NULL;
4891 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004893 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004895 type_T *stacktype;
4896
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004897 stacktype = stack->ga_len == 0 ? &t_void
4898 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004899 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004901 emsg(_(e_cannot_use_void));
4902 goto theend;
4903 }
4904 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4905 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004906 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4907 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004908 }
4909 }
4910
4911 /*
4912 * Loop over variables in "[var, var] = expr".
4913 * For "var = expr" and "let var: type" this is done only once.
4914 */
4915 if (var_count > 0)
4916 var_start = skipwhite(arg + 1); // skip over the "["
4917 else
4918 var_start = arg;
4919 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4920 {
4921 char_u *var_end = skip_var_one(var_start, FALSE);
4922 size_t varlen;
4923 int new_local = FALSE;
4924 int opt_type;
4925 int opt_flags = 0;
4926 assign_dest_T dest = dest_local;
4927 int vimvaridx = -1;
4928 lvar_T *lvar = NULL;
4929 lvar_T arg_lvar;
4930 int has_type = FALSE;
4931 int has_index = FALSE;
4932 int instr_count = -1;
4933
4934 p = (*var_start == '&' || *var_start == '$'
4935 || *var_start == '@') ? var_start + 1 : var_start;
4936 p = to_name_end(p, TRUE);
4937
4938 // "a: type" is declaring variable "a" with a type, not "a:".
4939 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4940 --var_end;
4941 if (is_decl && p == var_start + 2 && p[-1] == ':')
4942 --p;
4943
4944 varlen = p - var_start;
4945 vim_free(name);
4946 name = vim_strnsave(var_start, varlen);
4947 if (name == NULL)
4948 return NULL;
4949 if (!heredoc)
4950 type = &t_any;
4951
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004952 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004953 {
4954 if (*var_start == '&')
4955 {
4956 int cc;
4957 long numval;
4958
4959 dest = dest_option;
4960 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004962 emsg(_(e_const_option));
4963 goto theend;
4964 }
4965 if (is_decl)
4966 {
4967 semsg(_("E1052: Cannot declare an option: %s"), var_start);
4968 goto theend;
4969 }
4970 p = var_start;
4971 p = find_option_end(&p, &opt_flags);
4972 if (p == NULL)
4973 {
4974 // cannot happen?
4975 emsg(_(e_letunexp));
4976 goto theend;
4977 }
4978 cc = *p;
4979 *p = NUL;
4980 opt_type = get_option_value(var_start + 1, &numval,
4981 NULL, opt_flags);
4982 *p = cc;
4983 if (opt_type == -3)
4984 {
4985 semsg(_(e_unknown_option), var_start);
4986 goto theend;
4987 }
4988 if (opt_type == -2 || opt_type == 0)
4989 type = &t_string;
4990 else
4991 type = &t_number; // both number and boolean option
4992 }
4993 else if (*var_start == '$')
4994 {
4995 dest = dest_env;
4996 type = &t_string;
4997 if (is_decl)
4998 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004999 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005000 goto theend;
5001 }
5002 }
5003 else if (*var_start == '@')
5004 {
5005 if (!valid_yank_reg(var_start[1], TRUE))
5006 {
5007 emsg_invreg(var_start[1]);
5008 goto theend;
5009 }
5010 dest = dest_reg;
5011 type = &t_string;
5012 if (is_decl)
5013 {
5014 semsg(_("E1066: Cannot declare a register: %s"), name);
5015 goto theend;
5016 }
5017 }
5018 else if (STRNCMP(var_start, "g:", 2) == 0)
5019 {
5020 dest = dest_global;
5021 if (is_decl)
5022 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005023 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005024 goto theend;
5025 }
5026 }
5027 else if (STRNCMP(var_start, "b:", 2) == 0)
5028 {
5029 dest = dest_buffer;
5030 if (is_decl)
5031 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005032 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005033 goto theend;
5034 }
5035 }
5036 else if (STRNCMP(var_start, "w:", 2) == 0)
5037 {
5038 dest = dest_window;
5039 if (is_decl)
5040 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005041 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005042 goto theend;
5043 }
5044 }
5045 else if (STRNCMP(var_start, "t:", 2) == 0)
5046 {
5047 dest = dest_tab;
5048 if (is_decl)
5049 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005050 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005051 goto theend;
5052 }
5053 }
5054 else if (STRNCMP(var_start, "v:", 2) == 0)
5055 {
5056 typval_T *vtv;
5057 int di_flags;
5058
5059 vimvaridx = find_vim_var(name + 2, &di_flags);
5060 if (vimvaridx < 0)
5061 {
5062 semsg(_(e_var_notfound), var_start);
5063 goto theend;
5064 }
5065 // We use the current value of "sandbox" here, is that OK?
5066 if (var_check_ro(di_flags, name, FALSE))
5067 goto theend;
5068 dest = dest_vimvar;
5069 vtv = get_vim_var_tv(vimvaridx);
5070 type = typval2type(vtv);
5071 if (is_decl)
5072 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005073 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005074 goto theend;
5075 }
5076 }
5077 else
5078 {
5079 int idx;
5080
5081 for (idx = 0; reserved[idx] != NULL; ++idx)
5082 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005083 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005085 goto theend;
5086 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005087
5088 lvar = lookup_local(var_start, varlen, cctx);
5089 if (lvar == NULL)
5090 {
5091 CLEAR_FIELD(arg_lvar);
5092 if (lookup_arg(var_start, varlen,
5093 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5094 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005095 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005096 if (is_decl)
5097 {
5098 semsg(_(e_used_as_arg), name);
5099 goto theend;
5100 }
5101 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005102 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005103 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005104 if (lvar != NULL)
5105 {
5106 if (is_decl)
5107 {
5108 semsg(_("E1017: Variable already declared: %s"), name);
5109 goto theend;
5110 }
5111 else if (lvar->lv_const)
5112 {
5113 semsg(_("E1018: Cannot assign to a constant: %s"),
5114 name);
5115 goto theend;
5116 }
5117 }
5118 else if (STRNCMP(var_start, "s:", 2) == 0
5119 || lookup_script(var_start, varlen) == OK
5120 || find_imported(var_start, varlen, cctx) != NULL)
5121 {
5122 dest = dest_script;
5123 if (is_decl)
5124 {
5125 semsg(_("E1054: Variable already declared in the script: %s"),
5126 name);
5127 goto theend;
5128 }
5129 }
5130 else if (name[1] == ':' && name[2] != NUL)
5131 {
5132 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5133 name);
5134 goto theend;
5135 }
5136 else if (!is_decl)
5137 {
5138 semsg(_("E1089: unknown variable: %s"), name);
5139 goto theend;
5140 }
5141 }
5142 }
5143
5144 // handle "a:name" as a name, not index "name" on "a"
5145 if (varlen > 1 || var_start[varlen] != ':')
5146 p = var_end;
5147
5148 if (dest != dest_option)
5149 {
5150 if (is_decl && *p == ':')
5151 {
5152 // parse optional type: "let var: type = expr"
5153 if (!VIM_ISWHITE(p[1]))
5154 {
5155 semsg(_(e_white_after), ":");
5156 goto theend;
5157 }
5158 p = skipwhite(p + 1);
5159 type = parse_type(&p, cctx->ctx_type_list);
5160 has_type = TRUE;
5161 }
5162 else if (lvar != NULL)
5163 type = lvar->lv_type;
5164 }
5165
5166 if (oplen == 3 && !heredoc && dest != dest_global
5167 && type->tt_type != VAR_STRING
5168 && type->tt_type != VAR_ANY)
5169 {
5170 emsg(_("E1019: Can only concatenate to string"));
5171 goto theend;
5172 }
5173
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005174 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005175 {
5176 if (oplen > 1 && !heredoc)
5177 {
5178 // +=, /=, etc. require an existing variable
5179 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5180 name);
5181 goto theend;
5182 }
5183
5184 // new local variable
5185 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5186 goto theend;
5187 lvar = reserve_local(cctx, var_start, varlen,
5188 cmdidx == CMD_const, type);
5189 if (lvar == NULL)
5190 goto theend;
5191 new_local = TRUE;
5192 }
5193
5194 member_type = type;
5195 if (var_end > var_start + varlen)
5196 {
5197 // Something follows after the variable: "var[idx]".
5198 if (is_decl)
5199 {
5200 emsg(_("E1087: cannot use an index when declaring a variable"));
5201 goto theend;
5202 }
5203
5204 if (var_start[varlen] == '[')
5205 {
5206 has_index = TRUE;
5207 if (type->tt_member == NULL)
5208 {
5209 semsg(_("E1088: cannot use an index on %s"), name);
5210 goto theend;
5211 }
5212 member_type = type->tt_member;
5213 }
5214 else
5215 {
5216 semsg("Not supported yet: %s", var_start);
5217 goto theend;
5218 }
5219 }
5220 else if (lvar == &arg_lvar)
5221 {
5222 semsg(_("E1090: Cannot assign to argument %s"), name);
5223 goto theend;
5224 }
5225
5226 if (!heredoc)
5227 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005228 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005229 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005230 if (oplen > 0 && var_count == 0)
5231 {
5232 // skip over the "=" and the expression
5233 p = skipwhite(op + oplen);
5234 compile_expr0(&p, cctx);
5235 }
5236 }
5237 else if (oplen > 0)
5238 {
5239 type_T *stacktype;
5240
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005241 // For "var = expr" evaluate the expression.
5242 if (var_count == 0)
5243 {
5244 int r;
5245
5246 // for "+=", "*=", "..=" etc. first load the current value
5247 if (*op != '=')
5248 {
5249 generate_loadvar(cctx, dest, name, lvar, type);
5250
5251 if (has_index)
5252 {
5253 // TODO: get member from list or dict
5254 emsg("Index with operation not supported yet");
5255 goto theend;
5256 }
5257 }
5258
5259 // Compile the expression. Temporarily hide the new local
5260 // variable here, it is not available to this expression.
5261 if (new_local)
5262 --cctx->ctx_locals.ga_len;
5263 instr_count = instr->ga_len;
5264 p = skipwhite(op + oplen);
5265 r = compile_expr0(&p, cctx);
5266 if (new_local)
5267 ++cctx->ctx_locals.ga_len;
5268 if (r == FAIL)
5269 goto theend;
5270 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005271 else if (semicolon && var_idx == var_count - 1)
5272 {
5273 // For "[var; var] = expr" get the rest of the list
5274 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5275 goto theend;
5276 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005277 else
5278 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005279 // For "[var, var] = expr" get the "var_idx" item from the
5280 // list.
5281 if (generate_GETITEM(cctx, var_idx) == FAIL)
5282 return FAIL;
5283 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005284
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005285 stacktype = stack->ga_len == 0 ? &t_void
5286 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5287 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005289 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005290 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005291 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005292 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005293 emsg(_(e_cannot_use_void));
5294 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005295 }
5296 else
5297 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005298 // An empty list or dict has a &t_void member,
5299 // for a variable that implies &t_any.
5300 if (stacktype == &t_list_empty)
5301 lvar->lv_type = &t_list_any;
5302 else if (stacktype == &t_dict_empty)
5303 lvar->lv_type = &t_dict_any;
5304 else
5305 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005306 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005307 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005308 else
5309 {
5310 type_T *use_type = lvar->lv_type;
5311
5312 if (has_index)
5313 {
5314 use_type = use_type->tt_member;
5315 if (use_type == NULL)
5316 use_type = &t_void;
5317 }
5318 if (need_type(stacktype, use_type, -1, cctx)
5319 == FAIL)
5320 goto theend;
5321 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005322 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005323 else if (*p != '=' && need_type(stacktype, member_type, -1,
5324 cctx) == FAIL)
5325 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005326 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005327 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005329 emsg(_(e_const_req_value));
5330 goto theend;
5331 }
5332 else if (!has_type || dest == dest_option)
5333 {
5334 emsg(_(e_type_req));
5335 goto theend;
5336 }
5337 else
5338 {
5339 // variables are always initialized
5340 if (ga_grow(instr, 1) == FAIL)
5341 goto theend;
5342 switch (member_type->tt_type)
5343 {
5344 case VAR_BOOL:
5345 generate_PUSHBOOL(cctx, VVAL_FALSE);
5346 break;
5347 case VAR_FLOAT:
5348#ifdef FEAT_FLOAT
5349 generate_PUSHF(cctx, 0.0);
5350#endif
5351 break;
5352 case VAR_STRING:
5353 generate_PUSHS(cctx, NULL);
5354 break;
5355 case VAR_BLOB:
5356 generate_PUSHBLOB(cctx, NULL);
5357 break;
5358 case VAR_FUNC:
5359 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5360 break;
5361 case VAR_LIST:
5362 generate_NEWLIST(cctx, 0);
5363 break;
5364 case VAR_DICT:
5365 generate_NEWDICT(cctx, 0);
5366 break;
5367 case VAR_JOB:
5368 generate_PUSHJOB(cctx, NULL);
5369 break;
5370 case VAR_CHANNEL:
5371 generate_PUSHCHANNEL(cctx, NULL);
5372 break;
5373 case VAR_NUMBER:
5374 case VAR_UNKNOWN:
5375 case VAR_ANY:
5376 case VAR_PARTIAL:
5377 case VAR_VOID:
5378 case VAR_SPECIAL: // cannot happen
5379 generate_PUSHNR(cctx, 0);
5380 break;
5381 }
5382 }
5383 if (var_count == 0)
5384 end = p;
5385 }
5386
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005387 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005388 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005389 break;
5390
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005391 if (oplen > 0 && *op != '=')
5392 {
5393 type_T *expected = &t_number;
5394 type_T *stacktype;
5395
5396 // TODO: if type is known use float or any operation
5397
5398 if (*op == '.')
5399 expected = &t_string;
5400 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5401 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5402 goto theend;
5403
5404 if (*op == '.')
5405 generate_instr_drop(cctx, ISN_CONCAT, 1);
5406 else
5407 {
5408 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5409
5410 if (isn == NULL)
5411 goto theend;
5412 switch (*op)
5413 {
5414 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5415 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5416 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5417 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5418 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005420 }
5421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005423 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005424 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005425 int r;
5426
5427 // Compile the "idx" in "var[idx]".
5428 if (new_local)
5429 --cctx->ctx_locals.ga_len;
5430 p = skipwhite(var_start + varlen + 1);
5431 r = compile_expr0(&p, cctx);
5432 if (new_local)
5433 ++cctx->ctx_locals.ga_len;
5434 if (r == FAIL)
5435 goto theend;
5436 if (*skipwhite(p) != ']')
5437 {
5438 emsg(_(e_missbrac));
5439 goto theend;
5440 }
5441 if (type->tt_type == VAR_DICT
5442 && may_generate_2STRING(-1, cctx) == FAIL)
5443 goto theend;
5444 if (type->tt_type == VAR_LIST
5445 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005446 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005447 {
5448 emsg(_(e_number_exp));
5449 goto theend;
5450 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005451
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005452 // Load the dict or list. On the stack we then have:
5453 // - value
5454 // - index
5455 // - variable
5456 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005457
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005458 if (type->tt_type == VAR_LIST)
5459 {
5460 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5461 return FAIL;
5462 }
5463 else if (type->tt_type == VAR_DICT)
5464 {
5465 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5466 return FAIL;
5467 }
5468 else
5469 {
5470 emsg(_(e_listreq));
5471 goto theend;
5472 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005473 }
5474 else
5475 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005476 switch (dest)
5477 {
5478 case dest_option:
5479 generate_STOREOPT(cctx, name + 1, opt_flags);
5480 break;
5481 case dest_global:
5482 // include g: with the name, easier to execute that way
5483 generate_STORE(cctx, ISN_STOREG, 0, name);
5484 break;
5485 case dest_buffer:
5486 // include b: with the name, easier to execute that way
5487 generate_STORE(cctx, ISN_STOREB, 0, name);
5488 break;
5489 case dest_window:
5490 // include w: with the name, easier to execute that way
5491 generate_STORE(cctx, ISN_STOREW, 0, name);
5492 break;
5493 case dest_tab:
5494 // include t: with the name, easier to execute that way
5495 generate_STORE(cctx, ISN_STORET, 0, name);
5496 break;
5497 case dest_env:
5498 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5499 break;
5500 case dest_reg:
5501 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5502 break;
5503 case dest_vimvar:
5504 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5505 break;
5506 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005507 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005508 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5509 imported_T *import = NULL;
5510 int sid = current_sctx.sc_sid;
5511 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005512
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005513 if (name[1] != ':')
5514 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005515 import = find_imported(name, 0, cctx);
5516 if (import != NULL)
5517 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005518 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005519
5520 idx = get_script_item_idx(sid, rawname, TRUE);
5521 // TODO: specific type
5522 if (idx < 0)
5523 {
5524 char_u *name_s = name;
5525
5526 // Include s: in the name for store_var()
5527 if (name[1] != ':')
5528 {
5529 int len = (int)STRLEN(name) + 3;
5530
5531 name_s = alloc(len);
5532 if (name_s == NULL)
5533 name_s = name;
5534 else
5535 vim_snprintf((char *)name_s, len,
5536 "s:%s", name);
5537 }
5538 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005539 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005540 if (name_s != name)
5541 vim_free(name_s);
5542 }
5543 else
5544 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005545 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005546 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005547 break;
5548 case dest_local:
5549 if (lvar != NULL)
5550 {
5551 isn_T *isn = ((isn_T *)instr->ga_data)
5552 + instr->ga_len - 1;
5553
5554 // optimization: turn "var = 123" from ISN_PUSHNR +
5555 // ISN_STORE into ISN_STORENR
5556 if (!lvar->lv_from_outer
5557 && instr->ga_len == instr_count + 1
5558 && isn->isn_type == ISN_PUSHNR)
5559 {
5560 varnumber_T val = isn->isn_arg.number;
5561
5562 isn->isn_type = ISN_STORENR;
5563 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5564 isn->isn_arg.storenr.stnr_val = val;
5565 if (stack->ga_len > 0)
5566 --stack->ga_len;
5567 }
5568 else if (lvar->lv_from_outer)
5569 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005570 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005571 else
5572 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5573 }
5574 break;
5575 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005576 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005577
5578 if (var_idx + 1 < var_count)
5579 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005580 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005581
5582 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005583 if (var_count > 0 && !semicolon)
5584 {
5585 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5586 goto theend;
5587 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005588
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005589 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590
5591theend:
5592 vim_free(name);
5593 return ret;
5594}
5595
5596/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005597 * Check if "name" can be "unlet".
5598 */
5599 int
5600check_vim9_unlet(char_u *name)
5601{
5602 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5603 {
5604 semsg(_("E1081: Cannot unlet %s"), name);
5605 return FAIL;
5606 }
5607 return OK;
5608}
5609
5610/*
5611 * Callback passed to ex_unletlock().
5612 */
5613 static int
5614compile_unlet(
5615 lval_T *lvp,
5616 char_u *name_end,
5617 exarg_T *eap,
5618 int deep UNUSED,
5619 void *coookie)
5620{
5621 cctx_T *cctx = coookie;
5622
5623 if (lvp->ll_tv == NULL)
5624 {
5625 char_u *p = lvp->ll_name;
5626 int cc = *name_end;
5627 int ret = OK;
5628
5629 // Normal name. Only supports g:, w:, t: and b: namespaces.
5630 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005631 if (*p == '$')
5632 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5633 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005634 ret = FAIL;
5635 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005636 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005637
5638 *name_end = cc;
5639 return ret;
5640 }
5641
5642 // TODO: unlet {list}[idx]
5643 // TODO: unlet {dict}[key]
5644 emsg("Sorry, :unlet not fully implemented yet");
5645 return FAIL;
5646}
5647
5648/*
5649 * compile "unlet var", "lock var" and "unlock var"
5650 * "arg" points to "var".
5651 */
5652 static char_u *
5653compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5654{
5655 char_u *p = arg;
5656
5657 if (eap->cmdidx != CMD_unlet)
5658 {
5659 emsg("Sorry, :lock and unlock not implemented yet");
5660 return NULL;
5661 }
5662
5663 if (*p == '!')
5664 {
5665 p = skipwhite(p + 1);
5666 eap->forceit = TRUE;
5667 }
5668
5669 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5670 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5671}
5672
5673/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674 * Compile an :import command.
5675 */
5676 static char_u *
5677compile_import(char_u *arg, cctx_T *cctx)
5678{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005679 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005680}
5681
5682/*
5683 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5684 */
5685 static int
5686compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5687{
5688 garray_T *instr = &cctx->ctx_instr;
5689 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5690
5691 if (endlabel == NULL)
5692 return FAIL;
5693 endlabel->el_next = *el;
5694 *el = endlabel;
5695 endlabel->el_end_label = instr->ga_len;
5696
5697 generate_JUMP(cctx, when, 0);
5698 return OK;
5699}
5700
5701 static void
5702compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5703{
5704 garray_T *instr = &cctx->ctx_instr;
5705
5706 while (*el != NULL)
5707 {
5708 endlabel_T *cur = (*el);
5709 isn_T *isn;
5710
5711 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5712 isn->isn_arg.jump.jump_where = instr->ga_len;
5713 *el = cur->el_next;
5714 vim_free(cur);
5715 }
5716}
5717
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005718 static void
5719compile_free_jump_to_end(endlabel_T **el)
5720{
5721 while (*el != NULL)
5722 {
5723 endlabel_T *cur = (*el);
5724
5725 *el = cur->el_next;
5726 vim_free(cur);
5727 }
5728}
5729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005730/*
5731 * Create a new scope and set up the generic items.
5732 */
5733 static scope_T *
5734new_scope(cctx_T *cctx, scopetype_T type)
5735{
5736 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5737
5738 if (scope == NULL)
5739 return NULL;
5740 scope->se_outer = cctx->ctx_scope;
5741 cctx->ctx_scope = scope;
5742 scope->se_type = type;
5743 scope->se_local_count = cctx->ctx_locals.ga_len;
5744 return scope;
5745}
5746
5747/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005748 * Free the current scope and go back to the outer scope.
5749 */
5750 static void
5751drop_scope(cctx_T *cctx)
5752{
5753 scope_T *scope = cctx->ctx_scope;
5754
5755 if (scope == NULL)
5756 {
5757 iemsg("calling drop_scope() without a scope");
5758 return;
5759 }
5760 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005761 switch (scope->se_type)
5762 {
5763 case IF_SCOPE:
5764 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5765 case FOR_SCOPE:
5766 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5767 case WHILE_SCOPE:
5768 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5769 case TRY_SCOPE:
5770 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5771 case NO_SCOPE:
5772 case BLOCK_SCOPE:
5773 break;
5774 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005775 vim_free(scope);
5776}
5777
5778/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005779 * compile "if expr"
5780 *
5781 * "if expr" Produces instructions:
5782 * EVAL expr Push result of "expr"
5783 * JUMP_IF_FALSE end
5784 * ... body ...
5785 * end:
5786 *
5787 * "if expr | else" Produces instructions:
5788 * EVAL expr Push result of "expr"
5789 * JUMP_IF_FALSE else
5790 * ... body ...
5791 * JUMP_ALWAYS end
5792 * else:
5793 * ... body ...
5794 * end:
5795 *
5796 * "if expr1 | elseif expr2 | else" Produces instructions:
5797 * EVAL expr Push result of "expr"
5798 * JUMP_IF_FALSE elseif
5799 * ... body ...
5800 * JUMP_ALWAYS end
5801 * elseif:
5802 * EVAL expr Push result of "expr"
5803 * JUMP_IF_FALSE else
5804 * ... body ...
5805 * JUMP_ALWAYS end
5806 * else:
5807 * ... body ...
5808 * end:
5809 */
5810 static char_u *
5811compile_if(char_u *arg, cctx_T *cctx)
5812{
5813 char_u *p = arg;
5814 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005815 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005816 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005817 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005818 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005819
Bram Moolenaara5565e42020-05-09 15:44:01 +02005820 CLEAR_FIELD(ppconst);
5821 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005822 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005823 clear_ppconst(&ppconst);
5824 return NULL;
5825 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005826 if (cctx->ctx_skip == SKIP_YES)
5827 clear_ppconst(&ppconst);
5828 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005829 {
5830 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005831 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005832 clear_ppconst(&ppconst);
5833 }
5834 else
5835 {
5836 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005837 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005838 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005839 return NULL;
5840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005841
5842 scope = new_scope(cctx, IF_SCOPE);
5843 if (scope == NULL)
5844 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005845 scope->se_skip_save = skip_save;
5846 // "is_had_return" will be reset if any block does not end in :return
5847 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005849 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005850 {
5851 // "where" is set when ":elseif", "else" or ":endif" is found
5852 scope->se_u.se_if.is_if_label = instr->ga_len;
5853 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5854 }
5855 else
5856 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857
5858 return p;
5859}
5860
5861 static char_u *
5862compile_elseif(char_u *arg, cctx_T *cctx)
5863{
5864 char_u *p = arg;
5865 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005866 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005867 isn_T *isn;
5868 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005869 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870
5871 if (scope == NULL || scope->se_type != IF_SCOPE)
5872 {
5873 emsg(_(e_elseif_without_if));
5874 return NULL;
5875 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005876 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005877 if (!cctx->ctx_had_return)
5878 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005879
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005880 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005881 {
5882 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005884 return NULL;
5885 // previous "if" or "elseif" jumps here
5886 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5887 isn->isn_arg.jump.jump_where = instr->ga_len;
5888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005889
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005890 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005891 CLEAR_FIELD(ppconst);
5892 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005893 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005894 clear_ppconst(&ppconst);
5895 return NULL;
5896 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005897 if (scope->se_skip_save == SKIP_YES)
5898 clear_ppconst(&ppconst);
5899 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005900 {
5901 // The expression results in a constant.
5902 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005903 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005904 clear_ppconst(&ppconst);
5905 scope->se_u.se_if.is_if_label = -1;
5906 }
5907 else
5908 {
5909 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005910 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005911 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005912 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005914 // "where" is set when ":elseif", "else" or ":endif" is found
5915 scope->se_u.se_if.is_if_label = instr->ga_len;
5916 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005918
5919 return p;
5920}
5921
5922 static char_u *
5923compile_else(char_u *arg, cctx_T *cctx)
5924{
5925 char_u *p = arg;
5926 garray_T *instr = &cctx->ctx_instr;
5927 isn_T *isn;
5928 scope_T *scope = cctx->ctx_scope;
5929
5930 if (scope == NULL || scope->se_type != IF_SCOPE)
5931 {
5932 emsg(_(e_else_without_if));
5933 return NULL;
5934 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005935 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005936 if (!cctx->ctx_had_return)
5937 scope->se_u.se_if.is_had_return = FALSE;
5938 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005939
Bram Moolenaarefd88552020-06-18 20:50:10 +02005940 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005941 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005942 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005943 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005944 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005945 if (!cctx->ctx_had_return
5946 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5947 JUMP_ALWAYS, cctx) == FAIL)
5948 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005949 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005950
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005951 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005952 {
5953 if (scope->se_u.se_if.is_if_label >= 0)
5954 {
5955 // previous "if" or "elseif" jumps here
5956 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5957 isn->isn_arg.jump.jump_where = instr->ga_len;
5958 scope->se_u.se_if.is_if_label = -1;
5959 }
5960 }
5961
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005962 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005963 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005965
5966 return p;
5967}
5968
5969 static char_u *
5970compile_endif(char_u *arg, cctx_T *cctx)
5971{
5972 scope_T *scope = cctx->ctx_scope;
5973 ifscope_T *ifscope;
5974 garray_T *instr = &cctx->ctx_instr;
5975 isn_T *isn;
5976
5977 if (scope == NULL || scope->se_type != IF_SCOPE)
5978 {
5979 emsg(_(e_endif_without_if));
5980 return NULL;
5981 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005982 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005983 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005984 if (!cctx->ctx_had_return)
5985 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005987 if (scope->se_u.se_if.is_if_label >= 0)
5988 {
5989 // previous "if" or "elseif" jumps here
5990 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5991 isn->isn_arg.jump.jump_where = instr->ga_len;
5992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 // Fill in the "end" label in jumps at the end of the blocks.
5994 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005995 cctx->ctx_skip = scope->se_skip_save;
5996
5997 // If all the blocks end in :return and there is an :else then the
5998 // had_return flag is set.
5999 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006001 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002 return arg;
6003}
6004
6005/*
6006 * compile "for var in expr"
6007 *
6008 * Produces instructions:
6009 * PUSHNR -1
6010 * STORE loop-idx Set index to -1
6011 * EVAL expr Push result of "expr"
6012 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6013 * - if beyond end, jump to "end"
6014 * - otherwise get item from list and push it
6015 * STORE var Store item in "var"
6016 * ... body ...
6017 * JUMP top Jump back to repeat
6018 * end: DROP Drop the result of "expr"
6019 *
6020 */
6021 static char_u *
6022compile_for(char_u *arg, cctx_T *cctx)
6023{
6024 char_u *p;
6025 size_t varlen;
6026 garray_T *instr = &cctx->ctx_instr;
6027 garray_T *stack = &cctx->ctx_type_stack;
6028 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006029 lvar_T *loop_lvar; // loop iteration variable
6030 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031 type_T *vartype;
6032
6033 // TODO: list of variables: "for [key, value] in dict"
6034 // parse "var"
6035 for (p = arg; eval_isnamec1(*p); ++p)
6036 ;
6037 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006038 var_lvar = lookup_local(arg, varlen, cctx);
6039 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006040 {
6041 semsg(_("E1023: variable already defined: %s"), arg);
6042 return NULL;
6043 }
6044
6045 // consume "in"
6046 p = skipwhite(p);
6047 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6048 {
6049 emsg(_(e_missing_in));
6050 return NULL;
6051 }
6052 p = skipwhite(p + 2);
6053
6054
6055 scope = new_scope(cctx, FOR_SCOPE);
6056 if (scope == NULL)
6057 return NULL;
6058
6059 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006060 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6061 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006062 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006063 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006064 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006065 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006067
6068 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006069 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6070 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006071 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006072 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006073 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006077 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006078
6079 // compile "expr", it remains on the stack until "endfor"
6080 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006081 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006082 {
6083 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006084 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006087 // Now that we know the type of "var", check that it is a list, now or at
6088 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006089 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006090 if (need_type(vartype, &t_list_any, -1, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006091 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006092 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006093 return NULL;
6094 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006095 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006096 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006097
6098 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006099 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006100
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006101 generate_FOR(cctx, loop_lvar->lv_idx);
6102 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006103
6104 return arg;
6105}
6106
6107/*
6108 * compile "endfor"
6109 */
6110 static char_u *
6111compile_endfor(char_u *arg, cctx_T *cctx)
6112{
6113 garray_T *instr = &cctx->ctx_instr;
6114 scope_T *scope = cctx->ctx_scope;
6115 forscope_T *forscope;
6116 isn_T *isn;
6117
6118 if (scope == NULL || scope->se_type != FOR_SCOPE)
6119 {
6120 emsg(_(e_for));
6121 return NULL;
6122 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006123 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006124 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006125 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006126
6127 // At end of ":for" scope jump back to the FOR instruction.
6128 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6129
6130 // Fill in the "end" label in the FOR statement so it can jump here
6131 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6132 isn->isn_arg.forloop.for_end = instr->ga_len;
6133
6134 // Fill in the "end" label any BREAK statements
6135 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6136
6137 // Below the ":for" scope drop the "expr" list from the stack.
6138 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6139 return NULL;
6140
6141 vim_free(scope);
6142
6143 return arg;
6144}
6145
6146/*
6147 * compile "while expr"
6148 *
6149 * Produces instructions:
6150 * top: EVAL expr Push result of "expr"
6151 * JUMP_IF_FALSE end jump if false
6152 * ... body ...
6153 * JUMP top Jump back to repeat
6154 * end:
6155 *
6156 */
6157 static char_u *
6158compile_while(char_u *arg, cctx_T *cctx)
6159{
6160 char_u *p = arg;
6161 garray_T *instr = &cctx->ctx_instr;
6162 scope_T *scope;
6163
6164 scope = new_scope(cctx, WHILE_SCOPE);
6165 if (scope == NULL)
6166 return NULL;
6167
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006168 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006169
6170 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006171 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172 return NULL;
6173
6174 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006175 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006176 JUMP_IF_FALSE, cctx) == FAIL)
6177 return FAIL;
6178
6179 return p;
6180}
6181
6182/*
6183 * compile "endwhile"
6184 */
6185 static char_u *
6186compile_endwhile(char_u *arg, cctx_T *cctx)
6187{
6188 scope_T *scope = cctx->ctx_scope;
6189
6190 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6191 {
6192 emsg(_(e_while));
6193 return NULL;
6194 }
6195 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006196 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006197
6198 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006199 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200
6201 // Fill in the "end" label in the WHILE statement so it can jump here.
6202 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006203 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204
6205 vim_free(scope);
6206
6207 return arg;
6208}
6209
6210/*
6211 * compile "continue"
6212 */
6213 static char_u *
6214compile_continue(char_u *arg, cctx_T *cctx)
6215{
6216 scope_T *scope = cctx->ctx_scope;
6217
6218 for (;;)
6219 {
6220 if (scope == NULL)
6221 {
6222 emsg(_(e_continue));
6223 return NULL;
6224 }
6225 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6226 break;
6227 scope = scope->se_outer;
6228 }
6229
6230 // Jump back to the FOR or WHILE instruction.
6231 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006232 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6233 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006234 return arg;
6235}
6236
6237/*
6238 * compile "break"
6239 */
6240 static char_u *
6241compile_break(char_u *arg, cctx_T *cctx)
6242{
6243 scope_T *scope = cctx->ctx_scope;
6244 endlabel_T **el;
6245
6246 for (;;)
6247 {
6248 if (scope == NULL)
6249 {
6250 emsg(_(e_break));
6251 return NULL;
6252 }
6253 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6254 break;
6255 scope = scope->se_outer;
6256 }
6257
6258 // Jump to the end of the FOR or WHILE loop.
6259 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006260 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006262 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6264 return FAIL;
6265
6266 return arg;
6267}
6268
6269/*
6270 * compile "{" start of block
6271 */
6272 static char_u *
6273compile_block(char_u *arg, cctx_T *cctx)
6274{
6275 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6276 return NULL;
6277 return skipwhite(arg + 1);
6278}
6279
6280/*
6281 * compile end of block: drop one scope
6282 */
6283 static void
6284compile_endblock(cctx_T *cctx)
6285{
6286 scope_T *scope = cctx->ctx_scope;
6287
6288 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006289 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290 vim_free(scope);
6291}
6292
6293/*
6294 * compile "try"
6295 * Creates a new scope for the try-endtry, pointing to the first catch and
6296 * finally.
6297 * Creates another scope for the "try" block itself.
6298 * TRY instruction sets up exception handling at runtime.
6299 *
6300 * "try"
6301 * TRY -> catch1, -> finally push trystack entry
6302 * ... try block
6303 * "throw {exception}"
6304 * EVAL {exception}
6305 * THROW create exception
6306 * ... try block
6307 * " catch {expr}"
6308 * JUMP -> finally
6309 * catch1: PUSH exeception
6310 * EVAL {expr}
6311 * MATCH
6312 * JUMP nomatch -> catch2
6313 * CATCH remove exception
6314 * ... catch block
6315 * " catch"
6316 * JUMP -> finally
6317 * catch2: CATCH remove exception
6318 * ... catch block
6319 * " finally"
6320 * finally:
6321 * ... finally block
6322 * " endtry"
6323 * ENDTRY pop trystack entry, may rethrow
6324 */
6325 static char_u *
6326compile_try(char_u *arg, cctx_T *cctx)
6327{
6328 garray_T *instr = &cctx->ctx_instr;
6329 scope_T *try_scope;
6330 scope_T *scope;
6331
6332 // scope that holds the jumps that go to catch/finally/endtry
6333 try_scope = new_scope(cctx, TRY_SCOPE);
6334 if (try_scope == NULL)
6335 return NULL;
6336
6337 // "catch" is set when the first ":catch" is found.
6338 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006339 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006340 if (generate_instr(cctx, ISN_TRY) == NULL)
6341 return NULL;
6342
6343 // scope for the try block itself
6344 scope = new_scope(cctx, BLOCK_SCOPE);
6345 if (scope == NULL)
6346 return NULL;
6347
6348 return arg;
6349}
6350
6351/*
6352 * compile "catch {expr}"
6353 */
6354 static char_u *
6355compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6356{
6357 scope_T *scope = cctx->ctx_scope;
6358 garray_T *instr = &cctx->ctx_instr;
6359 char_u *p;
6360 isn_T *isn;
6361
6362 // end block scope from :try or :catch
6363 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6364 compile_endblock(cctx);
6365 scope = cctx->ctx_scope;
6366
6367 // Error if not in a :try scope
6368 if (scope == NULL || scope->se_type != TRY_SCOPE)
6369 {
6370 emsg(_(e_catch));
6371 return NULL;
6372 }
6373
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006374 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006375 {
6376 emsg(_("E1033: catch unreachable after catch-all"));
6377 return NULL;
6378 }
6379
6380 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006381 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382 JUMP_ALWAYS, cctx) == FAIL)
6383 return NULL;
6384
6385 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006386 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387 if (isn->isn_arg.try.try_catch == 0)
6388 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006389 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006390 {
6391 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006392 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006393 isn->isn_arg.jump.jump_where = instr->ga_len;
6394 }
6395
6396 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006397 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006399 scope->se_u.se_try.ts_caught_all = TRUE;
6400 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401 }
6402 else
6403 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006404 char_u *end;
6405 char_u *pat;
6406 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006407 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006408 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006410 // Push v:exception, push {expr} and MATCH
6411 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6412
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006413 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006414 if (*end != *p)
6415 {
6416 semsg(_("E1067: Separator mismatch: %s"), p);
6417 vim_free(tofree);
6418 return FAIL;
6419 }
6420 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006421 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006422 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006423 len = (int)(end - tofree);
6424 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006425 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006426 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006427 if (pat == NULL)
6428 return FAIL;
6429 if (generate_PUSHS(cctx, pat) == FAIL)
6430 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006432 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6433 return NULL;
6434
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006435 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6437 return NULL;
6438 }
6439
6440 if (generate_instr(cctx, ISN_CATCH) == NULL)
6441 return NULL;
6442
6443 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6444 return NULL;
6445 return p;
6446}
6447
6448 static char_u *
6449compile_finally(char_u *arg, cctx_T *cctx)
6450{
6451 scope_T *scope = cctx->ctx_scope;
6452 garray_T *instr = &cctx->ctx_instr;
6453 isn_T *isn;
6454
6455 // end block scope from :try or :catch
6456 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6457 compile_endblock(cctx);
6458 scope = cctx->ctx_scope;
6459
6460 // Error if not in a :try scope
6461 if (scope == NULL || scope->se_type != TRY_SCOPE)
6462 {
6463 emsg(_(e_finally));
6464 return NULL;
6465 }
6466
6467 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006468 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006469 if (isn->isn_arg.try.try_finally != 0)
6470 {
6471 emsg(_(e_finally_dup));
6472 return NULL;
6473 }
6474
6475 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006476 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477
Bram Moolenaar585fea72020-04-02 22:33:21 +02006478 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006479 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006480 {
6481 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006482 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483 isn->isn_arg.jump.jump_where = instr->ga_len;
6484 }
6485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 // TODO: set index in ts_finally_label jumps
6487
6488 return arg;
6489}
6490
6491 static char_u *
6492compile_endtry(char_u *arg, cctx_T *cctx)
6493{
6494 scope_T *scope = cctx->ctx_scope;
6495 garray_T *instr = &cctx->ctx_instr;
6496 isn_T *isn;
6497
6498 // end block scope from :catch or :finally
6499 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6500 compile_endblock(cctx);
6501 scope = cctx->ctx_scope;
6502
6503 // Error if not in a :try scope
6504 if (scope == NULL || scope->se_type != TRY_SCOPE)
6505 {
6506 if (scope == NULL)
6507 emsg(_(e_no_endtry));
6508 else if (scope->se_type == WHILE_SCOPE)
6509 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006510 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511 emsg(_(e_endfor));
6512 else
6513 emsg(_(e_endif));
6514 return NULL;
6515 }
6516
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006517 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6519 {
6520 emsg(_("E1032: missing :catch or :finally"));
6521 return NULL;
6522 }
6523
6524 // Fill in the "end" label in jumps at the end of the blocks, if not done
6525 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006526 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527
6528 // End :catch or :finally scope: set value in ISN_TRY instruction
6529 if (isn->isn_arg.try.try_finally == 0)
6530 isn->isn_arg.try.try_finally = instr->ga_len;
6531 compile_endblock(cctx);
6532
6533 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6534 return NULL;
6535 return arg;
6536}
6537
6538/*
6539 * compile "throw {expr}"
6540 */
6541 static char_u *
6542compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6543{
6544 char_u *p = skipwhite(arg);
6545
Bram Moolenaara5565e42020-05-09 15:44:01 +02006546 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 return NULL;
6548 if (may_generate_2STRING(-1, cctx) == FAIL)
6549 return NULL;
6550 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6551 return NULL;
6552
6553 return p;
6554}
6555
6556/*
6557 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006558 * compile "echomsg expr"
6559 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006560 * compile "execute expr"
6561 */
6562 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006563compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006564{
6565 char_u *p = arg;
6566 int count = 0;
6567
6568 for (;;)
6569 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006570 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006571 return NULL;
6572 ++count;
6573 p = skipwhite(p);
6574 if (ends_excmd(*p))
6575 break;
6576 }
6577
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006578 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6579 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6580 else if (cmdidx == CMD_execute)
6581 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6582 else if (cmdidx == CMD_echomsg)
6583 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6584 else
6585 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 return p;
6587}
6588
6589/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006590 * A command that is not compiled, execute with legacy code.
6591 */
6592 static char_u *
6593compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6594{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006595 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006596 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006597 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006598
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006599 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006600 goto theend;
6601
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006602 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006603 {
6604 long argt = excmd_get_argt(eap->cmdidx);
6605 int usefilter = FALSE;
6606
6607 has_expr = argt & (EX_XFILE | EX_EXPAND);
6608
6609 // If the command can be followed by a bar, find the bar and truncate
6610 // it, so that the following command can be compiled.
6611 // The '|' is overwritten with a NUL, it is put back below.
6612 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6613 && *eap->arg == '!')
6614 // :w !filter or :r !filter or :r! filter
6615 usefilter = TRUE;
6616 if ((argt & EX_TRLBAR) && !usefilter)
6617 {
6618 separate_nextcmd(eap);
6619 if (eap->nextcmd != NULL)
6620 nextcmd = eap->nextcmd;
6621 }
6622 }
6623
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006624 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6625 {
6626 // expand filename in "syntax include [@group] filename"
6627 has_expr = TRUE;
6628 eap->arg = skipwhite(eap->arg + 7);
6629 if (*eap->arg == '@')
6630 eap->arg = skiptowhite(eap->arg);
6631 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006632
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006633 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006634 {
6635 int count = 0;
6636 char_u *start = skipwhite(line);
6637
6638 // :cmd xxx`=expr1`yyy`=expr2`zzz
6639 // PUSHS ":cmd xxx"
6640 // eval expr1
6641 // PUSHS "yyy"
6642 // eval expr2
6643 // PUSHS "zzz"
6644 // EXECCONCAT 5
6645 for (;;)
6646 {
6647 if (p > start)
6648 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006649 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006650 ++count;
6651 }
6652 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006653 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006654 return NULL;
6655 may_generate_2STRING(-1, cctx);
6656 ++count;
6657 p = skipwhite(p);
6658 if (*p != '`')
6659 {
6660 emsg(_("E1083: missing backtick"));
6661 return NULL;
6662 }
6663 start = p + 1;
6664
6665 p = (char_u *)strstr((char *)start, "`=");
6666 if (p == NULL)
6667 {
6668 if (*skipwhite(start) != NUL)
6669 {
6670 generate_PUSHS(cctx, vim_strsave(start));
6671 ++count;
6672 }
6673 break;
6674 }
6675 }
6676 generate_EXECCONCAT(cctx, count);
6677 }
6678 else
6679 generate_EXEC(cctx, line);
6680
6681theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006682 if (*nextcmd != NUL)
6683 {
6684 // the parser expects a pointer to the bar, put it back
6685 --nextcmd;
6686 *nextcmd = '|';
6687 }
6688
6689 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006690}
6691
6692/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006693 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006694 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006695 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006696 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006697add_def_function(ufunc_T *ufunc)
6698{
6699 dfunc_T *dfunc;
6700
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006701 if (def_functions.ga_len == 0)
6702 {
6703 // The first position is not used, so that a zero uf_dfunc_idx means it
6704 // wasn't set.
6705 if (ga_grow(&def_functions, 1) == FAIL)
6706 return FAIL;
6707 ++def_functions.ga_len;
6708 }
6709
Bram Moolenaar09689a02020-05-09 22:50:08 +02006710 // Add the function to "def_functions".
6711 if (ga_grow(&def_functions, 1) == FAIL)
6712 return FAIL;
6713 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6714 CLEAR_POINTER(dfunc);
6715 dfunc->df_idx = def_functions.ga_len;
6716 ufunc->uf_dfunc_idx = dfunc->df_idx;
6717 dfunc->df_ufunc = ufunc;
6718 ++def_functions.ga_len;
6719 return OK;
6720}
6721
6722/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723 * After ex_function() has collected all the function lines: parse and compile
6724 * the lines into instructions.
6725 * Adds the function to "def_functions".
6726 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6727 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006728 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006729 * This can be used recursively through compile_lambda(), which may reallocate
6730 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006731 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006733 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006734compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006735{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006736 char_u *line = NULL;
6737 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006738 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739 cctx_T cctx;
6740 garray_T *instr;
6741 int called_emsg_before = called_emsg;
6742 int ret = FAIL;
6743 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006744 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006745 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006747 // When using a function that was compiled before: Free old instructions.
6748 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006749 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006750 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006751 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6752 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006753 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006755 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006756 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006757
Bram Moolenaara80faa82020-04-12 19:37:17 +02006758 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006759 cctx.ctx_ufunc = ufunc;
6760 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006761 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6763 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6764 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6765 cctx.ctx_type_list = &ufunc->uf_type_list;
6766 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6767 instr = &cctx.ctx_instr;
6768
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006769 // Set the context to the function, it may be compiled when called from
6770 // another script. Set the script version to the most modern one.
6771 // The line number will be set in next_line_from_context().
6772 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006773 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6774
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006775 // Make sure error messages are OK.
6776 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6777 if (do_estack_push)
6778 estack_push_ufunc(ufunc, 1);
6779
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006780 if (ufunc->uf_def_args.ga_len > 0)
6781 {
6782 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006783 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006784 int i;
6785 char_u *arg;
6786 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6787
6788 // Produce instructions for the default values of optional arguments.
6789 // Store the instruction index in uf_def_arg_idx[] so that we know
6790 // where to start when the function is called, depending on the number
6791 // of arguments.
6792 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6793 if (ufunc->uf_def_arg_idx == NULL)
6794 goto erret;
6795 for (i = 0; i < count; ++i)
6796 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006797 garray_T *stack = &cctx.ctx_type_stack;
6798 type_T *val_type;
6799 int arg_idx = first_def_arg + i;
6800
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006801 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6802 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006803 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006804 goto erret;
6805
6806 // If no type specified use the type of the default value.
6807 // Otherwise check that the default value type matches the
6808 // specified type.
6809 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6810 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6811 ufunc->uf_arg_types[arg_idx] = val_type;
6812 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6813 == FAIL)
6814 {
6815 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6816 arg_idx + 1);
6817 goto erret;
6818 }
6819
6820 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006821 goto erret;
6822 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006823 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6824 }
6825
6826 /*
6827 * Loop over all the lines of the function and generate instructions.
6828 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006829 for (;;)
6830 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006831 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006832 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006833 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006834 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006835
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006836 // Bail out on the first error to avoid a flood of errors and report
6837 // the right line number when inside try/catch.
6838 if (emsg_before != called_emsg)
6839 goto erret;
6840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006841 if (line != NULL && *line == '|')
6842 // the line continues after a '|'
6843 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006844 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006845 && !(*line == '#' && (line == cctx.ctx_line_start
6846 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006848 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006849 goto erret;
6850 }
6851 else
6852 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006853 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006854 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006855 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006856 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006858 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006859
Bram Moolenaara80faa82020-04-12 19:37:17 +02006860 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006861 ea.cmdlinep = &line;
6862 ea.cmd = skipwhite(line);
6863
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006864 // Some things can be recognized by the first character.
6865 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006866 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006867 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006868 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006869 if (ea.cmd[1] != '{')
6870 {
6871 line = (char_u *)"";
6872 continue;
6873 }
6874 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006876 case '}':
6877 {
6878 // "}" ends a block scope
6879 scopetype_T stype = cctx.ctx_scope == NULL
6880 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006882 if (stype == BLOCK_SCOPE)
6883 {
6884 compile_endblock(&cctx);
6885 line = ea.cmd;
6886 }
6887 else
6888 {
6889 emsg(_("E1025: using } outside of a block scope"));
6890 goto erret;
6891 }
6892 if (line != NULL)
6893 line = skipwhite(ea.cmd + 1);
6894 continue;
6895 }
6896
6897 case '{':
6898 // "{" starts a block scope
6899 // "{'a': 1}->func() is something else
6900 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6901 {
6902 line = compile_block(ea.cmd, &cctx);
6903 continue;
6904 }
6905 break;
6906
6907 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006908 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006909 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006910 }
6911
6912 /*
6913 * COMMAND MODIFIERS
6914 */
6915 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6916 {
6917 if (errormsg != NULL)
6918 goto erret;
6919 // empty line or comment
6920 line = (char_u *)"";
6921 continue;
6922 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006923 // TODO: use modifiers in the command
6924 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006925
6926 // Skip ":call" to get to the function name.
6927 if (checkforcmd(&ea.cmd, "call", 3))
6928 ea.cmd = skipwhite(ea.cmd);
6929
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006930 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006932 char_u *pskip;
6933
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006934 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006935 // find what follows.
6936 // Skip over "var.member", "var[idx]" and the like.
6937 // Also "&opt = val", "$ENV = val" and "@r = val".
6938 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006939 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006940 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006941 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006943 char_u *var_end;
6944 int oplen;
6945 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006946
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006947 var_end = find_name_end(pskip, NULL, NULL,
6948 FNE_CHECK_START | FNE_INCL_BR);
6949 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006950 if (oplen > 0)
6951 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006952 size_t len = p - ea.cmd;
6953
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006954 // Recognize an assignment if we recognize the variable
6955 // name:
6956 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006957 // "local = expr" where "local" is a local var.
6958 // "script = expr" where "script" is a script-local var.
6959 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006960 // "&opt = expr"
6961 // "$ENV = expr"
6962 // "@r = expr"
6963 if (*ea.cmd == '&'
6964 || *ea.cmd == '$'
6965 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006966 || ((len) > 2 && ea.cmd[1] == ':')
6967 || lookup_local(ea.cmd, len, &cctx) != NULL
6968 || lookup_arg(ea.cmd, len, NULL, NULL,
6969 NULL, &cctx) == OK
6970 || lookup_script(ea.cmd, len) == OK
6971 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006972 {
6973 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006974 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006975 goto erret;
6976 continue;
6977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978 }
6979 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006980
6981 if (*ea.cmd == '[')
6982 {
6983 // [var, var] = expr
6984 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6985 if (line == NULL)
6986 goto erret;
6987 if (line != ea.cmd)
6988 continue;
6989 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990 }
6991
6992 /*
6993 * COMMAND after range
6994 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006995 cmd = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006997 if (ea.cmd > cmd && !starts_with_colon)
6998 {
6999 emsg(_(e_colon_required));
7000 goto erret;
7001 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007002 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007003 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007004 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005
7006 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7007 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007008 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007009 {
7010 line += STRLEN(line);
7011 continue;
7012 }
7013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007015 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007016 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007017 // CMD_let cannot happen, compile_assignment() above is used
7018 iemsg("Command from find_ex_command() not handled");
7019 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021 }
7022
7023 p = skipwhite(p);
7024
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007025 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007026 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007027 && ea.cmdidx != CMD_elseif
7028 && ea.cmdidx != CMD_else
7029 && ea.cmdidx != CMD_endif)
7030 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007031 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007032 continue;
7033 }
7034
Bram Moolenaarefd88552020-06-18 20:50:10 +02007035 if (ea.cmdidx != CMD_elseif
7036 && ea.cmdidx != CMD_else
7037 && ea.cmdidx != CMD_endif
7038 && ea.cmdidx != CMD_endfor
7039 && ea.cmdidx != CMD_endwhile
7040 && ea.cmdidx != CMD_catch
7041 && ea.cmdidx != CMD_finally
7042 && ea.cmdidx != CMD_endtry)
7043 {
7044 if (cctx.ctx_had_return)
7045 {
7046 emsg(_("E1095: Unreachable code after :return"));
7047 goto erret;
7048 }
7049 }
7050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051 switch (ea.cmdidx)
7052 {
7053 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007054 ea.arg = p;
7055 line = compile_nested_function(&ea, &cctx);
7056 break;
7057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007059 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 goto erret;
7061
7062 case CMD_return:
7063 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007064 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007065 break;
7066
7067 case CMD_let:
7068 case CMD_const:
7069 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007070 if (line == p)
7071 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 break;
7073
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007074 case CMD_unlet:
7075 case CMD_unlockvar:
7076 case CMD_lockvar:
7077 line = compile_unletlock(p, &ea, &cctx);
7078 break;
7079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 case CMD_import:
7081 line = compile_import(p, &cctx);
7082 break;
7083
7084 case CMD_if:
7085 line = compile_if(p, &cctx);
7086 break;
7087 case CMD_elseif:
7088 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007089 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 break;
7091 case CMD_else:
7092 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007093 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094 break;
7095 case CMD_endif:
7096 line = compile_endif(p, &cctx);
7097 break;
7098
7099 case CMD_while:
7100 line = compile_while(p, &cctx);
7101 break;
7102 case CMD_endwhile:
7103 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007104 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105 break;
7106
7107 case CMD_for:
7108 line = compile_for(p, &cctx);
7109 break;
7110 case CMD_endfor:
7111 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007112 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 break;
7114 case CMD_continue:
7115 line = compile_continue(p, &cctx);
7116 break;
7117 case CMD_break:
7118 line = compile_break(p, &cctx);
7119 break;
7120
7121 case CMD_try:
7122 line = compile_try(p, &cctx);
7123 break;
7124 case CMD_catch:
7125 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007126 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127 break;
7128 case CMD_finally:
7129 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007130 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131 break;
7132 case CMD_endtry:
7133 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007134 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007135 break;
7136 case CMD_throw:
7137 line = compile_throw(p, &cctx);
7138 break;
7139
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007140 case CMD_eval:
7141 if (compile_expr0(&p, &cctx) == FAIL)
7142 goto erret;
7143
7144 // drop the return value
7145 generate_instr_drop(&cctx, ISN_DROP, 1);
7146
7147 line = skipwhite(p);
7148 break;
7149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007150 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007152 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007153 case CMD_echomsg:
7154 case CMD_echoerr:
7155 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007156 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007158 // TODO: other commands with an expression argument
7159
Bram Moolenaar002262f2020-07-08 17:47:57 +02007160 case CMD_SIZE:
7161 semsg(_("E476: Invalid command: %s"), ea.cmd);
7162 goto erret;
7163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007165 // Not recognized, execute with do_cmdline_cmd().
7166 ea.arg = p;
7167 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 break;
7169 }
7170 if (line == NULL)
7171 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007172 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173
7174 if (cctx.ctx_type_stack.ga_len < 0)
7175 {
7176 iemsg("Type stack underflow");
7177 goto erret;
7178 }
7179 }
7180
7181 if (cctx.ctx_scope != NULL)
7182 {
7183 if (cctx.ctx_scope->se_type == IF_SCOPE)
7184 emsg(_(e_endif));
7185 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7186 emsg(_(e_endwhile));
7187 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7188 emsg(_(e_endfor));
7189 else
7190 emsg(_("E1026: Missing }"));
7191 goto erret;
7192 }
7193
Bram Moolenaarefd88552020-06-18 20:50:10 +02007194 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 {
7196 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7197 {
7198 emsg(_("E1027: Missing return statement"));
7199 goto erret;
7200 }
7201
7202 // Return zero if there is no return at the end.
7203 generate_PUSHNR(&cctx, 0);
7204 generate_instr(&cctx, ISN_RETURN);
7205 }
7206
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007207 {
7208 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7209 + ufunc->uf_dfunc_idx;
7210 dfunc->df_deleted = FALSE;
7211 dfunc->df_instr = instr->ga_data;
7212 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007213 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007214 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007215 if (cctx.ctx_outer_used)
7216 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007217 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007219
7220 ret = OK;
7221
7222erret:
7223 if (ret == FAIL)
7224 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007225 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007226 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7227 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007228
7229 for (idx = 0; idx < instr->ga_len; ++idx)
7230 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007231 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007232
Bram Moolenaar45a15082020-05-25 00:28:33 +02007233 // if using the last entry in the table we might as well remove it
7234 if (!dfunc->df_deleted
7235 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007236 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007237 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007238
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007239 while (cctx.ctx_scope != NULL)
7240 drop_scope(&cctx);
7241
Bram Moolenaar20431c92020-03-20 18:39:46 +01007242 // Don't execute this function body.
7243 ga_clear_strings(&ufunc->uf_lines);
7244
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007245 if (errormsg != NULL)
7246 emsg(errormsg);
7247 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007248 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249 }
7250
7251 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007252 if (do_estack_push)
7253 estack_pop();
7254
Bram Moolenaar20431c92020-03-20 18:39:46 +01007255 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007256 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007257 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007258 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007259}
7260
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007261 void
7262set_function_type(ufunc_T *ufunc)
7263{
7264 int varargs = ufunc->uf_va_name != NULL;
7265 int argcount = ufunc->uf_args.ga_len;
7266
7267 // Create a type for the function, with the return type and any
7268 // argument types.
7269 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7270 // The type is included in "tt_args".
7271 if (argcount > 0 || varargs)
7272 {
7273 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7274 argcount, &ufunc->uf_type_list);
7275 // Add argument types to the function type.
7276 if (func_type_add_arg_types(ufunc->uf_func_type,
7277 argcount + varargs,
7278 &ufunc->uf_type_list) == FAIL)
7279 return;
7280 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7281 ufunc->uf_func_type->tt_min_argcount =
7282 argcount - ufunc->uf_def_args.ga_len;
7283 if (ufunc->uf_arg_types == NULL)
7284 {
7285 int i;
7286
7287 // lambda does not have argument types.
7288 for (i = 0; i < argcount; ++i)
7289 ufunc->uf_func_type->tt_args[i] = &t_any;
7290 }
7291 else
7292 mch_memmove(ufunc->uf_func_type->tt_args,
7293 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7294 if (varargs)
7295 {
7296 ufunc->uf_func_type->tt_args[argcount] =
7297 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7298 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7299 }
7300 }
7301 else
7302 // No arguments, can use a predefined type.
7303 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7304 argcount, &ufunc->uf_type_list);
7305}
7306
7307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308/*
7309 * Delete an instruction, free what it contains.
7310 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007311 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312delete_instr(isn_T *isn)
7313{
7314 switch (isn->isn_type)
7315 {
7316 case ISN_EXEC:
7317 case ISN_LOADENV:
7318 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007319 case ISN_LOADB:
7320 case ISN_LOADW:
7321 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007323 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324 case ISN_PUSHEXC:
7325 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007326 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007327 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007328 case ISN_STOREB:
7329 case ISN_STOREW:
7330 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007331 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332 vim_free(isn->isn_arg.string);
7333 break;
7334
7335 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007336 case ISN_STORES:
7337 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 break;
7339
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007340 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007341 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007342 vim_free(isn->isn_arg.unlet.ul_name);
7343 break;
7344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345 case ISN_STOREOPT:
7346 vim_free(isn->isn_arg.storeopt.so_name);
7347 break;
7348
7349 case ISN_PUSHBLOB: // push blob isn_arg.blob
7350 blob_unref(isn->isn_arg.blob);
7351 break;
7352
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007353 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007354#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007355 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007356#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007357 break;
7358
7359 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007360#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007361 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007362#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007363 break;
7364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 case ISN_UCALL:
7366 vim_free(isn->isn_arg.ufunc.cuf_name);
7367 break;
7368
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007369 case ISN_FUNCREF:
7370 {
7371 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7372 + isn->isn_arg.funcref.fr_func;
7373 func_ptr_unref(dfunc->df_ufunc);
7374 }
7375 break;
7376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 case ISN_2BOOL:
7378 case ISN_2STRING:
7379 case ISN_ADDBLOB:
7380 case ISN_ADDLIST:
7381 case ISN_BCALL:
7382 case ISN_CATCH:
7383 case ISN_CHECKNR:
7384 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007385 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 case ISN_COMPAREANY:
7387 case ISN_COMPAREBLOB:
7388 case ISN_COMPAREBOOL:
7389 case ISN_COMPAREDICT:
7390 case ISN_COMPAREFLOAT:
7391 case ISN_COMPAREFUNC:
7392 case ISN_COMPARELIST:
7393 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394 case ISN_COMPARESPECIAL:
7395 case ISN_COMPARESTRING:
7396 case ISN_CONCAT:
7397 case ISN_DCALL:
7398 case ISN_DROP:
7399 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007400 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007401 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007403 case ISN_EXECCONCAT:
7404 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007407 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007408 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007409 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 case ISN_JUMP:
7411 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007412 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413 case ISN_LOADSCRIPT:
7414 case ISN_LOADREG:
7415 case ISN_LOADV:
7416 case ISN_NEGATENR:
7417 case ISN_NEWDICT:
7418 case ISN_NEWLIST:
7419 case ISN_OPNR:
7420 case ISN_OPFLOAT:
7421 case ISN_OPANY:
7422 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007423 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424 case ISN_PUSHF:
7425 case ISN_PUSHNR:
7426 case ISN_PUSHBOOL:
7427 case ISN_PUSHSPEC:
7428 case ISN_RETURN:
7429 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007430 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007431 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007433 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007435 case ISN_STOREDICT:
7436 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007437 case ISN_THROW:
7438 case ISN_TRY:
7439 // nothing allocated
7440 break;
7441 }
7442}
7443
7444/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007445 * Free all instructions for "dfunc".
7446 */
7447 static void
7448delete_def_function_contents(dfunc_T *dfunc)
7449{
7450 int idx;
7451
7452 ga_clear(&dfunc->df_def_args_isn);
7453
7454 if (dfunc->df_instr != NULL)
7455 {
7456 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7457 delete_instr(dfunc->df_instr + idx);
7458 VIM_CLEAR(dfunc->df_instr);
7459 }
7460
7461 dfunc->df_deleted = TRUE;
7462}
7463
7464/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007465 * When a user function is deleted, clear the contents of any associated def
7466 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 */
7468 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007469clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007470{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007471 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 {
7473 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7474 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475
Bram Moolenaar20431c92020-03-20 18:39:46 +01007476 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007477 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478 }
7479}
7480
7481#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007482/*
7483 * Free all functions defined with ":def".
7484 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007485 void
7486free_def_functions(void)
7487{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007488 int idx;
7489
7490 for (idx = 0; idx < def_functions.ga_len; ++idx)
7491 {
7492 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7493
7494 delete_def_function_contents(dfunc);
7495 }
7496
7497 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007498}
7499#endif
7500
7501
7502#endif // FEAT_EVAL