blob: 2bfc4d0fe68f81009c334778ada9dfa4cefac7e2 [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");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200138static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200139static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140
Bram Moolenaar20431c92020-03-20 18:39:46 +0100141static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200142static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143
144/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200145 * Lookup variable "name" in the local scope and return it.
146 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200148 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149lookup_local(char_u *name, size_t len, cctx_T *cctx)
150{
151 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200152 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100154 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200155 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156
157 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
159 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161 if (STRNCMP(name, lvar->lv_name, len) == 0
162 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200163 {
164 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200165 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200168
169 // Find local in outer function scope.
170 if (cctx->ctx_outer != NULL)
171 {
172 lvar = lookup_local(name, len, cctx->ctx_outer);
173 if (lvar != NULL)
174 {
175 // TODO: are there situations we should not mark the outer scope as
176 // used?
177 cctx->ctx_outer_used = TRUE;
178 lvar->lv_from_outer = TRUE;
179 return lvar;
180 }
181 }
182
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200183 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100184}
185
186/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200187 * Lookup an argument in the current function and an enclosing function.
188 * Returns the argument index in "idxp"
189 * Returns the argument type in "type"
190 * Sets "gen_load_outer" to TRUE if found in outer scope.
191 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192 */
193 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200194lookup_arg(
195 char_u *name,
196 size_t len,
197 int *idxp,
198 type_T **type,
199 int *gen_load_outer,
200 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100201{
202 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200203 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100205 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200206 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
208 {
209 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
210
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200211 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
212 {
213 if (idxp != NULL)
214 {
215 // Arguments are located above the frame pointer. One further
216 // if there is a vararg argument
217 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
218 + STACK_FRAME_SIZE)
219 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
220
221 if (cctx->ctx_ufunc->uf_arg_types != NULL)
222 *type = cctx->ctx_ufunc->uf_arg_types[idx];
223 else
224 *type = &t_any;
225 }
226 return OK;
227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200230 va_name = cctx->ctx_ufunc->uf_va_name;
231 if (va_name != NULL
232 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
233 {
234 if (idxp != NULL)
235 {
236 // varargs is always the last argument
237 *idxp = -STACK_FRAME_SIZE - 1;
238 *type = cctx->ctx_ufunc->uf_va_type;
239 }
240 return OK;
241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200243 if (cctx->ctx_outer != NULL)
244 {
245 // Lookup the name for an argument of the outer function.
246 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
247 == OK)
248 {
249 *gen_load_outer = TRUE;
250 return OK;
251 }
252 }
253
254 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255}
256
257/*
258 * Lookup a variable in the current script.
259 * Returns OK or FAIL.
260 */
261 static int
262lookup_script(char_u *name, size_t len)
263{
264 int cc;
265 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
266 dictitem_T *di;
267
268 cc = name[len];
269 name[len] = NUL;
270 di = find_var_in_ht(ht, 0, name, TRUE);
271 name[len] = cc;
272 return di == NULL ? FAIL: OK;
273}
274
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100275/*
276 * Check if "p[len]" is already defined, either in script "import_sid" or in
277 * compilation context "cctx".
278 * Return FAIL and give an error if it defined.
279 */
280 int
281check_defined(char_u *p, int len, cctx_T *cctx)
282{
283 if (lookup_script(p, len) == OK
284 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200285 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100286 || find_imported(p, len, cctx) != NULL)))
287 {
288 semsg("E1073: imported name already defined: %s", p);
289 return FAIL;
290 }
291 return OK;
292}
293
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200294/*
295 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
296 * be freed later.
297 */
298 static type_T *
299alloc_type(garray_T *type_gap)
300{
301 type_T *type;
302
303 if (ga_grow(type_gap, 1) == FAIL)
304 return NULL;
305 type = ALLOC_CLEAR_ONE(type_T);
306 if (type != NULL)
307 {
308 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
309 ++type_gap->ga_len;
310 }
311 return type;
312}
313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200315get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100316{
317 type_T *type;
318
319 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200320 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200322 if (member_type->tt_type == VAR_VOID
323 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100324 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100325 if (member_type->tt_type == VAR_BOOL)
326 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100327 if (member_type->tt_type == VAR_NUMBER)
328 return &t_list_number;
329 if (member_type->tt_type == VAR_STRING)
330 return &t_list_string;
331
332 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200333 type = alloc_type(type_gap);
334 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100335 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 type->tt_type = VAR_LIST;
337 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200338 type->tt_argcount = 0;
339 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340 return type;
341}
342
343 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200344get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100345{
346 type_T *type;
347
348 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200349 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200351 if (member_type->tt_type == VAR_VOID
352 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100353 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100354 if (member_type->tt_type == VAR_BOOL)
355 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100356 if (member_type->tt_type == VAR_NUMBER)
357 return &t_dict_number;
358 if (member_type->tt_type == VAR_STRING)
359 return &t_dict_string;
360
361 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200362 type = alloc_type(type_gap);
363 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100364 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100365 type->tt_type = VAR_DICT;
366 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200367 type->tt_argcount = 0;
368 type->tt_args = NULL;
369 return type;
370}
371
372/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200373 * Allocate a new type for a function.
374 */
375 static type_T *
376alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
377{
378 type_T *type = alloc_type(type_gap);
379
380 if (type == NULL)
381 return &t_any;
382 type->tt_type = VAR_FUNC;
383 type->tt_member = ret_type;
384 type->tt_argcount = argcount;
385 type->tt_args = NULL;
386 return type;
387}
388
389/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200390 * Get a function type, based on the return type "ret_type".
391 * If "argcount" is -1 or 0 a predefined type can be used.
392 * If "argcount" > 0 always create a new type, so that arguments can be added.
393 */
394 static type_T *
395get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
396{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200397 // recognize commonly used types
398 if (argcount <= 0)
399 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200400 if (ret_type == &t_unknown)
401 {
402 // (argcount == 0) is not possible
403 return &t_func_unknown;
404 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200405 if (ret_type == &t_void)
406 {
407 if (argcount == 0)
408 return &t_func_0_void;
409 else
410 return &t_func_void;
411 }
412 if (ret_type == &t_any)
413 {
414 if (argcount == 0)
415 return &t_func_0_any;
416 else
417 return &t_func_any;
418 }
419 if (ret_type == &t_number)
420 {
421 if (argcount == 0)
422 return &t_func_0_number;
423 else
424 return &t_func_number;
425 }
426 if (ret_type == &t_string)
427 {
428 if (argcount == 0)
429 return &t_func_0_string;
430 else
431 return &t_func_string;
432 }
433 }
434
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200435 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436}
437
Bram Moolenaara8c17702020-04-01 21:17:24 +0200438/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200439 * For a function type, reserve space for "argcount" argument types (including
440 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200441 */
442 static int
443func_type_add_arg_types(
444 type_T *functype,
445 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200446 garray_T *type_gap)
447{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200448 // To make it easy to free the space needed for the argument types, add the
449 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200450 if (ga_grow(type_gap, 1) == FAIL)
451 return FAIL;
452 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
453 if (functype->tt_args == NULL)
454 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200455 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
456 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200457 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200458 return OK;
459}
460
461/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200462 * Return the type_T for a typval. Only for primitive types.
463 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200464 type_T *
Bram Moolenaara8c17702020-04-01 21:17:24 +0200465typval2type(typval_T *tv)
466{
467 if (tv->v_type == VAR_NUMBER)
468 return &t_number;
469 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200470 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200471 if (tv->v_type == VAR_STRING)
472 return &t_string;
473 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
474 return &t_list_string;
475 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
476 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200477 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200478}
479
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200480 static void
481type_mismatch(type_T *expected, type_T *actual)
482{
483 char *tofree1, *tofree2;
484
485 semsg(_("E1013: type mismatch, expected %s but got %s"),
486 type_name(expected, &tofree1), type_name(actual, &tofree2));
487 vim_free(tofree1);
488 vim_free(tofree2);
489}
490
491 static void
492arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
493{
494 char *tofree1, *tofree2;
495
496 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
497 argidx,
498 type_name(expected, &tofree1), type_name(actual, &tofree2));
499 vim_free(tofree1);
500 vim_free(tofree2);
501}
502
503/*
504 * Check if the expected and actual types match.
505 * Does not allow for assigning "any" to a specific type.
506 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200507 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200508check_type(type_T *expected, type_T *actual, int give_msg)
509{
510 int ret = OK;
511
512 // When expected is "unknown" we accept any actual type.
513 // When expected is "any" we accept any actual type except "void".
514 if (expected->tt_type != VAR_UNKNOWN
515 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
516
517 {
518 if (expected->tt_type != actual->tt_type)
519 {
520 if (give_msg)
521 type_mismatch(expected, actual);
522 return FAIL;
523 }
524 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
525 {
526 // "unknown" is used for an empty list or dict
527 if (actual->tt_member != &t_unknown)
528 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
529 }
530 else if (expected->tt_type == VAR_FUNC)
531 {
532 if (expected->tt_member != &t_unknown)
533 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
534 if (ret == OK && expected->tt_argcount != -1
535 && (actual->tt_argcount < expected->tt_min_argcount
536 || actual->tt_argcount > expected->tt_argcount))
537 ret = FAIL;
538 }
539 if (ret == FAIL && give_msg)
540 type_mismatch(expected, actual);
541 }
542 return ret;
543}
544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545/////////////////////////////////////////////////////////////////////
546// Following generate_ functions expect the caller to call ga_grow().
547
Bram Moolenaar080457c2020-03-03 21:53:32 +0100548#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
549#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551/*
552 * Generate an instruction without arguments.
553 * Returns a pointer to the new instruction, NULL if failed.
554 */
555 static isn_T *
556generate_instr(cctx_T *cctx, isntype_T isn_type)
557{
558 garray_T *instr = &cctx->ctx_instr;
559 isn_T *isn;
560
Bram Moolenaar080457c2020-03-03 21:53:32 +0100561 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 if (ga_grow(instr, 1) == FAIL)
563 return NULL;
564 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
565 isn->isn_type = isn_type;
566 isn->isn_lnum = cctx->ctx_lnum + 1;
567 ++instr->ga_len;
568
569 return isn;
570}
571
572/*
573 * Generate an instruction without arguments.
574 * "drop" will be removed from the stack.
575 * Returns a pointer to the new instruction, NULL if failed.
576 */
577 static isn_T *
578generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
579{
580 garray_T *stack = &cctx->ctx_type_stack;
581
Bram Moolenaar080457c2020-03-03 21:53:32 +0100582 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 stack->ga_len -= drop;
584 return generate_instr(cctx, isn_type);
585}
586
587/*
588 * Generate instruction "isn_type" and put "type" on the type stack.
589 */
590 static isn_T *
591generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
592{
593 isn_T *isn;
594 garray_T *stack = &cctx->ctx_type_stack;
595
596 if ((isn = generate_instr(cctx, isn_type)) == NULL)
597 return NULL;
598
599 if (ga_grow(stack, 1) == FAIL)
600 return NULL;
601 ((type_T **)stack->ga_data)[stack->ga_len] = type;
602 ++stack->ga_len;
603
604 return isn;
605}
606
607/*
608 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
609 */
610 static int
611may_generate_2STRING(int offset, cctx_T *cctx)
612{
613 isn_T *isn;
614 garray_T *stack = &cctx->ctx_type_stack;
615 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
616
617 if ((*type)->tt_type == VAR_STRING)
618 return OK;
619 *type = &t_string;
620
621 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
622 return FAIL;
623 isn->isn_arg.number = offset;
624
625 return OK;
626}
627
628 static int
629check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
630{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200631 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200633 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 {
635 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100636 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 else
638 semsg(_("E1036: %c requires number or float arguments"), *op);
639 return FAIL;
640 }
641 return OK;
642}
643
644/*
645 * Generate an instruction with two arguments. The instruction depends on the
646 * type of the arguments.
647 */
648 static int
649generate_two_op(cctx_T *cctx, char_u *op)
650{
651 garray_T *stack = &cctx->ctx_type_stack;
652 type_T *type1;
653 type_T *type2;
654 vartype_T vartype;
655 isn_T *isn;
656
Bram Moolenaar080457c2020-03-03 21:53:32 +0100657 RETURN_OK_IF_SKIP(cctx);
658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659 // Get the known type of the two items on the stack. If they are matching
660 // use a type-specific instruction. Otherwise fall back to runtime type
661 // checking.
662 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
663 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200664 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 if (type1->tt_type == type2->tt_type
666 && (type1->tt_type == VAR_NUMBER
667 || type1->tt_type == VAR_LIST
668#ifdef FEAT_FLOAT
669 || type1->tt_type == VAR_FLOAT
670#endif
671 || type1->tt_type == VAR_BLOB))
672 vartype = type1->tt_type;
673
674 switch (*op)
675 {
676 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200677 && type1->tt_type != VAR_ANY
678 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 && check_number_or_float(
680 type1->tt_type, type2->tt_type, op) == FAIL)
681 return FAIL;
682 isn = generate_instr_drop(cctx,
683 vartype == VAR_NUMBER ? ISN_OPNR
684 : vartype == VAR_LIST ? ISN_ADDLIST
685 : vartype == VAR_BLOB ? ISN_ADDBLOB
686#ifdef FEAT_FLOAT
687 : vartype == VAR_FLOAT ? ISN_OPFLOAT
688#endif
689 : ISN_OPANY, 1);
690 if (isn != NULL)
691 isn->isn_arg.op.op_type = EXPR_ADD;
692 break;
693
694 case '-':
695 case '*':
696 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
697 op) == FAIL)
698 return FAIL;
699 if (vartype == VAR_NUMBER)
700 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
701#ifdef FEAT_FLOAT
702 else if (vartype == VAR_FLOAT)
703 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
704#endif
705 else
706 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
707 if (isn != NULL)
708 isn->isn_arg.op.op_type = *op == '*'
709 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
710 break;
711
Bram Moolenaar4c683752020-04-05 21:38:23 +0200712 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200714 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 && type2->tt_type != VAR_NUMBER))
716 {
717 emsg(_("E1035: % requires number arguments"));
718 return FAIL;
719 }
720 isn = generate_instr_drop(cctx,
721 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
722 if (isn != NULL)
723 isn->isn_arg.op.op_type = EXPR_REM;
724 break;
725 }
726
727 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200728 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729 {
730 type_T *type = &t_any;
731
732#ifdef FEAT_FLOAT
733 // float+number and number+float results in float
734 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
735 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
736 type = &t_float;
737#endif
738 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
739 }
740
741 return OK;
742}
743
744/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200745 * Get the instruction to use for comparing "type1" with "type2"
746 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200748 static isntype_T
749get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750{
751 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752
Bram Moolenaar4c683752020-04-05 21:38:23 +0200753 if (type1 == VAR_UNKNOWN)
754 type1 = VAR_ANY;
755 if (type2 == VAR_UNKNOWN)
756 type2 = VAR_ANY;
757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 if (type1 == type2)
759 {
760 switch (type1)
761 {
762 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
763 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
764 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
765 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
766 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
767 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
768 case VAR_LIST: isntype = ISN_COMPARELIST; break;
769 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
770 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771 default: isntype = ISN_COMPAREANY; break;
772 }
773 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200774 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
776 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
777 isntype = ISN_COMPAREANY;
778
779 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
780 && (isntype == ISN_COMPAREBOOL
781 || isntype == ISN_COMPARESPECIAL
782 || isntype == ISN_COMPARENR
783 || isntype == ISN_COMPAREFLOAT))
784 {
785 semsg(_("E1037: Cannot use \"%s\" with %s"),
786 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200787 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788 }
789 if (isntype == ISN_DROP
790 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
791 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
792 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
793 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
794 && exptype != EXPR_IS && exptype != EXPR_ISNOT
795 && (type1 == VAR_BLOB || type2 == VAR_BLOB
796 || type1 == VAR_LIST || type2 == VAR_LIST))))
797 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100798 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200800 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200802 return isntype;
803}
804
805/*
806 * Generate an ISN_COMPARE* instruction with a boolean result.
807 */
808 static int
809generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
810{
811 isntype_T isntype;
812 isn_T *isn;
813 garray_T *stack = &cctx->ctx_type_stack;
814 vartype_T type1;
815 vartype_T type2;
816
817 RETURN_OK_IF_SKIP(cctx);
818
819 // Get the known type of the two items on the stack. If they are matching
820 // use a type-specific instruction. Otherwise fall back to runtime type
821 // checking.
822 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
823 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
824 isntype = get_compare_isn(exptype, type1, type2);
825 if (isntype == ISN_DROP)
826 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827
828 if ((isn = generate_instr(cctx, isntype)) == NULL)
829 return FAIL;
830 isn->isn_arg.op.op_type = exptype;
831 isn->isn_arg.op.op_ic = ic;
832
833 // takes two arguments, puts one bool back
834 if (stack->ga_len >= 2)
835 {
836 --stack->ga_len;
837 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
838 }
839
840 return OK;
841}
842
843/*
844 * Generate an ISN_2BOOL instruction.
845 */
846 static int
847generate_2BOOL(cctx_T *cctx, int invert)
848{
849 isn_T *isn;
850 garray_T *stack = &cctx->ctx_type_stack;
851
Bram Moolenaar080457c2020-03-03 21:53:32 +0100852 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
854 return FAIL;
855 isn->isn_arg.number = invert;
856
857 // type becomes bool
858 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
859
860 return OK;
861}
862
863 static int
864generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
865{
866 isn_T *isn;
867 garray_T *stack = &cctx->ctx_type_stack;
868
Bram Moolenaar080457c2020-03-03 21:53:32 +0100869 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
871 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200872 // TODO: whole type, e.g. for a function also arg and return types
873 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 isn->isn_arg.type.ct_off = offset;
875
876 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200877 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878
879 return OK;
880}
881
882/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200883 * Check that
884 * - "actual" is "expected" type or
885 * - "actual" is a type that can be "expected" type: add a runtime check; or
886 * - return FAIL.
887 */
888 static int
889need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
890{
891 if (check_type(expected, actual, FALSE) == OK)
892 return OK;
893 if (actual->tt_type != VAR_ANY
894 && actual->tt_type != VAR_UNKNOWN
895 && !(actual->tt_type == VAR_FUNC
896 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
897 {
898 type_mismatch(expected, actual);
899 return FAIL;
900 }
901 generate_TYPECHECK(cctx, expected, offset);
902 return OK;
903}
904
905/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 * Generate an ISN_PUSHNR instruction.
907 */
908 static int
909generate_PUSHNR(cctx_T *cctx, varnumber_T number)
910{
911 isn_T *isn;
912
Bram Moolenaar080457c2020-03-03 21:53:32 +0100913 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
915 return FAIL;
916 isn->isn_arg.number = number;
917
918 return OK;
919}
920
921/*
922 * Generate an ISN_PUSHBOOL instruction.
923 */
924 static int
925generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
926{
927 isn_T *isn;
928
Bram Moolenaar080457c2020-03-03 21:53:32 +0100929 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
931 return FAIL;
932 isn->isn_arg.number = number;
933
934 return OK;
935}
936
937/*
938 * Generate an ISN_PUSHSPEC instruction.
939 */
940 static int
941generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
942{
943 isn_T *isn;
944
Bram Moolenaar080457c2020-03-03 21:53:32 +0100945 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
947 return FAIL;
948 isn->isn_arg.number = number;
949
950 return OK;
951}
952
953#ifdef FEAT_FLOAT
954/*
955 * Generate an ISN_PUSHF instruction.
956 */
957 static int
958generate_PUSHF(cctx_T *cctx, float_T fnumber)
959{
960 isn_T *isn;
961
Bram Moolenaar080457c2020-03-03 21:53:32 +0100962 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
964 return FAIL;
965 isn->isn_arg.fnumber = fnumber;
966
967 return OK;
968}
969#endif
970
971/*
972 * Generate an ISN_PUSHS instruction.
973 * Consumes "str".
974 */
975 static int
976generate_PUSHS(cctx_T *cctx, char_u *str)
977{
978 isn_T *isn;
979
Bram Moolenaar080457c2020-03-03 21:53:32 +0100980 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
982 return FAIL;
983 isn->isn_arg.string = str;
984
985 return OK;
986}
987
988/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100989 * Generate an ISN_PUSHCHANNEL instruction.
990 * Consumes "channel".
991 */
992 static int
993generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
994{
995 isn_T *isn;
996
Bram Moolenaar080457c2020-03-03 21:53:32 +0100997 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100998 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
999 return FAIL;
1000 isn->isn_arg.channel = channel;
1001
1002 return OK;
1003}
1004
1005/*
1006 * Generate an ISN_PUSHJOB instruction.
1007 * Consumes "job".
1008 */
1009 static int
1010generate_PUSHJOB(cctx_T *cctx, job_T *job)
1011{
1012 isn_T *isn;
1013
Bram Moolenaar080457c2020-03-03 21:53:32 +01001014 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001015 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001016 return FAIL;
1017 isn->isn_arg.job = job;
1018
1019 return OK;
1020}
1021
1022/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 * Generate an ISN_PUSHBLOB instruction.
1024 * Consumes "blob".
1025 */
1026 static int
1027generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1028{
1029 isn_T *isn;
1030
Bram Moolenaar080457c2020-03-03 21:53:32 +01001031 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1033 return FAIL;
1034 isn->isn_arg.blob = blob;
1035
1036 return OK;
1037}
1038
1039/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001040 * Generate an ISN_PUSHFUNC instruction with name "name".
1041 * Consumes "name".
1042 */
1043 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001044generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001045{
1046 isn_T *isn;
1047
Bram Moolenaar080457c2020-03-03 21:53:32 +01001048 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001049 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001050 return FAIL;
1051 isn->isn_arg.string = name;
1052
1053 return OK;
1054}
1055
1056/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001057 * Generate an ISN_GETITEM instruction with "index".
1058 */
1059 static int
1060generate_GETITEM(cctx_T *cctx, int index)
1061{
1062 isn_T *isn;
1063 garray_T *stack = &cctx->ctx_type_stack;
1064 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1065 type_T *item_type = &t_any;
1066
1067 RETURN_OK_IF_SKIP(cctx);
1068
1069 if (type->tt_type == VAR_LIST)
1070 item_type = type->tt_member;
1071 else if (type->tt_type != VAR_ANY)
1072 {
1073 emsg(_(e_listreq));
1074 return FAIL;
1075 }
1076 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1077 return FAIL;
1078 isn->isn_arg.number = index;
1079
1080 // add the item type to the type stack
1081 if (ga_grow(stack, 1) == FAIL)
1082 return FAIL;
1083 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1084 ++stack->ga_len;
1085 return OK;
1086}
1087
1088/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001089 * Generate an ISN_SLICE instruction with "count".
1090 */
1091 static int
1092generate_SLICE(cctx_T *cctx, int count)
1093{
1094 isn_T *isn;
1095
1096 RETURN_OK_IF_SKIP(cctx);
1097 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1098 return FAIL;
1099 isn->isn_arg.number = count;
1100 return OK;
1101}
1102
1103/*
1104 * Generate an ISN_CHECKLEN instruction with "min_len".
1105 */
1106 static int
1107generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1108{
1109 isn_T *isn;
1110
1111 RETURN_OK_IF_SKIP(cctx);
1112
1113 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1114 return FAIL;
1115 isn->isn_arg.checklen.cl_min_len = min_len;
1116 isn->isn_arg.checklen.cl_more_OK = more_OK;
1117
1118 return OK;
1119}
1120
1121/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 * Generate an ISN_STORE instruction.
1123 */
1124 static int
1125generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1126{
1127 isn_T *isn;
1128
Bram Moolenaar080457c2020-03-03 21:53:32 +01001129 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1131 return FAIL;
1132 if (name != NULL)
1133 isn->isn_arg.string = vim_strsave(name);
1134 else
1135 isn->isn_arg.number = idx;
1136
1137 return OK;
1138}
1139
1140/*
1141 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1142 */
1143 static int
1144generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1145{
1146 isn_T *isn;
1147
Bram Moolenaar080457c2020-03-03 21:53:32 +01001148 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1150 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001151 isn->isn_arg.storenr.stnr_idx = idx;
1152 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153
1154 return OK;
1155}
1156
1157/*
1158 * Generate an ISN_STOREOPT instruction
1159 */
1160 static int
1161generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1162{
1163 isn_T *isn;
1164
Bram Moolenaar080457c2020-03-03 21:53:32 +01001165 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1167 return FAIL;
1168 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1169 isn->isn_arg.storeopt.so_flags = opt_flags;
1170
1171 return OK;
1172}
1173
1174/*
1175 * Generate an ISN_LOAD or similar instruction.
1176 */
1177 static int
1178generate_LOAD(
1179 cctx_T *cctx,
1180 isntype_T isn_type,
1181 int idx,
1182 char_u *name,
1183 type_T *type)
1184{
1185 isn_T *isn;
1186
Bram Moolenaar080457c2020-03-03 21:53:32 +01001187 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1189 return FAIL;
1190 if (name != NULL)
1191 isn->isn_arg.string = vim_strsave(name);
1192 else
1193 isn->isn_arg.number = idx;
1194
1195 return OK;
1196}
1197
1198/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001199 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001200 */
1201 static int
1202generate_LOADV(
1203 cctx_T *cctx,
1204 char_u *name,
1205 int error)
1206{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001207 int di_flags;
1208 int vidx = find_vim_var(name, &di_flags);
1209 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001210
Bram Moolenaar080457c2020-03-03 21:53:32 +01001211 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001212 if (vidx < 0)
1213 {
1214 if (error)
1215 semsg(_(e_var_notfound), name);
1216 return FAIL;
1217 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001218 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001219
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001220 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001221}
1222
1223/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001224 * Generate an ISN_UNLET instruction.
1225 */
1226 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001227generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001228{
1229 isn_T *isn;
1230
1231 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001232 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001233 return FAIL;
1234 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1235 isn->isn_arg.unlet.ul_forceit = forceit;
1236
1237 return OK;
1238}
1239
1240/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001241 * Generate an ISN_LOADS instruction.
1242 */
1243 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001244generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001246 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248 int sid,
1249 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250{
1251 isn_T *isn;
1252
Bram Moolenaar080457c2020-03-03 21:53:32 +01001253 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001254 if (isn_type == ISN_LOADS)
1255 isn = generate_instr_type(cctx, isn_type, type);
1256 else
1257 isn = generate_instr_drop(cctx, isn_type, 1);
1258 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001260 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1261 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262
1263 return OK;
1264}
1265
1266/*
1267 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1268 */
1269 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001270generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 cctx_T *cctx,
1272 isntype_T isn_type,
1273 int sid,
1274 int idx,
1275 type_T *type)
1276{
1277 isn_T *isn;
1278
Bram Moolenaar080457c2020-03-03 21:53:32 +01001279 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 if (isn_type == ISN_LOADSCRIPT)
1281 isn = generate_instr_type(cctx, isn_type, type);
1282 else
1283 isn = generate_instr_drop(cctx, isn_type, 1);
1284 if (isn == NULL)
1285 return FAIL;
1286 isn->isn_arg.script.script_sid = sid;
1287 isn->isn_arg.script.script_idx = idx;
1288 return OK;
1289}
1290
1291/*
1292 * Generate an ISN_NEWLIST instruction.
1293 */
1294 static int
1295generate_NEWLIST(cctx_T *cctx, int count)
1296{
1297 isn_T *isn;
1298 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299 type_T *type;
1300 type_T *member;
1301
Bram Moolenaar080457c2020-03-03 21:53:32 +01001302 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1304 return FAIL;
1305 isn->isn_arg.number = count;
1306
1307 // drop the value types
1308 stack->ga_len -= count;
1309
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001310 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001311 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 if (count > 0)
1313 member = ((type_T **)stack->ga_data)[stack->ga_len];
1314 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001315 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001316 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317
1318 // add the list type to the type stack
1319 if (ga_grow(stack, 1) == FAIL)
1320 return FAIL;
1321 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1322 ++stack->ga_len;
1323
1324 return OK;
1325}
1326
1327/*
1328 * Generate an ISN_NEWDICT instruction.
1329 */
1330 static int
1331generate_NEWDICT(cctx_T *cctx, int count)
1332{
1333 isn_T *isn;
1334 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 type_T *type;
1336 type_T *member;
1337
Bram Moolenaar080457c2020-03-03 21:53:32 +01001338 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1340 return FAIL;
1341 isn->isn_arg.number = count;
1342
1343 // drop the key and value types
1344 stack->ga_len -= 2 * count;
1345
Bram Moolenaar436472f2020-02-20 22:54:43 +01001346 // Use the first value type for the list member type. Use "void" for an
1347 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 if (count > 0)
1349 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1350 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001351 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001352 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353
1354 // add the dict type to the type stack
1355 if (ga_grow(stack, 1) == FAIL)
1356 return FAIL;
1357 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1358 ++stack->ga_len;
1359
1360 return OK;
1361}
1362
1363/*
1364 * Generate an ISN_FUNCREF instruction.
1365 */
1366 static int
1367generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1368{
1369 isn_T *isn;
1370 garray_T *stack = &cctx->ctx_type_stack;
1371
Bram Moolenaar080457c2020-03-03 21:53:32 +01001372 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1374 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001375 isn->isn_arg.funcref.fr_func = dfunc_idx;
1376 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377
1378 if (ga_grow(stack, 1) == FAIL)
1379 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001380 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 // TODO: argument and return types
1382 ++stack->ga_len;
1383
1384 return OK;
1385}
1386
1387/*
1388 * Generate an ISN_JUMP instruction.
1389 */
1390 static int
1391generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1392{
1393 isn_T *isn;
1394 garray_T *stack = &cctx->ctx_type_stack;
1395
Bram Moolenaar080457c2020-03-03 21:53:32 +01001396 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1398 return FAIL;
1399 isn->isn_arg.jump.jump_when = when;
1400 isn->isn_arg.jump.jump_where = where;
1401
1402 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1403 --stack->ga_len;
1404
1405 return OK;
1406}
1407
1408 static int
1409generate_FOR(cctx_T *cctx, int loop_idx)
1410{
1411 isn_T *isn;
1412 garray_T *stack = &cctx->ctx_type_stack;
1413
Bram Moolenaar080457c2020-03-03 21:53:32 +01001414 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1416 return FAIL;
1417 isn->isn_arg.forloop.for_idx = loop_idx;
1418
1419 if (ga_grow(stack, 1) == FAIL)
1420 return FAIL;
1421 // type doesn't matter, will be stored next
1422 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1423 ++stack->ga_len;
1424
1425 return OK;
1426}
1427
1428/*
1429 * Generate an ISN_BCALL instruction.
1430 * Return FAIL if the number of arguments is wrong.
1431 */
1432 static int
1433generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1434{
1435 isn_T *isn;
1436 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001437 type_T *argtypes[MAX_FUNC_ARGS];
1438 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439
Bram Moolenaar080457c2020-03-03 21:53:32 +01001440 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 if (check_internal_func(func_idx, argcount) == FAIL)
1442 return FAIL;
1443
1444 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1445 return FAIL;
1446 isn->isn_arg.bfunc.cbf_idx = func_idx;
1447 isn->isn_arg.bfunc.cbf_argcount = argcount;
1448
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001449 for (i = 0; i < argcount; ++i)
1450 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452 stack->ga_len -= argcount; // drop the arguments
1453 if (ga_grow(stack, 1) == FAIL)
1454 return FAIL;
1455 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001456 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 ++stack->ga_len; // add return value
1458
1459 return OK;
1460}
1461
1462/*
1463 * Generate an ISN_DCALL or ISN_UCALL instruction.
1464 * Return FAIL if the number of arguments is wrong.
1465 */
1466 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001467generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468{
1469 isn_T *isn;
1470 garray_T *stack = &cctx->ctx_type_stack;
1471 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001472 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473
Bram Moolenaar080457c2020-03-03 21:53:32 +01001474 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475 if (argcount > regular_args && !has_varargs(ufunc))
1476 {
1477 semsg(_(e_toomanyarg), ufunc->uf_name);
1478 return FAIL;
1479 }
1480 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1481 {
1482 semsg(_(e_toofewarg), ufunc->uf_name);
1483 return FAIL;
1484 }
1485
Bram Moolenaar822ba242020-05-24 23:00:18 +02001486 if (ufunc->uf_dfunc_idx != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001487 {
1488 int i;
1489
1490 for (i = 0; i < argcount; ++i)
1491 {
1492 type_T *expected;
1493 type_T *actual;
1494
1495 if (i < regular_args)
1496 {
1497 if (ufunc->uf_arg_types == NULL)
1498 continue;
1499 expected = ufunc->uf_arg_types[i];
1500 }
1501 else
1502 expected = ufunc->uf_va_type->tt_member;
1503 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001504 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001505 {
1506 arg_type_mismatch(expected, actual, i + 1);
1507 return FAIL;
1508 }
1509 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001510 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001511 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001512 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001513 }
1514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 if ((isn = generate_instr(cctx,
Bram Moolenaar822ba242020-05-24 23:00:18 +02001516 ufunc->uf_dfunc_idx != UF_NOT_COMPILED ? ISN_DCALL
1517 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 return FAIL;
Bram Moolenaar822ba242020-05-24 23:00:18 +02001519 if (ufunc->uf_dfunc_idx != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 {
1521 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1522 isn->isn_arg.dfunc.cdf_argcount = argcount;
1523 }
1524 else
1525 {
1526 // A user function may be deleted and redefined later, can't use the
1527 // ufunc pointer, need to look it up again at runtime.
1528 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1529 isn->isn_arg.ufunc.cuf_argcount = argcount;
1530 }
1531
1532 stack->ga_len -= argcount; // drop the arguments
1533 if (ga_grow(stack, 1) == FAIL)
1534 return FAIL;
1535 // add return value
1536 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1537 ++stack->ga_len;
1538
1539 return OK;
1540}
1541
1542/*
1543 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1544 */
1545 static int
1546generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1547{
1548 isn_T *isn;
1549 garray_T *stack = &cctx->ctx_type_stack;
1550
Bram Moolenaar080457c2020-03-03 21:53:32 +01001551 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1553 return FAIL;
1554 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1555 isn->isn_arg.ufunc.cuf_argcount = argcount;
1556
1557 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001558 if (ga_grow(stack, 1) == FAIL)
1559 return FAIL;
1560 // add return value
1561 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1562 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563
1564 return OK;
1565}
1566
1567/*
1568 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001569 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 */
1571 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001572generate_PCALL(
1573 cctx_T *cctx,
1574 int argcount,
1575 char_u *name,
1576 type_T *type,
1577 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578{
1579 isn_T *isn;
1580 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001581 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582
Bram Moolenaar080457c2020-03-03 21:53:32 +01001583 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001584
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001585 if (type->tt_type == VAR_ANY)
1586 ret_type = &t_any;
1587 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001588 {
1589 if (type->tt_argcount != -1)
1590 {
1591 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1592
1593 if (argcount < type->tt_min_argcount - varargs)
1594 {
1595 semsg(_(e_toofewarg), "[reference]");
1596 return FAIL;
1597 }
1598 if (!varargs && argcount > type->tt_argcount)
1599 {
1600 semsg(_(e_toomanyarg), "[reference]");
1601 return FAIL;
1602 }
1603 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001604 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001605 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001606 else
1607 {
1608 semsg(_("E1085: Not a callable type: %s"), name);
1609 return FAIL;
1610 }
1611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1613 return FAIL;
1614 isn->isn_arg.pfunc.cpf_top = at_top;
1615 isn->isn_arg.pfunc.cpf_argcount = argcount;
1616
1617 stack->ga_len -= argcount; // drop the arguments
1618
1619 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001620 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001622 // If partial is above the arguments it must be cleared and replaced with
1623 // the return value.
1624 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1625 return FAIL;
1626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 return OK;
1628}
1629
1630/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001631 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 */
1633 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001634generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635{
1636 isn_T *isn;
1637 garray_T *stack = &cctx->ctx_type_stack;
1638 type_T *type;
1639
Bram Moolenaar080457c2020-03-03 21:53:32 +01001640 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001641 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001643 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001645 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001647 if (type->tt_type != VAR_DICT && type != &t_any)
1648 {
1649 emsg(_(e_dictreq));
1650 return FAIL;
1651 }
1652 // change dict type to dict member type
1653 if (type->tt_type == VAR_DICT)
1654 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655
1656 return OK;
1657}
1658
1659/*
1660 * Generate an ISN_ECHO instruction.
1661 */
1662 static int
1663generate_ECHO(cctx_T *cctx, int with_white, int count)
1664{
1665 isn_T *isn;
1666
Bram Moolenaar080457c2020-03-03 21:53:32 +01001667 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1669 return FAIL;
1670 isn->isn_arg.echo.echo_with_white = with_white;
1671 isn->isn_arg.echo.echo_count = count;
1672
1673 return OK;
1674}
1675
Bram Moolenaarad39c092020-02-26 18:23:43 +01001676/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001677 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001678 */
1679 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001680generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001681{
1682 isn_T *isn;
1683
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001684 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001685 return FAIL;
1686 isn->isn_arg.number = count;
1687
1688 return OK;
1689}
1690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 static int
1692generate_EXEC(cctx_T *cctx, char_u *line)
1693{
1694 isn_T *isn;
1695
Bram Moolenaar080457c2020-03-03 21:53:32 +01001696 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1698 return FAIL;
1699 isn->isn_arg.string = vim_strsave(line);
1700 return OK;
1701}
1702
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001703 static int
1704generate_EXECCONCAT(cctx_T *cctx, int count)
1705{
1706 isn_T *isn;
1707
1708 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1709 return FAIL;
1710 isn->isn_arg.number = count;
1711 return OK;
1712}
1713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714/*
1715 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001716 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001717 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001718 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1720{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 lvar_T *lvar;
1722
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001723 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001725 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001726 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 }
1728
1729 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001730 return NULL;
1731 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001733 // Every local variable uses the next entry on the stack. We could re-use
1734 // the last ones when leaving a scope, but then variables used in a closure
1735 // might get overwritten. To keep things simple do not re-use stack
1736 // entries. This is less efficient, but memory is cheap these days.
1737 lvar->lv_idx = cctx->ctx_locals_count++;
1738
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001739 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 lvar->lv_const = isConst;
1741 lvar->lv_type = type;
1742
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001743 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744}
1745
1746/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001747 * Remove local variables above "new_top".
1748 */
1749 static void
1750unwind_locals(cctx_T *cctx, int new_top)
1751{
1752 if (cctx->ctx_locals.ga_len > new_top)
1753 {
1754 int idx;
1755 lvar_T *lvar;
1756
1757 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1758 {
1759 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1760 vim_free(lvar->lv_name);
1761 }
1762 }
1763 cctx->ctx_locals.ga_len = new_top;
1764}
1765
1766/*
1767 * Free all local variables.
1768 */
1769 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001770free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001771{
1772 unwind_locals(cctx, 0);
1773 ga_clear(&cctx->ctx_locals);
1774}
1775
1776/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 * Skip over a type definition and return a pointer to just after it.
1778 */
1779 char_u *
1780skip_type(char_u *start)
1781{
1782 char_u *p = start;
1783
1784 while (ASCII_ISALNUM(*p) || *p == '_')
1785 ++p;
1786
1787 // Skip over "<type>"; this is permissive about white space.
1788 if (*skipwhite(p) == '<')
1789 {
1790 p = skipwhite(p);
1791 p = skip_type(skipwhite(p + 1));
1792 p = skipwhite(p);
1793 if (*p == '>')
1794 ++p;
1795 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001796 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1797 {
1798 // handle func(args): type
1799 ++p;
1800 while (*p != ')' && *p != NUL)
1801 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001802 char_u *sp = p;
1803
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001804 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001805 if (p == sp)
1806 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001807 if (*p == ',')
1808 p = skipwhite(p + 1);
1809 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001810 if (*p == ')')
1811 {
1812 if (p[1] == ':')
1813 p = skip_type(skipwhite(p + 2));
1814 else
1815 p = skipwhite(p + 1);
1816 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001817 }
1818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 return p;
1820}
1821
1822/*
1823 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001824 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 * Returns NULL in case of failure.
1826 */
1827 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001828parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829{
1830 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001831 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832
1833 if (**arg != '<')
1834 {
1835 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001836 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 else
1838 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001839 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 }
1841 *arg = skipwhite(*arg + 1);
1842
Bram Moolenaard77a8522020-04-03 21:59:57 +02001843 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844
1845 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001846 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 {
1848 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001849 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 }
1851 ++*arg;
1852
1853 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001854 return get_list_type(member_type, type_gap);
1855 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856}
1857
1858/*
1859 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001860 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 */
1862 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001863parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864{
1865 char_u *p = *arg;
1866 size_t len;
1867
1868 // skip over the first word
1869 while (ASCII_ISALNUM(*p) || *p == '_')
1870 ++p;
1871 len = p - *arg;
1872
1873 switch (**arg)
1874 {
1875 case 'a':
1876 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1877 {
1878 *arg += len;
1879 return &t_any;
1880 }
1881 break;
1882 case 'b':
1883 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1884 {
1885 *arg += len;
1886 return &t_bool;
1887 }
1888 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1889 {
1890 *arg += len;
1891 return &t_blob;
1892 }
1893 break;
1894 case 'c':
1895 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1896 {
1897 *arg += len;
1898 return &t_channel;
1899 }
1900 break;
1901 case 'd':
1902 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1903 {
1904 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001905 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 }
1907 break;
1908 case 'f':
1909 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1910 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001911#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 *arg += len;
1913 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001914#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001915 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001916 return &t_any;
1917#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 }
1919 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1920 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001921 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001922 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001923 int argcount = -1;
1924 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001925 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001926 type_T *arg_type[MAX_FUNC_ARGS + 1];
1927
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001928 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001930 if (**arg == '(')
1931 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001932 // "func" may or may not return a value, "func()" does
1933 // not return a value.
1934 ret_type = &t_void;
1935
Bram Moolenaard77a8522020-04-03 21:59:57 +02001936 p = ++*arg;
1937 argcount = 0;
1938 while (*p != NUL && *p != ')')
1939 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001940 if (*p == '?')
1941 {
1942 if (first_optional == -1)
1943 first_optional = argcount;
1944 ++p;
1945 }
1946 else if (first_optional != -1)
1947 {
1948 emsg(_("E1007: mandatory argument after optional argument"));
1949 return &t_any;
1950 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001951 else if (STRNCMP(p, "...", 3) == 0)
1952 {
1953 flags |= TTFLAG_VARARGS;
1954 p += 3;
1955 }
1956
1957 arg_type[argcount++] = parse_type(&p, type_gap);
1958
1959 // Nothing comes after "...{type}".
1960 if (flags & TTFLAG_VARARGS)
1961 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001962
Bram Moolenaard77a8522020-04-03 21:59:57 +02001963 if (*p != ',' && *skipwhite(p) == ',')
1964 {
1965 semsg(_(e_no_white_before), ",");
1966 return &t_any;
1967 }
1968 if (*p == ',')
1969 {
1970 ++p;
1971 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001972 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001973 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001974 return &t_any;
1975 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001976 }
1977 p = skipwhite(p);
1978 if (argcount == MAX_FUNC_ARGS)
1979 {
1980 emsg(_("E740: Too many argument types"));
1981 return &t_any;
1982 }
1983 }
1984
1985 p = skipwhite(p);
1986 if (*p != ')')
1987 {
1988 emsg(_(e_missing_close));
1989 return &t_any;
1990 }
1991 *arg = p + 1;
1992 }
1993 if (**arg == ':')
1994 {
1995 // parse return type
1996 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001997 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001998 semsg(_(e_white_after), ":");
1999 *arg = skipwhite(*arg);
2000 ret_type = parse_type(arg, type_gap);
2001 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002002 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002003 type = get_func_type(ret_type, argcount, type_gap);
2004 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002005 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002006 type = alloc_func_type(ret_type, argcount, type_gap);
2007 type->tt_flags = flags;
2008 if (argcount > 0)
2009 {
2010 type->tt_argcount = argcount;
2011 type->tt_min_argcount = first_optional == -1
2012 ? argcount : first_optional;
2013 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002014 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002015 return &t_any;
2016 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002017 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002018 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002019 }
2020 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 }
2022 break;
2023 case 'j':
2024 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2025 {
2026 *arg += len;
2027 return &t_job;
2028 }
2029 break;
2030 case 'l':
2031 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2032 {
2033 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002034 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 }
2036 break;
2037 case 'n':
2038 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2039 {
2040 *arg += len;
2041 return &t_number;
2042 }
2043 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 case 's':
2045 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2046 {
2047 *arg += len;
2048 return &t_string;
2049 }
2050 break;
2051 case 'v':
2052 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2053 {
2054 *arg += len;
2055 return &t_void;
2056 }
2057 break;
2058 }
2059
2060 semsg(_("E1010: Type not recognized: %s"), *arg);
2061 return &t_any;
2062}
2063
2064/*
2065 * Check if "type1" and "type2" are exactly the same.
2066 */
2067 static int
2068equal_type(type_T *type1, type_T *type2)
2069{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002070 int i;
2071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 if (type1->tt_type != type2->tt_type)
2073 return FALSE;
2074 switch (type1->tt_type)
2075 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002077 case VAR_ANY:
2078 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 case VAR_SPECIAL:
2080 case VAR_BOOL:
2081 case VAR_NUMBER:
2082 case VAR_FLOAT:
2083 case VAR_STRING:
2084 case VAR_BLOB:
2085 case VAR_JOB:
2086 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002087 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088 case VAR_LIST:
2089 case VAR_DICT:
2090 return equal_type(type1->tt_member, type2->tt_member);
2091 case VAR_FUNC:
2092 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002093 if (!equal_type(type1->tt_member, type2->tt_member)
2094 || type1->tt_argcount != type2->tt_argcount)
2095 return FALSE;
2096 if (type1->tt_argcount < 0
2097 || type1->tt_args == NULL || type2->tt_args == NULL)
2098 return TRUE;
2099 for (i = 0; i < type1->tt_argcount; ++i)
2100 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2101 return FALSE;
2102 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 }
2104 return TRUE;
2105}
2106
2107/*
2108 * Find the common type of "type1" and "type2" and put it in "dest".
2109 * "type2" and "dest" may be the same.
2110 */
2111 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002112common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113{
2114 if (equal_type(type1, type2))
2115 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002116 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117 return;
2118 }
2119
2120 if (type1->tt_type == type2->tt_type)
2121 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2123 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002124 type_T *common;
2125
Bram Moolenaard77a8522020-04-03 21:59:57 +02002126 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002127 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002128 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002129 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002130 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 return;
2132 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002133 if (type1->tt_type == VAR_FUNC)
2134 {
2135 type_T *common;
2136
2137 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2138 if (type1->tt_argcount == type2->tt_argcount
2139 && type1->tt_argcount >= 0)
2140 {
2141 int argcount = type1->tt_argcount;
2142 int i;
2143
2144 *dest = alloc_func_type(common, argcount, type_gap);
2145 if (type1->tt_args != NULL && type2->tt_args != NULL)
2146 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002147 if (func_type_add_arg_types(*dest, argcount,
2148 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002149 for (i = 0; i < argcount; ++i)
2150 common_type(type1->tt_args[i], type2->tt_args[i],
2151 &(*dest)->tt_args[i], type_gap);
2152 }
2153 }
2154 else
2155 *dest = alloc_func_type(common, -1, type_gap);
2156 return;
2157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 }
2159
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002160 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161}
2162
2163 char *
2164vartype_name(vartype_T type)
2165{
2166 switch (type)
2167 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002168 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002169 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 case VAR_SPECIAL: return "special";
2172 case VAR_BOOL: return "bool";
2173 case VAR_NUMBER: return "number";
2174 case VAR_FLOAT: return "float";
2175 case VAR_STRING: return "string";
2176 case VAR_BLOB: return "blob";
2177 case VAR_JOB: return "job";
2178 case VAR_CHANNEL: return "channel";
2179 case VAR_LIST: return "list";
2180 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002181
2182 case VAR_FUNC:
2183 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002185 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186}
2187
2188/*
2189 * Return the name of a type.
2190 * The result may be in allocated memory, in which case "tofree" is set.
2191 */
2192 char *
2193type_name(type_T *type, char **tofree)
2194{
2195 char *name = vartype_name(type->tt_type);
2196
2197 *tofree = NULL;
2198 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2199 {
2200 char *member_free;
2201 char *member_name = type_name(type->tt_member, &member_free);
2202 size_t len;
2203
2204 len = STRLEN(name) + STRLEN(member_name) + 3;
2205 *tofree = alloc(len);
2206 if (*tofree != NULL)
2207 {
2208 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2209 vim_free(member_free);
2210 return *tofree;
2211 }
2212 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002213 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002214 {
2215 garray_T ga;
2216 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002217 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002218
2219 ga_init2(&ga, 1, 100);
2220 if (ga_grow(&ga, 20) == FAIL)
2221 return "[unknown]";
2222 *tofree = ga.ga_data;
2223 STRCPY(ga.ga_data, "func(");
2224 ga.ga_len += 5;
2225
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002226 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002227 {
2228 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002229 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002230 int len;
2231
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002232 if (type->tt_args == NULL)
2233 arg_type = "[unknown]";
2234 else
2235 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002236 if (i > 0)
2237 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002238 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002239 ga.ga_len += 2;
2240 }
2241 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002242 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002243 {
2244 vim_free(arg_free);
2245 return "[unknown]";
2246 }
2247 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002248 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002249 {
2250 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2251 ga.ga_len += 3;
2252 }
2253 else if (i >= type->tt_min_argcount)
2254 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002255 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002256 ga.ga_len += len;
2257 vim_free(arg_free);
2258 }
2259
2260 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002261 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002262 else
2263 {
2264 char *ret_free;
2265 char *ret_name = type_name(type->tt_member, &ret_free);
2266 int len;
2267
2268 len = (int)STRLEN(ret_name) + 4;
2269 if (ga_grow(&ga, len) == FAIL)
2270 {
2271 vim_free(ret_free);
2272 return "[unknown]";
2273 }
2274 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002275 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2276 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002277 vim_free(ret_free);
2278 }
2279 return ga.ga_data;
2280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281
2282 return name;
2283}
2284
2285/*
2286 * Find "name" in script-local items of script "sid".
2287 * Returns the index in "sn_var_vals" if found.
2288 * If found but not in "sn_var_vals" returns -1.
2289 * If not found returns -2.
2290 */
2291 int
2292get_script_item_idx(int sid, char_u *name, int check_writable)
2293{
2294 hashtab_T *ht;
2295 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002296 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 int idx;
2298
2299 // First look the name up in the hashtable.
2300 if (sid <= 0 || sid > script_items.ga_len)
2301 return -1;
2302 ht = &SCRIPT_VARS(sid);
2303 di = find_var_in_ht(ht, 0, name, TRUE);
2304 if (di == NULL)
2305 return -2;
2306
2307 // Now find the svar_T index in sn_var_vals.
2308 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2309 {
2310 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2311
2312 if (sv->sv_tv == &di->di_tv)
2313 {
2314 if (check_writable && sv->sv_const)
2315 semsg(_(e_readonlyvar), name);
2316 return idx;
2317 }
2318 }
2319 return -1;
2320}
2321
2322/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002323 * Find "name" in imported items of the current script or in "cctx" if not
2324 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 */
2326 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002327find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002329 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 int idx;
2331
2332 if (cctx != NULL)
2333 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2334 {
2335 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2336 + idx;
2337
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002338 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2339 : STRLEN(import->imp_name) == len
2340 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 return import;
2342 }
2343
2344 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2345 {
2346 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2347
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002348 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2349 : STRLEN(import->imp_name) == len
2350 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 return import;
2352 }
2353 return NULL;
2354}
2355
2356/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002357 * Free all imported variables.
2358 */
2359 static void
2360free_imported(cctx_T *cctx)
2361{
2362 int idx;
2363
2364 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2365 {
2366 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2367
2368 vim_free(import->imp_name);
2369 }
2370 ga_clear(&cctx->ctx_imports);
2371}
2372
2373/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002374 * Get the next line of the function from "cctx".
2375 * Returns NULL when at the end.
2376 */
2377 static char_u *
2378next_line_from_context(cctx_T *cctx)
2379{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002380 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002381
2382 do
2383 {
2384 ++cctx->ctx_lnum;
2385 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002386 {
2387 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002388 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002389 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002390 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002391 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002392 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002393 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002394 return line;
2395}
2396
2397/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002398 * Return TRUE if "p" points at a "#" but not at "#{".
2399 */
2400 static int
2401comment_start(char_u *p)
2402{
2403 return p[0] == '#' && p[1] != '{';
2404}
2405
2406/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002407 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002408 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002409 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2410 */
2411 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002412may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002413{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002414 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002415 {
2416 char_u *next = next_line_from_context(cctx);
2417
2418 if (next == NULL)
2419 return FAIL;
2420 *arg = skipwhite(next);
2421 }
2422 return OK;
2423}
2424
Bram Moolenaara5565e42020-05-09 15:44:01 +02002425// Structure passed between the compile_expr* functions to keep track of
2426// constants that have been parsed but for which no code was produced yet. If
2427// possible expressions on these constants are applied at compile time. If
2428// that is not possible, the code to push the constants needs to be generated
2429// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002430// Using 50 should be more than enough of 5 levels of ().
2431#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002432typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002433 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002434 int pp_used; // active entries in pp_tv[]
2435} ppconst_T;
2436
Bram Moolenaar1c747212020-05-09 18:28:34 +02002437static int compile_expr0(char_u **arg, cctx_T *cctx);
2438static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2439
Bram Moolenaara5565e42020-05-09 15:44:01 +02002440/*
2441 * Generate a PUSH instruction for "tv".
2442 * "tv" will be consumed or cleared.
2443 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2444 */
2445 static int
2446generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2447{
2448 if (tv != NULL)
2449 {
2450 switch (tv->v_type)
2451 {
2452 case VAR_UNKNOWN:
2453 break;
2454 case VAR_BOOL:
2455 generate_PUSHBOOL(cctx, tv->vval.v_number);
2456 break;
2457 case VAR_SPECIAL:
2458 generate_PUSHSPEC(cctx, tv->vval.v_number);
2459 break;
2460 case VAR_NUMBER:
2461 generate_PUSHNR(cctx, tv->vval.v_number);
2462 break;
2463#ifdef FEAT_FLOAT
2464 case VAR_FLOAT:
2465 generate_PUSHF(cctx, tv->vval.v_float);
2466 break;
2467#endif
2468 case VAR_BLOB:
2469 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2470 tv->vval.v_blob = NULL;
2471 break;
2472 case VAR_STRING:
2473 generate_PUSHS(cctx, tv->vval.v_string);
2474 tv->vval.v_string = NULL;
2475 break;
2476 default:
2477 iemsg("constant type not supported");
2478 clear_tv(tv);
2479 return FAIL;
2480 }
2481 tv->v_type = VAR_UNKNOWN;
2482 }
2483 return OK;
2484}
2485
2486/*
2487 * Generate code for any ppconst entries.
2488 */
2489 static int
2490generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2491{
2492 int i;
2493 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002494 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002495
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002496 cctx->ctx_skip = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002497 for (i = 0; i < ppconst->pp_used; ++i)
2498 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2499 ret = FAIL;
2500 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002501 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002502 return ret;
2503}
2504
2505/*
2506 * Clear ppconst constants. Used when failing.
2507 */
2508 static void
2509clear_ppconst(ppconst_T *ppconst)
2510{
2511 int i;
2512
2513 for (i = 0; i < ppconst->pp_used; ++i)
2514 clear_tv(&ppconst->pp_tv[i]);
2515 ppconst->pp_used = 0;
2516}
2517
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002518/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002519 * Generate an instruction to load script-local variable "name", without the
2520 * leading "s:".
2521 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 */
2523 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002524compile_load_scriptvar(
2525 cctx_T *cctx,
2526 char_u *name, // variable NUL terminated
2527 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002528 char_u **end, // end of variable
2529 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002531 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2533 imported_T *import;
2534
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002535 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002537 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002538 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2539 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 }
2541 if (idx >= 0)
2542 {
2543 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2544
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002545 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 current_sctx.sc_sid, idx, sv->sv_type);
2547 return OK;
2548 }
2549
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002550 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 if (import != NULL)
2552 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002553 if (import->imp_all)
2554 {
2555 char_u *p = skipwhite(*end);
2556 int name_len;
2557 ufunc_T *ufunc;
2558 type_T *type;
2559
2560 // Used "import * as Name", need to lookup the member.
2561 if (*p != '.')
2562 {
2563 semsg(_("E1060: expected dot after name: %s"), start);
2564 return FAIL;
2565 }
2566 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002567 if (VIM_ISWHITE(*p))
2568 {
2569 emsg(_("E1074: no white space allowed after dot"));
2570 return FAIL;
2571 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002572
2573 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2574 // TODO: what if it is a function?
2575 if (idx < 0)
2576 return FAIL;
2577 *end = p;
2578
2579 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2580 import->imp_sid,
2581 idx,
2582 type);
2583 }
2584 else
2585 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002586 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002587 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2588 import->imp_sid,
2589 import->imp_var_vals_idx,
2590 import->imp_type);
2591 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 return OK;
2593 }
2594
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002595 if (error)
2596 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 return FAIL;
2598}
2599
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002600 static int
2601generate_funcref(cctx_T *cctx, char_u *name)
2602{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002603 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002604
2605 if (ufunc == NULL)
2606 return FAIL;
2607
2608 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2609}
2610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611/*
2612 * Compile a variable name into a load instruction.
2613 * "end" points to just after the name.
2614 * When "error" is FALSE do not give an error when not found.
2615 */
2616 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002617compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618{
2619 type_T *type;
2620 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002621 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002623 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624
2625 if (*(*arg + 1) == ':')
2626 {
2627 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002628 if (end <= *arg + 2)
2629 name = vim_strsave((char_u *)"[empty]");
2630 else
2631 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 if (name == NULL)
2633 return FAIL;
2634
2635 if (**arg == 'v')
2636 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002637 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 }
2639 else if (**arg == 'g')
2640 {
2641 // Global variables can be defined later, thus we don't check if it
2642 // exists, give error at runtime.
2643 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2644 }
2645 else if (**arg == 's')
2646 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002647 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002649 else if (**arg == 'b')
2650 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002651 // Buffer-local variables can be defined later, thus we don't check
2652 // if it exists, give error at runtime.
2653 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002654 }
2655 else if (**arg == 'w')
2656 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002657 // Window-local variables can be defined later, thus we don't check
2658 // if it exists, give error at runtime.
2659 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002660 }
2661 else if (**arg == 't')
2662 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002663 // Tabpage-local variables can be defined later, thus we don't
2664 // check if it exists, give error at runtime.
2665 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002666 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 else
2668 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002669 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 goto theend;
2671 }
2672 }
2673 else
2674 {
2675 size_t len = end - *arg;
2676 int idx;
2677 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002678 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679
2680 name = vim_strnsave(*arg, end - *arg);
2681 if (name == NULL)
2682 return FAIL;
2683
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002684 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002686 if (!gen_load_outer)
2687 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 }
2689 else
2690 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002691 lvar_T *lvar = lookup_local(*arg, len, cctx);
2692
2693 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002695 type = lvar->lv_type;
2696 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002697 if (lvar->lv_from_outer)
2698 gen_load_outer = TRUE;
2699 else
2700 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 }
2702 else
2703 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002704 // "var" can be script-local even without using "s:" if it
2705 // already exists.
2706 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2707 == SCRIPT_VERSION_VIM9
2708 || lookup_script(*arg, len) == OK)
2709 res = compile_load_scriptvar(cctx, name, *arg, &end,
2710 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002711
Bram Moolenaara5565e42020-05-09 15:44:01 +02002712 // When the name starts with an uppercase letter or "x:" it
2713 // can be a user defined function.
2714 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2715 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 }
2717 }
2718 if (gen_load)
2719 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002720 if (gen_load_outer)
2721 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 }
2723
2724 *arg = end;
2725
2726theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002727 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 semsg(_(e_var_notfound), name);
2729 vim_free(name);
2730 return res;
2731}
2732
2733/*
2734 * Compile the argument expressions.
2735 * "arg" points to just after the "(" and is advanced to after the ")"
2736 */
2737 static int
2738compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2739{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002740 char_u *p = *arg;
2741 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742
Bram Moolenaare6085c52020-04-12 20:19:16 +02002743 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002745 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002746 {
2747 p = next_line_from_context(cctx);
2748 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002749 goto failret;
2750 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002751 p = skipwhite(p);
2752 }
2753 if (*p == ')')
2754 {
2755 *arg = p + 1;
2756 return OK;
2757 }
2758
Bram Moolenaara5565e42020-05-09 15:44:01 +02002759 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 return FAIL;
2761 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002762
2763 if (*p != ',' && *skipwhite(p) == ',')
2764 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002765 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002766 p = skipwhite(p);
2767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002769 {
2770 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002771 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002772 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002773 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002774 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002775 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002777failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002778 emsg(_(e_missing_close));
2779 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780}
2781
2782/*
2783 * Compile a function call: name(arg1, arg2)
2784 * "arg" points to "name", "arg + varlen" to the "(".
2785 * "argcount_init" is 1 for "value->method()"
2786 * Instructions:
2787 * EVAL arg1
2788 * EVAL arg2
2789 * BCALL / DCALL / UCALL
2790 */
2791 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002792compile_call(
2793 char_u **arg,
2794 size_t varlen,
2795 cctx_T *cctx,
2796 ppconst_T *ppconst,
2797 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798{
2799 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002800 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 int argcount = argcount_init;
2802 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002803 char_u fname_buf[FLEN_FIXED + 1];
2804 char_u *tofree = NULL;
2805 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002807 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808
Bram Moolenaara5565e42020-05-09 15:44:01 +02002809 // we can evaluate "has('name')" at compile time
2810 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2811 {
2812 char_u *s = skipwhite(*arg + varlen + 1);
2813 typval_T argvars[2];
2814
2815 argvars[0].v_type = VAR_UNKNOWN;
2816 if (*s == '"')
2817 (void)get_string_tv(&s, &argvars[0], TRUE);
2818 else if (*s == '\'')
2819 (void)get_lit_string_tv(&s, &argvars[0], TRUE);
2820 s = skipwhite(s);
2821 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2822 {
2823 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2824
2825 *arg = s + 1;
2826 argvars[1].v_type = VAR_UNKNOWN;
2827 tv->v_type = VAR_NUMBER;
2828 tv->vval.v_number = 0;
2829 f_has(argvars, tv);
2830 clear_tv(&argvars[0]);
2831 ++ppconst->pp_used;
2832 return OK;
2833 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002834 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002835 }
2836
2837 if (generate_ppconst(cctx, ppconst) == FAIL)
2838 return FAIL;
2839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 if (varlen >= sizeof(namebuf))
2841 {
2842 semsg(_("E1011: name too long: %s"), name);
2843 return FAIL;
2844 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002845 vim_strncpy(namebuf, *arg, varlen);
2846 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847
2848 *arg = skipwhite(*arg + varlen + 1);
2849 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002850 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002852 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 {
2854 int idx;
2855
2856 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002857 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002859 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002860 else
2861 semsg(_(e_unknownfunc), namebuf);
2862 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 }
2864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002866 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002868 {
2869 res = generate_CALL(cctx, ufunc, argcount);
2870 goto theend;
2871 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872
2873 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002874 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002876 if (STRNCMP(namebuf, "g:", 2) != 0
2877 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002878 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002879 garray_T *stack = &cctx->ctx_type_stack;
2880 type_T *type;
2881
2882 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2883 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002884 goto theend;
2885 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002887 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002888 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002889 if (STRNCMP(namebuf, "g:", 2) == 0)
2890 res = generate_UCALL(cctx, name, argcount);
2891 else
2892 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002893
2894theend:
2895 vim_free(tofree);
2896 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897}
2898
2899// like NAMESPACE_CHAR but with 'a' and 'l'.
2900#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2901
2902/*
2903 * Find the end of a variable or function name. Unlike find_name_end() this
2904 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002905 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 * Return a pointer to just after the name. Equal to "arg" if there is no
2907 * valid name.
2908 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002909 static char_u *
2910to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911{
2912 char_u *p;
2913
2914 // Quick check for valid starting character.
2915 if (!eval_isnamec1(*arg))
2916 return arg;
2917
2918 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2919 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2920 // and can be used in slice "[n:]".
2921 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002922 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2924 break;
2925 return p;
2926}
2927
2928/*
2929 * Like to_name_end() but also skip over a list or dict constant.
2930 */
2931 char_u *
2932to_name_const_end(char_u *arg)
2933{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002934 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 typval_T rettv;
2936
2937 if (p == arg && *arg == '[')
2938 {
2939
2940 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar32e35112020-05-14 22:41:15 +02002941 if (get_list_tv(&p, &rettv, 0, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 p = arg;
2943 }
2944 else if (p == arg && *arg == '#' && arg[1] == '{')
2945 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002946 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 ++p;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002948 if (eval_dict(&p, &rettv, 0, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 p = arg;
2950 }
2951 else if (p == arg && *arg == '{')
2952 {
2953 int ret = get_lambda_tv(&p, &rettv, FALSE);
2954
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002955 // Can be "{x -> ret}()".
2956 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 if (ret == NOTDONE)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002958 ret = eval_dict(&p, &rettv, 0, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 if (ret != OK)
2960 p = arg;
2961 }
2962
2963 return p;
2964}
2965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966/*
2967 * parse a list: [expr, expr]
2968 * "*arg" points to the '['.
2969 */
2970 static int
2971compile_list(char_u **arg, cctx_T *cctx)
2972{
2973 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002974 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 int count = 0;
2976
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002977 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002979 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002980 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002981 p = next_line_from_context(cctx);
2982 if (p == NULL)
2983 {
2984 semsg(_(e_list_end), *arg);
2985 return FAIL;
2986 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002987 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002988 p = skipwhite(p);
2989 }
2990 if (*p == ']')
2991 {
2992 ++p;
2993 // Allow for following comment, after at least one space.
2994 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2995 p += STRLEN(p);
2996 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002997 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002998 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 break;
3000 ++count;
3001 if (*p == ',')
3002 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003003 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 p = skipwhite(p);
3005 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003006 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007
3008 generate_NEWLIST(cctx, count);
3009 return OK;
3010}
3011
3012/*
3013 * parse a lambda: {arg, arg -> expr}
3014 * "*arg" points to the '{'.
3015 */
3016 static int
3017compile_lambda(char_u **arg, cctx_T *cctx)
3018{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 typval_T rettv;
3020 ufunc_T *ufunc;
3021
3022 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01003023 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003027 ++ufunc->uf_refcount;
3028 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003029 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030
3031 // The function will have one line: "return {expr}".
3032 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003033 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034
3035 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003036 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 return FAIL;
3038}
3039
3040/*
3041 * Compile a lamda call: expr->{lambda}(args)
3042 * "arg" points to the "{".
3043 */
3044 static int
3045compile_lambda_call(char_u **arg, cctx_T *cctx)
3046{
3047 ufunc_T *ufunc;
3048 typval_T rettv;
3049 int argcount = 1;
3050 int ret = FAIL;
3051
3052 // Get the funcref in "rettv".
3053 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
3054 return FAIL;
3055
3056 if (**arg != '(')
3057 {
3058 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003059 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 else
3061 semsg(_(e_missing_paren), "lambda");
3062 clear_tv(&rettv);
3063 return FAIL;
3064 }
3065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 ufunc = rettv.vval.v_partial->pt_func;
3067 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003068 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003069 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003070
3071 // The function will have one line: "return {expr}".
3072 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003073 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074
3075 // compile the arguments
3076 *arg = skipwhite(*arg + 1);
3077 if (compile_arguments(arg, cctx, &argcount) == OK)
3078 // call the compiled function
3079 ret = generate_CALL(cctx, ufunc, argcount);
3080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 return ret;
3082}
3083
3084/*
3085 * parse a dict: {'key': val} or #{key: val}
3086 * "*arg" points to the '{'.
3087 */
3088 static int
3089compile_dict(char_u **arg, cctx_T *cctx, int literal)
3090{
3091 garray_T *instr = &cctx->ctx_instr;
3092 int count = 0;
3093 dict_T *d = dict_alloc();
3094 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003095 char_u *whitep = *arg;
3096 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097
3098 if (d == NULL)
3099 return FAIL;
3100 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003101 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 {
3103 char_u *key = NULL;
3104
Bram Moolenaar2c330432020-04-13 14:41:35 +02003105 while (**arg == NUL || (literal && **arg == '"')
3106 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003107 {
3108 *arg = next_line_from_context(cctx);
3109 if (*arg == NULL)
3110 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003111 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003112 *arg = skipwhite(*arg);
3113 }
3114
3115 if (**arg == '}')
3116 break;
3117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 if (literal)
3119 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003120 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121
Bram Moolenaar2c330432020-04-13 14:41:35 +02003122 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 {
3124 semsg(_("E1014: Invalid key: %s"), *arg);
3125 return FAIL;
3126 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003127 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 if (generate_PUSHS(cctx, key) == FAIL)
3129 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003130 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 }
3132 else
3133 {
3134 isn_T *isn;
3135
Bram Moolenaara5565e42020-05-09 15:44:01 +02003136 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 return FAIL;
3138 // TODO: check type is string
3139 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3140 if (isn->isn_type == ISN_PUSHS)
3141 key = isn->isn_arg.string;
3142 }
3143
3144 // Check for duplicate keys, if using string keys.
3145 if (key != NULL)
3146 {
3147 item = dict_find(d, key, -1);
3148 if (item != NULL)
3149 {
3150 semsg(_(e_duplicate_key), key);
3151 goto failret;
3152 }
3153 item = dictitem_alloc(key);
3154 if (item != NULL)
3155 {
3156 item->di_tv.v_type = VAR_UNKNOWN;
3157 item->di_tv.v_lock = 0;
3158 if (dict_add(d, item) == FAIL)
3159 dictitem_free(item);
3160 }
3161 }
3162
3163 *arg = skipwhite(*arg);
3164 if (**arg != ':')
3165 {
3166 semsg(_(e_missing_dict_colon), *arg);
3167 return FAIL;
3168 }
3169
Bram Moolenaar2c330432020-04-13 14:41:35 +02003170 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003172 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003173 {
3174 *arg = next_line_from_context(cctx);
3175 if (*arg == NULL)
3176 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003177 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003178 *arg = skipwhite(*arg);
3179 }
3180
Bram Moolenaara5565e42020-05-09 15:44:01 +02003181 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 return FAIL;
3183 ++count;
3184
Bram Moolenaar2c330432020-04-13 14:41:35 +02003185 whitep = *arg;
3186 p = skipwhite(*arg);
3187 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003188 {
3189 *arg = next_line_from_context(cctx);
3190 if (*arg == NULL)
3191 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003192 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003193 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003194 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 if (**arg == '}')
3197 break;
3198 if (**arg != ',')
3199 {
3200 semsg(_(e_missing_dict_comma), *arg);
3201 goto failret;
3202 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003203 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 *arg = skipwhite(*arg + 1);
3205 }
3206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 *arg = *arg + 1;
3208
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003209 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003210 p = skipwhite(*arg);
3211 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003212 *arg += STRLEN(*arg);
3213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 dict_unref(d);
3215 return generate_NEWDICT(cctx, count);
3216
3217failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003218 if (*arg == NULL)
3219 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 dict_unref(d);
3221 return FAIL;
3222}
3223
3224/*
3225 * Compile "&option".
3226 */
3227 static int
3228compile_get_option(char_u **arg, cctx_T *cctx)
3229{
3230 typval_T rettv;
3231 char_u *start = *arg;
3232 int ret;
3233
3234 // parse the option and get the current value to get the type.
3235 rettv.v_type = VAR_UNKNOWN;
3236 ret = get_option_tv(arg, &rettv, TRUE);
3237 if (ret == OK)
3238 {
3239 // include the '&' in the name, get_option_tv() expects it.
3240 char_u *name = vim_strnsave(start, *arg - start);
3241 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3242
3243 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3244 vim_free(name);
3245 }
3246 clear_tv(&rettv);
3247
3248 return ret;
3249}
3250
3251/*
3252 * Compile "$VAR".
3253 */
3254 static int
3255compile_get_env(char_u **arg, cctx_T *cctx)
3256{
3257 char_u *start = *arg;
3258 int len;
3259 int ret;
3260 char_u *name;
3261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 ++*arg;
3263 len = get_env_len(arg);
3264 if (len == 0)
3265 {
3266 semsg(_(e_syntax_at), start - 1);
3267 return FAIL;
3268 }
3269
3270 // include the '$' in the name, get_env_tv() expects it.
3271 name = vim_strnsave(start, len + 1);
3272 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3273 vim_free(name);
3274 return ret;
3275}
3276
3277/*
3278 * Compile "@r".
3279 */
3280 static int
3281compile_get_register(char_u **arg, cctx_T *cctx)
3282{
3283 int ret;
3284
3285 ++*arg;
3286 if (**arg == NUL)
3287 {
3288 semsg(_(e_syntax_at), *arg - 1);
3289 return FAIL;
3290 }
3291 if (!valid_yank_reg(**arg, TRUE))
3292 {
3293 emsg_invreg(**arg);
3294 return FAIL;
3295 }
3296 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3297 ++*arg;
3298 return ret;
3299}
3300
3301/*
3302 * Apply leading '!', '-' and '+' to constant "rettv".
3303 */
3304 static int
3305apply_leader(typval_T *rettv, char_u *start, char_u *end)
3306{
3307 char_u *p = end;
3308
3309 // this works from end to start
3310 while (p > start)
3311 {
3312 --p;
3313 if (*p == '-' || *p == '+')
3314 {
3315 // only '-' has an effect, for '+' we only check the type
3316#ifdef FEAT_FLOAT
3317 if (rettv->v_type == VAR_FLOAT)
3318 {
3319 if (*p == '-')
3320 rettv->vval.v_float = -rettv->vval.v_float;
3321 }
3322 else
3323#endif
3324 {
3325 varnumber_T val;
3326 int error = FALSE;
3327
3328 // tv_get_number_chk() accepts a string, but we don't want that
3329 // here
3330 if (check_not_string(rettv) == FAIL)
3331 return FAIL;
3332 val = tv_get_number_chk(rettv, &error);
3333 clear_tv(rettv);
3334 if (error)
3335 return FAIL;
3336 if (*p == '-')
3337 val = -val;
3338 rettv->v_type = VAR_NUMBER;
3339 rettv->vval.v_number = val;
3340 }
3341 }
3342 else
3343 {
3344 int v = tv2bool(rettv);
3345
3346 // '!' is permissive in the type.
3347 clear_tv(rettv);
3348 rettv->v_type = VAR_BOOL;
3349 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3350 }
3351 }
3352 return OK;
3353}
3354
3355/*
3356 * Recognize v: variables that are constants and set "rettv".
3357 */
3358 static void
3359get_vim_constant(char_u **arg, typval_T *rettv)
3360{
3361 if (STRNCMP(*arg, "v:true", 6) == 0)
3362 {
3363 rettv->v_type = VAR_BOOL;
3364 rettv->vval.v_number = VVAL_TRUE;
3365 *arg += 6;
3366 }
3367 else if (STRNCMP(*arg, "v:false", 7) == 0)
3368 {
3369 rettv->v_type = VAR_BOOL;
3370 rettv->vval.v_number = VVAL_FALSE;
3371 *arg += 7;
3372 }
3373 else if (STRNCMP(*arg, "v:null", 6) == 0)
3374 {
3375 rettv->v_type = VAR_SPECIAL;
3376 rettv->vval.v_number = VVAL_NULL;
3377 *arg += 6;
3378 }
3379 else if (STRNCMP(*arg, "v:none", 6) == 0)
3380 {
3381 rettv->v_type = VAR_SPECIAL;
3382 rettv->vval.v_number = VVAL_NONE;
3383 *arg += 6;
3384 }
3385}
3386
Bram Moolenaar61a89812020-05-07 16:58:17 +02003387 static exptype_T
3388get_compare_type(char_u *p, int *len, int *type_is)
3389{
3390 exptype_T type = EXPR_UNKNOWN;
3391 int i;
3392
3393 switch (p[0])
3394 {
3395 case '=': if (p[1] == '=')
3396 type = EXPR_EQUAL;
3397 else if (p[1] == '~')
3398 type = EXPR_MATCH;
3399 break;
3400 case '!': if (p[1] == '=')
3401 type = EXPR_NEQUAL;
3402 else if (p[1] == '~')
3403 type = EXPR_NOMATCH;
3404 break;
3405 case '>': if (p[1] != '=')
3406 {
3407 type = EXPR_GREATER;
3408 *len = 1;
3409 }
3410 else
3411 type = EXPR_GEQUAL;
3412 break;
3413 case '<': if (p[1] != '=')
3414 {
3415 type = EXPR_SMALLER;
3416 *len = 1;
3417 }
3418 else
3419 type = EXPR_SEQUAL;
3420 break;
3421 case 'i': if (p[1] == 's')
3422 {
3423 // "is" and "isnot"; but not a prefix of a name
3424 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3425 *len = 5;
3426 i = p[*len];
3427 if (!isalnum(i) && i != '_')
3428 {
3429 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3430 *type_is = TRUE;
3431 }
3432 }
3433 break;
3434 }
3435 return type;
3436}
3437
3438/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 * Compile code to apply '-', '+' and '!'.
3440 */
3441 static int
3442compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3443{
3444 char_u *p = end;
3445
3446 // this works from end to start
3447 while (p > start)
3448 {
3449 --p;
3450 if (*p == '-' || *p == '+')
3451 {
3452 int negate = *p == '-';
3453 isn_T *isn;
3454
3455 // TODO: check type
3456 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3457 {
3458 --p;
3459 if (*p == '-')
3460 negate = !negate;
3461 }
3462 // only '-' has an effect, for '+' we only check the type
3463 if (negate)
3464 isn = generate_instr(cctx, ISN_NEGATENR);
3465 else
3466 isn = generate_instr(cctx, ISN_CHECKNR);
3467 if (isn == NULL)
3468 return FAIL;
3469 }
3470 else
3471 {
3472 int invert = TRUE;
3473
3474 while (p > start && p[-1] == '!')
3475 {
3476 --p;
3477 invert = !invert;
3478 }
3479 if (generate_2BOOL(cctx, invert) == FAIL)
3480 return FAIL;
3481 }
3482 }
3483 return OK;
3484}
3485
3486/*
3487 * Compile whatever comes after "name" or "name()".
3488 */
3489 static int
3490compile_subscript(
3491 char_u **arg,
3492 cctx_T *cctx,
3493 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003494 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003495 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496{
3497 for (;;)
3498 {
3499 if (**arg == '(')
3500 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003501 garray_T *stack = &cctx->ctx_type_stack;
3502 type_T *type;
3503 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003505 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003506 return FAIL;
3507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003509 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 *arg = skipwhite(*arg + 1);
3512 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3513 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003514 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 return FAIL;
3516 }
3517 else if (**arg == '-' && (*arg)[1] == '>')
3518 {
3519 char_u *p;
3520
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003521 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003522 return FAIL;
3523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 // something->method()
3525 // Apply the '!', '-' and '+' first:
3526 // -1.0->func() works like (-1.0)->func()
3527 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3528 return FAIL;
3529 *start_leader = end_leader; // don't apply again later
3530
3531 *arg = skipwhite(*arg + 2);
3532 if (**arg == '{')
3533 {
3534 // lambda call: list->{lambda}
3535 if (compile_lambda_call(arg, cctx) == FAIL)
3536 return FAIL;
3537 }
3538 else
3539 {
3540 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003541 p = *arg;
3542 if (ASCII_ISALPHA(*p) && p[1] == ':')
3543 p += 2;
3544 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 ;
3546 if (*p != '(')
3547 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003548 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 return FAIL;
3550 }
3551 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003552 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 return FAIL;
3554 }
3555 }
3556 else if (**arg == '[')
3557 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003558 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003559 type_T **typep;
3560
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003561 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003562 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003563 // TODO: blob index
3564 // TODO: more arguments
3565 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003566 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003567 return FAIL;
3568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003570 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 return FAIL;
3572
3573 if (**arg != ']')
3574 {
3575 emsg(_(e_missbrac));
3576 return FAIL;
3577 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003578 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003580 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3581 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003582 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003583 if ((*typep)->tt_type == VAR_LIST)
3584 *typep = (*typep)->tt_member;
3585 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3586 return FAIL;
3587 }
3588 else if ((*typep)->tt_type == VAR_DICT)
3589 {
3590 *typep = (*typep)->tt_member;
3591 if (may_generate_2STRING(-1, cctx) == FAIL)
3592 return FAIL;
3593 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3594 return FAIL;
3595 }
3596 else
3597 {
3598 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003599 return FAIL;
3600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 }
3602 else if (**arg == '.' && (*arg)[1] != '.')
3603 {
3604 char_u *p;
3605
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003606 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003607 return FAIL;
3608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 ++*arg;
3610 p = *arg;
3611 // dictionary member: dict.name
3612 if (eval_isnamec1(*p))
3613 while (eval_isnamec(*p))
3614 MB_PTR_ADV(p);
3615 if (p == *arg)
3616 {
3617 semsg(_(e_syntax_at), *arg);
3618 return FAIL;
3619 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003620 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 return FAIL;
3622 *arg = p;
3623 }
3624 else
3625 break;
3626 }
3627
3628 // TODO - see handle_subscript():
3629 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3630 // Don't do this when "Func" is already a partial that was bound
3631 // explicitly (pt_auto is FALSE).
3632
3633 return OK;
3634}
3635
3636/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003637 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3638 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003640 * If the value is a constant "ppconst->pp_ret" will be set.
3641 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003642 *
3643 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 */
3645
3646/*
3647 * number number constant
3648 * 0zFFFFFFFF Blob constant
3649 * "string" string constant
3650 * 'string' literal string constant
3651 * &option-name option value
3652 * @r register contents
3653 * identifier variable value
3654 * function() function call
3655 * $VAR environment variable
3656 * (expression) nested expression
3657 * [expr, expr] List
3658 * {key: val, key: val} Dictionary
3659 * #{key: val, key: val} Dictionary with literal keys
3660 *
3661 * Also handle:
3662 * ! in front logical NOT
3663 * - in front unary minus
3664 * + in front unary plus (ignored)
3665 * trailing (arg) funcref/partial call
3666 * trailing [] subscript in String or List
3667 * trailing .name entry in Dictionary
3668 * trailing ->name() method call
3669 */
3670 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003671compile_expr7(
3672 char_u **arg,
3673 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003674 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 char_u *start_leader, *end_leader;
3677 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003678 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003679 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680
3681 /*
3682 * Skip '!', '-' and '+' characters. They are handled later.
3683 */
3684 start_leader = *arg;
3685 while (**arg == '!' || **arg == '-' || **arg == '+')
3686 *arg = skipwhite(*arg + 1);
3687 end_leader = *arg;
3688
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003689 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 switch (**arg)
3691 {
3692 /*
3693 * Number constant.
3694 */
3695 case '0': // also for blob starting with 0z
3696 case '1':
3697 case '2':
3698 case '3':
3699 case '4':
3700 case '5':
3701 case '6':
3702 case '7':
3703 case '8':
3704 case '9':
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003705 case '.': if (get_number_tv(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 return FAIL;
3707 break;
3708
3709 /*
3710 * String constant: "string".
3711 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003712 case '"': if (get_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 return FAIL;
3714 break;
3715
3716 /*
3717 * Literal string constant: 'str''ing'.
3718 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003719 case '\'': if (get_lit_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 return FAIL;
3721 break;
3722
3723 /*
3724 * Constant Vim variable.
3725 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003726 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 ret = NOTDONE;
3728 break;
3729
3730 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003731 * "true" constant
3732 */
3733 case 't': if (STRNCMP(*arg, "true", 4) == 0
3734 && !eval_isnamec((*arg)[4]))
3735 {
3736 *arg += 4;
3737 rettv->v_type = VAR_BOOL;
3738 rettv->vval.v_number = VVAL_TRUE;
3739 }
3740 else
3741 ret = NOTDONE;
3742 break;
3743
3744 /*
3745 * "false" constant
3746 */
3747 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3748 && !eval_isnamec((*arg)[5]))
3749 {
3750 *arg += 5;
3751 rettv->v_type = VAR_BOOL;
3752 rettv->vval.v_number = VVAL_FALSE;
3753 }
3754 else
3755 ret = NOTDONE;
3756 break;
3757
3758 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 * List: [expr, expr]
3760 */
3761 case '[': ret = compile_list(arg, cctx);
3762 break;
3763
3764 /*
3765 * Dictionary: #{key: val, key: val}
3766 */
3767 case '#': if ((*arg)[1] == '{')
3768 {
3769 ++*arg;
3770 ret = compile_dict(arg, cctx, TRUE);
3771 }
3772 else
3773 ret = NOTDONE;
3774 break;
3775
3776 /*
3777 * Lambda: {arg, arg -> expr}
3778 * Dictionary: {'key': val, 'key': val}
3779 */
3780 case '{': {
3781 char_u *start = skipwhite(*arg + 1);
3782
3783 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003784 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003786 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 if (ret != FAIL && *start == '>')
3788 ret = compile_lambda(arg, cctx);
3789 else
3790 ret = compile_dict(arg, cctx, FALSE);
3791 }
3792 break;
3793
3794 /*
3795 * Option value: &name
3796 */
3797 case '&': ret = compile_get_option(arg, cctx);
3798 break;
3799
3800 /*
3801 * Environment variable: $VAR.
3802 */
3803 case '$': ret = compile_get_env(arg, cctx);
3804 break;
3805
3806 /*
3807 * Register contents: @r.
3808 */
3809 case '@': ret = compile_get_register(arg, cctx);
3810 break;
3811 /*
3812 * nested expression: (expression).
3813 */
3814 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003815
3816 // recursive!
3817 if (ppconst->pp_used <= PPSIZE - 10)
3818 {
3819 ret = compile_expr1(arg, cctx, ppconst);
3820 }
3821 else
3822 {
3823 // Not enough space in ppconst, flush constants.
3824 if (generate_ppconst(cctx, ppconst) == FAIL)
3825 return FAIL;
3826 ret = compile_expr0(arg, cctx);
3827 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 *arg = skipwhite(*arg);
3829 if (**arg == ')')
3830 ++*arg;
3831 else if (ret == OK)
3832 {
3833 emsg(_(e_missing_close));
3834 ret = FAIL;
3835 }
3836 break;
3837
3838 default: ret = NOTDONE;
3839 break;
3840 }
3841 if (ret == FAIL)
3842 return FAIL;
3843
Bram Moolenaar1c747212020-05-09 18:28:34 +02003844 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 {
3846 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003847 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003849 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 return FAIL;
3851 }
3852 start_leader = end_leader; // don't apply again below
3853
Bram Moolenaara5565e42020-05-09 15:44:01 +02003854 if (cctx->ctx_skip == TRUE)
3855 clear_tv(rettv);
3856 else
3857 // A constant expression can possibly be handled compile time,
3858 // return the value instead of generating code.
3859 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 }
3861 else if (ret == NOTDONE)
3862 {
3863 char_u *p;
3864 int r;
3865
3866 if (!eval_isnamec1(**arg))
3867 {
3868 semsg(_("E1015: Name expected: %s"), *arg);
3869 return FAIL;
3870 }
3871
3872 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003873 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003875 {
3876 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003879 {
3880 if (generate_ppconst(cctx, ppconst) == FAIL)
3881 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 if (r == FAIL)
3885 return FAIL;
3886 }
3887
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003888 // Handle following "[]", ".member", etc.
3889 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003890 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003891 ppconst) == FAIL)
3892 return FAIL;
3893 if (ppconst->pp_used > 0)
3894 {
3895 // apply the '!', '-' and '+' before the constant
3896 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3897 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3898 return FAIL;
3899 return OK;
3900 }
3901 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003903 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904}
3905
3906/*
3907 * * number multiplication
3908 * / number division
3909 * % number modulo
3910 */
3911 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003912compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913{
3914 char_u *op;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003915 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003917 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003918 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 return FAIL;
3920
3921 /*
3922 * Repeat computing, until no "*", "/" or "%" is following.
3923 */
3924 for (;;)
3925 {
3926 op = skipwhite(*arg);
3927 if (*op != '*' && *op != '/' && *op != '%')
3928 break;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003929
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003930 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 {
3932 char_u buf[3];
3933
3934 vim_strncpy(buf, op, 1);
3935 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003936 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 }
3938 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003939 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003940 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003942 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003943 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003945
3946 if (ppconst->pp_used == ppconst_used + 2
3947 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3948 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003949 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003950 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3951 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003952 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003954 // both are numbers: compute the result
3955 switch (*op)
3956 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003957 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003958 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003959 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003960 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003961 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003962 break;
3963 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003964 tv1->vval.v_number = res;
3965 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003966 }
3967 else
3968 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003969 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003970 generate_two_op(cctx, op);
3971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 }
3973
3974 return OK;
3975}
3976
3977/*
3978 * + number addition
3979 * - number subtraction
3980 * .. string concatenation
3981 */
3982 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003983compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984{
3985 char_u *op;
3986 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003987 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988
3989 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003990 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 return FAIL;
3992
3993 /*
3994 * Repeat computing, until no "+", "-" or ".." is following.
3995 */
3996 for (;;)
3997 {
3998 op = skipwhite(*arg);
3999 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
4000 break;
4001 oplen = (*op == '.' ? 2 : 1);
4002
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004003 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 {
4005 char_u buf[3];
4006
4007 vim_strncpy(buf, op, oplen);
4008 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004009 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 }
4011
4012 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004013 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004014 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004016 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004017 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 return FAIL;
4019
Bram Moolenaara5565e42020-05-09 15:44:01 +02004020 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004021 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004022 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4023 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4024 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4025 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004027 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4028 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004029
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004030 // concat/subtract/add constant numbers
4031 if (*op == '+')
4032 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4033 else if (*op == '-')
4034 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4035 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004036 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004037 // concatenate constant strings
4038 char_u *s1 = tv1->vval.v_string;
4039 char_u *s2 = tv2->vval.v_string;
4040 size_t len1 = STRLEN(s1);
4041
4042 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4043 if (tv1->vval.v_string == NULL)
4044 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004045 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004046 return FAIL;
4047 }
4048 mch_memmove(tv1->vval.v_string, s1, len1);
4049 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004050 vim_free(s1);
4051 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004052 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004053 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 }
4055 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004056 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004057 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004058 if (*op == '.')
4059 {
4060 if (may_generate_2STRING(-2, cctx) == FAIL
4061 || may_generate_2STRING(-1, cctx) == FAIL)
4062 return FAIL;
4063 generate_instr_drop(cctx, ISN_CONCAT, 1);
4064 }
4065 else
4066 generate_two_op(cctx, op);
4067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 }
4069
4070 return OK;
4071}
4072
4073/*
4074 * expr5a == expr5b
4075 * expr5a =~ expr5b
4076 * expr5a != expr5b
4077 * expr5a !~ expr5b
4078 * expr5a > expr5b
4079 * expr5a >= expr5b
4080 * expr5a < expr5b
4081 * expr5a <= expr5b
4082 * expr5a is expr5b
4083 * expr5a isnot expr5b
4084 *
4085 * Produces instructions:
4086 * EVAL expr5a Push result of "expr5a"
4087 * EVAL expr5b Push result of "expr5b"
4088 * COMPARE one of the compare instructions
4089 */
4090 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004091compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092{
4093 exptype_T type = EXPR_UNKNOWN;
4094 char_u *p;
4095 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004097 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098
4099 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004100 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 return FAIL;
4102
4103 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004104 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105
4106 /*
4107 * If there is a comparative operator, use it.
4108 */
4109 if (type != EXPR_UNKNOWN)
4110 {
4111 int ic = FALSE; // Default: do not ignore case
4112
4113 if (type_is && (p[len] == '?' || p[len] == '#'))
4114 {
4115 semsg(_(e_invexpr2), *arg);
4116 return FAIL;
4117 }
4118 // extra question mark appended: ignore case
4119 if (p[len] == '?')
4120 {
4121 ic = TRUE;
4122 ++len;
4123 }
4124 // extra '#' appended: match case (ignored)
4125 else if (p[len] == '#')
4126 ++len;
4127 // nothing appended: match case
4128
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004129 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 {
4131 char_u buf[7];
4132
4133 vim_strncpy(buf, p, len);
4134 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004135 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 }
4137
4138 // get the second variable
4139 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004140 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004141 return FAIL;
4142
Bram Moolenaara5565e42020-05-09 15:44:01 +02004143 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 return FAIL;
4145
Bram Moolenaara5565e42020-05-09 15:44:01 +02004146 if (ppconst->pp_used == ppconst_used + 2)
4147 {
4148 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4149 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4150 int ret;
4151
4152 // Both sides are a constant, compute the result now.
4153 // First check for a valid combination of types, this is more
4154 // strict than typval_compare().
4155 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4156 ret = FAIL;
4157 else
4158 {
4159 ret = typval_compare(tv1, tv2, type, ic);
4160 tv1->v_type = VAR_BOOL;
4161 tv1->vval.v_number = tv1->vval.v_number
4162 ? VVAL_TRUE : VVAL_FALSE;
4163 clear_tv(tv2);
4164 --ppconst->pp_used;
4165 }
4166 return ret;
4167 }
4168
4169 generate_ppconst(cctx, ppconst);
4170 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 }
4172
4173 return OK;
4174}
4175
Bram Moolenaar7f141552020-05-09 17:35:53 +02004176static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178/*
4179 * Compile || or &&.
4180 */
4181 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004182compile_and_or(
4183 char_u **arg,
4184 cctx_T *cctx,
4185 char *op,
4186 ppconst_T *ppconst,
4187 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188{
4189 char_u *p = skipwhite(*arg);
4190 int opchar = *op;
4191
4192 if (p[0] == opchar && p[1] == opchar)
4193 {
4194 garray_T *instr = &cctx->ctx_instr;
4195 garray_T end_ga;
4196
4197 /*
4198 * Repeat until there is no following "||" or "&&"
4199 */
4200 ga_init2(&end_ga, sizeof(int), 10);
4201 while (p[0] == opchar && p[1] == opchar)
4202 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004203 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4204 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004206 return FAIL;
4207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208
Bram Moolenaara5565e42020-05-09 15:44:01 +02004209 // TODO: use ppconst if the value is a constant
4210 generate_ppconst(cctx, ppconst);
4211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 if (ga_grow(&end_ga, 1) == FAIL)
4213 {
4214 ga_clear(&end_ga);
4215 return FAIL;
4216 }
4217 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4218 ++end_ga.ga_len;
4219 generate_JUMP(cctx, opchar == '|'
4220 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4221
4222 // eval the next expression
4223 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004224 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004225 return FAIL;
4226
Bram Moolenaara5565e42020-05-09 15:44:01 +02004227 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4228 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 {
4230 ga_clear(&end_ga);
4231 return FAIL;
4232 }
4233 p = skipwhite(*arg);
4234 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004235 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236
4237 // Fill in the end label in all jumps.
4238 while (end_ga.ga_len > 0)
4239 {
4240 isn_T *isn;
4241
4242 --end_ga.ga_len;
4243 isn = ((isn_T *)instr->ga_data)
4244 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4245 isn->isn_arg.jump.jump_where = instr->ga_len;
4246 }
4247 ga_clear(&end_ga);
4248 }
4249
4250 return OK;
4251}
4252
4253/*
4254 * expr4a && expr4a && expr4a logical AND
4255 *
4256 * Produces instructions:
4257 * EVAL expr4a Push result of "expr4a"
4258 * JUMP_AND_KEEP_IF_FALSE end
4259 * EVAL expr4b Push result of "expr4b"
4260 * JUMP_AND_KEEP_IF_FALSE end
4261 * EVAL expr4c Push result of "expr4c"
4262 * end:
4263 */
4264 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004265compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004267 int ppconst_used = ppconst->pp_used;
4268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004270 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 return FAIL;
4272
4273 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004274 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275}
4276
4277/*
4278 * expr3a || expr3b || expr3c logical OR
4279 *
4280 * Produces instructions:
4281 * EVAL expr3a Push result of "expr3a"
4282 * JUMP_AND_KEEP_IF_TRUE end
4283 * EVAL expr3b Push result of "expr3b"
4284 * JUMP_AND_KEEP_IF_TRUE end
4285 * EVAL expr3c Push result of "expr3c"
4286 * end:
4287 */
4288 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004289compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004291 int ppconst_used = ppconst->pp_used;
4292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004294 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 return FAIL;
4296
4297 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004298 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299}
4300
4301/*
4302 * Toplevel expression: expr2 ? expr1a : expr1b
4303 *
4304 * Produces instructions:
4305 * EVAL expr2 Push result of "expr"
4306 * JUMP_IF_FALSE alt jump if false
4307 * EVAL expr1a
4308 * JUMP_ALWAYS end
4309 * alt: EVAL expr1b
4310 * end:
4311 */
4312 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004313compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314{
4315 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317
Bram Moolenaar61a89812020-05-07 16:58:17 +02004318 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004319 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 return FAIL;
4321
4322 p = skipwhite(*arg);
4323 if (*p == '?')
4324 {
4325 garray_T *instr = &cctx->ctx_instr;
4326 garray_T *stack = &cctx->ctx_type_stack;
4327 int alt_idx = instr->ga_len;
4328 int end_idx;
4329 isn_T *isn;
4330 type_T *type1;
4331 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004332 int has_const_expr = FALSE;
4333 int const_value = FALSE;
4334 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004336 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4337 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004339 return FAIL;
4340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341
Bram Moolenaara5565e42020-05-09 15:44:01 +02004342 if (ppconst->pp_used == ppconst_used + 1)
4343 {
4344 // the condition is a constant, we know whether the ? or the :
4345 // expression is to be evaluated.
4346 has_const_expr = TRUE;
4347 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4348 clear_tv(&ppconst->pp_tv[ppconst_used]);
4349 --ppconst->pp_used;
4350 cctx->ctx_skip = save_skip == TRUE || !const_value;
4351 }
4352 else
4353 {
4354 generate_ppconst(cctx, ppconst);
4355 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357
4358 // evaluate the second expression; any type is accepted
4359 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004360 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004361 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004362 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004363 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364
Bram Moolenaara5565e42020-05-09 15:44:01 +02004365 if (!has_const_expr)
4366 {
4367 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368
Bram Moolenaara5565e42020-05-09 15:44:01 +02004369 // remember the type and drop it
4370 --stack->ga_len;
4371 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372
Bram Moolenaara5565e42020-05-09 15:44:01 +02004373 end_idx = instr->ga_len;
4374 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4375
4376 // jump here from JUMP_IF_FALSE
4377 isn = ((isn_T *)instr->ga_data) + alt_idx;
4378 isn->isn_arg.jump.jump_where = instr->ga_len;
4379 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380
4381 // Check for the ":".
4382 p = skipwhite(*arg);
4383 if (*p != ':')
4384 {
4385 emsg(_(e_missing_colon));
4386 return FAIL;
4387 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004388 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4389 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004391 return FAIL;
4392 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393
4394 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004395 if (has_const_expr)
4396 cctx->ctx_skip = save_skip == TRUE || const_value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004398 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004399 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004400 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004401 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402
Bram Moolenaara5565e42020-05-09 15:44:01 +02004403 if (!has_const_expr)
4404 {
4405 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406
Bram Moolenaara5565e42020-05-09 15:44:01 +02004407 // If the types differ, the result has a more generic type.
4408 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4409 common_type(type1, type2, &type2, cctx->ctx_type_list);
4410
4411 // jump here from JUMP_ALWAYS
4412 isn = ((isn_T *)instr->ga_data) + end_idx;
4413 isn->isn_arg.jump.jump_where = instr->ga_len;
4414 }
4415
4416 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 }
4418 return OK;
4419}
4420
4421/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004422 * Toplevel expression.
4423 */
4424 static int
4425compile_expr0(char_u **arg, cctx_T *cctx)
4426{
4427 ppconst_T ppconst;
4428
4429 CLEAR_FIELD(ppconst);
4430 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4431 {
4432 clear_ppconst(&ppconst);
4433 return FAIL;
4434 }
4435 if (generate_ppconst(cctx, &ppconst) == FAIL)
4436 return FAIL;
4437 return OK;
4438}
4439
4440/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 * compile "return [expr]"
4442 */
4443 static char_u *
4444compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4445{
4446 char_u *p = arg;
4447 garray_T *stack = &cctx->ctx_type_stack;
4448 type_T *stack_type;
4449
4450 if (*p != NUL && *p != '|' && *p != '\n')
4451 {
4452 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004453 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 return NULL;
4455
4456 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4457 if (set_return_type)
4458 cctx->ctx_ufunc->uf_ret_type = stack_type;
4459 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4460 == FAIL)
4461 return NULL;
4462 }
4463 else
4464 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004465 // "set_return_type" cannot be TRUE, only used for a lambda which
4466 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004467 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4468 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 {
4470 emsg(_("E1003: Missing return value"));
4471 return NULL;
4472 }
4473
4474 // No argument, return zero.
4475 generate_PUSHNR(cctx, 0);
4476 }
4477
4478 if (generate_instr(cctx, ISN_RETURN) == NULL)
4479 return NULL;
4480
4481 // "return val | endif" is possible
4482 return skipwhite(p);
4483}
4484
4485/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004486 * Get a line from the compilation context, compatible with exarg_T getline().
4487 * Return a pointer to the line in allocated memory.
4488 * Return NULL for end-of-file or some error.
4489 */
4490 static char_u *
4491exarg_getline(
4492 int c UNUSED,
4493 void *cookie,
4494 int indent UNUSED,
4495 int do_concat UNUSED)
4496{
4497 cctx_T *cctx = (cctx_T *)cookie;
4498
4499 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4500 {
4501 iemsg("Heredoc got to end");
4502 return NULL;
4503 }
4504 ++cctx->ctx_lnum;
4505 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4506 [cctx->ctx_lnum]);
4507}
4508
4509/*
4510 * Compile a nested :def command.
4511 */
4512 static char_u *
4513compile_nested_function(exarg_T *eap, cctx_T *cctx)
4514{
4515 char_u *name_start = eap->arg;
4516 char_u *name_end = to_name_end(eap->arg, FALSE);
4517 char_u *name = get_lambda_name();
4518 lvar_T *lvar;
4519 ufunc_T *ufunc;
4520
4521 eap->arg = name_end;
4522 eap->getline = exarg_getline;
4523 eap->cookie = cctx;
4524 eap->skip = cctx->ctx_skip == TRUE;
4525 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004526 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004527
Bram Moolenaar822ba242020-05-24 23:00:18 +02004528 if (ufunc == NULL)
4529 return NULL;
4530 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED
4531 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004532 return NULL;
4533
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004534 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004535 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004536 TRUE, ufunc->uf_func_type);
4537
4538 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4539 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4540 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004541
Bram Moolenaar61a89812020-05-07 16:58:17 +02004542 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004543 return (char_u *)"";
4544}
4545
4546/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 * Return the length of an assignment operator, or zero if there isn't one.
4548 */
4549 int
4550assignment_len(char_u *p, int *heredoc)
4551{
4552 if (*p == '=')
4553 {
4554 if (p[1] == '<' && p[2] == '<')
4555 {
4556 *heredoc = TRUE;
4557 return 3;
4558 }
4559 return 1;
4560 }
4561 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4562 return 2;
4563 if (STRNCMP(p, "..=", 3) == 0)
4564 return 3;
4565 return 0;
4566}
4567
4568// words that cannot be used as a variable
4569static char *reserved[] = {
4570 "true",
4571 "false",
4572 NULL
4573};
4574
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004575typedef enum {
4576 dest_local,
4577 dest_option,
4578 dest_env,
4579 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004580 dest_buffer,
4581 dest_window,
4582 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004583 dest_vimvar,
4584 dest_script,
4585 dest_reg,
4586} assign_dest_T;
4587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004589 * Generate the load instruction for "name".
4590 */
4591 static void
4592generate_loadvar(
4593 cctx_T *cctx,
4594 assign_dest_T dest,
4595 char_u *name,
4596 lvar_T *lvar,
4597 type_T *type)
4598{
4599 switch (dest)
4600 {
4601 case dest_option:
4602 // TODO: check the option exists
4603 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4604 break;
4605 case dest_global:
4606 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4607 break;
4608 case dest_buffer:
4609 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4610 break;
4611 case dest_window:
4612 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4613 break;
4614 case dest_tab:
4615 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4616 break;
4617 case dest_script:
4618 compile_load_scriptvar(cctx,
4619 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4620 break;
4621 case dest_env:
4622 // Include $ in the name here
4623 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4624 break;
4625 case dest_reg:
4626 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4627 break;
4628 case dest_vimvar:
4629 generate_LOADV(cctx, name + 2, TRUE);
4630 break;
4631 case dest_local:
4632 if (lvar->lv_from_outer)
4633 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4634 NULL, type);
4635 else
4636 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4637 break;
4638 }
4639}
4640
4641/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004642 * Compile declaration and assignment:
4643 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004645 * Return NULL for an error.
4646 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 */
4648 static char_u *
4649compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4650{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004651 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004653 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 char_u *ret = NULL;
4655 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004656 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004659 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 int oplen = 0;
4662 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004663 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004664 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004665 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004669 // Skip over the "var" or "[var, var]" to get to any "=".
4670 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4671 if (p == NULL)
4672 return *arg == '[' ? arg : NULL;
4673
4674 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004676 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 return NULL;
4678 }
4679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 sp = p;
4681 p = skipwhite(p);
4682 op = p;
4683 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004684
4685 if (var_count > 0 && oplen == 0)
4686 // can be something like "[1, 2]->func()"
4687 return arg;
4688
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4690 {
4691 char_u buf[4];
4692
4693 vim_strncpy(buf, op, oplen);
4694 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004695 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004696 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 if (heredoc)
4699 {
4700 list_T *l;
4701 listitem_T *li;
4702
4703 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004704 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004706 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707
4708 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004709 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 {
4711 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4712 li->li_tv.vval.v_string = NULL;
4713 }
4714 generate_NEWLIST(cctx, l->lv_len);
4715 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004716 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 list_free(l);
4718 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004719 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004721 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004723 // for "[var, var] = expr" evaluate the expression here, loop over the
4724 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004725
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004726 p = skipwhite(op + oplen);
4727 if (compile_expr0(&p, cctx) == FAIL)
4728 return NULL;
4729 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004731 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004733 type_T *stacktype;
4734
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004735 stacktype = stack->ga_len == 0 ? &t_void
4736 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004737 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004739 emsg(_(e_cannot_use_void));
4740 goto theend;
4741 }
4742 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4743 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004744 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4745 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004746 }
4747 }
4748
4749 /*
4750 * Loop over variables in "[var, var] = expr".
4751 * For "var = expr" and "let var: type" this is done only once.
4752 */
4753 if (var_count > 0)
4754 var_start = skipwhite(arg + 1); // skip over the "["
4755 else
4756 var_start = arg;
4757 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4758 {
4759 char_u *var_end = skip_var_one(var_start, FALSE);
4760 size_t varlen;
4761 int new_local = FALSE;
4762 int opt_type;
4763 int opt_flags = 0;
4764 assign_dest_T dest = dest_local;
4765 int vimvaridx = -1;
4766 lvar_T *lvar = NULL;
4767 lvar_T arg_lvar;
4768 int has_type = FALSE;
4769 int has_index = FALSE;
4770 int instr_count = -1;
4771
4772 p = (*var_start == '&' || *var_start == '$'
4773 || *var_start == '@') ? var_start + 1 : var_start;
4774 p = to_name_end(p, TRUE);
4775
4776 // "a: type" is declaring variable "a" with a type, not "a:".
4777 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4778 --var_end;
4779 if (is_decl && p == var_start + 2 && p[-1] == ':')
4780 --p;
4781
4782 varlen = p - var_start;
4783 vim_free(name);
4784 name = vim_strnsave(var_start, varlen);
4785 if (name == NULL)
4786 return NULL;
4787 if (!heredoc)
4788 type = &t_any;
4789
4790 if (cctx->ctx_skip != TRUE)
4791 {
4792 if (*var_start == '&')
4793 {
4794 int cc;
4795 long numval;
4796
4797 dest = dest_option;
4798 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004800 emsg(_(e_const_option));
4801 goto theend;
4802 }
4803 if (is_decl)
4804 {
4805 semsg(_("E1052: Cannot declare an option: %s"), var_start);
4806 goto theend;
4807 }
4808 p = var_start;
4809 p = find_option_end(&p, &opt_flags);
4810 if (p == NULL)
4811 {
4812 // cannot happen?
4813 emsg(_(e_letunexp));
4814 goto theend;
4815 }
4816 cc = *p;
4817 *p = NUL;
4818 opt_type = get_option_value(var_start + 1, &numval,
4819 NULL, opt_flags);
4820 *p = cc;
4821 if (opt_type == -3)
4822 {
4823 semsg(_(e_unknown_option), var_start);
4824 goto theend;
4825 }
4826 if (opt_type == -2 || opt_type == 0)
4827 type = &t_string;
4828 else
4829 type = &t_number; // both number and boolean option
4830 }
4831 else if (*var_start == '$')
4832 {
4833 dest = dest_env;
4834 type = &t_string;
4835 if (is_decl)
4836 {
4837 semsg(_("E1065: Cannot declare an environment variable: %s"),
4838 name);
4839 goto theend;
4840 }
4841 }
4842 else if (*var_start == '@')
4843 {
4844 if (!valid_yank_reg(var_start[1], TRUE))
4845 {
4846 emsg_invreg(var_start[1]);
4847 goto theend;
4848 }
4849 dest = dest_reg;
4850 type = &t_string;
4851 if (is_decl)
4852 {
4853 semsg(_("E1066: Cannot declare a register: %s"), name);
4854 goto theend;
4855 }
4856 }
4857 else if (STRNCMP(var_start, "g:", 2) == 0)
4858 {
4859 dest = dest_global;
4860 if (is_decl)
4861 {
4862 semsg(_("E1016: Cannot declare a global variable: %s"),
4863 name);
4864 goto theend;
4865 }
4866 }
4867 else if (STRNCMP(var_start, "b:", 2) == 0)
4868 {
4869 dest = dest_buffer;
4870 if (is_decl)
4871 {
4872 semsg(_("E1078: Cannot declare a buffer variable: %s"),
4873 name);
4874 goto theend;
4875 }
4876 }
4877 else if (STRNCMP(var_start, "w:", 2) == 0)
4878 {
4879 dest = dest_window;
4880 if (is_decl)
4881 {
4882 semsg(_("E1079: Cannot declare a window variable: %s"),
4883 name);
4884 goto theend;
4885 }
4886 }
4887 else if (STRNCMP(var_start, "t:", 2) == 0)
4888 {
4889 dest = dest_tab;
4890 if (is_decl)
4891 {
4892 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4893 goto theend;
4894 }
4895 }
4896 else if (STRNCMP(var_start, "v:", 2) == 0)
4897 {
4898 typval_T *vtv;
4899 int di_flags;
4900
4901 vimvaridx = find_vim_var(name + 2, &di_flags);
4902 if (vimvaridx < 0)
4903 {
4904 semsg(_(e_var_notfound), var_start);
4905 goto theend;
4906 }
4907 // We use the current value of "sandbox" here, is that OK?
4908 if (var_check_ro(di_flags, name, FALSE))
4909 goto theend;
4910 dest = dest_vimvar;
4911 vtv = get_vim_var_tv(vimvaridx);
4912 type = typval2type(vtv);
4913 if (is_decl)
4914 {
4915 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4916 goto theend;
4917 }
4918 }
4919 else
4920 {
4921 int idx;
4922
4923 for (idx = 0; reserved[idx] != NULL; ++idx)
4924 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004925 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004926 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004927 goto theend;
4928 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004929
4930 lvar = lookup_local(var_start, varlen, cctx);
4931 if (lvar == NULL)
4932 {
4933 CLEAR_FIELD(arg_lvar);
4934 if (lookup_arg(var_start, varlen,
4935 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4936 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004937 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004938 if (is_decl)
4939 {
4940 semsg(_(e_used_as_arg), name);
4941 goto theend;
4942 }
4943 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004944 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004945 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004946 if (lvar != NULL)
4947 {
4948 if (is_decl)
4949 {
4950 semsg(_("E1017: Variable already declared: %s"), name);
4951 goto theend;
4952 }
4953 else if (lvar->lv_const)
4954 {
4955 semsg(_("E1018: Cannot assign to a constant: %s"),
4956 name);
4957 goto theend;
4958 }
4959 }
4960 else if (STRNCMP(var_start, "s:", 2) == 0
4961 || lookup_script(var_start, varlen) == OK
4962 || find_imported(var_start, varlen, cctx) != NULL)
4963 {
4964 dest = dest_script;
4965 if (is_decl)
4966 {
4967 semsg(_("E1054: Variable already declared in the script: %s"),
4968 name);
4969 goto theend;
4970 }
4971 }
4972 else if (name[1] == ':' && name[2] != NUL)
4973 {
4974 semsg(_("E1082: Cannot use a namespaced variable: %s"),
4975 name);
4976 goto theend;
4977 }
4978 else if (!is_decl)
4979 {
4980 semsg(_("E1089: unknown variable: %s"), name);
4981 goto theend;
4982 }
4983 }
4984 }
4985
4986 // handle "a:name" as a name, not index "name" on "a"
4987 if (varlen > 1 || var_start[varlen] != ':')
4988 p = var_end;
4989
4990 if (dest != dest_option)
4991 {
4992 if (is_decl && *p == ':')
4993 {
4994 // parse optional type: "let var: type = expr"
4995 if (!VIM_ISWHITE(p[1]))
4996 {
4997 semsg(_(e_white_after), ":");
4998 goto theend;
4999 }
5000 p = skipwhite(p + 1);
5001 type = parse_type(&p, cctx->ctx_type_list);
5002 has_type = TRUE;
5003 }
5004 else if (lvar != NULL)
5005 type = lvar->lv_type;
5006 }
5007
5008 if (oplen == 3 && !heredoc && dest != dest_global
5009 && type->tt_type != VAR_STRING
5010 && type->tt_type != VAR_ANY)
5011 {
5012 emsg(_("E1019: Can only concatenate to string"));
5013 goto theend;
5014 }
5015
5016 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
5017 {
5018 if (oplen > 1 && !heredoc)
5019 {
5020 // +=, /=, etc. require an existing variable
5021 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5022 name);
5023 goto theend;
5024 }
5025
5026 // new local variable
5027 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5028 goto theend;
5029 lvar = reserve_local(cctx, var_start, varlen,
5030 cmdidx == CMD_const, type);
5031 if (lvar == NULL)
5032 goto theend;
5033 new_local = TRUE;
5034 }
5035
5036 member_type = type;
5037 if (var_end > var_start + varlen)
5038 {
5039 // Something follows after the variable: "var[idx]".
5040 if (is_decl)
5041 {
5042 emsg(_("E1087: cannot use an index when declaring a variable"));
5043 goto theend;
5044 }
5045
5046 if (var_start[varlen] == '[')
5047 {
5048 has_index = TRUE;
5049 if (type->tt_member == NULL)
5050 {
5051 semsg(_("E1088: cannot use an index on %s"), name);
5052 goto theend;
5053 }
5054 member_type = type->tt_member;
5055 }
5056 else
5057 {
5058 semsg("Not supported yet: %s", var_start);
5059 goto theend;
5060 }
5061 }
5062 else if (lvar == &arg_lvar)
5063 {
5064 semsg(_("E1090: Cannot assign to argument %s"), name);
5065 goto theend;
5066 }
5067
5068 if (!heredoc)
5069 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005070 if (cctx->ctx_skip == TRUE)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005071 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005072 if (oplen > 0 && var_count == 0)
5073 {
5074 // skip over the "=" and the expression
5075 p = skipwhite(op + oplen);
5076 compile_expr0(&p, cctx);
5077 }
5078 }
5079 else if (oplen > 0)
5080 {
5081 type_T *stacktype;
5082
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005083 // For "var = expr" evaluate the expression.
5084 if (var_count == 0)
5085 {
5086 int r;
5087
5088 // for "+=", "*=", "..=" etc. first load the current value
5089 if (*op != '=')
5090 {
5091 generate_loadvar(cctx, dest, name, lvar, type);
5092
5093 if (has_index)
5094 {
5095 // TODO: get member from list or dict
5096 emsg("Index with operation not supported yet");
5097 goto theend;
5098 }
5099 }
5100
5101 // Compile the expression. Temporarily hide the new local
5102 // variable here, it is not available to this expression.
5103 if (new_local)
5104 --cctx->ctx_locals.ga_len;
5105 instr_count = instr->ga_len;
5106 p = skipwhite(op + oplen);
5107 r = compile_expr0(&p, cctx);
5108 if (new_local)
5109 ++cctx->ctx_locals.ga_len;
5110 if (r == FAIL)
5111 goto theend;
5112 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005113 else if (semicolon && var_idx == var_count - 1)
5114 {
5115 // For "[var; var] = expr" get the rest of the list
5116 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5117 goto theend;
5118 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005119 else
5120 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005121 // For "[var, var] = expr" get the "var_idx" item from the
5122 // list.
5123 if (generate_GETITEM(cctx, var_idx) == FAIL)
5124 return FAIL;
5125 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005126
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005127 stacktype = stack->ga_len == 0 ? &t_void
5128 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5129 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005131 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005132 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005133 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005134 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005135 emsg(_(e_cannot_use_void));
5136 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005137 }
5138 else
5139 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005140 // An empty list or dict has a &t_void member,
5141 // for a variable that implies &t_any.
5142 if (stacktype == &t_list_empty)
5143 lvar->lv_type = &t_list_any;
5144 else if (stacktype == &t_dict_empty)
5145 lvar->lv_type = &t_dict_any;
5146 else
5147 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005148 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005149 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005150 else
5151 {
5152 type_T *use_type = lvar->lv_type;
5153
5154 if (has_index)
5155 {
5156 use_type = use_type->tt_member;
5157 if (use_type == NULL)
5158 use_type = &t_void;
5159 }
5160 if (need_type(stacktype, use_type, -1, cctx)
5161 == FAIL)
5162 goto theend;
5163 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005164 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005165 else if (*p != '=' && need_type(stacktype, member_type, -1,
5166 cctx) == FAIL)
5167 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005169 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005171 emsg(_(e_const_req_value));
5172 goto theend;
5173 }
5174 else if (!has_type || dest == dest_option)
5175 {
5176 emsg(_(e_type_req));
5177 goto theend;
5178 }
5179 else
5180 {
5181 // variables are always initialized
5182 if (ga_grow(instr, 1) == FAIL)
5183 goto theend;
5184 switch (member_type->tt_type)
5185 {
5186 case VAR_BOOL:
5187 generate_PUSHBOOL(cctx, VVAL_FALSE);
5188 break;
5189 case VAR_FLOAT:
5190#ifdef FEAT_FLOAT
5191 generate_PUSHF(cctx, 0.0);
5192#endif
5193 break;
5194 case VAR_STRING:
5195 generate_PUSHS(cctx, NULL);
5196 break;
5197 case VAR_BLOB:
5198 generate_PUSHBLOB(cctx, NULL);
5199 break;
5200 case VAR_FUNC:
5201 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5202 break;
5203 case VAR_LIST:
5204 generate_NEWLIST(cctx, 0);
5205 break;
5206 case VAR_DICT:
5207 generate_NEWDICT(cctx, 0);
5208 break;
5209 case VAR_JOB:
5210 generate_PUSHJOB(cctx, NULL);
5211 break;
5212 case VAR_CHANNEL:
5213 generate_PUSHCHANNEL(cctx, NULL);
5214 break;
5215 case VAR_NUMBER:
5216 case VAR_UNKNOWN:
5217 case VAR_ANY:
5218 case VAR_PARTIAL:
5219 case VAR_VOID:
5220 case VAR_SPECIAL: // cannot happen
5221 generate_PUSHNR(cctx, 0);
5222 break;
5223 }
5224 }
5225 if (var_count == 0)
5226 end = p;
5227 }
5228
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005229 // no need to parse more when skipping
5230 if (cctx->ctx_skip == TRUE)
5231 break;
5232
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005233 if (oplen > 0 && *op != '=')
5234 {
5235 type_T *expected = &t_number;
5236 type_T *stacktype;
5237
5238 // TODO: if type is known use float or any operation
5239
5240 if (*op == '.')
5241 expected = &t_string;
5242 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5243 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5244 goto theend;
5245
5246 if (*op == '.')
5247 generate_instr_drop(cctx, ISN_CONCAT, 1);
5248 else
5249 {
5250 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5251
5252 if (isn == NULL)
5253 goto theend;
5254 switch (*op)
5255 {
5256 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5257 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5258 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5259 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5260 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5261 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005262 }
5263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005264
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005265 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005266 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005267 int r;
5268
5269 // Compile the "idx" in "var[idx]".
5270 if (new_local)
5271 --cctx->ctx_locals.ga_len;
5272 p = skipwhite(var_start + varlen + 1);
5273 r = compile_expr0(&p, cctx);
5274 if (new_local)
5275 ++cctx->ctx_locals.ga_len;
5276 if (r == FAIL)
5277 goto theend;
5278 if (*skipwhite(p) != ']')
5279 {
5280 emsg(_(e_missbrac));
5281 goto theend;
5282 }
5283 if (type->tt_type == VAR_DICT
5284 && may_generate_2STRING(-1, cctx) == FAIL)
5285 goto theend;
5286 if (type->tt_type == VAR_LIST
5287 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005288 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005289 {
5290 emsg(_(e_number_exp));
5291 goto theend;
5292 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005293
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005294 // Load the dict or list. On the stack we then have:
5295 // - value
5296 // - index
5297 // - variable
5298 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005299
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005300 if (type->tt_type == VAR_LIST)
5301 {
5302 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5303 return FAIL;
5304 }
5305 else if (type->tt_type == VAR_DICT)
5306 {
5307 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5308 return FAIL;
5309 }
5310 else
5311 {
5312 emsg(_(e_listreq));
5313 goto theend;
5314 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005315 }
5316 else
5317 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005318 switch (dest)
5319 {
5320 case dest_option:
5321 generate_STOREOPT(cctx, name + 1, opt_flags);
5322 break;
5323 case dest_global:
5324 // include g: with the name, easier to execute that way
5325 generate_STORE(cctx, ISN_STOREG, 0, name);
5326 break;
5327 case dest_buffer:
5328 // include b: with the name, easier to execute that way
5329 generate_STORE(cctx, ISN_STOREB, 0, name);
5330 break;
5331 case dest_window:
5332 // include w: with the name, easier to execute that way
5333 generate_STORE(cctx, ISN_STOREW, 0, name);
5334 break;
5335 case dest_tab:
5336 // include t: with the name, easier to execute that way
5337 generate_STORE(cctx, ISN_STORET, 0, name);
5338 break;
5339 case dest_env:
5340 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5341 break;
5342 case dest_reg:
5343 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5344 break;
5345 case dest_vimvar:
5346 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5347 break;
5348 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005349 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005350 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5351 imported_T *import = NULL;
5352 int sid = current_sctx.sc_sid;
5353 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005354
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005355 if (name[1] != ':')
5356 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005357 import = find_imported(name, 0, cctx);
5358 if (import != NULL)
5359 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005360 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005361
5362 idx = get_script_item_idx(sid, rawname, TRUE);
5363 // TODO: specific type
5364 if (idx < 0)
5365 {
5366 char_u *name_s = name;
5367
5368 // Include s: in the name for store_var()
5369 if (name[1] != ':')
5370 {
5371 int len = (int)STRLEN(name) + 3;
5372
5373 name_s = alloc(len);
5374 if (name_s == NULL)
5375 name_s = name;
5376 else
5377 vim_snprintf((char *)name_s, len,
5378 "s:%s", name);
5379 }
5380 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005381 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382 if (name_s != name)
5383 vim_free(name_s);
5384 }
5385 else
5386 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005387 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005388 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005389 break;
5390 case dest_local:
5391 if (lvar != NULL)
5392 {
5393 isn_T *isn = ((isn_T *)instr->ga_data)
5394 + instr->ga_len - 1;
5395
5396 // optimization: turn "var = 123" from ISN_PUSHNR +
5397 // ISN_STORE into ISN_STORENR
5398 if (!lvar->lv_from_outer
5399 && instr->ga_len == instr_count + 1
5400 && isn->isn_type == ISN_PUSHNR)
5401 {
5402 varnumber_T val = isn->isn_arg.number;
5403
5404 isn->isn_type = ISN_STORENR;
5405 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5406 isn->isn_arg.storenr.stnr_val = val;
5407 if (stack->ga_len > 0)
5408 --stack->ga_len;
5409 }
5410 else if (lvar->lv_from_outer)
5411 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005412 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005413 else
5414 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5415 }
5416 break;
5417 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005418 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005419
5420 if (var_idx + 1 < var_count)
5421 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005423
5424 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005425 if (var_count > 0 && !semicolon)
5426 {
5427 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5428 goto theend;
5429 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005430
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005431 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005432
5433theend:
5434 vim_free(name);
5435 return ret;
5436}
5437
5438/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005439 * Check if "name" can be "unlet".
5440 */
5441 int
5442check_vim9_unlet(char_u *name)
5443{
5444 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5445 {
5446 semsg(_("E1081: Cannot unlet %s"), name);
5447 return FAIL;
5448 }
5449 return OK;
5450}
5451
5452/*
5453 * Callback passed to ex_unletlock().
5454 */
5455 static int
5456compile_unlet(
5457 lval_T *lvp,
5458 char_u *name_end,
5459 exarg_T *eap,
5460 int deep UNUSED,
5461 void *coookie)
5462{
5463 cctx_T *cctx = coookie;
5464
5465 if (lvp->ll_tv == NULL)
5466 {
5467 char_u *p = lvp->ll_name;
5468 int cc = *name_end;
5469 int ret = OK;
5470
5471 // Normal name. Only supports g:, w:, t: and b: namespaces.
5472 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005473 if (*p == '$')
5474 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5475 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005476 ret = FAIL;
5477 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005478 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005479
5480 *name_end = cc;
5481 return ret;
5482 }
5483
5484 // TODO: unlet {list}[idx]
5485 // TODO: unlet {dict}[key]
5486 emsg("Sorry, :unlet not fully implemented yet");
5487 return FAIL;
5488}
5489
5490/*
5491 * compile "unlet var", "lock var" and "unlock var"
5492 * "arg" points to "var".
5493 */
5494 static char_u *
5495compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5496{
5497 char_u *p = arg;
5498
5499 if (eap->cmdidx != CMD_unlet)
5500 {
5501 emsg("Sorry, :lock and unlock not implemented yet");
5502 return NULL;
5503 }
5504
5505 if (*p == '!')
5506 {
5507 p = skipwhite(p + 1);
5508 eap->forceit = TRUE;
5509 }
5510
5511 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5512 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5513}
5514
5515/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516 * Compile an :import command.
5517 */
5518 static char_u *
5519compile_import(char_u *arg, cctx_T *cctx)
5520{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005521 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005522}
5523
5524/*
5525 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5526 */
5527 static int
5528compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5529{
5530 garray_T *instr = &cctx->ctx_instr;
5531 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5532
5533 if (endlabel == NULL)
5534 return FAIL;
5535 endlabel->el_next = *el;
5536 *el = endlabel;
5537 endlabel->el_end_label = instr->ga_len;
5538
5539 generate_JUMP(cctx, when, 0);
5540 return OK;
5541}
5542
5543 static void
5544compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5545{
5546 garray_T *instr = &cctx->ctx_instr;
5547
5548 while (*el != NULL)
5549 {
5550 endlabel_T *cur = (*el);
5551 isn_T *isn;
5552
5553 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5554 isn->isn_arg.jump.jump_where = instr->ga_len;
5555 *el = cur->el_next;
5556 vim_free(cur);
5557 }
5558}
5559
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005560 static void
5561compile_free_jump_to_end(endlabel_T **el)
5562{
5563 while (*el != NULL)
5564 {
5565 endlabel_T *cur = (*el);
5566
5567 *el = cur->el_next;
5568 vim_free(cur);
5569 }
5570}
5571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005572/*
5573 * Create a new scope and set up the generic items.
5574 */
5575 static scope_T *
5576new_scope(cctx_T *cctx, scopetype_T type)
5577{
5578 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5579
5580 if (scope == NULL)
5581 return NULL;
5582 scope->se_outer = cctx->ctx_scope;
5583 cctx->ctx_scope = scope;
5584 scope->se_type = type;
5585 scope->se_local_count = cctx->ctx_locals.ga_len;
5586 return scope;
5587}
5588
5589/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005590 * Free the current scope and go back to the outer scope.
5591 */
5592 static void
5593drop_scope(cctx_T *cctx)
5594{
5595 scope_T *scope = cctx->ctx_scope;
5596
5597 if (scope == NULL)
5598 {
5599 iemsg("calling drop_scope() without a scope");
5600 return;
5601 }
5602 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005603 switch (scope->se_type)
5604 {
5605 case IF_SCOPE:
5606 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5607 case FOR_SCOPE:
5608 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5609 case WHILE_SCOPE:
5610 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5611 case TRY_SCOPE:
5612 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5613 case NO_SCOPE:
5614 case BLOCK_SCOPE:
5615 break;
5616 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005617 vim_free(scope);
5618}
5619
5620/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005621 * compile "if expr"
5622 *
5623 * "if expr" Produces instructions:
5624 * EVAL expr Push result of "expr"
5625 * JUMP_IF_FALSE end
5626 * ... body ...
5627 * end:
5628 *
5629 * "if expr | else" Produces instructions:
5630 * EVAL expr Push result of "expr"
5631 * JUMP_IF_FALSE else
5632 * ... body ...
5633 * JUMP_ALWAYS end
5634 * else:
5635 * ... body ...
5636 * end:
5637 *
5638 * "if expr1 | elseif expr2 | else" Produces instructions:
5639 * EVAL expr Push result of "expr"
5640 * JUMP_IF_FALSE elseif
5641 * ... body ...
5642 * JUMP_ALWAYS end
5643 * elseif:
5644 * EVAL expr Push result of "expr"
5645 * JUMP_IF_FALSE else
5646 * ... body ...
5647 * JUMP_ALWAYS end
5648 * else:
5649 * ... body ...
5650 * end:
5651 */
5652 static char_u *
5653compile_if(char_u *arg, cctx_T *cctx)
5654{
5655 char_u *p = arg;
5656 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005657 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658 scope_T *scope;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005659 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005660
Bram Moolenaara5565e42020-05-09 15:44:01 +02005661 CLEAR_FIELD(ppconst);
5662 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005663 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005664 clear_ppconst(&ppconst);
5665 return NULL;
5666 }
5667 if (instr->ga_len == instr_count && ppconst.pp_used == 1)
5668 {
5669 // The expression results in a constant.
5670 // TODO: how about nesting?
5671 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? FALSE : TRUE;
5672 clear_ppconst(&ppconst);
5673 }
5674 else
5675 {
5676 // Not a constant, generate instructions for the expression.
5677 cctx->ctx_skip = MAYBE;
5678 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005679 return NULL;
5680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681
5682 scope = new_scope(cctx, IF_SCOPE);
5683 if (scope == NULL)
5684 return NULL;
5685
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005686 if (cctx->ctx_skip == MAYBE)
5687 {
5688 // "where" is set when ":elseif", "else" or ":endif" is found
5689 scope->se_u.se_if.is_if_label = instr->ga_len;
5690 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5691 }
5692 else
5693 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005694
5695 return p;
5696}
5697
5698 static char_u *
5699compile_elseif(char_u *arg, cctx_T *cctx)
5700{
5701 char_u *p = arg;
5702 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005703 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005704 isn_T *isn;
5705 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005706 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005707
5708 if (scope == NULL || scope->se_type != IF_SCOPE)
5709 {
5710 emsg(_(e_elseif_without_if));
5711 return NULL;
5712 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005713 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005714
Bram Moolenaar158906c2020-02-06 20:39:45 +01005715 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005716 {
5717 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005718 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005719 return NULL;
5720 // previous "if" or "elseif" jumps here
5721 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5722 isn->isn_arg.jump.jump_where = instr->ga_len;
5723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005724
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005725 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005726 CLEAR_FIELD(ppconst);
5727 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005728 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005729 clear_ppconst(&ppconst);
5730 return NULL;
5731 }
5732 if (instr->ga_len == instr_count && ppconst.pp_used == 1)
5733 {
5734 // The expression results in a constant.
5735 // TODO: how about nesting?
5736 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? FALSE : TRUE;
5737 clear_ppconst(&ppconst);
5738 scope->se_u.se_if.is_if_label = -1;
5739 }
5740 else
5741 {
5742 // Not a constant, generate instructions for the expression.
5743 cctx->ctx_skip = MAYBE;
5744 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005745 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005747 // "where" is set when ":elseif", "else" or ":endif" is found
5748 scope->se_u.se_if.is_if_label = instr->ga_len;
5749 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5750 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005751
5752 return p;
5753}
5754
5755 static char_u *
5756compile_else(char_u *arg, cctx_T *cctx)
5757{
5758 char_u *p = arg;
5759 garray_T *instr = &cctx->ctx_instr;
5760 isn_T *isn;
5761 scope_T *scope = cctx->ctx_scope;
5762
5763 if (scope == NULL || scope->se_type != IF_SCOPE)
5764 {
5765 emsg(_(e_else_without_if));
5766 return NULL;
5767 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005768 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005769
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005770 // jump from previous block to the end, unless the else block is empty
5771 if (cctx->ctx_skip == MAYBE)
5772 {
5773 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005774 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005775 return NULL;
5776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777
Bram Moolenaar158906c2020-02-06 20:39:45 +01005778 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005779 {
5780 if (scope->se_u.se_if.is_if_label >= 0)
5781 {
5782 // previous "if" or "elseif" jumps here
5783 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5784 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005785 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005786 }
5787 }
5788
5789 if (cctx->ctx_skip != MAYBE)
5790 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005791
5792 return p;
5793}
5794
5795 static char_u *
5796compile_endif(char_u *arg, cctx_T *cctx)
5797{
5798 scope_T *scope = cctx->ctx_scope;
5799 ifscope_T *ifscope;
5800 garray_T *instr = &cctx->ctx_instr;
5801 isn_T *isn;
5802
5803 if (scope == NULL || scope->se_type != IF_SCOPE)
5804 {
5805 emsg(_(e_endif_without_if));
5806 return NULL;
5807 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005808 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005809 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005810
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005811 if (scope->se_u.se_if.is_if_label >= 0)
5812 {
5813 // previous "if" or "elseif" jumps here
5814 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5815 isn->isn_arg.jump.jump_where = instr->ga_len;
5816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005817 // Fill in the "end" label in jumps at the end of the blocks.
5818 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005819 // TODO: this should restore the value from before the :if
5820 cctx->ctx_skip = MAYBE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005821
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005822 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005823 return arg;
5824}
5825
5826/*
5827 * compile "for var in expr"
5828 *
5829 * Produces instructions:
5830 * PUSHNR -1
5831 * STORE loop-idx Set index to -1
5832 * EVAL expr Push result of "expr"
5833 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5834 * - if beyond end, jump to "end"
5835 * - otherwise get item from list and push it
5836 * STORE var Store item in "var"
5837 * ... body ...
5838 * JUMP top Jump back to repeat
5839 * end: DROP Drop the result of "expr"
5840 *
5841 */
5842 static char_u *
5843compile_for(char_u *arg, cctx_T *cctx)
5844{
5845 char_u *p;
5846 size_t varlen;
5847 garray_T *instr = &cctx->ctx_instr;
5848 garray_T *stack = &cctx->ctx_type_stack;
5849 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005850 lvar_T *loop_lvar; // loop iteration variable
5851 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852 type_T *vartype;
5853
5854 // TODO: list of variables: "for [key, value] in dict"
5855 // parse "var"
5856 for (p = arg; eval_isnamec1(*p); ++p)
5857 ;
5858 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005859 var_lvar = lookup_local(arg, varlen, cctx);
5860 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861 {
5862 semsg(_("E1023: variable already defined: %s"), arg);
5863 return NULL;
5864 }
5865
5866 // consume "in"
5867 p = skipwhite(p);
5868 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5869 {
5870 emsg(_(e_missing_in));
5871 return NULL;
5872 }
5873 p = skipwhite(p + 2);
5874
5875
5876 scope = new_scope(cctx, FOR_SCOPE);
5877 if (scope == NULL)
5878 return NULL;
5879
5880 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005881 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5882 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005883 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005884 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005885 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005888
5889 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005890 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5891 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005892 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005893 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005894 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005898 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005899
5900 // compile "expr", it remains on the stack until "endfor"
5901 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005902 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005903 {
5904 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005907
5908 // now we know the type of "var"
5909 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5910 if (vartype->tt_type != VAR_LIST)
5911 {
5912 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005913 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005914 return NULL;
5915 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005916 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005917 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005918
5919 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005920 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005921
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005922 generate_FOR(cctx, loop_lvar->lv_idx);
5923 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005924
5925 return arg;
5926}
5927
5928/*
5929 * compile "endfor"
5930 */
5931 static char_u *
5932compile_endfor(char_u *arg, cctx_T *cctx)
5933{
5934 garray_T *instr = &cctx->ctx_instr;
5935 scope_T *scope = cctx->ctx_scope;
5936 forscope_T *forscope;
5937 isn_T *isn;
5938
5939 if (scope == NULL || scope->se_type != FOR_SCOPE)
5940 {
5941 emsg(_(e_for));
5942 return NULL;
5943 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005944 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005945 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005946 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005947
5948 // At end of ":for" scope jump back to the FOR instruction.
5949 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5950
5951 // Fill in the "end" label in the FOR statement so it can jump here
5952 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5953 isn->isn_arg.forloop.for_end = instr->ga_len;
5954
5955 // Fill in the "end" label any BREAK statements
5956 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5957
5958 // Below the ":for" scope drop the "expr" list from the stack.
5959 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5960 return NULL;
5961
5962 vim_free(scope);
5963
5964 return arg;
5965}
5966
5967/*
5968 * compile "while expr"
5969 *
5970 * Produces instructions:
5971 * top: EVAL expr Push result of "expr"
5972 * JUMP_IF_FALSE end jump if false
5973 * ... body ...
5974 * JUMP top Jump back to repeat
5975 * end:
5976 *
5977 */
5978 static char_u *
5979compile_while(char_u *arg, cctx_T *cctx)
5980{
5981 char_u *p = arg;
5982 garray_T *instr = &cctx->ctx_instr;
5983 scope_T *scope;
5984
5985 scope = new_scope(cctx, WHILE_SCOPE);
5986 if (scope == NULL)
5987 return NULL;
5988
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005989 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990
5991 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005992 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 return NULL;
5994
5995 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005996 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005997 JUMP_IF_FALSE, cctx) == FAIL)
5998 return FAIL;
5999
6000 return p;
6001}
6002
6003/*
6004 * compile "endwhile"
6005 */
6006 static char_u *
6007compile_endwhile(char_u *arg, cctx_T *cctx)
6008{
6009 scope_T *scope = cctx->ctx_scope;
6010
6011 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6012 {
6013 emsg(_(e_while));
6014 return NULL;
6015 }
6016 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006017 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018
6019 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006020 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021
6022 // Fill in the "end" label in the WHILE statement so it can jump here.
6023 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006024 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006025
6026 vim_free(scope);
6027
6028 return arg;
6029}
6030
6031/*
6032 * compile "continue"
6033 */
6034 static char_u *
6035compile_continue(char_u *arg, cctx_T *cctx)
6036{
6037 scope_T *scope = cctx->ctx_scope;
6038
6039 for (;;)
6040 {
6041 if (scope == NULL)
6042 {
6043 emsg(_(e_continue));
6044 return NULL;
6045 }
6046 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6047 break;
6048 scope = scope->se_outer;
6049 }
6050
6051 // Jump back to the FOR or WHILE instruction.
6052 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006053 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6054 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055 return arg;
6056}
6057
6058/*
6059 * compile "break"
6060 */
6061 static char_u *
6062compile_break(char_u *arg, cctx_T *cctx)
6063{
6064 scope_T *scope = cctx->ctx_scope;
6065 endlabel_T **el;
6066
6067 for (;;)
6068 {
6069 if (scope == NULL)
6070 {
6071 emsg(_(e_break));
6072 return NULL;
6073 }
6074 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6075 break;
6076 scope = scope->se_outer;
6077 }
6078
6079 // Jump to the end of the FOR or WHILE loop.
6080 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006081 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006083 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006084 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6085 return FAIL;
6086
6087 return arg;
6088}
6089
6090/*
6091 * compile "{" start of block
6092 */
6093 static char_u *
6094compile_block(char_u *arg, cctx_T *cctx)
6095{
6096 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6097 return NULL;
6098 return skipwhite(arg + 1);
6099}
6100
6101/*
6102 * compile end of block: drop one scope
6103 */
6104 static void
6105compile_endblock(cctx_T *cctx)
6106{
6107 scope_T *scope = cctx->ctx_scope;
6108
6109 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006110 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111 vim_free(scope);
6112}
6113
6114/*
6115 * compile "try"
6116 * Creates a new scope for the try-endtry, pointing to the first catch and
6117 * finally.
6118 * Creates another scope for the "try" block itself.
6119 * TRY instruction sets up exception handling at runtime.
6120 *
6121 * "try"
6122 * TRY -> catch1, -> finally push trystack entry
6123 * ... try block
6124 * "throw {exception}"
6125 * EVAL {exception}
6126 * THROW create exception
6127 * ... try block
6128 * " catch {expr}"
6129 * JUMP -> finally
6130 * catch1: PUSH exeception
6131 * EVAL {expr}
6132 * MATCH
6133 * JUMP nomatch -> catch2
6134 * CATCH remove exception
6135 * ... catch block
6136 * " catch"
6137 * JUMP -> finally
6138 * catch2: CATCH remove exception
6139 * ... catch block
6140 * " finally"
6141 * finally:
6142 * ... finally block
6143 * " endtry"
6144 * ENDTRY pop trystack entry, may rethrow
6145 */
6146 static char_u *
6147compile_try(char_u *arg, cctx_T *cctx)
6148{
6149 garray_T *instr = &cctx->ctx_instr;
6150 scope_T *try_scope;
6151 scope_T *scope;
6152
6153 // scope that holds the jumps that go to catch/finally/endtry
6154 try_scope = new_scope(cctx, TRY_SCOPE);
6155 if (try_scope == NULL)
6156 return NULL;
6157
6158 // "catch" is set when the first ":catch" is found.
6159 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006160 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006161 if (generate_instr(cctx, ISN_TRY) == NULL)
6162 return NULL;
6163
6164 // scope for the try block itself
6165 scope = new_scope(cctx, BLOCK_SCOPE);
6166 if (scope == NULL)
6167 return NULL;
6168
6169 return arg;
6170}
6171
6172/*
6173 * compile "catch {expr}"
6174 */
6175 static char_u *
6176compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6177{
6178 scope_T *scope = cctx->ctx_scope;
6179 garray_T *instr = &cctx->ctx_instr;
6180 char_u *p;
6181 isn_T *isn;
6182
6183 // end block scope from :try or :catch
6184 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6185 compile_endblock(cctx);
6186 scope = cctx->ctx_scope;
6187
6188 // Error if not in a :try scope
6189 if (scope == NULL || scope->se_type != TRY_SCOPE)
6190 {
6191 emsg(_(e_catch));
6192 return NULL;
6193 }
6194
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006195 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196 {
6197 emsg(_("E1033: catch unreachable after catch-all"));
6198 return NULL;
6199 }
6200
6201 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006202 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006203 JUMP_ALWAYS, cctx) == FAIL)
6204 return NULL;
6205
6206 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006207 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006208 if (isn->isn_arg.try.try_catch == 0)
6209 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006210 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211 {
6212 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006213 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214 isn->isn_arg.jump.jump_where = instr->ga_len;
6215 }
6216
6217 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006218 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006220 scope->se_u.se_try.ts_caught_all = TRUE;
6221 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 }
6223 else
6224 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006225 char_u *end;
6226 char_u *pat;
6227 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006228 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006229 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006231 // Push v:exception, push {expr} and MATCH
6232 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6233
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006234 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006235 if (*end != *p)
6236 {
6237 semsg(_("E1067: Separator mismatch: %s"), p);
6238 vim_free(tofree);
6239 return FAIL;
6240 }
6241 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006242 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006243 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006244 len = (int)(end - tofree);
6245 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006246 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006247 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006248 if (pat == NULL)
6249 return FAIL;
6250 if (generate_PUSHS(cctx, pat) == FAIL)
6251 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6254 return NULL;
6255
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006256 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6258 return NULL;
6259 }
6260
6261 if (generate_instr(cctx, ISN_CATCH) == NULL)
6262 return NULL;
6263
6264 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6265 return NULL;
6266 return p;
6267}
6268
6269 static char_u *
6270compile_finally(char_u *arg, cctx_T *cctx)
6271{
6272 scope_T *scope = cctx->ctx_scope;
6273 garray_T *instr = &cctx->ctx_instr;
6274 isn_T *isn;
6275
6276 // end block scope from :try or :catch
6277 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6278 compile_endblock(cctx);
6279 scope = cctx->ctx_scope;
6280
6281 // Error if not in a :try scope
6282 if (scope == NULL || scope->se_type != TRY_SCOPE)
6283 {
6284 emsg(_(e_finally));
6285 return NULL;
6286 }
6287
6288 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006289 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290 if (isn->isn_arg.try.try_finally != 0)
6291 {
6292 emsg(_(e_finally_dup));
6293 return NULL;
6294 }
6295
6296 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006297 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298
Bram Moolenaar585fea72020-04-02 22:33:21 +02006299 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006300 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 {
6302 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006303 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 isn->isn_arg.jump.jump_where = instr->ga_len;
6305 }
6306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 // TODO: set index in ts_finally_label jumps
6308
6309 return arg;
6310}
6311
6312 static char_u *
6313compile_endtry(char_u *arg, cctx_T *cctx)
6314{
6315 scope_T *scope = cctx->ctx_scope;
6316 garray_T *instr = &cctx->ctx_instr;
6317 isn_T *isn;
6318
6319 // end block scope from :catch or :finally
6320 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6321 compile_endblock(cctx);
6322 scope = cctx->ctx_scope;
6323
6324 // Error if not in a :try scope
6325 if (scope == NULL || scope->se_type != TRY_SCOPE)
6326 {
6327 if (scope == NULL)
6328 emsg(_(e_no_endtry));
6329 else if (scope->se_type == WHILE_SCOPE)
6330 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006331 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332 emsg(_(e_endfor));
6333 else
6334 emsg(_(e_endif));
6335 return NULL;
6336 }
6337
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006338 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006339 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6340 {
6341 emsg(_("E1032: missing :catch or :finally"));
6342 return NULL;
6343 }
6344
6345 // Fill in the "end" label in jumps at the end of the blocks, if not done
6346 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006347 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348
6349 // End :catch or :finally scope: set value in ISN_TRY instruction
6350 if (isn->isn_arg.try.try_finally == 0)
6351 isn->isn_arg.try.try_finally = instr->ga_len;
6352 compile_endblock(cctx);
6353
6354 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6355 return NULL;
6356 return arg;
6357}
6358
6359/*
6360 * compile "throw {expr}"
6361 */
6362 static char_u *
6363compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6364{
6365 char_u *p = skipwhite(arg);
6366
Bram Moolenaara5565e42020-05-09 15:44:01 +02006367 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006368 return NULL;
6369 if (may_generate_2STRING(-1, cctx) == FAIL)
6370 return NULL;
6371 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6372 return NULL;
6373
6374 return p;
6375}
6376
6377/*
6378 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006379 * compile "echomsg expr"
6380 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006381 * compile "execute expr"
6382 */
6383 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006384compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006385{
6386 char_u *p = arg;
6387 int count = 0;
6388
6389 for (;;)
6390 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006391 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006392 return NULL;
6393 ++count;
6394 p = skipwhite(p);
6395 if (ends_excmd(*p))
6396 break;
6397 }
6398
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006399 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6400 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6401 else if (cmdidx == CMD_execute)
6402 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6403 else if (cmdidx == CMD_echomsg)
6404 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6405 else
6406 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006407 return p;
6408}
6409
6410/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006411 * A command that is not compiled, execute with legacy code.
6412 */
6413 static char_u *
6414compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6415{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006416 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006417 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006418
6419 if (cctx->ctx_skip == TRUE)
6420 goto theend;
6421
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006422 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6423 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006424 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6425 {
6426 // expand filename in "syntax include [@group] filename"
6427 has_expr = TRUE;
6428 eap->arg = skipwhite(eap->arg + 7);
6429 if (*eap->arg == '@')
6430 eap->arg = skiptowhite(eap->arg);
6431 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006432
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006433 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006434 {
6435 int count = 0;
6436 char_u *start = skipwhite(line);
6437
6438 // :cmd xxx`=expr1`yyy`=expr2`zzz
6439 // PUSHS ":cmd xxx"
6440 // eval expr1
6441 // PUSHS "yyy"
6442 // eval expr2
6443 // PUSHS "zzz"
6444 // EXECCONCAT 5
6445 for (;;)
6446 {
6447 if (p > start)
6448 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006449 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006450 ++count;
6451 }
6452 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006453 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006454 return NULL;
6455 may_generate_2STRING(-1, cctx);
6456 ++count;
6457 p = skipwhite(p);
6458 if (*p != '`')
6459 {
6460 emsg(_("E1083: missing backtick"));
6461 return NULL;
6462 }
6463 start = p + 1;
6464
6465 p = (char_u *)strstr((char *)start, "`=");
6466 if (p == NULL)
6467 {
6468 if (*skipwhite(start) != NUL)
6469 {
6470 generate_PUSHS(cctx, vim_strsave(start));
6471 ++count;
6472 }
6473 break;
6474 }
6475 }
6476 generate_EXECCONCAT(cctx, count);
6477 }
6478 else
6479 generate_EXEC(cctx, line);
6480
6481theend:
6482 return (char_u *)"";
6483}
6484
6485/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006486 * Add a function to the list of :def functions.
6487 * This "sets ufunc->uf_dfunc_idx" but the function isn't compiled yet.
6488 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006489 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006490add_def_function(ufunc_T *ufunc)
6491{
6492 dfunc_T *dfunc;
6493
6494 // Add the function to "def_functions".
6495 if (ga_grow(&def_functions, 1) == FAIL)
6496 return FAIL;
6497 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6498 CLEAR_POINTER(dfunc);
6499 dfunc->df_idx = def_functions.ga_len;
6500 ufunc->uf_dfunc_idx = dfunc->df_idx;
6501 dfunc->df_ufunc = ufunc;
6502 ++def_functions.ga_len;
6503 return OK;
6504}
6505
6506/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006507 * After ex_function() has collected all the function lines: parse and compile
6508 * the lines into instructions.
6509 * Adds the function to "def_functions".
6510 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6511 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006512 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006513 * This can be used recursively through compile_lambda(), which may reallocate
6514 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006515 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006517 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006518compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006519{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 char_u *line = NULL;
6521 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 char *errormsg = NULL; // error message
6523 int had_return = FALSE;
6524 cctx_T cctx;
6525 garray_T *instr;
6526 int called_emsg_before = called_emsg;
6527 int ret = FAIL;
6528 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006529 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006530 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006531
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006532 // When using a function that was compiled before: Free old instructions.
6533 // Otherwise add a new entry in "def_functions".
Bram Moolenaar09689a02020-05-09 22:50:08 +02006534 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006535 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006536 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6537 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006538 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006540 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006541 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542
Bram Moolenaara80faa82020-04-12 19:37:17 +02006543 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 cctx.ctx_ufunc = ufunc;
6545 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006546 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6548 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6549 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6550 cctx.ctx_type_list = &ufunc->uf_type_list;
6551 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6552 instr = &cctx.ctx_instr;
6553
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006554 // Set the context to the function, it may be compiled when called from
6555 // another script. Set the script version to the most modern one.
6556 // The line number will be set in next_line_from_context().
6557 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6559
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006560 // Make sure error messages are OK.
6561 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6562 if (do_estack_push)
6563 estack_push_ufunc(ufunc, 1);
6564
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006565 if (ufunc->uf_def_args.ga_len > 0)
6566 {
6567 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006568 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006569 int i;
6570 char_u *arg;
6571 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6572
6573 // Produce instructions for the default values of optional arguments.
6574 // Store the instruction index in uf_def_arg_idx[] so that we know
6575 // where to start when the function is called, depending on the number
6576 // of arguments.
6577 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6578 if (ufunc->uf_def_arg_idx == NULL)
6579 goto erret;
6580 for (i = 0; i < count; ++i)
6581 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006582 garray_T *stack = &cctx.ctx_type_stack;
6583 type_T *val_type;
6584 int arg_idx = first_def_arg + i;
6585
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006586 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6587 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006588 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006589 goto erret;
6590
6591 // If no type specified use the type of the default value.
6592 // Otherwise check that the default value type matches the
6593 // specified type.
6594 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6595 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6596 ufunc->uf_arg_types[arg_idx] = val_type;
6597 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6598 == FAIL)
6599 {
6600 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6601 arg_idx + 1);
6602 goto erret;
6603 }
6604
6605 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006606 goto erret;
6607 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006608 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6609 }
6610
6611 /*
6612 * Loop over all the lines of the function and generate instructions.
6613 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006614 for (;;)
6615 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006616 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006617 int starts_with_colon = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006618
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006619 // Bail out on the first error to avoid a flood of errors and report
6620 // the right line number when inside try/catch.
6621 if (emsg_before != called_emsg)
6622 goto erret;
6623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006624 if (line != NULL && *line == '|')
6625 // the line continues after a '|'
6626 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006627 else if (line != NULL && *line != NUL
6628 && !(*line == '#' && (line == cctx.ctx_line_start
6629 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006630 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006631 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632 goto erret;
6633 }
6634 else
6635 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006636 line = next_line_from_context(&cctx);
6637 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006638 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006640 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006641 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642
6643 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006644 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006645 ea.cmdlinep = &line;
6646 ea.cmd = skipwhite(line);
6647
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006648 // Some things can be recognized by the first character.
6649 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006650 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006651 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006652 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006653 if (ea.cmd[1] != '{')
6654 {
6655 line = (char_u *)"";
6656 continue;
6657 }
6658 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006660 case '}':
6661 {
6662 // "}" ends a block scope
6663 scopetype_T stype = cctx.ctx_scope == NULL
6664 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006666 if (stype == BLOCK_SCOPE)
6667 {
6668 compile_endblock(&cctx);
6669 line = ea.cmd;
6670 }
6671 else
6672 {
6673 emsg(_("E1025: using } outside of a block scope"));
6674 goto erret;
6675 }
6676 if (line != NULL)
6677 line = skipwhite(ea.cmd + 1);
6678 continue;
6679 }
6680
6681 case '{':
6682 // "{" starts a block scope
6683 // "{'a': 1}->func() is something else
6684 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6685 {
6686 line = compile_block(ea.cmd, &cctx);
6687 continue;
6688 }
6689 break;
6690
6691 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006692 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006693 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 }
6695
6696 /*
6697 * COMMAND MODIFIERS
6698 */
6699 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6700 {
6701 if (errormsg != NULL)
6702 goto erret;
6703 // empty line or comment
6704 line = (char_u *)"";
6705 continue;
6706 }
6707
6708 // Skip ":call" to get to the function name.
6709 if (checkforcmd(&ea.cmd, "call", 3))
6710 ea.cmd = skipwhite(ea.cmd);
6711
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006712 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006714 char_u *pskip;
6715
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006716 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006717 // find what follows.
6718 // Skip over "var.member", "var[idx]" and the like.
6719 // Also "&opt = val", "$ENV = val" and "@r = val".
6720 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006721 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006722 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006723 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006724 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006725 char_u *var_end;
6726 int oplen;
6727 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006729 var_end = find_name_end(pskip, NULL, NULL,
6730 FNE_CHECK_START | FNE_INCL_BR);
6731 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006732 if (oplen > 0)
6733 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006734 size_t len = p - ea.cmd;
6735
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006736 // Recognize an assignment if we recognize the variable
6737 // name:
6738 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006739 // "local = expr" where "local" is a local var.
6740 // "script = expr" where "script" is a script-local var.
6741 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006742 // "&opt = expr"
6743 // "$ENV = expr"
6744 // "@r = expr"
6745 if (*ea.cmd == '&'
6746 || *ea.cmd == '$'
6747 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006748 || ((len) > 2 && ea.cmd[1] == ':')
6749 || lookup_local(ea.cmd, len, &cctx) != NULL
6750 || lookup_arg(ea.cmd, len, NULL, NULL,
6751 NULL, &cctx) == OK
6752 || lookup_script(ea.cmd, len) == OK
6753 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006754 {
6755 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006756 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006757 goto erret;
6758 continue;
6759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760 }
6761 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006762
6763 if (*ea.cmd == '[')
6764 {
6765 // [var, var] = expr
6766 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6767 if (line == NULL)
6768 goto erret;
6769 if (line != ea.cmd)
6770 continue;
6771 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 }
6773
6774 /*
6775 * COMMAND after range
6776 */
6777 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006778 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006779 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006780 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006781
6782 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6783 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006784 if (cctx.ctx_skip == TRUE)
6785 {
6786 line += STRLEN(line);
6787 continue;
6788 }
6789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790 // Expression or function call.
6791 if (ea.cmdidx == CMD_eval)
6792 {
6793 p = ea.cmd;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006794 if (compile_expr0(&p, &cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 goto erret;
6796
6797 // drop the return value
6798 generate_instr_drop(&cctx, ISN_DROP, 1);
6799 line = p;
6800 continue;
6801 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006802 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803 iemsg("Command from find_ex_command() not handled");
6804 goto erret;
6805 }
6806
6807 p = skipwhite(p);
6808
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006809 if (cctx.ctx_skip == TRUE
6810 && ea.cmdidx != CMD_elseif
6811 && ea.cmdidx != CMD_else
6812 && ea.cmdidx != CMD_endif)
6813 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006814 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006815 continue;
6816 }
6817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006818 switch (ea.cmdidx)
6819 {
6820 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006821 ea.arg = p;
6822 line = compile_nested_function(&ea, &cctx);
6823 break;
6824
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006826 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006827 goto erret;
6828
6829 case CMD_return:
6830 line = compile_return(p, set_return_type, &cctx);
6831 had_return = TRUE;
6832 break;
6833
6834 case CMD_let:
6835 case CMD_const:
6836 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006837 if (line == p)
6838 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839 break;
6840
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006841 case CMD_unlet:
6842 case CMD_unlockvar:
6843 case CMD_lockvar:
6844 line = compile_unletlock(p, &ea, &cctx);
6845 break;
6846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847 case CMD_import:
6848 line = compile_import(p, &cctx);
6849 break;
6850
6851 case CMD_if:
6852 line = compile_if(p, &cctx);
6853 break;
6854 case CMD_elseif:
6855 line = compile_elseif(p, &cctx);
6856 break;
6857 case CMD_else:
6858 line = compile_else(p, &cctx);
6859 break;
6860 case CMD_endif:
6861 line = compile_endif(p, &cctx);
6862 break;
6863
6864 case CMD_while:
6865 line = compile_while(p, &cctx);
6866 break;
6867 case CMD_endwhile:
6868 line = compile_endwhile(p, &cctx);
6869 break;
6870
6871 case CMD_for:
6872 line = compile_for(p, &cctx);
6873 break;
6874 case CMD_endfor:
6875 line = compile_endfor(p, &cctx);
6876 break;
6877 case CMD_continue:
6878 line = compile_continue(p, &cctx);
6879 break;
6880 case CMD_break:
6881 line = compile_break(p, &cctx);
6882 break;
6883
6884 case CMD_try:
6885 line = compile_try(p, &cctx);
6886 break;
6887 case CMD_catch:
6888 line = compile_catch(p, &cctx);
6889 break;
6890 case CMD_finally:
6891 line = compile_finally(p, &cctx);
6892 break;
6893 case CMD_endtry:
6894 line = compile_endtry(p, &cctx);
6895 break;
6896 case CMD_throw:
6897 line = compile_throw(p, &cctx);
6898 break;
6899
6900 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006901 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006902 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006903 case CMD_echomsg:
6904 case CMD_echoerr:
6905 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006906 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006907
6908 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006909 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006910 // Not recognized, execute with do_cmdline_cmd().
6911 ea.arg = p;
6912 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 break;
6914 }
6915 if (line == NULL)
6916 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006917 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918
6919 if (cctx.ctx_type_stack.ga_len < 0)
6920 {
6921 iemsg("Type stack underflow");
6922 goto erret;
6923 }
6924 }
6925
6926 if (cctx.ctx_scope != NULL)
6927 {
6928 if (cctx.ctx_scope->se_type == IF_SCOPE)
6929 emsg(_(e_endif));
6930 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6931 emsg(_(e_endwhile));
6932 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6933 emsg(_(e_endfor));
6934 else
6935 emsg(_("E1026: Missing }"));
6936 goto erret;
6937 }
6938
6939 if (!had_return)
6940 {
6941 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6942 {
6943 emsg(_("E1027: Missing return statement"));
6944 goto erret;
6945 }
6946
6947 // Return zero if there is no return at the end.
6948 generate_PUSHNR(&cctx, 0);
6949 generate_instr(&cctx, ISN_RETURN);
6950 }
6951
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006952 {
6953 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6954 + ufunc->uf_dfunc_idx;
6955 dfunc->df_deleted = FALSE;
6956 dfunc->df_instr = instr->ga_data;
6957 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006958 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006959 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006960 if (cctx.ctx_outer_used)
6961 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006962 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963
6964 ret = OK;
6965
6966erret:
6967 if (ret == FAIL)
6968 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006969 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006970 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6971 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006972
6973 for (idx = 0; idx < instr->ga_len; ++idx)
6974 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006976
Bram Moolenaar45a15082020-05-25 00:28:33 +02006977 // if using the last entry in the table we might as well remove it
6978 if (!dfunc->df_deleted
6979 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006980 --def_functions.ga_len;
Bram Moolenaar45a15082020-05-25 00:28:33 +02006981 ufunc->uf_dfunc_idx = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006982
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006983 while (cctx.ctx_scope != NULL)
6984 drop_scope(&cctx);
6985
Bram Moolenaar20431c92020-03-20 18:39:46 +01006986 // Don't execute this function body.
6987 ga_clear_strings(&ufunc->uf_lines);
6988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006989 if (errormsg != NULL)
6990 emsg(errormsg);
6991 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006992 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993 }
6994
6995 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006996 if (do_estack_push)
6997 estack_pop();
6998
Bram Moolenaar20431c92020-03-20 18:39:46 +01006999 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007000 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007002 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003}
7004
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007005 void
7006set_function_type(ufunc_T *ufunc)
7007{
7008 int varargs = ufunc->uf_va_name != NULL;
7009 int argcount = ufunc->uf_args.ga_len;
7010
7011 // Create a type for the function, with the return type and any
7012 // argument types.
7013 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7014 // The type is included in "tt_args".
7015 if (argcount > 0 || varargs)
7016 {
7017 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7018 argcount, &ufunc->uf_type_list);
7019 // Add argument types to the function type.
7020 if (func_type_add_arg_types(ufunc->uf_func_type,
7021 argcount + varargs,
7022 &ufunc->uf_type_list) == FAIL)
7023 return;
7024 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7025 ufunc->uf_func_type->tt_min_argcount =
7026 argcount - ufunc->uf_def_args.ga_len;
7027 if (ufunc->uf_arg_types == NULL)
7028 {
7029 int i;
7030
7031 // lambda does not have argument types.
7032 for (i = 0; i < argcount; ++i)
7033 ufunc->uf_func_type->tt_args[i] = &t_any;
7034 }
7035 else
7036 mch_memmove(ufunc->uf_func_type->tt_args,
7037 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7038 if (varargs)
7039 {
7040 ufunc->uf_func_type->tt_args[argcount] =
7041 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7042 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7043 }
7044 }
7045 else
7046 // No arguments, can use a predefined type.
7047 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7048 argcount, &ufunc->uf_type_list);
7049}
7050
7051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052/*
7053 * Delete an instruction, free what it contains.
7054 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007055 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056delete_instr(isn_T *isn)
7057{
7058 switch (isn->isn_type)
7059 {
7060 case ISN_EXEC:
7061 case ISN_LOADENV:
7062 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007063 case ISN_LOADB:
7064 case ISN_LOADW:
7065 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007067 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 case ISN_PUSHEXC:
7069 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007070 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007072 case ISN_STOREB:
7073 case ISN_STOREW:
7074 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007075 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 vim_free(isn->isn_arg.string);
7077 break;
7078
7079 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007080 case ISN_STORES:
7081 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082 break;
7083
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007084 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007085 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007086 vim_free(isn->isn_arg.unlet.ul_name);
7087 break;
7088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089 case ISN_STOREOPT:
7090 vim_free(isn->isn_arg.storeopt.so_name);
7091 break;
7092
7093 case ISN_PUSHBLOB: // push blob isn_arg.blob
7094 blob_unref(isn->isn_arg.blob);
7095 break;
7096
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007097 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007098#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007099 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007100#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007101 break;
7102
7103 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007104#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007105 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007106#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007107 break;
7108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007109 case ISN_UCALL:
7110 vim_free(isn->isn_arg.ufunc.cuf_name);
7111 break;
7112
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007113 case ISN_FUNCREF:
7114 {
7115 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7116 + isn->isn_arg.funcref.fr_func;
7117 func_ptr_unref(dfunc->df_ufunc);
7118 }
7119 break;
7120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 case ISN_2BOOL:
7122 case ISN_2STRING:
7123 case ISN_ADDBLOB:
7124 case ISN_ADDLIST:
7125 case ISN_BCALL:
7126 case ISN_CATCH:
7127 case ISN_CHECKNR:
7128 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007129 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130 case ISN_COMPAREANY:
7131 case ISN_COMPAREBLOB:
7132 case ISN_COMPAREBOOL:
7133 case ISN_COMPAREDICT:
7134 case ISN_COMPAREFLOAT:
7135 case ISN_COMPAREFUNC:
7136 case ISN_COMPARELIST:
7137 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 case ISN_COMPARESPECIAL:
7139 case ISN_COMPARESTRING:
7140 case ISN_CONCAT:
7141 case ISN_DCALL:
7142 case ISN_DROP:
7143 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007144 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007145 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007147 case ISN_EXECCONCAT:
7148 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007150 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007151 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007152 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007153 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 case ISN_JUMP:
7155 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007156 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157 case ISN_LOADSCRIPT:
7158 case ISN_LOADREG:
7159 case ISN_LOADV:
7160 case ISN_NEGATENR:
7161 case ISN_NEWDICT:
7162 case ISN_NEWLIST:
7163 case ISN_OPNR:
7164 case ISN_OPFLOAT:
7165 case ISN_OPANY:
7166 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007167 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 case ISN_PUSHF:
7169 case ISN_PUSHNR:
7170 case ISN_PUSHBOOL:
7171 case ISN_PUSHSPEC:
7172 case ISN_RETURN:
7173 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007174 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007175 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007176 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007177 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007179 case ISN_STOREDICT:
7180 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007181 case ISN_THROW:
7182 case ISN_TRY:
7183 // nothing allocated
7184 break;
7185 }
7186}
7187
7188/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007189 * Free all instructions for "dfunc".
7190 */
7191 static void
7192delete_def_function_contents(dfunc_T *dfunc)
7193{
7194 int idx;
7195
7196 ga_clear(&dfunc->df_def_args_isn);
7197
7198 if (dfunc->df_instr != NULL)
7199 {
7200 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7201 delete_instr(dfunc->df_instr + idx);
7202 VIM_CLEAR(dfunc->df_instr);
7203 }
7204
7205 dfunc->df_deleted = TRUE;
7206}
7207
7208/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007209 * When a user function is deleted, delete any associated def function.
7210 */
7211 void
7212delete_def_function(ufunc_T *ufunc)
7213{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214 if (ufunc->uf_dfunc_idx >= 0)
7215 {
7216 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7217 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007218
Bram Moolenaar20431c92020-03-20 18:39:46 +01007219 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007220 }
7221}
7222
7223#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007224/*
7225 * Free all functions defined with ":def".
7226 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227 void
7228free_def_functions(void)
7229{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007230 int idx;
7231
7232 for (idx = 0; idx < def_functions.ga_len; ++idx)
7233 {
7234 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7235
7236 delete_def_function_contents(dfunc);
7237 }
7238
7239 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007240}
7241#endif
7242
7243
7244#endif // FEAT_EVAL