blob: 4b569f92f91fbfa598a03a7dcc11ccb9a91a62ab [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 Moolenaar65b95452020-07-19 14:03:09 +0200563/*
564 * Return FAIl if "expected" and "actual" don't match.
565 * TODO: better type comparison
566 */
567 int
568check_argtype(type_T *expected, typval_T *actual_tv)
569{
570 type_T actual;
571 type_T member;
572
573 // TODO: should should be done with more levels
574 CLEAR_FIELD(actual);
575 actual.tt_type = actual_tv->v_type;
576 if (actual_tv->v_type == VAR_LIST
577 && actual_tv->vval.v_list != NULL
578 && actual_tv->vval.v_list->lv_first != NULL)
579 {
580 // Use the type of the first member, it is the most specific.
581 CLEAR_FIELD(member);
582 member.tt_type = actual_tv->vval.v_list->lv_first->li_tv.v_type;
583 member.tt_member = &t_any;
584 actual.tt_member = &member;
585 }
586 else if (actual_tv->v_type == VAR_DICT
587 && actual_tv->vval.v_dict != NULL
588 && actual_tv->vval.v_dict->dv_hashtab.ht_used > 0)
589 {
590 dict_iterator_T iter;
591 typval_T *value;
592
593 // Use the type of the first value, it is the most specific.
594 dict_iterate_start(actual_tv, &iter);
595 dict_iterate_next(&iter, &value);
596 CLEAR_FIELD(member);
597 member.tt_type = value->v_type;
598 member.tt_member = &t_any;
599 actual.tt_member = &member;
600 }
601 else
602 actual.tt_member = &t_any;
603 return check_type(expected, &actual, TRUE);
604}
605
606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607/////////////////////////////////////////////////////////////////////
608// Following generate_ functions expect the caller to call ga_grow().
609
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200610#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
611#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613/*
614 * Generate an instruction without arguments.
615 * Returns a pointer to the new instruction, NULL if failed.
616 */
617 static isn_T *
618generate_instr(cctx_T *cctx, isntype_T isn_type)
619{
620 garray_T *instr = &cctx->ctx_instr;
621 isn_T *isn;
622
Bram Moolenaar080457c2020-03-03 21:53:32 +0100623 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 if (ga_grow(instr, 1) == FAIL)
625 return NULL;
626 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
627 isn->isn_type = isn_type;
628 isn->isn_lnum = cctx->ctx_lnum + 1;
629 ++instr->ga_len;
630
631 return isn;
632}
633
634/*
635 * Generate an instruction without arguments.
636 * "drop" will be removed from the stack.
637 * Returns a pointer to the new instruction, NULL if failed.
638 */
639 static isn_T *
640generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
641{
642 garray_T *stack = &cctx->ctx_type_stack;
643
Bram Moolenaar080457c2020-03-03 21:53:32 +0100644 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 stack->ga_len -= drop;
646 return generate_instr(cctx, isn_type);
647}
648
649/*
650 * Generate instruction "isn_type" and put "type" on the type stack.
651 */
652 static isn_T *
653generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
654{
655 isn_T *isn;
656 garray_T *stack = &cctx->ctx_type_stack;
657
658 if ((isn = generate_instr(cctx, isn_type)) == NULL)
659 return NULL;
660
661 if (ga_grow(stack, 1) == FAIL)
662 return NULL;
663 ((type_T **)stack->ga_data)[stack->ga_len] = type;
664 ++stack->ga_len;
665
666 return isn;
667}
668
669/*
670 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
671 */
672 static int
673may_generate_2STRING(int offset, cctx_T *cctx)
674{
675 isn_T *isn;
676 garray_T *stack = &cctx->ctx_type_stack;
677 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
678
679 if ((*type)->tt_type == VAR_STRING)
680 return OK;
681 *type = &t_string;
682
683 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
684 return FAIL;
685 isn->isn_arg.number = offset;
686
687 return OK;
688}
689
690 static int
691check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
692{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200693 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200695 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 {
697 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200698 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699 else
700 semsg(_("E1036: %c requires number or float arguments"), *op);
701 return FAIL;
702 }
703 return OK;
704}
705
706/*
707 * Generate an instruction with two arguments. The instruction depends on the
708 * type of the arguments.
709 */
710 static int
711generate_two_op(cctx_T *cctx, char_u *op)
712{
713 garray_T *stack = &cctx->ctx_type_stack;
714 type_T *type1;
715 type_T *type2;
716 vartype_T vartype;
717 isn_T *isn;
718
Bram Moolenaar080457c2020-03-03 21:53:32 +0100719 RETURN_OK_IF_SKIP(cctx);
720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721 // Get the known type of the two items on the stack. If they are matching
722 // use a type-specific instruction. Otherwise fall back to runtime type
723 // checking.
724 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
725 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200726 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 if (type1->tt_type == type2->tt_type
728 && (type1->tt_type == VAR_NUMBER
729 || type1->tt_type == VAR_LIST
730#ifdef FEAT_FLOAT
731 || type1->tt_type == VAR_FLOAT
732#endif
733 || type1->tt_type == VAR_BLOB))
734 vartype = type1->tt_type;
735
736 switch (*op)
737 {
738 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200739 && type1->tt_type != VAR_ANY
740 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 && check_number_or_float(
742 type1->tt_type, type2->tt_type, op) == FAIL)
743 return FAIL;
744 isn = generate_instr_drop(cctx,
745 vartype == VAR_NUMBER ? ISN_OPNR
746 : vartype == VAR_LIST ? ISN_ADDLIST
747 : vartype == VAR_BLOB ? ISN_ADDBLOB
748#ifdef FEAT_FLOAT
749 : vartype == VAR_FLOAT ? ISN_OPFLOAT
750#endif
751 : ISN_OPANY, 1);
752 if (isn != NULL)
753 isn->isn_arg.op.op_type = EXPR_ADD;
754 break;
755
756 case '-':
757 case '*':
758 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
759 op) == FAIL)
760 return FAIL;
761 if (vartype == VAR_NUMBER)
762 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
763#ifdef FEAT_FLOAT
764 else if (vartype == VAR_FLOAT)
765 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
766#endif
767 else
768 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
769 if (isn != NULL)
770 isn->isn_arg.op.op_type = *op == '*'
771 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
772 break;
773
Bram Moolenaar4c683752020-04-05 21:38:23 +0200774 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200776 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 && type2->tt_type != VAR_NUMBER))
778 {
779 emsg(_("E1035: % requires number arguments"));
780 return FAIL;
781 }
782 isn = generate_instr_drop(cctx,
783 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
784 if (isn != NULL)
785 isn->isn_arg.op.op_type = EXPR_REM;
786 break;
787 }
788
789 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200790 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100791 {
792 type_T *type = &t_any;
793
794#ifdef FEAT_FLOAT
795 // float+number and number+float results in float
796 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
797 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
798 type = &t_float;
799#endif
800 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
801 }
802
803 return OK;
804}
805
806/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200807 * Get the instruction to use for comparing "type1" with "type2"
808 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200810 static isntype_T
811get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812{
813 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814
Bram Moolenaar4c683752020-04-05 21:38:23 +0200815 if (type1 == VAR_UNKNOWN)
816 type1 = VAR_ANY;
817 if (type2 == VAR_UNKNOWN)
818 type2 = VAR_ANY;
819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 if (type1 == type2)
821 {
822 switch (type1)
823 {
824 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
825 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
826 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
827 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
828 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
829 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
830 case VAR_LIST: isntype = ISN_COMPARELIST; break;
831 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
832 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 default: isntype = ISN_COMPAREANY; break;
834 }
835 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200836 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
838 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
839 isntype = ISN_COMPAREANY;
840
841 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
842 && (isntype == ISN_COMPAREBOOL
843 || isntype == ISN_COMPARESPECIAL
844 || isntype == ISN_COMPARENR
845 || isntype == ISN_COMPAREFLOAT))
846 {
847 semsg(_("E1037: Cannot use \"%s\" with %s"),
848 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200849 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 }
851 if (isntype == ISN_DROP
852 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
853 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
854 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
855 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
856 && exptype != EXPR_IS && exptype != EXPR_ISNOT
857 && (type1 == VAR_BLOB || type2 == VAR_BLOB
858 || type1 == VAR_LIST || type2 == VAR_LIST))))
859 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100860 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200862 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100863 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200864 return isntype;
865}
866
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200867 int
868check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
869{
870 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
871 return FAIL;
872 return OK;
873}
874
Bram Moolenaara5565e42020-05-09 15:44:01 +0200875/*
876 * Generate an ISN_COMPARE* instruction with a boolean result.
877 */
878 static int
879generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
880{
881 isntype_T isntype;
882 isn_T *isn;
883 garray_T *stack = &cctx->ctx_type_stack;
884 vartype_T type1;
885 vartype_T type2;
886
887 RETURN_OK_IF_SKIP(cctx);
888
889 // Get the known type of the two items on the stack. If they are matching
890 // use a type-specific instruction. Otherwise fall back to runtime type
891 // checking.
892 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
893 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
894 isntype = get_compare_isn(exptype, type1, type2);
895 if (isntype == ISN_DROP)
896 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897
898 if ((isn = generate_instr(cctx, isntype)) == NULL)
899 return FAIL;
900 isn->isn_arg.op.op_type = exptype;
901 isn->isn_arg.op.op_ic = ic;
902
903 // takes two arguments, puts one bool back
904 if (stack->ga_len >= 2)
905 {
906 --stack->ga_len;
907 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
908 }
909
910 return OK;
911}
912
913/*
914 * Generate an ISN_2BOOL instruction.
915 */
916 static int
917generate_2BOOL(cctx_T *cctx, int invert)
918{
919 isn_T *isn;
920 garray_T *stack = &cctx->ctx_type_stack;
921
Bram Moolenaar080457c2020-03-03 21:53:32 +0100922 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
924 return FAIL;
925 isn->isn_arg.number = invert;
926
927 // type becomes bool
928 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
929
930 return OK;
931}
932
933 static int
934generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
935{
936 isn_T *isn;
937 garray_T *stack = &cctx->ctx_type_stack;
938
Bram Moolenaar080457c2020-03-03 21:53:32 +0100939 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
941 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200942 // TODO: whole type, e.g. for a function also arg and return types
943 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944 isn->isn_arg.type.ct_off = offset;
945
946 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200947 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948
949 return OK;
950}
951
952/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200953 * Check that
954 * - "actual" is "expected" type or
955 * - "actual" is a type that can be "expected" type: add a runtime check; or
956 * - return FAIL.
957 */
958 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200959need_type(
960 type_T *actual,
961 type_T *expected,
962 int offset,
963 cctx_T *cctx,
964 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200965{
966 if (check_type(expected, actual, FALSE) == OK)
967 return OK;
968 if (actual->tt_type != VAR_ANY
969 && actual->tt_type != VAR_UNKNOWN
970 && !(actual->tt_type == VAR_FUNC
971 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
972 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200973 if (!silent)
974 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200975 return FAIL;
976 }
977 generate_TYPECHECK(cctx, expected, offset);
978 return OK;
979}
980
981/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 * Generate an ISN_PUSHNR instruction.
983 */
984 static int
985generate_PUSHNR(cctx_T *cctx, varnumber_T number)
986{
987 isn_T *isn;
988
Bram Moolenaar080457c2020-03-03 21:53:32 +0100989 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
991 return FAIL;
992 isn->isn_arg.number = number;
993
994 return OK;
995}
996
997/*
998 * Generate an ISN_PUSHBOOL instruction.
999 */
1000 static int
1001generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1002{
1003 isn_T *isn;
1004
Bram Moolenaar080457c2020-03-03 21:53:32 +01001005 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1007 return FAIL;
1008 isn->isn_arg.number = number;
1009
1010 return OK;
1011}
1012
1013/*
1014 * Generate an ISN_PUSHSPEC instruction.
1015 */
1016 static int
1017generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1018{
1019 isn_T *isn;
1020
Bram Moolenaar080457c2020-03-03 21:53:32 +01001021 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1023 return FAIL;
1024 isn->isn_arg.number = number;
1025
1026 return OK;
1027}
1028
1029#ifdef FEAT_FLOAT
1030/*
1031 * Generate an ISN_PUSHF instruction.
1032 */
1033 static int
1034generate_PUSHF(cctx_T *cctx, float_T fnumber)
1035{
1036 isn_T *isn;
1037
Bram Moolenaar080457c2020-03-03 21:53:32 +01001038 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1040 return FAIL;
1041 isn->isn_arg.fnumber = fnumber;
1042
1043 return OK;
1044}
1045#endif
1046
1047/*
1048 * Generate an ISN_PUSHS instruction.
1049 * Consumes "str".
1050 */
1051 static int
1052generate_PUSHS(cctx_T *cctx, char_u *str)
1053{
1054 isn_T *isn;
1055
Bram Moolenaar080457c2020-03-03 21:53:32 +01001056 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1058 return FAIL;
1059 isn->isn_arg.string = str;
1060
1061 return OK;
1062}
1063
1064/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001065 * Generate an ISN_PUSHCHANNEL instruction.
1066 * Consumes "channel".
1067 */
1068 static int
1069generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1070{
1071 isn_T *isn;
1072
Bram Moolenaar080457c2020-03-03 21:53:32 +01001073 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001074 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1075 return FAIL;
1076 isn->isn_arg.channel = channel;
1077
1078 return OK;
1079}
1080
1081/*
1082 * Generate an ISN_PUSHJOB instruction.
1083 * Consumes "job".
1084 */
1085 static int
1086generate_PUSHJOB(cctx_T *cctx, job_T *job)
1087{
1088 isn_T *isn;
1089
Bram Moolenaar080457c2020-03-03 21:53:32 +01001090 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001091 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001092 return FAIL;
1093 isn->isn_arg.job = job;
1094
1095 return OK;
1096}
1097
1098/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 * Generate an ISN_PUSHBLOB instruction.
1100 * Consumes "blob".
1101 */
1102 static int
1103generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1104{
1105 isn_T *isn;
1106
Bram Moolenaar080457c2020-03-03 21:53:32 +01001107 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1109 return FAIL;
1110 isn->isn_arg.blob = blob;
1111
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001116 * Generate an ISN_PUSHFUNC instruction with name "name".
1117 * Consumes "name".
1118 */
1119 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001120generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001121{
1122 isn_T *isn;
1123
Bram Moolenaar080457c2020-03-03 21:53:32 +01001124 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001125 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001126 return FAIL;
1127 isn->isn_arg.string = name;
1128
1129 return OK;
1130}
1131
1132/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001133 * Generate an ISN_GETITEM instruction with "index".
1134 */
1135 static int
1136generate_GETITEM(cctx_T *cctx, int index)
1137{
1138 isn_T *isn;
1139 garray_T *stack = &cctx->ctx_type_stack;
1140 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1141 type_T *item_type = &t_any;
1142
1143 RETURN_OK_IF_SKIP(cctx);
1144
1145 if (type->tt_type == VAR_LIST)
1146 item_type = type->tt_member;
1147 else if (type->tt_type != VAR_ANY)
1148 {
1149 emsg(_(e_listreq));
1150 return FAIL;
1151 }
1152 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1153 return FAIL;
1154 isn->isn_arg.number = index;
1155
1156 // add the item type to the type stack
1157 if (ga_grow(stack, 1) == FAIL)
1158 return FAIL;
1159 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1160 ++stack->ga_len;
1161 return OK;
1162}
1163
1164/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001165 * Generate an ISN_SLICE instruction with "count".
1166 */
1167 static int
1168generate_SLICE(cctx_T *cctx, int count)
1169{
1170 isn_T *isn;
1171
1172 RETURN_OK_IF_SKIP(cctx);
1173 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1174 return FAIL;
1175 isn->isn_arg.number = count;
1176 return OK;
1177}
1178
1179/*
1180 * Generate an ISN_CHECKLEN instruction with "min_len".
1181 */
1182 static int
1183generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1184{
1185 isn_T *isn;
1186
1187 RETURN_OK_IF_SKIP(cctx);
1188
1189 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1190 return FAIL;
1191 isn->isn_arg.checklen.cl_min_len = min_len;
1192 isn->isn_arg.checklen.cl_more_OK = more_OK;
1193
1194 return OK;
1195}
1196
1197/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 * Generate an ISN_STORE instruction.
1199 */
1200 static int
1201generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
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_drop(cctx, isn_type, 1)) == 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/*
1217 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1218 */
1219 static int
1220generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1221{
1222 isn_T *isn;
1223
Bram Moolenaar080457c2020-03-03 21:53:32 +01001224 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1226 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001227 isn->isn_arg.storenr.stnr_idx = idx;
1228 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229
1230 return OK;
1231}
1232
1233/*
1234 * Generate an ISN_STOREOPT instruction
1235 */
1236 static int
1237generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1238{
1239 isn_T *isn;
1240
Bram Moolenaar080457c2020-03-03 21:53:32 +01001241 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1243 return FAIL;
1244 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1245 isn->isn_arg.storeopt.so_flags = opt_flags;
1246
1247 return OK;
1248}
1249
1250/*
1251 * Generate an ISN_LOAD or similar instruction.
1252 */
1253 static int
1254generate_LOAD(
1255 cctx_T *cctx,
1256 isntype_T isn_type,
1257 int idx,
1258 char_u *name,
1259 type_T *type)
1260{
1261 isn_T *isn;
1262
Bram Moolenaar080457c2020-03-03 21:53:32 +01001263 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1265 return FAIL;
1266 if (name != NULL)
1267 isn->isn_arg.string = vim_strsave(name);
1268 else
1269 isn->isn_arg.number = idx;
1270
1271 return OK;
1272}
1273
1274/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001275 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001276 */
1277 static int
1278generate_LOADV(
1279 cctx_T *cctx,
1280 char_u *name,
1281 int error)
1282{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001283 int di_flags;
1284 int vidx = find_vim_var(name, &di_flags);
1285 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286
Bram Moolenaar080457c2020-03-03 21:53:32 +01001287 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001288 if (vidx < 0)
1289 {
1290 if (error)
1291 semsg(_(e_var_notfound), name);
1292 return FAIL;
1293 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001294 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001295
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001296 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001297}
1298
1299/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001300 * Generate an ISN_UNLET instruction.
1301 */
1302 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001303generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001304{
1305 isn_T *isn;
1306
1307 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001308 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001309 return FAIL;
1310 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1311 isn->isn_arg.unlet.ul_forceit = forceit;
1312
1313 return OK;
1314}
1315
1316/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 * Generate an ISN_LOADS instruction.
1318 */
1319 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001320generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001322 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001324 int sid,
1325 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326{
1327 isn_T *isn;
1328
Bram Moolenaar080457c2020-03-03 21:53:32 +01001329 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001330 if (isn_type == ISN_LOADS)
1331 isn = generate_instr_type(cctx, isn_type, type);
1332 else
1333 isn = generate_instr_drop(cctx, isn_type, 1);
1334 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001336 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1337 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338
1339 return OK;
1340}
1341
1342/*
1343 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1344 */
1345 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001346generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 cctx_T *cctx,
1348 isntype_T isn_type,
1349 int sid,
1350 int idx,
1351 type_T *type)
1352{
1353 isn_T *isn;
1354
Bram Moolenaar080457c2020-03-03 21:53:32 +01001355 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 if (isn_type == ISN_LOADSCRIPT)
1357 isn = generate_instr_type(cctx, isn_type, type);
1358 else
1359 isn = generate_instr_drop(cctx, isn_type, 1);
1360 if (isn == NULL)
1361 return FAIL;
1362 isn->isn_arg.script.script_sid = sid;
1363 isn->isn_arg.script.script_idx = idx;
1364 return OK;
1365}
1366
1367/*
1368 * Generate an ISN_NEWLIST instruction.
1369 */
1370 static int
1371generate_NEWLIST(cctx_T *cctx, int count)
1372{
1373 isn_T *isn;
1374 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 type_T *type;
1376 type_T *member;
1377
Bram Moolenaar080457c2020-03-03 21:53:32 +01001378 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1380 return FAIL;
1381 isn->isn_arg.number = count;
1382
1383 // drop the value types
1384 stack->ga_len -= count;
1385
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001386 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001387 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 if (count > 0)
1389 member = ((type_T **)stack->ga_data)[stack->ga_len];
1390 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001391 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001392 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393
1394 // add the list type to the type stack
1395 if (ga_grow(stack, 1) == FAIL)
1396 return FAIL;
1397 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1398 ++stack->ga_len;
1399
1400 return OK;
1401}
1402
1403/*
1404 * Generate an ISN_NEWDICT instruction.
1405 */
1406 static int
1407generate_NEWDICT(cctx_T *cctx, int count)
1408{
1409 isn_T *isn;
1410 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 type_T *type;
1412 type_T *member;
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_NEWDICT)) == NULL)
1416 return FAIL;
1417 isn->isn_arg.number = count;
1418
1419 // drop the key and value types
1420 stack->ga_len -= 2 * count;
1421
Bram Moolenaar436472f2020-02-20 22:54:43 +01001422 // Use the first value type for the list member type. Use "void" for an
1423 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 if (count > 0)
1425 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1426 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001427 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001428 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429
1430 // add the dict type to the type stack
1431 if (ga_grow(stack, 1) == FAIL)
1432 return FAIL;
1433 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1434 ++stack->ga_len;
1435
1436 return OK;
1437}
1438
1439/*
1440 * Generate an ISN_FUNCREF instruction.
1441 */
1442 static int
1443generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1444{
1445 isn_T *isn;
1446 garray_T *stack = &cctx->ctx_type_stack;
1447
Bram Moolenaar080457c2020-03-03 21:53:32 +01001448 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1450 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001451 isn->isn_arg.funcref.fr_func = dfunc_idx;
1452 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453
1454 if (ga_grow(stack, 1) == FAIL)
1455 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001456 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 // TODO: argument and return types
1458 ++stack->ga_len;
1459
1460 return OK;
1461}
1462
1463/*
1464 * Generate an ISN_JUMP instruction.
1465 */
1466 static int
1467generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1468{
1469 isn_T *isn;
1470 garray_T *stack = &cctx->ctx_type_stack;
1471
Bram Moolenaar080457c2020-03-03 21:53:32 +01001472 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1474 return FAIL;
1475 isn->isn_arg.jump.jump_when = when;
1476 isn->isn_arg.jump.jump_where = where;
1477
1478 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1479 --stack->ga_len;
1480
1481 return OK;
1482}
1483
1484 static int
1485generate_FOR(cctx_T *cctx, int loop_idx)
1486{
1487 isn_T *isn;
1488 garray_T *stack = &cctx->ctx_type_stack;
1489
Bram Moolenaar080457c2020-03-03 21:53:32 +01001490 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1492 return FAIL;
1493 isn->isn_arg.forloop.for_idx = loop_idx;
1494
1495 if (ga_grow(stack, 1) == FAIL)
1496 return FAIL;
1497 // type doesn't matter, will be stored next
1498 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1499 ++stack->ga_len;
1500
1501 return OK;
1502}
1503
1504/*
1505 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001506 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 * Return FAIL if the number of arguments is wrong.
1508 */
1509 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001510generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511{
1512 isn_T *isn;
1513 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001514 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001515 type_T *argtypes[MAX_FUNC_ARGS];
1516 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517
Bram Moolenaar080457c2020-03-03 21:53:32 +01001518 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001519 argoff = check_internal_func(func_idx, argcount);
1520 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 return FAIL;
1522
Bram Moolenaar389df252020-07-09 21:20:47 +02001523 if (method_call && argoff > 1)
1524 {
1525 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1526 return FAIL;
1527 isn->isn_arg.shuffle.shfl_item = argcount;
1528 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1529 }
1530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1532 return FAIL;
1533 isn->isn_arg.bfunc.cbf_idx = func_idx;
1534 isn->isn_arg.bfunc.cbf_argcount = argcount;
1535
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001536 for (i = 0; i < argcount; ++i)
1537 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 stack->ga_len -= argcount; // drop the arguments
1540 if (ga_grow(stack, 1) == FAIL)
1541 return FAIL;
1542 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001543 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 ++stack->ga_len; // add return value
1545
1546 return OK;
1547}
1548
1549/*
1550 * Generate an ISN_DCALL or ISN_UCALL instruction.
1551 * Return FAIL if the number of arguments is wrong.
1552 */
1553 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001554generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555{
1556 isn_T *isn;
1557 garray_T *stack = &cctx->ctx_type_stack;
1558 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001559 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560
Bram Moolenaar080457c2020-03-03 21:53:32 +01001561 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 if (argcount > regular_args && !has_varargs(ufunc))
1563 {
1564 semsg(_(e_toomanyarg), ufunc->uf_name);
1565 return FAIL;
1566 }
1567 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1568 {
1569 semsg(_(e_toofewarg), ufunc->uf_name);
1570 return FAIL;
1571 }
1572
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001573 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001574 {
1575 int i;
1576
1577 for (i = 0; i < argcount; ++i)
1578 {
1579 type_T *expected;
1580 type_T *actual;
1581
1582 if (i < regular_args)
1583 {
1584 if (ufunc->uf_arg_types == NULL)
1585 continue;
1586 expected = ufunc->uf_arg_types[i];
1587 }
1588 else
1589 expected = ufunc->uf_va_type->tt_member;
1590 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001591 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001592 {
1593 arg_type_mismatch(expected, actual, i + 1);
1594 return FAIL;
1595 }
1596 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001597 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001598 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001599 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001600 }
1601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001603 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001604 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001606 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 {
1608 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1609 isn->isn_arg.dfunc.cdf_argcount = argcount;
1610 }
1611 else
1612 {
1613 // A user function may be deleted and redefined later, can't use the
1614 // ufunc pointer, need to look it up again at runtime.
1615 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1616 isn->isn_arg.ufunc.cuf_argcount = argcount;
1617 }
1618
1619 stack->ga_len -= argcount; // drop the arguments
1620 if (ga_grow(stack, 1) == FAIL)
1621 return FAIL;
1622 // add return value
1623 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1624 ++stack->ga_len;
1625
1626 return OK;
1627}
1628
1629/*
1630 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1631 */
1632 static int
1633generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1634{
1635 isn_T *isn;
1636 garray_T *stack = &cctx->ctx_type_stack;
1637
Bram Moolenaar080457c2020-03-03 21:53:32 +01001638 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1640 return FAIL;
1641 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1642 isn->isn_arg.ufunc.cuf_argcount = argcount;
1643
1644 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001645 if (ga_grow(stack, 1) == FAIL)
1646 return FAIL;
1647 // add return value
1648 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1649 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650
1651 return OK;
1652}
1653
1654/*
1655 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001656 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 */
1658 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001659generate_PCALL(
1660 cctx_T *cctx,
1661 int argcount,
1662 char_u *name,
1663 type_T *type,
1664 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665{
1666 isn_T *isn;
1667 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001668 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669
Bram Moolenaar080457c2020-03-03 21:53:32 +01001670 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001671
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001672 if (type->tt_type == VAR_ANY)
1673 ret_type = &t_any;
1674 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001675 {
1676 if (type->tt_argcount != -1)
1677 {
1678 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1679
1680 if (argcount < type->tt_min_argcount - varargs)
1681 {
1682 semsg(_(e_toofewarg), "[reference]");
1683 return FAIL;
1684 }
1685 if (!varargs && argcount > type->tt_argcount)
1686 {
1687 semsg(_(e_toomanyarg), "[reference]");
1688 return FAIL;
1689 }
1690 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001691 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001692 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001693 else
1694 {
1695 semsg(_("E1085: Not a callable type: %s"), name);
1696 return FAIL;
1697 }
1698
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1700 return FAIL;
1701 isn->isn_arg.pfunc.cpf_top = at_top;
1702 isn->isn_arg.pfunc.cpf_argcount = argcount;
1703
1704 stack->ga_len -= argcount; // drop the arguments
1705
1706 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001707 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001709 // If partial is above the arguments it must be cleared and replaced with
1710 // the return value.
1711 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1712 return FAIL;
1713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 return OK;
1715}
1716
1717/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001718 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 */
1720 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001721generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722{
1723 isn_T *isn;
1724 garray_T *stack = &cctx->ctx_type_stack;
1725 type_T *type;
1726
Bram Moolenaar080457c2020-03-03 21:53:32 +01001727 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001728 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001730 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001732 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001734 if (type->tt_type != VAR_DICT && type != &t_any)
1735 {
1736 emsg(_(e_dictreq));
1737 return FAIL;
1738 }
1739 // change dict type to dict member type
1740 if (type->tt_type == VAR_DICT)
1741 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742
1743 return OK;
1744}
1745
1746/*
1747 * Generate an ISN_ECHO instruction.
1748 */
1749 static int
1750generate_ECHO(cctx_T *cctx, int with_white, int count)
1751{
1752 isn_T *isn;
1753
Bram Moolenaar080457c2020-03-03 21:53:32 +01001754 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1756 return FAIL;
1757 isn->isn_arg.echo.echo_with_white = with_white;
1758 isn->isn_arg.echo.echo_count = count;
1759
1760 return OK;
1761}
1762
Bram Moolenaarad39c092020-02-26 18:23:43 +01001763/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001764 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001765 */
1766 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001767generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001768{
1769 isn_T *isn;
1770
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001771 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001772 return FAIL;
1773 isn->isn_arg.number = count;
1774
1775 return OK;
1776}
1777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 static int
1779generate_EXEC(cctx_T *cctx, char_u *line)
1780{
1781 isn_T *isn;
1782
Bram Moolenaar080457c2020-03-03 21:53:32 +01001783 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1785 return FAIL;
1786 isn->isn_arg.string = vim_strsave(line);
1787 return OK;
1788}
1789
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001790 static int
1791generate_EXECCONCAT(cctx_T *cctx, int count)
1792{
1793 isn_T *isn;
1794
1795 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1796 return FAIL;
1797 isn->isn_arg.number = count;
1798 return OK;
1799}
1800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801/*
1802 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001803 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001805 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1807{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 lvar_T *lvar;
1809
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001810 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001812 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001813 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814 }
1815
1816 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001817 return NULL;
1818 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001820 // Every local variable uses the next entry on the stack. We could re-use
1821 // the last ones when leaving a scope, but then variables used in a closure
1822 // might get overwritten. To keep things simple do not re-use stack
1823 // entries. This is less efficient, but memory is cheap these days.
1824 lvar->lv_idx = cctx->ctx_locals_count++;
1825
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001826 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 lvar->lv_const = isConst;
1828 lvar->lv_type = type;
1829
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001830 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831}
1832
1833/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001834 * Remove local variables above "new_top".
1835 */
1836 static void
1837unwind_locals(cctx_T *cctx, int new_top)
1838{
1839 if (cctx->ctx_locals.ga_len > new_top)
1840 {
1841 int idx;
1842 lvar_T *lvar;
1843
1844 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1845 {
1846 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1847 vim_free(lvar->lv_name);
1848 }
1849 }
1850 cctx->ctx_locals.ga_len = new_top;
1851}
1852
1853/*
1854 * Free all local variables.
1855 */
1856 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001857free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001858{
1859 unwind_locals(cctx, 0);
1860 ga_clear(&cctx->ctx_locals);
1861}
1862
1863/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 * Skip over a type definition and return a pointer to just after it.
1865 */
1866 char_u *
1867skip_type(char_u *start)
1868{
1869 char_u *p = start;
1870
1871 while (ASCII_ISALNUM(*p) || *p == '_')
1872 ++p;
1873
1874 // Skip over "<type>"; this is permissive about white space.
1875 if (*skipwhite(p) == '<')
1876 {
1877 p = skipwhite(p);
1878 p = skip_type(skipwhite(p + 1));
1879 p = skipwhite(p);
1880 if (*p == '>')
1881 ++p;
1882 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001883 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1884 {
1885 // handle func(args): type
1886 ++p;
1887 while (*p != ')' && *p != NUL)
1888 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001889 char_u *sp = p;
1890
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001891 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001892 if (p == sp)
1893 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001894 if (*p == ',')
1895 p = skipwhite(p + 1);
1896 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001897 if (*p == ')')
1898 {
1899 if (p[1] == ':')
1900 p = skip_type(skipwhite(p + 2));
1901 else
1902 p = skipwhite(p + 1);
1903 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001904 }
1905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 return p;
1907}
1908
1909/*
1910 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001911 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 * Returns NULL in case of failure.
1913 */
1914 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001915parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916{
1917 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001918 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919
1920 if (**arg != '<')
1921 {
1922 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001923 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 else
1925 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001926 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 }
1928 *arg = skipwhite(*arg + 1);
1929
Bram Moolenaard77a8522020-04-03 21:59:57 +02001930 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931
1932 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001933 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 {
1935 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001936 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 }
1938 ++*arg;
1939
1940 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001941 return get_list_type(member_type, type_gap);
1942 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943}
1944
1945/*
1946 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001947 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 */
1949 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001950parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951{
1952 char_u *p = *arg;
1953 size_t len;
1954
1955 // skip over the first word
1956 while (ASCII_ISALNUM(*p) || *p == '_')
1957 ++p;
1958 len = p - *arg;
1959
1960 switch (**arg)
1961 {
1962 case 'a':
1963 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1964 {
1965 *arg += len;
1966 return &t_any;
1967 }
1968 break;
1969 case 'b':
1970 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1971 {
1972 *arg += len;
1973 return &t_bool;
1974 }
1975 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1976 {
1977 *arg += len;
1978 return &t_blob;
1979 }
1980 break;
1981 case 'c':
1982 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1983 {
1984 *arg += len;
1985 return &t_channel;
1986 }
1987 break;
1988 case 'd':
1989 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1990 {
1991 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001992 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 }
1994 break;
1995 case 'f':
1996 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1997 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001998#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 *arg += len;
2000 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002001#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002002 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002003 return &t_any;
2004#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 }
2006 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2007 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002008 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002009 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002010 int argcount = -1;
2011 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002012 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002013 type_T *arg_type[MAX_FUNC_ARGS + 1];
2014
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002015 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002017 if (**arg == '(')
2018 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002019 // "func" may or may not return a value, "func()" does
2020 // not return a value.
2021 ret_type = &t_void;
2022
Bram Moolenaard77a8522020-04-03 21:59:57 +02002023 p = ++*arg;
2024 argcount = 0;
2025 while (*p != NUL && *p != ')')
2026 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002027 if (*p == '?')
2028 {
2029 if (first_optional == -1)
2030 first_optional = argcount;
2031 ++p;
2032 }
2033 else if (first_optional != -1)
2034 {
2035 emsg(_("E1007: mandatory argument after optional argument"));
2036 return &t_any;
2037 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002038 else if (STRNCMP(p, "...", 3) == 0)
2039 {
2040 flags |= TTFLAG_VARARGS;
2041 p += 3;
2042 }
2043
2044 arg_type[argcount++] = parse_type(&p, type_gap);
2045
2046 // Nothing comes after "...{type}".
2047 if (flags & TTFLAG_VARARGS)
2048 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002049
Bram Moolenaard77a8522020-04-03 21:59:57 +02002050 if (*p != ',' && *skipwhite(p) == ',')
2051 {
2052 semsg(_(e_no_white_before), ",");
2053 return &t_any;
2054 }
2055 if (*p == ',')
2056 {
2057 ++p;
2058 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002059 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002060 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002061 return &t_any;
2062 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002063 }
2064 p = skipwhite(p);
2065 if (argcount == MAX_FUNC_ARGS)
2066 {
2067 emsg(_("E740: Too many argument types"));
2068 return &t_any;
2069 }
2070 }
2071
2072 p = skipwhite(p);
2073 if (*p != ')')
2074 {
2075 emsg(_(e_missing_close));
2076 return &t_any;
2077 }
2078 *arg = p + 1;
2079 }
2080 if (**arg == ':')
2081 {
2082 // parse return type
2083 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002084 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002085 semsg(_(e_white_after), ":");
2086 *arg = skipwhite(*arg);
2087 ret_type = parse_type(arg, type_gap);
2088 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002089 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002090 type = get_func_type(ret_type, argcount, type_gap);
2091 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002092 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002093 type = alloc_func_type(ret_type, argcount, type_gap);
2094 type->tt_flags = flags;
2095 if (argcount > 0)
2096 {
2097 type->tt_argcount = argcount;
2098 type->tt_min_argcount = first_optional == -1
2099 ? argcount : first_optional;
2100 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002101 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002102 return &t_any;
2103 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002104 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002105 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002106 }
2107 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 }
2109 break;
2110 case 'j':
2111 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2112 {
2113 *arg += len;
2114 return &t_job;
2115 }
2116 break;
2117 case 'l':
2118 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2119 {
2120 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002121 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 }
2123 break;
2124 case 'n':
2125 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2126 {
2127 *arg += len;
2128 return &t_number;
2129 }
2130 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 case 's':
2132 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2133 {
2134 *arg += len;
2135 return &t_string;
2136 }
2137 break;
2138 case 'v':
2139 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2140 {
2141 *arg += len;
2142 return &t_void;
2143 }
2144 break;
2145 }
2146
2147 semsg(_("E1010: Type not recognized: %s"), *arg);
2148 return &t_any;
2149}
2150
2151/*
2152 * Check if "type1" and "type2" are exactly the same.
2153 */
2154 static int
2155equal_type(type_T *type1, type_T *type2)
2156{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002157 int i;
2158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 if (type1->tt_type != type2->tt_type)
2160 return FALSE;
2161 switch (type1->tt_type)
2162 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002164 case VAR_ANY:
2165 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 case VAR_SPECIAL:
2167 case VAR_BOOL:
2168 case VAR_NUMBER:
2169 case VAR_FLOAT:
2170 case VAR_STRING:
2171 case VAR_BLOB:
2172 case VAR_JOB:
2173 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002174 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 case VAR_LIST:
2176 case VAR_DICT:
2177 return equal_type(type1->tt_member, type2->tt_member);
2178 case VAR_FUNC:
2179 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002180 if (!equal_type(type1->tt_member, type2->tt_member)
2181 || type1->tt_argcount != type2->tt_argcount)
2182 return FALSE;
2183 if (type1->tt_argcount < 0
2184 || type1->tt_args == NULL || type2->tt_args == NULL)
2185 return TRUE;
2186 for (i = 0; i < type1->tt_argcount; ++i)
2187 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2188 return FALSE;
2189 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 }
2191 return TRUE;
2192}
2193
2194/*
2195 * Find the common type of "type1" and "type2" and put it in "dest".
2196 * "type2" and "dest" may be the same.
2197 */
2198 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002199common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200{
2201 if (equal_type(type1, type2))
2202 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002203 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 return;
2205 }
2206
2207 if (type1->tt_type == type2->tt_type)
2208 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2210 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002211 type_T *common;
2212
Bram Moolenaard77a8522020-04-03 21:59:57 +02002213 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002214 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002215 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002216 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002217 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218 return;
2219 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002220 if (type1->tt_type == VAR_FUNC)
2221 {
2222 type_T *common;
2223
2224 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2225 if (type1->tt_argcount == type2->tt_argcount
2226 && type1->tt_argcount >= 0)
2227 {
2228 int argcount = type1->tt_argcount;
2229 int i;
2230
2231 *dest = alloc_func_type(common, argcount, type_gap);
2232 if (type1->tt_args != NULL && type2->tt_args != NULL)
2233 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002234 if (func_type_add_arg_types(*dest, argcount,
2235 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002236 for (i = 0; i < argcount; ++i)
2237 common_type(type1->tt_args[i], type2->tt_args[i],
2238 &(*dest)->tt_args[i], type_gap);
2239 }
2240 }
2241 else
2242 *dest = alloc_func_type(common, -1, type_gap);
2243 return;
2244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 }
2246
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002247 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248}
2249
2250 char *
2251vartype_name(vartype_T type)
2252{
2253 switch (type)
2254 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002255 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002256 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 case VAR_SPECIAL: return "special";
2259 case VAR_BOOL: return "bool";
2260 case VAR_NUMBER: return "number";
2261 case VAR_FLOAT: return "float";
2262 case VAR_STRING: return "string";
2263 case VAR_BLOB: return "blob";
2264 case VAR_JOB: return "job";
2265 case VAR_CHANNEL: return "channel";
2266 case VAR_LIST: return "list";
2267 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002268
2269 case VAR_FUNC:
2270 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002272 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273}
2274
2275/*
2276 * Return the name of a type.
2277 * The result may be in allocated memory, in which case "tofree" is set.
2278 */
2279 char *
2280type_name(type_T *type, char **tofree)
2281{
2282 char *name = vartype_name(type->tt_type);
2283
2284 *tofree = NULL;
2285 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2286 {
2287 char *member_free;
2288 char *member_name = type_name(type->tt_member, &member_free);
2289 size_t len;
2290
2291 len = STRLEN(name) + STRLEN(member_name) + 3;
2292 *tofree = alloc(len);
2293 if (*tofree != NULL)
2294 {
2295 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2296 vim_free(member_free);
2297 return *tofree;
2298 }
2299 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002300 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002301 {
2302 garray_T ga;
2303 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002304 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002305
2306 ga_init2(&ga, 1, 100);
2307 if (ga_grow(&ga, 20) == FAIL)
2308 return "[unknown]";
2309 *tofree = ga.ga_data;
2310 STRCPY(ga.ga_data, "func(");
2311 ga.ga_len += 5;
2312
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002313 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002314 {
2315 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002316 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002317 int len;
2318
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002319 if (type->tt_args == NULL)
2320 arg_type = "[unknown]";
2321 else
2322 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002323 if (i > 0)
2324 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002325 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002326 ga.ga_len += 2;
2327 }
2328 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002329 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002330 {
2331 vim_free(arg_free);
2332 return "[unknown]";
2333 }
2334 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002335 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002336 {
2337 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2338 ga.ga_len += 3;
2339 }
2340 else if (i >= type->tt_min_argcount)
2341 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002342 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002343 ga.ga_len += len;
2344 vim_free(arg_free);
2345 }
2346
2347 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002348 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002349 else
2350 {
2351 char *ret_free;
2352 char *ret_name = type_name(type->tt_member, &ret_free);
2353 int len;
2354
2355 len = (int)STRLEN(ret_name) + 4;
2356 if (ga_grow(&ga, len) == FAIL)
2357 {
2358 vim_free(ret_free);
2359 return "[unknown]";
2360 }
2361 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002362 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2363 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002364 vim_free(ret_free);
2365 }
2366 return ga.ga_data;
2367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368
2369 return name;
2370}
2371
2372/*
2373 * Find "name" in script-local items of script "sid".
2374 * Returns the index in "sn_var_vals" if found.
2375 * If found but not in "sn_var_vals" returns -1.
2376 * If not found returns -2.
2377 */
2378 int
2379get_script_item_idx(int sid, char_u *name, int check_writable)
2380{
2381 hashtab_T *ht;
2382 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002383 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 int idx;
2385
2386 // First look the name up in the hashtable.
2387 if (sid <= 0 || sid > script_items.ga_len)
2388 return -1;
2389 ht = &SCRIPT_VARS(sid);
2390 di = find_var_in_ht(ht, 0, name, TRUE);
2391 if (di == NULL)
2392 return -2;
2393
2394 // Now find the svar_T index in sn_var_vals.
2395 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2396 {
2397 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2398
2399 if (sv->sv_tv == &di->di_tv)
2400 {
2401 if (check_writable && sv->sv_const)
2402 semsg(_(e_readonlyvar), name);
2403 return idx;
2404 }
2405 }
2406 return -1;
2407}
2408
2409/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002410 * Find "name" in imported items of the current script or in "cctx" if not
2411 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 */
2413 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002414find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002416 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 int idx;
2418
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002419 if (current_sctx.sc_sid <= 0)
2420 return NULL;
2421 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 if (cctx != NULL)
2423 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2424 {
2425 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2426 + idx;
2427
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002428 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2429 : STRLEN(import->imp_name) == len
2430 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431 return import;
2432 }
2433
2434 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2435 {
2436 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2437
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002438 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2439 : STRLEN(import->imp_name) == len
2440 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 return import;
2442 }
2443 return NULL;
2444}
2445
2446/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002447 * Free all imported variables.
2448 */
2449 static void
2450free_imported(cctx_T *cctx)
2451{
2452 int idx;
2453
2454 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2455 {
2456 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2457
2458 vim_free(import->imp_name);
2459 }
2460 ga_clear(&cctx->ctx_imports);
2461}
2462
2463/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002464 * Return TRUE if "p" points at a "#" but not at "#{".
2465 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002466 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002467vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002468{
2469 return p[0] == '#' && p[1] != '{';
2470}
2471
2472/*
2473 * Return a pointer to the next line that isn't empty or only contains a
2474 * comment. Skips over white space.
2475 * Returns NULL if there is none.
2476 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002477 char_u *
2478peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002479{
2480 int lnum = cctx->ctx_lnum;
2481
2482 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2483 {
2484 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002485 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002486
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002487 if (line == NULL)
2488 break;
2489 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002490 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002491 return p;
2492 }
2493 return NULL;
2494}
2495
2496/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002497 * Called when checking for a following operator at "arg". When the rest of
2498 * the line is empty or only a comment, peek the next line. If there is a next
2499 * line return a pointer to it and set "nextp".
2500 * Otherwise skip over white space.
2501 */
2502 static char_u *
2503may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2504{
2505 char_u *p = skipwhite(arg);
2506
2507 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002508 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002509 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002510 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002511 if (*nextp != NULL)
2512 return *nextp;
2513 }
2514 return p;
2515}
2516
2517/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002518 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002519 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002520 * Returns NULL when at the end.
2521 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002522 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002523next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002524{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002525 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002526
2527 do
2528 {
2529 ++cctx->ctx_lnum;
2530 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002531 {
2532 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002533 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002534 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002535 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002536 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002537 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002538 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002539 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002540 return line;
2541}
2542
2543/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002544 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002545 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002546 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2547 */
2548 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002549may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002550{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002551 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002552 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002553 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002554
2555 if (next == NULL)
2556 return FAIL;
2557 *arg = skipwhite(next);
2558 }
2559 return OK;
2560}
2561
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002562/*
2563 * Idem, and give an error when failed.
2564 */
2565 static int
2566may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2567{
2568 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2569 {
2570 emsg(_("E1097: line incomplete"));
2571 return FAIL;
2572 }
2573 return OK;
2574}
2575
2576
Bram Moolenaara5565e42020-05-09 15:44:01 +02002577// Structure passed between the compile_expr* functions to keep track of
2578// constants that have been parsed but for which no code was produced yet. If
2579// possible expressions on these constants are applied at compile time. If
2580// that is not possible, the code to push the constants needs to be generated
2581// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002582// Using 50 should be more than enough of 5 levels of ().
2583#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002584typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002585 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002586 int pp_used; // active entries in pp_tv[]
2587} ppconst_T;
2588
Bram Moolenaar1c747212020-05-09 18:28:34 +02002589static int compile_expr0(char_u **arg, cctx_T *cctx);
2590static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2591
Bram Moolenaara5565e42020-05-09 15:44:01 +02002592/*
2593 * Generate a PUSH instruction for "tv".
2594 * "tv" will be consumed or cleared.
2595 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2596 */
2597 static int
2598generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2599{
2600 if (tv != NULL)
2601 {
2602 switch (tv->v_type)
2603 {
2604 case VAR_UNKNOWN:
2605 break;
2606 case VAR_BOOL:
2607 generate_PUSHBOOL(cctx, tv->vval.v_number);
2608 break;
2609 case VAR_SPECIAL:
2610 generate_PUSHSPEC(cctx, tv->vval.v_number);
2611 break;
2612 case VAR_NUMBER:
2613 generate_PUSHNR(cctx, tv->vval.v_number);
2614 break;
2615#ifdef FEAT_FLOAT
2616 case VAR_FLOAT:
2617 generate_PUSHF(cctx, tv->vval.v_float);
2618 break;
2619#endif
2620 case VAR_BLOB:
2621 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2622 tv->vval.v_blob = NULL;
2623 break;
2624 case VAR_STRING:
2625 generate_PUSHS(cctx, tv->vval.v_string);
2626 tv->vval.v_string = NULL;
2627 break;
2628 default:
2629 iemsg("constant type not supported");
2630 clear_tv(tv);
2631 return FAIL;
2632 }
2633 tv->v_type = VAR_UNKNOWN;
2634 }
2635 return OK;
2636}
2637
2638/*
2639 * Generate code for any ppconst entries.
2640 */
2641 static int
2642generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2643{
2644 int i;
2645 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002646 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002647
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002648 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002649 for (i = 0; i < ppconst->pp_used; ++i)
2650 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2651 ret = FAIL;
2652 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002653 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002654 return ret;
2655}
2656
2657/*
2658 * Clear ppconst constants. Used when failing.
2659 */
2660 static void
2661clear_ppconst(ppconst_T *ppconst)
2662{
2663 int i;
2664
2665 for (i = 0; i < ppconst->pp_used; ++i)
2666 clear_tv(&ppconst->pp_tv[i]);
2667 ppconst->pp_used = 0;
2668}
2669
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002670/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002671 * Generate an instruction to load script-local variable "name", without the
2672 * leading "s:".
2673 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 */
2675 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002676compile_load_scriptvar(
2677 cctx_T *cctx,
2678 char_u *name, // variable NUL terminated
2679 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002680 char_u **end, // end of variable
2681 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002683 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2685 imported_T *import;
2686
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002687 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002689 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002690 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2691 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 }
2693 if (idx >= 0)
2694 {
2695 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2696
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002697 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 current_sctx.sc_sid, idx, sv->sv_type);
2699 return OK;
2700 }
2701
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002702 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 if (import != NULL)
2704 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002705 if (import->imp_all)
2706 {
2707 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002708 char_u *exp_name;
2709 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002710 ufunc_T *ufunc;
2711 type_T *type;
2712
2713 // Used "import * as Name", need to lookup the member.
2714 if (*p != '.')
2715 {
2716 semsg(_("E1060: expected dot after name: %s"), start);
2717 return FAIL;
2718 }
2719 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002720 if (VIM_ISWHITE(*p))
2721 {
2722 emsg(_("E1074: no white space allowed after dot"));
2723 return FAIL;
2724 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002725
Bram Moolenaar1c991142020-07-04 13:15:31 +02002726 // isolate one name
2727 exp_name = p;
2728 while (eval_isnamec(*p))
2729 ++p;
2730 cc = *p;
2731 *p = NUL;
2732
2733 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2734 *p = cc;
2735 p = skipwhite(p);
2736
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002737 // TODO: what if it is a function?
2738 if (idx < 0)
2739 return FAIL;
2740 *end = p;
2741
2742 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2743 import->imp_sid,
2744 idx,
2745 type);
2746 }
2747 else
2748 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002749 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002750 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2751 import->imp_sid,
2752 import->imp_var_vals_idx,
2753 import->imp_type);
2754 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 return OK;
2756 }
2757
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002758 if (error)
2759 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 return FAIL;
2761}
2762
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002763 static int
2764generate_funcref(cctx_T *cctx, char_u *name)
2765{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002766 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002767
2768 if (ufunc == NULL)
2769 return FAIL;
2770
Bram Moolenaarfe465a02020-07-07 22:50:12 +02002771 return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
2772 ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002773}
2774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775/*
2776 * Compile a variable name into a load instruction.
2777 * "end" points to just after the name.
2778 * When "error" is FALSE do not give an error when not found.
2779 */
2780 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002781compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782{
2783 type_T *type;
2784 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002785 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002787 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788
2789 if (*(*arg + 1) == ':')
2790 {
2791 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002792 if (end <= *arg + 2)
2793 name = vim_strsave((char_u *)"[empty]");
2794 else
2795 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 if (name == NULL)
2797 return FAIL;
2798
2799 if (**arg == 'v')
2800 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002801 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 }
2803 else if (**arg == 'g')
2804 {
2805 // Global variables can be defined later, thus we don't check if it
2806 // exists, give error at runtime.
2807 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2808 }
2809 else if (**arg == 's')
2810 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002811 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002813 else if (**arg == 'b')
2814 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002815 // Buffer-local variables can be defined later, thus we don't check
2816 // if it exists, give error at runtime.
2817 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002818 }
2819 else if (**arg == 'w')
2820 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002821 // Window-local variables can be defined later, thus we don't check
2822 // if it exists, give error at runtime.
2823 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002824 }
2825 else if (**arg == 't')
2826 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002827 // Tabpage-local variables can be defined later, thus we don't
2828 // check if it exists, give error at runtime.
2829 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 else
2832 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002833 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 goto theend;
2835 }
2836 }
2837 else
2838 {
2839 size_t len = end - *arg;
2840 int idx;
2841 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002842 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843
2844 name = vim_strnsave(*arg, end - *arg);
2845 if (name == NULL)
2846 return FAIL;
2847
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002848 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002850 if (!gen_load_outer)
2851 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 }
2853 else
2854 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002855 lvar_T *lvar = lookup_local(*arg, len, cctx);
2856
2857 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002859 type = lvar->lv_type;
2860 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002861 if (lvar->lv_from_outer)
2862 gen_load_outer = TRUE;
2863 else
2864 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 }
2866 else
2867 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002868 // "var" can be script-local even without using "s:" if it
2869 // already exists.
2870 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2871 == SCRIPT_VERSION_VIM9
2872 || lookup_script(*arg, len) == OK)
2873 res = compile_load_scriptvar(cctx, name, *arg, &end,
2874 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002875
Bram Moolenaara5565e42020-05-09 15:44:01 +02002876 // When the name starts with an uppercase letter or "x:" it
2877 // can be a user defined function.
2878 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2879 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 }
2881 }
2882 if (gen_load)
2883 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002884 if (gen_load_outer)
2885 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 }
2887
2888 *arg = end;
2889
2890theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002891 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 semsg(_(e_var_notfound), name);
2893 vim_free(name);
2894 return res;
2895}
2896
2897/*
2898 * Compile the argument expressions.
2899 * "arg" points to just after the "(" and is advanced to after the ")"
2900 */
2901 static int
2902compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2903{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002904 char_u *p = *arg;
2905 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906
Bram Moolenaare6085c52020-04-12 20:19:16 +02002907 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002909 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2910 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002911 if (*p == ')')
2912 {
2913 *arg = p + 1;
2914 return OK;
2915 }
2916
Bram Moolenaara5565e42020-05-09 15:44:01 +02002917 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 return FAIL;
2919 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002920
2921 if (*p != ',' && *skipwhite(p) == ',')
2922 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002923 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002924 p = skipwhite(p);
2925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002927 {
2928 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002929 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002930 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002931 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002932 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002933 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002935failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002936 emsg(_(e_missing_close));
2937 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938}
2939
2940/*
2941 * Compile a function call: name(arg1, arg2)
2942 * "arg" points to "name", "arg + varlen" to the "(".
2943 * "argcount_init" is 1 for "value->method()"
2944 * Instructions:
2945 * EVAL arg1
2946 * EVAL arg2
2947 * BCALL / DCALL / UCALL
2948 */
2949 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002950compile_call(
2951 char_u **arg,
2952 size_t varlen,
2953 cctx_T *cctx,
2954 ppconst_T *ppconst,
2955 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956{
2957 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002958 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 int argcount = argcount_init;
2960 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002961 char_u fname_buf[FLEN_FIXED + 1];
2962 char_u *tofree = NULL;
2963 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002965 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966
Bram Moolenaara5565e42020-05-09 15:44:01 +02002967 // we can evaluate "has('name')" at compile time
2968 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2969 {
2970 char_u *s = skipwhite(*arg + varlen + 1);
2971 typval_T argvars[2];
2972
2973 argvars[0].v_type = VAR_UNKNOWN;
2974 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002975 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002976 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002977 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002978 s = skipwhite(s);
2979 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2980 {
2981 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2982
2983 *arg = s + 1;
2984 argvars[1].v_type = VAR_UNKNOWN;
2985 tv->v_type = VAR_NUMBER;
2986 tv->vval.v_number = 0;
2987 f_has(argvars, tv);
2988 clear_tv(&argvars[0]);
2989 ++ppconst->pp_used;
2990 return OK;
2991 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002992 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002993 }
2994
2995 if (generate_ppconst(cctx, ppconst) == FAIL)
2996 return FAIL;
2997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 if (varlen >= sizeof(namebuf))
2999 {
3000 semsg(_("E1011: name too long: %s"), name);
3001 return FAIL;
3002 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003003 vim_strncpy(namebuf, *arg, varlen);
3004 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005
3006 *arg = skipwhite(*arg + varlen + 1);
3007 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003008 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003010 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 {
3012 int idx;
3013
3014 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003015 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003017 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003018 else
3019 semsg(_(e_unknownfunc), namebuf);
3020 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 }
3022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003024 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003026 {
3027 res = generate_CALL(cctx, ufunc, argcount);
3028 goto theend;
3029 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030
3031 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003032 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003034 if (STRNCMP(namebuf, "g:", 2) != 0
3035 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003036 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003037 garray_T *stack = &cctx->ctx_type_stack;
3038 type_T *type;
3039
3040 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3041 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003042 goto theend;
3043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003045 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003046 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003047 if (STRNCMP(namebuf, "g:", 2) == 0)
3048 res = generate_UCALL(cctx, name, argcount);
3049 else
3050 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003051
3052theend:
3053 vim_free(tofree);
3054 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055}
3056
3057// like NAMESPACE_CHAR but with 'a' and 'l'.
3058#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3059
3060/*
3061 * Find the end of a variable or function name. Unlike find_name_end() this
3062 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003063 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 * Return a pointer to just after the name. Equal to "arg" if there is no
3065 * valid name.
3066 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003067 static char_u *
3068to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069{
3070 char_u *p;
3071
3072 // Quick check for valid starting character.
3073 if (!eval_isnamec1(*arg))
3074 return arg;
3075
3076 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3077 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3078 // and can be used in slice "[n:]".
3079 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003080 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3082 break;
3083 return p;
3084}
3085
3086/*
3087 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003088 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 */
3090 char_u *
3091to_name_const_end(char_u *arg)
3092{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003093 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 typval_T rettv;
3095
3096 if (p == arg && *arg == '[')
3097 {
3098
3099 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003100 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 p = arg;
3102 }
3103 else if (p == arg && *arg == '#' && arg[1] == '{')
3104 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003105 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003107 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 p = arg;
3109 }
3110 else if (p == arg && *arg == '{')
3111 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003112 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003114 // Can be "{x -> ret}()".
3115 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003117 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 if (ret != OK)
3119 p = arg;
3120 }
3121
3122 return p;
3123}
3124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125/*
3126 * parse a list: [expr, expr]
3127 * "*arg" points to the '['.
3128 */
3129 static int
3130compile_list(char_u **arg, cctx_T *cctx)
3131{
3132 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003133 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 int count = 0;
3135
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003136 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003138 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003139 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003140 semsg(_(e_list_end), *arg);
3141 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003142 }
3143 if (*p == ']')
3144 {
3145 ++p;
3146 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003147 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003148 p += STRLEN(p);
3149 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003150 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003151 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 break;
3153 ++count;
3154 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003155 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003157 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3158 {
3159 semsg(_(e_white_after), ",");
3160 return FAIL;
3161 }
3162 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003163 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 p = skipwhite(p);
3165 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003166 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167
3168 generate_NEWLIST(cctx, count);
3169 return OK;
3170}
3171
3172/*
3173 * parse a lambda: {arg, arg -> expr}
3174 * "*arg" points to the '{'.
3175 */
3176 static int
3177compile_lambda(char_u **arg, cctx_T *cctx)
3178{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 typval_T rettv;
3180 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003181 evalarg_T evalarg;
3182
3183 CLEAR_FIELD(evalarg);
3184 evalarg.eval_flags = EVAL_EVALUATE;
3185 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186
3187 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003188 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003192 ++ufunc->uf_refcount;
3193 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003194 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195
3196 // The function will have one line: "return {expr}".
3197 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003198 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003200 clear_evalarg(&evalarg, NULL);
3201
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003202 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003203 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003204
3205 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 return FAIL;
3207}
3208
3209/*
3210 * Compile a lamda call: expr->{lambda}(args)
3211 * "arg" points to the "{".
3212 */
3213 static int
3214compile_lambda_call(char_u **arg, cctx_T *cctx)
3215{
3216 ufunc_T *ufunc;
3217 typval_T rettv;
3218 int argcount = 1;
3219 int ret = FAIL;
3220
3221 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003222 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 return FAIL;
3224
3225 if (**arg != '(')
3226 {
3227 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003228 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 else
3230 semsg(_(e_missing_paren), "lambda");
3231 clear_tv(&rettv);
3232 return FAIL;
3233 }
3234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 ufunc = rettv.vval.v_partial->pt_func;
3236 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003237 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003238 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003239
3240 // The function will have one line: "return {expr}".
3241 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003242 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243
3244 // compile the arguments
3245 *arg = skipwhite(*arg + 1);
3246 if (compile_arguments(arg, cctx, &argcount) == OK)
3247 // call the compiled function
3248 ret = generate_CALL(cctx, ufunc, argcount);
3249
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003250 if (ret == FAIL)
3251 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 return ret;
3253}
3254
3255/*
3256 * parse a dict: {'key': val} or #{key: val}
3257 * "*arg" points to the '{'.
3258 */
3259 static int
3260compile_dict(char_u **arg, cctx_T *cctx, int literal)
3261{
3262 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003263 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 int count = 0;
3265 dict_T *d = dict_alloc();
3266 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003267 char_u *whitep = *arg;
3268 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269
3270 if (d == NULL)
3271 return FAIL;
3272 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003273 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 {
3275 char_u *key = NULL;
3276
Bram Moolenaar23c55272020-06-21 16:58:13 +02003277 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003278 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003279 *arg = NULL;
3280 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003281 }
3282
3283 if (**arg == '}')
3284 break;
3285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 if (literal)
3287 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003288 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289
Bram Moolenaar2c330432020-04-13 14:41:35 +02003290 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 {
3292 semsg(_("E1014: Invalid key: %s"), *arg);
3293 return FAIL;
3294 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003295 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 if (generate_PUSHS(cctx, key) == FAIL)
3297 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003298 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 }
3300 else
3301 {
3302 isn_T *isn;
3303
Bram Moolenaara5565e42020-05-09 15:44:01 +02003304 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3307 if (isn->isn_type == ISN_PUSHS)
3308 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003309 else
3310 {
3311 type_T *keytype = ((type_T **)stack->ga_data)
3312 [stack->ga_len - 1];
3313 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3314 return FAIL;
3315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 }
3317
3318 // Check for duplicate keys, if using string keys.
3319 if (key != NULL)
3320 {
3321 item = dict_find(d, key, -1);
3322 if (item != NULL)
3323 {
3324 semsg(_(e_duplicate_key), key);
3325 goto failret;
3326 }
3327 item = dictitem_alloc(key);
3328 if (item != NULL)
3329 {
3330 item->di_tv.v_type = VAR_UNKNOWN;
3331 item->di_tv.v_lock = 0;
3332 if (dict_add(d, item) == FAIL)
3333 dictitem_free(item);
3334 }
3335 }
3336
3337 *arg = skipwhite(*arg);
3338 if (**arg != ':')
3339 {
3340 semsg(_(e_missing_dict_colon), *arg);
3341 return FAIL;
3342 }
3343
Bram Moolenaar2c330432020-04-13 14:41:35 +02003344 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003346 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003347 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003348 *arg = NULL;
3349 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003350 }
3351
Bram Moolenaara5565e42020-05-09 15:44:01 +02003352 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 return FAIL;
3354 ++count;
3355
Bram Moolenaar2c330432020-04-13 14:41:35 +02003356 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003357 *arg = skipwhite(*arg);
3358 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003359 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003360 *arg = NULL;
3361 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 if (**arg == '}')
3364 break;
3365 if (**arg != ',')
3366 {
3367 semsg(_(e_missing_dict_comma), *arg);
3368 goto failret;
3369 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003370 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 *arg = skipwhite(*arg + 1);
3372 }
3373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 *arg = *arg + 1;
3375
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003376 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003377 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003378 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003379 *arg += STRLEN(*arg);
3380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 dict_unref(d);
3382 return generate_NEWDICT(cctx, count);
3383
3384failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003385 if (*arg == NULL)
3386 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 dict_unref(d);
3388 return FAIL;
3389}
3390
3391/*
3392 * Compile "&option".
3393 */
3394 static int
3395compile_get_option(char_u **arg, cctx_T *cctx)
3396{
3397 typval_T rettv;
3398 char_u *start = *arg;
3399 int ret;
3400
3401 // parse the option and get the current value to get the type.
3402 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003403 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 if (ret == OK)
3405 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003406 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 char_u *name = vim_strnsave(start, *arg - start);
3408 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3409
3410 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3411 vim_free(name);
3412 }
3413 clear_tv(&rettv);
3414
3415 return ret;
3416}
3417
3418/*
3419 * Compile "$VAR".
3420 */
3421 static int
3422compile_get_env(char_u **arg, cctx_T *cctx)
3423{
3424 char_u *start = *arg;
3425 int len;
3426 int ret;
3427 char_u *name;
3428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 ++*arg;
3430 len = get_env_len(arg);
3431 if (len == 0)
3432 {
3433 semsg(_(e_syntax_at), start - 1);
3434 return FAIL;
3435 }
3436
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003437 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 name = vim_strnsave(start, len + 1);
3439 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3440 vim_free(name);
3441 return ret;
3442}
3443
3444/*
3445 * Compile "@r".
3446 */
3447 static int
3448compile_get_register(char_u **arg, cctx_T *cctx)
3449{
3450 int ret;
3451
3452 ++*arg;
3453 if (**arg == NUL)
3454 {
3455 semsg(_(e_syntax_at), *arg - 1);
3456 return FAIL;
3457 }
3458 if (!valid_yank_reg(**arg, TRUE))
3459 {
3460 emsg_invreg(**arg);
3461 return FAIL;
3462 }
3463 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3464 ++*arg;
3465 return ret;
3466}
3467
3468/*
3469 * Apply leading '!', '-' and '+' to constant "rettv".
3470 */
3471 static int
3472apply_leader(typval_T *rettv, char_u *start, char_u *end)
3473{
3474 char_u *p = end;
3475
3476 // this works from end to start
3477 while (p > start)
3478 {
3479 --p;
3480 if (*p == '-' || *p == '+')
3481 {
3482 // only '-' has an effect, for '+' we only check the type
3483#ifdef FEAT_FLOAT
3484 if (rettv->v_type == VAR_FLOAT)
3485 {
3486 if (*p == '-')
3487 rettv->vval.v_float = -rettv->vval.v_float;
3488 }
3489 else
3490#endif
3491 {
3492 varnumber_T val;
3493 int error = FALSE;
3494
3495 // tv_get_number_chk() accepts a string, but we don't want that
3496 // here
3497 if (check_not_string(rettv) == FAIL)
3498 return FAIL;
3499 val = tv_get_number_chk(rettv, &error);
3500 clear_tv(rettv);
3501 if (error)
3502 return FAIL;
3503 if (*p == '-')
3504 val = -val;
3505 rettv->v_type = VAR_NUMBER;
3506 rettv->vval.v_number = val;
3507 }
3508 }
3509 else
3510 {
3511 int v = tv2bool(rettv);
3512
3513 // '!' is permissive in the type.
3514 clear_tv(rettv);
3515 rettv->v_type = VAR_BOOL;
3516 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3517 }
3518 }
3519 return OK;
3520}
3521
3522/*
3523 * Recognize v: variables that are constants and set "rettv".
3524 */
3525 static void
3526get_vim_constant(char_u **arg, typval_T *rettv)
3527{
3528 if (STRNCMP(*arg, "v:true", 6) == 0)
3529 {
3530 rettv->v_type = VAR_BOOL;
3531 rettv->vval.v_number = VVAL_TRUE;
3532 *arg += 6;
3533 }
3534 else if (STRNCMP(*arg, "v:false", 7) == 0)
3535 {
3536 rettv->v_type = VAR_BOOL;
3537 rettv->vval.v_number = VVAL_FALSE;
3538 *arg += 7;
3539 }
3540 else if (STRNCMP(*arg, "v:null", 6) == 0)
3541 {
3542 rettv->v_type = VAR_SPECIAL;
3543 rettv->vval.v_number = VVAL_NULL;
3544 *arg += 6;
3545 }
3546 else if (STRNCMP(*arg, "v:none", 6) == 0)
3547 {
3548 rettv->v_type = VAR_SPECIAL;
3549 rettv->vval.v_number = VVAL_NONE;
3550 *arg += 6;
3551 }
3552}
3553
Bram Moolenaar61a89812020-05-07 16:58:17 +02003554 static exptype_T
3555get_compare_type(char_u *p, int *len, int *type_is)
3556{
3557 exptype_T type = EXPR_UNKNOWN;
3558 int i;
3559
3560 switch (p[0])
3561 {
3562 case '=': if (p[1] == '=')
3563 type = EXPR_EQUAL;
3564 else if (p[1] == '~')
3565 type = EXPR_MATCH;
3566 break;
3567 case '!': if (p[1] == '=')
3568 type = EXPR_NEQUAL;
3569 else if (p[1] == '~')
3570 type = EXPR_NOMATCH;
3571 break;
3572 case '>': if (p[1] != '=')
3573 {
3574 type = EXPR_GREATER;
3575 *len = 1;
3576 }
3577 else
3578 type = EXPR_GEQUAL;
3579 break;
3580 case '<': if (p[1] != '=')
3581 {
3582 type = EXPR_SMALLER;
3583 *len = 1;
3584 }
3585 else
3586 type = EXPR_SEQUAL;
3587 break;
3588 case 'i': if (p[1] == 's')
3589 {
3590 // "is" and "isnot"; but not a prefix of a name
3591 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3592 *len = 5;
3593 i = p[*len];
3594 if (!isalnum(i) && i != '_')
3595 {
3596 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3597 *type_is = TRUE;
3598 }
3599 }
3600 break;
3601 }
3602 return type;
3603}
3604
3605/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 * Compile code to apply '-', '+' and '!'.
3607 */
3608 static int
3609compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3610{
3611 char_u *p = end;
3612
3613 // this works from end to start
3614 while (p > start)
3615 {
3616 --p;
3617 if (*p == '-' || *p == '+')
3618 {
3619 int negate = *p == '-';
3620 isn_T *isn;
3621
3622 // TODO: check type
3623 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3624 {
3625 --p;
3626 if (*p == '-')
3627 negate = !negate;
3628 }
3629 // only '-' has an effect, for '+' we only check the type
3630 if (negate)
3631 isn = generate_instr(cctx, ISN_NEGATENR);
3632 else
3633 isn = generate_instr(cctx, ISN_CHECKNR);
3634 if (isn == NULL)
3635 return FAIL;
3636 }
3637 else
3638 {
3639 int invert = TRUE;
3640
3641 while (p > start && p[-1] == '!')
3642 {
3643 --p;
3644 invert = !invert;
3645 }
3646 if (generate_2BOOL(cctx, invert) == FAIL)
3647 return FAIL;
3648 }
3649 }
3650 return OK;
3651}
3652
3653/*
3654 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003655 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 */
3657 static int
3658compile_subscript(
3659 char_u **arg,
3660 cctx_T *cctx,
3661 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003662 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003663 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664{
3665 for (;;)
3666 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003667 char_u *p = skipwhite(*arg);
3668
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003669 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003670 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003671 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003672
3673 // If a following line starts with "->{" or "->X" advance to that
3674 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003675 // Also if a following line starts with ".x".
3676 if (next != NULL &&
3677 ((next[0] == '-' && next[1] == '>'
3678 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3679 || (next[0] == '.' && ASCII_ISALPHA(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003680 {
3681 next = next_line_from_context(cctx, TRUE);
3682 if (next == NULL)
3683 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003684 *arg = next;
3685 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003686 }
3687 }
3688
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003689 if (*p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003691 garray_T *stack = &cctx->ctx_type_stack;
3692 type_T *type;
3693 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003695 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003696 return FAIL;
3697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003699 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3700
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003701 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3703 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003704 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 return FAIL;
3706 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003707 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003709 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003710 return FAIL;
3711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 // something->method()
3713 // Apply the '!', '-' and '+' first:
3714 // -1.0->func() works like (-1.0)->func()
3715 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3716 return FAIL;
3717 *start_leader = end_leader; // don't apply again later
3718
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003719 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003720 *arg = skipwhite(p);
3721 if (may_get_next_line(p, arg, cctx) == FAIL)
3722 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 if (**arg == '{')
3724 {
3725 // lambda call: list->{lambda}
3726 if (compile_lambda_call(arg, cctx) == FAIL)
3727 return FAIL;
3728 }
3729 else
3730 {
3731 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003732 p = *arg;
3733 if (ASCII_ISALPHA(*p) && p[1] == ':')
3734 p += 2;
3735 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 ;
3737 if (*p != '(')
3738 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003739 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 return FAIL;
3741 }
3742 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003743 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 return FAIL;
3745 }
3746 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003747 else if (*p == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003749 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003750 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003751 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003752
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003753 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003754 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003755 // TODO: blob index
3756 // TODO: more arguments
3757 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003758 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003759 return FAIL;
3760
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003761 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003762 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003763 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003764 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003765 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 return FAIL;
3767
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003768 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3769 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 if (**arg != ']')
3771 {
3772 emsg(_(e_missbrac));
3773 return FAIL;
3774 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003775 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003777 // We can index a list and a dict. If we don't know the type
3778 // we can use the index value type.
3779 // TODO: If we don't know use an instruction to figure it out at
3780 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003781 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003782 vtype = (*typep)->tt_type;
3783 if (*typep == &t_any)
3784 {
3785 type_T *valtype = ((type_T **)stack->ga_data)
3786 [stack->ga_len - 1];
3787 if (valtype == &t_string)
3788 vtype = VAR_DICT;
3789 }
3790 if (vtype == VAR_DICT)
3791 {
3792 if ((*typep)->tt_type == VAR_DICT)
3793 *typep = (*typep)->tt_member;
3794 else if (need_type(*typep, &t_dict_any, -2, cctx, FALSE)
3795 == FAIL)
3796 return FAIL;
3797 if (may_generate_2STRING(-1, cctx) == FAIL)
3798 return FAIL;
3799 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3800 return FAIL;
3801 }
3802 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003803 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003804 if ((*typep)->tt_type == VAR_LIST)
3805 *typep = (*typep)->tt_member;
3806 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3807 return FAIL;
3808 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003809 else
3810 {
3811 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003812 return FAIL;
3813 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003815 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003817 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003818 return FAIL;
3819
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003820 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003821 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3822 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003824 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 if (eval_isnamec1(*p))
3826 while (eval_isnamec(*p))
3827 MB_PTR_ADV(p);
3828 if (p == *arg)
3829 {
3830 semsg(_(e_syntax_at), *arg);
3831 return FAIL;
3832 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003833 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 return FAIL;
3835 *arg = p;
3836 }
3837 else
3838 break;
3839 }
3840
3841 // TODO - see handle_subscript():
3842 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3843 // Don't do this when "Func" is already a partial that was bound
3844 // explicitly (pt_auto is FALSE).
3845
3846 return OK;
3847}
3848
3849/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003850 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3851 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003853 * If the value is a constant "ppconst->pp_ret" will be set.
3854 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003855 *
3856 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 */
3858
3859/*
3860 * number number constant
3861 * 0zFFFFFFFF Blob constant
3862 * "string" string constant
3863 * 'string' literal string constant
3864 * &option-name option value
3865 * @r register contents
3866 * identifier variable value
3867 * function() function call
3868 * $VAR environment variable
3869 * (expression) nested expression
3870 * [expr, expr] List
3871 * {key: val, key: val} Dictionary
3872 * #{key: val, key: val} Dictionary with literal keys
3873 *
3874 * Also handle:
3875 * ! in front logical NOT
3876 * - in front unary minus
3877 * + in front unary plus (ignored)
3878 * trailing (arg) funcref/partial call
3879 * trailing [] subscript in String or List
3880 * trailing .name entry in Dictionary
3881 * trailing ->name() method call
3882 */
3883 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003884compile_expr7(
3885 char_u **arg,
3886 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003887 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 char_u *start_leader, *end_leader;
3890 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003891 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003892 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893
3894 /*
3895 * Skip '!', '-' and '+' characters. They are handled later.
3896 */
3897 start_leader = *arg;
3898 while (**arg == '!' || **arg == '-' || **arg == '+')
3899 *arg = skipwhite(*arg + 1);
3900 end_leader = *arg;
3901
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003902 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 switch (**arg)
3904 {
3905 /*
3906 * Number constant.
3907 */
3908 case '0': // also for blob starting with 0z
3909 case '1':
3910 case '2':
3911 case '3':
3912 case '4':
3913 case '5':
3914 case '6':
3915 case '7':
3916 case '8':
3917 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003918 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 return FAIL;
3920 break;
3921
3922 /*
3923 * String constant: "string".
3924 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003925 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 return FAIL;
3927 break;
3928
3929 /*
3930 * Literal string constant: 'str''ing'.
3931 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003932 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 return FAIL;
3934 break;
3935
3936 /*
3937 * Constant Vim variable.
3938 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003939 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 ret = NOTDONE;
3941 break;
3942
3943 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003944 * "true" constant
3945 */
3946 case 't': if (STRNCMP(*arg, "true", 4) == 0
3947 && !eval_isnamec((*arg)[4]))
3948 {
3949 *arg += 4;
3950 rettv->v_type = VAR_BOOL;
3951 rettv->vval.v_number = VVAL_TRUE;
3952 }
3953 else
3954 ret = NOTDONE;
3955 break;
3956
3957 /*
3958 * "false" constant
3959 */
3960 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3961 && !eval_isnamec((*arg)[5]))
3962 {
3963 *arg += 5;
3964 rettv->v_type = VAR_BOOL;
3965 rettv->vval.v_number = VVAL_FALSE;
3966 }
3967 else
3968 ret = NOTDONE;
3969 break;
3970
3971 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 * List: [expr, expr]
3973 */
3974 case '[': ret = compile_list(arg, cctx);
3975 break;
3976
3977 /*
3978 * Dictionary: #{key: val, key: val}
3979 */
3980 case '#': if ((*arg)[1] == '{')
3981 {
3982 ++*arg;
3983 ret = compile_dict(arg, cctx, TRUE);
3984 }
3985 else
3986 ret = NOTDONE;
3987 break;
3988
3989 /*
3990 * Lambda: {arg, arg -> expr}
3991 * Dictionary: {'key': val, 'key': val}
3992 */
3993 case '{': {
3994 char_u *start = skipwhite(*arg + 1);
3995
3996 // Find out what comes after the arguments.
3997 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003998 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 if (ret != FAIL && *start == '>')
4000 ret = compile_lambda(arg, cctx);
4001 else
4002 ret = compile_dict(arg, cctx, FALSE);
4003 }
4004 break;
4005
4006 /*
4007 * Option value: &name
4008 */
4009 case '&': ret = compile_get_option(arg, cctx);
4010 break;
4011
4012 /*
4013 * Environment variable: $VAR.
4014 */
4015 case '$': ret = compile_get_env(arg, cctx);
4016 break;
4017
4018 /*
4019 * Register contents: @r.
4020 */
4021 case '@': ret = compile_get_register(arg, cctx);
4022 break;
4023 /*
4024 * nested expression: (expression).
4025 */
4026 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004027
4028 // recursive!
4029 if (ppconst->pp_used <= PPSIZE - 10)
4030 {
4031 ret = compile_expr1(arg, cctx, ppconst);
4032 }
4033 else
4034 {
4035 // Not enough space in ppconst, flush constants.
4036 if (generate_ppconst(cctx, ppconst) == FAIL)
4037 return FAIL;
4038 ret = compile_expr0(arg, cctx);
4039 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 *arg = skipwhite(*arg);
4041 if (**arg == ')')
4042 ++*arg;
4043 else if (ret == OK)
4044 {
4045 emsg(_(e_missing_close));
4046 ret = FAIL;
4047 }
4048 break;
4049
4050 default: ret = NOTDONE;
4051 break;
4052 }
4053 if (ret == FAIL)
4054 return FAIL;
4055
Bram Moolenaar1c747212020-05-09 18:28:34 +02004056 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 {
4058 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004059 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004061 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 return FAIL;
4063 }
4064 start_leader = end_leader; // don't apply again below
4065
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004066 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004067 clear_tv(rettv);
4068 else
4069 // A constant expression can possibly be handled compile time,
4070 // return the value instead of generating code.
4071 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 }
4073 else if (ret == NOTDONE)
4074 {
4075 char_u *p;
4076 int r;
4077
4078 if (!eval_isnamec1(**arg))
4079 {
4080 semsg(_("E1015: Name expected: %s"), *arg);
4081 return FAIL;
4082 }
4083
4084 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004085 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004087 {
4088 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004091 {
4092 if (generate_ppconst(cctx, ppconst) == FAIL)
4093 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 if (r == FAIL)
4097 return FAIL;
4098 }
4099
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004100 // Handle following "[]", ".member", etc.
4101 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004102 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004103 ppconst) == FAIL)
4104 return FAIL;
4105 if (ppconst->pp_used > 0)
4106 {
4107 // apply the '!', '-' and '+' before the constant
4108 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4109 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4110 return FAIL;
4111 return OK;
4112 }
4113 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004115 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116}
4117
4118/*
4119 * * number multiplication
4120 * / number division
4121 * % number modulo
4122 */
4123 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004124compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125{
4126 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004127 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004128 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004130 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004131 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 return FAIL;
4133
4134 /*
4135 * Repeat computing, until no "*", "/" or "%" is following.
4136 */
4137 for (;;)
4138 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004139 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 if (*op != '*' && *op != '/' && *op != '%')
4141 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004142 if (next != NULL)
4143 {
4144 *arg = next_line_from_context(cctx, TRUE);
4145 op = skipwhite(*arg);
4146 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004147
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004148 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 {
4150 char_u buf[3];
4151
4152 vim_strncpy(buf, op, 1);
4153 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004154 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 }
4156 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004157 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004158 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004160 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004161 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004163
4164 if (ppconst->pp_used == ppconst_used + 2
4165 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4166 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004167 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004168 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4169 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004170 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004172 // both are numbers: compute the result
4173 switch (*op)
4174 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004175 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004176 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004177 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004178 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004179 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004180 break;
4181 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004182 tv1->vval.v_number = res;
4183 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004184 }
4185 else
4186 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004187 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004188 generate_two_op(cctx, op);
4189 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 }
4191
4192 return OK;
4193}
4194
4195/*
4196 * + number addition
4197 * - number subtraction
4198 * .. string concatenation
4199 */
4200 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004201compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202{
4203 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004204 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004206 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207
4208 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004209 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 return FAIL;
4211
4212 /*
4213 * Repeat computing, until no "+", "-" or ".." is following.
4214 */
4215 for (;;)
4216 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004217 op = may_peek_next_line(cctx, *arg, &next);
4218 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 break;
4220 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004221 if (next != NULL)
4222 {
4223 *arg = next_line_from_context(cctx, TRUE);
4224 op = skipwhite(*arg);
4225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004227 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 {
4229 char_u buf[3];
4230
4231 vim_strncpy(buf, op, oplen);
4232 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004233 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 }
4235
4236 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004237 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004238 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004240 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004241 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 return FAIL;
4243
Bram Moolenaara5565e42020-05-09 15:44:01 +02004244 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004245 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004246 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4247 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4248 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4249 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004251 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4252 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004253
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004254 // concat/subtract/add constant numbers
4255 if (*op == '+')
4256 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4257 else if (*op == '-')
4258 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4259 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004260 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004261 // concatenate constant strings
4262 char_u *s1 = tv1->vval.v_string;
4263 char_u *s2 = tv2->vval.v_string;
4264 size_t len1 = STRLEN(s1);
4265
4266 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4267 if (tv1->vval.v_string == NULL)
4268 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004269 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004270 return FAIL;
4271 }
4272 mch_memmove(tv1->vval.v_string, s1, len1);
4273 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004274 vim_free(s1);
4275 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004276 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004277 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 }
4279 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004280 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004281 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004282 if (*op == '.')
4283 {
4284 if (may_generate_2STRING(-2, cctx) == FAIL
4285 || may_generate_2STRING(-1, cctx) == FAIL)
4286 return FAIL;
4287 generate_instr_drop(cctx, ISN_CONCAT, 1);
4288 }
4289 else
4290 generate_two_op(cctx, op);
4291 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 }
4293
4294 return OK;
4295}
4296
4297/*
4298 * expr5a == expr5b
4299 * expr5a =~ expr5b
4300 * expr5a != expr5b
4301 * expr5a !~ expr5b
4302 * expr5a > expr5b
4303 * expr5a >= expr5b
4304 * expr5a < expr5b
4305 * expr5a <= expr5b
4306 * expr5a is expr5b
4307 * expr5a isnot expr5b
4308 *
4309 * Produces instructions:
4310 * EVAL expr5a Push result of "expr5a"
4311 * EVAL expr5b Push result of "expr5b"
4312 * COMPARE one of the compare instructions
4313 */
4314 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004315compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316{
4317 exptype_T type = EXPR_UNKNOWN;
4318 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004319 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004322 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323
4324 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004325 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 return FAIL;
4327
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004328 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004329 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330
4331 /*
4332 * If there is a comparative operator, use it.
4333 */
4334 if (type != EXPR_UNKNOWN)
4335 {
4336 int ic = FALSE; // Default: do not ignore case
4337
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004338 if (next != NULL)
4339 {
4340 *arg = next_line_from_context(cctx, TRUE);
4341 p = skipwhite(*arg);
4342 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 if (type_is && (p[len] == '?' || p[len] == '#'))
4344 {
4345 semsg(_(e_invexpr2), *arg);
4346 return FAIL;
4347 }
4348 // extra question mark appended: ignore case
4349 if (p[len] == '?')
4350 {
4351 ic = TRUE;
4352 ++len;
4353 }
4354 // extra '#' appended: match case (ignored)
4355 else if (p[len] == '#')
4356 ++len;
4357 // nothing appended: match case
4358
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004359 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 {
4361 char_u buf[7];
4362
4363 vim_strncpy(buf, p, len);
4364 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004365 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 }
4367
4368 // get the second variable
4369 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004370 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004371 return FAIL;
4372
Bram Moolenaara5565e42020-05-09 15:44:01 +02004373 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 return FAIL;
4375
Bram Moolenaara5565e42020-05-09 15:44:01 +02004376 if (ppconst->pp_used == ppconst_used + 2)
4377 {
4378 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4379 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4380 int ret;
4381
4382 // Both sides are a constant, compute the result now.
4383 // First check for a valid combination of types, this is more
4384 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004385 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004386 ret = FAIL;
4387 else
4388 {
4389 ret = typval_compare(tv1, tv2, type, ic);
4390 tv1->v_type = VAR_BOOL;
4391 tv1->vval.v_number = tv1->vval.v_number
4392 ? VVAL_TRUE : VVAL_FALSE;
4393 clear_tv(tv2);
4394 --ppconst->pp_used;
4395 }
4396 return ret;
4397 }
4398
4399 generate_ppconst(cctx, ppconst);
4400 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 }
4402
4403 return OK;
4404}
4405
Bram Moolenaar7f141552020-05-09 17:35:53 +02004406static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408/*
4409 * Compile || or &&.
4410 */
4411 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004412compile_and_or(
4413 char_u **arg,
4414 cctx_T *cctx,
4415 char *op,
4416 ppconst_T *ppconst,
4417 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004419 char_u *next;
4420 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 int opchar = *op;
4422
4423 if (p[0] == opchar && p[1] == opchar)
4424 {
4425 garray_T *instr = &cctx->ctx_instr;
4426 garray_T end_ga;
4427
4428 /*
4429 * Repeat until there is no following "||" or "&&"
4430 */
4431 ga_init2(&end_ga, sizeof(int), 10);
4432 while (p[0] == opchar && p[1] == opchar)
4433 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004434 if (next != NULL)
4435 {
4436 *arg = next_line_from_context(cctx, TRUE);
4437 p = skipwhite(*arg);
4438 }
4439
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004440 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4441 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004443 return FAIL;
4444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445
Bram Moolenaara5565e42020-05-09 15:44:01 +02004446 // TODO: use ppconst if the value is a constant
4447 generate_ppconst(cctx, ppconst);
4448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 if (ga_grow(&end_ga, 1) == FAIL)
4450 {
4451 ga_clear(&end_ga);
4452 return FAIL;
4453 }
4454 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4455 ++end_ga.ga_len;
4456 generate_JUMP(cctx, opchar == '|'
4457 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4458
4459 // eval the next expression
4460 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004461 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004462 return FAIL;
4463
Bram Moolenaara5565e42020-05-09 15:44:01 +02004464 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4465 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 {
4467 ga_clear(&end_ga);
4468 return FAIL;
4469 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004470
4471 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004473 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474
4475 // Fill in the end label in all jumps.
4476 while (end_ga.ga_len > 0)
4477 {
4478 isn_T *isn;
4479
4480 --end_ga.ga_len;
4481 isn = ((isn_T *)instr->ga_data)
4482 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4483 isn->isn_arg.jump.jump_where = instr->ga_len;
4484 }
4485 ga_clear(&end_ga);
4486 }
4487
4488 return OK;
4489}
4490
4491/*
4492 * expr4a && expr4a && expr4a logical AND
4493 *
4494 * Produces instructions:
4495 * EVAL expr4a Push result of "expr4a"
4496 * JUMP_AND_KEEP_IF_FALSE end
4497 * EVAL expr4b Push result of "expr4b"
4498 * JUMP_AND_KEEP_IF_FALSE end
4499 * EVAL expr4c Push result of "expr4c"
4500 * end:
4501 */
4502 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004503compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004505 int ppconst_used = ppconst->pp_used;
4506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004508 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 return FAIL;
4510
4511 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004512 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513}
4514
4515/*
4516 * expr3a || expr3b || expr3c logical OR
4517 *
4518 * Produces instructions:
4519 * EVAL expr3a Push result of "expr3a"
4520 * JUMP_AND_KEEP_IF_TRUE end
4521 * EVAL expr3b Push result of "expr3b"
4522 * JUMP_AND_KEEP_IF_TRUE end
4523 * EVAL expr3c Push result of "expr3c"
4524 * end:
4525 */
4526 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004527compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004529 int ppconst_used = ppconst->pp_used;
4530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004532 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 return FAIL;
4534
4535 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004536 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537}
4538
4539/*
4540 * Toplevel expression: expr2 ? expr1a : expr1b
4541 *
4542 * Produces instructions:
4543 * EVAL expr2 Push result of "expr"
4544 * JUMP_IF_FALSE alt jump if false
4545 * EVAL expr1a
4546 * JUMP_ALWAYS end
4547 * alt: EVAL expr1b
4548 * end:
4549 */
4550 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004551compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552{
4553 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004555 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556
Bram Moolenaar61a89812020-05-07 16:58:17 +02004557 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004558 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 return FAIL;
4560
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004561 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 if (*p == '?')
4563 {
4564 garray_T *instr = &cctx->ctx_instr;
4565 garray_T *stack = &cctx->ctx_type_stack;
4566 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004567 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004569 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004571 int has_const_expr = FALSE;
4572 int const_value = FALSE;
4573 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004575 if (next != NULL)
4576 {
4577 *arg = next_line_from_context(cctx, TRUE);
4578 p = skipwhite(*arg);
4579 }
4580
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004581 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4582 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004584 return FAIL;
4585 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586
Bram Moolenaara5565e42020-05-09 15:44:01 +02004587 if (ppconst->pp_used == ppconst_used + 1)
4588 {
4589 // the condition is a constant, we know whether the ? or the :
4590 // expression is to be evaluated.
4591 has_const_expr = TRUE;
4592 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4593 clear_tv(&ppconst->pp_tv[ppconst_used]);
4594 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004595 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4596 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597 }
4598 else
4599 {
4600 generate_ppconst(cctx, ppconst);
4601 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4602 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603
4604 // evaluate the second expression; any type is accepted
4605 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004606 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004607 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004608 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004609 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610
Bram Moolenaara5565e42020-05-09 15:44:01 +02004611 if (!has_const_expr)
4612 {
4613 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614
Bram Moolenaara5565e42020-05-09 15:44:01 +02004615 // remember the type and drop it
4616 --stack->ga_len;
4617 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618
Bram Moolenaara5565e42020-05-09 15:44:01 +02004619 end_idx = instr->ga_len;
4620 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4621
4622 // jump here from JUMP_IF_FALSE
4623 isn = ((isn_T *)instr->ga_data) + alt_idx;
4624 isn->isn_arg.jump.jump_where = instr->ga_len;
4625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626
4627 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004628 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 if (*p != ':')
4630 {
4631 emsg(_(e_missing_colon));
4632 return FAIL;
4633 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004634 if (next != NULL)
4635 {
4636 *arg = next_line_from_context(cctx, TRUE);
4637 p = skipwhite(*arg);
4638 }
4639
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004640 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4641 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004643 return FAIL;
4644 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645
4646 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004647 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004648 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4649 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004651 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004652 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004653 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004654 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655
Bram Moolenaara5565e42020-05-09 15:44:01 +02004656 if (!has_const_expr)
4657 {
4658 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659
Bram Moolenaara5565e42020-05-09 15:44:01 +02004660 // If the types differ, the result has a more generic type.
4661 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4662 common_type(type1, type2, &type2, cctx->ctx_type_list);
4663
4664 // jump here from JUMP_ALWAYS
4665 isn = ((isn_T *)instr->ga_data) + end_idx;
4666 isn->isn_arg.jump.jump_where = instr->ga_len;
4667 }
4668
4669 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 }
4671 return OK;
4672}
4673
4674/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004675 * Toplevel expression.
4676 */
4677 static int
4678compile_expr0(char_u **arg, cctx_T *cctx)
4679{
4680 ppconst_T ppconst;
4681
4682 CLEAR_FIELD(ppconst);
4683 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4684 {
4685 clear_ppconst(&ppconst);
4686 return FAIL;
4687 }
4688 if (generate_ppconst(cctx, &ppconst) == FAIL)
4689 return FAIL;
4690 return OK;
4691}
4692
4693/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 * compile "return [expr]"
4695 */
4696 static char_u *
4697compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4698{
4699 char_u *p = arg;
4700 garray_T *stack = &cctx->ctx_type_stack;
4701 type_T *stack_type;
4702
4703 if (*p != NUL && *p != '|' && *p != '\n')
4704 {
4705 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004706 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 return NULL;
4708
4709 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4710 if (set_return_type)
4711 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004712 else
4713 {
4714 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4715 && stack_type->tt_type != VAR_VOID
4716 && stack_type->tt_type != VAR_UNKNOWN)
4717 {
4718 emsg(_("E1096: Returning a value in a function without a return type"));
4719 return NULL;
4720 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004721 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4722 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004724 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 }
4726 else
4727 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004728 // "set_return_type" cannot be TRUE, only used for a lambda which
4729 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004730 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4731 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 {
4733 emsg(_("E1003: Missing return value"));
4734 return NULL;
4735 }
4736
4737 // No argument, return zero.
4738 generate_PUSHNR(cctx, 0);
4739 }
4740
4741 if (generate_instr(cctx, ISN_RETURN) == NULL)
4742 return NULL;
4743
4744 // "return val | endif" is possible
4745 return skipwhite(p);
4746}
4747
4748/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004749 * Get a line from the compilation context, compatible with exarg_T getline().
4750 * Return a pointer to the line in allocated memory.
4751 * Return NULL for end-of-file or some error.
4752 */
4753 static char_u *
4754exarg_getline(
4755 int c UNUSED,
4756 void *cookie,
4757 int indent UNUSED,
4758 int do_concat UNUSED)
4759{
4760 cctx_T *cctx = (cctx_T *)cookie;
4761
4762 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4763 {
4764 iemsg("Heredoc got to end");
4765 return NULL;
4766 }
4767 ++cctx->ctx_lnum;
4768 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4769 [cctx->ctx_lnum]);
4770}
4771
4772/*
4773 * Compile a nested :def command.
4774 */
4775 static char_u *
4776compile_nested_function(exarg_T *eap, cctx_T *cctx)
4777{
4778 char_u *name_start = eap->arg;
4779 char_u *name_end = to_name_end(eap->arg, FALSE);
4780 char_u *name = get_lambda_name();
4781 lvar_T *lvar;
4782 ufunc_T *ufunc;
4783
4784 eap->arg = name_end;
4785 eap->getline = exarg_getline;
4786 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004787 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004788 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004789 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004790
Bram Moolenaar822ba242020-05-24 23:00:18 +02004791 if (ufunc == NULL)
4792 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004793 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004794 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004795 return NULL;
4796
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004797 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004798 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004799 TRUE, ufunc->uf_func_type);
4800
4801 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4802 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4803 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004804
Bram Moolenaar61a89812020-05-07 16:58:17 +02004805 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004806 return (char_u *)"";
4807}
4808
4809/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810 * Return the length of an assignment operator, or zero if there isn't one.
4811 */
4812 int
4813assignment_len(char_u *p, int *heredoc)
4814{
4815 if (*p == '=')
4816 {
4817 if (p[1] == '<' && p[2] == '<')
4818 {
4819 *heredoc = TRUE;
4820 return 3;
4821 }
4822 return 1;
4823 }
4824 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4825 return 2;
4826 if (STRNCMP(p, "..=", 3) == 0)
4827 return 3;
4828 return 0;
4829}
4830
4831// words that cannot be used as a variable
4832static char *reserved[] = {
4833 "true",
4834 "false",
4835 NULL
4836};
4837
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004838typedef enum {
4839 dest_local,
4840 dest_option,
4841 dest_env,
4842 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004843 dest_buffer,
4844 dest_window,
4845 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004846 dest_vimvar,
4847 dest_script,
4848 dest_reg,
4849} assign_dest_T;
4850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004852 * Generate the load instruction for "name".
4853 */
4854 static void
4855generate_loadvar(
4856 cctx_T *cctx,
4857 assign_dest_T dest,
4858 char_u *name,
4859 lvar_T *lvar,
4860 type_T *type)
4861{
4862 switch (dest)
4863 {
4864 case dest_option:
4865 // TODO: check the option exists
4866 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4867 break;
4868 case dest_global:
4869 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4870 break;
4871 case dest_buffer:
4872 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4873 break;
4874 case dest_window:
4875 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4876 break;
4877 case dest_tab:
4878 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4879 break;
4880 case dest_script:
4881 compile_load_scriptvar(cctx,
4882 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4883 break;
4884 case dest_env:
4885 // Include $ in the name here
4886 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4887 break;
4888 case dest_reg:
4889 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4890 break;
4891 case dest_vimvar:
4892 generate_LOADV(cctx, name + 2, TRUE);
4893 break;
4894 case dest_local:
4895 if (lvar->lv_from_outer)
4896 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4897 NULL, type);
4898 else
4899 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4900 break;
4901 }
4902}
4903
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004904 void
4905vim9_declare_error(char_u *name)
4906{
4907 char *scope = "";
4908
4909 switch (*name)
4910 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004911 case 'g': scope = _("global"); break;
4912 case 'b': scope = _("buffer"); break;
4913 case 'w': scope = _("window"); break;
4914 case 't': scope = _("tab"); break;
4915 case 'v': scope = "v:"; break;
4916 case '$': semsg(_(e_declare_env_var), name); return;
4917 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004918 }
4919 semsg(_(e_declare_var), scope, name);
4920}
4921
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004922/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004923 * Compile declaration and assignment:
4924 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004926 * Return NULL for an error.
4927 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 */
4929 static char_u *
4930compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4931{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004932 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004934 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 char_u *ret = NULL;
4936 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004937 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004940 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 int oplen = 0;
4943 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004944 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004945 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004946 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004950 // Skip over the "var" or "[var, var]" to get to any "=".
4951 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4952 if (p == NULL)
4953 return *arg == '[' ? arg : NULL;
4954
4955 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004957 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 return NULL;
4959 }
4960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 sp = p;
4962 p = skipwhite(p);
4963 op = p;
4964 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004965
4966 if (var_count > 0 && oplen == 0)
4967 // can be something like "[1, 2]->func()"
4968 return arg;
4969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4971 {
4972 char_u buf[4];
4973
4974 vim_strncpy(buf, op, oplen);
4975 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004976 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004977 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 if (heredoc)
4980 {
4981 list_T *l;
4982 listitem_T *li;
4983
4984 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004985 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004987 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988
4989 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004990 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 {
4992 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4993 li->li_tv.vval.v_string = NULL;
4994 }
4995 generate_NEWLIST(cctx, l->lv_len);
4996 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004997 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 list_free(l);
4999 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005000 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005002 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005004 // for "[var, var] = expr" evaluate the expression here, loop over the
5005 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005006
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005007 p = skipwhite(op + oplen);
5008 if (compile_expr0(&p, cctx) == FAIL)
5009 return NULL;
5010 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005012 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005014 type_T *stacktype;
5015
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005016 stacktype = stack->ga_len == 0 ? &t_void
5017 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005020 emsg(_(e_cannot_use_void));
5021 goto theend;
5022 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005023 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005024 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005025 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005026 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5027 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005028 }
5029 }
5030
5031 /*
5032 * Loop over variables in "[var, var] = expr".
5033 * For "var = expr" and "let var: type" this is done only once.
5034 */
5035 if (var_count > 0)
5036 var_start = skipwhite(arg + 1); // skip over the "["
5037 else
5038 var_start = arg;
5039 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5040 {
5041 char_u *var_end = skip_var_one(var_start, FALSE);
5042 size_t varlen;
5043 int new_local = FALSE;
5044 int opt_type;
5045 int opt_flags = 0;
5046 assign_dest_T dest = dest_local;
5047 int vimvaridx = -1;
5048 lvar_T *lvar = NULL;
5049 lvar_T arg_lvar;
5050 int has_type = FALSE;
5051 int has_index = FALSE;
5052 int instr_count = -1;
5053
5054 p = (*var_start == '&' || *var_start == '$'
5055 || *var_start == '@') ? var_start + 1 : var_start;
5056 p = to_name_end(p, TRUE);
5057
5058 // "a: type" is declaring variable "a" with a type, not "a:".
5059 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5060 --var_end;
5061 if (is_decl && p == var_start + 2 && p[-1] == ':')
5062 --p;
5063
5064 varlen = p - var_start;
5065 vim_free(name);
5066 name = vim_strnsave(var_start, varlen);
5067 if (name == NULL)
5068 return NULL;
5069 if (!heredoc)
5070 type = &t_any;
5071
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005072 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005073 {
5074 if (*var_start == '&')
5075 {
5076 int cc;
5077 long numval;
5078
5079 dest = dest_option;
5080 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005082 emsg(_(e_const_option));
5083 goto theend;
5084 }
5085 if (is_decl)
5086 {
5087 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5088 goto theend;
5089 }
5090 p = var_start;
5091 p = find_option_end(&p, &opt_flags);
5092 if (p == NULL)
5093 {
5094 // cannot happen?
5095 emsg(_(e_letunexp));
5096 goto theend;
5097 }
5098 cc = *p;
5099 *p = NUL;
5100 opt_type = get_option_value(var_start + 1, &numval,
5101 NULL, opt_flags);
5102 *p = cc;
5103 if (opt_type == -3)
5104 {
5105 semsg(_(e_unknown_option), var_start);
5106 goto theend;
5107 }
5108 if (opt_type == -2 || opt_type == 0)
5109 type = &t_string;
5110 else
5111 type = &t_number; // both number and boolean option
5112 }
5113 else if (*var_start == '$')
5114 {
5115 dest = dest_env;
5116 type = &t_string;
5117 if (is_decl)
5118 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005119 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005120 goto theend;
5121 }
5122 }
5123 else if (*var_start == '@')
5124 {
5125 if (!valid_yank_reg(var_start[1], TRUE))
5126 {
5127 emsg_invreg(var_start[1]);
5128 goto theend;
5129 }
5130 dest = dest_reg;
5131 type = &t_string;
5132 if (is_decl)
5133 {
5134 semsg(_("E1066: Cannot declare a register: %s"), name);
5135 goto theend;
5136 }
5137 }
5138 else if (STRNCMP(var_start, "g:", 2) == 0)
5139 {
5140 dest = dest_global;
5141 if (is_decl)
5142 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005143 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005144 goto theend;
5145 }
5146 }
5147 else if (STRNCMP(var_start, "b:", 2) == 0)
5148 {
5149 dest = dest_buffer;
5150 if (is_decl)
5151 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005152 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005153 goto theend;
5154 }
5155 }
5156 else if (STRNCMP(var_start, "w:", 2) == 0)
5157 {
5158 dest = dest_window;
5159 if (is_decl)
5160 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005161 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005162 goto theend;
5163 }
5164 }
5165 else if (STRNCMP(var_start, "t:", 2) == 0)
5166 {
5167 dest = dest_tab;
5168 if (is_decl)
5169 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005170 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005171 goto theend;
5172 }
5173 }
5174 else if (STRNCMP(var_start, "v:", 2) == 0)
5175 {
5176 typval_T *vtv;
5177 int di_flags;
5178
5179 vimvaridx = find_vim_var(name + 2, &di_flags);
5180 if (vimvaridx < 0)
5181 {
5182 semsg(_(e_var_notfound), var_start);
5183 goto theend;
5184 }
5185 // We use the current value of "sandbox" here, is that OK?
5186 if (var_check_ro(di_flags, name, FALSE))
5187 goto theend;
5188 dest = dest_vimvar;
5189 vtv = get_vim_var_tv(vimvaridx);
5190 type = typval2type(vtv);
5191 if (is_decl)
5192 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005193 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005194 goto theend;
5195 }
5196 }
5197 else
5198 {
5199 int idx;
5200
5201 for (idx = 0; reserved[idx] != NULL; ++idx)
5202 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005203 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005204 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005205 goto theend;
5206 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005207
5208 lvar = lookup_local(var_start, varlen, cctx);
5209 if (lvar == NULL)
5210 {
5211 CLEAR_FIELD(arg_lvar);
5212 if (lookup_arg(var_start, varlen,
5213 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5214 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005215 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005216 if (is_decl)
5217 {
5218 semsg(_(e_used_as_arg), name);
5219 goto theend;
5220 }
5221 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005222 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005223 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005224 if (lvar != NULL)
5225 {
5226 if (is_decl)
5227 {
5228 semsg(_("E1017: Variable already declared: %s"), name);
5229 goto theend;
5230 }
5231 else if (lvar->lv_const)
5232 {
5233 semsg(_("E1018: Cannot assign to a constant: %s"),
5234 name);
5235 goto theend;
5236 }
5237 }
5238 else if (STRNCMP(var_start, "s:", 2) == 0
5239 || lookup_script(var_start, varlen) == OK
5240 || find_imported(var_start, varlen, cctx) != NULL)
5241 {
5242 dest = dest_script;
5243 if (is_decl)
5244 {
5245 semsg(_("E1054: Variable already declared in the script: %s"),
5246 name);
5247 goto theend;
5248 }
5249 }
5250 else if (name[1] == ':' && name[2] != NUL)
5251 {
5252 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5253 name);
5254 goto theend;
5255 }
5256 else if (!is_decl)
5257 {
5258 semsg(_("E1089: unknown variable: %s"), name);
5259 goto theend;
5260 }
5261 }
5262 }
5263
5264 // handle "a:name" as a name, not index "name" on "a"
5265 if (varlen > 1 || var_start[varlen] != ':')
5266 p = var_end;
5267
5268 if (dest != dest_option)
5269 {
5270 if (is_decl && *p == ':')
5271 {
5272 // parse optional type: "let var: type = expr"
5273 if (!VIM_ISWHITE(p[1]))
5274 {
5275 semsg(_(e_white_after), ":");
5276 goto theend;
5277 }
5278 p = skipwhite(p + 1);
5279 type = parse_type(&p, cctx->ctx_type_list);
5280 has_type = TRUE;
5281 }
5282 else if (lvar != NULL)
5283 type = lvar->lv_type;
5284 }
5285
5286 if (oplen == 3 && !heredoc && dest != dest_global
5287 && type->tt_type != VAR_STRING
5288 && type->tt_type != VAR_ANY)
5289 {
5290 emsg(_("E1019: Can only concatenate to string"));
5291 goto theend;
5292 }
5293
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005294 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005295 {
5296 if (oplen > 1 && !heredoc)
5297 {
5298 // +=, /=, etc. require an existing variable
5299 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5300 name);
5301 goto theend;
5302 }
5303
5304 // new local variable
5305 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5306 goto theend;
5307 lvar = reserve_local(cctx, var_start, varlen,
5308 cmdidx == CMD_const, type);
5309 if (lvar == NULL)
5310 goto theend;
5311 new_local = TRUE;
5312 }
5313
5314 member_type = type;
5315 if (var_end > var_start + varlen)
5316 {
5317 // Something follows after the variable: "var[idx]".
5318 if (is_decl)
5319 {
5320 emsg(_("E1087: cannot use an index when declaring a variable"));
5321 goto theend;
5322 }
5323
5324 if (var_start[varlen] == '[')
5325 {
5326 has_index = TRUE;
5327 if (type->tt_member == NULL)
5328 {
5329 semsg(_("E1088: cannot use an index on %s"), name);
5330 goto theend;
5331 }
5332 member_type = type->tt_member;
5333 }
5334 else
5335 {
5336 semsg("Not supported yet: %s", var_start);
5337 goto theend;
5338 }
5339 }
5340 else if (lvar == &arg_lvar)
5341 {
5342 semsg(_("E1090: Cannot assign to argument %s"), name);
5343 goto theend;
5344 }
5345
5346 if (!heredoc)
5347 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005348 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005349 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005350 if (oplen > 0 && var_count == 0)
5351 {
5352 // skip over the "=" and the expression
5353 p = skipwhite(op + oplen);
5354 compile_expr0(&p, cctx);
5355 }
5356 }
5357 else if (oplen > 0)
5358 {
5359 type_T *stacktype;
5360
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005361 // For "var = expr" evaluate the expression.
5362 if (var_count == 0)
5363 {
5364 int r;
5365
5366 // for "+=", "*=", "..=" etc. first load the current value
5367 if (*op != '=')
5368 {
5369 generate_loadvar(cctx, dest, name, lvar, type);
5370
5371 if (has_index)
5372 {
5373 // TODO: get member from list or dict
5374 emsg("Index with operation not supported yet");
5375 goto theend;
5376 }
5377 }
5378
5379 // Compile the expression. Temporarily hide the new local
5380 // variable here, it is not available to this expression.
5381 if (new_local)
5382 --cctx->ctx_locals.ga_len;
5383 instr_count = instr->ga_len;
5384 p = skipwhite(op + oplen);
5385 r = compile_expr0(&p, cctx);
5386 if (new_local)
5387 ++cctx->ctx_locals.ga_len;
5388 if (r == FAIL)
5389 goto theend;
5390 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005391 else if (semicolon && var_idx == var_count - 1)
5392 {
5393 // For "[var; var] = expr" get the rest of the list
5394 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5395 goto theend;
5396 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005397 else
5398 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005399 // For "[var, var] = expr" get the "var_idx" item from the
5400 // list.
5401 if (generate_GETITEM(cctx, var_idx) == FAIL)
5402 return FAIL;
5403 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005404
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005405 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005406 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005407 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005408 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005409 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005410 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005411 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005412 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005413 emsg(_(e_cannot_use_void));
5414 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005415 }
5416 else
5417 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005418 // An empty list or dict has a &t_void member,
5419 // for a variable that implies &t_any.
5420 if (stacktype == &t_list_empty)
5421 lvar->lv_type = &t_list_any;
5422 else if (stacktype == &t_dict_empty)
5423 lvar->lv_type = &t_dict_any;
5424 else
5425 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005426 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005427 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005428 else
5429 {
5430 type_T *use_type = lvar->lv_type;
5431
5432 if (has_index)
5433 {
5434 use_type = use_type->tt_member;
5435 if (use_type == NULL)
5436 use_type = &t_void;
5437 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005438 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005439 == FAIL)
5440 goto theend;
5441 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005442 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005443 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005444 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005445 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005446 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005447 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005449 emsg(_(e_const_req_value));
5450 goto theend;
5451 }
5452 else if (!has_type || dest == dest_option)
5453 {
5454 emsg(_(e_type_req));
5455 goto theend;
5456 }
5457 else
5458 {
5459 // variables are always initialized
5460 if (ga_grow(instr, 1) == FAIL)
5461 goto theend;
5462 switch (member_type->tt_type)
5463 {
5464 case VAR_BOOL:
5465 generate_PUSHBOOL(cctx, VVAL_FALSE);
5466 break;
5467 case VAR_FLOAT:
5468#ifdef FEAT_FLOAT
5469 generate_PUSHF(cctx, 0.0);
5470#endif
5471 break;
5472 case VAR_STRING:
5473 generate_PUSHS(cctx, NULL);
5474 break;
5475 case VAR_BLOB:
5476 generate_PUSHBLOB(cctx, NULL);
5477 break;
5478 case VAR_FUNC:
5479 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5480 break;
5481 case VAR_LIST:
5482 generate_NEWLIST(cctx, 0);
5483 break;
5484 case VAR_DICT:
5485 generate_NEWDICT(cctx, 0);
5486 break;
5487 case VAR_JOB:
5488 generate_PUSHJOB(cctx, NULL);
5489 break;
5490 case VAR_CHANNEL:
5491 generate_PUSHCHANNEL(cctx, NULL);
5492 break;
5493 case VAR_NUMBER:
5494 case VAR_UNKNOWN:
5495 case VAR_ANY:
5496 case VAR_PARTIAL:
5497 case VAR_VOID:
5498 case VAR_SPECIAL: // cannot happen
5499 generate_PUSHNR(cctx, 0);
5500 break;
5501 }
5502 }
5503 if (var_count == 0)
5504 end = p;
5505 }
5506
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005507 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005508 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005509 break;
5510
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005511 if (oplen > 0 && *op != '=')
5512 {
5513 type_T *expected = &t_number;
5514 type_T *stacktype;
5515
5516 // TODO: if type is known use float or any operation
5517
5518 if (*op == '.')
5519 expected = &t_string;
5520 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005521 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005522 goto theend;
5523
5524 if (*op == '.')
5525 generate_instr_drop(cctx, ISN_CONCAT, 1);
5526 else
5527 {
5528 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5529
5530 if (isn == NULL)
5531 goto theend;
5532 switch (*op)
5533 {
5534 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5535 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5536 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5537 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5538 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5539 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005540 }
5541 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005543 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005544 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005545 int r;
5546
5547 // Compile the "idx" in "var[idx]".
5548 if (new_local)
5549 --cctx->ctx_locals.ga_len;
5550 p = skipwhite(var_start + varlen + 1);
5551 r = compile_expr0(&p, cctx);
5552 if (new_local)
5553 ++cctx->ctx_locals.ga_len;
5554 if (r == FAIL)
5555 goto theend;
5556 if (*skipwhite(p) != ']')
5557 {
5558 emsg(_(e_missbrac));
5559 goto theend;
5560 }
5561 if (type->tt_type == VAR_DICT
5562 && may_generate_2STRING(-1, cctx) == FAIL)
5563 goto theend;
5564 if (type->tt_type == VAR_LIST
5565 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005566 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005567 {
5568 emsg(_(e_number_exp));
5569 goto theend;
5570 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005571
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005572 // Load the dict or list. On the stack we then have:
5573 // - value
5574 // - index
5575 // - variable
5576 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005577
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005578 if (type->tt_type == VAR_LIST)
5579 {
5580 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5581 return FAIL;
5582 }
5583 else if (type->tt_type == VAR_DICT)
5584 {
5585 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5586 return FAIL;
5587 }
5588 else
5589 {
5590 emsg(_(e_listreq));
5591 goto theend;
5592 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005593 }
5594 else
5595 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005596 switch (dest)
5597 {
5598 case dest_option:
5599 generate_STOREOPT(cctx, name + 1, opt_flags);
5600 break;
5601 case dest_global:
5602 // include g: with the name, easier to execute that way
5603 generate_STORE(cctx, ISN_STOREG, 0, name);
5604 break;
5605 case dest_buffer:
5606 // include b: with the name, easier to execute that way
5607 generate_STORE(cctx, ISN_STOREB, 0, name);
5608 break;
5609 case dest_window:
5610 // include w: with the name, easier to execute that way
5611 generate_STORE(cctx, ISN_STOREW, 0, name);
5612 break;
5613 case dest_tab:
5614 // include t: with the name, easier to execute that way
5615 generate_STORE(cctx, ISN_STORET, 0, name);
5616 break;
5617 case dest_env:
5618 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5619 break;
5620 case dest_reg:
5621 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5622 break;
5623 case dest_vimvar:
5624 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5625 break;
5626 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005627 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005628 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5629 imported_T *import = NULL;
5630 int sid = current_sctx.sc_sid;
5631 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005632
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005633 if (name[1] != ':')
5634 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005635 import = find_imported(name, 0, cctx);
5636 if (import != NULL)
5637 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005638 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005639
5640 idx = get_script_item_idx(sid, rawname, TRUE);
5641 // TODO: specific type
5642 if (idx < 0)
5643 {
5644 char_u *name_s = name;
5645
5646 // Include s: in the name for store_var()
5647 if (name[1] != ':')
5648 {
5649 int len = (int)STRLEN(name) + 3;
5650
5651 name_s = alloc(len);
5652 if (name_s == NULL)
5653 name_s = name;
5654 else
5655 vim_snprintf((char *)name_s, len,
5656 "s:%s", name);
5657 }
5658 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005659 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005660 if (name_s != name)
5661 vim_free(name_s);
5662 }
5663 else
5664 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005665 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005666 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005667 break;
5668 case dest_local:
5669 if (lvar != NULL)
5670 {
5671 isn_T *isn = ((isn_T *)instr->ga_data)
5672 + instr->ga_len - 1;
5673
5674 // optimization: turn "var = 123" from ISN_PUSHNR +
5675 // ISN_STORE into ISN_STORENR
5676 if (!lvar->lv_from_outer
5677 && instr->ga_len == instr_count + 1
5678 && isn->isn_type == ISN_PUSHNR)
5679 {
5680 varnumber_T val = isn->isn_arg.number;
5681
5682 isn->isn_type = ISN_STORENR;
5683 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5684 isn->isn_arg.storenr.stnr_val = val;
5685 if (stack->ga_len > 0)
5686 --stack->ga_len;
5687 }
5688 else if (lvar->lv_from_outer)
5689 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005690 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005691 else
5692 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5693 }
5694 break;
5695 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005696 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005697
5698 if (var_idx + 1 < var_count)
5699 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005700 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005701
5702 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005703 if (var_count > 0 && !semicolon)
5704 {
5705 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5706 goto theend;
5707 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005708
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005709 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005710
5711theend:
5712 vim_free(name);
5713 return ret;
5714}
5715
5716/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005717 * Check if "name" can be "unlet".
5718 */
5719 int
5720check_vim9_unlet(char_u *name)
5721{
5722 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5723 {
5724 semsg(_("E1081: Cannot unlet %s"), name);
5725 return FAIL;
5726 }
5727 return OK;
5728}
5729
5730/*
5731 * Callback passed to ex_unletlock().
5732 */
5733 static int
5734compile_unlet(
5735 lval_T *lvp,
5736 char_u *name_end,
5737 exarg_T *eap,
5738 int deep UNUSED,
5739 void *coookie)
5740{
5741 cctx_T *cctx = coookie;
5742
5743 if (lvp->ll_tv == NULL)
5744 {
5745 char_u *p = lvp->ll_name;
5746 int cc = *name_end;
5747 int ret = OK;
5748
5749 // Normal name. Only supports g:, w:, t: and b: namespaces.
5750 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005751 if (*p == '$')
5752 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5753 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005754 ret = FAIL;
5755 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005756 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005757
5758 *name_end = cc;
5759 return ret;
5760 }
5761
5762 // TODO: unlet {list}[idx]
5763 // TODO: unlet {dict}[key]
5764 emsg("Sorry, :unlet not fully implemented yet");
5765 return FAIL;
5766}
5767
5768/*
5769 * compile "unlet var", "lock var" and "unlock var"
5770 * "arg" points to "var".
5771 */
5772 static char_u *
5773compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5774{
5775 char_u *p = arg;
5776
5777 if (eap->cmdidx != CMD_unlet)
5778 {
5779 emsg("Sorry, :lock and unlock not implemented yet");
5780 return NULL;
5781 }
5782
5783 if (*p == '!')
5784 {
5785 p = skipwhite(p + 1);
5786 eap->forceit = TRUE;
5787 }
5788
5789 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5790 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5791}
5792
5793/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005794 * Compile an :import command.
5795 */
5796 static char_u *
5797compile_import(char_u *arg, cctx_T *cctx)
5798{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005799 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005800}
5801
5802/*
5803 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5804 */
5805 static int
5806compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5807{
5808 garray_T *instr = &cctx->ctx_instr;
5809 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5810
5811 if (endlabel == NULL)
5812 return FAIL;
5813 endlabel->el_next = *el;
5814 *el = endlabel;
5815 endlabel->el_end_label = instr->ga_len;
5816
5817 generate_JUMP(cctx, when, 0);
5818 return OK;
5819}
5820
5821 static void
5822compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5823{
5824 garray_T *instr = &cctx->ctx_instr;
5825
5826 while (*el != NULL)
5827 {
5828 endlabel_T *cur = (*el);
5829 isn_T *isn;
5830
5831 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5832 isn->isn_arg.jump.jump_where = instr->ga_len;
5833 *el = cur->el_next;
5834 vim_free(cur);
5835 }
5836}
5837
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005838 static void
5839compile_free_jump_to_end(endlabel_T **el)
5840{
5841 while (*el != NULL)
5842 {
5843 endlabel_T *cur = (*el);
5844
5845 *el = cur->el_next;
5846 vim_free(cur);
5847 }
5848}
5849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850/*
5851 * Create a new scope and set up the generic items.
5852 */
5853 static scope_T *
5854new_scope(cctx_T *cctx, scopetype_T type)
5855{
5856 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5857
5858 if (scope == NULL)
5859 return NULL;
5860 scope->se_outer = cctx->ctx_scope;
5861 cctx->ctx_scope = scope;
5862 scope->se_type = type;
5863 scope->se_local_count = cctx->ctx_locals.ga_len;
5864 return scope;
5865}
5866
5867/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005868 * Free the current scope and go back to the outer scope.
5869 */
5870 static void
5871drop_scope(cctx_T *cctx)
5872{
5873 scope_T *scope = cctx->ctx_scope;
5874
5875 if (scope == NULL)
5876 {
5877 iemsg("calling drop_scope() without a scope");
5878 return;
5879 }
5880 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005881 switch (scope->se_type)
5882 {
5883 case IF_SCOPE:
5884 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5885 case FOR_SCOPE:
5886 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5887 case WHILE_SCOPE:
5888 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5889 case TRY_SCOPE:
5890 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5891 case NO_SCOPE:
5892 case BLOCK_SCOPE:
5893 break;
5894 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005895 vim_free(scope);
5896}
5897
5898/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005899 * compile "if expr"
5900 *
5901 * "if expr" Produces instructions:
5902 * EVAL expr Push result of "expr"
5903 * JUMP_IF_FALSE end
5904 * ... body ...
5905 * end:
5906 *
5907 * "if expr | else" Produces instructions:
5908 * EVAL expr Push result of "expr"
5909 * JUMP_IF_FALSE else
5910 * ... body ...
5911 * JUMP_ALWAYS end
5912 * else:
5913 * ... body ...
5914 * end:
5915 *
5916 * "if expr1 | elseif expr2 | else" Produces instructions:
5917 * EVAL expr Push result of "expr"
5918 * JUMP_IF_FALSE elseif
5919 * ... body ...
5920 * JUMP_ALWAYS end
5921 * elseif:
5922 * EVAL expr Push result of "expr"
5923 * JUMP_IF_FALSE else
5924 * ... body ...
5925 * JUMP_ALWAYS end
5926 * else:
5927 * ... body ...
5928 * end:
5929 */
5930 static char_u *
5931compile_if(char_u *arg, cctx_T *cctx)
5932{
5933 char_u *p = arg;
5934 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005935 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005937 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005938 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005939
Bram Moolenaara5565e42020-05-09 15:44:01 +02005940 CLEAR_FIELD(ppconst);
5941 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005942 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005943 clear_ppconst(&ppconst);
5944 return NULL;
5945 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005946 if (cctx->ctx_skip == SKIP_YES)
5947 clear_ppconst(&ppconst);
5948 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005949 {
5950 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005951 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005952 clear_ppconst(&ppconst);
5953 }
5954 else
5955 {
5956 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005957 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005958 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005959 return NULL;
5960 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961
5962 scope = new_scope(cctx, IF_SCOPE);
5963 if (scope == NULL)
5964 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005965 scope->se_skip_save = skip_save;
5966 // "is_had_return" will be reset if any block does not end in :return
5967 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005968
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005969 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005970 {
5971 // "where" is set when ":elseif", "else" or ":endif" is found
5972 scope->se_u.se_if.is_if_label = instr->ga_len;
5973 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5974 }
5975 else
5976 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977
5978 return p;
5979}
5980
5981 static char_u *
5982compile_elseif(char_u *arg, cctx_T *cctx)
5983{
5984 char_u *p = arg;
5985 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005986 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987 isn_T *isn;
5988 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005989 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990
5991 if (scope == NULL || scope->se_type != IF_SCOPE)
5992 {
5993 emsg(_(e_elseif_without_if));
5994 return NULL;
5995 }
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 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006000 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006001 {
6002 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006004 return NULL;
6005 // previous "if" or "elseif" jumps here
6006 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6007 isn->isn_arg.jump.jump_where = instr->ga_len;
6008 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006009
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006010 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006011 CLEAR_FIELD(ppconst);
6012 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006013 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006014 clear_ppconst(&ppconst);
6015 return NULL;
6016 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006017 if (scope->se_skip_save == SKIP_YES)
6018 clear_ppconst(&ppconst);
6019 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006020 {
6021 // The expression results in a constant.
6022 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006023 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006024 clear_ppconst(&ppconst);
6025 scope->se_u.se_if.is_if_label = -1;
6026 }
6027 else
6028 {
6029 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006030 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006031 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006032 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006034 // "where" is set when ":elseif", "else" or ":endif" is found
6035 scope->se_u.se_if.is_if_label = instr->ga_len;
6036 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6037 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038
6039 return p;
6040}
6041
6042 static char_u *
6043compile_else(char_u *arg, cctx_T *cctx)
6044{
6045 char_u *p = arg;
6046 garray_T *instr = &cctx->ctx_instr;
6047 isn_T *isn;
6048 scope_T *scope = cctx->ctx_scope;
6049
6050 if (scope == NULL || scope->se_type != IF_SCOPE)
6051 {
6052 emsg(_(e_else_without_if));
6053 return NULL;
6054 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006055 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006056 if (!cctx->ctx_had_return)
6057 scope->se_u.se_if.is_had_return = FALSE;
6058 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059
Bram Moolenaarefd88552020-06-18 20:50:10 +02006060 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006061 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006062 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006063 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006064 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006065 if (!cctx->ctx_had_return
6066 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6067 JUMP_ALWAYS, cctx) == FAIL)
6068 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006069 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006070
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006071 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006072 {
6073 if (scope->se_u.se_if.is_if_label >= 0)
6074 {
6075 // previous "if" or "elseif" jumps here
6076 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6077 isn->isn_arg.jump.jump_where = instr->ga_len;
6078 scope->se_u.se_if.is_if_label = -1;
6079 }
6080 }
6081
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006082 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006083 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006085
6086 return p;
6087}
6088
6089 static char_u *
6090compile_endif(char_u *arg, cctx_T *cctx)
6091{
6092 scope_T *scope = cctx->ctx_scope;
6093 ifscope_T *ifscope;
6094 garray_T *instr = &cctx->ctx_instr;
6095 isn_T *isn;
6096
6097 if (scope == NULL || scope->se_type != IF_SCOPE)
6098 {
6099 emsg(_(e_endif_without_if));
6100 return NULL;
6101 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006102 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006103 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006104 if (!cctx->ctx_had_return)
6105 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006107 if (scope->se_u.se_if.is_if_label >= 0)
6108 {
6109 // previous "if" or "elseif" jumps here
6110 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6111 isn->isn_arg.jump.jump_where = instr->ga_len;
6112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113 // Fill in the "end" label in jumps at the end of the blocks.
6114 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006115 cctx->ctx_skip = scope->se_skip_save;
6116
6117 // If all the blocks end in :return and there is an :else then the
6118 // had_return flag is set.
6119 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006120
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006121 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006122 return arg;
6123}
6124
6125/*
6126 * compile "for var in expr"
6127 *
6128 * Produces instructions:
6129 * PUSHNR -1
6130 * STORE loop-idx Set index to -1
6131 * EVAL expr Push result of "expr"
6132 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6133 * - if beyond end, jump to "end"
6134 * - otherwise get item from list and push it
6135 * STORE var Store item in "var"
6136 * ... body ...
6137 * JUMP top Jump back to repeat
6138 * end: DROP Drop the result of "expr"
6139 *
6140 */
6141 static char_u *
6142compile_for(char_u *arg, cctx_T *cctx)
6143{
6144 char_u *p;
6145 size_t varlen;
6146 garray_T *instr = &cctx->ctx_instr;
6147 garray_T *stack = &cctx->ctx_type_stack;
6148 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006149 lvar_T *loop_lvar; // loop iteration variable
6150 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151 type_T *vartype;
6152
6153 // TODO: list of variables: "for [key, value] in dict"
6154 // parse "var"
6155 for (p = arg; eval_isnamec1(*p); ++p)
6156 ;
6157 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006158 var_lvar = lookup_local(arg, varlen, cctx);
6159 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006160 {
6161 semsg(_("E1023: variable already defined: %s"), arg);
6162 return NULL;
6163 }
6164
6165 // consume "in"
6166 p = skipwhite(p);
6167 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6168 {
6169 emsg(_(e_missing_in));
6170 return NULL;
6171 }
6172 p = skipwhite(p + 2);
6173
6174
6175 scope = new_scope(cctx, FOR_SCOPE);
6176 if (scope == NULL)
6177 return NULL;
6178
6179 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006180 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6181 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006182 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006183 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006184 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187
6188 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006189 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6190 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006191 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006192 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006193 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006197 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006198
6199 // compile "expr", it remains on the stack until "endfor"
6200 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006201 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006202 {
6203 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006206
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006207 // Now that we know the type of "var", check that it is a list, now or at
6208 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006209 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006210 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006212 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006213 return NULL;
6214 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006215 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006216 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217
6218 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006219 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006220
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006221 generate_FOR(cctx, loop_lvar->lv_idx);
6222 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006223
6224 return arg;
6225}
6226
6227/*
6228 * compile "endfor"
6229 */
6230 static char_u *
6231compile_endfor(char_u *arg, cctx_T *cctx)
6232{
6233 garray_T *instr = &cctx->ctx_instr;
6234 scope_T *scope = cctx->ctx_scope;
6235 forscope_T *forscope;
6236 isn_T *isn;
6237
6238 if (scope == NULL || scope->se_type != FOR_SCOPE)
6239 {
6240 emsg(_(e_for));
6241 return NULL;
6242 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006243 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006245 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246
6247 // At end of ":for" scope jump back to the FOR instruction.
6248 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6249
6250 // Fill in the "end" label in the FOR statement so it can jump here
6251 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6252 isn->isn_arg.forloop.for_end = instr->ga_len;
6253
6254 // Fill in the "end" label any BREAK statements
6255 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6256
6257 // Below the ":for" scope drop the "expr" list from the stack.
6258 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6259 return NULL;
6260
6261 vim_free(scope);
6262
6263 return arg;
6264}
6265
6266/*
6267 * compile "while expr"
6268 *
6269 * Produces instructions:
6270 * top: EVAL expr Push result of "expr"
6271 * JUMP_IF_FALSE end jump if false
6272 * ... body ...
6273 * JUMP top Jump back to repeat
6274 * end:
6275 *
6276 */
6277 static char_u *
6278compile_while(char_u *arg, cctx_T *cctx)
6279{
6280 char_u *p = arg;
6281 garray_T *instr = &cctx->ctx_instr;
6282 scope_T *scope;
6283
6284 scope = new_scope(cctx, WHILE_SCOPE);
6285 if (scope == NULL)
6286 return NULL;
6287
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006288 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289
6290 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006291 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 return NULL;
6293
6294 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006295 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 JUMP_IF_FALSE, cctx) == FAIL)
6297 return FAIL;
6298
6299 return p;
6300}
6301
6302/*
6303 * compile "endwhile"
6304 */
6305 static char_u *
6306compile_endwhile(char_u *arg, cctx_T *cctx)
6307{
6308 scope_T *scope = cctx->ctx_scope;
6309
6310 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6311 {
6312 emsg(_(e_while));
6313 return NULL;
6314 }
6315 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006316 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317
6318 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006319 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320
6321 // Fill in the "end" label in the WHILE statement so it can jump here.
6322 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006323 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324
6325 vim_free(scope);
6326
6327 return arg;
6328}
6329
6330/*
6331 * compile "continue"
6332 */
6333 static char_u *
6334compile_continue(char_u *arg, cctx_T *cctx)
6335{
6336 scope_T *scope = cctx->ctx_scope;
6337
6338 for (;;)
6339 {
6340 if (scope == NULL)
6341 {
6342 emsg(_(e_continue));
6343 return NULL;
6344 }
6345 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6346 break;
6347 scope = scope->se_outer;
6348 }
6349
6350 // Jump back to the FOR or WHILE instruction.
6351 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006352 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6353 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354 return arg;
6355}
6356
6357/*
6358 * compile "break"
6359 */
6360 static char_u *
6361compile_break(char_u *arg, cctx_T *cctx)
6362{
6363 scope_T *scope = cctx->ctx_scope;
6364 endlabel_T **el;
6365
6366 for (;;)
6367 {
6368 if (scope == NULL)
6369 {
6370 emsg(_(e_break));
6371 return NULL;
6372 }
6373 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6374 break;
6375 scope = scope->se_outer;
6376 }
6377
6378 // Jump to the end of the FOR or WHILE loop.
6379 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006380 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006382 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6384 return FAIL;
6385
6386 return arg;
6387}
6388
6389/*
6390 * compile "{" start of block
6391 */
6392 static char_u *
6393compile_block(char_u *arg, cctx_T *cctx)
6394{
6395 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6396 return NULL;
6397 return skipwhite(arg + 1);
6398}
6399
6400/*
6401 * compile end of block: drop one scope
6402 */
6403 static void
6404compile_endblock(cctx_T *cctx)
6405{
6406 scope_T *scope = cctx->ctx_scope;
6407
6408 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006409 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006410 vim_free(scope);
6411}
6412
6413/*
6414 * compile "try"
6415 * Creates a new scope for the try-endtry, pointing to the first catch and
6416 * finally.
6417 * Creates another scope for the "try" block itself.
6418 * TRY instruction sets up exception handling at runtime.
6419 *
6420 * "try"
6421 * TRY -> catch1, -> finally push trystack entry
6422 * ... try block
6423 * "throw {exception}"
6424 * EVAL {exception}
6425 * THROW create exception
6426 * ... try block
6427 * " catch {expr}"
6428 * JUMP -> finally
6429 * catch1: PUSH exeception
6430 * EVAL {expr}
6431 * MATCH
6432 * JUMP nomatch -> catch2
6433 * CATCH remove exception
6434 * ... catch block
6435 * " catch"
6436 * JUMP -> finally
6437 * catch2: CATCH remove exception
6438 * ... catch block
6439 * " finally"
6440 * finally:
6441 * ... finally block
6442 * " endtry"
6443 * ENDTRY pop trystack entry, may rethrow
6444 */
6445 static char_u *
6446compile_try(char_u *arg, cctx_T *cctx)
6447{
6448 garray_T *instr = &cctx->ctx_instr;
6449 scope_T *try_scope;
6450 scope_T *scope;
6451
6452 // scope that holds the jumps that go to catch/finally/endtry
6453 try_scope = new_scope(cctx, TRY_SCOPE);
6454 if (try_scope == NULL)
6455 return NULL;
6456
6457 // "catch" is set when the first ":catch" is found.
6458 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006459 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006460 if (generate_instr(cctx, ISN_TRY) == NULL)
6461 return NULL;
6462
6463 // scope for the try block itself
6464 scope = new_scope(cctx, BLOCK_SCOPE);
6465 if (scope == NULL)
6466 return NULL;
6467
6468 return arg;
6469}
6470
6471/*
6472 * compile "catch {expr}"
6473 */
6474 static char_u *
6475compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6476{
6477 scope_T *scope = cctx->ctx_scope;
6478 garray_T *instr = &cctx->ctx_instr;
6479 char_u *p;
6480 isn_T *isn;
6481
6482 // end block scope from :try or :catch
6483 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6484 compile_endblock(cctx);
6485 scope = cctx->ctx_scope;
6486
6487 // Error if not in a :try scope
6488 if (scope == NULL || scope->se_type != TRY_SCOPE)
6489 {
6490 emsg(_(e_catch));
6491 return NULL;
6492 }
6493
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006494 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006495 {
6496 emsg(_("E1033: catch unreachable after catch-all"));
6497 return NULL;
6498 }
6499
6500 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006501 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006502 JUMP_ALWAYS, cctx) == FAIL)
6503 return NULL;
6504
6505 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006506 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006507 if (isn->isn_arg.try.try_catch == 0)
6508 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006509 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006510 {
6511 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006512 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 isn->isn_arg.jump.jump_where = instr->ga_len;
6514 }
6515
6516 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006517 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006519 scope->se_u.se_try.ts_caught_all = TRUE;
6520 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521 }
6522 else
6523 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006524 char_u *end;
6525 char_u *pat;
6526 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006527 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006528 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006530 // Push v:exception, push {expr} and MATCH
6531 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6532
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006533 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006534 if (*end != *p)
6535 {
6536 semsg(_("E1067: Separator mismatch: %s"), p);
6537 vim_free(tofree);
6538 return FAIL;
6539 }
6540 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006541 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006542 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006543 len = (int)(end - tofree);
6544 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006545 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006546 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006547 if (pat == NULL)
6548 return FAIL;
6549 if (generate_PUSHS(cctx, pat) == FAIL)
6550 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6553 return NULL;
6554
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006555 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6557 return NULL;
6558 }
6559
6560 if (generate_instr(cctx, ISN_CATCH) == NULL)
6561 return NULL;
6562
6563 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6564 return NULL;
6565 return p;
6566}
6567
6568 static char_u *
6569compile_finally(char_u *arg, cctx_T *cctx)
6570{
6571 scope_T *scope = cctx->ctx_scope;
6572 garray_T *instr = &cctx->ctx_instr;
6573 isn_T *isn;
6574
6575 // end block scope from :try or :catch
6576 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6577 compile_endblock(cctx);
6578 scope = cctx->ctx_scope;
6579
6580 // Error if not in a :try scope
6581 if (scope == NULL || scope->se_type != TRY_SCOPE)
6582 {
6583 emsg(_(e_finally));
6584 return NULL;
6585 }
6586
6587 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006588 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589 if (isn->isn_arg.try.try_finally != 0)
6590 {
6591 emsg(_(e_finally_dup));
6592 return NULL;
6593 }
6594
6595 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006596 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597
Bram Moolenaar585fea72020-04-02 22:33:21 +02006598 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006599 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 {
6601 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006602 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006604 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006605 }
6606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 // TODO: set index in ts_finally_label jumps
6608
6609 return arg;
6610}
6611
6612 static char_u *
6613compile_endtry(char_u *arg, cctx_T *cctx)
6614{
6615 scope_T *scope = cctx->ctx_scope;
6616 garray_T *instr = &cctx->ctx_instr;
6617 isn_T *isn;
6618
6619 // end block scope from :catch or :finally
6620 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6621 compile_endblock(cctx);
6622 scope = cctx->ctx_scope;
6623
6624 // Error if not in a :try scope
6625 if (scope == NULL || scope->se_type != TRY_SCOPE)
6626 {
6627 if (scope == NULL)
6628 emsg(_(e_no_endtry));
6629 else if (scope->se_type == WHILE_SCOPE)
6630 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006631 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632 emsg(_(e_endfor));
6633 else
6634 emsg(_(e_endif));
6635 return NULL;
6636 }
6637
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006638 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6640 {
6641 emsg(_("E1032: missing :catch or :finally"));
6642 return NULL;
6643 }
6644
6645 // Fill in the "end" label in jumps at the end of the blocks, if not done
6646 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006647 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006648
6649 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006650 if (isn->isn_arg.try.try_catch == 0)
6651 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006652 if (isn->isn_arg.try.try_finally == 0)
6653 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006654
6655 if (scope->se_u.se_try.ts_catch_label != 0)
6656 {
6657 // Last catch without match jumps here
6658 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6659 isn->isn_arg.jump.jump_where = instr->ga_len;
6660 }
6661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006662 compile_endblock(cctx);
6663
6664 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6665 return NULL;
6666 return arg;
6667}
6668
6669/*
6670 * compile "throw {expr}"
6671 */
6672 static char_u *
6673compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6674{
6675 char_u *p = skipwhite(arg);
6676
Bram Moolenaara5565e42020-05-09 15:44:01 +02006677 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 return NULL;
6679 if (may_generate_2STRING(-1, cctx) == FAIL)
6680 return NULL;
6681 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6682 return NULL;
6683
6684 return p;
6685}
6686
6687/*
6688 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006689 * compile "echomsg expr"
6690 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006691 * compile "execute expr"
6692 */
6693 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006694compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006695{
6696 char_u *p = arg;
6697 int count = 0;
6698
6699 for (;;)
6700 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006701 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006702 return NULL;
6703 ++count;
6704 p = skipwhite(p);
6705 if (ends_excmd(*p))
6706 break;
6707 }
6708
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006709 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6710 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6711 else if (cmdidx == CMD_execute)
6712 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6713 else if (cmdidx == CMD_echomsg)
6714 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6715 else
6716 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717 return p;
6718}
6719
6720/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006721 * A command that is not compiled, execute with legacy code.
6722 */
6723 static char_u *
6724compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6725{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006726 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006727 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006728 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006729
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006730 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006731 goto theend;
6732
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006733 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006734 {
6735 long argt = excmd_get_argt(eap->cmdidx);
6736 int usefilter = FALSE;
6737
6738 has_expr = argt & (EX_XFILE | EX_EXPAND);
6739
6740 // If the command can be followed by a bar, find the bar and truncate
6741 // it, so that the following command can be compiled.
6742 // The '|' is overwritten with a NUL, it is put back below.
6743 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6744 && *eap->arg == '!')
6745 // :w !filter or :r !filter or :r! filter
6746 usefilter = TRUE;
6747 if ((argt & EX_TRLBAR) && !usefilter)
6748 {
6749 separate_nextcmd(eap);
6750 if (eap->nextcmd != NULL)
6751 nextcmd = eap->nextcmd;
6752 }
6753 }
6754
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006755 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6756 {
6757 // expand filename in "syntax include [@group] filename"
6758 has_expr = TRUE;
6759 eap->arg = skipwhite(eap->arg + 7);
6760 if (*eap->arg == '@')
6761 eap->arg = skiptowhite(eap->arg);
6762 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006763
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006764 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006765 {
6766 int count = 0;
6767 char_u *start = skipwhite(line);
6768
6769 // :cmd xxx`=expr1`yyy`=expr2`zzz
6770 // PUSHS ":cmd xxx"
6771 // eval expr1
6772 // PUSHS "yyy"
6773 // eval expr2
6774 // PUSHS "zzz"
6775 // EXECCONCAT 5
6776 for (;;)
6777 {
6778 if (p > start)
6779 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006780 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006781 ++count;
6782 }
6783 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006784 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006785 return NULL;
6786 may_generate_2STRING(-1, cctx);
6787 ++count;
6788 p = skipwhite(p);
6789 if (*p != '`')
6790 {
6791 emsg(_("E1083: missing backtick"));
6792 return NULL;
6793 }
6794 start = p + 1;
6795
6796 p = (char_u *)strstr((char *)start, "`=");
6797 if (p == NULL)
6798 {
6799 if (*skipwhite(start) != NUL)
6800 {
6801 generate_PUSHS(cctx, vim_strsave(start));
6802 ++count;
6803 }
6804 break;
6805 }
6806 }
6807 generate_EXECCONCAT(cctx, count);
6808 }
6809 else
6810 generate_EXEC(cctx, line);
6811
6812theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006813 if (*nextcmd != NUL)
6814 {
6815 // the parser expects a pointer to the bar, put it back
6816 --nextcmd;
6817 *nextcmd = '|';
6818 }
6819
6820 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006821}
6822
6823/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006824 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006825 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006826 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006827 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006828add_def_function(ufunc_T *ufunc)
6829{
6830 dfunc_T *dfunc;
6831
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006832 if (def_functions.ga_len == 0)
6833 {
6834 // The first position is not used, so that a zero uf_dfunc_idx means it
6835 // wasn't set.
6836 if (ga_grow(&def_functions, 1) == FAIL)
6837 return FAIL;
6838 ++def_functions.ga_len;
6839 }
6840
Bram Moolenaar09689a02020-05-09 22:50:08 +02006841 // Add the function to "def_functions".
6842 if (ga_grow(&def_functions, 1) == FAIL)
6843 return FAIL;
6844 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6845 CLEAR_POINTER(dfunc);
6846 dfunc->df_idx = def_functions.ga_len;
6847 ufunc->uf_dfunc_idx = dfunc->df_idx;
6848 dfunc->df_ufunc = ufunc;
6849 ++def_functions.ga_len;
6850 return OK;
6851}
6852
6853/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006854 * After ex_function() has collected all the function lines: parse and compile
6855 * the lines into instructions.
6856 * Adds the function to "def_functions".
6857 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6858 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006859 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006860 * This can be used recursively through compile_lambda(), which may reallocate
6861 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006862 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006863 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006864 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006865compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006866{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006867 char_u *line = NULL;
6868 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870 cctx_T cctx;
6871 garray_T *instr;
6872 int called_emsg_before = called_emsg;
6873 int ret = FAIL;
6874 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006875 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006876 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006877
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006878 // When using a function that was compiled before: Free old instructions.
6879 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006880 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006882 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6883 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006884 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006886 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006887 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006888
Bram Moolenaar985116a2020-07-12 17:31:09 +02006889 ufunc->uf_def_status = UF_COMPILING;
6890
Bram Moolenaara80faa82020-04-12 19:37:17 +02006891 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892 cctx.ctx_ufunc = ufunc;
6893 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006894 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6896 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6897 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6898 cctx.ctx_type_list = &ufunc->uf_type_list;
6899 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6900 instr = &cctx.ctx_instr;
6901
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006902 // Set the context to the function, it may be compiled when called from
6903 // another script. Set the script version to the most modern one.
6904 // The line number will be set in next_line_from_context().
6905 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6907
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006908 // Make sure error messages are OK.
6909 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6910 if (do_estack_push)
6911 estack_push_ufunc(ufunc, 1);
6912
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006913 if (ufunc->uf_def_args.ga_len > 0)
6914 {
6915 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006916 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006917 int i;
6918 char_u *arg;
6919 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6920
6921 // Produce instructions for the default values of optional arguments.
6922 // Store the instruction index in uf_def_arg_idx[] so that we know
6923 // where to start when the function is called, depending on the number
6924 // of arguments.
6925 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6926 if (ufunc->uf_def_arg_idx == NULL)
6927 goto erret;
6928 for (i = 0; i < count; ++i)
6929 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006930 garray_T *stack = &cctx.ctx_type_stack;
6931 type_T *val_type;
6932 int arg_idx = first_def_arg + i;
6933
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006934 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6935 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006936 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006937 goto erret;
6938
6939 // If no type specified use the type of the default value.
6940 // Otherwise check that the default value type matches the
6941 // specified type.
6942 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6943 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6944 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006945 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006946 == FAIL)
6947 {
6948 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6949 arg_idx + 1);
6950 goto erret;
6951 }
6952
6953 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006954 goto erret;
6955 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006956 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6957 }
6958
6959 /*
6960 * Loop over all the lines of the function and generate instructions.
6961 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006962 for (;;)
6963 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006964 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006965 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006966 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006967 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006968
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006969 // Bail out on the first error to avoid a flood of errors and report
6970 // the right line number when inside try/catch.
6971 if (emsg_before != called_emsg)
6972 goto erret;
6973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974 if (line != NULL && *line == '|')
6975 // the line continues after a '|'
6976 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006977 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006978 && !(*line == '#' && (line == cctx.ctx_line_start
6979 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006980 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006981 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982 goto erret;
6983 }
6984 else
6985 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006986 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006987 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006988 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006989 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006991 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006992
Bram Moolenaara80faa82020-04-12 19:37:17 +02006993 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006994 ea.cmdlinep = &line;
6995 ea.cmd = skipwhite(line);
6996
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006997 // Some things can be recognized by the first character.
6998 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007000 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007001 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007002 if (ea.cmd[1] != '{')
7003 {
7004 line = (char_u *)"";
7005 continue;
7006 }
7007 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007008
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007009 case '}':
7010 {
7011 // "}" ends a block scope
7012 scopetype_T stype = cctx.ctx_scope == NULL
7013 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007015 if (stype == BLOCK_SCOPE)
7016 {
7017 compile_endblock(&cctx);
7018 line = ea.cmd;
7019 }
7020 else
7021 {
7022 emsg(_("E1025: using } outside of a block scope"));
7023 goto erret;
7024 }
7025 if (line != NULL)
7026 line = skipwhite(ea.cmd + 1);
7027 continue;
7028 }
7029
7030 case '{':
7031 // "{" starts a block scope
7032 // "{'a': 1}->func() is something else
7033 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7034 {
7035 line = compile_block(ea.cmd, &cctx);
7036 continue;
7037 }
7038 break;
7039
7040 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007041 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007042 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 }
7044
7045 /*
7046 * COMMAND MODIFIERS
7047 */
7048 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7049 {
7050 if (errormsg != NULL)
7051 goto erret;
7052 // empty line or comment
7053 line = (char_u *)"";
7054 continue;
7055 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007056 // TODO: use modifiers in the command
7057 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007058 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059
7060 // Skip ":call" to get to the function name.
7061 if (checkforcmd(&ea.cmd, "call", 3))
7062 ea.cmd = skipwhite(ea.cmd);
7063
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007064 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007065 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007066 char_u *pskip;
7067
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007068 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007069 // find what follows.
7070 // Skip over "var.member", "var[idx]" and the like.
7071 // Also "&opt = val", "$ENV = val" and "@r = val".
7072 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007073 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007074 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007075 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007077 char_u *var_end;
7078 int oplen;
7079 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007081 var_end = find_name_end(pskip, NULL, NULL,
7082 FNE_CHECK_START | FNE_INCL_BR);
7083 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007084 if (oplen > 0)
7085 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007086 size_t len = p - ea.cmd;
7087
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007088 // Recognize an assignment if we recognize the variable
7089 // name:
7090 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007091 // "local = expr" where "local" is a local var.
7092 // "script = expr" where "script" is a script-local var.
7093 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007094 // "&opt = expr"
7095 // "$ENV = expr"
7096 // "@r = expr"
7097 if (*ea.cmd == '&'
7098 || *ea.cmd == '$'
7099 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007100 || ((len) > 2 && ea.cmd[1] == ':')
7101 || lookup_local(ea.cmd, len, &cctx) != NULL
7102 || lookup_arg(ea.cmd, len, NULL, NULL,
7103 NULL, &cctx) == OK
7104 || lookup_script(ea.cmd, len) == OK
7105 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007106 {
7107 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007108 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007109 goto erret;
7110 continue;
7111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007112 }
7113 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007114
7115 if (*ea.cmd == '[')
7116 {
7117 // [var, var] = expr
7118 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7119 if (line == NULL)
7120 goto erret;
7121 if (line != ea.cmd)
7122 continue;
7123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007124 }
7125
7126 /*
7127 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007128 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007130 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007131 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007132 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007133 ea.cmd = skip_range(ea.cmd, NULL);
7134 if (ea.cmd > cmd && !starts_with_colon)
7135 {
7136 emsg(_(e_colon_required));
7137 goto erret;
7138 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007139 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007140 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007141 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007142 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143
7144 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7145 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007146 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007147 {
7148 line += STRLEN(line);
7149 continue;
7150 }
7151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007153 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007155 // CMD_let cannot happen, compile_assignment() above is used
7156 iemsg("Command from find_ex_command() not handled");
7157 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159 }
7160
7161 p = skipwhite(p);
7162
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007163 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007164 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007165 && ea.cmdidx != CMD_elseif
7166 && ea.cmdidx != CMD_else
7167 && ea.cmdidx != CMD_endif)
7168 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007169 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007170 continue;
7171 }
7172
Bram Moolenaarefd88552020-06-18 20:50:10 +02007173 if (ea.cmdidx != CMD_elseif
7174 && ea.cmdidx != CMD_else
7175 && ea.cmdidx != CMD_endif
7176 && ea.cmdidx != CMD_endfor
7177 && ea.cmdidx != CMD_endwhile
7178 && ea.cmdidx != CMD_catch
7179 && ea.cmdidx != CMD_finally
7180 && ea.cmdidx != CMD_endtry)
7181 {
7182 if (cctx.ctx_had_return)
7183 {
7184 emsg(_("E1095: Unreachable code after :return"));
7185 goto erret;
7186 }
7187 }
7188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007189 switch (ea.cmdidx)
7190 {
7191 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007192 ea.arg = p;
7193 line = compile_nested_function(&ea, &cctx);
7194 break;
7195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007197 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007198 goto erret;
7199
7200 case CMD_return:
7201 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007202 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007203 break;
7204
7205 case CMD_let:
7206 case CMD_const:
7207 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007208 if (line == p)
7209 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007210 break;
7211
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007212 case CMD_unlet:
7213 case CMD_unlockvar:
7214 case CMD_lockvar:
7215 line = compile_unletlock(p, &ea, &cctx);
7216 break;
7217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007218 case CMD_import:
7219 line = compile_import(p, &cctx);
7220 break;
7221
7222 case CMD_if:
7223 line = compile_if(p, &cctx);
7224 break;
7225 case CMD_elseif:
7226 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007227 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228 break;
7229 case CMD_else:
7230 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007231 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232 break;
7233 case CMD_endif:
7234 line = compile_endif(p, &cctx);
7235 break;
7236
7237 case CMD_while:
7238 line = compile_while(p, &cctx);
7239 break;
7240 case CMD_endwhile:
7241 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007242 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243 break;
7244
7245 case CMD_for:
7246 line = compile_for(p, &cctx);
7247 break;
7248 case CMD_endfor:
7249 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007250 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007251 break;
7252 case CMD_continue:
7253 line = compile_continue(p, &cctx);
7254 break;
7255 case CMD_break:
7256 line = compile_break(p, &cctx);
7257 break;
7258
7259 case CMD_try:
7260 line = compile_try(p, &cctx);
7261 break;
7262 case CMD_catch:
7263 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007264 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007265 break;
7266 case CMD_finally:
7267 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007268 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269 break;
7270 case CMD_endtry:
7271 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007272 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273 break;
7274 case CMD_throw:
7275 line = compile_throw(p, &cctx);
7276 break;
7277
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007278 case CMD_eval:
7279 if (compile_expr0(&p, &cctx) == FAIL)
7280 goto erret;
7281
7282 // drop the return value
7283 generate_instr_drop(&cctx, ISN_DROP, 1);
7284
7285 line = skipwhite(p);
7286 break;
7287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007288 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007289 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007290 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007291 case CMD_echomsg:
7292 case CMD_echoerr:
7293 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007294 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007296 // TODO: other commands with an expression argument
7297
Bram Moolenaar002262f2020-07-08 17:47:57 +02007298 case CMD_SIZE:
7299 semsg(_("E476: Invalid command: %s"), ea.cmd);
7300 goto erret;
7301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007302 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007303 // Not recognized, execute with do_cmdline_cmd().
7304 ea.arg = p;
7305 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306 break;
7307 }
7308 if (line == NULL)
7309 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007310 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311
7312 if (cctx.ctx_type_stack.ga_len < 0)
7313 {
7314 iemsg("Type stack underflow");
7315 goto erret;
7316 }
7317 }
7318
7319 if (cctx.ctx_scope != NULL)
7320 {
7321 if (cctx.ctx_scope->se_type == IF_SCOPE)
7322 emsg(_(e_endif));
7323 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7324 emsg(_(e_endwhile));
7325 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7326 emsg(_(e_endfor));
7327 else
7328 emsg(_("E1026: Missing }"));
7329 goto erret;
7330 }
7331
Bram Moolenaarefd88552020-06-18 20:50:10 +02007332 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007333 {
7334 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7335 {
7336 emsg(_("E1027: Missing return statement"));
7337 goto erret;
7338 }
7339
7340 // Return zero if there is no return at the end.
7341 generate_PUSHNR(&cctx, 0);
7342 generate_instr(&cctx, ISN_RETURN);
7343 }
7344
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007345 {
7346 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7347 + ufunc->uf_dfunc_idx;
7348 dfunc->df_deleted = FALSE;
7349 dfunc->df_instr = instr->ga_data;
7350 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007351 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007352 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007353 if (cctx.ctx_outer_used)
7354 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007355 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357
7358 ret = OK;
7359
7360erret:
7361 if (ret == FAIL)
7362 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007363 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007364 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7365 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007366
7367 for (idx = 0; idx < instr->ga_len; ++idx)
7368 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007370
Bram Moolenaar45a15082020-05-25 00:28:33 +02007371 // if using the last entry in the table we might as well remove it
7372 if (!dfunc->df_deleted
7373 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007374 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007375 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007376
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007377 while (cctx.ctx_scope != NULL)
7378 drop_scope(&cctx);
7379
Bram Moolenaar20431c92020-03-20 18:39:46 +01007380 // Don't execute this function body.
7381 ga_clear_strings(&ufunc->uf_lines);
7382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 if (errormsg != NULL)
7384 emsg(errormsg);
7385 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007386 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 }
7388
7389 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007390 if (do_estack_push)
7391 estack_pop();
7392
Bram Moolenaar20431c92020-03-20 18:39:46 +01007393 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007394 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007396 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397}
7398
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007399 void
7400set_function_type(ufunc_T *ufunc)
7401{
7402 int varargs = ufunc->uf_va_name != NULL;
7403 int argcount = ufunc->uf_args.ga_len;
7404
7405 // Create a type for the function, with the return type and any
7406 // argument types.
7407 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7408 // The type is included in "tt_args".
7409 if (argcount > 0 || varargs)
7410 {
7411 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7412 argcount, &ufunc->uf_type_list);
7413 // Add argument types to the function type.
7414 if (func_type_add_arg_types(ufunc->uf_func_type,
7415 argcount + varargs,
7416 &ufunc->uf_type_list) == FAIL)
7417 return;
7418 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7419 ufunc->uf_func_type->tt_min_argcount =
7420 argcount - ufunc->uf_def_args.ga_len;
7421 if (ufunc->uf_arg_types == NULL)
7422 {
7423 int i;
7424
7425 // lambda does not have argument types.
7426 for (i = 0; i < argcount; ++i)
7427 ufunc->uf_func_type->tt_args[i] = &t_any;
7428 }
7429 else
7430 mch_memmove(ufunc->uf_func_type->tt_args,
7431 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7432 if (varargs)
7433 {
7434 ufunc->uf_func_type->tt_args[argcount] =
7435 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7436 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7437 }
7438 }
7439 else
7440 // No arguments, can use a predefined type.
7441 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7442 argcount, &ufunc->uf_type_list);
7443}
7444
7445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446/*
7447 * Delete an instruction, free what it contains.
7448 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007449 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450delete_instr(isn_T *isn)
7451{
7452 switch (isn->isn_type)
7453 {
7454 case ISN_EXEC:
7455 case ISN_LOADENV:
7456 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007457 case ISN_LOADB:
7458 case ISN_LOADW:
7459 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007461 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462 case ISN_PUSHEXC:
7463 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007464 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007465 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007466 case ISN_STOREB:
7467 case ISN_STOREW:
7468 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007469 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007470 vim_free(isn->isn_arg.string);
7471 break;
7472
7473 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007474 case ISN_STORES:
7475 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476 break;
7477
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007478 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007479 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007480 vim_free(isn->isn_arg.unlet.ul_name);
7481 break;
7482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483 case ISN_STOREOPT:
7484 vim_free(isn->isn_arg.storeopt.so_name);
7485 break;
7486
7487 case ISN_PUSHBLOB: // push blob isn_arg.blob
7488 blob_unref(isn->isn_arg.blob);
7489 break;
7490
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007491 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007492#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007493 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007494#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007495 break;
7496
7497 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007499 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007500#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007501 break;
7502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007503 case ISN_UCALL:
7504 vim_free(isn->isn_arg.ufunc.cuf_name);
7505 break;
7506
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007507 case ISN_FUNCREF:
7508 {
7509 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7510 + isn->isn_arg.funcref.fr_func;
7511 func_ptr_unref(dfunc->df_ufunc);
7512 }
7513 break;
7514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515 case ISN_2BOOL:
7516 case ISN_2STRING:
7517 case ISN_ADDBLOB:
7518 case ISN_ADDLIST:
7519 case ISN_BCALL:
7520 case ISN_CATCH:
7521 case ISN_CHECKNR:
7522 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007523 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007524 case ISN_COMPAREANY:
7525 case ISN_COMPAREBLOB:
7526 case ISN_COMPAREBOOL:
7527 case ISN_COMPAREDICT:
7528 case ISN_COMPAREFLOAT:
7529 case ISN_COMPAREFUNC:
7530 case ISN_COMPARELIST:
7531 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532 case ISN_COMPARESPECIAL:
7533 case ISN_COMPARESTRING:
7534 case ISN_CONCAT:
7535 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007536 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537 case ISN_DROP:
7538 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007539 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007540 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007542 case ISN_EXECCONCAT:
7543 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007544 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007545 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007546 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007547 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007548 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549 case ISN_JUMP:
7550 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007551 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552 case ISN_LOADSCRIPT:
7553 case ISN_LOADREG:
7554 case ISN_LOADV:
7555 case ISN_NEGATENR:
7556 case ISN_NEWDICT:
7557 case ISN_NEWLIST:
7558 case ISN_OPNR:
7559 case ISN_OPFLOAT:
7560 case ISN_OPANY:
7561 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007562 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563 case ISN_PUSHF:
7564 case ISN_PUSHNR:
7565 case ISN_PUSHBOOL:
7566 case ISN_PUSHSPEC:
7567 case ISN_RETURN:
7568 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007569 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007570 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007571 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007572 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007574 case ISN_STOREDICT:
7575 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576 case ISN_THROW:
7577 case ISN_TRY:
7578 // nothing allocated
7579 break;
7580 }
7581}
7582
7583/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007584 * Free all instructions for "dfunc".
7585 */
7586 static void
7587delete_def_function_contents(dfunc_T *dfunc)
7588{
7589 int idx;
7590
7591 ga_clear(&dfunc->df_def_args_isn);
7592
7593 if (dfunc->df_instr != NULL)
7594 {
7595 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7596 delete_instr(dfunc->df_instr + idx);
7597 VIM_CLEAR(dfunc->df_instr);
7598 }
7599
7600 dfunc->df_deleted = TRUE;
7601}
7602
7603/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007604 * When a user function is deleted, clear the contents of any associated def
7605 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007606 */
7607 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007608clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007610 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007611 {
7612 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7613 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007614
Bram Moolenaar20431c92020-03-20 18:39:46 +01007615 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007616 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617 }
7618}
7619
7620#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007621/*
7622 * Free all functions defined with ":def".
7623 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624 void
7625free_def_functions(void)
7626{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007627 int idx;
7628
7629 for (idx = 0; idx < def_functions.ga_len; ++idx)
7630 {
7631 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7632
7633 delete_def_function_contents(dfunc);
7634 }
7635
7636 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007637}
7638#endif
7639
7640
7641#endif // FEAT_EVAL