blob: 8905ee3742314250c4b5d7147c7902a4be15a2e0 [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
915need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
916{
917 if (check_type(expected, actual, FALSE) == OK)
918 return OK;
919 if (actual->tt_type != VAR_ANY
920 && actual->tt_type != VAR_UNKNOWN
921 && !(actual->tt_type == VAR_FUNC
922 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
923 {
924 type_mismatch(expected, actual);
925 return FAIL;
926 }
927 generate_TYPECHECK(cctx, expected, offset);
928 return OK;
929}
930
931/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 * Generate an ISN_PUSHNR instruction.
933 */
934 static int
935generate_PUSHNR(cctx_T *cctx, varnumber_T number)
936{
937 isn_T *isn;
938
Bram Moolenaar080457c2020-03-03 21:53:32 +0100939 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
941 return FAIL;
942 isn->isn_arg.number = number;
943
944 return OK;
945}
946
947/*
948 * Generate an ISN_PUSHBOOL instruction.
949 */
950 static int
951generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
952{
953 isn_T *isn;
954
Bram Moolenaar080457c2020-03-03 21:53:32 +0100955 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
957 return FAIL;
958 isn->isn_arg.number = number;
959
960 return OK;
961}
962
963/*
964 * Generate an ISN_PUSHSPEC instruction.
965 */
966 static int
967generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
968{
969 isn_T *isn;
970
Bram Moolenaar080457c2020-03-03 21:53:32 +0100971 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
973 return FAIL;
974 isn->isn_arg.number = number;
975
976 return OK;
977}
978
979#ifdef FEAT_FLOAT
980/*
981 * Generate an ISN_PUSHF instruction.
982 */
983 static int
984generate_PUSHF(cctx_T *cctx, float_T fnumber)
985{
986 isn_T *isn;
987
Bram Moolenaar080457c2020-03-03 21:53:32 +0100988 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
990 return FAIL;
991 isn->isn_arg.fnumber = fnumber;
992
993 return OK;
994}
995#endif
996
997/*
998 * Generate an ISN_PUSHS instruction.
999 * Consumes "str".
1000 */
1001 static int
1002generate_PUSHS(cctx_T *cctx, char_u *str)
1003{
1004 isn_T *isn;
1005
Bram Moolenaar080457c2020-03-03 21:53:32 +01001006 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1008 return FAIL;
1009 isn->isn_arg.string = str;
1010
1011 return OK;
1012}
1013
1014/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001015 * Generate an ISN_PUSHCHANNEL instruction.
1016 * Consumes "channel".
1017 */
1018 static int
1019generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1020{
1021 isn_T *isn;
1022
Bram Moolenaar080457c2020-03-03 21:53:32 +01001023 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001024 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1025 return FAIL;
1026 isn->isn_arg.channel = channel;
1027
1028 return OK;
1029}
1030
1031/*
1032 * Generate an ISN_PUSHJOB instruction.
1033 * Consumes "job".
1034 */
1035 static int
1036generate_PUSHJOB(cctx_T *cctx, job_T *job)
1037{
1038 isn_T *isn;
1039
Bram Moolenaar080457c2020-03-03 21:53:32 +01001040 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001041 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001042 return FAIL;
1043 isn->isn_arg.job = job;
1044
1045 return OK;
1046}
1047
1048/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049 * Generate an ISN_PUSHBLOB instruction.
1050 * Consumes "blob".
1051 */
1052 static int
1053generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1054{
1055 isn_T *isn;
1056
Bram Moolenaar080457c2020-03-03 21:53:32 +01001057 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1059 return FAIL;
1060 isn->isn_arg.blob = blob;
1061
1062 return OK;
1063}
1064
1065/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001066 * Generate an ISN_PUSHFUNC instruction with name "name".
1067 * Consumes "name".
1068 */
1069 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001070generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001071{
1072 isn_T *isn;
1073
Bram Moolenaar080457c2020-03-03 21:53:32 +01001074 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001075 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001076 return FAIL;
1077 isn->isn_arg.string = name;
1078
1079 return OK;
1080}
1081
1082/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001083 * Generate an ISN_GETITEM instruction with "index".
1084 */
1085 static int
1086generate_GETITEM(cctx_T *cctx, int index)
1087{
1088 isn_T *isn;
1089 garray_T *stack = &cctx->ctx_type_stack;
1090 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1091 type_T *item_type = &t_any;
1092
1093 RETURN_OK_IF_SKIP(cctx);
1094
1095 if (type->tt_type == VAR_LIST)
1096 item_type = type->tt_member;
1097 else if (type->tt_type != VAR_ANY)
1098 {
1099 emsg(_(e_listreq));
1100 return FAIL;
1101 }
1102 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1103 return FAIL;
1104 isn->isn_arg.number = index;
1105
1106 // add the item type to the type stack
1107 if (ga_grow(stack, 1) == FAIL)
1108 return FAIL;
1109 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1110 ++stack->ga_len;
1111 return OK;
1112}
1113
1114/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001115 * Generate an ISN_SLICE instruction with "count".
1116 */
1117 static int
1118generate_SLICE(cctx_T *cctx, int count)
1119{
1120 isn_T *isn;
1121
1122 RETURN_OK_IF_SKIP(cctx);
1123 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1124 return FAIL;
1125 isn->isn_arg.number = count;
1126 return OK;
1127}
1128
1129/*
1130 * Generate an ISN_CHECKLEN instruction with "min_len".
1131 */
1132 static int
1133generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1134{
1135 isn_T *isn;
1136
1137 RETURN_OK_IF_SKIP(cctx);
1138
1139 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.checklen.cl_min_len = min_len;
1142 isn->isn_arg.checklen.cl_more_OK = more_OK;
1143
1144 return OK;
1145}
1146
1147/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 * Generate an ISN_STORE instruction.
1149 */
1150 static int
1151generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1157 return FAIL;
1158 if (name != NULL)
1159 isn->isn_arg.string = vim_strsave(name);
1160 else
1161 isn->isn_arg.number = idx;
1162
1163 return OK;
1164}
1165
1166/*
1167 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1168 */
1169 static int
1170generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1171{
1172 isn_T *isn;
1173
Bram Moolenaar080457c2020-03-03 21:53:32 +01001174 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1176 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001177 isn->isn_arg.storenr.stnr_idx = idx;
1178 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179
1180 return OK;
1181}
1182
1183/*
1184 * Generate an ISN_STOREOPT instruction
1185 */
1186 static int
1187generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1188{
1189 isn_T *isn;
1190
Bram Moolenaar080457c2020-03-03 21:53:32 +01001191 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1193 return FAIL;
1194 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1195 isn->isn_arg.storeopt.so_flags = opt_flags;
1196
1197 return OK;
1198}
1199
1200/*
1201 * Generate an ISN_LOAD or similar instruction.
1202 */
1203 static int
1204generate_LOAD(
1205 cctx_T *cctx,
1206 isntype_T isn_type,
1207 int idx,
1208 char_u *name,
1209 type_T *type)
1210{
1211 isn_T *isn;
1212
Bram Moolenaar080457c2020-03-03 21:53:32 +01001213 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1215 return FAIL;
1216 if (name != NULL)
1217 isn->isn_arg.string = vim_strsave(name);
1218 else
1219 isn->isn_arg.number = idx;
1220
1221 return OK;
1222}
1223
1224/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001225 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001226 */
1227 static int
1228generate_LOADV(
1229 cctx_T *cctx,
1230 char_u *name,
1231 int error)
1232{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001233 int di_flags;
1234 int vidx = find_vim_var(name, &di_flags);
1235 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001236
Bram Moolenaar080457c2020-03-03 21:53:32 +01001237 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001238 if (vidx < 0)
1239 {
1240 if (error)
1241 semsg(_(e_var_notfound), name);
1242 return FAIL;
1243 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001244 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001245
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001246 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001247}
1248
1249/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001250 * Generate an ISN_UNLET instruction.
1251 */
1252 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001253generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001254{
1255 isn_T *isn;
1256
1257 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001258 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001259 return FAIL;
1260 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1261 isn->isn_arg.unlet.ul_forceit = forceit;
1262
1263 return OK;
1264}
1265
1266/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 * Generate an ISN_LOADS instruction.
1268 */
1269 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001270generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001272 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001274 int sid,
1275 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276{
1277 isn_T *isn;
1278
Bram Moolenaar080457c2020-03-03 21:53:32 +01001279 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280 if (isn_type == ISN_LOADS)
1281 isn = generate_instr_type(cctx, isn_type, type);
1282 else
1283 isn = generate_instr_drop(cctx, isn_type, 1);
1284 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1287 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288
1289 return OK;
1290}
1291
1292/*
1293 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1294 */
1295 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001296generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 cctx_T *cctx,
1298 isntype_T isn_type,
1299 int sid,
1300 int idx,
1301 type_T *type)
1302{
1303 isn_T *isn;
1304
Bram Moolenaar080457c2020-03-03 21:53:32 +01001305 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 if (isn_type == ISN_LOADSCRIPT)
1307 isn = generate_instr_type(cctx, isn_type, type);
1308 else
1309 isn = generate_instr_drop(cctx, isn_type, 1);
1310 if (isn == NULL)
1311 return FAIL;
1312 isn->isn_arg.script.script_sid = sid;
1313 isn->isn_arg.script.script_idx = idx;
1314 return OK;
1315}
1316
1317/*
1318 * Generate an ISN_NEWLIST instruction.
1319 */
1320 static int
1321generate_NEWLIST(cctx_T *cctx, int count)
1322{
1323 isn_T *isn;
1324 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 type_T *type;
1326 type_T *member;
1327
Bram Moolenaar080457c2020-03-03 21:53:32 +01001328 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1330 return FAIL;
1331 isn->isn_arg.number = count;
1332
1333 // drop the value types
1334 stack->ga_len -= count;
1335
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001336 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001337 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 if (count > 0)
1339 member = ((type_T **)stack->ga_data)[stack->ga_len];
1340 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001341 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001342 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343
1344 // add the list type to the type stack
1345 if (ga_grow(stack, 1) == FAIL)
1346 return FAIL;
1347 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1348 ++stack->ga_len;
1349
1350 return OK;
1351}
1352
1353/*
1354 * Generate an ISN_NEWDICT instruction.
1355 */
1356 static int
1357generate_NEWDICT(cctx_T *cctx, int count)
1358{
1359 isn_T *isn;
1360 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 type_T *type;
1362 type_T *member;
1363
Bram Moolenaar080457c2020-03-03 21:53:32 +01001364 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1366 return FAIL;
1367 isn->isn_arg.number = count;
1368
1369 // drop the key and value types
1370 stack->ga_len -= 2 * count;
1371
Bram Moolenaar436472f2020-02-20 22:54:43 +01001372 // Use the first value type for the list member type. Use "void" for an
1373 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 if (count > 0)
1375 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1376 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001377 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001378 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379
1380 // add the dict type to the type stack
1381 if (ga_grow(stack, 1) == FAIL)
1382 return FAIL;
1383 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1384 ++stack->ga_len;
1385
1386 return OK;
1387}
1388
1389/*
1390 * Generate an ISN_FUNCREF instruction.
1391 */
1392 static int
1393generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1394{
1395 isn_T *isn;
1396 garray_T *stack = &cctx->ctx_type_stack;
1397
Bram Moolenaar080457c2020-03-03 21:53:32 +01001398 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1400 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001401 isn->isn_arg.funcref.fr_func = dfunc_idx;
1402 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403
1404 if (ga_grow(stack, 1) == FAIL)
1405 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001406 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 // TODO: argument and return types
1408 ++stack->ga_len;
1409
1410 return OK;
1411}
1412
1413/*
1414 * Generate an ISN_JUMP instruction.
1415 */
1416 static int
1417generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1418{
1419 isn_T *isn;
1420 garray_T *stack = &cctx->ctx_type_stack;
1421
Bram Moolenaar080457c2020-03-03 21:53:32 +01001422 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1424 return FAIL;
1425 isn->isn_arg.jump.jump_when = when;
1426 isn->isn_arg.jump.jump_where = where;
1427
1428 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1429 --stack->ga_len;
1430
1431 return OK;
1432}
1433
1434 static int
1435generate_FOR(cctx_T *cctx, int loop_idx)
1436{
1437 isn_T *isn;
1438 garray_T *stack = &cctx->ctx_type_stack;
1439
Bram Moolenaar080457c2020-03-03 21:53:32 +01001440 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1442 return FAIL;
1443 isn->isn_arg.forloop.for_idx = loop_idx;
1444
1445 if (ga_grow(stack, 1) == FAIL)
1446 return FAIL;
1447 // type doesn't matter, will be stored next
1448 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1449 ++stack->ga_len;
1450
1451 return OK;
1452}
1453
1454/*
1455 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001456 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 * Return FAIL if the number of arguments is wrong.
1458 */
1459 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001460generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461{
1462 isn_T *isn;
1463 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001464 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001465 type_T *argtypes[MAX_FUNC_ARGS];
1466 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467
Bram Moolenaar080457c2020-03-03 21:53:32 +01001468 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001469 argoff = check_internal_func(func_idx, argcount);
1470 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 return FAIL;
1472
Bram Moolenaar389df252020-07-09 21:20:47 +02001473 if (method_call && argoff > 1)
1474 {
1475 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1476 return FAIL;
1477 isn->isn_arg.shuffle.shfl_item = argcount;
1478 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1479 }
1480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1482 return FAIL;
1483 isn->isn_arg.bfunc.cbf_idx = func_idx;
1484 isn->isn_arg.bfunc.cbf_argcount = argcount;
1485
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001486 for (i = 0; i < argcount; ++i)
1487 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 stack->ga_len -= argcount; // drop the arguments
1490 if (ga_grow(stack, 1) == FAIL)
1491 return FAIL;
1492 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001493 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 ++stack->ga_len; // add return value
1495
1496 return OK;
1497}
1498
1499/*
1500 * Generate an ISN_DCALL or ISN_UCALL instruction.
1501 * Return FAIL if the number of arguments is wrong.
1502 */
1503 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001504generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505{
1506 isn_T *isn;
1507 garray_T *stack = &cctx->ctx_type_stack;
1508 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001509 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510
Bram Moolenaar080457c2020-03-03 21:53:32 +01001511 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512 if (argcount > regular_args && !has_varargs(ufunc))
1513 {
1514 semsg(_(e_toomanyarg), ufunc->uf_name);
1515 return FAIL;
1516 }
1517 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1518 {
1519 semsg(_(e_toofewarg), ufunc->uf_name);
1520 return FAIL;
1521 }
1522
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001523 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001524 {
1525 int i;
1526
1527 for (i = 0; i < argcount; ++i)
1528 {
1529 type_T *expected;
1530 type_T *actual;
1531
1532 if (i < regular_args)
1533 {
1534 if (ufunc->uf_arg_types == NULL)
1535 continue;
1536 expected = ufunc->uf_arg_types[i];
1537 }
1538 else
1539 expected = ufunc->uf_va_type->tt_member;
1540 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001541 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001542 {
1543 arg_type_mismatch(expected, actual, i + 1);
1544 return FAIL;
1545 }
1546 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001547 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001548 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001549 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001550 }
1551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001553 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001554 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001556 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 {
1558 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1559 isn->isn_arg.dfunc.cdf_argcount = argcount;
1560 }
1561 else
1562 {
1563 // A user function may be deleted and redefined later, can't use the
1564 // ufunc pointer, need to look it up again at runtime.
1565 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1566 isn->isn_arg.ufunc.cuf_argcount = argcount;
1567 }
1568
1569 stack->ga_len -= argcount; // drop the arguments
1570 if (ga_grow(stack, 1) == FAIL)
1571 return FAIL;
1572 // add return value
1573 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1574 ++stack->ga_len;
1575
1576 return OK;
1577}
1578
1579/*
1580 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1581 */
1582 static int
1583generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1584{
1585 isn_T *isn;
1586 garray_T *stack = &cctx->ctx_type_stack;
1587
Bram Moolenaar080457c2020-03-03 21:53:32 +01001588 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1590 return FAIL;
1591 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1592 isn->isn_arg.ufunc.cuf_argcount = argcount;
1593
1594 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001595 if (ga_grow(stack, 1) == FAIL)
1596 return FAIL;
1597 // add return value
1598 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1599 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600
1601 return OK;
1602}
1603
1604/*
1605 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001606 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 */
1608 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001609generate_PCALL(
1610 cctx_T *cctx,
1611 int argcount,
1612 char_u *name,
1613 type_T *type,
1614 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615{
1616 isn_T *isn;
1617 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001618 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619
Bram Moolenaar080457c2020-03-03 21:53:32 +01001620 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001621
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001622 if (type->tt_type == VAR_ANY)
1623 ret_type = &t_any;
1624 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001625 {
1626 if (type->tt_argcount != -1)
1627 {
1628 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1629
1630 if (argcount < type->tt_min_argcount - varargs)
1631 {
1632 semsg(_(e_toofewarg), "[reference]");
1633 return FAIL;
1634 }
1635 if (!varargs && argcount > type->tt_argcount)
1636 {
1637 semsg(_(e_toomanyarg), "[reference]");
1638 return FAIL;
1639 }
1640 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001641 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001642 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001643 else
1644 {
1645 semsg(_("E1085: Not a callable type: %s"), name);
1646 return FAIL;
1647 }
1648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1650 return FAIL;
1651 isn->isn_arg.pfunc.cpf_top = at_top;
1652 isn->isn_arg.pfunc.cpf_argcount = argcount;
1653
1654 stack->ga_len -= argcount; // drop the arguments
1655
1656 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001657 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001659 // If partial is above the arguments it must be cleared and replaced with
1660 // the return value.
1661 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1662 return FAIL;
1663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 return OK;
1665}
1666
1667/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001668 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 */
1670 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001671generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672{
1673 isn_T *isn;
1674 garray_T *stack = &cctx->ctx_type_stack;
1675 type_T *type;
1676
Bram Moolenaar080457c2020-03-03 21:53:32 +01001677 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001678 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001680 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001682 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001684 if (type->tt_type != VAR_DICT && type != &t_any)
1685 {
1686 emsg(_(e_dictreq));
1687 return FAIL;
1688 }
1689 // change dict type to dict member type
1690 if (type->tt_type == VAR_DICT)
1691 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692
1693 return OK;
1694}
1695
1696/*
1697 * Generate an ISN_ECHO instruction.
1698 */
1699 static int
1700generate_ECHO(cctx_T *cctx, int with_white, int count)
1701{
1702 isn_T *isn;
1703
Bram Moolenaar080457c2020-03-03 21:53:32 +01001704 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1706 return FAIL;
1707 isn->isn_arg.echo.echo_with_white = with_white;
1708 isn->isn_arg.echo.echo_count = count;
1709
1710 return OK;
1711}
1712
Bram Moolenaarad39c092020-02-26 18:23:43 +01001713/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001714 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001715 */
1716 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001717generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001718{
1719 isn_T *isn;
1720
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001721 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001722 return FAIL;
1723 isn->isn_arg.number = count;
1724
1725 return OK;
1726}
1727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 static int
1729generate_EXEC(cctx_T *cctx, char_u *line)
1730{
1731 isn_T *isn;
1732
Bram Moolenaar080457c2020-03-03 21:53:32 +01001733 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1735 return FAIL;
1736 isn->isn_arg.string = vim_strsave(line);
1737 return OK;
1738}
1739
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001740 static int
1741generate_EXECCONCAT(cctx_T *cctx, int count)
1742{
1743 isn_T *isn;
1744
1745 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1746 return FAIL;
1747 isn->isn_arg.number = count;
1748 return OK;
1749}
1750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751/*
1752 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001753 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001755 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1757{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 lvar_T *lvar;
1759
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001760 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001762 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001763 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 }
1765
1766 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001767 return NULL;
1768 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001770 // Every local variable uses the next entry on the stack. We could re-use
1771 // the last ones when leaving a scope, but then variables used in a closure
1772 // might get overwritten. To keep things simple do not re-use stack
1773 // entries. This is less efficient, but memory is cheap these days.
1774 lvar->lv_idx = cctx->ctx_locals_count++;
1775
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001776 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 lvar->lv_const = isConst;
1778 lvar->lv_type = type;
1779
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001780 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781}
1782
1783/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001784 * Remove local variables above "new_top".
1785 */
1786 static void
1787unwind_locals(cctx_T *cctx, int new_top)
1788{
1789 if (cctx->ctx_locals.ga_len > new_top)
1790 {
1791 int idx;
1792 lvar_T *lvar;
1793
1794 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1795 {
1796 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1797 vim_free(lvar->lv_name);
1798 }
1799 }
1800 cctx->ctx_locals.ga_len = new_top;
1801}
1802
1803/*
1804 * Free all local variables.
1805 */
1806 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001807free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001808{
1809 unwind_locals(cctx, 0);
1810 ga_clear(&cctx->ctx_locals);
1811}
1812
1813/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814 * Skip over a type definition and return a pointer to just after it.
1815 */
1816 char_u *
1817skip_type(char_u *start)
1818{
1819 char_u *p = start;
1820
1821 while (ASCII_ISALNUM(*p) || *p == '_')
1822 ++p;
1823
1824 // Skip over "<type>"; this is permissive about white space.
1825 if (*skipwhite(p) == '<')
1826 {
1827 p = skipwhite(p);
1828 p = skip_type(skipwhite(p + 1));
1829 p = skipwhite(p);
1830 if (*p == '>')
1831 ++p;
1832 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001833 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1834 {
1835 // handle func(args): type
1836 ++p;
1837 while (*p != ')' && *p != NUL)
1838 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001839 char_u *sp = p;
1840
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001841 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001842 if (p == sp)
1843 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001844 if (*p == ',')
1845 p = skipwhite(p + 1);
1846 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001847 if (*p == ')')
1848 {
1849 if (p[1] == ':')
1850 p = skip_type(skipwhite(p + 2));
1851 else
1852 p = skipwhite(p + 1);
1853 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001854 }
1855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 return p;
1857}
1858
1859/*
1860 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001861 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 * Returns NULL in case of failure.
1863 */
1864 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001865parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866{
1867 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001868 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869
1870 if (**arg != '<')
1871 {
1872 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001873 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 else
1875 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001876 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 }
1878 *arg = skipwhite(*arg + 1);
1879
Bram Moolenaard77a8522020-04-03 21:59:57 +02001880 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881
1882 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001883 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 {
1885 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001886 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 }
1888 ++*arg;
1889
1890 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001891 return get_list_type(member_type, type_gap);
1892 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893}
1894
1895/*
1896 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001897 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 */
1899 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001900parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901{
1902 char_u *p = *arg;
1903 size_t len;
1904
1905 // skip over the first word
1906 while (ASCII_ISALNUM(*p) || *p == '_')
1907 ++p;
1908 len = p - *arg;
1909
1910 switch (**arg)
1911 {
1912 case 'a':
1913 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1914 {
1915 *arg += len;
1916 return &t_any;
1917 }
1918 break;
1919 case 'b':
1920 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1921 {
1922 *arg += len;
1923 return &t_bool;
1924 }
1925 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1926 {
1927 *arg += len;
1928 return &t_blob;
1929 }
1930 break;
1931 case 'c':
1932 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1933 {
1934 *arg += len;
1935 return &t_channel;
1936 }
1937 break;
1938 case 'd':
1939 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1940 {
1941 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001942 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 }
1944 break;
1945 case 'f':
1946 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1947 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001948#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 *arg += len;
1950 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001951#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001952 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001953 return &t_any;
1954#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 }
1956 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1957 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001958 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001959 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001960 int argcount = -1;
1961 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001962 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001963 type_T *arg_type[MAX_FUNC_ARGS + 1];
1964
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001965 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001967 if (**arg == '(')
1968 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001969 // "func" may or may not return a value, "func()" does
1970 // not return a value.
1971 ret_type = &t_void;
1972
Bram Moolenaard77a8522020-04-03 21:59:57 +02001973 p = ++*arg;
1974 argcount = 0;
1975 while (*p != NUL && *p != ')')
1976 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001977 if (*p == '?')
1978 {
1979 if (first_optional == -1)
1980 first_optional = argcount;
1981 ++p;
1982 }
1983 else if (first_optional != -1)
1984 {
1985 emsg(_("E1007: mandatory argument after optional argument"));
1986 return &t_any;
1987 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001988 else if (STRNCMP(p, "...", 3) == 0)
1989 {
1990 flags |= TTFLAG_VARARGS;
1991 p += 3;
1992 }
1993
1994 arg_type[argcount++] = parse_type(&p, type_gap);
1995
1996 // Nothing comes after "...{type}".
1997 if (flags & TTFLAG_VARARGS)
1998 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001999
Bram Moolenaard77a8522020-04-03 21:59:57 +02002000 if (*p != ',' && *skipwhite(p) == ',')
2001 {
2002 semsg(_(e_no_white_before), ",");
2003 return &t_any;
2004 }
2005 if (*p == ',')
2006 {
2007 ++p;
2008 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002009 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002010 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002011 return &t_any;
2012 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002013 }
2014 p = skipwhite(p);
2015 if (argcount == MAX_FUNC_ARGS)
2016 {
2017 emsg(_("E740: Too many argument types"));
2018 return &t_any;
2019 }
2020 }
2021
2022 p = skipwhite(p);
2023 if (*p != ')')
2024 {
2025 emsg(_(e_missing_close));
2026 return &t_any;
2027 }
2028 *arg = p + 1;
2029 }
2030 if (**arg == ':')
2031 {
2032 // parse return type
2033 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002034 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002035 semsg(_(e_white_after), ":");
2036 *arg = skipwhite(*arg);
2037 ret_type = parse_type(arg, type_gap);
2038 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002039 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002040 type = get_func_type(ret_type, argcount, type_gap);
2041 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002042 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002043 type = alloc_func_type(ret_type, argcount, type_gap);
2044 type->tt_flags = flags;
2045 if (argcount > 0)
2046 {
2047 type->tt_argcount = argcount;
2048 type->tt_min_argcount = first_optional == -1
2049 ? argcount : first_optional;
2050 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002051 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002052 return &t_any;
2053 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002054 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002055 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002056 }
2057 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 }
2059 break;
2060 case 'j':
2061 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2062 {
2063 *arg += len;
2064 return &t_job;
2065 }
2066 break;
2067 case 'l':
2068 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2069 {
2070 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002071 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 }
2073 break;
2074 case 'n':
2075 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2076 {
2077 *arg += len;
2078 return &t_number;
2079 }
2080 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 case 's':
2082 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2083 {
2084 *arg += len;
2085 return &t_string;
2086 }
2087 break;
2088 case 'v':
2089 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2090 {
2091 *arg += len;
2092 return &t_void;
2093 }
2094 break;
2095 }
2096
2097 semsg(_("E1010: Type not recognized: %s"), *arg);
2098 return &t_any;
2099}
2100
2101/*
2102 * Check if "type1" and "type2" are exactly the same.
2103 */
2104 static int
2105equal_type(type_T *type1, type_T *type2)
2106{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002107 int i;
2108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 if (type1->tt_type != type2->tt_type)
2110 return FALSE;
2111 switch (type1->tt_type)
2112 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002114 case VAR_ANY:
2115 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 case VAR_SPECIAL:
2117 case VAR_BOOL:
2118 case VAR_NUMBER:
2119 case VAR_FLOAT:
2120 case VAR_STRING:
2121 case VAR_BLOB:
2122 case VAR_JOB:
2123 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002124 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 case VAR_LIST:
2126 case VAR_DICT:
2127 return equal_type(type1->tt_member, type2->tt_member);
2128 case VAR_FUNC:
2129 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002130 if (!equal_type(type1->tt_member, type2->tt_member)
2131 || type1->tt_argcount != type2->tt_argcount)
2132 return FALSE;
2133 if (type1->tt_argcount < 0
2134 || type1->tt_args == NULL || type2->tt_args == NULL)
2135 return TRUE;
2136 for (i = 0; i < type1->tt_argcount; ++i)
2137 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2138 return FALSE;
2139 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 }
2141 return TRUE;
2142}
2143
2144/*
2145 * Find the common type of "type1" and "type2" and put it in "dest".
2146 * "type2" and "dest" may be the same.
2147 */
2148 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002149common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150{
2151 if (equal_type(type1, type2))
2152 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002153 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 return;
2155 }
2156
2157 if (type1->tt_type == type2->tt_type)
2158 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2160 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002161 type_T *common;
2162
Bram Moolenaard77a8522020-04-03 21:59:57 +02002163 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002164 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002165 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002166 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002167 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 return;
2169 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002170 if (type1->tt_type == VAR_FUNC)
2171 {
2172 type_T *common;
2173
2174 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2175 if (type1->tt_argcount == type2->tt_argcount
2176 && type1->tt_argcount >= 0)
2177 {
2178 int argcount = type1->tt_argcount;
2179 int i;
2180
2181 *dest = alloc_func_type(common, argcount, type_gap);
2182 if (type1->tt_args != NULL && type2->tt_args != NULL)
2183 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002184 if (func_type_add_arg_types(*dest, argcount,
2185 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002186 for (i = 0; i < argcount; ++i)
2187 common_type(type1->tt_args[i], type2->tt_args[i],
2188 &(*dest)->tt_args[i], type_gap);
2189 }
2190 }
2191 else
2192 *dest = alloc_func_type(common, -1, type_gap);
2193 return;
2194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 }
2196
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002197 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198}
2199
2200 char *
2201vartype_name(vartype_T type)
2202{
2203 switch (type)
2204 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002205 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002206 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 case VAR_SPECIAL: return "special";
2209 case VAR_BOOL: return "bool";
2210 case VAR_NUMBER: return "number";
2211 case VAR_FLOAT: return "float";
2212 case VAR_STRING: return "string";
2213 case VAR_BLOB: return "blob";
2214 case VAR_JOB: return "job";
2215 case VAR_CHANNEL: return "channel";
2216 case VAR_LIST: return "list";
2217 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002218
2219 case VAR_FUNC:
2220 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002222 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223}
2224
2225/*
2226 * Return the name of a type.
2227 * The result may be in allocated memory, in which case "tofree" is set.
2228 */
2229 char *
2230type_name(type_T *type, char **tofree)
2231{
2232 char *name = vartype_name(type->tt_type);
2233
2234 *tofree = NULL;
2235 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2236 {
2237 char *member_free;
2238 char *member_name = type_name(type->tt_member, &member_free);
2239 size_t len;
2240
2241 len = STRLEN(name) + STRLEN(member_name) + 3;
2242 *tofree = alloc(len);
2243 if (*tofree != NULL)
2244 {
2245 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2246 vim_free(member_free);
2247 return *tofree;
2248 }
2249 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002250 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002251 {
2252 garray_T ga;
2253 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002254 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002255
2256 ga_init2(&ga, 1, 100);
2257 if (ga_grow(&ga, 20) == FAIL)
2258 return "[unknown]";
2259 *tofree = ga.ga_data;
2260 STRCPY(ga.ga_data, "func(");
2261 ga.ga_len += 5;
2262
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002263 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002264 {
2265 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002266 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002267 int len;
2268
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002269 if (type->tt_args == NULL)
2270 arg_type = "[unknown]";
2271 else
2272 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002273 if (i > 0)
2274 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002275 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002276 ga.ga_len += 2;
2277 }
2278 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002279 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002280 {
2281 vim_free(arg_free);
2282 return "[unknown]";
2283 }
2284 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002285 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002286 {
2287 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2288 ga.ga_len += 3;
2289 }
2290 else if (i >= type->tt_min_argcount)
2291 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002292 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002293 ga.ga_len += len;
2294 vim_free(arg_free);
2295 }
2296
2297 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002298 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002299 else
2300 {
2301 char *ret_free;
2302 char *ret_name = type_name(type->tt_member, &ret_free);
2303 int len;
2304
2305 len = (int)STRLEN(ret_name) + 4;
2306 if (ga_grow(&ga, len) == FAIL)
2307 {
2308 vim_free(ret_free);
2309 return "[unknown]";
2310 }
2311 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002312 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2313 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002314 vim_free(ret_free);
2315 }
2316 return ga.ga_data;
2317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318
2319 return name;
2320}
2321
2322/*
2323 * Find "name" in script-local items of script "sid".
2324 * Returns the index in "sn_var_vals" if found.
2325 * If found but not in "sn_var_vals" returns -1.
2326 * If not found returns -2.
2327 */
2328 int
2329get_script_item_idx(int sid, char_u *name, int check_writable)
2330{
2331 hashtab_T *ht;
2332 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002333 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 int idx;
2335
2336 // First look the name up in the hashtable.
2337 if (sid <= 0 || sid > script_items.ga_len)
2338 return -1;
2339 ht = &SCRIPT_VARS(sid);
2340 di = find_var_in_ht(ht, 0, name, TRUE);
2341 if (di == NULL)
2342 return -2;
2343
2344 // Now find the svar_T index in sn_var_vals.
2345 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2346 {
2347 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2348
2349 if (sv->sv_tv == &di->di_tv)
2350 {
2351 if (check_writable && sv->sv_const)
2352 semsg(_(e_readonlyvar), name);
2353 return idx;
2354 }
2355 }
2356 return -1;
2357}
2358
2359/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002360 * Find "name" in imported items of the current script or in "cctx" if not
2361 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 */
2363 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002364find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002366 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367 int idx;
2368
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002369 if (current_sctx.sc_sid <= 0)
2370 return NULL;
2371 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 if (cctx != NULL)
2373 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2374 {
2375 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2376 + idx;
2377
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002378 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2379 : STRLEN(import->imp_name) == len
2380 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 return import;
2382 }
2383
2384 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2385 {
2386 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2387
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002388 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2389 : STRLEN(import->imp_name) == len
2390 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 return import;
2392 }
2393 return NULL;
2394}
2395
2396/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002397 * Free all imported variables.
2398 */
2399 static void
2400free_imported(cctx_T *cctx)
2401{
2402 int idx;
2403
2404 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2405 {
2406 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2407
2408 vim_free(import->imp_name);
2409 }
2410 ga_clear(&cctx->ctx_imports);
2411}
2412
2413/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002414 * Return TRUE if "p" points at a "#" but not at "#{".
2415 */
2416 static int
2417comment_start(char_u *p)
2418{
2419 return p[0] == '#' && p[1] != '{';
2420}
2421
2422/*
2423 * Return a pointer to the next line that isn't empty or only contains a
2424 * comment. Skips over white space.
2425 * Returns NULL if there is none.
2426 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002427 char_u *
2428peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002429{
2430 int lnum = cctx->ctx_lnum;
2431
2432 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2433 {
2434 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002435 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002436
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002437 if (line == NULL)
2438 break;
2439 p = skipwhite(line);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002440 if (*p != NUL && !comment_start(p))
2441 return p;
2442 }
2443 return NULL;
2444}
2445
2446/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002447 * Called when checking for a following operator at "arg". When the rest of
2448 * the line is empty or only a comment, peek the next line. If there is a next
2449 * line return a pointer to it and set "nextp".
2450 * Otherwise skip over white space.
2451 */
2452 static char_u *
2453may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2454{
2455 char_u *p = skipwhite(arg);
2456
2457 *nextp = NULL;
2458 if (*p == NUL || (VIM_ISWHITE(*arg) && comment_start(p)))
2459 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002460 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002461 if (*nextp != NULL)
2462 return *nextp;
2463 }
2464 return p;
2465}
2466
2467/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002468 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002469 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002470 * Returns NULL when at the end.
2471 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002472 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002473next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002474{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002475 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002476
2477 do
2478 {
2479 ++cctx->ctx_lnum;
2480 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002481 {
2482 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002483 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002484 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002485 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002486 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002487 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002488 } while (line == NULL || *skipwhite(line) == NUL
2489 || (skip_comment && comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002490 return line;
2491}
2492
2493/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002494 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002495 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002496 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2497 */
2498 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002499may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002500{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002501 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002502 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002503 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002504
2505 if (next == NULL)
2506 return FAIL;
2507 *arg = skipwhite(next);
2508 }
2509 return OK;
2510}
2511
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002512/*
2513 * Idem, and give an error when failed.
2514 */
2515 static int
2516may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2517{
2518 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2519 {
2520 emsg(_("E1097: line incomplete"));
2521 return FAIL;
2522 }
2523 return OK;
2524}
2525
2526
Bram Moolenaara5565e42020-05-09 15:44:01 +02002527// Structure passed between the compile_expr* functions to keep track of
2528// constants that have been parsed but for which no code was produced yet. If
2529// possible expressions on these constants are applied at compile time. If
2530// that is not possible, the code to push the constants needs to be generated
2531// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002532// Using 50 should be more than enough of 5 levels of ().
2533#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002534typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002535 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002536 int pp_used; // active entries in pp_tv[]
2537} ppconst_T;
2538
Bram Moolenaar1c747212020-05-09 18:28:34 +02002539static int compile_expr0(char_u **arg, cctx_T *cctx);
2540static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2541
Bram Moolenaara5565e42020-05-09 15:44:01 +02002542/*
2543 * Generate a PUSH instruction for "tv".
2544 * "tv" will be consumed or cleared.
2545 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2546 */
2547 static int
2548generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2549{
2550 if (tv != NULL)
2551 {
2552 switch (tv->v_type)
2553 {
2554 case VAR_UNKNOWN:
2555 break;
2556 case VAR_BOOL:
2557 generate_PUSHBOOL(cctx, tv->vval.v_number);
2558 break;
2559 case VAR_SPECIAL:
2560 generate_PUSHSPEC(cctx, tv->vval.v_number);
2561 break;
2562 case VAR_NUMBER:
2563 generate_PUSHNR(cctx, tv->vval.v_number);
2564 break;
2565#ifdef FEAT_FLOAT
2566 case VAR_FLOAT:
2567 generate_PUSHF(cctx, tv->vval.v_float);
2568 break;
2569#endif
2570 case VAR_BLOB:
2571 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2572 tv->vval.v_blob = NULL;
2573 break;
2574 case VAR_STRING:
2575 generate_PUSHS(cctx, tv->vval.v_string);
2576 tv->vval.v_string = NULL;
2577 break;
2578 default:
2579 iemsg("constant type not supported");
2580 clear_tv(tv);
2581 return FAIL;
2582 }
2583 tv->v_type = VAR_UNKNOWN;
2584 }
2585 return OK;
2586}
2587
2588/*
2589 * Generate code for any ppconst entries.
2590 */
2591 static int
2592generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2593{
2594 int i;
2595 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002596 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002597
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002598 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002599 for (i = 0; i < ppconst->pp_used; ++i)
2600 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2601 ret = FAIL;
2602 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002603 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002604 return ret;
2605}
2606
2607/*
2608 * Clear ppconst constants. Used when failing.
2609 */
2610 static void
2611clear_ppconst(ppconst_T *ppconst)
2612{
2613 int i;
2614
2615 for (i = 0; i < ppconst->pp_used; ++i)
2616 clear_tv(&ppconst->pp_tv[i]);
2617 ppconst->pp_used = 0;
2618}
2619
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002620/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002621 * Generate an instruction to load script-local variable "name", without the
2622 * leading "s:".
2623 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 */
2625 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002626compile_load_scriptvar(
2627 cctx_T *cctx,
2628 char_u *name, // variable NUL terminated
2629 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002630 char_u **end, // end of variable
2631 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002633 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2635 imported_T *import;
2636
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002637 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002639 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002640 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2641 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 }
2643 if (idx >= 0)
2644 {
2645 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2646
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002647 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 current_sctx.sc_sid, idx, sv->sv_type);
2649 return OK;
2650 }
2651
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002652 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 if (import != NULL)
2654 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002655 if (import->imp_all)
2656 {
2657 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002658 char_u *exp_name;
2659 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002660 ufunc_T *ufunc;
2661 type_T *type;
2662
2663 // Used "import * as Name", need to lookup the member.
2664 if (*p != '.')
2665 {
2666 semsg(_("E1060: expected dot after name: %s"), start);
2667 return FAIL;
2668 }
2669 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002670 if (VIM_ISWHITE(*p))
2671 {
2672 emsg(_("E1074: no white space allowed after dot"));
2673 return FAIL;
2674 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002675
Bram Moolenaar1c991142020-07-04 13:15:31 +02002676 // isolate one name
2677 exp_name = p;
2678 while (eval_isnamec(*p))
2679 ++p;
2680 cc = *p;
2681 *p = NUL;
2682
2683 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2684 *p = cc;
2685 p = skipwhite(p);
2686
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002687 // TODO: what if it is a function?
2688 if (idx < 0)
2689 return FAIL;
2690 *end = p;
2691
2692 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2693 import->imp_sid,
2694 idx,
2695 type);
2696 }
2697 else
2698 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002699 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002700 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2701 import->imp_sid,
2702 import->imp_var_vals_idx,
2703 import->imp_type);
2704 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 return OK;
2706 }
2707
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002708 if (error)
2709 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 return FAIL;
2711}
2712
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002713 static int
2714generate_funcref(cctx_T *cctx, char_u *name)
2715{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002716 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002717
2718 if (ufunc == NULL)
2719 return FAIL;
2720
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002721 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2722 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002723}
2724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725/*
2726 * Compile a variable name into a load instruction.
2727 * "end" points to just after the name.
2728 * When "error" is FALSE do not give an error when not found.
2729 */
2730 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002731compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732{
2733 type_T *type;
2734 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002735 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002737 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738
2739 if (*(*arg + 1) == ':')
2740 {
2741 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002742 if (end <= *arg + 2)
2743 name = vim_strsave((char_u *)"[empty]");
2744 else
2745 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 if (name == NULL)
2747 return FAIL;
2748
2749 if (**arg == 'v')
2750 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002751 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 }
2753 else if (**arg == 'g')
2754 {
2755 // Global variables can be defined later, thus we don't check if it
2756 // exists, give error at runtime.
2757 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2758 }
2759 else if (**arg == 's')
2760 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002761 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002763 else if (**arg == 'b')
2764 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002765 // Buffer-local variables can be defined later, thus we don't check
2766 // if it exists, give error at runtime.
2767 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002768 }
2769 else if (**arg == 'w')
2770 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002771 // Window-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_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002774 }
2775 else if (**arg == 't')
2776 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002777 // Tabpage-local variables can be defined later, thus we don't
2778 // check if it exists, give error at runtime.
2779 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 else
2782 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002783 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 goto theend;
2785 }
2786 }
2787 else
2788 {
2789 size_t len = end - *arg;
2790 int idx;
2791 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002792 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793
2794 name = vim_strnsave(*arg, end - *arg);
2795 if (name == NULL)
2796 return FAIL;
2797
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002798 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002800 if (!gen_load_outer)
2801 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 }
2803 else
2804 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002805 lvar_T *lvar = lookup_local(*arg, len, cctx);
2806
2807 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002809 type = lvar->lv_type;
2810 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002811 if (lvar->lv_from_outer)
2812 gen_load_outer = TRUE;
2813 else
2814 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 }
2816 else
2817 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002818 // "var" can be script-local even without using "s:" if it
2819 // already exists.
2820 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2821 == SCRIPT_VERSION_VIM9
2822 || lookup_script(*arg, len) == OK)
2823 res = compile_load_scriptvar(cctx, name, *arg, &end,
2824 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002825
Bram Moolenaara5565e42020-05-09 15:44:01 +02002826 // When the name starts with an uppercase letter or "x:" it
2827 // can be a user defined function.
2828 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2829 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 }
2831 }
2832 if (gen_load)
2833 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002834 if (gen_load_outer)
2835 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 }
2837
2838 *arg = end;
2839
2840theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002841 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 semsg(_(e_var_notfound), name);
2843 vim_free(name);
2844 return res;
2845}
2846
2847/*
2848 * Compile the argument expressions.
2849 * "arg" points to just after the "(" and is advanced to after the ")"
2850 */
2851 static int
2852compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2853{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002854 char_u *p = *arg;
2855 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856
Bram Moolenaare6085c52020-04-12 20:19:16 +02002857 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002859 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2860 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002861 if (*p == ')')
2862 {
2863 *arg = p + 1;
2864 return OK;
2865 }
2866
Bram Moolenaara5565e42020-05-09 15:44:01 +02002867 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 return FAIL;
2869 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002870
2871 if (*p != ',' && *skipwhite(p) == ',')
2872 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002873 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002874 p = skipwhite(p);
2875 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002877 {
2878 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002879 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002880 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002881 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002882 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002883 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002885failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002886 emsg(_(e_missing_close));
2887 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888}
2889
2890/*
2891 * Compile a function call: name(arg1, arg2)
2892 * "arg" points to "name", "arg + varlen" to the "(".
2893 * "argcount_init" is 1 for "value->method()"
2894 * Instructions:
2895 * EVAL arg1
2896 * EVAL arg2
2897 * BCALL / DCALL / UCALL
2898 */
2899 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002900compile_call(
2901 char_u **arg,
2902 size_t varlen,
2903 cctx_T *cctx,
2904 ppconst_T *ppconst,
2905 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906{
2907 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002908 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 int argcount = argcount_init;
2910 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002911 char_u fname_buf[FLEN_FIXED + 1];
2912 char_u *tofree = NULL;
2913 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002915 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916
Bram Moolenaara5565e42020-05-09 15:44:01 +02002917 // we can evaluate "has('name')" at compile time
2918 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2919 {
2920 char_u *s = skipwhite(*arg + varlen + 1);
2921 typval_T argvars[2];
2922
2923 argvars[0].v_type = VAR_UNKNOWN;
2924 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002925 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002926 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002927 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002928 s = skipwhite(s);
2929 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2930 {
2931 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2932
2933 *arg = s + 1;
2934 argvars[1].v_type = VAR_UNKNOWN;
2935 tv->v_type = VAR_NUMBER;
2936 tv->vval.v_number = 0;
2937 f_has(argvars, tv);
2938 clear_tv(&argvars[0]);
2939 ++ppconst->pp_used;
2940 return OK;
2941 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002942 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002943 }
2944
2945 if (generate_ppconst(cctx, ppconst) == FAIL)
2946 return FAIL;
2947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 if (varlen >= sizeof(namebuf))
2949 {
2950 semsg(_("E1011: name too long: %s"), name);
2951 return FAIL;
2952 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002953 vim_strncpy(namebuf, *arg, varlen);
2954 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955
2956 *arg = skipwhite(*arg + varlen + 1);
2957 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002958 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002960 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 {
2962 int idx;
2963
2964 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002965 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002967 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002968 else
2969 semsg(_(e_unknownfunc), namebuf);
2970 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 }
2972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002974 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002976 {
2977 res = generate_CALL(cctx, ufunc, argcount);
2978 goto theend;
2979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980
2981 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002982 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002984 if (STRNCMP(namebuf, "g:", 2) != 0
2985 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002986 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002987 garray_T *stack = &cctx->ctx_type_stack;
2988 type_T *type;
2989
2990 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2991 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002992 goto theend;
2993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002995 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002996 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002997 if (STRNCMP(namebuf, "g:", 2) == 0)
2998 res = generate_UCALL(cctx, name, argcount);
2999 else
3000 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003001
3002theend:
3003 vim_free(tofree);
3004 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005}
3006
3007// like NAMESPACE_CHAR but with 'a' and 'l'.
3008#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3009
3010/*
3011 * Find the end of a variable or function name. Unlike find_name_end() this
3012 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003013 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 * Return a pointer to just after the name. Equal to "arg" if there is no
3015 * valid name.
3016 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003017 static char_u *
3018to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019{
3020 char_u *p;
3021
3022 // Quick check for valid starting character.
3023 if (!eval_isnamec1(*arg))
3024 return arg;
3025
3026 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3027 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3028 // and can be used in slice "[n:]".
3029 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003030 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3032 break;
3033 return p;
3034}
3035
3036/*
3037 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003038 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 */
3040 char_u *
3041to_name_const_end(char_u *arg)
3042{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003043 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 typval_T rettv;
3045
3046 if (p == arg && *arg == '[')
3047 {
3048
3049 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003050 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 p = arg;
3052 }
3053 else if (p == arg && *arg == '#' && arg[1] == '{')
3054 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003055 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003057 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 p = arg;
3059 }
3060 else if (p == arg && *arg == '{')
3061 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003062 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003064 // Can be "{x -> ret}()".
3065 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003067 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 if (ret != OK)
3069 p = arg;
3070 }
3071
3072 return p;
3073}
3074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075/*
3076 * parse a list: [expr, expr]
3077 * "*arg" points to the '['.
3078 */
3079 static int
3080compile_list(char_u **arg, cctx_T *cctx)
3081{
3082 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003083 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 int count = 0;
3085
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003086 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003088 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003089 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003090 semsg(_(e_list_end), *arg);
3091 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003092 }
3093 if (*p == ']')
3094 {
3095 ++p;
3096 // Allow for following comment, after at least one space.
3097 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
3098 p += STRLEN(p);
3099 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003100 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003101 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 break;
3103 ++count;
3104 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003105 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003107 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3108 {
3109 semsg(_(e_white_after), ",");
3110 return FAIL;
3111 }
3112 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003113 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 p = skipwhite(p);
3115 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003116 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117
3118 generate_NEWLIST(cctx, count);
3119 return OK;
3120}
3121
3122/*
3123 * parse a lambda: {arg, arg -> expr}
3124 * "*arg" points to the '{'.
3125 */
3126 static int
3127compile_lambda(char_u **arg, cctx_T *cctx)
3128{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 typval_T rettv;
3130 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003131 evalarg_T evalarg;
3132
3133 CLEAR_FIELD(evalarg);
3134 evalarg.eval_flags = EVAL_EVALUATE;
3135 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136
3137 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003138 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003142 ++ufunc->uf_refcount;
3143 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003144 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145
3146 // The function will have one line: "return {expr}".
3147 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003148 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003150 clear_evalarg(&evalarg, NULL);
3151
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003152 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003153 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 return FAIL;
3155}
3156
3157/*
3158 * Compile a lamda call: expr->{lambda}(args)
3159 * "arg" points to the "{".
3160 */
3161 static int
3162compile_lambda_call(char_u **arg, cctx_T *cctx)
3163{
3164 ufunc_T *ufunc;
3165 typval_T rettv;
3166 int argcount = 1;
3167 int ret = FAIL;
3168
3169 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003170 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 return FAIL;
3172
3173 if (**arg != '(')
3174 {
3175 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003176 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 else
3178 semsg(_(e_missing_paren), "lambda");
3179 clear_tv(&rettv);
3180 return FAIL;
3181 }
3182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 ufunc = rettv.vval.v_partial->pt_func;
3184 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003185 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003186 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003187
3188 // The function will have one line: "return {expr}".
3189 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003190 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191
3192 // compile the arguments
3193 *arg = skipwhite(*arg + 1);
3194 if (compile_arguments(arg, cctx, &argcount) == OK)
3195 // call the compiled function
3196 ret = generate_CALL(cctx, ufunc, argcount);
3197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 return ret;
3199}
3200
3201/*
3202 * parse a dict: {'key': val} or #{key: val}
3203 * "*arg" points to the '{'.
3204 */
3205 static int
3206compile_dict(char_u **arg, cctx_T *cctx, int literal)
3207{
3208 garray_T *instr = &cctx->ctx_instr;
3209 int count = 0;
3210 dict_T *d = dict_alloc();
3211 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003212 char_u *whitep = *arg;
3213 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214
3215 if (d == NULL)
3216 return FAIL;
3217 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003218 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 {
3220 char_u *key = NULL;
3221
Bram Moolenaar23c55272020-06-21 16:58:13 +02003222 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003223 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003224 *arg = NULL;
3225 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003226 }
3227
3228 if (**arg == '}')
3229 break;
3230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 if (literal)
3232 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003233 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234
Bram Moolenaar2c330432020-04-13 14:41:35 +02003235 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 {
3237 semsg(_("E1014: Invalid key: %s"), *arg);
3238 return FAIL;
3239 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003240 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 if (generate_PUSHS(cctx, key) == FAIL)
3242 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003243 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 }
3245 else
3246 {
3247 isn_T *isn;
3248
Bram Moolenaara5565e42020-05-09 15:44:01 +02003249 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 return FAIL;
3251 // TODO: check type is string
3252 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3253 if (isn->isn_type == ISN_PUSHS)
3254 key = isn->isn_arg.string;
3255 }
3256
3257 // Check for duplicate keys, if using string keys.
3258 if (key != NULL)
3259 {
3260 item = dict_find(d, key, -1);
3261 if (item != NULL)
3262 {
3263 semsg(_(e_duplicate_key), key);
3264 goto failret;
3265 }
3266 item = dictitem_alloc(key);
3267 if (item != NULL)
3268 {
3269 item->di_tv.v_type = VAR_UNKNOWN;
3270 item->di_tv.v_lock = 0;
3271 if (dict_add(d, item) == FAIL)
3272 dictitem_free(item);
3273 }
3274 }
3275
3276 *arg = skipwhite(*arg);
3277 if (**arg != ':')
3278 {
3279 semsg(_(e_missing_dict_colon), *arg);
3280 return FAIL;
3281 }
3282
Bram Moolenaar2c330432020-04-13 14:41:35 +02003283 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003285 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003286 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003287 *arg = NULL;
3288 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003289 }
3290
Bram Moolenaara5565e42020-05-09 15:44:01 +02003291 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 return FAIL;
3293 ++count;
3294
Bram Moolenaar2c330432020-04-13 14:41:35 +02003295 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003296 *arg = skipwhite(*arg);
3297 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003298 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003299 *arg = NULL;
3300 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003301 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 if (**arg == '}')
3303 break;
3304 if (**arg != ',')
3305 {
3306 semsg(_(e_missing_dict_comma), *arg);
3307 goto failret;
3308 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003309 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 *arg = skipwhite(*arg + 1);
3311 }
3312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 *arg = *arg + 1;
3314
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003315 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003316 p = skipwhite(*arg);
3317 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003318 *arg += STRLEN(*arg);
3319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 dict_unref(d);
3321 return generate_NEWDICT(cctx, count);
3322
3323failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003324 if (*arg == NULL)
3325 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 dict_unref(d);
3327 return FAIL;
3328}
3329
3330/*
3331 * Compile "&option".
3332 */
3333 static int
3334compile_get_option(char_u **arg, cctx_T *cctx)
3335{
3336 typval_T rettv;
3337 char_u *start = *arg;
3338 int ret;
3339
3340 // parse the option and get the current value to get the type.
3341 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003342 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 if (ret == OK)
3344 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003345 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 char_u *name = vim_strnsave(start, *arg - start);
3347 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3348
3349 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3350 vim_free(name);
3351 }
3352 clear_tv(&rettv);
3353
3354 return ret;
3355}
3356
3357/*
3358 * Compile "$VAR".
3359 */
3360 static int
3361compile_get_env(char_u **arg, cctx_T *cctx)
3362{
3363 char_u *start = *arg;
3364 int len;
3365 int ret;
3366 char_u *name;
3367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 ++*arg;
3369 len = get_env_len(arg);
3370 if (len == 0)
3371 {
3372 semsg(_(e_syntax_at), start - 1);
3373 return FAIL;
3374 }
3375
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003376 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 name = vim_strnsave(start, len + 1);
3378 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3379 vim_free(name);
3380 return ret;
3381}
3382
3383/*
3384 * Compile "@r".
3385 */
3386 static int
3387compile_get_register(char_u **arg, cctx_T *cctx)
3388{
3389 int ret;
3390
3391 ++*arg;
3392 if (**arg == NUL)
3393 {
3394 semsg(_(e_syntax_at), *arg - 1);
3395 return FAIL;
3396 }
3397 if (!valid_yank_reg(**arg, TRUE))
3398 {
3399 emsg_invreg(**arg);
3400 return FAIL;
3401 }
3402 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3403 ++*arg;
3404 return ret;
3405}
3406
3407/*
3408 * Apply leading '!', '-' and '+' to constant "rettv".
3409 */
3410 static int
3411apply_leader(typval_T *rettv, char_u *start, char_u *end)
3412{
3413 char_u *p = end;
3414
3415 // this works from end to start
3416 while (p > start)
3417 {
3418 --p;
3419 if (*p == '-' || *p == '+')
3420 {
3421 // only '-' has an effect, for '+' we only check the type
3422#ifdef FEAT_FLOAT
3423 if (rettv->v_type == VAR_FLOAT)
3424 {
3425 if (*p == '-')
3426 rettv->vval.v_float = -rettv->vval.v_float;
3427 }
3428 else
3429#endif
3430 {
3431 varnumber_T val;
3432 int error = FALSE;
3433
3434 // tv_get_number_chk() accepts a string, but we don't want that
3435 // here
3436 if (check_not_string(rettv) == FAIL)
3437 return FAIL;
3438 val = tv_get_number_chk(rettv, &error);
3439 clear_tv(rettv);
3440 if (error)
3441 return FAIL;
3442 if (*p == '-')
3443 val = -val;
3444 rettv->v_type = VAR_NUMBER;
3445 rettv->vval.v_number = val;
3446 }
3447 }
3448 else
3449 {
3450 int v = tv2bool(rettv);
3451
3452 // '!' is permissive in the type.
3453 clear_tv(rettv);
3454 rettv->v_type = VAR_BOOL;
3455 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3456 }
3457 }
3458 return OK;
3459}
3460
3461/*
3462 * Recognize v: variables that are constants and set "rettv".
3463 */
3464 static void
3465get_vim_constant(char_u **arg, typval_T *rettv)
3466{
3467 if (STRNCMP(*arg, "v:true", 6) == 0)
3468 {
3469 rettv->v_type = VAR_BOOL;
3470 rettv->vval.v_number = VVAL_TRUE;
3471 *arg += 6;
3472 }
3473 else if (STRNCMP(*arg, "v:false", 7) == 0)
3474 {
3475 rettv->v_type = VAR_BOOL;
3476 rettv->vval.v_number = VVAL_FALSE;
3477 *arg += 7;
3478 }
3479 else if (STRNCMP(*arg, "v:null", 6) == 0)
3480 {
3481 rettv->v_type = VAR_SPECIAL;
3482 rettv->vval.v_number = VVAL_NULL;
3483 *arg += 6;
3484 }
3485 else if (STRNCMP(*arg, "v:none", 6) == 0)
3486 {
3487 rettv->v_type = VAR_SPECIAL;
3488 rettv->vval.v_number = VVAL_NONE;
3489 *arg += 6;
3490 }
3491}
3492
Bram Moolenaar61a89812020-05-07 16:58:17 +02003493 static exptype_T
3494get_compare_type(char_u *p, int *len, int *type_is)
3495{
3496 exptype_T type = EXPR_UNKNOWN;
3497 int i;
3498
3499 switch (p[0])
3500 {
3501 case '=': if (p[1] == '=')
3502 type = EXPR_EQUAL;
3503 else if (p[1] == '~')
3504 type = EXPR_MATCH;
3505 break;
3506 case '!': if (p[1] == '=')
3507 type = EXPR_NEQUAL;
3508 else if (p[1] == '~')
3509 type = EXPR_NOMATCH;
3510 break;
3511 case '>': if (p[1] != '=')
3512 {
3513 type = EXPR_GREATER;
3514 *len = 1;
3515 }
3516 else
3517 type = EXPR_GEQUAL;
3518 break;
3519 case '<': if (p[1] != '=')
3520 {
3521 type = EXPR_SMALLER;
3522 *len = 1;
3523 }
3524 else
3525 type = EXPR_SEQUAL;
3526 break;
3527 case 'i': if (p[1] == 's')
3528 {
3529 // "is" and "isnot"; but not a prefix of a name
3530 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3531 *len = 5;
3532 i = p[*len];
3533 if (!isalnum(i) && i != '_')
3534 {
3535 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3536 *type_is = TRUE;
3537 }
3538 }
3539 break;
3540 }
3541 return type;
3542}
3543
3544/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 * Compile code to apply '-', '+' and '!'.
3546 */
3547 static int
3548compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3549{
3550 char_u *p = end;
3551
3552 // this works from end to start
3553 while (p > start)
3554 {
3555 --p;
3556 if (*p == '-' || *p == '+')
3557 {
3558 int negate = *p == '-';
3559 isn_T *isn;
3560
3561 // TODO: check type
3562 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3563 {
3564 --p;
3565 if (*p == '-')
3566 negate = !negate;
3567 }
3568 // only '-' has an effect, for '+' we only check the type
3569 if (negate)
3570 isn = generate_instr(cctx, ISN_NEGATENR);
3571 else
3572 isn = generate_instr(cctx, ISN_CHECKNR);
3573 if (isn == NULL)
3574 return FAIL;
3575 }
3576 else
3577 {
3578 int invert = TRUE;
3579
3580 while (p > start && p[-1] == '!')
3581 {
3582 --p;
3583 invert = !invert;
3584 }
3585 if (generate_2BOOL(cctx, invert) == FAIL)
3586 return FAIL;
3587 }
3588 }
3589 return OK;
3590}
3591
3592/*
3593 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003594 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 */
3596 static int
3597compile_subscript(
3598 char_u **arg,
3599 cctx_T *cctx,
3600 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003601 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003602 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603{
3604 for (;;)
3605 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003606 char_u *p = skipwhite(*arg);
3607
3608 if (*p == NUL || (VIM_ISWHITE(**arg) && comment_start(p)))
3609 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003610 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003611
3612 // If a following line starts with "->{" or "->X" advance to that
3613 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003614 // Also if a following line starts with ".x".
3615 if (next != NULL &&
3616 ((next[0] == '-' && next[1] == '>'
3617 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3618 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003619 {
3620 next = next_line_from_context(cctx, TRUE);
3621 if (next == NULL)
3622 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003623 *arg = next;
3624 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003625 }
3626 }
3627
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003628 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003630 garray_T *stack = &cctx->ctx_type_stack;
3631 type_T *type;
3632 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003634 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003635 return FAIL;
3636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003638 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3639
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003640 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3642 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003643 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 return FAIL;
3645 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003646 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003648 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003649 return FAIL;
3650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 // something->method()
3652 // Apply the '!', '-' and '+' first:
3653 // -1.0->func() works like (-1.0)->func()
3654 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3655 return FAIL;
3656 *start_leader = end_leader; // don't apply again later
3657
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003658 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003659 *arg = skipwhite(p);
3660 if (may_get_next_line(p, arg, cctx) == FAIL)
3661 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 if (**arg == '{')
3663 {
3664 // lambda call: list->{lambda}
3665 if (compile_lambda_call(arg, cctx) == FAIL)
3666 return FAIL;
3667 }
3668 else
3669 {
3670 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003671 p = *arg;
3672 if (ASCII_ISALPHA(*p) && p[1] == ':')
3673 p += 2;
3674 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 ;
3676 if (*p != '(')
3677 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003678 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 return FAIL;
3680 }
3681 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003682 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 return FAIL;
3684 }
3685 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003686 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003688 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003689 type_T **typep;
3690
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003691 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003692 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003693 // TODO: blob index
3694 // TODO: more arguments
3695 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003696 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003697 return FAIL;
3698
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003699 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003700 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003701 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003702 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003703 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 return FAIL;
3705
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003706 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3707 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 if (**arg != ']')
3709 {
3710 emsg(_(e_missbrac));
3711 return FAIL;
3712 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003713 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003715 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3716 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003717 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003718 if ((*typep)->tt_type == VAR_LIST)
3719 *typep = (*typep)->tt_member;
3720 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3721 return FAIL;
3722 }
3723 else if ((*typep)->tt_type == VAR_DICT)
3724 {
3725 *typep = (*typep)->tt_member;
3726 if (may_generate_2STRING(-1, cctx) == FAIL)
3727 return FAIL;
3728 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3729 return FAIL;
3730 }
3731 else
3732 {
3733 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003734 return FAIL;
3735 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003737 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003739 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003740 return FAIL;
3741
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003742 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003743 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3744 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003746 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 if (eval_isnamec1(*p))
3748 while (eval_isnamec(*p))
3749 MB_PTR_ADV(p);
3750 if (p == *arg)
3751 {
3752 semsg(_(e_syntax_at), *arg);
3753 return FAIL;
3754 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003755 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 return FAIL;
3757 *arg = p;
3758 }
3759 else
3760 break;
3761 }
3762
3763 // TODO - see handle_subscript():
3764 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3765 // Don't do this when "Func" is already a partial that was bound
3766 // explicitly (pt_auto is FALSE).
3767
3768 return OK;
3769}
3770
3771/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003772 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3773 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003775 * If the value is a constant "ppconst->pp_ret" will be set.
3776 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003777 *
3778 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 */
3780
3781/*
3782 * number number constant
3783 * 0zFFFFFFFF Blob constant
3784 * "string" string constant
3785 * 'string' literal string constant
3786 * &option-name option value
3787 * @r register contents
3788 * identifier variable value
3789 * function() function call
3790 * $VAR environment variable
3791 * (expression) nested expression
3792 * [expr, expr] List
3793 * {key: val, key: val} Dictionary
3794 * #{key: val, key: val} Dictionary with literal keys
3795 *
3796 * Also handle:
3797 * ! in front logical NOT
3798 * - in front unary minus
3799 * + in front unary plus (ignored)
3800 * trailing (arg) funcref/partial call
3801 * trailing [] subscript in String or List
3802 * trailing .name entry in Dictionary
3803 * trailing ->name() method call
3804 */
3805 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003806compile_expr7(
3807 char_u **arg,
3808 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003809 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 char_u *start_leader, *end_leader;
3812 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003813 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003814 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815
3816 /*
3817 * Skip '!', '-' and '+' characters. They are handled later.
3818 */
3819 start_leader = *arg;
3820 while (**arg == '!' || **arg == '-' || **arg == '+')
3821 *arg = skipwhite(*arg + 1);
3822 end_leader = *arg;
3823
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003824 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 switch (**arg)
3826 {
3827 /*
3828 * Number constant.
3829 */
3830 case '0': // also for blob starting with 0z
3831 case '1':
3832 case '2':
3833 case '3':
3834 case '4':
3835 case '5':
3836 case '6':
3837 case '7':
3838 case '8':
3839 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003840 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 return FAIL;
3842 break;
3843
3844 /*
3845 * String constant: "string".
3846 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003847 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 return FAIL;
3849 break;
3850
3851 /*
3852 * Literal string constant: 'str''ing'.
3853 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003854 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 return FAIL;
3856 break;
3857
3858 /*
3859 * Constant Vim variable.
3860 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003861 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 ret = NOTDONE;
3863 break;
3864
3865 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003866 * "true" constant
3867 */
3868 case 't': if (STRNCMP(*arg, "true", 4) == 0
3869 && !eval_isnamec((*arg)[4]))
3870 {
3871 *arg += 4;
3872 rettv->v_type = VAR_BOOL;
3873 rettv->vval.v_number = VVAL_TRUE;
3874 }
3875 else
3876 ret = NOTDONE;
3877 break;
3878
3879 /*
3880 * "false" constant
3881 */
3882 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3883 && !eval_isnamec((*arg)[5]))
3884 {
3885 *arg += 5;
3886 rettv->v_type = VAR_BOOL;
3887 rettv->vval.v_number = VVAL_FALSE;
3888 }
3889 else
3890 ret = NOTDONE;
3891 break;
3892
3893 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 * List: [expr, expr]
3895 */
3896 case '[': ret = compile_list(arg, cctx);
3897 break;
3898
3899 /*
3900 * Dictionary: #{key: val, key: val}
3901 */
3902 case '#': if ((*arg)[1] == '{')
3903 {
3904 ++*arg;
3905 ret = compile_dict(arg, cctx, TRUE);
3906 }
3907 else
3908 ret = NOTDONE;
3909 break;
3910
3911 /*
3912 * Lambda: {arg, arg -> expr}
3913 * Dictionary: {'key': val, 'key': val}
3914 */
3915 case '{': {
3916 char_u *start = skipwhite(*arg + 1);
3917
3918 // Find out what comes after the arguments.
3919 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003920 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 if (ret != FAIL && *start == '>')
3922 ret = compile_lambda(arg, cctx);
3923 else
3924 ret = compile_dict(arg, cctx, FALSE);
3925 }
3926 break;
3927
3928 /*
3929 * Option value: &name
3930 */
3931 case '&': ret = compile_get_option(arg, cctx);
3932 break;
3933
3934 /*
3935 * Environment variable: $VAR.
3936 */
3937 case '$': ret = compile_get_env(arg, cctx);
3938 break;
3939
3940 /*
3941 * Register contents: @r.
3942 */
3943 case '@': ret = compile_get_register(arg, cctx);
3944 break;
3945 /*
3946 * nested expression: (expression).
3947 */
3948 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003949
3950 // recursive!
3951 if (ppconst->pp_used <= PPSIZE - 10)
3952 {
3953 ret = compile_expr1(arg, cctx, ppconst);
3954 }
3955 else
3956 {
3957 // Not enough space in ppconst, flush constants.
3958 if (generate_ppconst(cctx, ppconst) == FAIL)
3959 return FAIL;
3960 ret = compile_expr0(arg, cctx);
3961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 *arg = skipwhite(*arg);
3963 if (**arg == ')')
3964 ++*arg;
3965 else if (ret == OK)
3966 {
3967 emsg(_(e_missing_close));
3968 ret = FAIL;
3969 }
3970 break;
3971
3972 default: ret = NOTDONE;
3973 break;
3974 }
3975 if (ret == FAIL)
3976 return FAIL;
3977
Bram Moolenaar1c747212020-05-09 18:28:34 +02003978 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 {
3980 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003981 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003983 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 return FAIL;
3985 }
3986 start_leader = end_leader; // don't apply again below
3987
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003988 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003989 clear_tv(rettv);
3990 else
3991 // A constant expression can possibly be handled compile time,
3992 // return the value instead of generating code.
3993 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 }
3995 else if (ret == NOTDONE)
3996 {
3997 char_u *p;
3998 int r;
3999
4000 if (!eval_isnamec1(**arg))
4001 {
4002 semsg(_("E1015: Name expected: %s"), *arg);
4003 return FAIL;
4004 }
4005
4006 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004007 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004009 {
4010 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004013 {
4014 if (generate_ppconst(cctx, ppconst) == FAIL)
4015 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004017 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 if (r == FAIL)
4019 return FAIL;
4020 }
4021
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004022 // Handle following "[]", ".member", etc.
4023 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004024 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004025 ppconst) == FAIL)
4026 return FAIL;
4027 if (ppconst->pp_used > 0)
4028 {
4029 // apply the '!', '-' and '+' before the constant
4030 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4031 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4032 return FAIL;
4033 return OK;
4034 }
4035 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004037 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038}
4039
4040/*
4041 * * number multiplication
4042 * / number division
4043 * % number modulo
4044 */
4045 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004046compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047{
4048 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004049 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004050 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004052 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004053 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 return FAIL;
4055
4056 /*
4057 * Repeat computing, until no "*", "/" or "%" is following.
4058 */
4059 for (;;)
4060 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004061 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 if (*op != '*' && *op != '/' && *op != '%')
4063 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004064 if (next != NULL)
4065 {
4066 *arg = next_line_from_context(cctx, TRUE);
4067 op = skipwhite(*arg);
4068 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004069
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004070 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 {
4072 char_u buf[3];
4073
4074 vim_strncpy(buf, op, 1);
4075 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004076 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 }
4078 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004079 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004080 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004082 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004083 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004085
4086 if (ppconst->pp_used == ppconst_used + 2
4087 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4088 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004089 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004090 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4091 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004092 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004094 // both are numbers: compute the result
4095 switch (*op)
4096 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004097 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004098 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004099 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004100 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004101 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004102 break;
4103 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004104 tv1->vval.v_number = res;
4105 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004106 }
4107 else
4108 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004109 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004110 generate_two_op(cctx, op);
4111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 }
4113
4114 return OK;
4115}
4116
4117/*
4118 * + number addition
4119 * - number subtraction
4120 * .. string concatenation
4121 */
4122 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004123compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124{
4125 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004126 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004128 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129
4130 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004131 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 return FAIL;
4133
4134 /*
4135 * Repeat computing, until no "+", "-" or ".." is following.
4136 */
4137 for (;;)
4138 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004139 op = may_peek_next_line(cctx, *arg, &next);
4140 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 break;
4142 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004143 if (next != NULL)
4144 {
4145 *arg = next_line_from_context(cctx, TRUE);
4146 op = skipwhite(*arg);
4147 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004149 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 {
4151 char_u buf[3];
4152
4153 vim_strncpy(buf, op, oplen);
4154 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004155 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 }
4157
4158 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004159 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004160 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004162 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004163 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 return FAIL;
4165
Bram Moolenaara5565e42020-05-09 15:44:01 +02004166 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004167 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004168 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4169 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4170 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4171 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004173 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4174 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004175
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004176 // concat/subtract/add constant numbers
4177 if (*op == '+')
4178 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4179 else if (*op == '-')
4180 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4181 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004182 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004183 // concatenate constant strings
4184 char_u *s1 = tv1->vval.v_string;
4185 char_u *s2 = tv2->vval.v_string;
4186 size_t len1 = STRLEN(s1);
4187
4188 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4189 if (tv1->vval.v_string == NULL)
4190 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004191 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004192 return FAIL;
4193 }
4194 mch_memmove(tv1->vval.v_string, s1, len1);
4195 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004196 vim_free(s1);
4197 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004198 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004199 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 }
4201 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004202 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004203 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004204 if (*op == '.')
4205 {
4206 if (may_generate_2STRING(-2, cctx) == FAIL
4207 || may_generate_2STRING(-1, cctx) == FAIL)
4208 return FAIL;
4209 generate_instr_drop(cctx, ISN_CONCAT, 1);
4210 }
4211 else
4212 generate_two_op(cctx, op);
4213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 }
4215
4216 return OK;
4217}
4218
4219/*
4220 * expr5a == expr5b
4221 * expr5a =~ expr5b
4222 * expr5a != expr5b
4223 * expr5a !~ expr5b
4224 * expr5a > expr5b
4225 * expr5a >= expr5b
4226 * expr5a < expr5b
4227 * expr5a <= expr5b
4228 * expr5a is expr5b
4229 * expr5a isnot expr5b
4230 *
4231 * Produces instructions:
4232 * EVAL expr5a Push result of "expr5a"
4233 * EVAL expr5b Push result of "expr5b"
4234 * COMPARE one of the compare instructions
4235 */
4236 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004237compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238{
4239 exptype_T type = EXPR_UNKNOWN;
4240 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004241 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004244 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245
4246 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004247 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 return FAIL;
4249
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004250 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004251 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252
4253 /*
4254 * If there is a comparative operator, use it.
4255 */
4256 if (type != EXPR_UNKNOWN)
4257 {
4258 int ic = FALSE; // Default: do not ignore case
4259
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004260 if (next != NULL)
4261 {
4262 *arg = next_line_from_context(cctx, TRUE);
4263 p = skipwhite(*arg);
4264 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 if (type_is && (p[len] == '?' || p[len] == '#'))
4266 {
4267 semsg(_(e_invexpr2), *arg);
4268 return FAIL;
4269 }
4270 // extra question mark appended: ignore case
4271 if (p[len] == '?')
4272 {
4273 ic = TRUE;
4274 ++len;
4275 }
4276 // extra '#' appended: match case (ignored)
4277 else if (p[len] == '#')
4278 ++len;
4279 // nothing appended: match case
4280
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004281 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 {
4283 char_u buf[7];
4284
4285 vim_strncpy(buf, p, len);
4286 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004287 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 }
4289
4290 // get the second variable
4291 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004292 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004293 return FAIL;
4294
Bram Moolenaara5565e42020-05-09 15:44:01 +02004295 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 return FAIL;
4297
Bram Moolenaara5565e42020-05-09 15:44:01 +02004298 if (ppconst->pp_used == ppconst_used + 2)
4299 {
4300 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4301 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4302 int ret;
4303
4304 // Both sides are a constant, compute the result now.
4305 // First check for a valid combination of types, this is more
4306 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004307 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004308 ret = FAIL;
4309 else
4310 {
4311 ret = typval_compare(tv1, tv2, type, ic);
4312 tv1->v_type = VAR_BOOL;
4313 tv1->vval.v_number = tv1->vval.v_number
4314 ? VVAL_TRUE : VVAL_FALSE;
4315 clear_tv(tv2);
4316 --ppconst->pp_used;
4317 }
4318 return ret;
4319 }
4320
4321 generate_ppconst(cctx, ppconst);
4322 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 }
4324
4325 return OK;
4326}
4327
Bram Moolenaar7f141552020-05-09 17:35:53 +02004328static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330/*
4331 * Compile || or &&.
4332 */
4333 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004334compile_and_or(
4335 char_u **arg,
4336 cctx_T *cctx,
4337 char *op,
4338 ppconst_T *ppconst,
4339 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004341 char_u *next;
4342 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 int opchar = *op;
4344
4345 if (p[0] == opchar && p[1] == opchar)
4346 {
4347 garray_T *instr = &cctx->ctx_instr;
4348 garray_T end_ga;
4349
4350 /*
4351 * Repeat until there is no following "||" or "&&"
4352 */
4353 ga_init2(&end_ga, sizeof(int), 10);
4354 while (p[0] == opchar && p[1] == opchar)
4355 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004356 if (next != NULL)
4357 {
4358 *arg = next_line_from_context(cctx, TRUE);
4359 p = skipwhite(*arg);
4360 }
4361
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004362 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4363 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004365 return FAIL;
4366 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367
Bram Moolenaara5565e42020-05-09 15:44:01 +02004368 // TODO: use ppconst if the value is a constant
4369 generate_ppconst(cctx, ppconst);
4370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 if (ga_grow(&end_ga, 1) == FAIL)
4372 {
4373 ga_clear(&end_ga);
4374 return FAIL;
4375 }
4376 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4377 ++end_ga.ga_len;
4378 generate_JUMP(cctx, opchar == '|'
4379 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4380
4381 // eval the next expression
4382 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004383 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004384 return FAIL;
4385
Bram Moolenaara5565e42020-05-09 15:44:01 +02004386 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4387 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 {
4389 ga_clear(&end_ga);
4390 return FAIL;
4391 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004392
4393 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004395 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396
4397 // Fill in the end label in all jumps.
4398 while (end_ga.ga_len > 0)
4399 {
4400 isn_T *isn;
4401
4402 --end_ga.ga_len;
4403 isn = ((isn_T *)instr->ga_data)
4404 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4405 isn->isn_arg.jump.jump_where = instr->ga_len;
4406 }
4407 ga_clear(&end_ga);
4408 }
4409
4410 return OK;
4411}
4412
4413/*
4414 * expr4a && expr4a && expr4a logical AND
4415 *
4416 * Produces instructions:
4417 * EVAL expr4a Push result of "expr4a"
4418 * JUMP_AND_KEEP_IF_FALSE end
4419 * EVAL expr4b Push result of "expr4b"
4420 * JUMP_AND_KEEP_IF_FALSE end
4421 * EVAL expr4c Push result of "expr4c"
4422 * end:
4423 */
4424 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004425compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004427 int ppconst_used = ppconst->pp_used;
4428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004430 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 return FAIL;
4432
4433 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004434 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435}
4436
4437/*
4438 * expr3a || expr3b || expr3c logical OR
4439 *
4440 * Produces instructions:
4441 * EVAL expr3a Push result of "expr3a"
4442 * JUMP_AND_KEEP_IF_TRUE end
4443 * EVAL expr3b Push result of "expr3b"
4444 * JUMP_AND_KEEP_IF_TRUE end
4445 * EVAL expr3c Push result of "expr3c"
4446 * end:
4447 */
4448 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004449compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004451 int ppconst_used = ppconst->pp_used;
4452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004454 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 return FAIL;
4456
4457 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004458 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459}
4460
4461/*
4462 * Toplevel expression: expr2 ? expr1a : expr1b
4463 *
4464 * Produces instructions:
4465 * EVAL expr2 Push result of "expr"
4466 * JUMP_IF_FALSE alt jump if false
4467 * EVAL expr1a
4468 * JUMP_ALWAYS end
4469 * alt: EVAL expr1b
4470 * end:
4471 */
4472 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004473compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474{
4475 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004476 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004477 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478
Bram Moolenaar61a89812020-05-07 16:58:17 +02004479 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004480 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 return FAIL;
4482
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004483 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 if (*p == '?')
4485 {
4486 garray_T *instr = &cctx->ctx_instr;
4487 garray_T *stack = &cctx->ctx_type_stack;
4488 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004489 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004491 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004493 int has_const_expr = FALSE;
4494 int const_value = FALSE;
4495 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004497 if (next != NULL)
4498 {
4499 *arg = next_line_from_context(cctx, TRUE);
4500 p = skipwhite(*arg);
4501 }
4502
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004503 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4504 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004506 return FAIL;
4507 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508
Bram Moolenaara5565e42020-05-09 15:44:01 +02004509 if (ppconst->pp_used == ppconst_used + 1)
4510 {
4511 // the condition is a constant, we know whether the ? or the :
4512 // expression is to be evaluated.
4513 has_const_expr = TRUE;
4514 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4515 clear_tv(&ppconst->pp_tv[ppconst_used]);
4516 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004517 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4518 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004519 }
4520 else
4521 {
4522 generate_ppconst(cctx, ppconst);
4523 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525
4526 // evaluate the second expression; any type is accepted
4527 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004528 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004529 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004530 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004531 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532
Bram Moolenaara5565e42020-05-09 15:44:01 +02004533 if (!has_const_expr)
4534 {
4535 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536
Bram Moolenaara5565e42020-05-09 15:44:01 +02004537 // remember the type and drop it
4538 --stack->ga_len;
4539 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540
Bram Moolenaara5565e42020-05-09 15:44:01 +02004541 end_idx = instr->ga_len;
4542 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4543
4544 // jump here from JUMP_IF_FALSE
4545 isn = ((isn_T *)instr->ga_data) + alt_idx;
4546 isn->isn_arg.jump.jump_where = instr->ga_len;
4547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548
4549 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004550 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 if (*p != ':')
4552 {
4553 emsg(_(e_missing_colon));
4554 return FAIL;
4555 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004556 if (next != NULL)
4557 {
4558 *arg = next_line_from_context(cctx, TRUE);
4559 p = skipwhite(*arg);
4560 }
4561
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004562 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4563 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004565 return FAIL;
4566 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567
4568 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004570 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4571 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004573 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004574 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004575 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004576 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577
Bram Moolenaara5565e42020-05-09 15:44:01 +02004578 if (!has_const_expr)
4579 {
4580 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581
Bram Moolenaara5565e42020-05-09 15:44:01 +02004582 // If the types differ, the result has a more generic type.
4583 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4584 common_type(type1, type2, &type2, cctx->ctx_type_list);
4585
4586 // jump here from JUMP_ALWAYS
4587 isn = ((isn_T *)instr->ga_data) + end_idx;
4588 isn->isn_arg.jump.jump_where = instr->ga_len;
4589 }
4590
4591 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 }
4593 return OK;
4594}
4595
4596/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597 * Toplevel expression.
4598 */
4599 static int
4600compile_expr0(char_u **arg, cctx_T *cctx)
4601{
4602 ppconst_T ppconst;
4603
4604 CLEAR_FIELD(ppconst);
4605 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4606 {
4607 clear_ppconst(&ppconst);
4608 return FAIL;
4609 }
4610 if (generate_ppconst(cctx, &ppconst) == FAIL)
4611 return FAIL;
4612 return OK;
4613}
4614
4615/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 * compile "return [expr]"
4617 */
4618 static char_u *
4619compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4620{
4621 char_u *p = arg;
4622 garray_T *stack = &cctx->ctx_type_stack;
4623 type_T *stack_type;
4624
4625 if (*p != NUL && *p != '|' && *p != '\n')
4626 {
4627 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004628 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 return NULL;
4630
4631 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4632 if (set_return_type)
4633 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004634 else
4635 {
4636 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4637 && stack_type->tt_type != VAR_VOID
4638 && stack_type->tt_type != VAR_UNKNOWN)
4639 {
4640 emsg(_("E1096: Returning a value in a function without a return type"));
4641 return NULL;
4642 }
4643 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 == FAIL)
4645 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004646 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 }
4648 else
4649 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004650 // "set_return_type" cannot be TRUE, only used for a lambda which
4651 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004652 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4653 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 {
4655 emsg(_("E1003: Missing return value"));
4656 return NULL;
4657 }
4658
4659 // No argument, return zero.
4660 generate_PUSHNR(cctx, 0);
4661 }
4662
4663 if (generate_instr(cctx, ISN_RETURN) == NULL)
4664 return NULL;
4665
4666 // "return val | endif" is possible
4667 return skipwhite(p);
4668}
4669
4670/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004671 * Get a line from the compilation context, compatible with exarg_T getline().
4672 * Return a pointer to the line in allocated memory.
4673 * Return NULL for end-of-file or some error.
4674 */
4675 static char_u *
4676exarg_getline(
4677 int c UNUSED,
4678 void *cookie,
4679 int indent UNUSED,
4680 int do_concat UNUSED)
4681{
4682 cctx_T *cctx = (cctx_T *)cookie;
4683
4684 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4685 {
4686 iemsg("Heredoc got to end");
4687 return NULL;
4688 }
4689 ++cctx->ctx_lnum;
4690 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4691 [cctx->ctx_lnum]);
4692}
4693
4694/*
4695 * Compile a nested :def command.
4696 */
4697 static char_u *
4698compile_nested_function(exarg_T *eap, cctx_T *cctx)
4699{
4700 char_u *name_start = eap->arg;
4701 char_u *name_end = to_name_end(eap->arg, FALSE);
4702 char_u *name = get_lambda_name();
4703 lvar_T *lvar;
4704 ufunc_T *ufunc;
4705
4706 eap->arg = name_end;
4707 eap->getline = exarg_getline;
4708 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004709 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004710 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004711 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004712
Bram Moolenaar822ba242020-05-24 23:00:18 +02004713 if (ufunc == NULL)
4714 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004715 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004716 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004717 return NULL;
4718
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004719 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004720 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004721 TRUE, ufunc->uf_func_type);
4722
4723 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4724 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4725 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004726
Bram Moolenaar61a89812020-05-07 16:58:17 +02004727 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004728 return (char_u *)"";
4729}
4730
4731/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 * Return the length of an assignment operator, or zero if there isn't one.
4733 */
4734 int
4735assignment_len(char_u *p, int *heredoc)
4736{
4737 if (*p == '=')
4738 {
4739 if (p[1] == '<' && p[2] == '<')
4740 {
4741 *heredoc = TRUE;
4742 return 3;
4743 }
4744 return 1;
4745 }
4746 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4747 return 2;
4748 if (STRNCMP(p, "..=", 3) == 0)
4749 return 3;
4750 return 0;
4751}
4752
4753// words that cannot be used as a variable
4754static char *reserved[] = {
4755 "true",
4756 "false",
4757 NULL
4758};
4759
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004760typedef enum {
4761 dest_local,
4762 dest_option,
4763 dest_env,
4764 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004765 dest_buffer,
4766 dest_window,
4767 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004768 dest_vimvar,
4769 dest_script,
4770 dest_reg,
4771} assign_dest_T;
4772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004774 * Generate the load instruction for "name".
4775 */
4776 static void
4777generate_loadvar(
4778 cctx_T *cctx,
4779 assign_dest_T dest,
4780 char_u *name,
4781 lvar_T *lvar,
4782 type_T *type)
4783{
4784 switch (dest)
4785 {
4786 case dest_option:
4787 // TODO: check the option exists
4788 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4789 break;
4790 case dest_global:
4791 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4792 break;
4793 case dest_buffer:
4794 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4795 break;
4796 case dest_window:
4797 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4798 break;
4799 case dest_tab:
4800 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4801 break;
4802 case dest_script:
4803 compile_load_scriptvar(cctx,
4804 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4805 break;
4806 case dest_env:
4807 // Include $ in the name here
4808 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4809 break;
4810 case dest_reg:
4811 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4812 break;
4813 case dest_vimvar:
4814 generate_LOADV(cctx, name + 2, TRUE);
4815 break;
4816 case dest_local:
4817 if (lvar->lv_from_outer)
4818 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4819 NULL, type);
4820 else
4821 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4822 break;
4823 }
4824}
4825
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004826 void
4827vim9_declare_error(char_u *name)
4828{
4829 char *scope = "";
4830
4831 switch (*name)
4832 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004833 case 'g': scope = _("global"); break;
4834 case 'b': scope = _("buffer"); break;
4835 case 'w': scope = _("window"); break;
4836 case 't': scope = _("tab"); break;
4837 case 'v': scope = "v:"; break;
4838 case '$': semsg(_(e_declare_env_var), name); return;
4839 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004840 }
4841 semsg(_(e_declare_var), scope, name);
4842}
4843
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004844/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004845 * Compile declaration and assignment:
4846 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004848 * Return NULL for an error.
4849 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 */
4851 static char_u *
4852compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4853{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004854 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004856 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 char_u *ret = NULL;
4858 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004859 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004862 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 int oplen = 0;
4865 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004866 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004867 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004868 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004872 // Skip over the "var" or "[var, var]" to get to any "=".
4873 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4874 if (p == NULL)
4875 return *arg == '[' ? arg : NULL;
4876
4877 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004879 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 return NULL;
4881 }
4882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 sp = p;
4884 p = skipwhite(p);
4885 op = p;
4886 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004887
4888 if (var_count > 0 && oplen == 0)
4889 // can be something like "[1, 2]->func()"
4890 return arg;
4891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4893 {
4894 char_u buf[4];
4895
4896 vim_strncpy(buf, op, oplen);
4897 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004898 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004899 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 if (heredoc)
4902 {
4903 list_T *l;
4904 listitem_T *li;
4905
4906 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004907 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004909 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910
4911 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004912 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913 {
4914 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4915 li->li_tv.vval.v_string = NULL;
4916 }
4917 generate_NEWLIST(cctx, l->lv_len);
4918 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004919 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 list_free(l);
4921 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004922 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004924 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004926 // for "[var, var] = expr" evaluate the expression here, loop over the
4927 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004928
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004929 p = skipwhite(op + oplen);
4930 if (compile_expr0(&p, cctx) == FAIL)
4931 return NULL;
4932 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004934 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004936 type_T *stacktype;
4937
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004938 stacktype = stack->ga_len == 0 ? &t_void
4939 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004940 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004942 emsg(_(e_cannot_use_void));
4943 goto theend;
4944 }
4945 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4946 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004947 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4948 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004949 }
4950 }
4951
4952 /*
4953 * Loop over variables in "[var, var] = expr".
4954 * For "var = expr" and "let var: type" this is done only once.
4955 */
4956 if (var_count > 0)
4957 var_start = skipwhite(arg + 1); // skip over the "["
4958 else
4959 var_start = arg;
4960 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4961 {
4962 char_u *var_end = skip_var_one(var_start, FALSE);
4963 size_t varlen;
4964 int new_local = FALSE;
4965 int opt_type;
4966 int opt_flags = 0;
4967 assign_dest_T dest = dest_local;
4968 int vimvaridx = -1;
4969 lvar_T *lvar = NULL;
4970 lvar_T arg_lvar;
4971 int has_type = FALSE;
4972 int has_index = FALSE;
4973 int instr_count = -1;
4974
4975 p = (*var_start == '&' || *var_start == '$'
4976 || *var_start == '@') ? var_start + 1 : var_start;
4977 p = to_name_end(p, TRUE);
4978
4979 // "a: type" is declaring variable "a" with a type, not "a:".
4980 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4981 --var_end;
4982 if (is_decl && p == var_start + 2 && p[-1] == ':')
4983 --p;
4984
4985 varlen = p - var_start;
4986 vim_free(name);
4987 name = vim_strnsave(var_start, varlen);
4988 if (name == NULL)
4989 return NULL;
4990 if (!heredoc)
4991 type = &t_any;
4992
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004993 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004994 {
4995 if (*var_start == '&')
4996 {
4997 int cc;
4998 long numval;
4999
5000 dest = dest_option;
5001 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005003 emsg(_(e_const_option));
5004 goto theend;
5005 }
5006 if (is_decl)
5007 {
5008 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5009 goto theend;
5010 }
5011 p = var_start;
5012 p = find_option_end(&p, &opt_flags);
5013 if (p == NULL)
5014 {
5015 // cannot happen?
5016 emsg(_(e_letunexp));
5017 goto theend;
5018 }
5019 cc = *p;
5020 *p = NUL;
5021 opt_type = get_option_value(var_start + 1, &numval,
5022 NULL, opt_flags);
5023 *p = cc;
5024 if (opt_type == -3)
5025 {
5026 semsg(_(e_unknown_option), var_start);
5027 goto theend;
5028 }
5029 if (opt_type == -2 || opt_type == 0)
5030 type = &t_string;
5031 else
5032 type = &t_number; // both number and boolean option
5033 }
5034 else if (*var_start == '$')
5035 {
5036 dest = dest_env;
5037 type = &t_string;
5038 if (is_decl)
5039 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005040 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005041 goto theend;
5042 }
5043 }
5044 else if (*var_start == '@')
5045 {
5046 if (!valid_yank_reg(var_start[1], TRUE))
5047 {
5048 emsg_invreg(var_start[1]);
5049 goto theend;
5050 }
5051 dest = dest_reg;
5052 type = &t_string;
5053 if (is_decl)
5054 {
5055 semsg(_("E1066: Cannot declare a register: %s"), name);
5056 goto theend;
5057 }
5058 }
5059 else if (STRNCMP(var_start, "g:", 2) == 0)
5060 {
5061 dest = dest_global;
5062 if (is_decl)
5063 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005064 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005065 goto theend;
5066 }
5067 }
5068 else if (STRNCMP(var_start, "b:", 2) == 0)
5069 {
5070 dest = dest_buffer;
5071 if (is_decl)
5072 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005073 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005074 goto theend;
5075 }
5076 }
5077 else if (STRNCMP(var_start, "w:", 2) == 0)
5078 {
5079 dest = dest_window;
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, "t:", 2) == 0)
5087 {
5088 dest = dest_tab;
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, "v:", 2) == 0)
5096 {
5097 typval_T *vtv;
5098 int di_flags;
5099
5100 vimvaridx = find_vim_var(name + 2, &di_flags);
5101 if (vimvaridx < 0)
5102 {
5103 semsg(_(e_var_notfound), var_start);
5104 goto theend;
5105 }
5106 // We use the current value of "sandbox" here, is that OK?
5107 if (var_check_ro(di_flags, name, FALSE))
5108 goto theend;
5109 dest = dest_vimvar;
5110 vtv = get_vim_var_tv(vimvaridx);
5111 type = typval2type(vtv);
5112 if (is_decl)
5113 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005114 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005115 goto theend;
5116 }
5117 }
5118 else
5119 {
5120 int idx;
5121
5122 for (idx = 0; reserved[idx] != NULL; ++idx)
5123 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005124 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005125 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005126 goto theend;
5127 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005128
5129 lvar = lookup_local(var_start, varlen, cctx);
5130 if (lvar == NULL)
5131 {
5132 CLEAR_FIELD(arg_lvar);
5133 if (lookup_arg(var_start, varlen,
5134 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5135 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005136 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005137 if (is_decl)
5138 {
5139 semsg(_(e_used_as_arg), name);
5140 goto theend;
5141 }
5142 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005143 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005144 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005145 if (lvar != NULL)
5146 {
5147 if (is_decl)
5148 {
5149 semsg(_("E1017: Variable already declared: %s"), name);
5150 goto theend;
5151 }
5152 else if (lvar->lv_const)
5153 {
5154 semsg(_("E1018: Cannot assign to a constant: %s"),
5155 name);
5156 goto theend;
5157 }
5158 }
5159 else if (STRNCMP(var_start, "s:", 2) == 0
5160 || lookup_script(var_start, varlen) == OK
5161 || find_imported(var_start, varlen, cctx) != NULL)
5162 {
5163 dest = dest_script;
5164 if (is_decl)
5165 {
5166 semsg(_("E1054: Variable already declared in the script: %s"),
5167 name);
5168 goto theend;
5169 }
5170 }
5171 else if (name[1] == ':' && name[2] != NUL)
5172 {
5173 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5174 name);
5175 goto theend;
5176 }
5177 else if (!is_decl)
5178 {
5179 semsg(_("E1089: unknown variable: %s"), name);
5180 goto theend;
5181 }
5182 }
5183 }
5184
5185 // handle "a:name" as a name, not index "name" on "a"
5186 if (varlen > 1 || var_start[varlen] != ':')
5187 p = var_end;
5188
5189 if (dest != dest_option)
5190 {
5191 if (is_decl && *p == ':')
5192 {
5193 // parse optional type: "let var: type = expr"
5194 if (!VIM_ISWHITE(p[1]))
5195 {
5196 semsg(_(e_white_after), ":");
5197 goto theend;
5198 }
5199 p = skipwhite(p + 1);
5200 type = parse_type(&p, cctx->ctx_type_list);
5201 has_type = TRUE;
5202 }
5203 else if (lvar != NULL)
5204 type = lvar->lv_type;
5205 }
5206
5207 if (oplen == 3 && !heredoc && dest != dest_global
5208 && type->tt_type != VAR_STRING
5209 && type->tt_type != VAR_ANY)
5210 {
5211 emsg(_("E1019: Can only concatenate to string"));
5212 goto theend;
5213 }
5214
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005215 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005216 {
5217 if (oplen > 1 && !heredoc)
5218 {
5219 // +=, /=, etc. require an existing variable
5220 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5221 name);
5222 goto theend;
5223 }
5224
5225 // new local variable
5226 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5227 goto theend;
5228 lvar = reserve_local(cctx, var_start, varlen,
5229 cmdidx == CMD_const, type);
5230 if (lvar == NULL)
5231 goto theend;
5232 new_local = TRUE;
5233 }
5234
5235 member_type = type;
5236 if (var_end > var_start + varlen)
5237 {
5238 // Something follows after the variable: "var[idx]".
5239 if (is_decl)
5240 {
5241 emsg(_("E1087: cannot use an index when declaring a variable"));
5242 goto theend;
5243 }
5244
5245 if (var_start[varlen] == '[')
5246 {
5247 has_index = TRUE;
5248 if (type->tt_member == NULL)
5249 {
5250 semsg(_("E1088: cannot use an index on %s"), name);
5251 goto theend;
5252 }
5253 member_type = type->tt_member;
5254 }
5255 else
5256 {
5257 semsg("Not supported yet: %s", var_start);
5258 goto theend;
5259 }
5260 }
5261 else if (lvar == &arg_lvar)
5262 {
5263 semsg(_("E1090: Cannot assign to argument %s"), name);
5264 goto theend;
5265 }
5266
5267 if (!heredoc)
5268 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005269 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005270 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005271 if (oplen > 0 && var_count == 0)
5272 {
5273 // skip over the "=" and the expression
5274 p = skipwhite(op + oplen);
5275 compile_expr0(&p, cctx);
5276 }
5277 }
5278 else if (oplen > 0)
5279 {
5280 type_T *stacktype;
5281
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005282 // For "var = expr" evaluate the expression.
5283 if (var_count == 0)
5284 {
5285 int r;
5286
5287 // for "+=", "*=", "..=" etc. first load the current value
5288 if (*op != '=')
5289 {
5290 generate_loadvar(cctx, dest, name, lvar, type);
5291
5292 if (has_index)
5293 {
5294 // TODO: get member from list or dict
5295 emsg("Index with operation not supported yet");
5296 goto theend;
5297 }
5298 }
5299
5300 // Compile the expression. Temporarily hide the new local
5301 // variable here, it is not available to this expression.
5302 if (new_local)
5303 --cctx->ctx_locals.ga_len;
5304 instr_count = instr->ga_len;
5305 p = skipwhite(op + oplen);
5306 r = compile_expr0(&p, cctx);
5307 if (new_local)
5308 ++cctx->ctx_locals.ga_len;
5309 if (r == FAIL)
5310 goto theend;
5311 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005312 else if (semicolon && var_idx == var_count - 1)
5313 {
5314 // For "[var; var] = expr" get the rest of the list
5315 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5316 goto theend;
5317 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005318 else
5319 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005320 // For "[var, var] = expr" get the "var_idx" item from the
5321 // list.
5322 if (generate_GETITEM(cctx, var_idx) == FAIL)
5323 return FAIL;
5324 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005325
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005326 stacktype = stack->ga_len == 0 ? &t_void
5327 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5328 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005329 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005330 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005331 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005332 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005334 emsg(_(e_cannot_use_void));
5335 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005336 }
5337 else
5338 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005339 // An empty list or dict has a &t_void member,
5340 // for a variable that implies &t_any.
5341 if (stacktype == &t_list_empty)
5342 lvar->lv_type = &t_list_any;
5343 else if (stacktype == &t_dict_empty)
5344 lvar->lv_type = &t_dict_any;
5345 else
5346 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005347 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005348 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005349 else
5350 {
5351 type_T *use_type = lvar->lv_type;
5352
5353 if (has_index)
5354 {
5355 use_type = use_type->tt_member;
5356 if (use_type == NULL)
5357 use_type = &t_void;
5358 }
5359 if (need_type(stacktype, use_type, -1, cctx)
5360 == FAIL)
5361 goto theend;
5362 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005363 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005364 else if (*p != '=' && need_type(stacktype, member_type, -1,
5365 cctx) == FAIL)
5366 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005368 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005370 emsg(_(e_const_req_value));
5371 goto theend;
5372 }
5373 else if (!has_type || dest == dest_option)
5374 {
5375 emsg(_(e_type_req));
5376 goto theend;
5377 }
5378 else
5379 {
5380 // variables are always initialized
5381 if (ga_grow(instr, 1) == FAIL)
5382 goto theend;
5383 switch (member_type->tt_type)
5384 {
5385 case VAR_BOOL:
5386 generate_PUSHBOOL(cctx, VVAL_FALSE);
5387 break;
5388 case VAR_FLOAT:
5389#ifdef FEAT_FLOAT
5390 generate_PUSHF(cctx, 0.0);
5391#endif
5392 break;
5393 case VAR_STRING:
5394 generate_PUSHS(cctx, NULL);
5395 break;
5396 case VAR_BLOB:
5397 generate_PUSHBLOB(cctx, NULL);
5398 break;
5399 case VAR_FUNC:
5400 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5401 break;
5402 case VAR_LIST:
5403 generate_NEWLIST(cctx, 0);
5404 break;
5405 case VAR_DICT:
5406 generate_NEWDICT(cctx, 0);
5407 break;
5408 case VAR_JOB:
5409 generate_PUSHJOB(cctx, NULL);
5410 break;
5411 case VAR_CHANNEL:
5412 generate_PUSHCHANNEL(cctx, NULL);
5413 break;
5414 case VAR_NUMBER:
5415 case VAR_UNKNOWN:
5416 case VAR_ANY:
5417 case VAR_PARTIAL:
5418 case VAR_VOID:
5419 case VAR_SPECIAL: // cannot happen
5420 generate_PUSHNR(cctx, 0);
5421 break;
5422 }
5423 }
5424 if (var_count == 0)
5425 end = p;
5426 }
5427
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005428 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005429 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005430 break;
5431
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005432 if (oplen > 0 && *op != '=')
5433 {
5434 type_T *expected = &t_number;
5435 type_T *stacktype;
5436
5437 // TODO: if type is known use float or any operation
5438
5439 if (*op == '.')
5440 expected = &t_string;
5441 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5442 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5443 goto theend;
5444
5445 if (*op == '.')
5446 generate_instr_drop(cctx, ISN_CONCAT, 1);
5447 else
5448 {
5449 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5450
5451 if (isn == NULL)
5452 goto theend;
5453 switch (*op)
5454 {
5455 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5456 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5457 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5458 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5459 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5460 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005461 }
5462 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005465 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005466 int r;
5467
5468 // Compile the "idx" in "var[idx]".
5469 if (new_local)
5470 --cctx->ctx_locals.ga_len;
5471 p = skipwhite(var_start + varlen + 1);
5472 r = compile_expr0(&p, cctx);
5473 if (new_local)
5474 ++cctx->ctx_locals.ga_len;
5475 if (r == FAIL)
5476 goto theend;
5477 if (*skipwhite(p) != ']')
5478 {
5479 emsg(_(e_missbrac));
5480 goto theend;
5481 }
5482 if (type->tt_type == VAR_DICT
5483 && may_generate_2STRING(-1, cctx) == FAIL)
5484 goto theend;
5485 if (type->tt_type == VAR_LIST
5486 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005487 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005488 {
5489 emsg(_(e_number_exp));
5490 goto theend;
5491 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005492
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005493 // Load the dict or list. On the stack we then have:
5494 // - value
5495 // - index
5496 // - variable
5497 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005498
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005499 if (type->tt_type == VAR_LIST)
5500 {
5501 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5502 return FAIL;
5503 }
5504 else if (type->tt_type == VAR_DICT)
5505 {
5506 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5507 return FAIL;
5508 }
5509 else
5510 {
5511 emsg(_(e_listreq));
5512 goto theend;
5513 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005514 }
5515 else
5516 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005517 switch (dest)
5518 {
5519 case dest_option:
5520 generate_STOREOPT(cctx, name + 1, opt_flags);
5521 break;
5522 case dest_global:
5523 // include g: with the name, easier to execute that way
5524 generate_STORE(cctx, ISN_STOREG, 0, name);
5525 break;
5526 case dest_buffer:
5527 // include b: with the name, easier to execute that way
5528 generate_STORE(cctx, ISN_STOREB, 0, name);
5529 break;
5530 case dest_window:
5531 // include w: with the name, easier to execute that way
5532 generate_STORE(cctx, ISN_STOREW, 0, name);
5533 break;
5534 case dest_tab:
5535 // include t: with the name, easier to execute that way
5536 generate_STORE(cctx, ISN_STORET, 0, name);
5537 break;
5538 case dest_env:
5539 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5540 break;
5541 case dest_reg:
5542 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5543 break;
5544 case dest_vimvar:
5545 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5546 break;
5547 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005548 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005549 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5550 imported_T *import = NULL;
5551 int sid = current_sctx.sc_sid;
5552 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005553
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005554 if (name[1] != ':')
5555 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005556 import = find_imported(name, 0, cctx);
5557 if (import != NULL)
5558 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005559 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005560
5561 idx = get_script_item_idx(sid, rawname, TRUE);
5562 // TODO: specific type
5563 if (idx < 0)
5564 {
5565 char_u *name_s = name;
5566
5567 // Include s: in the name for store_var()
5568 if (name[1] != ':')
5569 {
5570 int len = (int)STRLEN(name) + 3;
5571
5572 name_s = alloc(len);
5573 if (name_s == NULL)
5574 name_s = name;
5575 else
5576 vim_snprintf((char *)name_s, len,
5577 "s:%s", name);
5578 }
5579 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005580 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005581 if (name_s != name)
5582 vim_free(name_s);
5583 }
5584 else
5585 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005586 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005587 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005588 break;
5589 case dest_local:
5590 if (lvar != NULL)
5591 {
5592 isn_T *isn = ((isn_T *)instr->ga_data)
5593 + instr->ga_len - 1;
5594
5595 // optimization: turn "var = 123" from ISN_PUSHNR +
5596 // ISN_STORE into ISN_STORENR
5597 if (!lvar->lv_from_outer
5598 && instr->ga_len == instr_count + 1
5599 && isn->isn_type == ISN_PUSHNR)
5600 {
5601 varnumber_T val = isn->isn_arg.number;
5602
5603 isn->isn_type = ISN_STORENR;
5604 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5605 isn->isn_arg.storenr.stnr_val = val;
5606 if (stack->ga_len > 0)
5607 --stack->ga_len;
5608 }
5609 else if (lvar->lv_from_outer)
5610 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005611 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005612 else
5613 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5614 }
5615 break;
5616 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005617 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005618
5619 if (var_idx + 1 < var_count)
5620 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005621 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005622
5623 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005624 if (var_count > 0 && !semicolon)
5625 {
5626 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5627 goto theend;
5628 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005629
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005630 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005631
5632theend:
5633 vim_free(name);
5634 return ret;
5635}
5636
5637/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005638 * Check if "name" can be "unlet".
5639 */
5640 int
5641check_vim9_unlet(char_u *name)
5642{
5643 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5644 {
5645 semsg(_("E1081: Cannot unlet %s"), name);
5646 return FAIL;
5647 }
5648 return OK;
5649}
5650
5651/*
5652 * Callback passed to ex_unletlock().
5653 */
5654 static int
5655compile_unlet(
5656 lval_T *lvp,
5657 char_u *name_end,
5658 exarg_T *eap,
5659 int deep UNUSED,
5660 void *coookie)
5661{
5662 cctx_T *cctx = coookie;
5663
5664 if (lvp->ll_tv == NULL)
5665 {
5666 char_u *p = lvp->ll_name;
5667 int cc = *name_end;
5668 int ret = OK;
5669
5670 // Normal name. Only supports g:, w:, t: and b: namespaces.
5671 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005672 if (*p == '$')
5673 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5674 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005675 ret = FAIL;
5676 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005677 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005678
5679 *name_end = cc;
5680 return ret;
5681 }
5682
5683 // TODO: unlet {list}[idx]
5684 // TODO: unlet {dict}[key]
5685 emsg("Sorry, :unlet not fully implemented yet");
5686 return FAIL;
5687}
5688
5689/*
5690 * compile "unlet var", "lock var" and "unlock var"
5691 * "arg" points to "var".
5692 */
5693 static char_u *
5694compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5695{
5696 char_u *p = arg;
5697
5698 if (eap->cmdidx != CMD_unlet)
5699 {
5700 emsg("Sorry, :lock and unlock not implemented yet");
5701 return NULL;
5702 }
5703
5704 if (*p == '!')
5705 {
5706 p = skipwhite(p + 1);
5707 eap->forceit = TRUE;
5708 }
5709
5710 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5711 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5712}
5713
5714/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005715 * Compile an :import command.
5716 */
5717 static char_u *
5718compile_import(char_u *arg, cctx_T *cctx)
5719{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005720 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005721}
5722
5723/*
5724 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5725 */
5726 static int
5727compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5728{
5729 garray_T *instr = &cctx->ctx_instr;
5730 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5731
5732 if (endlabel == NULL)
5733 return FAIL;
5734 endlabel->el_next = *el;
5735 *el = endlabel;
5736 endlabel->el_end_label = instr->ga_len;
5737
5738 generate_JUMP(cctx, when, 0);
5739 return OK;
5740}
5741
5742 static void
5743compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5744{
5745 garray_T *instr = &cctx->ctx_instr;
5746
5747 while (*el != NULL)
5748 {
5749 endlabel_T *cur = (*el);
5750 isn_T *isn;
5751
5752 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5753 isn->isn_arg.jump.jump_where = instr->ga_len;
5754 *el = cur->el_next;
5755 vim_free(cur);
5756 }
5757}
5758
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005759 static void
5760compile_free_jump_to_end(endlabel_T **el)
5761{
5762 while (*el != NULL)
5763 {
5764 endlabel_T *cur = (*el);
5765
5766 *el = cur->el_next;
5767 vim_free(cur);
5768 }
5769}
5770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771/*
5772 * Create a new scope and set up the generic items.
5773 */
5774 static scope_T *
5775new_scope(cctx_T *cctx, scopetype_T type)
5776{
5777 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5778
5779 if (scope == NULL)
5780 return NULL;
5781 scope->se_outer = cctx->ctx_scope;
5782 cctx->ctx_scope = scope;
5783 scope->se_type = type;
5784 scope->se_local_count = cctx->ctx_locals.ga_len;
5785 return scope;
5786}
5787
5788/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005789 * Free the current scope and go back to the outer scope.
5790 */
5791 static void
5792drop_scope(cctx_T *cctx)
5793{
5794 scope_T *scope = cctx->ctx_scope;
5795
5796 if (scope == NULL)
5797 {
5798 iemsg("calling drop_scope() without a scope");
5799 return;
5800 }
5801 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005802 switch (scope->se_type)
5803 {
5804 case IF_SCOPE:
5805 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5806 case FOR_SCOPE:
5807 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5808 case WHILE_SCOPE:
5809 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5810 case TRY_SCOPE:
5811 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5812 case NO_SCOPE:
5813 case BLOCK_SCOPE:
5814 break;
5815 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005816 vim_free(scope);
5817}
5818
5819/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820 * compile "if expr"
5821 *
5822 * "if expr" Produces instructions:
5823 * EVAL expr Push result of "expr"
5824 * JUMP_IF_FALSE end
5825 * ... body ...
5826 * end:
5827 *
5828 * "if expr | else" Produces instructions:
5829 * EVAL expr Push result of "expr"
5830 * JUMP_IF_FALSE else
5831 * ... body ...
5832 * JUMP_ALWAYS end
5833 * else:
5834 * ... body ...
5835 * end:
5836 *
5837 * "if expr1 | elseif expr2 | else" Produces instructions:
5838 * EVAL expr Push result of "expr"
5839 * JUMP_IF_FALSE elseif
5840 * ... body ...
5841 * JUMP_ALWAYS end
5842 * elseif:
5843 * EVAL expr Push result of "expr"
5844 * JUMP_IF_FALSE else
5845 * ... body ...
5846 * JUMP_ALWAYS end
5847 * else:
5848 * ... body ...
5849 * end:
5850 */
5851 static char_u *
5852compile_if(char_u *arg, cctx_T *cctx)
5853{
5854 char_u *p = arg;
5855 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005856 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005858 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005859 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005860
Bram Moolenaara5565e42020-05-09 15:44:01 +02005861 CLEAR_FIELD(ppconst);
5862 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005863 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005864 clear_ppconst(&ppconst);
5865 return NULL;
5866 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005867 if (cctx->ctx_skip == SKIP_YES)
5868 clear_ppconst(&ppconst);
5869 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005870 {
5871 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005872 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005873 clear_ppconst(&ppconst);
5874 }
5875 else
5876 {
5877 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005878 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005879 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005880 return NULL;
5881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005882
5883 scope = new_scope(cctx, IF_SCOPE);
5884 if (scope == NULL)
5885 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005886 scope->se_skip_save = skip_save;
5887 // "is_had_return" will be reset if any block does not end in :return
5888 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005889
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005890 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005891 {
5892 // "where" is set when ":elseif", "else" or ":endif" is found
5893 scope->se_u.se_if.is_if_label = instr->ga_len;
5894 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5895 }
5896 else
5897 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898
5899 return p;
5900}
5901
5902 static char_u *
5903compile_elseif(char_u *arg, cctx_T *cctx)
5904{
5905 char_u *p = arg;
5906 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005907 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005908 isn_T *isn;
5909 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005910 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005911
5912 if (scope == NULL || scope->se_type != IF_SCOPE)
5913 {
5914 emsg(_(e_elseif_without_if));
5915 return NULL;
5916 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005917 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005918 if (!cctx->ctx_had_return)
5919 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005920
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005921 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005922 {
5923 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005924 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005925 return NULL;
5926 // previous "if" or "elseif" jumps here
5927 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5928 isn->isn_arg.jump.jump_where = instr->ga_len;
5929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005930
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005931 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005932 CLEAR_FIELD(ppconst);
5933 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005934 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005935 clear_ppconst(&ppconst);
5936 return NULL;
5937 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005938 if (scope->se_skip_save == SKIP_YES)
5939 clear_ppconst(&ppconst);
5940 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005941 {
5942 // The expression results in a constant.
5943 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005944 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005945 clear_ppconst(&ppconst);
5946 scope->se_u.se_if.is_if_label = -1;
5947 }
5948 else
5949 {
5950 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005951 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005952 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005953 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005954
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005955 // "where" is set when ":elseif", "else" or ":endif" is found
5956 scope->se_u.se_if.is_if_label = instr->ga_len;
5957 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005959
5960 return p;
5961}
5962
5963 static char_u *
5964compile_else(char_u *arg, cctx_T *cctx)
5965{
5966 char_u *p = arg;
5967 garray_T *instr = &cctx->ctx_instr;
5968 isn_T *isn;
5969 scope_T *scope = cctx->ctx_scope;
5970
5971 if (scope == NULL || scope->se_type != IF_SCOPE)
5972 {
5973 emsg(_(e_else_without_if));
5974 return NULL;
5975 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005976 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005977 if (!cctx->ctx_had_return)
5978 scope->se_u.se_if.is_had_return = FALSE;
5979 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980
Bram Moolenaarefd88552020-06-18 20:50:10 +02005981 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005982 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005983 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005984 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005985 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005986 if (!cctx->ctx_had_return
5987 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5988 JUMP_ALWAYS, cctx) == FAIL)
5989 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005990 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005991
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005992 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005993 {
5994 if (scope->se_u.se_if.is_if_label >= 0)
5995 {
5996 // previous "if" or "elseif" jumps here
5997 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5998 isn->isn_arg.jump.jump_where = instr->ga_len;
5999 scope->se_u.se_if.is_if_label = -1;
6000 }
6001 }
6002
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006003 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006004 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006006
6007 return p;
6008}
6009
6010 static char_u *
6011compile_endif(char_u *arg, cctx_T *cctx)
6012{
6013 scope_T *scope = cctx->ctx_scope;
6014 ifscope_T *ifscope;
6015 garray_T *instr = &cctx->ctx_instr;
6016 isn_T *isn;
6017
6018 if (scope == NULL || scope->se_type != IF_SCOPE)
6019 {
6020 emsg(_(e_endif_without_if));
6021 return NULL;
6022 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006023 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006024 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006025 if (!cctx->ctx_had_return)
6026 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006027
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006028 if (scope->se_u.se_if.is_if_label >= 0)
6029 {
6030 // previous "if" or "elseif" jumps here
6031 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6032 isn->isn_arg.jump.jump_where = instr->ga_len;
6033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034 // Fill in the "end" label in jumps at the end of the blocks.
6035 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006036 cctx->ctx_skip = scope->se_skip_save;
6037
6038 // If all the blocks end in :return and there is an :else then the
6039 // had_return flag is set.
6040 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006041
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006042 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006043 return arg;
6044}
6045
6046/*
6047 * compile "for var in expr"
6048 *
6049 * Produces instructions:
6050 * PUSHNR -1
6051 * STORE loop-idx Set index to -1
6052 * EVAL expr Push result of "expr"
6053 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6054 * - if beyond end, jump to "end"
6055 * - otherwise get item from list and push it
6056 * STORE var Store item in "var"
6057 * ... body ...
6058 * JUMP top Jump back to repeat
6059 * end: DROP Drop the result of "expr"
6060 *
6061 */
6062 static char_u *
6063compile_for(char_u *arg, cctx_T *cctx)
6064{
6065 char_u *p;
6066 size_t varlen;
6067 garray_T *instr = &cctx->ctx_instr;
6068 garray_T *stack = &cctx->ctx_type_stack;
6069 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006070 lvar_T *loop_lvar; // loop iteration variable
6071 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006072 type_T *vartype;
6073
6074 // TODO: list of variables: "for [key, value] in dict"
6075 // parse "var"
6076 for (p = arg; eval_isnamec1(*p); ++p)
6077 ;
6078 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006079 var_lvar = lookup_local(arg, varlen, cctx);
6080 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081 {
6082 semsg(_("E1023: variable already defined: %s"), arg);
6083 return NULL;
6084 }
6085
6086 // consume "in"
6087 p = skipwhite(p);
6088 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6089 {
6090 emsg(_(e_missing_in));
6091 return NULL;
6092 }
6093 p = skipwhite(p + 2);
6094
6095
6096 scope = new_scope(cctx, FOR_SCOPE);
6097 if (scope == NULL)
6098 return NULL;
6099
6100 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006101 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6102 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006103 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006104 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006105 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108
6109 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006110 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6111 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006112 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006113 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006114 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006115 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006117
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006118 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119
6120 // compile "expr", it remains on the stack until "endfor"
6121 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006122 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006123 {
6124 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006126 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006128 // Now that we know the type of "var", check that it is a list, now or at
6129 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006131 if (need_type(vartype, &t_list_any, -1, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006132 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006133 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134 return NULL;
6135 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006136 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006137 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006138
6139 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006140 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006141
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006142 generate_FOR(cctx, loop_lvar->lv_idx);
6143 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144
6145 return arg;
6146}
6147
6148/*
6149 * compile "endfor"
6150 */
6151 static char_u *
6152compile_endfor(char_u *arg, cctx_T *cctx)
6153{
6154 garray_T *instr = &cctx->ctx_instr;
6155 scope_T *scope = cctx->ctx_scope;
6156 forscope_T *forscope;
6157 isn_T *isn;
6158
6159 if (scope == NULL || scope->se_type != FOR_SCOPE)
6160 {
6161 emsg(_(e_for));
6162 return NULL;
6163 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006164 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006166 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006167
6168 // At end of ":for" scope jump back to the FOR instruction.
6169 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6170
6171 // Fill in the "end" label in the FOR statement so it can jump here
6172 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6173 isn->isn_arg.forloop.for_end = instr->ga_len;
6174
6175 // Fill in the "end" label any BREAK statements
6176 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6177
6178 // Below the ":for" scope drop the "expr" list from the stack.
6179 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6180 return NULL;
6181
6182 vim_free(scope);
6183
6184 return arg;
6185}
6186
6187/*
6188 * compile "while expr"
6189 *
6190 * Produces instructions:
6191 * top: EVAL expr Push result of "expr"
6192 * JUMP_IF_FALSE end jump if false
6193 * ... body ...
6194 * JUMP top Jump back to repeat
6195 * end:
6196 *
6197 */
6198 static char_u *
6199compile_while(char_u *arg, cctx_T *cctx)
6200{
6201 char_u *p = arg;
6202 garray_T *instr = &cctx->ctx_instr;
6203 scope_T *scope;
6204
6205 scope = new_scope(cctx, WHILE_SCOPE);
6206 if (scope == NULL)
6207 return NULL;
6208
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006209 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006210
6211 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006212 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006213 return NULL;
6214
6215 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006216 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217 JUMP_IF_FALSE, cctx) == FAIL)
6218 return FAIL;
6219
6220 return p;
6221}
6222
6223/*
6224 * compile "endwhile"
6225 */
6226 static char_u *
6227compile_endwhile(char_u *arg, cctx_T *cctx)
6228{
6229 scope_T *scope = cctx->ctx_scope;
6230
6231 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6232 {
6233 emsg(_(e_while));
6234 return NULL;
6235 }
6236 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006237 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006238
6239 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006240 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006241
6242 // Fill in the "end" label in the WHILE statement so it can jump here.
6243 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006244 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006245
6246 vim_free(scope);
6247
6248 return arg;
6249}
6250
6251/*
6252 * compile "continue"
6253 */
6254 static char_u *
6255compile_continue(char_u *arg, cctx_T *cctx)
6256{
6257 scope_T *scope = cctx->ctx_scope;
6258
6259 for (;;)
6260 {
6261 if (scope == NULL)
6262 {
6263 emsg(_(e_continue));
6264 return NULL;
6265 }
6266 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6267 break;
6268 scope = scope->se_outer;
6269 }
6270
6271 // Jump back to the FOR or WHILE instruction.
6272 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006273 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6274 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 return arg;
6276}
6277
6278/*
6279 * compile "break"
6280 */
6281 static char_u *
6282compile_break(char_u *arg, cctx_T *cctx)
6283{
6284 scope_T *scope = cctx->ctx_scope;
6285 endlabel_T **el;
6286
6287 for (;;)
6288 {
6289 if (scope == NULL)
6290 {
6291 emsg(_(e_break));
6292 return NULL;
6293 }
6294 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6295 break;
6296 scope = scope->se_outer;
6297 }
6298
6299 // Jump to the end of the FOR or WHILE loop.
6300 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006301 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006303 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6305 return FAIL;
6306
6307 return arg;
6308}
6309
6310/*
6311 * compile "{" start of block
6312 */
6313 static char_u *
6314compile_block(char_u *arg, cctx_T *cctx)
6315{
6316 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6317 return NULL;
6318 return skipwhite(arg + 1);
6319}
6320
6321/*
6322 * compile end of block: drop one scope
6323 */
6324 static void
6325compile_endblock(cctx_T *cctx)
6326{
6327 scope_T *scope = cctx->ctx_scope;
6328
6329 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006330 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006331 vim_free(scope);
6332}
6333
6334/*
6335 * compile "try"
6336 * Creates a new scope for the try-endtry, pointing to the first catch and
6337 * finally.
6338 * Creates another scope for the "try" block itself.
6339 * TRY instruction sets up exception handling at runtime.
6340 *
6341 * "try"
6342 * TRY -> catch1, -> finally push trystack entry
6343 * ... try block
6344 * "throw {exception}"
6345 * EVAL {exception}
6346 * THROW create exception
6347 * ... try block
6348 * " catch {expr}"
6349 * JUMP -> finally
6350 * catch1: PUSH exeception
6351 * EVAL {expr}
6352 * MATCH
6353 * JUMP nomatch -> catch2
6354 * CATCH remove exception
6355 * ... catch block
6356 * " catch"
6357 * JUMP -> finally
6358 * catch2: CATCH remove exception
6359 * ... catch block
6360 * " finally"
6361 * finally:
6362 * ... finally block
6363 * " endtry"
6364 * ENDTRY pop trystack entry, may rethrow
6365 */
6366 static char_u *
6367compile_try(char_u *arg, cctx_T *cctx)
6368{
6369 garray_T *instr = &cctx->ctx_instr;
6370 scope_T *try_scope;
6371 scope_T *scope;
6372
6373 // scope that holds the jumps that go to catch/finally/endtry
6374 try_scope = new_scope(cctx, TRY_SCOPE);
6375 if (try_scope == NULL)
6376 return NULL;
6377
6378 // "catch" is set when the first ":catch" is found.
6379 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006380 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 if (generate_instr(cctx, ISN_TRY) == NULL)
6382 return NULL;
6383
6384 // scope for the try block itself
6385 scope = new_scope(cctx, BLOCK_SCOPE);
6386 if (scope == NULL)
6387 return NULL;
6388
6389 return arg;
6390}
6391
6392/*
6393 * compile "catch {expr}"
6394 */
6395 static char_u *
6396compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6397{
6398 scope_T *scope = cctx->ctx_scope;
6399 garray_T *instr = &cctx->ctx_instr;
6400 char_u *p;
6401 isn_T *isn;
6402
6403 // end block scope from :try or :catch
6404 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6405 compile_endblock(cctx);
6406 scope = cctx->ctx_scope;
6407
6408 // Error if not in a :try scope
6409 if (scope == NULL || scope->se_type != TRY_SCOPE)
6410 {
6411 emsg(_(e_catch));
6412 return NULL;
6413 }
6414
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006415 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006416 {
6417 emsg(_("E1033: catch unreachable after catch-all"));
6418 return NULL;
6419 }
6420
6421 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006422 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423 JUMP_ALWAYS, cctx) == FAIL)
6424 return NULL;
6425
6426 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006427 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006428 if (isn->isn_arg.try.try_catch == 0)
6429 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006430 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431 {
6432 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006433 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434 isn->isn_arg.jump.jump_where = instr->ga_len;
6435 }
6436
6437 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006438 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006439 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006440 scope->se_u.se_try.ts_caught_all = TRUE;
6441 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442 }
6443 else
6444 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006445 char_u *end;
6446 char_u *pat;
6447 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006448 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006449 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006451 // Push v:exception, push {expr} and MATCH
6452 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6453
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006454 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006455 if (*end != *p)
6456 {
6457 semsg(_("E1067: Separator mismatch: %s"), p);
6458 vim_free(tofree);
6459 return FAIL;
6460 }
6461 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006462 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006463 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006464 len = (int)(end - tofree);
6465 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006466 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006467 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006468 if (pat == NULL)
6469 return FAIL;
6470 if (generate_PUSHS(cctx, pat) == FAIL)
6471 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006473 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6474 return NULL;
6475
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006476 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6478 return NULL;
6479 }
6480
6481 if (generate_instr(cctx, ISN_CATCH) == NULL)
6482 return NULL;
6483
6484 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6485 return NULL;
6486 return p;
6487}
6488
6489 static char_u *
6490compile_finally(char_u *arg, cctx_T *cctx)
6491{
6492 scope_T *scope = cctx->ctx_scope;
6493 garray_T *instr = &cctx->ctx_instr;
6494 isn_T *isn;
6495
6496 // end block scope from :try or :catch
6497 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6498 compile_endblock(cctx);
6499 scope = cctx->ctx_scope;
6500
6501 // Error if not in a :try scope
6502 if (scope == NULL || scope->se_type != TRY_SCOPE)
6503 {
6504 emsg(_(e_finally));
6505 return NULL;
6506 }
6507
6508 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006509 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006510 if (isn->isn_arg.try.try_finally != 0)
6511 {
6512 emsg(_(e_finally_dup));
6513 return NULL;
6514 }
6515
6516 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006517 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518
Bram Moolenaar585fea72020-04-02 22:33:21 +02006519 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006520 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521 {
6522 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006523 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524 isn->isn_arg.jump.jump_where = instr->ga_len;
6525 }
6526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527 // TODO: set index in ts_finally_label jumps
6528
6529 return arg;
6530}
6531
6532 static char_u *
6533compile_endtry(char_u *arg, cctx_T *cctx)
6534{
6535 scope_T *scope = cctx->ctx_scope;
6536 garray_T *instr = &cctx->ctx_instr;
6537 isn_T *isn;
6538
6539 // end block scope from :catch or :finally
6540 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6541 compile_endblock(cctx);
6542 scope = cctx->ctx_scope;
6543
6544 // Error if not in a :try scope
6545 if (scope == NULL || scope->se_type != TRY_SCOPE)
6546 {
6547 if (scope == NULL)
6548 emsg(_(e_no_endtry));
6549 else if (scope->se_type == WHILE_SCOPE)
6550 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006551 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552 emsg(_(e_endfor));
6553 else
6554 emsg(_(e_endif));
6555 return NULL;
6556 }
6557
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006558 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006559 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6560 {
6561 emsg(_("E1032: missing :catch or :finally"));
6562 return NULL;
6563 }
6564
6565 // Fill in the "end" label in jumps at the end of the blocks, if not done
6566 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006567 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006568
6569 // End :catch or :finally scope: set value in ISN_TRY instruction
6570 if (isn->isn_arg.try.try_finally == 0)
6571 isn->isn_arg.try.try_finally = instr->ga_len;
6572 compile_endblock(cctx);
6573
6574 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6575 return NULL;
6576 return arg;
6577}
6578
6579/*
6580 * compile "throw {expr}"
6581 */
6582 static char_u *
6583compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6584{
6585 char_u *p = skipwhite(arg);
6586
Bram Moolenaara5565e42020-05-09 15:44:01 +02006587 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588 return NULL;
6589 if (may_generate_2STRING(-1, cctx) == FAIL)
6590 return NULL;
6591 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6592 return NULL;
6593
6594 return p;
6595}
6596
6597/*
6598 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006599 * compile "echomsg expr"
6600 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006601 * compile "execute expr"
6602 */
6603 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006604compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006605{
6606 char_u *p = arg;
6607 int count = 0;
6608
6609 for (;;)
6610 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006611 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006612 return NULL;
6613 ++count;
6614 p = skipwhite(p);
6615 if (ends_excmd(*p))
6616 break;
6617 }
6618
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006619 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6620 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6621 else if (cmdidx == CMD_execute)
6622 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6623 else if (cmdidx == CMD_echomsg)
6624 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6625 else
6626 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006627 return p;
6628}
6629
6630/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006631 * A command that is not compiled, execute with legacy code.
6632 */
6633 static char_u *
6634compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6635{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006636 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006637 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006638 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006639
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006640 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006641 goto theend;
6642
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006643 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006644 {
6645 long argt = excmd_get_argt(eap->cmdidx);
6646 int usefilter = FALSE;
6647
6648 has_expr = argt & (EX_XFILE | EX_EXPAND);
6649
6650 // If the command can be followed by a bar, find the bar and truncate
6651 // it, so that the following command can be compiled.
6652 // The '|' is overwritten with a NUL, it is put back below.
6653 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6654 && *eap->arg == '!')
6655 // :w !filter or :r !filter or :r! filter
6656 usefilter = TRUE;
6657 if ((argt & EX_TRLBAR) && !usefilter)
6658 {
6659 separate_nextcmd(eap);
6660 if (eap->nextcmd != NULL)
6661 nextcmd = eap->nextcmd;
6662 }
6663 }
6664
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006665 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6666 {
6667 // expand filename in "syntax include [@group] filename"
6668 has_expr = TRUE;
6669 eap->arg = skipwhite(eap->arg + 7);
6670 if (*eap->arg == '@')
6671 eap->arg = skiptowhite(eap->arg);
6672 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006673
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006674 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006675 {
6676 int count = 0;
6677 char_u *start = skipwhite(line);
6678
6679 // :cmd xxx`=expr1`yyy`=expr2`zzz
6680 // PUSHS ":cmd xxx"
6681 // eval expr1
6682 // PUSHS "yyy"
6683 // eval expr2
6684 // PUSHS "zzz"
6685 // EXECCONCAT 5
6686 for (;;)
6687 {
6688 if (p > start)
6689 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006690 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006691 ++count;
6692 }
6693 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006694 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006695 return NULL;
6696 may_generate_2STRING(-1, cctx);
6697 ++count;
6698 p = skipwhite(p);
6699 if (*p != '`')
6700 {
6701 emsg(_("E1083: missing backtick"));
6702 return NULL;
6703 }
6704 start = p + 1;
6705
6706 p = (char_u *)strstr((char *)start, "`=");
6707 if (p == NULL)
6708 {
6709 if (*skipwhite(start) != NUL)
6710 {
6711 generate_PUSHS(cctx, vim_strsave(start));
6712 ++count;
6713 }
6714 break;
6715 }
6716 }
6717 generate_EXECCONCAT(cctx, count);
6718 }
6719 else
6720 generate_EXEC(cctx, line);
6721
6722theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006723 if (*nextcmd != NUL)
6724 {
6725 // the parser expects a pointer to the bar, put it back
6726 --nextcmd;
6727 *nextcmd = '|';
6728 }
6729
6730 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006731}
6732
6733/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006734 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006735 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006736 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006737 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006738add_def_function(ufunc_T *ufunc)
6739{
6740 dfunc_T *dfunc;
6741
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006742 if (def_functions.ga_len == 0)
6743 {
6744 // The first position is not used, so that a zero uf_dfunc_idx means it
6745 // wasn't set.
6746 if (ga_grow(&def_functions, 1) == FAIL)
6747 return FAIL;
6748 ++def_functions.ga_len;
6749 }
6750
Bram Moolenaar09689a02020-05-09 22:50:08 +02006751 // Add the function to "def_functions".
6752 if (ga_grow(&def_functions, 1) == FAIL)
6753 return FAIL;
6754 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6755 CLEAR_POINTER(dfunc);
6756 dfunc->df_idx = def_functions.ga_len;
6757 ufunc->uf_dfunc_idx = dfunc->df_idx;
6758 dfunc->df_ufunc = ufunc;
6759 ++def_functions.ga_len;
6760 return OK;
6761}
6762
6763/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 * After ex_function() has collected all the function lines: parse and compile
6765 * the lines into instructions.
6766 * Adds the function to "def_functions".
6767 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6768 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006769 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006770 * This can be used recursively through compile_lambda(), which may reallocate
6771 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006772 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006773 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006774 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006775compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006776{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 char_u *line = NULL;
6778 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 cctx_T cctx;
6781 garray_T *instr;
6782 int called_emsg_before = called_emsg;
6783 int ret = FAIL;
6784 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006785 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006786 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006787
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006788 // When using a function that was compiled before: Free old instructions.
6789 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006790 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006792 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6793 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006794 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006796 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006797 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006798
Bram Moolenaara80faa82020-04-12 19:37:17 +02006799 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006800 cctx.ctx_ufunc = ufunc;
6801 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006802 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6804 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6805 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6806 cctx.ctx_type_list = &ufunc->uf_type_list;
6807 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6808 instr = &cctx.ctx_instr;
6809
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006810 // Set the context to the function, it may be compiled when called from
6811 // another script. Set the script version to the most modern one.
6812 // The line number will be set in next_line_from_context().
6813 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006814 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6815
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006816 // Make sure error messages are OK.
6817 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6818 if (do_estack_push)
6819 estack_push_ufunc(ufunc, 1);
6820
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006821 if (ufunc->uf_def_args.ga_len > 0)
6822 {
6823 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006824 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006825 int i;
6826 char_u *arg;
6827 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6828
6829 // Produce instructions for the default values of optional arguments.
6830 // Store the instruction index in uf_def_arg_idx[] so that we know
6831 // where to start when the function is called, depending on the number
6832 // of arguments.
6833 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6834 if (ufunc->uf_def_arg_idx == NULL)
6835 goto erret;
6836 for (i = 0; i < count; ++i)
6837 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006838 garray_T *stack = &cctx.ctx_type_stack;
6839 type_T *val_type;
6840 int arg_idx = first_def_arg + i;
6841
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006842 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6843 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006844 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006845 goto erret;
6846
6847 // If no type specified use the type of the default value.
6848 // Otherwise check that the default value type matches the
6849 // specified type.
6850 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6851 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6852 ufunc->uf_arg_types[arg_idx] = val_type;
6853 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6854 == FAIL)
6855 {
6856 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6857 arg_idx + 1);
6858 goto erret;
6859 }
6860
6861 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006862 goto erret;
6863 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006864 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6865 }
6866
6867 /*
6868 * Loop over all the lines of the function and generate instructions.
6869 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870 for (;;)
6871 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006872 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006873 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006874 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006875 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006876
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006877 // Bail out on the first error to avoid a flood of errors and report
6878 // the right line number when inside try/catch.
6879 if (emsg_before != called_emsg)
6880 goto erret;
6881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 if (line != NULL && *line == '|')
6883 // the line continues after a '|'
6884 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006885 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006886 && !(*line == '#' && (line == cctx.ctx_line_start
6887 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006888 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006889 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890 goto erret;
6891 }
6892 else
6893 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006894 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006895 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006896 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006898 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006899 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900
Bram Moolenaara80faa82020-04-12 19:37:17 +02006901 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006902 ea.cmdlinep = &line;
6903 ea.cmd = skipwhite(line);
6904
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006905 // Some things can be recognized by the first character.
6906 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006907 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006908 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006909 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006910 if (ea.cmd[1] != '{')
6911 {
6912 line = (char_u *)"";
6913 continue;
6914 }
6915 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006916
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006917 case '}':
6918 {
6919 // "}" ends a block scope
6920 scopetype_T stype = cctx.ctx_scope == NULL
6921 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006923 if (stype == BLOCK_SCOPE)
6924 {
6925 compile_endblock(&cctx);
6926 line = ea.cmd;
6927 }
6928 else
6929 {
6930 emsg(_("E1025: using } outside of a block scope"));
6931 goto erret;
6932 }
6933 if (line != NULL)
6934 line = skipwhite(ea.cmd + 1);
6935 continue;
6936 }
6937
6938 case '{':
6939 // "{" starts a block scope
6940 // "{'a': 1}->func() is something else
6941 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6942 {
6943 line = compile_block(ea.cmd, &cctx);
6944 continue;
6945 }
6946 break;
6947
6948 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006949 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006950 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006951 }
6952
6953 /*
6954 * COMMAND MODIFIERS
6955 */
6956 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6957 {
6958 if (errormsg != NULL)
6959 goto erret;
6960 // empty line or comment
6961 line = (char_u *)"";
6962 continue;
6963 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006964 // TODO: use modifiers in the command
6965 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02006966 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967
6968 // Skip ":call" to get to the function name.
6969 if (checkforcmd(&ea.cmd, "call", 3))
6970 ea.cmd = skipwhite(ea.cmd);
6971
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006972 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006974 char_u *pskip;
6975
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006976 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006977 // find what follows.
6978 // Skip over "var.member", "var[idx]" and the like.
6979 // Also "&opt = val", "$ENV = val" and "@r = val".
6980 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006981 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006982 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006983 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006985 char_u *var_end;
6986 int oplen;
6987 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006989 var_end = find_name_end(pskip, NULL, NULL,
6990 FNE_CHECK_START | FNE_INCL_BR);
6991 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006992 if (oplen > 0)
6993 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006994 size_t len = p - ea.cmd;
6995
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006996 // Recognize an assignment if we recognize the variable
6997 // name:
6998 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006999 // "local = expr" where "local" is a local var.
7000 // "script = expr" where "script" is a script-local var.
7001 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007002 // "&opt = expr"
7003 // "$ENV = expr"
7004 // "@r = expr"
7005 if (*ea.cmd == '&'
7006 || *ea.cmd == '$'
7007 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007008 || ((len) > 2 && ea.cmd[1] == ':')
7009 || lookup_local(ea.cmd, len, &cctx) != NULL
7010 || lookup_arg(ea.cmd, len, NULL, NULL,
7011 NULL, &cctx) == OK
7012 || lookup_script(ea.cmd, len) == OK
7013 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007014 {
7015 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007016 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007017 goto erret;
7018 continue;
7019 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020 }
7021 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007022
7023 if (*ea.cmd == '[')
7024 {
7025 // [var, var] = expr
7026 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7027 if (line == NULL)
7028 goto erret;
7029 if (line != ea.cmd)
7030 continue;
7031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032 }
7033
7034 /*
7035 * COMMAND after range
7036 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007037 cmd = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007039 if (ea.cmd > cmd && !starts_with_colon)
7040 {
7041 emsg(_(e_colon_required));
7042 goto erret;
7043 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007044 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007045 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007046 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047
7048 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7049 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007050 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007051 {
7052 line += STRLEN(line);
7053 continue;
7054 }
7055
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007057 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007059 // CMD_let cannot happen, compile_assignment() above is used
7060 iemsg("Command from find_ex_command() not handled");
7061 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063 }
7064
7065 p = skipwhite(p);
7066
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007067 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007068 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007069 && ea.cmdidx != CMD_elseif
7070 && ea.cmdidx != CMD_else
7071 && ea.cmdidx != CMD_endif)
7072 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007073 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007074 continue;
7075 }
7076
Bram Moolenaarefd88552020-06-18 20:50:10 +02007077 if (ea.cmdidx != CMD_elseif
7078 && ea.cmdidx != CMD_else
7079 && ea.cmdidx != CMD_endif
7080 && ea.cmdidx != CMD_endfor
7081 && ea.cmdidx != CMD_endwhile
7082 && ea.cmdidx != CMD_catch
7083 && ea.cmdidx != CMD_finally
7084 && ea.cmdidx != CMD_endtry)
7085 {
7086 if (cctx.ctx_had_return)
7087 {
7088 emsg(_("E1095: Unreachable code after :return"));
7089 goto erret;
7090 }
7091 }
7092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093 switch (ea.cmdidx)
7094 {
7095 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007096 ea.arg = p;
7097 line = compile_nested_function(&ea, &cctx);
7098 break;
7099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007101 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102 goto erret;
7103
7104 case CMD_return:
7105 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007106 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 break;
7108
7109 case CMD_let:
7110 case CMD_const:
7111 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007112 if (line == p)
7113 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114 break;
7115
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007116 case CMD_unlet:
7117 case CMD_unlockvar:
7118 case CMD_lockvar:
7119 line = compile_unletlock(p, &ea, &cctx);
7120 break;
7121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007122 case CMD_import:
7123 line = compile_import(p, &cctx);
7124 break;
7125
7126 case CMD_if:
7127 line = compile_if(p, &cctx);
7128 break;
7129 case CMD_elseif:
7130 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007131 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132 break;
7133 case CMD_else:
7134 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007135 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136 break;
7137 case CMD_endif:
7138 line = compile_endif(p, &cctx);
7139 break;
7140
7141 case CMD_while:
7142 line = compile_while(p, &cctx);
7143 break;
7144 case CMD_endwhile:
7145 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007146 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147 break;
7148
7149 case CMD_for:
7150 line = compile_for(p, &cctx);
7151 break;
7152 case CMD_endfor:
7153 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007154 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 break;
7156 case CMD_continue:
7157 line = compile_continue(p, &cctx);
7158 break;
7159 case CMD_break:
7160 line = compile_break(p, &cctx);
7161 break;
7162
7163 case CMD_try:
7164 line = compile_try(p, &cctx);
7165 break;
7166 case CMD_catch:
7167 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007168 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169 break;
7170 case CMD_finally:
7171 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007172 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173 break;
7174 case CMD_endtry:
7175 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007176 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 break;
7178 case CMD_throw:
7179 line = compile_throw(p, &cctx);
7180 break;
7181
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007182 case CMD_eval:
7183 if (compile_expr0(&p, &cctx) == FAIL)
7184 goto erret;
7185
7186 // drop the return value
7187 generate_instr_drop(&cctx, ISN_DROP, 1);
7188
7189 line = skipwhite(p);
7190 break;
7191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007193 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007194 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007195 case CMD_echomsg:
7196 case CMD_echoerr:
7197 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007198 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007199
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007200 // TODO: other commands with an expression argument
7201
Bram Moolenaar002262f2020-07-08 17:47:57 +02007202 case CMD_SIZE:
7203 semsg(_("E476: Invalid command: %s"), ea.cmd);
7204 goto erret;
7205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007206 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007207 // Not recognized, execute with do_cmdline_cmd().
7208 ea.arg = p;
7209 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007210 break;
7211 }
7212 if (line == NULL)
7213 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007214 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215
7216 if (cctx.ctx_type_stack.ga_len < 0)
7217 {
7218 iemsg("Type stack underflow");
7219 goto erret;
7220 }
7221 }
7222
7223 if (cctx.ctx_scope != NULL)
7224 {
7225 if (cctx.ctx_scope->se_type == IF_SCOPE)
7226 emsg(_(e_endif));
7227 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7228 emsg(_(e_endwhile));
7229 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7230 emsg(_(e_endfor));
7231 else
7232 emsg(_("E1026: Missing }"));
7233 goto erret;
7234 }
7235
Bram Moolenaarefd88552020-06-18 20:50:10 +02007236 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007237 {
7238 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7239 {
7240 emsg(_("E1027: Missing return statement"));
7241 goto erret;
7242 }
7243
7244 // Return zero if there is no return at the end.
7245 generate_PUSHNR(&cctx, 0);
7246 generate_instr(&cctx, ISN_RETURN);
7247 }
7248
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007249 {
7250 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7251 + ufunc->uf_dfunc_idx;
7252 dfunc->df_deleted = FALSE;
7253 dfunc->df_instr = instr->ga_data;
7254 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007255 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007256 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007257 if (cctx.ctx_outer_used)
7258 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007259 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007260 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261
7262 ret = OK;
7263
7264erret:
7265 if (ret == FAIL)
7266 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007267 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007268 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7269 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007270
7271 for (idx = 0; idx < instr->ga_len; ++idx)
7272 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007274
Bram Moolenaar45a15082020-05-25 00:28:33 +02007275 // if using the last entry in the table we might as well remove it
7276 if (!dfunc->df_deleted
7277 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007278 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007279 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007280
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007281 while (cctx.ctx_scope != NULL)
7282 drop_scope(&cctx);
7283
Bram Moolenaar20431c92020-03-20 18:39:46 +01007284 // Don't execute this function body.
7285 ga_clear_strings(&ufunc->uf_lines);
7286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007287 if (errormsg != NULL)
7288 emsg(errormsg);
7289 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007290 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007291 }
7292
7293 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007294 if (do_estack_push)
7295 estack_pop();
7296
Bram Moolenaar20431c92020-03-20 18:39:46 +01007297 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007298 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007299 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007300 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301}
7302
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007303 void
7304set_function_type(ufunc_T *ufunc)
7305{
7306 int varargs = ufunc->uf_va_name != NULL;
7307 int argcount = ufunc->uf_args.ga_len;
7308
7309 // Create a type for the function, with the return type and any
7310 // argument types.
7311 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7312 // The type is included in "tt_args".
7313 if (argcount > 0 || varargs)
7314 {
7315 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7316 argcount, &ufunc->uf_type_list);
7317 // Add argument types to the function type.
7318 if (func_type_add_arg_types(ufunc->uf_func_type,
7319 argcount + varargs,
7320 &ufunc->uf_type_list) == FAIL)
7321 return;
7322 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7323 ufunc->uf_func_type->tt_min_argcount =
7324 argcount - ufunc->uf_def_args.ga_len;
7325 if (ufunc->uf_arg_types == NULL)
7326 {
7327 int i;
7328
7329 // lambda does not have argument types.
7330 for (i = 0; i < argcount; ++i)
7331 ufunc->uf_func_type->tt_args[i] = &t_any;
7332 }
7333 else
7334 mch_memmove(ufunc->uf_func_type->tt_args,
7335 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7336 if (varargs)
7337 {
7338 ufunc->uf_func_type->tt_args[argcount] =
7339 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7340 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7341 }
7342 }
7343 else
7344 // No arguments, can use a predefined type.
7345 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7346 argcount, &ufunc->uf_type_list);
7347}
7348
7349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350/*
7351 * Delete an instruction, free what it contains.
7352 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007353 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354delete_instr(isn_T *isn)
7355{
7356 switch (isn->isn_type)
7357 {
7358 case ISN_EXEC:
7359 case ISN_LOADENV:
7360 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007361 case ISN_LOADB:
7362 case ISN_LOADW:
7363 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007365 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 case ISN_PUSHEXC:
7367 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007368 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007370 case ISN_STOREB:
7371 case ISN_STOREW:
7372 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007373 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007374 vim_free(isn->isn_arg.string);
7375 break;
7376
7377 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007378 case ISN_STORES:
7379 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 break;
7381
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007382 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007383 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007384 vim_free(isn->isn_arg.unlet.ul_name);
7385 break;
7386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 case ISN_STOREOPT:
7388 vim_free(isn->isn_arg.storeopt.so_name);
7389 break;
7390
7391 case ISN_PUSHBLOB: // push blob isn_arg.blob
7392 blob_unref(isn->isn_arg.blob);
7393 break;
7394
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007395 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007396#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007397 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007398#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007399 break;
7400
7401 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007402#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007403 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007404#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007405 break;
7406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 case ISN_UCALL:
7408 vim_free(isn->isn_arg.ufunc.cuf_name);
7409 break;
7410
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007411 case ISN_FUNCREF:
7412 {
7413 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7414 + isn->isn_arg.funcref.fr_func;
7415 func_ptr_unref(dfunc->df_ufunc);
7416 }
7417 break;
7418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419 case ISN_2BOOL:
7420 case ISN_2STRING:
7421 case ISN_ADDBLOB:
7422 case ISN_ADDLIST:
7423 case ISN_BCALL:
7424 case ISN_CATCH:
7425 case ISN_CHECKNR:
7426 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007427 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428 case ISN_COMPAREANY:
7429 case ISN_COMPAREBLOB:
7430 case ISN_COMPAREBOOL:
7431 case ISN_COMPAREDICT:
7432 case ISN_COMPAREFLOAT:
7433 case ISN_COMPAREFUNC:
7434 case ISN_COMPARELIST:
7435 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007436 case ISN_COMPARESPECIAL:
7437 case ISN_COMPARESTRING:
7438 case ISN_CONCAT:
7439 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007440 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441 case ISN_DROP:
7442 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007443 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007444 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007446 case ISN_EXECCONCAT:
7447 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007448 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007450 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007451 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007452 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 case ISN_JUMP:
7454 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007455 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456 case ISN_LOADSCRIPT:
7457 case ISN_LOADREG:
7458 case ISN_LOADV:
7459 case ISN_NEGATENR:
7460 case ISN_NEWDICT:
7461 case ISN_NEWLIST:
7462 case ISN_OPNR:
7463 case ISN_OPFLOAT:
7464 case ISN_OPANY:
7465 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007466 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 case ISN_PUSHF:
7468 case ISN_PUSHNR:
7469 case ISN_PUSHBOOL:
7470 case ISN_PUSHSPEC:
7471 case ISN_RETURN:
7472 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007473 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007474 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007476 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007478 case ISN_STOREDICT:
7479 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 case ISN_THROW:
7481 case ISN_TRY:
7482 // nothing allocated
7483 break;
7484 }
7485}
7486
7487/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007488 * Free all instructions for "dfunc".
7489 */
7490 static void
7491delete_def_function_contents(dfunc_T *dfunc)
7492{
7493 int idx;
7494
7495 ga_clear(&dfunc->df_def_args_isn);
7496
7497 if (dfunc->df_instr != NULL)
7498 {
7499 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7500 delete_instr(dfunc->df_instr + idx);
7501 VIM_CLEAR(dfunc->df_instr);
7502 }
7503
7504 dfunc->df_deleted = TRUE;
7505}
7506
7507/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007508 * When a user function is deleted, clear the contents of any associated def
7509 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510 */
7511 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007512clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007514 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515 {
7516 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7517 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518
Bram Moolenaar20431c92020-03-20 18:39:46 +01007519 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007520 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521 }
7522}
7523
7524#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007525/*
7526 * Free all functions defined with ":def".
7527 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007528 void
7529free_def_functions(void)
7530{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007531 int idx;
7532
7533 for (idx = 0; idx < def_functions.ga_len; ++idx)
7534 {
7535 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7536
7537 delete_def_function_contents(dfunc);
7538 }
7539
7540 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541}
7542#endif
7543
7544
7545#endif // FEAT_EVAL