blob: 12b125a6868d3c8c014c8ec26bb312770bfca9ed [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 Moolenaar8a7d6542020-01-26 15:56:19 +01001089 * Generate an ISN_STORE instruction.
1090 */
1091 static int
1092generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1093{
1094 isn_T *isn;
1095
Bram Moolenaar080457c2020-03-03 21:53:32 +01001096 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1098 return FAIL;
1099 if (name != NULL)
1100 isn->isn_arg.string = vim_strsave(name);
1101 else
1102 isn->isn_arg.number = idx;
1103
1104 return OK;
1105}
1106
1107/*
1108 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1109 */
1110 static int
1111generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1112{
1113 isn_T *isn;
1114
Bram Moolenaar080457c2020-03-03 21:53:32 +01001115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1117 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001118 isn->isn_arg.storenr.stnr_idx = idx;
1119 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120
1121 return OK;
1122}
1123
1124/*
1125 * Generate an ISN_STOREOPT instruction
1126 */
1127 static int
1128generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1129{
1130 isn_T *isn;
1131
Bram Moolenaar080457c2020-03-03 21:53:32 +01001132 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1134 return FAIL;
1135 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1136 isn->isn_arg.storeopt.so_flags = opt_flags;
1137
1138 return OK;
1139}
1140
1141/*
1142 * Generate an ISN_LOAD or similar instruction.
1143 */
1144 static int
1145generate_LOAD(
1146 cctx_T *cctx,
1147 isntype_T isn_type,
1148 int idx,
1149 char_u *name,
1150 type_T *type)
1151{
1152 isn_T *isn;
1153
Bram Moolenaar080457c2020-03-03 21:53:32 +01001154 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1156 return FAIL;
1157 if (name != NULL)
1158 isn->isn_arg.string = vim_strsave(name);
1159 else
1160 isn->isn_arg.number = idx;
1161
1162 return OK;
1163}
1164
1165/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001166 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001167 */
1168 static int
1169generate_LOADV(
1170 cctx_T *cctx,
1171 char_u *name,
1172 int error)
1173{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001174 int di_flags;
1175 int vidx = find_vim_var(name, &di_flags);
1176 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001177
Bram Moolenaar080457c2020-03-03 21:53:32 +01001178 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001179 if (vidx < 0)
1180 {
1181 if (error)
1182 semsg(_(e_var_notfound), name);
1183 return FAIL;
1184 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001185 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001186
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001187 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001188}
1189
1190/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001191 * Generate an ISN_UNLET instruction.
1192 */
1193 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001194generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001195{
1196 isn_T *isn;
1197
1198 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001199 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001200 return FAIL;
1201 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1202 isn->isn_arg.unlet.ul_forceit = forceit;
1203
1204 return OK;
1205}
1206
1207/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 * Generate an ISN_LOADS instruction.
1209 */
1210 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001211generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001213 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001215 int sid,
1216 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217{
1218 isn_T *isn;
1219
Bram Moolenaar080457c2020-03-03 21:53:32 +01001220 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001221 if (isn_type == ISN_LOADS)
1222 isn = generate_instr_type(cctx, isn_type, type);
1223 else
1224 isn = generate_instr_drop(cctx, isn_type, 1);
1225 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001227 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1228 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229
1230 return OK;
1231}
1232
1233/*
1234 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1235 */
1236 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001237generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 cctx_T *cctx,
1239 isntype_T isn_type,
1240 int sid,
1241 int idx,
1242 type_T *type)
1243{
1244 isn_T *isn;
1245
Bram Moolenaar080457c2020-03-03 21:53:32 +01001246 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 if (isn_type == ISN_LOADSCRIPT)
1248 isn = generate_instr_type(cctx, isn_type, type);
1249 else
1250 isn = generate_instr_drop(cctx, isn_type, 1);
1251 if (isn == NULL)
1252 return FAIL;
1253 isn->isn_arg.script.script_sid = sid;
1254 isn->isn_arg.script.script_idx = idx;
1255 return OK;
1256}
1257
1258/*
1259 * Generate an ISN_NEWLIST instruction.
1260 */
1261 static int
1262generate_NEWLIST(cctx_T *cctx, int count)
1263{
1264 isn_T *isn;
1265 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 type_T *type;
1267 type_T *member;
1268
Bram Moolenaar080457c2020-03-03 21:53:32 +01001269 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1271 return FAIL;
1272 isn->isn_arg.number = count;
1273
1274 // drop the value types
1275 stack->ga_len -= count;
1276
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001277 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001278 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 if (count > 0)
1280 member = ((type_T **)stack->ga_data)[stack->ga_len];
1281 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001282 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001283 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284
1285 // add the list type to the type stack
1286 if (ga_grow(stack, 1) == FAIL)
1287 return FAIL;
1288 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1289 ++stack->ga_len;
1290
1291 return OK;
1292}
1293
1294/*
1295 * Generate an ISN_NEWDICT instruction.
1296 */
1297 static int
1298generate_NEWDICT(cctx_T *cctx, int count)
1299{
1300 isn_T *isn;
1301 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302 type_T *type;
1303 type_T *member;
1304
Bram Moolenaar080457c2020-03-03 21:53:32 +01001305 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1307 return FAIL;
1308 isn->isn_arg.number = count;
1309
1310 // drop the key and value types
1311 stack->ga_len -= 2 * count;
1312
Bram Moolenaar436472f2020-02-20 22:54:43 +01001313 // Use the first value type for the list member type. Use "void" for an
1314 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 if (count > 0)
1316 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1317 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001318 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001319 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320
1321 // add the dict type to the type stack
1322 if (ga_grow(stack, 1) == FAIL)
1323 return FAIL;
1324 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1325 ++stack->ga_len;
1326
1327 return OK;
1328}
1329
1330/*
1331 * Generate an ISN_FUNCREF instruction.
1332 */
1333 static int
1334generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1335{
1336 isn_T *isn;
1337 garray_T *stack = &cctx->ctx_type_stack;
1338
Bram Moolenaar080457c2020-03-03 21:53:32 +01001339 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1341 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001342 isn->isn_arg.funcref.fr_func = dfunc_idx;
1343 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344
1345 if (ga_grow(stack, 1) == FAIL)
1346 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001347 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 // TODO: argument and return types
1349 ++stack->ga_len;
1350
1351 return OK;
1352}
1353
1354/*
1355 * Generate an ISN_JUMP instruction.
1356 */
1357 static int
1358generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1359{
1360 isn_T *isn;
1361 garray_T *stack = &cctx->ctx_type_stack;
1362
Bram Moolenaar080457c2020-03-03 21:53:32 +01001363 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1365 return FAIL;
1366 isn->isn_arg.jump.jump_when = when;
1367 isn->isn_arg.jump.jump_where = where;
1368
1369 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1370 --stack->ga_len;
1371
1372 return OK;
1373}
1374
1375 static int
1376generate_FOR(cctx_T *cctx, int loop_idx)
1377{
1378 isn_T *isn;
1379 garray_T *stack = &cctx->ctx_type_stack;
1380
Bram Moolenaar080457c2020-03-03 21:53:32 +01001381 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1383 return FAIL;
1384 isn->isn_arg.forloop.for_idx = loop_idx;
1385
1386 if (ga_grow(stack, 1) == FAIL)
1387 return FAIL;
1388 // type doesn't matter, will be stored next
1389 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1390 ++stack->ga_len;
1391
1392 return OK;
1393}
1394
1395/*
1396 * Generate an ISN_BCALL instruction.
1397 * Return FAIL if the number of arguments is wrong.
1398 */
1399 static int
1400generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1401{
1402 isn_T *isn;
1403 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001404 type_T *argtypes[MAX_FUNC_ARGS];
1405 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406
Bram Moolenaar080457c2020-03-03 21:53:32 +01001407 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 if (check_internal_func(func_idx, argcount) == FAIL)
1409 return FAIL;
1410
1411 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1412 return FAIL;
1413 isn->isn_arg.bfunc.cbf_idx = func_idx;
1414 isn->isn_arg.bfunc.cbf_argcount = argcount;
1415
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001416 for (i = 0; i < argcount; ++i)
1417 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 stack->ga_len -= argcount; // drop the arguments
1420 if (ga_grow(stack, 1) == FAIL)
1421 return FAIL;
1422 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001423 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 ++stack->ga_len; // add return value
1425
1426 return OK;
1427}
1428
1429/*
1430 * Generate an ISN_DCALL or ISN_UCALL instruction.
1431 * Return FAIL if the number of arguments is wrong.
1432 */
1433 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001434generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435{
1436 isn_T *isn;
1437 garray_T *stack = &cctx->ctx_type_stack;
1438 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001439 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440
Bram Moolenaar080457c2020-03-03 21:53:32 +01001441 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 if (argcount > regular_args && !has_varargs(ufunc))
1443 {
1444 semsg(_(e_toomanyarg), ufunc->uf_name);
1445 return FAIL;
1446 }
1447 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1448 {
1449 semsg(_(e_toofewarg), ufunc->uf_name);
1450 return FAIL;
1451 }
1452
Bram Moolenaar822ba242020-05-24 23:00:18 +02001453 if (ufunc->uf_dfunc_idx != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001454 {
1455 int i;
1456
1457 for (i = 0; i < argcount; ++i)
1458 {
1459 type_T *expected;
1460 type_T *actual;
1461
1462 if (i < regular_args)
1463 {
1464 if (ufunc->uf_arg_types == NULL)
1465 continue;
1466 expected = ufunc->uf_arg_types[i];
1467 }
1468 else
1469 expected = ufunc->uf_va_type->tt_member;
1470 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001471 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001472 {
1473 arg_type_mismatch(expected, actual, i + 1);
1474 return FAIL;
1475 }
1476 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001477 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001478 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001479 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001480 }
1481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 if ((isn = generate_instr(cctx,
Bram Moolenaar822ba242020-05-24 23:00:18 +02001483 ufunc->uf_dfunc_idx != UF_NOT_COMPILED ? ISN_DCALL
1484 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 return FAIL;
Bram Moolenaar822ba242020-05-24 23:00:18 +02001486 if (ufunc->uf_dfunc_idx != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 {
1488 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1489 isn->isn_arg.dfunc.cdf_argcount = argcount;
1490 }
1491 else
1492 {
1493 // A user function may be deleted and redefined later, can't use the
1494 // ufunc pointer, need to look it up again at runtime.
1495 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1496 isn->isn_arg.ufunc.cuf_argcount = argcount;
1497 }
1498
1499 stack->ga_len -= argcount; // drop the arguments
1500 if (ga_grow(stack, 1) == FAIL)
1501 return FAIL;
1502 // add return value
1503 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1504 ++stack->ga_len;
1505
1506 return OK;
1507}
1508
1509/*
1510 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1511 */
1512 static int
1513generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1514{
1515 isn_T *isn;
1516 garray_T *stack = &cctx->ctx_type_stack;
1517
Bram Moolenaar080457c2020-03-03 21:53:32 +01001518 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1520 return FAIL;
1521 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1522 isn->isn_arg.ufunc.cuf_argcount = argcount;
1523
1524 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001525 if (ga_grow(stack, 1) == FAIL)
1526 return FAIL;
1527 // add return value
1528 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1529 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530
1531 return OK;
1532}
1533
1534/*
1535 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001536 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 */
1538 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001539generate_PCALL(
1540 cctx_T *cctx,
1541 int argcount,
1542 char_u *name,
1543 type_T *type,
1544 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545{
1546 isn_T *isn;
1547 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001548 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549
Bram Moolenaar080457c2020-03-03 21:53:32 +01001550 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001551
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001552 if (type->tt_type == VAR_ANY)
1553 ret_type = &t_any;
1554 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001555 {
1556 if (type->tt_argcount != -1)
1557 {
1558 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1559
1560 if (argcount < type->tt_min_argcount - varargs)
1561 {
1562 semsg(_(e_toofewarg), "[reference]");
1563 return FAIL;
1564 }
1565 if (!varargs && argcount > type->tt_argcount)
1566 {
1567 semsg(_(e_toomanyarg), "[reference]");
1568 return FAIL;
1569 }
1570 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001571 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001572 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001573 else
1574 {
1575 semsg(_("E1085: Not a callable type: %s"), name);
1576 return FAIL;
1577 }
1578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1580 return FAIL;
1581 isn->isn_arg.pfunc.cpf_top = at_top;
1582 isn->isn_arg.pfunc.cpf_argcount = argcount;
1583
1584 stack->ga_len -= argcount; // drop the arguments
1585
1586 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001587 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001589 // If partial is above the arguments it must be cleared and replaced with
1590 // the return value.
1591 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1592 return FAIL;
1593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 return OK;
1595}
1596
1597/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001598 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 */
1600 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001601generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602{
1603 isn_T *isn;
1604 garray_T *stack = &cctx->ctx_type_stack;
1605 type_T *type;
1606
Bram Moolenaar080457c2020-03-03 21:53:32 +01001607 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001608 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001610 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001612 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001614 if (type->tt_type != VAR_DICT && type != &t_any)
1615 {
1616 emsg(_(e_dictreq));
1617 return FAIL;
1618 }
1619 // change dict type to dict member type
1620 if (type->tt_type == VAR_DICT)
1621 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622
1623 return OK;
1624}
1625
1626/*
1627 * Generate an ISN_ECHO instruction.
1628 */
1629 static int
1630generate_ECHO(cctx_T *cctx, int with_white, int count)
1631{
1632 isn_T *isn;
1633
Bram Moolenaar080457c2020-03-03 21:53:32 +01001634 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1636 return FAIL;
1637 isn->isn_arg.echo.echo_with_white = with_white;
1638 isn->isn_arg.echo.echo_count = count;
1639
1640 return OK;
1641}
1642
Bram Moolenaarad39c092020-02-26 18:23:43 +01001643/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001644 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001645 */
1646 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001647generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001648{
1649 isn_T *isn;
1650
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001651 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001652 return FAIL;
1653 isn->isn_arg.number = count;
1654
1655 return OK;
1656}
1657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 static int
1659generate_EXEC(cctx_T *cctx, char_u *line)
1660{
1661 isn_T *isn;
1662
Bram Moolenaar080457c2020-03-03 21:53:32 +01001663 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1665 return FAIL;
1666 isn->isn_arg.string = vim_strsave(line);
1667 return OK;
1668}
1669
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001670 static int
1671generate_EXECCONCAT(cctx_T *cctx, int count)
1672{
1673 isn_T *isn;
1674
1675 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1676 return FAIL;
1677 isn->isn_arg.number = count;
1678 return OK;
1679}
1680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681/*
1682 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001683 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001685 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1687{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 lvar_T *lvar;
1689
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001690 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001692 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001693 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 }
1695
1696 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001697 return NULL;
1698 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001700 // Every local variable uses the next entry on the stack. We could re-use
1701 // the last ones when leaving a scope, but then variables used in a closure
1702 // might get overwritten. To keep things simple do not re-use stack
1703 // entries. This is less efficient, but memory is cheap these days.
1704 lvar->lv_idx = cctx->ctx_locals_count++;
1705
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001706 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 lvar->lv_const = isConst;
1708 lvar->lv_type = type;
1709
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001710 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711}
1712
1713/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001714 * Remove local variables above "new_top".
1715 */
1716 static void
1717unwind_locals(cctx_T *cctx, int new_top)
1718{
1719 if (cctx->ctx_locals.ga_len > new_top)
1720 {
1721 int idx;
1722 lvar_T *lvar;
1723
1724 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1725 {
1726 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1727 vim_free(lvar->lv_name);
1728 }
1729 }
1730 cctx->ctx_locals.ga_len = new_top;
1731}
1732
1733/*
1734 * Free all local variables.
1735 */
1736 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001737free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001738{
1739 unwind_locals(cctx, 0);
1740 ga_clear(&cctx->ctx_locals);
1741}
1742
1743/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 * Skip over a type definition and return a pointer to just after it.
1745 */
1746 char_u *
1747skip_type(char_u *start)
1748{
1749 char_u *p = start;
1750
1751 while (ASCII_ISALNUM(*p) || *p == '_')
1752 ++p;
1753
1754 // Skip over "<type>"; this is permissive about white space.
1755 if (*skipwhite(p) == '<')
1756 {
1757 p = skipwhite(p);
1758 p = skip_type(skipwhite(p + 1));
1759 p = skipwhite(p);
1760 if (*p == '>')
1761 ++p;
1762 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001763 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1764 {
1765 // handle func(args): type
1766 ++p;
1767 while (*p != ')' && *p != NUL)
1768 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001769 char_u *sp = p;
1770
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001771 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001772 if (p == sp)
1773 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001774 if (*p == ',')
1775 p = skipwhite(p + 1);
1776 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001777 if (*p == ')')
1778 {
1779 if (p[1] == ':')
1780 p = skip_type(skipwhite(p + 2));
1781 else
1782 p = skipwhite(p + 1);
1783 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001784 }
1785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 return p;
1787}
1788
1789/*
1790 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001791 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 * Returns NULL in case of failure.
1793 */
1794 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001795parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796{
1797 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001798 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799
1800 if (**arg != '<')
1801 {
1802 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001803 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 else
1805 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001806 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 }
1808 *arg = skipwhite(*arg + 1);
1809
Bram Moolenaard77a8522020-04-03 21:59:57 +02001810 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811
1812 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001813 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814 {
1815 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001816 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 }
1818 ++*arg;
1819
1820 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001821 return get_list_type(member_type, type_gap);
1822 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823}
1824
1825/*
1826 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001827 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 */
1829 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001830parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831{
1832 char_u *p = *arg;
1833 size_t len;
1834
1835 // skip over the first word
1836 while (ASCII_ISALNUM(*p) || *p == '_')
1837 ++p;
1838 len = p - *arg;
1839
1840 switch (**arg)
1841 {
1842 case 'a':
1843 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1844 {
1845 *arg += len;
1846 return &t_any;
1847 }
1848 break;
1849 case 'b':
1850 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1851 {
1852 *arg += len;
1853 return &t_bool;
1854 }
1855 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1856 {
1857 *arg += len;
1858 return &t_blob;
1859 }
1860 break;
1861 case 'c':
1862 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1863 {
1864 *arg += len;
1865 return &t_channel;
1866 }
1867 break;
1868 case 'd':
1869 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1870 {
1871 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001872 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 }
1874 break;
1875 case 'f':
1876 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1877 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001878#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879 *arg += len;
1880 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001881#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001882 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001883 return &t_any;
1884#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 }
1886 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1887 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001888 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001889 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001890 int argcount = -1;
1891 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001892 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001893 type_T *arg_type[MAX_FUNC_ARGS + 1];
1894
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001895 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001897 if (**arg == '(')
1898 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001899 // "func" may or may not return a value, "func()" does
1900 // not return a value.
1901 ret_type = &t_void;
1902
Bram Moolenaard77a8522020-04-03 21:59:57 +02001903 p = ++*arg;
1904 argcount = 0;
1905 while (*p != NUL && *p != ')')
1906 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001907 if (*p == '?')
1908 {
1909 if (first_optional == -1)
1910 first_optional = argcount;
1911 ++p;
1912 }
1913 else if (first_optional != -1)
1914 {
1915 emsg(_("E1007: mandatory argument after optional argument"));
1916 return &t_any;
1917 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001918 else if (STRNCMP(p, "...", 3) == 0)
1919 {
1920 flags |= TTFLAG_VARARGS;
1921 p += 3;
1922 }
1923
1924 arg_type[argcount++] = parse_type(&p, type_gap);
1925
1926 // Nothing comes after "...{type}".
1927 if (flags & TTFLAG_VARARGS)
1928 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001929
Bram Moolenaard77a8522020-04-03 21:59:57 +02001930 if (*p != ',' && *skipwhite(p) == ',')
1931 {
1932 semsg(_(e_no_white_before), ",");
1933 return &t_any;
1934 }
1935 if (*p == ',')
1936 {
1937 ++p;
1938 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001939 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001940 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001941 return &t_any;
1942 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001943 }
1944 p = skipwhite(p);
1945 if (argcount == MAX_FUNC_ARGS)
1946 {
1947 emsg(_("E740: Too many argument types"));
1948 return &t_any;
1949 }
1950 }
1951
1952 p = skipwhite(p);
1953 if (*p != ')')
1954 {
1955 emsg(_(e_missing_close));
1956 return &t_any;
1957 }
1958 *arg = p + 1;
1959 }
1960 if (**arg == ':')
1961 {
1962 // parse return type
1963 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001964 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001965 semsg(_(e_white_after), ":");
1966 *arg = skipwhite(*arg);
1967 ret_type = parse_type(arg, type_gap);
1968 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001969 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001970 type = get_func_type(ret_type, argcount, type_gap);
1971 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001972 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001973 type = alloc_func_type(ret_type, argcount, type_gap);
1974 type->tt_flags = flags;
1975 if (argcount > 0)
1976 {
1977 type->tt_argcount = argcount;
1978 type->tt_min_argcount = first_optional == -1
1979 ? argcount : first_optional;
1980 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001981 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001982 return &t_any;
1983 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001984 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001985 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001986 }
1987 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 }
1989 break;
1990 case 'j':
1991 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1992 {
1993 *arg += len;
1994 return &t_job;
1995 }
1996 break;
1997 case 'l':
1998 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1999 {
2000 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002001 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 }
2003 break;
2004 case 'n':
2005 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2006 {
2007 *arg += len;
2008 return &t_number;
2009 }
2010 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 case 's':
2012 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2013 {
2014 *arg += len;
2015 return &t_string;
2016 }
2017 break;
2018 case 'v':
2019 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2020 {
2021 *arg += len;
2022 return &t_void;
2023 }
2024 break;
2025 }
2026
2027 semsg(_("E1010: Type not recognized: %s"), *arg);
2028 return &t_any;
2029}
2030
2031/*
2032 * Check if "type1" and "type2" are exactly the same.
2033 */
2034 static int
2035equal_type(type_T *type1, type_T *type2)
2036{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002037 int i;
2038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 if (type1->tt_type != type2->tt_type)
2040 return FALSE;
2041 switch (type1->tt_type)
2042 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002044 case VAR_ANY:
2045 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 case VAR_SPECIAL:
2047 case VAR_BOOL:
2048 case VAR_NUMBER:
2049 case VAR_FLOAT:
2050 case VAR_STRING:
2051 case VAR_BLOB:
2052 case VAR_JOB:
2053 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002054 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055 case VAR_LIST:
2056 case VAR_DICT:
2057 return equal_type(type1->tt_member, type2->tt_member);
2058 case VAR_FUNC:
2059 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002060 if (!equal_type(type1->tt_member, type2->tt_member)
2061 || type1->tt_argcount != type2->tt_argcount)
2062 return FALSE;
2063 if (type1->tt_argcount < 0
2064 || type1->tt_args == NULL || type2->tt_args == NULL)
2065 return TRUE;
2066 for (i = 0; i < type1->tt_argcount; ++i)
2067 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2068 return FALSE;
2069 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 }
2071 return TRUE;
2072}
2073
2074/*
2075 * Find the common type of "type1" and "type2" and put it in "dest".
2076 * "type2" and "dest" may be the same.
2077 */
2078 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002079common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080{
2081 if (equal_type(type1, type2))
2082 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002083 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084 return;
2085 }
2086
2087 if (type1->tt_type == type2->tt_type)
2088 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2090 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002091 type_T *common;
2092
Bram Moolenaard77a8522020-04-03 21:59:57 +02002093 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002094 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002095 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002096 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002097 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 return;
2099 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002100 if (type1->tt_type == VAR_FUNC)
2101 {
2102 type_T *common;
2103
2104 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2105 if (type1->tt_argcount == type2->tt_argcount
2106 && type1->tt_argcount >= 0)
2107 {
2108 int argcount = type1->tt_argcount;
2109 int i;
2110
2111 *dest = alloc_func_type(common, argcount, type_gap);
2112 if (type1->tt_args != NULL && type2->tt_args != NULL)
2113 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002114 if (func_type_add_arg_types(*dest, argcount,
2115 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002116 for (i = 0; i < argcount; ++i)
2117 common_type(type1->tt_args[i], type2->tt_args[i],
2118 &(*dest)->tt_args[i], type_gap);
2119 }
2120 }
2121 else
2122 *dest = alloc_func_type(common, -1, type_gap);
2123 return;
2124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 }
2126
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002127 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128}
2129
2130 char *
2131vartype_name(vartype_T type)
2132{
2133 switch (type)
2134 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002135 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002136 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 case VAR_SPECIAL: return "special";
2139 case VAR_BOOL: return "bool";
2140 case VAR_NUMBER: return "number";
2141 case VAR_FLOAT: return "float";
2142 case VAR_STRING: return "string";
2143 case VAR_BLOB: return "blob";
2144 case VAR_JOB: return "job";
2145 case VAR_CHANNEL: return "channel";
2146 case VAR_LIST: return "list";
2147 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002148
2149 case VAR_FUNC:
2150 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002152 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153}
2154
2155/*
2156 * Return the name of a type.
2157 * The result may be in allocated memory, in which case "tofree" is set.
2158 */
2159 char *
2160type_name(type_T *type, char **tofree)
2161{
2162 char *name = vartype_name(type->tt_type);
2163
2164 *tofree = NULL;
2165 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2166 {
2167 char *member_free;
2168 char *member_name = type_name(type->tt_member, &member_free);
2169 size_t len;
2170
2171 len = STRLEN(name) + STRLEN(member_name) + 3;
2172 *tofree = alloc(len);
2173 if (*tofree != NULL)
2174 {
2175 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2176 vim_free(member_free);
2177 return *tofree;
2178 }
2179 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002180 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002181 {
2182 garray_T ga;
2183 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002184 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002185
2186 ga_init2(&ga, 1, 100);
2187 if (ga_grow(&ga, 20) == FAIL)
2188 return "[unknown]";
2189 *tofree = ga.ga_data;
2190 STRCPY(ga.ga_data, "func(");
2191 ga.ga_len += 5;
2192
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002193 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002194 {
2195 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002196 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002197 int len;
2198
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002199 if (type->tt_args == NULL)
2200 arg_type = "[unknown]";
2201 else
2202 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002203 if (i > 0)
2204 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002205 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002206 ga.ga_len += 2;
2207 }
2208 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002209 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002210 {
2211 vim_free(arg_free);
2212 return "[unknown]";
2213 }
2214 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002215 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002216 {
2217 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2218 ga.ga_len += 3;
2219 }
2220 else if (i >= type->tt_min_argcount)
2221 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002222 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002223 ga.ga_len += len;
2224 vim_free(arg_free);
2225 }
2226
2227 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002228 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002229 else
2230 {
2231 char *ret_free;
2232 char *ret_name = type_name(type->tt_member, &ret_free);
2233 int len;
2234
2235 len = (int)STRLEN(ret_name) + 4;
2236 if (ga_grow(&ga, len) == FAIL)
2237 {
2238 vim_free(ret_free);
2239 return "[unknown]";
2240 }
2241 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002242 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2243 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002244 vim_free(ret_free);
2245 }
2246 return ga.ga_data;
2247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248
2249 return name;
2250}
2251
2252/*
2253 * Find "name" in script-local items of script "sid".
2254 * Returns the index in "sn_var_vals" if found.
2255 * If found but not in "sn_var_vals" returns -1.
2256 * If not found returns -2.
2257 */
2258 int
2259get_script_item_idx(int sid, char_u *name, int check_writable)
2260{
2261 hashtab_T *ht;
2262 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002263 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 int idx;
2265
2266 // First look the name up in the hashtable.
2267 if (sid <= 0 || sid > script_items.ga_len)
2268 return -1;
2269 ht = &SCRIPT_VARS(sid);
2270 di = find_var_in_ht(ht, 0, name, TRUE);
2271 if (di == NULL)
2272 return -2;
2273
2274 // Now find the svar_T index in sn_var_vals.
2275 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2276 {
2277 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2278
2279 if (sv->sv_tv == &di->di_tv)
2280 {
2281 if (check_writable && sv->sv_const)
2282 semsg(_(e_readonlyvar), name);
2283 return idx;
2284 }
2285 }
2286 return -1;
2287}
2288
2289/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002290 * Find "name" in imported items of the current script or in "cctx" if not
2291 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 */
2293 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002294find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002296 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 int idx;
2298
2299 if (cctx != NULL)
2300 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2301 {
2302 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2303 + idx;
2304
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002305 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2306 : STRLEN(import->imp_name) == len
2307 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 return import;
2309 }
2310
2311 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2312 {
2313 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2314
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002315 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2316 : STRLEN(import->imp_name) == len
2317 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 return import;
2319 }
2320 return NULL;
2321}
2322
2323/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002324 * Free all imported variables.
2325 */
2326 static void
2327free_imported(cctx_T *cctx)
2328{
2329 int idx;
2330
2331 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2332 {
2333 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2334
2335 vim_free(import->imp_name);
2336 }
2337 ga_clear(&cctx->ctx_imports);
2338}
2339
2340/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002341 * Get the next line of the function from "cctx".
2342 * Returns NULL when at the end.
2343 */
2344 static char_u *
2345next_line_from_context(cctx_T *cctx)
2346{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002347 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002348
2349 do
2350 {
2351 ++cctx->ctx_lnum;
2352 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002353 {
2354 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002355 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002356 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002357 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002358 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002359 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002360 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002361 return line;
2362}
2363
2364/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002365 * Return TRUE if "p" points at a "#" but not at "#{".
2366 */
2367 static int
2368comment_start(char_u *p)
2369{
2370 return p[0] == '#' && p[1] != '{';
2371}
2372
2373/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002374 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002375 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002376 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2377 */
2378 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002379may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002380{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002381 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002382 {
2383 char_u *next = next_line_from_context(cctx);
2384
2385 if (next == NULL)
2386 return FAIL;
2387 *arg = skipwhite(next);
2388 }
2389 return OK;
2390}
2391
Bram Moolenaara5565e42020-05-09 15:44:01 +02002392// Structure passed between the compile_expr* functions to keep track of
2393// constants that have been parsed but for which no code was produced yet. If
2394// possible expressions on these constants are applied at compile time. If
2395// that is not possible, the code to push the constants needs to be generated
2396// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002397// Using 50 should be more than enough of 5 levels of ().
2398#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002399typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002400 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002401 int pp_used; // active entries in pp_tv[]
2402} ppconst_T;
2403
Bram Moolenaar1c747212020-05-09 18:28:34 +02002404static int compile_expr0(char_u **arg, cctx_T *cctx);
2405static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2406
Bram Moolenaara5565e42020-05-09 15:44:01 +02002407/*
2408 * Generate a PUSH instruction for "tv".
2409 * "tv" will be consumed or cleared.
2410 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2411 */
2412 static int
2413generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2414{
2415 if (tv != NULL)
2416 {
2417 switch (tv->v_type)
2418 {
2419 case VAR_UNKNOWN:
2420 break;
2421 case VAR_BOOL:
2422 generate_PUSHBOOL(cctx, tv->vval.v_number);
2423 break;
2424 case VAR_SPECIAL:
2425 generate_PUSHSPEC(cctx, tv->vval.v_number);
2426 break;
2427 case VAR_NUMBER:
2428 generate_PUSHNR(cctx, tv->vval.v_number);
2429 break;
2430#ifdef FEAT_FLOAT
2431 case VAR_FLOAT:
2432 generate_PUSHF(cctx, tv->vval.v_float);
2433 break;
2434#endif
2435 case VAR_BLOB:
2436 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2437 tv->vval.v_blob = NULL;
2438 break;
2439 case VAR_STRING:
2440 generate_PUSHS(cctx, tv->vval.v_string);
2441 tv->vval.v_string = NULL;
2442 break;
2443 default:
2444 iemsg("constant type not supported");
2445 clear_tv(tv);
2446 return FAIL;
2447 }
2448 tv->v_type = VAR_UNKNOWN;
2449 }
2450 return OK;
2451}
2452
2453/*
2454 * Generate code for any ppconst entries.
2455 */
2456 static int
2457generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2458{
2459 int i;
2460 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002461 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002462
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002463 cctx->ctx_skip = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002464 for (i = 0; i < ppconst->pp_used; ++i)
2465 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2466 ret = FAIL;
2467 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002468 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002469 return ret;
2470}
2471
2472/*
2473 * Clear ppconst constants. Used when failing.
2474 */
2475 static void
2476clear_ppconst(ppconst_T *ppconst)
2477{
2478 int i;
2479
2480 for (i = 0; i < ppconst->pp_used; ++i)
2481 clear_tv(&ppconst->pp_tv[i]);
2482 ppconst->pp_used = 0;
2483}
2484
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002485/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002486 * Generate an instruction to load script-local variable "name", without the
2487 * leading "s:".
2488 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 */
2490 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002491compile_load_scriptvar(
2492 cctx_T *cctx,
2493 char_u *name, // variable NUL terminated
2494 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002495 char_u **end, // end of variable
2496 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002498 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2500 imported_T *import;
2501
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002502 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002504 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002505 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2506 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 }
2508 if (idx >= 0)
2509 {
2510 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2511
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002512 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 current_sctx.sc_sid, idx, sv->sv_type);
2514 return OK;
2515 }
2516
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002517 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 if (import != NULL)
2519 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002520 if (import->imp_all)
2521 {
2522 char_u *p = skipwhite(*end);
2523 int name_len;
2524 ufunc_T *ufunc;
2525 type_T *type;
2526
2527 // Used "import * as Name", need to lookup the member.
2528 if (*p != '.')
2529 {
2530 semsg(_("E1060: expected dot after name: %s"), start);
2531 return FAIL;
2532 }
2533 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002534 if (VIM_ISWHITE(*p))
2535 {
2536 emsg(_("E1074: no white space allowed after dot"));
2537 return FAIL;
2538 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002539
2540 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2541 // TODO: what if it is a function?
2542 if (idx < 0)
2543 return FAIL;
2544 *end = p;
2545
2546 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2547 import->imp_sid,
2548 idx,
2549 type);
2550 }
2551 else
2552 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002553 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002554 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2555 import->imp_sid,
2556 import->imp_var_vals_idx,
2557 import->imp_type);
2558 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 return OK;
2560 }
2561
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002562 if (error)
2563 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 return FAIL;
2565}
2566
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002567 static int
2568generate_funcref(cctx_T *cctx, char_u *name)
2569{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002570 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002571
2572 if (ufunc == NULL)
2573 return FAIL;
2574
2575 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2576}
2577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578/*
2579 * Compile a variable name into a load instruction.
2580 * "end" points to just after the name.
2581 * When "error" is FALSE do not give an error when not found.
2582 */
2583 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002584compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585{
2586 type_T *type;
2587 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002588 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002590 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591
2592 if (*(*arg + 1) == ':')
2593 {
2594 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002595 if (end <= *arg + 2)
2596 name = vim_strsave((char_u *)"[empty]");
2597 else
2598 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 if (name == NULL)
2600 return FAIL;
2601
2602 if (**arg == 'v')
2603 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002604 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 }
2606 else if (**arg == 'g')
2607 {
2608 // Global variables can be defined later, thus we don't check if it
2609 // exists, give error at runtime.
2610 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2611 }
2612 else if (**arg == 's')
2613 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002614 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002616 else if (**arg == 'b')
2617 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002618 // Buffer-local variables can be defined later, thus we don't check
2619 // if it exists, give error at runtime.
2620 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002621 }
2622 else if (**arg == 'w')
2623 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002624 // Window-local variables can be defined later, thus we don't check
2625 // if it exists, give error at runtime.
2626 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002627 }
2628 else if (**arg == 't')
2629 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002630 // Tabpage-local variables can be defined later, thus we don't
2631 // check if it exists, give error at runtime.
2632 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 else
2635 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002636 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 goto theend;
2638 }
2639 }
2640 else
2641 {
2642 size_t len = end - *arg;
2643 int idx;
2644 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002645 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646
2647 name = vim_strnsave(*arg, end - *arg);
2648 if (name == NULL)
2649 return FAIL;
2650
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002651 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002653 if (!gen_load_outer)
2654 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 }
2656 else
2657 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002658 lvar_T *lvar = lookup_local(*arg, len, cctx);
2659
2660 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002662 type = lvar->lv_type;
2663 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002664 if (lvar->lv_from_outer)
2665 gen_load_outer = TRUE;
2666 else
2667 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 }
2669 else
2670 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002671 // "var" can be script-local even without using "s:" if it
2672 // already exists.
2673 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2674 == SCRIPT_VERSION_VIM9
2675 || lookup_script(*arg, len) == OK)
2676 res = compile_load_scriptvar(cctx, name, *arg, &end,
2677 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002678
Bram Moolenaara5565e42020-05-09 15:44:01 +02002679 // When the name starts with an uppercase letter or "x:" it
2680 // can be a user defined function.
2681 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2682 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 }
2684 }
2685 if (gen_load)
2686 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002687 if (gen_load_outer)
2688 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 }
2690
2691 *arg = end;
2692
2693theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002694 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 semsg(_(e_var_notfound), name);
2696 vim_free(name);
2697 return res;
2698}
2699
2700/*
2701 * Compile the argument expressions.
2702 * "arg" points to just after the "(" and is advanced to after the ")"
2703 */
2704 static int
2705compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2706{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002707 char_u *p = *arg;
2708 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709
Bram Moolenaare6085c52020-04-12 20:19:16 +02002710 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002712 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002713 {
2714 p = next_line_from_context(cctx);
2715 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002716 goto failret;
2717 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002718 p = skipwhite(p);
2719 }
2720 if (*p == ')')
2721 {
2722 *arg = p + 1;
2723 return OK;
2724 }
2725
Bram Moolenaara5565e42020-05-09 15:44:01 +02002726 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 return FAIL;
2728 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002729
2730 if (*p != ',' && *skipwhite(p) == ',')
2731 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002732 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002733 p = skipwhite(p);
2734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002736 {
2737 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002738 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002739 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002740 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002741 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002742 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002744failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002745 emsg(_(e_missing_close));
2746 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747}
2748
2749/*
2750 * Compile a function call: name(arg1, arg2)
2751 * "arg" points to "name", "arg + varlen" to the "(".
2752 * "argcount_init" is 1 for "value->method()"
2753 * Instructions:
2754 * EVAL arg1
2755 * EVAL arg2
2756 * BCALL / DCALL / UCALL
2757 */
2758 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002759compile_call(
2760 char_u **arg,
2761 size_t varlen,
2762 cctx_T *cctx,
2763 ppconst_T *ppconst,
2764 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765{
2766 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002767 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 int argcount = argcount_init;
2769 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002770 char_u fname_buf[FLEN_FIXED + 1];
2771 char_u *tofree = NULL;
2772 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002774 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775
Bram Moolenaara5565e42020-05-09 15:44:01 +02002776 // we can evaluate "has('name')" at compile time
2777 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2778 {
2779 char_u *s = skipwhite(*arg + varlen + 1);
2780 typval_T argvars[2];
2781
2782 argvars[0].v_type = VAR_UNKNOWN;
2783 if (*s == '"')
2784 (void)get_string_tv(&s, &argvars[0], TRUE);
2785 else if (*s == '\'')
2786 (void)get_lit_string_tv(&s, &argvars[0], TRUE);
2787 s = skipwhite(s);
2788 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2789 {
2790 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2791
2792 *arg = s + 1;
2793 argvars[1].v_type = VAR_UNKNOWN;
2794 tv->v_type = VAR_NUMBER;
2795 tv->vval.v_number = 0;
2796 f_has(argvars, tv);
2797 clear_tv(&argvars[0]);
2798 ++ppconst->pp_used;
2799 return OK;
2800 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002801 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002802 }
2803
2804 if (generate_ppconst(cctx, ppconst) == FAIL)
2805 return FAIL;
2806
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 if (varlen >= sizeof(namebuf))
2808 {
2809 semsg(_("E1011: name too long: %s"), name);
2810 return FAIL;
2811 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002812 vim_strncpy(namebuf, *arg, varlen);
2813 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814
2815 *arg = skipwhite(*arg + varlen + 1);
2816 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002817 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002819 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 {
2821 int idx;
2822
2823 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002824 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002826 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002827 else
2828 semsg(_(e_unknownfunc), namebuf);
2829 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 }
2831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002833 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002835 {
2836 res = generate_CALL(cctx, ufunc, argcount);
2837 goto theend;
2838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839
2840 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002841 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002843 if (STRNCMP(namebuf, "g:", 2) != 0
2844 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002845 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002846 garray_T *stack = &cctx->ctx_type_stack;
2847 type_T *type;
2848
2849 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2850 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002851 goto theend;
2852 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002854 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002855 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002856 if (STRNCMP(namebuf, "g:", 2) == 0)
2857 res = generate_UCALL(cctx, name, argcount);
2858 else
2859 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002860
2861theend:
2862 vim_free(tofree);
2863 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864}
2865
2866// like NAMESPACE_CHAR but with 'a' and 'l'.
2867#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2868
2869/*
2870 * Find the end of a variable or function name. Unlike find_name_end() this
2871 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002872 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 * Return a pointer to just after the name. Equal to "arg" if there is no
2874 * valid name.
2875 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002876 static char_u *
2877to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878{
2879 char_u *p;
2880
2881 // Quick check for valid starting character.
2882 if (!eval_isnamec1(*arg))
2883 return arg;
2884
2885 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2886 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2887 // and can be used in slice "[n:]".
2888 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002889 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2891 break;
2892 return p;
2893}
2894
2895/*
2896 * Like to_name_end() but also skip over a list or dict constant.
2897 */
2898 char_u *
2899to_name_const_end(char_u *arg)
2900{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002901 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 typval_T rettv;
2903
2904 if (p == arg && *arg == '[')
2905 {
2906
2907 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar32e35112020-05-14 22:41:15 +02002908 if (get_list_tv(&p, &rettv, 0, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 p = arg;
2910 }
2911 else if (p == arg && *arg == '#' && arg[1] == '{')
2912 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002913 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 ++p;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002915 if (eval_dict(&p, &rettv, 0, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 p = arg;
2917 }
2918 else if (p == arg && *arg == '{')
2919 {
2920 int ret = get_lambda_tv(&p, &rettv, FALSE);
2921
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002922 // Can be "{x -> ret}()".
2923 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 if (ret == NOTDONE)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002925 ret = eval_dict(&p, &rettv, 0, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 if (ret != OK)
2927 p = arg;
2928 }
2929
2930 return p;
2931}
2932
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933/*
2934 * parse a list: [expr, expr]
2935 * "*arg" points to the '['.
2936 */
2937 static int
2938compile_list(char_u **arg, cctx_T *cctx)
2939{
2940 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002941 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 int count = 0;
2943
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002944 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002946 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002947 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002948 p = next_line_from_context(cctx);
2949 if (p == NULL)
2950 {
2951 semsg(_(e_list_end), *arg);
2952 return FAIL;
2953 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002954 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002955 p = skipwhite(p);
2956 }
2957 if (*p == ']')
2958 {
2959 ++p;
2960 // Allow for following comment, after at least one space.
2961 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2962 p += STRLEN(p);
2963 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002964 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002965 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 break;
2967 ++count;
2968 if (*p == ',')
2969 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002970 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 p = skipwhite(p);
2972 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002973 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974
2975 generate_NEWLIST(cctx, count);
2976 return OK;
2977}
2978
2979/*
2980 * parse a lambda: {arg, arg -> expr}
2981 * "*arg" points to the '{'.
2982 */
2983 static int
2984compile_lambda(char_u **arg, cctx_T *cctx)
2985{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 typval_T rettv;
2987 ufunc_T *ufunc;
2988
2989 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002990 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002994 ++ufunc->uf_refcount;
2995 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002996 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997
2998 // The function will have one line: "return {expr}".
2999 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003000 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001
3002 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003003 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 return FAIL;
3005}
3006
3007/*
3008 * Compile a lamda call: expr->{lambda}(args)
3009 * "arg" points to the "{".
3010 */
3011 static int
3012compile_lambda_call(char_u **arg, cctx_T *cctx)
3013{
3014 ufunc_T *ufunc;
3015 typval_T rettv;
3016 int argcount = 1;
3017 int ret = FAIL;
3018
3019 // Get the funcref in "rettv".
3020 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
3021 return FAIL;
3022
3023 if (**arg != '(')
3024 {
3025 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003026 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 else
3028 semsg(_(e_missing_paren), "lambda");
3029 clear_tv(&rettv);
3030 return FAIL;
3031 }
3032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 ufunc = rettv.vval.v_partial->pt_func;
3034 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003035 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003036 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003037
3038 // The function will have one line: "return {expr}".
3039 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003040 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041
3042 // compile the arguments
3043 *arg = skipwhite(*arg + 1);
3044 if (compile_arguments(arg, cctx, &argcount) == OK)
3045 // call the compiled function
3046 ret = generate_CALL(cctx, ufunc, argcount);
3047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 return ret;
3049}
3050
3051/*
3052 * parse a dict: {'key': val} or #{key: val}
3053 * "*arg" points to the '{'.
3054 */
3055 static int
3056compile_dict(char_u **arg, cctx_T *cctx, int literal)
3057{
3058 garray_T *instr = &cctx->ctx_instr;
3059 int count = 0;
3060 dict_T *d = dict_alloc();
3061 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003062 char_u *whitep = *arg;
3063 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064
3065 if (d == NULL)
3066 return FAIL;
3067 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003068 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 {
3070 char_u *key = NULL;
3071
Bram Moolenaar2c330432020-04-13 14:41:35 +02003072 while (**arg == NUL || (literal && **arg == '"')
3073 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003074 {
3075 *arg = next_line_from_context(cctx);
3076 if (*arg == NULL)
3077 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003078 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003079 *arg = skipwhite(*arg);
3080 }
3081
3082 if (**arg == '}')
3083 break;
3084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 if (literal)
3086 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003087 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088
Bram Moolenaar2c330432020-04-13 14:41:35 +02003089 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 {
3091 semsg(_("E1014: Invalid key: %s"), *arg);
3092 return FAIL;
3093 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003094 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 if (generate_PUSHS(cctx, key) == FAIL)
3096 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003097 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 }
3099 else
3100 {
3101 isn_T *isn;
3102
Bram Moolenaara5565e42020-05-09 15:44:01 +02003103 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 return FAIL;
3105 // TODO: check type is string
3106 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3107 if (isn->isn_type == ISN_PUSHS)
3108 key = isn->isn_arg.string;
3109 }
3110
3111 // Check for duplicate keys, if using string keys.
3112 if (key != NULL)
3113 {
3114 item = dict_find(d, key, -1);
3115 if (item != NULL)
3116 {
3117 semsg(_(e_duplicate_key), key);
3118 goto failret;
3119 }
3120 item = dictitem_alloc(key);
3121 if (item != NULL)
3122 {
3123 item->di_tv.v_type = VAR_UNKNOWN;
3124 item->di_tv.v_lock = 0;
3125 if (dict_add(d, item) == FAIL)
3126 dictitem_free(item);
3127 }
3128 }
3129
3130 *arg = skipwhite(*arg);
3131 if (**arg != ':')
3132 {
3133 semsg(_(e_missing_dict_colon), *arg);
3134 return FAIL;
3135 }
3136
Bram Moolenaar2c330432020-04-13 14:41:35 +02003137 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003139 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003140 {
3141 *arg = next_line_from_context(cctx);
3142 if (*arg == NULL)
3143 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003144 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003145 *arg = skipwhite(*arg);
3146 }
3147
Bram Moolenaara5565e42020-05-09 15:44:01 +02003148 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 return FAIL;
3150 ++count;
3151
Bram Moolenaar2c330432020-04-13 14:41:35 +02003152 whitep = *arg;
3153 p = skipwhite(*arg);
3154 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003155 {
3156 *arg = next_line_from_context(cctx);
3157 if (*arg == NULL)
3158 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003159 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003160 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003161 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003162 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 if (**arg == '}')
3164 break;
3165 if (**arg != ',')
3166 {
3167 semsg(_(e_missing_dict_comma), *arg);
3168 goto failret;
3169 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003170 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 *arg = skipwhite(*arg + 1);
3172 }
3173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 *arg = *arg + 1;
3175
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003176 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003177 p = skipwhite(*arg);
3178 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003179 *arg += STRLEN(*arg);
3180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 dict_unref(d);
3182 return generate_NEWDICT(cctx, count);
3183
3184failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003185 if (*arg == NULL)
3186 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 dict_unref(d);
3188 return FAIL;
3189}
3190
3191/*
3192 * Compile "&option".
3193 */
3194 static int
3195compile_get_option(char_u **arg, cctx_T *cctx)
3196{
3197 typval_T rettv;
3198 char_u *start = *arg;
3199 int ret;
3200
3201 // parse the option and get the current value to get the type.
3202 rettv.v_type = VAR_UNKNOWN;
3203 ret = get_option_tv(arg, &rettv, TRUE);
3204 if (ret == OK)
3205 {
3206 // include the '&' in the name, get_option_tv() expects it.
3207 char_u *name = vim_strnsave(start, *arg - start);
3208 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3209
3210 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3211 vim_free(name);
3212 }
3213 clear_tv(&rettv);
3214
3215 return ret;
3216}
3217
3218/*
3219 * Compile "$VAR".
3220 */
3221 static int
3222compile_get_env(char_u **arg, cctx_T *cctx)
3223{
3224 char_u *start = *arg;
3225 int len;
3226 int ret;
3227 char_u *name;
3228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 ++*arg;
3230 len = get_env_len(arg);
3231 if (len == 0)
3232 {
3233 semsg(_(e_syntax_at), start - 1);
3234 return FAIL;
3235 }
3236
3237 // include the '$' in the name, get_env_tv() expects it.
3238 name = vim_strnsave(start, len + 1);
3239 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3240 vim_free(name);
3241 return ret;
3242}
3243
3244/*
3245 * Compile "@r".
3246 */
3247 static int
3248compile_get_register(char_u **arg, cctx_T *cctx)
3249{
3250 int ret;
3251
3252 ++*arg;
3253 if (**arg == NUL)
3254 {
3255 semsg(_(e_syntax_at), *arg - 1);
3256 return FAIL;
3257 }
3258 if (!valid_yank_reg(**arg, TRUE))
3259 {
3260 emsg_invreg(**arg);
3261 return FAIL;
3262 }
3263 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3264 ++*arg;
3265 return ret;
3266}
3267
3268/*
3269 * Apply leading '!', '-' and '+' to constant "rettv".
3270 */
3271 static int
3272apply_leader(typval_T *rettv, char_u *start, char_u *end)
3273{
3274 char_u *p = end;
3275
3276 // this works from end to start
3277 while (p > start)
3278 {
3279 --p;
3280 if (*p == '-' || *p == '+')
3281 {
3282 // only '-' has an effect, for '+' we only check the type
3283#ifdef FEAT_FLOAT
3284 if (rettv->v_type == VAR_FLOAT)
3285 {
3286 if (*p == '-')
3287 rettv->vval.v_float = -rettv->vval.v_float;
3288 }
3289 else
3290#endif
3291 {
3292 varnumber_T val;
3293 int error = FALSE;
3294
3295 // tv_get_number_chk() accepts a string, but we don't want that
3296 // here
3297 if (check_not_string(rettv) == FAIL)
3298 return FAIL;
3299 val = tv_get_number_chk(rettv, &error);
3300 clear_tv(rettv);
3301 if (error)
3302 return FAIL;
3303 if (*p == '-')
3304 val = -val;
3305 rettv->v_type = VAR_NUMBER;
3306 rettv->vval.v_number = val;
3307 }
3308 }
3309 else
3310 {
3311 int v = tv2bool(rettv);
3312
3313 // '!' is permissive in the type.
3314 clear_tv(rettv);
3315 rettv->v_type = VAR_BOOL;
3316 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3317 }
3318 }
3319 return OK;
3320}
3321
3322/*
3323 * Recognize v: variables that are constants and set "rettv".
3324 */
3325 static void
3326get_vim_constant(char_u **arg, typval_T *rettv)
3327{
3328 if (STRNCMP(*arg, "v:true", 6) == 0)
3329 {
3330 rettv->v_type = VAR_BOOL;
3331 rettv->vval.v_number = VVAL_TRUE;
3332 *arg += 6;
3333 }
3334 else if (STRNCMP(*arg, "v:false", 7) == 0)
3335 {
3336 rettv->v_type = VAR_BOOL;
3337 rettv->vval.v_number = VVAL_FALSE;
3338 *arg += 7;
3339 }
3340 else if (STRNCMP(*arg, "v:null", 6) == 0)
3341 {
3342 rettv->v_type = VAR_SPECIAL;
3343 rettv->vval.v_number = VVAL_NULL;
3344 *arg += 6;
3345 }
3346 else if (STRNCMP(*arg, "v:none", 6) == 0)
3347 {
3348 rettv->v_type = VAR_SPECIAL;
3349 rettv->vval.v_number = VVAL_NONE;
3350 *arg += 6;
3351 }
3352}
3353
Bram Moolenaar61a89812020-05-07 16:58:17 +02003354 static exptype_T
3355get_compare_type(char_u *p, int *len, int *type_is)
3356{
3357 exptype_T type = EXPR_UNKNOWN;
3358 int i;
3359
3360 switch (p[0])
3361 {
3362 case '=': if (p[1] == '=')
3363 type = EXPR_EQUAL;
3364 else if (p[1] == '~')
3365 type = EXPR_MATCH;
3366 break;
3367 case '!': if (p[1] == '=')
3368 type = EXPR_NEQUAL;
3369 else if (p[1] == '~')
3370 type = EXPR_NOMATCH;
3371 break;
3372 case '>': if (p[1] != '=')
3373 {
3374 type = EXPR_GREATER;
3375 *len = 1;
3376 }
3377 else
3378 type = EXPR_GEQUAL;
3379 break;
3380 case '<': if (p[1] != '=')
3381 {
3382 type = EXPR_SMALLER;
3383 *len = 1;
3384 }
3385 else
3386 type = EXPR_SEQUAL;
3387 break;
3388 case 'i': if (p[1] == 's')
3389 {
3390 // "is" and "isnot"; but not a prefix of a name
3391 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3392 *len = 5;
3393 i = p[*len];
3394 if (!isalnum(i) && i != '_')
3395 {
3396 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3397 *type_is = TRUE;
3398 }
3399 }
3400 break;
3401 }
3402 return type;
3403}
3404
3405/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 * Compile code to apply '-', '+' and '!'.
3407 */
3408 static int
3409compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3410{
3411 char_u *p = end;
3412
3413 // this works from end to start
3414 while (p > start)
3415 {
3416 --p;
3417 if (*p == '-' || *p == '+')
3418 {
3419 int negate = *p == '-';
3420 isn_T *isn;
3421
3422 // TODO: check type
3423 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3424 {
3425 --p;
3426 if (*p == '-')
3427 negate = !negate;
3428 }
3429 // only '-' has an effect, for '+' we only check the type
3430 if (negate)
3431 isn = generate_instr(cctx, ISN_NEGATENR);
3432 else
3433 isn = generate_instr(cctx, ISN_CHECKNR);
3434 if (isn == NULL)
3435 return FAIL;
3436 }
3437 else
3438 {
3439 int invert = TRUE;
3440
3441 while (p > start && p[-1] == '!')
3442 {
3443 --p;
3444 invert = !invert;
3445 }
3446 if (generate_2BOOL(cctx, invert) == FAIL)
3447 return FAIL;
3448 }
3449 }
3450 return OK;
3451}
3452
3453/*
3454 * Compile whatever comes after "name" or "name()".
3455 */
3456 static int
3457compile_subscript(
3458 char_u **arg,
3459 cctx_T *cctx,
3460 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003461 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003462 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463{
3464 for (;;)
3465 {
3466 if (**arg == '(')
3467 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003468 garray_T *stack = &cctx->ctx_type_stack;
3469 type_T *type;
3470 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003472 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003473 return FAIL;
3474
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003476 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 *arg = skipwhite(*arg + 1);
3479 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3480 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003481 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 return FAIL;
3483 }
3484 else if (**arg == '-' && (*arg)[1] == '>')
3485 {
3486 char_u *p;
3487
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003488 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003489 return FAIL;
3490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 // something->method()
3492 // Apply the '!', '-' and '+' first:
3493 // -1.0->func() works like (-1.0)->func()
3494 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3495 return FAIL;
3496 *start_leader = end_leader; // don't apply again later
3497
3498 *arg = skipwhite(*arg + 2);
3499 if (**arg == '{')
3500 {
3501 // lambda call: list->{lambda}
3502 if (compile_lambda_call(arg, cctx) == FAIL)
3503 return FAIL;
3504 }
3505 else
3506 {
3507 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003508 p = *arg;
3509 if (ASCII_ISALPHA(*p) && p[1] == ':')
3510 p += 2;
3511 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 ;
3513 if (*p != '(')
3514 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003515 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 return FAIL;
3517 }
3518 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003519 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 return FAIL;
3521 }
3522 }
3523 else if (**arg == '[')
3524 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003525 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003526 type_T **typep;
3527
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003528 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003529 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003530 // TODO: blob index
3531 // TODO: more arguments
3532 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003533 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003534 return FAIL;
3535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003537 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 return FAIL;
3539
3540 if (**arg != ']')
3541 {
3542 emsg(_(e_missbrac));
3543 return FAIL;
3544 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003545 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003547 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3548 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003549 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003550 if ((*typep)->tt_type == VAR_LIST)
3551 *typep = (*typep)->tt_member;
3552 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3553 return FAIL;
3554 }
3555 else if ((*typep)->tt_type == VAR_DICT)
3556 {
3557 *typep = (*typep)->tt_member;
3558 if (may_generate_2STRING(-1, cctx) == FAIL)
3559 return FAIL;
3560 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3561 return FAIL;
3562 }
3563 else
3564 {
3565 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003566 return FAIL;
3567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 }
3569 else if (**arg == '.' && (*arg)[1] != '.')
3570 {
3571 char_u *p;
3572
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003573 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003574 return FAIL;
3575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 ++*arg;
3577 p = *arg;
3578 // dictionary member: dict.name
3579 if (eval_isnamec1(*p))
3580 while (eval_isnamec(*p))
3581 MB_PTR_ADV(p);
3582 if (p == *arg)
3583 {
3584 semsg(_(e_syntax_at), *arg);
3585 return FAIL;
3586 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003587 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 return FAIL;
3589 *arg = p;
3590 }
3591 else
3592 break;
3593 }
3594
3595 // TODO - see handle_subscript():
3596 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3597 // Don't do this when "Func" is already a partial that was bound
3598 // explicitly (pt_auto is FALSE).
3599
3600 return OK;
3601}
3602
3603/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003604 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3605 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003607 * If the value is a constant "ppconst->pp_ret" will be set.
3608 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003609 *
3610 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 */
3612
3613/*
3614 * number number constant
3615 * 0zFFFFFFFF Blob constant
3616 * "string" string constant
3617 * 'string' literal string constant
3618 * &option-name option value
3619 * @r register contents
3620 * identifier variable value
3621 * function() function call
3622 * $VAR environment variable
3623 * (expression) nested expression
3624 * [expr, expr] List
3625 * {key: val, key: val} Dictionary
3626 * #{key: val, key: val} Dictionary with literal keys
3627 *
3628 * Also handle:
3629 * ! in front logical NOT
3630 * - in front unary minus
3631 * + in front unary plus (ignored)
3632 * trailing (arg) funcref/partial call
3633 * trailing [] subscript in String or List
3634 * trailing .name entry in Dictionary
3635 * trailing ->name() method call
3636 */
3637 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003638compile_expr7(
3639 char_u **arg,
3640 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003641 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 char_u *start_leader, *end_leader;
3644 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003645 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003646 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647
3648 /*
3649 * Skip '!', '-' and '+' characters. They are handled later.
3650 */
3651 start_leader = *arg;
3652 while (**arg == '!' || **arg == '-' || **arg == '+')
3653 *arg = skipwhite(*arg + 1);
3654 end_leader = *arg;
3655
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003656 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 switch (**arg)
3658 {
3659 /*
3660 * Number constant.
3661 */
3662 case '0': // also for blob starting with 0z
3663 case '1':
3664 case '2':
3665 case '3':
3666 case '4':
3667 case '5':
3668 case '6':
3669 case '7':
3670 case '8':
3671 case '9':
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003672 case '.': if (get_number_tv(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 return FAIL;
3674 break;
3675
3676 /*
3677 * String constant: "string".
3678 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003679 case '"': if (get_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 return FAIL;
3681 break;
3682
3683 /*
3684 * Literal string constant: 'str''ing'.
3685 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003686 case '\'': if (get_lit_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 return FAIL;
3688 break;
3689
3690 /*
3691 * Constant Vim variable.
3692 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003693 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 ret = NOTDONE;
3695 break;
3696
3697 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003698 * "true" constant
3699 */
3700 case 't': if (STRNCMP(*arg, "true", 4) == 0
3701 && !eval_isnamec((*arg)[4]))
3702 {
3703 *arg += 4;
3704 rettv->v_type = VAR_BOOL;
3705 rettv->vval.v_number = VVAL_TRUE;
3706 }
3707 else
3708 ret = NOTDONE;
3709 break;
3710
3711 /*
3712 * "false" constant
3713 */
3714 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3715 && !eval_isnamec((*arg)[5]))
3716 {
3717 *arg += 5;
3718 rettv->v_type = VAR_BOOL;
3719 rettv->vval.v_number = VVAL_FALSE;
3720 }
3721 else
3722 ret = NOTDONE;
3723 break;
3724
3725 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 * List: [expr, expr]
3727 */
3728 case '[': ret = compile_list(arg, cctx);
3729 break;
3730
3731 /*
3732 * Dictionary: #{key: val, key: val}
3733 */
3734 case '#': if ((*arg)[1] == '{')
3735 {
3736 ++*arg;
3737 ret = compile_dict(arg, cctx, TRUE);
3738 }
3739 else
3740 ret = NOTDONE;
3741 break;
3742
3743 /*
3744 * Lambda: {arg, arg -> expr}
3745 * Dictionary: {'key': val, 'key': val}
3746 */
3747 case '{': {
3748 char_u *start = skipwhite(*arg + 1);
3749
3750 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003751 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003753 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 if (ret != FAIL && *start == '>')
3755 ret = compile_lambda(arg, cctx);
3756 else
3757 ret = compile_dict(arg, cctx, FALSE);
3758 }
3759 break;
3760
3761 /*
3762 * Option value: &name
3763 */
3764 case '&': ret = compile_get_option(arg, cctx);
3765 break;
3766
3767 /*
3768 * Environment variable: $VAR.
3769 */
3770 case '$': ret = compile_get_env(arg, cctx);
3771 break;
3772
3773 /*
3774 * Register contents: @r.
3775 */
3776 case '@': ret = compile_get_register(arg, cctx);
3777 break;
3778 /*
3779 * nested expression: (expression).
3780 */
3781 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003782
3783 // recursive!
3784 if (ppconst->pp_used <= PPSIZE - 10)
3785 {
3786 ret = compile_expr1(arg, cctx, ppconst);
3787 }
3788 else
3789 {
3790 // Not enough space in ppconst, flush constants.
3791 if (generate_ppconst(cctx, ppconst) == FAIL)
3792 return FAIL;
3793 ret = compile_expr0(arg, cctx);
3794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 *arg = skipwhite(*arg);
3796 if (**arg == ')')
3797 ++*arg;
3798 else if (ret == OK)
3799 {
3800 emsg(_(e_missing_close));
3801 ret = FAIL;
3802 }
3803 break;
3804
3805 default: ret = NOTDONE;
3806 break;
3807 }
3808 if (ret == FAIL)
3809 return FAIL;
3810
Bram Moolenaar1c747212020-05-09 18:28:34 +02003811 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 {
3813 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003814 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003816 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 return FAIL;
3818 }
3819 start_leader = end_leader; // don't apply again below
3820
Bram Moolenaara5565e42020-05-09 15:44:01 +02003821 if (cctx->ctx_skip == TRUE)
3822 clear_tv(rettv);
3823 else
3824 // A constant expression can possibly be handled compile time,
3825 // return the value instead of generating code.
3826 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 }
3828 else if (ret == NOTDONE)
3829 {
3830 char_u *p;
3831 int r;
3832
3833 if (!eval_isnamec1(**arg))
3834 {
3835 semsg(_("E1015: Name expected: %s"), *arg);
3836 return FAIL;
3837 }
3838
3839 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003840 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003842 {
3843 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003846 {
3847 if (generate_ppconst(cctx, ppconst) == FAIL)
3848 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003850 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 if (r == FAIL)
3852 return FAIL;
3853 }
3854
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003855 // Handle following "[]", ".member", etc.
3856 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003857 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003858 ppconst) == FAIL)
3859 return FAIL;
3860 if (ppconst->pp_used > 0)
3861 {
3862 // apply the '!', '-' and '+' before the constant
3863 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3864 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3865 return FAIL;
3866 return OK;
3867 }
3868 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003870 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871}
3872
3873/*
3874 * * number multiplication
3875 * / number division
3876 * % number modulo
3877 */
3878 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003879compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880{
3881 char_u *op;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003882 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003884 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003885 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 return FAIL;
3887
3888 /*
3889 * Repeat computing, until no "*", "/" or "%" is following.
3890 */
3891 for (;;)
3892 {
3893 op = skipwhite(*arg);
3894 if (*op != '*' && *op != '/' && *op != '%')
3895 break;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003896
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003897 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 {
3899 char_u buf[3];
3900
3901 vim_strncpy(buf, op, 1);
3902 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003903 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 }
3905 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003906 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003907 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003909 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003910 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003912
3913 if (ppconst->pp_used == ppconst_used + 2
3914 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3915 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003916 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003917 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3918 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003919 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003921 // both are numbers: compute the result
3922 switch (*op)
3923 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003924 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003925 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003926 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003927 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003928 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003929 break;
3930 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003931 tv1->vval.v_number = res;
3932 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003933 }
3934 else
3935 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003936 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003937 generate_two_op(cctx, op);
3938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 }
3940
3941 return OK;
3942}
3943
3944/*
3945 * + number addition
3946 * - number subtraction
3947 * .. string concatenation
3948 */
3949 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003950compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951{
3952 char_u *op;
3953 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003954 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955
3956 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003957 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 return FAIL;
3959
3960 /*
3961 * Repeat computing, until no "+", "-" or ".." is following.
3962 */
3963 for (;;)
3964 {
3965 op = skipwhite(*arg);
3966 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3967 break;
3968 oplen = (*op == '.' ? 2 : 1);
3969
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003970 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 {
3972 char_u buf[3];
3973
3974 vim_strncpy(buf, op, oplen);
3975 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003976 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 }
3978
3979 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003980 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003981 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003983 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003984 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 return FAIL;
3986
Bram Moolenaara5565e42020-05-09 15:44:01 +02003987 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003988 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003989 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3990 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3991 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3992 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003994 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3995 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003996
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003997 // concat/subtract/add constant numbers
3998 if (*op == '+')
3999 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4000 else if (*op == '-')
4001 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4002 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004003 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004004 // concatenate constant strings
4005 char_u *s1 = tv1->vval.v_string;
4006 char_u *s2 = tv2->vval.v_string;
4007 size_t len1 = STRLEN(s1);
4008
4009 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4010 if (tv1->vval.v_string == NULL)
4011 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004012 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004013 return FAIL;
4014 }
4015 mch_memmove(tv1->vval.v_string, s1, len1);
4016 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004017 vim_free(s1);
4018 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004019 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004020 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 }
4022 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004023 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004024 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004025 if (*op == '.')
4026 {
4027 if (may_generate_2STRING(-2, cctx) == FAIL
4028 || may_generate_2STRING(-1, cctx) == FAIL)
4029 return FAIL;
4030 generate_instr_drop(cctx, ISN_CONCAT, 1);
4031 }
4032 else
4033 generate_two_op(cctx, op);
4034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 }
4036
4037 return OK;
4038}
4039
4040/*
4041 * expr5a == expr5b
4042 * expr5a =~ expr5b
4043 * expr5a != expr5b
4044 * expr5a !~ expr5b
4045 * expr5a > expr5b
4046 * expr5a >= expr5b
4047 * expr5a < expr5b
4048 * expr5a <= expr5b
4049 * expr5a is expr5b
4050 * expr5a isnot expr5b
4051 *
4052 * Produces instructions:
4053 * EVAL expr5a Push result of "expr5a"
4054 * EVAL expr5b Push result of "expr5b"
4055 * COMPARE one of the compare instructions
4056 */
4057 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004058compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059{
4060 exptype_T type = EXPR_UNKNOWN;
4061 char_u *p;
4062 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004064 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065
4066 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004067 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 return FAIL;
4069
4070 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004071 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072
4073 /*
4074 * If there is a comparative operator, use it.
4075 */
4076 if (type != EXPR_UNKNOWN)
4077 {
4078 int ic = FALSE; // Default: do not ignore case
4079
4080 if (type_is && (p[len] == '?' || p[len] == '#'))
4081 {
4082 semsg(_(e_invexpr2), *arg);
4083 return FAIL;
4084 }
4085 // extra question mark appended: ignore case
4086 if (p[len] == '?')
4087 {
4088 ic = TRUE;
4089 ++len;
4090 }
4091 // extra '#' appended: match case (ignored)
4092 else if (p[len] == '#')
4093 ++len;
4094 // nothing appended: match case
4095
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004096 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 {
4098 char_u buf[7];
4099
4100 vim_strncpy(buf, p, len);
4101 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004102 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 }
4104
4105 // get the second variable
4106 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004107 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004108 return FAIL;
4109
Bram Moolenaara5565e42020-05-09 15:44:01 +02004110 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 return FAIL;
4112
Bram Moolenaara5565e42020-05-09 15:44:01 +02004113 if (ppconst->pp_used == ppconst_used + 2)
4114 {
4115 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4116 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4117 int ret;
4118
4119 // Both sides are a constant, compute the result now.
4120 // First check for a valid combination of types, this is more
4121 // strict than typval_compare().
4122 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4123 ret = FAIL;
4124 else
4125 {
4126 ret = typval_compare(tv1, tv2, type, ic);
4127 tv1->v_type = VAR_BOOL;
4128 tv1->vval.v_number = tv1->vval.v_number
4129 ? VVAL_TRUE : VVAL_FALSE;
4130 clear_tv(tv2);
4131 --ppconst->pp_used;
4132 }
4133 return ret;
4134 }
4135
4136 generate_ppconst(cctx, ppconst);
4137 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 }
4139
4140 return OK;
4141}
4142
Bram Moolenaar7f141552020-05-09 17:35:53 +02004143static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145/*
4146 * Compile || or &&.
4147 */
4148 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004149compile_and_or(
4150 char_u **arg,
4151 cctx_T *cctx,
4152 char *op,
4153 ppconst_T *ppconst,
4154 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155{
4156 char_u *p = skipwhite(*arg);
4157 int opchar = *op;
4158
4159 if (p[0] == opchar && p[1] == opchar)
4160 {
4161 garray_T *instr = &cctx->ctx_instr;
4162 garray_T end_ga;
4163
4164 /*
4165 * Repeat until there is no following "||" or "&&"
4166 */
4167 ga_init2(&end_ga, sizeof(int), 10);
4168 while (p[0] == opchar && p[1] == opchar)
4169 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004170 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4171 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004173 return FAIL;
4174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175
Bram Moolenaara5565e42020-05-09 15:44:01 +02004176 // TODO: use ppconst if the value is a constant
4177 generate_ppconst(cctx, ppconst);
4178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 if (ga_grow(&end_ga, 1) == FAIL)
4180 {
4181 ga_clear(&end_ga);
4182 return FAIL;
4183 }
4184 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4185 ++end_ga.ga_len;
4186 generate_JUMP(cctx, opchar == '|'
4187 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4188
4189 // eval the next expression
4190 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004191 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004192 return FAIL;
4193
Bram Moolenaara5565e42020-05-09 15:44:01 +02004194 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4195 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 {
4197 ga_clear(&end_ga);
4198 return FAIL;
4199 }
4200 p = skipwhite(*arg);
4201 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004202 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203
4204 // Fill in the end label in all jumps.
4205 while (end_ga.ga_len > 0)
4206 {
4207 isn_T *isn;
4208
4209 --end_ga.ga_len;
4210 isn = ((isn_T *)instr->ga_data)
4211 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4212 isn->isn_arg.jump.jump_where = instr->ga_len;
4213 }
4214 ga_clear(&end_ga);
4215 }
4216
4217 return OK;
4218}
4219
4220/*
4221 * expr4a && expr4a && expr4a logical AND
4222 *
4223 * Produces instructions:
4224 * EVAL expr4a Push result of "expr4a"
4225 * JUMP_AND_KEEP_IF_FALSE end
4226 * EVAL expr4b Push result of "expr4b"
4227 * JUMP_AND_KEEP_IF_FALSE end
4228 * EVAL expr4c Push result of "expr4c"
4229 * end:
4230 */
4231 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004232compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004234 int ppconst_used = ppconst->pp_used;
4235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004237 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 return FAIL;
4239
4240 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004241 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242}
4243
4244/*
4245 * expr3a || expr3b || expr3c logical OR
4246 *
4247 * Produces instructions:
4248 * EVAL expr3a Push result of "expr3a"
4249 * JUMP_AND_KEEP_IF_TRUE end
4250 * EVAL expr3b Push result of "expr3b"
4251 * JUMP_AND_KEEP_IF_TRUE end
4252 * EVAL expr3c Push result of "expr3c"
4253 * end:
4254 */
4255 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004256compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004258 int ppconst_used = ppconst->pp_used;
4259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004261 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 return FAIL;
4263
4264 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004265 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266}
4267
4268/*
4269 * Toplevel expression: expr2 ? expr1a : expr1b
4270 *
4271 * Produces instructions:
4272 * EVAL expr2 Push result of "expr"
4273 * JUMP_IF_FALSE alt jump if false
4274 * EVAL expr1a
4275 * JUMP_ALWAYS end
4276 * alt: EVAL expr1b
4277 * end:
4278 */
4279 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004280compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281{
4282 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004283 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284
Bram Moolenaar61a89812020-05-07 16:58:17 +02004285 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004286 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 return FAIL;
4288
4289 p = skipwhite(*arg);
4290 if (*p == '?')
4291 {
4292 garray_T *instr = &cctx->ctx_instr;
4293 garray_T *stack = &cctx->ctx_type_stack;
4294 int alt_idx = instr->ga_len;
4295 int end_idx;
4296 isn_T *isn;
4297 type_T *type1;
4298 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004299 int has_const_expr = FALSE;
4300 int const_value = FALSE;
4301 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004303 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4304 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004306 return FAIL;
4307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308
Bram Moolenaara5565e42020-05-09 15:44:01 +02004309 if (ppconst->pp_used == ppconst_used + 1)
4310 {
4311 // the condition is a constant, we know whether the ? or the :
4312 // expression is to be evaluated.
4313 has_const_expr = TRUE;
4314 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4315 clear_tv(&ppconst->pp_tv[ppconst_used]);
4316 --ppconst->pp_used;
4317 cctx->ctx_skip = save_skip == TRUE || !const_value;
4318 }
4319 else
4320 {
4321 generate_ppconst(cctx, ppconst);
4322 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4323 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324
4325 // evaluate the second expression; any type is accepted
4326 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004327 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004328 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004329 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004330 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331
Bram Moolenaara5565e42020-05-09 15:44:01 +02004332 if (!has_const_expr)
4333 {
4334 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335
Bram Moolenaara5565e42020-05-09 15:44:01 +02004336 // remember the type and drop it
4337 --stack->ga_len;
4338 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339
Bram Moolenaara5565e42020-05-09 15:44:01 +02004340 end_idx = instr->ga_len;
4341 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4342
4343 // jump here from JUMP_IF_FALSE
4344 isn = ((isn_T *)instr->ga_data) + alt_idx;
4345 isn->isn_arg.jump.jump_where = instr->ga_len;
4346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347
4348 // Check for the ":".
4349 p = skipwhite(*arg);
4350 if (*p != ':')
4351 {
4352 emsg(_(e_missing_colon));
4353 return FAIL;
4354 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004355 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4356 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004358 return FAIL;
4359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360
4361 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004362 if (has_const_expr)
4363 cctx->ctx_skip = save_skip == TRUE || const_value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004365 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004366 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004367 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004368 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369
Bram Moolenaara5565e42020-05-09 15:44:01 +02004370 if (!has_const_expr)
4371 {
4372 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373
Bram Moolenaara5565e42020-05-09 15:44:01 +02004374 // If the types differ, the result has a more generic type.
4375 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4376 common_type(type1, type2, &type2, cctx->ctx_type_list);
4377
4378 // jump here from JUMP_ALWAYS
4379 isn = ((isn_T *)instr->ga_data) + end_idx;
4380 isn->isn_arg.jump.jump_where = instr->ga_len;
4381 }
4382
4383 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 }
4385 return OK;
4386}
4387
4388/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004389 * Toplevel expression.
4390 */
4391 static int
4392compile_expr0(char_u **arg, cctx_T *cctx)
4393{
4394 ppconst_T ppconst;
4395
4396 CLEAR_FIELD(ppconst);
4397 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4398 {
4399 clear_ppconst(&ppconst);
4400 return FAIL;
4401 }
4402 if (generate_ppconst(cctx, &ppconst) == FAIL)
4403 return FAIL;
4404 return OK;
4405}
4406
4407/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 * compile "return [expr]"
4409 */
4410 static char_u *
4411compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4412{
4413 char_u *p = arg;
4414 garray_T *stack = &cctx->ctx_type_stack;
4415 type_T *stack_type;
4416
4417 if (*p != NUL && *p != '|' && *p != '\n')
4418 {
4419 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004420 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 return NULL;
4422
4423 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4424 if (set_return_type)
4425 cctx->ctx_ufunc->uf_ret_type = stack_type;
4426 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4427 == FAIL)
4428 return NULL;
4429 }
4430 else
4431 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004432 // "set_return_type" cannot be TRUE, only used for a lambda which
4433 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004434 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4435 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 {
4437 emsg(_("E1003: Missing return value"));
4438 return NULL;
4439 }
4440
4441 // No argument, return zero.
4442 generate_PUSHNR(cctx, 0);
4443 }
4444
4445 if (generate_instr(cctx, ISN_RETURN) == NULL)
4446 return NULL;
4447
4448 // "return val | endif" is possible
4449 return skipwhite(p);
4450}
4451
4452/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004453 * Get a line from the compilation context, compatible with exarg_T getline().
4454 * Return a pointer to the line in allocated memory.
4455 * Return NULL for end-of-file or some error.
4456 */
4457 static char_u *
4458exarg_getline(
4459 int c UNUSED,
4460 void *cookie,
4461 int indent UNUSED,
4462 int do_concat UNUSED)
4463{
4464 cctx_T *cctx = (cctx_T *)cookie;
4465
4466 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4467 {
4468 iemsg("Heredoc got to end");
4469 return NULL;
4470 }
4471 ++cctx->ctx_lnum;
4472 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4473 [cctx->ctx_lnum]);
4474}
4475
4476/*
4477 * Compile a nested :def command.
4478 */
4479 static char_u *
4480compile_nested_function(exarg_T *eap, cctx_T *cctx)
4481{
4482 char_u *name_start = eap->arg;
4483 char_u *name_end = to_name_end(eap->arg, FALSE);
4484 char_u *name = get_lambda_name();
4485 lvar_T *lvar;
4486 ufunc_T *ufunc;
4487
4488 eap->arg = name_end;
4489 eap->getline = exarg_getline;
4490 eap->cookie = cctx;
4491 eap->skip = cctx->ctx_skip == TRUE;
4492 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004493 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004494
Bram Moolenaar822ba242020-05-24 23:00:18 +02004495 if (ufunc == NULL)
4496 return NULL;
4497 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED
4498 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004499 return NULL;
4500
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004501 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004502 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004503 TRUE, ufunc->uf_func_type);
4504
4505 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4506 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4507 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004508
Bram Moolenaar61a89812020-05-07 16:58:17 +02004509 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004510 return (char_u *)"";
4511}
4512
4513/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 * Return the length of an assignment operator, or zero if there isn't one.
4515 */
4516 int
4517assignment_len(char_u *p, int *heredoc)
4518{
4519 if (*p == '=')
4520 {
4521 if (p[1] == '<' && p[2] == '<')
4522 {
4523 *heredoc = TRUE;
4524 return 3;
4525 }
4526 return 1;
4527 }
4528 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4529 return 2;
4530 if (STRNCMP(p, "..=", 3) == 0)
4531 return 3;
4532 return 0;
4533}
4534
4535// words that cannot be used as a variable
4536static char *reserved[] = {
4537 "true",
4538 "false",
4539 NULL
4540};
4541
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004542typedef enum {
4543 dest_local,
4544 dest_option,
4545 dest_env,
4546 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004547 dest_buffer,
4548 dest_window,
4549 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004550 dest_vimvar,
4551 dest_script,
4552 dest_reg,
4553} assign_dest_T;
4554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004556 * Generate the load instruction for "name".
4557 */
4558 static void
4559generate_loadvar(
4560 cctx_T *cctx,
4561 assign_dest_T dest,
4562 char_u *name,
4563 lvar_T *lvar,
4564 type_T *type)
4565{
4566 switch (dest)
4567 {
4568 case dest_option:
4569 // TODO: check the option exists
4570 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4571 break;
4572 case dest_global:
4573 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4574 break;
4575 case dest_buffer:
4576 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4577 break;
4578 case dest_window:
4579 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4580 break;
4581 case dest_tab:
4582 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4583 break;
4584 case dest_script:
4585 compile_load_scriptvar(cctx,
4586 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4587 break;
4588 case dest_env:
4589 // Include $ in the name here
4590 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4591 break;
4592 case dest_reg:
4593 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4594 break;
4595 case dest_vimvar:
4596 generate_LOADV(cctx, name + 2, TRUE);
4597 break;
4598 case dest_local:
4599 if (lvar->lv_from_outer)
4600 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4601 NULL, type);
4602 else
4603 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4604 break;
4605 }
4606}
4607
4608/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004609 * Compile declaration and assignment:
4610 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004612 * Return NULL for an error.
4613 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 */
4615 static char_u *
4616compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4617{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004618 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004620 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 char_u *ret = NULL;
4622 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004623 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004626 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 int oplen = 0;
4629 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004630 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004631 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004632 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004636 // Skip over the "var" or "[var, var]" to get to any "=".
4637 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4638 if (p == NULL)
4639 return *arg == '[' ? arg : NULL;
4640
4641 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004643 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 return NULL;
4645 }
4646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 sp = p;
4648 p = skipwhite(p);
4649 op = p;
4650 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004651
4652 if (var_count > 0 && oplen == 0)
4653 // can be something like "[1, 2]->func()"
4654 return arg;
4655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4657 {
4658 char_u buf[4];
4659
4660 vim_strncpy(buf, op, oplen);
4661 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004662 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004663 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 if (heredoc)
4666 {
4667 list_T *l;
4668 listitem_T *li;
4669
4670 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004671 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004673 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674
4675 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004676 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 {
4678 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4679 li->li_tv.vval.v_string = NULL;
4680 }
4681 generate_NEWLIST(cctx, l->lv_len);
4682 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004683 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 list_free(l);
4685 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004686 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004688 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004690 // for "[var, var] = expr" evaluate the expression here, loop over the
4691 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004692
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004693 p = skipwhite(op + oplen);
4694 if (compile_expr0(&p, cctx) == FAIL)
4695 return NULL;
4696 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004698 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004700 type_T *stacktype;
4701
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004702 stacktype = stack->ga_len == 0 ? &t_void
4703 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004704 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004706 emsg(_(e_cannot_use_void));
4707 goto theend;
4708 }
4709 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4710 goto theend;
4711 // TODO: check length of list to be var_count (or more if
4712 // "semicolon" set)
4713 }
4714 }
4715
4716 /*
4717 * Loop over variables in "[var, var] = expr".
4718 * For "var = expr" and "let var: type" this is done only once.
4719 */
4720 if (var_count > 0)
4721 var_start = skipwhite(arg + 1); // skip over the "["
4722 else
4723 var_start = arg;
4724 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4725 {
4726 char_u *var_end = skip_var_one(var_start, FALSE);
4727 size_t varlen;
4728 int new_local = FALSE;
4729 int opt_type;
4730 int opt_flags = 0;
4731 assign_dest_T dest = dest_local;
4732 int vimvaridx = -1;
4733 lvar_T *lvar = NULL;
4734 lvar_T arg_lvar;
4735 int has_type = FALSE;
4736 int has_index = FALSE;
4737 int instr_count = -1;
4738
4739 p = (*var_start == '&' || *var_start == '$'
4740 || *var_start == '@') ? var_start + 1 : var_start;
4741 p = to_name_end(p, TRUE);
4742
4743 // "a: type" is declaring variable "a" with a type, not "a:".
4744 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4745 --var_end;
4746 if (is_decl && p == var_start + 2 && p[-1] == ':')
4747 --p;
4748
4749 varlen = p - var_start;
4750 vim_free(name);
4751 name = vim_strnsave(var_start, varlen);
4752 if (name == NULL)
4753 return NULL;
4754 if (!heredoc)
4755 type = &t_any;
4756
4757 if (cctx->ctx_skip != TRUE)
4758 {
4759 if (*var_start == '&')
4760 {
4761 int cc;
4762 long numval;
4763
4764 dest = dest_option;
4765 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004767 emsg(_(e_const_option));
4768 goto theend;
4769 }
4770 if (is_decl)
4771 {
4772 semsg(_("E1052: Cannot declare an option: %s"), var_start);
4773 goto theend;
4774 }
4775 p = var_start;
4776 p = find_option_end(&p, &opt_flags);
4777 if (p == NULL)
4778 {
4779 // cannot happen?
4780 emsg(_(e_letunexp));
4781 goto theend;
4782 }
4783 cc = *p;
4784 *p = NUL;
4785 opt_type = get_option_value(var_start + 1, &numval,
4786 NULL, opt_flags);
4787 *p = cc;
4788 if (opt_type == -3)
4789 {
4790 semsg(_(e_unknown_option), var_start);
4791 goto theend;
4792 }
4793 if (opt_type == -2 || opt_type == 0)
4794 type = &t_string;
4795 else
4796 type = &t_number; // both number and boolean option
4797 }
4798 else if (*var_start == '$')
4799 {
4800 dest = dest_env;
4801 type = &t_string;
4802 if (is_decl)
4803 {
4804 semsg(_("E1065: Cannot declare an environment variable: %s"),
4805 name);
4806 goto theend;
4807 }
4808 }
4809 else if (*var_start == '@')
4810 {
4811 if (!valid_yank_reg(var_start[1], TRUE))
4812 {
4813 emsg_invreg(var_start[1]);
4814 goto theend;
4815 }
4816 dest = dest_reg;
4817 type = &t_string;
4818 if (is_decl)
4819 {
4820 semsg(_("E1066: Cannot declare a register: %s"), name);
4821 goto theend;
4822 }
4823 }
4824 else if (STRNCMP(var_start, "g:", 2) == 0)
4825 {
4826 dest = dest_global;
4827 if (is_decl)
4828 {
4829 semsg(_("E1016: Cannot declare a global variable: %s"),
4830 name);
4831 goto theend;
4832 }
4833 }
4834 else if (STRNCMP(var_start, "b:", 2) == 0)
4835 {
4836 dest = dest_buffer;
4837 if (is_decl)
4838 {
4839 semsg(_("E1078: Cannot declare a buffer variable: %s"),
4840 name);
4841 goto theend;
4842 }
4843 }
4844 else if (STRNCMP(var_start, "w:", 2) == 0)
4845 {
4846 dest = dest_window;
4847 if (is_decl)
4848 {
4849 semsg(_("E1079: Cannot declare a window variable: %s"),
4850 name);
4851 goto theend;
4852 }
4853 }
4854 else if (STRNCMP(var_start, "t:", 2) == 0)
4855 {
4856 dest = dest_tab;
4857 if (is_decl)
4858 {
4859 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4860 goto theend;
4861 }
4862 }
4863 else if (STRNCMP(var_start, "v:", 2) == 0)
4864 {
4865 typval_T *vtv;
4866 int di_flags;
4867
4868 vimvaridx = find_vim_var(name + 2, &di_flags);
4869 if (vimvaridx < 0)
4870 {
4871 semsg(_(e_var_notfound), var_start);
4872 goto theend;
4873 }
4874 // We use the current value of "sandbox" here, is that OK?
4875 if (var_check_ro(di_flags, name, FALSE))
4876 goto theend;
4877 dest = dest_vimvar;
4878 vtv = get_vim_var_tv(vimvaridx);
4879 type = typval2type(vtv);
4880 if (is_decl)
4881 {
4882 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4883 goto theend;
4884 }
4885 }
4886 else
4887 {
4888 int idx;
4889
4890 for (idx = 0; reserved[idx] != NULL; ++idx)
4891 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004892 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004893 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004894 goto theend;
4895 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004896
4897 lvar = lookup_local(var_start, varlen, cctx);
4898 if (lvar == NULL)
4899 {
4900 CLEAR_FIELD(arg_lvar);
4901 if (lookup_arg(var_start, varlen,
4902 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4903 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004904 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004905 if (is_decl)
4906 {
4907 semsg(_(e_used_as_arg), name);
4908 goto theend;
4909 }
4910 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004911 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004912 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004913 if (lvar != NULL)
4914 {
4915 if (is_decl)
4916 {
4917 semsg(_("E1017: Variable already declared: %s"), name);
4918 goto theend;
4919 }
4920 else if (lvar->lv_const)
4921 {
4922 semsg(_("E1018: Cannot assign to a constant: %s"),
4923 name);
4924 goto theend;
4925 }
4926 }
4927 else if (STRNCMP(var_start, "s:", 2) == 0
4928 || lookup_script(var_start, varlen) == OK
4929 || find_imported(var_start, varlen, cctx) != NULL)
4930 {
4931 dest = dest_script;
4932 if (is_decl)
4933 {
4934 semsg(_("E1054: Variable already declared in the script: %s"),
4935 name);
4936 goto theend;
4937 }
4938 }
4939 else if (name[1] == ':' && name[2] != NUL)
4940 {
4941 semsg(_("E1082: Cannot use a namespaced variable: %s"),
4942 name);
4943 goto theend;
4944 }
4945 else if (!is_decl)
4946 {
4947 semsg(_("E1089: unknown variable: %s"), name);
4948 goto theend;
4949 }
4950 }
4951 }
4952
4953 // handle "a:name" as a name, not index "name" on "a"
4954 if (varlen > 1 || var_start[varlen] != ':')
4955 p = var_end;
4956
4957 if (dest != dest_option)
4958 {
4959 if (is_decl && *p == ':')
4960 {
4961 // parse optional type: "let var: type = expr"
4962 if (!VIM_ISWHITE(p[1]))
4963 {
4964 semsg(_(e_white_after), ":");
4965 goto theend;
4966 }
4967 p = skipwhite(p + 1);
4968 type = parse_type(&p, cctx->ctx_type_list);
4969 has_type = TRUE;
4970 }
4971 else if (lvar != NULL)
4972 type = lvar->lv_type;
4973 }
4974
4975 if (oplen == 3 && !heredoc && dest != dest_global
4976 && type->tt_type != VAR_STRING
4977 && type->tt_type != VAR_ANY)
4978 {
4979 emsg(_("E1019: Can only concatenate to string"));
4980 goto theend;
4981 }
4982
4983 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
4984 {
4985 if (oplen > 1 && !heredoc)
4986 {
4987 // +=, /=, etc. require an existing variable
4988 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4989 name);
4990 goto theend;
4991 }
4992
4993 // new local variable
4994 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
4995 goto theend;
4996 lvar = reserve_local(cctx, var_start, varlen,
4997 cmdidx == CMD_const, type);
4998 if (lvar == NULL)
4999 goto theend;
5000 new_local = TRUE;
5001 }
5002
5003 member_type = type;
5004 if (var_end > var_start + varlen)
5005 {
5006 // Something follows after the variable: "var[idx]".
5007 if (is_decl)
5008 {
5009 emsg(_("E1087: cannot use an index when declaring a variable"));
5010 goto theend;
5011 }
5012
5013 if (var_start[varlen] == '[')
5014 {
5015 has_index = TRUE;
5016 if (type->tt_member == NULL)
5017 {
5018 semsg(_("E1088: cannot use an index on %s"), name);
5019 goto theend;
5020 }
5021 member_type = type->tt_member;
5022 }
5023 else
5024 {
5025 semsg("Not supported yet: %s", var_start);
5026 goto theend;
5027 }
5028 }
5029 else if (lvar == &arg_lvar)
5030 {
5031 semsg(_("E1090: Cannot assign to argument %s"), name);
5032 goto theend;
5033 }
5034
5035 if (!heredoc)
5036 {
5037 if (oplen > 0)
5038 {
5039 // For "var = expr" evaluate the expression.
5040 if (var_count == 0)
5041 {
5042 int r;
5043
5044 // for "+=", "*=", "..=" etc. first load the current value
5045 if (*op != '=')
5046 {
5047 generate_loadvar(cctx, dest, name, lvar, type);
5048
5049 if (has_index)
5050 {
5051 // TODO: get member from list or dict
5052 emsg("Index with operation not supported yet");
5053 goto theend;
5054 }
5055 }
5056
5057 // Compile the expression. Temporarily hide the new local
5058 // variable here, it is not available to this expression.
5059 if (new_local)
5060 --cctx->ctx_locals.ga_len;
5061 instr_count = instr->ga_len;
5062 p = skipwhite(op + oplen);
5063 r = compile_expr0(&p, cctx);
5064 if (new_local)
5065 ++cctx->ctx_locals.ga_len;
5066 if (r == FAIL)
5067 goto theend;
5068 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005069 else
5070 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005071 // For "[var, var] = expr" get the "var_idx" item from the
5072 // list.
5073 if (generate_GETITEM(cctx, var_idx) == FAIL)
5074 return FAIL;
5075 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005076
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005077 if (cctx->ctx_skip != TRUE)
5078 {
5079 type_T *stacktype;
5080
5081 stacktype = stack->ga_len == 0 ? &t_void
5082 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5083 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005084 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005085 if (new_local && !has_type)
5086 {
5087 if (stacktype->tt_type == VAR_VOID)
5088 {
5089 emsg(_(e_cannot_use_void));
5090 goto theend;
5091 }
5092 else
5093 {
5094 // An empty list or dict has a &t_void member,
5095 // for a variable that implies &t_any.
5096 if (stacktype == &t_list_empty)
5097 lvar->lv_type = &t_list_any;
5098 else if (stacktype == &t_dict_empty)
5099 lvar->lv_type = &t_dict_any;
5100 else
5101 lvar->lv_type = stacktype;
5102 }
5103 }
5104 else
5105 {
5106 type_T *use_type = lvar->lv_type;
5107
5108 if (has_index)
5109 {
5110 use_type = use_type->tt_member;
5111 if (use_type == NULL)
5112 use_type = &t_void;
5113 }
5114 if (need_type(stacktype, use_type, -1, cctx)
5115 == FAIL)
5116 goto theend;
5117 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005118 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005119 else if (*p != '=' && need_type(stacktype, member_type, -1,
5120 cctx) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005121 goto theend;
5122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005124 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005126 emsg(_(e_const_req_value));
5127 goto theend;
5128 }
5129 else if (!has_type || dest == dest_option)
5130 {
5131 emsg(_(e_type_req));
5132 goto theend;
5133 }
5134 else
5135 {
5136 // variables are always initialized
5137 if (ga_grow(instr, 1) == FAIL)
5138 goto theend;
5139 switch (member_type->tt_type)
5140 {
5141 case VAR_BOOL:
5142 generate_PUSHBOOL(cctx, VVAL_FALSE);
5143 break;
5144 case VAR_FLOAT:
5145#ifdef FEAT_FLOAT
5146 generate_PUSHF(cctx, 0.0);
5147#endif
5148 break;
5149 case VAR_STRING:
5150 generate_PUSHS(cctx, NULL);
5151 break;
5152 case VAR_BLOB:
5153 generate_PUSHBLOB(cctx, NULL);
5154 break;
5155 case VAR_FUNC:
5156 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5157 break;
5158 case VAR_LIST:
5159 generate_NEWLIST(cctx, 0);
5160 break;
5161 case VAR_DICT:
5162 generate_NEWDICT(cctx, 0);
5163 break;
5164 case VAR_JOB:
5165 generate_PUSHJOB(cctx, NULL);
5166 break;
5167 case VAR_CHANNEL:
5168 generate_PUSHCHANNEL(cctx, NULL);
5169 break;
5170 case VAR_NUMBER:
5171 case VAR_UNKNOWN:
5172 case VAR_ANY:
5173 case VAR_PARTIAL:
5174 case VAR_VOID:
5175 case VAR_SPECIAL: // cannot happen
5176 generate_PUSHNR(cctx, 0);
5177 break;
5178 }
5179 }
5180 if (var_count == 0)
5181 end = p;
5182 }
5183
5184 if (oplen > 0 && *op != '=')
5185 {
5186 type_T *expected = &t_number;
5187 type_T *stacktype;
5188
5189 // TODO: if type is known use float or any operation
5190
5191 if (*op == '.')
5192 expected = &t_string;
5193 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5194 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5195 goto theend;
5196
5197 if (*op == '.')
5198 generate_instr_drop(cctx, ISN_CONCAT, 1);
5199 else
5200 {
5201 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5202
5203 if (isn == NULL)
5204 goto theend;
5205 switch (*op)
5206 {
5207 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5208 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5209 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5210 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5211 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213 }
5214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005216 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005217 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005218 int r;
5219
5220 // Compile the "idx" in "var[idx]".
5221 if (new_local)
5222 --cctx->ctx_locals.ga_len;
5223 p = skipwhite(var_start + varlen + 1);
5224 r = compile_expr0(&p, cctx);
5225 if (new_local)
5226 ++cctx->ctx_locals.ga_len;
5227 if (r == FAIL)
5228 goto theend;
5229 if (*skipwhite(p) != ']')
5230 {
5231 emsg(_(e_missbrac));
5232 goto theend;
5233 }
5234 if (type->tt_type == VAR_DICT
5235 && may_generate_2STRING(-1, cctx) == FAIL)
5236 goto theend;
5237 if (type->tt_type == VAR_LIST
5238 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005239 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005240 {
5241 emsg(_(e_number_exp));
5242 goto theend;
5243 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005244
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005245 // Load the dict or list. On the stack we then have:
5246 // - value
5247 // - index
5248 // - variable
5249 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005250
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005251 if (type->tt_type == VAR_LIST)
5252 {
5253 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5254 return FAIL;
5255 }
5256 else if (type->tt_type == VAR_DICT)
5257 {
5258 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5259 return FAIL;
5260 }
5261 else
5262 {
5263 emsg(_(e_listreq));
5264 goto theend;
5265 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005266 }
5267 else
5268 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005269 switch (dest)
5270 {
5271 case dest_option:
5272 generate_STOREOPT(cctx, name + 1, opt_flags);
5273 break;
5274 case dest_global:
5275 // include g: with the name, easier to execute that way
5276 generate_STORE(cctx, ISN_STOREG, 0, name);
5277 break;
5278 case dest_buffer:
5279 // include b: with the name, easier to execute that way
5280 generate_STORE(cctx, ISN_STOREB, 0, name);
5281 break;
5282 case dest_window:
5283 // include w: with the name, easier to execute that way
5284 generate_STORE(cctx, ISN_STOREW, 0, name);
5285 break;
5286 case dest_tab:
5287 // include t: with the name, easier to execute that way
5288 generate_STORE(cctx, ISN_STORET, 0, name);
5289 break;
5290 case dest_env:
5291 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5292 break;
5293 case dest_reg:
5294 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5295 break;
5296 case dest_vimvar:
5297 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5298 break;
5299 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005300 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005301 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5302 imported_T *import = NULL;
5303 int sid = current_sctx.sc_sid;
5304 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005305
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005306 if (name[1] != ':')
5307 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005308 import = find_imported(name, 0, cctx);
5309 if (import != NULL)
5310 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005311 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005312
5313 idx = get_script_item_idx(sid, rawname, TRUE);
5314 // TODO: specific type
5315 if (idx < 0)
5316 {
5317 char_u *name_s = name;
5318
5319 // Include s: in the name for store_var()
5320 if (name[1] != ':')
5321 {
5322 int len = (int)STRLEN(name) + 3;
5323
5324 name_s = alloc(len);
5325 if (name_s == NULL)
5326 name_s = name;
5327 else
5328 vim_snprintf((char *)name_s, len,
5329 "s:%s", name);
5330 }
5331 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005332 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333 if (name_s != name)
5334 vim_free(name_s);
5335 }
5336 else
5337 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005338 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005339 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005340 break;
5341 case dest_local:
5342 if (lvar != NULL)
5343 {
5344 isn_T *isn = ((isn_T *)instr->ga_data)
5345 + instr->ga_len - 1;
5346
5347 // optimization: turn "var = 123" from ISN_PUSHNR +
5348 // ISN_STORE into ISN_STORENR
5349 if (!lvar->lv_from_outer
5350 && instr->ga_len == instr_count + 1
5351 && isn->isn_type == ISN_PUSHNR)
5352 {
5353 varnumber_T val = isn->isn_arg.number;
5354
5355 isn->isn_type = ISN_STORENR;
5356 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5357 isn->isn_arg.storenr.stnr_val = val;
5358 if (stack->ga_len > 0)
5359 --stack->ga_len;
5360 }
5361 else if (lvar->lv_from_outer)
5362 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005363 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005364 else
5365 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5366 }
5367 break;
5368 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005369 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005370
5371 if (var_idx + 1 < var_count)
5372 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005374
5375 // for "[var, var] = expr" drop the "expr" value
5376 if (var_count > 0 && generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5377 goto theend;
5378
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005379 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005380
5381theend:
5382 vim_free(name);
5383 return ret;
5384}
5385
5386/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005387 * Check if "name" can be "unlet".
5388 */
5389 int
5390check_vim9_unlet(char_u *name)
5391{
5392 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5393 {
5394 semsg(_("E1081: Cannot unlet %s"), name);
5395 return FAIL;
5396 }
5397 return OK;
5398}
5399
5400/*
5401 * Callback passed to ex_unletlock().
5402 */
5403 static int
5404compile_unlet(
5405 lval_T *lvp,
5406 char_u *name_end,
5407 exarg_T *eap,
5408 int deep UNUSED,
5409 void *coookie)
5410{
5411 cctx_T *cctx = coookie;
5412
5413 if (lvp->ll_tv == NULL)
5414 {
5415 char_u *p = lvp->ll_name;
5416 int cc = *name_end;
5417 int ret = OK;
5418
5419 // Normal name. Only supports g:, w:, t: and b: namespaces.
5420 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005421 if (*p == '$')
5422 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5423 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005424 ret = FAIL;
5425 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005426 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005427
5428 *name_end = cc;
5429 return ret;
5430 }
5431
5432 // TODO: unlet {list}[idx]
5433 // TODO: unlet {dict}[key]
5434 emsg("Sorry, :unlet not fully implemented yet");
5435 return FAIL;
5436}
5437
5438/*
5439 * compile "unlet var", "lock var" and "unlock var"
5440 * "arg" points to "var".
5441 */
5442 static char_u *
5443compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5444{
5445 char_u *p = arg;
5446
5447 if (eap->cmdidx != CMD_unlet)
5448 {
5449 emsg("Sorry, :lock and unlock not implemented yet");
5450 return NULL;
5451 }
5452
5453 if (*p == '!')
5454 {
5455 p = skipwhite(p + 1);
5456 eap->forceit = TRUE;
5457 }
5458
5459 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5460 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5461}
5462
5463/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005464 * Compile an :import command.
5465 */
5466 static char_u *
5467compile_import(char_u *arg, cctx_T *cctx)
5468{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005469 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470}
5471
5472/*
5473 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5474 */
5475 static int
5476compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5477{
5478 garray_T *instr = &cctx->ctx_instr;
5479 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5480
5481 if (endlabel == NULL)
5482 return FAIL;
5483 endlabel->el_next = *el;
5484 *el = endlabel;
5485 endlabel->el_end_label = instr->ga_len;
5486
5487 generate_JUMP(cctx, when, 0);
5488 return OK;
5489}
5490
5491 static void
5492compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5493{
5494 garray_T *instr = &cctx->ctx_instr;
5495
5496 while (*el != NULL)
5497 {
5498 endlabel_T *cur = (*el);
5499 isn_T *isn;
5500
5501 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5502 isn->isn_arg.jump.jump_where = instr->ga_len;
5503 *el = cur->el_next;
5504 vim_free(cur);
5505 }
5506}
5507
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005508 static void
5509compile_free_jump_to_end(endlabel_T **el)
5510{
5511 while (*el != NULL)
5512 {
5513 endlabel_T *cur = (*el);
5514
5515 *el = cur->el_next;
5516 vim_free(cur);
5517 }
5518}
5519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005520/*
5521 * Create a new scope and set up the generic items.
5522 */
5523 static scope_T *
5524new_scope(cctx_T *cctx, scopetype_T type)
5525{
5526 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5527
5528 if (scope == NULL)
5529 return NULL;
5530 scope->se_outer = cctx->ctx_scope;
5531 cctx->ctx_scope = scope;
5532 scope->se_type = type;
5533 scope->se_local_count = cctx->ctx_locals.ga_len;
5534 return scope;
5535}
5536
5537/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005538 * Free the current scope and go back to the outer scope.
5539 */
5540 static void
5541drop_scope(cctx_T *cctx)
5542{
5543 scope_T *scope = cctx->ctx_scope;
5544
5545 if (scope == NULL)
5546 {
5547 iemsg("calling drop_scope() without a scope");
5548 return;
5549 }
5550 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005551 switch (scope->se_type)
5552 {
5553 case IF_SCOPE:
5554 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5555 case FOR_SCOPE:
5556 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5557 case WHILE_SCOPE:
5558 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5559 case TRY_SCOPE:
5560 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5561 case NO_SCOPE:
5562 case BLOCK_SCOPE:
5563 break;
5564 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005565 vim_free(scope);
5566}
5567
5568/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005569 * compile "if expr"
5570 *
5571 * "if expr" Produces instructions:
5572 * EVAL expr Push result of "expr"
5573 * JUMP_IF_FALSE end
5574 * ... body ...
5575 * end:
5576 *
5577 * "if expr | else" Produces instructions:
5578 * EVAL expr Push result of "expr"
5579 * JUMP_IF_FALSE else
5580 * ... body ...
5581 * JUMP_ALWAYS end
5582 * else:
5583 * ... body ...
5584 * end:
5585 *
5586 * "if expr1 | elseif expr2 | else" Produces instructions:
5587 * EVAL expr Push result of "expr"
5588 * JUMP_IF_FALSE elseif
5589 * ... body ...
5590 * JUMP_ALWAYS end
5591 * elseif:
5592 * EVAL expr Push result of "expr"
5593 * JUMP_IF_FALSE else
5594 * ... body ...
5595 * JUMP_ALWAYS end
5596 * else:
5597 * ... body ...
5598 * end:
5599 */
5600 static char_u *
5601compile_if(char_u *arg, cctx_T *cctx)
5602{
5603 char_u *p = arg;
5604 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005605 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005606 scope_T *scope;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005607 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005608
Bram Moolenaara5565e42020-05-09 15:44:01 +02005609 CLEAR_FIELD(ppconst);
5610 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005611 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005612 clear_ppconst(&ppconst);
5613 return NULL;
5614 }
5615 if (instr->ga_len == instr_count && ppconst.pp_used == 1)
5616 {
5617 // The expression results in a constant.
5618 // TODO: how about nesting?
5619 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? FALSE : TRUE;
5620 clear_ppconst(&ppconst);
5621 }
5622 else
5623 {
5624 // Not a constant, generate instructions for the expression.
5625 cctx->ctx_skip = MAYBE;
5626 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005627 return NULL;
5628 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005629
5630 scope = new_scope(cctx, IF_SCOPE);
5631 if (scope == NULL)
5632 return NULL;
5633
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005634 if (cctx->ctx_skip == MAYBE)
5635 {
5636 // "where" is set when ":elseif", "else" or ":endif" is found
5637 scope->se_u.se_if.is_if_label = instr->ga_len;
5638 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5639 }
5640 else
5641 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642
5643 return p;
5644}
5645
5646 static char_u *
5647compile_elseif(char_u *arg, cctx_T *cctx)
5648{
5649 char_u *p = arg;
5650 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005651 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652 isn_T *isn;
5653 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005654 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005655
5656 if (scope == NULL || scope->se_type != IF_SCOPE)
5657 {
5658 emsg(_(e_elseif_without_if));
5659 return NULL;
5660 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005661 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005662
Bram Moolenaar158906c2020-02-06 20:39:45 +01005663 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005664 {
5665 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005667 return NULL;
5668 // previous "if" or "elseif" jumps here
5669 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5670 isn->isn_arg.jump.jump_where = instr->ga_len;
5671 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005673 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005674 CLEAR_FIELD(ppconst);
5675 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005676 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005677 clear_ppconst(&ppconst);
5678 return NULL;
5679 }
5680 if (instr->ga_len == instr_count && ppconst.pp_used == 1)
5681 {
5682 // The expression results in a constant.
5683 // TODO: how about nesting?
5684 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? FALSE : TRUE;
5685 clear_ppconst(&ppconst);
5686 scope->se_u.se_if.is_if_label = -1;
5687 }
5688 else
5689 {
5690 // Not a constant, generate instructions for the expression.
5691 cctx->ctx_skip = MAYBE;
5692 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005693 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005694
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005695 // "where" is set when ":elseif", "else" or ":endif" is found
5696 scope->se_u.se_if.is_if_label = instr->ga_len;
5697 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005699
5700 return p;
5701}
5702
5703 static char_u *
5704compile_else(char_u *arg, cctx_T *cctx)
5705{
5706 char_u *p = arg;
5707 garray_T *instr = &cctx->ctx_instr;
5708 isn_T *isn;
5709 scope_T *scope = cctx->ctx_scope;
5710
5711 if (scope == NULL || scope->se_type != IF_SCOPE)
5712 {
5713 emsg(_(e_else_without_if));
5714 return NULL;
5715 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005716 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005717
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005718 // jump from previous block to the end, unless the else block is empty
5719 if (cctx->ctx_skip == MAYBE)
5720 {
5721 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005722 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005723 return NULL;
5724 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005725
Bram Moolenaar158906c2020-02-06 20:39:45 +01005726 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005727 {
5728 if (scope->se_u.se_if.is_if_label >= 0)
5729 {
5730 // previous "if" or "elseif" jumps here
5731 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5732 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005733 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005734 }
5735 }
5736
5737 if (cctx->ctx_skip != MAYBE)
5738 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005739
5740 return p;
5741}
5742
5743 static char_u *
5744compile_endif(char_u *arg, cctx_T *cctx)
5745{
5746 scope_T *scope = cctx->ctx_scope;
5747 ifscope_T *ifscope;
5748 garray_T *instr = &cctx->ctx_instr;
5749 isn_T *isn;
5750
5751 if (scope == NULL || scope->se_type != IF_SCOPE)
5752 {
5753 emsg(_(e_endif_without_if));
5754 return NULL;
5755 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005756 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005757 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005758
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005759 if (scope->se_u.se_if.is_if_label >= 0)
5760 {
5761 // previous "if" or "elseif" jumps here
5762 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5763 isn->isn_arg.jump.jump_where = instr->ga_len;
5764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005765 // Fill in the "end" label in jumps at the end of the blocks.
5766 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005767 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005768
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005769 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005770 return arg;
5771}
5772
5773/*
5774 * compile "for var in expr"
5775 *
5776 * Produces instructions:
5777 * PUSHNR -1
5778 * STORE loop-idx Set index to -1
5779 * EVAL expr Push result of "expr"
5780 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5781 * - if beyond end, jump to "end"
5782 * - otherwise get item from list and push it
5783 * STORE var Store item in "var"
5784 * ... body ...
5785 * JUMP top Jump back to repeat
5786 * end: DROP Drop the result of "expr"
5787 *
5788 */
5789 static char_u *
5790compile_for(char_u *arg, cctx_T *cctx)
5791{
5792 char_u *p;
5793 size_t varlen;
5794 garray_T *instr = &cctx->ctx_instr;
5795 garray_T *stack = &cctx->ctx_type_stack;
5796 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005797 lvar_T *loop_lvar; // loop iteration variable
5798 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005799 type_T *vartype;
5800
5801 // TODO: list of variables: "for [key, value] in dict"
5802 // parse "var"
5803 for (p = arg; eval_isnamec1(*p); ++p)
5804 ;
5805 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005806 var_lvar = lookup_local(arg, varlen, cctx);
5807 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005808 {
5809 semsg(_("E1023: variable already defined: %s"), arg);
5810 return NULL;
5811 }
5812
5813 // consume "in"
5814 p = skipwhite(p);
5815 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5816 {
5817 emsg(_(e_missing_in));
5818 return NULL;
5819 }
5820 p = skipwhite(p + 2);
5821
5822
5823 scope = new_scope(cctx, FOR_SCOPE);
5824 if (scope == NULL)
5825 return NULL;
5826
5827 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005828 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5829 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005830 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005831 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005832 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005833 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005834 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005835
5836 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005837 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5838 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005839 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005840 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005841 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005842 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005843 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005845 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005846
5847 // compile "expr", it remains on the stack until "endfor"
5848 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005849 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005850 {
5851 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005854
5855 // now we know the type of "var"
5856 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5857 if (vartype->tt_type != VAR_LIST)
5858 {
5859 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005860 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861 return NULL;
5862 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005863 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005864 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005865
5866 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005867 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005868
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005869 generate_FOR(cctx, loop_lvar->lv_idx);
5870 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005871
5872 return arg;
5873}
5874
5875/*
5876 * compile "endfor"
5877 */
5878 static char_u *
5879compile_endfor(char_u *arg, cctx_T *cctx)
5880{
5881 garray_T *instr = &cctx->ctx_instr;
5882 scope_T *scope = cctx->ctx_scope;
5883 forscope_T *forscope;
5884 isn_T *isn;
5885
5886 if (scope == NULL || scope->se_type != FOR_SCOPE)
5887 {
5888 emsg(_(e_for));
5889 return NULL;
5890 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005891 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005893 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005894
5895 // At end of ":for" scope jump back to the FOR instruction.
5896 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5897
5898 // Fill in the "end" label in the FOR statement so it can jump here
5899 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5900 isn->isn_arg.forloop.for_end = instr->ga_len;
5901
5902 // Fill in the "end" label any BREAK statements
5903 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5904
5905 // Below the ":for" scope drop the "expr" list from the stack.
5906 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5907 return NULL;
5908
5909 vim_free(scope);
5910
5911 return arg;
5912}
5913
5914/*
5915 * compile "while expr"
5916 *
5917 * Produces instructions:
5918 * top: EVAL expr Push result of "expr"
5919 * JUMP_IF_FALSE end jump if false
5920 * ... body ...
5921 * JUMP top Jump back to repeat
5922 * end:
5923 *
5924 */
5925 static char_u *
5926compile_while(char_u *arg, cctx_T *cctx)
5927{
5928 char_u *p = arg;
5929 garray_T *instr = &cctx->ctx_instr;
5930 scope_T *scope;
5931
5932 scope = new_scope(cctx, WHILE_SCOPE);
5933 if (scope == NULL)
5934 return NULL;
5935
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005936 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005937
5938 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005939 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940 return NULL;
5941
5942 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005943 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005944 JUMP_IF_FALSE, cctx) == FAIL)
5945 return FAIL;
5946
5947 return p;
5948}
5949
5950/*
5951 * compile "endwhile"
5952 */
5953 static char_u *
5954compile_endwhile(char_u *arg, cctx_T *cctx)
5955{
5956 scope_T *scope = cctx->ctx_scope;
5957
5958 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5959 {
5960 emsg(_(e_while));
5961 return NULL;
5962 }
5963 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005964 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005965
5966 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005967 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005968
5969 // Fill in the "end" label in the WHILE statement so it can jump here.
5970 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005971 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005972
5973 vim_free(scope);
5974
5975 return arg;
5976}
5977
5978/*
5979 * compile "continue"
5980 */
5981 static char_u *
5982compile_continue(char_u *arg, cctx_T *cctx)
5983{
5984 scope_T *scope = cctx->ctx_scope;
5985
5986 for (;;)
5987 {
5988 if (scope == NULL)
5989 {
5990 emsg(_(e_continue));
5991 return NULL;
5992 }
5993 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5994 break;
5995 scope = scope->se_outer;
5996 }
5997
5998 // Jump back to the FOR or WHILE instruction.
5999 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006000 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6001 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002 return arg;
6003}
6004
6005/*
6006 * compile "break"
6007 */
6008 static char_u *
6009compile_break(char_u *arg, cctx_T *cctx)
6010{
6011 scope_T *scope = cctx->ctx_scope;
6012 endlabel_T **el;
6013
6014 for (;;)
6015 {
6016 if (scope == NULL)
6017 {
6018 emsg(_(e_break));
6019 return NULL;
6020 }
6021 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6022 break;
6023 scope = scope->se_outer;
6024 }
6025
6026 // Jump to the end of the FOR or WHILE loop.
6027 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006028 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006030 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6032 return FAIL;
6033
6034 return arg;
6035}
6036
6037/*
6038 * compile "{" start of block
6039 */
6040 static char_u *
6041compile_block(char_u *arg, cctx_T *cctx)
6042{
6043 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6044 return NULL;
6045 return skipwhite(arg + 1);
6046}
6047
6048/*
6049 * compile end of block: drop one scope
6050 */
6051 static void
6052compile_endblock(cctx_T *cctx)
6053{
6054 scope_T *scope = cctx->ctx_scope;
6055
6056 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006057 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058 vim_free(scope);
6059}
6060
6061/*
6062 * compile "try"
6063 * Creates a new scope for the try-endtry, pointing to the first catch and
6064 * finally.
6065 * Creates another scope for the "try" block itself.
6066 * TRY instruction sets up exception handling at runtime.
6067 *
6068 * "try"
6069 * TRY -> catch1, -> finally push trystack entry
6070 * ... try block
6071 * "throw {exception}"
6072 * EVAL {exception}
6073 * THROW create exception
6074 * ... try block
6075 * " catch {expr}"
6076 * JUMP -> finally
6077 * catch1: PUSH exeception
6078 * EVAL {expr}
6079 * MATCH
6080 * JUMP nomatch -> catch2
6081 * CATCH remove exception
6082 * ... catch block
6083 * " catch"
6084 * JUMP -> finally
6085 * catch2: CATCH remove exception
6086 * ... catch block
6087 * " finally"
6088 * finally:
6089 * ... finally block
6090 * " endtry"
6091 * ENDTRY pop trystack entry, may rethrow
6092 */
6093 static char_u *
6094compile_try(char_u *arg, cctx_T *cctx)
6095{
6096 garray_T *instr = &cctx->ctx_instr;
6097 scope_T *try_scope;
6098 scope_T *scope;
6099
6100 // scope that holds the jumps that go to catch/finally/endtry
6101 try_scope = new_scope(cctx, TRY_SCOPE);
6102 if (try_scope == NULL)
6103 return NULL;
6104
6105 // "catch" is set when the first ":catch" is found.
6106 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006107 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108 if (generate_instr(cctx, ISN_TRY) == NULL)
6109 return NULL;
6110
6111 // scope for the try block itself
6112 scope = new_scope(cctx, BLOCK_SCOPE);
6113 if (scope == NULL)
6114 return NULL;
6115
6116 return arg;
6117}
6118
6119/*
6120 * compile "catch {expr}"
6121 */
6122 static char_u *
6123compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6124{
6125 scope_T *scope = cctx->ctx_scope;
6126 garray_T *instr = &cctx->ctx_instr;
6127 char_u *p;
6128 isn_T *isn;
6129
6130 // end block scope from :try or :catch
6131 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6132 compile_endblock(cctx);
6133 scope = cctx->ctx_scope;
6134
6135 // Error if not in a :try scope
6136 if (scope == NULL || scope->se_type != TRY_SCOPE)
6137 {
6138 emsg(_(e_catch));
6139 return NULL;
6140 }
6141
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006142 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143 {
6144 emsg(_("E1033: catch unreachable after catch-all"));
6145 return NULL;
6146 }
6147
6148 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006149 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006150 JUMP_ALWAYS, cctx) == FAIL)
6151 return NULL;
6152
6153 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006154 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006155 if (isn->isn_arg.try.try_catch == 0)
6156 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006157 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006158 {
6159 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006160 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006161 isn->isn_arg.jump.jump_where = instr->ga_len;
6162 }
6163
6164 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006165 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006166 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006167 scope->se_u.se_try.ts_caught_all = TRUE;
6168 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006169 }
6170 else
6171 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006172 char_u *end;
6173 char_u *pat;
6174 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006175 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006176 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006178 // Push v:exception, push {expr} and MATCH
6179 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6180
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006181 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006182 if (*end != *p)
6183 {
6184 semsg(_("E1067: Separator mismatch: %s"), p);
6185 vim_free(tofree);
6186 return FAIL;
6187 }
6188 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006189 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006190 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006191 len = (int)(end - tofree);
6192 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006193 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006194 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006195 if (pat == NULL)
6196 return FAIL;
6197 if (generate_PUSHS(cctx, pat) == FAIL)
6198 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6201 return NULL;
6202
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006203 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6205 return NULL;
6206 }
6207
6208 if (generate_instr(cctx, ISN_CATCH) == NULL)
6209 return NULL;
6210
6211 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6212 return NULL;
6213 return p;
6214}
6215
6216 static char_u *
6217compile_finally(char_u *arg, cctx_T *cctx)
6218{
6219 scope_T *scope = cctx->ctx_scope;
6220 garray_T *instr = &cctx->ctx_instr;
6221 isn_T *isn;
6222
6223 // end block scope from :try or :catch
6224 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6225 compile_endblock(cctx);
6226 scope = cctx->ctx_scope;
6227
6228 // Error if not in a :try scope
6229 if (scope == NULL || scope->se_type != TRY_SCOPE)
6230 {
6231 emsg(_(e_finally));
6232 return NULL;
6233 }
6234
6235 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006236 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237 if (isn->isn_arg.try.try_finally != 0)
6238 {
6239 emsg(_(e_finally_dup));
6240 return NULL;
6241 }
6242
6243 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006244 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006245
Bram Moolenaar585fea72020-04-02 22:33:21 +02006246 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006247 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006248 {
6249 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006250 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 isn->isn_arg.jump.jump_where = instr->ga_len;
6252 }
6253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254 // TODO: set index in ts_finally_label jumps
6255
6256 return arg;
6257}
6258
6259 static char_u *
6260compile_endtry(char_u *arg, cctx_T *cctx)
6261{
6262 scope_T *scope = cctx->ctx_scope;
6263 garray_T *instr = &cctx->ctx_instr;
6264 isn_T *isn;
6265
6266 // end block scope from :catch or :finally
6267 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6268 compile_endblock(cctx);
6269 scope = cctx->ctx_scope;
6270
6271 // Error if not in a :try scope
6272 if (scope == NULL || scope->se_type != TRY_SCOPE)
6273 {
6274 if (scope == NULL)
6275 emsg(_(e_no_endtry));
6276 else if (scope->se_type == WHILE_SCOPE)
6277 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006278 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279 emsg(_(e_endfor));
6280 else
6281 emsg(_(e_endif));
6282 return NULL;
6283 }
6284
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006285 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6287 {
6288 emsg(_("E1032: missing :catch or :finally"));
6289 return NULL;
6290 }
6291
6292 // Fill in the "end" label in jumps at the end of the blocks, if not done
6293 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006294 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006295
6296 // End :catch or :finally scope: set value in ISN_TRY instruction
6297 if (isn->isn_arg.try.try_finally == 0)
6298 isn->isn_arg.try.try_finally = instr->ga_len;
6299 compile_endblock(cctx);
6300
6301 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6302 return NULL;
6303 return arg;
6304}
6305
6306/*
6307 * compile "throw {expr}"
6308 */
6309 static char_u *
6310compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6311{
6312 char_u *p = skipwhite(arg);
6313
Bram Moolenaara5565e42020-05-09 15:44:01 +02006314 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 return NULL;
6316 if (may_generate_2STRING(-1, cctx) == FAIL)
6317 return NULL;
6318 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6319 return NULL;
6320
6321 return p;
6322}
6323
6324/*
6325 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006326 * compile "echomsg expr"
6327 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006328 * compile "execute expr"
6329 */
6330 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006331compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006332{
6333 char_u *p = arg;
6334 int count = 0;
6335
6336 for (;;)
6337 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006338 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006339 return NULL;
6340 ++count;
6341 p = skipwhite(p);
6342 if (ends_excmd(*p))
6343 break;
6344 }
6345
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006346 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6347 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6348 else if (cmdidx == CMD_execute)
6349 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6350 else if (cmdidx == CMD_echomsg)
6351 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6352 else
6353 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354 return p;
6355}
6356
6357/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006358 * A command that is not compiled, execute with legacy code.
6359 */
6360 static char_u *
6361compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6362{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006363 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006364 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006365
6366 if (cctx->ctx_skip == TRUE)
6367 goto theend;
6368
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006369 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6370 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006371 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6372 {
6373 // expand filename in "syntax include [@group] filename"
6374 has_expr = TRUE;
6375 eap->arg = skipwhite(eap->arg + 7);
6376 if (*eap->arg == '@')
6377 eap->arg = skiptowhite(eap->arg);
6378 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006379
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006380 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006381 {
6382 int count = 0;
6383 char_u *start = skipwhite(line);
6384
6385 // :cmd xxx`=expr1`yyy`=expr2`zzz
6386 // PUSHS ":cmd xxx"
6387 // eval expr1
6388 // PUSHS "yyy"
6389 // eval expr2
6390 // PUSHS "zzz"
6391 // EXECCONCAT 5
6392 for (;;)
6393 {
6394 if (p > start)
6395 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006396 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006397 ++count;
6398 }
6399 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006400 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006401 return NULL;
6402 may_generate_2STRING(-1, cctx);
6403 ++count;
6404 p = skipwhite(p);
6405 if (*p != '`')
6406 {
6407 emsg(_("E1083: missing backtick"));
6408 return NULL;
6409 }
6410 start = p + 1;
6411
6412 p = (char_u *)strstr((char *)start, "`=");
6413 if (p == NULL)
6414 {
6415 if (*skipwhite(start) != NUL)
6416 {
6417 generate_PUSHS(cctx, vim_strsave(start));
6418 ++count;
6419 }
6420 break;
6421 }
6422 }
6423 generate_EXECCONCAT(cctx, count);
6424 }
6425 else
6426 generate_EXEC(cctx, line);
6427
6428theend:
6429 return (char_u *)"";
6430}
6431
6432/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006433 * Add a function to the list of :def functions.
6434 * This "sets ufunc->uf_dfunc_idx" but the function isn't compiled yet.
6435 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006436 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006437add_def_function(ufunc_T *ufunc)
6438{
6439 dfunc_T *dfunc;
6440
6441 // Add the function to "def_functions".
6442 if (ga_grow(&def_functions, 1) == FAIL)
6443 return FAIL;
6444 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6445 CLEAR_POINTER(dfunc);
6446 dfunc->df_idx = def_functions.ga_len;
6447 ufunc->uf_dfunc_idx = dfunc->df_idx;
6448 dfunc->df_ufunc = ufunc;
6449 ++def_functions.ga_len;
6450 return OK;
6451}
6452
6453/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454 * After ex_function() has collected all the function lines: parse and compile
6455 * the lines into instructions.
6456 * Adds the function to "def_functions".
6457 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6458 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006459 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006460 * This can be used recursively through compile_lambda(), which may reallocate
6461 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006462 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006463 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006464 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006465compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006466{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006467 char_u *line = NULL;
6468 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006469 char *errormsg = NULL; // error message
6470 int had_return = FALSE;
6471 cctx_T cctx;
6472 garray_T *instr;
6473 int called_emsg_before = called_emsg;
6474 int ret = FAIL;
6475 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006476 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006477 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006478
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006479 // When using a function that was compiled before: Free old instructions.
6480 // Otherwise add a new entry in "def_functions".
Bram Moolenaar09689a02020-05-09 22:50:08 +02006481 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006483 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6484 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006485 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006487 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006488 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006489
Bram Moolenaara80faa82020-04-12 19:37:17 +02006490 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006491 cctx.ctx_ufunc = ufunc;
6492 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006493 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6495 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6496 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6497 cctx.ctx_type_list = &ufunc->uf_type_list;
6498 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6499 instr = &cctx.ctx_instr;
6500
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006501 // Set the context to the function, it may be compiled when called from
6502 // another script. Set the script version to the most modern one.
6503 // The line number will be set in next_line_from_context().
6504 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006505 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6506
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006507 // Make sure error messages are OK.
6508 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6509 if (do_estack_push)
6510 estack_push_ufunc(ufunc, 1);
6511
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006512 if (ufunc->uf_def_args.ga_len > 0)
6513 {
6514 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006515 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006516 int i;
6517 char_u *arg;
6518 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6519
6520 // Produce instructions for the default values of optional arguments.
6521 // Store the instruction index in uf_def_arg_idx[] so that we know
6522 // where to start when the function is called, depending on the number
6523 // of arguments.
6524 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6525 if (ufunc->uf_def_arg_idx == NULL)
6526 goto erret;
6527 for (i = 0; i < count; ++i)
6528 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006529 garray_T *stack = &cctx.ctx_type_stack;
6530 type_T *val_type;
6531 int arg_idx = first_def_arg + i;
6532
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006533 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6534 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006535 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006536 goto erret;
6537
6538 // If no type specified use the type of the default value.
6539 // Otherwise check that the default value type matches the
6540 // specified type.
6541 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6542 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6543 ufunc->uf_arg_types[arg_idx] = val_type;
6544 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6545 == FAIL)
6546 {
6547 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6548 arg_idx + 1);
6549 goto erret;
6550 }
6551
6552 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006553 goto erret;
6554 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006555 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6556 }
6557
6558 /*
6559 * Loop over all the lines of the function and generate instructions.
6560 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006561 for (;;)
6562 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006563 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006564 int starts_with_colon = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006565
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006566 // Bail out on the first error to avoid a flood of errors and report
6567 // the right line number when inside try/catch.
6568 if (emsg_before != called_emsg)
6569 goto erret;
6570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571 if (line != NULL && *line == '|')
6572 // the line continues after a '|'
6573 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006574 else if (line != NULL && *line != NUL
6575 && !(*line == '#' && (line == cctx.ctx_line_start
6576 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006577 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006578 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006579 goto erret;
6580 }
6581 else
6582 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006583 line = next_line_from_context(&cctx);
6584 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006585 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006588 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589
6590 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006591 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592 ea.cmdlinep = &line;
6593 ea.cmd = skipwhite(line);
6594
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006595 // Some things can be recognized by the first character.
6596 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006598 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006599 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006600 if (ea.cmd[1] != '{')
6601 {
6602 line = (char_u *)"";
6603 continue;
6604 }
6605 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006607 case '}':
6608 {
6609 // "}" ends a block scope
6610 scopetype_T stype = cctx.ctx_scope == NULL
6611 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006612
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006613 if (stype == BLOCK_SCOPE)
6614 {
6615 compile_endblock(&cctx);
6616 line = ea.cmd;
6617 }
6618 else
6619 {
6620 emsg(_("E1025: using } outside of a block scope"));
6621 goto erret;
6622 }
6623 if (line != NULL)
6624 line = skipwhite(ea.cmd + 1);
6625 continue;
6626 }
6627
6628 case '{':
6629 // "{" starts a block scope
6630 // "{'a': 1}->func() is something else
6631 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6632 {
6633 line = compile_block(ea.cmd, &cctx);
6634 continue;
6635 }
6636 break;
6637
6638 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006639 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006640 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 }
6642
6643 /*
6644 * COMMAND MODIFIERS
6645 */
6646 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6647 {
6648 if (errormsg != NULL)
6649 goto erret;
6650 // empty line or comment
6651 line = (char_u *)"";
6652 continue;
6653 }
6654
6655 // Skip ":call" to get to the function name.
6656 if (checkforcmd(&ea.cmd, "call", 3))
6657 ea.cmd = skipwhite(ea.cmd);
6658
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006659 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006661 char_u *pskip;
6662
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006663 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006664 // find what follows.
6665 // Skip over "var.member", "var[idx]" and the like.
6666 // Also "&opt = val", "$ENV = val" and "@r = val".
6667 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006668 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006669 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006670 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006671 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006672 char_u *var_end;
6673 int oplen;
6674 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006676 var_end = find_name_end(pskip, NULL, NULL,
6677 FNE_CHECK_START | FNE_INCL_BR);
6678 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006679 if (oplen > 0)
6680 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006681 size_t len = p - ea.cmd;
6682
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006683 // Recognize an assignment if we recognize the variable
6684 // name:
6685 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006686 // "local = expr" where "local" is a local var.
6687 // "script = expr" where "script" is a script-local var.
6688 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006689 // "&opt = expr"
6690 // "$ENV = expr"
6691 // "@r = expr"
6692 if (*ea.cmd == '&'
6693 || *ea.cmd == '$'
6694 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006695 || ((len) > 2 && ea.cmd[1] == ':')
6696 || lookup_local(ea.cmd, len, &cctx) != NULL
6697 || lookup_arg(ea.cmd, len, NULL, NULL,
6698 NULL, &cctx) == OK
6699 || lookup_script(ea.cmd, len) == OK
6700 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006701 {
6702 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006703 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006704 goto erret;
6705 continue;
6706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006707 }
6708 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006709
6710 if (*ea.cmd == '[')
6711 {
6712 // [var, var] = expr
6713 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6714 if (line == NULL)
6715 goto erret;
6716 if (line != ea.cmd)
6717 continue;
6718 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 }
6720
6721 /*
6722 * COMMAND after range
6723 */
6724 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006725 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006726 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006727 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728
6729 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6730 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006731 if (cctx.ctx_skip == TRUE)
6732 {
6733 line += STRLEN(line);
6734 continue;
6735 }
6736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737 // Expression or function call.
6738 if (ea.cmdidx == CMD_eval)
6739 {
6740 p = ea.cmd;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006741 if (compile_expr0(&p, &cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006742 goto erret;
6743
6744 // drop the return value
6745 generate_instr_drop(&cctx, ISN_DROP, 1);
6746 line = p;
6747 continue;
6748 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006749 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006750 iemsg("Command from find_ex_command() not handled");
6751 goto erret;
6752 }
6753
6754 p = skipwhite(p);
6755
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006756 if (cctx.ctx_skip == TRUE
6757 && ea.cmdidx != CMD_elseif
6758 && ea.cmdidx != CMD_else
6759 && ea.cmdidx != CMD_endif)
6760 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006761 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006762 continue;
6763 }
6764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006765 switch (ea.cmdidx)
6766 {
6767 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006768 ea.arg = p;
6769 line = compile_nested_function(&ea, &cctx);
6770 break;
6771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006773 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006774 goto erret;
6775
6776 case CMD_return:
6777 line = compile_return(p, set_return_type, &cctx);
6778 had_return = TRUE;
6779 break;
6780
6781 case CMD_let:
6782 case CMD_const:
6783 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006784 if (line == p)
6785 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 break;
6787
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006788 case CMD_unlet:
6789 case CMD_unlockvar:
6790 case CMD_lockvar:
6791 line = compile_unletlock(p, &ea, &cctx);
6792 break;
6793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006794 case CMD_import:
6795 line = compile_import(p, &cctx);
6796 break;
6797
6798 case CMD_if:
6799 line = compile_if(p, &cctx);
6800 break;
6801 case CMD_elseif:
6802 line = compile_elseif(p, &cctx);
6803 break;
6804 case CMD_else:
6805 line = compile_else(p, &cctx);
6806 break;
6807 case CMD_endif:
6808 line = compile_endif(p, &cctx);
6809 break;
6810
6811 case CMD_while:
6812 line = compile_while(p, &cctx);
6813 break;
6814 case CMD_endwhile:
6815 line = compile_endwhile(p, &cctx);
6816 break;
6817
6818 case CMD_for:
6819 line = compile_for(p, &cctx);
6820 break;
6821 case CMD_endfor:
6822 line = compile_endfor(p, &cctx);
6823 break;
6824 case CMD_continue:
6825 line = compile_continue(p, &cctx);
6826 break;
6827 case CMD_break:
6828 line = compile_break(p, &cctx);
6829 break;
6830
6831 case CMD_try:
6832 line = compile_try(p, &cctx);
6833 break;
6834 case CMD_catch:
6835 line = compile_catch(p, &cctx);
6836 break;
6837 case CMD_finally:
6838 line = compile_finally(p, &cctx);
6839 break;
6840 case CMD_endtry:
6841 line = compile_endtry(p, &cctx);
6842 break;
6843 case CMD_throw:
6844 line = compile_throw(p, &cctx);
6845 break;
6846
6847 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006848 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006849 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006850 case CMD_echomsg:
6851 case CMD_echoerr:
6852 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006853 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006854
6855 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006856 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006857 // Not recognized, execute with do_cmdline_cmd().
6858 ea.arg = p;
6859 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860 break;
6861 }
6862 if (line == NULL)
6863 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006864 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006865
6866 if (cctx.ctx_type_stack.ga_len < 0)
6867 {
6868 iemsg("Type stack underflow");
6869 goto erret;
6870 }
6871 }
6872
6873 if (cctx.ctx_scope != NULL)
6874 {
6875 if (cctx.ctx_scope->se_type == IF_SCOPE)
6876 emsg(_(e_endif));
6877 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6878 emsg(_(e_endwhile));
6879 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6880 emsg(_(e_endfor));
6881 else
6882 emsg(_("E1026: Missing }"));
6883 goto erret;
6884 }
6885
6886 if (!had_return)
6887 {
6888 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6889 {
6890 emsg(_("E1027: Missing return statement"));
6891 goto erret;
6892 }
6893
6894 // Return zero if there is no return at the end.
6895 generate_PUSHNR(&cctx, 0);
6896 generate_instr(&cctx, ISN_RETURN);
6897 }
6898
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006899 {
6900 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6901 + ufunc->uf_dfunc_idx;
6902 dfunc->df_deleted = FALSE;
6903 dfunc->df_instr = instr->ga_data;
6904 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006905 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006906 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006907 if (cctx.ctx_outer_used)
6908 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006909 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006910
6911 ret = OK;
6912
6913erret:
6914 if (ret == FAIL)
6915 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006916 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006917 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6918 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006919
6920 for (idx = 0; idx < instr->ga_len; ++idx)
6921 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006923
Bram Moolenaar45a15082020-05-25 00:28:33 +02006924 // if using the last entry in the table we might as well remove it
6925 if (!dfunc->df_deleted
6926 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006927 --def_functions.ga_len;
Bram Moolenaar45a15082020-05-25 00:28:33 +02006928 ufunc->uf_dfunc_idx = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006929
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006930 while (cctx.ctx_scope != NULL)
6931 drop_scope(&cctx);
6932
Bram Moolenaar20431c92020-03-20 18:39:46 +01006933 // Don't execute this function body.
6934 ga_clear_strings(&ufunc->uf_lines);
6935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 if (errormsg != NULL)
6937 emsg(errormsg);
6938 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006939 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940 }
6941
6942 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006943 if (do_estack_push)
6944 estack_pop();
6945
Bram Moolenaar20431c92020-03-20 18:39:46 +01006946 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006947 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006949 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950}
6951
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006952 void
6953set_function_type(ufunc_T *ufunc)
6954{
6955 int varargs = ufunc->uf_va_name != NULL;
6956 int argcount = ufunc->uf_args.ga_len;
6957
6958 // Create a type for the function, with the return type and any
6959 // argument types.
6960 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6961 // The type is included in "tt_args".
6962 if (argcount > 0 || varargs)
6963 {
6964 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6965 argcount, &ufunc->uf_type_list);
6966 // Add argument types to the function type.
6967 if (func_type_add_arg_types(ufunc->uf_func_type,
6968 argcount + varargs,
6969 &ufunc->uf_type_list) == FAIL)
6970 return;
6971 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6972 ufunc->uf_func_type->tt_min_argcount =
6973 argcount - ufunc->uf_def_args.ga_len;
6974 if (ufunc->uf_arg_types == NULL)
6975 {
6976 int i;
6977
6978 // lambda does not have argument types.
6979 for (i = 0; i < argcount; ++i)
6980 ufunc->uf_func_type->tt_args[i] = &t_any;
6981 }
6982 else
6983 mch_memmove(ufunc->uf_func_type->tt_args,
6984 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6985 if (varargs)
6986 {
6987 ufunc->uf_func_type->tt_args[argcount] =
6988 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
6989 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6990 }
6991 }
6992 else
6993 // No arguments, can use a predefined type.
6994 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6995 argcount, &ufunc->uf_type_list);
6996}
6997
6998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999/*
7000 * Delete an instruction, free what it contains.
7001 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007002 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003delete_instr(isn_T *isn)
7004{
7005 switch (isn->isn_type)
7006 {
7007 case ISN_EXEC:
7008 case ISN_LOADENV:
7009 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007010 case ISN_LOADB:
7011 case ISN_LOADW:
7012 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007013 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007014 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 case ISN_PUSHEXC:
7016 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007017 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007019 case ISN_STOREB:
7020 case ISN_STOREW:
7021 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007022 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 vim_free(isn->isn_arg.string);
7024 break;
7025
7026 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007027 case ISN_STORES:
7028 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 break;
7030
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007031 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007032 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007033 vim_free(isn->isn_arg.unlet.ul_name);
7034 break;
7035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007036 case ISN_STOREOPT:
7037 vim_free(isn->isn_arg.storeopt.so_name);
7038 break;
7039
7040 case ISN_PUSHBLOB: // push blob isn_arg.blob
7041 blob_unref(isn->isn_arg.blob);
7042 break;
7043
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007044 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007045#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007046 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007047#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007048 break;
7049
7050 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007051#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007052 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007053#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007054 break;
7055
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 case ISN_UCALL:
7057 vim_free(isn->isn_arg.ufunc.cuf_name);
7058 break;
7059
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007060 case ISN_FUNCREF:
7061 {
7062 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7063 + isn->isn_arg.funcref.fr_func;
7064 func_ptr_unref(dfunc->df_ufunc);
7065 }
7066 break;
7067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 case ISN_2BOOL:
7069 case ISN_2STRING:
7070 case ISN_ADDBLOB:
7071 case ISN_ADDLIST:
7072 case ISN_BCALL:
7073 case ISN_CATCH:
7074 case ISN_CHECKNR:
7075 case ISN_CHECKTYPE:
7076 case ISN_COMPAREANY:
7077 case ISN_COMPAREBLOB:
7078 case ISN_COMPAREBOOL:
7079 case ISN_COMPAREDICT:
7080 case ISN_COMPAREFLOAT:
7081 case ISN_COMPAREFUNC:
7082 case ISN_COMPARELIST:
7083 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 case ISN_COMPARESPECIAL:
7085 case ISN_COMPARESTRING:
7086 case ISN_CONCAT:
7087 case ISN_DCALL:
7088 case ISN_DROP:
7089 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007090 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007091 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007093 case ISN_EXECCONCAT:
7094 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007095 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007096 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007097 case ISN_GETITEM:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007098 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099 case ISN_JUMP:
7100 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007101 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102 case ISN_LOADSCRIPT:
7103 case ISN_LOADREG:
7104 case ISN_LOADV:
7105 case ISN_NEGATENR:
7106 case ISN_NEWDICT:
7107 case ISN_NEWLIST:
7108 case ISN_OPNR:
7109 case ISN_OPFLOAT:
7110 case ISN_OPANY:
7111 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007112 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 case ISN_PUSHF:
7114 case ISN_PUSHNR:
7115 case ISN_PUSHBOOL:
7116 case ISN_PUSHSPEC:
7117 case ISN_RETURN:
7118 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007119 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007120 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007122 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007124 case ISN_STOREDICT:
7125 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126 case ISN_THROW:
7127 case ISN_TRY:
7128 // nothing allocated
7129 break;
7130 }
7131}
7132
7133/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007134 * Free all instructions for "dfunc".
7135 */
7136 static void
7137delete_def_function_contents(dfunc_T *dfunc)
7138{
7139 int idx;
7140
7141 ga_clear(&dfunc->df_def_args_isn);
7142
7143 if (dfunc->df_instr != NULL)
7144 {
7145 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7146 delete_instr(dfunc->df_instr + idx);
7147 VIM_CLEAR(dfunc->df_instr);
7148 }
7149
7150 dfunc->df_deleted = TRUE;
7151}
7152
7153/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 * When a user function is deleted, delete any associated def function.
7155 */
7156 void
7157delete_def_function(ufunc_T *ufunc)
7158{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159 if (ufunc->uf_dfunc_idx >= 0)
7160 {
7161 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7162 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163
Bram Moolenaar20431c92020-03-20 18:39:46 +01007164 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007165 }
7166}
7167
7168#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007169/*
7170 * Free all functions defined with ":def".
7171 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007172 void
7173free_def_functions(void)
7174{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007175 int idx;
7176
7177 for (idx = 0; idx < def_functions.ga_len; ++idx)
7178 {
7179 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7180
7181 delete_def_function_contents(dfunc);
7182 }
7183
7184 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185}
7186#endif
7187
7188
7189#endif // FEAT_EVAL