blob: 079d160083eb92c9b308d894a31302f73f13920b [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
26/*
27 * Chain of jump instructions where the end label needs to be set.
28 */
29typedef struct endlabel_S endlabel_T;
30struct endlabel_S {
31 endlabel_T *el_next; // chain end_label locations
32 int el_end_label; // instruction idx where to set end
33};
34
35/*
36 * info specific for the scope of :if / elseif / else
37 */
38typedef struct {
39 int is_if_label; // instruction idx at IF or ELSEIF
40 endlabel_T *is_end_label; // instructions to set end label
41} ifscope_T;
42
43/*
44 * info specific for the scope of :while
45 */
46typedef struct {
47 int ws_top_label; // instruction idx at WHILE
48 endlabel_T *ws_end_label; // instructions to set end
49} whilescope_T;
50
51/*
52 * info specific for the scope of :for
53 */
54typedef struct {
55 int fs_top_label; // instruction idx at FOR
56 endlabel_T *fs_end_label; // break instructions
57} forscope_T;
58
59/*
60 * info specific for the scope of :try
61 */
62typedef struct {
63 int ts_try_label; // instruction idx at TRY
64 endlabel_T *ts_end_label; // jump to :finally or :endtry
65 int ts_catch_label; // instruction idx of last CATCH
66 int ts_caught_all; // "catch" without argument encountered
67} tryscope_T;
68
69typedef enum {
70 NO_SCOPE,
71 IF_SCOPE,
72 WHILE_SCOPE,
73 FOR_SCOPE,
74 TRY_SCOPE,
75 BLOCK_SCOPE
76} scopetype_T;
77
78/*
79 * Info for one scope, pointed to by "ctx_scope".
80 */
81typedef struct scope_S scope_T;
82struct scope_S {
83 scope_T *se_outer; // scope containing this one
84 scopetype_T se_type;
85 int se_local_count; // ctx_locals.ga_len before scope
86 union {
87 ifscope_T se_if;
88 whilescope_T se_while;
89 forscope_T se_for;
90 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +010091 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092};
93
94/*
95 * Entry for "ctx_locals". Used for arguments and local variables.
96 */
97typedef struct {
98 char_u *lv_name;
99 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200100 int lv_idx; // index of the variable on the stack
101 int lv_from_outer; // when TRUE using ctx_outer scope
102 int lv_const; // when TRUE cannot be assigned to
103 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104} lvar_T;
105
106/*
107 * Context for compiling lines of Vim script.
108 * Stores info about the local variables and condition stack.
109 */
110struct cctx_S {
111 ufunc_T *ctx_ufunc; // current function
112 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200113 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114 garray_T ctx_instr; // generated instructions
115
116 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200117 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100118
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200119 int ctx_closure_count; // number of closures created in the
120 // function
121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100122 garray_T ctx_imports; // imported items
123
Bram Moolenaara259d8d2020-01-31 20:10:50 +0100124 int ctx_skip; // when TRUE skip commands, when FALSE skip
125 // commands after "else"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126 scope_T *ctx_scope; // current scope, NULL at toplevel
127
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200128 cctx_T *ctx_outer; // outer scope for lambda or nested
129 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200130 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200133 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100134};
135
136static char e_var_notfound[] = N_("E1001: variable not found: %s");
137static char e_syntax_at[] = N_("E1002: Syntax error at %s");
138
139static int compile_expr1(char_u **arg, cctx_T *cctx);
140static int compile_expr2(char_u **arg, cctx_T *cctx);
141static int compile_expr3(char_u **arg, cctx_T *cctx);
Bram Moolenaar20431c92020-03-20 18:39:46 +0100142static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200143static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
144static int check_type(type_T *expected, type_T *actual, int give_msg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145
146/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200147 * Lookup variable "name" in the local scope and return it.
148 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200150 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151lookup_local(char_u *name, size_t len, cctx_T *cctx)
152{
153 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200154 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100156 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200157 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200158
159 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
161 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 if (STRNCMP(name, lvar->lv_name, len) == 0
164 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200165 {
166 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200167 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170
171 // Find local in outer function scope.
172 if (cctx->ctx_outer != NULL)
173 {
174 lvar = lookup_local(name, len, cctx->ctx_outer);
175 if (lvar != NULL)
176 {
177 // TODO: are there situations we should not mark the outer scope as
178 // used?
179 cctx->ctx_outer_used = TRUE;
180 lvar->lv_from_outer = TRUE;
181 return lvar;
182 }
183 }
184
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200185 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100186}
187
188/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200189 * Lookup an argument in the current function and an enclosing function.
190 * Returns the argument index in "idxp"
191 * Returns the argument type in "type"
192 * Sets "gen_load_outer" to TRUE if found in outer scope.
193 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100194 */
195 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200196lookup_arg(
197 char_u *name,
198 size_t len,
199 int *idxp,
200 type_T **type,
201 int *gen_load_outer,
202 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203{
204 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200205 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100207 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200208 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100209 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
210 {
211 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
212
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200213 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
214 {
215 if (idxp != NULL)
216 {
217 // Arguments are located above the frame pointer. One further
218 // if there is a vararg argument
219 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
220 + STACK_FRAME_SIZE)
221 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
222
223 if (cctx->ctx_ufunc->uf_arg_types != NULL)
224 *type = cctx->ctx_ufunc->uf_arg_types[idx];
225 else
226 *type = &t_any;
227 }
228 return OK;
229 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100231
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200232 va_name = cctx->ctx_ufunc->uf_va_name;
233 if (va_name != NULL
234 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
235 {
236 if (idxp != NULL)
237 {
238 // varargs is always the last argument
239 *idxp = -STACK_FRAME_SIZE - 1;
240 *type = cctx->ctx_ufunc->uf_va_type;
241 }
242 return OK;
243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200245 if (cctx->ctx_outer != NULL)
246 {
247 // Lookup the name for an argument of the outer function.
248 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
249 == OK)
250 {
251 *gen_load_outer = TRUE;
252 return OK;
253 }
254 }
255
256 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257}
258
259/*
260 * Lookup a variable in the current script.
261 * Returns OK or FAIL.
262 */
263 static int
264lookup_script(char_u *name, size_t len)
265{
266 int cc;
267 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
268 dictitem_T *di;
269
270 cc = name[len];
271 name[len] = NUL;
272 di = find_var_in_ht(ht, 0, name, TRUE);
273 name[len] = cc;
274 return di == NULL ? FAIL: OK;
275}
276
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100277/*
278 * Check if "p[len]" is already defined, either in script "import_sid" or in
279 * compilation context "cctx".
280 * Return FAIL and give an error if it defined.
281 */
282 int
283check_defined(char_u *p, int len, cctx_T *cctx)
284{
285 if (lookup_script(p, len) == OK
286 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200287 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100288 || find_imported(p, len, cctx) != NULL)))
289 {
290 semsg("E1073: imported name already defined: %s", p);
291 return FAIL;
292 }
293 return OK;
294}
295
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200296/*
297 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
298 * be freed later.
299 */
300 static type_T *
301alloc_type(garray_T *type_gap)
302{
303 type_T *type;
304
305 if (ga_grow(type_gap, 1) == FAIL)
306 return NULL;
307 type = ALLOC_CLEAR_ONE(type_T);
308 if (type != NULL)
309 {
310 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
311 ++type_gap->ga_len;
312 }
313 return type;
314}
315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100316 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200317get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318{
319 type_T *type;
320
321 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200322 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200324 if (member_type->tt_type == VAR_VOID
325 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100326 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100327 if (member_type->tt_type == VAR_BOOL)
328 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329 if (member_type->tt_type == VAR_NUMBER)
330 return &t_list_number;
331 if (member_type->tt_type == VAR_STRING)
332 return &t_list_string;
333
334 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200335 type = alloc_type(type_gap);
336 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100337 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338 type->tt_type = VAR_LIST;
339 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200340 type->tt_argcount = 0;
341 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342 return type;
343}
344
345 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200346get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100347{
348 type_T *type;
349
350 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200351 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200353 if (member_type->tt_type == VAR_VOID
354 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100355 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100356 if (member_type->tt_type == VAR_BOOL)
357 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358 if (member_type->tt_type == VAR_NUMBER)
359 return &t_dict_number;
360 if (member_type->tt_type == VAR_STRING)
361 return &t_dict_string;
362
363 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200364 type = alloc_type(type_gap);
365 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100366 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 type->tt_type = VAR_DICT;
368 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200369 type->tt_argcount = 0;
370 type->tt_args = NULL;
371 return type;
372}
373
374/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200375 * Allocate a new type for a function.
376 */
377 static type_T *
378alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
379{
380 type_T *type = alloc_type(type_gap);
381
382 if (type == NULL)
383 return &t_any;
384 type->tt_type = VAR_FUNC;
385 type->tt_member = ret_type;
386 type->tt_argcount = argcount;
387 type->tt_args = NULL;
388 return type;
389}
390
391/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200392 * Get a function type, based on the return type "ret_type".
393 * If "argcount" is -1 or 0 a predefined type can be used.
394 * If "argcount" > 0 always create a new type, so that arguments can be added.
395 */
396 static type_T *
397get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
398{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200399 // recognize commonly used types
400 if (argcount <= 0)
401 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200402 if (ret_type == &t_unknown)
403 {
404 // (argcount == 0) is not possible
405 return &t_func_unknown;
406 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200407 if (ret_type == &t_void)
408 {
409 if (argcount == 0)
410 return &t_func_0_void;
411 else
412 return &t_func_void;
413 }
414 if (ret_type == &t_any)
415 {
416 if (argcount == 0)
417 return &t_func_0_any;
418 else
419 return &t_func_any;
420 }
421 if (ret_type == &t_number)
422 {
423 if (argcount == 0)
424 return &t_func_0_number;
425 else
426 return &t_func_number;
427 }
428 if (ret_type == &t_string)
429 {
430 if (argcount == 0)
431 return &t_func_0_string;
432 else
433 return &t_func_string;
434 }
435 }
436
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200437 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100438}
439
Bram Moolenaara8c17702020-04-01 21:17:24 +0200440/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200441 * For a function type, reserve space for "argcount" argument types (including
442 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200443 */
444 static int
445func_type_add_arg_types(
446 type_T *functype,
447 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200448 garray_T *type_gap)
449{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200450 // To make it easy to free the space needed for the argument types, add the
451 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200452 if (ga_grow(type_gap, 1) == FAIL)
453 return FAIL;
454 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
455 if (functype->tt_args == NULL)
456 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200457 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
458 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200459 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200460 return OK;
461}
462
463/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200464 * Return the type_T for a typval. Only for primitive types.
465 */
466 static type_T *
467typval2type(typval_T *tv)
468{
469 if (tv->v_type == VAR_NUMBER)
470 return &t_number;
471 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200472 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200473 if (tv->v_type == VAR_STRING)
474 return &t_string;
475 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
476 return &t_list_string;
477 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
478 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200479 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200480}
481
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200482 static void
483type_mismatch(type_T *expected, type_T *actual)
484{
485 char *tofree1, *tofree2;
486
487 semsg(_("E1013: type mismatch, expected %s but got %s"),
488 type_name(expected, &tofree1), type_name(actual, &tofree2));
489 vim_free(tofree1);
490 vim_free(tofree2);
491}
492
493 static void
494arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
495{
496 char *tofree1, *tofree2;
497
498 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
499 argidx,
500 type_name(expected, &tofree1), type_name(actual, &tofree2));
501 vim_free(tofree1);
502 vim_free(tofree2);
503}
504
505/*
506 * Check if the expected and actual types match.
507 * Does not allow for assigning "any" to a specific type.
508 */
509 static int
510check_type(type_T *expected, type_T *actual, int give_msg)
511{
512 int ret = OK;
513
514 // When expected is "unknown" we accept any actual type.
515 // When expected is "any" we accept any actual type except "void".
516 if (expected->tt_type != VAR_UNKNOWN
517 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
518
519 {
520 if (expected->tt_type != actual->tt_type)
521 {
522 if (give_msg)
523 type_mismatch(expected, actual);
524 return FAIL;
525 }
526 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
527 {
528 // "unknown" is used for an empty list or dict
529 if (actual->tt_member != &t_unknown)
530 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
531 }
532 else if (expected->tt_type == VAR_FUNC)
533 {
534 if (expected->tt_member != &t_unknown)
535 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
536 if (ret == OK && expected->tt_argcount != -1
537 && (actual->tt_argcount < expected->tt_min_argcount
538 || actual->tt_argcount > expected->tt_argcount))
539 ret = FAIL;
540 }
541 if (ret == FAIL && give_msg)
542 type_mismatch(expected, actual);
543 }
544 return ret;
545}
546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100547/////////////////////////////////////////////////////////////////////
548// Following generate_ functions expect the caller to call ga_grow().
549
Bram Moolenaar080457c2020-03-03 21:53:32 +0100550#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
551#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553/*
554 * Generate an instruction without arguments.
555 * Returns a pointer to the new instruction, NULL if failed.
556 */
557 static isn_T *
558generate_instr(cctx_T *cctx, isntype_T isn_type)
559{
560 garray_T *instr = &cctx->ctx_instr;
561 isn_T *isn;
562
Bram Moolenaar080457c2020-03-03 21:53:32 +0100563 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100564 if (ga_grow(instr, 1) == FAIL)
565 return NULL;
566 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
567 isn->isn_type = isn_type;
568 isn->isn_lnum = cctx->ctx_lnum + 1;
569 ++instr->ga_len;
570
571 return isn;
572}
573
574/*
575 * Generate an instruction without arguments.
576 * "drop" will be removed from the stack.
577 * Returns a pointer to the new instruction, NULL if failed.
578 */
579 static isn_T *
580generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
581{
582 garray_T *stack = &cctx->ctx_type_stack;
583
Bram Moolenaar080457c2020-03-03 21:53:32 +0100584 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 stack->ga_len -= drop;
586 return generate_instr(cctx, isn_type);
587}
588
589/*
590 * Generate instruction "isn_type" and put "type" on the type stack.
591 */
592 static isn_T *
593generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
594{
595 isn_T *isn;
596 garray_T *stack = &cctx->ctx_type_stack;
597
598 if ((isn = generate_instr(cctx, isn_type)) == NULL)
599 return NULL;
600
601 if (ga_grow(stack, 1) == FAIL)
602 return NULL;
603 ((type_T **)stack->ga_data)[stack->ga_len] = type;
604 ++stack->ga_len;
605
606 return isn;
607}
608
609/*
610 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
611 */
612 static int
613may_generate_2STRING(int offset, cctx_T *cctx)
614{
615 isn_T *isn;
616 garray_T *stack = &cctx->ctx_type_stack;
617 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
618
619 if ((*type)->tt_type == VAR_STRING)
620 return OK;
621 *type = &t_string;
622
623 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
624 return FAIL;
625 isn->isn_arg.number = offset;
626
627 return OK;
628}
629
630 static int
631check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
632{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200633 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200635 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 {
637 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100638 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 else
640 semsg(_("E1036: %c requires number or float arguments"), *op);
641 return FAIL;
642 }
643 return OK;
644}
645
646/*
647 * Generate an instruction with two arguments. The instruction depends on the
648 * type of the arguments.
649 */
650 static int
651generate_two_op(cctx_T *cctx, char_u *op)
652{
653 garray_T *stack = &cctx->ctx_type_stack;
654 type_T *type1;
655 type_T *type2;
656 vartype_T vartype;
657 isn_T *isn;
658
Bram Moolenaar080457c2020-03-03 21:53:32 +0100659 RETURN_OK_IF_SKIP(cctx);
660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100661 // Get the known type of the two items on the stack. If they are matching
662 // use a type-specific instruction. Otherwise fall back to runtime type
663 // checking.
664 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
665 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200666 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667 if (type1->tt_type == type2->tt_type
668 && (type1->tt_type == VAR_NUMBER
669 || type1->tt_type == VAR_LIST
670#ifdef FEAT_FLOAT
671 || type1->tt_type == VAR_FLOAT
672#endif
673 || type1->tt_type == VAR_BLOB))
674 vartype = type1->tt_type;
675
676 switch (*op)
677 {
678 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200679 && type1->tt_type != VAR_ANY
680 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 && check_number_or_float(
682 type1->tt_type, type2->tt_type, op) == FAIL)
683 return FAIL;
684 isn = generate_instr_drop(cctx,
685 vartype == VAR_NUMBER ? ISN_OPNR
686 : vartype == VAR_LIST ? ISN_ADDLIST
687 : vartype == VAR_BLOB ? ISN_ADDBLOB
688#ifdef FEAT_FLOAT
689 : vartype == VAR_FLOAT ? ISN_OPFLOAT
690#endif
691 : ISN_OPANY, 1);
692 if (isn != NULL)
693 isn->isn_arg.op.op_type = EXPR_ADD;
694 break;
695
696 case '-':
697 case '*':
698 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
699 op) == FAIL)
700 return FAIL;
701 if (vartype == VAR_NUMBER)
702 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
703#ifdef FEAT_FLOAT
704 else if (vartype == VAR_FLOAT)
705 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
706#endif
707 else
708 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
709 if (isn != NULL)
710 isn->isn_arg.op.op_type = *op == '*'
711 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
712 break;
713
Bram Moolenaar4c683752020-04-05 21:38:23 +0200714 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200716 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 && type2->tt_type != VAR_NUMBER))
718 {
719 emsg(_("E1035: % requires number arguments"));
720 return FAIL;
721 }
722 isn = generate_instr_drop(cctx,
723 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
724 if (isn != NULL)
725 isn->isn_arg.op.op_type = EXPR_REM;
726 break;
727 }
728
729 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200730 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731 {
732 type_T *type = &t_any;
733
734#ifdef FEAT_FLOAT
735 // float+number and number+float results in float
736 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
737 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
738 type = &t_float;
739#endif
740 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
741 }
742
743 return OK;
744}
745
746/*
747 * Generate an ISN_COMPARE* instruction with a boolean result.
748 */
749 static int
750generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
751{
752 isntype_T isntype = ISN_DROP;
753 isn_T *isn;
754 garray_T *stack = &cctx->ctx_type_stack;
755 vartype_T type1;
756 vartype_T type2;
757
Bram Moolenaar080457c2020-03-03 21:53:32 +0100758 RETURN_OK_IF_SKIP(cctx);
759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 // Get the known type of the two items on the stack. If they are matching
761 // use a type-specific instruction. Otherwise fall back to runtime type
762 // checking.
763 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
764 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200765 if (type1 == VAR_UNKNOWN)
766 type1 = VAR_ANY;
767 if (type2 == VAR_UNKNOWN)
768 type2 = VAR_ANY;
769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 if (type1 == type2)
771 {
772 switch (type1)
773 {
774 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
775 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
776 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
777 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
778 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
779 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
780 case VAR_LIST: isntype = ISN_COMPARELIST; break;
781 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
782 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 default: isntype = ISN_COMPAREANY; break;
784 }
785 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200786 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
788 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
789 isntype = ISN_COMPAREANY;
790
791 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
792 && (isntype == ISN_COMPAREBOOL
793 || isntype == ISN_COMPARESPECIAL
794 || isntype == ISN_COMPARENR
795 || isntype == ISN_COMPAREFLOAT))
796 {
797 semsg(_("E1037: Cannot use \"%s\" with %s"),
798 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
799 return FAIL;
800 }
801 if (isntype == ISN_DROP
802 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
803 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
804 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
805 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
806 && exptype != EXPR_IS && exptype != EXPR_ISNOT
807 && (type1 == VAR_BLOB || type2 == VAR_BLOB
808 || type1 == VAR_LIST || type2 == VAR_LIST))))
809 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100810 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 vartype_name(type1), vartype_name(type2));
812 return FAIL;
813 }
814
815 if ((isn = generate_instr(cctx, isntype)) == NULL)
816 return FAIL;
817 isn->isn_arg.op.op_type = exptype;
818 isn->isn_arg.op.op_ic = ic;
819
820 // takes two arguments, puts one bool back
821 if (stack->ga_len >= 2)
822 {
823 --stack->ga_len;
824 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
825 }
826
827 return OK;
828}
829
830/*
831 * Generate an ISN_2BOOL instruction.
832 */
833 static int
834generate_2BOOL(cctx_T *cctx, int invert)
835{
836 isn_T *isn;
837 garray_T *stack = &cctx->ctx_type_stack;
838
Bram Moolenaar080457c2020-03-03 21:53:32 +0100839 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
841 return FAIL;
842 isn->isn_arg.number = invert;
843
844 // type becomes bool
845 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
846
847 return OK;
848}
849
850 static int
851generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
852{
853 isn_T *isn;
854 garray_T *stack = &cctx->ctx_type_stack;
855
Bram Moolenaar080457c2020-03-03 21:53:32 +0100856 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
858 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200859 // TODO: whole type, e.g. for a function also arg and return types
860 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 isn->isn_arg.type.ct_off = offset;
862
863 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200864 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865
866 return OK;
867}
868
869/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200870 * Check that
871 * - "actual" is "expected" type or
872 * - "actual" is a type that can be "expected" type: add a runtime check; or
873 * - return FAIL.
874 */
875 static int
876need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
877{
878 if (check_type(expected, actual, FALSE) == OK)
879 return OK;
880 if (actual->tt_type != VAR_ANY
881 && actual->tt_type != VAR_UNKNOWN
882 && !(actual->tt_type == VAR_FUNC
883 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
884 {
885 type_mismatch(expected, actual);
886 return FAIL;
887 }
888 generate_TYPECHECK(cctx, expected, offset);
889 return OK;
890}
891
892/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 * Generate an ISN_PUSHNR instruction.
894 */
895 static int
896generate_PUSHNR(cctx_T *cctx, varnumber_T number)
897{
898 isn_T *isn;
899
Bram Moolenaar080457c2020-03-03 21:53:32 +0100900 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
902 return FAIL;
903 isn->isn_arg.number = number;
904
905 return OK;
906}
907
908/*
909 * Generate an ISN_PUSHBOOL instruction.
910 */
911 static int
912generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
913{
914 isn_T *isn;
915
Bram Moolenaar080457c2020-03-03 21:53:32 +0100916 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
918 return FAIL;
919 isn->isn_arg.number = number;
920
921 return OK;
922}
923
924/*
925 * Generate an ISN_PUSHSPEC instruction.
926 */
927 static int
928generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
929{
930 isn_T *isn;
931
Bram Moolenaar080457c2020-03-03 21:53:32 +0100932 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
934 return FAIL;
935 isn->isn_arg.number = number;
936
937 return OK;
938}
939
940#ifdef FEAT_FLOAT
941/*
942 * Generate an ISN_PUSHF instruction.
943 */
944 static int
945generate_PUSHF(cctx_T *cctx, float_T fnumber)
946{
947 isn_T *isn;
948
Bram Moolenaar080457c2020-03-03 21:53:32 +0100949 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
951 return FAIL;
952 isn->isn_arg.fnumber = fnumber;
953
954 return OK;
955}
956#endif
957
958/*
959 * Generate an ISN_PUSHS instruction.
960 * Consumes "str".
961 */
962 static int
963generate_PUSHS(cctx_T *cctx, char_u *str)
964{
965 isn_T *isn;
966
Bram Moolenaar080457c2020-03-03 21:53:32 +0100967 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
969 return FAIL;
970 isn->isn_arg.string = str;
971
972 return OK;
973}
974
975/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100976 * Generate an ISN_PUSHCHANNEL instruction.
977 * Consumes "channel".
978 */
979 static int
980generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
981{
982 isn_T *isn;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100985 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
986 return FAIL;
987 isn->isn_arg.channel = channel;
988
989 return OK;
990}
991
992/*
993 * Generate an ISN_PUSHJOB instruction.
994 * Consumes "job".
995 */
996 static int
997generate_PUSHJOB(cctx_T *cctx, job_T *job)
998{
999 isn_T *isn;
1000
Bram Moolenaar080457c2020-03-03 21:53:32 +01001001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001002 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001003 return FAIL;
1004 isn->isn_arg.job = job;
1005
1006 return OK;
1007}
1008
1009/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010 * Generate an ISN_PUSHBLOB instruction.
1011 * Consumes "blob".
1012 */
1013 static int
1014generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1015{
1016 isn_T *isn;
1017
Bram Moolenaar080457c2020-03-03 21:53:32 +01001018 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1020 return FAIL;
1021 isn->isn_arg.blob = blob;
1022
1023 return OK;
1024}
1025
1026/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001027 * Generate an ISN_PUSHFUNC instruction with name "name".
1028 * Consumes "name".
1029 */
1030 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001031generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001032{
1033 isn_T *isn;
1034
Bram Moolenaar080457c2020-03-03 21:53:32 +01001035 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001036 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001037 return FAIL;
1038 isn->isn_arg.string = name;
1039
1040 return OK;
1041}
1042
1043/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 * Generate an ISN_STORE instruction.
1045 */
1046 static int
1047generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1048{
1049 isn_T *isn;
1050
Bram Moolenaar080457c2020-03-03 21:53:32 +01001051 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1053 return FAIL;
1054 if (name != NULL)
1055 isn->isn_arg.string = vim_strsave(name);
1056 else
1057 isn->isn_arg.number = idx;
1058
1059 return OK;
1060}
1061
1062/*
1063 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1064 */
1065 static int
1066generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1067{
1068 isn_T *isn;
1069
Bram Moolenaar080457c2020-03-03 21:53:32 +01001070 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1072 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001073 isn->isn_arg.storenr.stnr_idx = idx;
1074 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075
1076 return OK;
1077}
1078
1079/*
1080 * Generate an ISN_STOREOPT instruction
1081 */
1082 static int
1083generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1084{
1085 isn_T *isn;
1086
Bram Moolenaar080457c2020-03-03 21:53:32 +01001087 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1089 return FAIL;
1090 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1091 isn->isn_arg.storeopt.so_flags = opt_flags;
1092
1093 return OK;
1094}
1095
1096/*
1097 * Generate an ISN_LOAD or similar instruction.
1098 */
1099 static int
1100generate_LOAD(
1101 cctx_T *cctx,
1102 isntype_T isn_type,
1103 int idx,
1104 char_u *name,
1105 type_T *type)
1106{
1107 isn_T *isn;
1108
Bram Moolenaar080457c2020-03-03 21:53:32 +01001109 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1111 return FAIL;
1112 if (name != NULL)
1113 isn->isn_arg.string = vim_strsave(name);
1114 else
1115 isn->isn_arg.number = idx;
1116
1117 return OK;
1118}
1119
1120/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001121 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001122 */
1123 static int
1124generate_LOADV(
1125 cctx_T *cctx,
1126 char_u *name,
1127 int error)
1128{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001129 int di_flags;
1130 int vidx = find_vim_var(name, &di_flags);
1131 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001132
Bram Moolenaar080457c2020-03-03 21:53:32 +01001133 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001134 if (vidx < 0)
1135 {
1136 if (error)
1137 semsg(_(e_var_notfound), name);
1138 return FAIL;
1139 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001140 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001141
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001142 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001143}
1144
1145/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001146 * Generate an ISN_UNLET instruction.
1147 */
1148 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001149generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001150{
1151 isn_T *isn;
1152
1153 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001154 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001155 return FAIL;
1156 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1157 isn->isn_arg.unlet.ul_forceit = forceit;
1158
1159 return OK;
1160}
1161
1162/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 * Generate an ISN_LOADS instruction.
1164 */
1165 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001166generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001168 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001170 int sid,
1171 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172{
1173 isn_T *isn;
1174
Bram Moolenaar080457c2020-03-03 21:53:32 +01001175 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001176 if (isn_type == ISN_LOADS)
1177 isn = generate_instr_type(cctx, isn_type, type);
1178 else
1179 isn = generate_instr_drop(cctx, isn_type, 1);
1180 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001182 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1183 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184
1185 return OK;
1186}
1187
1188/*
1189 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1190 */
1191 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001192generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 cctx_T *cctx,
1194 isntype_T isn_type,
1195 int sid,
1196 int idx,
1197 type_T *type)
1198{
1199 isn_T *isn;
1200
Bram Moolenaar080457c2020-03-03 21:53:32 +01001201 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 if (isn_type == ISN_LOADSCRIPT)
1203 isn = generate_instr_type(cctx, isn_type, type);
1204 else
1205 isn = generate_instr_drop(cctx, isn_type, 1);
1206 if (isn == NULL)
1207 return FAIL;
1208 isn->isn_arg.script.script_sid = sid;
1209 isn->isn_arg.script.script_idx = idx;
1210 return OK;
1211}
1212
1213/*
1214 * Generate an ISN_NEWLIST instruction.
1215 */
1216 static int
1217generate_NEWLIST(cctx_T *cctx, int count)
1218{
1219 isn_T *isn;
1220 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 type_T *type;
1222 type_T *member;
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_NEWLIST)) == NULL)
1226 return FAIL;
1227 isn->isn_arg.number = count;
1228
1229 // drop the value types
1230 stack->ga_len -= count;
1231
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001232 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001233 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 if (count > 0)
1235 member = ((type_T **)stack->ga_data)[stack->ga_len];
1236 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001237 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001238 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239
1240 // add the list type to the type stack
1241 if (ga_grow(stack, 1) == FAIL)
1242 return FAIL;
1243 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1244 ++stack->ga_len;
1245
1246 return OK;
1247}
1248
1249/*
1250 * Generate an ISN_NEWDICT instruction.
1251 */
1252 static int
1253generate_NEWDICT(cctx_T *cctx, int count)
1254{
1255 isn_T *isn;
1256 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 type_T *type;
1258 type_T *member;
1259
Bram Moolenaar080457c2020-03-03 21:53:32 +01001260 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1262 return FAIL;
1263 isn->isn_arg.number = count;
1264
1265 // drop the key and value types
1266 stack->ga_len -= 2 * count;
1267
Bram Moolenaar436472f2020-02-20 22:54:43 +01001268 // Use the first value type for the list member type. Use "void" for an
1269 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 if (count > 0)
1271 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1272 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001273 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001274 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275
1276 // add the dict type to the type stack
1277 if (ga_grow(stack, 1) == FAIL)
1278 return FAIL;
1279 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1280 ++stack->ga_len;
1281
1282 return OK;
1283}
1284
1285/*
1286 * Generate an ISN_FUNCREF instruction.
1287 */
1288 static int
1289generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1290{
1291 isn_T *isn;
1292 garray_T *stack = &cctx->ctx_type_stack;
1293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1296 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001297 isn->isn_arg.funcref.fr_func = dfunc_idx;
1298 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299
1300 if (ga_grow(stack, 1) == FAIL)
1301 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001302 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 // TODO: argument and return types
1304 ++stack->ga_len;
1305
1306 return OK;
1307}
1308
1309/*
1310 * Generate an ISN_JUMP instruction.
1311 */
1312 static int
1313generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1314{
1315 isn_T *isn;
1316 garray_T *stack = &cctx->ctx_type_stack;
1317
Bram Moolenaar080457c2020-03-03 21:53:32 +01001318 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1320 return FAIL;
1321 isn->isn_arg.jump.jump_when = when;
1322 isn->isn_arg.jump.jump_where = where;
1323
1324 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1325 --stack->ga_len;
1326
1327 return OK;
1328}
1329
1330 static int
1331generate_FOR(cctx_T *cctx, int loop_idx)
1332{
1333 isn_T *isn;
1334 garray_T *stack = &cctx->ctx_type_stack;
1335
Bram Moolenaar080457c2020-03-03 21:53:32 +01001336 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1338 return FAIL;
1339 isn->isn_arg.forloop.for_idx = loop_idx;
1340
1341 if (ga_grow(stack, 1) == FAIL)
1342 return FAIL;
1343 // type doesn't matter, will be stored next
1344 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1345 ++stack->ga_len;
1346
1347 return OK;
1348}
1349
1350/*
1351 * Generate an ISN_BCALL instruction.
1352 * Return FAIL if the number of arguments is wrong.
1353 */
1354 static int
1355generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1356{
1357 isn_T *isn;
1358 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001359 type_T *argtypes[MAX_FUNC_ARGS];
1360 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361
Bram Moolenaar080457c2020-03-03 21:53:32 +01001362 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 if (check_internal_func(func_idx, argcount) == FAIL)
1364 return FAIL;
1365
1366 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1367 return FAIL;
1368 isn->isn_arg.bfunc.cbf_idx = func_idx;
1369 isn->isn_arg.bfunc.cbf_argcount = argcount;
1370
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001371 for (i = 0; i < argcount; ++i)
1372 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 stack->ga_len -= argcount; // drop the arguments
1375 if (ga_grow(stack, 1) == FAIL)
1376 return FAIL;
1377 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001378 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 ++stack->ga_len; // add return value
1380
1381 return OK;
1382}
1383
1384/*
1385 * Generate an ISN_DCALL or ISN_UCALL instruction.
1386 * Return FAIL if the number of arguments is wrong.
1387 */
1388 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001389generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390{
1391 isn_T *isn;
1392 garray_T *stack = &cctx->ctx_type_stack;
1393 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001394 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395
Bram Moolenaar080457c2020-03-03 21:53:32 +01001396 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 if (argcount > regular_args && !has_varargs(ufunc))
1398 {
1399 semsg(_(e_toomanyarg), ufunc->uf_name);
1400 return FAIL;
1401 }
1402 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1403 {
1404 semsg(_(e_toofewarg), ufunc->uf_name);
1405 return FAIL;
1406 }
1407
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001408 if (ufunc->uf_dfunc_idx >= 0)
1409 {
1410 int i;
1411
1412 for (i = 0; i < argcount; ++i)
1413 {
1414 type_T *expected;
1415 type_T *actual;
1416
1417 if (i < regular_args)
1418 {
1419 if (ufunc->uf_arg_types == NULL)
1420 continue;
1421 expected = ufunc->uf_arg_types[i];
1422 }
1423 else
1424 expected = ufunc->uf_va_type->tt_member;
1425 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001426 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001427 {
1428 arg_type_mismatch(expected, actual, i + 1);
1429 return FAIL;
1430 }
1431 }
1432 }
1433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 if ((isn = generate_instr(cctx,
1435 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1436 return FAIL;
1437 if (ufunc->uf_dfunc_idx >= 0)
1438 {
1439 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1440 isn->isn_arg.dfunc.cdf_argcount = argcount;
1441 }
1442 else
1443 {
1444 // A user function may be deleted and redefined later, can't use the
1445 // ufunc pointer, need to look it up again at runtime.
1446 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1447 isn->isn_arg.ufunc.cuf_argcount = argcount;
1448 }
1449
1450 stack->ga_len -= argcount; // drop the arguments
1451 if (ga_grow(stack, 1) == FAIL)
1452 return FAIL;
1453 // add return value
1454 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1455 ++stack->ga_len;
1456
1457 return OK;
1458}
1459
1460/*
1461 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1462 */
1463 static int
1464generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1465{
1466 isn_T *isn;
1467 garray_T *stack = &cctx->ctx_type_stack;
1468
Bram Moolenaar080457c2020-03-03 21:53:32 +01001469 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1471 return FAIL;
1472 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1473 isn->isn_arg.ufunc.cuf_argcount = argcount;
1474
1475 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001476 if (ga_grow(stack, 1) == FAIL)
1477 return FAIL;
1478 // add return value
1479 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1480 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481
1482 return OK;
1483}
1484
1485/*
1486 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001487 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 */
1489 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001490generate_PCALL(
1491 cctx_T *cctx,
1492 int argcount,
1493 char_u *name,
1494 type_T *type,
1495 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496{
1497 isn_T *isn;
1498 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001499 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500
Bram Moolenaar080457c2020-03-03 21:53:32 +01001501 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001502
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001503 if (type->tt_type == VAR_ANY)
1504 ret_type = &t_any;
1505 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001506 {
1507 if (type->tt_argcount != -1)
1508 {
1509 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1510
1511 if (argcount < type->tt_min_argcount - varargs)
1512 {
1513 semsg(_(e_toofewarg), "[reference]");
1514 return FAIL;
1515 }
1516 if (!varargs && argcount > type->tt_argcount)
1517 {
1518 semsg(_(e_toomanyarg), "[reference]");
1519 return FAIL;
1520 }
1521 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001522 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001523 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001524 else
1525 {
1526 semsg(_("E1085: Not a callable type: %s"), name);
1527 return FAIL;
1528 }
1529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1531 return FAIL;
1532 isn->isn_arg.pfunc.cpf_top = at_top;
1533 isn->isn_arg.pfunc.cpf_argcount = argcount;
1534
1535 stack->ga_len -= argcount; // drop the arguments
1536
1537 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001538 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001540 // If partial is above the arguments it must be cleared and replaced with
1541 // the return value.
1542 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1543 return FAIL;
1544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 return OK;
1546}
1547
1548/*
1549 * Generate an ISN_MEMBER instruction.
1550 */
1551 static int
1552generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1553{
1554 isn_T *isn;
1555 garray_T *stack = &cctx->ctx_type_stack;
1556 type_T *type;
1557
Bram Moolenaar080457c2020-03-03 21:53:32 +01001558 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1560 return FAIL;
1561 isn->isn_arg.string = vim_strnsave(name, (int)len);
1562
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001563 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001565 if (type->tt_type != VAR_DICT && type != &t_any)
1566 {
1567 emsg(_(e_dictreq));
1568 return FAIL;
1569 }
1570 // change dict type to dict member type
1571 if (type->tt_type == VAR_DICT)
1572 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573
1574 return OK;
1575}
1576
1577/*
1578 * Generate an ISN_ECHO instruction.
1579 */
1580 static int
1581generate_ECHO(cctx_T *cctx, int with_white, int count)
1582{
1583 isn_T *isn;
1584
Bram Moolenaar080457c2020-03-03 21:53:32 +01001585 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1587 return FAIL;
1588 isn->isn_arg.echo.echo_with_white = with_white;
1589 isn->isn_arg.echo.echo_count = count;
1590
1591 return OK;
1592}
1593
Bram Moolenaarad39c092020-02-26 18:23:43 +01001594/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001595 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001596 */
1597 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001598generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001599{
1600 isn_T *isn;
1601
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001602 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001603 return FAIL;
1604 isn->isn_arg.number = count;
1605
1606 return OK;
1607}
1608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 static int
1610generate_EXEC(cctx_T *cctx, char_u *line)
1611{
1612 isn_T *isn;
1613
Bram Moolenaar080457c2020-03-03 21:53:32 +01001614 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1616 return FAIL;
1617 isn->isn_arg.string = vim_strsave(line);
1618 return OK;
1619}
1620
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001621 static int
1622generate_EXECCONCAT(cctx_T *cctx, int count)
1623{
1624 isn_T *isn;
1625
1626 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1627 return FAIL;
1628 isn->isn_arg.number = count;
1629 return OK;
1630}
1631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632/*
1633 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001634 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001636 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1638{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 lvar_T *lvar;
1640
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001641 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 {
1643 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001644 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 }
1646
1647 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001648 return NULL;
1649 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001651 // Every local variable uses the next entry on the stack. We could re-use
1652 // the last ones when leaving a scope, but then variables used in a closure
1653 // might get overwritten. To keep things simple do not re-use stack
1654 // entries. This is less efficient, but memory is cheap these days.
1655 lvar->lv_idx = cctx->ctx_locals_count++;
1656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1658 lvar->lv_const = isConst;
1659 lvar->lv_type = type;
1660
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001661 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662}
1663
1664/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001665 * Remove local variables above "new_top".
1666 */
1667 static void
1668unwind_locals(cctx_T *cctx, int new_top)
1669{
1670 if (cctx->ctx_locals.ga_len > new_top)
1671 {
1672 int idx;
1673 lvar_T *lvar;
1674
1675 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1676 {
1677 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1678 vim_free(lvar->lv_name);
1679 }
1680 }
1681 cctx->ctx_locals.ga_len = new_top;
1682}
1683
1684/*
1685 * Free all local variables.
1686 */
1687 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001688free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001689{
1690 unwind_locals(cctx, 0);
1691 ga_clear(&cctx->ctx_locals);
1692}
1693
1694/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 * Skip over a type definition and return a pointer to just after it.
1696 */
1697 char_u *
1698skip_type(char_u *start)
1699{
1700 char_u *p = start;
1701
1702 while (ASCII_ISALNUM(*p) || *p == '_')
1703 ++p;
1704
1705 // Skip over "<type>"; this is permissive about white space.
1706 if (*skipwhite(p) == '<')
1707 {
1708 p = skipwhite(p);
1709 p = skip_type(skipwhite(p + 1));
1710 p = skipwhite(p);
1711 if (*p == '>')
1712 ++p;
1713 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001714 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1715 {
1716 // handle func(args): type
1717 ++p;
1718 while (*p != ')' && *p != NUL)
1719 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001720 char_u *sp = p;
1721
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001722 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001723 if (p == sp)
1724 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001725 if (*p == ',')
1726 p = skipwhite(p + 1);
1727 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001728 if (*p == ')')
1729 {
1730 if (p[1] == ':')
1731 p = skip_type(skipwhite(p + 2));
1732 else
1733 p = skipwhite(p + 1);
1734 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001735 }
1736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 return p;
1738}
1739
1740/*
1741 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001742 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 * Returns NULL in case of failure.
1744 */
1745 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001746parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747{
1748 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001749 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750
1751 if (**arg != '<')
1752 {
1753 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001754 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 else
1756 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001757 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 }
1759 *arg = skipwhite(*arg + 1);
1760
Bram Moolenaard77a8522020-04-03 21:59:57 +02001761 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762
1763 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001764 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 {
1766 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001767 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 }
1769 ++*arg;
1770
1771 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001772 return get_list_type(member_type, type_gap);
1773 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774}
1775
1776/*
1777 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001778 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 */
1780 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001781parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782{
1783 char_u *p = *arg;
1784 size_t len;
1785
1786 // skip over the first word
1787 while (ASCII_ISALNUM(*p) || *p == '_')
1788 ++p;
1789 len = p - *arg;
1790
1791 switch (**arg)
1792 {
1793 case 'a':
1794 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1795 {
1796 *arg += len;
1797 return &t_any;
1798 }
1799 break;
1800 case 'b':
1801 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1802 {
1803 *arg += len;
1804 return &t_bool;
1805 }
1806 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1807 {
1808 *arg += len;
1809 return &t_blob;
1810 }
1811 break;
1812 case 'c':
1813 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1814 {
1815 *arg += len;
1816 return &t_channel;
1817 }
1818 break;
1819 case 'd':
1820 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1821 {
1822 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001823 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 }
1825 break;
1826 case 'f':
1827 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1828 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001829#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 *arg += len;
1831 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001832#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001833 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001834 return &t_any;
1835#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 }
1837 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1838 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001839 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001840 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001841 int argcount = -1;
1842 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001843 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001844 type_T *arg_type[MAX_FUNC_ARGS + 1];
1845
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001846 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001848 if (**arg == '(')
1849 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001850 // "func" may or may not return a value, "func()" does
1851 // not return a value.
1852 ret_type = &t_void;
1853
Bram Moolenaard77a8522020-04-03 21:59:57 +02001854 p = ++*arg;
1855 argcount = 0;
1856 while (*p != NUL && *p != ')')
1857 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001858 if (*p == '?')
1859 {
1860 if (first_optional == -1)
1861 first_optional = argcount;
1862 ++p;
1863 }
1864 else if (first_optional != -1)
1865 {
1866 emsg(_("E1007: mandatory argument after optional argument"));
1867 return &t_any;
1868 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001869 else if (STRNCMP(p, "...", 3) == 0)
1870 {
1871 flags |= TTFLAG_VARARGS;
1872 p += 3;
1873 }
1874
1875 arg_type[argcount++] = parse_type(&p, type_gap);
1876
1877 // Nothing comes after "...{type}".
1878 if (flags & TTFLAG_VARARGS)
1879 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001880
Bram Moolenaard77a8522020-04-03 21:59:57 +02001881 if (*p != ',' && *skipwhite(p) == ',')
1882 {
1883 semsg(_(e_no_white_before), ",");
1884 return &t_any;
1885 }
1886 if (*p == ',')
1887 {
1888 ++p;
1889 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001890 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001891 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001892 return &t_any;
1893 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001894 }
1895 p = skipwhite(p);
1896 if (argcount == MAX_FUNC_ARGS)
1897 {
1898 emsg(_("E740: Too many argument types"));
1899 return &t_any;
1900 }
1901 }
1902
1903 p = skipwhite(p);
1904 if (*p != ')')
1905 {
1906 emsg(_(e_missing_close));
1907 return &t_any;
1908 }
1909 *arg = p + 1;
1910 }
1911 if (**arg == ':')
1912 {
1913 // parse return type
1914 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001915 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001916 semsg(_(e_white_after), ":");
1917 *arg = skipwhite(*arg);
1918 ret_type = parse_type(arg, type_gap);
1919 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001920 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001921 type = get_func_type(ret_type, argcount, type_gap);
1922 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001923 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001924 type = alloc_func_type(ret_type, argcount, type_gap);
1925 type->tt_flags = flags;
1926 if (argcount > 0)
1927 {
1928 type->tt_argcount = argcount;
1929 type->tt_min_argcount = first_optional == -1
1930 ? argcount : first_optional;
1931 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001932 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001933 return &t_any;
1934 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001935 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001936 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001937 }
1938 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 }
1940 break;
1941 case 'j':
1942 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1943 {
1944 *arg += len;
1945 return &t_job;
1946 }
1947 break;
1948 case 'l':
1949 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1950 {
1951 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001952 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 }
1954 break;
1955 case 'n':
1956 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1957 {
1958 *arg += len;
1959 return &t_number;
1960 }
1961 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 case 's':
1963 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1964 {
1965 *arg += len;
1966 return &t_string;
1967 }
1968 break;
1969 case 'v':
1970 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1971 {
1972 *arg += len;
1973 return &t_void;
1974 }
1975 break;
1976 }
1977
1978 semsg(_("E1010: Type not recognized: %s"), *arg);
1979 return &t_any;
1980}
1981
1982/*
1983 * Check if "type1" and "type2" are exactly the same.
1984 */
1985 static int
1986equal_type(type_T *type1, type_T *type2)
1987{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001988 int i;
1989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 if (type1->tt_type != type2->tt_type)
1991 return FALSE;
1992 switch (type1->tt_type)
1993 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001995 case VAR_ANY:
1996 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 case VAR_SPECIAL:
1998 case VAR_BOOL:
1999 case VAR_NUMBER:
2000 case VAR_FLOAT:
2001 case VAR_STRING:
2002 case VAR_BLOB:
2003 case VAR_JOB:
2004 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002005 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 case VAR_LIST:
2007 case VAR_DICT:
2008 return equal_type(type1->tt_member, type2->tt_member);
2009 case VAR_FUNC:
2010 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002011 if (!equal_type(type1->tt_member, type2->tt_member)
2012 || type1->tt_argcount != type2->tt_argcount)
2013 return FALSE;
2014 if (type1->tt_argcount < 0
2015 || type1->tt_args == NULL || type2->tt_args == NULL)
2016 return TRUE;
2017 for (i = 0; i < type1->tt_argcount; ++i)
2018 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2019 return FALSE;
2020 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 }
2022 return TRUE;
2023}
2024
2025/*
2026 * Find the common type of "type1" and "type2" and put it in "dest".
2027 * "type2" and "dest" may be the same.
2028 */
2029 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002030common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031{
2032 if (equal_type(type1, type2))
2033 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002034 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 return;
2036 }
2037
2038 if (type1->tt_type == type2->tt_type)
2039 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2041 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002042 type_T *common;
2043
Bram Moolenaard77a8522020-04-03 21:59:57 +02002044 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002045 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002046 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002047 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002048 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049 return;
2050 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002051 if (type1->tt_type == VAR_FUNC)
2052 {
2053 type_T *common;
2054
2055 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2056 if (type1->tt_argcount == type2->tt_argcount
2057 && type1->tt_argcount >= 0)
2058 {
2059 int argcount = type1->tt_argcount;
2060 int i;
2061
2062 *dest = alloc_func_type(common, argcount, type_gap);
2063 if (type1->tt_args != NULL && type2->tt_args != NULL)
2064 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002065 if (func_type_add_arg_types(*dest, argcount,
2066 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002067 for (i = 0; i < argcount; ++i)
2068 common_type(type1->tt_args[i], type2->tt_args[i],
2069 &(*dest)->tt_args[i], type_gap);
2070 }
2071 }
2072 else
2073 *dest = alloc_func_type(common, -1, type_gap);
2074 return;
2075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 }
2077
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002078 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079}
2080
2081 char *
2082vartype_name(vartype_T type)
2083{
2084 switch (type)
2085 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002086 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002087 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 case VAR_SPECIAL: return "special";
2090 case VAR_BOOL: return "bool";
2091 case VAR_NUMBER: return "number";
2092 case VAR_FLOAT: return "float";
2093 case VAR_STRING: return "string";
2094 case VAR_BLOB: return "blob";
2095 case VAR_JOB: return "job";
2096 case VAR_CHANNEL: return "channel";
2097 case VAR_LIST: return "list";
2098 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002099
2100 case VAR_FUNC:
2101 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002103 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104}
2105
2106/*
2107 * Return the name of a type.
2108 * The result may be in allocated memory, in which case "tofree" is set.
2109 */
2110 char *
2111type_name(type_T *type, char **tofree)
2112{
2113 char *name = vartype_name(type->tt_type);
2114
2115 *tofree = NULL;
2116 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2117 {
2118 char *member_free;
2119 char *member_name = type_name(type->tt_member, &member_free);
2120 size_t len;
2121
2122 len = STRLEN(name) + STRLEN(member_name) + 3;
2123 *tofree = alloc(len);
2124 if (*tofree != NULL)
2125 {
2126 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2127 vim_free(member_free);
2128 return *tofree;
2129 }
2130 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002131 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002132 {
2133 garray_T ga;
2134 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002135 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002136
2137 ga_init2(&ga, 1, 100);
2138 if (ga_grow(&ga, 20) == FAIL)
2139 return "[unknown]";
2140 *tofree = ga.ga_data;
2141 STRCPY(ga.ga_data, "func(");
2142 ga.ga_len += 5;
2143
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002144 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002145 {
2146 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002147 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002148 int len;
2149
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002150 if (type->tt_args == NULL)
2151 arg_type = "[unknown]";
2152 else
2153 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002154 if (i > 0)
2155 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002156 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002157 ga.ga_len += 2;
2158 }
2159 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002160 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002161 {
2162 vim_free(arg_free);
2163 return "[unknown]";
2164 }
2165 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002166 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002167 {
2168 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2169 ga.ga_len += 3;
2170 }
2171 else if (i >= type->tt_min_argcount)
2172 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002173 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002174 ga.ga_len += len;
2175 vim_free(arg_free);
2176 }
2177
2178 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002179 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002180 else
2181 {
2182 char *ret_free;
2183 char *ret_name = type_name(type->tt_member, &ret_free);
2184 int len;
2185
2186 len = (int)STRLEN(ret_name) + 4;
2187 if (ga_grow(&ga, len) == FAIL)
2188 {
2189 vim_free(ret_free);
2190 return "[unknown]";
2191 }
2192 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002193 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2194 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002195 vim_free(ret_free);
2196 }
2197 return ga.ga_data;
2198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199
2200 return name;
2201}
2202
2203/*
2204 * Find "name" in script-local items of script "sid".
2205 * Returns the index in "sn_var_vals" if found.
2206 * If found but not in "sn_var_vals" returns -1.
2207 * If not found returns -2.
2208 */
2209 int
2210get_script_item_idx(int sid, char_u *name, int check_writable)
2211{
2212 hashtab_T *ht;
2213 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002214 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 int idx;
2216
2217 // First look the name up in the hashtable.
2218 if (sid <= 0 || sid > script_items.ga_len)
2219 return -1;
2220 ht = &SCRIPT_VARS(sid);
2221 di = find_var_in_ht(ht, 0, name, TRUE);
2222 if (di == NULL)
2223 return -2;
2224
2225 // Now find the svar_T index in sn_var_vals.
2226 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2227 {
2228 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2229
2230 if (sv->sv_tv == &di->di_tv)
2231 {
2232 if (check_writable && sv->sv_const)
2233 semsg(_(e_readonlyvar), name);
2234 return idx;
2235 }
2236 }
2237 return -1;
2238}
2239
2240/*
2241 * Find "name" in imported items of the current script/
2242 */
2243 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002244find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002246 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 int idx;
2248
2249 if (cctx != NULL)
2250 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2251 {
2252 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2253 + idx;
2254
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002255 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2256 : STRLEN(import->imp_name) == len
2257 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 return import;
2259 }
2260
2261 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2262 {
2263 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2264
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002265 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2266 : STRLEN(import->imp_name) == len
2267 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 return import;
2269 }
2270 return NULL;
2271}
2272
2273/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002274 * Free all imported variables.
2275 */
2276 static void
2277free_imported(cctx_T *cctx)
2278{
2279 int idx;
2280
2281 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2282 {
2283 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2284
2285 vim_free(import->imp_name);
2286 }
2287 ga_clear(&cctx->ctx_imports);
2288}
2289
2290/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002291 * Get the next line of the function from "cctx".
2292 * Returns NULL when at the end.
2293 */
2294 static char_u *
2295next_line_from_context(cctx_T *cctx)
2296{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002297 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002298
2299 do
2300 {
2301 ++cctx->ctx_lnum;
2302 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002303 {
2304 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002305 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002306 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002307 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002308 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002309 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2310 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002311 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002312 return line;
2313}
2314
2315/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002316 * Return TRUE if "p" points at a "#" but not at "#{".
2317 */
2318 static int
2319comment_start(char_u *p)
2320{
2321 return p[0] == '#' && p[1] != '{';
2322}
2323
2324/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002325 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002326 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002327 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2328 */
2329 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002330may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002331{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002332 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002333 {
2334 char_u *next = next_line_from_context(cctx);
2335
2336 if (next == NULL)
2337 return FAIL;
2338 *arg = skipwhite(next);
2339 }
2340 return OK;
2341}
2342
2343/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002344 * Generate an instruction to load script-local variable "name", without the
2345 * leading "s:".
2346 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 */
2348 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002349compile_load_scriptvar(
2350 cctx_T *cctx,
2351 char_u *name, // variable NUL terminated
2352 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002353 char_u **end, // end of variable
2354 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002356 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2358 imported_T *import;
2359
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002360 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002362 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002363 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2364 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 }
2366 if (idx >= 0)
2367 {
2368 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2369
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002370 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 current_sctx.sc_sid, idx, sv->sv_type);
2372 return OK;
2373 }
2374
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002375 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 if (import != NULL)
2377 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002378 if (import->imp_all)
2379 {
2380 char_u *p = skipwhite(*end);
2381 int name_len;
2382 ufunc_T *ufunc;
2383 type_T *type;
2384
2385 // Used "import * as Name", need to lookup the member.
2386 if (*p != '.')
2387 {
2388 semsg(_("E1060: expected dot after name: %s"), start);
2389 return FAIL;
2390 }
2391 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002392 if (VIM_ISWHITE(*p))
2393 {
2394 emsg(_("E1074: no white space allowed after dot"));
2395 return FAIL;
2396 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002397
2398 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2399 // TODO: what if it is a function?
2400 if (idx < 0)
2401 return FAIL;
2402 *end = p;
2403
2404 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2405 import->imp_sid,
2406 idx,
2407 type);
2408 }
2409 else
2410 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002411 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002412 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2413 import->imp_sid,
2414 import->imp_var_vals_idx,
2415 import->imp_type);
2416 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 return OK;
2418 }
2419
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002420 if (error)
2421 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 return FAIL;
2423}
2424
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002425 static int
2426generate_funcref(cctx_T *cctx, char_u *name)
2427{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002428 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002429
2430 if (ufunc == NULL)
2431 return FAIL;
2432
2433 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2434}
2435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436/*
2437 * Compile a variable name into a load instruction.
2438 * "end" points to just after the name.
2439 * When "error" is FALSE do not give an error when not found.
2440 */
2441 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002442compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443{
2444 type_T *type;
2445 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002446 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002448 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449
2450 if (*(*arg + 1) == ':')
2451 {
2452 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002453 if (end <= *arg + 2)
2454 name = vim_strsave((char_u *)"[empty]");
2455 else
2456 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 if (name == NULL)
2458 return FAIL;
2459
2460 if (**arg == 'v')
2461 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002462 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 }
2464 else if (**arg == 'g')
2465 {
2466 // Global variables can be defined later, thus we don't check if it
2467 // exists, give error at runtime.
2468 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2469 }
2470 else if (**arg == 's')
2471 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002472 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002474 else if (**arg == 'b')
2475 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002476 // Buffer-local variables can be defined later, thus we don't check
2477 // if it exists, give error at runtime.
2478 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002479 }
2480 else if (**arg == 'w')
2481 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002482 // Window-local variables can be defined later, thus we don't check
2483 // if it exists, give error at runtime.
2484 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002485 }
2486 else if (**arg == 't')
2487 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002488 // Tabpage-local variables can be defined later, thus we don't
2489 // check if it exists, give error at runtime.
2490 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002491 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 else
2493 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002494 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 goto theend;
2496 }
2497 }
2498 else
2499 {
2500 size_t len = end - *arg;
2501 int idx;
2502 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002503 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504
2505 name = vim_strnsave(*arg, end - *arg);
2506 if (name == NULL)
2507 return FAIL;
2508
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002509 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002511 if (!gen_load_outer)
2512 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 }
2514 else
2515 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002516 lvar_T *lvar = lookup_local(*arg, len, cctx);
2517
2518 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002520 type = lvar->lv_type;
2521 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002522 if (lvar->lv_from_outer)
2523 gen_load_outer = TRUE;
2524 else
2525 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 }
2527 else
2528 {
2529 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2530 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2531 res = generate_PUSHBOOL(cctx, **arg == 't'
2532 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002533 else
2534 {
2535 // "var" can be script-local even without using "s:" if it
2536 // already exists.
2537 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2538 == SCRIPT_VERSION_VIM9
2539 || lookup_script(*arg, len) == OK)
2540 res = compile_load_scriptvar(cctx, name, *arg, &end,
2541 FALSE);
2542
2543 // When the name starts with an uppercase letter or "x:" it
2544 // can be a user defined function.
2545 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2546 res = generate_funcref(cctx, name);
2547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 }
2549 }
2550 if (gen_load)
2551 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002552 if (gen_load_outer)
2553 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 }
2555
2556 *arg = end;
2557
2558theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002559 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 semsg(_(e_var_notfound), name);
2561 vim_free(name);
2562 return res;
2563}
2564
2565/*
2566 * Compile the argument expressions.
2567 * "arg" points to just after the "(" and is advanced to after the ")"
2568 */
2569 static int
2570compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2571{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002572 char_u *p = *arg;
2573 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574
Bram Moolenaare6085c52020-04-12 20:19:16 +02002575 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002577 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002578 {
2579 p = next_line_from_context(cctx);
2580 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002581 goto failret;
2582 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002583 p = skipwhite(p);
2584 }
2585 if (*p == ')')
2586 {
2587 *arg = p + 1;
2588 return OK;
2589 }
2590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 if (compile_expr1(&p, cctx) == FAIL)
2592 return FAIL;
2593 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002594
2595 if (*p != ',' && *skipwhite(p) == ',')
2596 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002597 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002598 p = skipwhite(p);
2599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002601 {
2602 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002603 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002604 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002605 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002606 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002607 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002609failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002610 emsg(_(e_missing_close));
2611 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612}
2613
2614/*
2615 * Compile a function call: name(arg1, arg2)
2616 * "arg" points to "name", "arg + varlen" to the "(".
2617 * "argcount_init" is 1 for "value->method()"
2618 * Instructions:
2619 * EVAL arg1
2620 * EVAL arg2
2621 * BCALL / DCALL / UCALL
2622 */
2623 static int
2624compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2625{
2626 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002627 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 int argcount = argcount_init;
2629 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002630 char_u fname_buf[FLEN_FIXED + 1];
2631 char_u *tofree = NULL;
2632 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002634 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635
2636 if (varlen >= sizeof(namebuf))
2637 {
2638 semsg(_("E1011: name too long: %s"), name);
2639 return FAIL;
2640 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002641 vim_strncpy(namebuf, *arg, varlen);
2642 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643
2644 *arg = skipwhite(*arg + varlen + 1);
2645 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002646 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002648 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 {
2650 int idx;
2651
2652 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002653 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002655 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002656 else
2657 semsg(_(e_unknownfunc), namebuf);
2658 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 }
2660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002662 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002664 {
2665 res = generate_CALL(cctx, ufunc, argcount);
2666 goto theend;
2667 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668
2669 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002670 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002672 if (STRNCMP(namebuf, "g:", 2) != 0
2673 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002674 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002675 garray_T *stack = &cctx->ctx_type_stack;
2676 type_T *type;
2677
2678 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2679 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002680 goto theend;
2681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002683 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002684 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002685 if (STRNCMP(namebuf, "g:", 2) == 0)
2686 res = generate_UCALL(cctx, name, argcount);
2687 else
2688 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002689
2690theend:
2691 vim_free(tofree);
2692 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693}
2694
2695// like NAMESPACE_CHAR but with 'a' and 'l'.
2696#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2697
2698/*
2699 * Find the end of a variable or function name. Unlike find_name_end() this
2700 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002701 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 * Return a pointer to just after the name. Equal to "arg" if there is no
2703 * valid name.
2704 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002705 static char_u *
2706to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707{
2708 char_u *p;
2709
2710 // Quick check for valid starting character.
2711 if (!eval_isnamec1(*arg))
2712 return arg;
2713
2714 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2715 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2716 // and can be used in slice "[n:]".
2717 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002718 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2720 break;
2721 return p;
2722}
2723
2724/*
2725 * Like to_name_end() but also skip over a list or dict constant.
2726 */
2727 char_u *
2728to_name_const_end(char_u *arg)
2729{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002730 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 typval_T rettv;
2732
2733 if (p == arg && *arg == '[')
2734 {
2735
2736 // Can be "[1, 2, 3]->Func()".
2737 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2738 p = arg;
2739 }
2740 else if (p == arg && *arg == '#' && arg[1] == '{')
2741 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002742 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 ++p;
2744 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2745 p = arg;
2746 }
2747 else if (p == arg && *arg == '{')
2748 {
2749 int ret = get_lambda_tv(&p, &rettv, FALSE);
2750
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002751 // Can be "{x -> ret}()".
2752 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 if (ret == NOTDONE)
2754 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2755 if (ret != OK)
2756 p = arg;
2757 }
2758
2759 return p;
2760}
2761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762/*
2763 * parse a list: [expr, expr]
2764 * "*arg" points to the '['.
2765 */
2766 static int
2767compile_list(char_u **arg, cctx_T *cctx)
2768{
2769 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002770 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 int count = 0;
2772
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002773 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002775 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002776 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002777 p = next_line_from_context(cctx);
2778 if (p == NULL)
2779 {
2780 semsg(_(e_list_end), *arg);
2781 return FAIL;
2782 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002783 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002784 p = skipwhite(p);
2785 }
2786 if (*p == ']')
2787 {
2788 ++p;
2789 // Allow for following comment, after at least one space.
2790 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2791 p += STRLEN(p);
2792 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 if (compile_expr1(&p, cctx) == FAIL)
2795 break;
2796 ++count;
2797 if (*p == ',')
2798 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002799 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 p = skipwhite(p);
2801 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002802 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803
2804 generate_NEWLIST(cctx, count);
2805 return OK;
2806}
2807
2808/*
2809 * parse a lambda: {arg, arg -> expr}
2810 * "*arg" points to the '{'.
2811 */
2812 static int
2813compile_lambda(char_u **arg, cctx_T *cctx)
2814{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 typval_T rettv;
2816 ufunc_T *ufunc;
2817
2818 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002819 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002823 ++ufunc->uf_refcount;
2824 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002825 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826
2827 // The function will have one line: "return {expr}".
2828 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002829 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830
2831 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002832 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 return FAIL;
2834}
2835
2836/*
2837 * Compile a lamda call: expr->{lambda}(args)
2838 * "arg" points to the "{".
2839 */
2840 static int
2841compile_lambda_call(char_u **arg, cctx_T *cctx)
2842{
2843 ufunc_T *ufunc;
2844 typval_T rettv;
2845 int argcount = 1;
2846 int ret = FAIL;
2847
2848 // Get the funcref in "rettv".
2849 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2850 return FAIL;
2851
2852 if (**arg != '(')
2853 {
2854 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002855 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 else
2857 semsg(_(e_missing_paren), "lambda");
2858 clear_tv(&rettv);
2859 return FAIL;
2860 }
2861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 ufunc = rettv.vval.v_partial->pt_func;
2863 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002864 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002865 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002866
2867 // The function will have one line: "return {expr}".
2868 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002869 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870
2871 // compile the arguments
2872 *arg = skipwhite(*arg + 1);
2873 if (compile_arguments(arg, cctx, &argcount) == OK)
2874 // call the compiled function
2875 ret = generate_CALL(cctx, ufunc, argcount);
2876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 return ret;
2878}
2879
2880/*
2881 * parse a dict: {'key': val} or #{key: val}
2882 * "*arg" points to the '{'.
2883 */
2884 static int
2885compile_dict(char_u **arg, cctx_T *cctx, int literal)
2886{
2887 garray_T *instr = &cctx->ctx_instr;
2888 int count = 0;
2889 dict_T *d = dict_alloc();
2890 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002891 char_u *whitep = *arg;
2892 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893
2894 if (d == NULL)
2895 return FAIL;
2896 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002897 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 {
2899 char_u *key = NULL;
2900
Bram Moolenaar2c330432020-04-13 14:41:35 +02002901 while (**arg == NUL || (literal && **arg == '"')
2902 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002903 {
2904 *arg = next_line_from_context(cctx);
2905 if (*arg == NULL)
2906 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002907 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002908 *arg = skipwhite(*arg);
2909 }
2910
2911 if (**arg == '}')
2912 break;
2913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 if (literal)
2915 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002916 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917
Bram Moolenaar2c330432020-04-13 14:41:35 +02002918 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 {
2920 semsg(_("E1014: Invalid key: %s"), *arg);
2921 return FAIL;
2922 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002923 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 if (generate_PUSHS(cctx, key) == FAIL)
2925 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002926 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 }
2928 else
2929 {
2930 isn_T *isn;
2931
2932 if (compile_expr1(arg, cctx) == FAIL)
2933 return FAIL;
2934 // TODO: check type is string
2935 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2936 if (isn->isn_type == ISN_PUSHS)
2937 key = isn->isn_arg.string;
2938 }
2939
2940 // Check for duplicate keys, if using string keys.
2941 if (key != NULL)
2942 {
2943 item = dict_find(d, key, -1);
2944 if (item != NULL)
2945 {
2946 semsg(_(e_duplicate_key), key);
2947 goto failret;
2948 }
2949 item = dictitem_alloc(key);
2950 if (item != NULL)
2951 {
2952 item->di_tv.v_type = VAR_UNKNOWN;
2953 item->di_tv.v_lock = 0;
2954 if (dict_add(d, item) == FAIL)
2955 dictitem_free(item);
2956 }
2957 }
2958
2959 *arg = skipwhite(*arg);
2960 if (**arg != ':')
2961 {
2962 semsg(_(e_missing_dict_colon), *arg);
2963 return FAIL;
2964 }
2965
Bram Moolenaar2c330432020-04-13 14:41:35 +02002966 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002968 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002969 {
2970 *arg = next_line_from_context(cctx);
2971 if (*arg == NULL)
2972 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002973 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002974 *arg = skipwhite(*arg);
2975 }
2976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 if (compile_expr1(arg, cctx) == FAIL)
2978 return FAIL;
2979 ++count;
2980
Bram Moolenaar2c330432020-04-13 14:41:35 +02002981 whitep = *arg;
2982 p = skipwhite(*arg);
2983 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002984 {
2985 *arg = next_line_from_context(cctx);
2986 if (*arg == NULL)
2987 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002988 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002989 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002990 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 if (**arg == '}')
2993 break;
2994 if (**arg != ',')
2995 {
2996 semsg(_(e_missing_dict_comma), *arg);
2997 goto failret;
2998 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002999 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 *arg = skipwhite(*arg + 1);
3001 }
3002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 *arg = *arg + 1;
3004
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003005 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003006 p = skipwhite(*arg);
3007 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003008 *arg += STRLEN(*arg);
3009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 dict_unref(d);
3011 return generate_NEWDICT(cctx, count);
3012
3013failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003014 if (*arg == NULL)
3015 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 dict_unref(d);
3017 return FAIL;
3018}
3019
3020/*
3021 * Compile "&option".
3022 */
3023 static int
3024compile_get_option(char_u **arg, cctx_T *cctx)
3025{
3026 typval_T rettv;
3027 char_u *start = *arg;
3028 int ret;
3029
3030 // parse the option and get the current value to get the type.
3031 rettv.v_type = VAR_UNKNOWN;
3032 ret = get_option_tv(arg, &rettv, TRUE);
3033 if (ret == OK)
3034 {
3035 // include the '&' in the name, get_option_tv() expects it.
3036 char_u *name = vim_strnsave(start, *arg - start);
3037 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3038
3039 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3040 vim_free(name);
3041 }
3042 clear_tv(&rettv);
3043
3044 return ret;
3045}
3046
3047/*
3048 * Compile "$VAR".
3049 */
3050 static int
3051compile_get_env(char_u **arg, cctx_T *cctx)
3052{
3053 char_u *start = *arg;
3054 int len;
3055 int ret;
3056 char_u *name;
3057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 ++*arg;
3059 len = get_env_len(arg);
3060 if (len == 0)
3061 {
3062 semsg(_(e_syntax_at), start - 1);
3063 return FAIL;
3064 }
3065
3066 // include the '$' in the name, get_env_tv() expects it.
3067 name = vim_strnsave(start, len + 1);
3068 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3069 vim_free(name);
3070 return ret;
3071}
3072
3073/*
3074 * Compile "@r".
3075 */
3076 static int
3077compile_get_register(char_u **arg, cctx_T *cctx)
3078{
3079 int ret;
3080
3081 ++*arg;
3082 if (**arg == NUL)
3083 {
3084 semsg(_(e_syntax_at), *arg - 1);
3085 return FAIL;
3086 }
3087 if (!valid_yank_reg(**arg, TRUE))
3088 {
3089 emsg_invreg(**arg);
3090 return FAIL;
3091 }
3092 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3093 ++*arg;
3094 return ret;
3095}
3096
3097/*
3098 * Apply leading '!', '-' and '+' to constant "rettv".
3099 */
3100 static int
3101apply_leader(typval_T *rettv, char_u *start, char_u *end)
3102{
3103 char_u *p = end;
3104
3105 // this works from end to start
3106 while (p > start)
3107 {
3108 --p;
3109 if (*p == '-' || *p == '+')
3110 {
3111 // only '-' has an effect, for '+' we only check the type
3112#ifdef FEAT_FLOAT
3113 if (rettv->v_type == VAR_FLOAT)
3114 {
3115 if (*p == '-')
3116 rettv->vval.v_float = -rettv->vval.v_float;
3117 }
3118 else
3119#endif
3120 {
3121 varnumber_T val;
3122 int error = FALSE;
3123
3124 // tv_get_number_chk() accepts a string, but we don't want that
3125 // here
3126 if (check_not_string(rettv) == FAIL)
3127 return FAIL;
3128 val = tv_get_number_chk(rettv, &error);
3129 clear_tv(rettv);
3130 if (error)
3131 return FAIL;
3132 if (*p == '-')
3133 val = -val;
3134 rettv->v_type = VAR_NUMBER;
3135 rettv->vval.v_number = val;
3136 }
3137 }
3138 else
3139 {
3140 int v = tv2bool(rettv);
3141
3142 // '!' is permissive in the type.
3143 clear_tv(rettv);
3144 rettv->v_type = VAR_BOOL;
3145 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3146 }
3147 }
3148 return OK;
3149}
3150
3151/*
3152 * Recognize v: variables that are constants and set "rettv".
3153 */
3154 static void
3155get_vim_constant(char_u **arg, typval_T *rettv)
3156{
3157 if (STRNCMP(*arg, "v:true", 6) == 0)
3158 {
3159 rettv->v_type = VAR_BOOL;
3160 rettv->vval.v_number = VVAL_TRUE;
3161 *arg += 6;
3162 }
3163 else if (STRNCMP(*arg, "v:false", 7) == 0)
3164 {
3165 rettv->v_type = VAR_BOOL;
3166 rettv->vval.v_number = VVAL_FALSE;
3167 *arg += 7;
3168 }
3169 else if (STRNCMP(*arg, "v:null", 6) == 0)
3170 {
3171 rettv->v_type = VAR_SPECIAL;
3172 rettv->vval.v_number = VVAL_NULL;
3173 *arg += 6;
3174 }
3175 else if (STRNCMP(*arg, "v:none", 6) == 0)
3176 {
3177 rettv->v_type = VAR_SPECIAL;
3178 rettv->vval.v_number = VVAL_NONE;
3179 *arg += 6;
3180 }
3181}
3182
3183/*
Bram Moolenaar61a89812020-05-07 16:58:17 +02003184 * Evaluate an expression that is a constant:
3185 * has(arg)
3186 *
3187 * Also handle:
3188 * ! in front logical NOT
3189 *
3190 * Return FAIL if the expression is not a constant.
3191 */
3192 static int
3193evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3194{
3195 typval_T argvars[2];
3196 char_u *start_leader, *end_leader;
3197 int has_call = FALSE;
3198
3199 /*
3200 * Skip '!' characters. They are handled later.
3201 * TODO: '-' and '+' characters
3202 */
3203 start_leader = *arg;
3204 while (**arg == '!')
3205 *arg = skipwhite(*arg + 1);
3206 end_leader = *arg;
3207
3208 /*
3209 * Recognize only a few types of constants for now.
3210 */
3211 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
3212 {
3213 tv->v_type = VAR_BOOL;
3214 tv->vval.v_number = VVAL_TRUE;
3215 *arg += 4;
3216 return OK;
3217 }
3218 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
3219 {
3220 tv->v_type = VAR_BOOL;
3221 tv->vval.v_number = VVAL_FALSE;
3222 *arg += 5;
3223 return OK;
3224 }
3225
3226 if (STRNCMP("has(", *arg, 4) == 0)
3227 {
3228 has_call = TRUE;
3229 *arg = skipwhite(*arg + 4);
3230 }
3231
3232 if (**arg == '"')
3233 {
3234 if (get_string_tv(arg, tv, TRUE) == FAIL)
3235 return FAIL;
3236 }
3237 else if (**arg == '\'')
3238 {
3239 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3240 return FAIL;
3241 }
3242 else
3243 return FAIL;
3244
3245 if (has_call)
3246 {
3247 *arg = skipwhite(*arg);
3248 if (**arg != ')')
3249 return FAIL;
3250 *arg = *arg + 1;
3251
3252 argvars[0] = *tv;
3253 argvars[1].v_type = VAR_UNKNOWN;
3254 tv->v_type = VAR_NUMBER;
3255 tv->vval.v_number = 0;
3256 f_has(argvars, tv);
3257 clear_tv(&argvars[0]);
3258
3259 while (start_leader < end_leader)
3260 {
3261 if (*start_leader == '!')
3262 tv->vval.v_number = !tv->vval.v_number;
3263 ++start_leader;
3264 }
3265 }
3266 else if (end_leader > start_leader)
3267 {
3268 clear_tv(tv);
3269 return FAIL;
3270 }
3271
3272 return OK;
3273}
3274
3275/*
3276 * * number multiplication
3277 * / number division
3278 * % number modulo
3279 */
3280 static int
3281evaluate_const_expr6(char_u **arg, cctx_T *cctx, typval_T *tv)
3282{
3283 char_u *op;
3284
3285 // get the first variable
3286 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
3287 return FAIL;
3288
3289 /*
3290 * Repeat computing, until no "*", "/" or "%" is following.
3291 */
3292 for (;;)
3293 {
3294 op = skipwhite(*arg);
3295 if (*op != '*' && *op != '/' && *op != '%')
3296 break;
3297 // TODO: not implemented yet.
3298 clear_tv(tv);
3299 return FAIL;
3300 }
3301 return OK;
3302}
3303
3304/*
3305 * + number addition
3306 * - number subtraction
3307 * .. string concatenation
3308 */
3309 static int
3310evaluate_const_expr5(char_u **arg, cctx_T *cctx, typval_T *tv)
3311{
3312 char_u *op;
3313 int oplen;
3314
3315 // get the first variable
3316 if (evaluate_const_expr6(arg, cctx, tv) == FAIL)
3317 return FAIL;
3318
3319 /*
3320 * Repeat computing, until no "+", "-" or ".." is following.
3321 */
3322 for (;;)
3323 {
3324 op = skipwhite(*arg);
3325 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3326 break;
3327 oplen = (*op == '.' ? 2 : 1);
3328
3329 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3330 {
3331 clear_tv(tv);
3332 return FAIL;
3333 }
3334
3335 if (*op == '.' && tv->v_type == VAR_STRING)
3336 {
3337 typval_T tv2;
3338 size_t len1;
3339 char_u *s1, *s2;
3340
3341 tv2.v_type = VAR_UNKNOWN;
3342 *arg = skipwhite(op + oplen);
3343
3344 // TODO: what if we fail???
3345 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
3346 return FAIL;
3347
3348 // get the second variable
3349 if (evaluate_const_expr6(arg, cctx, &tv2) == FAIL)
3350 {
3351 clear_tv(tv);
3352 return FAIL;
3353 }
3354 if (tv2.v_type != VAR_STRING)
3355 {
3356 clear_tv(tv);
3357 clear_tv(&tv2);
3358 return FAIL;
3359 }
3360 s1 = tv->vval.v_string;
3361 len1 = STRLEN(s1);
3362 s2 = tv2.vval.v_string;
3363 tv->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3364 if (tv->vval.v_string == NULL)
3365 {
3366 vim_free(s1);
3367 vim_free(s2);
3368 return FAIL;
3369 }
3370 mch_memmove(tv->vval.v_string, s1, len1);
3371 STRCPY(tv->vval.v_string + len1, s2);
3372 continue;
3373 }
3374
3375 // TODO: Not implemented yet.
3376 clear_tv(tv);
3377 return FAIL;
3378 }
3379 return OK;
3380}
3381
3382 static exptype_T
3383get_compare_type(char_u *p, int *len, int *type_is)
3384{
3385 exptype_T type = EXPR_UNKNOWN;
3386 int i;
3387
3388 switch (p[0])
3389 {
3390 case '=': if (p[1] == '=')
3391 type = EXPR_EQUAL;
3392 else if (p[1] == '~')
3393 type = EXPR_MATCH;
3394 break;
3395 case '!': if (p[1] == '=')
3396 type = EXPR_NEQUAL;
3397 else if (p[1] == '~')
3398 type = EXPR_NOMATCH;
3399 break;
3400 case '>': if (p[1] != '=')
3401 {
3402 type = EXPR_GREATER;
3403 *len = 1;
3404 }
3405 else
3406 type = EXPR_GEQUAL;
3407 break;
3408 case '<': if (p[1] != '=')
3409 {
3410 type = EXPR_SMALLER;
3411 *len = 1;
3412 }
3413 else
3414 type = EXPR_SEQUAL;
3415 break;
3416 case 'i': if (p[1] == 's')
3417 {
3418 // "is" and "isnot"; but not a prefix of a name
3419 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3420 *len = 5;
3421 i = p[*len];
3422 if (!isalnum(i) && i != '_')
3423 {
3424 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3425 *type_is = TRUE;
3426 }
3427 }
3428 break;
3429 }
3430 return type;
3431}
3432
3433/*
3434 * Only comparing strings is supported right now.
3435 * expr5a == expr5b
3436 */
3437 static int
3438evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3439{
3440 exptype_T type = EXPR_UNKNOWN;
3441 char_u *p;
3442 int len = 2;
3443 int type_is = FALSE;
3444
3445 // get the first variable
3446 if (evaluate_const_expr5(arg, cctx, tv) == FAIL)
3447 return FAIL;
3448
3449 p = skipwhite(*arg);
3450 type = get_compare_type(p, &len, &type_is);
3451
3452 /*
3453 * If there is a comparative operator, use it.
3454 */
3455 if (type != EXPR_UNKNOWN)
3456 {
3457 typval_T tv2;
3458 char_u *s1, *s2;
3459 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3460 int n;
3461
3462 // TODO: Only string == string is supported now
3463 if (tv->v_type != VAR_STRING)
3464 return FAIL;
3465 if (type != EXPR_EQUAL)
3466 return FAIL;
3467
3468 // get the second variable
3469 init_tv(&tv2);
3470 *arg = skipwhite(p + len);
3471 if (evaluate_const_expr5(arg, cctx, &tv2) == FAIL
3472 || tv2.v_type != VAR_STRING)
3473 {
3474 clear_tv(&tv2);
3475 return FAIL;
3476 }
3477 s1 = tv_get_string_buf(tv, buf1);
3478 s2 = tv_get_string_buf(&tv2, buf2);
3479 n = STRCMP(s1, s2);
3480 clear_tv(tv);
3481 clear_tv(&tv2);
3482 tv->v_type = VAR_BOOL;
3483 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
3484 }
3485
3486 return OK;
3487}
3488
3489static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3490
3491/*
3492 * Compile constant || or &&.
3493 */
3494 static int
3495evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3496{
3497 char_u *p = skipwhite(*arg);
3498 int opchar = *op;
3499
3500 if (p[0] == opchar && p[1] == opchar)
3501 {
3502 int val = tv2bool(tv);
3503
3504 /*
3505 * Repeat until there is no following "||" or "&&"
3506 */
3507 while (p[0] == opchar && p[1] == opchar)
3508 {
3509 typval_T tv2;
3510
3511 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3512 return FAIL;
3513
3514 // eval the next expression
3515 *arg = skipwhite(p + 2);
3516 tv2.v_type = VAR_UNKNOWN;
3517 tv2.v_lock = 0;
3518 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
3519 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
3520 {
3521 clear_tv(&tv2);
3522 return FAIL;
3523 }
3524 if ((opchar == '&') == val)
3525 {
3526 // false || tv2 or true && tv2: use tv2
3527 clear_tv(tv);
3528 *tv = tv2;
3529 val = tv2bool(tv);
3530 }
3531 else
3532 clear_tv(&tv2);
3533 p = skipwhite(*arg);
3534 }
3535 }
3536
3537 return OK;
3538}
3539
3540/*
3541 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
3542 * Return FAIL if the expression is not a constant.
3543 */
3544 static int
3545evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
3546{
3547 // evaluate the first expression
3548 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
3549 return FAIL;
3550
3551 // || and && work almost the same
3552 return evaluate_const_and_or(arg, cctx, "&&", tv);
3553}
3554
3555/*
3556 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
3557 * Return FAIL if the expression is not a constant.
3558 */
3559 static int
3560evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
3561{
3562 // evaluate the first expression
3563 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
3564 return FAIL;
3565
3566 // || and && work almost the same
3567 return evaluate_const_and_or(arg, cctx, "||", tv);
3568}
3569
3570/*
3571 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
3572 * E.g. for "has('feature')".
3573 * This does not produce error messages. "tv" should be cleared afterwards.
3574 * Return FAIL if the expression is not a constant.
3575 */
3576 static int
3577evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
3578{
3579 char_u *p;
3580
3581 // evaluate the first expression
3582 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
3583 return FAIL;
3584
3585 p = skipwhite(*arg);
3586 if (*p == '?')
3587 {
3588 int val = tv2bool(tv);
3589 typval_T tv2;
3590
3591 // require space before and after the ?
3592 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3593 return FAIL;
3594
3595 // evaluate the second expression; any type is accepted
3596 clear_tv(tv);
3597 *arg = skipwhite(p + 1);
3598 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
3599 return FAIL;
3600
3601 // Check for the ":".
3602 p = skipwhite(*arg);
3603 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3604 return FAIL;
3605
3606 // evaluate the third expression
3607 *arg = skipwhite(p + 1);
3608 tv2.v_type = VAR_UNKNOWN;
3609 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
3610 {
3611 clear_tv(&tv2);
3612 return FAIL;
3613 }
3614 if (val)
3615 {
3616 // use the expr after "?"
3617 clear_tv(&tv2);
3618 }
3619 else
3620 {
3621 // use the expr after ":"
3622 clear_tv(tv);
3623 *tv = tv2;
3624 }
3625 }
3626 return OK;
3627}
3628
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003629// Structure passed between the compile_expr* functions to keep track of
3630// constants that have been parsed but for which no code was produced yet. If
3631// possible expressions on these constants are applied at compile time. If
3632// that is not possible, the code to push the constants needs to be generated
3633// before other instructions.
3634typedef struct {
3635 typval_T pp_tv[10]; // stack of ppconst constants
3636 int pp_used; // active entries in pp_tv[]
3637} ppconst_T;
3638
3639/*
3640 * Generate a PUSH instruction for "tv".
3641 * "tv" will be consumed or cleared.
3642 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
3643 */
3644 static int
3645generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
3646{
3647 if (tv != NULL)
3648 {
3649 switch (tv->v_type)
3650 {
3651 case VAR_UNKNOWN:
3652 break;
3653 case VAR_BOOL:
3654 generate_PUSHBOOL(cctx, tv->vval.v_number);
3655 break;
3656 case VAR_SPECIAL:
3657 generate_PUSHSPEC(cctx, tv->vval.v_number);
3658 break;
3659 case VAR_NUMBER:
3660 generate_PUSHNR(cctx, tv->vval.v_number);
3661 break;
3662#ifdef FEAT_FLOAT
3663 case VAR_FLOAT:
3664 generate_PUSHF(cctx, tv->vval.v_float);
3665 break;
3666#endif
3667 case VAR_BLOB:
3668 generate_PUSHBLOB(cctx, tv->vval.v_blob);
3669 tv->vval.v_blob = NULL;
3670 break;
3671 case VAR_STRING:
3672 generate_PUSHS(cctx, tv->vval.v_string);
3673 tv->vval.v_string = NULL;
3674 break;
3675 default:
3676 iemsg("constant type not supported");
3677 clear_tv(tv);
3678 return FAIL;
3679 }
3680 tv->v_type = VAR_UNKNOWN;
3681 }
3682 return OK;
3683}
3684
3685/*
3686 * Generate code for any ppconst entries.
3687 */
3688 static int
3689generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
3690{
3691 int i;
3692 int ret = OK;
3693
3694 for (i = 0; i < ppconst->pp_used; ++i)
3695 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
3696 ret = FAIL;
3697 ppconst->pp_used = 0;
3698 return ret;
3699}
3700
3701/*
3702 * Clear ppconst constants. Used when failing.
3703 */
3704 static void
3705clear_ppconst(ppconst_T *ppconst)
3706{
3707 int i;
3708
3709 for (i = 0; i < ppconst->pp_used; ++i)
3710 clear_tv(&ppconst->pp_tv[i]);
3711 ppconst->pp_used = 0;
3712}
3713
Bram Moolenaar61a89812020-05-07 16:58:17 +02003714/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 * Compile code to apply '-', '+' and '!'.
3716 */
3717 static int
3718compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3719{
3720 char_u *p = end;
3721
3722 // this works from end to start
3723 while (p > start)
3724 {
3725 --p;
3726 if (*p == '-' || *p == '+')
3727 {
3728 int negate = *p == '-';
3729 isn_T *isn;
3730
3731 // TODO: check type
3732 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3733 {
3734 --p;
3735 if (*p == '-')
3736 negate = !negate;
3737 }
3738 // only '-' has an effect, for '+' we only check the type
3739 if (negate)
3740 isn = generate_instr(cctx, ISN_NEGATENR);
3741 else
3742 isn = generate_instr(cctx, ISN_CHECKNR);
3743 if (isn == NULL)
3744 return FAIL;
3745 }
3746 else
3747 {
3748 int invert = TRUE;
3749
3750 while (p > start && p[-1] == '!')
3751 {
3752 --p;
3753 invert = !invert;
3754 }
3755 if (generate_2BOOL(cctx, invert) == FAIL)
3756 return FAIL;
3757 }
3758 }
3759 return OK;
3760}
3761
3762/*
3763 * Compile whatever comes after "name" or "name()".
3764 */
3765 static int
3766compile_subscript(
3767 char_u **arg,
3768 cctx_T *cctx,
3769 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003770 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003771 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772{
3773 for (;;)
3774 {
3775 if (**arg == '(')
3776 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003777 garray_T *stack = &cctx->ctx_type_stack;
3778 type_T *type;
3779 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003781 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003782 return FAIL;
3783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003785 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 *arg = skipwhite(*arg + 1);
3788 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3789 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003790 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 return FAIL;
3792 }
3793 else if (**arg == '-' && (*arg)[1] == '>')
3794 {
3795 char_u *p;
3796
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003797 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003798 return FAIL;
3799
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 // something->method()
3801 // Apply the '!', '-' and '+' first:
3802 // -1.0->func() works like (-1.0)->func()
3803 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3804 return FAIL;
3805 *start_leader = end_leader; // don't apply again later
3806
3807 *arg = skipwhite(*arg + 2);
3808 if (**arg == '{')
3809 {
3810 // lambda call: list->{lambda}
3811 if (compile_lambda_call(arg, cctx) == FAIL)
3812 return FAIL;
3813 }
3814 else
3815 {
3816 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003817 p = *arg;
3818 if (ASCII_ISALPHA(*p) && p[1] == ':')
3819 p += 2;
3820 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 ;
3822 if (*p != '(')
3823 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003824 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 return FAIL;
3826 }
3827 // TODO: base value may not be the first argument
3828 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
3829 return FAIL;
3830 }
3831 }
3832 else if (**arg == '[')
3833 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003834 garray_T *stack;
3835 type_T **typep;
3836
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003837 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003838 return FAIL;
3839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 // list index: list[123]
3841 // TODO: more arguments
3842 // TODO: dict member dict['name']
3843 *arg = skipwhite(*arg + 1);
3844 if (compile_expr1(arg, cctx) == FAIL)
3845 return FAIL;
3846
3847 if (**arg != ']')
3848 {
3849 emsg(_(e_missbrac));
3850 return FAIL;
3851 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003852 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853
3854 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3855 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003856 stack = &cctx->ctx_type_stack;
3857 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3858 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3859 {
3860 emsg(_(e_listreq));
3861 return FAIL;
3862 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003863 if ((*typep)->tt_type == VAR_LIST)
3864 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 }
3866 else if (**arg == '.' && (*arg)[1] != '.')
3867 {
3868 char_u *p;
3869
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003870 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003871 return FAIL;
3872
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 ++*arg;
3874 p = *arg;
3875 // dictionary member: dict.name
3876 if (eval_isnamec1(*p))
3877 while (eval_isnamec(*p))
3878 MB_PTR_ADV(p);
3879 if (p == *arg)
3880 {
3881 semsg(_(e_syntax_at), *arg);
3882 return FAIL;
3883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3885 return FAIL;
3886 *arg = p;
3887 }
3888 else
3889 break;
3890 }
3891
3892 // TODO - see handle_subscript():
3893 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3894 // Don't do this when "Func" is already a partial that was bound
3895 // explicitly (pt_auto is FALSE).
3896
3897 return OK;
3898}
3899
3900/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003901 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3902 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003904 * If the value is a constant "ppconst->pp_ret" will be set.
3905 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003906 *
3907 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 */
3909
3910/*
3911 * number number constant
3912 * 0zFFFFFFFF Blob constant
3913 * "string" string constant
3914 * 'string' literal string constant
3915 * &option-name option value
3916 * @r register contents
3917 * identifier variable value
3918 * function() function call
3919 * $VAR environment variable
3920 * (expression) nested expression
3921 * [expr, expr] List
3922 * {key: val, key: val} Dictionary
3923 * #{key: val, key: val} Dictionary with literal keys
3924 *
3925 * Also handle:
3926 * ! in front logical NOT
3927 * - in front unary minus
3928 * + in front unary plus (ignored)
3929 * trailing (arg) funcref/partial call
3930 * trailing [] subscript in String or List
3931 * trailing .name entry in Dictionary
3932 * trailing ->name() method call
3933 */
3934 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003935compile_expr7(
3936 char_u **arg,
3937 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003938 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 char_u *start_leader, *end_leader;
3941 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003942 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943
3944 /*
3945 * Skip '!', '-' and '+' characters. They are handled later.
3946 */
3947 start_leader = *arg;
3948 while (**arg == '!' || **arg == '-' || **arg == '+')
3949 *arg = skipwhite(*arg + 1);
3950 end_leader = *arg;
3951
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003952 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 switch (**arg)
3954 {
3955 /*
3956 * Number constant.
3957 */
3958 case '0': // also for blob starting with 0z
3959 case '1':
3960 case '2':
3961 case '3':
3962 case '4':
3963 case '5':
3964 case '6':
3965 case '7':
3966 case '8':
3967 case '9':
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003968 case '.': if (get_number_tv(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 return FAIL;
3970 break;
3971
3972 /*
3973 * String constant: "string".
3974 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003975 case '"': if (get_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 return FAIL;
3977 break;
3978
3979 /*
3980 * Literal string constant: 'str''ing'.
3981 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003982 case '\'': if (get_lit_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 return FAIL;
3984 break;
3985
3986 /*
3987 * Constant Vim variable.
3988 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003989 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 ret = NOTDONE;
3991 break;
3992
3993 /*
3994 * List: [expr, expr]
3995 */
3996 case '[': ret = compile_list(arg, cctx);
3997 break;
3998
3999 /*
4000 * Dictionary: #{key: val, key: val}
4001 */
4002 case '#': if ((*arg)[1] == '{')
4003 {
4004 ++*arg;
4005 ret = compile_dict(arg, cctx, TRUE);
4006 }
4007 else
4008 ret = NOTDONE;
4009 break;
4010
4011 /*
4012 * Lambda: {arg, arg -> expr}
4013 * Dictionary: {'key': val, 'key': val}
4014 */
4015 case '{': {
4016 char_u *start = skipwhite(*arg + 1);
4017
4018 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004019 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004021 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 if (ret != FAIL && *start == '>')
4023 ret = compile_lambda(arg, cctx);
4024 else
4025 ret = compile_dict(arg, cctx, FALSE);
4026 }
4027 break;
4028
4029 /*
4030 * Option value: &name
4031 */
4032 case '&': ret = compile_get_option(arg, cctx);
4033 break;
4034
4035 /*
4036 * Environment variable: $VAR.
4037 */
4038 case '$': ret = compile_get_env(arg, cctx);
4039 break;
4040
4041 /*
4042 * Register contents: @r.
4043 */
4044 case '@': ret = compile_get_register(arg, cctx);
4045 break;
4046 /*
4047 * nested expression: (expression).
4048 */
4049 case '(': *arg = skipwhite(*arg + 1);
4050 ret = compile_expr1(arg, cctx); // recursive!
4051 *arg = skipwhite(*arg);
4052 if (**arg == ')')
4053 ++*arg;
4054 else if (ret == OK)
4055 {
4056 emsg(_(e_missing_close));
4057 ret = FAIL;
4058 }
4059 break;
4060
4061 default: ret = NOTDONE;
4062 break;
4063 }
4064 if (ret == FAIL)
4065 return FAIL;
4066
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004067 if (rettv->v_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 {
4069 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004070 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004072 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 return FAIL;
4074 }
4075 start_leader = end_leader; // don't apply again below
4076
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004077 // A constant expression can possibly be handled compile time, return
4078 // the value instead of generating code.
4079 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 }
4081 else if (ret == NOTDONE)
4082 {
4083 char_u *p;
4084 int r;
4085
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004086 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004087 return FAIL;
4088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 if (!eval_isnamec1(**arg))
4090 {
4091 semsg(_("E1015: Name expected: %s"), *arg);
4092 return FAIL;
4093 }
4094
4095 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004096 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 if (*p == '(')
4098 r = compile_call(arg, p - *arg, cctx, 0);
4099 else
4100 r = compile_load(arg, p, cctx, TRUE);
4101 if (r == FAIL)
4102 return FAIL;
4103 }
4104
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004105 // Handle following "[]", ".member", etc.
4106 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004107 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004108 ppconst) == FAIL
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004109 || compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004111 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112}
4113
4114/*
4115 * * number multiplication
4116 * / number division
4117 * % number modulo
4118 */
4119 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004120compile_expr6(
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004121 char_u **arg,
4122 cctx_T *cctx,
4123 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124{
4125 char_u *op;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004126 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004128 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004129 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 return FAIL;
4131
4132 /*
4133 * Repeat computing, until no "*", "/" or "%" is following.
4134 */
4135 for (;;)
4136 {
4137 op = skipwhite(*arg);
4138 if (*op != '*' && *op != '/' && *op != '%')
4139 break;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004140
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004141 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 {
4143 char_u buf[3];
4144
4145 vim_strncpy(buf, op, 1);
4146 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004147 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 }
4149 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004150 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004151 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004153 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004154 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004156
4157 if (ppconst->pp_used == ppconst_used + 2
4158 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4159 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004160 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004161 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4162 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004163 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004165 // both are numbers: compute the result
4166 switch (*op)
4167 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004168 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004169 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004170 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004171 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004172 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004173 break;
4174 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004175 tv1->vval.v_number = res;
4176 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004177 }
4178 else
4179 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004180 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004181 generate_two_op(cctx, op);
4182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 }
4184
4185 return OK;
4186}
4187
4188/*
4189 * + number addition
4190 * - number subtraction
4191 * .. string concatenation
4192 */
4193 static int
4194compile_expr5(char_u **arg, cctx_T *cctx)
4195{
4196 char_u *op;
4197 int oplen;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004198 ppconst_T ppconst;
4199 int ppconst_used = 0;
4200
4201 CLEAR_FIELD(ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202
4203 // get the first variable
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004204 if (compile_expr6(arg, cctx, &ppconst) == FAIL)
4205 {
4206 clear_ppconst(&ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209
4210 /*
4211 * Repeat computing, until no "+", "-" or ".." is following.
4212 */
4213 for (;;)
4214 {
4215 op = skipwhite(*arg);
4216 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
4217 break;
4218 oplen = (*op == '.' ? 2 : 1);
4219
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004220 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 {
4222 char_u buf[3];
4223
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004224 clear_ppconst(&ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 vim_strncpy(buf, op, oplen);
4226 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004227 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 }
4229
4230 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004231 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004232 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004233 clear_ppconst(&ppconst);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004234 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004237 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004238 if (compile_expr6(arg, cctx, &ppconst) == FAIL)
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004239 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004240 clear_ppconst(&ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004244 if (ppconst.pp_used == ppconst_used + 2
4245 && (*op == '.'
4246 ? (ppconst.pp_tv[ppconst_used].v_type == VAR_STRING
4247 && ppconst.pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4248 : (ppconst.pp_tv[ppconst_used].v_type == VAR_NUMBER
4249 && ppconst.pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004251 typval_T *tv1 = &ppconst.pp_tv[ppconst_used];
4252 typval_T *tv2 = &ppconst.pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004253
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004254 // concat/subtract/add constant numbers
4255 if (*op == '+')
4256 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4257 else if (*op == '-')
4258 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4259 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004260 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004261 // concatenate constant strings
4262 char_u *s1 = tv1->vval.v_string;
4263 char_u *s2 = tv2->vval.v_string;
4264 size_t len1 = STRLEN(s1);
4265
4266 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4267 if (tv1->vval.v_string == NULL)
4268 {
4269 clear_ppconst(&ppconst);
4270 return FAIL;
4271 }
4272 mch_memmove(tv1->vval.v_string, s1, len1);
4273 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004274 vim_free(s1);
4275 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004276 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004277 --ppconst.pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 }
4279 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004280 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004281 generate_ppconst(cctx, &ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004282 if (*op == '.')
4283 {
4284 if (may_generate_2STRING(-2, cctx) == FAIL
4285 || may_generate_2STRING(-1, cctx) == FAIL)
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004286 {
4287 clear_ppconst(&ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004288 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004289 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004290 generate_instr_drop(cctx, ISN_CONCAT, 1);
4291 }
4292 else
4293 generate_two_op(cctx, op);
4294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 }
4296
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004297 // TODO: move to caller
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004298 generate_ppconst(cctx, &ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 return OK;
4301}
4302
4303/*
4304 * expr5a == expr5b
4305 * expr5a =~ expr5b
4306 * expr5a != expr5b
4307 * expr5a !~ expr5b
4308 * expr5a > expr5b
4309 * expr5a >= expr5b
4310 * expr5a < expr5b
4311 * expr5a <= expr5b
4312 * expr5a is expr5b
4313 * expr5a isnot expr5b
4314 *
4315 * Produces instructions:
4316 * EVAL expr5a Push result of "expr5a"
4317 * EVAL expr5b Push result of "expr5b"
4318 * COMPARE one of the compare instructions
4319 */
4320 static int
4321compile_expr4(char_u **arg, cctx_T *cctx)
4322{
4323 exptype_T type = EXPR_UNKNOWN;
4324 char_u *p;
4325 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 int type_is = FALSE;
4327
4328 // get the first variable
4329 if (compile_expr5(arg, cctx) == FAIL)
4330 return FAIL;
4331
4332 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004333 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334
4335 /*
4336 * If there is a comparative operator, use it.
4337 */
4338 if (type != EXPR_UNKNOWN)
4339 {
4340 int ic = FALSE; // Default: do not ignore case
4341
4342 if (type_is && (p[len] == '?' || p[len] == '#'))
4343 {
4344 semsg(_(e_invexpr2), *arg);
4345 return FAIL;
4346 }
4347 // extra question mark appended: ignore case
4348 if (p[len] == '?')
4349 {
4350 ic = TRUE;
4351 ++len;
4352 }
4353 // extra '#' appended: match case (ignored)
4354 else if (p[len] == '#')
4355 ++len;
4356 // nothing appended: match case
4357
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004358 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 {
4360 char_u buf[7];
4361
4362 vim_strncpy(buf, p, len);
4363 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004364 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365 }
4366
4367 // get the second variable
4368 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004369 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004370 return FAIL;
4371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 if (compile_expr5(arg, cctx) == FAIL)
4373 return FAIL;
4374
4375 generate_COMPARE(cctx, type, ic);
4376 }
4377
4378 return OK;
4379}
4380
4381/*
4382 * Compile || or &&.
4383 */
4384 static int
4385compile_and_or(char_u **arg, cctx_T *cctx, char *op)
4386{
4387 char_u *p = skipwhite(*arg);
4388 int opchar = *op;
4389
4390 if (p[0] == opchar && p[1] == opchar)
4391 {
4392 garray_T *instr = &cctx->ctx_instr;
4393 garray_T end_ga;
4394
4395 /*
4396 * Repeat until there is no following "||" or "&&"
4397 */
4398 ga_init2(&end_ga, sizeof(int), 10);
4399 while (p[0] == opchar && p[1] == opchar)
4400 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004401 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4402 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004404 return FAIL;
4405 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406
4407 if (ga_grow(&end_ga, 1) == FAIL)
4408 {
4409 ga_clear(&end_ga);
4410 return FAIL;
4411 }
4412 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4413 ++end_ga.ga_len;
4414 generate_JUMP(cctx, opchar == '|'
4415 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4416
4417 // eval the next expression
4418 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004419 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004420 return FAIL;
4421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 if ((opchar == '|' ? compile_expr3(arg, cctx)
4423 : compile_expr4(arg, cctx)) == FAIL)
4424 {
4425 ga_clear(&end_ga);
4426 return FAIL;
4427 }
4428 p = skipwhite(*arg);
4429 }
4430
4431 // Fill in the end label in all jumps.
4432 while (end_ga.ga_len > 0)
4433 {
4434 isn_T *isn;
4435
4436 --end_ga.ga_len;
4437 isn = ((isn_T *)instr->ga_data)
4438 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4439 isn->isn_arg.jump.jump_where = instr->ga_len;
4440 }
4441 ga_clear(&end_ga);
4442 }
4443
4444 return OK;
4445}
4446
4447/*
4448 * expr4a && expr4a && expr4a logical AND
4449 *
4450 * Produces instructions:
4451 * EVAL expr4a Push result of "expr4a"
4452 * JUMP_AND_KEEP_IF_FALSE end
4453 * EVAL expr4b Push result of "expr4b"
4454 * JUMP_AND_KEEP_IF_FALSE end
4455 * EVAL expr4c Push result of "expr4c"
4456 * end:
4457 */
4458 static int
4459compile_expr3(char_u **arg, cctx_T *cctx)
4460{
4461 // get the first variable
4462 if (compile_expr4(arg, cctx) == FAIL)
4463 return FAIL;
4464
4465 // || and && work almost the same
4466 return compile_and_or(arg, cctx, "&&");
4467}
4468
4469/*
4470 * expr3a || expr3b || expr3c logical OR
4471 *
4472 * Produces instructions:
4473 * EVAL expr3a Push result of "expr3a"
4474 * JUMP_AND_KEEP_IF_TRUE end
4475 * EVAL expr3b Push result of "expr3b"
4476 * JUMP_AND_KEEP_IF_TRUE end
4477 * EVAL expr3c Push result of "expr3c"
4478 * end:
4479 */
4480 static int
4481compile_expr2(char_u **arg, cctx_T *cctx)
4482{
4483 // eval the first expression
4484 if (compile_expr3(arg, cctx) == FAIL)
4485 return FAIL;
4486
4487 // || and && work almost the same
4488 return compile_and_or(arg, cctx, "||");
4489}
4490
4491/*
4492 * Toplevel expression: expr2 ? expr1a : expr1b
4493 *
4494 * Produces instructions:
4495 * EVAL expr2 Push result of "expr"
4496 * JUMP_IF_FALSE alt jump if false
4497 * EVAL expr1a
4498 * JUMP_ALWAYS end
4499 * alt: EVAL expr1b
4500 * end:
4501 */
4502 static int
4503compile_expr1(char_u **arg, cctx_T *cctx)
4504{
4505 char_u *p;
4506
Bram Moolenaar61a89812020-05-07 16:58:17 +02004507 // Evaluate the first expression.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004508 if (compile_expr2(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 return FAIL;
4510
4511 p = skipwhite(*arg);
4512 if (*p == '?')
4513 {
4514 garray_T *instr = &cctx->ctx_instr;
4515 garray_T *stack = &cctx->ctx_type_stack;
4516 int alt_idx = instr->ga_len;
4517 int end_idx;
4518 isn_T *isn;
4519 type_T *type1;
4520 type_T *type2;
4521
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004522 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4523 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004525 return FAIL;
4526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527
4528 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4529
4530 // evaluate the second expression; any type is accepted
4531 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004532 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004533 return FAIL;
4534
Bram Moolenaara6d53682020-01-28 23:04:06 +01004535 if (compile_expr1(arg, cctx) == FAIL)
4536 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537
4538 // remember the type and drop it
4539 --stack->ga_len;
4540 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
4541
4542 end_idx = instr->ga_len;
4543 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4544
4545 // jump here from JUMP_IF_FALSE
4546 isn = ((isn_T *)instr->ga_data) + alt_idx;
4547 isn->isn_arg.jump.jump_where = instr->ga_len;
4548
4549 // Check for the ":".
4550 p = skipwhite(*arg);
4551 if (*p != ':')
4552 {
4553 emsg(_(e_missing_colon));
4554 return FAIL;
4555 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004556 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4557 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004559 return FAIL;
4560 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561
4562 // evaluate the third expression
4563 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004564 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004565 return FAIL;
4566
Bram Moolenaara6d53682020-01-28 23:04:06 +01004567 if (compile_expr1(arg, cctx) == FAIL)
4568 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569
4570 // If the types differ, the result has a more generic type.
4571 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004572 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573
4574 // jump here from JUMP_ALWAYS
4575 isn = ((isn_T *)instr->ga_data) + end_idx;
4576 isn->isn_arg.jump.jump_where = instr->ga_len;
4577 }
4578 return OK;
4579}
4580
4581/*
4582 * compile "return [expr]"
4583 */
4584 static char_u *
4585compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4586{
4587 char_u *p = arg;
4588 garray_T *stack = &cctx->ctx_type_stack;
4589 type_T *stack_type;
4590
4591 if (*p != NUL && *p != '|' && *p != '\n')
4592 {
4593 // compile return argument into instructions
4594 if (compile_expr1(&p, cctx) == FAIL)
4595 return NULL;
4596
4597 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4598 if (set_return_type)
4599 cctx->ctx_ufunc->uf_ret_type = stack_type;
4600 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4601 == FAIL)
4602 return NULL;
4603 }
4604 else
4605 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004606 // "set_return_type" cannot be TRUE, only used for a lambda which
4607 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004608 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4609 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 {
4611 emsg(_("E1003: Missing return value"));
4612 return NULL;
4613 }
4614
4615 // No argument, return zero.
4616 generate_PUSHNR(cctx, 0);
4617 }
4618
4619 if (generate_instr(cctx, ISN_RETURN) == NULL)
4620 return NULL;
4621
4622 // "return val | endif" is possible
4623 return skipwhite(p);
4624}
4625
4626/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004627 * Get a line from the compilation context, compatible with exarg_T getline().
4628 * Return a pointer to the line in allocated memory.
4629 * Return NULL for end-of-file or some error.
4630 */
4631 static char_u *
4632exarg_getline(
4633 int c UNUSED,
4634 void *cookie,
4635 int indent UNUSED,
4636 int do_concat UNUSED)
4637{
4638 cctx_T *cctx = (cctx_T *)cookie;
4639
4640 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4641 {
4642 iemsg("Heredoc got to end");
4643 return NULL;
4644 }
4645 ++cctx->ctx_lnum;
4646 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4647 [cctx->ctx_lnum]);
4648}
4649
4650/*
4651 * Compile a nested :def command.
4652 */
4653 static char_u *
4654compile_nested_function(exarg_T *eap, cctx_T *cctx)
4655{
4656 char_u *name_start = eap->arg;
4657 char_u *name_end = to_name_end(eap->arg, FALSE);
4658 char_u *name = get_lambda_name();
4659 lvar_T *lvar;
4660 ufunc_T *ufunc;
4661
4662 eap->arg = name_end;
4663 eap->getline = exarg_getline;
4664 eap->cookie = cctx;
4665 eap->skip = cctx->ctx_skip == TRUE;
4666 eap->forceit = FALSE;
4667 ufunc = def_function(eap, name, cctx);
4668
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004669 if (ufunc == NULL || ufunc->uf_dfunc_idx < 0)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004670 return NULL;
4671
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004672 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004673 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004674 TRUE, ufunc->uf_func_type);
4675
4676 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4677 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4678 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004679
Bram Moolenaar61a89812020-05-07 16:58:17 +02004680 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004681 return (char_u *)"";
4682}
4683
4684/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 * Return the length of an assignment operator, or zero if there isn't one.
4686 */
4687 int
4688assignment_len(char_u *p, int *heredoc)
4689{
4690 if (*p == '=')
4691 {
4692 if (p[1] == '<' && p[2] == '<')
4693 {
4694 *heredoc = TRUE;
4695 return 3;
4696 }
4697 return 1;
4698 }
4699 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4700 return 2;
4701 if (STRNCMP(p, "..=", 3) == 0)
4702 return 3;
4703 return 0;
4704}
4705
4706// words that cannot be used as a variable
4707static char *reserved[] = {
4708 "true",
4709 "false",
4710 NULL
4711};
4712
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004713typedef enum {
4714 dest_local,
4715 dest_option,
4716 dest_env,
4717 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004718 dest_buffer,
4719 dest_window,
4720 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004721 dest_vimvar,
4722 dest_script,
4723 dest_reg,
4724} assign_dest_T;
4725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726/*
4727 * compile "let var [= expr]", "const var = expr" and "var = expr"
4728 * "arg" points to "var".
4729 */
4730 static char_u *
4731compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4732{
4733 char_u *p;
4734 char_u *ret = NULL;
4735 int var_count = 0;
4736 int semicolon = 0;
4737 size_t varlen;
4738 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004739 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004742 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004744 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 int oplen = 0;
4746 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004747 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004748 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 char_u *name;
4750 char_u *sp;
4751 int has_type = FALSE;
4752 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4753 int instr_count = -1;
4754
4755 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4756 if (p == NULL)
4757 return NULL;
4758 if (var_count > 0)
4759 {
4760 // TODO: let [var, var] = list
4761 emsg("Cannot handle a list yet");
4762 return NULL;
4763 }
4764
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004765 // "a: type" is declaring variable "a" with a type, not "a:".
4766 if (is_decl && p == arg + 2 && p[-1] == ':')
4767 --p;
4768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 varlen = p - arg;
4770 name = vim_strnsave(arg, (int)varlen);
4771 if (name == NULL)
4772 return NULL;
4773
Bram Moolenaar080457c2020-03-03 21:53:32 +01004774 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004776 if (*arg == '&')
4777 {
4778 int cc;
4779 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780
Bram Moolenaar080457c2020-03-03 21:53:32 +01004781 dest = dest_option;
4782 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004784 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004785 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 if (is_decl)
4788 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004789 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 goto theend;
4791 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004792 p = arg;
4793 p = find_option_end(&p, &opt_flags);
4794 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004796 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004797 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004798 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004799 }
4800 cc = *p;
4801 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004802 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004803 *p = cc;
4804 if (opt_type == -3)
4805 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004806 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004807 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004808 }
4809 if (opt_type == -2 || opt_type == 0)
4810 type = &t_string;
4811 else
4812 type = &t_number; // both number and boolean option
4813 }
4814 else if (*arg == '$')
4815 {
4816 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004817 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004818 if (is_decl)
4819 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004820 semsg(_("E1065: Cannot declare an environment variable: %s"),
4821 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004822 goto theend;
4823 }
4824 }
4825 else if (*arg == '@')
4826 {
4827 if (!valid_yank_reg(arg[1], TRUE))
4828 {
4829 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004830 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004831 }
4832 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004833 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004834 if (is_decl)
4835 {
4836 semsg(_("E1066: Cannot declare a register: %s"), name);
4837 goto theend;
4838 }
4839 }
4840 else if (STRNCMP(arg, "g:", 2) == 0)
4841 {
4842 dest = dest_global;
4843 if (is_decl)
4844 {
4845 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4846 goto theend;
4847 }
4848 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004849 else if (STRNCMP(arg, "b:", 2) == 0)
4850 {
4851 dest = dest_buffer;
4852 if (is_decl)
4853 {
4854 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4855 goto theend;
4856 }
4857 }
4858 else if (STRNCMP(arg, "w:", 2) == 0)
4859 {
4860 dest = dest_window;
4861 if (is_decl)
4862 {
4863 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4864 goto theend;
4865 }
4866 }
4867 else if (STRNCMP(arg, "t:", 2) == 0)
4868 {
4869 dest = dest_tab;
4870 if (is_decl)
4871 {
4872 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4873 goto theend;
4874 }
4875 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004876 else if (STRNCMP(arg, "v:", 2) == 0)
4877 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004878 typval_T *vtv;
4879 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004880
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004881 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004882 if (vimvaridx < 0)
4883 {
4884 semsg(_(e_var_notfound), arg);
4885 goto theend;
4886 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004887 // We use the current value of "sandbox" here, is that OK?
4888 if (var_check_ro(di_flags, name, FALSE))
4889 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004890 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004891 vtv = get_vim_var_tv(vimvaridx);
4892 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004893 if (is_decl)
4894 {
4895 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4896 goto theend;
4897 }
4898 }
4899 else
4900 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004901 int idx;
4902
Bram Moolenaar080457c2020-03-03 21:53:32 +01004903 for (idx = 0; reserved[idx] != NULL; ++idx)
4904 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004906 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 goto theend;
4908 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004909
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004910 lvar = lookup_local(arg, varlen, cctx);
4911 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004913 if (is_decl)
4914 {
4915 semsg(_("E1017: Variable already declared: %s"), name);
4916 goto theend;
4917 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004918 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004919 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004920 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4921 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004922 }
4923 }
4924 else if (STRNCMP(arg, "s:", 2) == 0
4925 || lookup_script(arg, varlen) == OK
4926 || find_imported(arg, varlen, cctx) != NULL)
4927 {
4928 dest = dest_script;
4929 if (is_decl)
4930 {
4931 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004932 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004933 goto theend;
4934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004936 else if (name[1] == ':' && name[2] != NUL)
4937 {
4938 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
4939 goto theend;
4940 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 }
4942 }
4943
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004944 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 {
4946 if (is_decl && *p == ':')
4947 {
4948 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004949 if (!VIM_ISWHITE(p[1]))
4950 {
4951 semsg(_(e_white_after), ":");
4952 goto theend;
4953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 p = skipwhite(p + 1);
4955 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 has_type = TRUE;
4957 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004958 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 }
4961
4962 sp = p;
4963 p = skipwhite(p);
4964 op = p;
4965 oplen = assignment_len(p, &heredoc);
4966 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4967 {
4968 char_u buf[4];
4969
4970 vim_strncpy(buf, op, oplen);
4971 semsg(_(e_white_both), buf);
4972 }
4973
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004974 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004975 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004977 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978 goto theend;
4979 }
4980
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004981 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 {
4983 if (oplen > 1 && !heredoc)
4984 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004985 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4987 name);
4988 goto theend;
4989 }
4990
4991 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004992 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004993 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004994 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4995 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004997 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 }
4999
5000 if (heredoc)
5001 {
5002 list_T *l;
5003 listitem_T *li;
5004
5005 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005006 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005008 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009
5010 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005011 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 {
5013 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5014 li->li_tv.vval.v_string = NULL;
5015 }
5016 generate_NEWLIST(cctx, l->lv_len);
5017 type = &t_list_string;
5018 list_free(l);
5019 p += STRLEN(p);
5020 }
5021 else if (oplen > 0)
5022 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02005023 int r;
5024 type_T *stacktype;
5025 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027 // for "+=", "*=", "..=" etc. first load the current value
5028 if (*op != '=')
5029 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005030 switch (dest)
5031 {
5032 case dest_option:
5033 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02005034 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005035 break;
5036 case dest_global:
5037 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5038 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02005039 case dest_buffer:
5040 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5041 break;
5042 case dest_window:
5043 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5044 break;
5045 case dest_tab:
5046 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5047 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005048 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01005049 compile_load_scriptvar(cctx,
5050 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005051 break;
5052 case dest_env:
5053 // Include $ in the name here
5054 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5055 break;
5056 case dest_reg:
5057 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
5058 break;
5059 case dest_vimvar:
5060 generate_LOADV(cctx, name + 2, TRUE);
5061 break;
5062 case dest_local:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005063 if (lvar->lv_from_outer)
5064 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5065 NULL, type);
5066 else
5067 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005068 break;
5069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 }
5071
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005072 // Compile the expression. Temporarily hide the new local variable
5073 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02005074 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005075 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 instr_count = instr->ga_len;
5077 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005078 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02005079 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005080 ++cctx->ctx_locals.ga_len;
5081 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082 goto theend;
5083
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005084 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005086 stack = &cctx->ctx_type_stack;
5087 stacktype = stack->ga_len == 0 ? &t_void
5088 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005089 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005091 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005093 if (stacktype->tt_type == VAR_VOID)
5094 {
5095 emsg(_("E1031: Cannot use void value"));
5096 goto theend;
5097 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005098 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005099 {
5100 // An empty list or dict has a &t_void member, for a
5101 // variable that implies &t_any.
5102 if (stacktype == &t_list_empty)
5103 lvar->lv_type = &t_list_any;
5104 else if (stacktype == &t_dict_empty)
5105 lvar->lv_type = &t_dict_any;
5106 else
5107 lvar->lv_type = stacktype;
5108 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005109 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005110 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
5111 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005113 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005114 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115 }
5116 }
5117 else if (cmdidx == CMD_const)
5118 {
5119 emsg(_("E1021: const requires a value"));
5120 goto theend;
5121 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005122 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 {
5124 emsg(_("E1022: type or initialization required"));
5125 goto theend;
5126 }
5127 else
5128 {
5129 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 if (ga_grow(instr, 1) == FAIL)
5131 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005132 switch (type->tt_type)
5133 {
5134 case VAR_BOOL:
5135 generate_PUSHBOOL(cctx, VVAL_FALSE);
5136 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005137 case VAR_FLOAT:
5138#ifdef FEAT_FLOAT
5139 generate_PUSHF(cctx, 0.0);
5140#endif
5141 break;
5142 case VAR_STRING:
5143 generate_PUSHS(cctx, NULL);
5144 break;
5145 case VAR_BLOB:
5146 generate_PUSHBLOB(cctx, NULL);
5147 break;
5148 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005149 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005150 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005151 case VAR_LIST:
5152 generate_NEWLIST(cctx, 0);
5153 break;
5154 case VAR_DICT:
5155 generate_NEWDICT(cctx, 0);
5156 break;
5157 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005158 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005159 break;
5160 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005161 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005162 break;
5163 case VAR_NUMBER:
5164 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005165 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02005166 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01005167 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02005168 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01005169 generate_PUSHNR(cctx, 0);
5170 break;
5171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172 }
5173
5174 if (oplen > 0 && *op != '=')
5175 {
5176 type_T *expected = &t_number;
5177 garray_T *stack = &cctx->ctx_type_stack;
5178 type_T *stacktype;
5179
5180 // TODO: if type is known use float or any operation
5181
5182 if (*op == '.')
5183 expected = &t_string;
5184 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5185 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5186 goto theend;
5187
5188 if (*op == '.')
5189 generate_instr_drop(cctx, ISN_CONCAT, 1);
5190 else
5191 {
5192 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5193
5194 if (isn == NULL)
5195 goto theend;
5196 switch (*op)
5197 {
5198 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5199 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5200 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5201 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5202 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5203 }
5204 }
5205 }
5206
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005207 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005209 case dest_option:
5210 generate_STOREOPT(cctx, name + 1, opt_flags);
5211 break;
5212 case dest_global:
5213 // include g: with the name, easier to execute that way
5214 generate_STORE(cctx, ISN_STOREG, 0, name);
5215 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02005216 case dest_buffer:
5217 // include b: with the name, easier to execute that way
5218 generate_STORE(cctx, ISN_STOREB, 0, name);
5219 break;
5220 case dest_window:
5221 // include w: with the name, easier to execute that way
5222 generate_STORE(cctx, ISN_STOREW, 0, name);
5223 break;
5224 case dest_tab:
5225 // include t: with the name, easier to execute that way
5226 generate_STORE(cctx, ISN_STORET, 0, name);
5227 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005228 case dest_env:
5229 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5230 break;
5231 case dest_reg:
5232 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5233 break;
5234 case dest_vimvar:
5235 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5236 break;
5237 case dest_script:
5238 {
5239 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5240 imported_T *import = NULL;
5241 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005242 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005243
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005244 if (name[1] != ':')
5245 {
5246 import = find_imported(name, 0, cctx);
5247 if (import != NULL)
5248 sid = import->imp_sid;
5249 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005250
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005251 idx = get_script_item_idx(sid, rawname, TRUE);
5252 // TODO: specific type
5253 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005254 {
5255 char_u *name_s = name;
5256
5257 // Include s: in the name for store_var()
5258 if (name[1] != ':')
5259 {
5260 int len = (int)STRLEN(name) + 3;
5261
5262 name_s = alloc(len);
5263 if (name_s == NULL)
5264 name_s = name;
5265 else
5266 vim_snprintf((char *)name_s, len, "s:%s", name);
5267 }
5268 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
5269 if (name_s != name)
5270 vim_free(name_s);
5271 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005272 else
5273 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5274 sid, idx, &t_any);
5275 }
5276 break;
5277 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005278 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005279 {
5280 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005281
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005282 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
5283 // into ISN_STORENR
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005284 if (!lvar->lv_from_outer && instr->ga_len == instr_count + 1
5285 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005286 {
5287 varnumber_T val = isn->isn_arg.number;
5288 garray_T *stack = &cctx->ctx_type_stack;
5289
5290 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005291 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01005292 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005293 if (stack->ga_len > 0)
5294 --stack->ga_len;
5295 }
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005296 else if (lvar->lv_from_outer)
5297 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005298 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005299 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005300 }
5301 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302 }
5303 ret = p;
5304
5305theend:
5306 vim_free(name);
5307 return ret;
5308}
5309
5310/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005311 * Check if "name" can be "unlet".
5312 */
5313 int
5314check_vim9_unlet(char_u *name)
5315{
5316 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5317 {
5318 semsg(_("E1081: Cannot unlet %s"), name);
5319 return FAIL;
5320 }
5321 return OK;
5322}
5323
5324/*
5325 * Callback passed to ex_unletlock().
5326 */
5327 static int
5328compile_unlet(
5329 lval_T *lvp,
5330 char_u *name_end,
5331 exarg_T *eap,
5332 int deep UNUSED,
5333 void *coookie)
5334{
5335 cctx_T *cctx = coookie;
5336
5337 if (lvp->ll_tv == NULL)
5338 {
5339 char_u *p = lvp->ll_name;
5340 int cc = *name_end;
5341 int ret = OK;
5342
5343 // Normal name. Only supports g:, w:, t: and b: namespaces.
5344 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005345 if (*p == '$')
5346 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5347 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005348 ret = FAIL;
5349 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005350 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005351
5352 *name_end = cc;
5353 return ret;
5354 }
5355
5356 // TODO: unlet {list}[idx]
5357 // TODO: unlet {dict}[key]
5358 emsg("Sorry, :unlet not fully implemented yet");
5359 return FAIL;
5360}
5361
5362/*
5363 * compile "unlet var", "lock var" and "unlock var"
5364 * "arg" points to "var".
5365 */
5366 static char_u *
5367compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5368{
5369 char_u *p = arg;
5370
5371 if (eap->cmdidx != CMD_unlet)
5372 {
5373 emsg("Sorry, :lock and unlock not implemented yet");
5374 return NULL;
5375 }
5376
5377 if (*p == '!')
5378 {
5379 p = skipwhite(p + 1);
5380 eap->forceit = TRUE;
5381 }
5382
5383 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5384 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5385}
5386
5387/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005388 * Compile an :import command.
5389 */
5390 static char_u *
5391compile_import(char_u *arg, cctx_T *cctx)
5392{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005393 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394}
5395
5396/*
5397 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5398 */
5399 static int
5400compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5401{
5402 garray_T *instr = &cctx->ctx_instr;
5403 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5404
5405 if (endlabel == NULL)
5406 return FAIL;
5407 endlabel->el_next = *el;
5408 *el = endlabel;
5409 endlabel->el_end_label = instr->ga_len;
5410
5411 generate_JUMP(cctx, when, 0);
5412 return OK;
5413}
5414
5415 static void
5416compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5417{
5418 garray_T *instr = &cctx->ctx_instr;
5419
5420 while (*el != NULL)
5421 {
5422 endlabel_T *cur = (*el);
5423 isn_T *isn;
5424
5425 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5426 isn->isn_arg.jump.jump_where = instr->ga_len;
5427 *el = cur->el_next;
5428 vim_free(cur);
5429 }
5430}
5431
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005432 static void
5433compile_free_jump_to_end(endlabel_T **el)
5434{
5435 while (*el != NULL)
5436 {
5437 endlabel_T *cur = (*el);
5438
5439 *el = cur->el_next;
5440 vim_free(cur);
5441 }
5442}
5443
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005444/*
5445 * Create a new scope and set up the generic items.
5446 */
5447 static scope_T *
5448new_scope(cctx_T *cctx, scopetype_T type)
5449{
5450 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5451
5452 if (scope == NULL)
5453 return NULL;
5454 scope->se_outer = cctx->ctx_scope;
5455 cctx->ctx_scope = scope;
5456 scope->se_type = type;
5457 scope->se_local_count = cctx->ctx_locals.ga_len;
5458 return scope;
5459}
5460
5461/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005462 * Free the current scope and go back to the outer scope.
5463 */
5464 static void
5465drop_scope(cctx_T *cctx)
5466{
5467 scope_T *scope = cctx->ctx_scope;
5468
5469 if (scope == NULL)
5470 {
5471 iemsg("calling drop_scope() without a scope");
5472 return;
5473 }
5474 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005475 switch (scope->se_type)
5476 {
5477 case IF_SCOPE:
5478 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5479 case FOR_SCOPE:
5480 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5481 case WHILE_SCOPE:
5482 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5483 case TRY_SCOPE:
5484 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5485 case NO_SCOPE:
5486 case BLOCK_SCOPE:
5487 break;
5488 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005489 vim_free(scope);
5490}
5491
5492/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493 * compile "if expr"
5494 *
5495 * "if expr" Produces instructions:
5496 * EVAL expr Push result of "expr"
5497 * JUMP_IF_FALSE end
5498 * ... body ...
5499 * end:
5500 *
5501 * "if expr | else" Produces instructions:
5502 * EVAL expr Push result of "expr"
5503 * JUMP_IF_FALSE else
5504 * ... body ...
5505 * JUMP_ALWAYS end
5506 * else:
5507 * ... body ...
5508 * end:
5509 *
5510 * "if expr1 | elseif expr2 | else" Produces instructions:
5511 * EVAL expr Push result of "expr"
5512 * JUMP_IF_FALSE elseif
5513 * ... body ...
5514 * JUMP_ALWAYS end
5515 * elseif:
5516 * EVAL expr Push result of "expr"
5517 * JUMP_IF_FALSE else
5518 * ... body ...
5519 * JUMP_ALWAYS end
5520 * else:
5521 * ... body ...
5522 * end:
5523 */
5524 static char_u *
5525compile_if(char_u *arg, cctx_T *cctx)
5526{
5527 char_u *p = arg;
5528 garray_T *instr = &cctx->ctx_instr;
5529 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005530 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005532 // compile "expr"; if we know it evaluates to FALSE skip the block
5533 tv.v_type = VAR_UNKNOWN;
5534 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5535 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5536 else
5537 cctx->ctx_skip = MAYBE;
5538 clear_tv(&tv);
5539 if (cctx->ctx_skip == MAYBE)
5540 {
5541 p = arg;
5542 if (compile_expr1(&p, cctx) == FAIL)
5543 return NULL;
5544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005545
5546 scope = new_scope(cctx, IF_SCOPE);
5547 if (scope == NULL)
5548 return NULL;
5549
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005550 if (cctx->ctx_skip == MAYBE)
5551 {
5552 // "where" is set when ":elseif", "else" or ":endif" is found
5553 scope->se_u.se_if.is_if_label = instr->ga_len;
5554 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5555 }
5556 else
5557 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005558
5559 return p;
5560}
5561
5562 static char_u *
5563compile_elseif(char_u *arg, cctx_T *cctx)
5564{
5565 char_u *p = arg;
5566 garray_T *instr = &cctx->ctx_instr;
5567 isn_T *isn;
5568 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005569 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005570
5571 if (scope == NULL || scope->se_type != IF_SCOPE)
5572 {
5573 emsg(_(e_elseif_without_if));
5574 return NULL;
5575 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005576 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005577
Bram Moolenaar158906c2020-02-06 20:39:45 +01005578 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005579 {
5580 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005582 return NULL;
5583 // previous "if" or "elseif" jumps here
5584 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5585 isn->isn_arg.jump.jump_where = instr->ga_len;
5586 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005587
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005588 // compile "expr"; if we know it evaluates to FALSE skip the block
5589 tv.v_type = VAR_UNKNOWN;
5590 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5591 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5592 else
5593 cctx->ctx_skip = MAYBE;
5594 clear_tv(&tv);
5595 if (cctx->ctx_skip == MAYBE)
5596 {
5597 p = arg;
5598 if (compile_expr1(&p, cctx) == FAIL)
5599 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005600
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005601 // "where" is set when ":elseif", "else" or ":endif" is found
5602 scope->se_u.se_if.is_if_label = instr->ga_len;
5603 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5604 }
5605 else
5606 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607
5608 return p;
5609}
5610
5611 static char_u *
5612compile_else(char_u *arg, cctx_T *cctx)
5613{
5614 char_u *p = arg;
5615 garray_T *instr = &cctx->ctx_instr;
5616 isn_T *isn;
5617 scope_T *scope = cctx->ctx_scope;
5618
5619 if (scope == NULL || scope->se_type != IF_SCOPE)
5620 {
5621 emsg(_(e_else_without_if));
5622 return NULL;
5623 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005624 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005626 // jump from previous block to the end, unless the else block is empty
5627 if (cctx->ctx_skip == MAYBE)
5628 {
5629 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005630 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005631 return NULL;
5632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633
Bram Moolenaar158906c2020-02-06 20:39:45 +01005634 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005635 {
5636 if (scope->se_u.se_if.is_if_label >= 0)
5637 {
5638 // previous "if" or "elseif" jumps here
5639 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5640 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005641 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005642 }
5643 }
5644
5645 if (cctx->ctx_skip != MAYBE)
5646 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647
5648 return p;
5649}
5650
5651 static char_u *
5652compile_endif(char_u *arg, cctx_T *cctx)
5653{
5654 scope_T *scope = cctx->ctx_scope;
5655 ifscope_T *ifscope;
5656 garray_T *instr = &cctx->ctx_instr;
5657 isn_T *isn;
5658
5659 if (scope == NULL || scope->se_type != IF_SCOPE)
5660 {
5661 emsg(_(e_endif_without_if));
5662 return NULL;
5663 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005664 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005665 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005667 if (scope->se_u.se_if.is_if_label >= 0)
5668 {
5669 // previous "if" or "elseif" jumps here
5670 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5671 isn->isn_arg.jump.jump_where = instr->ga_len;
5672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005673 // Fill in the "end" label in jumps at the end of the blocks.
5674 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005675 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005676
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005677 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005678 return arg;
5679}
5680
5681/*
5682 * compile "for var in expr"
5683 *
5684 * Produces instructions:
5685 * PUSHNR -1
5686 * STORE loop-idx Set index to -1
5687 * EVAL expr Push result of "expr"
5688 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5689 * - if beyond end, jump to "end"
5690 * - otherwise get item from list and push it
5691 * STORE var Store item in "var"
5692 * ... body ...
5693 * JUMP top Jump back to repeat
5694 * end: DROP Drop the result of "expr"
5695 *
5696 */
5697 static char_u *
5698compile_for(char_u *arg, cctx_T *cctx)
5699{
5700 char_u *p;
5701 size_t varlen;
5702 garray_T *instr = &cctx->ctx_instr;
5703 garray_T *stack = &cctx->ctx_type_stack;
5704 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005705 lvar_T *loop_lvar; // loop iteration variable
5706 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005707 type_T *vartype;
5708
5709 // TODO: list of variables: "for [key, value] in dict"
5710 // parse "var"
5711 for (p = arg; eval_isnamec1(*p); ++p)
5712 ;
5713 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005714 var_lvar = lookup_local(arg, varlen, cctx);
5715 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005716 {
5717 semsg(_("E1023: variable already defined: %s"), arg);
5718 return NULL;
5719 }
5720
5721 // consume "in"
5722 p = skipwhite(p);
5723 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5724 {
5725 emsg(_(e_missing_in));
5726 return NULL;
5727 }
5728 p = skipwhite(p + 2);
5729
5730
5731 scope = new_scope(cctx, FOR_SCOPE);
5732 if (scope == NULL)
5733 return NULL;
5734
5735 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005736 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5737 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005738 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005739 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005740 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005741 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005743
5744 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005745 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5746 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005747 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005748 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005749 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005750 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005752
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005753 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005754
5755 // compile "expr", it remains on the stack until "endfor"
5756 arg = p;
5757 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005758 {
5759 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005760 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005761 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005762
5763 // now we know the type of "var"
5764 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5765 if (vartype->tt_type != VAR_LIST)
5766 {
5767 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005768 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005769 return NULL;
5770 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005771 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005772 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005773
5774 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005775 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005776
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005777 generate_FOR(cctx, loop_lvar->lv_idx);
5778 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005779
5780 return arg;
5781}
5782
5783/*
5784 * compile "endfor"
5785 */
5786 static char_u *
5787compile_endfor(char_u *arg, cctx_T *cctx)
5788{
5789 garray_T *instr = &cctx->ctx_instr;
5790 scope_T *scope = cctx->ctx_scope;
5791 forscope_T *forscope;
5792 isn_T *isn;
5793
5794 if (scope == NULL || scope->se_type != FOR_SCOPE)
5795 {
5796 emsg(_(e_for));
5797 return NULL;
5798 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005799 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005800 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005801 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005802
5803 // At end of ":for" scope jump back to the FOR instruction.
5804 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5805
5806 // Fill in the "end" label in the FOR statement so it can jump here
5807 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5808 isn->isn_arg.forloop.for_end = instr->ga_len;
5809
5810 // Fill in the "end" label any BREAK statements
5811 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5812
5813 // Below the ":for" scope drop the "expr" list from the stack.
5814 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5815 return NULL;
5816
5817 vim_free(scope);
5818
5819 return arg;
5820}
5821
5822/*
5823 * compile "while expr"
5824 *
5825 * Produces instructions:
5826 * top: EVAL expr Push result of "expr"
5827 * JUMP_IF_FALSE end jump if false
5828 * ... body ...
5829 * JUMP top Jump back to repeat
5830 * end:
5831 *
5832 */
5833 static char_u *
5834compile_while(char_u *arg, cctx_T *cctx)
5835{
5836 char_u *p = arg;
5837 garray_T *instr = &cctx->ctx_instr;
5838 scope_T *scope;
5839
5840 scope = new_scope(cctx, WHILE_SCOPE);
5841 if (scope == NULL)
5842 return NULL;
5843
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005844 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005845
5846 // compile "expr"
5847 if (compile_expr1(&p, cctx) == FAIL)
5848 return NULL;
5849
5850 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005851 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852 JUMP_IF_FALSE, cctx) == FAIL)
5853 return FAIL;
5854
5855 return p;
5856}
5857
5858/*
5859 * compile "endwhile"
5860 */
5861 static char_u *
5862compile_endwhile(char_u *arg, cctx_T *cctx)
5863{
5864 scope_T *scope = cctx->ctx_scope;
5865
5866 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5867 {
5868 emsg(_(e_while));
5869 return NULL;
5870 }
5871 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005872 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005873
5874 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005875 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005876
5877 // Fill in the "end" label in the WHILE statement so it can jump here.
5878 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005879 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005880
5881 vim_free(scope);
5882
5883 return arg;
5884}
5885
5886/*
5887 * compile "continue"
5888 */
5889 static char_u *
5890compile_continue(char_u *arg, cctx_T *cctx)
5891{
5892 scope_T *scope = cctx->ctx_scope;
5893
5894 for (;;)
5895 {
5896 if (scope == NULL)
5897 {
5898 emsg(_(e_continue));
5899 return NULL;
5900 }
5901 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5902 break;
5903 scope = scope->se_outer;
5904 }
5905
5906 // Jump back to the FOR or WHILE instruction.
5907 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005908 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5909 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005910 return arg;
5911}
5912
5913/*
5914 * compile "break"
5915 */
5916 static char_u *
5917compile_break(char_u *arg, cctx_T *cctx)
5918{
5919 scope_T *scope = cctx->ctx_scope;
5920 endlabel_T **el;
5921
5922 for (;;)
5923 {
5924 if (scope == NULL)
5925 {
5926 emsg(_(e_break));
5927 return NULL;
5928 }
5929 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5930 break;
5931 scope = scope->se_outer;
5932 }
5933
5934 // Jump to the end of the FOR or WHILE loop.
5935 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005936 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005937 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005938 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005939 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5940 return FAIL;
5941
5942 return arg;
5943}
5944
5945/*
5946 * compile "{" start of block
5947 */
5948 static char_u *
5949compile_block(char_u *arg, cctx_T *cctx)
5950{
5951 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5952 return NULL;
5953 return skipwhite(arg + 1);
5954}
5955
5956/*
5957 * compile end of block: drop one scope
5958 */
5959 static void
5960compile_endblock(cctx_T *cctx)
5961{
5962 scope_T *scope = cctx->ctx_scope;
5963
5964 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005965 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005966 vim_free(scope);
5967}
5968
5969/*
5970 * compile "try"
5971 * Creates a new scope for the try-endtry, pointing to the first catch and
5972 * finally.
5973 * Creates another scope for the "try" block itself.
5974 * TRY instruction sets up exception handling at runtime.
5975 *
5976 * "try"
5977 * TRY -> catch1, -> finally push trystack entry
5978 * ... try block
5979 * "throw {exception}"
5980 * EVAL {exception}
5981 * THROW create exception
5982 * ... try block
5983 * " catch {expr}"
5984 * JUMP -> finally
5985 * catch1: PUSH exeception
5986 * EVAL {expr}
5987 * MATCH
5988 * JUMP nomatch -> catch2
5989 * CATCH remove exception
5990 * ... catch block
5991 * " catch"
5992 * JUMP -> finally
5993 * catch2: CATCH remove exception
5994 * ... catch block
5995 * " finally"
5996 * finally:
5997 * ... finally block
5998 * " endtry"
5999 * ENDTRY pop trystack entry, may rethrow
6000 */
6001 static char_u *
6002compile_try(char_u *arg, cctx_T *cctx)
6003{
6004 garray_T *instr = &cctx->ctx_instr;
6005 scope_T *try_scope;
6006 scope_T *scope;
6007
6008 // scope that holds the jumps that go to catch/finally/endtry
6009 try_scope = new_scope(cctx, TRY_SCOPE);
6010 if (try_scope == NULL)
6011 return NULL;
6012
6013 // "catch" is set when the first ":catch" is found.
6014 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006015 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016 if (generate_instr(cctx, ISN_TRY) == NULL)
6017 return NULL;
6018
6019 // scope for the try block itself
6020 scope = new_scope(cctx, BLOCK_SCOPE);
6021 if (scope == NULL)
6022 return NULL;
6023
6024 return arg;
6025}
6026
6027/*
6028 * compile "catch {expr}"
6029 */
6030 static char_u *
6031compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6032{
6033 scope_T *scope = cctx->ctx_scope;
6034 garray_T *instr = &cctx->ctx_instr;
6035 char_u *p;
6036 isn_T *isn;
6037
6038 // end block scope from :try or :catch
6039 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6040 compile_endblock(cctx);
6041 scope = cctx->ctx_scope;
6042
6043 // Error if not in a :try scope
6044 if (scope == NULL || scope->se_type != TRY_SCOPE)
6045 {
6046 emsg(_(e_catch));
6047 return NULL;
6048 }
6049
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006050 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006051 {
6052 emsg(_("E1033: catch unreachable after catch-all"));
6053 return NULL;
6054 }
6055
6056 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006057 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058 JUMP_ALWAYS, cctx) == FAIL)
6059 return NULL;
6060
6061 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006062 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063 if (isn->isn_arg.try.try_catch == 0)
6064 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006065 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066 {
6067 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006068 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006069 isn->isn_arg.jump.jump_where = instr->ga_len;
6070 }
6071
6072 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006073 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006075 scope->se_u.se_try.ts_caught_all = TRUE;
6076 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006077 }
6078 else
6079 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006080 char_u *end;
6081 char_u *pat;
6082 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006083 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006084 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086 // Push v:exception, push {expr} and MATCH
6087 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6088
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006089 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006090 if (*end != *p)
6091 {
6092 semsg(_("E1067: Separator mismatch: %s"), p);
6093 vim_free(tofree);
6094 return FAIL;
6095 }
6096 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006097 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006098 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006099 len = (int)(end - tofree);
6100 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006101 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006102 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006103 if (pat == NULL)
6104 return FAIL;
6105 if (generate_PUSHS(cctx, pat) == FAIL)
6106 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6109 return NULL;
6110
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006111 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6113 return NULL;
6114 }
6115
6116 if (generate_instr(cctx, ISN_CATCH) == NULL)
6117 return NULL;
6118
6119 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6120 return NULL;
6121 return p;
6122}
6123
6124 static char_u *
6125compile_finally(char_u *arg, cctx_T *cctx)
6126{
6127 scope_T *scope = cctx->ctx_scope;
6128 garray_T *instr = &cctx->ctx_instr;
6129 isn_T *isn;
6130
6131 // end block scope from :try or :catch
6132 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6133 compile_endblock(cctx);
6134 scope = cctx->ctx_scope;
6135
6136 // Error if not in a :try scope
6137 if (scope == NULL || scope->se_type != TRY_SCOPE)
6138 {
6139 emsg(_(e_finally));
6140 return NULL;
6141 }
6142
6143 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006144 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006145 if (isn->isn_arg.try.try_finally != 0)
6146 {
6147 emsg(_(e_finally_dup));
6148 return NULL;
6149 }
6150
6151 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006152 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006153
Bram Moolenaar585fea72020-04-02 22:33:21 +02006154 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006155 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156 {
6157 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006158 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006159 isn->isn_arg.jump.jump_where = instr->ga_len;
6160 }
6161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006162 // TODO: set index in ts_finally_label jumps
6163
6164 return arg;
6165}
6166
6167 static char_u *
6168compile_endtry(char_u *arg, cctx_T *cctx)
6169{
6170 scope_T *scope = cctx->ctx_scope;
6171 garray_T *instr = &cctx->ctx_instr;
6172 isn_T *isn;
6173
6174 // end block scope from :catch or :finally
6175 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6176 compile_endblock(cctx);
6177 scope = cctx->ctx_scope;
6178
6179 // Error if not in a :try scope
6180 if (scope == NULL || scope->se_type != TRY_SCOPE)
6181 {
6182 if (scope == NULL)
6183 emsg(_(e_no_endtry));
6184 else if (scope->se_type == WHILE_SCOPE)
6185 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006186 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187 emsg(_(e_endfor));
6188 else
6189 emsg(_(e_endif));
6190 return NULL;
6191 }
6192
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006193 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6195 {
6196 emsg(_("E1032: missing :catch or :finally"));
6197 return NULL;
6198 }
6199
6200 // Fill in the "end" label in jumps at the end of the blocks, if not done
6201 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006202 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006203
6204 // End :catch or :finally scope: set value in ISN_TRY instruction
6205 if (isn->isn_arg.try.try_finally == 0)
6206 isn->isn_arg.try.try_finally = instr->ga_len;
6207 compile_endblock(cctx);
6208
6209 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6210 return NULL;
6211 return arg;
6212}
6213
6214/*
6215 * compile "throw {expr}"
6216 */
6217 static char_u *
6218compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6219{
6220 char_u *p = skipwhite(arg);
6221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 if (compile_expr1(&p, cctx) == FAIL)
6223 return NULL;
6224 if (may_generate_2STRING(-1, cctx) == FAIL)
6225 return NULL;
6226 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6227 return NULL;
6228
6229 return p;
6230}
6231
6232/*
6233 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006234 * compile "echomsg expr"
6235 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006236 * compile "execute expr"
6237 */
6238 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006239compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006240{
6241 char_u *p = arg;
6242 int count = 0;
6243
6244 for (;;)
6245 {
6246 if (compile_expr1(&p, cctx) == FAIL)
6247 return NULL;
6248 ++count;
6249 p = skipwhite(p);
6250 if (ends_excmd(*p))
6251 break;
6252 }
6253
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006254 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6255 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6256 else if (cmdidx == CMD_execute)
6257 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6258 else if (cmdidx == CMD_echomsg)
6259 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6260 else
6261 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262 return p;
6263}
6264
6265/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006266 * A command that is not compiled, execute with legacy code.
6267 */
6268 static char_u *
6269compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6270{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006271 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006272 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006273
6274 if (cctx->ctx_skip == TRUE)
6275 goto theend;
6276
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006277 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6278 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006279 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6280 {
6281 // expand filename in "syntax include [@group] filename"
6282 has_expr = TRUE;
6283 eap->arg = skipwhite(eap->arg + 7);
6284 if (*eap->arg == '@')
6285 eap->arg = skiptowhite(eap->arg);
6286 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006287
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006288 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006289 {
6290 int count = 0;
6291 char_u *start = skipwhite(line);
6292
6293 // :cmd xxx`=expr1`yyy`=expr2`zzz
6294 // PUSHS ":cmd xxx"
6295 // eval expr1
6296 // PUSHS "yyy"
6297 // eval expr2
6298 // PUSHS "zzz"
6299 // EXECCONCAT 5
6300 for (;;)
6301 {
6302 if (p > start)
6303 {
6304 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
6305 ++count;
6306 }
6307 p += 2;
6308 if (compile_expr1(&p, cctx) == FAIL)
6309 return NULL;
6310 may_generate_2STRING(-1, cctx);
6311 ++count;
6312 p = skipwhite(p);
6313 if (*p != '`')
6314 {
6315 emsg(_("E1083: missing backtick"));
6316 return NULL;
6317 }
6318 start = p + 1;
6319
6320 p = (char_u *)strstr((char *)start, "`=");
6321 if (p == NULL)
6322 {
6323 if (*skipwhite(start) != NUL)
6324 {
6325 generate_PUSHS(cctx, vim_strsave(start));
6326 ++count;
6327 }
6328 break;
6329 }
6330 }
6331 generate_EXECCONCAT(cctx, count);
6332 }
6333 else
6334 generate_EXEC(cctx, line);
6335
6336theend:
6337 return (char_u *)"";
6338}
6339
6340/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006341 * After ex_function() has collected all the function lines: parse and compile
6342 * the lines into instructions.
6343 * Adds the function to "def_functions".
6344 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6345 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006346 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006347 * This can be used recursively through compile_lambda(), which may reallocate
6348 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006349 */
6350 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006351compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 char_u *line = NULL;
6354 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355 char *errormsg = NULL; // error message
6356 int had_return = FALSE;
6357 cctx_T cctx;
6358 garray_T *instr;
6359 int called_emsg_before = called_emsg;
6360 int ret = FAIL;
6361 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006362 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006364 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006365 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01006366
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006367 if (ufunc->uf_dfunc_idx >= 0)
6368 {
6369 // Redefining a function that was compiled before.
6370 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
6371
6372 // Free old instructions.
6373 delete_def_function_contents(dfunc);
6374 }
6375 else
6376 {
6377 // Add the function to "def_functions".
6378 if (ga_grow(&def_functions, 1) == FAIL)
6379 return;
6380 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006381 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006382 dfunc->df_idx = def_functions.ga_len;
6383 ufunc->uf_dfunc_idx = dfunc->df_idx;
6384 dfunc->df_ufunc = ufunc;
6385 ++def_functions.ga_len;
6386 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387 }
6388
Bram Moolenaara80faa82020-04-12 19:37:17 +02006389 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006390 cctx.ctx_ufunc = ufunc;
6391 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006392 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006393 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6394 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6395 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6396 cctx.ctx_type_list = &ufunc->uf_type_list;
6397 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6398 instr = &cctx.ctx_instr;
6399
6400 // Most modern script version.
6401 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6402
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006403 if (ufunc->uf_def_args.ga_len > 0)
6404 {
6405 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006406 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006407 int i;
6408 char_u *arg;
6409 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6410
6411 // Produce instructions for the default values of optional arguments.
6412 // Store the instruction index in uf_def_arg_idx[] so that we know
6413 // where to start when the function is called, depending on the number
6414 // of arguments.
6415 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6416 if (ufunc->uf_def_arg_idx == NULL)
6417 goto erret;
6418 for (i = 0; i < count; ++i)
6419 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006420 garray_T *stack = &cctx.ctx_type_stack;
6421 type_T *val_type;
6422 int arg_idx = first_def_arg + i;
6423
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006424 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6425 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006426 if (compile_expr1(&arg, &cctx) == FAIL)
6427 goto erret;
6428
6429 // If no type specified use the type of the default value.
6430 // Otherwise check that the default value type matches the
6431 // specified type.
6432 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6433 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6434 ufunc->uf_arg_types[arg_idx] = val_type;
6435 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6436 == FAIL)
6437 {
6438 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6439 arg_idx + 1);
6440 goto erret;
6441 }
6442
6443 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006444 goto erret;
6445 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006446 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6447 }
6448
6449 /*
6450 * Loop over all the lines of the function and generate instructions.
6451 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006452 for (;;)
6453 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006454 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006455 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006456
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006457 // Bail out on the first error to avoid a flood of errors and report
6458 // the right line number when inside try/catch.
6459 if (emsg_before != called_emsg)
6460 goto erret;
6461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006462 if (line != NULL && *line == '|')
6463 // the line continues after a '|'
6464 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006465 else if (line != NULL && *line != NUL
6466 && !(*line == '#' && (line == cctx.ctx_line_start
6467 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006469 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006470 goto erret;
6471 }
6472 else
6473 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006474 line = next_line_from_context(&cctx);
6475 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006476 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006478 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006479 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006480
6481 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006482 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483 ea.cmdlinep = &line;
6484 ea.cmd = skipwhite(line);
6485
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006486 // Some things can be recognized by the first character.
6487 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006489 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006490 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006491 if (ea.cmd[1] != '{')
6492 {
6493 line = (char_u *)"";
6494 continue;
6495 }
6496 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006498 case '}':
6499 {
6500 // "}" ends a block scope
6501 scopetype_T stype = cctx.ctx_scope == NULL
6502 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006503
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006504 if (stype == BLOCK_SCOPE)
6505 {
6506 compile_endblock(&cctx);
6507 line = ea.cmd;
6508 }
6509 else
6510 {
6511 emsg(_("E1025: using } outside of a block scope"));
6512 goto erret;
6513 }
6514 if (line != NULL)
6515 line = skipwhite(ea.cmd + 1);
6516 continue;
6517 }
6518
6519 case '{':
6520 // "{" starts a block scope
6521 // "{'a': 1}->func() is something else
6522 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6523 {
6524 line = compile_block(ea.cmd, &cctx);
6525 continue;
6526 }
6527 break;
6528
6529 case ':':
6530 is_ex_command = TRUE;
6531 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006532 }
6533
6534 /*
6535 * COMMAND MODIFIERS
6536 */
6537 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6538 {
6539 if (errormsg != NULL)
6540 goto erret;
6541 // empty line or comment
6542 line = (char_u *)"";
6543 continue;
6544 }
6545
6546 // Skip ":call" to get to the function name.
6547 if (checkforcmd(&ea.cmd, "call", 3))
6548 ea.cmd = skipwhite(ea.cmd);
6549
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006550 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006552 // Assuming the command starts with a variable or function name,
6553 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6554 // val".
6555 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6556 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006557 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006558 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006559 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006560 int oplen;
6561 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006563 oplen = assignment_len(skipwhite(p), &heredoc);
6564 if (oplen > 0)
6565 {
6566 // Recognize an assignment if we recognize the variable
6567 // name:
6568 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006569 // "local = expr" where "local" is a local var.
6570 // "script = expr" where "script" is a script-local var.
6571 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006572 // "&opt = expr"
6573 // "$ENV = expr"
6574 // "@r = expr"
6575 if (*ea.cmd == '&'
6576 || *ea.cmd == '$'
6577 || *ea.cmd == '@'
6578 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006579 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006580 || lookup_script(ea.cmd, p - ea.cmd) == OK
6581 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6582 {
6583 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6584 if (line == NULL)
6585 goto erret;
6586 continue;
6587 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588 }
6589 }
6590 }
6591
6592 /*
6593 * COMMAND after range
6594 */
6595 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006596 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6597 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006598 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599
6600 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6601 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006602 if (cctx.ctx_skip == TRUE)
6603 {
6604 line += STRLEN(line);
6605 continue;
6606 }
6607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 // Expression or function call.
6609 if (ea.cmdidx == CMD_eval)
6610 {
6611 p = ea.cmd;
6612 if (compile_expr1(&p, &cctx) == FAIL)
6613 goto erret;
6614
6615 // drop the return value
6616 generate_instr_drop(&cctx, ISN_DROP, 1);
6617 line = p;
6618 continue;
6619 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006620 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621 iemsg("Command from find_ex_command() not handled");
6622 goto erret;
6623 }
6624
6625 p = skipwhite(p);
6626
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006627 if (cctx.ctx_skip == TRUE
6628 && ea.cmdidx != CMD_elseif
6629 && ea.cmdidx != CMD_else
6630 && ea.cmdidx != CMD_endif)
6631 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006632 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006633 continue;
6634 }
6635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636 switch (ea.cmdidx)
6637 {
6638 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006639 ea.arg = p;
6640 line = compile_nested_function(&ea, &cctx);
6641 break;
6642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006644 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006645 goto erret;
6646
6647 case CMD_return:
6648 line = compile_return(p, set_return_type, &cctx);
6649 had_return = TRUE;
6650 break;
6651
6652 case CMD_let:
6653 case CMD_const:
6654 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6655 break;
6656
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006657 case CMD_unlet:
6658 case CMD_unlockvar:
6659 case CMD_lockvar:
6660 line = compile_unletlock(p, &ea, &cctx);
6661 break;
6662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663 case CMD_import:
6664 line = compile_import(p, &cctx);
6665 break;
6666
6667 case CMD_if:
6668 line = compile_if(p, &cctx);
6669 break;
6670 case CMD_elseif:
6671 line = compile_elseif(p, &cctx);
6672 break;
6673 case CMD_else:
6674 line = compile_else(p, &cctx);
6675 break;
6676 case CMD_endif:
6677 line = compile_endif(p, &cctx);
6678 break;
6679
6680 case CMD_while:
6681 line = compile_while(p, &cctx);
6682 break;
6683 case CMD_endwhile:
6684 line = compile_endwhile(p, &cctx);
6685 break;
6686
6687 case CMD_for:
6688 line = compile_for(p, &cctx);
6689 break;
6690 case CMD_endfor:
6691 line = compile_endfor(p, &cctx);
6692 break;
6693 case CMD_continue:
6694 line = compile_continue(p, &cctx);
6695 break;
6696 case CMD_break:
6697 line = compile_break(p, &cctx);
6698 break;
6699
6700 case CMD_try:
6701 line = compile_try(p, &cctx);
6702 break;
6703 case CMD_catch:
6704 line = compile_catch(p, &cctx);
6705 break;
6706 case CMD_finally:
6707 line = compile_finally(p, &cctx);
6708 break;
6709 case CMD_endtry:
6710 line = compile_endtry(p, &cctx);
6711 break;
6712 case CMD_throw:
6713 line = compile_throw(p, &cctx);
6714 break;
6715
6716 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006718 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006719 case CMD_echomsg:
6720 case CMD_echoerr:
6721 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006722 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723
6724 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006725 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006726 // Not recognized, execute with do_cmdline_cmd().
6727 ea.arg = p;
6728 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729 break;
6730 }
6731 if (line == NULL)
6732 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006733 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734
6735 if (cctx.ctx_type_stack.ga_len < 0)
6736 {
6737 iemsg("Type stack underflow");
6738 goto erret;
6739 }
6740 }
6741
6742 if (cctx.ctx_scope != NULL)
6743 {
6744 if (cctx.ctx_scope->se_type == IF_SCOPE)
6745 emsg(_(e_endif));
6746 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6747 emsg(_(e_endwhile));
6748 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6749 emsg(_(e_endfor));
6750 else
6751 emsg(_("E1026: Missing }"));
6752 goto erret;
6753 }
6754
6755 if (!had_return)
6756 {
6757 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6758 {
6759 emsg(_("E1027: Missing return statement"));
6760 goto erret;
6761 }
6762
6763 // Return zero if there is no return at the end.
6764 generate_PUSHNR(&cctx, 0);
6765 generate_instr(&cctx, ISN_RETURN);
6766 }
6767
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006768 {
6769 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6770 + ufunc->uf_dfunc_idx;
6771 dfunc->df_deleted = FALSE;
6772 dfunc->df_instr = instr->ga_data;
6773 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006774 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006775 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006776 if (cctx.ctx_outer_used)
6777 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006778 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006780 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006781 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006782 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006783
6784 // Create a type for the function, with the return type and any
6785 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006786 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6787 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006788 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006789 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006790 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6791 argcount, &ufunc->uf_type_list);
6792 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006793 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006794 argcount + varargs,
6795 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006796 {
6797 ret = FAIL;
6798 goto erret;
6799 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006800 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6801 ufunc->uf_func_type->tt_min_argcount =
6802 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006803 if (ufunc->uf_arg_types == NULL)
6804 {
6805 int i;
6806
6807 // lambda does not have argument types.
6808 for (i = 0; i < argcount; ++i)
6809 ufunc->uf_func_type->tt_args[i] = &t_any;
6810 }
6811 else
6812 mch_memmove(ufunc->uf_func_type->tt_args,
6813 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006814 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006815 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006816 ufunc->uf_func_type->tt_args[argcount] =
6817 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006818 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6819 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006820 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006821 else
6822 // No arguments, can use a predefined type.
6823 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6824 argcount, &ufunc->uf_type_list);
6825
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006826 }
6827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006828 ret = OK;
6829
6830erret:
6831 if (ret == FAIL)
6832 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006833 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006834 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6835 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006836
6837 for (idx = 0; idx < instr->ga_len; ++idx)
6838 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006841 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006842 if (!dfunc->df_deleted)
6843 --def_functions.ga_len;
6844
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006845 while (cctx.ctx_scope != NULL)
6846 drop_scope(&cctx);
6847
Bram Moolenaar20431c92020-03-20 18:39:46 +01006848 // Don't execute this function body.
6849 ga_clear_strings(&ufunc->uf_lines);
6850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006851 if (errormsg != NULL)
6852 emsg(errormsg);
6853 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006854 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006855 }
6856
6857 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006858 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006859 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006861}
6862
6863/*
6864 * Delete an instruction, free what it contains.
6865 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006866 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006867delete_instr(isn_T *isn)
6868{
6869 switch (isn->isn_type)
6870 {
6871 case ISN_EXEC:
6872 case ISN_LOADENV:
6873 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006874 case ISN_LOADB:
6875 case ISN_LOADW:
6876 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006877 case ISN_LOADOPT:
6878 case ISN_MEMBER:
6879 case ISN_PUSHEXC:
6880 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006881 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006883 case ISN_STOREB:
6884 case ISN_STOREW:
6885 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006886 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887 vim_free(isn->isn_arg.string);
6888 break;
6889
6890 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006891 case ISN_STORES:
6892 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006893 break;
6894
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006895 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006896 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006897 vim_free(isn->isn_arg.unlet.ul_name);
6898 break;
6899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900 case ISN_STOREOPT:
6901 vim_free(isn->isn_arg.storeopt.so_name);
6902 break;
6903
6904 case ISN_PUSHBLOB: // push blob isn_arg.blob
6905 blob_unref(isn->isn_arg.blob);
6906 break;
6907
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006908 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006909#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006910 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006911#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006912 break;
6913
6914 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006915#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006916 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006917#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006918 break;
6919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006920 case ISN_UCALL:
6921 vim_free(isn->isn_arg.ufunc.cuf_name);
6922 break;
6923
Bram Moolenaar221fcc72020-05-05 19:46:20 +02006924 case ISN_FUNCREF:
6925 {
6926 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6927 + isn->isn_arg.funcref.fr_func;
6928 func_ptr_unref(dfunc->df_ufunc);
6929 }
6930 break;
6931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932 case ISN_2BOOL:
6933 case ISN_2STRING:
6934 case ISN_ADDBLOB:
6935 case ISN_ADDLIST:
6936 case ISN_BCALL:
6937 case ISN_CATCH:
6938 case ISN_CHECKNR:
6939 case ISN_CHECKTYPE:
6940 case ISN_COMPAREANY:
6941 case ISN_COMPAREBLOB:
6942 case ISN_COMPAREBOOL:
6943 case ISN_COMPAREDICT:
6944 case ISN_COMPAREFLOAT:
6945 case ISN_COMPAREFUNC:
6946 case ISN_COMPARELIST:
6947 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 case ISN_COMPARESPECIAL:
6949 case ISN_COMPARESTRING:
6950 case ISN_CONCAT:
6951 case ISN_DCALL:
6952 case ISN_DROP:
6953 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006954 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006955 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006957 case ISN_EXECCONCAT:
6958 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006960 case ISN_INDEX:
6961 case ISN_JUMP:
6962 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006963 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006964 case ISN_LOADSCRIPT:
6965 case ISN_LOADREG:
6966 case ISN_LOADV:
6967 case ISN_NEGATENR:
6968 case ISN_NEWDICT:
6969 case ISN_NEWLIST:
6970 case ISN_OPNR:
6971 case ISN_OPFLOAT:
6972 case ISN_OPANY:
6973 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006974 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975 case ISN_PUSHF:
6976 case ISN_PUSHNR:
6977 case ISN_PUSHBOOL:
6978 case ISN_PUSHSPEC:
6979 case ISN_RETURN:
6980 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02006981 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006982 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006984 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006985 case ISN_STORESCRIPT:
6986 case ISN_THROW:
6987 case ISN_TRY:
6988 // nothing allocated
6989 break;
6990 }
6991}
6992
6993/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006994 * Free all instructions for "dfunc".
6995 */
6996 static void
6997delete_def_function_contents(dfunc_T *dfunc)
6998{
6999 int idx;
7000
7001 ga_clear(&dfunc->df_def_args_isn);
7002
7003 if (dfunc->df_instr != NULL)
7004 {
7005 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7006 delete_instr(dfunc->df_instr + idx);
7007 VIM_CLEAR(dfunc->df_instr);
7008 }
7009
7010 dfunc->df_deleted = TRUE;
7011}
7012
7013/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014 * When a user function is deleted, delete any associated def function.
7015 */
7016 void
7017delete_def_function(ufunc_T *ufunc)
7018{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 if (ufunc->uf_dfunc_idx >= 0)
7020 {
7021 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7022 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023
Bram Moolenaar20431c92020-03-20 18:39:46 +01007024 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 }
7026}
7027
7028#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007029/*
7030 * Free all functions defined with ":def".
7031 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032 void
7033free_def_functions(void)
7034{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007035 int idx;
7036
7037 for (idx = 0; idx < def_functions.ga_len; ++idx)
7038 {
7039 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7040
7041 delete_def_function_contents(dfunc);
7042 }
7043
7044 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045}
7046#endif
7047
7048
7049#endif // FEAT_EVAL