blob: 93ab3c8c05b128c935a9d7546e7cc06ce255585e [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 Moolenaara5565e42020-05-09 15:44:01 +02002504// Structure passed between the compile_expr* functions to keep track of
2505// constants that have been parsed but for which no code was produced yet. If
2506// possible expressions on these constants are applied at compile time. If
2507// that is not possible, the code to push the constants needs to be generated
2508// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002509// Using 50 should be more than enough of 5 levels of ().
2510#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002511typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002512 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002513 int pp_used; // active entries in pp_tv[]
2514} ppconst_T;
2515
Bram Moolenaar1c747212020-05-09 18:28:34 +02002516static int compile_expr0(char_u **arg, cctx_T *cctx);
2517static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2518
Bram Moolenaara5565e42020-05-09 15:44:01 +02002519/*
2520 * Generate a PUSH instruction for "tv".
2521 * "tv" will be consumed or cleared.
2522 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2523 */
2524 static int
2525generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2526{
2527 if (tv != NULL)
2528 {
2529 switch (tv->v_type)
2530 {
2531 case VAR_UNKNOWN:
2532 break;
2533 case VAR_BOOL:
2534 generate_PUSHBOOL(cctx, tv->vval.v_number);
2535 break;
2536 case VAR_SPECIAL:
2537 generate_PUSHSPEC(cctx, tv->vval.v_number);
2538 break;
2539 case VAR_NUMBER:
2540 generate_PUSHNR(cctx, tv->vval.v_number);
2541 break;
2542#ifdef FEAT_FLOAT
2543 case VAR_FLOAT:
2544 generate_PUSHF(cctx, tv->vval.v_float);
2545 break;
2546#endif
2547 case VAR_BLOB:
2548 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2549 tv->vval.v_blob = NULL;
2550 break;
2551 case VAR_STRING:
2552 generate_PUSHS(cctx, tv->vval.v_string);
2553 tv->vval.v_string = NULL;
2554 break;
2555 default:
2556 iemsg("constant type not supported");
2557 clear_tv(tv);
2558 return FAIL;
2559 }
2560 tv->v_type = VAR_UNKNOWN;
2561 }
2562 return OK;
2563}
2564
2565/*
2566 * Generate code for any ppconst entries.
2567 */
2568 static int
2569generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2570{
2571 int i;
2572 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002573 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002574
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002575 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002576 for (i = 0; i < ppconst->pp_used; ++i)
2577 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2578 ret = FAIL;
2579 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002580 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002581 return ret;
2582}
2583
2584/*
2585 * Clear ppconst constants. Used when failing.
2586 */
2587 static void
2588clear_ppconst(ppconst_T *ppconst)
2589{
2590 int i;
2591
2592 for (i = 0; i < ppconst->pp_used; ++i)
2593 clear_tv(&ppconst->pp_tv[i]);
2594 ppconst->pp_used = 0;
2595}
2596
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002597/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002598 * Generate an instruction to load script-local variable "name", without the
2599 * leading "s:".
2600 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 */
2602 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002603compile_load_scriptvar(
2604 cctx_T *cctx,
2605 char_u *name, // variable NUL terminated
2606 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002607 char_u **end, // end of variable
2608 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002610 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2612 imported_T *import;
2613
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002614 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002616 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002617 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2618 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 }
2620 if (idx >= 0)
2621 {
2622 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2623
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002624 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 current_sctx.sc_sid, idx, sv->sv_type);
2626 return OK;
2627 }
2628
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002629 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 if (import != NULL)
2631 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002632 if (import->imp_all)
2633 {
2634 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002635 char_u *exp_name;
2636 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002637 ufunc_T *ufunc;
2638 type_T *type;
2639
2640 // Used "import * as Name", need to lookup the member.
2641 if (*p != '.')
2642 {
2643 semsg(_("E1060: expected dot after name: %s"), start);
2644 return FAIL;
2645 }
2646 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002647 if (VIM_ISWHITE(*p))
2648 {
2649 emsg(_("E1074: no white space allowed after dot"));
2650 return FAIL;
2651 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002652
Bram Moolenaar1c991142020-07-04 13:15:31 +02002653 // isolate one name
2654 exp_name = p;
2655 while (eval_isnamec(*p))
2656 ++p;
2657 cc = *p;
2658 *p = NUL;
2659
2660 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2661 *p = cc;
2662 p = skipwhite(p);
2663
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002664 // TODO: what if it is a function?
2665 if (idx < 0)
2666 return FAIL;
2667 *end = p;
2668
2669 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2670 import->imp_sid,
2671 idx,
2672 type);
2673 }
2674 else
2675 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002676 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002677 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2678 import->imp_sid,
2679 import->imp_var_vals_idx,
2680 import->imp_type);
2681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 return OK;
2683 }
2684
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002685 if (error)
2686 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 return FAIL;
2688}
2689
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002690 static int
2691generate_funcref(cctx_T *cctx, char_u *name)
2692{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002693 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002694
2695 if (ufunc == NULL)
2696 return FAIL;
2697
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002698 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2699 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002700}
2701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702/*
2703 * Compile a variable name into a load instruction.
2704 * "end" points to just after the name.
2705 * When "error" is FALSE do not give an error when not found.
2706 */
2707 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002708compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709{
2710 type_T *type;
2711 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002712 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002714 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715
2716 if (*(*arg + 1) == ':')
2717 {
2718 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002719 if (end <= *arg + 2)
2720 name = vim_strsave((char_u *)"[empty]");
2721 else
2722 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 if (name == NULL)
2724 return FAIL;
2725
2726 if (**arg == 'v')
2727 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002728 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 }
2730 else if (**arg == 'g')
2731 {
2732 // Global variables can be defined later, thus we don't check if it
2733 // exists, give error at runtime.
2734 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2735 }
2736 else if (**arg == 's')
2737 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002738 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002740 else if (**arg == 'b')
2741 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002742 // Buffer-local variables can be defined later, thus we don't check
2743 // if it exists, give error at runtime.
2744 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002745 }
2746 else if (**arg == 'w')
2747 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002748 // Window-local variables can be defined later, thus we don't check
2749 // if it exists, give error at runtime.
2750 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002751 }
2752 else if (**arg == 't')
2753 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002754 // Tabpage-local variables can be defined later, thus we don't
2755 // check if it exists, give error at runtime.
2756 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 else
2759 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002760 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 goto theend;
2762 }
2763 }
2764 else
2765 {
2766 size_t len = end - *arg;
2767 int idx;
2768 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002769 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770
2771 name = vim_strnsave(*arg, end - *arg);
2772 if (name == NULL)
2773 return FAIL;
2774
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002775 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002777 if (!gen_load_outer)
2778 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779 }
2780 else
2781 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002782 lvar_T *lvar = lookup_local(*arg, len, cctx);
2783
2784 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002786 type = lvar->lv_type;
2787 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002788 if (lvar->lv_from_outer)
2789 gen_load_outer = TRUE;
2790 else
2791 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 }
2793 else
2794 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002795 // "var" can be script-local even without using "s:" if it
2796 // already exists.
2797 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2798 == SCRIPT_VERSION_VIM9
2799 || lookup_script(*arg, len) == OK)
2800 res = compile_load_scriptvar(cctx, name, *arg, &end,
2801 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002802
Bram Moolenaara5565e42020-05-09 15:44:01 +02002803 // When the name starts with an uppercase letter or "x:" it
2804 // can be a user defined function.
2805 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2806 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 }
2808 }
2809 if (gen_load)
2810 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002811 if (gen_load_outer)
2812 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 }
2814
2815 *arg = end;
2816
2817theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002818 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 semsg(_(e_var_notfound), name);
2820 vim_free(name);
2821 return res;
2822}
2823
2824/*
2825 * Compile the argument expressions.
2826 * "arg" points to just after the "(" and is advanced to after the ")"
2827 */
2828 static int
2829compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2830{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002831 char_u *p = *arg;
2832 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833
Bram Moolenaare6085c52020-04-12 20:19:16 +02002834 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002836 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2837 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002838 if (*p == ')')
2839 {
2840 *arg = p + 1;
2841 return OK;
2842 }
2843
Bram Moolenaara5565e42020-05-09 15:44:01 +02002844 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 return FAIL;
2846 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002847
2848 if (*p != ',' && *skipwhite(p) == ',')
2849 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002850 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002851 p = skipwhite(p);
2852 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002854 {
2855 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002856 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002857 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002858 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002859 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002860 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002862failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002863 emsg(_(e_missing_close));
2864 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865}
2866
2867/*
2868 * Compile a function call: name(arg1, arg2)
2869 * "arg" points to "name", "arg + varlen" to the "(".
2870 * "argcount_init" is 1 for "value->method()"
2871 * Instructions:
2872 * EVAL arg1
2873 * EVAL arg2
2874 * BCALL / DCALL / UCALL
2875 */
2876 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002877compile_call(
2878 char_u **arg,
2879 size_t varlen,
2880 cctx_T *cctx,
2881 ppconst_T *ppconst,
2882 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883{
2884 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002885 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 int argcount = argcount_init;
2887 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002888 char_u fname_buf[FLEN_FIXED + 1];
2889 char_u *tofree = NULL;
2890 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002892 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893
Bram Moolenaara5565e42020-05-09 15:44:01 +02002894 // we can evaluate "has('name')" at compile time
2895 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2896 {
2897 char_u *s = skipwhite(*arg + varlen + 1);
2898 typval_T argvars[2];
2899
2900 argvars[0].v_type = VAR_UNKNOWN;
2901 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002902 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002903 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002904 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002905 s = skipwhite(s);
2906 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2907 {
2908 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2909
2910 *arg = s + 1;
2911 argvars[1].v_type = VAR_UNKNOWN;
2912 tv->v_type = VAR_NUMBER;
2913 tv->vval.v_number = 0;
2914 f_has(argvars, tv);
2915 clear_tv(&argvars[0]);
2916 ++ppconst->pp_used;
2917 return OK;
2918 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002919 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002920 }
2921
2922 if (generate_ppconst(cctx, ppconst) == FAIL)
2923 return FAIL;
2924
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 if (varlen >= sizeof(namebuf))
2926 {
2927 semsg(_("E1011: name too long: %s"), name);
2928 return FAIL;
2929 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002930 vim_strncpy(namebuf, *arg, varlen);
2931 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932
2933 *arg = skipwhite(*arg + varlen + 1);
2934 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002935 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002937 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 {
2939 int idx;
2940
2941 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002942 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002944 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002945 else
2946 semsg(_(e_unknownfunc), namebuf);
2947 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 }
2949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002951 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002953 {
2954 res = generate_CALL(cctx, ufunc, argcount);
2955 goto theend;
2956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957
2958 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002959 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002961 if (STRNCMP(namebuf, "g:", 2) != 0
2962 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002963 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002964 garray_T *stack = &cctx->ctx_type_stack;
2965 type_T *type;
2966
2967 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2968 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002969 goto theend;
2970 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002972 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002973 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002974 if (STRNCMP(namebuf, "g:", 2) == 0)
2975 res = generate_UCALL(cctx, name, argcount);
2976 else
2977 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002978
2979theend:
2980 vim_free(tofree);
2981 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982}
2983
2984// like NAMESPACE_CHAR but with 'a' and 'l'.
2985#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2986
2987/*
2988 * Find the end of a variable or function name. Unlike find_name_end() this
2989 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002990 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 * Return a pointer to just after the name. Equal to "arg" if there is no
2992 * valid name.
2993 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002994 static char_u *
2995to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996{
2997 char_u *p;
2998
2999 // Quick check for valid starting character.
3000 if (!eval_isnamec1(*arg))
3001 return arg;
3002
3003 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3004 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3005 // and can be used in slice "[n:]".
3006 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003007 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3009 break;
3010 return p;
3011}
3012
3013/*
3014 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003015 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 */
3017 char_u *
3018to_name_const_end(char_u *arg)
3019{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003020 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 typval_T rettv;
3022
3023 if (p == arg && *arg == '[')
3024 {
3025
3026 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003027 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 p = arg;
3029 }
3030 else if (p == arg && *arg == '#' && arg[1] == '{')
3031 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003032 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003034 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 p = arg;
3036 }
3037 else if (p == arg && *arg == '{')
3038 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003039 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003041 // Can be "{x -> ret}()".
3042 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003044 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 if (ret != OK)
3046 p = arg;
3047 }
3048
3049 return p;
3050}
3051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052/*
3053 * parse a list: [expr, expr]
3054 * "*arg" points to the '['.
3055 */
3056 static int
3057compile_list(char_u **arg, cctx_T *cctx)
3058{
3059 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003060 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 int count = 0;
3062
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003063 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003065 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003066 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003067 semsg(_(e_list_end), *arg);
3068 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003069 }
3070 if (*p == ']')
3071 {
3072 ++p;
3073 // Allow for following comment, after at least one space.
3074 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
3075 p += STRLEN(p);
3076 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003077 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003078 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 break;
3080 ++count;
3081 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003082 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003084 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3085 {
3086 semsg(_(e_white_after), ",");
3087 return FAIL;
3088 }
3089 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003090 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 p = skipwhite(p);
3092 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003093 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094
3095 generate_NEWLIST(cctx, count);
3096 return OK;
3097}
3098
3099/*
3100 * parse a lambda: {arg, arg -> expr}
3101 * "*arg" points to the '{'.
3102 */
3103 static int
3104compile_lambda(char_u **arg, cctx_T *cctx)
3105{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 typval_T rettv;
3107 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003108 evalarg_T evalarg;
3109
3110 CLEAR_FIELD(evalarg);
3111 evalarg.eval_flags = EVAL_EVALUATE;
3112 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113
3114 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003115 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003119 ++ufunc->uf_refcount;
3120 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003121 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122
3123 // The function will have one line: "return {expr}".
3124 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003125 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003127 clear_evalarg(&evalarg, NULL);
3128
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003129 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003130 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 return FAIL;
3132}
3133
3134/*
3135 * Compile a lamda call: expr->{lambda}(args)
3136 * "arg" points to the "{".
3137 */
3138 static int
3139compile_lambda_call(char_u **arg, cctx_T *cctx)
3140{
3141 ufunc_T *ufunc;
3142 typval_T rettv;
3143 int argcount = 1;
3144 int ret = FAIL;
3145
3146 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003147 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 return FAIL;
3149
3150 if (**arg != '(')
3151 {
3152 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003153 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 else
3155 semsg(_(e_missing_paren), "lambda");
3156 clear_tv(&rettv);
3157 return FAIL;
3158 }
3159
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 ufunc = rettv.vval.v_partial->pt_func;
3161 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003162 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003163 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003164
3165 // The function will have one line: "return {expr}".
3166 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003167 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168
3169 // compile the arguments
3170 *arg = skipwhite(*arg + 1);
3171 if (compile_arguments(arg, cctx, &argcount) == OK)
3172 // call the compiled function
3173 ret = generate_CALL(cctx, ufunc, argcount);
3174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 return ret;
3176}
3177
3178/*
3179 * parse a dict: {'key': val} or #{key: val}
3180 * "*arg" points to the '{'.
3181 */
3182 static int
3183compile_dict(char_u **arg, cctx_T *cctx, int literal)
3184{
3185 garray_T *instr = &cctx->ctx_instr;
3186 int count = 0;
3187 dict_T *d = dict_alloc();
3188 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003189 char_u *whitep = *arg;
3190 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191
3192 if (d == NULL)
3193 return FAIL;
3194 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003195 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 {
3197 char_u *key = NULL;
3198
Bram Moolenaar23c55272020-06-21 16:58:13 +02003199 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003200 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003201 *arg = NULL;
3202 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003203 }
3204
3205 if (**arg == '}')
3206 break;
3207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 if (literal)
3209 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003210 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211
Bram Moolenaar2c330432020-04-13 14:41:35 +02003212 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 {
3214 semsg(_("E1014: Invalid key: %s"), *arg);
3215 return FAIL;
3216 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003217 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 if (generate_PUSHS(cctx, key) == FAIL)
3219 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003220 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 }
3222 else
3223 {
3224 isn_T *isn;
3225
Bram Moolenaara5565e42020-05-09 15:44:01 +02003226 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 return FAIL;
3228 // TODO: check type is string
3229 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3230 if (isn->isn_type == ISN_PUSHS)
3231 key = isn->isn_arg.string;
3232 }
3233
3234 // Check for duplicate keys, if using string keys.
3235 if (key != NULL)
3236 {
3237 item = dict_find(d, key, -1);
3238 if (item != NULL)
3239 {
3240 semsg(_(e_duplicate_key), key);
3241 goto failret;
3242 }
3243 item = dictitem_alloc(key);
3244 if (item != NULL)
3245 {
3246 item->di_tv.v_type = VAR_UNKNOWN;
3247 item->di_tv.v_lock = 0;
3248 if (dict_add(d, item) == FAIL)
3249 dictitem_free(item);
3250 }
3251 }
3252
3253 *arg = skipwhite(*arg);
3254 if (**arg != ':')
3255 {
3256 semsg(_(e_missing_dict_colon), *arg);
3257 return FAIL;
3258 }
3259
Bram Moolenaar2c330432020-04-13 14:41:35 +02003260 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003262 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003263 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003264 *arg = NULL;
3265 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003266 }
3267
Bram Moolenaara5565e42020-05-09 15:44:01 +02003268 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 return FAIL;
3270 ++count;
3271
Bram Moolenaar2c330432020-04-13 14:41:35 +02003272 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003273 *arg = skipwhite(*arg);
3274 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003275 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003276 *arg = NULL;
3277 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003278 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 if (**arg == '}')
3280 break;
3281 if (**arg != ',')
3282 {
3283 semsg(_(e_missing_dict_comma), *arg);
3284 goto failret;
3285 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003286 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 *arg = skipwhite(*arg + 1);
3288 }
3289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 *arg = *arg + 1;
3291
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003292 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003293 p = skipwhite(*arg);
3294 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003295 *arg += STRLEN(*arg);
3296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 dict_unref(d);
3298 return generate_NEWDICT(cctx, count);
3299
3300failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003301 if (*arg == NULL)
3302 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 dict_unref(d);
3304 return FAIL;
3305}
3306
3307/*
3308 * Compile "&option".
3309 */
3310 static int
3311compile_get_option(char_u **arg, cctx_T *cctx)
3312{
3313 typval_T rettv;
3314 char_u *start = *arg;
3315 int ret;
3316
3317 // parse the option and get the current value to get the type.
3318 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003319 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 if (ret == OK)
3321 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003322 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 char_u *name = vim_strnsave(start, *arg - start);
3324 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3325
3326 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3327 vim_free(name);
3328 }
3329 clear_tv(&rettv);
3330
3331 return ret;
3332}
3333
3334/*
3335 * Compile "$VAR".
3336 */
3337 static int
3338compile_get_env(char_u **arg, cctx_T *cctx)
3339{
3340 char_u *start = *arg;
3341 int len;
3342 int ret;
3343 char_u *name;
3344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 ++*arg;
3346 len = get_env_len(arg);
3347 if (len == 0)
3348 {
3349 semsg(_(e_syntax_at), start - 1);
3350 return FAIL;
3351 }
3352
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003353 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 name = vim_strnsave(start, len + 1);
3355 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3356 vim_free(name);
3357 return ret;
3358}
3359
3360/*
3361 * Compile "@r".
3362 */
3363 static int
3364compile_get_register(char_u **arg, cctx_T *cctx)
3365{
3366 int ret;
3367
3368 ++*arg;
3369 if (**arg == NUL)
3370 {
3371 semsg(_(e_syntax_at), *arg - 1);
3372 return FAIL;
3373 }
3374 if (!valid_yank_reg(**arg, TRUE))
3375 {
3376 emsg_invreg(**arg);
3377 return FAIL;
3378 }
3379 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3380 ++*arg;
3381 return ret;
3382}
3383
3384/*
3385 * Apply leading '!', '-' and '+' to constant "rettv".
3386 */
3387 static int
3388apply_leader(typval_T *rettv, char_u *start, char_u *end)
3389{
3390 char_u *p = end;
3391
3392 // this works from end to start
3393 while (p > start)
3394 {
3395 --p;
3396 if (*p == '-' || *p == '+')
3397 {
3398 // only '-' has an effect, for '+' we only check the type
3399#ifdef FEAT_FLOAT
3400 if (rettv->v_type == VAR_FLOAT)
3401 {
3402 if (*p == '-')
3403 rettv->vval.v_float = -rettv->vval.v_float;
3404 }
3405 else
3406#endif
3407 {
3408 varnumber_T val;
3409 int error = FALSE;
3410
3411 // tv_get_number_chk() accepts a string, but we don't want that
3412 // here
3413 if (check_not_string(rettv) == FAIL)
3414 return FAIL;
3415 val = tv_get_number_chk(rettv, &error);
3416 clear_tv(rettv);
3417 if (error)
3418 return FAIL;
3419 if (*p == '-')
3420 val = -val;
3421 rettv->v_type = VAR_NUMBER;
3422 rettv->vval.v_number = val;
3423 }
3424 }
3425 else
3426 {
3427 int v = tv2bool(rettv);
3428
3429 // '!' is permissive in the type.
3430 clear_tv(rettv);
3431 rettv->v_type = VAR_BOOL;
3432 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3433 }
3434 }
3435 return OK;
3436}
3437
3438/*
3439 * Recognize v: variables that are constants and set "rettv".
3440 */
3441 static void
3442get_vim_constant(char_u **arg, typval_T *rettv)
3443{
3444 if (STRNCMP(*arg, "v:true", 6) == 0)
3445 {
3446 rettv->v_type = VAR_BOOL;
3447 rettv->vval.v_number = VVAL_TRUE;
3448 *arg += 6;
3449 }
3450 else if (STRNCMP(*arg, "v:false", 7) == 0)
3451 {
3452 rettv->v_type = VAR_BOOL;
3453 rettv->vval.v_number = VVAL_FALSE;
3454 *arg += 7;
3455 }
3456 else if (STRNCMP(*arg, "v:null", 6) == 0)
3457 {
3458 rettv->v_type = VAR_SPECIAL;
3459 rettv->vval.v_number = VVAL_NULL;
3460 *arg += 6;
3461 }
3462 else if (STRNCMP(*arg, "v:none", 6) == 0)
3463 {
3464 rettv->v_type = VAR_SPECIAL;
3465 rettv->vval.v_number = VVAL_NONE;
3466 *arg += 6;
3467 }
3468}
3469
Bram Moolenaar61a89812020-05-07 16:58:17 +02003470 static exptype_T
3471get_compare_type(char_u *p, int *len, int *type_is)
3472{
3473 exptype_T type = EXPR_UNKNOWN;
3474 int i;
3475
3476 switch (p[0])
3477 {
3478 case '=': if (p[1] == '=')
3479 type = EXPR_EQUAL;
3480 else if (p[1] == '~')
3481 type = EXPR_MATCH;
3482 break;
3483 case '!': if (p[1] == '=')
3484 type = EXPR_NEQUAL;
3485 else if (p[1] == '~')
3486 type = EXPR_NOMATCH;
3487 break;
3488 case '>': if (p[1] != '=')
3489 {
3490 type = EXPR_GREATER;
3491 *len = 1;
3492 }
3493 else
3494 type = EXPR_GEQUAL;
3495 break;
3496 case '<': if (p[1] != '=')
3497 {
3498 type = EXPR_SMALLER;
3499 *len = 1;
3500 }
3501 else
3502 type = EXPR_SEQUAL;
3503 break;
3504 case 'i': if (p[1] == 's')
3505 {
3506 // "is" and "isnot"; but not a prefix of a name
3507 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3508 *len = 5;
3509 i = p[*len];
3510 if (!isalnum(i) && i != '_')
3511 {
3512 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3513 *type_is = TRUE;
3514 }
3515 }
3516 break;
3517 }
3518 return type;
3519}
3520
3521/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 * Compile code to apply '-', '+' and '!'.
3523 */
3524 static int
3525compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3526{
3527 char_u *p = end;
3528
3529 // this works from end to start
3530 while (p > start)
3531 {
3532 --p;
3533 if (*p == '-' || *p == '+')
3534 {
3535 int negate = *p == '-';
3536 isn_T *isn;
3537
3538 // TODO: check type
3539 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3540 {
3541 --p;
3542 if (*p == '-')
3543 negate = !negate;
3544 }
3545 // only '-' has an effect, for '+' we only check the type
3546 if (negate)
3547 isn = generate_instr(cctx, ISN_NEGATENR);
3548 else
3549 isn = generate_instr(cctx, ISN_CHECKNR);
3550 if (isn == NULL)
3551 return FAIL;
3552 }
3553 else
3554 {
3555 int invert = TRUE;
3556
3557 while (p > start && p[-1] == '!')
3558 {
3559 --p;
3560 invert = !invert;
3561 }
3562 if (generate_2BOOL(cctx, invert) == FAIL)
3563 return FAIL;
3564 }
3565 }
3566 return OK;
3567}
3568
3569/*
3570 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003571 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 */
3573 static int
3574compile_subscript(
3575 char_u **arg,
3576 cctx_T *cctx,
3577 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003578 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003579 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580{
3581 for (;;)
3582 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003583 char_u *p = skipwhite(*arg);
3584
3585 if (*p == NUL || (VIM_ISWHITE(**arg) && comment_start(p)))
3586 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003587 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003588
3589 // If a following line starts with "->{" or "->X" advance to that
3590 // line, so that a line break before "->" is allowed.
3591 if (next != NULL && next[0] == '-' && next[1] == '>'
3592 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3593 {
3594 next = next_line_from_context(cctx, TRUE);
3595 if (next == NULL)
3596 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003597 *arg = next;
3598 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003599 }
3600 }
3601
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003602 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003604 garray_T *stack = &cctx->ctx_type_stack;
3605 type_T *type;
3606 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003608 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003609 return FAIL;
3610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003612 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3613
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003614 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3616 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003617 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 return FAIL;
3619 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003620 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003622 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003623 return FAIL;
3624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 // something->method()
3626 // Apply the '!', '-' and '+' first:
3627 // -1.0->func() works like (-1.0)->func()
3628 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3629 return FAIL;
3630 *start_leader = end_leader; // don't apply again later
3631
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003632 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003633 *arg = skipwhite(p);
3634 if (may_get_next_line(p, arg, cctx) == FAIL)
3635 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 if (**arg == '{')
3637 {
3638 // lambda call: list->{lambda}
3639 if (compile_lambda_call(arg, cctx) == FAIL)
3640 return FAIL;
3641 }
3642 else
3643 {
3644 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003645 p = *arg;
3646 if (ASCII_ISALPHA(*p) && p[1] == ':')
3647 p += 2;
3648 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 ;
3650 if (*p != '(')
3651 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003652 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 return FAIL;
3654 }
3655 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003656 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 return FAIL;
3658 }
3659 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003660 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003662 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003663 type_T **typep;
3664
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003665 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003666 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003667 // TODO: blob index
3668 // TODO: more arguments
3669 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003670 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003671 return FAIL;
3672
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003673 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003674 *arg = skipwhite(p);
3675 if (may_get_next_line(p, arg, cctx) == FAIL)
3676 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003677 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 return FAIL;
3679
3680 if (**arg != ']')
3681 {
3682 emsg(_(e_missbrac));
3683 return FAIL;
3684 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003685 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003687 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3688 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003689 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003690 if ((*typep)->tt_type == VAR_LIST)
3691 *typep = (*typep)->tt_member;
3692 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3693 return FAIL;
3694 }
3695 else if ((*typep)->tt_type == VAR_DICT)
3696 {
3697 *typep = (*typep)->tt_member;
3698 if (may_generate_2STRING(-1, cctx) == FAIL)
3699 return FAIL;
3700 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3701 return FAIL;
3702 }
3703 else
3704 {
3705 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003706 return FAIL;
3707 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003709 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003711 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003712 return FAIL;
3713
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003714 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003715 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3716 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003718 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 if (eval_isnamec1(*p))
3720 while (eval_isnamec(*p))
3721 MB_PTR_ADV(p);
3722 if (p == *arg)
3723 {
3724 semsg(_(e_syntax_at), *arg);
3725 return FAIL;
3726 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003727 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 return FAIL;
3729 *arg = p;
3730 }
3731 else
3732 break;
3733 }
3734
3735 // TODO - see handle_subscript():
3736 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3737 // Don't do this when "Func" is already a partial that was bound
3738 // explicitly (pt_auto is FALSE).
3739
3740 return OK;
3741}
3742
3743/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003744 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3745 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003747 * If the value is a constant "ppconst->pp_ret" will be set.
3748 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003749 *
3750 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 */
3752
3753/*
3754 * number number constant
3755 * 0zFFFFFFFF Blob constant
3756 * "string" string constant
3757 * 'string' literal string constant
3758 * &option-name option value
3759 * @r register contents
3760 * identifier variable value
3761 * function() function call
3762 * $VAR environment variable
3763 * (expression) nested expression
3764 * [expr, expr] List
3765 * {key: val, key: val} Dictionary
3766 * #{key: val, key: val} Dictionary with literal keys
3767 *
3768 * Also handle:
3769 * ! in front logical NOT
3770 * - in front unary minus
3771 * + in front unary plus (ignored)
3772 * trailing (arg) funcref/partial call
3773 * trailing [] subscript in String or List
3774 * trailing .name entry in Dictionary
3775 * trailing ->name() method call
3776 */
3777 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003778compile_expr7(
3779 char_u **arg,
3780 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003781 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 char_u *start_leader, *end_leader;
3784 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003785 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003786 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787
3788 /*
3789 * Skip '!', '-' and '+' characters. They are handled later.
3790 */
3791 start_leader = *arg;
3792 while (**arg == '!' || **arg == '-' || **arg == '+')
3793 *arg = skipwhite(*arg + 1);
3794 end_leader = *arg;
3795
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003796 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 switch (**arg)
3798 {
3799 /*
3800 * Number constant.
3801 */
3802 case '0': // also for blob starting with 0z
3803 case '1':
3804 case '2':
3805 case '3':
3806 case '4':
3807 case '5':
3808 case '6':
3809 case '7':
3810 case '8':
3811 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003812 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 return FAIL;
3814 break;
3815
3816 /*
3817 * String constant: "string".
3818 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003819 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 return FAIL;
3821 break;
3822
3823 /*
3824 * Literal string constant: 'str''ing'.
3825 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003826 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 return FAIL;
3828 break;
3829
3830 /*
3831 * Constant Vim variable.
3832 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003833 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 ret = NOTDONE;
3835 break;
3836
3837 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003838 * "true" constant
3839 */
3840 case 't': if (STRNCMP(*arg, "true", 4) == 0
3841 && !eval_isnamec((*arg)[4]))
3842 {
3843 *arg += 4;
3844 rettv->v_type = VAR_BOOL;
3845 rettv->vval.v_number = VVAL_TRUE;
3846 }
3847 else
3848 ret = NOTDONE;
3849 break;
3850
3851 /*
3852 * "false" constant
3853 */
3854 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3855 && !eval_isnamec((*arg)[5]))
3856 {
3857 *arg += 5;
3858 rettv->v_type = VAR_BOOL;
3859 rettv->vval.v_number = VVAL_FALSE;
3860 }
3861 else
3862 ret = NOTDONE;
3863 break;
3864
3865 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 * List: [expr, expr]
3867 */
3868 case '[': ret = compile_list(arg, cctx);
3869 break;
3870
3871 /*
3872 * Dictionary: #{key: val, key: val}
3873 */
3874 case '#': if ((*arg)[1] == '{')
3875 {
3876 ++*arg;
3877 ret = compile_dict(arg, cctx, TRUE);
3878 }
3879 else
3880 ret = NOTDONE;
3881 break;
3882
3883 /*
3884 * Lambda: {arg, arg -> expr}
3885 * Dictionary: {'key': val, 'key': val}
3886 */
3887 case '{': {
3888 char_u *start = skipwhite(*arg + 1);
3889
3890 // Find out what comes after the arguments.
3891 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003892 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 if (ret != FAIL && *start == '>')
3894 ret = compile_lambda(arg, cctx);
3895 else
3896 ret = compile_dict(arg, cctx, FALSE);
3897 }
3898 break;
3899
3900 /*
3901 * Option value: &name
3902 */
3903 case '&': ret = compile_get_option(arg, cctx);
3904 break;
3905
3906 /*
3907 * Environment variable: $VAR.
3908 */
3909 case '$': ret = compile_get_env(arg, cctx);
3910 break;
3911
3912 /*
3913 * Register contents: @r.
3914 */
3915 case '@': ret = compile_get_register(arg, cctx);
3916 break;
3917 /*
3918 * nested expression: (expression).
3919 */
3920 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003921
3922 // recursive!
3923 if (ppconst->pp_used <= PPSIZE - 10)
3924 {
3925 ret = compile_expr1(arg, cctx, ppconst);
3926 }
3927 else
3928 {
3929 // Not enough space in ppconst, flush constants.
3930 if (generate_ppconst(cctx, ppconst) == FAIL)
3931 return FAIL;
3932 ret = compile_expr0(arg, cctx);
3933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 *arg = skipwhite(*arg);
3935 if (**arg == ')')
3936 ++*arg;
3937 else if (ret == OK)
3938 {
3939 emsg(_(e_missing_close));
3940 ret = FAIL;
3941 }
3942 break;
3943
3944 default: ret = NOTDONE;
3945 break;
3946 }
3947 if (ret == FAIL)
3948 return FAIL;
3949
Bram Moolenaar1c747212020-05-09 18:28:34 +02003950 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 {
3952 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003953 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003955 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 return FAIL;
3957 }
3958 start_leader = end_leader; // don't apply again below
3959
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003960 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003961 clear_tv(rettv);
3962 else
3963 // A constant expression can possibly be handled compile time,
3964 // return the value instead of generating code.
3965 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 }
3967 else if (ret == NOTDONE)
3968 {
3969 char_u *p;
3970 int r;
3971
3972 if (!eval_isnamec1(**arg))
3973 {
3974 semsg(_("E1015: Name expected: %s"), *arg);
3975 return FAIL;
3976 }
3977
3978 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003979 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003981 {
3982 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3983 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003985 {
3986 if (generate_ppconst(cctx, ppconst) == FAIL)
3987 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003989 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 if (r == FAIL)
3991 return FAIL;
3992 }
3993
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003994 // Handle following "[]", ".member", etc.
3995 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003996 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003997 ppconst) == FAIL)
3998 return FAIL;
3999 if (ppconst->pp_used > 0)
4000 {
4001 // apply the '!', '-' and '+' before the constant
4002 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4003 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4004 return FAIL;
4005 return OK;
4006 }
4007 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004009 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010}
4011
4012/*
4013 * * number multiplication
4014 * / number division
4015 * % number modulo
4016 */
4017 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004018compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019{
4020 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004021 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004022 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004024 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004025 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 return FAIL;
4027
4028 /*
4029 * Repeat computing, until no "*", "/" or "%" is following.
4030 */
4031 for (;;)
4032 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004033 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 if (*op != '*' && *op != '/' && *op != '%')
4035 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004036 if (next != NULL)
4037 {
4038 *arg = next_line_from_context(cctx, TRUE);
4039 op = skipwhite(*arg);
4040 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004041
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004042 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 {
4044 char_u buf[3];
4045
4046 vim_strncpy(buf, op, 1);
4047 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004048 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 }
4050 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004051 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004052 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004054 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004055 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004057
4058 if (ppconst->pp_used == ppconst_used + 2
4059 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4060 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004061 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004062 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4063 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004064 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004066 // both are numbers: compute the result
4067 switch (*op)
4068 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004069 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004070 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004071 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004072 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004073 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004074 break;
4075 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004076 tv1->vval.v_number = res;
4077 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004078 }
4079 else
4080 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004081 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004082 generate_two_op(cctx, op);
4083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 }
4085
4086 return OK;
4087}
4088
4089/*
4090 * + number addition
4091 * - number subtraction
4092 * .. string concatenation
4093 */
4094 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004095compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096{
4097 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004098 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004100 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101
4102 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004103 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 return FAIL;
4105
4106 /*
4107 * Repeat computing, until no "+", "-" or ".." is following.
4108 */
4109 for (;;)
4110 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004111 op = may_peek_next_line(cctx, *arg, &next);
4112 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 break;
4114 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004115 if (next != NULL)
4116 {
4117 *arg = next_line_from_context(cctx, TRUE);
4118 op = skipwhite(*arg);
4119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004121 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 {
4123 char_u buf[3];
4124
4125 vim_strncpy(buf, op, oplen);
4126 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004127 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 }
4129
4130 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004131 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004132 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004134 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004135 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 return FAIL;
4137
Bram Moolenaara5565e42020-05-09 15:44:01 +02004138 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004139 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004140 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4141 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4142 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4143 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004145 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4146 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004147
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004148 // concat/subtract/add constant numbers
4149 if (*op == '+')
4150 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4151 else if (*op == '-')
4152 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4153 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004154 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004155 // concatenate constant strings
4156 char_u *s1 = tv1->vval.v_string;
4157 char_u *s2 = tv2->vval.v_string;
4158 size_t len1 = STRLEN(s1);
4159
4160 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4161 if (tv1->vval.v_string == NULL)
4162 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004163 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004164 return FAIL;
4165 }
4166 mch_memmove(tv1->vval.v_string, s1, len1);
4167 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004168 vim_free(s1);
4169 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004170 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004171 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 }
4173 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004174 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004175 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004176 if (*op == '.')
4177 {
4178 if (may_generate_2STRING(-2, cctx) == FAIL
4179 || may_generate_2STRING(-1, cctx) == FAIL)
4180 return FAIL;
4181 generate_instr_drop(cctx, ISN_CONCAT, 1);
4182 }
4183 else
4184 generate_two_op(cctx, op);
4185 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 }
4187
4188 return OK;
4189}
4190
4191/*
4192 * expr5a == expr5b
4193 * expr5a =~ expr5b
4194 * expr5a != expr5b
4195 * expr5a !~ expr5b
4196 * expr5a > expr5b
4197 * expr5a >= expr5b
4198 * expr5a < expr5b
4199 * expr5a <= expr5b
4200 * expr5a is expr5b
4201 * expr5a isnot expr5b
4202 *
4203 * Produces instructions:
4204 * EVAL expr5a Push result of "expr5a"
4205 * EVAL expr5b Push result of "expr5b"
4206 * COMPARE one of the compare instructions
4207 */
4208 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004209compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210{
4211 exptype_T type = EXPR_UNKNOWN;
4212 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004213 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004216 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217
4218 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004219 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 return FAIL;
4221
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004222 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004223 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224
4225 /*
4226 * If there is a comparative operator, use it.
4227 */
4228 if (type != EXPR_UNKNOWN)
4229 {
4230 int ic = FALSE; // Default: do not ignore case
4231
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004232 if (next != NULL)
4233 {
4234 *arg = next_line_from_context(cctx, TRUE);
4235 p = skipwhite(*arg);
4236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 if (type_is && (p[len] == '?' || p[len] == '#'))
4238 {
4239 semsg(_(e_invexpr2), *arg);
4240 return FAIL;
4241 }
4242 // extra question mark appended: ignore case
4243 if (p[len] == '?')
4244 {
4245 ic = TRUE;
4246 ++len;
4247 }
4248 // extra '#' appended: match case (ignored)
4249 else if (p[len] == '#')
4250 ++len;
4251 // nothing appended: match case
4252
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004253 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 {
4255 char_u buf[7];
4256
4257 vim_strncpy(buf, p, len);
4258 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004259 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 }
4261
4262 // get the second variable
4263 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004264 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004265 return FAIL;
4266
Bram Moolenaara5565e42020-05-09 15:44:01 +02004267 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 return FAIL;
4269
Bram Moolenaara5565e42020-05-09 15:44:01 +02004270 if (ppconst->pp_used == ppconst_used + 2)
4271 {
4272 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4273 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4274 int ret;
4275
4276 // Both sides are a constant, compute the result now.
4277 // First check for a valid combination of types, this is more
4278 // strict than typval_compare().
4279 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4280 ret = FAIL;
4281 else
4282 {
4283 ret = typval_compare(tv1, tv2, type, ic);
4284 tv1->v_type = VAR_BOOL;
4285 tv1->vval.v_number = tv1->vval.v_number
4286 ? VVAL_TRUE : VVAL_FALSE;
4287 clear_tv(tv2);
4288 --ppconst->pp_used;
4289 }
4290 return ret;
4291 }
4292
4293 generate_ppconst(cctx, ppconst);
4294 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 }
4296
4297 return OK;
4298}
4299
Bram Moolenaar7f141552020-05-09 17:35:53 +02004300static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302/*
4303 * Compile || or &&.
4304 */
4305 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004306compile_and_or(
4307 char_u **arg,
4308 cctx_T *cctx,
4309 char *op,
4310 ppconst_T *ppconst,
4311 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004313 char_u *next;
4314 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 int opchar = *op;
4316
4317 if (p[0] == opchar && p[1] == opchar)
4318 {
4319 garray_T *instr = &cctx->ctx_instr;
4320 garray_T end_ga;
4321
4322 /*
4323 * Repeat until there is no following "||" or "&&"
4324 */
4325 ga_init2(&end_ga, sizeof(int), 10);
4326 while (p[0] == opchar && p[1] == opchar)
4327 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004328 if (next != NULL)
4329 {
4330 *arg = next_line_from_context(cctx, TRUE);
4331 p = skipwhite(*arg);
4332 }
4333
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004334 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4335 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004337 return FAIL;
4338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339
Bram Moolenaara5565e42020-05-09 15:44:01 +02004340 // TODO: use ppconst if the value is a constant
4341 generate_ppconst(cctx, ppconst);
4342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 if (ga_grow(&end_ga, 1) == FAIL)
4344 {
4345 ga_clear(&end_ga);
4346 return FAIL;
4347 }
4348 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4349 ++end_ga.ga_len;
4350 generate_JUMP(cctx, opchar == '|'
4351 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4352
4353 // eval the next expression
4354 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004355 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004356 return FAIL;
4357
Bram Moolenaara5565e42020-05-09 15:44:01 +02004358 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4359 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 {
4361 ga_clear(&end_ga);
4362 return FAIL;
4363 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004364
4365 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004367 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368
4369 // Fill in the end label in all jumps.
4370 while (end_ga.ga_len > 0)
4371 {
4372 isn_T *isn;
4373
4374 --end_ga.ga_len;
4375 isn = ((isn_T *)instr->ga_data)
4376 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4377 isn->isn_arg.jump.jump_where = instr->ga_len;
4378 }
4379 ga_clear(&end_ga);
4380 }
4381
4382 return OK;
4383}
4384
4385/*
4386 * expr4a && expr4a && expr4a logical AND
4387 *
4388 * Produces instructions:
4389 * EVAL expr4a Push result of "expr4a"
4390 * JUMP_AND_KEEP_IF_FALSE end
4391 * EVAL expr4b Push result of "expr4b"
4392 * JUMP_AND_KEEP_IF_FALSE end
4393 * EVAL expr4c Push result of "expr4c"
4394 * end:
4395 */
4396 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004397compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004399 int ppconst_used = ppconst->pp_used;
4400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004402 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 return FAIL;
4404
4405 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004406 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407}
4408
4409/*
4410 * expr3a || expr3b || expr3c logical OR
4411 *
4412 * Produces instructions:
4413 * EVAL expr3a Push result of "expr3a"
4414 * JUMP_AND_KEEP_IF_TRUE end
4415 * EVAL expr3b Push result of "expr3b"
4416 * JUMP_AND_KEEP_IF_TRUE end
4417 * EVAL expr3c Push result of "expr3c"
4418 * end:
4419 */
4420 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004421compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004423 int ppconst_used = ppconst->pp_used;
4424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004426 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 return FAIL;
4428
4429 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004430 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431}
4432
4433/*
4434 * Toplevel expression: expr2 ? expr1a : expr1b
4435 *
4436 * Produces instructions:
4437 * EVAL expr2 Push result of "expr"
4438 * JUMP_IF_FALSE alt jump if false
4439 * EVAL expr1a
4440 * JUMP_ALWAYS end
4441 * alt: EVAL expr1b
4442 * end:
4443 */
4444 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004445compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446{
4447 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004448 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004449 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450
Bram Moolenaar61a89812020-05-07 16:58:17 +02004451 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004452 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 return FAIL;
4454
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004455 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 if (*p == '?')
4457 {
4458 garray_T *instr = &cctx->ctx_instr;
4459 garray_T *stack = &cctx->ctx_type_stack;
4460 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004461 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004463 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004465 int has_const_expr = FALSE;
4466 int const_value = FALSE;
4467 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004469 if (next != NULL)
4470 {
4471 *arg = next_line_from_context(cctx, TRUE);
4472 p = skipwhite(*arg);
4473 }
4474
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004475 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4476 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004478 return FAIL;
4479 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480
Bram Moolenaara5565e42020-05-09 15:44:01 +02004481 if (ppconst->pp_used == ppconst_used + 1)
4482 {
4483 // the condition is a constant, we know whether the ? or the :
4484 // expression is to be evaluated.
4485 has_const_expr = TRUE;
4486 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4487 clear_tv(&ppconst->pp_tv[ppconst_used]);
4488 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004489 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4490 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004491 }
4492 else
4493 {
4494 generate_ppconst(cctx, ppconst);
4495 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497
4498 // evaluate the second expression; any type is accepted
4499 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004500 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004501 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004502 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004503 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504
Bram Moolenaara5565e42020-05-09 15:44:01 +02004505 if (!has_const_expr)
4506 {
4507 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508
Bram Moolenaara5565e42020-05-09 15:44:01 +02004509 // remember the type and drop it
4510 --stack->ga_len;
4511 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512
Bram Moolenaara5565e42020-05-09 15:44:01 +02004513 end_idx = instr->ga_len;
4514 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4515
4516 // jump here from JUMP_IF_FALSE
4517 isn = ((isn_T *)instr->ga_data) + alt_idx;
4518 isn->isn_arg.jump.jump_where = instr->ga_len;
4519 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520
4521 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004522 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 if (*p != ':')
4524 {
4525 emsg(_(e_missing_colon));
4526 return FAIL;
4527 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004528 if (next != NULL)
4529 {
4530 *arg = next_line_from_context(cctx, TRUE);
4531 p = skipwhite(*arg);
4532 }
4533
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004534 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4535 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004537 return FAIL;
4538 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539
4540 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004541 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004542 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4543 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004545 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004546 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004547 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004548 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549
Bram Moolenaara5565e42020-05-09 15:44:01 +02004550 if (!has_const_expr)
4551 {
4552 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554 // If the types differ, the result has a more generic type.
4555 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4556 common_type(type1, type2, &type2, cctx->ctx_type_list);
4557
4558 // jump here from JUMP_ALWAYS
4559 isn = ((isn_T *)instr->ga_data) + end_idx;
4560 isn->isn_arg.jump.jump_where = instr->ga_len;
4561 }
4562
4563 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 }
4565 return OK;
4566}
4567
4568/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569 * Toplevel expression.
4570 */
4571 static int
4572compile_expr0(char_u **arg, cctx_T *cctx)
4573{
4574 ppconst_T ppconst;
4575
4576 CLEAR_FIELD(ppconst);
4577 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4578 {
4579 clear_ppconst(&ppconst);
4580 return FAIL;
4581 }
4582 if (generate_ppconst(cctx, &ppconst) == FAIL)
4583 return FAIL;
4584 return OK;
4585}
4586
4587/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 * compile "return [expr]"
4589 */
4590 static char_u *
4591compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4592{
4593 char_u *p = arg;
4594 garray_T *stack = &cctx->ctx_type_stack;
4595 type_T *stack_type;
4596
4597 if (*p != NUL && *p != '|' && *p != '\n')
4598 {
4599 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004600 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 return NULL;
4602
4603 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4604 if (set_return_type)
4605 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004606 else
4607 {
4608 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4609 && stack_type->tt_type != VAR_VOID
4610 && stack_type->tt_type != VAR_UNKNOWN)
4611 {
4612 emsg(_("E1096: Returning a value in a function without a return type"));
4613 return NULL;
4614 }
4615 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 == FAIL)
4617 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004618 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 }
4620 else
4621 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004622 // "set_return_type" cannot be TRUE, only used for a lambda which
4623 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004624 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4625 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 {
4627 emsg(_("E1003: Missing return value"));
4628 return NULL;
4629 }
4630
4631 // No argument, return zero.
4632 generate_PUSHNR(cctx, 0);
4633 }
4634
4635 if (generate_instr(cctx, ISN_RETURN) == NULL)
4636 return NULL;
4637
4638 // "return val | endif" is possible
4639 return skipwhite(p);
4640}
4641
4642/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004643 * Get a line from the compilation context, compatible with exarg_T getline().
4644 * Return a pointer to the line in allocated memory.
4645 * Return NULL for end-of-file or some error.
4646 */
4647 static char_u *
4648exarg_getline(
4649 int c UNUSED,
4650 void *cookie,
4651 int indent UNUSED,
4652 int do_concat UNUSED)
4653{
4654 cctx_T *cctx = (cctx_T *)cookie;
4655
4656 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4657 {
4658 iemsg("Heredoc got to end");
4659 return NULL;
4660 }
4661 ++cctx->ctx_lnum;
4662 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4663 [cctx->ctx_lnum]);
4664}
4665
4666/*
4667 * Compile a nested :def command.
4668 */
4669 static char_u *
4670compile_nested_function(exarg_T *eap, cctx_T *cctx)
4671{
4672 char_u *name_start = eap->arg;
4673 char_u *name_end = to_name_end(eap->arg, FALSE);
4674 char_u *name = get_lambda_name();
4675 lvar_T *lvar;
4676 ufunc_T *ufunc;
4677
4678 eap->arg = name_end;
4679 eap->getline = exarg_getline;
4680 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004681 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004682 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004683 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004684
Bram Moolenaar822ba242020-05-24 23:00:18 +02004685 if (ufunc == NULL)
4686 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004687 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004688 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004689 return NULL;
4690
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004691 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004692 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004693 TRUE, ufunc->uf_func_type);
4694
4695 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4696 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4697 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004698
Bram Moolenaar61a89812020-05-07 16:58:17 +02004699 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004700 return (char_u *)"";
4701}
4702
4703/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 * Return the length of an assignment operator, or zero if there isn't one.
4705 */
4706 int
4707assignment_len(char_u *p, int *heredoc)
4708{
4709 if (*p == '=')
4710 {
4711 if (p[1] == '<' && p[2] == '<')
4712 {
4713 *heredoc = TRUE;
4714 return 3;
4715 }
4716 return 1;
4717 }
4718 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4719 return 2;
4720 if (STRNCMP(p, "..=", 3) == 0)
4721 return 3;
4722 return 0;
4723}
4724
4725// words that cannot be used as a variable
4726static char *reserved[] = {
4727 "true",
4728 "false",
4729 NULL
4730};
4731
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004732typedef enum {
4733 dest_local,
4734 dest_option,
4735 dest_env,
4736 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004737 dest_buffer,
4738 dest_window,
4739 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004740 dest_vimvar,
4741 dest_script,
4742 dest_reg,
4743} assign_dest_T;
4744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004746 * Generate the load instruction for "name".
4747 */
4748 static void
4749generate_loadvar(
4750 cctx_T *cctx,
4751 assign_dest_T dest,
4752 char_u *name,
4753 lvar_T *lvar,
4754 type_T *type)
4755{
4756 switch (dest)
4757 {
4758 case dest_option:
4759 // TODO: check the option exists
4760 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4761 break;
4762 case dest_global:
4763 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4764 break;
4765 case dest_buffer:
4766 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4767 break;
4768 case dest_window:
4769 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4770 break;
4771 case dest_tab:
4772 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4773 break;
4774 case dest_script:
4775 compile_load_scriptvar(cctx,
4776 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4777 break;
4778 case dest_env:
4779 // Include $ in the name here
4780 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4781 break;
4782 case dest_reg:
4783 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4784 break;
4785 case dest_vimvar:
4786 generate_LOADV(cctx, name + 2, TRUE);
4787 break;
4788 case dest_local:
4789 if (lvar->lv_from_outer)
4790 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4791 NULL, type);
4792 else
4793 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4794 break;
4795 }
4796}
4797
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004798 void
4799vim9_declare_error(char_u *name)
4800{
4801 char *scope = "";
4802
4803 switch (*name)
4804 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004805 case 'g': scope = _("global"); break;
4806 case 'b': scope = _("buffer"); break;
4807 case 'w': scope = _("window"); break;
4808 case 't': scope = _("tab"); break;
4809 case 'v': scope = "v:"; break;
4810 case '$': semsg(_(e_declare_env_var), name); return;
4811 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004812 }
4813 semsg(_(e_declare_var), scope, name);
4814}
4815
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004816/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004817 * Compile declaration and assignment:
4818 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004820 * Return NULL for an error.
4821 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 */
4823 static char_u *
4824compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4825{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004826 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004828 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 char_u *ret = NULL;
4830 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004831 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004834 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836 int oplen = 0;
4837 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004838 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004839 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004840 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004844 // Skip over the "var" or "[var, var]" to get to any "=".
4845 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4846 if (p == NULL)
4847 return *arg == '[' ? arg : NULL;
4848
4849 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004851 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 return NULL;
4853 }
4854
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855 sp = p;
4856 p = skipwhite(p);
4857 op = p;
4858 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004859
4860 if (var_count > 0 && oplen == 0)
4861 // can be something like "[1, 2]->func()"
4862 return arg;
4863
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4865 {
4866 char_u buf[4];
4867
4868 vim_strncpy(buf, op, oplen);
4869 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004870 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004871 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004872
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 if (heredoc)
4874 {
4875 list_T *l;
4876 listitem_T *li;
4877
4878 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004879 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004881 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882
4883 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004884 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 {
4886 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4887 li->li_tv.vval.v_string = NULL;
4888 }
4889 generate_NEWLIST(cctx, l->lv_len);
4890 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004891 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 list_free(l);
4893 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004894 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004896 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004898 // for "[var, var] = expr" evaluate the expression here, loop over the
4899 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004900
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004901 p = skipwhite(op + oplen);
4902 if (compile_expr0(&p, cctx) == FAIL)
4903 return NULL;
4904 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004906 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004908 type_T *stacktype;
4909
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004910 stacktype = stack->ga_len == 0 ? &t_void
4911 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004912 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004914 emsg(_(e_cannot_use_void));
4915 goto theend;
4916 }
4917 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4918 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004919 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4920 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004921 }
4922 }
4923
4924 /*
4925 * Loop over variables in "[var, var] = expr".
4926 * For "var = expr" and "let var: type" this is done only once.
4927 */
4928 if (var_count > 0)
4929 var_start = skipwhite(arg + 1); // skip over the "["
4930 else
4931 var_start = arg;
4932 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4933 {
4934 char_u *var_end = skip_var_one(var_start, FALSE);
4935 size_t varlen;
4936 int new_local = FALSE;
4937 int opt_type;
4938 int opt_flags = 0;
4939 assign_dest_T dest = dest_local;
4940 int vimvaridx = -1;
4941 lvar_T *lvar = NULL;
4942 lvar_T arg_lvar;
4943 int has_type = FALSE;
4944 int has_index = FALSE;
4945 int instr_count = -1;
4946
4947 p = (*var_start == '&' || *var_start == '$'
4948 || *var_start == '@') ? var_start + 1 : var_start;
4949 p = to_name_end(p, TRUE);
4950
4951 // "a: type" is declaring variable "a" with a type, not "a:".
4952 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4953 --var_end;
4954 if (is_decl && p == var_start + 2 && p[-1] == ':')
4955 --p;
4956
4957 varlen = p - var_start;
4958 vim_free(name);
4959 name = vim_strnsave(var_start, varlen);
4960 if (name == NULL)
4961 return NULL;
4962 if (!heredoc)
4963 type = &t_any;
4964
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004965 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004966 {
4967 if (*var_start == '&')
4968 {
4969 int cc;
4970 long numval;
4971
4972 dest = dest_option;
4973 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004975 emsg(_(e_const_option));
4976 goto theend;
4977 }
4978 if (is_decl)
4979 {
4980 semsg(_("E1052: Cannot declare an option: %s"), var_start);
4981 goto theend;
4982 }
4983 p = var_start;
4984 p = find_option_end(&p, &opt_flags);
4985 if (p == NULL)
4986 {
4987 // cannot happen?
4988 emsg(_(e_letunexp));
4989 goto theend;
4990 }
4991 cc = *p;
4992 *p = NUL;
4993 opt_type = get_option_value(var_start + 1, &numval,
4994 NULL, opt_flags);
4995 *p = cc;
4996 if (opt_type == -3)
4997 {
4998 semsg(_(e_unknown_option), var_start);
4999 goto theend;
5000 }
5001 if (opt_type == -2 || opt_type == 0)
5002 type = &t_string;
5003 else
5004 type = &t_number; // both number and boolean option
5005 }
5006 else if (*var_start == '$')
5007 {
5008 dest = dest_env;
5009 type = &t_string;
5010 if (is_decl)
5011 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005012 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005013 goto theend;
5014 }
5015 }
5016 else if (*var_start == '@')
5017 {
5018 if (!valid_yank_reg(var_start[1], TRUE))
5019 {
5020 emsg_invreg(var_start[1]);
5021 goto theend;
5022 }
5023 dest = dest_reg;
5024 type = &t_string;
5025 if (is_decl)
5026 {
5027 semsg(_("E1066: Cannot declare a register: %s"), name);
5028 goto theend;
5029 }
5030 }
5031 else if (STRNCMP(var_start, "g:", 2) == 0)
5032 {
5033 dest = dest_global;
5034 if (is_decl)
5035 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005036 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005037 goto theend;
5038 }
5039 }
5040 else if (STRNCMP(var_start, "b:", 2) == 0)
5041 {
5042 dest = dest_buffer;
5043 if (is_decl)
5044 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005045 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005046 goto theend;
5047 }
5048 }
5049 else if (STRNCMP(var_start, "w:", 2) == 0)
5050 {
5051 dest = dest_window;
5052 if (is_decl)
5053 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005054 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005055 goto theend;
5056 }
5057 }
5058 else if (STRNCMP(var_start, "t:", 2) == 0)
5059 {
5060 dest = dest_tab;
5061 if (is_decl)
5062 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005063 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005064 goto theend;
5065 }
5066 }
5067 else if (STRNCMP(var_start, "v:", 2) == 0)
5068 {
5069 typval_T *vtv;
5070 int di_flags;
5071
5072 vimvaridx = find_vim_var(name + 2, &di_flags);
5073 if (vimvaridx < 0)
5074 {
5075 semsg(_(e_var_notfound), var_start);
5076 goto theend;
5077 }
5078 // We use the current value of "sandbox" here, is that OK?
5079 if (var_check_ro(di_flags, name, FALSE))
5080 goto theend;
5081 dest = dest_vimvar;
5082 vtv = get_vim_var_tv(vimvaridx);
5083 type = typval2type(vtv);
5084 if (is_decl)
5085 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005086 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005087 goto theend;
5088 }
5089 }
5090 else
5091 {
5092 int idx;
5093
5094 for (idx = 0; reserved[idx] != NULL; ++idx)
5095 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005096 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005097 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005098 goto theend;
5099 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005100
5101 lvar = lookup_local(var_start, varlen, cctx);
5102 if (lvar == NULL)
5103 {
5104 CLEAR_FIELD(arg_lvar);
5105 if (lookup_arg(var_start, varlen,
5106 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5107 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005108 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005109 if (is_decl)
5110 {
5111 semsg(_(e_used_as_arg), name);
5112 goto theend;
5113 }
5114 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005115 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005116 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005117 if (lvar != NULL)
5118 {
5119 if (is_decl)
5120 {
5121 semsg(_("E1017: Variable already declared: %s"), name);
5122 goto theend;
5123 }
5124 else if (lvar->lv_const)
5125 {
5126 semsg(_("E1018: Cannot assign to a constant: %s"),
5127 name);
5128 goto theend;
5129 }
5130 }
5131 else if (STRNCMP(var_start, "s:", 2) == 0
5132 || lookup_script(var_start, varlen) == OK
5133 || find_imported(var_start, varlen, cctx) != NULL)
5134 {
5135 dest = dest_script;
5136 if (is_decl)
5137 {
5138 semsg(_("E1054: Variable already declared in the script: %s"),
5139 name);
5140 goto theend;
5141 }
5142 }
5143 else if (name[1] == ':' && name[2] != NUL)
5144 {
5145 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5146 name);
5147 goto theend;
5148 }
5149 else if (!is_decl)
5150 {
5151 semsg(_("E1089: unknown variable: %s"), name);
5152 goto theend;
5153 }
5154 }
5155 }
5156
5157 // handle "a:name" as a name, not index "name" on "a"
5158 if (varlen > 1 || var_start[varlen] != ':')
5159 p = var_end;
5160
5161 if (dest != dest_option)
5162 {
5163 if (is_decl && *p == ':')
5164 {
5165 // parse optional type: "let var: type = expr"
5166 if (!VIM_ISWHITE(p[1]))
5167 {
5168 semsg(_(e_white_after), ":");
5169 goto theend;
5170 }
5171 p = skipwhite(p + 1);
5172 type = parse_type(&p, cctx->ctx_type_list);
5173 has_type = TRUE;
5174 }
5175 else if (lvar != NULL)
5176 type = lvar->lv_type;
5177 }
5178
5179 if (oplen == 3 && !heredoc && dest != dest_global
5180 && type->tt_type != VAR_STRING
5181 && type->tt_type != VAR_ANY)
5182 {
5183 emsg(_("E1019: Can only concatenate to string"));
5184 goto theend;
5185 }
5186
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005187 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005188 {
5189 if (oplen > 1 && !heredoc)
5190 {
5191 // +=, /=, etc. require an existing variable
5192 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5193 name);
5194 goto theend;
5195 }
5196
5197 // new local variable
5198 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5199 goto theend;
5200 lvar = reserve_local(cctx, var_start, varlen,
5201 cmdidx == CMD_const, type);
5202 if (lvar == NULL)
5203 goto theend;
5204 new_local = TRUE;
5205 }
5206
5207 member_type = type;
5208 if (var_end > var_start + varlen)
5209 {
5210 // Something follows after the variable: "var[idx]".
5211 if (is_decl)
5212 {
5213 emsg(_("E1087: cannot use an index when declaring a variable"));
5214 goto theend;
5215 }
5216
5217 if (var_start[varlen] == '[')
5218 {
5219 has_index = TRUE;
5220 if (type->tt_member == NULL)
5221 {
5222 semsg(_("E1088: cannot use an index on %s"), name);
5223 goto theend;
5224 }
5225 member_type = type->tt_member;
5226 }
5227 else
5228 {
5229 semsg("Not supported yet: %s", var_start);
5230 goto theend;
5231 }
5232 }
5233 else if (lvar == &arg_lvar)
5234 {
5235 semsg(_("E1090: Cannot assign to argument %s"), name);
5236 goto theend;
5237 }
5238
5239 if (!heredoc)
5240 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005241 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005242 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005243 if (oplen > 0 && var_count == 0)
5244 {
5245 // skip over the "=" and the expression
5246 p = skipwhite(op + oplen);
5247 compile_expr0(&p, cctx);
5248 }
5249 }
5250 else if (oplen > 0)
5251 {
5252 type_T *stacktype;
5253
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005254 // For "var = expr" evaluate the expression.
5255 if (var_count == 0)
5256 {
5257 int r;
5258
5259 // for "+=", "*=", "..=" etc. first load the current value
5260 if (*op != '=')
5261 {
5262 generate_loadvar(cctx, dest, name, lvar, type);
5263
5264 if (has_index)
5265 {
5266 // TODO: get member from list or dict
5267 emsg("Index with operation not supported yet");
5268 goto theend;
5269 }
5270 }
5271
5272 // Compile the expression. Temporarily hide the new local
5273 // variable here, it is not available to this expression.
5274 if (new_local)
5275 --cctx->ctx_locals.ga_len;
5276 instr_count = instr->ga_len;
5277 p = skipwhite(op + oplen);
5278 r = compile_expr0(&p, cctx);
5279 if (new_local)
5280 ++cctx->ctx_locals.ga_len;
5281 if (r == FAIL)
5282 goto theend;
5283 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005284 else if (semicolon && var_idx == var_count - 1)
5285 {
5286 // For "[var; var] = expr" get the rest of the list
5287 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5288 goto theend;
5289 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005290 else
5291 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005292 // For "[var, var] = expr" get the "var_idx" item from the
5293 // list.
5294 if (generate_GETITEM(cctx, var_idx) == FAIL)
5295 return FAIL;
5296 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005297
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005298 stacktype = stack->ga_len == 0 ? &t_void
5299 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5300 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005301 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005302 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005303 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005304 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005305 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005306 emsg(_(e_cannot_use_void));
5307 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005308 }
5309 else
5310 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005311 // An empty list or dict has a &t_void member,
5312 // for a variable that implies &t_any.
5313 if (stacktype == &t_list_empty)
5314 lvar->lv_type = &t_list_any;
5315 else if (stacktype == &t_dict_empty)
5316 lvar->lv_type = &t_dict_any;
5317 else
5318 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005319 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005320 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005321 else
5322 {
5323 type_T *use_type = lvar->lv_type;
5324
5325 if (has_index)
5326 {
5327 use_type = use_type->tt_member;
5328 if (use_type == NULL)
5329 use_type = &t_void;
5330 }
5331 if (need_type(stacktype, use_type, -1, cctx)
5332 == FAIL)
5333 goto theend;
5334 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005335 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005336 else if (*p != '=' && need_type(stacktype, member_type, -1,
5337 cctx) == FAIL)
5338 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005340 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005342 emsg(_(e_const_req_value));
5343 goto theend;
5344 }
5345 else if (!has_type || dest == dest_option)
5346 {
5347 emsg(_(e_type_req));
5348 goto theend;
5349 }
5350 else
5351 {
5352 // variables are always initialized
5353 if (ga_grow(instr, 1) == FAIL)
5354 goto theend;
5355 switch (member_type->tt_type)
5356 {
5357 case VAR_BOOL:
5358 generate_PUSHBOOL(cctx, VVAL_FALSE);
5359 break;
5360 case VAR_FLOAT:
5361#ifdef FEAT_FLOAT
5362 generate_PUSHF(cctx, 0.0);
5363#endif
5364 break;
5365 case VAR_STRING:
5366 generate_PUSHS(cctx, NULL);
5367 break;
5368 case VAR_BLOB:
5369 generate_PUSHBLOB(cctx, NULL);
5370 break;
5371 case VAR_FUNC:
5372 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5373 break;
5374 case VAR_LIST:
5375 generate_NEWLIST(cctx, 0);
5376 break;
5377 case VAR_DICT:
5378 generate_NEWDICT(cctx, 0);
5379 break;
5380 case VAR_JOB:
5381 generate_PUSHJOB(cctx, NULL);
5382 break;
5383 case VAR_CHANNEL:
5384 generate_PUSHCHANNEL(cctx, NULL);
5385 break;
5386 case VAR_NUMBER:
5387 case VAR_UNKNOWN:
5388 case VAR_ANY:
5389 case VAR_PARTIAL:
5390 case VAR_VOID:
5391 case VAR_SPECIAL: // cannot happen
5392 generate_PUSHNR(cctx, 0);
5393 break;
5394 }
5395 }
5396 if (var_count == 0)
5397 end = p;
5398 }
5399
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005400 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005401 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005402 break;
5403
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005404 if (oplen > 0 && *op != '=')
5405 {
5406 type_T *expected = &t_number;
5407 type_T *stacktype;
5408
5409 // TODO: if type is known use float or any operation
5410
5411 if (*op == '.')
5412 expected = &t_string;
5413 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5414 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5415 goto theend;
5416
5417 if (*op == '.')
5418 generate_instr_drop(cctx, ISN_CONCAT, 1);
5419 else
5420 {
5421 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5422
5423 if (isn == NULL)
5424 goto theend;
5425 switch (*op)
5426 {
5427 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5428 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5429 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5430 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5431 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005433 }
5434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005435
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005436 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005437 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005438 int r;
5439
5440 // Compile the "idx" in "var[idx]".
5441 if (new_local)
5442 --cctx->ctx_locals.ga_len;
5443 p = skipwhite(var_start + varlen + 1);
5444 r = compile_expr0(&p, cctx);
5445 if (new_local)
5446 ++cctx->ctx_locals.ga_len;
5447 if (r == FAIL)
5448 goto theend;
5449 if (*skipwhite(p) != ']')
5450 {
5451 emsg(_(e_missbrac));
5452 goto theend;
5453 }
5454 if (type->tt_type == VAR_DICT
5455 && may_generate_2STRING(-1, cctx) == FAIL)
5456 goto theend;
5457 if (type->tt_type == VAR_LIST
5458 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005459 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005460 {
5461 emsg(_(e_number_exp));
5462 goto theend;
5463 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005464
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005465 // Load the dict or list. On the stack we then have:
5466 // - value
5467 // - index
5468 // - variable
5469 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005470
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005471 if (type->tt_type == VAR_LIST)
5472 {
5473 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5474 return FAIL;
5475 }
5476 else if (type->tt_type == VAR_DICT)
5477 {
5478 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5479 return FAIL;
5480 }
5481 else
5482 {
5483 emsg(_(e_listreq));
5484 goto theend;
5485 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005486 }
5487 else
5488 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005489 switch (dest)
5490 {
5491 case dest_option:
5492 generate_STOREOPT(cctx, name + 1, opt_flags);
5493 break;
5494 case dest_global:
5495 // include g: with the name, easier to execute that way
5496 generate_STORE(cctx, ISN_STOREG, 0, name);
5497 break;
5498 case dest_buffer:
5499 // include b: with the name, easier to execute that way
5500 generate_STORE(cctx, ISN_STOREB, 0, name);
5501 break;
5502 case dest_window:
5503 // include w: with the name, easier to execute that way
5504 generate_STORE(cctx, ISN_STOREW, 0, name);
5505 break;
5506 case dest_tab:
5507 // include t: with the name, easier to execute that way
5508 generate_STORE(cctx, ISN_STORET, 0, name);
5509 break;
5510 case dest_env:
5511 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5512 break;
5513 case dest_reg:
5514 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5515 break;
5516 case dest_vimvar:
5517 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5518 break;
5519 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005520 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005521 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5522 imported_T *import = NULL;
5523 int sid = current_sctx.sc_sid;
5524 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005525
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005526 if (name[1] != ':')
5527 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005528 import = find_imported(name, 0, cctx);
5529 if (import != NULL)
5530 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005531 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005532
5533 idx = get_script_item_idx(sid, rawname, TRUE);
5534 // TODO: specific type
5535 if (idx < 0)
5536 {
5537 char_u *name_s = name;
5538
5539 // Include s: in the name for store_var()
5540 if (name[1] != ':')
5541 {
5542 int len = (int)STRLEN(name) + 3;
5543
5544 name_s = alloc(len);
5545 if (name_s == NULL)
5546 name_s = name;
5547 else
5548 vim_snprintf((char *)name_s, len,
5549 "s:%s", name);
5550 }
5551 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005552 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005553 if (name_s != name)
5554 vim_free(name_s);
5555 }
5556 else
5557 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005558 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005559 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005560 break;
5561 case dest_local:
5562 if (lvar != NULL)
5563 {
5564 isn_T *isn = ((isn_T *)instr->ga_data)
5565 + instr->ga_len - 1;
5566
5567 // optimization: turn "var = 123" from ISN_PUSHNR +
5568 // ISN_STORE into ISN_STORENR
5569 if (!lvar->lv_from_outer
5570 && instr->ga_len == instr_count + 1
5571 && isn->isn_type == ISN_PUSHNR)
5572 {
5573 varnumber_T val = isn->isn_arg.number;
5574
5575 isn->isn_type = ISN_STORENR;
5576 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5577 isn->isn_arg.storenr.stnr_val = val;
5578 if (stack->ga_len > 0)
5579 --stack->ga_len;
5580 }
5581 else if (lvar->lv_from_outer)
5582 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005583 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005584 else
5585 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5586 }
5587 break;
5588 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005589 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005590
5591 if (var_idx + 1 < var_count)
5592 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005593 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005594
5595 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005596 if (var_count > 0 && !semicolon)
5597 {
5598 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5599 goto theend;
5600 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005601
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005602 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005603
5604theend:
5605 vim_free(name);
5606 return ret;
5607}
5608
5609/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005610 * Check if "name" can be "unlet".
5611 */
5612 int
5613check_vim9_unlet(char_u *name)
5614{
5615 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5616 {
5617 semsg(_("E1081: Cannot unlet %s"), name);
5618 return FAIL;
5619 }
5620 return OK;
5621}
5622
5623/*
5624 * Callback passed to ex_unletlock().
5625 */
5626 static int
5627compile_unlet(
5628 lval_T *lvp,
5629 char_u *name_end,
5630 exarg_T *eap,
5631 int deep UNUSED,
5632 void *coookie)
5633{
5634 cctx_T *cctx = coookie;
5635
5636 if (lvp->ll_tv == NULL)
5637 {
5638 char_u *p = lvp->ll_name;
5639 int cc = *name_end;
5640 int ret = OK;
5641
5642 // Normal name. Only supports g:, w:, t: and b: namespaces.
5643 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005644 if (*p == '$')
5645 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5646 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005647 ret = FAIL;
5648 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005649 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005650
5651 *name_end = cc;
5652 return ret;
5653 }
5654
5655 // TODO: unlet {list}[idx]
5656 // TODO: unlet {dict}[key]
5657 emsg("Sorry, :unlet not fully implemented yet");
5658 return FAIL;
5659}
5660
5661/*
5662 * compile "unlet var", "lock var" and "unlock var"
5663 * "arg" points to "var".
5664 */
5665 static char_u *
5666compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5667{
5668 char_u *p = arg;
5669
5670 if (eap->cmdidx != CMD_unlet)
5671 {
5672 emsg("Sorry, :lock and unlock not implemented yet");
5673 return NULL;
5674 }
5675
5676 if (*p == '!')
5677 {
5678 p = skipwhite(p + 1);
5679 eap->forceit = TRUE;
5680 }
5681
5682 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5683 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5684}
5685
5686/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005687 * Compile an :import command.
5688 */
5689 static char_u *
5690compile_import(char_u *arg, cctx_T *cctx)
5691{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005692 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005693}
5694
5695/*
5696 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5697 */
5698 static int
5699compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5700{
5701 garray_T *instr = &cctx->ctx_instr;
5702 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5703
5704 if (endlabel == NULL)
5705 return FAIL;
5706 endlabel->el_next = *el;
5707 *el = endlabel;
5708 endlabel->el_end_label = instr->ga_len;
5709
5710 generate_JUMP(cctx, when, 0);
5711 return OK;
5712}
5713
5714 static void
5715compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5716{
5717 garray_T *instr = &cctx->ctx_instr;
5718
5719 while (*el != NULL)
5720 {
5721 endlabel_T *cur = (*el);
5722 isn_T *isn;
5723
5724 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5725 isn->isn_arg.jump.jump_where = instr->ga_len;
5726 *el = cur->el_next;
5727 vim_free(cur);
5728 }
5729}
5730
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005731 static void
5732compile_free_jump_to_end(endlabel_T **el)
5733{
5734 while (*el != NULL)
5735 {
5736 endlabel_T *cur = (*el);
5737
5738 *el = cur->el_next;
5739 vim_free(cur);
5740 }
5741}
5742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005743/*
5744 * Create a new scope and set up the generic items.
5745 */
5746 static scope_T *
5747new_scope(cctx_T *cctx, scopetype_T type)
5748{
5749 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5750
5751 if (scope == NULL)
5752 return NULL;
5753 scope->se_outer = cctx->ctx_scope;
5754 cctx->ctx_scope = scope;
5755 scope->se_type = type;
5756 scope->se_local_count = cctx->ctx_locals.ga_len;
5757 return scope;
5758}
5759
5760/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005761 * Free the current scope and go back to the outer scope.
5762 */
5763 static void
5764drop_scope(cctx_T *cctx)
5765{
5766 scope_T *scope = cctx->ctx_scope;
5767
5768 if (scope == NULL)
5769 {
5770 iemsg("calling drop_scope() without a scope");
5771 return;
5772 }
5773 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005774 switch (scope->se_type)
5775 {
5776 case IF_SCOPE:
5777 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5778 case FOR_SCOPE:
5779 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5780 case WHILE_SCOPE:
5781 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5782 case TRY_SCOPE:
5783 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5784 case NO_SCOPE:
5785 case BLOCK_SCOPE:
5786 break;
5787 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005788 vim_free(scope);
5789}
5790
5791/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005792 * compile "if expr"
5793 *
5794 * "if expr" Produces instructions:
5795 * EVAL expr Push result of "expr"
5796 * JUMP_IF_FALSE end
5797 * ... body ...
5798 * end:
5799 *
5800 * "if expr | else" Produces instructions:
5801 * EVAL expr Push result of "expr"
5802 * JUMP_IF_FALSE else
5803 * ... body ...
5804 * JUMP_ALWAYS end
5805 * else:
5806 * ... body ...
5807 * end:
5808 *
5809 * "if expr1 | elseif expr2 | else" Produces instructions:
5810 * EVAL expr Push result of "expr"
5811 * JUMP_IF_FALSE elseif
5812 * ... body ...
5813 * JUMP_ALWAYS end
5814 * elseif:
5815 * EVAL expr Push result of "expr"
5816 * JUMP_IF_FALSE else
5817 * ... body ...
5818 * JUMP_ALWAYS end
5819 * else:
5820 * ... body ...
5821 * end:
5822 */
5823 static char_u *
5824compile_if(char_u *arg, cctx_T *cctx)
5825{
5826 char_u *p = arg;
5827 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005828 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005829 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005830 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005831 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005832
Bram Moolenaara5565e42020-05-09 15:44:01 +02005833 CLEAR_FIELD(ppconst);
5834 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005835 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005836 clear_ppconst(&ppconst);
5837 return NULL;
5838 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005839 if (cctx->ctx_skip == SKIP_YES)
5840 clear_ppconst(&ppconst);
5841 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005842 {
5843 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005844 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005845 clear_ppconst(&ppconst);
5846 }
5847 else
5848 {
5849 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005850 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005851 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005852 return NULL;
5853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005854
5855 scope = new_scope(cctx, IF_SCOPE);
5856 if (scope == NULL)
5857 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005858 scope->se_skip_save = skip_save;
5859 // "is_had_return" will be reset if any block does not end in :return
5860 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005862 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005863 {
5864 // "where" is set when ":elseif", "else" or ":endif" is found
5865 scope->se_u.se_if.is_if_label = instr->ga_len;
5866 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5867 }
5868 else
5869 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870
5871 return p;
5872}
5873
5874 static char_u *
5875compile_elseif(char_u *arg, cctx_T *cctx)
5876{
5877 char_u *p = arg;
5878 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005879 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005880 isn_T *isn;
5881 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005882 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883
5884 if (scope == NULL || scope->se_type != IF_SCOPE)
5885 {
5886 emsg(_(e_elseif_without_if));
5887 return NULL;
5888 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005889 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005890 if (!cctx->ctx_had_return)
5891 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005893 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005894 {
5895 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005897 return NULL;
5898 // previous "if" or "elseif" jumps here
5899 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5900 isn->isn_arg.jump.jump_where = instr->ga_len;
5901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005903 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005904 CLEAR_FIELD(ppconst);
5905 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005906 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005907 clear_ppconst(&ppconst);
5908 return NULL;
5909 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005910 if (scope->se_skip_save == SKIP_YES)
5911 clear_ppconst(&ppconst);
5912 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005913 {
5914 // The expression results in a constant.
5915 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005916 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005917 clear_ppconst(&ppconst);
5918 scope->se_u.se_if.is_if_label = -1;
5919 }
5920 else
5921 {
5922 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005923 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005924 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005925 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005926
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005927 // "where" is set when ":elseif", "else" or ":endif" is found
5928 scope->se_u.se_if.is_if_label = instr->ga_len;
5929 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005931
5932 return p;
5933}
5934
5935 static char_u *
5936compile_else(char_u *arg, cctx_T *cctx)
5937{
5938 char_u *p = arg;
5939 garray_T *instr = &cctx->ctx_instr;
5940 isn_T *isn;
5941 scope_T *scope = cctx->ctx_scope;
5942
5943 if (scope == NULL || scope->se_type != IF_SCOPE)
5944 {
5945 emsg(_(e_else_without_if));
5946 return NULL;
5947 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005948 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005949 if (!cctx->ctx_had_return)
5950 scope->se_u.se_if.is_had_return = FALSE;
5951 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005952
Bram Moolenaarefd88552020-06-18 20:50:10 +02005953 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005954 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005955 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005956 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005957 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005958 if (!cctx->ctx_had_return
5959 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5960 JUMP_ALWAYS, cctx) == FAIL)
5961 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005962 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005963
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005964 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005965 {
5966 if (scope->se_u.se_if.is_if_label >= 0)
5967 {
5968 // previous "if" or "elseif" jumps here
5969 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5970 isn->isn_arg.jump.jump_where = instr->ga_len;
5971 scope->se_u.se_if.is_if_label = -1;
5972 }
5973 }
5974
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005975 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005976 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005978
5979 return p;
5980}
5981
5982 static char_u *
5983compile_endif(char_u *arg, cctx_T *cctx)
5984{
5985 scope_T *scope = cctx->ctx_scope;
5986 ifscope_T *ifscope;
5987 garray_T *instr = &cctx->ctx_instr;
5988 isn_T *isn;
5989
5990 if (scope == NULL || scope->se_type != IF_SCOPE)
5991 {
5992 emsg(_(e_endif_without_if));
5993 return NULL;
5994 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005995 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005996 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005997 if (!cctx->ctx_had_return)
5998 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006000 if (scope->se_u.se_if.is_if_label >= 0)
6001 {
6002 // previous "if" or "elseif" jumps here
6003 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6004 isn->isn_arg.jump.jump_where = instr->ga_len;
6005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006006 // Fill in the "end" label in jumps at the end of the blocks.
6007 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006008 cctx->ctx_skip = scope->se_skip_save;
6009
6010 // If all the blocks end in :return and there is an :else then the
6011 // had_return flag is set.
6012 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006013
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006014 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 return arg;
6016}
6017
6018/*
6019 * compile "for var in expr"
6020 *
6021 * Produces instructions:
6022 * PUSHNR -1
6023 * STORE loop-idx Set index to -1
6024 * EVAL expr Push result of "expr"
6025 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6026 * - if beyond end, jump to "end"
6027 * - otherwise get item from list and push it
6028 * STORE var Store item in "var"
6029 * ... body ...
6030 * JUMP top Jump back to repeat
6031 * end: DROP Drop the result of "expr"
6032 *
6033 */
6034 static char_u *
6035compile_for(char_u *arg, cctx_T *cctx)
6036{
6037 char_u *p;
6038 size_t varlen;
6039 garray_T *instr = &cctx->ctx_instr;
6040 garray_T *stack = &cctx->ctx_type_stack;
6041 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006042 lvar_T *loop_lvar; // loop iteration variable
6043 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006044 type_T *vartype;
6045
6046 // TODO: list of variables: "for [key, value] in dict"
6047 // parse "var"
6048 for (p = arg; eval_isnamec1(*p); ++p)
6049 ;
6050 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006051 var_lvar = lookup_local(arg, varlen, cctx);
6052 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053 {
6054 semsg(_("E1023: variable already defined: %s"), arg);
6055 return NULL;
6056 }
6057
6058 // consume "in"
6059 p = skipwhite(p);
6060 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6061 {
6062 emsg(_(e_missing_in));
6063 return NULL;
6064 }
6065 p = skipwhite(p + 2);
6066
6067
6068 scope = new_scope(cctx, FOR_SCOPE);
6069 if (scope == NULL)
6070 return NULL;
6071
6072 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006073 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6074 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006075 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006076 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006077 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006078 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006079 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080
6081 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006082 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6083 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006084 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006085 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006086 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006087 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006089
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006090 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006091
6092 // compile "expr", it remains on the stack until "endfor"
6093 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006094 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006095 {
6096 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006097 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006099
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006100 // Now that we know the type of "var", check that it is a list, now or at
6101 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006103 if (need_type(vartype, &t_list_any, -1, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006104 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006105 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106 return NULL;
6107 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006108 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006109 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006110
6111 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006112 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006114 generate_FOR(cctx, loop_lvar->lv_idx);
6115 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116
6117 return arg;
6118}
6119
6120/*
6121 * compile "endfor"
6122 */
6123 static char_u *
6124compile_endfor(char_u *arg, cctx_T *cctx)
6125{
6126 garray_T *instr = &cctx->ctx_instr;
6127 scope_T *scope = cctx->ctx_scope;
6128 forscope_T *forscope;
6129 isn_T *isn;
6130
6131 if (scope == NULL || scope->se_type != FOR_SCOPE)
6132 {
6133 emsg(_(e_for));
6134 return NULL;
6135 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006136 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006138 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139
6140 // At end of ":for" scope jump back to the FOR instruction.
6141 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6142
6143 // Fill in the "end" label in the FOR statement so it can jump here
6144 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6145 isn->isn_arg.forloop.for_end = instr->ga_len;
6146
6147 // Fill in the "end" label any BREAK statements
6148 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6149
6150 // Below the ":for" scope drop the "expr" list from the stack.
6151 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6152 return NULL;
6153
6154 vim_free(scope);
6155
6156 return arg;
6157}
6158
6159/*
6160 * compile "while expr"
6161 *
6162 * Produces instructions:
6163 * top: EVAL expr Push result of "expr"
6164 * JUMP_IF_FALSE end jump if false
6165 * ... body ...
6166 * JUMP top Jump back to repeat
6167 * end:
6168 *
6169 */
6170 static char_u *
6171compile_while(char_u *arg, cctx_T *cctx)
6172{
6173 char_u *p = arg;
6174 garray_T *instr = &cctx->ctx_instr;
6175 scope_T *scope;
6176
6177 scope = new_scope(cctx, WHILE_SCOPE);
6178 if (scope == NULL)
6179 return NULL;
6180
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006181 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006182
6183 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006184 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185 return NULL;
6186
6187 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006188 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006189 JUMP_IF_FALSE, cctx) == FAIL)
6190 return FAIL;
6191
6192 return p;
6193}
6194
6195/*
6196 * compile "endwhile"
6197 */
6198 static char_u *
6199compile_endwhile(char_u *arg, cctx_T *cctx)
6200{
6201 scope_T *scope = cctx->ctx_scope;
6202
6203 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6204 {
6205 emsg(_(e_while));
6206 return NULL;
6207 }
6208 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006209 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006210
6211 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006212 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006213
6214 // Fill in the "end" label in the WHILE statement so it can jump here.
6215 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006216 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217
6218 vim_free(scope);
6219
6220 return arg;
6221}
6222
6223/*
6224 * compile "continue"
6225 */
6226 static char_u *
6227compile_continue(char_u *arg, cctx_T *cctx)
6228{
6229 scope_T *scope = cctx->ctx_scope;
6230
6231 for (;;)
6232 {
6233 if (scope == NULL)
6234 {
6235 emsg(_(e_continue));
6236 return NULL;
6237 }
6238 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6239 break;
6240 scope = scope->se_outer;
6241 }
6242
6243 // Jump back to the FOR or WHILE instruction.
6244 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006245 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6246 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247 return arg;
6248}
6249
6250/*
6251 * compile "break"
6252 */
6253 static char_u *
6254compile_break(char_u *arg, cctx_T *cctx)
6255{
6256 scope_T *scope = cctx->ctx_scope;
6257 endlabel_T **el;
6258
6259 for (;;)
6260 {
6261 if (scope == NULL)
6262 {
6263 emsg(_(e_break));
6264 return NULL;
6265 }
6266 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6267 break;
6268 scope = scope->se_outer;
6269 }
6270
6271 // Jump to the end of the FOR or WHILE loop.
6272 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006273 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006275 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006276 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6277 return FAIL;
6278
6279 return arg;
6280}
6281
6282/*
6283 * compile "{" start of block
6284 */
6285 static char_u *
6286compile_block(char_u *arg, cctx_T *cctx)
6287{
6288 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6289 return NULL;
6290 return skipwhite(arg + 1);
6291}
6292
6293/*
6294 * compile end of block: drop one scope
6295 */
6296 static void
6297compile_endblock(cctx_T *cctx)
6298{
6299 scope_T *scope = cctx->ctx_scope;
6300
6301 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006302 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 vim_free(scope);
6304}
6305
6306/*
6307 * compile "try"
6308 * Creates a new scope for the try-endtry, pointing to the first catch and
6309 * finally.
6310 * Creates another scope for the "try" block itself.
6311 * TRY instruction sets up exception handling at runtime.
6312 *
6313 * "try"
6314 * TRY -> catch1, -> finally push trystack entry
6315 * ... try block
6316 * "throw {exception}"
6317 * EVAL {exception}
6318 * THROW create exception
6319 * ... try block
6320 * " catch {expr}"
6321 * JUMP -> finally
6322 * catch1: PUSH exeception
6323 * EVAL {expr}
6324 * MATCH
6325 * JUMP nomatch -> catch2
6326 * CATCH remove exception
6327 * ... catch block
6328 * " catch"
6329 * JUMP -> finally
6330 * catch2: CATCH remove exception
6331 * ... catch block
6332 * " finally"
6333 * finally:
6334 * ... finally block
6335 * " endtry"
6336 * ENDTRY pop trystack entry, may rethrow
6337 */
6338 static char_u *
6339compile_try(char_u *arg, cctx_T *cctx)
6340{
6341 garray_T *instr = &cctx->ctx_instr;
6342 scope_T *try_scope;
6343 scope_T *scope;
6344
6345 // scope that holds the jumps that go to catch/finally/endtry
6346 try_scope = new_scope(cctx, TRY_SCOPE);
6347 if (try_scope == NULL)
6348 return NULL;
6349
6350 // "catch" is set when the first ":catch" is found.
6351 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006352 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 if (generate_instr(cctx, ISN_TRY) == NULL)
6354 return NULL;
6355
6356 // scope for the try block itself
6357 scope = new_scope(cctx, BLOCK_SCOPE);
6358 if (scope == NULL)
6359 return NULL;
6360
6361 return arg;
6362}
6363
6364/*
6365 * compile "catch {expr}"
6366 */
6367 static char_u *
6368compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6369{
6370 scope_T *scope = cctx->ctx_scope;
6371 garray_T *instr = &cctx->ctx_instr;
6372 char_u *p;
6373 isn_T *isn;
6374
6375 // end block scope from :try or :catch
6376 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6377 compile_endblock(cctx);
6378 scope = cctx->ctx_scope;
6379
6380 // Error if not in a :try scope
6381 if (scope == NULL || scope->se_type != TRY_SCOPE)
6382 {
6383 emsg(_(e_catch));
6384 return NULL;
6385 }
6386
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006387 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006388 {
6389 emsg(_("E1033: catch unreachable after catch-all"));
6390 return NULL;
6391 }
6392
6393 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006394 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006395 JUMP_ALWAYS, cctx) == FAIL)
6396 return NULL;
6397
6398 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006399 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 if (isn->isn_arg.try.try_catch == 0)
6401 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006402 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403 {
6404 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006405 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406 isn->isn_arg.jump.jump_where = instr->ga_len;
6407 }
6408
6409 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006410 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006411 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006412 scope->se_u.se_try.ts_caught_all = TRUE;
6413 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006414 }
6415 else
6416 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006417 char_u *end;
6418 char_u *pat;
6419 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006420 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006421 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423 // Push v:exception, push {expr} and MATCH
6424 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6425
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006426 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006427 if (*end != *p)
6428 {
6429 semsg(_("E1067: Separator mismatch: %s"), p);
6430 vim_free(tofree);
6431 return FAIL;
6432 }
6433 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006434 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006435 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006436 len = (int)(end - tofree);
6437 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006438 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006439 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006440 if (pat == NULL)
6441 return FAIL;
6442 if (generate_PUSHS(cctx, pat) == FAIL)
6443 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006445 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6446 return NULL;
6447
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006448 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6450 return NULL;
6451 }
6452
6453 if (generate_instr(cctx, ISN_CATCH) == NULL)
6454 return NULL;
6455
6456 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6457 return NULL;
6458 return p;
6459}
6460
6461 static char_u *
6462compile_finally(char_u *arg, cctx_T *cctx)
6463{
6464 scope_T *scope = cctx->ctx_scope;
6465 garray_T *instr = &cctx->ctx_instr;
6466 isn_T *isn;
6467
6468 // end block scope from :try or :catch
6469 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6470 compile_endblock(cctx);
6471 scope = cctx->ctx_scope;
6472
6473 // Error if not in a :try scope
6474 if (scope == NULL || scope->se_type != TRY_SCOPE)
6475 {
6476 emsg(_(e_finally));
6477 return NULL;
6478 }
6479
6480 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006481 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482 if (isn->isn_arg.try.try_finally != 0)
6483 {
6484 emsg(_(e_finally_dup));
6485 return NULL;
6486 }
6487
6488 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006489 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006490
Bram Moolenaar585fea72020-04-02 22:33:21 +02006491 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006492 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006493 {
6494 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006495 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006496 isn->isn_arg.jump.jump_where = instr->ga_len;
6497 }
6498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006499 // TODO: set index in ts_finally_label jumps
6500
6501 return arg;
6502}
6503
6504 static char_u *
6505compile_endtry(char_u *arg, cctx_T *cctx)
6506{
6507 scope_T *scope = cctx->ctx_scope;
6508 garray_T *instr = &cctx->ctx_instr;
6509 isn_T *isn;
6510
6511 // end block scope from :catch or :finally
6512 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6513 compile_endblock(cctx);
6514 scope = cctx->ctx_scope;
6515
6516 // Error if not in a :try scope
6517 if (scope == NULL || scope->se_type != TRY_SCOPE)
6518 {
6519 if (scope == NULL)
6520 emsg(_(e_no_endtry));
6521 else if (scope->se_type == WHILE_SCOPE)
6522 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006523 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524 emsg(_(e_endfor));
6525 else
6526 emsg(_(e_endif));
6527 return NULL;
6528 }
6529
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006530 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006531 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6532 {
6533 emsg(_("E1032: missing :catch or :finally"));
6534 return NULL;
6535 }
6536
6537 // Fill in the "end" label in jumps at the end of the blocks, if not done
6538 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006539 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540
6541 // End :catch or :finally scope: set value in ISN_TRY instruction
6542 if (isn->isn_arg.try.try_finally == 0)
6543 isn->isn_arg.try.try_finally = instr->ga_len;
6544 compile_endblock(cctx);
6545
6546 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6547 return NULL;
6548 return arg;
6549}
6550
6551/*
6552 * compile "throw {expr}"
6553 */
6554 static char_u *
6555compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6556{
6557 char_u *p = skipwhite(arg);
6558
Bram Moolenaara5565e42020-05-09 15:44:01 +02006559 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560 return NULL;
6561 if (may_generate_2STRING(-1, cctx) == FAIL)
6562 return NULL;
6563 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6564 return NULL;
6565
6566 return p;
6567}
6568
6569/*
6570 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006571 * compile "echomsg expr"
6572 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006573 * compile "execute expr"
6574 */
6575 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006576compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006577{
6578 char_u *p = arg;
6579 int count = 0;
6580
6581 for (;;)
6582 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006583 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006584 return NULL;
6585 ++count;
6586 p = skipwhite(p);
6587 if (ends_excmd(*p))
6588 break;
6589 }
6590
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006591 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6592 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6593 else if (cmdidx == CMD_execute)
6594 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6595 else if (cmdidx == CMD_echomsg)
6596 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6597 else
6598 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 return p;
6600}
6601
6602/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006603 * A command that is not compiled, execute with legacy code.
6604 */
6605 static char_u *
6606compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6607{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006608 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006609 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006610 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006611
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006612 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006613 goto theend;
6614
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006615 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006616 {
6617 long argt = excmd_get_argt(eap->cmdidx);
6618 int usefilter = FALSE;
6619
6620 has_expr = argt & (EX_XFILE | EX_EXPAND);
6621
6622 // If the command can be followed by a bar, find the bar and truncate
6623 // it, so that the following command can be compiled.
6624 // The '|' is overwritten with a NUL, it is put back below.
6625 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6626 && *eap->arg == '!')
6627 // :w !filter or :r !filter or :r! filter
6628 usefilter = TRUE;
6629 if ((argt & EX_TRLBAR) && !usefilter)
6630 {
6631 separate_nextcmd(eap);
6632 if (eap->nextcmd != NULL)
6633 nextcmd = eap->nextcmd;
6634 }
6635 }
6636
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006637 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6638 {
6639 // expand filename in "syntax include [@group] filename"
6640 has_expr = TRUE;
6641 eap->arg = skipwhite(eap->arg + 7);
6642 if (*eap->arg == '@')
6643 eap->arg = skiptowhite(eap->arg);
6644 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006645
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006646 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006647 {
6648 int count = 0;
6649 char_u *start = skipwhite(line);
6650
6651 // :cmd xxx`=expr1`yyy`=expr2`zzz
6652 // PUSHS ":cmd xxx"
6653 // eval expr1
6654 // PUSHS "yyy"
6655 // eval expr2
6656 // PUSHS "zzz"
6657 // EXECCONCAT 5
6658 for (;;)
6659 {
6660 if (p > start)
6661 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006662 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006663 ++count;
6664 }
6665 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006666 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006667 return NULL;
6668 may_generate_2STRING(-1, cctx);
6669 ++count;
6670 p = skipwhite(p);
6671 if (*p != '`')
6672 {
6673 emsg(_("E1083: missing backtick"));
6674 return NULL;
6675 }
6676 start = p + 1;
6677
6678 p = (char_u *)strstr((char *)start, "`=");
6679 if (p == NULL)
6680 {
6681 if (*skipwhite(start) != NUL)
6682 {
6683 generate_PUSHS(cctx, vim_strsave(start));
6684 ++count;
6685 }
6686 break;
6687 }
6688 }
6689 generate_EXECCONCAT(cctx, count);
6690 }
6691 else
6692 generate_EXEC(cctx, line);
6693
6694theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006695 if (*nextcmd != NUL)
6696 {
6697 // the parser expects a pointer to the bar, put it back
6698 --nextcmd;
6699 *nextcmd = '|';
6700 }
6701
6702 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006703}
6704
6705/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006706 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006707 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006708 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006709 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006710add_def_function(ufunc_T *ufunc)
6711{
6712 dfunc_T *dfunc;
6713
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006714 if (def_functions.ga_len == 0)
6715 {
6716 // The first position is not used, so that a zero uf_dfunc_idx means it
6717 // wasn't set.
6718 if (ga_grow(&def_functions, 1) == FAIL)
6719 return FAIL;
6720 ++def_functions.ga_len;
6721 }
6722
Bram Moolenaar09689a02020-05-09 22:50:08 +02006723 // Add the function to "def_functions".
6724 if (ga_grow(&def_functions, 1) == FAIL)
6725 return FAIL;
6726 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6727 CLEAR_POINTER(dfunc);
6728 dfunc->df_idx = def_functions.ga_len;
6729 ufunc->uf_dfunc_idx = dfunc->df_idx;
6730 dfunc->df_ufunc = ufunc;
6731 ++def_functions.ga_len;
6732 return OK;
6733}
6734
6735/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006736 * After ex_function() has collected all the function lines: parse and compile
6737 * the lines into instructions.
6738 * Adds the function to "def_functions".
6739 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6740 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006741 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006742 * This can be used recursively through compile_lambda(), which may reallocate
6743 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006744 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006746 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006747compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006748{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749 char_u *line = NULL;
6750 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 cctx_T cctx;
6753 garray_T *instr;
6754 int called_emsg_before = called_emsg;
6755 int ret = FAIL;
6756 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006757 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006758 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006759
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006760 // When using a function that was compiled before: Free old instructions.
6761 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006762 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006763 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006764 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6765 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006766 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006768 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006769 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770
Bram Moolenaara80faa82020-04-12 19:37:17 +02006771 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 cctx.ctx_ufunc = ufunc;
6773 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006774 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6776 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6777 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6778 cctx.ctx_type_list = &ufunc->uf_type_list;
6779 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6780 instr = &cctx.ctx_instr;
6781
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006782 // Set the context to the function, it may be compiled when called from
6783 // another script. Set the script version to the most modern one.
6784 // The line number will be set in next_line_from_context().
6785 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6787
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006788 // Make sure error messages are OK.
6789 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6790 if (do_estack_push)
6791 estack_push_ufunc(ufunc, 1);
6792
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006793 if (ufunc->uf_def_args.ga_len > 0)
6794 {
6795 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006796 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006797 int i;
6798 char_u *arg;
6799 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6800
6801 // Produce instructions for the default values of optional arguments.
6802 // Store the instruction index in uf_def_arg_idx[] so that we know
6803 // where to start when the function is called, depending on the number
6804 // of arguments.
6805 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6806 if (ufunc->uf_def_arg_idx == NULL)
6807 goto erret;
6808 for (i = 0; i < count; ++i)
6809 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006810 garray_T *stack = &cctx.ctx_type_stack;
6811 type_T *val_type;
6812 int arg_idx = first_def_arg + i;
6813
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006814 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6815 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006816 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006817 goto erret;
6818
6819 // If no type specified use the type of the default value.
6820 // Otherwise check that the default value type matches the
6821 // specified type.
6822 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6823 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6824 ufunc->uf_arg_types[arg_idx] = val_type;
6825 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6826 == FAIL)
6827 {
6828 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6829 arg_idx + 1);
6830 goto erret;
6831 }
6832
6833 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006834 goto erret;
6835 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006836 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6837 }
6838
6839 /*
6840 * Loop over all the lines of the function and generate instructions.
6841 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006842 for (;;)
6843 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006844 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006845 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006846 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006847 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006848
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006849 // Bail out on the first error to avoid a flood of errors and report
6850 // the right line number when inside try/catch.
6851 if (emsg_before != called_emsg)
6852 goto erret;
6853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006854 if (line != NULL && *line == '|')
6855 // the line continues after a '|'
6856 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006857 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006858 && !(*line == '#' && (line == cctx.ctx_line_start
6859 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006861 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006862 goto erret;
6863 }
6864 else
6865 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006866 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006867 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006868 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006871 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006872
Bram Moolenaara80faa82020-04-12 19:37:17 +02006873 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874 ea.cmdlinep = &line;
6875 ea.cmd = skipwhite(line);
6876
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006877 // Some things can be recognized by the first character.
6878 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006880 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006881 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006882 if (ea.cmd[1] != '{')
6883 {
6884 line = (char_u *)"";
6885 continue;
6886 }
6887 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006888
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006889 case '}':
6890 {
6891 // "}" ends a block scope
6892 scopetype_T stype = cctx.ctx_scope == NULL
6893 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006894
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006895 if (stype == BLOCK_SCOPE)
6896 {
6897 compile_endblock(&cctx);
6898 line = ea.cmd;
6899 }
6900 else
6901 {
6902 emsg(_("E1025: using } outside of a block scope"));
6903 goto erret;
6904 }
6905 if (line != NULL)
6906 line = skipwhite(ea.cmd + 1);
6907 continue;
6908 }
6909
6910 case '{':
6911 // "{" starts a block scope
6912 // "{'a': 1}->func() is something else
6913 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6914 {
6915 line = compile_block(ea.cmd, &cctx);
6916 continue;
6917 }
6918 break;
6919
6920 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006921 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006922 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923 }
6924
6925 /*
6926 * COMMAND MODIFIERS
6927 */
6928 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6929 {
6930 if (errormsg != NULL)
6931 goto erret;
6932 // empty line or comment
6933 line = (char_u *)"";
6934 continue;
6935 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006936 // TODO: use modifiers in the command
6937 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938
6939 // Skip ":call" to get to the function name.
6940 if (checkforcmd(&ea.cmd, "call", 3))
6941 ea.cmd = skipwhite(ea.cmd);
6942
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006943 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006945 char_u *pskip;
6946
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006947 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006948 // find what follows.
6949 // Skip over "var.member", "var[idx]" and the like.
6950 // Also "&opt = val", "$ENV = val" and "@r = val".
6951 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006952 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006953 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006954 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006956 char_u *var_end;
6957 int oplen;
6958 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006960 var_end = find_name_end(pskip, NULL, NULL,
6961 FNE_CHECK_START | FNE_INCL_BR);
6962 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006963 if (oplen > 0)
6964 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006965 size_t len = p - ea.cmd;
6966
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006967 // Recognize an assignment if we recognize the variable
6968 // name:
6969 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006970 // "local = expr" where "local" is a local var.
6971 // "script = expr" where "script" is a script-local var.
6972 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006973 // "&opt = expr"
6974 // "$ENV = expr"
6975 // "@r = expr"
6976 if (*ea.cmd == '&'
6977 || *ea.cmd == '$'
6978 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006979 || ((len) > 2 && ea.cmd[1] == ':')
6980 || lookup_local(ea.cmd, len, &cctx) != NULL
6981 || lookup_arg(ea.cmd, len, NULL, NULL,
6982 NULL, &cctx) == OK
6983 || lookup_script(ea.cmd, len) == OK
6984 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006985 {
6986 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006987 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006988 goto erret;
6989 continue;
6990 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991 }
6992 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006993
6994 if (*ea.cmd == '[')
6995 {
6996 // [var, var] = expr
6997 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6998 if (line == NULL)
6999 goto erret;
7000 if (line != ea.cmd)
7001 continue;
7002 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003 }
7004
7005 /*
7006 * COMMAND after range
7007 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007008 cmd = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007010 if (ea.cmd > cmd && !starts_with_colon)
7011 {
7012 emsg(_(e_colon_required));
7013 goto erret;
7014 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007015 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007016 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007017 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018
7019 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7020 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007021 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007022 {
7023 line += STRLEN(line);
7024 continue;
7025 }
7026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007028 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007030 // CMD_let cannot happen, compile_assignment() above is used
7031 iemsg("Command from find_ex_command() not handled");
7032 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034 }
7035
7036 p = skipwhite(p);
7037
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007038 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007039 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007040 && ea.cmdidx != CMD_elseif
7041 && ea.cmdidx != CMD_else
7042 && ea.cmdidx != CMD_endif)
7043 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007044 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007045 continue;
7046 }
7047
Bram Moolenaarefd88552020-06-18 20:50:10 +02007048 if (ea.cmdidx != CMD_elseif
7049 && ea.cmdidx != CMD_else
7050 && ea.cmdidx != CMD_endif
7051 && ea.cmdidx != CMD_endfor
7052 && ea.cmdidx != CMD_endwhile
7053 && ea.cmdidx != CMD_catch
7054 && ea.cmdidx != CMD_finally
7055 && ea.cmdidx != CMD_endtry)
7056 {
7057 if (cctx.ctx_had_return)
7058 {
7059 emsg(_("E1095: Unreachable code after :return"));
7060 goto erret;
7061 }
7062 }
7063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 switch (ea.cmdidx)
7065 {
7066 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007067 ea.arg = p;
7068 line = compile_nested_function(&ea, &cctx);
7069 break;
7070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007072 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073 goto erret;
7074
7075 case CMD_return:
7076 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007077 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078 break;
7079
7080 case CMD_let:
7081 case CMD_const:
7082 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007083 if (line == p)
7084 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007085 break;
7086
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007087 case CMD_unlet:
7088 case CMD_unlockvar:
7089 case CMD_lockvar:
7090 line = compile_unletlock(p, &ea, &cctx);
7091 break;
7092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093 case CMD_import:
7094 line = compile_import(p, &cctx);
7095 break;
7096
7097 case CMD_if:
7098 line = compile_if(p, &cctx);
7099 break;
7100 case CMD_elseif:
7101 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007102 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 break;
7104 case CMD_else:
7105 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007106 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 break;
7108 case CMD_endif:
7109 line = compile_endif(p, &cctx);
7110 break;
7111
7112 case CMD_while:
7113 line = compile_while(p, &cctx);
7114 break;
7115 case CMD_endwhile:
7116 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007117 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 break;
7119
7120 case CMD_for:
7121 line = compile_for(p, &cctx);
7122 break;
7123 case CMD_endfor:
7124 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007125 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126 break;
7127 case CMD_continue:
7128 line = compile_continue(p, &cctx);
7129 break;
7130 case CMD_break:
7131 line = compile_break(p, &cctx);
7132 break;
7133
7134 case CMD_try:
7135 line = compile_try(p, &cctx);
7136 break;
7137 case CMD_catch:
7138 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007139 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007140 break;
7141 case CMD_finally:
7142 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007143 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 break;
7145 case CMD_endtry:
7146 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007147 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007148 break;
7149 case CMD_throw:
7150 line = compile_throw(p, &cctx);
7151 break;
7152
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007153 case CMD_eval:
7154 if (compile_expr0(&p, &cctx) == FAIL)
7155 goto erret;
7156
7157 // drop the return value
7158 generate_instr_drop(&cctx, ISN_DROP, 1);
7159
7160 line = skipwhite(p);
7161 break;
7162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007165 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007166 case CMD_echomsg:
7167 case CMD_echoerr:
7168 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007169 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007170
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007171 // TODO: other commands with an expression argument
7172
Bram Moolenaar002262f2020-07-08 17:47:57 +02007173 case CMD_SIZE:
7174 semsg(_("E476: Invalid command: %s"), ea.cmd);
7175 goto erret;
7176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007178 // Not recognized, execute with do_cmdline_cmd().
7179 ea.arg = p;
7180 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007181 break;
7182 }
7183 if (line == NULL)
7184 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007185 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186
7187 if (cctx.ctx_type_stack.ga_len < 0)
7188 {
7189 iemsg("Type stack underflow");
7190 goto erret;
7191 }
7192 }
7193
7194 if (cctx.ctx_scope != NULL)
7195 {
7196 if (cctx.ctx_scope->se_type == IF_SCOPE)
7197 emsg(_(e_endif));
7198 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7199 emsg(_(e_endwhile));
7200 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7201 emsg(_(e_endfor));
7202 else
7203 emsg(_("E1026: Missing }"));
7204 goto erret;
7205 }
7206
Bram Moolenaarefd88552020-06-18 20:50:10 +02007207 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007208 {
7209 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7210 {
7211 emsg(_("E1027: Missing return statement"));
7212 goto erret;
7213 }
7214
7215 // Return zero if there is no return at the end.
7216 generate_PUSHNR(&cctx, 0);
7217 generate_instr(&cctx, ISN_RETURN);
7218 }
7219
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007220 {
7221 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7222 + ufunc->uf_dfunc_idx;
7223 dfunc->df_deleted = FALSE;
7224 dfunc->df_instr = instr->ga_data;
7225 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007226 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007227 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007228 if (cctx.ctx_outer_used)
7229 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007230 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232
7233 ret = OK;
7234
7235erret:
7236 if (ret == FAIL)
7237 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007238 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007239 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7240 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007241
7242 for (idx = 0; idx < instr->ga_len; ++idx)
7243 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007245
Bram Moolenaar45a15082020-05-25 00:28:33 +02007246 // if using the last entry in the table we might as well remove it
7247 if (!dfunc->df_deleted
7248 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007249 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007250 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007251
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007252 while (cctx.ctx_scope != NULL)
7253 drop_scope(&cctx);
7254
Bram Moolenaar20431c92020-03-20 18:39:46 +01007255 // Don't execute this function body.
7256 ga_clear_strings(&ufunc->uf_lines);
7257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 if (errormsg != NULL)
7259 emsg(errormsg);
7260 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007261 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007262 }
7263
7264 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007265 if (do_estack_push)
7266 estack_pop();
7267
Bram Moolenaar20431c92020-03-20 18:39:46 +01007268 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007269 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007270 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007271 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007272}
7273
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007274 void
7275set_function_type(ufunc_T *ufunc)
7276{
7277 int varargs = ufunc->uf_va_name != NULL;
7278 int argcount = ufunc->uf_args.ga_len;
7279
7280 // Create a type for the function, with the return type and any
7281 // argument types.
7282 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7283 // The type is included in "tt_args".
7284 if (argcount > 0 || varargs)
7285 {
7286 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7287 argcount, &ufunc->uf_type_list);
7288 // Add argument types to the function type.
7289 if (func_type_add_arg_types(ufunc->uf_func_type,
7290 argcount + varargs,
7291 &ufunc->uf_type_list) == FAIL)
7292 return;
7293 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7294 ufunc->uf_func_type->tt_min_argcount =
7295 argcount - ufunc->uf_def_args.ga_len;
7296 if (ufunc->uf_arg_types == NULL)
7297 {
7298 int i;
7299
7300 // lambda does not have argument types.
7301 for (i = 0; i < argcount; ++i)
7302 ufunc->uf_func_type->tt_args[i] = &t_any;
7303 }
7304 else
7305 mch_memmove(ufunc->uf_func_type->tt_args,
7306 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7307 if (varargs)
7308 {
7309 ufunc->uf_func_type->tt_args[argcount] =
7310 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7311 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7312 }
7313 }
7314 else
7315 // No arguments, can use a predefined type.
7316 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7317 argcount, &ufunc->uf_type_list);
7318}
7319
7320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321/*
7322 * Delete an instruction, free what it contains.
7323 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007324 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007325delete_instr(isn_T *isn)
7326{
7327 switch (isn->isn_type)
7328 {
7329 case ISN_EXEC:
7330 case ISN_LOADENV:
7331 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007332 case ISN_LOADB:
7333 case ISN_LOADW:
7334 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007335 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007336 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 case ISN_PUSHEXC:
7338 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007339 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007341 case ISN_STOREB:
7342 case ISN_STOREW:
7343 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007344 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345 vim_free(isn->isn_arg.string);
7346 break;
7347
7348 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007349 case ISN_STORES:
7350 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 break;
7352
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007353 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007354 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007355 vim_free(isn->isn_arg.unlet.ul_name);
7356 break;
7357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007358 case ISN_STOREOPT:
7359 vim_free(isn->isn_arg.storeopt.so_name);
7360 break;
7361
7362 case ISN_PUSHBLOB: // push blob isn_arg.blob
7363 blob_unref(isn->isn_arg.blob);
7364 break;
7365
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007366 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007367#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007368 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007369#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007370 break;
7371
7372 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007373#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007374 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007375#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007376 break;
7377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378 case ISN_UCALL:
7379 vim_free(isn->isn_arg.ufunc.cuf_name);
7380 break;
7381
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007382 case ISN_FUNCREF:
7383 {
7384 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7385 + isn->isn_arg.funcref.fr_func;
7386 func_ptr_unref(dfunc->df_ufunc);
7387 }
7388 break;
7389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 case ISN_2BOOL:
7391 case ISN_2STRING:
7392 case ISN_ADDBLOB:
7393 case ISN_ADDLIST:
7394 case ISN_BCALL:
7395 case ISN_CATCH:
7396 case ISN_CHECKNR:
7397 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007398 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 case ISN_COMPAREANY:
7400 case ISN_COMPAREBLOB:
7401 case ISN_COMPAREBOOL:
7402 case ISN_COMPAREDICT:
7403 case ISN_COMPAREFLOAT:
7404 case ISN_COMPAREFUNC:
7405 case ISN_COMPARELIST:
7406 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 case ISN_COMPARESPECIAL:
7408 case ISN_COMPARESTRING:
7409 case ISN_CONCAT:
7410 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007411 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 case ISN_DROP:
7413 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007414 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007415 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007417 case ISN_EXECCONCAT:
7418 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007421 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007422 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007423 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424 case ISN_JUMP:
7425 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007426 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 case ISN_LOADSCRIPT:
7428 case ISN_LOADREG:
7429 case ISN_LOADV:
7430 case ISN_NEGATENR:
7431 case ISN_NEWDICT:
7432 case ISN_NEWLIST:
7433 case ISN_OPNR:
7434 case ISN_OPFLOAT:
7435 case ISN_OPANY:
7436 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007437 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007438 case ISN_PUSHF:
7439 case ISN_PUSHNR:
7440 case ISN_PUSHBOOL:
7441 case ISN_PUSHSPEC:
7442 case ISN_RETURN:
7443 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007444 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007445 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007447 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007448 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007449 case ISN_STOREDICT:
7450 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007451 case ISN_THROW:
7452 case ISN_TRY:
7453 // nothing allocated
7454 break;
7455 }
7456}
7457
7458/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007459 * Free all instructions for "dfunc".
7460 */
7461 static void
7462delete_def_function_contents(dfunc_T *dfunc)
7463{
7464 int idx;
7465
7466 ga_clear(&dfunc->df_def_args_isn);
7467
7468 if (dfunc->df_instr != NULL)
7469 {
7470 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7471 delete_instr(dfunc->df_instr + idx);
7472 VIM_CLEAR(dfunc->df_instr);
7473 }
7474
7475 dfunc->df_deleted = TRUE;
7476}
7477
7478/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007479 * When a user function is deleted, clear the contents of any associated def
7480 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481 */
7482 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007483clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007484{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007485 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 {
7487 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7488 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489
Bram Moolenaar20431c92020-03-20 18:39:46 +01007490 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007491 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007492 }
7493}
7494
7495#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007496/*
7497 * Free all functions defined with ":def".
7498 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007499 void
7500free_def_functions(void)
7501{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007502 int idx;
7503
7504 for (idx = 0; idx < def_functions.ga_len; ++idx)
7505 {
7506 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7507
7508 delete_def_function_contents(dfunc);
7509 }
7510
7511 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007512}
7513#endif
7514
7515
7516#endif // FEAT_EVAL