blob: 81d5324cc8add9b80c1947173b6c05cdec77d75b [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 */
2466 static 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;
3751
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003752 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003753 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003754 // TODO: blob index
3755 // TODO: more arguments
3756 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003757 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003758 return FAIL;
3759
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003760 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003761 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003762 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003763 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003764 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 return FAIL;
3766
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003767 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3768 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 if (**arg != ']')
3770 {
3771 emsg(_(e_missbrac));
3772 return FAIL;
3773 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003774 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003776 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3777 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003778 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003779 if ((*typep)->tt_type == VAR_LIST)
3780 *typep = (*typep)->tt_member;
3781 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3782 return FAIL;
3783 }
3784 else if ((*typep)->tt_type == VAR_DICT)
3785 {
3786 *typep = (*typep)->tt_member;
3787 if (may_generate_2STRING(-1, cctx) == FAIL)
3788 return FAIL;
3789 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3790 return FAIL;
3791 }
3792 else
3793 {
3794 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003795 return FAIL;
3796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003798 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003800 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003801 return FAIL;
3802
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003803 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003804 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3805 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003807 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 if (eval_isnamec1(*p))
3809 while (eval_isnamec(*p))
3810 MB_PTR_ADV(p);
3811 if (p == *arg)
3812 {
3813 semsg(_(e_syntax_at), *arg);
3814 return FAIL;
3815 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003816 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 return FAIL;
3818 *arg = p;
3819 }
3820 else
3821 break;
3822 }
3823
3824 // TODO - see handle_subscript():
3825 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3826 // Don't do this when "Func" is already a partial that was bound
3827 // explicitly (pt_auto is FALSE).
3828
3829 return OK;
3830}
3831
3832/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003833 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3834 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003836 * If the value is a constant "ppconst->pp_ret" will be set.
3837 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003838 *
3839 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 */
3841
3842/*
3843 * number number constant
3844 * 0zFFFFFFFF Blob constant
3845 * "string" string constant
3846 * 'string' literal string constant
3847 * &option-name option value
3848 * @r register contents
3849 * identifier variable value
3850 * function() function call
3851 * $VAR environment variable
3852 * (expression) nested expression
3853 * [expr, expr] List
3854 * {key: val, key: val} Dictionary
3855 * #{key: val, key: val} Dictionary with literal keys
3856 *
3857 * Also handle:
3858 * ! in front logical NOT
3859 * - in front unary minus
3860 * + in front unary plus (ignored)
3861 * trailing (arg) funcref/partial call
3862 * trailing [] subscript in String or List
3863 * trailing .name entry in Dictionary
3864 * trailing ->name() method call
3865 */
3866 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003867compile_expr7(
3868 char_u **arg,
3869 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003870 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 char_u *start_leader, *end_leader;
3873 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003874 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003875 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876
3877 /*
3878 * Skip '!', '-' and '+' characters. They are handled later.
3879 */
3880 start_leader = *arg;
3881 while (**arg == '!' || **arg == '-' || **arg == '+')
3882 *arg = skipwhite(*arg + 1);
3883 end_leader = *arg;
3884
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003885 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 switch (**arg)
3887 {
3888 /*
3889 * Number constant.
3890 */
3891 case '0': // also for blob starting with 0z
3892 case '1':
3893 case '2':
3894 case '3':
3895 case '4':
3896 case '5':
3897 case '6':
3898 case '7':
3899 case '8':
3900 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003901 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 return FAIL;
3903 break;
3904
3905 /*
3906 * String constant: "string".
3907 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003908 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 return FAIL;
3910 break;
3911
3912 /*
3913 * Literal string constant: 'str''ing'.
3914 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003915 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 return FAIL;
3917 break;
3918
3919 /*
3920 * Constant Vim variable.
3921 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003922 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 ret = NOTDONE;
3924 break;
3925
3926 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003927 * "true" constant
3928 */
3929 case 't': if (STRNCMP(*arg, "true", 4) == 0
3930 && !eval_isnamec((*arg)[4]))
3931 {
3932 *arg += 4;
3933 rettv->v_type = VAR_BOOL;
3934 rettv->vval.v_number = VVAL_TRUE;
3935 }
3936 else
3937 ret = NOTDONE;
3938 break;
3939
3940 /*
3941 * "false" constant
3942 */
3943 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3944 && !eval_isnamec((*arg)[5]))
3945 {
3946 *arg += 5;
3947 rettv->v_type = VAR_BOOL;
3948 rettv->vval.v_number = VVAL_FALSE;
3949 }
3950 else
3951 ret = NOTDONE;
3952 break;
3953
3954 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 * List: [expr, expr]
3956 */
3957 case '[': ret = compile_list(arg, cctx);
3958 break;
3959
3960 /*
3961 * Dictionary: #{key: val, key: val}
3962 */
3963 case '#': if ((*arg)[1] == '{')
3964 {
3965 ++*arg;
3966 ret = compile_dict(arg, cctx, TRUE);
3967 }
3968 else
3969 ret = NOTDONE;
3970 break;
3971
3972 /*
3973 * Lambda: {arg, arg -> expr}
3974 * Dictionary: {'key': val, 'key': val}
3975 */
3976 case '{': {
3977 char_u *start = skipwhite(*arg + 1);
3978
3979 // Find out what comes after the arguments.
3980 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003981 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 if (ret != FAIL && *start == '>')
3983 ret = compile_lambda(arg, cctx);
3984 else
3985 ret = compile_dict(arg, cctx, FALSE);
3986 }
3987 break;
3988
3989 /*
3990 * Option value: &name
3991 */
3992 case '&': ret = compile_get_option(arg, cctx);
3993 break;
3994
3995 /*
3996 * Environment variable: $VAR.
3997 */
3998 case '$': ret = compile_get_env(arg, cctx);
3999 break;
4000
4001 /*
4002 * Register contents: @r.
4003 */
4004 case '@': ret = compile_get_register(arg, cctx);
4005 break;
4006 /*
4007 * nested expression: (expression).
4008 */
4009 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004010
4011 // recursive!
4012 if (ppconst->pp_used <= PPSIZE - 10)
4013 {
4014 ret = compile_expr1(arg, cctx, ppconst);
4015 }
4016 else
4017 {
4018 // Not enough space in ppconst, flush constants.
4019 if (generate_ppconst(cctx, ppconst) == FAIL)
4020 return FAIL;
4021 ret = compile_expr0(arg, cctx);
4022 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 *arg = skipwhite(*arg);
4024 if (**arg == ')')
4025 ++*arg;
4026 else if (ret == OK)
4027 {
4028 emsg(_(e_missing_close));
4029 ret = FAIL;
4030 }
4031 break;
4032
4033 default: ret = NOTDONE;
4034 break;
4035 }
4036 if (ret == FAIL)
4037 return FAIL;
4038
Bram Moolenaar1c747212020-05-09 18:28:34 +02004039 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 {
4041 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004042 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004044 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 return FAIL;
4046 }
4047 start_leader = end_leader; // don't apply again below
4048
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004049 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004050 clear_tv(rettv);
4051 else
4052 // A constant expression can possibly be handled compile time,
4053 // return the value instead of generating code.
4054 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 }
4056 else if (ret == NOTDONE)
4057 {
4058 char_u *p;
4059 int r;
4060
4061 if (!eval_isnamec1(**arg))
4062 {
4063 semsg(_("E1015: Name expected: %s"), *arg);
4064 return FAIL;
4065 }
4066
4067 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004068 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004070 {
4071 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4072 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004074 {
4075 if (generate_ppconst(cctx, ppconst) == FAIL)
4076 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004078 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 if (r == FAIL)
4080 return FAIL;
4081 }
4082
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004083 // Handle following "[]", ".member", etc.
4084 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004085 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004086 ppconst) == FAIL)
4087 return FAIL;
4088 if (ppconst->pp_used > 0)
4089 {
4090 // apply the '!', '-' and '+' before the constant
4091 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4092 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4093 return FAIL;
4094 return OK;
4095 }
4096 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004098 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099}
4100
4101/*
4102 * * number multiplication
4103 * / number division
4104 * % number modulo
4105 */
4106 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004107compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108{
4109 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004110 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004111 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004113 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004114 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 return FAIL;
4116
4117 /*
4118 * Repeat computing, until no "*", "/" or "%" is following.
4119 */
4120 for (;;)
4121 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004122 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 if (*op != '*' && *op != '/' && *op != '%')
4124 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004125 if (next != NULL)
4126 {
4127 *arg = next_line_from_context(cctx, TRUE);
4128 op = skipwhite(*arg);
4129 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004130
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004131 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 {
4133 char_u buf[3];
4134
4135 vim_strncpy(buf, op, 1);
4136 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004137 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 }
4139 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004140 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004141 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004143 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004144 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004146
4147 if (ppconst->pp_used == ppconst_used + 2
4148 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4149 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004150 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004151 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4152 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004153 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004155 // both are numbers: compute the result
4156 switch (*op)
4157 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004158 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004159 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004160 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004161 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004162 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004163 break;
4164 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004165 tv1->vval.v_number = res;
4166 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004167 }
4168 else
4169 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004170 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004171 generate_two_op(cctx, op);
4172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 }
4174
4175 return OK;
4176}
4177
4178/*
4179 * + number addition
4180 * - number subtraction
4181 * .. string concatenation
4182 */
4183 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004184compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185{
4186 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004187 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004189 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190
4191 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004192 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 return FAIL;
4194
4195 /*
4196 * Repeat computing, until no "+", "-" or ".." is following.
4197 */
4198 for (;;)
4199 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004200 op = may_peek_next_line(cctx, *arg, &next);
4201 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 break;
4203 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004204 if (next != NULL)
4205 {
4206 *arg = next_line_from_context(cctx, TRUE);
4207 op = skipwhite(*arg);
4208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004210 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 {
4212 char_u buf[3];
4213
4214 vim_strncpy(buf, op, oplen);
4215 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004216 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 }
4218
4219 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004220 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004221 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004223 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004224 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 return FAIL;
4226
Bram Moolenaara5565e42020-05-09 15:44:01 +02004227 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004228 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004229 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4230 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4231 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4232 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004234 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4235 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004236
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004237 // concat/subtract/add constant numbers
4238 if (*op == '+')
4239 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4240 else if (*op == '-')
4241 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4242 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004243 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004244 // concatenate constant strings
4245 char_u *s1 = tv1->vval.v_string;
4246 char_u *s2 = tv2->vval.v_string;
4247 size_t len1 = STRLEN(s1);
4248
4249 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4250 if (tv1->vval.v_string == NULL)
4251 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004252 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004253 return FAIL;
4254 }
4255 mch_memmove(tv1->vval.v_string, s1, len1);
4256 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004257 vim_free(s1);
4258 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004259 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004260 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 }
4262 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004263 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004264 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004265 if (*op == '.')
4266 {
4267 if (may_generate_2STRING(-2, cctx) == FAIL
4268 || may_generate_2STRING(-1, cctx) == FAIL)
4269 return FAIL;
4270 generate_instr_drop(cctx, ISN_CONCAT, 1);
4271 }
4272 else
4273 generate_two_op(cctx, op);
4274 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 }
4276
4277 return OK;
4278}
4279
4280/*
4281 * expr5a == expr5b
4282 * expr5a =~ expr5b
4283 * expr5a != expr5b
4284 * expr5a !~ expr5b
4285 * expr5a > expr5b
4286 * expr5a >= expr5b
4287 * expr5a < expr5b
4288 * expr5a <= expr5b
4289 * expr5a is expr5b
4290 * expr5a isnot expr5b
4291 *
4292 * Produces instructions:
4293 * EVAL expr5a Push result of "expr5a"
4294 * EVAL expr5b Push result of "expr5b"
4295 * COMPARE one of the compare instructions
4296 */
4297 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004298compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299{
4300 exptype_T type = EXPR_UNKNOWN;
4301 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004302 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004305 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306
4307 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004308 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 return FAIL;
4310
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004311 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004312 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313
4314 /*
4315 * If there is a comparative operator, use it.
4316 */
4317 if (type != EXPR_UNKNOWN)
4318 {
4319 int ic = FALSE; // Default: do not ignore case
4320
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004321 if (next != NULL)
4322 {
4323 *arg = next_line_from_context(cctx, TRUE);
4324 p = skipwhite(*arg);
4325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 if (type_is && (p[len] == '?' || p[len] == '#'))
4327 {
4328 semsg(_(e_invexpr2), *arg);
4329 return FAIL;
4330 }
4331 // extra question mark appended: ignore case
4332 if (p[len] == '?')
4333 {
4334 ic = TRUE;
4335 ++len;
4336 }
4337 // extra '#' appended: match case (ignored)
4338 else if (p[len] == '#')
4339 ++len;
4340 // nothing appended: match case
4341
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004342 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 {
4344 char_u buf[7];
4345
4346 vim_strncpy(buf, p, len);
4347 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004348 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 }
4350
4351 // get the second variable
4352 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004353 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004354 return FAIL;
4355
Bram Moolenaara5565e42020-05-09 15:44:01 +02004356 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 return FAIL;
4358
Bram Moolenaara5565e42020-05-09 15:44:01 +02004359 if (ppconst->pp_used == ppconst_used + 2)
4360 {
4361 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4362 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4363 int ret;
4364
4365 // Both sides are a constant, compute the result now.
4366 // First check for a valid combination of types, this is more
4367 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004368 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004369 ret = FAIL;
4370 else
4371 {
4372 ret = typval_compare(tv1, tv2, type, ic);
4373 tv1->v_type = VAR_BOOL;
4374 tv1->vval.v_number = tv1->vval.v_number
4375 ? VVAL_TRUE : VVAL_FALSE;
4376 clear_tv(tv2);
4377 --ppconst->pp_used;
4378 }
4379 return ret;
4380 }
4381
4382 generate_ppconst(cctx, ppconst);
4383 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 }
4385
4386 return OK;
4387}
4388
Bram Moolenaar7f141552020-05-09 17:35:53 +02004389static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4390
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391/*
4392 * Compile || or &&.
4393 */
4394 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004395compile_and_or(
4396 char_u **arg,
4397 cctx_T *cctx,
4398 char *op,
4399 ppconst_T *ppconst,
4400 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004402 char_u *next;
4403 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 int opchar = *op;
4405
4406 if (p[0] == opchar && p[1] == opchar)
4407 {
4408 garray_T *instr = &cctx->ctx_instr;
4409 garray_T end_ga;
4410
4411 /*
4412 * Repeat until there is no following "||" or "&&"
4413 */
4414 ga_init2(&end_ga, sizeof(int), 10);
4415 while (p[0] == opchar && p[1] == opchar)
4416 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004417 if (next != NULL)
4418 {
4419 *arg = next_line_from_context(cctx, TRUE);
4420 p = skipwhite(*arg);
4421 }
4422
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004423 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4424 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004426 return FAIL;
4427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428
Bram Moolenaara5565e42020-05-09 15:44:01 +02004429 // TODO: use ppconst if the value is a constant
4430 generate_ppconst(cctx, ppconst);
4431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 if (ga_grow(&end_ga, 1) == FAIL)
4433 {
4434 ga_clear(&end_ga);
4435 return FAIL;
4436 }
4437 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4438 ++end_ga.ga_len;
4439 generate_JUMP(cctx, opchar == '|'
4440 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4441
4442 // eval the next expression
4443 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004444 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004445 return FAIL;
4446
Bram Moolenaara5565e42020-05-09 15:44:01 +02004447 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4448 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 {
4450 ga_clear(&end_ga);
4451 return FAIL;
4452 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004453
4454 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004456 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457
4458 // Fill in the end label in all jumps.
4459 while (end_ga.ga_len > 0)
4460 {
4461 isn_T *isn;
4462
4463 --end_ga.ga_len;
4464 isn = ((isn_T *)instr->ga_data)
4465 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4466 isn->isn_arg.jump.jump_where = instr->ga_len;
4467 }
4468 ga_clear(&end_ga);
4469 }
4470
4471 return OK;
4472}
4473
4474/*
4475 * expr4a && expr4a && expr4a logical AND
4476 *
4477 * Produces instructions:
4478 * EVAL expr4a Push result of "expr4a"
4479 * JUMP_AND_KEEP_IF_FALSE end
4480 * EVAL expr4b Push result of "expr4b"
4481 * JUMP_AND_KEEP_IF_FALSE end
4482 * EVAL expr4c Push result of "expr4c"
4483 * end:
4484 */
4485 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004486compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004488 int ppconst_used = ppconst->pp_used;
4489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004491 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 return FAIL;
4493
4494 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004495 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496}
4497
4498/*
4499 * expr3a || expr3b || expr3c logical OR
4500 *
4501 * Produces instructions:
4502 * EVAL expr3a Push result of "expr3a"
4503 * JUMP_AND_KEEP_IF_TRUE end
4504 * EVAL expr3b Push result of "expr3b"
4505 * JUMP_AND_KEEP_IF_TRUE end
4506 * EVAL expr3c Push result of "expr3c"
4507 * end:
4508 */
4509 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004510compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004512 int ppconst_used = ppconst->pp_used;
4513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004515 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 return FAIL;
4517
4518 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004519 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520}
4521
4522/*
4523 * Toplevel expression: expr2 ? expr1a : expr1b
4524 *
4525 * Produces instructions:
4526 * EVAL expr2 Push result of "expr"
4527 * JUMP_IF_FALSE alt jump if false
4528 * EVAL expr1a
4529 * JUMP_ALWAYS end
4530 * alt: EVAL expr1b
4531 * end:
4532 */
4533 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004534compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535{
4536 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004537 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004538 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539
Bram Moolenaar61a89812020-05-07 16:58:17 +02004540 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004541 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 return FAIL;
4543
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004544 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 if (*p == '?')
4546 {
4547 garray_T *instr = &cctx->ctx_instr;
4548 garray_T *stack = &cctx->ctx_type_stack;
4549 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004550 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004552 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554 int has_const_expr = FALSE;
4555 int const_value = FALSE;
4556 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004558 if (next != NULL)
4559 {
4560 *arg = next_line_from_context(cctx, TRUE);
4561 p = skipwhite(*arg);
4562 }
4563
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004564 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4565 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004567 return FAIL;
4568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569
Bram Moolenaara5565e42020-05-09 15:44:01 +02004570 if (ppconst->pp_used == ppconst_used + 1)
4571 {
4572 // the condition is a constant, we know whether the ? or the :
4573 // expression is to be evaluated.
4574 has_const_expr = TRUE;
4575 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4576 clear_tv(&ppconst->pp_tv[ppconst_used]);
4577 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004578 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4579 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004580 }
4581 else
4582 {
4583 generate_ppconst(cctx, ppconst);
4584 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4585 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586
4587 // evaluate the second expression; any type is accepted
4588 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004589 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004590 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004591 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004592 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593
Bram Moolenaara5565e42020-05-09 15:44:01 +02004594 if (!has_const_expr)
4595 {
4596 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597
Bram Moolenaara5565e42020-05-09 15:44:01 +02004598 // remember the type and drop it
4599 --stack->ga_len;
4600 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601
Bram Moolenaara5565e42020-05-09 15:44:01 +02004602 end_idx = instr->ga_len;
4603 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4604
4605 // jump here from JUMP_IF_FALSE
4606 isn = ((isn_T *)instr->ga_data) + alt_idx;
4607 isn->isn_arg.jump.jump_where = instr->ga_len;
4608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609
4610 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004611 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 if (*p != ':')
4613 {
4614 emsg(_(e_missing_colon));
4615 return FAIL;
4616 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004617 if (next != NULL)
4618 {
4619 *arg = next_line_from_context(cctx, TRUE);
4620 p = skipwhite(*arg);
4621 }
4622
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004623 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4624 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004626 return FAIL;
4627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628
4629 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004630 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004631 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4632 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004634 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004635 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004636 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004637 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638
Bram Moolenaara5565e42020-05-09 15:44:01 +02004639 if (!has_const_expr)
4640 {
4641 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642
Bram Moolenaara5565e42020-05-09 15:44:01 +02004643 // If the types differ, the result has a more generic type.
4644 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4645 common_type(type1, type2, &type2, cctx->ctx_type_list);
4646
4647 // jump here from JUMP_ALWAYS
4648 isn = ((isn_T *)instr->ga_data) + end_idx;
4649 isn->isn_arg.jump.jump_where = instr->ga_len;
4650 }
4651
4652 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 }
4654 return OK;
4655}
4656
4657/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004658 * Toplevel expression.
4659 */
4660 static int
4661compile_expr0(char_u **arg, cctx_T *cctx)
4662{
4663 ppconst_T ppconst;
4664
4665 CLEAR_FIELD(ppconst);
4666 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4667 {
4668 clear_ppconst(&ppconst);
4669 return FAIL;
4670 }
4671 if (generate_ppconst(cctx, &ppconst) == FAIL)
4672 return FAIL;
4673 return OK;
4674}
4675
4676/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 * compile "return [expr]"
4678 */
4679 static char_u *
4680compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4681{
4682 char_u *p = arg;
4683 garray_T *stack = &cctx->ctx_type_stack;
4684 type_T *stack_type;
4685
4686 if (*p != NUL && *p != '|' && *p != '\n')
4687 {
4688 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004689 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 return NULL;
4691
4692 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4693 if (set_return_type)
4694 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004695 else
4696 {
4697 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4698 && stack_type->tt_type != VAR_VOID
4699 && stack_type->tt_type != VAR_UNKNOWN)
4700 {
4701 emsg(_("E1096: Returning a value in a function without a return type"));
4702 return NULL;
4703 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004704 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4705 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004707 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 }
4709 else
4710 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004711 // "set_return_type" cannot be TRUE, only used for a lambda which
4712 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004713 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4714 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 {
4716 emsg(_("E1003: Missing return value"));
4717 return NULL;
4718 }
4719
4720 // No argument, return zero.
4721 generate_PUSHNR(cctx, 0);
4722 }
4723
4724 if (generate_instr(cctx, ISN_RETURN) == NULL)
4725 return NULL;
4726
4727 // "return val | endif" is possible
4728 return skipwhite(p);
4729}
4730
4731/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004732 * Get a line from the compilation context, compatible with exarg_T getline().
4733 * Return a pointer to the line in allocated memory.
4734 * Return NULL for end-of-file or some error.
4735 */
4736 static char_u *
4737exarg_getline(
4738 int c UNUSED,
4739 void *cookie,
4740 int indent UNUSED,
4741 int do_concat UNUSED)
4742{
4743 cctx_T *cctx = (cctx_T *)cookie;
4744
4745 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4746 {
4747 iemsg("Heredoc got to end");
4748 return NULL;
4749 }
4750 ++cctx->ctx_lnum;
4751 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4752 [cctx->ctx_lnum]);
4753}
4754
4755/*
4756 * Compile a nested :def command.
4757 */
4758 static char_u *
4759compile_nested_function(exarg_T *eap, cctx_T *cctx)
4760{
4761 char_u *name_start = eap->arg;
4762 char_u *name_end = to_name_end(eap->arg, FALSE);
4763 char_u *name = get_lambda_name();
4764 lvar_T *lvar;
4765 ufunc_T *ufunc;
4766
4767 eap->arg = name_end;
4768 eap->getline = exarg_getline;
4769 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004770 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004771 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004772 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004773
Bram Moolenaar822ba242020-05-24 23:00:18 +02004774 if (ufunc == NULL)
4775 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004776 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004777 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004778 return NULL;
4779
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004780 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004781 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004782 TRUE, ufunc->uf_func_type);
4783
4784 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4785 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4786 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004787
Bram Moolenaar61a89812020-05-07 16:58:17 +02004788 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004789 return (char_u *)"";
4790}
4791
4792/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 * Return the length of an assignment operator, or zero if there isn't one.
4794 */
4795 int
4796assignment_len(char_u *p, int *heredoc)
4797{
4798 if (*p == '=')
4799 {
4800 if (p[1] == '<' && p[2] == '<')
4801 {
4802 *heredoc = TRUE;
4803 return 3;
4804 }
4805 return 1;
4806 }
4807 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4808 return 2;
4809 if (STRNCMP(p, "..=", 3) == 0)
4810 return 3;
4811 return 0;
4812}
4813
4814// words that cannot be used as a variable
4815static char *reserved[] = {
4816 "true",
4817 "false",
4818 NULL
4819};
4820
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004821typedef enum {
4822 dest_local,
4823 dest_option,
4824 dest_env,
4825 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004826 dest_buffer,
4827 dest_window,
4828 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004829 dest_vimvar,
4830 dest_script,
4831 dest_reg,
4832} assign_dest_T;
4833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004835 * Generate the load instruction for "name".
4836 */
4837 static void
4838generate_loadvar(
4839 cctx_T *cctx,
4840 assign_dest_T dest,
4841 char_u *name,
4842 lvar_T *lvar,
4843 type_T *type)
4844{
4845 switch (dest)
4846 {
4847 case dest_option:
4848 // TODO: check the option exists
4849 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4850 break;
4851 case dest_global:
4852 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4853 break;
4854 case dest_buffer:
4855 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4856 break;
4857 case dest_window:
4858 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4859 break;
4860 case dest_tab:
4861 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4862 break;
4863 case dest_script:
4864 compile_load_scriptvar(cctx,
4865 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4866 break;
4867 case dest_env:
4868 // Include $ in the name here
4869 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4870 break;
4871 case dest_reg:
4872 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4873 break;
4874 case dest_vimvar:
4875 generate_LOADV(cctx, name + 2, TRUE);
4876 break;
4877 case dest_local:
4878 if (lvar->lv_from_outer)
4879 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4880 NULL, type);
4881 else
4882 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4883 break;
4884 }
4885}
4886
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004887 void
4888vim9_declare_error(char_u *name)
4889{
4890 char *scope = "";
4891
4892 switch (*name)
4893 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004894 case 'g': scope = _("global"); break;
4895 case 'b': scope = _("buffer"); break;
4896 case 'w': scope = _("window"); break;
4897 case 't': scope = _("tab"); break;
4898 case 'v': scope = "v:"; break;
4899 case '$': semsg(_(e_declare_env_var), name); return;
4900 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004901 }
4902 semsg(_(e_declare_var), scope, name);
4903}
4904
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004905/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004906 * Compile declaration and assignment:
4907 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004909 * Return NULL for an error.
4910 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 */
4912 static char_u *
4913compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4914{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004915 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004917 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 char_u *ret = NULL;
4919 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004920 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004923 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 int oplen = 0;
4926 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004927 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004928 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004929 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004933 // Skip over the "var" or "[var, var]" to get to any "=".
4934 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4935 if (p == NULL)
4936 return *arg == '[' ? arg : NULL;
4937
4938 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004940 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 return NULL;
4942 }
4943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 sp = p;
4945 p = skipwhite(p);
4946 op = p;
4947 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004948
4949 if (var_count > 0 && oplen == 0)
4950 // can be something like "[1, 2]->func()"
4951 return arg;
4952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4954 {
4955 char_u buf[4];
4956
4957 vim_strncpy(buf, op, oplen);
4958 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004959 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004960 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 if (heredoc)
4963 {
4964 list_T *l;
4965 listitem_T *li;
4966
4967 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004968 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004970 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971
4972 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004973 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 {
4975 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4976 li->li_tv.vval.v_string = NULL;
4977 }
4978 generate_NEWLIST(cctx, l->lv_len);
4979 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004980 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 list_free(l);
4982 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004983 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004985 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004987 // for "[var, var] = expr" evaluate the expression here, loop over the
4988 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004989
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004990 p = skipwhite(op + oplen);
4991 if (compile_expr0(&p, cctx) == FAIL)
4992 return NULL;
4993 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004995 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004997 type_T *stacktype;
4998
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004999 stacktype = stack->ga_len == 0 ? &t_void
5000 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005001 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005003 emsg(_(e_cannot_use_void));
5004 goto theend;
5005 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005006 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005007 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005008 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005009 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5010 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005011 }
5012 }
5013
5014 /*
5015 * Loop over variables in "[var, var] = expr".
5016 * For "var = expr" and "let var: type" this is done only once.
5017 */
5018 if (var_count > 0)
5019 var_start = skipwhite(arg + 1); // skip over the "["
5020 else
5021 var_start = arg;
5022 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5023 {
5024 char_u *var_end = skip_var_one(var_start, FALSE);
5025 size_t varlen;
5026 int new_local = FALSE;
5027 int opt_type;
5028 int opt_flags = 0;
5029 assign_dest_T dest = dest_local;
5030 int vimvaridx = -1;
5031 lvar_T *lvar = NULL;
5032 lvar_T arg_lvar;
5033 int has_type = FALSE;
5034 int has_index = FALSE;
5035 int instr_count = -1;
5036
5037 p = (*var_start == '&' || *var_start == '$'
5038 || *var_start == '@') ? var_start + 1 : var_start;
5039 p = to_name_end(p, TRUE);
5040
5041 // "a: type" is declaring variable "a" with a type, not "a:".
5042 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5043 --var_end;
5044 if (is_decl && p == var_start + 2 && p[-1] == ':')
5045 --p;
5046
5047 varlen = p - var_start;
5048 vim_free(name);
5049 name = vim_strnsave(var_start, varlen);
5050 if (name == NULL)
5051 return NULL;
5052 if (!heredoc)
5053 type = &t_any;
5054
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005055 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005056 {
5057 if (*var_start == '&')
5058 {
5059 int cc;
5060 long numval;
5061
5062 dest = dest_option;
5063 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005065 emsg(_(e_const_option));
5066 goto theend;
5067 }
5068 if (is_decl)
5069 {
5070 semsg(_("E1052: Cannot declare an option: %s"), var_start);
5071 goto theend;
5072 }
5073 p = var_start;
5074 p = find_option_end(&p, &opt_flags);
5075 if (p == NULL)
5076 {
5077 // cannot happen?
5078 emsg(_(e_letunexp));
5079 goto theend;
5080 }
5081 cc = *p;
5082 *p = NUL;
5083 opt_type = get_option_value(var_start + 1, &numval,
5084 NULL, opt_flags);
5085 *p = cc;
5086 if (opt_type == -3)
5087 {
5088 semsg(_(e_unknown_option), var_start);
5089 goto theend;
5090 }
5091 if (opt_type == -2 || opt_type == 0)
5092 type = &t_string;
5093 else
5094 type = &t_number; // both number and boolean option
5095 }
5096 else if (*var_start == '$')
5097 {
5098 dest = dest_env;
5099 type = &t_string;
5100 if (is_decl)
5101 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005102 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005103 goto theend;
5104 }
5105 }
5106 else if (*var_start == '@')
5107 {
5108 if (!valid_yank_reg(var_start[1], TRUE))
5109 {
5110 emsg_invreg(var_start[1]);
5111 goto theend;
5112 }
5113 dest = dest_reg;
5114 type = &t_string;
5115 if (is_decl)
5116 {
5117 semsg(_("E1066: Cannot declare a register: %s"), name);
5118 goto theend;
5119 }
5120 }
5121 else if (STRNCMP(var_start, "g:", 2) == 0)
5122 {
5123 dest = dest_global;
5124 if (is_decl)
5125 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005126 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005127 goto theend;
5128 }
5129 }
5130 else if (STRNCMP(var_start, "b:", 2) == 0)
5131 {
5132 dest = dest_buffer;
5133 if (is_decl)
5134 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005135 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005136 goto theend;
5137 }
5138 }
5139 else if (STRNCMP(var_start, "w:", 2) == 0)
5140 {
5141 dest = dest_window;
5142 if (is_decl)
5143 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005144 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005145 goto theend;
5146 }
5147 }
5148 else if (STRNCMP(var_start, "t:", 2) == 0)
5149 {
5150 dest = dest_tab;
5151 if (is_decl)
5152 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005153 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005154 goto theend;
5155 }
5156 }
5157 else if (STRNCMP(var_start, "v:", 2) == 0)
5158 {
5159 typval_T *vtv;
5160 int di_flags;
5161
5162 vimvaridx = find_vim_var(name + 2, &di_flags);
5163 if (vimvaridx < 0)
5164 {
5165 semsg(_(e_var_notfound), var_start);
5166 goto theend;
5167 }
5168 // We use the current value of "sandbox" here, is that OK?
5169 if (var_check_ro(di_flags, name, FALSE))
5170 goto theend;
5171 dest = dest_vimvar;
5172 vtv = get_vim_var_tv(vimvaridx);
5173 type = typval2type(vtv);
5174 if (is_decl)
5175 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005176 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005177 goto theend;
5178 }
5179 }
5180 else
5181 {
5182 int idx;
5183
5184 for (idx = 0; reserved[idx] != NULL; ++idx)
5185 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005186 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005187 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005188 goto theend;
5189 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005190
5191 lvar = lookup_local(var_start, varlen, cctx);
5192 if (lvar == NULL)
5193 {
5194 CLEAR_FIELD(arg_lvar);
5195 if (lookup_arg(var_start, varlen,
5196 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5197 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005198 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005199 if (is_decl)
5200 {
5201 semsg(_(e_used_as_arg), name);
5202 goto theend;
5203 }
5204 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005205 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005206 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005207 if (lvar != NULL)
5208 {
5209 if (is_decl)
5210 {
5211 semsg(_("E1017: Variable already declared: %s"), name);
5212 goto theend;
5213 }
5214 else if (lvar->lv_const)
5215 {
5216 semsg(_("E1018: Cannot assign to a constant: %s"),
5217 name);
5218 goto theend;
5219 }
5220 }
5221 else if (STRNCMP(var_start, "s:", 2) == 0
5222 || lookup_script(var_start, varlen) == OK
5223 || find_imported(var_start, varlen, cctx) != NULL)
5224 {
5225 dest = dest_script;
5226 if (is_decl)
5227 {
5228 semsg(_("E1054: Variable already declared in the script: %s"),
5229 name);
5230 goto theend;
5231 }
5232 }
5233 else if (name[1] == ':' && name[2] != NUL)
5234 {
5235 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5236 name);
5237 goto theend;
5238 }
5239 else if (!is_decl)
5240 {
5241 semsg(_("E1089: unknown variable: %s"), name);
5242 goto theend;
5243 }
5244 }
5245 }
5246
5247 // handle "a:name" as a name, not index "name" on "a"
5248 if (varlen > 1 || var_start[varlen] != ':')
5249 p = var_end;
5250
5251 if (dest != dest_option)
5252 {
5253 if (is_decl && *p == ':')
5254 {
5255 // parse optional type: "let var: type = expr"
5256 if (!VIM_ISWHITE(p[1]))
5257 {
5258 semsg(_(e_white_after), ":");
5259 goto theend;
5260 }
5261 p = skipwhite(p + 1);
5262 type = parse_type(&p, cctx->ctx_type_list);
5263 has_type = TRUE;
5264 }
5265 else if (lvar != NULL)
5266 type = lvar->lv_type;
5267 }
5268
5269 if (oplen == 3 && !heredoc && dest != dest_global
5270 && type->tt_type != VAR_STRING
5271 && type->tt_type != VAR_ANY)
5272 {
5273 emsg(_("E1019: Can only concatenate to string"));
5274 goto theend;
5275 }
5276
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005277 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005278 {
5279 if (oplen > 1 && !heredoc)
5280 {
5281 // +=, /=, etc. require an existing variable
5282 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5283 name);
5284 goto theend;
5285 }
5286
5287 // new local variable
5288 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5289 goto theend;
5290 lvar = reserve_local(cctx, var_start, varlen,
5291 cmdidx == CMD_const, type);
5292 if (lvar == NULL)
5293 goto theend;
5294 new_local = TRUE;
5295 }
5296
5297 member_type = type;
5298 if (var_end > var_start + varlen)
5299 {
5300 // Something follows after the variable: "var[idx]".
5301 if (is_decl)
5302 {
5303 emsg(_("E1087: cannot use an index when declaring a variable"));
5304 goto theend;
5305 }
5306
5307 if (var_start[varlen] == '[')
5308 {
5309 has_index = TRUE;
5310 if (type->tt_member == NULL)
5311 {
5312 semsg(_("E1088: cannot use an index on %s"), name);
5313 goto theend;
5314 }
5315 member_type = type->tt_member;
5316 }
5317 else
5318 {
5319 semsg("Not supported yet: %s", var_start);
5320 goto theend;
5321 }
5322 }
5323 else if (lvar == &arg_lvar)
5324 {
5325 semsg(_("E1090: Cannot assign to argument %s"), name);
5326 goto theend;
5327 }
5328
5329 if (!heredoc)
5330 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005331 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005332 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005333 if (oplen > 0 && var_count == 0)
5334 {
5335 // skip over the "=" and the expression
5336 p = skipwhite(op + oplen);
5337 compile_expr0(&p, cctx);
5338 }
5339 }
5340 else if (oplen > 0)
5341 {
5342 type_T *stacktype;
5343
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005344 // For "var = expr" evaluate the expression.
5345 if (var_count == 0)
5346 {
5347 int r;
5348
5349 // for "+=", "*=", "..=" etc. first load the current value
5350 if (*op != '=')
5351 {
5352 generate_loadvar(cctx, dest, name, lvar, type);
5353
5354 if (has_index)
5355 {
5356 // TODO: get member from list or dict
5357 emsg("Index with operation not supported yet");
5358 goto theend;
5359 }
5360 }
5361
5362 // Compile the expression. Temporarily hide the new local
5363 // variable here, it is not available to this expression.
5364 if (new_local)
5365 --cctx->ctx_locals.ga_len;
5366 instr_count = instr->ga_len;
5367 p = skipwhite(op + oplen);
5368 r = compile_expr0(&p, cctx);
5369 if (new_local)
5370 ++cctx->ctx_locals.ga_len;
5371 if (r == FAIL)
5372 goto theend;
5373 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005374 else if (semicolon && var_idx == var_count - 1)
5375 {
5376 // For "[var; var] = expr" get the rest of the list
5377 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5378 goto theend;
5379 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005380 else
5381 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382 // For "[var, var] = expr" get the "var_idx" item from the
5383 // list.
5384 if (generate_GETITEM(cctx, var_idx) == FAIL)
5385 return FAIL;
5386 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005387
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005388 stacktype = stack->ga_len == 0 ? &t_void
5389 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5390 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005391 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005392 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005393 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005394 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005395 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005396 emsg(_(e_cannot_use_void));
5397 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005398 }
5399 else
5400 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005401 // An empty list or dict has a &t_void member,
5402 // for a variable that implies &t_any.
5403 if (stacktype == &t_list_empty)
5404 lvar->lv_type = &t_list_any;
5405 else if (stacktype == &t_dict_empty)
5406 lvar->lv_type = &t_dict_any;
5407 else
5408 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005409 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005410 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005411 else
5412 {
5413 type_T *use_type = lvar->lv_type;
5414
5415 if (has_index)
5416 {
5417 use_type = use_type->tt_member;
5418 if (use_type == NULL)
5419 use_type = &t_void;
5420 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005421 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005422 == FAIL)
5423 goto theend;
5424 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005425 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005426 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005427 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005428 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005430 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005432 emsg(_(e_const_req_value));
5433 goto theend;
5434 }
5435 else if (!has_type || dest == dest_option)
5436 {
5437 emsg(_(e_type_req));
5438 goto theend;
5439 }
5440 else
5441 {
5442 // variables are always initialized
5443 if (ga_grow(instr, 1) == FAIL)
5444 goto theend;
5445 switch (member_type->tt_type)
5446 {
5447 case VAR_BOOL:
5448 generate_PUSHBOOL(cctx, VVAL_FALSE);
5449 break;
5450 case VAR_FLOAT:
5451#ifdef FEAT_FLOAT
5452 generate_PUSHF(cctx, 0.0);
5453#endif
5454 break;
5455 case VAR_STRING:
5456 generate_PUSHS(cctx, NULL);
5457 break;
5458 case VAR_BLOB:
5459 generate_PUSHBLOB(cctx, NULL);
5460 break;
5461 case VAR_FUNC:
5462 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5463 break;
5464 case VAR_LIST:
5465 generate_NEWLIST(cctx, 0);
5466 break;
5467 case VAR_DICT:
5468 generate_NEWDICT(cctx, 0);
5469 break;
5470 case VAR_JOB:
5471 generate_PUSHJOB(cctx, NULL);
5472 break;
5473 case VAR_CHANNEL:
5474 generate_PUSHCHANNEL(cctx, NULL);
5475 break;
5476 case VAR_NUMBER:
5477 case VAR_UNKNOWN:
5478 case VAR_ANY:
5479 case VAR_PARTIAL:
5480 case VAR_VOID:
5481 case VAR_SPECIAL: // cannot happen
5482 generate_PUSHNR(cctx, 0);
5483 break;
5484 }
5485 }
5486 if (var_count == 0)
5487 end = p;
5488 }
5489
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005490 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005491 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005492 break;
5493
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005494 if (oplen > 0 && *op != '=')
5495 {
5496 type_T *expected = &t_number;
5497 type_T *stacktype;
5498
5499 // TODO: if type is known use float or any operation
5500
5501 if (*op == '.')
5502 expected = &t_string;
5503 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005504 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005505 goto theend;
5506
5507 if (*op == '.')
5508 generate_instr_drop(cctx, ISN_CONCAT, 1);
5509 else
5510 {
5511 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5512
5513 if (isn == NULL)
5514 goto theend;
5515 switch (*op)
5516 {
5517 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5518 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5519 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5520 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5521 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005523 }
5524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005526 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005527 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005528 int r;
5529
5530 // Compile the "idx" in "var[idx]".
5531 if (new_local)
5532 --cctx->ctx_locals.ga_len;
5533 p = skipwhite(var_start + varlen + 1);
5534 r = compile_expr0(&p, cctx);
5535 if (new_local)
5536 ++cctx->ctx_locals.ga_len;
5537 if (r == FAIL)
5538 goto theend;
5539 if (*skipwhite(p) != ']')
5540 {
5541 emsg(_(e_missbrac));
5542 goto theend;
5543 }
5544 if (type->tt_type == VAR_DICT
5545 && may_generate_2STRING(-1, cctx) == FAIL)
5546 goto theend;
5547 if (type->tt_type == VAR_LIST
5548 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005549 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005550 {
5551 emsg(_(e_number_exp));
5552 goto theend;
5553 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005554
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005555 // Load the dict or list. On the stack we then have:
5556 // - value
5557 // - index
5558 // - variable
5559 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005560
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005561 if (type->tt_type == VAR_LIST)
5562 {
5563 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5564 return FAIL;
5565 }
5566 else if (type->tt_type == VAR_DICT)
5567 {
5568 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5569 return FAIL;
5570 }
5571 else
5572 {
5573 emsg(_(e_listreq));
5574 goto theend;
5575 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005576 }
5577 else
5578 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005579 switch (dest)
5580 {
5581 case dest_option:
5582 generate_STOREOPT(cctx, name + 1, opt_flags);
5583 break;
5584 case dest_global:
5585 // include g: with the name, easier to execute that way
5586 generate_STORE(cctx, ISN_STOREG, 0, name);
5587 break;
5588 case dest_buffer:
5589 // include b: with the name, easier to execute that way
5590 generate_STORE(cctx, ISN_STOREB, 0, name);
5591 break;
5592 case dest_window:
5593 // include w: with the name, easier to execute that way
5594 generate_STORE(cctx, ISN_STOREW, 0, name);
5595 break;
5596 case dest_tab:
5597 // include t: with the name, easier to execute that way
5598 generate_STORE(cctx, ISN_STORET, 0, name);
5599 break;
5600 case dest_env:
5601 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5602 break;
5603 case dest_reg:
5604 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5605 break;
5606 case dest_vimvar:
5607 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5608 break;
5609 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005610 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005611 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5612 imported_T *import = NULL;
5613 int sid = current_sctx.sc_sid;
5614 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005615
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005616 if (name[1] != ':')
5617 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005618 import = find_imported(name, 0, cctx);
5619 if (import != NULL)
5620 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005621 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005622
5623 idx = get_script_item_idx(sid, rawname, TRUE);
5624 // TODO: specific type
5625 if (idx < 0)
5626 {
5627 char_u *name_s = name;
5628
5629 // Include s: in the name for store_var()
5630 if (name[1] != ':')
5631 {
5632 int len = (int)STRLEN(name) + 3;
5633
5634 name_s = alloc(len);
5635 if (name_s == NULL)
5636 name_s = name;
5637 else
5638 vim_snprintf((char *)name_s, len,
5639 "s:%s", name);
5640 }
5641 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005642 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005643 if (name_s != name)
5644 vim_free(name_s);
5645 }
5646 else
5647 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005648 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005649 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005650 break;
5651 case dest_local:
5652 if (lvar != NULL)
5653 {
5654 isn_T *isn = ((isn_T *)instr->ga_data)
5655 + instr->ga_len - 1;
5656
5657 // optimization: turn "var = 123" from ISN_PUSHNR +
5658 // ISN_STORE into ISN_STORENR
5659 if (!lvar->lv_from_outer
5660 && instr->ga_len == instr_count + 1
5661 && isn->isn_type == ISN_PUSHNR)
5662 {
5663 varnumber_T val = isn->isn_arg.number;
5664
5665 isn->isn_type = ISN_STORENR;
5666 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5667 isn->isn_arg.storenr.stnr_val = val;
5668 if (stack->ga_len > 0)
5669 --stack->ga_len;
5670 }
5671 else if (lvar->lv_from_outer)
5672 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005673 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005674 else
5675 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5676 }
5677 break;
5678 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005679 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005680
5681 if (var_idx + 1 < var_count)
5682 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005683 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005684
5685 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005686 if (var_count > 0 && !semicolon)
5687 {
5688 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5689 goto theend;
5690 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005691
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005692 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005693
5694theend:
5695 vim_free(name);
5696 return ret;
5697}
5698
5699/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005700 * Check if "name" can be "unlet".
5701 */
5702 int
5703check_vim9_unlet(char_u *name)
5704{
5705 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5706 {
5707 semsg(_("E1081: Cannot unlet %s"), name);
5708 return FAIL;
5709 }
5710 return OK;
5711}
5712
5713/*
5714 * Callback passed to ex_unletlock().
5715 */
5716 static int
5717compile_unlet(
5718 lval_T *lvp,
5719 char_u *name_end,
5720 exarg_T *eap,
5721 int deep UNUSED,
5722 void *coookie)
5723{
5724 cctx_T *cctx = coookie;
5725
5726 if (lvp->ll_tv == NULL)
5727 {
5728 char_u *p = lvp->ll_name;
5729 int cc = *name_end;
5730 int ret = OK;
5731
5732 // Normal name. Only supports g:, w:, t: and b: namespaces.
5733 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005734 if (*p == '$')
5735 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5736 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005737 ret = FAIL;
5738 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005739 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005740
5741 *name_end = cc;
5742 return ret;
5743 }
5744
5745 // TODO: unlet {list}[idx]
5746 // TODO: unlet {dict}[key]
5747 emsg("Sorry, :unlet not fully implemented yet");
5748 return FAIL;
5749}
5750
5751/*
5752 * compile "unlet var", "lock var" and "unlock var"
5753 * "arg" points to "var".
5754 */
5755 static char_u *
5756compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5757{
5758 char_u *p = arg;
5759
5760 if (eap->cmdidx != CMD_unlet)
5761 {
5762 emsg("Sorry, :lock and unlock not implemented yet");
5763 return NULL;
5764 }
5765
5766 if (*p == '!')
5767 {
5768 p = skipwhite(p + 1);
5769 eap->forceit = TRUE;
5770 }
5771
5772 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5773 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5774}
5775
5776/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777 * Compile an :import command.
5778 */
5779 static char_u *
5780compile_import(char_u *arg, cctx_T *cctx)
5781{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005782 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005783}
5784
5785/*
5786 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5787 */
5788 static int
5789compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5790{
5791 garray_T *instr = &cctx->ctx_instr;
5792 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5793
5794 if (endlabel == NULL)
5795 return FAIL;
5796 endlabel->el_next = *el;
5797 *el = endlabel;
5798 endlabel->el_end_label = instr->ga_len;
5799
5800 generate_JUMP(cctx, when, 0);
5801 return OK;
5802}
5803
5804 static void
5805compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5806{
5807 garray_T *instr = &cctx->ctx_instr;
5808
5809 while (*el != NULL)
5810 {
5811 endlabel_T *cur = (*el);
5812 isn_T *isn;
5813
5814 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5815 isn->isn_arg.jump.jump_where = instr->ga_len;
5816 *el = cur->el_next;
5817 vim_free(cur);
5818 }
5819}
5820
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005821 static void
5822compile_free_jump_to_end(endlabel_T **el)
5823{
5824 while (*el != NULL)
5825 {
5826 endlabel_T *cur = (*el);
5827
5828 *el = cur->el_next;
5829 vim_free(cur);
5830 }
5831}
5832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005833/*
5834 * Create a new scope and set up the generic items.
5835 */
5836 static scope_T *
5837new_scope(cctx_T *cctx, scopetype_T type)
5838{
5839 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5840
5841 if (scope == NULL)
5842 return NULL;
5843 scope->se_outer = cctx->ctx_scope;
5844 cctx->ctx_scope = scope;
5845 scope->se_type = type;
5846 scope->se_local_count = cctx->ctx_locals.ga_len;
5847 return scope;
5848}
5849
5850/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005851 * Free the current scope and go back to the outer scope.
5852 */
5853 static void
5854drop_scope(cctx_T *cctx)
5855{
5856 scope_T *scope = cctx->ctx_scope;
5857
5858 if (scope == NULL)
5859 {
5860 iemsg("calling drop_scope() without a scope");
5861 return;
5862 }
5863 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005864 switch (scope->se_type)
5865 {
5866 case IF_SCOPE:
5867 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5868 case FOR_SCOPE:
5869 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5870 case WHILE_SCOPE:
5871 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5872 case TRY_SCOPE:
5873 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5874 case NO_SCOPE:
5875 case BLOCK_SCOPE:
5876 break;
5877 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005878 vim_free(scope);
5879}
5880
5881/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005882 * compile "if expr"
5883 *
5884 * "if expr" Produces instructions:
5885 * EVAL expr Push result of "expr"
5886 * JUMP_IF_FALSE end
5887 * ... body ...
5888 * end:
5889 *
5890 * "if expr | else" Produces instructions:
5891 * EVAL expr Push result of "expr"
5892 * JUMP_IF_FALSE else
5893 * ... body ...
5894 * JUMP_ALWAYS end
5895 * else:
5896 * ... body ...
5897 * end:
5898 *
5899 * "if expr1 | elseif expr2 | else" Produces instructions:
5900 * EVAL expr Push result of "expr"
5901 * JUMP_IF_FALSE elseif
5902 * ... body ...
5903 * JUMP_ALWAYS end
5904 * elseif:
5905 * EVAL expr Push result of "expr"
5906 * JUMP_IF_FALSE else
5907 * ... body ...
5908 * JUMP_ALWAYS end
5909 * else:
5910 * ... body ...
5911 * end:
5912 */
5913 static char_u *
5914compile_if(char_u *arg, cctx_T *cctx)
5915{
5916 char_u *p = arg;
5917 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005918 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005919 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005920 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005921 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005922
Bram Moolenaara5565e42020-05-09 15:44:01 +02005923 CLEAR_FIELD(ppconst);
5924 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005925 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005926 clear_ppconst(&ppconst);
5927 return NULL;
5928 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005929 if (cctx->ctx_skip == SKIP_YES)
5930 clear_ppconst(&ppconst);
5931 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005932 {
5933 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005934 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005935 clear_ppconst(&ppconst);
5936 }
5937 else
5938 {
5939 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005940 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005941 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005942 return NULL;
5943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005944
5945 scope = new_scope(cctx, IF_SCOPE);
5946 if (scope == NULL)
5947 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005948 scope->se_skip_save = skip_save;
5949 // "is_had_return" will be reset if any block does not end in :return
5950 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005951
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005952 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005953 {
5954 // "where" is set when ":elseif", "else" or ":endif" is found
5955 scope->se_u.se_if.is_if_label = instr->ga_len;
5956 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5957 }
5958 else
5959 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005960
5961 return p;
5962}
5963
5964 static char_u *
5965compile_elseif(char_u *arg, cctx_T *cctx)
5966{
5967 char_u *p = arg;
5968 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005969 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 isn_T *isn;
5971 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005972 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973
5974 if (scope == NULL || scope->se_type != IF_SCOPE)
5975 {
5976 emsg(_(e_elseif_without_if));
5977 return NULL;
5978 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005979 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005980 if (!cctx->ctx_had_return)
5981 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005982
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005983 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005984 {
5985 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005987 return NULL;
5988 // previous "if" or "elseif" jumps here
5989 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5990 isn->isn_arg.jump.jump_where = instr->ga_len;
5991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005992
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005993 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005994 CLEAR_FIELD(ppconst);
5995 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005996 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005997 clear_ppconst(&ppconst);
5998 return NULL;
5999 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006000 if (scope->se_skip_save == SKIP_YES)
6001 clear_ppconst(&ppconst);
6002 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006003 {
6004 // The expression results in a constant.
6005 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006006 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006007 clear_ppconst(&ppconst);
6008 scope->se_u.se_if.is_if_label = -1;
6009 }
6010 else
6011 {
6012 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006013 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006014 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006015 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006017 // "where" is set when ":elseif", "else" or ":endif" is found
6018 scope->se_u.se_if.is_if_label = instr->ga_len;
6019 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021
6022 return p;
6023}
6024
6025 static char_u *
6026compile_else(char_u *arg, cctx_T *cctx)
6027{
6028 char_u *p = arg;
6029 garray_T *instr = &cctx->ctx_instr;
6030 isn_T *isn;
6031 scope_T *scope = cctx->ctx_scope;
6032
6033 if (scope == NULL || scope->se_type != IF_SCOPE)
6034 {
6035 emsg(_(e_else_without_if));
6036 return NULL;
6037 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006038 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006039 if (!cctx->ctx_had_return)
6040 scope->se_u.se_if.is_had_return = FALSE;
6041 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042
Bram Moolenaarefd88552020-06-18 20:50:10 +02006043 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006044 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006045 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006046 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006047 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006048 if (!cctx->ctx_had_return
6049 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6050 JUMP_ALWAYS, cctx) == FAIL)
6051 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006052 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006053
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006054 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006055 {
6056 if (scope->se_u.se_if.is_if_label >= 0)
6057 {
6058 // previous "if" or "elseif" jumps here
6059 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6060 isn->isn_arg.jump.jump_where = instr->ga_len;
6061 scope->se_u.se_if.is_if_label = -1;
6062 }
6063 }
6064
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006065 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006066 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006068
6069 return p;
6070}
6071
6072 static char_u *
6073compile_endif(char_u *arg, cctx_T *cctx)
6074{
6075 scope_T *scope = cctx->ctx_scope;
6076 ifscope_T *ifscope;
6077 garray_T *instr = &cctx->ctx_instr;
6078 isn_T *isn;
6079
6080 if (scope == NULL || scope->se_type != IF_SCOPE)
6081 {
6082 emsg(_(e_endif_without_if));
6083 return NULL;
6084 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006085 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006086 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006087 if (!cctx->ctx_had_return)
6088 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006089
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006090 if (scope->se_u.se_if.is_if_label >= 0)
6091 {
6092 // previous "if" or "elseif" jumps here
6093 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6094 isn->isn_arg.jump.jump_where = instr->ga_len;
6095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096 // Fill in the "end" label in jumps at the end of the blocks.
6097 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006098 cctx->ctx_skip = scope->se_skip_save;
6099
6100 // If all the blocks end in :return and there is an :else then the
6101 // had_return flag is set.
6102 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006103
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006104 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006105 return arg;
6106}
6107
6108/*
6109 * compile "for var in expr"
6110 *
6111 * Produces instructions:
6112 * PUSHNR -1
6113 * STORE loop-idx Set index to -1
6114 * EVAL expr Push result of "expr"
6115 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6116 * - if beyond end, jump to "end"
6117 * - otherwise get item from list and push it
6118 * STORE var Store item in "var"
6119 * ... body ...
6120 * JUMP top Jump back to repeat
6121 * end: DROP Drop the result of "expr"
6122 *
6123 */
6124 static char_u *
6125compile_for(char_u *arg, cctx_T *cctx)
6126{
6127 char_u *p;
6128 size_t varlen;
6129 garray_T *instr = &cctx->ctx_instr;
6130 garray_T *stack = &cctx->ctx_type_stack;
6131 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006132 lvar_T *loop_lvar; // loop iteration variable
6133 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134 type_T *vartype;
6135
6136 // TODO: list of variables: "for [key, value] in dict"
6137 // parse "var"
6138 for (p = arg; eval_isnamec1(*p); ++p)
6139 ;
6140 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006141 var_lvar = lookup_local(arg, varlen, cctx);
6142 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143 {
6144 semsg(_("E1023: variable already defined: %s"), arg);
6145 return NULL;
6146 }
6147
6148 // consume "in"
6149 p = skipwhite(p);
6150 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6151 {
6152 emsg(_(e_missing_in));
6153 return NULL;
6154 }
6155 p = skipwhite(p + 2);
6156
6157
6158 scope = new_scope(cctx, FOR_SCOPE);
6159 if (scope == NULL)
6160 return NULL;
6161
6162 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006163 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6164 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006165 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006166 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006167 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006168 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006170
6171 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006172 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6173 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006174 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006175 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006176 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006179
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006180 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006181
6182 // compile "expr", it remains on the stack until "endfor"
6183 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006184 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006185 {
6186 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006188 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006189
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006190 // Now that we know the type of "var", check that it is a list, now or at
6191 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006192 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006193 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006195 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196 return NULL;
6197 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006198 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006199 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200
6201 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006202 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006203
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006204 generate_FOR(cctx, loop_lvar->lv_idx);
6205 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006206
6207 return arg;
6208}
6209
6210/*
6211 * compile "endfor"
6212 */
6213 static char_u *
6214compile_endfor(char_u *arg, cctx_T *cctx)
6215{
6216 garray_T *instr = &cctx->ctx_instr;
6217 scope_T *scope = cctx->ctx_scope;
6218 forscope_T *forscope;
6219 isn_T *isn;
6220
6221 if (scope == NULL || scope->se_type != FOR_SCOPE)
6222 {
6223 emsg(_(e_for));
6224 return NULL;
6225 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006226 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006227 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006228 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229
6230 // At end of ":for" scope jump back to the FOR instruction.
6231 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6232
6233 // Fill in the "end" label in the FOR statement so it can jump here
6234 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6235 isn->isn_arg.forloop.for_end = instr->ga_len;
6236
6237 // Fill in the "end" label any BREAK statements
6238 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6239
6240 // Below the ":for" scope drop the "expr" list from the stack.
6241 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6242 return NULL;
6243
6244 vim_free(scope);
6245
6246 return arg;
6247}
6248
6249/*
6250 * compile "while expr"
6251 *
6252 * Produces instructions:
6253 * top: EVAL expr Push result of "expr"
6254 * JUMP_IF_FALSE end jump if false
6255 * ... body ...
6256 * JUMP top Jump back to repeat
6257 * end:
6258 *
6259 */
6260 static char_u *
6261compile_while(char_u *arg, cctx_T *cctx)
6262{
6263 char_u *p = arg;
6264 garray_T *instr = &cctx->ctx_instr;
6265 scope_T *scope;
6266
6267 scope = new_scope(cctx, WHILE_SCOPE);
6268 if (scope == NULL)
6269 return NULL;
6270
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006271 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272
6273 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006274 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 return NULL;
6276
6277 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006278 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279 JUMP_IF_FALSE, cctx) == FAIL)
6280 return FAIL;
6281
6282 return p;
6283}
6284
6285/*
6286 * compile "endwhile"
6287 */
6288 static char_u *
6289compile_endwhile(char_u *arg, cctx_T *cctx)
6290{
6291 scope_T *scope = cctx->ctx_scope;
6292
6293 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6294 {
6295 emsg(_(e_while));
6296 return NULL;
6297 }
6298 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006299 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006300
6301 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006302 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303
6304 // Fill in the "end" label in the WHILE statement so it can jump here.
6305 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006306 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307
6308 vim_free(scope);
6309
6310 return arg;
6311}
6312
6313/*
6314 * compile "continue"
6315 */
6316 static char_u *
6317compile_continue(char_u *arg, cctx_T *cctx)
6318{
6319 scope_T *scope = cctx->ctx_scope;
6320
6321 for (;;)
6322 {
6323 if (scope == NULL)
6324 {
6325 emsg(_(e_continue));
6326 return NULL;
6327 }
6328 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6329 break;
6330 scope = scope->se_outer;
6331 }
6332
6333 // Jump back to the FOR or WHILE instruction.
6334 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006335 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6336 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337 return arg;
6338}
6339
6340/*
6341 * compile "break"
6342 */
6343 static char_u *
6344compile_break(char_u *arg, cctx_T *cctx)
6345{
6346 scope_T *scope = cctx->ctx_scope;
6347 endlabel_T **el;
6348
6349 for (;;)
6350 {
6351 if (scope == NULL)
6352 {
6353 emsg(_(e_break));
6354 return NULL;
6355 }
6356 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6357 break;
6358 scope = scope->se_outer;
6359 }
6360
6361 // Jump to the end of the FOR or WHILE loop.
6362 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006363 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006364 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006365 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6367 return FAIL;
6368
6369 return arg;
6370}
6371
6372/*
6373 * compile "{" start of block
6374 */
6375 static char_u *
6376compile_block(char_u *arg, cctx_T *cctx)
6377{
6378 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6379 return NULL;
6380 return skipwhite(arg + 1);
6381}
6382
6383/*
6384 * compile end of block: drop one scope
6385 */
6386 static void
6387compile_endblock(cctx_T *cctx)
6388{
6389 scope_T *scope = cctx->ctx_scope;
6390
6391 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006392 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006393 vim_free(scope);
6394}
6395
6396/*
6397 * compile "try"
6398 * Creates a new scope for the try-endtry, pointing to the first catch and
6399 * finally.
6400 * Creates another scope for the "try" block itself.
6401 * TRY instruction sets up exception handling at runtime.
6402 *
6403 * "try"
6404 * TRY -> catch1, -> finally push trystack entry
6405 * ... try block
6406 * "throw {exception}"
6407 * EVAL {exception}
6408 * THROW create exception
6409 * ... try block
6410 * " catch {expr}"
6411 * JUMP -> finally
6412 * catch1: PUSH exeception
6413 * EVAL {expr}
6414 * MATCH
6415 * JUMP nomatch -> catch2
6416 * CATCH remove exception
6417 * ... catch block
6418 * " catch"
6419 * JUMP -> finally
6420 * catch2: CATCH remove exception
6421 * ... catch block
6422 * " finally"
6423 * finally:
6424 * ... finally block
6425 * " endtry"
6426 * ENDTRY pop trystack entry, may rethrow
6427 */
6428 static char_u *
6429compile_try(char_u *arg, cctx_T *cctx)
6430{
6431 garray_T *instr = &cctx->ctx_instr;
6432 scope_T *try_scope;
6433 scope_T *scope;
6434
6435 // scope that holds the jumps that go to catch/finally/endtry
6436 try_scope = new_scope(cctx, TRY_SCOPE);
6437 if (try_scope == NULL)
6438 return NULL;
6439
6440 // "catch" is set when the first ":catch" is found.
6441 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006442 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006443 if (generate_instr(cctx, ISN_TRY) == NULL)
6444 return NULL;
6445
6446 // scope for the try block itself
6447 scope = new_scope(cctx, BLOCK_SCOPE);
6448 if (scope == NULL)
6449 return NULL;
6450
6451 return arg;
6452}
6453
6454/*
6455 * compile "catch {expr}"
6456 */
6457 static char_u *
6458compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6459{
6460 scope_T *scope = cctx->ctx_scope;
6461 garray_T *instr = &cctx->ctx_instr;
6462 char_u *p;
6463 isn_T *isn;
6464
6465 // end block scope from :try or :catch
6466 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6467 compile_endblock(cctx);
6468 scope = cctx->ctx_scope;
6469
6470 // Error if not in a :try scope
6471 if (scope == NULL || scope->se_type != TRY_SCOPE)
6472 {
6473 emsg(_(e_catch));
6474 return NULL;
6475 }
6476
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006477 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006478 {
6479 emsg(_("E1033: catch unreachable after catch-all"));
6480 return NULL;
6481 }
6482
6483 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006484 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006485 JUMP_ALWAYS, cctx) == FAIL)
6486 return NULL;
6487
6488 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006489 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006490 if (isn->isn_arg.try.try_catch == 0)
6491 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006492 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006493 {
6494 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006495 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006496 isn->isn_arg.jump.jump_where = instr->ga_len;
6497 }
6498
6499 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006500 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006501 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006502 scope->se_u.se_try.ts_caught_all = TRUE;
6503 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006504 }
6505 else
6506 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006507 char_u *end;
6508 char_u *pat;
6509 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006510 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006511 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 // Push v:exception, push {expr} and MATCH
6514 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6515
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006516 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006517 if (*end != *p)
6518 {
6519 semsg(_("E1067: Separator mismatch: %s"), p);
6520 vim_free(tofree);
6521 return FAIL;
6522 }
6523 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006524 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006525 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006526 len = (int)(end - tofree);
6527 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006528 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006529 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006530 if (pat == NULL)
6531 return FAIL;
6532 if (generate_PUSHS(cctx, pat) == FAIL)
6533 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006535 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6536 return NULL;
6537
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006538 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6540 return NULL;
6541 }
6542
6543 if (generate_instr(cctx, ISN_CATCH) == NULL)
6544 return NULL;
6545
6546 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6547 return NULL;
6548 return p;
6549}
6550
6551 static char_u *
6552compile_finally(char_u *arg, cctx_T *cctx)
6553{
6554 scope_T *scope = cctx->ctx_scope;
6555 garray_T *instr = &cctx->ctx_instr;
6556 isn_T *isn;
6557
6558 // end block scope from :try or :catch
6559 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6560 compile_endblock(cctx);
6561 scope = cctx->ctx_scope;
6562
6563 // Error if not in a :try scope
6564 if (scope == NULL || scope->se_type != TRY_SCOPE)
6565 {
6566 emsg(_(e_finally));
6567 return NULL;
6568 }
6569
6570 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006571 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572 if (isn->isn_arg.try.try_finally != 0)
6573 {
6574 emsg(_(e_finally_dup));
6575 return NULL;
6576 }
6577
6578 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006579 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580
Bram Moolenaar585fea72020-04-02 22:33:21 +02006581 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006582 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583 {
6584 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006585 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006587 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588 }
6589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 // TODO: set index in ts_finally_label jumps
6591
6592 return arg;
6593}
6594
6595 static char_u *
6596compile_endtry(char_u *arg, cctx_T *cctx)
6597{
6598 scope_T *scope = cctx->ctx_scope;
6599 garray_T *instr = &cctx->ctx_instr;
6600 isn_T *isn;
6601
6602 // end block scope from :catch or :finally
6603 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6604 compile_endblock(cctx);
6605 scope = cctx->ctx_scope;
6606
6607 // Error if not in a :try scope
6608 if (scope == NULL || scope->se_type != TRY_SCOPE)
6609 {
6610 if (scope == NULL)
6611 emsg(_(e_no_endtry));
6612 else if (scope->se_type == WHILE_SCOPE)
6613 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006614 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 emsg(_(e_endfor));
6616 else
6617 emsg(_(e_endif));
6618 return NULL;
6619 }
6620
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006621 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6623 {
6624 emsg(_("E1032: missing :catch or :finally"));
6625 return NULL;
6626 }
6627
6628 // Fill in the "end" label in jumps at the end of the blocks, if not done
6629 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006630 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631
6632 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006633 if (isn->isn_arg.try.try_catch == 0)
6634 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006635 if (isn->isn_arg.try.try_finally == 0)
6636 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006637
6638 if (scope->se_u.se_try.ts_catch_label != 0)
6639 {
6640 // Last catch without match jumps here
6641 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6642 isn->isn_arg.jump.jump_where = instr->ga_len;
6643 }
6644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006645 compile_endblock(cctx);
6646
6647 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6648 return NULL;
6649 return arg;
6650}
6651
6652/*
6653 * compile "throw {expr}"
6654 */
6655 static char_u *
6656compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6657{
6658 char_u *p = skipwhite(arg);
6659
Bram Moolenaara5565e42020-05-09 15:44:01 +02006660 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006661 return NULL;
6662 if (may_generate_2STRING(-1, cctx) == FAIL)
6663 return NULL;
6664 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6665 return NULL;
6666
6667 return p;
6668}
6669
6670/*
6671 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006672 * compile "echomsg expr"
6673 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006674 * compile "execute expr"
6675 */
6676 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006677compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006678{
6679 char_u *p = arg;
6680 int count = 0;
6681
6682 for (;;)
6683 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006684 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006685 return NULL;
6686 ++count;
6687 p = skipwhite(p);
6688 if (ends_excmd(*p))
6689 break;
6690 }
6691
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006692 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6693 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6694 else if (cmdidx == CMD_execute)
6695 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6696 else if (cmdidx == CMD_echomsg)
6697 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6698 else
6699 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 return p;
6701}
6702
6703/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006704 * A command that is not compiled, execute with legacy code.
6705 */
6706 static char_u *
6707compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6708{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006709 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006710 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006711 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006712
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006713 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006714 goto theend;
6715
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006716 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006717 {
6718 long argt = excmd_get_argt(eap->cmdidx);
6719 int usefilter = FALSE;
6720
6721 has_expr = argt & (EX_XFILE | EX_EXPAND);
6722
6723 // If the command can be followed by a bar, find the bar and truncate
6724 // it, so that the following command can be compiled.
6725 // The '|' is overwritten with a NUL, it is put back below.
6726 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6727 && *eap->arg == '!')
6728 // :w !filter or :r !filter or :r! filter
6729 usefilter = TRUE;
6730 if ((argt & EX_TRLBAR) && !usefilter)
6731 {
6732 separate_nextcmd(eap);
6733 if (eap->nextcmd != NULL)
6734 nextcmd = eap->nextcmd;
6735 }
6736 }
6737
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006738 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6739 {
6740 // expand filename in "syntax include [@group] filename"
6741 has_expr = TRUE;
6742 eap->arg = skipwhite(eap->arg + 7);
6743 if (*eap->arg == '@')
6744 eap->arg = skiptowhite(eap->arg);
6745 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006746
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006747 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006748 {
6749 int count = 0;
6750 char_u *start = skipwhite(line);
6751
6752 // :cmd xxx`=expr1`yyy`=expr2`zzz
6753 // PUSHS ":cmd xxx"
6754 // eval expr1
6755 // PUSHS "yyy"
6756 // eval expr2
6757 // PUSHS "zzz"
6758 // EXECCONCAT 5
6759 for (;;)
6760 {
6761 if (p > start)
6762 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006763 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006764 ++count;
6765 }
6766 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006767 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006768 return NULL;
6769 may_generate_2STRING(-1, cctx);
6770 ++count;
6771 p = skipwhite(p);
6772 if (*p != '`')
6773 {
6774 emsg(_("E1083: missing backtick"));
6775 return NULL;
6776 }
6777 start = p + 1;
6778
6779 p = (char_u *)strstr((char *)start, "`=");
6780 if (p == NULL)
6781 {
6782 if (*skipwhite(start) != NUL)
6783 {
6784 generate_PUSHS(cctx, vim_strsave(start));
6785 ++count;
6786 }
6787 break;
6788 }
6789 }
6790 generate_EXECCONCAT(cctx, count);
6791 }
6792 else
6793 generate_EXEC(cctx, line);
6794
6795theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006796 if (*nextcmd != NUL)
6797 {
6798 // the parser expects a pointer to the bar, put it back
6799 --nextcmd;
6800 *nextcmd = '|';
6801 }
6802
6803 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006804}
6805
6806/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006807 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006808 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006809 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006810 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006811add_def_function(ufunc_T *ufunc)
6812{
6813 dfunc_T *dfunc;
6814
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006815 if (def_functions.ga_len == 0)
6816 {
6817 // The first position is not used, so that a zero uf_dfunc_idx means it
6818 // wasn't set.
6819 if (ga_grow(&def_functions, 1) == FAIL)
6820 return FAIL;
6821 ++def_functions.ga_len;
6822 }
6823
Bram Moolenaar09689a02020-05-09 22:50:08 +02006824 // Add the function to "def_functions".
6825 if (ga_grow(&def_functions, 1) == FAIL)
6826 return FAIL;
6827 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6828 CLEAR_POINTER(dfunc);
6829 dfunc->df_idx = def_functions.ga_len;
6830 ufunc->uf_dfunc_idx = dfunc->df_idx;
6831 dfunc->df_ufunc = ufunc;
6832 ++def_functions.ga_len;
6833 return OK;
6834}
6835
6836/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006837 * After ex_function() has collected all the function lines: parse and compile
6838 * the lines into instructions.
6839 * Adds the function to "def_functions".
6840 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6841 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006842 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006843 * This can be used recursively through compile_lambda(), which may reallocate
6844 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006845 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006846 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006847 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006848compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006849{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006850 char_u *line = NULL;
6851 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006852 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853 cctx_T cctx;
6854 garray_T *instr;
6855 int called_emsg_before = called_emsg;
6856 int ret = FAIL;
6857 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006858 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006859 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006861 // When using a function that was compiled before: Free old instructions.
6862 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006863 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006864 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006865 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6866 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006867 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006869 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006870 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006871
Bram Moolenaar985116a2020-07-12 17:31:09 +02006872 ufunc->uf_def_status = UF_COMPILING;
6873
Bram Moolenaara80faa82020-04-12 19:37:17 +02006874 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875 cctx.ctx_ufunc = ufunc;
6876 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006877 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6879 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6880 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6881 cctx.ctx_type_list = &ufunc->uf_type_list;
6882 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6883 instr = &cctx.ctx_instr;
6884
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006885 // Set the context to the function, it may be compiled when called from
6886 // another script. Set the script version to the most modern one.
6887 // The line number will be set in next_line_from_context().
6888 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6890
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006891 // Make sure error messages are OK.
6892 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6893 if (do_estack_push)
6894 estack_push_ufunc(ufunc, 1);
6895
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006896 if (ufunc->uf_def_args.ga_len > 0)
6897 {
6898 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006899 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006900 int i;
6901 char_u *arg;
6902 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6903
6904 // Produce instructions for the default values of optional arguments.
6905 // Store the instruction index in uf_def_arg_idx[] so that we know
6906 // where to start when the function is called, depending on the number
6907 // of arguments.
6908 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6909 if (ufunc->uf_def_arg_idx == NULL)
6910 goto erret;
6911 for (i = 0; i < count; ++i)
6912 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006913 garray_T *stack = &cctx.ctx_type_stack;
6914 type_T *val_type;
6915 int arg_idx = first_def_arg + i;
6916
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006917 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6918 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006919 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006920 goto erret;
6921
6922 // If no type specified use the type of the default value.
6923 // Otherwise check that the default value type matches the
6924 // specified type.
6925 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6926 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6927 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006928 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006929 == FAIL)
6930 {
6931 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6932 arg_idx + 1);
6933 goto erret;
6934 }
6935
6936 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006937 goto erret;
6938 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006939 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6940 }
6941
6942 /*
6943 * Loop over all the lines of the function and generate instructions.
6944 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 for (;;)
6946 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006947 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006948 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006949 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006950 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006951
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006952 // Bail out on the first error to avoid a flood of errors and report
6953 // the right line number when inside try/catch.
6954 if (emsg_before != called_emsg)
6955 goto erret;
6956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957 if (line != NULL && *line == '|')
6958 // the line continues after a '|'
6959 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006960 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006961 && !(*line == '#' && (line == cctx.ctx_line_start
6962 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006964 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006965 goto erret;
6966 }
6967 else
6968 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006969 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006970 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006971 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006972 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006974 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975
Bram Moolenaara80faa82020-04-12 19:37:17 +02006976 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 ea.cmdlinep = &line;
6978 ea.cmd = skipwhite(line);
6979
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006980 // Some things can be recognized by the first character.
6981 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006983 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006984 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006985 if (ea.cmd[1] != '{')
6986 {
6987 line = (char_u *)"";
6988 continue;
6989 }
6990 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006992 case '}':
6993 {
6994 // "}" ends a block scope
6995 scopetype_T stype = cctx.ctx_scope == NULL
6996 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006998 if (stype == BLOCK_SCOPE)
6999 {
7000 compile_endblock(&cctx);
7001 line = ea.cmd;
7002 }
7003 else
7004 {
7005 emsg(_("E1025: using } outside of a block scope"));
7006 goto erret;
7007 }
7008 if (line != NULL)
7009 line = skipwhite(ea.cmd + 1);
7010 continue;
7011 }
7012
7013 case '{':
7014 // "{" starts a block scope
7015 // "{'a': 1}->func() is something else
7016 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7017 {
7018 line = compile_block(ea.cmd, &cctx);
7019 continue;
7020 }
7021 break;
7022
7023 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007024 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007025 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026 }
7027
7028 /*
7029 * COMMAND MODIFIERS
7030 */
7031 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7032 {
7033 if (errormsg != NULL)
7034 goto erret;
7035 // empty line or comment
7036 line = (char_u *)"";
7037 continue;
7038 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007039 // TODO: use modifiers in the command
7040 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007041 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042
7043 // Skip ":call" to get to the function name.
7044 if (checkforcmd(&ea.cmd, "call", 3))
7045 ea.cmd = skipwhite(ea.cmd);
7046
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007047 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007049 char_u *pskip;
7050
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007051 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007052 // find what follows.
7053 // Skip over "var.member", "var[idx]" and the like.
7054 // Also "&opt = val", "$ENV = val" and "@r = val".
7055 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007056 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007057 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007058 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007060 char_u *var_end;
7061 int oplen;
7062 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007064 var_end = find_name_end(pskip, NULL, NULL,
7065 FNE_CHECK_START | FNE_INCL_BR);
7066 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007067 if (oplen > 0)
7068 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007069 size_t len = p - ea.cmd;
7070
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007071 // Recognize an assignment if we recognize the variable
7072 // name:
7073 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007074 // "local = expr" where "local" is a local var.
7075 // "script = expr" where "script" is a script-local var.
7076 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007077 // "&opt = expr"
7078 // "$ENV = expr"
7079 // "@r = expr"
7080 if (*ea.cmd == '&'
7081 || *ea.cmd == '$'
7082 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007083 || ((len) > 2 && ea.cmd[1] == ':')
7084 || lookup_local(ea.cmd, len, &cctx) != NULL
7085 || lookup_arg(ea.cmd, len, NULL, NULL,
7086 NULL, &cctx) == OK
7087 || lookup_script(ea.cmd, len) == OK
7088 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007089 {
7090 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007091 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007092 goto erret;
7093 continue;
7094 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007095 }
7096 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007097
7098 if (*ea.cmd == '[')
7099 {
7100 // [var, var] = expr
7101 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7102 if (line == NULL)
7103 goto erret;
7104 if (line != ea.cmd)
7105 continue;
7106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 }
7108
7109 /*
7110 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007111 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007112 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007113 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007114 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007115 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007116 ea.cmd = skip_range(ea.cmd, NULL);
7117 if (ea.cmd > cmd && !starts_with_colon)
7118 {
7119 emsg(_(e_colon_required));
7120 goto erret;
7121 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007122 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007123 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007124 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007125 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126
7127 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7128 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007129 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007130 {
7131 line += STRLEN(line);
7132 continue;
7133 }
7134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007135 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007136 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007137 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007138 // CMD_let cannot happen, compile_assignment() above is used
7139 iemsg("Command from find_ex_command() not handled");
7140 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 }
7143
7144 p = skipwhite(p);
7145
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007146 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007147 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007148 && ea.cmdidx != CMD_elseif
7149 && ea.cmdidx != CMD_else
7150 && ea.cmdidx != CMD_endif)
7151 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007152 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007153 continue;
7154 }
7155
Bram Moolenaarefd88552020-06-18 20:50:10 +02007156 if (ea.cmdidx != CMD_elseif
7157 && ea.cmdidx != CMD_else
7158 && ea.cmdidx != CMD_endif
7159 && ea.cmdidx != CMD_endfor
7160 && ea.cmdidx != CMD_endwhile
7161 && ea.cmdidx != CMD_catch
7162 && ea.cmdidx != CMD_finally
7163 && ea.cmdidx != CMD_endtry)
7164 {
7165 if (cctx.ctx_had_return)
7166 {
7167 emsg(_("E1095: Unreachable code after :return"));
7168 goto erret;
7169 }
7170 }
7171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007172 switch (ea.cmdidx)
7173 {
7174 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007175 ea.arg = p;
7176 line = compile_nested_function(&ea, &cctx);
7177 break;
7178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007179 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007180 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007181 goto erret;
7182
7183 case CMD_return:
7184 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007185 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186 break;
7187
7188 case CMD_let:
7189 case CMD_const:
7190 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007191 if (line == p)
7192 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007193 break;
7194
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007195 case CMD_unlet:
7196 case CMD_unlockvar:
7197 case CMD_lockvar:
7198 line = compile_unletlock(p, &ea, &cctx);
7199 break;
7200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007201 case CMD_import:
7202 line = compile_import(p, &cctx);
7203 break;
7204
7205 case CMD_if:
7206 line = compile_if(p, &cctx);
7207 break;
7208 case CMD_elseif:
7209 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007210 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211 break;
7212 case CMD_else:
7213 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007214 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215 break;
7216 case CMD_endif:
7217 line = compile_endif(p, &cctx);
7218 break;
7219
7220 case CMD_while:
7221 line = compile_while(p, &cctx);
7222 break;
7223 case CMD_endwhile:
7224 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007225 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007226 break;
7227
7228 case CMD_for:
7229 line = compile_for(p, &cctx);
7230 break;
7231 case CMD_endfor:
7232 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007233 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234 break;
7235 case CMD_continue:
7236 line = compile_continue(p, &cctx);
7237 break;
7238 case CMD_break:
7239 line = compile_break(p, &cctx);
7240 break;
7241
7242 case CMD_try:
7243 line = compile_try(p, &cctx);
7244 break;
7245 case CMD_catch:
7246 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007247 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007248 break;
7249 case CMD_finally:
7250 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007251 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007252 break;
7253 case CMD_endtry:
7254 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007255 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256 break;
7257 case CMD_throw:
7258 line = compile_throw(p, &cctx);
7259 break;
7260
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007261 case CMD_eval:
7262 if (compile_expr0(&p, &cctx) == FAIL)
7263 goto erret;
7264
7265 // drop the return value
7266 generate_instr_drop(&cctx, ISN_DROP, 1);
7267
7268 line = skipwhite(p);
7269 break;
7270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007272 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007273 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007274 case CMD_echomsg:
7275 case CMD_echoerr:
7276 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007277 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007278
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007279 // TODO: other commands with an expression argument
7280
Bram Moolenaar002262f2020-07-08 17:47:57 +02007281 case CMD_SIZE:
7282 semsg(_("E476: Invalid command: %s"), ea.cmd);
7283 goto erret;
7284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007285 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007286 // Not recognized, execute with do_cmdline_cmd().
7287 ea.arg = p;
7288 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007289 break;
7290 }
7291 if (line == NULL)
7292 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007293 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007294
7295 if (cctx.ctx_type_stack.ga_len < 0)
7296 {
7297 iemsg("Type stack underflow");
7298 goto erret;
7299 }
7300 }
7301
7302 if (cctx.ctx_scope != NULL)
7303 {
7304 if (cctx.ctx_scope->se_type == IF_SCOPE)
7305 emsg(_(e_endif));
7306 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7307 emsg(_(e_endwhile));
7308 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7309 emsg(_(e_endfor));
7310 else
7311 emsg(_("E1026: Missing }"));
7312 goto erret;
7313 }
7314
Bram Moolenaarefd88552020-06-18 20:50:10 +02007315 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316 {
7317 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7318 {
7319 emsg(_("E1027: Missing return statement"));
7320 goto erret;
7321 }
7322
7323 // Return zero if there is no return at the end.
7324 generate_PUSHNR(&cctx, 0);
7325 generate_instr(&cctx, ISN_RETURN);
7326 }
7327
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007328 {
7329 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7330 + ufunc->uf_dfunc_idx;
7331 dfunc->df_deleted = FALSE;
7332 dfunc->df_instr = instr->ga_data;
7333 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007334 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007335 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007336 if (cctx.ctx_outer_used)
7337 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007338 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007339 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340
7341 ret = OK;
7342
7343erret:
7344 if (ret == FAIL)
7345 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007346 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007347 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7348 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007349
7350 for (idx = 0; idx < instr->ga_len; ++idx)
7351 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007352 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007353
Bram Moolenaar45a15082020-05-25 00:28:33 +02007354 // if using the last entry in the table we might as well remove it
7355 if (!dfunc->df_deleted
7356 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007357 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007358 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007359
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007360 while (cctx.ctx_scope != NULL)
7361 drop_scope(&cctx);
7362
Bram Moolenaar20431c92020-03-20 18:39:46 +01007363 // Don't execute this function body.
7364 ga_clear_strings(&ufunc->uf_lines);
7365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 if (errormsg != NULL)
7367 emsg(errormsg);
7368 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007369 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007370 }
7371
7372 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007373 if (do_estack_push)
7374 estack_pop();
7375
Bram Moolenaar20431c92020-03-20 18:39:46 +01007376 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007377 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007379 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380}
7381
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007382 void
7383set_function_type(ufunc_T *ufunc)
7384{
7385 int varargs = ufunc->uf_va_name != NULL;
7386 int argcount = ufunc->uf_args.ga_len;
7387
7388 // Create a type for the function, with the return type and any
7389 // argument types.
7390 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7391 // The type is included in "tt_args".
7392 if (argcount > 0 || varargs)
7393 {
7394 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7395 argcount, &ufunc->uf_type_list);
7396 // Add argument types to the function type.
7397 if (func_type_add_arg_types(ufunc->uf_func_type,
7398 argcount + varargs,
7399 &ufunc->uf_type_list) == FAIL)
7400 return;
7401 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7402 ufunc->uf_func_type->tt_min_argcount =
7403 argcount - ufunc->uf_def_args.ga_len;
7404 if (ufunc->uf_arg_types == NULL)
7405 {
7406 int i;
7407
7408 // lambda does not have argument types.
7409 for (i = 0; i < argcount; ++i)
7410 ufunc->uf_func_type->tt_args[i] = &t_any;
7411 }
7412 else
7413 mch_memmove(ufunc->uf_func_type->tt_args,
7414 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7415 if (varargs)
7416 {
7417 ufunc->uf_func_type->tt_args[argcount] =
7418 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7419 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7420 }
7421 }
7422 else
7423 // No arguments, can use a predefined type.
7424 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7425 argcount, &ufunc->uf_type_list);
7426}
7427
7428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429/*
7430 * Delete an instruction, free what it contains.
7431 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007432 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433delete_instr(isn_T *isn)
7434{
7435 switch (isn->isn_type)
7436 {
7437 case ISN_EXEC:
7438 case ISN_LOADENV:
7439 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007440 case ISN_LOADB:
7441 case ISN_LOADW:
7442 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007444 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445 case ISN_PUSHEXC:
7446 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007447 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007448 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007449 case ISN_STOREB:
7450 case ISN_STOREW:
7451 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007452 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 vim_free(isn->isn_arg.string);
7454 break;
7455
7456 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007457 case ISN_STORES:
7458 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459 break;
7460
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007461 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007462 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007463 vim_free(isn->isn_arg.unlet.ul_name);
7464 break;
7465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466 case ISN_STOREOPT:
7467 vim_free(isn->isn_arg.storeopt.so_name);
7468 break;
7469
7470 case ISN_PUSHBLOB: // push blob isn_arg.blob
7471 blob_unref(isn->isn_arg.blob);
7472 break;
7473
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007474 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007475#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007476 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007477#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007478 break;
7479
7480 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007481#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007482 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007483#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007484 break;
7485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 case ISN_UCALL:
7487 vim_free(isn->isn_arg.ufunc.cuf_name);
7488 break;
7489
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007490 case ISN_FUNCREF:
7491 {
7492 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7493 + isn->isn_arg.funcref.fr_func;
7494 func_ptr_unref(dfunc->df_ufunc);
7495 }
7496 break;
7497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007498 case ISN_2BOOL:
7499 case ISN_2STRING:
7500 case ISN_ADDBLOB:
7501 case ISN_ADDLIST:
7502 case ISN_BCALL:
7503 case ISN_CATCH:
7504 case ISN_CHECKNR:
7505 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007506 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007507 case ISN_COMPAREANY:
7508 case ISN_COMPAREBLOB:
7509 case ISN_COMPAREBOOL:
7510 case ISN_COMPAREDICT:
7511 case ISN_COMPAREFLOAT:
7512 case ISN_COMPAREFUNC:
7513 case ISN_COMPARELIST:
7514 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515 case ISN_COMPARESPECIAL:
7516 case ISN_COMPARESTRING:
7517 case ISN_CONCAT:
7518 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007519 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007520 case ISN_DROP:
7521 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007522 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007523 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007524 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007525 case ISN_EXECCONCAT:
7526 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007527 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007528 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007529 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007530 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007531 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532 case ISN_JUMP:
7533 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007534 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007535 case ISN_LOADSCRIPT:
7536 case ISN_LOADREG:
7537 case ISN_LOADV:
7538 case ISN_NEGATENR:
7539 case ISN_NEWDICT:
7540 case ISN_NEWLIST:
7541 case ISN_OPNR:
7542 case ISN_OPFLOAT:
7543 case ISN_OPANY:
7544 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007545 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007546 case ISN_PUSHF:
7547 case ISN_PUSHNR:
7548 case ISN_PUSHBOOL:
7549 case ISN_PUSHSPEC:
7550 case ISN_RETURN:
7551 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007552 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007553 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007554 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007555 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007557 case ISN_STOREDICT:
7558 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007559 case ISN_THROW:
7560 case ISN_TRY:
7561 // nothing allocated
7562 break;
7563 }
7564}
7565
7566/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007567 * Free all instructions for "dfunc".
7568 */
7569 static void
7570delete_def_function_contents(dfunc_T *dfunc)
7571{
7572 int idx;
7573
7574 ga_clear(&dfunc->df_def_args_isn);
7575
7576 if (dfunc->df_instr != NULL)
7577 {
7578 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7579 delete_instr(dfunc->df_instr + idx);
7580 VIM_CLEAR(dfunc->df_instr);
7581 }
7582
7583 dfunc->df_deleted = TRUE;
7584}
7585
7586/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007587 * When a user function is deleted, clear the contents of any associated def
7588 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007589 */
7590 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007591clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007592{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007593 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594 {
7595 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7596 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007597
Bram Moolenaar20431c92020-03-20 18:39:46 +01007598 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007599 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007600 }
7601}
7602
7603#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007604/*
7605 * Free all functions defined with ":def".
7606 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607 void
7608free_def_functions(void)
7609{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007610 int idx;
7611
7612 for (idx = 0; idx < def_functions.ga_len; ++idx)
7613 {
7614 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7615
7616 delete_def_function_contents(dfunc);
7617 }
7618
7619 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007620}
7621#endif
7622
7623
7624#endif // FEAT_EVAL