blob: 634706b508fef51d910c5ff7f2aa350440774c63 [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
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002423vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002424{
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 Moolenaarf5be8cd2020-07-17 20:36:00 +02002446 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002447 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;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002464 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002465 {
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
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002495 || (skip_comment && vim9_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 Moolenaarf5be8cd2020-07-17 20:36:00 +02002507 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_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.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003103 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003104 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 Moolenaarf5be8cd2020-07-17 20:36:00 +02003160
3161 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 return FAIL;
3163}
3164
3165/*
3166 * Compile a lamda call: expr->{lambda}(args)
3167 * "arg" points to the "{".
3168 */
3169 static int
3170compile_lambda_call(char_u **arg, cctx_T *cctx)
3171{
3172 ufunc_T *ufunc;
3173 typval_T rettv;
3174 int argcount = 1;
3175 int ret = FAIL;
3176
3177 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003178 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 return FAIL;
3180
3181 if (**arg != '(')
3182 {
3183 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003184 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 else
3186 semsg(_(e_missing_paren), "lambda");
3187 clear_tv(&rettv);
3188 return FAIL;
3189 }
3190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 ufunc = rettv.vval.v_partial->pt_func;
3192 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003193 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003194 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003195
3196 // The function will have one line: "return {expr}".
3197 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003198 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199
3200 // compile the arguments
3201 *arg = skipwhite(*arg + 1);
3202 if (compile_arguments(arg, cctx, &argcount) == OK)
3203 // call the compiled function
3204 ret = generate_CALL(cctx, ufunc, argcount);
3205
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003206 if (ret == FAIL)
3207 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 return ret;
3209}
3210
3211/*
3212 * parse a dict: {'key': val} or #{key: val}
3213 * "*arg" points to the '{'.
3214 */
3215 static int
3216compile_dict(char_u **arg, cctx_T *cctx, int literal)
3217{
3218 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003219 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 int count = 0;
3221 dict_T *d = dict_alloc();
3222 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003223 char_u *whitep = *arg;
3224 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225
3226 if (d == NULL)
3227 return FAIL;
3228 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003229 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 {
3231 char_u *key = NULL;
3232
Bram Moolenaar23c55272020-06-21 16:58:13 +02003233 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003234 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003235 *arg = NULL;
3236 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003237 }
3238
3239 if (**arg == '}')
3240 break;
3241
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 if (literal)
3243 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003244 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245
Bram Moolenaar2c330432020-04-13 14:41:35 +02003246 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 {
3248 semsg(_("E1014: Invalid key: %s"), *arg);
3249 return FAIL;
3250 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003251 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 if (generate_PUSHS(cctx, key) == FAIL)
3253 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003254 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 }
3256 else
3257 {
3258 isn_T *isn;
3259
Bram Moolenaara5565e42020-05-09 15:44:01 +02003260 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3263 if (isn->isn_type == ISN_PUSHS)
3264 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003265 else
3266 {
3267 type_T *keytype = ((type_T **)stack->ga_data)
3268 [stack->ga_len - 1];
3269 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3270 return FAIL;
3271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 }
3273
3274 // Check for duplicate keys, if using string keys.
3275 if (key != NULL)
3276 {
3277 item = dict_find(d, key, -1);
3278 if (item != NULL)
3279 {
3280 semsg(_(e_duplicate_key), key);
3281 goto failret;
3282 }
3283 item = dictitem_alloc(key);
3284 if (item != NULL)
3285 {
3286 item->di_tv.v_type = VAR_UNKNOWN;
3287 item->di_tv.v_lock = 0;
3288 if (dict_add(d, item) == FAIL)
3289 dictitem_free(item);
3290 }
3291 }
3292
3293 *arg = skipwhite(*arg);
3294 if (**arg != ':')
3295 {
3296 semsg(_(e_missing_dict_colon), *arg);
3297 return FAIL;
3298 }
3299
Bram Moolenaar2c330432020-04-13 14:41:35 +02003300 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003302 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003303 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003304 *arg = NULL;
3305 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003306 }
3307
Bram Moolenaara5565e42020-05-09 15:44:01 +02003308 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 return FAIL;
3310 ++count;
3311
Bram Moolenaar2c330432020-04-13 14:41:35 +02003312 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003313 *arg = skipwhite(*arg);
3314 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003315 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003316 *arg = NULL;
3317 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 if (**arg == '}')
3320 break;
3321 if (**arg != ',')
3322 {
3323 semsg(_(e_missing_dict_comma), *arg);
3324 goto failret;
3325 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003326 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 *arg = skipwhite(*arg + 1);
3328 }
3329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 *arg = *arg + 1;
3331
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003332 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003333 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003334 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003335 *arg += STRLEN(*arg);
3336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 dict_unref(d);
3338 return generate_NEWDICT(cctx, count);
3339
3340failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003341 if (*arg == NULL)
3342 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 dict_unref(d);
3344 return FAIL;
3345}
3346
3347/*
3348 * Compile "&option".
3349 */
3350 static int
3351compile_get_option(char_u **arg, cctx_T *cctx)
3352{
3353 typval_T rettv;
3354 char_u *start = *arg;
3355 int ret;
3356
3357 // parse the option and get the current value to get the type.
3358 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003359 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 if (ret == OK)
3361 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003362 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 char_u *name = vim_strnsave(start, *arg - start);
3364 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3365
3366 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3367 vim_free(name);
3368 }
3369 clear_tv(&rettv);
3370
3371 return ret;
3372}
3373
3374/*
3375 * Compile "$VAR".
3376 */
3377 static int
3378compile_get_env(char_u **arg, cctx_T *cctx)
3379{
3380 char_u *start = *arg;
3381 int len;
3382 int ret;
3383 char_u *name;
3384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 ++*arg;
3386 len = get_env_len(arg);
3387 if (len == 0)
3388 {
3389 semsg(_(e_syntax_at), start - 1);
3390 return FAIL;
3391 }
3392
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003393 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 name = vim_strnsave(start, len + 1);
3395 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3396 vim_free(name);
3397 return ret;
3398}
3399
3400/*
3401 * Compile "@r".
3402 */
3403 static int
3404compile_get_register(char_u **arg, cctx_T *cctx)
3405{
3406 int ret;
3407
3408 ++*arg;
3409 if (**arg == NUL)
3410 {
3411 semsg(_(e_syntax_at), *arg - 1);
3412 return FAIL;
3413 }
3414 if (!valid_yank_reg(**arg, TRUE))
3415 {
3416 emsg_invreg(**arg);
3417 return FAIL;
3418 }
3419 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3420 ++*arg;
3421 return ret;
3422}
3423
3424/*
3425 * Apply leading '!', '-' and '+' to constant "rettv".
3426 */
3427 static int
3428apply_leader(typval_T *rettv, char_u *start, char_u *end)
3429{
3430 char_u *p = end;
3431
3432 // this works from end to start
3433 while (p > start)
3434 {
3435 --p;
3436 if (*p == '-' || *p == '+')
3437 {
3438 // only '-' has an effect, for '+' we only check the type
3439#ifdef FEAT_FLOAT
3440 if (rettv->v_type == VAR_FLOAT)
3441 {
3442 if (*p == '-')
3443 rettv->vval.v_float = -rettv->vval.v_float;
3444 }
3445 else
3446#endif
3447 {
3448 varnumber_T val;
3449 int error = FALSE;
3450
3451 // tv_get_number_chk() accepts a string, but we don't want that
3452 // here
3453 if (check_not_string(rettv) == FAIL)
3454 return FAIL;
3455 val = tv_get_number_chk(rettv, &error);
3456 clear_tv(rettv);
3457 if (error)
3458 return FAIL;
3459 if (*p == '-')
3460 val = -val;
3461 rettv->v_type = VAR_NUMBER;
3462 rettv->vval.v_number = val;
3463 }
3464 }
3465 else
3466 {
3467 int v = tv2bool(rettv);
3468
3469 // '!' is permissive in the type.
3470 clear_tv(rettv);
3471 rettv->v_type = VAR_BOOL;
3472 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3473 }
3474 }
3475 return OK;
3476}
3477
3478/*
3479 * Recognize v: variables that are constants and set "rettv".
3480 */
3481 static void
3482get_vim_constant(char_u **arg, typval_T *rettv)
3483{
3484 if (STRNCMP(*arg, "v:true", 6) == 0)
3485 {
3486 rettv->v_type = VAR_BOOL;
3487 rettv->vval.v_number = VVAL_TRUE;
3488 *arg += 6;
3489 }
3490 else if (STRNCMP(*arg, "v:false", 7) == 0)
3491 {
3492 rettv->v_type = VAR_BOOL;
3493 rettv->vval.v_number = VVAL_FALSE;
3494 *arg += 7;
3495 }
3496 else if (STRNCMP(*arg, "v:null", 6) == 0)
3497 {
3498 rettv->v_type = VAR_SPECIAL;
3499 rettv->vval.v_number = VVAL_NULL;
3500 *arg += 6;
3501 }
3502 else if (STRNCMP(*arg, "v:none", 6) == 0)
3503 {
3504 rettv->v_type = VAR_SPECIAL;
3505 rettv->vval.v_number = VVAL_NONE;
3506 *arg += 6;
3507 }
3508}
3509
Bram Moolenaar61a89812020-05-07 16:58:17 +02003510 static exptype_T
3511get_compare_type(char_u *p, int *len, int *type_is)
3512{
3513 exptype_T type = EXPR_UNKNOWN;
3514 int i;
3515
3516 switch (p[0])
3517 {
3518 case '=': if (p[1] == '=')
3519 type = EXPR_EQUAL;
3520 else if (p[1] == '~')
3521 type = EXPR_MATCH;
3522 break;
3523 case '!': if (p[1] == '=')
3524 type = EXPR_NEQUAL;
3525 else if (p[1] == '~')
3526 type = EXPR_NOMATCH;
3527 break;
3528 case '>': if (p[1] != '=')
3529 {
3530 type = EXPR_GREATER;
3531 *len = 1;
3532 }
3533 else
3534 type = EXPR_GEQUAL;
3535 break;
3536 case '<': if (p[1] != '=')
3537 {
3538 type = EXPR_SMALLER;
3539 *len = 1;
3540 }
3541 else
3542 type = EXPR_SEQUAL;
3543 break;
3544 case 'i': if (p[1] == 's')
3545 {
3546 // "is" and "isnot"; but not a prefix of a name
3547 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3548 *len = 5;
3549 i = p[*len];
3550 if (!isalnum(i) && i != '_')
3551 {
3552 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3553 *type_is = TRUE;
3554 }
3555 }
3556 break;
3557 }
3558 return type;
3559}
3560
3561/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 * Compile code to apply '-', '+' and '!'.
3563 */
3564 static int
3565compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3566{
3567 char_u *p = end;
3568
3569 // this works from end to start
3570 while (p > start)
3571 {
3572 --p;
3573 if (*p == '-' || *p == '+')
3574 {
3575 int negate = *p == '-';
3576 isn_T *isn;
3577
3578 // TODO: check type
3579 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3580 {
3581 --p;
3582 if (*p == '-')
3583 negate = !negate;
3584 }
3585 // only '-' has an effect, for '+' we only check the type
3586 if (negate)
3587 isn = generate_instr(cctx, ISN_NEGATENR);
3588 else
3589 isn = generate_instr(cctx, ISN_CHECKNR);
3590 if (isn == NULL)
3591 return FAIL;
3592 }
3593 else
3594 {
3595 int invert = TRUE;
3596
3597 while (p > start && p[-1] == '!')
3598 {
3599 --p;
3600 invert = !invert;
3601 }
3602 if (generate_2BOOL(cctx, invert) == FAIL)
3603 return FAIL;
3604 }
3605 }
3606 return OK;
3607}
3608
3609/*
3610 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003611 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 */
3613 static int
3614compile_subscript(
3615 char_u **arg,
3616 cctx_T *cctx,
3617 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003618 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003619 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620{
3621 for (;;)
3622 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003623 char_u *p = skipwhite(*arg);
3624
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003625 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003626 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003627 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003628
3629 // If a following line starts with "->{" or "->X" advance to that
3630 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003631 // Also if a following line starts with ".x".
3632 if (next != NULL &&
3633 ((next[0] == '-' && next[1] == '>'
3634 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3635 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003636 {
3637 next = next_line_from_context(cctx, TRUE);
3638 if (next == NULL)
3639 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003640 *arg = next;
3641 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003642 }
3643 }
3644
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003645 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003647 garray_T *stack = &cctx->ctx_type_stack;
3648 type_T *type;
3649 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003651 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003652 return FAIL;
3653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003655 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3656
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003657 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3659 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003660 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 return FAIL;
3662 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003663 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003665 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003666 return FAIL;
3667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 // something->method()
3669 // Apply the '!', '-' and '+' first:
3670 // -1.0->func() works like (-1.0)->func()
3671 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3672 return FAIL;
3673 *start_leader = end_leader; // don't apply again later
3674
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003675 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003676 *arg = skipwhite(p);
3677 if (may_get_next_line(p, arg, cctx) == FAIL)
3678 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 if (**arg == '{')
3680 {
3681 // lambda call: list->{lambda}
3682 if (compile_lambda_call(arg, cctx) == FAIL)
3683 return FAIL;
3684 }
3685 else
3686 {
3687 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003688 p = *arg;
3689 if (ASCII_ISALPHA(*p) && p[1] == ':')
3690 p += 2;
3691 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 ;
3693 if (*p != '(')
3694 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003695 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 return FAIL;
3697 }
3698 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003699 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 return FAIL;
3701 }
3702 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003703 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003705 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003706 type_T **typep;
3707
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003708 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003709 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003710 // TODO: blob index
3711 // TODO: more arguments
3712 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003713 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003714 return FAIL;
3715
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003716 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003717 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003718 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003719 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003720 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 return FAIL;
3722
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003723 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3724 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 if (**arg != ']')
3726 {
3727 emsg(_(e_missbrac));
3728 return FAIL;
3729 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003730 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003732 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3733 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003734 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003735 if ((*typep)->tt_type == VAR_LIST)
3736 *typep = (*typep)->tt_member;
3737 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3738 return FAIL;
3739 }
3740 else if ((*typep)->tt_type == VAR_DICT)
3741 {
3742 *typep = (*typep)->tt_member;
3743 if (may_generate_2STRING(-1, cctx) == FAIL)
3744 return FAIL;
3745 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3746 return FAIL;
3747 }
3748 else
3749 {
3750 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003751 return FAIL;
3752 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003754 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003756 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003757 return FAIL;
3758
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003759 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003760 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3761 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003763 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 if (eval_isnamec1(*p))
3765 while (eval_isnamec(*p))
3766 MB_PTR_ADV(p);
3767 if (p == *arg)
3768 {
3769 semsg(_(e_syntax_at), *arg);
3770 return FAIL;
3771 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003772 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 return FAIL;
3774 *arg = p;
3775 }
3776 else
3777 break;
3778 }
3779
3780 // TODO - see handle_subscript():
3781 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3782 // Don't do this when "Func" is already a partial that was bound
3783 // explicitly (pt_auto is FALSE).
3784
3785 return OK;
3786}
3787
3788/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003789 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3790 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003792 * If the value is a constant "ppconst->pp_ret" will be set.
3793 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003794 *
3795 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 */
3797
3798/*
3799 * number number constant
3800 * 0zFFFFFFFF Blob constant
3801 * "string" string constant
3802 * 'string' literal string constant
3803 * &option-name option value
3804 * @r register contents
3805 * identifier variable value
3806 * function() function call
3807 * $VAR environment variable
3808 * (expression) nested expression
3809 * [expr, expr] List
3810 * {key: val, key: val} Dictionary
3811 * #{key: val, key: val} Dictionary with literal keys
3812 *
3813 * Also handle:
3814 * ! in front logical NOT
3815 * - in front unary minus
3816 * + in front unary plus (ignored)
3817 * trailing (arg) funcref/partial call
3818 * trailing [] subscript in String or List
3819 * trailing .name entry in Dictionary
3820 * trailing ->name() method call
3821 */
3822 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003823compile_expr7(
3824 char_u **arg,
3825 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003826 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 char_u *start_leader, *end_leader;
3829 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003830 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003831 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832
3833 /*
3834 * Skip '!', '-' and '+' characters. They are handled later.
3835 */
3836 start_leader = *arg;
3837 while (**arg == '!' || **arg == '-' || **arg == '+')
3838 *arg = skipwhite(*arg + 1);
3839 end_leader = *arg;
3840
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003841 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 switch (**arg)
3843 {
3844 /*
3845 * Number constant.
3846 */
3847 case '0': // also for blob starting with 0z
3848 case '1':
3849 case '2':
3850 case '3':
3851 case '4':
3852 case '5':
3853 case '6':
3854 case '7':
3855 case '8':
3856 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003857 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 return FAIL;
3859 break;
3860
3861 /*
3862 * String constant: "string".
3863 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003864 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 return FAIL;
3866 break;
3867
3868 /*
3869 * Literal string constant: 'str''ing'.
3870 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003871 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 return FAIL;
3873 break;
3874
3875 /*
3876 * Constant Vim variable.
3877 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003878 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 ret = NOTDONE;
3880 break;
3881
3882 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003883 * "true" constant
3884 */
3885 case 't': if (STRNCMP(*arg, "true", 4) == 0
3886 && !eval_isnamec((*arg)[4]))
3887 {
3888 *arg += 4;
3889 rettv->v_type = VAR_BOOL;
3890 rettv->vval.v_number = VVAL_TRUE;
3891 }
3892 else
3893 ret = NOTDONE;
3894 break;
3895
3896 /*
3897 * "false" constant
3898 */
3899 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3900 && !eval_isnamec((*arg)[5]))
3901 {
3902 *arg += 5;
3903 rettv->v_type = VAR_BOOL;
3904 rettv->vval.v_number = VVAL_FALSE;
3905 }
3906 else
3907 ret = NOTDONE;
3908 break;
3909
3910 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 * List: [expr, expr]
3912 */
3913 case '[': ret = compile_list(arg, cctx);
3914 break;
3915
3916 /*
3917 * Dictionary: #{key: val, key: val}
3918 */
3919 case '#': if ((*arg)[1] == '{')
3920 {
3921 ++*arg;
3922 ret = compile_dict(arg, cctx, TRUE);
3923 }
3924 else
3925 ret = NOTDONE;
3926 break;
3927
3928 /*
3929 * Lambda: {arg, arg -> expr}
3930 * Dictionary: {'key': val, 'key': val}
3931 */
3932 case '{': {
3933 char_u *start = skipwhite(*arg + 1);
3934
3935 // Find out what comes after the arguments.
3936 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003937 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 if (ret != FAIL && *start == '>')
3939 ret = compile_lambda(arg, cctx);
3940 else
3941 ret = compile_dict(arg, cctx, FALSE);
3942 }
3943 break;
3944
3945 /*
3946 * Option value: &name
3947 */
3948 case '&': ret = compile_get_option(arg, cctx);
3949 break;
3950
3951 /*
3952 * Environment variable: $VAR.
3953 */
3954 case '$': ret = compile_get_env(arg, cctx);
3955 break;
3956
3957 /*
3958 * Register contents: @r.
3959 */
3960 case '@': ret = compile_get_register(arg, cctx);
3961 break;
3962 /*
3963 * nested expression: (expression).
3964 */
3965 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003966
3967 // recursive!
3968 if (ppconst->pp_used <= PPSIZE - 10)
3969 {
3970 ret = compile_expr1(arg, cctx, ppconst);
3971 }
3972 else
3973 {
3974 // Not enough space in ppconst, flush constants.
3975 if (generate_ppconst(cctx, ppconst) == FAIL)
3976 return FAIL;
3977 ret = compile_expr0(arg, cctx);
3978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 *arg = skipwhite(*arg);
3980 if (**arg == ')')
3981 ++*arg;
3982 else if (ret == OK)
3983 {
3984 emsg(_(e_missing_close));
3985 ret = FAIL;
3986 }
3987 break;
3988
3989 default: ret = NOTDONE;
3990 break;
3991 }
3992 if (ret == FAIL)
3993 return FAIL;
3994
Bram Moolenaar1c747212020-05-09 18:28:34 +02003995 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 {
3997 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003998 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004000 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 return FAIL;
4002 }
4003 start_leader = end_leader; // don't apply again below
4004
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004005 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004006 clear_tv(rettv);
4007 else
4008 // A constant expression can possibly be handled compile time,
4009 // return the value instead of generating code.
4010 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 }
4012 else if (ret == NOTDONE)
4013 {
4014 char_u *p;
4015 int r;
4016
4017 if (!eval_isnamec1(**arg))
4018 {
4019 semsg(_("E1015: Name expected: %s"), *arg);
4020 return FAIL;
4021 }
4022
4023 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004024 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004026 {
4027 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4028 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004030 {
4031 if (generate_ppconst(cctx, ppconst) == FAIL)
4032 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 if (r == FAIL)
4036 return FAIL;
4037 }
4038
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004039 // Handle following "[]", ".member", etc.
4040 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004041 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004042 ppconst) == FAIL)
4043 return FAIL;
4044 if (ppconst->pp_used > 0)
4045 {
4046 // apply the '!', '-' and '+' before the constant
4047 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4048 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4049 return FAIL;
4050 return OK;
4051 }
4052 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004054 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055}
4056
4057/*
4058 * * number multiplication
4059 * / number division
4060 * % number modulo
4061 */
4062 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004063compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064{
4065 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004066 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004067 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004069 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004070 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 return FAIL;
4072
4073 /*
4074 * Repeat computing, until no "*", "/" or "%" is following.
4075 */
4076 for (;;)
4077 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004078 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 if (*op != '*' && *op != '/' && *op != '%')
4080 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004081 if (next != NULL)
4082 {
4083 *arg = next_line_from_context(cctx, TRUE);
4084 op = skipwhite(*arg);
4085 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004086
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004087 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 {
4089 char_u buf[3];
4090
4091 vim_strncpy(buf, op, 1);
4092 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004093 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 }
4095 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004096 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004097 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004099 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004100 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004102
4103 if (ppconst->pp_used == ppconst_used + 2
4104 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4105 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004106 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004107 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4108 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004109 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004111 // both are numbers: compute the result
4112 switch (*op)
4113 {
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;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004116 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004117 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004118 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004119 break;
4120 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004121 tv1->vval.v_number = res;
4122 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004123 }
4124 else
4125 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004126 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004127 generate_two_op(cctx, op);
4128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 }
4130
4131 return OK;
4132}
4133
4134/*
4135 * + number addition
4136 * - number subtraction
4137 * .. string concatenation
4138 */
4139 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004140compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141{
4142 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004143 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004145 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146
4147 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004148 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 return FAIL;
4150
4151 /*
4152 * Repeat computing, until no "+", "-" or ".." is following.
4153 */
4154 for (;;)
4155 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004156 op = may_peek_next_line(cctx, *arg, &next);
4157 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 break;
4159 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004160 if (next != NULL)
4161 {
4162 *arg = next_line_from_context(cctx, TRUE);
4163 op = skipwhite(*arg);
4164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004166 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 {
4168 char_u buf[3];
4169
4170 vim_strncpy(buf, op, oplen);
4171 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004172 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 }
4174
4175 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004176 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004177 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004179 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004180 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 return FAIL;
4182
Bram Moolenaara5565e42020-05-09 15:44:01 +02004183 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004184 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004185 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4186 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4187 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4188 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004190 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4191 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004192
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004193 // concat/subtract/add constant numbers
4194 if (*op == '+')
4195 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4196 else if (*op == '-')
4197 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4198 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004199 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004200 // concatenate constant strings
4201 char_u *s1 = tv1->vval.v_string;
4202 char_u *s2 = tv2->vval.v_string;
4203 size_t len1 = STRLEN(s1);
4204
4205 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4206 if (tv1->vval.v_string == NULL)
4207 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004208 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004209 return FAIL;
4210 }
4211 mch_memmove(tv1->vval.v_string, s1, len1);
4212 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004213 vim_free(s1);
4214 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004215 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004216 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 }
4218 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004219 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004220 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004221 if (*op == '.')
4222 {
4223 if (may_generate_2STRING(-2, cctx) == FAIL
4224 || may_generate_2STRING(-1, cctx) == FAIL)
4225 return FAIL;
4226 generate_instr_drop(cctx, ISN_CONCAT, 1);
4227 }
4228 else
4229 generate_two_op(cctx, op);
4230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 }
4232
4233 return OK;
4234}
4235
4236/*
4237 * expr5a == expr5b
4238 * expr5a =~ expr5b
4239 * expr5a != expr5b
4240 * expr5a !~ expr5b
4241 * expr5a > expr5b
4242 * expr5a >= expr5b
4243 * expr5a < expr5b
4244 * expr5a <= expr5b
4245 * expr5a is expr5b
4246 * expr5a isnot expr5b
4247 *
4248 * Produces instructions:
4249 * EVAL expr5a Push result of "expr5a"
4250 * EVAL expr5b Push result of "expr5b"
4251 * COMPARE one of the compare instructions
4252 */
4253 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004254compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255{
4256 exptype_T type = EXPR_UNKNOWN;
4257 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004258 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004261 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262
4263 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004264 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 return FAIL;
4266
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004267 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004268 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269
4270 /*
4271 * If there is a comparative operator, use it.
4272 */
4273 if (type != EXPR_UNKNOWN)
4274 {
4275 int ic = FALSE; // Default: do not ignore case
4276
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004277 if (next != NULL)
4278 {
4279 *arg = next_line_from_context(cctx, TRUE);
4280 p = skipwhite(*arg);
4281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 if (type_is && (p[len] == '?' || p[len] == '#'))
4283 {
4284 semsg(_(e_invexpr2), *arg);
4285 return FAIL;
4286 }
4287 // extra question mark appended: ignore case
4288 if (p[len] == '?')
4289 {
4290 ic = TRUE;
4291 ++len;
4292 }
4293 // extra '#' appended: match case (ignored)
4294 else if (p[len] == '#')
4295 ++len;
4296 // nothing appended: match case
4297
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004298 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 {
4300 char_u buf[7];
4301
4302 vim_strncpy(buf, p, len);
4303 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004304 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 }
4306
4307 // get the second variable
4308 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004309 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004310 return FAIL;
4311
Bram Moolenaara5565e42020-05-09 15:44:01 +02004312 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 return FAIL;
4314
Bram Moolenaara5565e42020-05-09 15:44:01 +02004315 if (ppconst->pp_used == ppconst_used + 2)
4316 {
4317 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4318 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4319 int ret;
4320
4321 // Both sides are a constant, compute the result now.
4322 // First check for a valid combination of types, this is more
4323 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004324 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004325 ret = FAIL;
4326 else
4327 {
4328 ret = typval_compare(tv1, tv2, type, ic);
4329 tv1->v_type = VAR_BOOL;
4330 tv1->vval.v_number = tv1->vval.v_number
4331 ? VVAL_TRUE : VVAL_FALSE;
4332 clear_tv(tv2);
4333 --ppconst->pp_used;
4334 }
4335 return ret;
4336 }
4337
4338 generate_ppconst(cctx, ppconst);
4339 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 }
4341
4342 return OK;
4343}
4344
Bram Moolenaar7f141552020-05-09 17:35:53 +02004345static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4346
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347/*
4348 * Compile || or &&.
4349 */
4350 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004351compile_and_or(
4352 char_u **arg,
4353 cctx_T *cctx,
4354 char *op,
4355 ppconst_T *ppconst,
4356 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004358 char_u *next;
4359 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 int opchar = *op;
4361
4362 if (p[0] == opchar && p[1] == opchar)
4363 {
4364 garray_T *instr = &cctx->ctx_instr;
4365 garray_T end_ga;
4366
4367 /*
4368 * Repeat until there is no following "||" or "&&"
4369 */
4370 ga_init2(&end_ga, sizeof(int), 10);
4371 while (p[0] == opchar && p[1] == opchar)
4372 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004373 if (next != NULL)
4374 {
4375 *arg = next_line_from_context(cctx, TRUE);
4376 p = skipwhite(*arg);
4377 }
4378
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004379 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4380 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004382 return FAIL;
4383 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384
Bram Moolenaara5565e42020-05-09 15:44:01 +02004385 // TODO: use ppconst if the value is a constant
4386 generate_ppconst(cctx, ppconst);
4387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 if (ga_grow(&end_ga, 1) == FAIL)
4389 {
4390 ga_clear(&end_ga);
4391 return FAIL;
4392 }
4393 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4394 ++end_ga.ga_len;
4395 generate_JUMP(cctx, opchar == '|'
4396 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4397
4398 // eval the next expression
4399 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004400 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004401 return FAIL;
4402
Bram Moolenaara5565e42020-05-09 15:44:01 +02004403 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4404 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 {
4406 ga_clear(&end_ga);
4407 return FAIL;
4408 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004409
4410 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004412 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413
4414 // Fill in the end label in all jumps.
4415 while (end_ga.ga_len > 0)
4416 {
4417 isn_T *isn;
4418
4419 --end_ga.ga_len;
4420 isn = ((isn_T *)instr->ga_data)
4421 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4422 isn->isn_arg.jump.jump_where = instr->ga_len;
4423 }
4424 ga_clear(&end_ga);
4425 }
4426
4427 return OK;
4428}
4429
4430/*
4431 * expr4a && expr4a && expr4a logical AND
4432 *
4433 * Produces instructions:
4434 * EVAL expr4a Push result of "expr4a"
4435 * JUMP_AND_KEEP_IF_FALSE end
4436 * EVAL expr4b Push result of "expr4b"
4437 * JUMP_AND_KEEP_IF_FALSE end
4438 * EVAL expr4c Push result of "expr4c"
4439 * end:
4440 */
4441 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004442compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004444 int ppconst_used = ppconst->pp_used;
4445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004447 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 return FAIL;
4449
4450 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004451 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452}
4453
4454/*
4455 * expr3a || expr3b || expr3c logical OR
4456 *
4457 * Produces instructions:
4458 * EVAL expr3a Push result of "expr3a"
4459 * JUMP_AND_KEEP_IF_TRUE end
4460 * EVAL expr3b Push result of "expr3b"
4461 * JUMP_AND_KEEP_IF_TRUE end
4462 * EVAL expr3c Push result of "expr3c"
4463 * end:
4464 */
4465 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004466compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004468 int ppconst_used = ppconst->pp_used;
4469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004471 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 return FAIL;
4473
4474 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004475 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476}
4477
4478/*
4479 * Toplevel expression: expr2 ? expr1a : expr1b
4480 *
4481 * Produces instructions:
4482 * EVAL expr2 Push result of "expr"
4483 * JUMP_IF_FALSE alt jump if false
4484 * EVAL expr1a
4485 * JUMP_ALWAYS end
4486 * alt: EVAL expr1b
4487 * end:
4488 */
4489 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004490compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491{
4492 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004493 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004494 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495
Bram Moolenaar61a89812020-05-07 16:58:17 +02004496 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004497 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 return FAIL;
4499
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004500 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 if (*p == '?')
4502 {
4503 garray_T *instr = &cctx->ctx_instr;
4504 garray_T *stack = &cctx->ctx_type_stack;
4505 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004506 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004508 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004510 int has_const_expr = FALSE;
4511 int const_value = FALSE;
4512 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004514 if (next != NULL)
4515 {
4516 *arg = next_line_from_context(cctx, TRUE);
4517 p = skipwhite(*arg);
4518 }
4519
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004520 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4521 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004523 return FAIL;
4524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525
Bram Moolenaara5565e42020-05-09 15:44:01 +02004526 if (ppconst->pp_used == ppconst_used + 1)
4527 {
4528 // the condition is a constant, we know whether the ? or the :
4529 // expression is to be evaluated.
4530 has_const_expr = TRUE;
4531 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4532 clear_tv(&ppconst->pp_tv[ppconst_used]);
4533 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004534 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4535 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004536 }
4537 else
4538 {
4539 generate_ppconst(cctx, ppconst);
4540 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4541 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542
4543 // evaluate the second expression; any type is accepted
4544 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004545 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004546 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004547 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004548 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549
Bram Moolenaara5565e42020-05-09 15:44:01 +02004550 if (!has_const_expr)
4551 {
4552 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554 // remember the type and drop it
4555 --stack->ga_len;
4556 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557
Bram Moolenaara5565e42020-05-09 15:44:01 +02004558 end_idx = instr->ga_len;
4559 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4560
4561 // jump here from JUMP_IF_FALSE
4562 isn = ((isn_T *)instr->ga_data) + alt_idx;
4563 isn->isn_arg.jump.jump_where = instr->ga_len;
4564 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565
4566 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004567 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 if (*p != ':')
4569 {
4570 emsg(_(e_missing_colon));
4571 return FAIL;
4572 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004573 if (next != NULL)
4574 {
4575 *arg = next_line_from_context(cctx, TRUE);
4576 p = skipwhite(*arg);
4577 }
4578
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004579 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4580 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004582 return FAIL;
4583 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584
4585 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004586 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004587 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4588 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004590 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004591 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004592 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004593 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594
Bram Moolenaara5565e42020-05-09 15:44:01 +02004595 if (!has_const_expr)
4596 {
4597 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598
Bram Moolenaara5565e42020-05-09 15:44:01 +02004599 // If the types differ, the result has a more generic type.
4600 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4601 common_type(type1, type2, &type2, cctx->ctx_type_list);
4602
4603 // jump here from JUMP_ALWAYS
4604 isn = ((isn_T *)instr->ga_data) + end_idx;
4605 isn->isn_arg.jump.jump_where = instr->ga_len;
4606 }
4607
4608 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 }
4610 return OK;
4611}
4612
4613/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004614 * Toplevel expression.
4615 */
4616 static int
4617compile_expr0(char_u **arg, cctx_T *cctx)
4618{
4619 ppconst_T ppconst;
4620
4621 CLEAR_FIELD(ppconst);
4622 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4623 {
4624 clear_ppconst(&ppconst);
4625 return FAIL;
4626 }
4627 if (generate_ppconst(cctx, &ppconst) == FAIL)
4628 return FAIL;
4629 return OK;
4630}
4631
4632/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 * compile "return [expr]"
4634 */
4635 static char_u *
4636compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4637{
4638 char_u *p = arg;
4639 garray_T *stack = &cctx->ctx_type_stack;
4640 type_T *stack_type;
4641
4642 if (*p != NUL && *p != '|' && *p != '\n')
4643 {
4644 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004645 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 return NULL;
4647
4648 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4649 if (set_return_type)
4650 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004651 else
4652 {
4653 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4654 && stack_type->tt_type != VAR_VOID
4655 && stack_type->tt_type != VAR_UNKNOWN)
4656 {
4657 emsg(_("E1096: Returning a value in a function without a return type"));
4658 return NULL;
4659 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004660 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4661 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004663 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 }
4665 else
4666 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004667 // "set_return_type" cannot be TRUE, only used for a lambda which
4668 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004669 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4670 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 {
4672 emsg(_("E1003: Missing return value"));
4673 return NULL;
4674 }
4675
4676 // No argument, return zero.
4677 generate_PUSHNR(cctx, 0);
4678 }
4679
4680 if (generate_instr(cctx, ISN_RETURN) == NULL)
4681 return NULL;
4682
4683 // "return val | endif" is possible
4684 return skipwhite(p);
4685}
4686
4687/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004688 * Get a line from the compilation context, compatible with exarg_T getline().
4689 * Return a pointer to the line in allocated memory.
4690 * Return NULL for end-of-file or some error.
4691 */
4692 static char_u *
4693exarg_getline(
4694 int c UNUSED,
4695 void *cookie,
4696 int indent UNUSED,
4697 int do_concat UNUSED)
4698{
4699 cctx_T *cctx = (cctx_T *)cookie;
4700
4701 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4702 {
4703 iemsg("Heredoc got to end");
4704 return NULL;
4705 }
4706 ++cctx->ctx_lnum;
4707 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4708 [cctx->ctx_lnum]);
4709}
4710
4711/*
4712 * Compile a nested :def command.
4713 */
4714 static char_u *
4715compile_nested_function(exarg_T *eap, cctx_T *cctx)
4716{
4717 char_u *name_start = eap->arg;
4718 char_u *name_end = to_name_end(eap->arg, FALSE);
4719 char_u *name = get_lambda_name();
4720 lvar_T *lvar;
4721 ufunc_T *ufunc;
4722
4723 eap->arg = name_end;
4724 eap->getline = exarg_getline;
4725 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004726 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004727 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004728 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004729
Bram Moolenaar822ba242020-05-24 23:00:18 +02004730 if (ufunc == NULL)
4731 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004732 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004733 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004734 return NULL;
4735
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004736 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004737 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004738 TRUE, ufunc->uf_func_type);
4739
4740 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4741 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4742 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004743
Bram Moolenaar61a89812020-05-07 16:58:17 +02004744 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004745 return (char_u *)"";
4746}
4747
4748/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 * Return the length of an assignment operator, or zero if there isn't one.
4750 */
4751 int
4752assignment_len(char_u *p, int *heredoc)
4753{
4754 if (*p == '=')
4755 {
4756 if (p[1] == '<' && p[2] == '<')
4757 {
4758 *heredoc = TRUE;
4759 return 3;
4760 }
4761 return 1;
4762 }
4763 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4764 return 2;
4765 if (STRNCMP(p, "..=", 3) == 0)
4766 return 3;
4767 return 0;
4768}
4769
4770// words that cannot be used as a variable
4771static char *reserved[] = {
4772 "true",
4773 "false",
4774 NULL
4775};
4776
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004777typedef enum {
4778 dest_local,
4779 dest_option,
4780 dest_env,
4781 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004782 dest_buffer,
4783 dest_window,
4784 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004785 dest_vimvar,
4786 dest_script,
4787 dest_reg,
4788} assign_dest_T;
4789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004791 * Generate the load instruction for "name".
4792 */
4793 static void
4794generate_loadvar(
4795 cctx_T *cctx,
4796 assign_dest_T dest,
4797 char_u *name,
4798 lvar_T *lvar,
4799 type_T *type)
4800{
4801 switch (dest)
4802 {
4803 case dest_option:
4804 // TODO: check the option exists
4805 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4806 break;
4807 case dest_global:
4808 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4809 break;
4810 case dest_buffer:
4811 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4812 break;
4813 case dest_window:
4814 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4815 break;
4816 case dest_tab:
4817 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4818 break;
4819 case dest_script:
4820 compile_load_scriptvar(cctx,
4821 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4822 break;
4823 case dest_env:
4824 // Include $ in the name here
4825 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4826 break;
4827 case dest_reg:
4828 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4829 break;
4830 case dest_vimvar:
4831 generate_LOADV(cctx, name + 2, TRUE);
4832 break;
4833 case dest_local:
4834 if (lvar->lv_from_outer)
4835 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4836 NULL, type);
4837 else
4838 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4839 break;
4840 }
4841}
4842
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004843 void
4844vim9_declare_error(char_u *name)
4845{
4846 char *scope = "";
4847
4848 switch (*name)
4849 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004850 case 'g': scope = _("global"); break;
4851 case 'b': scope = _("buffer"); break;
4852 case 'w': scope = _("window"); break;
4853 case 't': scope = _("tab"); break;
4854 case 'v': scope = "v:"; break;
4855 case '$': semsg(_(e_declare_env_var), name); return;
4856 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004857 }
4858 semsg(_(e_declare_var), scope, name);
4859}
4860
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004861/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004862 * Compile declaration and assignment:
4863 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004865 * Return NULL for an error.
4866 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 */
4868 static char_u *
4869compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4870{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004871 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004873 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 char_u *ret = NULL;
4875 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004876 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004879 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 int oplen = 0;
4882 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004883 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004884 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004885 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004889 // Skip over the "var" or "[var, var]" to get to any "=".
4890 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4891 if (p == NULL)
4892 return *arg == '[' ? arg : NULL;
4893
4894 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004896 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 return NULL;
4898 }
4899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 sp = p;
4901 p = skipwhite(p);
4902 op = p;
4903 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004904
4905 if (var_count > 0 && oplen == 0)
4906 // can be something like "[1, 2]->func()"
4907 return arg;
4908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4910 {
4911 char_u buf[4];
4912
4913 vim_strncpy(buf, op, oplen);
4914 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004915 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004916 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 if (heredoc)
4919 {
4920 list_T *l;
4921 listitem_T *li;
4922
4923 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004924 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004926 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004927
4928 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004929 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 {
4931 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4932 li->li_tv.vval.v_string = NULL;
4933 }
4934 generate_NEWLIST(cctx, l->lv_len);
4935 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004936 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 list_free(l);
4938 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004939 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004941 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004943 // for "[var, var] = expr" evaluate the expression here, loop over the
4944 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004945
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004946 p = skipwhite(op + oplen);
4947 if (compile_expr0(&p, cctx) == FAIL)
4948 return NULL;
4949 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004951 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004953 type_T *stacktype;
4954
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004955 stacktype = stack->ga_len == 0 ? &t_void
4956 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004957 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004959 emsg(_(e_cannot_use_void));
4960 goto theend;
4961 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004962 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004963 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004964 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004965 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4966 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004967 }
4968 }
4969
4970 /*
4971 * Loop over variables in "[var, var] = expr".
4972 * For "var = expr" and "let var: type" this is done only once.
4973 */
4974 if (var_count > 0)
4975 var_start = skipwhite(arg + 1); // skip over the "["
4976 else
4977 var_start = arg;
4978 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4979 {
4980 char_u *var_end = skip_var_one(var_start, FALSE);
4981 size_t varlen;
4982 int new_local = FALSE;
4983 int opt_type;
4984 int opt_flags = 0;
4985 assign_dest_T dest = dest_local;
4986 int vimvaridx = -1;
4987 lvar_T *lvar = NULL;
4988 lvar_T arg_lvar;
4989 int has_type = FALSE;
4990 int has_index = FALSE;
4991 int instr_count = -1;
4992
4993 p = (*var_start == '&' || *var_start == '$'
4994 || *var_start == '@') ? var_start + 1 : var_start;
4995 p = to_name_end(p, TRUE);
4996
4997 // "a: type" is declaring variable "a" with a type, not "a:".
4998 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4999 --var_end;
5000 if (is_decl && p == var_start + 2 && p[-1] == ':')
5001 --p;
5002
5003 varlen = p - var_start;
5004 vim_free(name);
5005 name = vim_strnsave(var_start, varlen);
5006 if (name == NULL)
5007 return NULL;
5008 if (!heredoc)
5009 type = &t_any;
5010
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005011 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005012 {
5013 if (*var_start == '&')
5014 {
5015 int cc;
5016 long numval;
5017
5018 dest = dest_option;
5019 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005021 emsg(_(e_const_option));
5022 goto theend;
5023 }
5024 if (is_decl)
5025 {
5026 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5027 goto theend;
5028 }
5029 p = var_start;
5030 p = find_option_end(&p, &opt_flags);
5031 if (p == NULL)
5032 {
5033 // cannot happen?
5034 emsg(_(e_letunexp));
5035 goto theend;
5036 }
5037 cc = *p;
5038 *p = NUL;
5039 opt_type = get_option_value(var_start + 1, &numval,
5040 NULL, opt_flags);
5041 *p = cc;
5042 if (opt_type == -3)
5043 {
5044 semsg(_(e_unknown_option), var_start);
5045 goto theend;
5046 }
5047 if (opt_type == -2 || opt_type == 0)
5048 type = &t_string;
5049 else
5050 type = &t_number; // both number and boolean option
5051 }
5052 else if (*var_start == '$')
5053 {
5054 dest = dest_env;
5055 type = &t_string;
5056 if (is_decl)
5057 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005058 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059 goto theend;
5060 }
5061 }
5062 else if (*var_start == '@')
5063 {
5064 if (!valid_yank_reg(var_start[1], TRUE))
5065 {
5066 emsg_invreg(var_start[1]);
5067 goto theend;
5068 }
5069 dest = dest_reg;
5070 type = &t_string;
5071 if (is_decl)
5072 {
5073 semsg(_("E1066: Cannot declare a register: %s"), name);
5074 goto theend;
5075 }
5076 }
5077 else if (STRNCMP(var_start, "g:", 2) == 0)
5078 {
5079 dest = dest_global;
5080 if (is_decl)
5081 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005082 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005083 goto theend;
5084 }
5085 }
5086 else if (STRNCMP(var_start, "b:", 2) == 0)
5087 {
5088 dest = dest_buffer;
5089 if (is_decl)
5090 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005091 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005092 goto theend;
5093 }
5094 }
5095 else if (STRNCMP(var_start, "w:", 2) == 0)
5096 {
5097 dest = dest_window;
5098 if (is_decl)
5099 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005100 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005101 goto theend;
5102 }
5103 }
5104 else if (STRNCMP(var_start, "t:", 2) == 0)
5105 {
5106 dest = dest_tab;
5107 if (is_decl)
5108 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005109 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005110 goto theend;
5111 }
5112 }
5113 else if (STRNCMP(var_start, "v:", 2) == 0)
5114 {
5115 typval_T *vtv;
5116 int di_flags;
5117
5118 vimvaridx = find_vim_var(name + 2, &di_flags);
5119 if (vimvaridx < 0)
5120 {
5121 semsg(_(e_var_notfound), var_start);
5122 goto theend;
5123 }
5124 // We use the current value of "sandbox" here, is that OK?
5125 if (var_check_ro(di_flags, name, FALSE))
5126 goto theend;
5127 dest = dest_vimvar;
5128 vtv = get_vim_var_tv(vimvaridx);
5129 type = typval2type(vtv);
5130 if (is_decl)
5131 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005132 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005133 goto theend;
5134 }
5135 }
5136 else
5137 {
5138 int idx;
5139
5140 for (idx = 0; reserved[idx] != NULL; ++idx)
5141 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005142 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005143 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005144 goto theend;
5145 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005146
5147 lvar = lookup_local(var_start, varlen, cctx);
5148 if (lvar == NULL)
5149 {
5150 CLEAR_FIELD(arg_lvar);
5151 if (lookup_arg(var_start, varlen,
5152 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5153 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005154 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005155 if (is_decl)
5156 {
5157 semsg(_(e_used_as_arg), name);
5158 goto theend;
5159 }
5160 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005161 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005162 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005163 if (lvar != NULL)
5164 {
5165 if (is_decl)
5166 {
5167 semsg(_("E1017: Variable already declared: %s"), name);
5168 goto theend;
5169 }
5170 else if (lvar->lv_const)
5171 {
5172 semsg(_("E1018: Cannot assign to a constant: %s"),
5173 name);
5174 goto theend;
5175 }
5176 }
5177 else if (STRNCMP(var_start, "s:", 2) == 0
5178 || lookup_script(var_start, varlen) == OK
5179 || find_imported(var_start, varlen, cctx) != NULL)
5180 {
5181 dest = dest_script;
5182 if (is_decl)
5183 {
5184 semsg(_("E1054: Variable already declared in the script: %s"),
5185 name);
5186 goto theend;
5187 }
5188 }
5189 else if (name[1] == ':' && name[2] != NUL)
5190 {
5191 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5192 name);
5193 goto theend;
5194 }
5195 else if (!is_decl)
5196 {
5197 semsg(_("E1089: unknown variable: %s"), name);
5198 goto theend;
5199 }
5200 }
5201 }
5202
5203 // handle "a:name" as a name, not index "name" on "a"
5204 if (varlen > 1 || var_start[varlen] != ':')
5205 p = var_end;
5206
5207 if (dest != dest_option)
5208 {
5209 if (is_decl && *p == ':')
5210 {
5211 // parse optional type: "let var: type = expr"
5212 if (!VIM_ISWHITE(p[1]))
5213 {
5214 semsg(_(e_white_after), ":");
5215 goto theend;
5216 }
5217 p = skipwhite(p + 1);
5218 type = parse_type(&p, cctx->ctx_type_list);
5219 has_type = TRUE;
5220 }
5221 else if (lvar != NULL)
5222 type = lvar->lv_type;
5223 }
5224
5225 if (oplen == 3 && !heredoc && dest != dest_global
5226 && type->tt_type != VAR_STRING
5227 && type->tt_type != VAR_ANY)
5228 {
5229 emsg(_("E1019: Can only concatenate to string"));
5230 goto theend;
5231 }
5232
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005233 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005234 {
5235 if (oplen > 1 && !heredoc)
5236 {
5237 // +=, /=, etc. require an existing variable
5238 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5239 name);
5240 goto theend;
5241 }
5242
5243 // new local variable
5244 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5245 goto theend;
5246 lvar = reserve_local(cctx, var_start, varlen,
5247 cmdidx == CMD_const, type);
5248 if (lvar == NULL)
5249 goto theend;
5250 new_local = TRUE;
5251 }
5252
5253 member_type = type;
5254 if (var_end > var_start + varlen)
5255 {
5256 // Something follows after the variable: "var[idx]".
5257 if (is_decl)
5258 {
5259 emsg(_("E1087: cannot use an index when declaring a variable"));
5260 goto theend;
5261 }
5262
5263 if (var_start[varlen] == '[')
5264 {
5265 has_index = TRUE;
5266 if (type->tt_member == NULL)
5267 {
5268 semsg(_("E1088: cannot use an index on %s"), name);
5269 goto theend;
5270 }
5271 member_type = type->tt_member;
5272 }
5273 else
5274 {
5275 semsg("Not supported yet: %s", var_start);
5276 goto theend;
5277 }
5278 }
5279 else if (lvar == &arg_lvar)
5280 {
5281 semsg(_("E1090: Cannot assign to argument %s"), name);
5282 goto theend;
5283 }
5284
5285 if (!heredoc)
5286 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005287 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005289 if (oplen > 0 && var_count == 0)
5290 {
5291 // skip over the "=" and the expression
5292 p = skipwhite(op + oplen);
5293 compile_expr0(&p, cctx);
5294 }
5295 }
5296 else if (oplen > 0)
5297 {
5298 type_T *stacktype;
5299
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005300 // For "var = expr" evaluate the expression.
5301 if (var_count == 0)
5302 {
5303 int r;
5304
5305 // for "+=", "*=", "..=" etc. first load the current value
5306 if (*op != '=')
5307 {
5308 generate_loadvar(cctx, dest, name, lvar, type);
5309
5310 if (has_index)
5311 {
5312 // TODO: get member from list or dict
5313 emsg("Index with operation not supported yet");
5314 goto theend;
5315 }
5316 }
5317
5318 // Compile the expression. Temporarily hide the new local
5319 // variable here, it is not available to this expression.
5320 if (new_local)
5321 --cctx->ctx_locals.ga_len;
5322 instr_count = instr->ga_len;
5323 p = skipwhite(op + oplen);
5324 r = compile_expr0(&p, cctx);
5325 if (new_local)
5326 ++cctx->ctx_locals.ga_len;
5327 if (r == FAIL)
5328 goto theend;
5329 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005330 else if (semicolon && var_idx == var_count - 1)
5331 {
5332 // For "[var; var] = expr" get the rest of the list
5333 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5334 goto theend;
5335 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005336 else
5337 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005338 // For "[var, var] = expr" get the "var_idx" item from the
5339 // list.
5340 if (generate_GETITEM(cctx, var_idx) == FAIL)
5341 return FAIL;
5342 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005343
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005344 stacktype = stack->ga_len == 0 ? &t_void
5345 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5346 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005347 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005348 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005349 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005350 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005351 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005352 emsg(_(e_cannot_use_void));
5353 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005354 }
5355 else
5356 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005357 // An empty list or dict has a &t_void member,
5358 // for a variable that implies &t_any.
5359 if (stacktype == &t_list_empty)
5360 lvar->lv_type = &t_list_any;
5361 else if (stacktype == &t_dict_empty)
5362 lvar->lv_type = &t_dict_any;
5363 else
5364 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005365 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005366 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005367 else
5368 {
5369 type_T *use_type = lvar->lv_type;
5370
5371 if (has_index)
5372 {
5373 use_type = use_type->tt_member;
5374 if (use_type == NULL)
5375 use_type = &t_void;
5376 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005377 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005378 == FAIL)
5379 goto theend;
5380 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005381 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005382 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005383 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005384 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005385 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005386 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005388 emsg(_(e_const_req_value));
5389 goto theend;
5390 }
5391 else if (!has_type || dest == dest_option)
5392 {
5393 emsg(_(e_type_req));
5394 goto theend;
5395 }
5396 else
5397 {
5398 // variables are always initialized
5399 if (ga_grow(instr, 1) == FAIL)
5400 goto theend;
5401 switch (member_type->tt_type)
5402 {
5403 case VAR_BOOL:
5404 generate_PUSHBOOL(cctx, VVAL_FALSE);
5405 break;
5406 case VAR_FLOAT:
5407#ifdef FEAT_FLOAT
5408 generate_PUSHF(cctx, 0.0);
5409#endif
5410 break;
5411 case VAR_STRING:
5412 generate_PUSHS(cctx, NULL);
5413 break;
5414 case VAR_BLOB:
5415 generate_PUSHBLOB(cctx, NULL);
5416 break;
5417 case VAR_FUNC:
5418 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5419 break;
5420 case VAR_LIST:
5421 generate_NEWLIST(cctx, 0);
5422 break;
5423 case VAR_DICT:
5424 generate_NEWDICT(cctx, 0);
5425 break;
5426 case VAR_JOB:
5427 generate_PUSHJOB(cctx, NULL);
5428 break;
5429 case VAR_CHANNEL:
5430 generate_PUSHCHANNEL(cctx, NULL);
5431 break;
5432 case VAR_NUMBER:
5433 case VAR_UNKNOWN:
5434 case VAR_ANY:
5435 case VAR_PARTIAL:
5436 case VAR_VOID:
5437 case VAR_SPECIAL: // cannot happen
5438 generate_PUSHNR(cctx, 0);
5439 break;
5440 }
5441 }
5442 if (var_count == 0)
5443 end = p;
5444 }
5445
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005446 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005447 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005448 break;
5449
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005450 if (oplen > 0 && *op != '=')
5451 {
5452 type_T *expected = &t_number;
5453 type_T *stacktype;
5454
5455 // TODO: if type is known use float or any operation
5456
5457 if (*op == '.')
5458 expected = &t_string;
5459 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005460 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005461 goto theend;
5462
5463 if (*op == '.')
5464 generate_instr_drop(cctx, ISN_CONCAT, 1);
5465 else
5466 {
5467 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5468
5469 if (isn == NULL)
5470 goto theend;
5471 switch (*op)
5472 {
5473 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5474 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5475 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5476 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5477 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005479 }
5480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005481
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005482 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005483 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005484 int r;
5485
5486 // Compile the "idx" in "var[idx]".
5487 if (new_local)
5488 --cctx->ctx_locals.ga_len;
5489 p = skipwhite(var_start + varlen + 1);
5490 r = compile_expr0(&p, cctx);
5491 if (new_local)
5492 ++cctx->ctx_locals.ga_len;
5493 if (r == FAIL)
5494 goto theend;
5495 if (*skipwhite(p) != ']')
5496 {
5497 emsg(_(e_missbrac));
5498 goto theend;
5499 }
5500 if (type->tt_type == VAR_DICT
5501 && may_generate_2STRING(-1, cctx) == FAIL)
5502 goto theend;
5503 if (type->tt_type == VAR_LIST
5504 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005505 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005506 {
5507 emsg(_(e_number_exp));
5508 goto theend;
5509 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005510
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005511 // Load the dict or list. On the stack we then have:
5512 // - value
5513 // - index
5514 // - variable
5515 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005516
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005517 if (type->tt_type == VAR_LIST)
5518 {
5519 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5520 return FAIL;
5521 }
5522 else if (type->tt_type == VAR_DICT)
5523 {
5524 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5525 return FAIL;
5526 }
5527 else
5528 {
5529 emsg(_(e_listreq));
5530 goto theend;
5531 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005532 }
5533 else
5534 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005535 switch (dest)
5536 {
5537 case dest_option:
5538 generate_STOREOPT(cctx, name + 1, opt_flags);
5539 break;
5540 case dest_global:
5541 // include g: with the name, easier to execute that way
5542 generate_STORE(cctx, ISN_STOREG, 0, name);
5543 break;
5544 case dest_buffer:
5545 // include b: with the name, easier to execute that way
5546 generate_STORE(cctx, ISN_STOREB, 0, name);
5547 break;
5548 case dest_window:
5549 // include w: with the name, easier to execute that way
5550 generate_STORE(cctx, ISN_STOREW, 0, name);
5551 break;
5552 case dest_tab:
5553 // include t: with the name, easier to execute that way
5554 generate_STORE(cctx, ISN_STORET, 0, name);
5555 break;
5556 case dest_env:
5557 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5558 break;
5559 case dest_reg:
5560 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5561 break;
5562 case dest_vimvar:
5563 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5564 break;
5565 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005566 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005567 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5568 imported_T *import = NULL;
5569 int sid = current_sctx.sc_sid;
5570 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005571
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005572 if (name[1] != ':')
5573 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005574 import = find_imported(name, 0, cctx);
5575 if (import != NULL)
5576 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005577 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005578
5579 idx = get_script_item_idx(sid, rawname, TRUE);
5580 // TODO: specific type
5581 if (idx < 0)
5582 {
5583 char_u *name_s = name;
5584
5585 // Include s: in the name for store_var()
5586 if (name[1] != ':')
5587 {
5588 int len = (int)STRLEN(name) + 3;
5589
5590 name_s = alloc(len);
5591 if (name_s == NULL)
5592 name_s = name;
5593 else
5594 vim_snprintf((char *)name_s, len,
5595 "s:%s", name);
5596 }
5597 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005598 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005599 if (name_s != name)
5600 vim_free(name_s);
5601 }
5602 else
5603 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005604 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005605 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005606 break;
5607 case dest_local:
5608 if (lvar != NULL)
5609 {
5610 isn_T *isn = ((isn_T *)instr->ga_data)
5611 + instr->ga_len - 1;
5612
5613 // optimization: turn "var = 123" from ISN_PUSHNR +
5614 // ISN_STORE into ISN_STORENR
5615 if (!lvar->lv_from_outer
5616 && instr->ga_len == instr_count + 1
5617 && isn->isn_type == ISN_PUSHNR)
5618 {
5619 varnumber_T val = isn->isn_arg.number;
5620
5621 isn->isn_type = ISN_STORENR;
5622 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5623 isn->isn_arg.storenr.stnr_val = val;
5624 if (stack->ga_len > 0)
5625 --stack->ga_len;
5626 }
5627 else if (lvar->lv_from_outer)
5628 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005629 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005630 else
5631 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5632 }
5633 break;
5634 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005635 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005636
5637 if (var_idx + 1 < var_count)
5638 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005639 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005640
5641 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005642 if (var_count > 0 && !semicolon)
5643 {
5644 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5645 goto theend;
5646 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005647
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005648 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005649
5650theend:
5651 vim_free(name);
5652 return ret;
5653}
5654
5655/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005656 * Check if "name" can be "unlet".
5657 */
5658 int
5659check_vim9_unlet(char_u *name)
5660{
5661 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5662 {
5663 semsg(_("E1081: Cannot unlet %s"), name);
5664 return FAIL;
5665 }
5666 return OK;
5667}
5668
5669/*
5670 * Callback passed to ex_unletlock().
5671 */
5672 static int
5673compile_unlet(
5674 lval_T *lvp,
5675 char_u *name_end,
5676 exarg_T *eap,
5677 int deep UNUSED,
5678 void *coookie)
5679{
5680 cctx_T *cctx = coookie;
5681
5682 if (lvp->ll_tv == NULL)
5683 {
5684 char_u *p = lvp->ll_name;
5685 int cc = *name_end;
5686 int ret = OK;
5687
5688 // Normal name. Only supports g:, w:, t: and b: namespaces.
5689 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005690 if (*p == '$')
5691 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5692 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005693 ret = FAIL;
5694 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005695 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005696
5697 *name_end = cc;
5698 return ret;
5699 }
5700
5701 // TODO: unlet {list}[idx]
5702 // TODO: unlet {dict}[key]
5703 emsg("Sorry, :unlet not fully implemented yet");
5704 return FAIL;
5705}
5706
5707/*
5708 * compile "unlet var", "lock var" and "unlock var"
5709 * "arg" points to "var".
5710 */
5711 static char_u *
5712compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5713{
5714 char_u *p = arg;
5715
5716 if (eap->cmdidx != CMD_unlet)
5717 {
5718 emsg("Sorry, :lock and unlock not implemented yet");
5719 return NULL;
5720 }
5721
5722 if (*p == '!')
5723 {
5724 p = skipwhite(p + 1);
5725 eap->forceit = TRUE;
5726 }
5727
5728 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5729 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5730}
5731
5732/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005733 * Compile an :import command.
5734 */
5735 static char_u *
5736compile_import(char_u *arg, cctx_T *cctx)
5737{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005738 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005739}
5740
5741/*
5742 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5743 */
5744 static int
5745compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5746{
5747 garray_T *instr = &cctx->ctx_instr;
5748 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5749
5750 if (endlabel == NULL)
5751 return FAIL;
5752 endlabel->el_next = *el;
5753 *el = endlabel;
5754 endlabel->el_end_label = instr->ga_len;
5755
5756 generate_JUMP(cctx, when, 0);
5757 return OK;
5758}
5759
5760 static void
5761compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5762{
5763 garray_T *instr = &cctx->ctx_instr;
5764
5765 while (*el != NULL)
5766 {
5767 endlabel_T *cur = (*el);
5768 isn_T *isn;
5769
5770 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5771 isn->isn_arg.jump.jump_where = instr->ga_len;
5772 *el = cur->el_next;
5773 vim_free(cur);
5774 }
5775}
5776
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005777 static void
5778compile_free_jump_to_end(endlabel_T **el)
5779{
5780 while (*el != NULL)
5781 {
5782 endlabel_T *cur = (*el);
5783
5784 *el = cur->el_next;
5785 vim_free(cur);
5786 }
5787}
5788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005789/*
5790 * Create a new scope and set up the generic items.
5791 */
5792 static scope_T *
5793new_scope(cctx_T *cctx, scopetype_T type)
5794{
5795 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5796
5797 if (scope == NULL)
5798 return NULL;
5799 scope->se_outer = cctx->ctx_scope;
5800 cctx->ctx_scope = scope;
5801 scope->se_type = type;
5802 scope->se_local_count = cctx->ctx_locals.ga_len;
5803 return scope;
5804}
5805
5806/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005807 * Free the current scope and go back to the outer scope.
5808 */
5809 static void
5810drop_scope(cctx_T *cctx)
5811{
5812 scope_T *scope = cctx->ctx_scope;
5813
5814 if (scope == NULL)
5815 {
5816 iemsg("calling drop_scope() without a scope");
5817 return;
5818 }
5819 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005820 switch (scope->se_type)
5821 {
5822 case IF_SCOPE:
5823 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5824 case FOR_SCOPE:
5825 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5826 case WHILE_SCOPE:
5827 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5828 case TRY_SCOPE:
5829 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5830 case NO_SCOPE:
5831 case BLOCK_SCOPE:
5832 break;
5833 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005834 vim_free(scope);
5835}
5836
5837/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005838 * compile "if expr"
5839 *
5840 * "if expr" Produces instructions:
5841 * EVAL expr Push result of "expr"
5842 * JUMP_IF_FALSE end
5843 * ... body ...
5844 * end:
5845 *
5846 * "if expr | else" Produces instructions:
5847 * EVAL expr Push result of "expr"
5848 * JUMP_IF_FALSE else
5849 * ... body ...
5850 * JUMP_ALWAYS end
5851 * else:
5852 * ... body ...
5853 * end:
5854 *
5855 * "if expr1 | elseif expr2 | else" Produces instructions:
5856 * EVAL expr Push result of "expr"
5857 * JUMP_IF_FALSE elseif
5858 * ... body ...
5859 * JUMP_ALWAYS end
5860 * elseif:
5861 * EVAL expr Push result of "expr"
5862 * JUMP_IF_FALSE else
5863 * ... body ...
5864 * JUMP_ALWAYS end
5865 * else:
5866 * ... body ...
5867 * end:
5868 */
5869 static char_u *
5870compile_if(char_u *arg, cctx_T *cctx)
5871{
5872 char_u *p = arg;
5873 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005874 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005875 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005876 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005877 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005878
Bram Moolenaara5565e42020-05-09 15:44:01 +02005879 CLEAR_FIELD(ppconst);
5880 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005881 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005882 clear_ppconst(&ppconst);
5883 return NULL;
5884 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005885 if (cctx->ctx_skip == SKIP_YES)
5886 clear_ppconst(&ppconst);
5887 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005888 {
5889 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005890 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005891 clear_ppconst(&ppconst);
5892 }
5893 else
5894 {
5895 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005896 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005897 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005898 return NULL;
5899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005900
5901 scope = new_scope(cctx, IF_SCOPE);
5902 if (scope == NULL)
5903 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005904 scope->se_skip_save = skip_save;
5905 // "is_had_return" will be reset if any block does not end in :return
5906 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005907
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005908 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005909 {
5910 // "where" is set when ":elseif", "else" or ":endif" is found
5911 scope->se_u.se_if.is_if_label = instr->ga_len;
5912 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5913 }
5914 else
5915 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916
5917 return p;
5918}
5919
5920 static char_u *
5921compile_elseif(char_u *arg, cctx_T *cctx)
5922{
5923 char_u *p = arg;
5924 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005925 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005926 isn_T *isn;
5927 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005928 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005929
5930 if (scope == NULL || scope->se_type != IF_SCOPE)
5931 {
5932 emsg(_(e_elseif_without_if));
5933 return NULL;
5934 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005935 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005936 if (!cctx->ctx_had_return)
5937 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005938
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005939 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005940 {
5941 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005943 return NULL;
5944 // previous "if" or "elseif" jumps here
5945 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5946 isn->isn_arg.jump.jump_where = instr->ga_len;
5947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005948
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005949 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005950 CLEAR_FIELD(ppconst);
5951 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005952 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005953 clear_ppconst(&ppconst);
5954 return NULL;
5955 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005956 if (scope->se_skip_save == SKIP_YES)
5957 clear_ppconst(&ppconst);
5958 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005959 {
5960 // The expression results in a constant.
5961 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005962 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005963 clear_ppconst(&ppconst);
5964 scope->se_u.se_if.is_if_label = -1;
5965 }
5966 else
5967 {
5968 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005969 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005970 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005971 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005972
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005973 // "where" is set when ":elseif", "else" or ":endif" is found
5974 scope->se_u.se_if.is_if_label = instr->ga_len;
5975 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5976 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977
5978 return p;
5979}
5980
5981 static char_u *
5982compile_else(char_u *arg, cctx_T *cctx)
5983{
5984 char_u *p = arg;
5985 garray_T *instr = &cctx->ctx_instr;
5986 isn_T *isn;
5987 scope_T *scope = cctx->ctx_scope;
5988
5989 if (scope == NULL || scope->se_type != IF_SCOPE)
5990 {
5991 emsg(_(e_else_without_if));
5992 return NULL;
5993 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005994 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005995 if (!cctx->ctx_had_return)
5996 scope->se_u.se_if.is_had_return = FALSE;
5997 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998
Bram Moolenaarefd88552020-06-18 20:50:10 +02005999 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006000 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006001 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006002 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006003 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006004 if (!cctx->ctx_had_return
6005 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6006 JUMP_ALWAYS, cctx) == FAIL)
6007 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006008 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006009
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006010 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006011 {
6012 if (scope->se_u.se_if.is_if_label >= 0)
6013 {
6014 // previous "if" or "elseif" jumps here
6015 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6016 isn->isn_arg.jump.jump_where = instr->ga_len;
6017 scope->se_u.se_if.is_if_label = -1;
6018 }
6019 }
6020
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006021 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006022 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024
6025 return p;
6026}
6027
6028 static char_u *
6029compile_endif(char_u *arg, cctx_T *cctx)
6030{
6031 scope_T *scope = cctx->ctx_scope;
6032 ifscope_T *ifscope;
6033 garray_T *instr = &cctx->ctx_instr;
6034 isn_T *isn;
6035
6036 if (scope == NULL || scope->se_type != IF_SCOPE)
6037 {
6038 emsg(_(e_endif_without_if));
6039 return NULL;
6040 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006041 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006042 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006043 if (!cctx->ctx_had_return)
6044 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006045
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006046 if (scope->se_u.se_if.is_if_label >= 0)
6047 {
6048 // previous "if" or "elseif" jumps here
6049 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6050 isn->isn_arg.jump.jump_where = instr->ga_len;
6051 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052 // Fill in the "end" label in jumps at the end of the blocks.
6053 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006054 cctx->ctx_skip = scope->se_skip_save;
6055
6056 // If all the blocks end in :return and there is an :else then the
6057 // had_return flag is set.
6058 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006060 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061 return arg;
6062}
6063
6064/*
6065 * compile "for var in expr"
6066 *
6067 * Produces instructions:
6068 * PUSHNR -1
6069 * STORE loop-idx Set index to -1
6070 * EVAL expr Push result of "expr"
6071 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6072 * - if beyond end, jump to "end"
6073 * - otherwise get item from list and push it
6074 * STORE var Store item in "var"
6075 * ... body ...
6076 * JUMP top Jump back to repeat
6077 * end: DROP Drop the result of "expr"
6078 *
6079 */
6080 static char_u *
6081compile_for(char_u *arg, cctx_T *cctx)
6082{
6083 char_u *p;
6084 size_t varlen;
6085 garray_T *instr = &cctx->ctx_instr;
6086 garray_T *stack = &cctx->ctx_type_stack;
6087 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006088 lvar_T *loop_lvar; // loop iteration variable
6089 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006090 type_T *vartype;
6091
6092 // TODO: list of variables: "for [key, value] in dict"
6093 // parse "var"
6094 for (p = arg; eval_isnamec1(*p); ++p)
6095 ;
6096 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006097 var_lvar = lookup_local(arg, varlen, cctx);
6098 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006099 {
6100 semsg(_("E1023: variable already defined: %s"), arg);
6101 return NULL;
6102 }
6103
6104 // consume "in"
6105 p = skipwhite(p);
6106 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6107 {
6108 emsg(_(e_missing_in));
6109 return NULL;
6110 }
6111 p = skipwhite(p + 2);
6112
6113
6114 scope = new_scope(cctx, FOR_SCOPE);
6115 if (scope == NULL)
6116 return NULL;
6117
6118 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006119 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6120 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006121 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006122 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006123 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006124 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006126
6127 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006128 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6129 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006130 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006131 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006132 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006133 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006136 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137
6138 // compile "expr", it remains on the stack until "endfor"
6139 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006140 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006141 {
6142 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006144 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006145
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006146 // Now that we know the type of "var", check that it is a list, now or at
6147 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006148 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006149 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006150 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006151 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006152 return NULL;
6153 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006154 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006155 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156
6157 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006158 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006159
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006160 generate_FOR(cctx, loop_lvar->lv_idx);
6161 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006162
6163 return arg;
6164}
6165
6166/*
6167 * compile "endfor"
6168 */
6169 static char_u *
6170compile_endfor(char_u *arg, cctx_T *cctx)
6171{
6172 garray_T *instr = &cctx->ctx_instr;
6173 scope_T *scope = cctx->ctx_scope;
6174 forscope_T *forscope;
6175 isn_T *isn;
6176
6177 if (scope == NULL || scope->se_type != FOR_SCOPE)
6178 {
6179 emsg(_(e_for));
6180 return NULL;
6181 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006182 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006184 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185
6186 // At end of ":for" scope jump back to the FOR instruction.
6187 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6188
6189 // Fill in the "end" label in the FOR statement so it can jump here
6190 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6191 isn->isn_arg.forloop.for_end = instr->ga_len;
6192
6193 // Fill in the "end" label any BREAK statements
6194 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6195
6196 // Below the ":for" scope drop the "expr" list from the stack.
6197 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6198 return NULL;
6199
6200 vim_free(scope);
6201
6202 return arg;
6203}
6204
6205/*
6206 * compile "while expr"
6207 *
6208 * Produces instructions:
6209 * top: EVAL expr Push result of "expr"
6210 * JUMP_IF_FALSE end jump if false
6211 * ... body ...
6212 * JUMP top Jump back to repeat
6213 * end:
6214 *
6215 */
6216 static char_u *
6217compile_while(char_u *arg, cctx_T *cctx)
6218{
6219 char_u *p = arg;
6220 garray_T *instr = &cctx->ctx_instr;
6221 scope_T *scope;
6222
6223 scope = new_scope(cctx, WHILE_SCOPE);
6224 if (scope == NULL)
6225 return NULL;
6226
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006227 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006228
6229 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006230 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006231 return NULL;
6232
6233 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006234 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235 JUMP_IF_FALSE, cctx) == FAIL)
6236 return FAIL;
6237
6238 return p;
6239}
6240
6241/*
6242 * compile "endwhile"
6243 */
6244 static char_u *
6245compile_endwhile(char_u *arg, cctx_T *cctx)
6246{
6247 scope_T *scope = cctx->ctx_scope;
6248
6249 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6250 {
6251 emsg(_(e_while));
6252 return NULL;
6253 }
6254 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006255 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256
6257 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006258 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259
6260 // Fill in the "end" label in the WHILE statement so it can jump here.
6261 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006262 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263
6264 vim_free(scope);
6265
6266 return arg;
6267}
6268
6269/*
6270 * compile "continue"
6271 */
6272 static char_u *
6273compile_continue(char_u *arg, cctx_T *cctx)
6274{
6275 scope_T *scope = cctx->ctx_scope;
6276
6277 for (;;)
6278 {
6279 if (scope == NULL)
6280 {
6281 emsg(_(e_continue));
6282 return NULL;
6283 }
6284 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6285 break;
6286 scope = scope->se_outer;
6287 }
6288
6289 // Jump back to the FOR or WHILE instruction.
6290 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006291 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6292 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 return arg;
6294}
6295
6296/*
6297 * compile "break"
6298 */
6299 static char_u *
6300compile_break(char_u *arg, cctx_T *cctx)
6301{
6302 scope_T *scope = cctx->ctx_scope;
6303 endlabel_T **el;
6304
6305 for (;;)
6306 {
6307 if (scope == NULL)
6308 {
6309 emsg(_(e_break));
6310 return NULL;
6311 }
6312 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6313 break;
6314 scope = scope->se_outer;
6315 }
6316
6317 // Jump to the end of the FOR or WHILE loop.
6318 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006319 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006321 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6323 return FAIL;
6324
6325 return arg;
6326}
6327
6328/*
6329 * compile "{" start of block
6330 */
6331 static char_u *
6332compile_block(char_u *arg, cctx_T *cctx)
6333{
6334 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6335 return NULL;
6336 return skipwhite(arg + 1);
6337}
6338
6339/*
6340 * compile end of block: drop one scope
6341 */
6342 static void
6343compile_endblock(cctx_T *cctx)
6344{
6345 scope_T *scope = cctx->ctx_scope;
6346
6347 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006348 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006349 vim_free(scope);
6350}
6351
6352/*
6353 * compile "try"
6354 * Creates a new scope for the try-endtry, pointing to the first catch and
6355 * finally.
6356 * Creates another scope for the "try" block itself.
6357 * TRY instruction sets up exception handling at runtime.
6358 *
6359 * "try"
6360 * TRY -> catch1, -> finally push trystack entry
6361 * ... try block
6362 * "throw {exception}"
6363 * EVAL {exception}
6364 * THROW create exception
6365 * ... try block
6366 * " catch {expr}"
6367 * JUMP -> finally
6368 * catch1: PUSH exeception
6369 * EVAL {expr}
6370 * MATCH
6371 * JUMP nomatch -> catch2
6372 * CATCH remove exception
6373 * ... catch block
6374 * " catch"
6375 * JUMP -> finally
6376 * catch2: CATCH remove exception
6377 * ... catch block
6378 * " finally"
6379 * finally:
6380 * ... finally block
6381 * " endtry"
6382 * ENDTRY pop trystack entry, may rethrow
6383 */
6384 static char_u *
6385compile_try(char_u *arg, cctx_T *cctx)
6386{
6387 garray_T *instr = &cctx->ctx_instr;
6388 scope_T *try_scope;
6389 scope_T *scope;
6390
6391 // scope that holds the jumps that go to catch/finally/endtry
6392 try_scope = new_scope(cctx, TRY_SCOPE);
6393 if (try_scope == NULL)
6394 return NULL;
6395
6396 // "catch" is set when the first ":catch" is found.
6397 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006398 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399 if (generate_instr(cctx, ISN_TRY) == NULL)
6400 return NULL;
6401
6402 // scope for the try block itself
6403 scope = new_scope(cctx, BLOCK_SCOPE);
6404 if (scope == NULL)
6405 return NULL;
6406
6407 return arg;
6408}
6409
6410/*
6411 * compile "catch {expr}"
6412 */
6413 static char_u *
6414compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6415{
6416 scope_T *scope = cctx->ctx_scope;
6417 garray_T *instr = &cctx->ctx_instr;
6418 char_u *p;
6419 isn_T *isn;
6420
6421 // end block scope from :try or :catch
6422 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6423 compile_endblock(cctx);
6424 scope = cctx->ctx_scope;
6425
6426 // Error if not in a :try scope
6427 if (scope == NULL || scope->se_type != TRY_SCOPE)
6428 {
6429 emsg(_(e_catch));
6430 return NULL;
6431 }
6432
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006433 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434 {
6435 emsg(_("E1033: catch unreachable after catch-all"));
6436 return NULL;
6437 }
6438
6439 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006440 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441 JUMP_ALWAYS, cctx) == FAIL)
6442 return NULL;
6443
6444 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006445 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446 if (isn->isn_arg.try.try_catch == 0)
6447 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006448 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449 {
6450 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006451 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006452 isn->isn_arg.jump.jump_where = instr->ga_len;
6453 }
6454
6455 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006456 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006458 scope->se_u.se_try.ts_caught_all = TRUE;
6459 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006460 }
6461 else
6462 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006463 char_u *end;
6464 char_u *pat;
6465 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006466 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006467 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006469 // Push v:exception, push {expr} and MATCH
6470 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6471
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006472 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006473 if (*end != *p)
6474 {
6475 semsg(_("E1067: Separator mismatch: %s"), p);
6476 vim_free(tofree);
6477 return FAIL;
6478 }
6479 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006480 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006481 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006482 len = (int)(end - tofree);
6483 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006484 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006485 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006486 if (pat == NULL)
6487 return FAIL;
6488 if (generate_PUSHS(cctx, pat) == FAIL)
6489 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006491 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6492 return NULL;
6493
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006494 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006495 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6496 return NULL;
6497 }
6498
6499 if (generate_instr(cctx, ISN_CATCH) == NULL)
6500 return NULL;
6501
6502 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6503 return NULL;
6504 return p;
6505}
6506
6507 static char_u *
6508compile_finally(char_u *arg, cctx_T *cctx)
6509{
6510 scope_T *scope = cctx->ctx_scope;
6511 garray_T *instr = &cctx->ctx_instr;
6512 isn_T *isn;
6513
6514 // end block scope from :try or :catch
6515 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6516 compile_endblock(cctx);
6517 scope = cctx->ctx_scope;
6518
6519 // Error if not in a :try scope
6520 if (scope == NULL || scope->se_type != TRY_SCOPE)
6521 {
6522 emsg(_(e_finally));
6523 return NULL;
6524 }
6525
6526 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006527 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 if (isn->isn_arg.try.try_finally != 0)
6529 {
6530 emsg(_(e_finally_dup));
6531 return NULL;
6532 }
6533
6534 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006535 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006536
Bram Moolenaar585fea72020-04-02 22:33:21 +02006537 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006538 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 {
6540 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006541 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006543 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 }
6545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006546 // TODO: set index in ts_finally_label jumps
6547
6548 return arg;
6549}
6550
6551 static char_u *
6552compile_endtry(char_u *arg, cctx_T *cctx)
6553{
6554 scope_T *scope = cctx->ctx_scope;
6555 garray_T *instr = &cctx->ctx_instr;
6556 isn_T *isn;
6557
6558 // end block scope from :catch or :finally
6559 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6560 compile_endblock(cctx);
6561 scope = cctx->ctx_scope;
6562
6563 // Error if not in a :try scope
6564 if (scope == NULL || scope->se_type != TRY_SCOPE)
6565 {
6566 if (scope == NULL)
6567 emsg(_(e_no_endtry));
6568 else if (scope->se_type == WHILE_SCOPE)
6569 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006570 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571 emsg(_(e_endfor));
6572 else
6573 emsg(_(e_endif));
6574 return NULL;
6575 }
6576
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006577 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6579 {
6580 emsg(_("E1032: missing :catch or :finally"));
6581 return NULL;
6582 }
6583
6584 // Fill in the "end" label in jumps at the end of the blocks, if not done
6585 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006586 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587
6588 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006589 if (isn->isn_arg.try.try_catch == 0)
6590 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591 if (isn->isn_arg.try.try_finally == 0)
6592 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006593
6594 if (scope->se_u.se_try.ts_catch_label != 0)
6595 {
6596 // Last catch without match jumps here
6597 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6598 isn->isn_arg.jump.jump_where = instr->ga_len;
6599 }
6600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 compile_endblock(cctx);
6602
6603 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6604 return NULL;
6605 return arg;
6606}
6607
6608/*
6609 * compile "throw {expr}"
6610 */
6611 static char_u *
6612compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6613{
6614 char_u *p = skipwhite(arg);
6615
Bram Moolenaara5565e42020-05-09 15:44:01 +02006616 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 return NULL;
6618 if (may_generate_2STRING(-1, cctx) == FAIL)
6619 return NULL;
6620 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6621 return NULL;
6622
6623 return p;
6624}
6625
6626/*
6627 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006628 * compile "echomsg expr"
6629 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006630 * compile "execute expr"
6631 */
6632 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006633compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006634{
6635 char_u *p = arg;
6636 int count = 0;
6637
6638 for (;;)
6639 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006640 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006641 return NULL;
6642 ++count;
6643 p = skipwhite(p);
6644 if (ends_excmd(*p))
6645 break;
6646 }
6647
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006648 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6649 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6650 else if (cmdidx == CMD_execute)
6651 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6652 else if (cmdidx == CMD_echomsg)
6653 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6654 else
6655 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 return p;
6657}
6658
6659/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006660 * A command that is not compiled, execute with legacy code.
6661 */
6662 static char_u *
6663compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6664{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006665 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006666 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006667 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006668
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006669 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006670 goto theend;
6671
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006672 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006673 {
6674 long argt = excmd_get_argt(eap->cmdidx);
6675 int usefilter = FALSE;
6676
6677 has_expr = argt & (EX_XFILE | EX_EXPAND);
6678
6679 // If the command can be followed by a bar, find the bar and truncate
6680 // it, so that the following command can be compiled.
6681 // The '|' is overwritten with a NUL, it is put back below.
6682 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6683 && *eap->arg == '!')
6684 // :w !filter or :r !filter or :r! filter
6685 usefilter = TRUE;
6686 if ((argt & EX_TRLBAR) && !usefilter)
6687 {
6688 separate_nextcmd(eap);
6689 if (eap->nextcmd != NULL)
6690 nextcmd = eap->nextcmd;
6691 }
6692 }
6693
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006694 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6695 {
6696 // expand filename in "syntax include [@group] filename"
6697 has_expr = TRUE;
6698 eap->arg = skipwhite(eap->arg + 7);
6699 if (*eap->arg == '@')
6700 eap->arg = skiptowhite(eap->arg);
6701 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006702
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006703 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006704 {
6705 int count = 0;
6706 char_u *start = skipwhite(line);
6707
6708 // :cmd xxx`=expr1`yyy`=expr2`zzz
6709 // PUSHS ":cmd xxx"
6710 // eval expr1
6711 // PUSHS "yyy"
6712 // eval expr2
6713 // PUSHS "zzz"
6714 // EXECCONCAT 5
6715 for (;;)
6716 {
6717 if (p > start)
6718 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006719 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006720 ++count;
6721 }
6722 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006723 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006724 return NULL;
6725 may_generate_2STRING(-1, cctx);
6726 ++count;
6727 p = skipwhite(p);
6728 if (*p != '`')
6729 {
6730 emsg(_("E1083: missing backtick"));
6731 return NULL;
6732 }
6733 start = p + 1;
6734
6735 p = (char_u *)strstr((char *)start, "`=");
6736 if (p == NULL)
6737 {
6738 if (*skipwhite(start) != NUL)
6739 {
6740 generate_PUSHS(cctx, vim_strsave(start));
6741 ++count;
6742 }
6743 break;
6744 }
6745 }
6746 generate_EXECCONCAT(cctx, count);
6747 }
6748 else
6749 generate_EXEC(cctx, line);
6750
6751theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006752 if (*nextcmd != NUL)
6753 {
6754 // the parser expects a pointer to the bar, put it back
6755 --nextcmd;
6756 *nextcmd = '|';
6757 }
6758
6759 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006760}
6761
6762/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006763 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006764 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006765 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006766 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006767add_def_function(ufunc_T *ufunc)
6768{
6769 dfunc_T *dfunc;
6770
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006771 if (def_functions.ga_len == 0)
6772 {
6773 // The first position is not used, so that a zero uf_dfunc_idx means it
6774 // wasn't set.
6775 if (ga_grow(&def_functions, 1) == FAIL)
6776 return FAIL;
6777 ++def_functions.ga_len;
6778 }
6779
Bram Moolenaar09689a02020-05-09 22:50:08 +02006780 // Add the function to "def_functions".
6781 if (ga_grow(&def_functions, 1) == FAIL)
6782 return FAIL;
6783 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6784 CLEAR_POINTER(dfunc);
6785 dfunc->df_idx = def_functions.ga_len;
6786 ufunc->uf_dfunc_idx = dfunc->df_idx;
6787 dfunc->df_ufunc = ufunc;
6788 ++def_functions.ga_len;
6789 return OK;
6790}
6791
6792/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 * After ex_function() has collected all the function lines: parse and compile
6794 * the lines into instructions.
6795 * Adds the function to "def_functions".
6796 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6797 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006798 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006799 * This can be used recursively through compile_lambda(), which may reallocate
6800 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006801 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006802 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006803 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006804compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806 char_u *line = NULL;
6807 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809 cctx_T cctx;
6810 garray_T *instr;
6811 int called_emsg_before = called_emsg;
6812 int ret = FAIL;
6813 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006814 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006815 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006816
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006817 // When using a function that was compiled before: Free old instructions.
6818 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006819 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006821 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6822 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006823 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006825 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006826 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006827
Bram Moolenaar985116a2020-07-12 17:31:09 +02006828 ufunc->uf_def_status = UF_COMPILING;
6829
Bram Moolenaara80faa82020-04-12 19:37:17 +02006830 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831 cctx.ctx_ufunc = ufunc;
6832 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006833 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006834 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6835 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6836 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6837 cctx.ctx_type_list = &ufunc->uf_type_list;
6838 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6839 instr = &cctx.ctx_instr;
6840
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006841 // Set the context to the function, it may be compiled when called from
6842 // another script. Set the script version to the most modern one.
6843 // The line number will be set in next_line_from_context().
6844 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6846
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006847 // Make sure error messages are OK.
6848 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6849 if (do_estack_push)
6850 estack_push_ufunc(ufunc, 1);
6851
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006852 if (ufunc->uf_def_args.ga_len > 0)
6853 {
6854 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006855 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006856 int i;
6857 char_u *arg;
6858 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6859
6860 // Produce instructions for the default values of optional arguments.
6861 // Store the instruction index in uf_def_arg_idx[] so that we know
6862 // where to start when the function is called, depending on the number
6863 // of arguments.
6864 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6865 if (ufunc->uf_def_arg_idx == NULL)
6866 goto erret;
6867 for (i = 0; i < count; ++i)
6868 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006869 garray_T *stack = &cctx.ctx_type_stack;
6870 type_T *val_type;
6871 int arg_idx = first_def_arg + i;
6872
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006873 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6874 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006875 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006876 goto erret;
6877
6878 // If no type specified use the type of the default value.
6879 // Otherwise check that the default value type matches the
6880 // specified type.
6881 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6882 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6883 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006884 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006885 == FAIL)
6886 {
6887 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6888 arg_idx + 1);
6889 goto erret;
6890 }
6891
6892 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006893 goto erret;
6894 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006895 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6896 }
6897
6898 /*
6899 * Loop over all the lines of the function and generate instructions.
6900 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006901 for (;;)
6902 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006903 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006904 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006905 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006906 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006907
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006908 // Bail out on the first error to avoid a flood of errors and report
6909 // the right line number when inside try/catch.
6910 if (emsg_before != called_emsg)
6911 goto erret;
6912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 if (line != NULL && *line == '|')
6914 // the line continues after a '|'
6915 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006916 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006917 && !(*line == '#' && (line == cctx.ctx_line_start
6918 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006920 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921 goto erret;
6922 }
6923 else
6924 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006925 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006926 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006927 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006928 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006930 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931
Bram Moolenaara80faa82020-04-12 19:37:17 +02006932 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 ea.cmdlinep = &line;
6934 ea.cmd = skipwhite(line);
6935
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006936 // Some things can be recognized by the first character.
6937 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006939 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006940 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006941 if (ea.cmd[1] != '{')
6942 {
6943 line = (char_u *)"";
6944 continue;
6945 }
6946 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006947
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006948 case '}':
6949 {
6950 // "}" ends a block scope
6951 scopetype_T stype = cctx.ctx_scope == NULL
6952 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006954 if (stype == BLOCK_SCOPE)
6955 {
6956 compile_endblock(&cctx);
6957 line = ea.cmd;
6958 }
6959 else
6960 {
6961 emsg(_("E1025: using } outside of a block scope"));
6962 goto erret;
6963 }
6964 if (line != NULL)
6965 line = skipwhite(ea.cmd + 1);
6966 continue;
6967 }
6968
6969 case '{':
6970 // "{" starts a block scope
6971 // "{'a': 1}->func() is something else
6972 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6973 {
6974 line = compile_block(ea.cmd, &cctx);
6975 continue;
6976 }
6977 break;
6978
6979 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006980 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006981 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982 }
6983
6984 /*
6985 * COMMAND MODIFIERS
6986 */
6987 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6988 {
6989 if (errormsg != NULL)
6990 goto erret;
6991 // empty line or comment
6992 line = (char_u *)"";
6993 continue;
6994 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006995 // TODO: use modifiers in the command
6996 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02006997 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006998
6999 // Skip ":call" to get to the function name.
7000 if (checkforcmd(&ea.cmd, "call", 3))
7001 ea.cmd = skipwhite(ea.cmd);
7002
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007003 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007004 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007005 char_u *pskip;
7006
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007007 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007008 // find what follows.
7009 // Skip over "var.member", "var[idx]" and the like.
7010 // Also "&opt = val", "$ENV = val" and "@r = val".
7011 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007012 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007013 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007014 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007016 char_u *var_end;
7017 int oplen;
7018 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007020 var_end = find_name_end(pskip, NULL, NULL,
7021 FNE_CHECK_START | FNE_INCL_BR);
7022 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007023 if (oplen > 0)
7024 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007025 size_t len = p - ea.cmd;
7026
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007027 // Recognize an assignment if we recognize the variable
7028 // name:
7029 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007030 // "local = expr" where "local" is a local var.
7031 // "script = expr" where "script" is a script-local var.
7032 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007033 // "&opt = expr"
7034 // "$ENV = expr"
7035 // "@r = expr"
7036 if (*ea.cmd == '&'
7037 || *ea.cmd == '$'
7038 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007039 || ((len) > 2 && ea.cmd[1] == ':')
7040 || lookup_local(ea.cmd, len, &cctx) != NULL
7041 || lookup_arg(ea.cmd, len, NULL, NULL,
7042 NULL, &cctx) == OK
7043 || lookup_script(ea.cmd, len) == OK
7044 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007045 {
7046 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007047 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007048 goto erret;
7049 continue;
7050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051 }
7052 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007053
7054 if (*ea.cmd == '[')
7055 {
7056 // [var, var] = expr
7057 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7058 if (line == NULL)
7059 goto erret;
7060 if (line != ea.cmd)
7061 continue;
7062 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063 }
7064
7065 /*
7066 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007067 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007069 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007070 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007071 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007072 ea.cmd = skip_range(ea.cmd, NULL);
7073 if (ea.cmd > cmd && !starts_with_colon)
7074 {
7075 emsg(_(e_colon_required));
7076 goto erret;
7077 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007078 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007079 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007080 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007081 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082
7083 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7084 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007085 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007086 {
7087 line += STRLEN(line);
7088 continue;
7089 }
7090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007091 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007092 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007094 // CMD_let cannot happen, compile_assignment() above is used
7095 iemsg("Command from find_ex_command() not handled");
7096 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007098 }
7099
7100 p = skipwhite(p);
7101
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007102 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007103 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007104 && ea.cmdidx != CMD_elseif
7105 && ea.cmdidx != CMD_else
7106 && ea.cmdidx != CMD_endif)
7107 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007108 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007109 continue;
7110 }
7111
Bram Moolenaarefd88552020-06-18 20:50:10 +02007112 if (ea.cmdidx != CMD_elseif
7113 && ea.cmdidx != CMD_else
7114 && ea.cmdidx != CMD_endif
7115 && ea.cmdidx != CMD_endfor
7116 && ea.cmdidx != CMD_endwhile
7117 && ea.cmdidx != CMD_catch
7118 && ea.cmdidx != CMD_finally
7119 && ea.cmdidx != CMD_endtry)
7120 {
7121 if (cctx.ctx_had_return)
7122 {
7123 emsg(_("E1095: Unreachable code after :return"));
7124 goto erret;
7125 }
7126 }
7127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128 switch (ea.cmdidx)
7129 {
7130 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007131 ea.arg = p;
7132 line = compile_nested_function(&ea, &cctx);
7133 break;
7134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007135 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007136 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007137 goto erret;
7138
7139 case CMD_return:
7140 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007141 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 break;
7143
7144 case CMD_let:
7145 case CMD_const:
7146 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007147 if (line == p)
7148 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149 break;
7150
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007151 case CMD_unlet:
7152 case CMD_unlockvar:
7153 case CMD_lockvar:
7154 line = compile_unletlock(p, &ea, &cctx);
7155 break;
7156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157 case CMD_import:
7158 line = compile_import(p, &cctx);
7159 break;
7160
7161 case CMD_if:
7162 line = compile_if(p, &cctx);
7163 break;
7164 case CMD_elseif:
7165 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007166 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007167 break;
7168 case CMD_else:
7169 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007170 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171 break;
7172 case CMD_endif:
7173 line = compile_endif(p, &cctx);
7174 break;
7175
7176 case CMD_while:
7177 line = compile_while(p, &cctx);
7178 break;
7179 case CMD_endwhile:
7180 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007181 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007182 break;
7183
7184 case CMD_for:
7185 line = compile_for(p, &cctx);
7186 break;
7187 case CMD_endfor:
7188 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007189 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190 break;
7191 case CMD_continue:
7192 line = compile_continue(p, &cctx);
7193 break;
7194 case CMD_break:
7195 line = compile_break(p, &cctx);
7196 break;
7197
7198 case CMD_try:
7199 line = compile_try(p, &cctx);
7200 break;
7201 case CMD_catch:
7202 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007203 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007204 break;
7205 case CMD_finally:
7206 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007207 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007208 break;
7209 case CMD_endtry:
7210 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007211 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007212 break;
7213 case CMD_throw:
7214 line = compile_throw(p, &cctx);
7215 break;
7216
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007217 case CMD_eval:
7218 if (compile_expr0(&p, &cctx) == FAIL)
7219 goto erret;
7220
7221 // drop the return value
7222 generate_instr_drop(&cctx, ISN_DROP, 1);
7223
7224 line = skipwhite(p);
7225 break;
7226
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007229 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007230 case CMD_echomsg:
7231 case CMD_echoerr:
7232 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007233 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007235 // TODO: other commands with an expression argument
7236
Bram Moolenaar002262f2020-07-08 17:47:57 +02007237 case CMD_SIZE:
7238 semsg(_("E476: Invalid command: %s"), ea.cmd);
7239 goto erret;
7240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007242 // Not recognized, execute with do_cmdline_cmd().
7243 ea.arg = p;
7244 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007245 break;
7246 }
7247 if (line == NULL)
7248 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007249 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007250
7251 if (cctx.ctx_type_stack.ga_len < 0)
7252 {
7253 iemsg("Type stack underflow");
7254 goto erret;
7255 }
7256 }
7257
7258 if (cctx.ctx_scope != NULL)
7259 {
7260 if (cctx.ctx_scope->se_type == IF_SCOPE)
7261 emsg(_(e_endif));
7262 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7263 emsg(_(e_endwhile));
7264 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7265 emsg(_(e_endfor));
7266 else
7267 emsg(_("E1026: Missing }"));
7268 goto erret;
7269 }
7270
Bram Moolenaarefd88552020-06-18 20:50:10 +02007271 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007272 {
7273 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7274 {
7275 emsg(_("E1027: Missing return statement"));
7276 goto erret;
7277 }
7278
7279 // Return zero if there is no return at the end.
7280 generate_PUSHNR(&cctx, 0);
7281 generate_instr(&cctx, ISN_RETURN);
7282 }
7283
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007284 {
7285 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7286 + ufunc->uf_dfunc_idx;
7287 dfunc->df_deleted = FALSE;
7288 dfunc->df_instr = instr->ga_data;
7289 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007290 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007291 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007292 if (cctx.ctx_outer_used)
7293 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007294 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007296
7297 ret = OK;
7298
7299erret:
7300 if (ret == FAIL)
7301 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007302 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007303 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7304 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007305
7306 for (idx = 0; idx < instr->ga_len; ++idx)
7307 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007309
Bram Moolenaar45a15082020-05-25 00:28:33 +02007310 // if using the last entry in the table we might as well remove it
7311 if (!dfunc->df_deleted
7312 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007313 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007314 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007315
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007316 while (cctx.ctx_scope != NULL)
7317 drop_scope(&cctx);
7318
Bram Moolenaar20431c92020-03-20 18:39:46 +01007319 // Don't execute this function body.
7320 ga_clear_strings(&ufunc->uf_lines);
7321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 if (errormsg != NULL)
7323 emsg(errormsg);
7324 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007325 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326 }
7327
7328 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007329 if (do_estack_push)
7330 estack_pop();
7331
Bram Moolenaar20431c92020-03-20 18:39:46 +01007332 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007333 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007335 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336}
7337
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007338 void
7339set_function_type(ufunc_T *ufunc)
7340{
7341 int varargs = ufunc->uf_va_name != NULL;
7342 int argcount = ufunc->uf_args.ga_len;
7343
7344 // Create a type for the function, with the return type and any
7345 // argument types.
7346 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7347 // The type is included in "tt_args".
7348 if (argcount > 0 || varargs)
7349 {
7350 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7351 argcount, &ufunc->uf_type_list);
7352 // Add argument types to the function type.
7353 if (func_type_add_arg_types(ufunc->uf_func_type,
7354 argcount + varargs,
7355 &ufunc->uf_type_list) == FAIL)
7356 return;
7357 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7358 ufunc->uf_func_type->tt_min_argcount =
7359 argcount - ufunc->uf_def_args.ga_len;
7360 if (ufunc->uf_arg_types == NULL)
7361 {
7362 int i;
7363
7364 // lambda does not have argument types.
7365 for (i = 0; i < argcount; ++i)
7366 ufunc->uf_func_type->tt_args[i] = &t_any;
7367 }
7368 else
7369 mch_memmove(ufunc->uf_func_type->tt_args,
7370 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7371 if (varargs)
7372 {
7373 ufunc->uf_func_type->tt_args[argcount] =
7374 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7375 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7376 }
7377 }
7378 else
7379 // No arguments, can use a predefined type.
7380 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7381 argcount, &ufunc->uf_type_list);
7382}
7383
7384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385/*
7386 * Delete an instruction, free what it contains.
7387 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007388 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007389delete_instr(isn_T *isn)
7390{
7391 switch (isn->isn_type)
7392 {
7393 case ISN_EXEC:
7394 case ISN_LOADENV:
7395 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007396 case ISN_LOADB:
7397 case ISN_LOADW:
7398 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007400 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 case ISN_PUSHEXC:
7402 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007403 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007405 case ISN_STOREB:
7406 case ISN_STOREW:
7407 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007408 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409 vim_free(isn->isn_arg.string);
7410 break;
7411
7412 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007413 case ISN_STORES:
7414 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007415 break;
7416
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007417 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007418 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007419 vim_free(isn->isn_arg.unlet.ul_name);
7420 break;
7421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422 case ISN_STOREOPT:
7423 vim_free(isn->isn_arg.storeopt.so_name);
7424 break;
7425
7426 case ISN_PUSHBLOB: // push blob isn_arg.blob
7427 blob_unref(isn->isn_arg.blob);
7428 break;
7429
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007430 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007431#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007432 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007433#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007434 break;
7435
7436 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007437#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007438 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007439#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007440 break;
7441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007442 case ISN_UCALL:
7443 vim_free(isn->isn_arg.ufunc.cuf_name);
7444 break;
7445
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007446 case ISN_FUNCREF:
7447 {
7448 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7449 + isn->isn_arg.funcref.fr_func;
7450 func_ptr_unref(dfunc->df_ufunc);
7451 }
7452 break;
7453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007454 case ISN_2BOOL:
7455 case ISN_2STRING:
7456 case ISN_ADDBLOB:
7457 case ISN_ADDLIST:
7458 case ISN_BCALL:
7459 case ISN_CATCH:
7460 case ISN_CHECKNR:
7461 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007462 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463 case ISN_COMPAREANY:
7464 case ISN_COMPAREBLOB:
7465 case ISN_COMPAREBOOL:
7466 case ISN_COMPAREDICT:
7467 case ISN_COMPAREFLOAT:
7468 case ISN_COMPAREFUNC:
7469 case ISN_COMPARELIST:
7470 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471 case ISN_COMPARESPECIAL:
7472 case ISN_COMPARESTRING:
7473 case ISN_CONCAT:
7474 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007475 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476 case ISN_DROP:
7477 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007478 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007479 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007481 case ISN_EXECCONCAT:
7482 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007484 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007485 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007486 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007487 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 case ISN_JUMP:
7489 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007490 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007491 case ISN_LOADSCRIPT:
7492 case ISN_LOADREG:
7493 case ISN_LOADV:
7494 case ISN_NEGATENR:
7495 case ISN_NEWDICT:
7496 case ISN_NEWLIST:
7497 case ISN_OPNR:
7498 case ISN_OPFLOAT:
7499 case ISN_OPANY:
7500 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007501 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502 case ISN_PUSHF:
7503 case ISN_PUSHNR:
7504 case ISN_PUSHBOOL:
7505 case ISN_PUSHSPEC:
7506 case ISN_RETURN:
7507 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007508 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007509 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007511 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007512 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007513 case ISN_STOREDICT:
7514 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515 case ISN_THROW:
7516 case ISN_TRY:
7517 // nothing allocated
7518 break;
7519 }
7520}
7521
7522/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007523 * Free all instructions for "dfunc".
7524 */
7525 static void
7526delete_def_function_contents(dfunc_T *dfunc)
7527{
7528 int idx;
7529
7530 ga_clear(&dfunc->df_def_args_isn);
7531
7532 if (dfunc->df_instr != NULL)
7533 {
7534 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7535 delete_instr(dfunc->df_instr + idx);
7536 VIM_CLEAR(dfunc->df_instr);
7537 }
7538
7539 dfunc->df_deleted = TRUE;
7540}
7541
7542/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007543 * When a user function is deleted, clear the contents of any associated def
7544 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007545 */
7546 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007547clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007549 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007550 {
7551 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7552 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007553
Bram Moolenaar20431c92020-03-20 18:39:46 +01007554 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007555 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556 }
7557}
7558
7559#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007560/*
7561 * Free all functions defined with ":def".
7562 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563 void
7564free_def_functions(void)
7565{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007566 int idx;
7567
7568 for (idx = 0; idx < def_functions.ga_len; ++idx)
7569 {
7570 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7571
7572 delete_def_function_contents(dfunc);
7573 }
7574
7575 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576}
7577#endif
7578
7579
7580#endif // FEAT_EVAL