blob: 39190c972595144bcc682b631ffc74e8b5481d42 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
146static char e_var_notfound[] = N_("E1001: variable not found: %s");
147static char e_syntax_at[] = N_("E1002: Syntax error at %s");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200148static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200149static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150
Bram Moolenaar20431c92020-03-20 18:39:46 +0100151static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200152static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153
154/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200155 * Lookup variable "name" in the local scope and return it.
156 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200158 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159lookup_local(char_u *name, size_t len, cctx_T *cctx)
160{
161 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100164 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200165 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166
167 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
169 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 if (STRNCMP(name, lvar->lv_name, len) == 0
172 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200173 {
174 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200175 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200178
179 // Find local in outer function scope.
180 if (cctx->ctx_outer != NULL)
181 {
182 lvar = lookup_local(name, len, cctx->ctx_outer);
183 if (lvar != NULL)
184 {
185 // TODO: are there situations we should not mark the outer scope as
186 // used?
187 cctx->ctx_outer_used = TRUE;
188 lvar->lv_from_outer = TRUE;
189 return lvar;
190 }
191 }
192
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100194}
195
196/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200197 * Lookup an argument in the current function and an enclosing function.
198 * Returns the argument index in "idxp"
199 * Returns the argument type in "type"
200 * Sets "gen_load_outer" to TRUE if found in outer scope.
201 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202 */
203 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200204lookup_arg(
205 char_u *name,
206 size_t len,
207 int *idxp,
208 type_T **type,
209 int *gen_load_outer,
210 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211{
212 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200213 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100215 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200216 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
218 {
219 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
220
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200221 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
222 {
223 if (idxp != NULL)
224 {
225 // Arguments are located above the frame pointer. One further
226 // if there is a vararg argument
227 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
228 + STACK_FRAME_SIZE)
229 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
230
231 if (cctx->ctx_ufunc->uf_arg_types != NULL)
232 *type = cctx->ctx_ufunc->uf_arg_types[idx];
233 else
234 *type = &t_any;
235 }
236 return OK;
237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200240 va_name = cctx->ctx_ufunc->uf_va_name;
241 if (va_name != NULL
242 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
243 {
244 if (idxp != NULL)
245 {
246 // varargs is always the last argument
247 *idxp = -STACK_FRAME_SIZE - 1;
248 *type = cctx->ctx_ufunc->uf_va_type;
249 }
250 return OK;
251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200253 if (cctx->ctx_outer != NULL)
254 {
255 // Lookup the name for an argument of the outer function.
256 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
257 == OK)
258 {
259 *gen_load_outer = TRUE;
260 return OK;
261 }
262 }
263
264 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265}
266
267/*
268 * Lookup a variable in the current script.
269 * Returns OK or FAIL.
270 */
271 static int
272lookup_script(char_u *name, size_t len)
273{
274 int cc;
275 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
276 dictitem_T *di;
277
278 cc = name[len];
279 name[len] = NUL;
280 di = find_var_in_ht(ht, 0, name, TRUE);
281 name[len] = cc;
282 return di == NULL ? FAIL: OK;
283}
284
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100285/*
286 * Check if "p[len]" is already defined, either in script "import_sid" or in
287 * compilation context "cctx".
288 * Return FAIL and give an error if it defined.
289 */
290 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200291check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100292{
293 if (lookup_script(p, len) == OK
294 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200295 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100296 || find_imported(p, len, cctx) != NULL)))
297 {
298 semsg("E1073: imported name already defined: %s", p);
299 return FAIL;
300 }
301 return OK;
302}
303
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200304/*
305 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
306 * be freed later.
307 */
308 static type_T *
309alloc_type(garray_T *type_gap)
310{
311 type_T *type;
312
313 if (ga_grow(type_gap, 1) == FAIL)
314 return NULL;
315 type = ALLOC_CLEAR_ONE(type_T);
316 if (type != NULL)
317 {
318 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
319 ++type_gap->ga_len;
320 }
321 return type;
322}
323
Bram Moolenaar6110e792020-07-08 19:35:21 +0200324 void
325clear_type_list(garray_T *gap)
326{
327 while (gap->ga_len > 0)
328 vim_free(((type_T **)gap->ga_data)[--gap->ga_len]);
329 ga_clear(gap);
330}
331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200333get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334{
335 type_T *type;
336
337 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200338 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100339 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200340 if (member_type->tt_type == VAR_VOID
341 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100342 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100343 if (member_type->tt_type == VAR_BOOL)
344 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100345 if (member_type->tt_type == VAR_NUMBER)
346 return &t_list_number;
347 if (member_type->tt_type == VAR_STRING)
348 return &t_list_string;
349
350 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200351 type = alloc_type(type_gap);
352 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100353 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100354 type->tt_type = VAR_LIST;
355 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200356 type->tt_argcount = 0;
357 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358 return type;
359}
360
361 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200362get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363{
364 type_T *type;
365
366 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200367 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100368 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200369 if (member_type->tt_type == VAR_VOID
370 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100371 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100372 if (member_type->tt_type == VAR_BOOL)
373 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100374 if (member_type->tt_type == VAR_NUMBER)
375 return &t_dict_number;
376 if (member_type->tt_type == VAR_STRING)
377 return &t_dict_string;
378
379 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200380 type = alloc_type(type_gap);
381 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100382 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 type->tt_type = VAR_DICT;
384 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200385 type->tt_argcount = 0;
386 type->tt_args = NULL;
387 return type;
388}
389
390/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200391 * Allocate a new type for a function.
392 */
393 static type_T *
394alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
395{
396 type_T *type = alloc_type(type_gap);
397
398 if (type == NULL)
399 return &t_any;
400 type->tt_type = VAR_FUNC;
401 type->tt_member = ret_type;
402 type->tt_argcount = argcount;
403 type->tt_args = NULL;
404 return type;
405}
406
407/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200408 * Get a function type, based on the return type "ret_type".
409 * If "argcount" is -1 or 0 a predefined type can be used.
410 * If "argcount" > 0 always create a new type, so that arguments can be added.
411 */
412 static type_T *
413get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
414{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200415 // recognize commonly used types
416 if (argcount <= 0)
417 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200418 if (ret_type == &t_unknown)
419 {
420 // (argcount == 0) is not possible
421 return &t_func_unknown;
422 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200423 if (ret_type == &t_void)
424 {
425 if (argcount == 0)
426 return &t_func_0_void;
427 else
428 return &t_func_void;
429 }
430 if (ret_type == &t_any)
431 {
432 if (argcount == 0)
433 return &t_func_0_any;
434 else
435 return &t_func_any;
436 }
437 if (ret_type == &t_number)
438 {
439 if (argcount == 0)
440 return &t_func_0_number;
441 else
442 return &t_func_number;
443 }
444 if (ret_type == &t_string)
445 {
446 if (argcount == 0)
447 return &t_func_0_string;
448 else
449 return &t_func_string;
450 }
451 }
452
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200453 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454}
455
Bram Moolenaara8c17702020-04-01 21:17:24 +0200456/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200457 * For a function type, reserve space for "argcount" argument types (including
458 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200459 */
460 static int
461func_type_add_arg_types(
462 type_T *functype,
463 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200464 garray_T *type_gap)
465{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200466 // To make it easy to free the space needed for the argument types, add the
467 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200468 if (ga_grow(type_gap, 1) == FAIL)
469 return FAIL;
470 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
471 if (functype->tt_args == NULL)
472 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200473 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
474 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200475 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 return OK;
477}
478
479/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200480 * Return the type_T for a typval. Only for primitive types.
481 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200482 type_T *
Bram Moolenaara8c17702020-04-01 21:17:24 +0200483typval2type(typval_T *tv)
484{
485 if (tv->v_type == VAR_NUMBER)
486 return &t_number;
487 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200488 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200489 if (tv->v_type == VAR_STRING)
490 return &t_string;
491 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
492 return &t_list_string;
493 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
494 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200495 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200496}
497
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200498 static void
499type_mismatch(type_T *expected, type_T *actual)
500{
501 char *tofree1, *tofree2;
502
503 semsg(_("E1013: type mismatch, expected %s but got %s"),
504 type_name(expected, &tofree1), type_name(actual, &tofree2));
505 vim_free(tofree1);
506 vim_free(tofree2);
507}
508
509 static void
510arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
511{
512 char *tofree1, *tofree2;
513
514 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
515 argidx,
516 type_name(expected, &tofree1), type_name(actual, &tofree2));
517 vim_free(tofree1);
518 vim_free(tofree2);
519}
520
521/*
522 * Check if the expected and actual types match.
523 * Does not allow for assigning "any" to a specific type.
524 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200525 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200526check_type(type_T *expected, type_T *actual, int give_msg)
527{
528 int ret = OK;
529
530 // When expected is "unknown" we accept any actual type.
531 // When expected is "any" we accept any actual type except "void".
532 if (expected->tt_type != VAR_UNKNOWN
533 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
534
535 {
536 if (expected->tt_type != actual->tt_type)
537 {
538 if (give_msg)
539 type_mismatch(expected, actual);
540 return FAIL;
541 }
542 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
543 {
544 // "unknown" is used for an empty list or dict
545 if (actual->tt_member != &t_unknown)
546 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
547 }
548 else if (expected->tt_type == VAR_FUNC)
549 {
550 if (expected->tt_member != &t_unknown)
551 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
552 if (ret == OK && expected->tt_argcount != -1
553 && (actual->tt_argcount < expected->tt_min_argcount
554 || actual->tt_argcount > expected->tt_argcount))
555 ret = FAIL;
556 }
557 if (ret == FAIL && give_msg)
558 type_mismatch(expected, actual);
559 }
560 return ret;
561}
562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563/////////////////////////////////////////////////////////////////////
564// Following generate_ functions expect the caller to call ga_grow().
565
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200566#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
567#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569/*
570 * Generate an instruction without arguments.
571 * Returns a pointer to the new instruction, NULL if failed.
572 */
573 static isn_T *
574generate_instr(cctx_T *cctx, isntype_T isn_type)
575{
576 garray_T *instr = &cctx->ctx_instr;
577 isn_T *isn;
578
Bram Moolenaar080457c2020-03-03 21:53:32 +0100579 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 if (ga_grow(instr, 1) == FAIL)
581 return NULL;
582 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
583 isn->isn_type = isn_type;
584 isn->isn_lnum = cctx->ctx_lnum + 1;
585 ++instr->ga_len;
586
587 return isn;
588}
589
590/*
591 * Generate an instruction without arguments.
592 * "drop" will be removed from the stack.
593 * Returns a pointer to the new instruction, NULL if failed.
594 */
595 static isn_T *
596generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
597{
598 garray_T *stack = &cctx->ctx_type_stack;
599
Bram Moolenaar080457c2020-03-03 21:53:32 +0100600 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 stack->ga_len -= drop;
602 return generate_instr(cctx, isn_type);
603}
604
605/*
606 * Generate instruction "isn_type" and put "type" on the type stack.
607 */
608 static isn_T *
609generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
610{
611 isn_T *isn;
612 garray_T *stack = &cctx->ctx_type_stack;
613
614 if ((isn = generate_instr(cctx, isn_type)) == NULL)
615 return NULL;
616
617 if (ga_grow(stack, 1) == FAIL)
618 return NULL;
619 ((type_T **)stack->ga_data)[stack->ga_len] = type;
620 ++stack->ga_len;
621
622 return isn;
623}
624
625/*
626 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
627 */
628 static int
629may_generate_2STRING(int offset, cctx_T *cctx)
630{
631 isn_T *isn;
632 garray_T *stack = &cctx->ctx_type_stack;
633 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
634
635 if ((*type)->tt_type == VAR_STRING)
636 return OK;
637 *type = &t_string;
638
639 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
640 return FAIL;
641 isn->isn_arg.number = offset;
642
643 return OK;
644}
645
646 static int
647check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
648{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200649 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200651 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 {
653 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200654 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 else
656 semsg(_("E1036: %c requires number or float arguments"), *op);
657 return FAIL;
658 }
659 return OK;
660}
661
662/*
663 * Generate an instruction with two arguments. The instruction depends on the
664 * type of the arguments.
665 */
666 static int
667generate_two_op(cctx_T *cctx, char_u *op)
668{
669 garray_T *stack = &cctx->ctx_type_stack;
670 type_T *type1;
671 type_T *type2;
672 vartype_T vartype;
673 isn_T *isn;
674
Bram Moolenaar080457c2020-03-03 21:53:32 +0100675 RETURN_OK_IF_SKIP(cctx);
676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 // Get the known type of the two items on the stack. If they are matching
678 // use a type-specific instruction. Otherwise fall back to runtime type
679 // checking.
680 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
681 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 if (type1->tt_type == type2->tt_type
684 && (type1->tt_type == VAR_NUMBER
685 || type1->tt_type == VAR_LIST
686#ifdef FEAT_FLOAT
687 || type1->tt_type == VAR_FLOAT
688#endif
689 || type1->tt_type == VAR_BLOB))
690 vartype = type1->tt_type;
691
692 switch (*op)
693 {
694 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200695 && type1->tt_type != VAR_ANY
696 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 && check_number_or_float(
698 type1->tt_type, type2->tt_type, op) == FAIL)
699 return FAIL;
700 isn = generate_instr_drop(cctx,
701 vartype == VAR_NUMBER ? ISN_OPNR
702 : vartype == VAR_LIST ? ISN_ADDLIST
703 : vartype == VAR_BLOB ? ISN_ADDBLOB
704#ifdef FEAT_FLOAT
705 : vartype == VAR_FLOAT ? ISN_OPFLOAT
706#endif
707 : ISN_OPANY, 1);
708 if (isn != NULL)
709 isn->isn_arg.op.op_type = EXPR_ADD;
710 break;
711
712 case '-':
713 case '*':
714 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
715 op) == FAIL)
716 return FAIL;
717 if (vartype == VAR_NUMBER)
718 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
719#ifdef FEAT_FLOAT
720 else if (vartype == VAR_FLOAT)
721 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
722#endif
723 else
724 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
725 if (isn != NULL)
726 isn->isn_arg.op.op_type = *op == '*'
727 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
728 break;
729
Bram Moolenaar4c683752020-04-05 21:38:23 +0200730 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200732 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 && type2->tt_type != VAR_NUMBER))
734 {
735 emsg(_("E1035: % requires number arguments"));
736 return FAIL;
737 }
738 isn = generate_instr_drop(cctx,
739 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
740 if (isn != NULL)
741 isn->isn_arg.op.op_type = EXPR_REM;
742 break;
743 }
744
745 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200746 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 {
748 type_T *type = &t_any;
749
750#ifdef FEAT_FLOAT
751 // float+number and number+float results in float
752 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
753 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
754 type = &t_float;
755#endif
756 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
757 }
758
759 return OK;
760}
761
762/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200763 * Get the instruction to use for comparing "type1" with "type2"
764 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200766 static isntype_T
767get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768{
769 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770
Bram Moolenaar4c683752020-04-05 21:38:23 +0200771 if (type1 == VAR_UNKNOWN)
772 type1 = VAR_ANY;
773 if (type2 == VAR_UNKNOWN)
774 type2 = VAR_ANY;
775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 if (type1 == type2)
777 {
778 switch (type1)
779 {
780 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
781 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
782 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
783 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
784 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
785 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
786 case VAR_LIST: isntype = ISN_COMPARELIST; break;
787 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
788 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 default: isntype = ISN_COMPAREANY; break;
790 }
791 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200792 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
794 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
795 isntype = ISN_COMPAREANY;
796
797 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
798 && (isntype == ISN_COMPAREBOOL
799 || isntype == ISN_COMPARESPECIAL
800 || isntype == ISN_COMPARENR
801 || isntype == ISN_COMPAREFLOAT))
802 {
803 semsg(_("E1037: Cannot use \"%s\" with %s"),
804 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200805 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 }
807 if (isntype == ISN_DROP
808 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
809 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
810 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
811 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
812 && exptype != EXPR_IS && exptype != EXPR_ISNOT
813 && (type1 == VAR_BLOB || type2 == VAR_BLOB
814 || type1 == VAR_LIST || type2 == VAR_LIST))))
815 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100816 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200818 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200820 return isntype;
821}
822
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200823 int
824check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
825{
826 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
827 return FAIL;
828 return OK;
829}
830
Bram Moolenaara5565e42020-05-09 15:44:01 +0200831/*
832 * Generate an ISN_COMPARE* instruction with a boolean result.
833 */
834 static int
835generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
836{
837 isntype_T isntype;
838 isn_T *isn;
839 garray_T *stack = &cctx->ctx_type_stack;
840 vartype_T type1;
841 vartype_T type2;
842
843 RETURN_OK_IF_SKIP(cctx);
844
845 // Get the known type of the two items on the stack. If they are matching
846 // use a type-specific instruction. Otherwise fall back to runtime type
847 // checking.
848 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
849 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
850 isntype = get_compare_isn(exptype, type1, type2);
851 if (isntype == ISN_DROP)
852 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853
854 if ((isn = generate_instr(cctx, isntype)) == NULL)
855 return FAIL;
856 isn->isn_arg.op.op_type = exptype;
857 isn->isn_arg.op.op_ic = ic;
858
859 // takes two arguments, puts one bool back
860 if (stack->ga_len >= 2)
861 {
862 --stack->ga_len;
863 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
864 }
865
866 return OK;
867}
868
869/*
870 * Generate an ISN_2BOOL instruction.
871 */
872 static int
873generate_2BOOL(cctx_T *cctx, int invert)
874{
875 isn_T *isn;
876 garray_T *stack = &cctx->ctx_type_stack;
877
Bram Moolenaar080457c2020-03-03 21:53:32 +0100878 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
880 return FAIL;
881 isn->isn_arg.number = invert;
882
883 // type becomes bool
884 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
885
886 return OK;
887}
888
889 static int
890generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
891{
892 isn_T *isn;
893 garray_T *stack = &cctx->ctx_type_stack;
894
Bram Moolenaar080457c2020-03-03 21:53:32 +0100895 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
897 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200898 // TODO: whole type, e.g. for a function also arg and return types
899 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 isn->isn_arg.type.ct_off = offset;
901
902 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200903 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904
905 return OK;
906}
907
908/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200909 * Check that
910 * - "actual" is "expected" type or
911 * - "actual" is a type that can be "expected" type: add a runtime check; or
912 * - return FAIL.
913 */
914 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200915need_type(
916 type_T *actual,
917 type_T *expected,
918 int offset,
919 cctx_T *cctx,
920 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200921{
922 if (check_type(expected, actual, FALSE) == OK)
923 return OK;
924 if (actual->tt_type != VAR_ANY
925 && actual->tt_type != VAR_UNKNOWN
926 && !(actual->tt_type == VAR_FUNC
927 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
928 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200929 if (!silent)
930 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200931 return FAIL;
932 }
933 generate_TYPECHECK(cctx, expected, offset);
934 return OK;
935}
936
937/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 * Generate an ISN_PUSHNR instruction.
939 */
940 static int
941generate_PUSHNR(cctx_T *cctx, varnumber_T number)
942{
943 isn_T *isn;
944
Bram Moolenaar080457c2020-03-03 21:53:32 +0100945 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
947 return FAIL;
948 isn->isn_arg.number = number;
949
950 return OK;
951}
952
953/*
954 * Generate an ISN_PUSHBOOL instruction.
955 */
956 static int
957generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
958{
959 isn_T *isn;
960
Bram Moolenaar080457c2020-03-03 21:53:32 +0100961 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
963 return FAIL;
964 isn->isn_arg.number = number;
965
966 return OK;
967}
968
969/*
970 * Generate an ISN_PUSHSPEC instruction.
971 */
972 static int
973generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
974{
975 isn_T *isn;
976
Bram Moolenaar080457c2020-03-03 21:53:32 +0100977 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
979 return FAIL;
980 isn->isn_arg.number = number;
981
982 return OK;
983}
984
985#ifdef FEAT_FLOAT
986/*
987 * Generate an ISN_PUSHF instruction.
988 */
989 static int
990generate_PUSHF(cctx_T *cctx, float_T fnumber)
991{
992 isn_T *isn;
993
Bram Moolenaar080457c2020-03-03 21:53:32 +0100994 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
996 return FAIL;
997 isn->isn_arg.fnumber = fnumber;
998
999 return OK;
1000}
1001#endif
1002
1003/*
1004 * Generate an ISN_PUSHS instruction.
1005 * Consumes "str".
1006 */
1007 static int
1008generate_PUSHS(cctx_T *cctx, char_u *str)
1009{
1010 isn_T *isn;
1011
Bram Moolenaar080457c2020-03-03 21:53:32 +01001012 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1014 return FAIL;
1015 isn->isn_arg.string = str;
1016
1017 return OK;
1018}
1019
1020/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001021 * Generate an ISN_PUSHCHANNEL instruction.
1022 * Consumes "channel".
1023 */
1024 static int
1025generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1026{
1027 isn_T *isn;
1028
Bram Moolenaar080457c2020-03-03 21:53:32 +01001029 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001030 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1031 return FAIL;
1032 isn->isn_arg.channel = channel;
1033
1034 return OK;
1035}
1036
1037/*
1038 * Generate an ISN_PUSHJOB instruction.
1039 * Consumes "job".
1040 */
1041 static int
1042generate_PUSHJOB(cctx_T *cctx, job_T *job)
1043{
1044 isn_T *isn;
1045
Bram Moolenaar080457c2020-03-03 21:53:32 +01001046 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001047 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001048 return FAIL;
1049 isn->isn_arg.job = job;
1050
1051 return OK;
1052}
1053
1054/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 * Generate an ISN_PUSHBLOB instruction.
1056 * Consumes "blob".
1057 */
1058 static int
1059generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1060{
1061 isn_T *isn;
1062
Bram Moolenaar080457c2020-03-03 21:53:32 +01001063 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1065 return FAIL;
1066 isn->isn_arg.blob = blob;
1067
1068 return OK;
1069}
1070
1071/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001072 * Generate an ISN_PUSHFUNC instruction with name "name".
1073 * Consumes "name".
1074 */
1075 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001076generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001077{
1078 isn_T *isn;
1079
Bram Moolenaar080457c2020-03-03 21:53:32 +01001080 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001081 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001082 return FAIL;
1083 isn->isn_arg.string = name;
1084
1085 return OK;
1086}
1087
1088/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001089 * Generate an ISN_GETITEM instruction with "index".
1090 */
1091 static int
1092generate_GETITEM(cctx_T *cctx, int index)
1093{
1094 isn_T *isn;
1095 garray_T *stack = &cctx->ctx_type_stack;
1096 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1097 type_T *item_type = &t_any;
1098
1099 RETURN_OK_IF_SKIP(cctx);
1100
1101 if (type->tt_type == VAR_LIST)
1102 item_type = type->tt_member;
1103 else if (type->tt_type != VAR_ANY)
1104 {
1105 emsg(_(e_listreq));
1106 return FAIL;
1107 }
1108 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1109 return FAIL;
1110 isn->isn_arg.number = index;
1111
1112 // add the item type to the type stack
1113 if (ga_grow(stack, 1) == FAIL)
1114 return FAIL;
1115 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1116 ++stack->ga_len;
1117 return OK;
1118}
1119
1120/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001121 * Generate an ISN_SLICE instruction with "count".
1122 */
1123 static int
1124generate_SLICE(cctx_T *cctx, int count)
1125{
1126 isn_T *isn;
1127
1128 RETURN_OK_IF_SKIP(cctx);
1129 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1130 return FAIL;
1131 isn->isn_arg.number = count;
1132 return OK;
1133}
1134
1135/*
1136 * Generate an ISN_CHECKLEN instruction with "min_len".
1137 */
1138 static int
1139generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1140{
1141 isn_T *isn;
1142
1143 RETURN_OK_IF_SKIP(cctx);
1144
1145 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1146 return FAIL;
1147 isn->isn_arg.checklen.cl_min_len = min_len;
1148 isn->isn_arg.checklen.cl_more_OK = more_OK;
1149
1150 return OK;
1151}
1152
1153/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 * Generate an ISN_STORE instruction.
1155 */
1156 static int
1157generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1158{
1159 isn_T *isn;
1160
Bram Moolenaar080457c2020-03-03 21:53:32 +01001161 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1163 return FAIL;
1164 if (name != NULL)
1165 isn->isn_arg.string = vim_strsave(name);
1166 else
1167 isn->isn_arg.number = idx;
1168
1169 return OK;
1170}
1171
1172/*
1173 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1174 */
1175 static int
1176generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1177{
1178 isn_T *isn;
1179
Bram Moolenaar080457c2020-03-03 21:53:32 +01001180 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1182 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001183 isn->isn_arg.storenr.stnr_idx = idx;
1184 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185
1186 return OK;
1187}
1188
1189/*
1190 * Generate an ISN_STOREOPT instruction
1191 */
1192 static int
1193generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1194{
1195 isn_T *isn;
1196
Bram Moolenaar080457c2020-03-03 21:53:32 +01001197 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1199 return FAIL;
1200 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1201 isn->isn_arg.storeopt.so_flags = opt_flags;
1202
1203 return OK;
1204}
1205
1206/*
1207 * Generate an ISN_LOAD or similar instruction.
1208 */
1209 static int
1210generate_LOAD(
1211 cctx_T *cctx,
1212 isntype_T isn_type,
1213 int idx,
1214 char_u *name,
1215 type_T *type)
1216{
1217 isn_T *isn;
1218
Bram Moolenaar080457c2020-03-03 21:53:32 +01001219 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1221 return FAIL;
1222 if (name != NULL)
1223 isn->isn_arg.string = vim_strsave(name);
1224 else
1225 isn->isn_arg.number = idx;
1226
1227 return OK;
1228}
1229
1230/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001231 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001232 */
1233 static int
1234generate_LOADV(
1235 cctx_T *cctx,
1236 char_u *name,
1237 int error)
1238{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001239 int di_flags;
1240 int vidx = find_vim_var(name, &di_flags);
1241 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001242
Bram Moolenaar080457c2020-03-03 21:53:32 +01001243 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001244 if (vidx < 0)
1245 {
1246 if (error)
1247 semsg(_(e_var_notfound), name);
1248 return FAIL;
1249 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001250 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001251
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001252 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001253}
1254
1255/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001256 * Generate an ISN_UNLET instruction.
1257 */
1258 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001259generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001260{
1261 isn_T *isn;
1262
1263 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001264 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001265 return FAIL;
1266 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1267 isn->isn_arg.unlet.ul_forceit = forceit;
1268
1269 return OK;
1270}
1271
1272/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 * Generate an ISN_LOADS instruction.
1274 */
1275 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001276generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001278 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280 int sid,
1281 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282{
1283 isn_T *isn;
1284
Bram Moolenaar080457c2020-03-03 21:53:32 +01001285 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286 if (isn_type == ISN_LOADS)
1287 isn = generate_instr_type(cctx, isn_type, type);
1288 else
1289 isn = generate_instr_drop(cctx, isn_type, 1);
1290 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001292 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1293 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001294
1295 return OK;
1296}
1297
1298/*
1299 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1300 */
1301 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001302generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 cctx_T *cctx,
1304 isntype_T isn_type,
1305 int sid,
1306 int idx,
1307 type_T *type)
1308{
1309 isn_T *isn;
1310
Bram Moolenaar080457c2020-03-03 21:53:32 +01001311 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 if (isn_type == ISN_LOADSCRIPT)
1313 isn = generate_instr_type(cctx, isn_type, type);
1314 else
1315 isn = generate_instr_drop(cctx, isn_type, 1);
1316 if (isn == NULL)
1317 return FAIL;
1318 isn->isn_arg.script.script_sid = sid;
1319 isn->isn_arg.script.script_idx = idx;
1320 return OK;
1321}
1322
1323/*
1324 * Generate an ISN_NEWLIST instruction.
1325 */
1326 static int
1327generate_NEWLIST(cctx_T *cctx, int count)
1328{
1329 isn_T *isn;
1330 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 type_T *type;
1332 type_T *member;
1333
Bram Moolenaar080457c2020-03-03 21:53:32 +01001334 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1336 return FAIL;
1337 isn->isn_arg.number = count;
1338
1339 // drop the value types
1340 stack->ga_len -= count;
1341
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001342 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001343 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if (count > 0)
1345 member = ((type_T **)stack->ga_data)[stack->ga_len];
1346 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001347 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001348 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349
1350 // add the list type to the type stack
1351 if (ga_grow(stack, 1) == FAIL)
1352 return FAIL;
1353 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1354 ++stack->ga_len;
1355
1356 return OK;
1357}
1358
1359/*
1360 * Generate an ISN_NEWDICT instruction.
1361 */
1362 static int
1363generate_NEWDICT(cctx_T *cctx, int count)
1364{
1365 isn_T *isn;
1366 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 type_T *type;
1368 type_T *member;
1369
Bram Moolenaar080457c2020-03-03 21:53:32 +01001370 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1372 return FAIL;
1373 isn->isn_arg.number = count;
1374
1375 // drop the key and value types
1376 stack->ga_len -= 2 * count;
1377
Bram Moolenaar436472f2020-02-20 22:54:43 +01001378 // Use the first value type for the list member type. Use "void" for an
1379 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 if (count > 0)
1381 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1382 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001383 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001384 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385
1386 // add the dict type to the type stack
1387 if (ga_grow(stack, 1) == FAIL)
1388 return FAIL;
1389 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1390 ++stack->ga_len;
1391
1392 return OK;
1393}
1394
1395/*
1396 * Generate an ISN_FUNCREF instruction.
1397 */
1398 static int
1399generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1400{
1401 isn_T *isn;
1402 garray_T *stack = &cctx->ctx_type_stack;
1403
Bram Moolenaar080457c2020-03-03 21:53:32 +01001404 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1406 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001407 isn->isn_arg.funcref.fr_func = dfunc_idx;
1408 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409
1410 if (ga_grow(stack, 1) == FAIL)
1411 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001412 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 // TODO: argument and return types
1414 ++stack->ga_len;
1415
1416 return OK;
1417}
1418
1419/*
1420 * Generate an ISN_JUMP instruction.
1421 */
1422 static int
1423generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1424{
1425 isn_T *isn;
1426 garray_T *stack = &cctx->ctx_type_stack;
1427
Bram Moolenaar080457c2020-03-03 21:53:32 +01001428 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1430 return FAIL;
1431 isn->isn_arg.jump.jump_when = when;
1432 isn->isn_arg.jump.jump_where = where;
1433
1434 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1435 --stack->ga_len;
1436
1437 return OK;
1438}
1439
1440 static int
1441generate_FOR(cctx_T *cctx, int loop_idx)
1442{
1443 isn_T *isn;
1444 garray_T *stack = &cctx->ctx_type_stack;
1445
Bram Moolenaar080457c2020-03-03 21:53:32 +01001446 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1448 return FAIL;
1449 isn->isn_arg.forloop.for_idx = loop_idx;
1450
1451 if (ga_grow(stack, 1) == FAIL)
1452 return FAIL;
1453 // type doesn't matter, will be stored next
1454 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1455 ++stack->ga_len;
1456
1457 return OK;
1458}
1459
1460/*
1461 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001462 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 * Return FAIL if the number of arguments is wrong.
1464 */
1465 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001466generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467{
1468 isn_T *isn;
1469 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001470 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001471 type_T *argtypes[MAX_FUNC_ARGS];
1472 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473
Bram Moolenaar080457c2020-03-03 21:53:32 +01001474 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001475 argoff = check_internal_func(func_idx, argcount);
1476 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477 return FAIL;
1478
Bram Moolenaar389df252020-07-09 21:20:47 +02001479 if (method_call && argoff > 1)
1480 {
1481 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1482 return FAIL;
1483 isn->isn_arg.shuffle.shfl_item = argcount;
1484 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1485 }
1486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1488 return FAIL;
1489 isn->isn_arg.bfunc.cbf_idx = func_idx;
1490 isn->isn_arg.bfunc.cbf_argcount = argcount;
1491
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001492 for (i = 0; i < argcount; ++i)
1493 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 stack->ga_len -= argcount; // drop the arguments
1496 if (ga_grow(stack, 1) == FAIL)
1497 return FAIL;
1498 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001499 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 ++stack->ga_len; // add return value
1501
1502 return OK;
1503}
1504
1505/*
1506 * Generate an ISN_DCALL or ISN_UCALL instruction.
1507 * Return FAIL if the number of arguments is wrong.
1508 */
1509 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001510generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511{
1512 isn_T *isn;
1513 garray_T *stack = &cctx->ctx_type_stack;
1514 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001515 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516
Bram Moolenaar080457c2020-03-03 21:53:32 +01001517 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 if (argcount > regular_args && !has_varargs(ufunc))
1519 {
1520 semsg(_(e_toomanyarg), ufunc->uf_name);
1521 return FAIL;
1522 }
1523 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1524 {
1525 semsg(_(e_toofewarg), ufunc->uf_name);
1526 return FAIL;
1527 }
1528
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001529 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001530 {
1531 int i;
1532
1533 for (i = 0; i < argcount; ++i)
1534 {
1535 type_T *expected;
1536 type_T *actual;
1537
1538 if (i < regular_args)
1539 {
1540 if (ufunc->uf_arg_types == NULL)
1541 continue;
1542 expected = ufunc->uf_arg_types[i];
1543 }
1544 else
1545 expected = ufunc->uf_va_type->tt_member;
1546 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001547 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001548 {
1549 arg_type_mismatch(expected, actual, i + 1);
1550 return FAIL;
1551 }
1552 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001553 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001554 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001555 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001556 }
1557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001559 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001560 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001562 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 {
1564 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1565 isn->isn_arg.dfunc.cdf_argcount = argcount;
1566 }
1567 else
1568 {
1569 // A user function may be deleted and redefined later, can't use the
1570 // ufunc pointer, need to look it up again at runtime.
1571 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1572 isn->isn_arg.ufunc.cuf_argcount = argcount;
1573 }
1574
1575 stack->ga_len -= argcount; // drop the arguments
1576 if (ga_grow(stack, 1) == FAIL)
1577 return FAIL;
1578 // add return value
1579 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1580 ++stack->ga_len;
1581
1582 return OK;
1583}
1584
1585/*
1586 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1587 */
1588 static int
1589generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1590{
1591 isn_T *isn;
1592 garray_T *stack = &cctx->ctx_type_stack;
1593
Bram Moolenaar080457c2020-03-03 21:53:32 +01001594 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1596 return FAIL;
1597 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1598 isn->isn_arg.ufunc.cuf_argcount = argcount;
1599
1600 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001601 if (ga_grow(stack, 1) == FAIL)
1602 return FAIL;
1603 // add return value
1604 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1605 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606
1607 return OK;
1608}
1609
1610/*
1611 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001612 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 */
1614 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001615generate_PCALL(
1616 cctx_T *cctx,
1617 int argcount,
1618 char_u *name,
1619 type_T *type,
1620 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621{
1622 isn_T *isn;
1623 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001624 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625
Bram Moolenaar080457c2020-03-03 21:53:32 +01001626 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001627
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001628 if (type->tt_type == VAR_ANY)
1629 ret_type = &t_any;
1630 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001631 {
1632 if (type->tt_argcount != -1)
1633 {
1634 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1635
1636 if (argcount < type->tt_min_argcount - varargs)
1637 {
1638 semsg(_(e_toofewarg), "[reference]");
1639 return FAIL;
1640 }
1641 if (!varargs && argcount > type->tt_argcount)
1642 {
1643 semsg(_(e_toomanyarg), "[reference]");
1644 return FAIL;
1645 }
1646 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001647 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001648 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001649 else
1650 {
1651 semsg(_("E1085: Not a callable type: %s"), name);
1652 return FAIL;
1653 }
1654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1656 return FAIL;
1657 isn->isn_arg.pfunc.cpf_top = at_top;
1658 isn->isn_arg.pfunc.cpf_argcount = argcount;
1659
1660 stack->ga_len -= argcount; // drop the arguments
1661
1662 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001663 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001665 // If partial is above the arguments it must be cleared and replaced with
1666 // the return value.
1667 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1668 return FAIL;
1669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 return OK;
1671}
1672
1673/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001674 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 */
1676 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001677generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678{
1679 isn_T *isn;
1680 garray_T *stack = &cctx->ctx_type_stack;
1681 type_T *type;
1682
Bram Moolenaar080457c2020-03-03 21:53:32 +01001683 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001684 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001686 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001688 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001690 if (type->tt_type != VAR_DICT && type != &t_any)
1691 {
1692 emsg(_(e_dictreq));
1693 return FAIL;
1694 }
1695 // change dict type to dict member type
1696 if (type->tt_type == VAR_DICT)
1697 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698
1699 return OK;
1700}
1701
1702/*
1703 * Generate an ISN_ECHO instruction.
1704 */
1705 static int
1706generate_ECHO(cctx_T *cctx, int with_white, int count)
1707{
1708 isn_T *isn;
1709
Bram Moolenaar080457c2020-03-03 21:53:32 +01001710 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1712 return FAIL;
1713 isn->isn_arg.echo.echo_with_white = with_white;
1714 isn->isn_arg.echo.echo_count = count;
1715
1716 return OK;
1717}
1718
Bram Moolenaarad39c092020-02-26 18:23:43 +01001719/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001720 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001721 */
1722 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001723generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001724{
1725 isn_T *isn;
1726
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001727 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001728 return FAIL;
1729 isn->isn_arg.number = count;
1730
1731 return OK;
1732}
1733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 static int
1735generate_EXEC(cctx_T *cctx, char_u *line)
1736{
1737 isn_T *isn;
1738
Bram Moolenaar080457c2020-03-03 21:53:32 +01001739 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1741 return FAIL;
1742 isn->isn_arg.string = vim_strsave(line);
1743 return OK;
1744}
1745
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001746 static int
1747generate_EXECCONCAT(cctx_T *cctx, int count)
1748{
1749 isn_T *isn;
1750
1751 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1752 return FAIL;
1753 isn->isn_arg.number = count;
1754 return OK;
1755}
1756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757/*
1758 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001759 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001761 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1763{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 lvar_T *lvar;
1765
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001766 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001768 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001769 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 }
1771
1772 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001773 return NULL;
1774 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001776 // Every local variable uses the next entry on the stack. We could re-use
1777 // the last ones when leaving a scope, but then variables used in a closure
1778 // might get overwritten. To keep things simple do not re-use stack
1779 // entries. This is less efficient, but memory is cheap these days.
1780 lvar->lv_idx = cctx->ctx_locals_count++;
1781
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001782 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 lvar->lv_const = isConst;
1784 lvar->lv_type = type;
1785
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001786 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787}
1788
1789/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001790 * Remove local variables above "new_top".
1791 */
1792 static void
1793unwind_locals(cctx_T *cctx, int new_top)
1794{
1795 if (cctx->ctx_locals.ga_len > new_top)
1796 {
1797 int idx;
1798 lvar_T *lvar;
1799
1800 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1801 {
1802 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1803 vim_free(lvar->lv_name);
1804 }
1805 }
1806 cctx->ctx_locals.ga_len = new_top;
1807}
1808
1809/*
1810 * Free all local variables.
1811 */
1812 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001813free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001814{
1815 unwind_locals(cctx, 0);
1816 ga_clear(&cctx->ctx_locals);
1817}
1818
1819/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 * Skip over a type definition and return a pointer to just after it.
1821 */
1822 char_u *
1823skip_type(char_u *start)
1824{
1825 char_u *p = start;
1826
1827 while (ASCII_ISALNUM(*p) || *p == '_')
1828 ++p;
1829
1830 // Skip over "<type>"; this is permissive about white space.
1831 if (*skipwhite(p) == '<')
1832 {
1833 p = skipwhite(p);
1834 p = skip_type(skipwhite(p + 1));
1835 p = skipwhite(p);
1836 if (*p == '>')
1837 ++p;
1838 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001839 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1840 {
1841 // handle func(args): type
1842 ++p;
1843 while (*p != ')' && *p != NUL)
1844 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001845 char_u *sp = p;
1846
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001847 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001848 if (p == sp)
1849 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001850 if (*p == ',')
1851 p = skipwhite(p + 1);
1852 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001853 if (*p == ')')
1854 {
1855 if (p[1] == ':')
1856 p = skip_type(skipwhite(p + 2));
1857 else
1858 p = skipwhite(p + 1);
1859 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001860 }
1861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 return p;
1863}
1864
1865/*
1866 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001867 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 * Returns NULL in case of failure.
1869 */
1870 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001871parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872{
1873 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001874 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875
1876 if (**arg != '<')
1877 {
1878 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001879 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 else
1881 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001882 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 }
1884 *arg = skipwhite(*arg + 1);
1885
Bram Moolenaard77a8522020-04-03 21:59:57 +02001886 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887
1888 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001889 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 {
1891 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001892 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 }
1894 ++*arg;
1895
1896 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001897 return get_list_type(member_type, type_gap);
1898 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899}
1900
1901/*
1902 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001903 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 */
1905 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001906parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907{
1908 char_u *p = *arg;
1909 size_t len;
1910
1911 // skip over the first word
1912 while (ASCII_ISALNUM(*p) || *p == '_')
1913 ++p;
1914 len = p - *arg;
1915
1916 switch (**arg)
1917 {
1918 case 'a':
1919 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1920 {
1921 *arg += len;
1922 return &t_any;
1923 }
1924 break;
1925 case 'b':
1926 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1927 {
1928 *arg += len;
1929 return &t_bool;
1930 }
1931 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1932 {
1933 *arg += len;
1934 return &t_blob;
1935 }
1936 break;
1937 case 'c':
1938 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1939 {
1940 *arg += len;
1941 return &t_channel;
1942 }
1943 break;
1944 case 'd':
1945 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1946 {
1947 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001948 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 }
1950 break;
1951 case 'f':
1952 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1953 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001954#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 *arg += len;
1956 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001957#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001958 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001959 return &t_any;
1960#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 }
1962 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1963 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001964 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001965 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001966 int argcount = -1;
1967 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001968 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001969 type_T *arg_type[MAX_FUNC_ARGS + 1];
1970
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001971 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001973 if (**arg == '(')
1974 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001975 // "func" may or may not return a value, "func()" does
1976 // not return a value.
1977 ret_type = &t_void;
1978
Bram Moolenaard77a8522020-04-03 21:59:57 +02001979 p = ++*arg;
1980 argcount = 0;
1981 while (*p != NUL && *p != ')')
1982 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001983 if (*p == '?')
1984 {
1985 if (first_optional == -1)
1986 first_optional = argcount;
1987 ++p;
1988 }
1989 else if (first_optional != -1)
1990 {
1991 emsg(_("E1007: mandatory argument after optional argument"));
1992 return &t_any;
1993 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001994 else if (STRNCMP(p, "...", 3) == 0)
1995 {
1996 flags |= TTFLAG_VARARGS;
1997 p += 3;
1998 }
1999
2000 arg_type[argcount++] = parse_type(&p, type_gap);
2001
2002 // Nothing comes after "...{type}".
2003 if (flags & TTFLAG_VARARGS)
2004 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002005
Bram Moolenaard77a8522020-04-03 21:59:57 +02002006 if (*p != ',' && *skipwhite(p) == ',')
2007 {
2008 semsg(_(e_no_white_before), ",");
2009 return &t_any;
2010 }
2011 if (*p == ',')
2012 {
2013 ++p;
2014 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002015 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002016 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002017 return &t_any;
2018 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002019 }
2020 p = skipwhite(p);
2021 if (argcount == MAX_FUNC_ARGS)
2022 {
2023 emsg(_("E740: Too many argument types"));
2024 return &t_any;
2025 }
2026 }
2027
2028 p = skipwhite(p);
2029 if (*p != ')')
2030 {
2031 emsg(_(e_missing_close));
2032 return &t_any;
2033 }
2034 *arg = p + 1;
2035 }
2036 if (**arg == ':')
2037 {
2038 // parse return type
2039 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002040 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002041 semsg(_(e_white_after), ":");
2042 *arg = skipwhite(*arg);
2043 ret_type = parse_type(arg, type_gap);
2044 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002045 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002046 type = get_func_type(ret_type, argcount, type_gap);
2047 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002048 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002049 type = alloc_func_type(ret_type, argcount, type_gap);
2050 type->tt_flags = flags;
2051 if (argcount > 0)
2052 {
2053 type->tt_argcount = argcount;
2054 type->tt_min_argcount = first_optional == -1
2055 ? argcount : first_optional;
2056 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002057 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002058 return &t_any;
2059 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002060 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002061 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002062 }
2063 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 }
2065 break;
2066 case 'j':
2067 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2068 {
2069 *arg += len;
2070 return &t_job;
2071 }
2072 break;
2073 case 'l':
2074 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2075 {
2076 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002077 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 }
2079 break;
2080 case 'n':
2081 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2082 {
2083 *arg += len;
2084 return &t_number;
2085 }
2086 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 case 's':
2088 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2089 {
2090 *arg += len;
2091 return &t_string;
2092 }
2093 break;
2094 case 'v':
2095 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2096 {
2097 *arg += len;
2098 return &t_void;
2099 }
2100 break;
2101 }
2102
2103 semsg(_("E1010: Type not recognized: %s"), *arg);
2104 return &t_any;
2105}
2106
2107/*
2108 * Check if "type1" and "type2" are exactly the same.
2109 */
2110 static int
2111equal_type(type_T *type1, type_T *type2)
2112{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002113 int i;
2114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 if (type1->tt_type != type2->tt_type)
2116 return FALSE;
2117 switch (type1->tt_type)
2118 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002120 case VAR_ANY:
2121 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 case VAR_SPECIAL:
2123 case VAR_BOOL:
2124 case VAR_NUMBER:
2125 case VAR_FLOAT:
2126 case VAR_STRING:
2127 case VAR_BLOB:
2128 case VAR_JOB:
2129 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002130 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 case VAR_LIST:
2132 case VAR_DICT:
2133 return equal_type(type1->tt_member, type2->tt_member);
2134 case VAR_FUNC:
2135 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002136 if (!equal_type(type1->tt_member, type2->tt_member)
2137 || type1->tt_argcount != type2->tt_argcount)
2138 return FALSE;
2139 if (type1->tt_argcount < 0
2140 || type1->tt_args == NULL || type2->tt_args == NULL)
2141 return TRUE;
2142 for (i = 0; i < type1->tt_argcount; ++i)
2143 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2144 return FALSE;
2145 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 }
2147 return TRUE;
2148}
2149
2150/*
2151 * Find the common type of "type1" and "type2" and put it in "dest".
2152 * "type2" and "dest" may be the same.
2153 */
2154 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002155common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156{
2157 if (equal_type(type1, type2))
2158 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002159 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 return;
2161 }
2162
2163 if (type1->tt_type == type2->tt_type)
2164 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2166 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002167 type_T *common;
2168
Bram Moolenaard77a8522020-04-03 21:59:57 +02002169 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002170 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002171 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002172 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002173 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 return;
2175 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002176 if (type1->tt_type == VAR_FUNC)
2177 {
2178 type_T *common;
2179
2180 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2181 if (type1->tt_argcount == type2->tt_argcount
2182 && type1->tt_argcount >= 0)
2183 {
2184 int argcount = type1->tt_argcount;
2185 int i;
2186
2187 *dest = alloc_func_type(common, argcount, type_gap);
2188 if (type1->tt_args != NULL && type2->tt_args != NULL)
2189 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002190 if (func_type_add_arg_types(*dest, argcount,
2191 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002192 for (i = 0; i < argcount; ++i)
2193 common_type(type1->tt_args[i], type2->tt_args[i],
2194 &(*dest)->tt_args[i], type_gap);
2195 }
2196 }
2197 else
2198 *dest = alloc_func_type(common, -1, type_gap);
2199 return;
2200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 }
2202
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002203 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204}
2205
2206 char *
2207vartype_name(vartype_T type)
2208{
2209 switch (type)
2210 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002211 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002212 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 case VAR_SPECIAL: return "special";
2215 case VAR_BOOL: return "bool";
2216 case VAR_NUMBER: return "number";
2217 case VAR_FLOAT: return "float";
2218 case VAR_STRING: return "string";
2219 case VAR_BLOB: return "blob";
2220 case VAR_JOB: return "job";
2221 case VAR_CHANNEL: return "channel";
2222 case VAR_LIST: return "list";
2223 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002224
2225 case VAR_FUNC:
2226 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002228 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229}
2230
2231/*
2232 * Return the name of a type.
2233 * The result may be in allocated memory, in which case "tofree" is set.
2234 */
2235 char *
2236type_name(type_T *type, char **tofree)
2237{
2238 char *name = vartype_name(type->tt_type);
2239
2240 *tofree = NULL;
2241 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2242 {
2243 char *member_free;
2244 char *member_name = type_name(type->tt_member, &member_free);
2245 size_t len;
2246
2247 len = STRLEN(name) + STRLEN(member_name) + 3;
2248 *tofree = alloc(len);
2249 if (*tofree != NULL)
2250 {
2251 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2252 vim_free(member_free);
2253 return *tofree;
2254 }
2255 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002256 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002257 {
2258 garray_T ga;
2259 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002260 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002261
2262 ga_init2(&ga, 1, 100);
2263 if (ga_grow(&ga, 20) == FAIL)
2264 return "[unknown]";
2265 *tofree = ga.ga_data;
2266 STRCPY(ga.ga_data, "func(");
2267 ga.ga_len += 5;
2268
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002269 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002270 {
2271 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002272 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002273 int len;
2274
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002275 if (type->tt_args == NULL)
2276 arg_type = "[unknown]";
2277 else
2278 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002279 if (i > 0)
2280 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002281 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002282 ga.ga_len += 2;
2283 }
2284 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002285 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002286 {
2287 vim_free(arg_free);
2288 return "[unknown]";
2289 }
2290 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002291 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002292 {
2293 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2294 ga.ga_len += 3;
2295 }
2296 else if (i >= type->tt_min_argcount)
2297 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002298 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002299 ga.ga_len += len;
2300 vim_free(arg_free);
2301 }
2302
2303 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002304 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002305 else
2306 {
2307 char *ret_free;
2308 char *ret_name = type_name(type->tt_member, &ret_free);
2309 int len;
2310
2311 len = (int)STRLEN(ret_name) + 4;
2312 if (ga_grow(&ga, len) == FAIL)
2313 {
2314 vim_free(ret_free);
2315 return "[unknown]";
2316 }
2317 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002318 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2319 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002320 vim_free(ret_free);
2321 }
2322 return ga.ga_data;
2323 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324
2325 return name;
2326}
2327
2328/*
2329 * Find "name" in script-local items of script "sid".
2330 * Returns the index in "sn_var_vals" if found.
2331 * If found but not in "sn_var_vals" returns -1.
2332 * If not found returns -2.
2333 */
2334 int
2335get_script_item_idx(int sid, char_u *name, int check_writable)
2336{
2337 hashtab_T *ht;
2338 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002339 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 int idx;
2341
2342 // First look the name up in the hashtable.
2343 if (sid <= 0 || sid > script_items.ga_len)
2344 return -1;
2345 ht = &SCRIPT_VARS(sid);
2346 di = find_var_in_ht(ht, 0, name, TRUE);
2347 if (di == NULL)
2348 return -2;
2349
2350 // Now find the svar_T index in sn_var_vals.
2351 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2352 {
2353 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2354
2355 if (sv->sv_tv == &di->di_tv)
2356 {
2357 if (check_writable && sv->sv_const)
2358 semsg(_(e_readonlyvar), name);
2359 return idx;
2360 }
2361 }
2362 return -1;
2363}
2364
2365/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002366 * Find "name" in imported items of the current script or in "cctx" if not
2367 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 */
2369 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002370find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002372 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 int idx;
2374
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002375 if (current_sctx.sc_sid <= 0)
2376 return NULL;
2377 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 if (cctx != NULL)
2379 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2380 {
2381 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2382 + idx;
2383
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002384 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2385 : STRLEN(import->imp_name) == len
2386 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 return import;
2388 }
2389
2390 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2391 {
2392 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2393
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002394 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2395 : STRLEN(import->imp_name) == len
2396 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 return import;
2398 }
2399 return NULL;
2400}
2401
2402/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002403 * Free all imported variables.
2404 */
2405 static void
2406free_imported(cctx_T *cctx)
2407{
2408 int idx;
2409
2410 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2411 {
2412 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2413
2414 vim_free(import->imp_name);
2415 }
2416 ga_clear(&cctx->ctx_imports);
2417}
2418
2419/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002420 * Return TRUE if "p" points at a "#" but not at "#{".
2421 */
2422 static int
2423comment_start(char_u *p)
2424{
2425 return p[0] == '#' && p[1] != '{';
2426}
2427
2428/*
2429 * Return a pointer to the next line that isn't empty or only contains a
2430 * comment. Skips over white space.
2431 * Returns NULL if there is none.
2432 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002433 char_u *
2434peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002435{
2436 int lnum = cctx->ctx_lnum;
2437
2438 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2439 {
2440 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002441 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002442
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002443 if (line == NULL)
2444 break;
2445 p = skipwhite(line);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002446 if (*p != NUL && !comment_start(p))
2447 return p;
2448 }
2449 return NULL;
2450}
2451
2452/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002453 * Called when checking for a following operator at "arg". When the rest of
2454 * the line is empty or only a comment, peek the next line. If there is a next
2455 * line return a pointer to it and set "nextp".
2456 * Otherwise skip over white space.
2457 */
2458 static char_u *
2459may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2460{
2461 char_u *p = skipwhite(arg);
2462
2463 *nextp = NULL;
2464 if (*p == NUL || (VIM_ISWHITE(*arg) && comment_start(p)))
2465 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002466 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002467 if (*nextp != NULL)
2468 return *nextp;
2469 }
2470 return p;
2471}
2472
2473/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002474 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002475 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002476 * Returns NULL when at the end.
2477 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002478 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002479next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002480{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002481 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002482
2483 do
2484 {
2485 ++cctx->ctx_lnum;
2486 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002487 {
2488 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002489 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002490 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002491 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002492 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002493 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002494 } while (line == NULL || *skipwhite(line) == NUL
2495 || (skip_comment && comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002496 return line;
2497}
2498
2499/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002500 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002501 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002502 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2503 */
2504 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002505may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002506{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002507 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002508 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002509 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002510
2511 if (next == NULL)
2512 return FAIL;
2513 *arg = skipwhite(next);
2514 }
2515 return OK;
2516}
2517
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002518/*
2519 * Idem, and give an error when failed.
2520 */
2521 static int
2522may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2523{
2524 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2525 {
2526 emsg(_("E1097: line incomplete"));
2527 return FAIL;
2528 }
2529 return OK;
2530}
2531
2532
Bram Moolenaara5565e42020-05-09 15:44:01 +02002533// Structure passed between the compile_expr* functions to keep track of
2534// constants that have been parsed but for which no code was produced yet. If
2535// possible expressions on these constants are applied at compile time. If
2536// that is not possible, the code to push the constants needs to be generated
2537// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002538// Using 50 should be more than enough of 5 levels of ().
2539#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002540typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002541 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002542 int pp_used; // active entries in pp_tv[]
2543} ppconst_T;
2544
Bram Moolenaar1c747212020-05-09 18:28:34 +02002545static int compile_expr0(char_u **arg, cctx_T *cctx);
2546static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2547
Bram Moolenaara5565e42020-05-09 15:44:01 +02002548/*
2549 * Generate a PUSH instruction for "tv".
2550 * "tv" will be consumed or cleared.
2551 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2552 */
2553 static int
2554generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2555{
2556 if (tv != NULL)
2557 {
2558 switch (tv->v_type)
2559 {
2560 case VAR_UNKNOWN:
2561 break;
2562 case VAR_BOOL:
2563 generate_PUSHBOOL(cctx, tv->vval.v_number);
2564 break;
2565 case VAR_SPECIAL:
2566 generate_PUSHSPEC(cctx, tv->vval.v_number);
2567 break;
2568 case VAR_NUMBER:
2569 generate_PUSHNR(cctx, tv->vval.v_number);
2570 break;
2571#ifdef FEAT_FLOAT
2572 case VAR_FLOAT:
2573 generate_PUSHF(cctx, tv->vval.v_float);
2574 break;
2575#endif
2576 case VAR_BLOB:
2577 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2578 tv->vval.v_blob = NULL;
2579 break;
2580 case VAR_STRING:
2581 generate_PUSHS(cctx, tv->vval.v_string);
2582 tv->vval.v_string = NULL;
2583 break;
2584 default:
2585 iemsg("constant type not supported");
2586 clear_tv(tv);
2587 return FAIL;
2588 }
2589 tv->v_type = VAR_UNKNOWN;
2590 }
2591 return OK;
2592}
2593
2594/*
2595 * Generate code for any ppconst entries.
2596 */
2597 static int
2598generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2599{
2600 int i;
2601 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002602 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002603
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002604 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002605 for (i = 0; i < ppconst->pp_used; ++i)
2606 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2607 ret = FAIL;
2608 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002609 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002610 return ret;
2611}
2612
2613/*
2614 * Clear ppconst constants. Used when failing.
2615 */
2616 static void
2617clear_ppconst(ppconst_T *ppconst)
2618{
2619 int i;
2620
2621 for (i = 0; i < ppconst->pp_used; ++i)
2622 clear_tv(&ppconst->pp_tv[i]);
2623 ppconst->pp_used = 0;
2624}
2625
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002626/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002627 * Generate an instruction to load script-local variable "name", without the
2628 * leading "s:".
2629 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 */
2631 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002632compile_load_scriptvar(
2633 cctx_T *cctx,
2634 char_u *name, // variable NUL terminated
2635 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002636 char_u **end, // end of variable
2637 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002639 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2641 imported_T *import;
2642
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002643 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002645 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002646 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2647 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 }
2649 if (idx >= 0)
2650 {
2651 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2652
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002653 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 current_sctx.sc_sid, idx, sv->sv_type);
2655 return OK;
2656 }
2657
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002658 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 if (import != NULL)
2660 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002661 if (import->imp_all)
2662 {
2663 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002664 char_u *exp_name;
2665 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002666 ufunc_T *ufunc;
2667 type_T *type;
2668
2669 // Used "import * as Name", need to lookup the member.
2670 if (*p != '.')
2671 {
2672 semsg(_("E1060: expected dot after name: %s"), start);
2673 return FAIL;
2674 }
2675 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002676 if (VIM_ISWHITE(*p))
2677 {
2678 emsg(_("E1074: no white space allowed after dot"));
2679 return FAIL;
2680 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002681
Bram Moolenaar1c991142020-07-04 13:15:31 +02002682 // isolate one name
2683 exp_name = p;
2684 while (eval_isnamec(*p))
2685 ++p;
2686 cc = *p;
2687 *p = NUL;
2688
2689 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2690 *p = cc;
2691 p = skipwhite(p);
2692
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002693 // TODO: what if it is a function?
2694 if (idx < 0)
2695 return FAIL;
2696 *end = p;
2697
2698 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2699 import->imp_sid,
2700 idx,
2701 type);
2702 }
2703 else
2704 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002705 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002706 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2707 import->imp_sid,
2708 import->imp_var_vals_idx,
2709 import->imp_type);
2710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 return OK;
2712 }
2713
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002714 if (error)
2715 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 return FAIL;
2717}
2718
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002719 static int
2720generate_funcref(cctx_T *cctx, char_u *name)
2721{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002722 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002723
2724 if (ufunc == NULL)
2725 return FAIL;
2726
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002727 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2728 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002729}
2730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731/*
2732 * Compile a variable name into a load instruction.
2733 * "end" points to just after the name.
2734 * When "error" is FALSE do not give an error when not found.
2735 */
2736 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002737compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738{
2739 type_T *type;
2740 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002741 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002743 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744
2745 if (*(*arg + 1) == ':')
2746 {
2747 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002748 if (end <= *arg + 2)
2749 name = vim_strsave((char_u *)"[empty]");
2750 else
2751 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 if (name == NULL)
2753 return FAIL;
2754
2755 if (**arg == 'v')
2756 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002757 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 }
2759 else if (**arg == 'g')
2760 {
2761 // Global variables can be defined later, thus we don't check if it
2762 // exists, give error at runtime.
2763 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2764 }
2765 else if (**arg == 's')
2766 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002767 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002769 else if (**arg == 'b')
2770 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002771 // Buffer-local variables can be defined later, thus we don't check
2772 // if it exists, give error at runtime.
2773 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002774 }
2775 else if (**arg == 'w')
2776 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002777 // Window-local variables can be defined later, thus we don't check
2778 // if it exists, give error at runtime.
2779 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002780 }
2781 else if (**arg == 't')
2782 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002783 // Tabpage-local variables can be defined later, thus we don't
2784 // check if it exists, give error at runtime.
2785 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 else
2788 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002789 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 goto theend;
2791 }
2792 }
2793 else
2794 {
2795 size_t len = end - *arg;
2796 int idx;
2797 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002798 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799
2800 name = vim_strnsave(*arg, end - *arg);
2801 if (name == NULL)
2802 return FAIL;
2803
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002804 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002806 if (!gen_load_outer)
2807 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 }
2809 else
2810 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002811 lvar_T *lvar = lookup_local(*arg, len, cctx);
2812
2813 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002815 type = lvar->lv_type;
2816 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002817 if (lvar->lv_from_outer)
2818 gen_load_outer = TRUE;
2819 else
2820 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 }
2822 else
2823 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002824 // "var" can be script-local even without using "s:" if it
2825 // already exists.
2826 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2827 == SCRIPT_VERSION_VIM9
2828 || lookup_script(*arg, len) == OK)
2829 res = compile_load_scriptvar(cctx, name, *arg, &end,
2830 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002831
Bram Moolenaara5565e42020-05-09 15:44:01 +02002832 // When the name starts with an uppercase letter or "x:" it
2833 // can be a user defined function.
2834 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2835 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 }
2837 }
2838 if (gen_load)
2839 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002840 if (gen_load_outer)
2841 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 }
2843
2844 *arg = end;
2845
2846theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002847 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 semsg(_(e_var_notfound), name);
2849 vim_free(name);
2850 return res;
2851}
2852
2853/*
2854 * Compile the argument expressions.
2855 * "arg" points to just after the "(" and is advanced to after the ")"
2856 */
2857 static int
2858compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2859{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002860 char_u *p = *arg;
2861 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862
Bram Moolenaare6085c52020-04-12 20:19:16 +02002863 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002865 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2866 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002867 if (*p == ')')
2868 {
2869 *arg = p + 1;
2870 return OK;
2871 }
2872
Bram Moolenaara5565e42020-05-09 15:44:01 +02002873 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 return FAIL;
2875 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002876
2877 if (*p != ',' && *skipwhite(p) == ',')
2878 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002879 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002880 p = skipwhite(p);
2881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002883 {
2884 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002885 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002886 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002887 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002888 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002889 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002891failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002892 emsg(_(e_missing_close));
2893 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894}
2895
2896/*
2897 * Compile a function call: name(arg1, arg2)
2898 * "arg" points to "name", "arg + varlen" to the "(".
2899 * "argcount_init" is 1 for "value->method()"
2900 * Instructions:
2901 * EVAL arg1
2902 * EVAL arg2
2903 * BCALL / DCALL / UCALL
2904 */
2905 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002906compile_call(
2907 char_u **arg,
2908 size_t varlen,
2909 cctx_T *cctx,
2910 ppconst_T *ppconst,
2911 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912{
2913 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002914 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 int argcount = argcount_init;
2916 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002917 char_u fname_buf[FLEN_FIXED + 1];
2918 char_u *tofree = NULL;
2919 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002921 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922
Bram Moolenaara5565e42020-05-09 15:44:01 +02002923 // we can evaluate "has('name')" at compile time
2924 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2925 {
2926 char_u *s = skipwhite(*arg + varlen + 1);
2927 typval_T argvars[2];
2928
2929 argvars[0].v_type = VAR_UNKNOWN;
2930 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002931 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002932 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002933 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002934 s = skipwhite(s);
2935 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2936 {
2937 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2938
2939 *arg = s + 1;
2940 argvars[1].v_type = VAR_UNKNOWN;
2941 tv->v_type = VAR_NUMBER;
2942 tv->vval.v_number = 0;
2943 f_has(argvars, tv);
2944 clear_tv(&argvars[0]);
2945 ++ppconst->pp_used;
2946 return OK;
2947 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002948 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002949 }
2950
2951 if (generate_ppconst(cctx, ppconst) == FAIL)
2952 return FAIL;
2953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 if (varlen >= sizeof(namebuf))
2955 {
2956 semsg(_("E1011: name too long: %s"), name);
2957 return FAIL;
2958 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002959 vim_strncpy(namebuf, *arg, varlen);
2960 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961
2962 *arg = skipwhite(*arg + varlen + 1);
2963 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002964 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002966 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 {
2968 int idx;
2969
2970 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002971 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002973 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002974 else
2975 semsg(_(e_unknownfunc), namebuf);
2976 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 }
2978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002980 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002982 {
2983 res = generate_CALL(cctx, ufunc, argcount);
2984 goto theend;
2985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986
2987 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002988 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002990 if (STRNCMP(namebuf, "g:", 2) != 0
2991 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002992 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002993 garray_T *stack = &cctx->ctx_type_stack;
2994 type_T *type;
2995
2996 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2997 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002998 goto theend;
2999 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003001 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003002 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003003 if (STRNCMP(namebuf, "g:", 2) == 0)
3004 res = generate_UCALL(cctx, name, argcount);
3005 else
3006 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003007
3008theend:
3009 vim_free(tofree);
3010 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011}
3012
3013// like NAMESPACE_CHAR but with 'a' and 'l'.
3014#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3015
3016/*
3017 * Find the end of a variable or function name. Unlike find_name_end() this
3018 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003019 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 * Return a pointer to just after the name. Equal to "arg" if there is no
3021 * valid name.
3022 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003023 static char_u *
3024to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025{
3026 char_u *p;
3027
3028 // Quick check for valid starting character.
3029 if (!eval_isnamec1(*arg))
3030 return arg;
3031
3032 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3033 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3034 // and can be used in slice "[n:]".
3035 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003036 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3038 break;
3039 return p;
3040}
3041
3042/*
3043 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003044 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 */
3046 char_u *
3047to_name_const_end(char_u *arg)
3048{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003049 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 typval_T rettv;
3051
3052 if (p == arg && *arg == '[')
3053 {
3054
3055 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003056 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 p = arg;
3058 }
3059 else if (p == arg && *arg == '#' && arg[1] == '{')
3060 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003061 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003063 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 p = arg;
3065 }
3066 else if (p == arg && *arg == '{')
3067 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003068 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003070 // Can be "{x -> ret}()".
3071 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003073 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 if (ret != OK)
3075 p = arg;
3076 }
3077
3078 return p;
3079}
3080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081/*
3082 * parse a list: [expr, expr]
3083 * "*arg" points to the '['.
3084 */
3085 static int
3086compile_list(char_u **arg, cctx_T *cctx)
3087{
3088 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003089 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 int count = 0;
3091
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003092 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003094 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003095 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003096 semsg(_(e_list_end), *arg);
3097 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003098 }
3099 if (*p == ']')
3100 {
3101 ++p;
3102 // Allow for following comment, after at least one space.
3103 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
3104 p += STRLEN(p);
3105 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003106 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003107 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 break;
3109 ++count;
3110 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003113 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3114 {
3115 semsg(_(e_white_after), ",");
3116 return FAIL;
3117 }
3118 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003119 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 p = skipwhite(p);
3121 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003122 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123
3124 generate_NEWLIST(cctx, count);
3125 return OK;
3126}
3127
3128/*
3129 * parse a lambda: {arg, arg -> expr}
3130 * "*arg" points to the '{'.
3131 */
3132 static int
3133compile_lambda(char_u **arg, cctx_T *cctx)
3134{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 typval_T rettv;
3136 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003137 evalarg_T evalarg;
3138
3139 CLEAR_FIELD(evalarg);
3140 evalarg.eval_flags = EVAL_EVALUATE;
3141 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142
3143 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003144 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003148 ++ufunc->uf_refcount;
3149 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003150 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151
3152 // The function will have one line: "return {expr}".
3153 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003154 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003156 clear_evalarg(&evalarg, NULL);
3157
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003158 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003159 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 return FAIL;
3161}
3162
3163/*
3164 * Compile a lamda call: expr->{lambda}(args)
3165 * "arg" points to the "{".
3166 */
3167 static int
3168compile_lambda_call(char_u **arg, cctx_T *cctx)
3169{
3170 ufunc_T *ufunc;
3171 typval_T rettv;
3172 int argcount = 1;
3173 int ret = FAIL;
3174
3175 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003176 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 return FAIL;
3178
3179 if (**arg != '(')
3180 {
3181 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003182 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 else
3184 semsg(_(e_missing_paren), "lambda");
3185 clear_tv(&rettv);
3186 return FAIL;
3187 }
3188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 ufunc = rettv.vval.v_partial->pt_func;
3190 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003191 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003192 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003193
3194 // The function will have one line: "return {expr}".
3195 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003196 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197
3198 // compile the arguments
3199 *arg = skipwhite(*arg + 1);
3200 if (compile_arguments(arg, cctx, &argcount) == OK)
3201 // call the compiled function
3202 ret = generate_CALL(cctx, ufunc, argcount);
3203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 return ret;
3205}
3206
3207/*
3208 * parse a dict: {'key': val} or #{key: val}
3209 * "*arg" points to the '{'.
3210 */
3211 static int
3212compile_dict(char_u **arg, cctx_T *cctx, int literal)
3213{
3214 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003215 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 int count = 0;
3217 dict_T *d = dict_alloc();
3218 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003219 char_u *whitep = *arg;
3220 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221
3222 if (d == NULL)
3223 return FAIL;
3224 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003225 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 {
3227 char_u *key = NULL;
3228
Bram Moolenaar23c55272020-06-21 16:58:13 +02003229 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003230 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003231 *arg = NULL;
3232 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003233 }
3234
3235 if (**arg == '}')
3236 break;
3237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 if (literal)
3239 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003240 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241
Bram Moolenaar2c330432020-04-13 14:41:35 +02003242 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 {
3244 semsg(_("E1014: Invalid key: %s"), *arg);
3245 return FAIL;
3246 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003247 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 if (generate_PUSHS(cctx, key) == FAIL)
3249 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003250 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 }
3252 else
3253 {
3254 isn_T *isn;
3255
Bram Moolenaara5565e42020-05-09 15:44:01 +02003256 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3259 if (isn->isn_type == ISN_PUSHS)
3260 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003261 else
3262 {
3263 type_T *keytype = ((type_T **)stack->ga_data)
3264 [stack->ga_len - 1];
3265 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3266 return FAIL;
3267 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 }
3269
3270 // Check for duplicate keys, if using string keys.
3271 if (key != NULL)
3272 {
3273 item = dict_find(d, key, -1);
3274 if (item != NULL)
3275 {
3276 semsg(_(e_duplicate_key), key);
3277 goto failret;
3278 }
3279 item = dictitem_alloc(key);
3280 if (item != NULL)
3281 {
3282 item->di_tv.v_type = VAR_UNKNOWN;
3283 item->di_tv.v_lock = 0;
3284 if (dict_add(d, item) == FAIL)
3285 dictitem_free(item);
3286 }
3287 }
3288
3289 *arg = skipwhite(*arg);
3290 if (**arg != ':')
3291 {
3292 semsg(_(e_missing_dict_colon), *arg);
3293 return FAIL;
3294 }
3295
Bram Moolenaar2c330432020-04-13 14:41:35 +02003296 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003298 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003299 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003300 *arg = NULL;
3301 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003302 }
3303
Bram Moolenaara5565e42020-05-09 15:44:01 +02003304 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 return FAIL;
3306 ++count;
3307
Bram Moolenaar2c330432020-04-13 14:41:35 +02003308 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003309 *arg = skipwhite(*arg);
3310 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003311 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003312 *arg = NULL;
3313 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 if (**arg == '}')
3316 break;
3317 if (**arg != ',')
3318 {
3319 semsg(_(e_missing_dict_comma), *arg);
3320 goto failret;
3321 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003322 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 *arg = skipwhite(*arg + 1);
3324 }
3325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 *arg = *arg + 1;
3327
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003328 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003329 p = skipwhite(*arg);
3330 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003331 *arg += STRLEN(*arg);
3332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 dict_unref(d);
3334 return generate_NEWDICT(cctx, count);
3335
3336failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003337 if (*arg == NULL)
3338 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 dict_unref(d);
3340 return FAIL;
3341}
3342
3343/*
3344 * Compile "&option".
3345 */
3346 static int
3347compile_get_option(char_u **arg, cctx_T *cctx)
3348{
3349 typval_T rettv;
3350 char_u *start = *arg;
3351 int ret;
3352
3353 // parse the option and get the current value to get the type.
3354 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003355 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 if (ret == OK)
3357 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003358 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 char_u *name = vim_strnsave(start, *arg - start);
3360 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3361
3362 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3363 vim_free(name);
3364 }
3365 clear_tv(&rettv);
3366
3367 return ret;
3368}
3369
3370/*
3371 * Compile "$VAR".
3372 */
3373 static int
3374compile_get_env(char_u **arg, cctx_T *cctx)
3375{
3376 char_u *start = *arg;
3377 int len;
3378 int ret;
3379 char_u *name;
3380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 ++*arg;
3382 len = get_env_len(arg);
3383 if (len == 0)
3384 {
3385 semsg(_(e_syntax_at), start - 1);
3386 return FAIL;
3387 }
3388
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003389 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 name = vim_strnsave(start, len + 1);
3391 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3392 vim_free(name);
3393 return ret;
3394}
3395
3396/*
3397 * Compile "@r".
3398 */
3399 static int
3400compile_get_register(char_u **arg, cctx_T *cctx)
3401{
3402 int ret;
3403
3404 ++*arg;
3405 if (**arg == NUL)
3406 {
3407 semsg(_(e_syntax_at), *arg - 1);
3408 return FAIL;
3409 }
3410 if (!valid_yank_reg(**arg, TRUE))
3411 {
3412 emsg_invreg(**arg);
3413 return FAIL;
3414 }
3415 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3416 ++*arg;
3417 return ret;
3418}
3419
3420/*
3421 * Apply leading '!', '-' and '+' to constant "rettv".
3422 */
3423 static int
3424apply_leader(typval_T *rettv, char_u *start, char_u *end)
3425{
3426 char_u *p = end;
3427
3428 // this works from end to start
3429 while (p > start)
3430 {
3431 --p;
3432 if (*p == '-' || *p == '+')
3433 {
3434 // only '-' has an effect, for '+' we only check the type
3435#ifdef FEAT_FLOAT
3436 if (rettv->v_type == VAR_FLOAT)
3437 {
3438 if (*p == '-')
3439 rettv->vval.v_float = -rettv->vval.v_float;
3440 }
3441 else
3442#endif
3443 {
3444 varnumber_T val;
3445 int error = FALSE;
3446
3447 // tv_get_number_chk() accepts a string, but we don't want that
3448 // here
3449 if (check_not_string(rettv) == FAIL)
3450 return FAIL;
3451 val = tv_get_number_chk(rettv, &error);
3452 clear_tv(rettv);
3453 if (error)
3454 return FAIL;
3455 if (*p == '-')
3456 val = -val;
3457 rettv->v_type = VAR_NUMBER;
3458 rettv->vval.v_number = val;
3459 }
3460 }
3461 else
3462 {
3463 int v = tv2bool(rettv);
3464
3465 // '!' is permissive in the type.
3466 clear_tv(rettv);
3467 rettv->v_type = VAR_BOOL;
3468 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3469 }
3470 }
3471 return OK;
3472}
3473
3474/*
3475 * Recognize v: variables that are constants and set "rettv".
3476 */
3477 static void
3478get_vim_constant(char_u **arg, typval_T *rettv)
3479{
3480 if (STRNCMP(*arg, "v:true", 6) == 0)
3481 {
3482 rettv->v_type = VAR_BOOL;
3483 rettv->vval.v_number = VVAL_TRUE;
3484 *arg += 6;
3485 }
3486 else if (STRNCMP(*arg, "v:false", 7) == 0)
3487 {
3488 rettv->v_type = VAR_BOOL;
3489 rettv->vval.v_number = VVAL_FALSE;
3490 *arg += 7;
3491 }
3492 else if (STRNCMP(*arg, "v:null", 6) == 0)
3493 {
3494 rettv->v_type = VAR_SPECIAL;
3495 rettv->vval.v_number = VVAL_NULL;
3496 *arg += 6;
3497 }
3498 else if (STRNCMP(*arg, "v:none", 6) == 0)
3499 {
3500 rettv->v_type = VAR_SPECIAL;
3501 rettv->vval.v_number = VVAL_NONE;
3502 *arg += 6;
3503 }
3504}
3505
Bram Moolenaar61a89812020-05-07 16:58:17 +02003506 static exptype_T
3507get_compare_type(char_u *p, int *len, int *type_is)
3508{
3509 exptype_T type = EXPR_UNKNOWN;
3510 int i;
3511
3512 switch (p[0])
3513 {
3514 case '=': if (p[1] == '=')
3515 type = EXPR_EQUAL;
3516 else if (p[1] == '~')
3517 type = EXPR_MATCH;
3518 break;
3519 case '!': if (p[1] == '=')
3520 type = EXPR_NEQUAL;
3521 else if (p[1] == '~')
3522 type = EXPR_NOMATCH;
3523 break;
3524 case '>': if (p[1] != '=')
3525 {
3526 type = EXPR_GREATER;
3527 *len = 1;
3528 }
3529 else
3530 type = EXPR_GEQUAL;
3531 break;
3532 case '<': if (p[1] != '=')
3533 {
3534 type = EXPR_SMALLER;
3535 *len = 1;
3536 }
3537 else
3538 type = EXPR_SEQUAL;
3539 break;
3540 case 'i': if (p[1] == 's')
3541 {
3542 // "is" and "isnot"; but not a prefix of a name
3543 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3544 *len = 5;
3545 i = p[*len];
3546 if (!isalnum(i) && i != '_')
3547 {
3548 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3549 *type_is = TRUE;
3550 }
3551 }
3552 break;
3553 }
3554 return type;
3555}
3556
3557/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 * Compile code to apply '-', '+' and '!'.
3559 */
3560 static int
3561compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3562{
3563 char_u *p = end;
3564
3565 // this works from end to start
3566 while (p > start)
3567 {
3568 --p;
3569 if (*p == '-' || *p == '+')
3570 {
3571 int negate = *p == '-';
3572 isn_T *isn;
3573
3574 // TODO: check type
3575 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3576 {
3577 --p;
3578 if (*p == '-')
3579 negate = !negate;
3580 }
3581 // only '-' has an effect, for '+' we only check the type
3582 if (negate)
3583 isn = generate_instr(cctx, ISN_NEGATENR);
3584 else
3585 isn = generate_instr(cctx, ISN_CHECKNR);
3586 if (isn == NULL)
3587 return FAIL;
3588 }
3589 else
3590 {
3591 int invert = TRUE;
3592
3593 while (p > start && p[-1] == '!')
3594 {
3595 --p;
3596 invert = !invert;
3597 }
3598 if (generate_2BOOL(cctx, invert) == FAIL)
3599 return FAIL;
3600 }
3601 }
3602 return OK;
3603}
3604
3605/*
3606 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003607 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 */
3609 static int
3610compile_subscript(
3611 char_u **arg,
3612 cctx_T *cctx,
3613 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003614 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003615 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616{
3617 for (;;)
3618 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003619 char_u *p = skipwhite(*arg);
3620
3621 if (*p == NUL || (VIM_ISWHITE(**arg) && comment_start(p)))
3622 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003623 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003624
3625 // If a following line starts with "->{" or "->X" advance to that
3626 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003627 // Also if a following line starts with ".x".
3628 if (next != NULL &&
3629 ((next[0] == '-' && next[1] == '>'
3630 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3631 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003632 {
3633 next = next_line_from_context(cctx, TRUE);
3634 if (next == NULL)
3635 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003636 *arg = next;
3637 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003638 }
3639 }
3640
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003641 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003643 garray_T *stack = &cctx->ctx_type_stack;
3644 type_T *type;
3645 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003647 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003648 return FAIL;
3649
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003651 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3652
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003653 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3655 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003656 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 return FAIL;
3658 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003659 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003661 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003662 return FAIL;
3663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 // something->method()
3665 // Apply the '!', '-' and '+' first:
3666 // -1.0->func() works like (-1.0)->func()
3667 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3668 return FAIL;
3669 *start_leader = end_leader; // don't apply again later
3670
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003671 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003672 *arg = skipwhite(p);
3673 if (may_get_next_line(p, arg, cctx) == FAIL)
3674 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 if (**arg == '{')
3676 {
3677 // lambda call: list->{lambda}
3678 if (compile_lambda_call(arg, cctx) == FAIL)
3679 return FAIL;
3680 }
3681 else
3682 {
3683 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003684 p = *arg;
3685 if (ASCII_ISALPHA(*p) && p[1] == ':')
3686 p += 2;
3687 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 ;
3689 if (*p != '(')
3690 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003691 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 return FAIL;
3693 }
3694 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003695 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 return FAIL;
3697 }
3698 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003699 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003701 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003702 type_T **typep;
3703
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003704 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003705 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003706 // TODO: blob index
3707 // TODO: more arguments
3708 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003709 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003710 return FAIL;
3711
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003712 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003713 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003714 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003715 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003716 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 return FAIL;
3718
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003719 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3720 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 if (**arg != ']')
3722 {
3723 emsg(_(e_missbrac));
3724 return FAIL;
3725 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003726 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003728 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3729 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003730 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003731 if ((*typep)->tt_type == VAR_LIST)
3732 *typep = (*typep)->tt_member;
3733 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3734 return FAIL;
3735 }
3736 else if ((*typep)->tt_type == VAR_DICT)
3737 {
3738 *typep = (*typep)->tt_member;
3739 if (may_generate_2STRING(-1, cctx) == FAIL)
3740 return FAIL;
3741 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3742 return FAIL;
3743 }
3744 else
3745 {
3746 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003747 return FAIL;
3748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003750 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003752 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003753 return FAIL;
3754
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003755 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003756 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003759 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 if (eval_isnamec1(*p))
3761 while (eval_isnamec(*p))
3762 MB_PTR_ADV(p);
3763 if (p == *arg)
3764 {
3765 semsg(_(e_syntax_at), *arg);
3766 return FAIL;
3767 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003768 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 return FAIL;
3770 *arg = p;
3771 }
3772 else
3773 break;
3774 }
3775
3776 // TODO - see handle_subscript():
3777 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3778 // Don't do this when "Func" is already a partial that was bound
3779 // explicitly (pt_auto is FALSE).
3780
3781 return OK;
3782}
3783
3784/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003785 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3786 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003788 * If the value is a constant "ppconst->pp_ret" will be set.
3789 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003790 *
3791 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 */
3793
3794/*
3795 * number number constant
3796 * 0zFFFFFFFF Blob constant
3797 * "string" string constant
3798 * 'string' literal string constant
3799 * &option-name option value
3800 * @r register contents
3801 * identifier variable value
3802 * function() function call
3803 * $VAR environment variable
3804 * (expression) nested expression
3805 * [expr, expr] List
3806 * {key: val, key: val} Dictionary
3807 * #{key: val, key: val} Dictionary with literal keys
3808 *
3809 * Also handle:
3810 * ! in front logical NOT
3811 * - in front unary minus
3812 * + in front unary plus (ignored)
3813 * trailing (arg) funcref/partial call
3814 * trailing [] subscript in String or List
3815 * trailing .name entry in Dictionary
3816 * trailing ->name() method call
3817 */
3818 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003819compile_expr7(
3820 char_u **arg,
3821 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003822 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 char_u *start_leader, *end_leader;
3825 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003826 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003827 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828
3829 /*
3830 * Skip '!', '-' and '+' characters. They are handled later.
3831 */
3832 start_leader = *arg;
3833 while (**arg == '!' || **arg == '-' || **arg == '+')
3834 *arg = skipwhite(*arg + 1);
3835 end_leader = *arg;
3836
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003837 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 switch (**arg)
3839 {
3840 /*
3841 * Number constant.
3842 */
3843 case '0': // also for blob starting with 0z
3844 case '1':
3845 case '2':
3846 case '3':
3847 case '4':
3848 case '5':
3849 case '6':
3850 case '7':
3851 case '8':
3852 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003853 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 return FAIL;
3855 break;
3856
3857 /*
3858 * String constant: "string".
3859 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003860 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 return FAIL;
3862 break;
3863
3864 /*
3865 * Literal string constant: 'str''ing'.
3866 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003867 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 return FAIL;
3869 break;
3870
3871 /*
3872 * Constant Vim variable.
3873 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003874 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 ret = NOTDONE;
3876 break;
3877
3878 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003879 * "true" constant
3880 */
3881 case 't': if (STRNCMP(*arg, "true", 4) == 0
3882 && !eval_isnamec((*arg)[4]))
3883 {
3884 *arg += 4;
3885 rettv->v_type = VAR_BOOL;
3886 rettv->vval.v_number = VVAL_TRUE;
3887 }
3888 else
3889 ret = NOTDONE;
3890 break;
3891
3892 /*
3893 * "false" constant
3894 */
3895 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3896 && !eval_isnamec((*arg)[5]))
3897 {
3898 *arg += 5;
3899 rettv->v_type = VAR_BOOL;
3900 rettv->vval.v_number = VVAL_FALSE;
3901 }
3902 else
3903 ret = NOTDONE;
3904 break;
3905
3906 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 * List: [expr, expr]
3908 */
3909 case '[': ret = compile_list(arg, cctx);
3910 break;
3911
3912 /*
3913 * Dictionary: #{key: val, key: val}
3914 */
3915 case '#': if ((*arg)[1] == '{')
3916 {
3917 ++*arg;
3918 ret = compile_dict(arg, cctx, TRUE);
3919 }
3920 else
3921 ret = NOTDONE;
3922 break;
3923
3924 /*
3925 * Lambda: {arg, arg -> expr}
3926 * Dictionary: {'key': val, 'key': val}
3927 */
3928 case '{': {
3929 char_u *start = skipwhite(*arg + 1);
3930
3931 // Find out what comes after the arguments.
3932 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003933 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 if (ret != FAIL && *start == '>')
3935 ret = compile_lambda(arg, cctx);
3936 else
3937 ret = compile_dict(arg, cctx, FALSE);
3938 }
3939 break;
3940
3941 /*
3942 * Option value: &name
3943 */
3944 case '&': ret = compile_get_option(arg, cctx);
3945 break;
3946
3947 /*
3948 * Environment variable: $VAR.
3949 */
3950 case '$': ret = compile_get_env(arg, cctx);
3951 break;
3952
3953 /*
3954 * Register contents: @r.
3955 */
3956 case '@': ret = compile_get_register(arg, cctx);
3957 break;
3958 /*
3959 * nested expression: (expression).
3960 */
3961 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003962
3963 // recursive!
3964 if (ppconst->pp_used <= PPSIZE - 10)
3965 {
3966 ret = compile_expr1(arg, cctx, ppconst);
3967 }
3968 else
3969 {
3970 // Not enough space in ppconst, flush constants.
3971 if (generate_ppconst(cctx, ppconst) == FAIL)
3972 return FAIL;
3973 ret = compile_expr0(arg, cctx);
3974 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 *arg = skipwhite(*arg);
3976 if (**arg == ')')
3977 ++*arg;
3978 else if (ret == OK)
3979 {
3980 emsg(_(e_missing_close));
3981 ret = FAIL;
3982 }
3983 break;
3984
3985 default: ret = NOTDONE;
3986 break;
3987 }
3988 if (ret == FAIL)
3989 return FAIL;
3990
Bram Moolenaar1c747212020-05-09 18:28:34 +02003991 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 {
3993 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003994 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003996 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997 return FAIL;
3998 }
3999 start_leader = end_leader; // don't apply again below
4000
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004001 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004002 clear_tv(rettv);
4003 else
4004 // A constant expression can possibly be handled compile time,
4005 // return the value instead of generating code.
4006 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 }
4008 else if (ret == NOTDONE)
4009 {
4010 char_u *p;
4011 int r;
4012
4013 if (!eval_isnamec1(**arg))
4014 {
4015 semsg(_("E1015: Name expected: %s"), *arg);
4016 return FAIL;
4017 }
4018
4019 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004020 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004022 {
4023 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4024 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004026 {
4027 if (generate_ppconst(cctx, ppconst) == FAIL)
4028 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 if (r == FAIL)
4032 return FAIL;
4033 }
4034
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004035 // Handle following "[]", ".member", etc.
4036 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004037 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004038 ppconst) == FAIL)
4039 return FAIL;
4040 if (ppconst->pp_used > 0)
4041 {
4042 // apply the '!', '-' and '+' before the constant
4043 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4044 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4045 return FAIL;
4046 return OK;
4047 }
4048 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004050 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051}
4052
4053/*
4054 * * number multiplication
4055 * / number division
4056 * % number modulo
4057 */
4058 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004059compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060{
4061 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004062 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004063 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004065 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004066 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 return FAIL;
4068
4069 /*
4070 * Repeat computing, until no "*", "/" or "%" is following.
4071 */
4072 for (;;)
4073 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004074 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 if (*op != '*' && *op != '/' && *op != '%')
4076 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004077 if (next != NULL)
4078 {
4079 *arg = next_line_from_context(cctx, TRUE);
4080 op = skipwhite(*arg);
4081 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004082
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004083 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 {
4085 char_u buf[3];
4086
4087 vim_strncpy(buf, op, 1);
4088 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004089 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 }
4091 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004092 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004093 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004095 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004096 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004098
4099 if (ppconst->pp_used == ppconst_used + 2
4100 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4101 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004102 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004103 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4104 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004105 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004107 // both are numbers: compute the result
4108 switch (*op)
4109 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004110 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004111 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004112 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004113 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004114 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004115 break;
4116 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004117 tv1->vval.v_number = res;
4118 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004119 }
4120 else
4121 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004122 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004123 generate_two_op(cctx, op);
4124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 }
4126
4127 return OK;
4128}
4129
4130/*
4131 * + number addition
4132 * - number subtraction
4133 * .. string concatenation
4134 */
4135 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004136compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137{
4138 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004139 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004141 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142
4143 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004144 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 return FAIL;
4146
4147 /*
4148 * Repeat computing, until no "+", "-" or ".." is following.
4149 */
4150 for (;;)
4151 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004152 op = may_peek_next_line(cctx, *arg, &next);
4153 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 break;
4155 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004156 if (next != NULL)
4157 {
4158 *arg = next_line_from_context(cctx, TRUE);
4159 op = skipwhite(*arg);
4160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004162 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 {
4164 char_u buf[3];
4165
4166 vim_strncpy(buf, op, oplen);
4167 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004168 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 }
4170
4171 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004172 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004173 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004175 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004176 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 return FAIL;
4178
Bram Moolenaara5565e42020-05-09 15:44:01 +02004179 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004180 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4182 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4183 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4184 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004186 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4187 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004188
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004189 // concat/subtract/add constant numbers
4190 if (*op == '+')
4191 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4192 else if (*op == '-')
4193 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4194 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004195 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004196 // concatenate constant strings
4197 char_u *s1 = tv1->vval.v_string;
4198 char_u *s2 = tv2->vval.v_string;
4199 size_t len1 = STRLEN(s1);
4200
4201 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4202 if (tv1->vval.v_string == NULL)
4203 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004204 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004205 return FAIL;
4206 }
4207 mch_memmove(tv1->vval.v_string, s1, len1);
4208 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004209 vim_free(s1);
4210 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004211 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004212 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 }
4214 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004215 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004216 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004217 if (*op == '.')
4218 {
4219 if (may_generate_2STRING(-2, cctx) == FAIL
4220 || may_generate_2STRING(-1, cctx) == FAIL)
4221 return FAIL;
4222 generate_instr_drop(cctx, ISN_CONCAT, 1);
4223 }
4224 else
4225 generate_two_op(cctx, op);
4226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 }
4228
4229 return OK;
4230}
4231
4232/*
4233 * expr5a == expr5b
4234 * expr5a =~ expr5b
4235 * expr5a != expr5b
4236 * expr5a !~ expr5b
4237 * expr5a > expr5b
4238 * expr5a >= expr5b
4239 * expr5a < expr5b
4240 * expr5a <= expr5b
4241 * expr5a is expr5b
4242 * expr5a isnot expr5b
4243 *
4244 * Produces instructions:
4245 * EVAL expr5a Push result of "expr5a"
4246 * EVAL expr5b Push result of "expr5b"
4247 * COMPARE one of the compare instructions
4248 */
4249 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004250compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251{
4252 exptype_T type = EXPR_UNKNOWN;
4253 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004254 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004257 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258
4259 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004260 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 return FAIL;
4262
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004263 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004264 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265
4266 /*
4267 * If there is a comparative operator, use it.
4268 */
4269 if (type != EXPR_UNKNOWN)
4270 {
4271 int ic = FALSE; // Default: do not ignore case
4272
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004273 if (next != NULL)
4274 {
4275 *arg = next_line_from_context(cctx, TRUE);
4276 p = skipwhite(*arg);
4277 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 if (type_is && (p[len] == '?' || p[len] == '#'))
4279 {
4280 semsg(_(e_invexpr2), *arg);
4281 return FAIL;
4282 }
4283 // extra question mark appended: ignore case
4284 if (p[len] == '?')
4285 {
4286 ic = TRUE;
4287 ++len;
4288 }
4289 // extra '#' appended: match case (ignored)
4290 else if (p[len] == '#')
4291 ++len;
4292 // nothing appended: match case
4293
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004294 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 {
4296 char_u buf[7];
4297
4298 vim_strncpy(buf, p, len);
4299 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004300 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 }
4302
4303 // get the second variable
4304 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004305 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004306 return FAIL;
4307
Bram Moolenaara5565e42020-05-09 15:44:01 +02004308 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 return FAIL;
4310
Bram Moolenaara5565e42020-05-09 15:44:01 +02004311 if (ppconst->pp_used == ppconst_used + 2)
4312 {
4313 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4314 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4315 int ret;
4316
4317 // Both sides are a constant, compute the result now.
4318 // First check for a valid combination of types, this is more
4319 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004320 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004321 ret = FAIL;
4322 else
4323 {
4324 ret = typval_compare(tv1, tv2, type, ic);
4325 tv1->v_type = VAR_BOOL;
4326 tv1->vval.v_number = tv1->vval.v_number
4327 ? VVAL_TRUE : VVAL_FALSE;
4328 clear_tv(tv2);
4329 --ppconst->pp_used;
4330 }
4331 return ret;
4332 }
4333
4334 generate_ppconst(cctx, ppconst);
4335 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 }
4337
4338 return OK;
4339}
4340
Bram Moolenaar7f141552020-05-09 17:35:53 +02004341static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343/*
4344 * Compile || or &&.
4345 */
4346 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004347compile_and_or(
4348 char_u **arg,
4349 cctx_T *cctx,
4350 char *op,
4351 ppconst_T *ppconst,
4352 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004354 char_u *next;
4355 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 int opchar = *op;
4357
4358 if (p[0] == opchar && p[1] == opchar)
4359 {
4360 garray_T *instr = &cctx->ctx_instr;
4361 garray_T end_ga;
4362
4363 /*
4364 * Repeat until there is no following "||" or "&&"
4365 */
4366 ga_init2(&end_ga, sizeof(int), 10);
4367 while (p[0] == opchar && p[1] == opchar)
4368 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004369 if (next != NULL)
4370 {
4371 *arg = next_line_from_context(cctx, TRUE);
4372 p = skipwhite(*arg);
4373 }
4374
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004375 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4376 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004378 return FAIL;
4379 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380
Bram Moolenaara5565e42020-05-09 15:44:01 +02004381 // TODO: use ppconst if the value is a constant
4382 generate_ppconst(cctx, ppconst);
4383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 if (ga_grow(&end_ga, 1) == FAIL)
4385 {
4386 ga_clear(&end_ga);
4387 return FAIL;
4388 }
4389 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4390 ++end_ga.ga_len;
4391 generate_JUMP(cctx, opchar == '|'
4392 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4393
4394 // eval the next expression
4395 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004396 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004397 return FAIL;
4398
Bram Moolenaara5565e42020-05-09 15:44:01 +02004399 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4400 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 {
4402 ga_clear(&end_ga);
4403 return FAIL;
4404 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004405
4406 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004408 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409
4410 // Fill in the end label in all jumps.
4411 while (end_ga.ga_len > 0)
4412 {
4413 isn_T *isn;
4414
4415 --end_ga.ga_len;
4416 isn = ((isn_T *)instr->ga_data)
4417 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4418 isn->isn_arg.jump.jump_where = instr->ga_len;
4419 }
4420 ga_clear(&end_ga);
4421 }
4422
4423 return OK;
4424}
4425
4426/*
4427 * expr4a && expr4a && expr4a logical AND
4428 *
4429 * Produces instructions:
4430 * EVAL expr4a Push result of "expr4a"
4431 * JUMP_AND_KEEP_IF_FALSE end
4432 * EVAL expr4b Push result of "expr4b"
4433 * JUMP_AND_KEEP_IF_FALSE end
4434 * EVAL expr4c Push result of "expr4c"
4435 * end:
4436 */
4437 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004438compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004440 int ppconst_used = ppconst->pp_used;
4441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004443 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 return FAIL;
4445
4446 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004447 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448}
4449
4450/*
4451 * expr3a || expr3b || expr3c logical OR
4452 *
4453 * Produces instructions:
4454 * EVAL expr3a Push result of "expr3a"
4455 * JUMP_AND_KEEP_IF_TRUE end
4456 * EVAL expr3b Push result of "expr3b"
4457 * JUMP_AND_KEEP_IF_TRUE end
4458 * EVAL expr3c Push result of "expr3c"
4459 * end:
4460 */
4461 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004462compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004464 int ppconst_used = ppconst->pp_used;
4465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004467 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 return FAIL;
4469
4470 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004471 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472}
4473
4474/*
4475 * Toplevel expression: expr2 ? expr1a : expr1b
4476 *
4477 * Produces instructions:
4478 * EVAL expr2 Push result of "expr"
4479 * JUMP_IF_FALSE alt jump if false
4480 * EVAL expr1a
4481 * JUMP_ALWAYS end
4482 * alt: EVAL expr1b
4483 * end:
4484 */
4485 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004486compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487{
4488 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004489 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004490 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491
Bram Moolenaar61a89812020-05-07 16:58:17 +02004492 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004493 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 return FAIL;
4495
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004496 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 if (*p == '?')
4498 {
4499 garray_T *instr = &cctx->ctx_instr;
4500 garray_T *stack = &cctx->ctx_type_stack;
4501 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004502 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004504 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004506 int has_const_expr = FALSE;
4507 int const_value = FALSE;
4508 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004510 if (next != NULL)
4511 {
4512 *arg = next_line_from_context(cctx, TRUE);
4513 p = skipwhite(*arg);
4514 }
4515
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004516 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4517 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004519 return FAIL;
4520 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521
Bram Moolenaara5565e42020-05-09 15:44:01 +02004522 if (ppconst->pp_used == ppconst_used + 1)
4523 {
4524 // the condition is a constant, we know whether the ? or the :
4525 // expression is to be evaluated.
4526 has_const_expr = TRUE;
4527 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4528 clear_tv(&ppconst->pp_tv[ppconst_used]);
4529 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004530 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4531 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004532 }
4533 else
4534 {
4535 generate_ppconst(cctx, ppconst);
4536 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538
4539 // evaluate the second expression; any type is accepted
4540 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004541 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004542 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004543 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004544 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545
Bram Moolenaara5565e42020-05-09 15:44:01 +02004546 if (!has_const_expr)
4547 {
4548 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549
Bram Moolenaara5565e42020-05-09 15:44:01 +02004550 // remember the type and drop it
4551 --stack->ga_len;
4552 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554 end_idx = instr->ga_len;
4555 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4556
4557 // jump here from JUMP_IF_FALSE
4558 isn = ((isn_T *)instr->ga_data) + alt_idx;
4559 isn->isn_arg.jump.jump_where = instr->ga_len;
4560 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561
4562 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004563 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 if (*p != ':')
4565 {
4566 emsg(_(e_missing_colon));
4567 return FAIL;
4568 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004569 if (next != NULL)
4570 {
4571 *arg = next_line_from_context(cctx, TRUE);
4572 p = skipwhite(*arg);
4573 }
4574
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004575 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4576 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004578 return FAIL;
4579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580
4581 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004582 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004583 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4584 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004586 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004587 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004588 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004589 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590
Bram Moolenaara5565e42020-05-09 15:44:01 +02004591 if (!has_const_expr)
4592 {
4593 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594
Bram Moolenaara5565e42020-05-09 15:44:01 +02004595 // If the types differ, the result has a more generic type.
4596 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4597 common_type(type1, type2, &type2, cctx->ctx_type_list);
4598
4599 // jump here from JUMP_ALWAYS
4600 isn = ((isn_T *)instr->ga_data) + end_idx;
4601 isn->isn_arg.jump.jump_where = instr->ga_len;
4602 }
4603
4604 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 }
4606 return OK;
4607}
4608
4609/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004610 * Toplevel expression.
4611 */
4612 static int
4613compile_expr0(char_u **arg, cctx_T *cctx)
4614{
4615 ppconst_T ppconst;
4616
4617 CLEAR_FIELD(ppconst);
4618 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4619 {
4620 clear_ppconst(&ppconst);
4621 return FAIL;
4622 }
4623 if (generate_ppconst(cctx, &ppconst) == FAIL)
4624 return FAIL;
4625 return OK;
4626}
4627
4628/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 * compile "return [expr]"
4630 */
4631 static char_u *
4632compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4633{
4634 char_u *p = arg;
4635 garray_T *stack = &cctx->ctx_type_stack;
4636 type_T *stack_type;
4637
4638 if (*p != NUL && *p != '|' && *p != '\n')
4639 {
4640 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004641 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 return NULL;
4643
4644 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4645 if (set_return_type)
4646 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004647 else
4648 {
4649 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4650 && stack_type->tt_type != VAR_VOID
4651 && stack_type->tt_type != VAR_UNKNOWN)
4652 {
4653 emsg(_("E1096: Returning a value in a function without a return type"));
4654 return NULL;
4655 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004656 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4657 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004659 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 }
4661 else
4662 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004663 // "set_return_type" cannot be TRUE, only used for a lambda which
4664 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004665 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4666 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 {
4668 emsg(_("E1003: Missing return value"));
4669 return NULL;
4670 }
4671
4672 // No argument, return zero.
4673 generate_PUSHNR(cctx, 0);
4674 }
4675
4676 if (generate_instr(cctx, ISN_RETURN) == NULL)
4677 return NULL;
4678
4679 // "return val | endif" is possible
4680 return skipwhite(p);
4681}
4682
4683/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004684 * Get a line from the compilation context, compatible with exarg_T getline().
4685 * Return a pointer to the line in allocated memory.
4686 * Return NULL for end-of-file or some error.
4687 */
4688 static char_u *
4689exarg_getline(
4690 int c UNUSED,
4691 void *cookie,
4692 int indent UNUSED,
4693 int do_concat UNUSED)
4694{
4695 cctx_T *cctx = (cctx_T *)cookie;
4696
4697 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4698 {
4699 iemsg("Heredoc got to end");
4700 return NULL;
4701 }
4702 ++cctx->ctx_lnum;
4703 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4704 [cctx->ctx_lnum]);
4705}
4706
4707/*
4708 * Compile a nested :def command.
4709 */
4710 static char_u *
4711compile_nested_function(exarg_T *eap, cctx_T *cctx)
4712{
4713 char_u *name_start = eap->arg;
4714 char_u *name_end = to_name_end(eap->arg, FALSE);
4715 char_u *name = get_lambda_name();
4716 lvar_T *lvar;
4717 ufunc_T *ufunc;
4718
4719 eap->arg = name_end;
4720 eap->getline = exarg_getline;
4721 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004722 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004723 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004724 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004725
Bram Moolenaar822ba242020-05-24 23:00:18 +02004726 if (ufunc == NULL)
4727 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004728 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004729 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004730 return NULL;
4731
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004732 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004733 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004734 TRUE, ufunc->uf_func_type);
4735
4736 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4737 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4738 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004739
Bram Moolenaar61a89812020-05-07 16:58:17 +02004740 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004741 return (char_u *)"";
4742}
4743
4744/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 * Return the length of an assignment operator, or zero if there isn't one.
4746 */
4747 int
4748assignment_len(char_u *p, int *heredoc)
4749{
4750 if (*p == '=')
4751 {
4752 if (p[1] == '<' && p[2] == '<')
4753 {
4754 *heredoc = TRUE;
4755 return 3;
4756 }
4757 return 1;
4758 }
4759 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4760 return 2;
4761 if (STRNCMP(p, "..=", 3) == 0)
4762 return 3;
4763 return 0;
4764}
4765
4766// words that cannot be used as a variable
4767static char *reserved[] = {
4768 "true",
4769 "false",
4770 NULL
4771};
4772
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004773typedef enum {
4774 dest_local,
4775 dest_option,
4776 dest_env,
4777 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004778 dest_buffer,
4779 dest_window,
4780 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004781 dest_vimvar,
4782 dest_script,
4783 dest_reg,
4784} assign_dest_T;
4785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004787 * Generate the load instruction for "name".
4788 */
4789 static void
4790generate_loadvar(
4791 cctx_T *cctx,
4792 assign_dest_T dest,
4793 char_u *name,
4794 lvar_T *lvar,
4795 type_T *type)
4796{
4797 switch (dest)
4798 {
4799 case dest_option:
4800 // TODO: check the option exists
4801 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4802 break;
4803 case dest_global:
4804 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4805 break;
4806 case dest_buffer:
4807 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4808 break;
4809 case dest_window:
4810 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4811 break;
4812 case dest_tab:
4813 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4814 break;
4815 case dest_script:
4816 compile_load_scriptvar(cctx,
4817 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4818 break;
4819 case dest_env:
4820 // Include $ in the name here
4821 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4822 break;
4823 case dest_reg:
4824 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4825 break;
4826 case dest_vimvar:
4827 generate_LOADV(cctx, name + 2, TRUE);
4828 break;
4829 case dest_local:
4830 if (lvar->lv_from_outer)
4831 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4832 NULL, type);
4833 else
4834 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4835 break;
4836 }
4837}
4838
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004839 void
4840vim9_declare_error(char_u *name)
4841{
4842 char *scope = "";
4843
4844 switch (*name)
4845 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004846 case 'g': scope = _("global"); break;
4847 case 'b': scope = _("buffer"); break;
4848 case 'w': scope = _("window"); break;
4849 case 't': scope = _("tab"); break;
4850 case 'v': scope = "v:"; break;
4851 case '$': semsg(_(e_declare_env_var), name); return;
4852 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004853 }
4854 semsg(_(e_declare_var), scope, name);
4855}
4856
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004857/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004858 * Compile declaration and assignment:
4859 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004861 * Return NULL for an error.
4862 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 */
4864 static char_u *
4865compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4866{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004867 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004869 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 char_u *ret = NULL;
4871 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004872 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004875 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 int oplen = 0;
4878 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004879 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004880 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004881 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004885 // Skip over the "var" or "[var, var]" to get to any "=".
4886 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4887 if (p == NULL)
4888 return *arg == '[' ? arg : NULL;
4889
4890 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004892 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 return NULL;
4894 }
4895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 sp = p;
4897 p = skipwhite(p);
4898 op = p;
4899 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004900
4901 if (var_count > 0 && oplen == 0)
4902 // can be something like "[1, 2]->func()"
4903 return arg;
4904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4906 {
4907 char_u buf[4];
4908
4909 vim_strncpy(buf, op, oplen);
4910 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004911 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004912 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 if (heredoc)
4915 {
4916 list_T *l;
4917 listitem_T *li;
4918
4919 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004920 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004922 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923
4924 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004925 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 {
4927 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4928 li->li_tv.vval.v_string = NULL;
4929 }
4930 generate_NEWLIST(cctx, l->lv_len);
4931 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004932 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 list_free(l);
4934 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004935 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004937 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004939 // for "[var, var] = expr" evaluate the expression here, loop over the
4940 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004941
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004942 p = skipwhite(op + oplen);
4943 if (compile_expr0(&p, cctx) == FAIL)
4944 return NULL;
4945 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004947 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004949 type_T *stacktype;
4950
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004951 stacktype = stack->ga_len == 0 ? &t_void
4952 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004953 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004955 emsg(_(e_cannot_use_void));
4956 goto theend;
4957 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004958 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004959 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004960 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4961 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004962 }
4963 }
4964
4965 /*
4966 * Loop over variables in "[var, var] = expr".
4967 * For "var = expr" and "let var: type" this is done only once.
4968 */
4969 if (var_count > 0)
4970 var_start = skipwhite(arg + 1); // skip over the "["
4971 else
4972 var_start = arg;
4973 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4974 {
4975 char_u *var_end = skip_var_one(var_start, FALSE);
4976 size_t varlen;
4977 int new_local = FALSE;
4978 int opt_type;
4979 int opt_flags = 0;
4980 assign_dest_T dest = dest_local;
4981 int vimvaridx = -1;
4982 lvar_T *lvar = NULL;
4983 lvar_T arg_lvar;
4984 int has_type = FALSE;
4985 int has_index = FALSE;
4986 int instr_count = -1;
4987
4988 p = (*var_start == '&' || *var_start == '$'
4989 || *var_start == '@') ? var_start + 1 : var_start;
4990 p = to_name_end(p, TRUE);
4991
4992 // "a: type" is declaring variable "a" with a type, not "a:".
4993 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4994 --var_end;
4995 if (is_decl && p == var_start + 2 && p[-1] == ':')
4996 --p;
4997
4998 varlen = p - var_start;
4999 vim_free(name);
5000 name = vim_strnsave(var_start, varlen);
5001 if (name == NULL)
5002 return NULL;
5003 if (!heredoc)
5004 type = &t_any;
5005
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005006 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005007 {
5008 if (*var_start == '&')
5009 {
5010 int cc;
5011 long numval;
5012
5013 dest = dest_option;
5014 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005016 emsg(_(e_const_option));
5017 goto theend;
5018 }
5019 if (is_decl)
5020 {
5021 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5022 goto theend;
5023 }
5024 p = var_start;
5025 p = find_option_end(&p, &opt_flags);
5026 if (p == NULL)
5027 {
5028 // cannot happen?
5029 emsg(_(e_letunexp));
5030 goto theend;
5031 }
5032 cc = *p;
5033 *p = NUL;
5034 opt_type = get_option_value(var_start + 1, &numval,
5035 NULL, opt_flags);
5036 *p = cc;
5037 if (opt_type == -3)
5038 {
5039 semsg(_(e_unknown_option), var_start);
5040 goto theend;
5041 }
5042 if (opt_type == -2 || opt_type == 0)
5043 type = &t_string;
5044 else
5045 type = &t_number; // both number and boolean option
5046 }
5047 else if (*var_start == '$')
5048 {
5049 dest = dest_env;
5050 type = &t_string;
5051 if (is_decl)
5052 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005053 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005054 goto theend;
5055 }
5056 }
5057 else if (*var_start == '@')
5058 {
5059 if (!valid_yank_reg(var_start[1], TRUE))
5060 {
5061 emsg_invreg(var_start[1]);
5062 goto theend;
5063 }
5064 dest = dest_reg;
5065 type = &t_string;
5066 if (is_decl)
5067 {
5068 semsg(_("E1066: Cannot declare a register: %s"), name);
5069 goto theend;
5070 }
5071 }
5072 else if (STRNCMP(var_start, "g:", 2) == 0)
5073 {
5074 dest = dest_global;
5075 if (is_decl)
5076 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005077 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005078 goto theend;
5079 }
5080 }
5081 else if (STRNCMP(var_start, "b:", 2) == 0)
5082 {
5083 dest = dest_buffer;
5084 if (is_decl)
5085 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005086 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005087 goto theend;
5088 }
5089 }
5090 else if (STRNCMP(var_start, "w:", 2) == 0)
5091 {
5092 dest = dest_window;
5093 if (is_decl)
5094 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005095 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005096 goto theend;
5097 }
5098 }
5099 else if (STRNCMP(var_start, "t:", 2) == 0)
5100 {
5101 dest = dest_tab;
5102 if (is_decl)
5103 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005104 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005105 goto theend;
5106 }
5107 }
5108 else if (STRNCMP(var_start, "v:", 2) == 0)
5109 {
5110 typval_T *vtv;
5111 int di_flags;
5112
5113 vimvaridx = find_vim_var(name + 2, &di_flags);
5114 if (vimvaridx < 0)
5115 {
5116 semsg(_(e_var_notfound), var_start);
5117 goto theend;
5118 }
5119 // We use the current value of "sandbox" here, is that OK?
5120 if (var_check_ro(di_flags, name, FALSE))
5121 goto theend;
5122 dest = dest_vimvar;
5123 vtv = get_vim_var_tv(vimvaridx);
5124 type = typval2type(vtv);
5125 if (is_decl)
5126 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005127 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005128 goto theend;
5129 }
5130 }
5131 else
5132 {
5133 int idx;
5134
5135 for (idx = 0; reserved[idx] != NULL; ++idx)
5136 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005137 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005138 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005139 goto theend;
5140 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005141
5142 lvar = lookup_local(var_start, varlen, cctx);
5143 if (lvar == NULL)
5144 {
5145 CLEAR_FIELD(arg_lvar);
5146 if (lookup_arg(var_start, varlen,
5147 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5148 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005149 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005150 if (is_decl)
5151 {
5152 semsg(_(e_used_as_arg), name);
5153 goto theend;
5154 }
5155 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005156 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005157 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005158 if (lvar != NULL)
5159 {
5160 if (is_decl)
5161 {
5162 semsg(_("E1017: Variable already declared: %s"), name);
5163 goto theend;
5164 }
5165 else if (lvar->lv_const)
5166 {
5167 semsg(_("E1018: Cannot assign to a constant: %s"),
5168 name);
5169 goto theend;
5170 }
5171 }
5172 else if (STRNCMP(var_start, "s:", 2) == 0
5173 || lookup_script(var_start, varlen) == OK
5174 || find_imported(var_start, varlen, cctx) != NULL)
5175 {
5176 dest = dest_script;
5177 if (is_decl)
5178 {
5179 semsg(_("E1054: Variable already declared in the script: %s"),
5180 name);
5181 goto theend;
5182 }
5183 }
5184 else if (name[1] == ':' && name[2] != NUL)
5185 {
5186 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5187 name);
5188 goto theend;
5189 }
5190 else if (!is_decl)
5191 {
5192 semsg(_("E1089: unknown variable: %s"), name);
5193 goto theend;
5194 }
5195 }
5196 }
5197
5198 // handle "a:name" as a name, not index "name" on "a"
5199 if (varlen > 1 || var_start[varlen] != ':')
5200 p = var_end;
5201
5202 if (dest != dest_option)
5203 {
5204 if (is_decl && *p == ':')
5205 {
5206 // parse optional type: "let var: type = expr"
5207 if (!VIM_ISWHITE(p[1]))
5208 {
5209 semsg(_(e_white_after), ":");
5210 goto theend;
5211 }
5212 p = skipwhite(p + 1);
5213 type = parse_type(&p, cctx->ctx_type_list);
5214 has_type = TRUE;
5215 }
5216 else if (lvar != NULL)
5217 type = lvar->lv_type;
5218 }
5219
5220 if (oplen == 3 && !heredoc && dest != dest_global
5221 && type->tt_type != VAR_STRING
5222 && type->tt_type != VAR_ANY)
5223 {
5224 emsg(_("E1019: Can only concatenate to string"));
5225 goto theend;
5226 }
5227
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005228 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005229 {
5230 if (oplen > 1 && !heredoc)
5231 {
5232 // +=, /=, etc. require an existing variable
5233 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5234 name);
5235 goto theend;
5236 }
5237
5238 // new local variable
5239 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5240 goto theend;
5241 lvar = reserve_local(cctx, var_start, varlen,
5242 cmdidx == CMD_const, type);
5243 if (lvar == NULL)
5244 goto theend;
5245 new_local = TRUE;
5246 }
5247
5248 member_type = type;
5249 if (var_end > var_start + varlen)
5250 {
5251 // Something follows after the variable: "var[idx]".
5252 if (is_decl)
5253 {
5254 emsg(_("E1087: cannot use an index when declaring a variable"));
5255 goto theend;
5256 }
5257
5258 if (var_start[varlen] == '[')
5259 {
5260 has_index = TRUE;
5261 if (type->tt_member == NULL)
5262 {
5263 semsg(_("E1088: cannot use an index on %s"), name);
5264 goto theend;
5265 }
5266 member_type = type->tt_member;
5267 }
5268 else
5269 {
5270 semsg("Not supported yet: %s", var_start);
5271 goto theend;
5272 }
5273 }
5274 else if (lvar == &arg_lvar)
5275 {
5276 semsg(_("E1090: Cannot assign to argument %s"), name);
5277 goto theend;
5278 }
5279
5280 if (!heredoc)
5281 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005282 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005283 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005284 if (oplen > 0 && var_count == 0)
5285 {
5286 // skip over the "=" and the expression
5287 p = skipwhite(op + oplen);
5288 compile_expr0(&p, cctx);
5289 }
5290 }
5291 else if (oplen > 0)
5292 {
5293 type_T *stacktype;
5294
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005295 // For "var = expr" evaluate the expression.
5296 if (var_count == 0)
5297 {
5298 int r;
5299
5300 // for "+=", "*=", "..=" etc. first load the current value
5301 if (*op != '=')
5302 {
5303 generate_loadvar(cctx, dest, name, lvar, type);
5304
5305 if (has_index)
5306 {
5307 // TODO: get member from list or dict
5308 emsg("Index with operation not supported yet");
5309 goto theend;
5310 }
5311 }
5312
5313 // Compile the expression. Temporarily hide the new local
5314 // variable here, it is not available to this expression.
5315 if (new_local)
5316 --cctx->ctx_locals.ga_len;
5317 instr_count = instr->ga_len;
5318 p = skipwhite(op + oplen);
5319 r = compile_expr0(&p, cctx);
5320 if (new_local)
5321 ++cctx->ctx_locals.ga_len;
5322 if (r == FAIL)
5323 goto theend;
5324 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005325 else if (semicolon && var_idx == var_count - 1)
5326 {
5327 // For "[var; var] = expr" get the rest of the list
5328 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5329 goto theend;
5330 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005331 else
5332 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333 // For "[var, var] = expr" get the "var_idx" item from the
5334 // list.
5335 if (generate_GETITEM(cctx, var_idx) == FAIL)
5336 return FAIL;
5337 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005338
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005339 stacktype = stack->ga_len == 0 ? &t_void
5340 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5341 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005342 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005343 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005344 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005345 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005346 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005347 emsg(_(e_cannot_use_void));
5348 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005349 }
5350 else
5351 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005352 // An empty list or dict has a &t_void member,
5353 // for a variable that implies &t_any.
5354 if (stacktype == &t_list_empty)
5355 lvar->lv_type = &t_list_any;
5356 else if (stacktype == &t_dict_empty)
5357 lvar->lv_type = &t_dict_any;
5358 else
5359 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005360 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005361 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005362 else
5363 {
5364 type_T *use_type = lvar->lv_type;
5365
5366 if (has_index)
5367 {
5368 use_type = use_type->tt_member;
5369 if (use_type == NULL)
5370 use_type = &t_void;
5371 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005372 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005373 == FAIL)
5374 goto theend;
5375 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005376 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005377 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005378 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005379 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005380 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005381 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005382 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005383 emsg(_(e_const_req_value));
5384 goto theend;
5385 }
5386 else if (!has_type || dest == dest_option)
5387 {
5388 emsg(_(e_type_req));
5389 goto theend;
5390 }
5391 else
5392 {
5393 // variables are always initialized
5394 if (ga_grow(instr, 1) == FAIL)
5395 goto theend;
5396 switch (member_type->tt_type)
5397 {
5398 case VAR_BOOL:
5399 generate_PUSHBOOL(cctx, VVAL_FALSE);
5400 break;
5401 case VAR_FLOAT:
5402#ifdef FEAT_FLOAT
5403 generate_PUSHF(cctx, 0.0);
5404#endif
5405 break;
5406 case VAR_STRING:
5407 generate_PUSHS(cctx, NULL);
5408 break;
5409 case VAR_BLOB:
5410 generate_PUSHBLOB(cctx, NULL);
5411 break;
5412 case VAR_FUNC:
5413 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5414 break;
5415 case VAR_LIST:
5416 generate_NEWLIST(cctx, 0);
5417 break;
5418 case VAR_DICT:
5419 generate_NEWDICT(cctx, 0);
5420 break;
5421 case VAR_JOB:
5422 generate_PUSHJOB(cctx, NULL);
5423 break;
5424 case VAR_CHANNEL:
5425 generate_PUSHCHANNEL(cctx, NULL);
5426 break;
5427 case VAR_NUMBER:
5428 case VAR_UNKNOWN:
5429 case VAR_ANY:
5430 case VAR_PARTIAL:
5431 case VAR_VOID:
5432 case VAR_SPECIAL: // cannot happen
5433 generate_PUSHNR(cctx, 0);
5434 break;
5435 }
5436 }
5437 if (var_count == 0)
5438 end = p;
5439 }
5440
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005441 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005442 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005443 break;
5444
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005445 if (oplen > 0 && *op != '=')
5446 {
5447 type_T *expected = &t_number;
5448 type_T *stacktype;
5449
5450 // TODO: if type is known use float or any operation
5451
5452 if (*op == '.')
5453 expected = &t_string;
5454 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005455 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005456 goto theend;
5457
5458 if (*op == '.')
5459 generate_instr_drop(cctx, ISN_CONCAT, 1);
5460 else
5461 {
5462 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5463
5464 if (isn == NULL)
5465 goto theend;
5466 switch (*op)
5467 {
5468 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5469 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5470 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5471 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5472 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474 }
5475 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005477 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005478 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005479 int r;
5480
5481 // Compile the "idx" in "var[idx]".
5482 if (new_local)
5483 --cctx->ctx_locals.ga_len;
5484 p = skipwhite(var_start + varlen + 1);
5485 r = compile_expr0(&p, cctx);
5486 if (new_local)
5487 ++cctx->ctx_locals.ga_len;
5488 if (r == FAIL)
5489 goto theend;
5490 if (*skipwhite(p) != ']')
5491 {
5492 emsg(_(e_missbrac));
5493 goto theend;
5494 }
5495 if (type->tt_type == VAR_DICT
5496 && may_generate_2STRING(-1, cctx) == FAIL)
5497 goto theend;
5498 if (type->tt_type == VAR_LIST
5499 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005500 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005501 {
5502 emsg(_(e_number_exp));
5503 goto theend;
5504 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005505
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005506 // Load the dict or list. On the stack we then have:
5507 // - value
5508 // - index
5509 // - variable
5510 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005511
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005512 if (type->tt_type == VAR_LIST)
5513 {
5514 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5515 return FAIL;
5516 }
5517 else if (type->tt_type == VAR_DICT)
5518 {
5519 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5520 return FAIL;
5521 }
5522 else
5523 {
5524 emsg(_(e_listreq));
5525 goto theend;
5526 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005527 }
5528 else
5529 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005530 switch (dest)
5531 {
5532 case dest_option:
5533 generate_STOREOPT(cctx, name + 1, opt_flags);
5534 break;
5535 case dest_global:
5536 // include g: with the name, easier to execute that way
5537 generate_STORE(cctx, ISN_STOREG, 0, name);
5538 break;
5539 case dest_buffer:
5540 // include b: with the name, easier to execute that way
5541 generate_STORE(cctx, ISN_STOREB, 0, name);
5542 break;
5543 case dest_window:
5544 // include w: with the name, easier to execute that way
5545 generate_STORE(cctx, ISN_STOREW, 0, name);
5546 break;
5547 case dest_tab:
5548 // include t: with the name, easier to execute that way
5549 generate_STORE(cctx, ISN_STORET, 0, name);
5550 break;
5551 case dest_env:
5552 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5553 break;
5554 case dest_reg:
5555 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5556 break;
5557 case dest_vimvar:
5558 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5559 break;
5560 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005561 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005562 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5563 imported_T *import = NULL;
5564 int sid = current_sctx.sc_sid;
5565 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005566
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005567 if (name[1] != ':')
5568 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005569 import = find_imported(name, 0, cctx);
5570 if (import != NULL)
5571 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005572 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005573
5574 idx = get_script_item_idx(sid, rawname, TRUE);
5575 // TODO: specific type
5576 if (idx < 0)
5577 {
5578 char_u *name_s = name;
5579
5580 // Include s: in the name for store_var()
5581 if (name[1] != ':')
5582 {
5583 int len = (int)STRLEN(name) + 3;
5584
5585 name_s = alloc(len);
5586 if (name_s == NULL)
5587 name_s = name;
5588 else
5589 vim_snprintf((char *)name_s, len,
5590 "s:%s", name);
5591 }
5592 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005593 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005594 if (name_s != name)
5595 vim_free(name_s);
5596 }
5597 else
5598 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005599 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005600 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005601 break;
5602 case dest_local:
5603 if (lvar != NULL)
5604 {
5605 isn_T *isn = ((isn_T *)instr->ga_data)
5606 + instr->ga_len - 1;
5607
5608 // optimization: turn "var = 123" from ISN_PUSHNR +
5609 // ISN_STORE into ISN_STORENR
5610 if (!lvar->lv_from_outer
5611 && instr->ga_len == instr_count + 1
5612 && isn->isn_type == ISN_PUSHNR)
5613 {
5614 varnumber_T val = isn->isn_arg.number;
5615
5616 isn->isn_type = ISN_STORENR;
5617 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5618 isn->isn_arg.storenr.stnr_val = val;
5619 if (stack->ga_len > 0)
5620 --stack->ga_len;
5621 }
5622 else if (lvar->lv_from_outer)
5623 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005624 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005625 else
5626 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5627 }
5628 break;
5629 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005630 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005631
5632 if (var_idx + 1 < var_count)
5633 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005634 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005635
5636 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005637 if (var_count > 0 && !semicolon)
5638 {
5639 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5640 goto theend;
5641 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005642
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005643 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644
5645theend:
5646 vim_free(name);
5647 return ret;
5648}
5649
5650/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005651 * Check if "name" can be "unlet".
5652 */
5653 int
5654check_vim9_unlet(char_u *name)
5655{
5656 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5657 {
5658 semsg(_("E1081: Cannot unlet %s"), name);
5659 return FAIL;
5660 }
5661 return OK;
5662}
5663
5664/*
5665 * Callback passed to ex_unletlock().
5666 */
5667 static int
5668compile_unlet(
5669 lval_T *lvp,
5670 char_u *name_end,
5671 exarg_T *eap,
5672 int deep UNUSED,
5673 void *coookie)
5674{
5675 cctx_T *cctx = coookie;
5676
5677 if (lvp->ll_tv == NULL)
5678 {
5679 char_u *p = lvp->ll_name;
5680 int cc = *name_end;
5681 int ret = OK;
5682
5683 // Normal name. Only supports g:, w:, t: and b: namespaces.
5684 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005685 if (*p == '$')
5686 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5687 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005688 ret = FAIL;
5689 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005690 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005691
5692 *name_end = cc;
5693 return ret;
5694 }
5695
5696 // TODO: unlet {list}[idx]
5697 // TODO: unlet {dict}[key]
5698 emsg("Sorry, :unlet not fully implemented yet");
5699 return FAIL;
5700}
5701
5702/*
5703 * compile "unlet var", "lock var" and "unlock var"
5704 * "arg" points to "var".
5705 */
5706 static char_u *
5707compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5708{
5709 char_u *p = arg;
5710
5711 if (eap->cmdidx != CMD_unlet)
5712 {
5713 emsg("Sorry, :lock and unlock not implemented yet");
5714 return NULL;
5715 }
5716
5717 if (*p == '!')
5718 {
5719 p = skipwhite(p + 1);
5720 eap->forceit = TRUE;
5721 }
5722
5723 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5724 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5725}
5726
5727/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005728 * Compile an :import command.
5729 */
5730 static char_u *
5731compile_import(char_u *arg, cctx_T *cctx)
5732{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005733 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005734}
5735
5736/*
5737 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5738 */
5739 static int
5740compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5741{
5742 garray_T *instr = &cctx->ctx_instr;
5743 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5744
5745 if (endlabel == NULL)
5746 return FAIL;
5747 endlabel->el_next = *el;
5748 *el = endlabel;
5749 endlabel->el_end_label = instr->ga_len;
5750
5751 generate_JUMP(cctx, when, 0);
5752 return OK;
5753}
5754
5755 static void
5756compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5757{
5758 garray_T *instr = &cctx->ctx_instr;
5759
5760 while (*el != NULL)
5761 {
5762 endlabel_T *cur = (*el);
5763 isn_T *isn;
5764
5765 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5766 isn->isn_arg.jump.jump_where = instr->ga_len;
5767 *el = cur->el_next;
5768 vim_free(cur);
5769 }
5770}
5771
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005772 static void
5773compile_free_jump_to_end(endlabel_T **el)
5774{
5775 while (*el != NULL)
5776 {
5777 endlabel_T *cur = (*el);
5778
5779 *el = cur->el_next;
5780 vim_free(cur);
5781 }
5782}
5783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005784/*
5785 * Create a new scope and set up the generic items.
5786 */
5787 static scope_T *
5788new_scope(cctx_T *cctx, scopetype_T type)
5789{
5790 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5791
5792 if (scope == NULL)
5793 return NULL;
5794 scope->se_outer = cctx->ctx_scope;
5795 cctx->ctx_scope = scope;
5796 scope->se_type = type;
5797 scope->se_local_count = cctx->ctx_locals.ga_len;
5798 return scope;
5799}
5800
5801/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005802 * Free the current scope and go back to the outer scope.
5803 */
5804 static void
5805drop_scope(cctx_T *cctx)
5806{
5807 scope_T *scope = cctx->ctx_scope;
5808
5809 if (scope == NULL)
5810 {
5811 iemsg("calling drop_scope() without a scope");
5812 return;
5813 }
5814 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005815 switch (scope->se_type)
5816 {
5817 case IF_SCOPE:
5818 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5819 case FOR_SCOPE:
5820 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5821 case WHILE_SCOPE:
5822 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5823 case TRY_SCOPE:
5824 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5825 case NO_SCOPE:
5826 case BLOCK_SCOPE:
5827 break;
5828 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005829 vim_free(scope);
5830}
5831
5832/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005833 * compile "if expr"
5834 *
5835 * "if expr" Produces instructions:
5836 * EVAL expr Push result of "expr"
5837 * JUMP_IF_FALSE end
5838 * ... body ...
5839 * end:
5840 *
5841 * "if expr | else" Produces instructions:
5842 * EVAL expr Push result of "expr"
5843 * JUMP_IF_FALSE else
5844 * ... body ...
5845 * JUMP_ALWAYS end
5846 * else:
5847 * ... body ...
5848 * end:
5849 *
5850 * "if expr1 | elseif expr2 | else" Produces instructions:
5851 * EVAL expr Push result of "expr"
5852 * JUMP_IF_FALSE elseif
5853 * ... body ...
5854 * JUMP_ALWAYS end
5855 * elseif:
5856 * EVAL expr Push result of "expr"
5857 * JUMP_IF_FALSE else
5858 * ... body ...
5859 * JUMP_ALWAYS end
5860 * else:
5861 * ... body ...
5862 * end:
5863 */
5864 static char_u *
5865compile_if(char_u *arg, cctx_T *cctx)
5866{
5867 char_u *p = arg;
5868 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005869 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005871 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005872 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005873
Bram Moolenaara5565e42020-05-09 15:44:01 +02005874 CLEAR_FIELD(ppconst);
5875 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005876 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005877 clear_ppconst(&ppconst);
5878 return NULL;
5879 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005880 if (cctx->ctx_skip == SKIP_YES)
5881 clear_ppconst(&ppconst);
5882 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005883 {
5884 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005885 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005886 clear_ppconst(&ppconst);
5887 }
5888 else
5889 {
5890 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005891 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005892 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005893 return NULL;
5894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895
5896 scope = new_scope(cctx, IF_SCOPE);
5897 if (scope == NULL)
5898 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005899 scope->se_skip_save = skip_save;
5900 // "is_had_return" will be reset if any block does not end in :return
5901 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005903 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005904 {
5905 // "where" is set when ":elseif", "else" or ":endif" is found
5906 scope->se_u.se_if.is_if_label = instr->ga_len;
5907 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5908 }
5909 else
5910 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005911
5912 return p;
5913}
5914
5915 static char_u *
5916compile_elseif(char_u *arg, cctx_T *cctx)
5917{
5918 char_u *p = arg;
5919 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005920 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005921 isn_T *isn;
5922 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005923 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005924
5925 if (scope == NULL || scope->se_type != IF_SCOPE)
5926 {
5927 emsg(_(e_elseif_without_if));
5928 return NULL;
5929 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005930 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005931 if (!cctx->ctx_had_return)
5932 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005934 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005935 {
5936 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005937 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005938 return NULL;
5939 // previous "if" or "elseif" jumps here
5940 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5941 isn->isn_arg.jump.jump_where = instr->ga_len;
5942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005943
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005944 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005945 CLEAR_FIELD(ppconst);
5946 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005947 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005948 clear_ppconst(&ppconst);
5949 return NULL;
5950 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005951 if (scope->se_skip_save == SKIP_YES)
5952 clear_ppconst(&ppconst);
5953 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005954 {
5955 // The expression results in a constant.
5956 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005957 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005958 clear_ppconst(&ppconst);
5959 scope->se_u.se_if.is_if_label = -1;
5960 }
5961 else
5962 {
5963 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005964 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005965 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005966 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005967
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005968 // "where" is set when ":elseif", "else" or ":endif" is found
5969 scope->se_u.se_if.is_if_label = instr->ga_len;
5970 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005972
5973 return p;
5974}
5975
5976 static char_u *
5977compile_else(char_u *arg, cctx_T *cctx)
5978{
5979 char_u *p = arg;
5980 garray_T *instr = &cctx->ctx_instr;
5981 isn_T *isn;
5982 scope_T *scope = cctx->ctx_scope;
5983
5984 if (scope == NULL || scope->se_type != IF_SCOPE)
5985 {
5986 emsg(_(e_else_without_if));
5987 return NULL;
5988 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005989 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005990 if (!cctx->ctx_had_return)
5991 scope->se_u.se_if.is_had_return = FALSE;
5992 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993
Bram Moolenaarefd88552020-06-18 20:50:10 +02005994 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005995 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005996 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005997 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005998 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005999 if (!cctx->ctx_had_return
6000 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6001 JUMP_ALWAYS, cctx) == FAIL)
6002 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006003 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006004
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006005 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006006 {
6007 if (scope->se_u.se_if.is_if_label >= 0)
6008 {
6009 // previous "if" or "elseif" jumps here
6010 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6011 isn->isn_arg.jump.jump_where = instr->ga_len;
6012 scope->se_u.se_if.is_if_label = -1;
6013 }
6014 }
6015
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006016 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006017 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006019
6020 return p;
6021}
6022
6023 static char_u *
6024compile_endif(char_u *arg, cctx_T *cctx)
6025{
6026 scope_T *scope = cctx->ctx_scope;
6027 ifscope_T *ifscope;
6028 garray_T *instr = &cctx->ctx_instr;
6029 isn_T *isn;
6030
6031 if (scope == NULL || scope->se_type != IF_SCOPE)
6032 {
6033 emsg(_(e_endif_without_if));
6034 return NULL;
6035 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006036 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006037 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006038 if (!cctx->ctx_had_return)
6039 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006040
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006041 if (scope->se_u.se_if.is_if_label >= 0)
6042 {
6043 // previous "if" or "elseif" jumps here
6044 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6045 isn->isn_arg.jump.jump_where = instr->ga_len;
6046 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006047 // Fill in the "end" label in jumps at the end of the blocks.
6048 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006049 cctx->ctx_skip = scope->se_skip_save;
6050
6051 // If all the blocks end in :return and there is an :else then the
6052 // had_return flag is set.
6053 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006054
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006055 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056 return arg;
6057}
6058
6059/*
6060 * compile "for var in expr"
6061 *
6062 * Produces instructions:
6063 * PUSHNR -1
6064 * STORE loop-idx Set index to -1
6065 * EVAL expr Push result of "expr"
6066 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6067 * - if beyond end, jump to "end"
6068 * - otherwise get item from list and push it
6069 * STORE var Store item in "var"
6070 * ... body ...
6071 * JUMP top Jump back to repeat
6072 * end: DROP Drop the result of "expr"
6073 *
6074 */
6075 static char_u *
6076compile_for(char_u *arg, cctx_T *cctx)
6077{
6078 char_u *p;
6079 size_t varlen;
6080 garray_T *instr = &cctx->ctx_instr;
6081 garray_T *stack = &cctx->ctx_type_stack;
6082 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006083 lvar_T *loop_lvar; // loop iteration variable
6084 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006085 type_T *vartype;
6086
6087 // TODO: list of variables: "for [key, value] in dict"
6088 // parse "var"
6089 for (p = arg; eval_isnamec1(*p); ++p)
6090 ;
6091 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006092 var_lvar = lookup_local(arg, varlen, cctx);
6093 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006094 {
6095 semsg(_("E1023: variable already defined: %s"), arg);
6096 return NULL;
6097 }
6098
6099 // consume "in"
6100 p = skipwhite(p);
6101 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6102 {
6103 emsg(_(e_missing_in));
6104 return NULL;
6105 }
6106 p = skipwhite(p + 2);
6107
6108
6109 scope = new_scope(cctx, FOR_SCOPE);
6110 if (scope == NULL)
6111 return NULL;
6112
6113 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006114 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6115 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006116 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006117 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006118 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006121
6122 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006123 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6124 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006125 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006126 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006127 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006128 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006131 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006132
6133 // compile "expr", it remains on the stack until "endfor"
6134 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006135 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006136 {
6137 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006138 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006140
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006141 // Now that we know the type of "var", check that it is a list, now or at
6142 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006144 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006145 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006146 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006147 return NULL;
6148 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006149 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006150 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151
6152 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006153 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006154
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006155 generate_FOR(cctx, loop_lvar->lv_idx);
6156 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006157
6158 return arg;
6159}
6160
6161/*
6162 * compile "endfor"
6163 */
6164 static char_u *
6165compile_endfor(char_u *arg, cctx_T *cctx)
6166{
6167 garray_T *instr = &cctx->ctx_instr;
6168 scope_T *scope = cctx->ctx_scope;
6169 forscope_T *forscope;
6170 isn_T *isn;
6171
6172 if (scope == NULL || scope->se_type != FOR_SCOPE)
6173 {
6174 emsg(_(e_for));
6175 return NULL;
6176 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006177 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006178 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006179 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006180
6181 // At end of ":for" scope jump back to the FOR instruction.
6182 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6183
6184 // Fill in the "end" label in the FOR statement so it can jump here
6185 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6186 isn->isn_arg.forloop.for_end = instr->ga_len;
6187
6188 // Fill in the "end" label any BREAK statements
6189 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6190
6191 // Below the ":for" scope drop the "expr" list from the stack.
6192 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6193 return NULL;
6194
6195 vim_free(scope);
6196
6197 return arg;
6198}
6199
6200/*
6201 * compile "while expr"
6202 *
6203 * Produces instructions:
6204 * top: EVAL expr Push result of "expr"
6205 * JUMP_IF_FALSE end jump if false
6206 * ... body ...
6207 * JUMP top Jump back to repeat
6208 * end:
6209 *
6210 */
6211 static char_u *
6212compile_while(char_u *arg, cctx_T *cctx)
6213{
6214 char_u *p = arg;
6215 garray_T *instr = &cctx->ctx_instr;
6216 scope_T *scope;
6217
6218 scope = new_scope(cctx, WHILE_SCOPE);
6219 if (scope == NULL)
6220 return NULL;
6221
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006222 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006223
6224 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006225 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226 return NULL;
6227
6228 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006229 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 JUMP_IF_FALSE, cctx) == FAIL)
6231 return FAIL;
6232
6233 return p;
6234}
6235
6236/*
6237 * compile "endwhile"
6238 */
6239 static char_u *
6240compile_endwhile(char_u *arg, cctx_T *cctx)
6241{
6242 scope_T *scope = cctx->ctx_scope;
6243
6244 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6245 {
6246 emsg(_(e_while));
6247 return NULL;
6248 }
6249 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006250 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251
6252 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006253 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254
6255 // Fill in the "end" label in the WHILE statement so it can jump here.
6256 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006257 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006258
6259 vim_free(scope);
6260
6261 return arg;
6262}
6263
6264/*
6265 * compile "continue"
6266 */
6267 static char_u *
6268compile_continue(char_u *arg, cctx_T *cctx)
6269{
6270 scope_T *scope = cctx->ctx_scope;
6271
6272 for (;;)
6273 {
6274 if (scope == NULL)
6275 {
6276 emsg(_(e_continue));
6277 return NULL;
6278 }
6279 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6280 break;
6281 scope = scope->se_outer;
6282 }
6283
6284 // Jump back to the FOR or WHILE instruction.
6285 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006286 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6287 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006288 return arg;
6289}
6290
6291/*
6292 * compile "break"
6293 */
6294 static char_u *
6295compile_break(char_u *arg, cctx_T *cctx)
6296{
6297 scope_T *scope = cctx->ctx_scope;
6298 endlabel_T **el;
6299
6300 for (;;)
6301 {
6302 if (scope == NULL)
6303 {
6304 emsg(_(e_break));
6305 return NULL;
6306 }
6307 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6308 break;
6309 scope = scope->se_outer;
6310 }
6311
6312 // Jump to the end of the FOR or WHILE loop.
6313 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006314 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006316 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6318 return FAIL;
6319
6320 return arg;
6321}
6322
6323/*
6324 * compile "{" start of block
6325 */
6326 static char_u *
6327compile_block(char_u *arg, cctx_T *cctx)
6328{
6329 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6330 return NULL;
6331 return skipwhite(arg + 1);
6332}
6333
6334/*
6335 * compile end of block: drop one scope
6336 */
6337 static void
6338compile_endblock(cctx_T *cctx)
6339{
6340 scope_T *scope = cctx->ctx_scope;
6341
6342 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006343 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344 vim_free(scope);
6345}
6346
6347/*
6348 * compile "try"
6349 * Creates a new scope for the try-endtry, pointing to the first catch and
6350 * finally.
6351 * Creates another scope for the "try" block itself.
6352 * TRY instruction sets up exception handling at runtime.
6353 *
6354 * "try"
6355 * TRY -> catch1, -> finally push trystack entry
6356 * ... try block
6357 * "throw {exception}"
6358 * EVAL {exception}
6359 * THROW create exception
6360 * ... try block
6361 * " catch {expr}"
6362 * JUMP -> finally
6363 * catch1: PUSH exeception
6364 * EVAL {expr}
6365 * MATCH
6366 * JUMP nomatch -> catch2
6367 * CATCH remove exception
6368 * ... catch block
6369 * " catch"
6370 * JUMP -> finally
6371 * catch2: CATCH remove exception
6372 * ... catch block
6373 * " finally"
6374 * finally:
6375 * ... finally block
6376 * " endtry"
6377 * ENDTRY pop trystack entry, may rethrow
6378 */
6379 static char_u *
6380compile_try(char_u *arg, cctx_T *cctx)
6381{
6382 garray_T *instr = &cctx->ctx_instr;
6383 scope_T *try_scope;
6384 scope_T *scope;
6385
6386 // scope that holds the jumps that go to catch/finally/endtry
6387 try_scope = new_scope(cctx, TRY_SCOPE);
6388 if (try_scope == NULL)
6389 return NULL;
6390
6391 // "catch" is set when the first ":catch" is found.
6392 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006393 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394 if (generate_instr(cctx, ISN_TRY) == NULL)
6395 return NULL;
6396
6397 // scope for the try block itself
6398 scope = new_scope(cctx, BLOCK_SCOPE);
6399 if (scope == NULL)
6400 return NULL;
6401
6402 return arg;
6403}
6404
6405/*
6406 * compile "catch {expr}"
6407 */
6408 static char_u *
6409compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6410{
6411 scope_T *scope = cctx->ctx_scope;
6412 garray_T *instr = &cctx->ctx_instr;
6413 char_u *p;
6414 isn_T *isn;
6415
6416 // end block scope from :try or :catch
6417 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6418 compile_endblock(cctx);
6419 scope = cctx->ctx_scope;
6420
6421 // Error if not in a :try scope
6422 if (scope == NULL || scope->se_type != TRY_SCOPE)
6423 {
6424 emsg(_(e_catch));
6425 return NULL;
6426 }
6427
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006428 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006429 {
6430 emsg(_("E1033: catch unreachable after catch-all"));
6431 return NULL;
6432 }
6433
6434 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006435 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436 JUMP_ALWAYS, cctx) == FAIL)
6437 return NULL;
6438
6439 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006440 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441 if (isn->isn_arg.try.try_catch == 0)
6442 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006443 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006444 {
6445 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006446 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006447 isn->isn_arg.jump.jump_where = instr->ga_len;
6448 }
6449
6450 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006451 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006452 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006453 scope->se_u.se_try.ts_caught_all = TRUE;
6454 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006455 }
6456 else
6457 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006458 char_u *end;
6459 char_u *pat;
6460 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006461 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006462 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464 // Push v:exception, push {expr} and MATCH
6465 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6466
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006467 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006468 if (*end != *p)
6469 {
6470 semsg(_("E1067: Separator mismatch: %s"), p);
6471 vim_free(tofree);
6472 return FAIL;
6473 }
6474 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006475 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006476 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006477 len = (int)(end - tofree);
6478 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006479 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006480 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006481 if (pat == NULL)
6482 return FAIL;
6483 if (generate_PUSHS(cctx, pat) == FAIL)
6484 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6487 return NULL;
6488
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006489 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006490 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6491 return NULL;
6492 }
6493
6494 if (generate_instr(cctx, ISN_CATCH) == NULL)
6495 return NULL;
6496
6497 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6498 return NULL;
6499 return p;
6500}
6501
6502 static char_u *
6503compile_finally(char_u *arg, cctx_T *cctx)
6504{
6505 scope_T *scope = cctx->ctx_scope;
6506 garray_T *instr = &cctx->ctx_instr;
6507 isn_T *isn;
6508
6509 // end block scope from :try or :catch
6510 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6511 compile_endblock(cctx);
6512 scope = cctx->ctx_scope;
6513
6514 // Error if not in a :try scope
6515 if (scope == NULL || scope->se_type != TRY_SCOPE)
6516 {
6517 emsg(_(e_finally));
6518 return NULL;
6519 }
6520
6521 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006522 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 if (isn->isn_arg.try.try_finally != 0)
6524 {
6525 emsg(_(e_finally_dup));
6526 return NULL;
6527 }
6528
6529 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006530 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006531
Bram Moolenaar585fea72020-04-02 22:33:21 +02006532 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006533 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006534 {
6535 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006536 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537 isn->isn_arg.jump.jump_where = instr->ga_len;
6538 }
6539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540 // TODO: set index in ts_finally_label jumps
6541
6542 return arg;
6543}
6544
6545 static char_u *
6546compile_endtry(char_u *arg, cctx_T *cctx)
6547{
6548 scope_T *scope = cctx->ctx_scope;
6549 garray_T *instr = &cctx->ctx_instr;
6550 isn_T *isn;
6551
6552 // end block scope from :catch or :finally
6553 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6554 compile_endblock(cctx);
6555 scope = cctx->ctx_scope;
6556
6557 // Error if not in a :try scope
6558 if (scope == NULL || scope->se_type != TRY_SCOPE)
6559 {
6560 if (scope == NULL)
6561 emsg(_(e_no_endtry));
6562 else if (scope->se_type == WHILE_SCOPE)
6563 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006564 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006565 emsg(_(e_endfor));
6566 else
6567 emsg(_(e_endif));
6568 return NULL;
6569 }
6570
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006571 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6573 {
6574 emsg(_("E1032: missing :catch or :finally"));
6575 return NULL;
6576 }
6577
6578 // Fill in the "end" label in jumps at the end of the blocks, if not done
6579 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006580 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006581
6582 // End :catch or :finally scope: set value in ISN_TRY instruction
6583 if (isn->isn_arg.try.try_finally == 0)
6584 isn->isn_arg.try.try_finally = instr->ga_len;
6585 compile_endblock(cctx);
6586
6587 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6588 return NULL;
6589 return arg;
6590}
6591
6592/*
6593 * compile "throw {expr}"
6594 */
6595 static char_u *
6596compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6597{
6598 char_u *p = skipwhite(arg);
6599
Bram Moolenaara5565e42020-05-09 15:44:01 +02006600 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 return NULL;
6602 if (may_generate_2STRING(-1, cctx) == FAIL)
6603 return NULL;
6604 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6605 return NULL;
6606
6607 return p;
6608}
6609
6610/*
6611 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006612 * compile "echomsg expr"
6613 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006614 * compile "execute expr"
6615 */
6616 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006617compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006618{
6619 char_u *p = arg;
6620 int count = 0;
6621
6622 for (;;)
6623 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006624 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006625 return NULL;
6626 ++count;
6627 p = skipwhite(p);
6628 if (ends_excmd(*p))
6629 break;
6630 }
6631
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006632 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6633 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6634 else if (cmdidx == CMD_execute)
6635 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6636 else if (cmdidx == CMD_echomsg)
6637 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6638 else
6639 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006640 return p;
6641}
6642
6643/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006644 * A command that is not compiled, execute with legacy code.
6645 */
6646 static char_u *
6647compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6648{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006649 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006650 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006651 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006652
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006653 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006654 goto theend;
6655
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006656 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006657 {
6658 long argt = excmd_get_argt(eap->cmdidx);
6659 int usefilter = FALSE;
6660
6661 has_expr = argt & (EX_XFILE | EX_EXPAND);
6662
6663 // If the command can be followed by a bar, find the bar and truncate
6664 // it, so that the following command can be compiled.
6665 // The '|' is overwritten with a NUL, it is put back below.
6666 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6667 && *eap->arg == '!')
6668 // :w !filter or :r !filter or :r! filter
6669 usefilter = TRUE;
6670 if ((argt & EX_TRLBAR) && !usefilter)
6671 {
6672 separate_nextcmd(eap);
6673 if (eap->nextcmd != NULL)
6674 nextcmd = eap->nextcmd;
6675 }
6676 }
6677
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006678 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6679 {
6680 // expand filename in "syntax include [@group] filename"
6681 has_expr = TRUE;
6682 eap->arg = skipwhite(eap->arg + 7);
6683 if (*eap->arg == '@')
6684 eap->arg = skiptowhite(eap->arg);
6685 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006686
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006687 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006688 {
6689 int count = 0;
6690 char_u *start = skipwhite(line);
6691
6692 // :cmd xxx`=expr1`yyy`=expr2`zzz
6693 // PUSHS ":cmd xxx"
6694 // eval expr1
6695 // PUSHS "yyy"
6696 // eval expr2
6697 // PUSHS "zzz"
6698 // EXECCONCAT 5
6699 for (;;)
6700 {
6701 if (p > start)
6702 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006703 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006704 ++count;
6705 }
6706 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006707 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006708 return NULL;
6709 may_generate_2STRING(-1, cctx);
6710 ++count;
6711 p = skipwhite(p);
6712 if (*p != '`')
6713 {
6714 emsg(_("E1083: missing backtick"));
6715 return NULL;
6716 }
6717 start = p + 1;
6718
6719 p = (char_u *)strstr((char *)start, "`=");
6720 if (p == NULL)
6721 {
6722 if (*skipwhite(start) != NUL)
6723 {
6724 generate_PUSHS(cctx, vim_strsave(start));
6725 ++count;
6726 }
6727 break;
6728 }
6729 }
6730 generate_EXECCONCAT(cctx, count);
6731 }
6732 else
6733 generate_EXEC(cctx, line);
6734
6735theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006736 if (*nextcmd != NUL)
6737 {
6738 // the parser expects a pointer to the bar, put it back
6739 --nextcmd;
6740 *nextcmd = '|';
6741 }
6742
6743 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006744}
6745
6746/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006747 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006748 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006749 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006750 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006751add_def_function(ufunc_T *ufunc)
6752{
6753 dfunc_T *dfunc;
6754
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006755 if (def_functions.ga_len == 0)
6756 {
6757 // The first position is not used, so that a zero uf_dfunc_idx means it
6758 // wasn't set.
6759 if (ga_grow(&def_functions, 1) == FAIL)
6760 return FAIL;
6761 ++def_functions.ga_len;
6762 }
6763
Bram Moolenaar09689a02020-05-09 22:50:08 +02006764 // Add the function to "def_functions".
6765 if (ga_grow(&def_functions, 1) == FAIL)
6766 return FAIL;
6767 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6768 CLEAR_POINTER(dfunc);
6769 dfunc->df_idx = def_functions.ga_len;
6770 ufunc->uf_dfunc_idx = dfunc->df_idx;
6771 dfunc->df_ufunc = ufunc;
6772 ++def_functions.ga_len;
6773 return OK;
6774}
6775
6776/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 * After ex_function() has collected all the function lines: parse and compile
6778 * the lines into instructions.
6779 * Adds the function to "def_functions".
6780 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6781 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006782 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006783 * This can be used recursively through compile_lambda(), which may reallocate
6784 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006785 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006787 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006788compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790 char_u *line = NULL;
6791 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 cctx_T cctx;
6794 garray_T *instr;
6795 int called_emsg_before = called_emsg;
6796 int ret = FAIL;
6797 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006798 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006799 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006800
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006801 // When using a function that was compiled before: Free old instructions.
6802 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006803 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006805 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6806 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006807 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006809 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006810 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006811
Bram Moolenaar985116a2020-07-12 17:31:09 +02006812 ufunc->uf_def_status = UF_COMPILING;
6813
Bram Moolenaara80faa82020-04-12 19:37:17 +02006814 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006815 cctx.ctx_ufunc = ufunc;
6816 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006817 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006818 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6819 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6820 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6821 cctx.ctx_type_list = &ufunc->uf_type_list;
6822 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6823 instr = &cctx.ctx_instr;
6824
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006825 // Set the context to the function, it may be compiled when called from
6826 // another script. Set the script version to the most modern one.
6827 // The line number will be set in next_line_from_context().
6828 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006829 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6830
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006831 // Make sure error messages are OK.
6832 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6833 if (do_estack_push)
6834 estack_push_ufunc(ufunc, 1);
6835
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006836 if (ufunc->uf_def_args.ga_len > 0)
6837 {
6838 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006839 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006840 int i;
6841 char_u *arg;
6842 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6843
6844 // Produce instructions for the default values of optional arguments.
6845 // Store the instruction index in uf_def_arg_idx[] so that we know
6846 // where to start when the function is called, depending on the number
6847 // of arguments.
6848 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6849 if (ufunc->uf_def_arg_idx == NULL)
6850 goto erret;
6851 for (i = 0; i < count; ++i)
6852 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006853 garray_T *stack = &cctx.ctx_type_stack;
6854 type_T *val_type;
6855 int arg_idx = first_def_arg + i;
6856
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006857 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6858 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006859 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006860 goto erret;
6861
6862 // If no type specified use the type of the default value.
6863 // Otherwise check that the default value type matches the
6864 // specified type.
6865 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6866 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6867 ufunc->uf_arg_types[arg_idx] = val_type;
6868 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6869 == FAIL)
6870 {
6871 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6872 arg_idx + 1);
6873 goto erret;
6874 }
6875
6876 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006877 goto erret;
6878 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006879 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6880 }
6881
6882 /*
6883 * Loop over all the lines of the function and generate instructions.
6884 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885 for (;;)
6886 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006887 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006888 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006889 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006890 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006891
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006892 // Bail out on the first error to avoid a flood of errors and report
6893 // the right line number when inside try/catch.
6894 if (emsg_before != called_emsg)
6895 goto erret;
6896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 if (line != NULL && *line == '|')
6898 // the line continues after a '|'
6899 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006900 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006901 && !(*line == '#' && (line == cctx.ctx_line_start
6902 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006904 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006905 goto erret;
6906 }
6907 else
6908 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006909 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006910 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006911 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006914 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006915
Bram Moolenaara80faa82020-04-12 19:37:17 +02006916 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006917 ea.cmdlinep = &line;
6918 ea.cmd = skipwhite(line);
6919
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006920 // Some things can be recognized by the first character.
6921 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006923 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006924 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006925 if (ea.cmd[1] != '{')
6926 {
6927 line = (char_u *)"";
6928 continue;
6929 }
6930 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006932 case '}':
6933 {
6934 // "}" ends a block scope
6935 scopetype_T stype = cctx.ctx_scope == NULL
6936 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006938 if (stype == BLOCK_SCOPE)
6939 {
6940 compile_endblock(&cctx);
6941 line = ea.cmd;
6942 }
6943 else
6944 {
6945 emsg(_("E1025: using } outside of a block scope"));
6946 goto erret;
6947 }
6948 if (line != NULL)
6949 line = skipwhite(ea.cmd + 1);
6950 continue;
6951 }
6952
6953 case '{':
6954 // "{" starts a block scope
6955 // "{'a': 1}->func() is something else
6956 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6957 {
6958 line = compile_block(ea.cmd, &cctx);
6959 continue;
6960 }
6961 break;
6962
6963 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006964 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006965 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966 }
6967
6968 /*
6969 * COMMAND MODIFIERS
6970 */
6971 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6972 {
6973 if (errormsg != NULL)
6974 goto erret;
6975 // empty line or comment
6976 line = (char_u *)"";
6977 continue;
6978 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006979 // TODO: use modifiers in the command
6980 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02006981 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982
6983 // Skip ":call" to get to the function name.
6984 if (checkforcmd(&ea.cmd, "call", 3))
6985 ea.cmd = skipwhite(ea.cmd);
6986
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006987 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006989 char_u *pskip;
6990
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006991 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006992 // find what follows.
6993 // Skip over "var.member", "var[idx]" and the like.
6994 // Also "&opt = val", "$ENV = val" and "@r = val".
6995 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006996 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006997 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006998 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007000 char_u *var_end;
7001 int oplen;
7002 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007004 var_end = find_name_end(pskip, NULL, NULL,
7005 FNE_CHECK_START | FNE_INCL_BR);
7006 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007007 if (oplen > 0)
7008 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007009 size_t len = p - ea.cmd;
7010
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007011 // Recognize an assignment if we recognize the variable
7012 // name:
7013 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007014 // "local = expr" where "local" is a local var.
7015 // "script = expr" where "script" is a script-local var.
7016 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007017 // "&opt = expr"
7018 // "$ENV = expr"
7019 // "@r = expr"
7020 if (*ea.cmd == '&'
7021 || *ea.cmd == '$'
7022 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007023 || ((len) > 2 && ea.cmd[1] == ':')
7024 || lookup_local(ea.cmd, len, &cctx) != NULL
7025 || lookup_arg(ea.cmd, len, NULL, NULL,
7026 NULL, &cctx) == OK
7027 || lookup_script(ea.cmd, len) == OK
7028 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007029 {
7030 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007031 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007032 goto erret;
7033 continue;
7034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035 }
7036 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007037
7038 if (*ea.cmd == '[')
7039 {
7040 // [var, var] = expr
7041 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7042 if (line == NULL)
7043 goto erret;
7044 if (line != ea.cmd)
7045 continue;
7046 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047 }
7048
7049 /*
7050 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007051 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007053 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007054 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007055 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007056 ea.cmd = skip_range(ea.cmd, NULL);
7057 if (ea.cmd > cmd && !starts_with_colon)
7058 {
7059 emsg(_(e_colon_required));
7060 goto erret;
7061 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007062 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007063 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007064 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007065 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066
7067 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7068 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007069 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007070 {
7071 line += STRLEN(line);
7072 continue;
7073 }
7074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007076 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007078 // CMD_let cannot happen, compile_assignment() above is used
7079 iemsg("Command from find_ex_command() not handled");
7080 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082 }
7083
7084 p = skipwhite(p);
7085
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007086 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007087 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007088 && ea.cmdidx != CMD_elseif
7089 && ea.cmdidx != CMD_else
7090 && ea.cmdidx != CMD_endif)
7091 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007092 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007093 continue;
7094 }
7095
Bram Moolenaarefd88552020-06-18 20:50:10 +02007096 if (ea.cmdidx != CMD_elseif
7097 && ea.cmdidx != CMD_else
7098 && ea.cmdidx != CMD_endif
7099 && ea.cmdidx != CMD_endfor
7100 && ea.cmdidx != CMD_endwhile
7101 && ea.cmdidx != CMD_catch
7102 && ea.cmdidx != CMD_finally
7103 && ea.cmdidx != CMD_endtry)
7104 {
7105 if (cctx.ctx_had_return)
7106 {
7107 emsg(_("E1095: Unreachable code after :return"));
7108 goto erret;
7109 }
7110 }
7111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007112 switch (ea.cmdidx)
7113 {
7114 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007115 ea.arg = p;
7116 line = compile_nested_function(&ea, &cctx);
7117 break;
7118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007119 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007120 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 goto erret;
7122
7123 case CMD_return:
7124 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007125 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126 break;
7127
7128 case CMD_let:
7129 case CMD_const:
7130 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007131 if (line == p)
7132 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 break;
7134
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007135 case CMD_unlet:
7136 case CMD_unlockvar:
7137 case CMD_lockvar:
7138 line = compile_unletlock(p, &ea, &cctx);
7139 break;
7140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141 case CMD_import:
7142 line = compile_import(p, &cctx);
7143 break;
7144
7145 case CMD_if:
7146 line = compile_if(p, &cctx);
7147 break;
7148 case CMD_elseif:
7149 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007150 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 break;
7152 case CMD_else:
7153 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007154 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 break;
7156 case CMD_endif:
7157 line = compile_endif(p, &cctx);
7158 break;
7159
7160 case CMD_while:
7161 line = compile_while(p, &cctx);
7162 break;
7163 case CMD_endwhile:
7164 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007165 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166 break;
7167
7168 case CMD_for:
7169 line = compile_for(p, &cctx);
7170 break;
7171 case CMD_endfor:
7172 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007173 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007174 break;
7175 case CMD_continue:
7176 line = compile_continue(p, &cctx);
7177 break;
7178 case CMD_break:
7179 line = compile_break(p, &cctx);
7180 break;
7181
7182 case CMD_try:
7183 line = compile_try(p, &cctx);
7184 break;
7185 case CMD_catch:
7186 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007187 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188 break;
7189 case CMD_finally:
7190 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007191 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192 break;
7193 case CMD_endtry:
7194 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007195 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196 break;
7197 case CMD_throw:
7198 line = compile_throw(p, &cctx);
7199 break;
7200
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007201 case CMD_eval:
7202 if (compile_expr0(&p, &cctx) == FAIL)
7203 goto erret;
7204
7205 // drop the return value
7206 generate_instr_drop(&cctx, ISN_DROP, 1);
7207
7208 line = skipwhite(p);
7209 break;
7210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007212 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007213 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007214 case CMD_echomsg:
7215 case CMD_echoerr:
7216 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007217 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007218
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007219 // TODO: other commands with an expression argument
7220
Bram Moolenaar002262f2020-07-08 17:47:57 +02007221 case CMD_SIZE:
7222 semsg(_("E476: Invalid command: %s"), ea.cmd);
7223 goto erret;
7224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007226 // Not recognized, execute with do_cmdline_cmd().
7227 ea.arg = p;
7228 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007229 break;
7230 }
7231 if (line == NULL)
7232 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007233 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234
7235 if (cctx.ctx_type_stack.ga_len < 0)
7236 {
7237 iemsg("Type stack underflow");
7238 goto erret;
7239 }
7240 }
7241
7242 if (cctx.ctx_scope != NULL)
7243 {
7244 if (cctx.ctx_scope->se_type == IF_SCOPE)
7245 emsg(_(e_endif));
7246 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7247 emsg(_(e_endwhile));
7248 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7249 emsg(_(e_endfor));
7250 else
7251 emsg(_("E1026: Missing }"));
7252 goto erret;
7253 }
7254
Bram Moolenaarefd88552020-06-18 20:50:10 +02007255 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256 {
7257 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7258 {
7259 emsg(_("E1027: Missing return statement"));
7260 goto erret;
7261 }
7262
7263 // Return zero if there is no return at the end.
7264 generate_PUSHNR(&cctx, 0);
7265 generate_instr(&cctx, ISN_RETURN);
7266 }
7267
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007268 {
7269 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7270 + ufunc->uf_dfunc_idx;
7271 dfunc->df_deleted = FALSE;
7272 dfunc->df_instr = instr->ga_data;
7273 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007274 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007275 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007276 if (cctx.ctx_outer_used)
7277 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007278 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007279 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007280
7281 ret = OK;
7282
7283erret:
7284 if (ret == FAIL)
7285 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007286 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007287 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7288 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007289
7290 for (idx = 0; idx < instr->ga_len; ++idx)
7291 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007292 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007293
Bram Moolenaar45a15082020-05-25 00:28:33 +02007294 // if using the last entry in the table we might as well remove it
7295 if (!dfunc->df_deleted
7296 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007297 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007298 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007299
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007300 while (cctx.ctx_scope != NULL)
7301 drop_scope(&cctx);
7302
Bram Moolenaar20431c92020-03-20 18:39:46 +01007303 // Don't execute this function body.
7304 ga_clear_strings(&ufunc->uf_lines);
7305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306 if (errormsg != NULL)
7307 emsg(errormsg);
7308 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007309 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 }
7311
7312 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007313 if (do_estack_push)
7314 estack_pop();
7315
Bram Moolenaar20431c92020-03-20 18:39:46 +01007316 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007317 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007318 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007319 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007320}
7321
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007322 void
7323set_function_type(ufunc_T *ufunc)
7324{
7325 int varargs = ufunc->uf_va_name != NULL;
7326 int argcount = ufunc->uf_args.ga_len;
7327
7328 // Create a type for the function, with the return type and any
7329 // argument types.
7330 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7331 // The type is included in "tt_args".
7332 if (argcount > 0 || varargs)
7333 {
7334 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7335 argcount, &ufunc->uf_type_list);
7336 // Add argument types to the function type.
7337 if (func_type_add_arg_types(ufunc->uf_func_type,
7338 argcount + varargs,
7339 &ufunc->uf_type_list) == FAIL)
7340 return;
7341 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7342 ufunc->uf_func_type->tt_min_argcount =
7343 argcount - ufunc->uf_def_args.ga_len;
7344 if (ufunc->uf_arg_types == NULL)
7345 {
7346 int i;
7347
7348 // lambda does not have argument types.
7349 for (i = 0; i < argcount; ++i)
7350 ufunc->uf_func_type->tt_args[i] = &t_any;
7351 }
7352 else
7353 mch_memmove(ufunc->uf_func_type->tt_args,
7354 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7355 if (varargs)
7356 {
7357 ufunc->uf_func_type->tt_args[argcount] =
7358 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7359 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7360 }
7361 }
7362 else
7363 // No arguments, can use a predefined type.
7364 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7365 argcount, &ufunc->uf_type_list);
7366}
7367
7368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369/*
7370 * Delete an instruction, free what it contains.
7371 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007372 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373delete_instr(isn_T *isn)
7374{
7375 switch (isn->isn_type)
7376 {
7377 case ISN_EXEC:
7378 case ISN_LOADENV:
7379 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007380 case ISN_LOADB:
7381 case ISN_LOADW:
7382 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007384 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385 case ISN_PUSHEXC:
7386 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007387 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007389 case ISN_STOREB:
7390 case ISN_STOREW:
7391 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007392 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393 vim_free(isn->isn_arg.string);
7394 break;
7395
7396 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007397 case ISN_STORES:
7398 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 break;
7400
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007401 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007402 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007403 vim_free(isn->isn_arg.unlet.ul_name);
7404 break;
7405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406 case ISN_STOREOPT:
7407 vim_free(isn->isn_arg.storeopt.so_name);
7408 break;
7409
7410 case ISN_PUSHBLOB: // push blob isn_arg.blob
7411 blob_unref(isn->isn_arg.blob);
7412 break;
7413
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007414 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007415#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007416 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007417#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007418 break;
7419
7420 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007421#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007422 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007423#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007424 break;
7425
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 case ISN_UCALL:
7427 vim_free(isn->isn_arg.ufunc.cuf_name);
7428 break;
7429
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007430 case ISN_FUNCREF:
7431 {
7432 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7433 + isn->isn_arg.funcref.fr_func;
7434 func_ptr_unref(dfunc->df_ufunc);
7435 }
7436 break;
7437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007438 case ISN_2BOOL:
7439 case ISN_2STRING:
7440 case ISN_ADDBLOB:
7441 case ISN_ADDLIST:
7442 case ISN_BCALL:
7443 case ISN_CATCH:
7444 case ISN_CHECKNR:
7445 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007446 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447 case ISN_COMPAREANY:
7448 case ISN_COMPAREBLOB:
7449 case ISN_COMPAREBOOL:
7450 case ISN_COMPAREDICT:
7451 case ISN_COMPAREFLOAT:
7452 case ISN_COMPAREFUNC:
7453 case ISN_COMPARELIST:
7454 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455 case ISN_COMPARESPECIAL:
7456 case ISN_COMPARESTRING:
7457 case ISN_CONCAT:
7458 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007459 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 case ISN_DROP:
7461 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007462 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007463 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007465 case ISN_EXECCONCAT:
7466 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007469 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007470 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007471 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 case ISN_JUMP:
7473 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007474 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475 case ISN_LOADSCRIPT:
7476 case ISN_LOADREG:
7477 case ISN_LOADV:
7478 case ISN_NEGATENR:
7479 case ISN_NEWDICT:
7480 case ISN_NEWLIST:
7481 case ISN_OPNR:
7482 case ISN_OPFLOAT:
7483 case ISN_OPANY:
7484 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007485 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 case ISN_PUSHF:
7487 case ISN_PUSHNR:
7488 case ISN_PUSHBOOL:
7489 case ISN_PUSHSPEC:
7490 case ISN_RETURN:
7491 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007492 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007493 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007495 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007497 case ISN_STOREDICT:
7498 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007499 case ISN_THROW:
7500 case ISN_TRY:
7501 // nothing allocated
7502 break;
7503 }
7504}
7505
7506/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007507 * Free all instructions for "dfunc".
7508 */
7509 static void
7510delete_def_function_contents(dfunc_T *dfunc)
7511{
7512 int idx;
7513
7514 ga_clear(&dfunc->df_def_args_isn);
7515
7516 if (dfunc->df_instr != NULL)
7517 {
7518 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7519 delete_instr(dfunc->df_instr + idx);
7520 VIM_CLEAR(dfunc->df_instr);
7521 }
7522
7523 dfunc->df_deleted = TRUE;
7524}
7525
7526/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007527 * When a user function is deleted, clear the contents of any associated def
7528 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529 */
7530 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007531clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007533 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007534 {
7535 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7536 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537
Bram Moolenaar20431c92020-03-20 18:39:46 +01007538 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007539 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007540 }
7541}
7542
7543#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007544/*
7545 * Free all functions defined with ":def".
7546 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007547 void
7548free_def_functions(void)
7549{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007550 int idx;
7551
7552 for (idx = 0; idx < def_functions.ga_len; ++idx)
7553 {
7554 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7555
7556 delete_def_function_contents(dfunc);
7557 }
7558
7559 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007560}
7561#endif
7562
7563
7564#endif // FEAT_EVAL