blob: 3c0336ac4ae68c1192b46ed43181d73a6f861ff3 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
146static char e_var_notfound[] = N_("E1001: variable not found: %s");
147static char e_syntax_at[] = N_("E1002: Syntax error at %s");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200148static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200149static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150
Bram Moolenaar20431c92020-03-20 18:39:46 +0100151static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200152static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153
154/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200155 * Lookup variable "name" in the local scope and return it.
156 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200158 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159lookup_local(char_u *name, size_t len, cctx_T *cctx)
160{
161 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100164 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200165 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166
167 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
169 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 if (STRNCMP(name, lvar->lv_name, len) == 0
172 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200173 {
174 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200175 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200178
179 // Find local in outer function scope.
180 if (cctx->ctx_outer != NULL)
181 {
182 lvar = lookup_local(name, len, cctx->ctx_outer);
183 if (lvar != NULL)
184 {
185 // TODO: are there situations we should not mark the outer scope as
186 // used?
187 cctx->ctx_outer_used = TRUE;
188 lvar->lv_from_outer = TRUE;
189 return lvar;
190 }
191 }
192
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100194}
195
196/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200197 * Lookup an argument in the current function and an enclosing function.
198 * Returns the argument index in "idxp"
199 * Returns the argument type in "type"
200 * Sets "gen_load_outer" to TRUE if found in outer scope.
201 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202 */
203 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200204lookup_arg(
205 char_u *name,
206 size_t len,
207 int *idxp,
208 type_T **type,
209 int *gen_load_outer,
210 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211{
212 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200213 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100215 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200216 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
218 {
219 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
220
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200221 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
222 {
223 if (idxp != NULL)
224 {
225 // Arguments are located above the frame pointer. One further
226 // if there is a vararg argument
227 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
228 + STACK_FRAME_SIZE)
229 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
230
231 if (cctx->ctx_ufunc->uf_arg_types != NULL)
232 *type = cctx->ctx_ufunc->uf_arg_types[idx];
233 else
234 *type = &t_any;
235 }
236 return OK;
237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200240 va_name = cctx->ctx_ufunc->uf_va_name;
241 if (va_name != NULL
242 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
243 {
244 if (idxp != NULL)
245 {
246 // varargs is always the last argument
247 *idxp = -STACK_FRAME_SIZE - 1;
248 *type = cctx->ctx_ufunc->uf_va_type;
249 }
250 return OK;
251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200253 if (cctx->ctx_outer != NULL)
254 {
255 // Lookup the name for an argument of the outer function.
256 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
257 == OK)
258 {
259 *gen_load_outer = TRUE;
260 return OK;
261 }
262 }
263
264 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265}
266
267/*
268 * Lookup a variable in the current script.
269 * Returns OK or FAIL.
270 */
271 static int
272lookup_script(char_u *name, size_t len)
273{
274 int cc;
275 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
276 dictitem_T *di;
277
278 cc = name[len];
279 name[len] = NUL;
280 di = find_var_in_ht(ht, 0, name, TRUE);
281 name[len] = cc;
282 return di == NULL ? FAIL: OK;
283}
284
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100285/*
286 * Check if "p[len]" is already defined, either in script "import_sid" or in
287 * compilation context "cctx".
288 * Return FAIL and give an error if it defined.
289 */
290 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200291check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100292{
293 if (lookup_script(p, len) == OK
294 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200295 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100296 || find_imported(p, len, cctx) != NULL)))
297 {
298 semsg("E1073: imported name already defined: %s", p);
299 return FAIL;
300 }
301 return OK;
302}
303
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200304/*
305 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
306 * be freed later.
307 */
308 static type_T *
309alloc_type(garray_T *type_gap)
310{
311 type_T *type;
312
313 if (ga_grow(type_gap, 1) == FAIL)
314 return NULL;
315 type = ALLOC_CLEAR_ONE(type_T);
316 if (type != NULL)
317 {
318 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
319 ++type_gap->ga_len;
320 }
321 return type;
322}
323
Bram Moolenaar6110e792020-07-08 19:35:21 +0200324 void
325clear_type_list(garray_T *gap)
326{
327 while (gap->ga_len > 0)
328 vim_free(((type_T **)gap->ga_data)[--gap->ga_len]);
329 ga_clear(gap);
330}
331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200333get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334{
335 type_T *type;
336
337 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200338 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100339 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200340 if (member_type->tt_type == VAR_VOID
341 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100342 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100343 if (member_type->tt_type == VAR_BOOL)
344 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100345 if (member_type->tt_type == VAR_NUMBER)
346 return &t_list_number;
347 if (member_type->tt_type == VAR_STRING)
348 return &t_list_string;
349
350 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200351 type = alloc_type(type_gap);
352 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100353 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100354 type->tt_type = VAR_LIST;
355 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200356 type->tt_argcount = 0;
357 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358 return type;
359}
360
361 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200362get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363{
364 type_T *type;
365
366 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200367 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100368 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200369 if (member_type->tt_type == VAR_VOID
370 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100371 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100372 if (member_type->tt_type == VAR_BOOL)
373 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100374 if (member_type->tt_type == VAR_NUMBER)
375 return &t_dict_number;
376 if (member_type->tt_type == VAR_STRING)
377 return &t_dict_string;
378
379 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200380 type = alloc_type(type_gap);
381 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100382 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 type->tt_type = VAR_DICT;
384 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200385 type->tt_argcount = 0;
386 type->tt_args = NULL;
387 return type;
388}
389
390/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200391 * Allocate a new type for a function.
392 */
393 static type_T *
394alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
395{
396 type_T *type = alloc_type(type_gap);
397
398 if (type == NULL)
399 return &t_any;
400 type->tt_type = VAR_FUNC;
401 type->tt_member = ret_type;
402 type->tt_argcount = argcount;
403 type->tt_args = NULL;
404 return type;
405}
406
407/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200408 * Get a function type, based on the return type "ret_type".
409 * If "argcount" is -1 or 0 a predefined type can be used.
410 * If "argcount" > 0 always create a new type, so that arguments can be added.
411 */
412 static type_T *
413get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
414{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200415 // recognize commonly used types
416 if (argcount <= 0)
417 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200418 if (ret_type == &t_unknown)
419 {
420 // (argcount == 0) is not possible
421 return &t_func_unknown;
422 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200423 if (ret_type == &t_void)
424 {
425 if (argcount == 0)
426 return &t_func_0_void;
427 else
428 return &t_func_void;
429 }
430 if (ret_type == &t_any)
431 {
432 if (argcount == 0)
433 return &t_func_0_any;
434 else
435 return &t_func_any;
436 }
437 if (ret_type == &t_number)
438 {
439 if (argcount == 0)
440 return &t_func_0_number;
441 else
442 return &t_func_number;
443 }
444 if (ret_type == &t_string)
445 {
446 if (argcount == 0)
447 return &t_func_0_string;
448 else
449 return &t_func_string;
450 }
451 }
452
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200453 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454}
455
Bram Moolenaara8c17702020-04-01 21:17:24 +0200456/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200457 * For a function type, reserve space for "argcount" argument types (including
458 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200459 */
460 static int
461func_type_add_arg_types(
462 type_T *functype,
463 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200464 garray_T *type_gap)
465{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200466 // To make it easy to free the space needed for the argument types, add the
467 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200468 if (ga_grow(type_gap, 1) == FAIL)
469 return FAIL;
470 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
471 if (functype->tt_args == NULL)
472 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200473 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
474 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200475 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 return OK;
477}
478
479/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200480 * Return the type_T for a typval. Only for primitive types.
481 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200482 type_T *
Bram Moolenaara8c17702020-04-01 21:17:24 +0200483typval2type(typval_T *tv)
484{
485 if (tv->v_type == VAR_NUMBER)
486 return &t_number;
487 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200488 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200489 if (tv->v_type == VAR_STRING)
490 return &t_string;
491 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
492 return &t_list_string;
493 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
494 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200495 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200496}
497
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200498 static void
499type_mismatch(type_T *expected, type_T *actual)
500{
501 char *tofree1, *tofree2;
502
503 semsg(_("E1013: type mismatch, expected %s but got %s"),
504 type_name(expected, &tofree1), type_name(actual, &tofree2));
505 vim_free(tofree1);
506 vim_free(tofree2);
507}
508
509 static void
510arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
511{
512 char *tofree1, *tofree2;
513
514 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
515 argidx,
516 type_name(expected, &tofree1), type_name(actual, &tofree2));
517 vim_free(tofree1);
518 vim_free(tofree2);
519}
520
521/*
522 * Check if the expected and actual types match.
523 * Does not allow for assigning "any" to a specific type.
524 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200525 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200526check_type(type_T *expected, type_T *actual, int give_msg)
527{
528 int ret = OK;
529
530 // When expected is "unknown" we accept any actual type.
531 // When expected is "any" we accept any actual type except "void".
532 if (expected->tt_type != VAR_UNKNOWN
533 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
534
535 {
536 if (expected->tt_type != actual->tt_type)
537 {
538 if (give_msg)
539 type_mismatch(expected, actual);
540 return FAIL;
541 }
542 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
543 {
544 // "unknown" is used for an empty list or dict
545 if (actual->tt_member != &t_unknown)
546 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
547 }
548 else if (expected->tt_type == VAR_FUNC)
549 {
550 if (expected->tt_member != &t_unknown)
551 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
552 if (ret == OK && expected->tt_argcount != -1
553 && (actual->tt_argcount < expected->tt_min_argcount
554 || actual->tt_argcount > expected->tt_argcount))
555 ret = FAIL;
556 }
557 if (ret == FAIL && give_msg)
558 type_mismatch(expected, actual);
559 }
560 return ret;
561}
562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563/////////////////////////////////////////////////////////////////////
564// Following generate_ functions expect the caller to call ga_grow().
565
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200566#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
567#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569/*
570 * Generate an instruction without arguments.
571 * Returns a pointer to the new instruction, NULL if failed.
572 */
573 static isn_T *
574generate_instr(cctx_T *cctx, isntype_T isn_type)
575{
576 garray_T *instr = &cctx->ctx_instr;
577 isn_T *isn;
578
Bram Moolenaar080457c2020-03-03 21:53:32 +0100579 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 if (ga_grow(instr, 1) == FAIL)
581 return NULL;
582 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
583 isn->isn_type = isn_type;
584 isn->isn_lnum = cctx->ctx_lnum + 1;
585 ++instr->ga_len;
586
587 return isn;
588}
589
590/*
591 * Generate an instruction without arguments.
592 * "drop" will be removed from the stack.
593 * Returns a pointer to the new instruction, NULL if failed.
594 */
595 static isn_T *
596generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
597{
598 garray_T *stack = &cctx->ctx_type_stack;
599
Bram Moolenaar080457c2020-03-03 21:53:32 +0100600 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 stack->ga_len -= drop;
602 return generate_instr(cctx, isn_type);
603}
604
605/*
606 * Generate instruction "isn_type" and put "type" on the type stack.
607 */
608 static isn_T *
609generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
610{
611 isn_T *isn;
612 garray_T *stack = &cctx->ctx_type_stack;
613
614 if ((isn = generate_instr(cctx, isn_type)) == NULL)
615 return NULL;
616
617 if (ga_grow(stack, 1) == FAIL)
618 return NULL;
619 ((type_T **)stack->ga_data)[stack->ga_len] = type;
620 ++stack->ga_len;
621
622 return isn;
623}
624
625/*
626 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
627 */
628 static int
629may_generate_2STRING(int offset, cctx_T *cctx)
630{
631 isn_T *isn;
632 garray_T *stack = &cctx->ctx_type_stack;
633 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
634
635 if ((*type)->tt_type == VAR_STRING)
636 return OK;
637 *type = &t_string;
638
639 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
640 return FAIL;
641 isn->isn_arg.number = offset;
642
643 return OK;
644}
645
646 static int
647check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
648{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200649 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200651 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 {
653 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200654 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 else
656 semsg(_("E1036: %c requires number or float arguments"), *op);
657 return FAIL;
658 }
659 return OK;
660}
661
662/*
663 * Generate an instruction with two arguments. The instruction depends on the
664 * type of the arguments.
665 */
666 static int
667generate_two_op(cctx_T *cctx, char_u *op)
668{
669 garray_T *stack = &cctx->ctx_type_stack;
670 type_T *type1;
671 type_T *type2;
672 vartype_T vartype;
673 isn_T *isn;
674
Bram Moolenaar080457c2020-03-03 21:53:32 +0100675 RETURN_OK_IF_SKIP(cctx);
676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 // Get the known type of the two items on the stack. If they are matching
678 // use a type-specific instruction. Otherwise fall back to runtime type
679 // checking.
680 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
681 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 if (type1->tt_type == type2->tt_type
684 && (type1->tt_type == VAR_NUMBER
685 || type1->tt_type == VAR_LIST
686#ifdef FEAT_FLOAT
687 || type1->tt_type == VAR_FLOAT
688#endif
689 || type1->tt_type == VAR_BLOB))
690 vartype = type1->tt_type;
691
692 switch (*op)
693 {
694 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200695 && type1->tt_type != VAR_ANY
696 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 && check_number_or_float(
698 type1->tt_type, type2->tt_type, op) == FAIL)
699 return FAIL;
700 isn = generate_instr_drop(cctx,
701 vartype == VAR_NUMBER ? ISN_OPNR
702 : vartype == VAR_LIST ? ISN_ADDLIST
703 : vartype == VAR_BLOB ? ISN_ADDBLOB
704#ifdef FEAT_FLOAT
705 : vartype == VAR_FLOAT ? ISN_OPFLOAT
706#endif
707 : ISN_OPANY, 1);
708 if (isn != NULL)
709 isn->isn_arg.op.op_type = EXPR_ADD;
710 break;
711
712 case '-':
713 case '*':
714 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
715 op) == FAIL)
716 return FAIL;
717 if (vartype == VAR_NUMBER)
718 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
719#ifdef FEAT_FLOAT
720 else if (vartype == VAR_FLOAT)
721 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
722#endif
723 else
724 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
725 if (isn != NULL)
726 isn->isn_arg.op.op_type = *op == '*'
727 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
728 break;
729
Bram Moolenaar4c683752020-04-05 21:38:23 +0200730 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200732 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 && type2->tt_type != VAR_NUMBER))
734 {
735 emsg(_("E1035: % requires number arguments"));
736 return FAIL;
737 }
738 isn = generate_instr_drop(cctx,
739 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
740 if (isn != NULL)
741 isn->isn_arg.op.op_type = EXPR_REM;
742 break;
743 }
744
745 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200746 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 {
748 type_T *type = &t_any;
749
750#ifdef FEAT_FLOAT
751 // float+number and number+float results in float
752 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
753 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
754 type = &t_float;
755#endif
756 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
757 }
758
759 return OK;
760}
761
762/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200763 * Get the instruction to use for comparing "type1" with "type2"
764 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200766 static isntype_T
767get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768{
769 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770
Bram Moolenaar4c683752020-04-05 21:38:23 +0200771 if (type1 == VAR_UNKNOWN)
772 type1 = VAR_ANY;
773 if (type2 == VAR_UNKNOWN)
774 type2 = VAR_ANY;
775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 if (type1 == type2)
777 {
778 switch (type1)
779 {
780 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
781 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
782 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
783 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
784 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
785 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
786 case VAR_LIST: isntype = ISN_COMPARELIST; break;
787 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
788 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 default: isntype = ISN_COMPAREANY; break;
790 }
791 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200792 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
794 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
795 isntype = ISN_COMPAREANY;
796
797 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
798 && (isntype == ISN_COMPAREBOOL
799 || isntype == ISN_COMPARESPECIAL
800 || isntype == ISN_COMPARENR
801 || isntype == ISN_COMPAREFLOAT))
802 {
803 semsg(_("E1037: Cannot use \"%s\" with %s"),
804 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200805 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 }
807 if (isntype == ISN_DROP
808 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
809 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
810 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
811 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
812 && exptype != EXPR_IS && exptype != EXPR_ISNOT
813 && (type1 == VAR_BLOB || type2 == VAR_BLOB
814 || type1 == VAR_LIST || type2 == VAR_LIST))))
815 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100816 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200818 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200820 return isntype;
821}
822
823/*
824 * Generate an ISN_COMPARE* instruction with a boolean result.
825 */
826 static int
827generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
828{
829 isntype_T isntype;
830 isn_T *isn;
831 garray_T *stack = &cctx->ctx_type_stack;
832 vartype_T type1;
833 vartype_T type2;
834
835 RETURN_OK_IF_SKIP(cctx);
836
837 // Get the known type of the two items on the stack. If they are matching
838 // use a type-specific instruction. Otherwise fall back to runtime type
839 // checking.
840 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
841 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
842 isntype = get_compare_isn(exptype, type1, type2);
843 if (isntype == ISN_DROP)
844 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845
846 if ((isn = generate_instr(cctx, isntype)) == NULL)
847 return FAIL;
848 isn->isn_arg.op.op_type = exptype;
849 isn->isn_arg.op.op_ic = ic;
850
851 // takes two arguments, puts one bool back
852 if (stack->ga_len >= 2)
853 {
854 --stack->ga_len;
855 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
856 }
857
858 return OK;
859}
860
861/*
862 * Generate an ISN_2BOOL instruction.
863 */
864 static int
865generate_2BOOL(cctx_T *cctx, int invert)
866{
867 isn_T *isn;
868 garray_T *stack = &cctx->ctx_type_stack;
869
Bram Moolenaar080457c2020-03-03 21:53:32 +0100870 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
872 return FAIL;
873 isn->isn_arg.number = invert;
874
875 // type becomes bool
876 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
877
878 return OK;
879}
880
881 static int
882generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
883{
884 isn_T *isn;
885 garray_T *stack = &cctx->ctx_type_stack;
886
Bram Moolenaar080457c2020-03-03 21:53:32 +0100887 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
889 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200890 // TODO: whole type, e.g. for a function also arg and return types
891 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 isn->isn_arg.type.ct_off = offset;
893
894 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200895 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896
897 return OK;
898}
899
900/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200901 * Check that
902 * - "actual" is "expected" type or
903 * - "actual" is a type that can be "expected" type: add a runtime check; or
904 * - return FAIL.
905 */
906 static int
907need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
908{
909 if (check_type(expected, actual, FALSE) == OK)
910 return OK;
911 if (actual->tt_type != VAR_ANY
912 && actual->tt_type != VAR_UNKNOWN
913 && !(actual->tt_type == VAR_FUNC
914 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
915 {
916 type_mismatch(expected, actual);
917 return FAIL;
918 }
919 generate_TYPECHECK(cctx, expected, offset);
920 return OK;
921}
922
923/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 * Generate an ISN_PUSHNR instruction.
925 */
926 static int
927generate_PUSHNR(cctx_T *cctx, varnumber_T number)
928{
929 isn_T *isn;
930
Bram Moolenaar080457c2020-03-03 21:53:32 +0100931 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
933 return FAIL;
934 isn->isn_arg.number = number;
935
936 return OK;
937}
938
939/*
940 * Generate an ISN_PUSHBOOL instruction.
941 */
942 static int
943generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
944{
945 isn_T *isn;
946
Bram Moolenaar080457c2020-03-03 21:53:32 +0100947 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
949 return FAIL;
950 isn->isn_arg.number = number;
951
952 return OK;
953}
954
955/*
956 * Generate an ISN_PUSHSPEC instruction.
957 */
958 static int
959generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
960{
961 isn_T *isn;
962
Bram Moolenaar080457c2020-03-03 21:53:32 +0100963 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
965 return FAIL;
966 isn->isn_arg.number = number;
967
968 return OK;
969}
970
971#ifdef FEAT_FLOAT
972/*
973 * Generate an ISN_PUSHF instruction.
974 */
975 static int
976generate_PUSHF(cctx_T *cctx, float_T fnumber)
977{
978 isn_T *isn;
979
Bram Moolenaar080457c2020-03-03 21:53:32 +0100980 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
982 return FAIL;
983 isn->isn_arg.fnumber = fnumber;
984
985 return OK;
986}
987#endif
988
989/*
990 * Generate an ISN_PUSHS instruction.
991 * Consumes "str".
992 */
993 static int
994generate_PUSHS(cctx_T *cctx, char_u *str)
995{
996 isn_T *isn;
997
Bram Moolenaar080457c2020-03-03 21:53:32 +0100998 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1000 return FAIL;
1001 isn->isn_arg.string = str;
1002
1003 return OK;
1004}
1005
1006/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001007 * Generate an ISN_PUSHCHANNEL instruction.
1008 * Consumes "channel".
1009 */
1010 static int
1011generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1012{
1013 isn_T *isn;
1014
Bram Moolenaar080457c2020-03-03 21:53:32 +01001015 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001016 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1017 return FAIL;
1018 isn->isn_arg.channel = channel;
1019
1020 return OK;
1021}
1022
1023/*
1024 * Generate an ISN_PUSHJOB instruction.
1025 * Consumes "job".
1026 */
1027 static int
1028generate_PUSHJOB(cctx_T *cctx, job_T *job)
1029{
1030 isn_T *isn;
1031
Bram Moolenaar080457c2020-03-03 21:53:32 +01001032 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001033 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001034 return FAIL;
1035 isn->isn_arg.job = job;
1036
1037 return OK;
1038}
1039
1040/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 * Generate an ISN_PUSHBLOB instruction.
1042 * Consumes "blob".
1043 */
1044 static int
1045generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1046{
1047 isn_T *isn;
1048
Bram Moolenaar080457c2020-03-03 21:53:32 +01001049 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1051 return FAIL;
1052 isn->isn_arg.blob = blob;
1053
1054 return OK;
1055}
1056
1057/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001058 * Generate an ISN_PUSHFUNC instruction with name "name".
1059 * Consumes "name".
1060 */
1061 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001062generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001063{
1064 isn_T *isn;
1065
Bram Moolenaar080457c2020-03-03 21:53:32 +01001066 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001067 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001068 return FAIL;
1069 isn->isn_arg.string = name;
1070
1071 return OK;
1072}
1073
1074/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001075 * Generate an ISN_GETITEM instruction with "index".
1076 */
1077 static int
1078generate_GETITEM(cctx_T *cctx, int index)
1079{
1080 isn_T *isn;
1081 garray_T *stack = &cctx->ctx_type_stack;
1082 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1083 type_T *item_type = &t_any;
1084
1085 RETURN_OK_IF_SKIP(cctx);
1086
1087 if (type->tt_type == VAR_LIST)
1088 item_type = type->tt_member;
1089 else if (type->tt_type != VAR_ANY)
1090 {
1091 emsg(_(e_listreq));
1092 return FAIL;
1093 }
1094 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1095 return FAIL;
1096 isn->isn_arg.number = index;
1097
1098 // add the item type to the type stack
1099 if (ga_grow(stack, 1) == FAIL)
1100 return FAIL;
1101 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1102 ++stack->ga_len;
1103 return OK;
1104}
1105
1106/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001107 * Generate an ISN_SLICE instruction with "count".
1108 */
1109 static int
1110generate_SLICE(cctx_T *cctx, int count)
1111{
1112 isn_T *isn;
1113
1114 RETURN_OK_IF_SKIP(cctx);
1115 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1116 return FAIL;
1117 isn->isn_arg.number = count;
1118 return OK;
1119}
1120
1121/*
1122 * Generate an ISN_CHECKLEN instruction with "min_len".
1123 */
1124 static int
1125generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1126{
1127 isn_T *isn;
1128
1129 RETURN_OK_IF_SKIP(cctx);
1130
1131 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1132 return FAIL;
1133 isn->isn_arg.checklen.cl_min_len = min_len;
1134 isn->isn_arg.checklen.cl_more_OK = more_OK;
1135
1136 return OK;
1137}
1138
1139/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 * Generate an ISN_STORE instruction.
1141 */
1142 static int
1143generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1144{
1145 isn_T *isn;
1146
Bram Moolenaar080457c2020-03-03 21:53:32 +01001147 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1149 return FAIL;
1150 if (name != NULL)
1151 isn->isn_arg.string = vim_strsave(name);
1152 else
1153 isn->isn_arg.number = idx;
1154
1155 return OK;
1156}
1157
1158/*
1159 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1160 */
1161 static int
1162generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1163{
1164 isn_T *isn;
1165
Bram Moolenaar080457c2020-03-03 21:53:32 +01001166 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1168 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001169 isn->isn_arg.storenr.stnr_idx = idx;
1170 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171
1172 return OK;
1173}
1174
1175/*
1176 * Generate an ISN_STOREOPT instruction
1177 */
1178 static int
1179generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1180{
1181 isn_T *isn;
1182
Bram Moolenaar080457c2020-03-03 21:53:32 +01001183 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1185 return FAIL;
1186 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1187 isn->isn_arg.storeopt.so_flags = opt_flags;
1188
1189 return OK;
1190}
1191
1192/*
1193 * Generate an ISN_LOAD or similar instruction.
1194 */
1195 static int
1196generate_LOAD(
1197 cctx_T *cctx,
1198 isntype_T isn_type,
1199 int idx,
1200 char_u *name,
1201 type_T *type)
1202{
1203 isn_T *isn;
1204
Bram Moolenaar080457c2020-03-03 21:53:32 +01001205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1207 return FAIL;
1208 if (name != NULL)
1209 isn->isn_arg.string = vim_strsave(name);
1210 else
1211 isn->isn_arg.number = idx;
1212
1213 return OK;
1214}
1215
1216/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001217 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001218 */
1219 static int
1220generate_LOADV(
1221 cctx_T *cctx,
1222 char_u *name,
1223 int error)
1224{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001225 int di_flags;
1226 int vidx = find_vim_var(name, &di_flags);
1227 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001228
Bram Moolenaar080457c2020-03-03 21:53:32 +01001229 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001230 if (vidx < 0)
1231 {
1232 if (error)
1233 semsg(_(e_var_notfound), name);
1234 return FAIL;
1235 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001236 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001237
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001238 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001239}
1240
1241/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001242 * Generate an ISN_UNLET instruction.
1243 */
1244 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001245generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001246{
1247 isn_T *isn;
1248
1249 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001250 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001251 return FAIL;
1252 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1253 isn->isn_arg.unlet.ul_forceit = forceit;
1254
1255 return OK;
1256}
1257
1258/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 * Generate an ISN_LOADS instruction.
1260 */
1261 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001262generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001264 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001266 int sid,
1267 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268{
1269 isn_T *isn;
1270
Bram Moolenaar080457c2020-03-03 21:53:32 +01001271 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001272 if (isn_type == ISN_LOADS)
1273 isn = generate_instr_type(cctx, isn_type, type);
1274 else
1275 isn = generate_instr_drop(cctx, isn_type, 1);
1276 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001278 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1279 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280
1281 return OK;
1282}
1283
1284/*
1285 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1286 */
1287 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001288generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289 cctx_T *cctx,
1290 isntype_T isn_type,
1291 int sid,
1292 int idx,
1293 type_T *type)
1294{
1295 isn_T *isn;
1296
Bram Moolenaar080457c2020-03-03 21:53:32 +01001297 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 if (isn_type == ISN_LOADSCRIPT)
1299 isn = generate_instr_type(cctx, isn_type, type);
1300 else
1301 isn = generate_instr_drop(cctx, isn_type, 1);
1302 if (isn == NULL)
1303 return FAIL;
1304 isn->isn_arg.script.script_sid = sid;
1305 isn->isn_arg.script.script_idx = idx;
1306 return OK;
1307}
1308
1309/*
1310 * Generate an ISN_NEWLIST instruction.
1311 */
1312 static int
1313generate_NEWLIST(cctx_T *cctx, int count)
1314{
1315 isn_T *isn;
1316 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 type_T *type;
1318 type_T *member;
1319
Bram Moolenaar080457c2020-03-03 21:53:32 +01001320 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1322 return FAIL;
1323 isn->isn_arg.number = count;
1324
1325 // drop the value types
1326 stack->ga_len -= count;
1327
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001328 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001329 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 if (count > 0)
1331 member = ((type_T **)stack->ga_data)[stack->ga_len];
1332 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001333 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001334 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335
1336 // add the list type to the type stack
1337 if (ga_grow(stack, 1) == FAIL)
1338 return FAIL;
1339 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1340 ++stack->ga_len;
1341
1342 return OK;
1343}
1344
1345/*
1346 * Generate an ISN_NEWDICT instruction.
1347 */
1348 static int
1349generate_NEWDICT(cctx_T *cctx, int count)
1350{
1351 isn_T *isn;
1352 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353 type_T *type;
1354 type_T *member;
1355
Bram Moolenaar080457c2020-03-03 21:53:32 +01001356 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1358 return FAIL;
1359 isn->isn_arg.number = count;
1360
1361 // drop the key and value types
1362 stack->ga_len -= 2 * count;
1363
Bram Moolenaar436472f2020-02-20 22:54:43 +01001364 // Use the first value type for the list member type. Use "void" for an
1365 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 if (count > 0)
1367 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1368 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001369 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001370 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371
1372 // add the dict type to the type stack
1373 if (ga_grow(stack, 1) == FAIL)
1374 return FAIL;
1375 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1376 ++stack->ga_len;
1377
1378 return OK;
1379}
1380
1381/*
1382 * Generate an ISN_FUNCREF instruction.
1383 */
1384 static int
1385generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1386{
1387 isn_T *isn;
1388 garray_T *stack = &cctx->ctx_type_stack;
1389
Bram Moolenaar080457c2020-03-03 21:53:32 +01001390 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1392 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001393 isn->isn_arg.funcref.fr_func = dfunc_idx;
1394 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395
1396 if (ga_grow(stack, 1) == FAIL)
1397 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001398 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 // TODO: argument and return types
1400 ++stack->ga_len;
1401
1402 return OK;
1403}
1404
1405/*
1406 * Generate an ISN_JUMP instruction.
1407 */
1408 static int
1409generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1410{
1411 isn_T *isn;
1412 garray_T *stack = &cctx->ctx_type_stack;
1413
Bram Moolenaar080457c2020-03-03 21:53:32 +01001414 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1416 return FAIL;
1417 isn->isn_arg.jump.jump_when = when;
1418 isn->isn_arg.jump.jump_where = where;
1419
1420 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1421 --stack->ga_len;
1422
1423 return OK;
1424}
1425
1426 static int
1427generate_FOR(cctx_T *cctx, int loop_idx)
1428{
1429 isn_T *isn;
1430 garray_T *stack = &cctx->ctx_type_stack;
1431
Bram Moolenaar080457c2020-03-03 21:53:32 +01001432 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1434 return FAIL;
1435 isn->isn_arg.forloop.for_idx = loop_idx;
1436
1437 if (ga_grow(stack, 1) == FAIL)
1438 return FAIL;
1439 // type doesn't matter, will be stored next
1440 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1441 ++stack->ga_len;
1442
1443 return OK;
1444}
1445
1446/*
1447 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001448 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449 * Return FAIL if the number of arguments is wrong.
1450 */
1451 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001452generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453{
1454 isn_T *isn;
1455 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001456 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001457 type_T *argtypes[MAX_FUNC_ARGS];
1458 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459
Bram Moolenaar080457c2020-03-03 21:53:32 +01001460 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001461 argoff = check_internal_func(func_idx, argcount);
1462 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 return FAIL;
1464
Bram Moolenaar389df252020-07-09 21:20:47 +02001465 if (method_call && argoff > 1)
1466 {
1467 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1468 return FAIL;
1469 isn->isn_arg.shuffle.shfl_item = argcount;
1470 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1471 }
1472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1474 return FAIL;
1475 isn->isn_arg.bfunc.cbf_idx = func_idx;
1476 isn->isn_arg.bfunc.cbf_argcount = argcount;
1477
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001478 for (i = 0; i < argcount; ++i)
1479 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 stack->ga_len -= argcount; // drop the arguments
1482 if (ga_grow(stack, 1) == FAIL)
1483 return FAIL;
1484 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001485 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 ++stack->ga_len; // add return value
1487
1488 return OK;
1489}
1490
1491/*
1492 * Generate an ISN_DCALL or ISN_UCALL instruction.
1493 * Return FAIL if the number of arguments is wrong.
1494 */
1495 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001496generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497{
1498 isn_T *isn;
1499 garray_T *stack = &cctx->ctx_type_stack;
1500 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001501 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502
Bram Moolenaar080457c2020-03-03 21:53:32 +01001503 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 if (argcount > regular_args && !has_varargs(ufunc))
1505 {
1506 semsg(_(e_toomanyarg), ufunc->uf_name);
1507 return FAIL;
1508 }
1509 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1510 {
1511 semsg(_(e_toofewarg), ufunc->uf_name);
1512 return FAIL;
1513 }
1514
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001515 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001516 {
1517 int i;
1518
1519 for (i = 0; i < argcount; ++i)
1520 {
1521 type_T *expected;
1522 type_T *actual;
1523
1524 if (i < regular_args)
1525 {
1526 if (ufunc->uf_arg_types == NULL)
1527 continue;
1528 expected = ufunc->uf_arg_types[i];
1529 }
1530 else
1531 expected = ufunc->uf_va_type->tt_member;
1532 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001533 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001534 {
1535 arg_type_mismatch(expected, actual, i + 1);
1536 return FAIL;
1537 }
1538 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001539 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001540 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001541 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001542 }
1543
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001545 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001546 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001548 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 {
1550 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1551 isn->isn_arg.dfunc.cdf_argcount = argcount;
1552 }
1553 else
1554 {
1555 // A user function may be deleted and redefined later, can't use the
1556 // ufunc pointer, need to look it up again at runtime.
1557 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1558 isn->isn_arg.ufunc.cuf_argcount = argcount;
1559 }
1560
1561 stack->ga_len -= argcount; // drop the arguments
1562 if (ga_grow(stack, 1) == FAIL)
1563 return FAIL;
1564 // add return value
1565 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1566 ++stack->ga_len;
1567
1568 return OK;
1569}
1570
1571/*
1572 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1573 */
1574 static int
1575generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1576{
1577 isn_T *isn;
1578 garray_T *stack = &cctx->ctx_type_stack;
1579
Bram Moolenaar080457c2020-03-03 21:53:32 +01001580 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1582 return FAIL;
1583 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1584 isn->isn_arg.ufunc.cuf_argcount = argcount;
1585
1586 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001587 if (ga_grow(stack, 1) == FAIL)
1588 return FAIL;
1589 // add return value
1590 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1591 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592
1593 return OK;
1594}
1595
1596/*
1597 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001598 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 */
1600 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001601generate_PCALL(
1602 cctx_T *cctx,
1603 int argcount,
1604 char_u *name,
1605 type_T *type,
1606 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607{
1608 isn_T *isn;
1609 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001610 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611
Bram Moolenaar080457c2020-03-03 21:53:32 +01001612 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001613
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001614 if (type->tt_type == VAR_ANY)
1615 ret_type = &t_any;
1616 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001617 {
1618 if (type->tt_argcount != -1)
1619 {
1620 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1621
1622 if (argcount < type->tt_min_argcount - varargs)
1623 {
1624 semsg(_(e_toofewarg), "[reference]");
1625 return FAIL;
1626 }
1627 if (!varargs && argcount > type->tt_argcount)
1628 {
1629 semsg(_(e_toomanyarg), "[reference]");
1630 return FAIL;
1631 }
1632 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001633 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001634 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001635 else
1636 {
1637 semsg(_("E1085: Not a callable type: %s"), name);
1638 return FAIL;
1639 }
1640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1642 return FAIL;
1643 isn->isn_arg.pfunc.cpf_top = at_top;
1644 isn->isn_arg.pfunc.cpf_argcount = argcount;
1645
1646 stack->ga_len -= argcount; // drop the arguments
1647
1648 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001649 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001651 // If partial is above the arguments it must be cleared and replaced with
1652 // the return value.
1653 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1654 return FAIL;
1655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 return OK;
1657}
1658
1659/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001660 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661 */
1662 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001663generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664{
1665 isn_T *isn;
1666 garray_T *stack = &cctx->ctx_type_stack;
1667 type_T *type;
1668
Bram Moolenaar080457c2020-03-03 21:53:32 +01001669 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001670 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001672 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001674 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001676 if (type->tt_type != VAR_DICT && type != &t_any)
1677 {
1678 emsg(_(e_dictreq));
1679 return FAIL;
1680 }
1681 // change dict type to dict member type
1682 if (type->tt_type == VAR_DICT)
1683 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684
1685 return OK;
1686}
1687
1688/*
1689 * Generate an ISN_ECHO instruction.
1690 */
1691 static int
1692generate_ECHO(cctx_T *cctx, int with_white, int count)
1693{
1694 isn_T *isn;
1695
Bram Moolenaar080457c2020-03-03 21:53:32 +01001696 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1698 return FAIL;
1699 isn->isn_arg.echo.echo_with_white = with_white;
1700 isn->isn_arg.echo.echo_count = count;
1701
1702 return OK;
1703}
1704
Bram Moolenaarad39c092020-02-26 18:23:43 +01001705/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001706 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001707 */
1708 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001709generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001710{
1711 isn_T *isn;
1712
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001713 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001714 return FAIL;
1715 isn->isn_arg.number = count;
1716
1717 return OK;
1718}
1719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 static int
1721generate_EXEC(cctx_T *cctx, char_u *line)
1722{
1723 isn_T *isn;
1724
Bram Moolenaar080457c2020-03-03 21:53:32 +01001725 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1727 return FAIL;
1728 isn->isn_arg.string = vim_strsave(line);
1729 return OK;
1730}
1731
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001732 static int
1733generate_EXECCONCAT(cctx_T *cctx, int count)
1734{
1735 isn_T *isn;
1736
1737 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1738 return FAIL;
1739 isn->isn_arg.number = count;
1740 return OK;
1741}
1742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743/*
1744 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001745 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001747 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1749{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 lvar_T *lvar;
1751
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001752 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001754 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001755 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 }
1757
1758 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001759 return NULL;
1760 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001762 // Every local variable uses the next entry on the stack. We could re-use
1763 // the last ones when leaving a scope, but then variables used in a closure
1764 // might get overwritten. To keep things simple do not re-use stack
1765 // entries. This is less efficient, but memory is cheap these days.
1766 lvar->lv_idx = cctx->ctx_locals_count++;
1767
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001768 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 lvar->lv_const = isConst;
1770 lvar->lv_type = type;
1771
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001772 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773}
1774
1775/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001776 * Remove local variables above "new_top".
1777 */
1778 static void
1779unwind_locals(cctx_T *cctx, int new_top)
1780{
1781 if (cctx->ctx_locals.ga_len > new_top)
1782 {
1783 int idx;
1784 lvar_T *lvar;
1785
1786 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1787 {
1788 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1789 vim_free(lvar->lv_name);
1790 }
1791 }
1792 cctx->ctx_locals.ga_len = new_top;
1793}
1794
1795/*
1796 * Free all local variables.
1797 */
1798 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001799free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001800{
1801 unwind_locals(cctx, 0);
1802 ga_clear(&cctx->ctx_locals);
1803}
1804
1805/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 * Skip over a type definition and return a pointer to just after it.
1807 */
1808 char_u *
1809skip_type(char_u *start)
1810{
1811 char_u *p = start;
1812
1813 while (ASCII_ISALNUM(*p) || *p == '_')
1814 ++p;
1815
1816 // Skip over "<type>"; this is permissive about white space.
1817 if (*skipwhite(p) == '<')
1818 {
1819 p = skipwhite(p);
1820 p = skip_type(skipwhite(p + 1));
1821 p = skipwhite(p);
1822 if (*p == '>')
1823 ++p;
1824 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001825 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1826 {
1827 // handle func(args): type
1828 ++p;
1829 while (*p != ')' && *p != NUL)
1830 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001831 char_u *sp = p;
1832
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001833 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001834 if (p == sp)
1835 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001836 if (*p == ',')
1837 p = skipwhite(p + 1);
1838 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001839 if (*p == ')')
1840 {
1841 if (p[1] == ':')
1842 p = skip_type(skipwhite(p + 2));
1843 else
1844 p = skipwhite(p + 1);
1845 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001846 }
1847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 return p;
1849}
1850
1851/*
1852 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001853 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 * Returns NULL in case of failure.
1855 */
1856 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001857parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858{
1859 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001860 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861
1862 if (**arg != '<')
1863 {
1864 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001865 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 else
1867 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001868 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 }
1870 *arg = skipwhite(*arg + 1);
1871
Bram Moolenaard77a8522020-04-03 21:59:57 +02001872 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873
1874 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001875 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 {
1877 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001878 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879 }
1880 ++*arg;
1881
1882 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001883 return get_list_type(member_type, type_gap);
1884 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885}
1886
1887/*
1888 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001889 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 */
1891 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001892parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893{
1894 char_u *p = *arg;
1895 size_t len;
1896
1897 // skip over the first word
1898 while (ASCII_ISALNUM(*p) || *p == '_')
1899 ++p;
1900 len = p - *arg;
1901
1902 switch (**arg)
1903 {
1904 case 'a':
1905 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1906 {
1907 *arg += len;
1908 return &t_any;
1909 }
1910 break;
1911 case 'b':
1912 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1913 {
1914 *arg += len;
1915 return &t_bool;
1916 }
1917 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1918 {
1919 *arg += len;
1920 return &t_blob;
1921 }
1922 break;
1923 case 'c':
1924 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1925 {
1926 *arg += len;
1927 return &t_channel;
1928 }
1929 break;
1930 case 'd':
1931 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1932 {
1933 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001934 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 }
1936 break;
1937 case 'f':
1938 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1939 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001940#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 *arg += len;
1942 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001943#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001944 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001945 return &t_any;
1946#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 }
1948 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1949 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001950 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001951 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001952 int argcount = -1;
1953 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001954 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001955 type_T *arg_type[MAX_FUNC_ARGS + 1];
1956
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001957 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001959 if (**arg == '(')
1960 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001961 // "func" may or may not return a value, "func()" does
1962 // not return a value.
1963 ret_type = &t_void;
1964
Bram Moolenaard77a8522020-04-03 21:59:57 +02001965 p = ++*arg;
1966 argcount = 0;
1967 while (*p != NUL && *p != ')')
1968 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001969 if (*p == '?')
1970 {
1971 if (first_optional == -1)
1972 first_optional = argcount;
1973 ++p;
1974 }
1975 else if (first_optional != -1)
1976 {
1977 emsg(_("E1007: mandatory argument after optional argument"));
1978 return &t_any;
1979 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001980 else if (STRNCMP(p, "...", 3) == 0)
1981 {
1982 flags |= TTFLAG_VARARGS;
1983 p += 3;
1984 }
1985
1986 arg_type[argcount++] = parse_type(&p, type_gap);
1987
1988 // Nothing comes after "...{type}".
1989 if (flags & TTFLAG_VARARGS)
1990 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001991
Bram Moolenaard77a8522020-04-03 21:59:57 +02001992 if (*p != ',' && *skipwhite(p) == ',')
1993 {
1994 semsg(_(e_no_white_before), ",");
1995 return &t_any;
1996 }
1997 if (*p == ',')
1998 {
1999 ++p;
2000 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002001 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002002 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002003 return &t_any;
2004 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002005 }
2006 p = skipwhite(p);
2007 if (argcount == MAX_FUNC_ARGS)
2008 {
2009 emsg(_("E740: Too many argument types"));
2010 return &t_any;
2011 }
2012 }
2013
2014 p = skipwhite(p);
2015 if (*p != ')')
2016 {
2017 emsg(_(e_missing_close));
2018 return &t_any;
2019 }
2020 *arg = p + 1;
2021 }
2022 if (**arg == ':')
2023 {
2024 // parse return type
2025 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002026 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002027 semsg(_(e_white_after), ":");
2028 *arg = skipwhite(*arg);
2029 ret_type = parse_type(arg, type_gap);
2030 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002031 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002032 type = get_func_type(ret_type, argcount, type_gap);
2033 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002034 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002035 type = alloc_func_type(ret_type, argcount, type_gap);
2036 type->tt_flags = flags;
2037 if (argcount > 0)
2038 {
2039 type->tt_argcount = argcount;
2040 type->tt_min_argcount = first_optional == -1
2041 ? argcount : first_optional;
2042 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002043 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002044 return &t_any;
2045 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002046 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002047 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002048 }
2049 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 }
2051 break;
2052 case 'j':
2053 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2054 {
2055 *arg += len;
2056 return &t_job;
2057 }
2058 break;
2059 case 'l':
2060 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2061 {
2062 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002063 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 }
2065 break;
2066 case 'n':
2067 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2068 {
2069 *arg += len;
2070 return &t_number;
2071 }
2072 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 case 's':
2074 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2075 {
2076 *arg += len;
2077 return &t_string;
2078 }
2079 break;
2080 case 'v':
2081 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2082 {
2083 *arg += len;
2084 return &t_void;
2085 }
2086 break;
2087 }
2088
2089 semsg(_("E1010: Type not recognized: %s"), *arg);
2090 return &t_any;
2091}
2092
2093/*
2094 * Check if "type1" and "type2" are exactly the same.
2095 */
2096 static int
2097equal_type(type_T *type1, type_T *type2)
2098{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002099 int i;
2100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 if (type1->tt_type != type2->tt_type)
2102 return FALSE;
2103 switch (type1->tt_type)
2104 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002106 case VAR_ANY:
2107 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 case VAR_SPECIAL:
2109 case VAR_BOOL:
2110 case VAR_NUMBER:
2111 case VAR_FLOAT:
2112 case VAR_STRING:
2113 case VAR_BLOB:
2114 case VAR_JOB:
2115 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002116 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117 case VAR_LIST:
2118 case VAR_DICT:
2119 return equal_type(type1->tt_member, type2->tt_member);
2120 case VAR_FUNC:
2121 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002122 if (!equal_type(type1->tt_member, type2->tt_member)
2123 || type1->tt_argcount != type2->tt_argcount)
2124 return FALSE;
2125 if (type1->tt_argcount < 0
2126 || type1->tt_args == NULL || type2->tt_args == NULL)
2127 return TRUE;
2128 for (i = 0; i < type1->tt_argcount; ++i)
2129 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2130 return FALSE;
2131 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 }
2133 return TRUE;
2134}
2135
2136/*
2137 * Find the common type of "type1" and "type2" and put it in "dest".
2138 * "type2" and "dest" may be the same.
2139 */
2140 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002141common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142{
2143 if (equal_type(type1, type2))
2144 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002145 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 return;
2147 }
2148
2149 if (type1->tt_type == type2->tt_type)
2150 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2152 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002153 type_T *common;
2154
Bram Moolenaard77a8522020-04-03 21:59:57 +02002155 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002156 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002157 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002158 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002159 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 return;
2161 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002162 if (type1->tt_type == VAR_FUNC)
2163 {
2164 type_T *common;
2165
2166 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2167 if (type1->tt_argcount == type2->tt_argcount
2168 && type1->tt_argcount >= 0)
2169 {
2170 int argcount = type1->tt_argcount;
2171 int i;
2172
2173 *dest = alloc_func_type(common, argcount, type_gap);
2174 if (type1->tt_args != NULL && type2->tt_args != NULL)
2175 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002176 if (func_type_add_arg_types(*dest, argcount,
2177 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002178 for (i = 0; i < argcount; ++i)
2179 common_type(type1->tt_args[i], type2->tt_args[i],
2180 &(*dest)->tt_args[i], type_gap);
2181 }
2182 }
2183 else
2184 *dest = alloc_func_type(common, -1, type_gap);
2185 return;
2186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 }
2188
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002189 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190}
2191
2192 char *
2193vartype_name(vartype_T type)
2194{
2195 switch (type)
2196 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002197 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002198 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 case VAR_SPECIAL: return "special";
2201 case VAR_BOOL: return "bool";
2202 case VAR_NUMBER: return "number";
2203 case VAR_FLOAT: return "float";
2204 case VAR_STRING: return "string";
2205 case VAR_BLOB: return "blob";
2206 case VAR_JOB: return "job";
2207 case VAR_CHANNEL: return "channel";
2208 case VAR_LIST: return "list";
2209 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002210
2211 case VAR_FUNC:
2212 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002214 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215}
2216
2217/*
2218 * Return the name of a type.
2219 * The result may be in allocated memory, in which case "tofree" is set.
2220 */
2221 char *
2222type_name(type_T *type, char **tofree)
2223{
2224 char *name = vartype_name(type->tt_type);
2225
2226 *tofree = NULL;
2227 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2228 {
2229 char *member_free;
2230 char *member_name = type_name(type->tt_member, &member_free);
2231 size_t len;
2232
2233 len = STRLEN(name) + STRLEN(member_name) + 3;
2234 *tofree = alloc(len);
2235 if (*tofree != NULL)
2236 {
2237 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2238 vim_free(member_free);
2239 return *tofree;
2240 }
2241 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002242 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002243 {
2244 garray_T ga;
2245 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002246 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002247
2248 ga_init2(&ga, 1, 100);
2249 if (ga_grow(&ga, 20) == FAIL)
2250 return "[unknown]";
2251 *tofree = ga.ga_data;
2252 STRCPY(ga.ga_data, "func(");
2253 ga.ga_len += 5;
2254
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002255 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002256 {
2257 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002258 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002259 int len;
2260
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002261 if (type->tt_args == NULL)
2262 arg_type = "[unknown]";
2263 else
2264 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002265 if (i > 0)
2266 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002267 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002268 ga.ga_len += 2;
2269 }
2270 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002271 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002272 {
2273 vim_free(arg_free);
2274 return "[unknown]";
2275 }
2276 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002277 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002278 {
2279 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2280 ga.ga_len += 3;
2281 }
2282 else if (i >= type->tt_min_argcount)
2283 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002284 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002285 ga.ga_len += len;
2286 vim_free(arg_free);
2287 }
2288
2289 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002290 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002291 else
2292 {
2293 char *ret_free;
2294 char *ret_name = type_name(type->tt_member, &ret_free);
2295 int len;
2296
2297 len = (int)STRLEN(ret_name) + 4;
2298 if (ga_grow(&ga, len) == FAIL)
2299 {
2300 vim_free(ret_free);
2301 return "[unknown]";
2302 }
2303 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002304 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2305 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002306 vim_free(ret_free);
2307 }
2308 return ga.ga_data;
2309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310
2311 return name;
2312}
2313
2314/*
2315 * Find "name" in script-local items of script "sid".
2316 * Returns the index in "sn_var_vals" if found.
2317 * If found but not in "sn_var_vals" returns -1.
2318 * If not found returns -2.
2319 */
2320 int
2321get_script_item_idx(int sid, char_u *name, int check_writable)
2322{
2323 hashtab_T *ht;
2324 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002325 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 int idx;
2327
2328 // First look the name up in the hashtable.
2329 if (sid <= 0 || sid > script_items.ga_len)
2330 return -1;
2331 ht = &SCRIPT_VARS(sid);
2332 di = find_var_in_ht(ht, 0, name, TRUE);
2333 if (di == NULL)
2334 return -2;
2335
2336 // Now find the svar_T index in sn_var_vals.
2337 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2338 {
2339 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2340
2341 if (sv->sv_tv == &di->di_tv)
2342 {
2343 if (check_writable && sv->sv_const)
2344 semsg(_(e_readonlyvar), name);
2345 return idx;
2346 }
2347 }
2348 return -1;
2349}
2350
2351/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002352 * Find "name" in imported items of the current script or in "cctx" if not
2353 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 */
2355 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002356find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002358 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 int idx;
2360
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002361 if (current_sctx.sc_sid <= 0)
2362 return NULL;
2363 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 if (cctx != NULL)
2365 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2366 {
2367 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2368 + idx;
2369
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002370 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2371 : STRLEN(import->imp_name) == len
2372 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 return import;
2374 }
2375
2376 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2377 {
2378 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2379
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002380 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2381 : STRLEN(import->imp_name) == len
2382 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 return import;
2384 }
2385 return NULL;
2386}
2387
2388/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002389 * Free all imported variables.
2390 */
2391 static void
2392free_imported(cctx_T *cctx)
2393{
2394 int idx;
2395
2396 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2397 {
2398 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2399
2400 vim_free(import->imp_name);
2401 }
2402 ga_clear(&cctx->ctx_imports);
2403}
2404
2405/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002406 * Return TRUE if "p" points at a "#" but not at "#{".
2407 */
2408 static int
2409comment_start(char_u *p)
2410{
2411 return p[0] == '#' && p[1] != '{';
2412}
2413
2414/*
2415 * Return a pointer to the next line that isn't empty or only contains a
2416 * comment. Skips over white space.
2417 * Returns NULL if there is none.
2418 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002419 char_u *
2420peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002421{
2422 int lnum = cctx->ctx_lnum;
2423
2424 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2425 {
2426 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002427 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002428
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002429 if (line == NULL)
2430 break;
2431 p = skipwhite(line);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002432 if (*p != NUL && !comment_start(p))
2433 return p;
2434 }
2435 return NULL;
2436}
2437
2438/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002439 * Called when checking for a following operator at "arg". When the rest of
2440 * the line is empty or only a comment, peek the next line. If there is a next
2441 * line return a pointer to it and set "nextp".
2442 * Otherwise skip over white space.
2443 */
2444 static char_u *
2445may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2446{
2447 char_u *p = skipwhite(arg);
2448
2449 *nextp = NULL;
2450 if (*p == NUL || (VIM_ISWHITE(*arg) && comment_start(p)))
2451 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002452 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002453 if (*nextp != NULL)
2454 return *nextp;
2455 }
2456 return p;
2457}
2458
2459/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002460 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002461 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002462 * Returns NULL when at the end.
2463 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002464 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002465next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002466{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002467 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002468
2469 do
2470 {
2471 ++cctx->ctx_lnum;
2472 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002473 {
2474 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002475 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002476 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002477 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002478 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002479 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002480 } while (line == NULL || *skipwhite(line) == NUL
2481 || (skip_comment && comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002482 return line;
2483}
2484
2485/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002486 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002487 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002488 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2489 */
2490 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002491may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002492{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002493 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002494 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002495 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002496
2497 if (next == NULL)
2498 return FAIL;
2499 *arg = skipwhite(next);
2500 }
2501 return OK;
2502}
2503
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002504/*
2505 * Idem, and give an error when failed.
2506 */
2507 static int
2508may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2509{
2510 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2511 {
2512 emsg(_("E1097: line incomplete"));
2513 return FAIL;
2514 }
2515 return OK;
2516}
2517
2518
Bram Moolenaara5565e42020-05-09 15:44:01 +02002519// Structure passed between the compile_expr* functions to keep track of
2520// constants that have been parsed but for which no code was produced yet. If
2521// possible expressions on these constants are applied at compile time. If
2522// that is not possible, the code to push the constants needs to be generated
2523// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002524// Using 50 should be more than enough of 5 levels of ().
2525#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002526typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002527 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002528 int pp_used; // active entries in pp_tv[]
2529} ppconst_T;
2530
Bram Moolenaar1c747212020-05-09 18:28:34 +02002531static int compile_expr0(char_u **arg, cctx_T *cctx);
2532static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2533
Bram Moolenaara5565e42020-05-09 15:44:01 +02002534/*
2535 * Generate a PUSH instruction for "tv".
2536 * "tv" will be consumed or cleared.
2537 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2538 */
2539 static int
2540generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2541{
2542 if (tv != NULL)
2543 {
2544 switch (tv->v_type)
2545 {
2546 case VAR_UNKNOWN:
2547 break;
2548 case VAR_BOOL:
2549 generate_PUSHBOOL(cctx, tv->vval.v_number);
2550 break;
2551 case VAR_SPECIAL:
2552 generate_PUSHSPEC(cctx, tv->vval.v_number);
2553 break;
2554 case VAR_NUMBER:
2555 generate_PUSHNR(cctx, tv->vval.v_number);
2556 break;
2557#ifdef FEAT_FLOAT
2558 case VAR_FLOAT:
2559 generate_PUSHF(cctx, tv->vval.v_float);
2560 break;
2561#endif
2562 case VAR_BLOB:
2563 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2564 tv->vval.v_blob = NULL;
2565 break;
2566 case VAR_STRING:
2567 generate_PUSHS(cctx, tv->vval.v_string);
2568 tv->vval.v_string = NULL;
2569 break;
2570 default:
2571 iemsg("constant type not supported");
2572 clear_tv(tv);
2573 return FAIL;
2574 }
2575 tv->v_type = VAR_UNKNOWN;
2576 }
2577 return OK;
2578}
2579
2580/*
2581 * Generate code for any ppconst entries.
2582 */
2583 static int
2584generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2585{
2586 int i;
2587 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002588 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002589
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002590 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002591 for (i = 0; i < ppconst->pp_used; ++i)
2592 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2593 ret = FAIL;
2594 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002595 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002596 return ret;
2597}
2598
2599/*
2600 * Clear ppconst constants. Used when failing.
2601 */
2602 static void
2603clear_ppconst(ppconst_T *ppconst)
2604{
2605 int i;
2606
2607 for (i = 0; i < ppconst->pp_used; ++i)
2608 clear_tv(&ppconst->pp_tv[i]);
2609 ppconst->pp_used = 0;
2610}
2611
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002612/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002613 * Generate an instruction to load script-local variable "name", without the
2614 * leading "s:".
2615 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 */
2617 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002618compile_load_scriptvar(
2619 cctx_T *cctx,
2620 char_u *name, // variable NUL terminated
2621 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002622 char_u **end, // end of variable
2623 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002625 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2627 imported_T *import;
2628
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002629 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002631 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002632 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2633 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 }
2635 if (idx >= 0)
2636 {
2637 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2638
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002639 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 current_sctx.sc_sid, idx, sv->sv_type);
2641 return OK;
2642 }
2643
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002644 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 if (import != NULL)
2646 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002647 if (import->imp_all)
2648 {
2649 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002650 char_u *exp_name;
2651 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002652 ufunc_T *ufunc;
2653 type_T *type;
2654
2655 // Used "import * as Name", need to lookup the member.
2656 if (*p != '.')
2657 {
2658 semsg(_("E1060: expected dot after name: %s"), start);
2659 return FAIL;
2660 }
2661 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002662 if (VIM_ISWHITE(*p))
2663 {
2664 emsg(_("E1074: no white space allowed after dot"));
2665 return FAIL;
2666 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002667
Bram Moolenaar1c991142020-07-04 13:15:31 +02002668 // isolate one name
2669 exp_name = p;
2670 while (eval_isnamec(*p))
2671 ++p;
2672 cc = *p;
2673 *p = NUL;
2674
2675 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2676 *p = cc;
2677 p = skipwhite(p);
2678
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002679 // TODO: what if it is a function?
2680 if (idx < 0)
2681 return FAIL;
2682 *end = p;
2683
2684 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2685 import->imp_sid,
2686 idx,
2687 type);
2688 }
2689 else
2690 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002691 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002692 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2693 import->imp_sid,
2694 import->imp_var_vals_idx,
2695 import->imp_type);
2696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 return OK;
2698 }
2699
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002700 if (error)
2701 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 return FAIL;
2703}
2704
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002705 static int
2706generate_funcref(cctx_T *cctx, char_u *name)
2707{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002708 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002709
2710 if (ufunc == NULL)
2711 return FAIL;
2712
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002713 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2714 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002715}
2716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717/*
2718 * Compile a variable name into a load instruction.
2719 * "end" points to just after the name.
2720 * When "error" is FALSE do not give an error when not found.
2721 */
2722 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002723compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724{
2725 type_T *type;
2726 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002727 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002729 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730
2731 if (*(*arg + 1) == ':')
2732 {
2733 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002734 if (end <= *arg + 2)
2735 name = vim_strsave((char_u *)"[empty]");
2736 else
2737 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 if (name == NULL)
2739 return FAIL;
2740
2741 if (**arg == 'v')
2742 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002743 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744 }
2745 else if (**arg == 'g')
2746 {
2747 // Global variables can be defined later, thus we don't check if it
2748 // exists, give error at runtime.
2749 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2750 }
2751 else if (**arg == 's')
2752 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002753 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002755 else if (**arg == 'b')
2756 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002757 // Buffer-local variables can be defined later, thus we don't check
2758 // if it exists, give error at runtime.
2759 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002760 }
2761 else if (**arg == 'w')
2762 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002763 // Window-local variables can be defined later, thus we don't check
2764 // if it exists, give error at runtime.
2765 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002766 }
2767 else if (**arg == 't')
2768 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002769 // Tabpage-local variables can be defined later, thus we don't
2770 // check if it exists, give error at runtime.
2771 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002772 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 else
2774 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002775 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 goto theend;
2777 }
2778 }
2779 else
2780 {
2781 size_t len = end - *arg;
2782 int idx;
2783 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002784 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785
2786 name = vim_strnsave(*arg, end - *arg);
2787 if (name == NULL)
2788 return FAIL;
2789
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002790 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002792 if (!gen_load_outer)
2793 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 }
2795 else
2796 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002797 lvar_T *lvar = lookup_local(*arg, len, cctx);
2798
2799 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002801 type = lvar->lv_type;
2802 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002803 if (lvar->lv_from_outer)
2804 gen_load_outer = TRUE;
2805 else
2806 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 }
2808 else
2809 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002810 // "var" can be script-local even without using "s:" if it
2811 // already exists.
2812 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2813 == SCRIPT_VERSION_VIM9
2814 || lookup_script(*arg, len) == OK)
2815 res = compile_load_scriptvar(cctx, name, *arg, &end,
2816 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002817
Bram Moolenaara5565e42020-05-09 15:44:01 +02002818 // When the name starts with an uppercase letter or "x:" it
2819 // can be a user defined function.
2820 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2821 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 }
2823 }
2824 if (gen_load)
2825 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002826 if (gen_load_outer)
2827 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 }
2829
2830 *arg = end;
2831
2832theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002833 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 semsg(_(e_var_notfound), name);
2835 vim_free(name);
2836 return res;
2837}
2838
2839/*
2840 * Compile the argument expressions.
2841 * "arg" points to just after the "(" and is advanced to after the ")"
2842 */
2843 static int
2844compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2845{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002846 char_u *p = *arg;
2847 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848
Bram Moolenaare6085c52020-04-12 20:19:16 +02002849 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002851 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2852 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002853 if (*p == ')')
2854 {
2855 *arg = p + 1;
2856 return OK;
2857 }
2858
Bram Moolenaara5565e42020-05-09 15:44:01 +02002859 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 return FAIL;
2861 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002862
2863 if (*p != ',' && *skipwhite(p) == ',')
2864 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002865 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002866 p = skipwhite(p);
2867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002869 {
2870 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002871 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002872 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002873 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002874 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002875 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002877failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002878 emsg(_(e_missing_close));
2879 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880}
2881
2882/*
2883 * Compile a function call: name(arg1, arg2)
2884 * "arg" points to "name", "arg + varlen" to the "(".
2885 * "argcount_init" is 1 for "value->method()"
2886 * Instructions:
2887 * EVAL arg1
2888 * EVAL arg2
2889 * BCALL / DCALL / UCALL
2890 */
2891 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002892compile_call(
2893 char_u **arg,
2894 size_t varlen,
2895 cctx_T *cctx,
2896 ppconst_T *ppconst,
2897 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898{
2899 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002900 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 int argcount = argcount_init;
2902 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002903 char_u fname_buf[FLEN_FIXED + 1];
2904 char_u *tofree = NULL;
2905 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002907 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908
Bram Moolenaara5565e42020-05-09 15:44:01 +02002909 // we can evaluate "has('name')" at compile time
2910 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2911 {
2912 char_u *s = skipwhite(*arg + varlen + 1);
2913 typval_T argvars[2];
2914
2915 argvars[0].v_type = VAR_UNKNOWN;
2916 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002917 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002918 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002919 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002920 s = skipwhite(s);
2921 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2922 {
2923 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2924
2925 *arg = s + 1;
2926 argvars[1].v_type = VAR_UNKNOWN;
2927 tv->v_type = VAR_NUMBER;
2928 tv->vval.v_number = 0;
2929 f_has(argvars, tv);
2930 clear_tv(&argvars[0]);
2931 ++ppconst->pp_used;
2932 return OK;
2933 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002934 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002935 }
2936
2937 if (generate_ppconst(cctx, ppconst) == FAIL)
2938 return FAIL;
2939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 if (varlen >= sizeof(namebuf))
2941 {
2942 semsg(_("E1011: name too long: %s"), name);
2943 return FAIL;
2944 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002945 vim_strncpy(namebuf, *arg, varlen);
2946 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947
2948 *arg = skipwhite(*arg + varlen + 1);
2949 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002950 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002952 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 {
2954 int idx;
2955
2956 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002957 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002959 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002960 else
2961 semsg(_(e_unknownfunc), namebuf);
2962 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 }
2964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002966 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002968 {
2969 res = generate_CALL(cctx, ufunc, argcount);
2970 goto theend;
2971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972
2973 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002974 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002976 if (STRNCMP(namebuf, "g:", 2) != 0
2977 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002978 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002979 garray_T *stack = &cctx->ctx_type_stack;
2980 type_T *type;
2981
2982 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2983 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002984 goto theend;
2985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002987 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002988 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002989 if (STRNCMP(namebuf, "g:", 2) == 0)
2990 res = generate_UCALL(cctx, name, argcount);
2991 else
2992 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002993
2994theend:
2995 vim_free(tofree);
2996 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997}
2998
2999// like NAMESPACE_CHAR but with 'a' and 'l'.
3000#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3001
3002/*
3003 * Find the end of a variable or function name. Unlike find_name_end() this
3004 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003005 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 * Return a pointer to just after the name. Equal to "arg" if there is no
3007 * valid name.
3008 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003009 static char_u *
3010to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011{
3012 char_u *p;
3013
3014 // Quick check for valid starting character.
3015 if (!eval_isnamec1(*arg))
3016 return arg;
3017
3018 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3019 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3020 // and can be used in slice "[n:]".
3021 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003022 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3024 break;
3025 return p;
3026}
3027
3028/*
3029 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003030 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 */
3032 char_u *
3033to_name_const_end(char_u *arg)
3034{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003035 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 typval_T rettv;
3037
3038 if (p == arg && *arg == '[')
3039 {
3040
3041 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003042 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 p = arg;
3044 }
3045 else if (p == arg && *arg == '#' && arg[1] == '{')
3046 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003047 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003049 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 p = arg;
3051 }
3052 else if (p == arg && *arg == '{')
3053 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003054 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003056 // Can be "{x -> ret}()".
3057 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003059 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 if (ret != OK)
3061 p = arg;
3062 }
3063
3064 return p;
3065}
3066
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067/*
3068 * parse a list: [expr, expr]
3069 * "*arg" points to the '['.
3070 */
3071 static int
3072compile_list(char_u **arg, cctx_T *cctx)
3073{
3074 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003075 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 int count = 0;
3077
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003078 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003080 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003081 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003082 semsg(_(e_list_end), *arg);
3083 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003084 }
3085 if (*p == ']')
3086 {
3087 ++p;
3088 // Allow for following comment, after at least one space.
3089 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
3090 p += STRLEN(p);
3091 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003092 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003093 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 break;
3095 ++count;
3096 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003097 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003099 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3100 {
3101 semsg(_(e_white_after), ",");
3102 return FAIL;
3103 }
3104 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003105 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 p = skipwhite(p);
3107 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003108 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109
3110 generate_NEWLIST(cctx, count);
3111 return OK;
3112}
3113
3114/*
3115 * parse a lambda: {arg, arg -> expr}
3116 * "*arg" points to the '{'.
3117 */
3118 static int
3119compile_lambda(char_u **arg, cctx_T *cctx)
3120{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 typval_T rettv;
3122 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003123 evalarg_T evalarg;
3124
3125 CLEAR_FIELD(evalarg);
3126 evalarg.eval_flags = EVAL_EVALUATE;
3127 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128
3129 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003130 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003134 ++ufunc->uf_refcount;
3135 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003136 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137
3138 // The function will have one line: "return {expr}".
3139 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003140 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003142 clear_evalarg(&evalarg, NULL);
3143
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003144 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003145 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 return FAIL;
3147}
3148
3149/*
3150 * Compile a lamda call: expr->{lambda}(args)
3151 * "arg" points to the "{".
3152 */
3153 static int
3154compile_lambda_call(char_u **arg, cctx_T *cctx)
3155{
3156 ufunc_T *ufunc;
3157 typval_T rettv;
3158 int argcount = 1;
3159 int ret = FAIL;
3160
3161 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003162 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 return FAIL;
3164
3165 if (**arg != '(')
3166 {
3167 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003168 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 else
3170 semsg(_(e_missing_paren), "lambda");
3171 clear_tv(&rettv);
3172 return FAIL;
3173 }
3174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 ufunc = rettv.vval.v_partial->pt_func;
3176 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003177 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003178 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003179
3180 // The function will have one line: "return {expr}".
3181 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003182 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183
3184 // compile the arguments
3185 *arg = skipwhite(*arg + 1);
3186 if (compile_arguments(arg, cctx, &argcount) == OK)
3187 // call the compiled function
3188 ret = generate_CALL(cctx, ufunc, argcount);
3189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 return ret;
3191}
3192
3193/*
3194 * parse a dict: {'key': val} or #{key: val}
3195 * "*arg" points to the '{'.
3196 */
3197 static int
3198compile_dict(char_u **arg, cctx_T *cctx, int literal)
3199{
3200 garray_T *instr = &cctx->ctx_instr;
3201 int count = 0;
3202 dict_T *d = dict_alloc();
3203 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003204 char_u *whitep = *arg;
3205 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206
3207 if (d == NULL)
3208 return FAIL;
3209 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003210 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 {
3212 char_u *key = NULL;
3213
Bram Moolenaar23c55272020-06-21 16:58:13 +02003214 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003215 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003216 *arg = NULL;
3217 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003218 }
3219
3220 if (**arg == '}')
3221 break;
3222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 if (literal)
3224 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003225 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226
Bram Moolenaar2c330432020-04-13 14:41:35 +02003227 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 {
3229 semsg(_("E1014: Invalid key: %s"), *arg);
3230 return FAIL;
3231 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003232 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 if (generate_PUSHS(cctx, key) == FAIL)
3234 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003235 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 }
3237 else
3238 {
3239 isn_T *isn;
3240
Bram Moolenaara5565e42020-05-09 15:44:01 +02003241 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 return FAIL;
3243 // TODO: check type is string
3244 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3245 if (isn->isn_type == ISN_PUSHS)
3246 key = isn->isn_arg.string;
3247 }
3248
3249 // Check for duplicate keys, if using string keys.
3250 if (key != NULL)
3251 {
3252 item = dict_find(d, key, -1);
3253 if (item != NULL)
3254 {
3255 semsg(_(e_duplicate_key), key);
3256 goto failret;
3257 }
3258 item = dictitem_alloc(key);
3259 if (item != NULL)
3260 {
3261 item->di_tv.v_type = VAR_UNKNOWN;
3262 item->di_tv.v_lock = 0;
3263 if (dict_add(d, item) == FAIL)
3264 dictitem_free(item);
3265 }
3266 }
3267
3268 *arg = skipwhite(*arg);
3269 if (**arg != ':')
3270 {
3271 semsg(_(e_missing_dict_colon), *arg);
3272 return FAIL;
3273 }
3274
Bram Moolenaar2c330432020-04-13 14:41:35 +02003275 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003277 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003278 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003279 *arg = NULL;
3280 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003281 }
3282
Bram Moolenaara5565e42020-05-09 15:44:01 +02003283 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 return FAIL;
3285 ++count;
3286
Bram Moolenaar2c330432020-04-13 14:41:35 +02003287 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003288 *arg = skipwhite(*arg);
3289 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003290 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003291 *arg = NULL;
3292 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 if (**arg == '}')
3295 break;
3296 if (**arg != ',')
3297 {
3298 semsg(_(e_missing_dict_comma), *arg);
3299 goto failret;
3300 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003301 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 *arg = skipwhite(*arg + 1);
3303 }
3304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 *arg = *arg + 1;
3306
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003307 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003308 p = skipwhite(*arg);
3309 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003310 *arg += STRLEN(*arg);
3311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 dict_unref(d);
3313 return generate_NEWDICT(cctx, count);
3314
3315failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003316 if (*arg == NULL)
3317 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 dict_unref(d);
3319 return FAIL;
3320}
3321
3322/*
3323 * Compile "&option".
3324 */
3325 static int
3326compile_get_option(char_u **arg, cctx_T *cctx)
3327{
3328 typval_T rettv;
3329 char_u *start = *arg;
3330 int ret;
3331
3332 // parse the option and get the current value to get the type.
3333 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003334 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 if (ret == OK)
3336 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003337 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 char_u *name = vim_strnsave(start, *arg - start);
3339 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3340
3341 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3342 vim_free(name);
3343 }
3344 clear_tv(&rettv);
3345
3346 return ret;
3347}
3348
3349/*
3350 * Compile "$VAR".
3351 */
3352 static int
3353compile_get_env(char_u **arg, cctx_T *cctx)
3354{
3355 char_u *start = *arg;
3356 int len;
3357 int ret;
3358 char_u *name;
3359
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 ++*arg;
3361 len = get_env_len(arg);
3362 if (len == 0)
3363 {
3364 semsg(_(e_syntax_at), start - 1);
3365 return FAIL;
3366 }
3367
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003368 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 name = vim_strnsave(start, len + 1);
3370 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3371 vim_free(name);
3372 return ret;
3373}
3374
3375/*
3376 * Compile "@r".
3377 */
3378 static int
3379compile_get_register(char_u **arg, cctx_T *cctx)
3380{
3381 int ret;
3382
3383 ++*arg;
3384 if (**arg == NUL)
3385 {
3386 semsg(_(e_syntax_at), *arg - 1);
3387 return FAIL;
3388 }
3389 if (!valid_yank_reg(**arg, TRUE))
3390 {
3391 emsg_invreg(**arg);
3392 return FAIL;
3393 }
3394 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3395 ++*arg;
3396 return ret;
3397}
3398
3399/*
3400 * Apply leading '!', '-' and '+' to constant "rettv".
3401 */
3402 static int
3403apply_leader(typval_T *rettv, char_u *start, char_u *end)
3404{
3405 char_u *p = end;
3406
3407 // this works from end to start
3408 while (p > start)
3409 {
3410 --p;
3411 if (*p == '-' || *p == '+')
3412 {
3413 // only '-' has an effect, for '+' we only check the type
3414#ifdef FEAT_FLOAT
3415 if (rettv->v_type == VAR_FLOAT)
3416 {
3417 if (*p == '-')
3418 rettv->vval.v_float = -rettv->vval.v_float;
3419 }
3420 else
3421#endif
3422 {
3423 varnumber_T val;
3424 int error = FALSE;
3425
3426 // tv_get_number_chk() accepts a string, but we don't want that
3427 // here
3428 if (check_not_string(rettv) == FAIL)
3429 return FAIL;
3430 val = tv_get_number_chk(rettv, &error);
3431 clear_tv(rettv);
3432 if (error)
3433 return FAIL;
3434 if (*p == '-')
3435 val = -val;
3436 rettv->v_type = VAR_NUMBER;
3437 rettv->vval.v_number = val;
3438 }
3439 }
3440 else
3441 {
3442 int v = tv2bool(rettv);
3443
3444 // '!' is permissive in the type.
3445 clear_tv(rettv);
3446 rettv->v_type = VAR_BOOL;
3447 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3448 }
3449 }
3450 return OK;
3451}
3452
3453/*
3454 * Recognize v: variables that are constants and set "rettv".
3455 */
3456 static void
3457get_vim_constant(char_u **arg, typval_T *rettv)
3458{
3459 if (STRNCMP(*arg, "v:true", 6) == 0)
3460 {
3461 rettv->v_type = VAR_BOOL;
3462 rettv->vval.v_number = VVAL_TRUE;
3463 *arg += 6;
3464 }
3465 else if (STRNCMP(*arg, "v:false", 7) == 0)
3466 {
3467 rettv->v_type = VAR_BOOL;
3468 rettv->vval.v_number = VVAL_FALSE;
3469 *arg += 7;
3470 }
3471 else if (STRNCMP(*arg, "v:null", 6) == 0)
3472 {
3473 rettv->v_type = VAR_SPECIAL;
3474 rettv->vval.v_number = VVAL_NULL;
3475 *arg += 6;
3476 }
3477 else if (STRNCMP(*arg, "v:none", 6) == 0)
3478 {
3479 rettv->v_type = VAR_SPECIAL;
3480 rettv->vval.v_number = VVAL_NONE;
3481 *arg += 6;
3482 }
3483}
3484
Bram Moolenaar61a89812020-05-07 16:58:17 +02003485 static exptype_T
3486get_compare_type(char_u *p, int *len, int *type_is)
3487{
3488 exptype_T type = EXPR_UNKNOWN;
3489 int i;
3490
3491 switch (p[0])
3492 {
3493 case '=': if (p[1] == '=')
3494 type = EXPR_EQUAL;
3495 else if (p[1] == '~')
3496 type = EXPR_MATCH;
3497 break;
3498 case '!': if (p[1] == '=')
3499 type = EXPR_NEQUAL;
3500 else if (p[1] == '~')
3501 type = EXPR_NOMATCH;
3502 break;
3503 case '>': if (p[1] != '=')
3504 {
3505 type = EXPR_GREATER;
3506 *len = 1;
3507 }
3508 else
3509 type = EXPR_GEQUAL;
3510 break;
3511 case '<': if (p[1] != '=')
3512 {
3513 type = EXPR_SMALLER;
3514 *len = 1;
3515 }
3516 else
3517 type = EXPR_SEQUAL;
3518 break;
3519 case 'i': if (p[1] == 's')
3520 {
3521 // "is" and "isnot"; but not a prefix of a name
3522 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3523 *len = 5;
3524 i = p[*len];
3525 if (!isalnum(i) && i != '_')
3526 {
3527 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3528 *type_is = TRUE;
3529 }
3530 }
3531 break;
3532 }
3533 return type;
3534}
3535
3536/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 * Compile code to apply '-', '+' and '!'.
3538 */
3539 static int
3540compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3541{
3542 char_u *p = end;
3543
3544 // this works from end to start
3545 while (p > start)
3546 {
3547 --p;
3548 if (*p == '-' || *p == '+')
3549 {
3550 int negate = *p == '-';
3551 isn_T *isn;
3552
3553 // TODO: check type
3554 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3555 {
3556 --p;
3557 if (*p == '-')
3558 negate = !negate;
3559 }
3560 // only '-' has an effect, for '+' we only check the type
3561 if (negate)
3562 isn = generate_instr(cctx, ISN_NEGATENR);
3563 else
3564 isn = generate_instr(cctx, ISN_CHECKNR);
3565 if (isn == NULL)
3566 return FAIL;
3567 }
3568 else
3569 {
3570 int invert = TRUE;
3571
3572 while (p > start && p[-1] == '!')
3573 {
3574 --p;
3575 invert = !invert;
3576 }
3577 if (generate_2BOOL(cctx, invert) == FAIL)
3578 return FAIL;
3579 }
3580 }
3581 return OK;
3582}
3583
3584/*
3585 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003586 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 */
3588 static int
3589compile_subscript(
3590 char_u **arg,
3591 cctx_T *cctx,
3592 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003593 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003594 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595{
3596 for (;;)
3597 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003598 char_u *p = skipwhite(*arg);
3599
3600 if (*p == NUL || (VIM_ISWHITE(**arg) && comment_start(p)))
3601 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003602 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003603
3604 // If a following line starts with "->{" or "->X" advance to that
3605 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003606 // Also if a following line starts with ".x".
3607 if (next != NULL &&
3608 ((next[0] == '-' && next[1] == '>'
3609 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3610 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003611 {
3612 next = next_line_from_context(cctx, TRUE);
3613 if (next == NULL)
3614 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003615 *arg = next;
3616 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003617 }
3618 }
3619
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003620 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003622 garray_T *stack = &cctx->ctx_type_stack;
3623 type_T *type;
3624 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003626 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003627 return FAIL;
3628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003630 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3631
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003632 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3634 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003635 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 return FAIL;
3637 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003638 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003640 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003641 return FAIL;
3642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 // something->method()
3644 // Apply the '!', '-' and '+' first:
3645 // -1.0->func() works like (-1.0)->func()
3646 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3647 return FAIL;
3648 *start_leader = end_leader; // don't apply again later
3649
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003650 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003651 *arg = skipwhite(p);
3652 if (may_get_next_line(p, arg, cctx) == FAIL)
3653 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 if (**arg == '{')
3655 {
3656 // lambda call: list->{lambda}
3657 if (compile_lambda_call(arg, cctx) == FAIL)
3658 return FAIL;
3659 }
3660 else
3661 {
3662 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003663 p = *arg;
3664 if (ASCII_ISALPHA(*p) && p[1] == ':')
3665 p += 2;
3666 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 ;
3668 if (*p != '(')
3669 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003670 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 return FAIL;
3672 }
3673 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003674 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 return FAIL;
3676 }
3677 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003678 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003680 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003681 type_T **typep;
3682
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003683 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003684 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003685 // TODO: blob index
3686 // TODO: more arguments
3687 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003688 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003689 return FAIL;
3690
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003691 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003692 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003693 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003694 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003695 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 return FAIL;
3697
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003698 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3699 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 if (**arg != ']')
3701 {
3702 emsg(_(e_missbrac));
3703 return FAIL;
3704 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003705 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003707 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3708 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003709 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003710 if ((*typep)->tt_type == VAR_LIST)
3711 *typep = (*typep)->tt_member;
3712 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3713 return FAIL;
3714 }
3715 else if ((*typep)->tt_type == VAR_DICT)
3716 {
3717 *typep = (*typep)->tt_member;
3718 if (may_generate_2STRING(-1, cctx) == FAIL)
3719 return FAIL;
3720 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3721 return FAIL;
3722 }
3723 else
3724 {
3725 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003726 return FAIL;
3727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003729 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003731 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003732 return FAIL;
3733
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003734 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003735 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3736 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003738 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 if (eval_isnamec1(*p))
3740 while (eval_isnamec(*p))
3741 MB_PTR_ADV(p);
3742 if (p == *arg)
3743 {
3744 semsg(_(e_syntax_at), *arg);
3745 return FAIL;
3746 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003747 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 return FAIL;
3749 *arg = p;
3750 }
3751 else
3752 break;
3753 }
3754
3755 // TODO - see handle_subscript():
3756 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3757 // Don't do this when "Func" is already a partial that was bound
3758 // explicitly (pt_auto is FALSE).
3759
3760 return OK;
3761}
3762
3763/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003764 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3765 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003767 * If the value is a constant "ppconst->pp_ret" will be set.
3768 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003769 *
3770 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 */
3772
3773/*
3774 * number number constant
3775 * 0zFFFFFFFF Blob constant
3776 * "string" string constant
3777 * 'string' literal string constant
3778 * &option-name option value
3779 * @r register contents
3780 * identifier variable value
3781 * function() function call
3782 * $VAR environment variable
3783 * (expression) nested expression
3784 * [expr, expr] List
3785 * {key: val, key: val} Dictionary
3786 * #{key: val, key: val} Dictionary with literal keys
3787 *
3788 * Also handle:
3789 * ! in front logical NOT
3790 * - in front unary minus
3791 * + in front unary plus (ignored)
3792 * trailing (arg) funcref/partial call
3793 * trailing [] subscript in String or List
3794 * trailing .name entry in Dictionary
3795 * trailing ->name() method call
3796 */
3797 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003798compile_expr7(
3799 char_u **arg,
3800 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003801 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 char_u *start_leader, *end_leader;
3804 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003805 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003806 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
3808 /*
3809 * Skip '!', '-' and '+' characters. They are handled later.
3810 */
3811 start_leader = *arg;
3812 while (**arg == '!' || **arg == '-' || **arg == '+')
3813 *arg = skipwhite(*arg + 1);
3814 end_leader = *arg;
3815
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003816 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 switch (**arg)
3818 {
3819 /*
3820 * Number constant.
3821 */
3822 case '0': // also for blob starting with 0z
3823 case '1':
3824 case '2':
3825 case '3':
3826 case '4':
3827 case '5':
3828 case '6':
3829 case '7':
3830 case '8':
3831 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003832 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 return FAIL;
3834 break;
3835
3836 /*
3837 * String constant: "string".
3838 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003839 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 return FAIL;
3841 break;
3842
3843 /*
3844 * Literal string constant: 'str''ing'.
3845 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003846 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 return FAIL;
3848 break;
3849
3850 /*
3851 * Constant Vim variable.
3852 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003853 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 ret = NOTDONE;
3855 break;
3856
3857 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003858 * "true" constant
3859 */
3860 case 't': if (STRNCMP(*arg, "true", 4) == 0
3861 && !eval_isnamec((*arg)[4]))
3862 {
3863 *arg += 4;
3864 rettv->v_type = VAR_BOOL;
3865 rettv->vval.v_number = VVAL_TRUE;
3866 }
3867 else
3868 ret = NOTDONE;
3869 break;
3870
3871 /*
3872 * "false" constant
3873 */
3874 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3875 && !eval_isnamec((*arg)[5]))
3876 {
3877 *arg += 5;
3878 rettv->v_type = VAR_BOOL;
3879 rettv->vval.v_number = VVAL_FALSE;
3880 }
3881 else
3882 ret = NOTDONE;
3883 break;
3884
3885 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 * List: [expr, expr]
3887 */
3888 case '[': ret = compile_list(arg, cctx);
3889 break;
3890
3891 /*
3892 * Dictionary: #{key: val, key: val}
3893 */
3894 case '#': if ((*arg)[1] == '{')
3895 {
3896 ++*arg;
3897 ret = compile_dict(arg, cctx, TRUE);
3898 }
3899 else
3900 ret = NOTDONE;
3901 break;
3902
3903 /*
3904 * Lambda: {arg, arg -> expr}
3905 * Dictionary: {'key': val, 'key': val}
3906 */
3907 case '{': {
3908 char_u *start = skipwhite(*arg + 1);
3909
3910 // Find out what comes after the arguments.
3911 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003912 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 if (ret != FAIL && *start == '>')
3914 ret = compile_lambda(arg, cctx);
3915 else
3916 ret = compile_dict(arg, cctx, FALSE);
3917 }
3918 break;
3919
3920 /*
3921 * Option value: &name
3922 */
3923 case '&': ret = compile_get_option(arg, cctx);
3924 break;
3925
3926 /*
3927 * Environment variable: $VAR.
3928 */
3929 case '$': ret = compile_get_env(arg, cctx);
3930 break;
3931
3932 /*
3933 * Register contents: @r.
3934 */
3935 case '@': ret = compile_get_register(arg, cctx);
3936 break;
3937 /*
3938 * nested expression: (expression).
3939 */
3940 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003941
3942 // recursive!
3943 if (ppconst->pp_used <= PPSIZE - 10)
3944 {
3945 ret = compile_expr1(arg, cctx, ppconst);
3946 }
3947 else
3948 {
3949 // Not enough space in ppconst, flush constants.
3950 if (generate_ppconst(cctx, ppconst) == FAIL)
3951 return FAIL;
3952 ret = compile_expr0(arg, cctx);
3953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 *arg = skipwhite(*arg);
3955 if (**arg == ')')
3956 ++*arg;
3957 else if (ret == OK)
3958 {
3959 emsg(_(e_missing_close));
3960 ret = FAIL;
3961 }
3962 break;
3963
3964 default: ret = NOTDONE;
3965 break;
3966 }
3967 if (ret == FAIL)
3968 return FAIL;
3969
Bram Moolenaar1c747212020-05-09 18:28:34 +02003970 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 {
3972 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003973 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003975 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 return FAIL;
3977 }
3978 start_leader = end_leader; // don't apply again below
3979
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003980 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003981 clear_tv(rettv);
3982 else
3983 // A constant expression can possibly be handled compile time,
3984 // return the value instead of generating code.
3985 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 }
3987 else if (ret == NOTDONE)
3988 {
3989 char_u *p;
3990 int r;
3991
3992 if (!eval_isnamec1(**arg))
3993 {
3994 semsg(_("E1015: Name expected: %s"), *arg);
3995 return FAIL;
3996 }
3997
3998 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003999 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004001 {
4002 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004005 {
4006 if (generate_ppconst(cctx, ppconst) == FAIL)
4007 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004009 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 if (r == FAIL)
4011 return FAIL;
4012 }
4013
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004014 // Handle following "[]", ".member", etc.
4015 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004016 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004017 ppconst) == FAIL)
4018 return FAIL;
4019 if (ppconst->pp_used > 0)
4020 {
4021 // apply the '!', '-' and '+' before the constant
4022 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4023 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4024 return FAIL;
4025 return OK;
4026 }
4027 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004029 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030}
4031
4032/*
4033 * * number multiplication
4034 * / number division
4035 * % number modulo
4036 */
4037 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004038compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039{
4040 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004041 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004042 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004044 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004045 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 return FAIL;
4047
4048 /*
4049 * Repeat computing, until no "*", "/" or "%" is following.
4050 */
4051 for (;;)
4052 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004053 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 if (*op != '*' && *op != '/' && *op != '%')
4055 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004056 if (next != NULL)
4057 {
4058 *arg = next_line_from_context(cctx, TRUE);
4059 op = skipwhite(*arg);
4060 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004061
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004062 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 {
4064 char_u buf[3];
4065
4066 vim_strncpy(buf, op, 1);
4067 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004068 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 }
4070 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004071 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004072 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004074 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004075 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004077
4078 if (ppconst->pp_used == ppconst_used + 2
4079 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4080 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004081 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004082 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4083 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004084 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004086 // both are numbers: compute the result
4087 switch (*op)
4088 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004089 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004090 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004091 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004092 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004093 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004094 break;
4095 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004096 tv1->vval.v_number = res;
4097 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004098 }
4099 else
4100 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004101 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004102 generate_two_op(cctx, op);
4103 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 }
4105
4106 return OK;
4107}
4108
4109/*
4110 * + number addition
4111 * - number subtraction
4112 * .. string concatenation
4113 */
4114 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004115compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116{
4117 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004118 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004120 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121
4122 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004123 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 return FAIL;
4125
4126 /*
4127 * Repeat computing, until no "+", "-" or ".." is following.
4128 */
4129 for (;;)
4130 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004131 op = may_peek_next_line(cctx, *arg, &next);
4132 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 break;
4134 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004135 if (next != NULL)
4136 {
4137 *arg = next_line_from_context(cctx, TRUE);
4138 op = skipwhite(*arg);
4139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004141 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 {
4143 char_u buf[3];
4144
4145 vim_strncpy(buf, op, oplen);
4146 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004147 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 }
4149
4150 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004151 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004152 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004154 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004155 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 return FAIL;
4157
Bram Moolenaara5565e42020-05-09 15:44:01 +02004158 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004159 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004160 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4161 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4162 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4163 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004165 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4166 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004167
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004168 // concat/subtract/add constant numbers
4169 if (*op == '+')
4170 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4171 else if (*op == '-')
4172 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4173 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004174 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004175 // concatenate constant strings
4176 char_u *s1 = tv1->vval.v_string;
4177 char_u *s2 = tv2->vval.v_string;
4178 size_t len1 = STRLEN(s1);
4179
4180 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4181 if (tv1->vval.v_string == NULL)
4182 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004183 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004184 return FAIL;
4185 }
4186 mch_memmove(tv1->vval.v_string, s1, len1);
4187 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004188 vim_free(s1);
4189 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004190 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004191 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 }
4193 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004194 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004195 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004196 if (*op == '.')
4197 {
4198 if (may_generate_2STRING(-2, cctx) == FAIL
4199 || may_generate_2STRING(-1, cctx) == FAIL)
4200 return FAIL;
4201 generate_instr_drop(cctx, ISN_CONCAT, 1);
4202 }
4203 else
4204 generate_two_op(cctx, op);
4205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 }
4207
4208 return OK;
4209}
4210
4211/*
4212 * expr5a == expr5b
4213 * expr5a =~ expr5b
4214 * expr5a != expr5b
4215 * expr5a !~ expr5b
4216 * expr5a > expr5b
4217 * expr5a >= expr5b
4218 * expr5a < expr5b
4219 * expr5a <= expr5b
4220 * expr5a is expr5b
4221 * expr5a isnot expr5b
4222 *
4223 * Produces instructions:
4224 * EVAL expr5a Push result of "expr5a"
4225 * EVAL expr5b Push result of "expr5b"
4226 * COMPARE one of the compare instructions
4227 */
4228 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004229compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230{
4231 exptype_T type = EXPR_UNKNOWN;
4232 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004233 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004236 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237
4238 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004239 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 return FAIL;
4241
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004242 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004243 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244
4245 /*
4246 * If there is a comparative operator, use it.
4247 */
4248 if (type != EXPR_UNKNOWN)
4249 {
4250 int ic = FALSE; // Default: do not ignore case
4251
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004252 if (next != NULL)
4253 {
4254 *arg = next_line_from_context(cctx, TRUE);
4255 p = skipwhite(*arg);
4256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 if (type_is && (p[len] == '?' || p[len] == '#'))
4258 {
4259 semsg(_(e_invexpr2), *arg);
4260 return FAIL;
4261 }
4262 // extra question mark appended: ignore case
4263 if (p[len] == '?')
4264 {
4265 ic = TRUE;
4266 ++len;
4267 }
4268 // extra '#' appended: match case (ignored)
4269 else if (p[len] == '#')
4270 ++len;
4271 // nothing appended: match case
4272
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004273 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 {
4275 char_u buf[7];
4276
4277 vim_strncpy(buf, p, len);
4278 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004279 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 }
4281
4282 // get the second variable
4283 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004284 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004285 return FAIL;
4286
Bram Moolenaara5565e42020-05-09 15:44:01 +02004287 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 return FAIL;
4289
Bram Moolenaara5565e42020-05-09 15:44:01 +02004290 if (ppconst->pp_used == ppconst_used + 2)
4291 {
4292 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4293 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4294 int ret;
4295
4296 // Both sides are a constant, compute the result now.
4297 // First check for a valid combination of types, this is more
4298 // strict than typval_compare().
4299 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4300 ret = FAIL;
4301 else
4302 {
4303 ret = typval_compare(tv1, tv2, type, ic);
4304 tv1->v_type = VAR_BOOL;
4305 tv1->vval.v_number = tv1->vval.v_number
4306 ? VVAL_TRUE : VVAL_FALSE;
4307 clear_tv(tv2);
4308 --ppconst->pp_used;
4309 }
4310 return ret;
4311 }
4312
4313 generate_ppconst(cctx, ppconst);
4314 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 }
4316
4317 return OK;
4318}
4319
Bram Moolenaar7f141552020-05-09 17:35:53 +02004320static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322/*
4323 * Compile || or &&.
4324 */
4325 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004326compile_and_or(
4327 char_u **arg,
4328 cctx_T *cctx,
4329 char *op,
4330 ppconst_T *ppconst,
4331 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004333 char_u *next;
4334 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 int opchar = *op;
4336
4337 if (p[0] == opchar && p[1] == opchar)
4338 {
4339 garray_T *instr = &cctx->ctx_instr;
4340 garray_T end_ga;
4341
4342 /*
4343 * Repeat until there is no following "||" or "&&"
4344 */
4345 ga_init2(&end_ga, sizeof(int), 10);
4346 while (p[0] == opchar && p[1] == opchar)
4347 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004348 if (next != NULL)
4349 {
4350 *arg = next_line_from_context(cctx, TRUE);
4351 p = skipwhite(*arg);
4352 }
4353
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004354 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4355 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004357 return FAIL;
4358 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359
Bram Moolenaara5565e42020-05-09 15:44:01 +02004360 // TODO: use ppconst if the value is a constant
4361 generate_ppconst(cctx, ppconst);
4362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 if (ga_grow(&end_ga, 1) == FAIL)
4364 {
4365 ga_clear(&end_ga);
4366 return FAIL;
4367 }
4368 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4369 ++end_ga.ga_len;
4370 generate_JUMP(cctx, opchar == '|'
4371 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4372
4373 // eval the next expression
4374 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004375 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004376 return FAIL;
4377
Bram Moolenaara5565e42020-05-09 15:44:01 +02004378 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4379 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 {
4381 ga_clear(&end_ga);
4382 return FAIL;
4383 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004384
4385 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004387 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388
4389 // Fill in the end label in all jumps.
4390 while (end_ga.ga_len > 0)
4391 {
4392 isn_T *isn;
4393
4394 --end_ga.ga_len;
4395 isn = ((isn_T *)instr->ga_data)
4396 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4397 isn->isn_arg.jump.jump_where = instr->ga_len;
4398 }
4399 ga_clear(&end_ga);
4400 }
4401
4402 return OK;
4403}
4404
4405/*
4406 * expr4a && expr4a && expr4a logical AND
4407 *
4408 * Produces instructions:
4409 * EVAL expr4a Push result of "expr4a"
4410 * JUMP_AND_KEEP_IF_FALSE end
4411 * EVAL expr4b Push result of "expr4b"
4412 * JUMP_AND_KEEP_IF_FALSE end
4413 * EVAL expr4c Push result of "expr4c"
4414 * end:
4415 */
4416 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004417compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004419 int ppconst_used = ppconst->pp_used;
4420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004422 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 return FAIL;
4424
4425 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004426 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427}
4428
4429/*
4430 * expr3a || expr3b || expr3c logical OR
4431 *
4432 * Produces instructions:
4433 * EVAL expr3a Push result of "expr3a"
4434 * JUMP_AND_KEEP_IF_TRUE end
4435 * EVAL expr3b Push result of "expr3b"
4436 * JUMP_AND_KEEP_IF_TRUE end
4437 * EVAL expr3c Push result of "expr3c"
4438 * end:
4439 */
4440 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004441compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004443 int ppconst_used = ppconst->pp_used;
4444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004446 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 return FAIL;
4448
4449 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004450 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451}
4452
4453/*
4454 * Toplevel expression: expr2 ? expr1a : expr1b
4455 *
4456 * Produces instructions:
4457 * EVAL expr2 Push result of "expr"
4458 * JUMP_IF_FALSE alt jump if false
4459 * EVAL expr1a
4460 * JUMP_ALWAYS end
4461 * alt: EVAL expr1b
4462 * end:
4463 */
4464 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004465compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466{
4467 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004468 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004469 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470
Bram Moolenaar61a89812020-05-07 16:58:17 +02004471 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004472 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 return FAIL;
4474
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004475 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 if (*p == '?')
4477 {
4478 garray_T *instr = &cctx->ctx_instr;
4479 garray_T *stack = &cctx->ctx_type_stack;
4480 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004481 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004483 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004485 int has_const_expr = FALSE;
4486 int const_value = FALSE;
4487 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004489 if (next != NULL)
4490 {
4491 *arg = next_line_from_context(cctx, TRUE);
4492 p = skipwhite(*arg);
4493 }
4494
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004495 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4496 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004498 return FAIL;
4499 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500
Bram Moolenaara5565e42020-05-09 15:44:01 +02004501 if (ppconst->pp_used == ppconst_used + 1)
4502 {
4503 // the condition is a constant, we know whether the ? or the :
4504 // expression is to be evaluated.
4505 has_const_expr = TRUE;
4506 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4507 clear_tv(&ppconst->pp_tv[ppconst_used]);
4508 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004509 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4510 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004511 }
4512 else
4513 {
4514 generate_ppconst(cctx, ppconst);
4515 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517
4518 // evaluate the second expression; any type is accepted
4519 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004520 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004521 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004522 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004523 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524
Bram Moolenaara5565e42020-05-09 15:44:01 +02004525 if (!has_const_expr)
4526 {
4527 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528
Bram Moolenaara5565e42020-05-09 15:44:01 +02004529 // remember the type and drop it
4530 --stack->ga_len;
4531 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532
Bram Moolenaara5565e42020-05-09 15:44:01 +02004533 end_idx = instr->ga_len;
4534 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4535
4536 // jump here from JUMP_IF_FALSE
4537 isn = ((isn_T *)instr->ga_data) + alt_idx;
4538 isn->isn_arg.jump.jump_where = instr->ga_len;
4539 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540
4541 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004542 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 if (*p != ':')
4544 {
4545 emsg(_(e_missing_colon));
4546 return FAIL;
4547 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004548 if (next != NULL)
4549 {
4550 *arg = next_line_from_context(cctx, TRUE);
4551 p = skipwhite(*arg);
4552 }
4553
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004554 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4555 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004557 return FAIL;
4558 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559
4560 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004561 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004562 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4563 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004565 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004566 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004567 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004568 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569
Bram Moolenaara5565e42020-05-09 15:44:01 +02004570 if (!has_const_expr)
4571 {
4572 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573
Bram Moolenaara5565e42020-05-09 15:44:01 +02004574 // If the types differ, the result has a more generic type.
4575 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4576 common_type(type1, type2, &type2, cctx->ctx_type_list);
4577
4578 // jump here from JUMP_ALWAYS
4579 isn = ((isn_T *)instr->ga_data) + end_idx;
4580 isn->isn_arg.jump.jump_where = instr->ga_len;
4581 }
4582
4583 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 }
4585 return OK;
4586}
4587
4588/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004589 * Toplevel expression.
4590 */
4591 static int
4592compile_expr0(char_u **arg, cctx_T *cctx)
4593{
4594 ppconst_T ppconst;
4595
4596 CLEAR_FIELD(ppconst);
4597 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4598 {
4599 clear_ppconst(&ppconst);
4600 return FAIL;
4601 }
4602 if (generate_ppconst(cctx, &ppconst) == FAIL)
4603 return FAIL;
4604 return OK;
4605}
4606
4607/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 * compile "return [expr]"
4609 */
4610 static char_u *
4611compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4612{
4613 char_u *p = arg;
4614 garray_T *stack = &cctx->ctx_type_stack;
4615 type_T *stack_type;
4616
4617 if (*p != NUL && *p != '|' && *p != '\n')
4618 {
4619 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004620 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 return NULL;
4622
4623 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4624 if (set_return_type)
4625 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004626 else
4627 {
4628 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4629 && stack_type->tt_type != VAR_VOID
4630 && stack_type->tt_type != VAR_UNKNOWN)
4631 {
4632 emsg(_("E1096: Returning a value in a function without a return type"));
4633 return NULL;
4634 }
4635 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 == FAIL)
4637 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 }
4640 else
4641 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004642 // "set_return_type" cannot be TRUE, only used for a lambda which
4643 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004644 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4645 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 {
4647 emsg(_("E1003: Missing return value"));
4648 return NULL;
4649 }
4650
4651 // No argument, return zero.
4652 generate_PUSHNR(cctx, 0);
4653 }
4654
4655 if (generate_instr(cctx, ISN_RETURN) == NULL)
4656 return NULL;
4657
4658 // "return val | endif" is possible
4659 return skipwhite(p);
4660}
4661
4662/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004663 * Get a line from the compilation context, compatible with exarg_T getline().
4664 * Return a pointer to the line in allocated memory.
4665 * Return NULL for end-of-file or some error.
4666 */
4667 static char_u *
4668exarg_getline(
4669 int c UNUSED,
4670 void *cookie,
4671 int indent UNUSED,
4672 int do_concat UNUSED)
4673{
4674 cctx_T *cctx = (cctx_T *)cookie;
4675
4676 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4677 {
4678 iemsg("Heredoc got to end");
4679 return NULL;
4680 }
4681 ++cctx->ctx_lnum;
4682 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4683 [cctx->ctx_lnum]);
4684}
4685
4686/*
4687 * Compile a nested :def command.
4688 */
4689 static char_u *
4690compile_nested_function(exarg_T *eap, cctx_T *cctx)
4691{
4692 char_u *name_start = eap->arg;
4693 char_u *name_end = to_name_end(eap->arg, FALSE);
4694 char_u *name = get_lambda_name();
4695 lvar_T *lvar;
4696 ufunc_T *ufunc;
4697
4698 eap->arg = name_end;
4699 eap->getline = exarg_getline;
4700 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004701 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004702 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004703 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004704
Bram Moolenaar822ba242020-05-24 23:00:18 +02004705 if (ufunc == NULL)
4706 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004707 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004708 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004709 return NULL;
4710
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004711 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004712 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004713 TRUE, ufunc->uf_func_type);
4714
4715 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4716 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4717 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004718
Bram Moolenaar61a89812020-05-07 16:58:17 +02004719 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004720 return (char_u *)"";
4721}
4722
4723/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 * Return the length of an assignment operator, or zero if there isn't one.
4725 */
4726 int
4727assignment_len(char_u *p, int *heredoc)
4728{
4729 if (*p == '=')
4730 {
4731 if (p[1] == '<' && p[2] == '<')
4732 {
4733 *heredoc = TRUE;
4734 return 3;
4735 }
4736 return 1;
4737 }
4738 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4739 return 2;
4740 if (STRNCMP(p, "..=", 3) == 0)
4741 return 3;
4742 return 0;
4743}
4744
4745// words that cannot be used as a variable
4746static char *reserved[] = {
4747 "true",
4748 "false",
4749 NULL
4750};
4751
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004752typedef enum {
4753 dest_local,
4754 dest_option,
4755 dest_env,
4756 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004757 dest_buffer,
4758 dest_window,
4759 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004760 dest_vimvar,
4761 dest_script,
4762 dest_reg,
4763} assign_dest_T;
4764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004766 * Generate the load instruction for "name".
4767 */
4768 static void
4769generate_loadvar(
4770 cctx_T *cctx,
4771 assign_dest_T dest,
4772 char_u *name,
4773 lvar_T *lvar,
4774 type_T *type)
4775{
4776 switch (dest)
4777 {
4778 case dest_option:
4779 // TODO: check the option exists
4780 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4781 break;
4782 case dest_global:
4783 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4784 break;
4785 case dest_buffer:
4786 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4787 break;
4788 case dest_window:
4789 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4790 break;
4791 case dest_tab:
4792 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4793 break;
4794 case dest_script:
4795 compile_load_scriptvar(cctx,
4796 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4797 break;
4798 case dest_env:
4799 // Include $ in the name here
4800 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4801 break;
4802 case dest_reg:
4803 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4804 break;
4805 case dest_vimvar:
4806 generate_LOADV(cctx, name + 2, TRUE);
4807 break;
4808 case dest_local:
4809 if (lvar->lv_from_outer)
4810 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4811 NULL, type);
4812 else
4813 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4814 break;
4815 }
4816}
4817
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004818 void
4819vim9_declare_error(char_u *name)
4820{
4821 char *scope = "";
4822
4823 switch (*name)
4824 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004825 case 'g': scope = _("global"); break;
4826 case 'b': scope = _("buffer"); break;
4827 case 'w': scope = _("window"); break;
4828 case 't': scope = _("tab"); break;
4829 case 'v': scope = "v:"; break;
4830 case '$': semsg(_(e_declare_env_var), name); return;
4831 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004832 }
4833 semsg(_(e_declare_var), scope, name);
4834}
4835
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004836/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004837 * Compile declaration and assignment:
4838 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004840 * Return NULL for an error.
4841 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 */
4843 static char_u *
4844compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4845{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004846 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004848 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 char_u *ret = NULL;
4850 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004851 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004854 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 int oplen = 0;
4857 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004858 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004859 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004860 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004864 // Skip over the "var" or "[var, var]" to get to any "=".
4865 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4866 if (p == NULL)
4867 return *arg == '[' ? arg : NULL;
4868
4869 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004871 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 return NULL;
4873 }
4874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 sp = p;
4876 p = skipwhite(p);
4877 op = p;
4878 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004879
4880 if (var_count > 0 && oplen == 0)
4881 // can be something like "[1, 2]->func()"
4882 return arg;
4883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4885 {
4886 char_u buf[4];
4887
4888 vim_strncpy(buf, op, oplen);
4889 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004890 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004891 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 if (heredoc)
4894 {
4895 list_T *l;
4896 listitem_T *li;
4897
4898 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004899 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004901 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902
4903 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004904 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 {
4906 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4907 li->li_tv.vval.v_string = NULL;
4908 }
4909 generate_NEWLIST(cctx, l->lv_len);
4910 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004911 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 list_free(l);
4913 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004914 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004916 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004918 // for "[var, var] = expr" evaluate the expression here, loop over the
4919 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004920
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004921 p = skipwhite(op + oplen);
4922 if (compile_expr0(&p, cctx) == FAIL)
4923 return NULL;
4924 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004926 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004927 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004928 type_T *stacktype;
4929
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004930 stacktype = stack->ga_len == 0 ? &t_void
4931 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004932 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004934 emsg(_(e_cannot_use_void));
4935 goto theend;
4936 }
4937 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4938 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004939 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4940 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004941 }
4942 }
4943
4944 /*
4945 * Loop over variables in "[var, var] = expr".
4946 * For "var = expr" and "let var: type" this is done only once.
4947 */
4948 if (var_count > 0)
4949 var_start = skipwhite(arg + 1); // skip over the "["
4950 else
4951 var_start = arg;
4952 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4953 {
4954 char_u *var_end = skip_var_one(var_start, FALSE);
4955 size_t varlen;
4956 int new_local = FALSE;
4957 int opt_type;
4958 int opt_flags = 0;
4959 assign_dest_T dest = dest_local;
4960 int vimvaridx = -1;
4961 lvar_T *lvar = NULL;
4962 lvar_T arg_lvar;
4963 int has_type = FALSE;
4964 int has_index = FALSE;
4965 int instr_count = -1;
4966
4967 p = (*var_start == '&' || *var_start == '$'
4968 || *var_start == '@') ? var_start + 1 : var_start;
4969 p = to_name_end(p, TRUE);
4970
4971 // "a: type" is declaring variable "a" with a type, not "a:".
4972 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4973 --var_end;
4974 if (is_decl && p == var_start + 2 && p[-1] == ':')
4975 --p;
4976
4977 varlen = p - var_start;
4978 vim_free(name);
4979 name = vim_strnsave(var_start, varlen);
4980 if (name == NULL)
4981 return NULL;
4982 if (!heredoc)
4983 type = &t_any;
4984
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004985 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004986 {
4987 if (*var_start == '&')
4988 {
4989 int cc;
4990 long numval;
4991
4992 dest = dest_option;
4993 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004995 emsg(_(e_const_option));
4996 goto theend;
4997 }
4998 if (is_decl)
4999 {
5000 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5001 goto theend;
5002 }
5003 p = var_start;
5004 p = find_option_end(&p, &opt_flags);
5005 if (p == NULL)
5006 {
5007 // cannot happen?
5008 emsg(_(e_letunexp));
5009 goto theend;
5010 }
5011 cc = *p;
5012 *p = NUL;
5013 opt_type = get_option_value(var_start + 1, &numval,
5014 NULL, opt_flags);
5015 *p = cc;
5016 if (opt_type == -3)
5017 {
5018 semsg(_(e_unknown_option), var_start);
5019 goto theend;
5020 }
5021 if (opt_type == -2 || opt_type == 0)
5022 type = &t_string;
5023 else
5024 type = &t_number; // both number and boolean option
5025 }
5026 else if (*var_start == '$')
5027 {
5028 dest = dest_env;
5029 type = &t_string;
5030 if (is_decl)
5031 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005032 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005033 goto theend;
5034 }
5035 }
5036 else if (*var_start == '@')
5037 {
5038 if (!valid_yank_reg(var_start[1], TRUE))
5039 {
5040 emsg_invreg(var_start[1]);
5041 goto theend;
5042 }
5043 dest = dest_reg;
5044 type = &t_string;
5045 if (is_decl)
5046 {
5047 semsg(_("E1066: Cannot declare a register: %s"), name);
5048 goto theend;
5049 }
5050 }
5051 else if (STRNCMP(var_start, "g:", 2) == 0)
5052 {
5053 dest = dest_global;
5054 if (is_decl)
5055 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005056 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005057 goto theend;
5058 }
5059 }
5060 else if (STRNCMP(var_start, "b:", 2) == 0)
5061 {
5062 dest = dest_buffer;
5063 if (is_decl)
5064 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005065 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005066 goto theend;
5067 }
5068 }
5069 else if (STRNCMP(var_start, "w:", 2) == 0)
5070 {
5071 dest = dest_window;
5072 if (is_decl)
5073 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005074 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005075 goto theend;
5076 }
5077 }
5078 else if (STRNCMP(var_start, "t:", 2) == 0)
5079 {
5080 dest = dest_tab;
5081 if (is_decl)
5082 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005083 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 goto theend;
5085 }
5086 }
5087 else if (STRNCMP(var_start, "v:", 2) == 0)
5088 {
5089 typval_T *vtv;
5090 int di_flags;
5091
5092 vimvaridx = find_vim_var(name + 2, &di_flags);
5093 if (vimvaridx < 0)
5094 {
5095 semsg(_(e_var_notfound), var_start);
5096 goto theend;
5097 }
5098 // We use the current value of "sandbox" here, is that OK?
5099 if (var_check_ro(di_flags, name, FALSE))
5100 goto theend;
5101 dest = dest_vimvar;
5102 vtv = get_vim_var_tv(vimvaridx);
5103 type = typval2type(vtv);
5104 if (is_decl)
5105 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005106 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005107 goto theend;
5108 }
5109 }
5110 else
5111 {
5112 int idx;
5113
5114 for (idx = 0; reserved[idx] != NULL; ++idx)
5115 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005116 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005117 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005118 goto theend;
5119 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005120
5121 lvar = lookup_local(var_start, varlen, cctx);
5122 if (lvar == NULL)
5123 {
5124 CLEAR_FIELD(arg_lvar);
5125 if (lookup_arg(var_start, varlen,
5126 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5127 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005128 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005129 if (is_decl)
5130 {
5131 semsg(_(e_used_as_arg), name);
5132 goto theend;
5133 }
5134 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005135 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005136 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005137 if (lvar != NULL)
5138 {
5139 if (is_decl)
5140 {
5141 semsg(_("E1017: Variable already declared: %s"), name);
5142 goto theend;
5143 }
5144 else if (lvar->lv_const)
5145 {
5146 semsg(_("E1018: Cannot assign to a constant: %s"),
5147 name);
5148 goto theend;
5149 }
5150 }
5151 else if (STRNCMP(var_start, "s:", 2) == 0
5152 || lookup_script(var_start, varlen) == OK
5153 || find_imported(var_start, varlen, cctx) != NULL)
5154 {
5155 dest = dest_script;
5156 if (is_decl)
5157 {
5158 semsg(_("E1054: Variable already declared in the script: %s"),
5159 name);
5160 goto theend;
5161 }
5162 }
5163 else if (name[1] == ':' && name[2] != NUL)
5164 {
5165 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5166 name);
5167 goto theend;
5168 }
5169 else if (!is_decl)
5170 {
5171 semsg(_("E1089: unknown variable: %s"), name);
5172 goto theend;
5173 }
5174 }
5175 }
5176
5177 // handle "a:name" as a name, not index "name" on "a"
5178 if (varlen > 1 || var_start[varlen] != ':')
5179 p = var_end;
5180
5181 if (dest != dest_option)
5182 {
5183 if (is_decl && *p == ':')
5184 {
5185 // parse optional type: "let var: type = expr"
5186 if (!VIM_ISWHITE(p[1]))
5187 {
5188 semsg(_(e_white_after), ":");
5189 goto theend;
5190 }
5191 p = skipwhite(p + 1);
5192 type = parse_type(&p, cctx->ctx_type_list);
5193 has_type = TRUE;
5194 }
5195 else if (lvar != NULL)
5196 type = lvar->lv_type;
5197 }
5198
5199 if (oplen == 3 && !heredoc && dest != dest_global
5200 && type->tt_type != VAR_STRING
5201 && type->tt_type != VAR_ANY)
5202 {
5203 emsg(_("E1019: Can only concatenate to string"));
5204 goto theend;
5205 }
5206
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005207 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005208 {
5209 if (oplen > 1 && !heredoc)
5210 {
5211 // +=, /=, etc. require an existing variable
5212 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5213 name);
5214 goto theend;
5215 }
5216
5217 // new local variable
5218 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5219 goto theend;
5220 lvar = reserve_local(cctx, var_start, varlen,
5221 cmdidx == CMD_const, type);
5222 if (lvar == NULL)
5223 goto theend;
5224 new_local = TRUE;
5225 }
5226
5227 member_type = type;
5228 if (var_end > var_start + varlen)
5229 {
5230 // Something follows after the variable: "var[idx]".
5231 if (is_decl)
5232 {
5233 emsg(_("E1087: cannot use an index when declaring a variable"));
5234 goto theend;
5235 }
5236
5237 if (var_start[varlen] == '[')
5238 {
5239 has_index = TRUE;
5240 if (type->tt_member == NULL)
5241 {
5242 semsg(_("E1088: cannot use an index on %s"), name);
5243 goto theend;
5244 }
5245 member_type = type->tt_member;
5246 }
5247 else
5248 {
5249 semsg("Not supported yet: %s", var_start);
5250 goto theend;
5251 }
5252 }
5253 else if (lvar == &arg_lvar)
5254 {
5255 semsg(_("E1090: Cannot assign to argument %s"), name);
5256 goto theend;
5257 }
5258
5259 if (!heredoc)
5260 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005261 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005263 if (oplen > 0 && var_count == 0)
5264 {
5265 // skip over the "=" and the expression
5266 p = skipwhite(op + oplen);
5267 compile_expr0(&p, cctx);
5268 }
5269 }
5270 else if (oplen > 0)
5271 {
5272 type_T *stacktype;
5273
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005274 // For "var = expr" evaluate the expression.
5275 if (var_count == 0)
5276 {
5277 int r;
5278
5279 // for "+=", "*=", "..=" etc. first load the current value
5280 if (*op != '=')
5281 {
5282 generate_loadvar(cctx, dest, name, lvar, type);
5283
5284 if (has_index)
5285 {
5286 // TODO: get member from list or dict
5287 emsg("Index with operation not supported yet");
5288 goto theend;
5289 }
5290 }
5291
5292 // Compile the expression. Temporarily hide the new local
5293 // variable here, it is not available to this expression.
5294 if (new_local)
5295 --cctx->ctx_locals.ga_len;
5296 instr_count = instr->ga_len;
5297 p = skipwhite(op + oplen);
5298 r = compile_expr0(&p, cctx);
5299 if (new_local)
5300 ++cctx->ctx_locals.ga_len;
5301 if (r == FAIL)
5302 goto theend;
5303 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005304 else if (semicolon && var_idx == var_count - 1)
5305 {
5306 // For "[var; var] = expr" get the rest of the list
5307 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5308 goto theend;
5309 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005310 else
5311 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005312 // For "[var, var] = expr" get the "var_idx" item from the
5313 // list.
5314 if (generate_GETITEM(cctx, var_idx) == FAIL)
5315 return FAIL;
5316 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005317
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005318 stacktype = stack->ga_len == 0 ? &t_void
5319 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5320 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005321 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005322 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005323 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005324 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005325 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005326 emsg(_(e_cannot_use_void));
5327 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005328 }
5329 else
5330 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005331 // An empty list or dict has a &t_void member,
5332 // for a variable that implies &t_any.
5333 if (stacktype == &t_list_empty)
5334 lvar->lv_type = &t_list_any;
5335 else if (stacktype == &t_dict_empty)
5336 lvar->lv_type = &t_dict_any;
5337 else
5338 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005339 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005340 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005341 else
5342 {
5343 type_T *use_type = lvar->lv_type;
5344
5345 if (has_index)
5346 {
5347 use_type = use_type->tt_member;
5348 if (use_type == NULL)
5349 use_type = &t_void;
5350 }
5351 if (need_type(stacktype, use_type, -1, cctx)
5352 == FAIL)
5353 goto theend;
5354 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005355 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005356 else if (*p != '=' && need_type(stacktype, member_type, -1,
5357 cctx) == FAIL)
5358 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005359 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005360 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005362 emsg(_(e_const_req_value));
5363 goto theend;
5364 }
5365 else if (!has_type || dest == dest_option)
5366 {
5367 emsg(_(e_type_req));
5368 goto theend;
5369 }
5370 else
5371 {
5372 // variables are always initialized
5373 if (ga_grow(instr, 1) == FAIL)
5374 goto theend;
5375 switch (member_type->tt_type)
5376 {
5377 case VAR_BOOL:
5378 generate_PUSHBOOL(cctx, VVAL_FALSE);
5379 break;
5380 case VAR_FLOAT:
5381#ifdef FEAT_FLOAT
5382 generate_PUSHF(cctx, 0.0);
5383#endif
5384 break;
5385 case VAR_STRING:
5386 generate_PUSHS(cctx, NULL);
5387 break;
5388 case VAR_BLOB:
5389 generate_PUSHBLOB(cctx, NULL);
5390 break;
5391 case VAR_FUNC:
5392 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5393 break;
5394 case VAR_LIST:
5395 generate_NEWLIST(cctx, 0);
5396 break;
5397 case VAR_DICT:
5398 generate_NEWDICT(cctx, 0);
5399 break;
5400 case VAR_JOB:
5401 generate_PUSHJOB(cctx, NULL);
5402 break;
5403 case VAR_CHANNEL:
5404 generate_PUSHCHANNEL(cctx, NULL);
5405 break;
5406 case VAR_NUMBER:
5407 case VAR_UNKNOWN:
5408 case VAR_ANY:
5409 case VAR_PARTIAL:
5410 case VAR_VOID:
5411 case VAR_SPECIAL: // cannot happen
5412 generate_PUSHNR(cctx, 0);
5413 break;
5414 }
5415 }
5416 if (var_count == 0)
5417 end = p;
5418 }
5419
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005420 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005421 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005422 break;
5423
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005424 if (oplen > 0 && *op != '=')
5425 {
5426 type_T *expected = &t_number;
5427 type_T *stacktype;
5428
5429 // TODO: if type is known use float or any operation
5430
5431 if (*op == '.')
5432 expected = &t_string;
5433 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5434 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5435 goto theend;
5436
5437 if (*op == '.')
5438 generate_instr_drop(cctx, ISN_CONCAT, 1);
5439 else
5440 {
5441 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5442
5443 if (isn == NULL)
5444 goto theend;
5445 switch (*op)
5446 {
5447 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5448 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5449 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5450 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5451 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005453 }
5454 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005456 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005457 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005458 int r;
5459
5460 // Compile the "idx" in "var[idx]".
5461 if (new_local)
5462 --cctx->ctx_locals.ga_len;
5463 p = skipwhite(var_start + varlen + 1);
5464 r = compile_expr0(&p, cctx);
5465 if (new_local)
5466 ++cctx->ctx_locals.ga_len;
5467 if (r == FAIL)
5468 goto theend;
5469 if (*skipwhite(p) != ']')
5470 {
5471 emsg(_(e_missbrac));
5472 goto theend;
5473 }
5474 if (type->tt_type == VAR_DICT
5475 && may_generate_2STRING(-1, cctx) == FAIL)
5476 goto theend;
5477 if (type->tt_type == VAR_LIST
5478 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005479 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005480 {
5481 emsg(_(e_number_exp));
5482 goto theend;
5483 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005484
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005485 // Load the dict or list. On the stack we then have:
5486 // - value
5487 // - index
5488 // - variable
5489 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005490
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005491 if (type->tt_type == VAR_LIST)
5492 {
5493 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5494 return FAIL;
5495 }
5496 else if (type->tt_type == VAR_DICT)
5497 {
5498 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5499 return FAIL;
5500 }
5501 else
5502 {
5503 emsg(_(e_listreq));
5504 goto theend;
5505 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005506 }
5507 else
5508 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005509 switch (dest)
5510 {
5511 case dest_option:
5512 generate_STOREOPT(cctx, name + 1, opt_flags);
5513 break;
5514 case dest_global:
5515 // include g: with the name, easier to execute that way
5516 generate_STORE(cctx, ISN_STOREG, 0, name);
5517 break;
5518 case dest_buffer:
5519 // include b: with the name, easier to execute that way
5520 generate_STORE(cctx, ISN_STOREB, 0, name);
5521 break;
5522 case dest_window:
5523 // include w: with the name, easier to execute that way
5524 generate_STORE(cctx, ISN_STOREW, 0, name);
5525 break;
5526 case dest_tab:
5527 // include t: with the name, easier to execute that way
5528 generate_STORE(cctx, ISN_STORET, 0, name);
5529 break;
5530 case dest_env:
5531 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5532 break;
5533 case dest_reg:
5534 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5535 break;
5536 case dest_vimvar:
5537 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5538 break;
5539 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005540 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005541 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5542 imported_T *import = NULL;
5543 int sid = current_sctx.sc_sid;
5544 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005545
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005546 if (name[1] != ':')
5547 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005548 import = find_imported(name, 0, cctx);
5549 if (import != NULL)
5550 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005551 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005552
5553 idx = get_script_item_idx(sid, rawname, TRUE);
5554 // TODO: specific type
5555 if (idx < 0)
5556 {
5557 char_u *name_s = name;
5558
5559 // Include s: in the name for store_var()
5560 if (name[1] != ':')
5561 {
5562 int len = (int)STRLEN(name) + 3;
5563
5564 name_s = alloc(len);
5565 if (name_s == NULL)
5566 name_s = name;
5567 else
5568 vim_snprintf((char *)name_s, len,
5569 "s:%s", name);
5570 }
5571 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005572 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005573 if (name_s != name)
5574 vim_free(name_s);
5575 }
5576 else
5577 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005578 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005579 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005580 break;
5581 case dest_local:
5582 if (lvar != NULL)
5583 {
5584 isn_T *isn = ((isn_T *)instr->ga_data)
5585 + instr->ga_len - 1;
5586
5587 // optimization: turn "var = 123" from ISN_PUSHNR +
5588 // ISN_STORE into ISN_STORENR
5589 if (!lvar->lv_from_outer
5590 && instr->ga_len == instr_count + 1
5591 && isn->isn_type == ISN_PUSHNR)
5592 {
5593 varnumber_T val = isn->isn_arg.number;
5594
5595 isn->isn_type = ISN_STORENR;
5596 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5597 isn->isn_arg.storenr.stnr_val = val;
5598 if (stack->ga_len > 0)
5599 --stack->ga_len;
5600 }
5601 else if (lvar->lv_from_outer)
5602 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005603 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005604 else
5605 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5606 }
5607 break;
5608 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005609 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005610
5611 if (var_idx + 1 < var_count)
5612 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005614
5615 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005616 if (var_count > 0 && !semicolon)
5617 {
5618 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5619 goto theend;
5620 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005621
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005622 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005623
5624theend:
5625 vim_free(name);
5626 return ret;
5627}
5628
5629/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005630 * Check if "name" can be "unlet".
5631 */
5632 int
5633check_vim9_unlet(char_u *name)
5634{
5635 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5636 {
5637 semsg(_("E1081: Cannot unlet %s"), name);
5638 return FAIL;
5639 }
5640 return OK;
5641}
5642
5643/*
5644 * Callback passed to ex_unletlock().
5645 */
5646 static int
5647compile_unlet(
5648 lval_T *lvp,
5649 char_u *name_end,
5650 exarg_T *eap,
5651 int deep UNUSED,
5652 void *coookie)
5653{
5654 cctx_T *cctx = coookie;
5655
5656 if (lvp->ll_tv == NULL)
5657 {
5658 char_u *p = lvp->ll_name;
5659 int cc = *name_end;
5660 int ret = OK;
5661
5662 // Normal name. Only supports g:, w:, t: and b: namespaces.
5663 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005664 if (*p == '$')
5665 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5666 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005667 ret = FAIL;
5668 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005669 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005670
5671 *name_end = cc;
5672 return ret;
5673 }
5674
5675 // TODO: unlet {list}[idx]
5676 // TODO: unlet {dict}[key]
5677 emsg("Sorry, :unlet not fully implemented yet");
5678 return FAIL;
5679}
5680
5681/*
5682 * compile "unlet var", "lock var" and "unlock var"
5683 * "arg" points to "var".
5684 */
5685 static char_u *
5686compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5687{
5688 char_u *p = arg;
5689
5690 if (eap->cmdidx != CMD_unlet)
5691 {
5692 emsg("Sorry, :lock and unlock not implemented yet");
5693 return NULL;
5694 }
5695
5696 if (*p == '!')
5697 {
5698 p = skipwhite(p + 1);
5699 eap->forceit = TRUE;
5700 }
5701
5702 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5703 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5704}
5705
5706/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005707 * Compile an :import command.
5708 */
5709 static char_u *
5710compile_import(char_u *arg, cctx_T *cctx)
5711{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005712 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005713}
5714
5715/*
5716 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5717 */
5718 static int
5719compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5720{
5721 garray_T *instr = &cctx->ctx_instr;
5722 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5723
5724 if (endlabel == NULL)
5725 return FAIL;
5726 endlabel->el_next = *el;
5727 *el = endlabel;
5728 endlabel->el_end_label = instr->ga_len;
5729
5730 generate_JUMP(cctx, when, 0);
5731 return OK;
5732}
5733
5734 static void
5735compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5736{
5737 garray_T *instr = &cctx->ctx_instr;
5738
5739 while (*el != NULL)
5740 {
5741 endlabel_T *cur = (*el);
5742 isn_T *isn;
5743
5744 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5745 isn->isn_arg.jump.jump_where = instr->ga_len;
5746 *el = cur->el_next;
5747 vim_free(cur);
5748 }
5749}
5750
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005751 static void
5752compile_free_jump_to_end(endlabel_T **el)
5753{
5754 while (*el != NULL)
5755 {
5756 endlabel_T *cur = (*el);
5757
5758 *el = cur->el_next;
5759 vim_free(cur);
5760 }
5761}
5762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763/*
5764 * Create a new scope and set up the generic items.
5765 */
5766 static scope_T *
5767new_scope(cctx_T *cctx, scopetype_T type)
5768{
5769 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5770
5771 if (scope == NULL)
5772 return NULL;
5773 scope->se_outer = cctx->ctx_scope;
5774 cctx->ctx_scope = scope;
5775 scope->se_type = type;
5776 scope->se_local_count = cctx->ctx_locals.ga_len;
5777 return scope;
5778}
5779
5780/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005781 * Free the current scope and go back to the outer scope.
5782 */
5783 static void
5784drop_scope(cctx_T *cctx)
5785{
5786 scope_T *scope = cctx->ctx_scope;
5787
5788 if (scope == NULL)
5789 {
5790 iemsg("calling drop_scope() without a scope");
5791 return;
5792 }
5793 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005794 switch (scope->se_type)
5795 {
5796 case IF_SCOPE:
5797 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5798 case FOR_SCOPE:
5799 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5800 case WHILE_SCOPE:
5801 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5802 case TRY_SCOPE:
5803 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5804 case NO_SCOPE:
5805 case BLOCK_SCOPE:
5806 break;
5807 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005808 vim_free(scope);
5809}
5810
5811/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005812 * compile "if expr"
5813 *
5814 * "if expr" Produces instructions:
5815 * EVAL expr Push result of "expr"
5816 * JUMP_IF_FALSE end
5817 * ... body ...
5818 * end:
5819 *
5820 * "if expr | else" Produces instructions:
5821 * EVAL expr Push result of "expr"
5822 * JUMP_IF_FALSE else
5823 * ... body ...
5824 * JUMP_ALWAYS end
5825 * else:
5826 * ... body ...
5827 * end:
5828 *
5829 * "if expr1 | elseif expr2 | else" Produces instructions:
5830 * EVAL expr Push result of "expr"
5831 * JUMP_IF_FALSE elseif
5832 * ... body ...
5833 * JUMP_ALWAYS end
5834 * elseif:
5835 * EVAL expr Push result of "expr"
5836 * JUMP_IF_FALSE else
5837 * ... body ...
5838 * JUMP_ALWAYS end
5839 * else:
5840 * ... body ...
5841 * end:
5842 */
5843 static char_u *
5844compile_if(char_u *arg, cctx_T *cctx)
5845{
5846 char_u *p = arg;
5847 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005848 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005849 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005850 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005851 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852
Bram Moolenaara5565e42020-05-09 15:44:01 +02005853 CLEAR_FIELD(ppconst);
5854 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005855 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005856 clear_ppconst(&ppconst);
5857 return NULL;
5858 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005859 if (cctx->ctx_skip == SKIP_YES)
5860 clear_ppconst(&ppconst);
5861 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005862 {
5863 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005864 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005865 clear_ppconst(&ppconst);
5866 }
5867 else
5868 {
5869 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005870 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005871 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005872 return NULL;
5873 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005874
5875 scope = new_scope(cctx, IF_SCOPE);
5876 if (scope == NULL)
5877 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005878 scope->se_skip_save = skip_save;
5879 // "is_had_return" will be reset if any block does not end in :return
5880 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005881
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005882 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005883 {
5884 // "where" is set when ":elseif", "else" or ":endif" is found
5885 scope->se_u.se_if.is_if_label = instr->ga_len;
5886 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5887 }
5888 else
5889 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890
5891 return p;
5892}
5893
5894 static char_u *
5895compile_elseif(char_u *arg, cctx_T *cctx)
5896{
5897 char_u *p = arg;
5898 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005899 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005900 isn_T *isn;
5901 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005902 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005903
5904 if (scope == NULL || scope->se_type != IF_SCOPE)
5905 {
5906 emsg(_(e_elseif_without_if));
5907 return NULL;
5908 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005909 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005910 if (!cctx->ctx_had_return)
5911 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005913 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005914 {
5915 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005917 return NULL;
5918 // previous "if" or "elseif" jumps here
5919 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5920 isn->isn_arg.jump.jump_where = instr->ga_len;
5921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005922
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005923 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005924 CLEAR_FIELD(ppconst);
5925 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005926 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005927 clear_ppconst(&ppconst);
5928 return NULL;
5929 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005930 if (scope->se_skip_save == SKIP_YES)
5931 clear_ppconst(&ppconst);
5932 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005933 {
5934 // The expression results in a constant.
5935 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005936 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005937 clear_ppconst(&ppconst);
5938 scope->se_u.se_if.is_if_label = -1;
5939 }
5940 else
5941 {
5942 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005943 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005944 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005945 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005946
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005947 // "where" is set when ":elseif", "else" or ":endif" is found
5948 scope->se_u.se_if.is_if_label = instr->ga_len;
5949 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5950 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005951
5952 return p;
5953}
5954
5955 static char_u *
5956compile_else(char_u *arg, cctx_T *cctx)
5957{
5958 char_u *p = arg;
5959 garray_T *instr = &cctx->ctx_instr;
5960 isn_T *isn;
5961 scope_T *scope = cctx->ctx_scope;
5962
5963 if (scope == NULL || scope->se_type != IF_SCOPE)
5964 {
5965 emsg(_(e_else_without_if));
5966 return NULL;
5967 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005968 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005969 if (!cctx->ctx_had_return)
5970 scope->se_u.se_if.is_had_return = FALSE;
5971 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005972
Bram Moolenaarefd88552020-06-18 20:50:10 +02005973 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005974 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005975 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005976 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005977 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005978 if (!cctx->ctx_had_return
5979 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5980 JUMP_ALWAYS, cctx) == FAIL)
5981 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005982 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005983
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005984 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005985 {
5986 if (scope->se_u.se_if.is_if_label >= 0)
5987 {
5988 // previous "if" or "elseif" jumps here
5989 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5990 isn->isn_arg.jump.jump_where = instr->ga_len;
5991 scope->se_u.se_if.is_if_label = -1;
5992 }
5993 }
5994
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005995 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005996 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5997 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998
5999 return p;
6000}
6001
6002 static char_u *
6003compile_endif(char_u *arg, cctx_T *cctx)
6004{
6005 scope_T *scope = cctx->ctx_scope;
6006 ifscope_T *ifscope;
6007 garray_T *instr = &cctx->ctx_instr;
6008 isn_T *isn;
6009
6010 if (scope == NULL || scope->se_type != IF_SCOPE)
6011 {
6012 emsg(_(e_endif_without_if));
6013 return NULL;
6014 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006015 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006016 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006017 if (!cctx->ctx_had_return)
6018 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006019
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006020 if (scope->se_u.se_if.is_if_label >= 0)
6021 {
6022 // previous "if" or "elseif" jumps here
6023 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6024 isn->isn_arg.jump.jump_where = instr->ga_len;
6025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026 // Fill in the "end" label in jumps at the end of the blocks.
6027 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006028 cctx->ctx_skip = scope->se_skip_save;
6029
6030 // If all the blocks end in :return and there is an :else then the
6031 // had_return flag is set.
6032 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006034 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035 return arg;
6036}
6037
6038/*
6039 * compile "for var in expr"
6040 *
6041 * Produces instructions:
6042 * PUSHNR -1
6043 * STORE loop-idx Set index to -1
6044 * EVAL expr Push result of "expr"
6045 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6046 * - if beyond end, jump to "end"
6047 * - otherwise get item from list and push it
6048 * STORE var Store item in "var"
6049 * ... body ...
6050 * JUMP top Jump back to repeat
6051 * end: DROP Drop the result of "expr"
6052 *
6053 */
6054 static char_u *
6055compile_for(char_u *arg, cctx_T *cctx)
6056{
6057 char_u *p;
6058 size_t varlen;
6059 garray_T *instr = &cctx->ctx_instr;
6060 garray_T *stack = &cctx->ctx_type_stack;
6061 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006062 lvar_T *loop_lvar; // loop iteration variable
6063 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064 type_T *vartype;
6065
6066 // TODO: list of variables: "for [key, value] in dict"
6067 // parse "var"
6068 for (p = arg; eval_isnamec1(*p); ++p)
6069 ;
6070 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006071 var_lvar = lookup_local(arg, varlen, cctx);
6072 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006073 {
6074 semsg(_("E1023: variable already defined: %s"), arg);
6075 return NULL;
6076 }
6077
6078 // consume "in"
6079 p = skipwhite(p);
6080 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6081 {
6082 emsg(_(e_missing_in));
6083 return NULL;
6084 }
6085 p = skipwhite(p + 2);
6086
6087
6088 scope = new_scope(cctx, FOR_SCOPE);
6089 if (scope == NULL)
6090 return NULL;
6091
6092 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006093 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6094 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006095 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006096 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006097 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006098 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006100
6101 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006102 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6103 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006104 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006105 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006106 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006110 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111
6112 // compile "expr", it remains on the stack until "endfor"
6113 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006114 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006115 {
6116 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006117 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006120 // Now that we know the type of "var", check that it is a list, now or at
6121 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006122 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006123 if (need_type(vartype, &t_list_any, -1, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006124 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006125 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006126 return NULL;
6127 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006128 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006129 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130
6131 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006132 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006133
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006134 generate_FOR(cctx, loop_lvar->lv_idx);
6135 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006136
6137 return arg;
6138}
6139
6140/*
6141 * compile "endfor"
6142 */
6143 static char_u *
6144compile_endfor(char_u *arg, cctx_T *cctx)
6145{
6146 garray_T *instr = &cctx->ctx_instr;
6147 scope_T *scope = cctx->ctx_scope;
6148 forscope_T *forscope;
6149 isn_T *isn;
6150
6151 if (scope == NULL || scope->se_type != FOR_SCOPE)
6152 {
6153 emsg(_(e_for));
6154 return NULL;
6155 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006156 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006157 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006158 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006159
6160 // At end of ":for" scope jump back to the FOR instruction.
6161 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6162
6163 // Fill in the "end" label in the FOR statement so it can jump here
6164 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6165 isn->isn_arg.forloop.for_end = instr->ga_len;
6166
6167 // Fill in the "end" label any BREAK statements
6168 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6169
6170 // Below the ":for" scope drop the "expr" list from the stack.
6171 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6172 return NULL;
6173
6174 vim_free(scope);
6175
6176 return arg;
6177}
6178
6179/*
6180 * compile "while expr"
6181 *
6182 * Produces instructions:
6183 * top: EVAL expr Push result of "expr"
6184 * JUMP_IF_FALSE end jump if false
6185 * ... body ...
6186 * JUMP top Jump back to repeat
6187 * end:
6188 *
6189 */
6190 static char_u *
6191compile_while(char_u *arg, cctx_T *cctx)
6192{
6193 char_u *p = arg;
6194 garray_T *instr = &cctx->ctx_instr;
6195 scope_T *scope;
6196
6197 scope = new_scope(cctx, WHILE_SCOPE);
6198 if (scope == NULL)
6199 return NULL;
6200
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006201 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006202
6203 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006204 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006205 return NULL;
6206
6207 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006208 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006209 JUMP_IF_FALSE, cctx) == FAIL)
6210 return FAIL;
6211
6212 return p;
6213}
6214
6215/*
6216 * compile "endwhile"
6217 */
6218 static char_u *
6219compile_endwhile(char_u *arg, cctx_T *cctx)
6220{
6221 scope_T *scope = cctx->ctx_scope;
6222
6223 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6224 {
6225 emsg(_(e_while));
6226 return NULL;
6227 }
6228 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006229 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230
6231 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006232 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233
6234 // Fill in the "end" label in the WHILE statement so it can jump here.
6235 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006236 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237
6238 vim_free(scope);
6239
6240 return arg;
6241}
6242
6243/*
6244 * compile "continue"
6245 */
6246 static char_u *
6247compile_continue(char_u *arg, cctx_T *cctx)
6248{
6249 scope_T *scope = cctx->ctx_scope;
6250
6251 for (;;)
6252 {
6253 if (scope == NULL)
6254 {
6255 emsg(_(e_continue));
6256 return NULL;
6257 }
6258 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6259 break;
6260 scope = scope->se_outer;
6261 }
6262
6263 // Jump back to the FOR or WHILE instruction.
6264 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006265 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6266 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 return arg;
6268}
6269
6270/*
6271 * compile "break"
6272 */
6273 static char_u *
6274compile_break(char_u *arg, cctx_T *cctx)
6275{
6276 scope_T *scope = cctx->ctx_scope;
6277 endlabel_T **el;
6278
6279 for (;;)
6280 {
6281 if (scope == NULL)
6282 {
6283 emsg(_(e_break));
6284 return NULL;
6285 }
6286 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6287 break;
6288 scope = scope->se_outer;
6289 }
6290
6291 // Jump to the end of the FOR or WHILE loop.
6292 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006293 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006295 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6297 return FAIL;
6298
6299 return arg;
6300}
6301
6302/*
6303 * compile "{" start of block
6304 */
6305 static char_u *
6306compile_block(char_u *arg, cctx_T *cctx)
6307{
6308 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6309 return NULL;
6310 return skipwhite(arg + 1);
6311}
6312
6313/*
6314 * compile end of block: drop one scope
6315 */
6316 static void
6317compile_endblock(cctx_T *cctx)
6318{
6319 scope_T *scope = cctx->ctx_scope;
6320
6321 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006322 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323 vim_free(scope);
6324}
6325
6326/*
6327 * compile "try"
6328 * Creates a new scope for the try-endtry, pointing to the first catch and
6329 * finally.
6330 * Creates another scope for the "try" block itself.
6331 * TRY instruction sets up exception handling at runtime.
6332 *
6333 * "try"
6334 * TRY -> catch1, -> finally push trystack entry
6335 * ... try block
6336 * "throw {exception}"
6337 * EVAL {exception}
6338 * THROW create exception
6339 * ... try block
6340 * " catch {expr}"
6341 * JUMP -> finally
6342 * catch1: PUSH exeception
6343 * EVAL {expr}
6344 * MATCH
6345 * JUMP nomatch -> catch2
6346 * CATCH remove exception
6347 * ... catch block
6348 * " catch"
6349 * JUMP -> finally
6350 * catch2: CATCH remove exception
6351 * ... catch block
6352 * " finally"
6353 * finally:
6354 * ... finally block
6355 * " endtry"
6356 * ENDTRY pop trystack entry, may rethrow
6357 */
6358 static char_u *
6359compile_try(char_u *arg, cctx_T *cctx)
6360{
6361 garray_T *instr = &cctx->ctx_instr;
6362 scope_T *try_scope;
6363 scope_T *scope;
6364
6365 // scope that holds the jumps that go to catch/finally/endtry
6366 try_scope = new_scope(cctx, TRY_SCOPE);
6367 if (try_scope == NULL)
6368 return NULL;
6369
6370 // "catch" is set when the first ":catch" is found.
6371 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006372 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006373 if (generate_instr(cctx, ISN_TRY) == NULL)
6374 return NULL;
6375
6376 // scope for the try block itself
6377 scope = new_scope(cctx, BLOCK_SCOPE);
6378 if (scope == NULL)
6379 return NULL;
6380
6381 return arg;
6382}
6383
6384/*
6385 * compile "catch {expr}"
6386 */
6387 static char_u *
6388compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6389{
6390 scope_T *scope = cctx->ctx_scope;
6391 garray_T *instr = &cctx->ctx_instr;
6392 char_u *p;
6393 isn_T *isn;
6394
6395 // end block scope from :try or :catch
6396 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6397 compile_endblock(cctx);
6398 scope = cctx->ctx_scope;
6399
6400 // Error if not in a :try scope
6401 if (scope == NULL || scope->se_type != TRY_SCOPE)
6402 {
6403 emsg(_(e_catch));
6404 return NULL;
6405 }
6406
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006407 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 {
6409 emsg(_("E1033: catch unreachable after catch-all"));
6410 return NULL;
6411 }
6412
6413 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006414 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415 JUMP_ALWAYS, cctx) == FAIL)
6416 return NULL;
6417
6418 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006419 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006420 if (isn->isn_arg.try.try_catch == 0)
6421 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006422 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423 {
6424 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006425 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006426 isn->isn_arg.jump.jump_where = instr->ga_len;
6427 }
6428
6429 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006430 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006432 scope->se_u.se_try.ts_caught_all = TRUE;
6433 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434 }
6435 else
6436 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006437 char_u *end;
6438 char_u *pat;
6439 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006440 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006441 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006443 // Push v:exception, push {expr} and MATCH
6444 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6445
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006446 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006447 if (*end != *p)
6448 {
6449 semsg(_("E1067: Separator mismatch: %s"), p);
6450 vim_free(tofree);
6451 return FAIL;
6452 }
6453 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006454 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006455 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006456 len = (int)(end - tofree);
6457 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006458 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006459 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006460 if (pat == NULL)
6461 return FAIL;
6462 if (generate_PUSHS(cctx, pat) == FAIL)
6463 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006465 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6466 return NULL;
6467
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006468 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006469 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6470 return NULL;
6471 }
6472
6473 if (generate_instr(cctx, ISN_CATCH) == NULL)
6474 return NULL;
6475
6476 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6477 return NULL;
6478 return p;
6479}
6480
6481 static char_u *
6482compile_finally(char_u *arg, cctx_T *cctx)
6483{
6484 scope_T *scope = cctx->ctx_scope;
6485 garray_T *instr = &cctx->ctx_instr;
6486 isn_T *isn;
6487
6488 // end block scope from :try or :catch
6489 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6490 compile_endblock(cctx);
6491 scope = cctx->ctx_scope;
6492
6493 // Error if not in a :try scope
6494 if (scope == NULL || scope->se_type != TRY_SCOPE)
6495 {
6496 emsg(_(e_finally));
6497 return NULL;
6498 }
6499
6500 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006501 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006502 if (isn->isn_arg.try.try_finally != 0)
6503 {
6504 emsg(_(e_finally_dup));
6505 return NULL;
6506 }
6507
6508 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006509 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006510
Bram Moolenaar585fea72020-04-02 22:33:21 +02006511 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006512 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 {
6514 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006515 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516 isn->isn_arg.jump.jump_where = instr->ga_len;
6517 }
6518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006519 // TODO: set index in ts_finally_label jumps
6520
6521 return arg;
6522}
6523
6524 static char_u *
6525compile_endtry(char_u *arg, cctx_T *cctx)
6526{
6527 scope_T *scope = cctx->ctx_scope;
6528 garray_T *instr = &cctx->ctx_instr;
6529 isn_T *isn;
6530
6531 // end block scope from :catch or :finally
6532 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6533 compile_endblock(cctx);
6534 scope = cctx->ctx_scope;
6535
6536 // Error if not in a :try scope
6537 if (scope == NULL || scope->se_type != TRY_SCOPE)
6538 {
6539 if (scope == NULL)
6540 emsg(_(e_no_endtry));
6541 else if (scope->se_type == WHILE_SCOPE)
6542 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006543 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 emsg(_(e_endfor));
6545 else
6546 emsg(_(e_endif));
6547 return NULL;
6548 }
6549
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006550 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6552 {
6553 emsg(_("E1032: missing :catch or :finally"));
6554 return NULL;
6555 }
6556
6557 // Fill in the "end" label in jumps at the end of the blocks, if not done
6558 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006559 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560
6561 // End :catch or :finally scope: set value in ISN_TRY instruction
6562 if (isn->isn_arg.try.try_finally == 0)
6563 isn->isn_arg.try.try_finally = instr->ga_len;
6564 compile_endblock(cctx);
6565
6566 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6567 return NULL;
6568 return arg;
6569}
6570
6571/*
6572 * compile "throw {expr}"
6573 */
6574 static char_u *
6575compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6576{
6577 char_u *p = skipwhite(arg);
6578
Bram Moolenaara5565e42020-05-09 15:44:01 +02006579 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 return NULL;
6581 if (may_generate_2STRING(-1, cctx) == FAIL)
6582 return NULL;
6583 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6584 return NULL;
6585
6586 return p;
6587}
6588
6589/*
6590 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006591 * compile "echomsg expr"
6592 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006593 * compile "execute expr"
6594 */
6595 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006596compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006597{
6598 char_u *p = arg;
6599 int count = 0;
6600
6601 for (;;)
6602 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006603 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006604 return NULL;
6605 ++count;
6606 p = skipwhite(p);
6607 if (ends_excmd(*p))
6608 break;
6609 }
6610
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006611 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6612 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6613 else if (cmdidx == CMD_execute)
6614 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6615 else if (cmdidx == CMD_echomsg)
6616 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6617 else
6618 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619 return p;
6620}
6621
6622/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006623 * A command that is not compiled, execute with legacy code.
6624 */
6625 static char_u *
6626compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6627{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006628 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006629 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006630 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006631
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006632 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006633 goto theend;
6634
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006635 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006636 {
6637 long argt = excmd_get_argt(eap->cmdidx);
6638 int usefilter = FALSE;
6639
6640 has_expr = argt & (EX_XFILE | EX_EXPAND);
6641
6642 // If the command can be followed by a bar, find the bar and truncate
6643 // it, so that the following command can be compiled.
6644 // The '|' is overwritten with a NUL, it is put back below.
6645 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6646 && *eap->arg == '!')
6647 // :w !filter or :r !filter or :r! filter
6648 usefilter = TRUE;
6649 if ((argt & EX_TRLBAR) && !usefilter)
6650 {
6651 separate_nextcmd(eap);
6652 if (eap->nextcmd != NULL)
6653 nextcmd = eap->nextcmd;
6654 }
6655 }
6656
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006657 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6658 {
6659 // expand filename in "syntax include [@group] filename"
6660 has_expr = TRUE;
6661 eap->arg = skipwhite(eap->arg + 7);
6662 if (*eap->arg == '@')
6663 eap->arg = skiptowhite(eap->arg);
6664 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006665
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006666 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006667 {
6668 int count = 0;
6669 char_u *start = skipwhite(line);
6670
6671 // :cmd xxx`=expr1`yyy`=expr2`zzz
6672 // PUSHS ":cmd xxx"
6673 // eval expr1
6674 // PUSHS "yyy"
6675 // eval expr2
6676 // PUSHS "zzz"
6677 // EXECCONCAT 5
6678 for (;;)
6679 {
6680 if (p > start)
6681 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006682 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006683 ++count;
6684 }
6685 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006686 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006687 return NULL;
6688 may_generate_2STRING(-1, cctx);
6689 ++count;
6690 p = skipwhite(p);
6691 if (*p != '`')
6692 {
6693 emsg(_("E1083: missing backtick"));
6694 return NULL;
6695 }
6696 start = p + 1;
6697
6698 p = (char_u *)strstr((char *)start, "`=");
6699 if (p == NULL)
6700 {
6701 if (*skipwhite(start) != NUL)
6702 {
6703 generate_PUSHS(cctx, vim_strsave(start));
6704 ++count;
6705 }
6706 break;
6707 }
6708 }
6709 generate_EXECCONCAT(cctx, count);
6710 }
6711 else
6712 generate_EXEC(cctx, line);
6713
6714theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006715 if (*nextcmd != NUL)
6716 {
6717 // the parser expects a pointer to the bar, put it back
6718 --nextcmd;
6719 *nextcmd = '|';
6720 }
6721
6722 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006723}
6724
6725/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006726 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006727 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006728 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006729 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006730add_def_function(ufunc_T *ufunc)
6731{
6732 dfunc_T *dfunc;
6733
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006734 if (def_functions.ga_len == 0)
6735 {
6736 // The first position is not used, so that a zero uf_dfunc_idx means it
6737 // wasn't set.
6738 if (ga_grow(&def_functions, 1) == FAIL)
6739 return FAIL;
6740 ++def_functions.ga_len;
6741 }
6742
Bram Moolenaar09689a02020-05-09 22:50:08 +02006743 // Add the function to "def_functions".
6744 if (ga_grow(&def_functions, 1) == FAIL)
6745 return FAIL;
6746 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6747 CLEAR_POINTER(dfunc);
6748 dfunc->df_idx = def_functions.ga_len;
6749 ufunc->uf_dfunc_idx = dfunc->df_idx;
6750 dfunc->df_ufunc = ufunc;
6751 ++def_functions.ga_len;
6752 return OK;
6753}
6754
6755/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756 * After ex_function() has collected all the function lines: parse and compile
6757 * the lines into instructions.
6758 * Adds the function to "def_functions".
6759 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6760 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006761 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006762 * This can be used recursively through compile_lambda(), which may reallocate
6763 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006764 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006765 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006766 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006767compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006768{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769 char_u *line = NULL;
6770 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006771 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 cctx_T cctx;
6773 garray_T *instr;
6774 int called_emsg_before = called_emsg;
6775 int ret = FAIL;
6776 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006777 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006778 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006780 // When using a function that was compiled before: Free old instructions.
6781 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006782 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006783 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006784 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6785 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006786 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006787 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006788 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006789 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790
Bram Moolenaara80faa82020-04-12 19:37:17 +02006791 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792 cctx.ctx_ufunc = ufunc;
6793 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006794 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6796 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6797 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6798 cctx.ctx_type_list = &ufunc->uf_type_list;
6799 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6800 instr = &cctx.ctx_instr;
6801
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006802 // Set the context to the function, it may be compiled when called from
6803 // another script. Set the script version to the most modern one.
6804 // The line number will be set in next_line_from_context().
6805 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6807
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006808 // Make sure error messages are OK.
6809 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6810 if (do_estack_push)
6811 estack_push_ufunc(ufunc, 1);
6812
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006813 if (ufunc->uf_def_args.ga_len > 0)
6814 {
6815 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006816 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006817 int i;
6818 char_u *arg;
6819 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6820
6821 // Produce instructions for the default values of optional arguments.
6822 // Store the instruction index in uf_def_arg_idx[] so that we know
6823 // where to start when the function is called, depending on the number
6824 // of arguments.
6825 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6826 if (ufunc->uf_def_arg_idx == NULL)
6827 goto erret;
6828 for (i = 0; i < count; ++i)
6829 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006830 garray_T *stack = &cctx.ctx_type_stack;
6831 type_T *val_type;
6832 int arg_idx = first_def_arg + i;
6833
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006834 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6835 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006836 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006837 goto erret;
6838
6839 // If no type specified use the type of the default value.
6840 // Otherwise check that the default value type matches the
6841 // specified type.
6842 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6843 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6844 ufunc->uf_arg_types[arg_idx] = val_type;
6845 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6846 == FAIL)
6847 {
6848 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6849 arg_idx + 1);
6850 goto erret;
6851 }
6852
6853 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006854 goto erret;
6855 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006856 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6857 }
6858
6859 /*
6860 * Loop over all the lines of the function and generate instructions.
6861 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006862 for (;;)
6863 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006864 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006865 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006866 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006867 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006868
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006869 // Bail out on the first error to avoid a flood of errors and report
6870 // the right line number when inside try/catch.
6871 if (emsg_before != called_emsg)
6872 goto erret;
6873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874 if (line != NULL && *line == '|')
6875 // the line continues after a '|'
6876 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006877 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006878 && !(*line == '#' && (line == cctx.ctx_line_start
6879 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006881 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 goto erret;
6883 }
6884 else
6885 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006886 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006887 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006888 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006891 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892
Bram Moolenaara80faa82020-04-12 19:37:17 +02006893 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006894 ea.cmdlinep = &line;
6895 ea.cmd = skipwhite(line);
6896
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006897 // Some things can be recognized by the first character.
6898 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006899 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006900 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006901 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006902 if (ea.cmd[1] != '{')
6903 {
6904 line = (char_u *)"";
6905 continue;
6906 }
6907 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006909 case '}':
6910 {
6911 // "}" ends a block scope
6912 scopetype_T stype = cctx.ctx_scope == NULL
6913 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006915 if (stype == BLOCK_SCOPE)
6916 {
6917 compile_endblock(&cctx);
6918 line = ea.cmd;
6919 }
6920 else
6921 {
6922 emsg(_("E1025: using } outside of a block scope"));
6923 goto erret;
6924 }
6925 if (line != NULL)
6926 line = skipwhite(ea.cmd + 1);
6927 continue;
6928 }
6929
6930 case '{':
6931 // "{" starts a block scope
6932 // "{'a': 1}->func() is something else
6933 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6934 {
6935 line = compile_block(ea.cmd, &cctx);
6936 continue;
6937 }
6938 break;
6939
6940 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006941 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006942 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943 }
6944
6945 /*
6946 * COMMAND MODIFIERS
6947 */
6948 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6949 {
6950 if (errormsg != NULL)
6951 goto erret;
6952 // empty line or comment
6953 line = (char_u *)"";
6954 continue;
6955 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006956 // TODO: use modifiers in the command
6957 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006958
6959 // Skip ":call" to get to the function name.
6960 if (checkforcmd(&ea.cmd, "call", 3))
6961 ea.cmd = skipwhite(ea.cmd);
6962
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006963 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006964 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006965 char_u *pskip;
6966
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006967 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006968 // find what follows.
6969 // Skip over "var.member", "var[idx]" and the like.
6970 // Also "&opt = val", "$ENV = val" and "@r = val".
6971 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006972 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006973 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006974 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006976 char_u *var_end;
6977 int oplen;
6978 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006980 var_end = find_name_end(pskip, NULL, NULL,
6981 FNE_CHECK_START | FNE_INCL_BR);
6982 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006983 if (oplen > 0)
6984 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006985 size_t len = p - ea.cmd;
6986
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006987 // Recognize an assignment if we recognize the variable
6988 // name:
6989 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006990 // "local = expr" where "local" is a local var.
6991 // "script = expr" where "script" is a script-local var.
6992 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006993 // "&opt = expr"
6994 // "$ENV = expr"
6995 // "@r = expr"
6996 if (*ea.cmd == '&'
6997 || *ea.cmd == '$'
6998 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006999 || ((len) > 2 && ea.cmd[1] == ':')
7000 || lookup_local(ea.cmd, len, &cctx) != NULL
7001 || lookup_arg(ea.cmd, len, NULL, NULL,
7002 NULL, &cctx) == OK
7003 || lookup_script(ea.cmd, len) == OK
7004 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007005 {
7006 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007007 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007008 goto erret;
7009 continue;
7010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 }
7012 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007013
7014 if (*ea.cmd == '[')
7015 {
7016 // [var, var] = expr
7017 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7018 if (line == NULL)
7019 goto erret;
7020 if (line != ea.cmd)
7021 continue;
7022 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 }
7024
7025 /*
7026 * COMMAND after range
7027 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007028 cmd = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007030 if (ea.cmd > cmd && !starts_with_colon)
7031 {
7032 emsg(_(e_colon_required));
7033 goto erret;
7034 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007035 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007036 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007037 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038
7039 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7040 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007041 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007042 {
7043 line += STRLEN(line);
7044 continue;
7045 }
7046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007048 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007050 // CMD_let cannot happen, compile_assignment() above is used
7051 iemsg("Command from find_ex_command() not handled");
7052 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 }
7055
7056 p = skipwhite(p);
7057
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007058 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007059 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007060 && ea.cmdidx != CMD_elseif
7061 && ea.cmdidx != CMD_else
7062 && ea.cmdidx != CMD_endif)
7063 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007064 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007065 continue;
7066 }
7067
Bram Moolenaarefd88552020-06-18 20:50:10 +02007068 if (ea.cmdidx != CMD_elseif
7069 && ea.cmdidx != CMD_else
7070 && ea.cmdidx != CMD_endif
7071 && ea.cmdidx != CMD_endfor
7072 && ea.cmdidx != CMD_endwhile
7073 && ea.cmdidx != CMD_catch
7074 && ea.cmdidx != CMD_finally
7075 && ea.cmdidx != CMD_endtry)
7076 {
7077 if (cctx.ctx_had_return)
7078 {
7079 emsg(_("E1095: Unreachable code after :return"));
7080 goto erret;
7081 }
7082 }
7083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 switch (ea.cmdidx)
7085 {
7086 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007087 ea.arg = p;
7088 line = compile_nested_function(&ea, &cctx);
7089 break;
7090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007091 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007092 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093 goto erret;
7094
7095 case CMD_return:
7096 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007097 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007098 break;
7099
7100 case CMD_let:
7101 case CMD_const:
7102 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007103 if (line == p)
7104 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105 break;
7106
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007107 case CMD_unlet:
7108 case CMD_unlockvar:
7109 case CMD_lockvar:
7110 line = compile_unletlock(p, &ea, &cctx);
7111 break;
7112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 case CMD_import:
7114 line = compile_import(p, &cctx);
7115 break;
7116
7117 case CMD_if:
7118 line = compile_if(p, &cctx);
7119 break;
7120 case CMD_elseif:
7121 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007122 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123 break;
7124 case CMD_else:
7125 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007126 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127 break;
7128 case CMD_endif:
7129 line = compile_endif(p, &cctx);
7130 break;
7131
7132 case CMD_while:
7133 line = compile_while(p, &cctx);
7134 break;
7135 case CMD_endwhile:
7136 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007137 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 break;
7139
7140 case CMD_for:
7141 line = compile_for(p, &cctx);
7142 break;
7143 case CMD_endfor:
7144 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007145 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146 break;
7147 case CMD_continue:
7148 line = compile_continue(p, &cctx);
7149 break;
7150 case CMD_break:
7151 line = compile_break(p, &cctx);
7152 break;
7153
7154 case CMD_try:
7155 line = compile_try(p, &cctx);
7156 break;
7157 case CMD_catch:
7158 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007159 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007160 break;
7161 case CMD_finally:
7162 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007163 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 break;
7165 case CMD_endtry:
7166 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007167 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 break;
7169 case CMD_throw:
7170 line = compile_throw(p, &cctx);
7171 break;
7172
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007173 case CMD_eval:
7174 if (compile_expr0(&p, &cctx) == FAIL)
7175 goto erret;
7176
7177 // drop the return value
7178 generate_instr_drop(&cctx, ISN_DROP, 1);
7179
7180 line = skipwhite(p);
7181 break;
7182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007185 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007186 case CMD_echomsg:
7187 case CMD_echoerr:
7188 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007189 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007191 // TODO: other commands with an expression argument
7192
Bram Moolenaar002262f2020-07-08 17:47:57 +02007193 case CMD_SIZE:
7194 semsg(_("E476: Invalid command: %s"), ea.cmd);
7195 goto erret;
7196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007197 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007198 // Not recognized, execute with do_cmdline_cmd().
7199 ea.arg = p;
7200 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007201 break;
7202 }
7203 if (line == NULL)
7204 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007205 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007206
7207 if (cctx.ctx_type_stack.ga_len < 0)
7208 {
7209 iemsg("Type stack underflow");
7210 goto erret;
7211 }
7212 }
7213
7214 if (cctx.ctx_scope != NULL)
7215 {
7216 if (cctx.ctx_scope->se_type == IF_SCOPE)
7217 emsg(_(e_endif));
7218 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7219 emsg(_(e_endwhile));
7220 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7221 emsg(_(e_endfor));
7222 else
7223 emsg(_("E1026: Missing }"));
7224 goto erret;
7225 }
7226
Bram Moolenaarefd88552020-06-18 20:50:10 +02007227 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228 {
7229 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7230 {
7231 emsg(_("E1027: Missing return statement"));
7232 goto erret;
7233 }
7234
7235 // Return zero if there is no return at the end.
7236 generate_PUSHNR(&cctx, 0);
7237 generate_instr(&cctx, ISN_RETURN);
7238 }
7239
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007240 {
7241 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7242 + ufunc->uf_dfunc_idx;
7243 dfunc->df_deleted = FALSE;
7244 dfunc->df_instr = instr->ga_data;
7245 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007246 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007247 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007248 if (cctx.ctx_outer_used)
7249 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007250 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007252
7253 ret = OK;
7254
7255erret:
7256 if (ret == FAIL)
7257 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007258 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007259 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7260 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007261
7262 for (idx = 0; idx < instr->ga_len; ++idx)
7263 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007264 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007265
Bram Moolenaar45a15082020-05-25 00:28:33 +02007266 // if using the last entry in the table we might as well remove it
7267 if (!dfunc->df_deleted
7268 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007269 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007270 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007271
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007272 while (cctx.ctx_scope != NULL)
7273 drop_scope(&cctx);
7274
Bram Moolenaar20431c92020-03-20 18:39:46 +01007275 // Don't execute this function body.
7276 ga_clear_strings(&ufunc->uf_lines);
7277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007278 if (errormsg != NULL)
7279 emsg(errormsg);
7280 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007281 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007282 }
7283
7284 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007285 if (do_estack_push)
7286 estack_pop();
7287
Bram Moolenaar20431c92020-03-20 18:39:46 +01007288 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007289 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007291 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007292}
7293
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007294 void
7295set_function_type(ufunc_T *ufunc)
7296{
7297 int varargs = ufunc->uf_va_name != NULL;
7298 int argcount = ufunc->uf_args.ga_len;
7299
7300 // Create a type for the function, with the return type and any
7301 // argument types.
7302 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7303 // The type is included in "tt_args".
7304 if (argcount > 0 || varargs)
7305 {
7306 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7307 argcount, &ufunc->uf_type_list);
7308 // Add argument types to the function type.
7309 if (func_type_add_arg_types(ufunc->uf_func_type,
7310 argcount + varargs,
7311 &ufunc->uf_type_list) == FAIL)
7312 return;
7313 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7314 ufunc->uf_func_type->tt_min_argcount =
7315 argcount - ufunc->uf_def_args.ga_len;
7316 if (ufunc->uf_arg_types == NULL)
7317 {
7318 int i;
7319
7320 // lambda does not have argument types.
7321 for (i = 0; i < argcount; ++i)
7322 ufunc->uf_func_type->tt_args[i] = &t_any;
7323 }
7324 else
7325 mch_memmove(ufunc->uf_func_type->tt_args,
7326 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7327 if (varargs)
7328 {
7329 ufunc->uf_func_type->tt_args[argcount] =
7330 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7331 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7332 }
7333 }
7334 else
7335 // No arguments, can use a predefined type.
7336 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7337 argcount, &ufunc->uf_type_list);
7338}
7339
7340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341/*
7342 * Delete an instruction, free what it contains.
7343 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007344 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345delete_instr(isn_T *isn)
7346{
7347 switch (isn->isn_type)
7348 {
7349 case ISN_EXEC:
7350 case ISN_LOADENV:
7351 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007352 case ISN_LOADB:
7353 case ISN_LOADW:
7354 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007356 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 case ISN_PUSHEXC:
7358 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007359 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007361 case ISN_STOREB:
7362 case ISN_STOREW:
7363 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007364 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 vim_free(isn->isn_arg.string);
7366 break;
7367
7368 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007369 case ISN_STORES:
7370 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007371 break;
7372
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007373 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007374 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007375 vim_free(isn->isn_arg.unlet.ul_name);
7376 break;
7377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378 case ISN_STOREOPT:
7379 vim_free(isn->isn_arg.storeopt.so_name);
7380 break;
7381
7382 case ISN_PUSHBLOB: // push blob isn_arg.blob
7383 blob_unref(isn->isn_arg.blob);
7384 break;
7385
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007386 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007387#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007388 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007389#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007390 break;
7391
7392 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007393#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007394 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007395#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007396 break;
7397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398 case ISN_UCALL:
7399 vim_free(isn->isn_arg.ufunc.cuf_name);
7400 break;
7401
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007402 case ISN_FUNCREF:
7403 {
7404 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7405 + isn->isn_arg.funcref.fr_func;
7406 func_ptr_unref(dfunc->df_ufunc);
7407 }
7408 break;
7409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 case ISN_2BOOL:
7411 case ISN_2STRING:
7412 case ISN_ADDBLOB:
7413 case ISN_ADDLIST:
7414 case ISN_BCALL:
7415 case ISN_CATCH:
7416 case ISN_CHECKNR:
7417 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007418 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419 case ISN_COMPAREANY:
7420 case ISN_COMPAREBLOB:
7421 case ISN_COMPAREBOOL:
7422 case ISN_COMPAREDICT:
7423 case ISN_COMPAREFLOAT:
7424 case ISN_COMPAREFUNC:
7425 case ISN_COMPARELIST:
7426 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 case ISN_COMPARESPECIAL:
7428 case ISN_COMPARESTRING:
7429 case ISN_CONCAT:
7430 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007431 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 case ISN_DROP:
7433 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007434 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007435 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007436 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007437 case ISN_EXECCONCAT:
7438 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007440 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007441 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007442 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007443 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 case ISN_JUMP:
7445 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007446 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447 case ISN_LOADSCRIPT:
7448 case ISN_LOADREG:
7449 case ISN_LOADV:
7450 case ISN_NEGATENR:
7451 case ISN_NEWDICT:
7452 case ISN_NEWLIST:
7453 case ISN_OPNR:
7454 case ISN_OPFLOAT:
7455 case ISN_OPANY:
7456 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007457 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458 case ISN_PUSHF:
7459 case ISN_PUSHNR:
7460 case ISN_PUSHBOOL:
7461 case ISN_PUSHSPEC:
7462 case ISN_RETURN:
7463 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007464 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007465 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007467 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007469 case ISN_STOREDICT:
7470 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471 case ISN_THROW:
7472 case ISN_TRY:
7473 // nothing allocated
7474 break;
7475 }
7476}
7477
7478/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007479 * Free all instructions for "dfunc".
7480 */
7481 static void
7482delete_def_function_contents(dfunc_T *dfunc)
7483{
7484 int idx;
7485
7486 ga_clear(&dfunc->df_def_args_isn);
7487
7488 if (dfunc->df_instr != NULL)
7489 {
7490 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7491 delete_instr(dfunc->df_instr + idx);
7492 VIM_CLEAR(dfunc->df_instr);
7493 }
7494
7495 dfunc->df_deleted = TRUE;
7496}
7497
7498/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007499 * When a user function is deleted, clear the contents of any associated def
7500 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007501 */
7502 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007503clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007504{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007505 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506 {
7507 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7508 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509
Bram Moolenaar20431c92020-03-20 18:39:46 +01007510 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007511 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007512 }
7513}
7514
7515#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007516/*
7517 * Free all functions defined with ":def".
7518 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007519 void
7520free_def_functions(void)
7521{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007522 int idx;
7523
7524 for (idx = 0; idx < def_functions.ga_len; ++idx)
7525 {
7526 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7527
7528 delete_def_function_contents(dfunc);
7529 }
7530
7531 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532}
7533#endif
7534
7535
7536#endif // FEAT_EVAL