blob: 944efa197dd2424050aa640ba0a1f16e2f55b20b [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
26/*
27 * Chain of jump instructions where the end label needs to be set.
28 */
29typedef struct endlabel_S endlabel_T;
30struct endlabel_S {
31 endlabel_T *el_next; // chain end_label locations
32 int el_end_label; // instruction idx where to set end
33};
34
35/*
36 * info specific for the scope of :if / elseif / else
37 */
38typedef struct {
39 int is_if_label; // instruction idx at IF or ELSEIF
40 endlabel_T *is_end_label; // instructions to set end label
41} ifscope_T;
42
43/*
44 * info specific for the scope of :while
45 */
46typedef struct {
47 int ws_top_label; // instruction idx at WHILE
48 endlabel_T *ws_end_label; // instructions to set end
49} whilescope_T;
50
51/*
52 * info specific for the scope of :for
53 */
54typedef struct {
55 int fs_top_label; // instruction idx at FOR
56 endlabel_T *fs_end_label; // break instructions
57} forscope_T;
58
59/*
60 * info specific for the scope of :try
61 */
62typedef struct {
63 int ts_try_label; // instruction idx at TRY
64 endlabel_T *ts_end_label; // jump to :finally or :endtry
65 int ts_catch_label; // instruction idx of last CATCH
66 int ts_caught_all; // "catch" without argument encountered
67} tryscope_T;
68
69typedef enum {
70 NO_SCOPE,
71 IF_SCOPE,
72 WHILE_SCOPE,
73 FOR_SCOPE,
74 TRY_SCOPE,
75 BLOCK_SCOPE
76} scopetype_T;
77
78/*
79 * Info for one scope, pointed to by "ctx_scope".
80 */
81typedef struct scope_S scope_T;
82struct scope_S {
83 scope_T *se_outer; // scope containing this one
84 scopetype_T se_type;
85 int se_local_count; // ctx_locals.ga_len before scope
86 union {
87 ifscope_T se_if;
88 whilescope_T se_while;
89 forscope_T se_for;
90 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +010091 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092};
93
94/*
95 * Entry for "ctx_locals". Used for arguments and local variables.
96 */
97typedef struct {
98 char_u *lv_name;
99 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200100 int lv_idx; // index of the variable on the stack
101 int lv_from_outer; // when TRUE using ctx_outer scope
102 int lv_const; // when TRUE cannot be assigned to
103 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104} lvar_T;
105
106/*
107 * Context for compiling lines of Vim script.
108 * Stores info about the local variables and condition stack.
109 */
110struct cctx_S {
111 ufunc_T *ctx_ufunc; // current function
112 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200113 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114 garray_T ctx_instr; // generated instructions
115
116 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200117 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100118
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200119 int ctx_closure_count; // number of closures created in the
120 // function
121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100122 garray_T ctx_imports; // imported items
123
Bram Moolenaara259d8d2020-01-31 20:10:50 +0100124 int ctx_skip; // when TRUE skip commands, when FALSE skip
125 // commands after "else"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126 scope_T *ctx_scope; // current scope, NULL at toplevel
127
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200128 cctx_T *ctx_outer; // outer scope for lambda or nested
129 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200130 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200133 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100134};
135
136static char e_var_notfound[] = N_("E1001: variable not found: %s");
137static char e_syntax_at[] = N_("E1002: Syntax error at %s");
138
Bram Moolenaara5565e42020-05-09 15:44:01 +0200139static int compile_expr0(char_u **arg, cctx_T *cctx);
Bram Moolenaar20431c92020-03-20 18:39:46 +0100140static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200141static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
142static int check_type(type_T *expected, type_T *actual, int give_msg);
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 */
464 static type_T *
465typval2type(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 */
507 static int
508check_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 Moolenaar8a7d6542020-01-26 15:56:19 +01001057 * Generate an ISN_STORE instruction.
1058 */
1059 static int
1060generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1061{
1062 isn_T *isn;
1063
Bram Moolenaar080457c2020-03-03 21:53:32 +01001064 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1066 return FAIL;
1067 if (name != NULL)
1068 isn->isn_arg.string = vim_strsave(name);
1069 else
1070 isn->isn_arg.number = idx;
1071
1072 return OK;
1073}
1074
1075/*
1076 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1077 */
1078 static int
1079generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1080{
1081 isn_T *isn;
1082
Bram Moolenaar080457c2020-03-03 21:53:32 +01001083 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1085 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001086 isn->isn_arg.storenr.stnr_idx = idx;
1087 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088
1089 return OK;
1090}
1091
1092/*
1093 * Generate an ISN_STOREOPT instruction
1094 */
1095 static int
1096generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1097{
1098 isn_T *isn;
1099
Bram Moolenaar080457c2020-03-03 21:53:32 +01001100 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1102 return FAIL;
1103 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1104 isn->isn_arg.storeopt.so_flags = opt_flags;
1105
1106 return OK;
1107}
1108
1109/*
1110 * Generate an ISN_LOAD or similar instruction.
1111 */
1112 static int
1113generate_LOAD(
1114 cctx_T *cctx,
1115 isntype_T isn_type,
1116 int idx,
1117 char_u *name,
1118 type_T *type)
1119{
1120 isn_T *isn;
1121
Bram Moolenaar080457c2020-03-03 21:53:32 +01001122 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1124 return FAIL;
1125 if (name != NULL)
1126 isn->isn_arg.string = vim_strsave(name);
1127 else
1128 isn->isn_arg.number = idx;
1129
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001134 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001135 */
1136 static int
1137generate_LOADV(
1138 cctx_T *cctx,
1139 char_u *name,
1140 int error)
1141{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001142 int di_flags;
1143 int vidx = find_vim_var(name, &di_flags);
1144 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001145
Bram Moolenaar080457c2020-03-03 21:53:32 +01001146 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001147 if (vidx < 0)
1148 {
1149 if (error)
1150 semsg(_(e_var_notfound), name);
1151 return FAIL;
1152 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001153 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001154
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001155 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001156}
1157
1158/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001159 * Generate an ISN_UNLET instruction.
1160 */
1161 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001162generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001163{
1164 isn_T *isn;
1165
1166 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001167 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001168 return FAIL;
1169 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1170 isn->isn_arg.unlet.ul_forceit = forceit;
1171
1172 return OK;
1173}
1174
1175/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 * Generate an ISN_LOADS instruction.
1177 */
1178 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001179generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001181 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001183 int sid,
1184 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185{
1186 isn_T *isn;
1187
Bram Moolenaar080457c2020-03-03 21:53:32 +01001188 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001189 if (isn_type == ISN_LOADS)
1190 isn = generate_instr_type(cctx, isn_type, type);
1191 else
1192 isn = generate_instr_drop(cctx, isn_type, 1);
1193 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001195 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1196 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197
1198 return OK;
1199}
1200
1201/*
1202 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1203 */
1204 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001205generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 cctx_T *cctx,
1207 isntype_T isn_type,
1208 int sid,
1209 int idx,
1210 type_T *type)
1211{
1212 isn_T *isn;
1213
Bram Moolenaar080457c2020-03-03 21:53:32 +01001214 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 if (isn_type == ISN_LOADSCRIPT)
1216 isn = generate_instr_type(cctx, isn_type, type);
1217 else
1218 isn = generate_instr_drop(cctx, isn_type, 1);
1219 if (isn == NULL)
1220 return FAIL;
1221 isn->isn_arg.script.script_sid = sid;
1222 isn->isn_arg.script.script_idx = idx;
1223 return OK;
1224}
1225
1226/*
1227 * Generate an ISN_NEWLIST instruction.
1228 */
1229 static int
1230generate_NEWLIST(cctx_T *cctx, int count)
1231{
1232 isn_T *isn;
1233 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 type_T *type;
1235 type_T *member;
1236
Bram Moolenaar080457c2020-03-03 21:53:32 +01001237 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1239 return FAIL;
1240 isn->isn_arg.number = count;
1241
1242 // drop the value types
1243 stack->ga_len -= count;
1244
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001245 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001246 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 if (count > 0)
1248 member = ((type_T **)stack->ga_data)[stack->ga_len];
1249 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001250 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001251 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252
1253 // add the list type to the type stack
1254 if (ga_grow(stack, 1) == FAIL)
1255 return FAIL;
1256 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1257 ++stack->ga_len;
1258
1259 return OK;
1260}
1261
1262/*
1263 * Generate an ISN_NEWDICT instruction.
1264 */
1265 static int
1266generate_NEWDICT(cctx_T *cctx, int count)
1267{
1268 isn_T *isn;
1269 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 type_T *type;
1271 type_T *member;
1272
Bram Moolenaar080457c2020-03-03 21:53:32 +01001273 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1275 return FAIL;
1276 isn->isn_arg.number = count;
1277
1278 // drop the key and value types
1279 stack->ga_len -= 2 * count;
1280
Bram Moolenaar436472f2020-02-20 22:54:43 +01001281 // Use the first value type for the list member type. Use "void" for an
1282 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 if (count > 0)
1284 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1285 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001286 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001287 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288
1289 // add the dict type to the type stack
1290 if (ga_grow(stack, 1) == FAIL)
1291 return FAIL;
1292 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1293 ++stack->ga_len;
1294
1295 return OK;
1296}
1297
1298/*
1299 * Generate an ISN_FUNCREF instruction.
1300 */
1301 static int
1302generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1303{
1304 isn_T *isn;
1305 garray_T *stack = &cctx->ctx_type_stack;
1306
Bram Moolenaar080457c2020-03-03 21:53:32 +01001307 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1309 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001310 isn->isn_arg.funcref.fr_func = dfunc_idx;
1311 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312
1313 if (ga_grow(stack, 1) == FAIL)
1314 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001315 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 // TODO: argument and return types
1317 ++stack->ga_len;
1318
1319 return OK;
1320}
1321
1322/*
1323 * Generate an ISN_JUMP instruction.
1324 */
1325 static int
1326generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1327{
1328 isn_T *isn;
1329 garray_T *stack = &cctx->ctx_type_stack;
1330
Bram Moolenaar080457c2020-03-03 21:53:32 +01001331 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1333 return FAIL;
1334 isn->isn_arg.jump.jump_when = when;
1335 isn->isn_arg.jump.jump_where = where;
1336
1337 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1338 --stack->ga_len;
1339
1340 return OK;
1341}
1342
1343 static int
1344generate_FOR(cctx_T *cctx, int loop_idx)
1345{
1346 isn_T *isn;
1347 garray_T *stack = &cctx->ctx_type_stack;
1348
Bram Moolenaar080457c2020-03-03 21:53:32 +01001349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1351 return FAIL;
1352 isn->isn_arg.forloop.for_idx = loop_idx;
1353
1354 if (ga_grow(stack, 1) == FAIL)
1355 return FAIL;
1356 // type doesn't matter, will be stored next
1357 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1358 ++stack->ga_len;
1359
1360 return OK;
1361}
1362
1363/*
1364 * Generate an ISN_BCALL instruction.
1365 * Return FAIL if the number of arguments is wrong.
1366 */
1367 static int
1368generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1369{
1370 isn_T *isn;
1371 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001372 type_T *argtypes[MAX_FUNC_ARGS];
1373 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374
Bram Moolenaar080457c2020-03-03 21:53:32 +01001375 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if (check_internal_func(func_idx, argcount) == FAIL)
1377 return FAIL;
1378
1379 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1380 return FAIL;
1381 isn->isn_arg.bfunc.cbf_idx = func_idx;
1382 isn->isn_arg.bfunc.cbf_argcount = argcount;
1383
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001384 for (i = 0; i < argcount; ++i)
1385 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 stack->ga_len -= argcount; // drop the arguments
1388 if (ga_grow(stack, 1) == FAIL)
1389 return FAIL;
1390 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001391 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 ++stack->ga_len; // add return value
1393
1394 return OK;
1395}
1396
1397/*
1398 * Generate an ISN_DCALL or ISN_UCALL instruction.
1399 * Return FAIL if the number of arguments is wrong.
1400 */
1401 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001402generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403{
1404 isn_T *isn;
1405 garray_T *stack = &cctx->ctx_type_stack;
1406 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001407 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408
Bram Moolenaar080457c2020-03-03 21:53:32 +01001409 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 if (argcount > regular_args && !has_varargs(ufunc))
1411 {
1412 semsg(_(e_toomanyarg), ufunc->uf_name);
1413 return FAIL;
1414 }
1415 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1416 {
1417 semsg(_(e_toofewarg), ufunc->uf_name);
1418 return FAIL;
1419 }
1420
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001421 if (ufunc->uf_dfunc_idx >= 0)
1422 {
1423 int i;
1424
1425 for (i = 0; i < argcount; ++i)
1426 {
1427 type_T *expected;
1428 type_T *actual;
1429
1430 if (i < regular_args)
1431 {
1432 if (ufunc->uf_arg_types == NULL)
1433 continue;
1434 expected = ufunc->uf_arg_types[i];
1435 }
1436 else
1437 expected = ufunc->uf_va_type->tt_member;
1438 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001439 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001440 {
1441 arg_type_mismatch(expected, actual, i + 1);
1442 return FAIL;
1443 }
1444 }
1445 }
1446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 if ((isn = generate_instr(cctx,
1448 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1449 return FAIL;
1450 if (ufunc->uf_dfunc_idx >= 0)
1451 {
1452 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1453 isn->isn_arg.dfunc.cdf_argcount = argcount;
1454 }
1455 else
1456 {
1457 // A user function may be deleted and redefined later, can't use the
1458 // ufunc pointer, need to look it up again at runtime.
1459 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1460 isn->isn_arg.ufunc.cuf_argcount = argcount;
1461 }
1462
1463 stack->ga_len -= argcount; // drop the arguments
1464 if (ga_grow(stack, 1) == FAIL)
1465 return FAIL;
1466 // add return value
1467 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1468 ++stack->ga_len;
1469
1470 return OK;
1471}
1472
1473/*
1474 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1475 */
1476 static int
1477generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1478{
1479 isn_T *isn;
1480 garray_T *stack = &cctx->ctx_type_stack;
1481
Bram Moolenaar080457c2020-03-03 21:53:32 +01001482 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1484 return FAIL;
1485 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1486 isn->isn_arg.ufunc.cuf_argcount = argcount;
1487
1488 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001489 if (ga_grow(stack, 1) == FAIL)
1490 return FAIL;
1491 // add return value
1492 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1493 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494
1495 return OK;
1496}
1497
1498/*
1499 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001500 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 */
1502 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001503generate_PCALL(
1504 cctx_T *cctx,
1505 int argcount,
1506 char_u *name,
1507 type_T *type,
1508 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509{
1510 isn_T *isn;
1511 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001512 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513
Bram Moolenaar080457c2020-03-03 21:53:32 +01001514 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001515
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001516 if (type->tt_type == VAR_ANY)
1517 ret_type = &t_any;
1518 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001519 {
1520 if (type->tt_argcount != -1)
1521 {
1522 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1523
1524 if (argcount < type->tt_min_argcount - varargs)
1525 {
1526 semsg(_(e_toofewarg), "[reference]");
1527 return FAIL;
1528 }
1529 if (!varargs && argcount > type->tt_argcount)
1530 {
1531 semsg(_(e_toomanyarg), "[reference]");
1532 return FAIL;
1533 }
1534 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001535 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001536 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001537 else
1538 {
1539 semsg(_("E1085: Not a callable type: %s"), name);
1540 return FAIL;
1541 }
1542
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1544 return FAIL;
1545 isn->isn_arg.pfunc.cpf_top = at_top;
1546 isn->isn_arg.pfunc.cpf_argcount = argcount;
1547
1548 stack->ga_len -= argcount; // drop the arguments
1549
1550 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001551 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001553 // If partial is above the arguments it must be cleared and replaced with
1554 // the return value.
1555 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1556 return FAIL;
1557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 return OK;
1559}
1560
1561/*
1562 * Generate an ISN_MEMBER instruction.
1563 */
1564 static int
1565generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1566{
1567 isn_T *isn;
1568 garray_T *stack = &cctx->ctx_type_stack;
1569 type_T *type;
1570
Bram Moolenaar080457c2020-03-03 21:53:32 +01001571 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1573 return FAIL;
1574 isn->isn_arg.string = vim_strnsave(name, (int)len);
1575
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001576 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001578 if (type->tt_type != VAR_DICT && type != &t_any)
1579 {
1580 emsg(_(e_dictreq));
1581 return FAIL;
1582 }
1583 // change dict type to dict member type
1584 if (type->tt_type == VAR_DICT)
1585 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586
1587 return OK;
1588}
1589
1590/*
1591 * Generate an ISN_ECHO instruction.
1592 */
1593 static int
1594generate_ECHO(cctx_T *cctx, int with_white, int count)
1595{
1596 isn_T *isn;
1597
Bram Moolenaar080457c2020-03-03 21:53:32 +01001598 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1600 return FAIL;
1601 isn->isn_arg.echo.echo_with_white = with_white;
1602 isn->isn_arg.echo.echo_count = count;
1603
1604 return OK;
1605}
1606
Bram Moolenaarad39c092020-02-26 18:23:43 +01001607/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001608 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001609 */
1610 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001611generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001612{
1613 isn_T *isn;
1614
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001615 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001616 return FAIL;
1617 isn->isn_arg.number = count;
1618
1619 return OK;
1620}
1621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 static int
1623generate_EXEC(cctx_T *cctx, char_u *line)
1624{
1625 isn_T *isn;
1626
Bram Moolenaar080457c2020-03-03 21:53:32 +01001627 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1629 return FAIL;
1630 isn->isn_arg.string = vim_strsave(line);
1631 return OK;
1632}
1633
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001634 static int
1635generate_EXECCONCAT(cctx_T *cctx, int count)
1636{
1637 isn_T *isn;
1638
1639 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1640 return FAIL;
1641 isn->isn_arg.number = count;
1642 return OK;
1643}
1644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645/*
1646 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001647 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001649 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1651{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 lvar_T *lvar;
1653
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001654 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 {
1656 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001657 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 }
1659
1660 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001661 return NULL;
1662 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001664 // Every local variable uses the next entry on the stack. We could re-use
1665 // the last ones when leaving a scope, but then variables used in a closure
1666 // might get overwritten. To keep things simple do not re-use stack
1667 // entries. This is less efficient, but memory is cheap these days.
1668 lvar->lv_idx = cctx->ctx_locals_count++;
1669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1671 lvar->lv_const = isConst;
1672 lvar->lv_type = type;
1673
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001674 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675}
1676
1677/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001678 * Remove local variables above "new_top".
1679 */
1680 static void
1681unwind_locals(cctx_T *cctx, int new_top)
1682{
1683 if (cctx->ctx_locals.ga_len > new_top)
1684 {
1685 int idx;
1686 lvar_T *lvar;
1687
1688 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1689 {
1690 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1691 vim_free(lvar->lv_name);
1692 }
1693 }
1694 cctx->ctx_locals.ga_len = new_top;
1695}
1696
1697/*
1698 * Free all local variables.
1699 */
1700 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001701free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001702{
1703 unwind_locals(cctx, 0);
1704 ga_clear(&cctx->ctx_locals);
1705}
1706
1707/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 * Skip over a type definition and return a pointer to just after it.
1709 */
1710 char_u *
1711skip_type(char_u *start)
1712{
1713 char_u *p = start;
1714
1715 while (ASCII_ISALNUM(*p) || *p == '_')
1716 ++p;
1717
1718 // Skip over "<type>"; this is permissive about white space.
1719 if (*skipwhite(p) == '<')
1720 {
1721 p = skipwhite(p);
1722 p = skip_type(skipwhite(p + 1));
1723 p = skipwhite(p);
1724 if (*p == '>')
1725 ++p;
1726 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001727 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1728 {
1729 // handle func(args): type
1730 ++p;
1731 while (*p != ')' && *p != NUL)
1732 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001733 char_u *sp = p;
1734
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001735 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001736 if (p == sp)
1737 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001738 if (*p == ',')
1739 p = skipwhite(p + 1);
1740 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001741 if (*p == ')')
1742 {
1743 if (p[1] == ':')
1744 p = skip_type(skipwhite(p + 2));
1745 else
1746 p = skipwhite(p + 1);
1747 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001748 }
1749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 return p;
1751}
1752
1753/*
1754 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001755 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 * Returns NULL in case of failure.
1757 */
1758 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001759parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760{
1761 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001762 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763
1764 if (**arg != '<')
1765 {
1766 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001767 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 else
1769 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001770 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 }
1772 *arg = skipwhite(*arg + 1);
1773
Bram Moolenaard77a8522020-04-03 21:59:57 +02001774 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775
1776 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001777 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 {
1779 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001780 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 }
1782 ++*arg;
1783
1784 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001785 return get_list_type(member_type, type_gap);
1786 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787}
1788
1789/*
1790 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001791 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 */
1793 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001794parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795{
1796 char_u *p = *arg;
1797 size_t len;
1798
1799 // skip over the first word
1800 while (ASCII_ISALNUM(*p) || *p == '_')
1801 ++p;
1802 len = p - *arg;
1803
1804 switch (**arg)
1805 {
1806 case 'a':
1807 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1808 {
1809 *arg += len;
1810 return &t_any;
1811 }
1812 break;
1813 case 'b':
1814 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1815 {
1816 *arg += len;
1817 return &t_bool;
1818 }
1819 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1820 {
1821 *arg += len;
1822 return &t_blob;
1823 }
1824 break;
1825 case 'c':
1826 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1827 {
1828 *arg += len;
1829 return &t_channel;
1830 }
1831 break;
1832 case 'd':
1833 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1834 {
1835 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001836 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 }
1838 break;
1839 case 'f':
1840 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1841 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001842#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 *arg += len;
1844 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001845#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001846 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001847 return &t_any;
1848#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 }
1850 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1851 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001852 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001853 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001854 int argcount = -1;
1855 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001856 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001857 type_T *arg_type[MAX_FUNC_ARGS + 1];
1858
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001859 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001861 if (**arg == '(')
1862 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001863 // "func" may or may not return a value, "func()" does
1864 // not return a value.
1865 ret_type = &t_void;
1866
Bram Moolenaard77a8522020-04-03 21:59:57 +02001867 p = ++*arg;
1868 argcount = 0;
1869 while (*p != NUL && *p != ')')
1870 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001871 if (*p == '?')
1872 {
1873 if (first_optional == -1)
1874 first_optional = argcount;
1875 ++p;
1876 }
1877 else if (first_optional != -1)
1878 {
1879 emsg(_("E1007: mandatory argument after optional argument"));
1880 return &t_any;
1881 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001882 else if (STRNCMP(p, "...", 3) == 0)
1883 {
1884 flags |= TTFLAG_VARARGS;
1885 p += 3;
1886 }
1887
1888 arg_type[argcount++] = parse_type(&p, type_gap);
1889
1890 // Nothing comes after "...{type}".
1891 if (flags & TTFLAG_VARARGS)
1892 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001893
Bram Moolenaard77a8522020-04-03 21:59:57 +02001894 if (*p != ',' && *skipwhite(p) == ',')
1895 {
1896 semsg(_(e_no_white_before), ",");
1897 return &t_any;
1898 }
1899 if (*p == ',')
1900 {
1901 ++p;
1902 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001903 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001904 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001905 return &t_any;
1906 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001907 }
1908 p = skipwhite(p);
1909 if (argcount == MAX_FUNC_ARGS)
1910 {
1911 emsg(_("E740: Too many argument types"));
1912 return &t_any;
1913 }
1914 }
1915
1916 p = skipwhite(p);
1917 if (*p != ')')
1918 {
1919 emsg(_(e_missing_close));
1920 return &t_any;
1921 }
1922 *arg = p + 1;
1923 }
1924 if (**arg == ':')
1925 {
1926 // parse return type
1927 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001928 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001929 semsg(_(e_white_after), ":");
1930 *arg = skipwhite(*arg);
1931 ret_type = parse_type(arg, type_gap);
1932 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001933 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001934 type = get_func_type(ret_type, argcount, type_gap);
1935 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001936 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001937 type = alloc_func_type(ret_type, argcount, type_gap);
1938 type->tt_flags = flags;
1939 if (argcount > 0)
1940 {
1941 type->tt_argcount = argcount;
1942 type->tt_min_argcount = first_optional == -1
1943 ? argcount : first_optional;
1944 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001945 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001946 return &t_any;
1947 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001948 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001949 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001950 }
1951 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 }
1953 break;
1954 case 'j':
1955 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1956 {
1957 *arg += len;
1958 return &t_job;
1959 }
1960 break;
1961 case 'l':
1962 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1963 {
1964 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001965 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 }
1967 break;
1968 case 'n':
1969 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1970 {
1971 *arg += len;
1972 return &t_number;
1973 }
1974 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 case 's':
1976 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1977 {
1978 *arg += len;
1979 return &t_string;
1980 }
1981 break;
1982 case 'v':
1983 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1984 {
1985 *arg += len;
1986 return &t_void;
1987 }
1988 break;
1989 }
1990
1991 semsg(_("E1010: Type not recognized: %s"), *arg);
1992 return &t_any;
1993}
1994
1995/*
1996 * Check if "type1" and "type2" are exactly the same.
1997 */
1998 static int
1999equal_type(type_T *type1, type_T *type2)
2000{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002001 int i;
2002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 if (type1->tt_type != type2->tt_type)
2004 return FALSE;
2005 switch (type1->tt_type)
2006 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002008 case VAR_ANY:
2009 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 case VAR_SPECIAL:
2011 case VAR_BOOL:
2012 case VAR_NUMBER:
2013 case VAR_FLOAT:
2014 case VAR_STRING:
2015 case VAR_BLOB:
2016 case VAR_JOB:
2017 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002018 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 case VAR_LIST:
2020 case VAR_DICT:
2021 return equal_type(type1->tt_member, type2->tt_member);
2022 case VAR_FUNC:
2023 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002024 if (!equal_type(type1->tt_member, type2->tt_member)
2025 || type1->tt_argcount != type2->tt_argcount)
2026 return FALSE;
2027 if (type1->tt_argcount < 0
2028 || type1->tt_args == NULL || type2->tt_args == NULL)
2029 return TRUE;
2030 for (i = 0; i < type1->tt_argcount; ++i)
2031 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2032 return FALSE;
2033 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 }
2035 return TRUE;
2036}
2037
2038/*
2039 * Find the common type of "type1" and "type2" and put it in "dest".
2040 * "type2" and "dest" may be the same.
2041 */
2042 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002043common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044{
2045 if (equal_type(type1, type2))
2046 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002047 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 return;
2049 }
2050
2051 if (type1->tt_type == type2->tt_type)
2052 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2054 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002055 type_T *common;
2056
Bram Moolenaard77a8522020-04-03 21:59:57 +02002057 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002058 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002059 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002060 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002061 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 return;
2063 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002064 if (type1->tt_type == VAR_FUNC)
2065 {
2066 type_T *common;
2067
2068 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2069 if (type1->tt_argcount == type2->tt_argcount
2070 && type1->tt_argcount >= 0)
2071 {
2072 int argcount = type1->tt_argcount;
2073 int i;
2074
2075 *dest = alloc_func_type(common, argcount, type_gap);
2076 if (type1->tt_args != NULL && type2->tt_args != NULL)
2077 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002078 if (func_type_add_arg_types(*dest, argcount,
2079 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002080 for (i = 0; i < argcount; ++i)
2081 common_type(type1->tt_args[i], type2->tt_args[i],
2082 &(*dest)->tt_args[i], type_gap);
2083 }
2084 }
2085 else
2086 *dest = alloc_func_type(common, -1, type_gap);
2087 return;
2088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 }
2090
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002091 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092}
2093
2094 char *
2095vartype_name(vartype_T type)
2096{
2097 switch (type)
2098 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002099 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002100 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 case VAR_SPECIAL: return "special";
2103 case VAR_BOOL: return "bool";
2104 case VAR_NUMBER: return "number";
2105 case VAR_FLOAT: return "float";
2106 case VAR_STRING: return "string";
2107 case VAR_BLOB: return "blob";
2108 case VAR_JOB: return "job";
2109 case VAR_CHANNEL: return "channel";
2110 case VAR_LIST: return "list";
2111 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002112
2113 case VAR_FUNC:
2114 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002116 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117}
2118
2119/*
2120 * Return the name of a type.
2121 * The result may be in allocated memory, in which case "tofree" is set.
2122 */
2123 char *
2124type_name(type_T *type, char **tofree)
2125{
2126 char *name = vartype_name(type->tt_type);
2127
2128 *tofree = NULL;
2129 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2130 {
2131 char *member_free;
2132 char *member_name = type_name(type->tt_member, &member_free);
2133 size_t len;
2134
2135 len = STRLEN(name) + STRLEN(member_name) + 3;
2136 *tofree = alloc(len);
2137 if (*tofree != NULL)
2138 {
2139 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2140 vim_free(member_free);
2141 return *tofree;
2142 }
2143 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002144 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002145 {
2146 garray_T ga;
2147 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002148 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002149
2150 ga_init2(&ga, 1, 100);
2151 if (ga_grow(&ga, 20) == FAIL)
2152 return "[unknown]";
2153 *tofree = ga.ga_data;
2154 STRCPY(ga.ga_data, "func(");
2155 ga.ga_len += 5;
2156
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002157 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002158 {
2159 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002160 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002161 int len;
2162
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002163 if (type->tt_args == NULL)
2164 arg_type = "[unknown]";
2165 else
2166 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002167 if (i > 0)
2168 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002169 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002170 ga.ga_len += 2;
2171 }
2172 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002173 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002174 {
2175 vim_free(arg_free);
2176 return "[unknown]";
2177 }
2178 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002179 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002180 {
2181 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2182 ga.ga_len += 3;
2183 }
2184 else if (i >= type->tt_min_argcount)
2185 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002186 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002187 ga.ga_len += len;
2188 vim_free(arg_free);
2189 }
2190
2191 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002192 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002193 else
2194 {
2195 char *ret_free;
2196 char *ret_name = type_name(type->tt_member, &ret_free);
2197 int len;
2198
2199 len = (int)STRLEN(ret_name) + 4;
2200 if (ga_grow(&ga, len) == FAIL)
2201 {
2202 vim_free(ret_free);
2203 return "[unknown]";
2204 }
2205 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002206 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2207 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002208 vim_free(ret_free);
2209 }
2210 return ga.ga_data;
2211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212
2213 return name;
2214}
2215
2216/*
2217 * Find "name" in script-local items of script "sid".
2218 * Returns the index in "sn_var_vals" if found.
2219 * If found but not in "sn_var_vals" returns -1.
2220 * If not found returns -2.
2221 */
2222 int
2223get_script_item_idx(int sid, char_u *name, int check_writable)
2224{
2225 hashtab_T *ht;
2226 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002227 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 int idx;
2229
2230 // First look the name up in the hashtable.
2231 if (sid <= 0 || sid > script_items.ga_len)
2232 return -1;
2233 ht = &SCRIPT_VARS(sid);
2234 di = find_var_in_ht(ht, 0, name, TRUE);
2235 if (di == NULL)
2236 return -2;
2237
2238 // Now find the svar_T index in sn_var_vals.
2239 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2240 {
2241 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2242
2243 if (sv->sv_tv == &di->di_tv)
2244 {
2245 if (check_writable && sv->sv_const)
2246 semsg(_(e_readonlyvar), name);
2247 return idx;
2248 }
2249 }
2250 return -1;
2251}
2252
2253/*
2254 * Find "name" in imported items of the current script/
2255 */
2256 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002257find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002259 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 int idx;
2261
2262 if (cctx != NULL)
2263 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2264 {
2265 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2266 + idx;
2267
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002268 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2269 : STRLEN(import->imp_name) == len
2270 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 return import;
2272 }
2273
2274 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2275 {
2276 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2277
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002278 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2279 : STRLEN(import->imp_name) == len
2280 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 return import;
2282 }
2283 return NULL;
2284}
2285
2286/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002287 * Free all imported variables.
2288 */
2289 static void
2290free_imported(cctx_T *cctx)
2291{
2292 int idx;
2293
2294 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2295 {
2296 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2297
2298 vim_free(import->imp_name);
2299 }
2300 ga_clear(&cctx->ctx_imports);
2301}
2302
2303/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002304 * Get the next line of the function from "cctx".
2305 * Returns NULL when at the end.
2306 */
2307 static char_u *
2308next_line_from_context(cctx_T *cctx)
2309{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002310 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002311
2312 do
2313 {
2314 ++cctx->ctx_lnum;
2315 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002316 {
2317 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002318 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002319 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002320 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002321 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002322 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2323 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002324 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002325 return line;
2326}
2327
2328/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002329 * Return TRUE if "p" points at a "#" but not at "#{".
2330 */
2331 static int
2332comment_start(char_u *p)
2333{
2334 return p[0] == '#' && p[1] != '{';
2335}
2336
2337/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002338 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002339 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002340 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2341 */
2342 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002343may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002344{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002345 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002346 {
2347 char_u *next = next_line_from_context(cctx);
2348
2349 if (next == NULL)
2350 return FAIL;
2351 *arg = skipwhite(next);
2352 }
2353 return OK;
2354}
2355
Bram Moolenaara5565e42020-05-09 15:44:01 +02002356// Structure passed between the compile_expr* functions to keep track of
2357// constants that have been parsed but for which no code was produced yet. If
2358// possible expressions on these constants are applied at compile time. If
2359// that is not possible, the code to push the constants needs to be generated
2360// before other instructions.
2361typedef struct {
2362 typval_T pp_tv[10]; // stack of ppconst constants
2363 int pp_used; // active entries in pp_tv[]
2364} ppconst_T;
2365
2366/*
2367 * Generate a PUSH instruction for "tv".
2368 * "tv" will be consumed or cleared.
2369 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2370 */
2371 static int
2372generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2373{
2374 if (tv != NULL)
2375 {
2376 switch (tv->v_type)
2377 {
2378 case VAR_UNKNOWN:
2379 break;
2380 case VAR_BOOL:
2381 generate_PUSHBOOL(cctx, tv->vval.v_number);
2382 break;
2383 case VAR_SPECIAL:
2384 generate_PUSHSPEC(cctx, tv->vval.v_number);
2385 break;
2386 case VAR_NUMBER:
2387 generate_PUSHNR(cctx, tv->vval.v_number);
2388 break;
2389#ifdef FEAT_FLOAT
2390 case VAR_FLOAT:
2391 generate_PUSHF(cctx, tv->vval.v_float);
2392 break;
2393#endif
2394 case VAR_BLOB:
2395 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2396 tv->vval.v_blob = NULL;
2397 break;
2398 case VAR_STRING:
2399 generate_PUSHS(cctx, tv->vval.v_string);
2400 tv->vval.v_string = NULL;
2401 break;
2402 default:
2403 iemsg("constant type not supported");
2404 clear_tv(tv);
2405 return FAIL;
2406 }
2407 tv->v_type = VAR_UNKNOWN;
2408 }
2409 return OK;
2410}
2411
2412/*
2413 * Generate code for any ppconst entries.
2414 */
2415 static int
2416generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2417{
2418 int i;
2419 int ret = OK;
2420
2421 for (i = 0; i < ppconst->pp_used; ++i)
2422 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2423 ret = FAIL;
2424 ppconst->pp_used = 0;
2425 return ret;
2426}
2427
2428/*
2429 * Clear ppconst constants. Used when failing.
2430 */
2431 static void
2432clear_ppconst(ppconst_T *ppconst)
2433{
2434 int i;
2435
2436 for (i = 0; i < ppconst->pp_used; ++i)
2437 clear_tv(&ppconst->pp_tv[i]);
2438 ppconst->pp_used = 0;
2439}
2440
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002441/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002442 * Generate an instruction to load script-local variable "name", without the
2443 * leading "s:".
2444 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 */
2446 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002447compile_load_scriptvar(
2448 cctx_T *cctx,
2449 char_u *name, // variable NUL terminated
2450 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002451 char_u **end, // end of variable
2452 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002454 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2456 imported_T *import;
2457
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002458 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002460 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002461 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2462 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 }
2464 if (idx >= 0)
2465 {
2466 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2467
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002468 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 current_sctx.sc_sid, idx, sv->sv_type);
2470 return OK;
2471 }
2472
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002473 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 if (import != NULL)
2475 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002476 if (import->imp_all)
2477 {
2478 char_u *p = skipwhite(*end);
2479 int name_len;
2480 ufunc_T *ufunc;
2481 type_T *type;
2482
2483 // Used "import * as Name", need to lookup the member.
2484 if (*p != '.')
2485 {
2486 semsg(_("E1060: expected dot after name: %s"), start);
2487 return FAIL;
2488 }
2489 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002490 if (VIM_ISWHITE(*p))
2491 {
2492 emsg(_("E1074: no white space allowed after dot"));
2493 return FAIL;
2494 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002495
2496 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2497 // TODO: what if it is a function?
2498 if (idx < 0)
2499 return FAIL;
2500 *end = p;
2501
2502 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2503 import->imp_sid,
2504 idx,
2505 type);
2506 }
2507 else
2508 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002509 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002510 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2511 import->imp_sid,
2512 import->imp_var_vals_idx,
2513 import->imp_type);
2514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 return OK;
2516 }
2517
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002518 if (error)
2519 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 return FAIL;
2521}
2522
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002523 static int
2524generate_funcref(cctx_T *cctx, char_u *name)
2525{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002526 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002527
2528 if (ufunc == NULL)
2529 return FAIL;
2530
2531 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2532}
2533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534/*
2535 * Compile a variable name into a load instruction.
2536 * "end" points to just after the name.
2537 * When "error" is FALSE do not give an error when not found.
2538 */
2539 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002540compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541{
2542 type_T *type;
2543 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002544 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002546 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547
2548 if (*(*arg + 1) == ':')
2549 {
2550 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002551 if (end <= *arg + 2)
2552 name = vim_strsave((char_u *)"[empty]");
2553 else
2554 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 if (name == NULL)
2556 return FAIL;
2557
2558 if (**arg == 'v')
2559 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002560 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 }
2562 else if (**arg == 'g')
2563 {
2564 // Global variables can be defined later, thus we don't check if it
2565 // exists, give error at runtime.
2566 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2567 }
2568 else if (**arg == 's')
2569 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002570 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002572 else if (**arg == 'b')
2573 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002574 // Buffer-local variables can be defined later, thus we don't check
2575 // if it exists, give error at runtime.
2576 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002577 }
2578 else if (**arg == 'w')
2579 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002580 // Window-local variables can be defined later, thus we don't check
2581 // if it exists, give error at runtime.
2582 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002583 }
2584 else if (**arg == 't')
2585 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002586 // Tabpage-local variables can be defined later, thus we don't
2587 // check if it exists, give error at runtime.
2588 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002589 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 else
2591 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002592 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 goto theend;
2594 }
2595 }
2596 else
2597 {
2598 size_t len = end - *arg;
2599 int idx;
2600 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002601 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602
2603 name = vim_strnsave(*arg, end - *arg);
2604 if (name == NULL)
2605 return FAIL;
2606
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002607 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002609 if (!gen_load_outer)
2610 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 }
2612 else
2613 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002614 lvar_T *lvar = lookup_local(*arg, len, cctx);
2615
2616 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002618 type = lvar->lv_type;
2619 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002620 if (lvar->lv_from_outer)
2621 gen_load_outer = TRUE;
2622 else
2623 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 }
2625 else
2626 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002627 // "var" can be script-local even without using "s:" if it
2628 // already exists.
2629 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2630 == SCRIPT_VERSION_VIM9
2631 || lookup_script(*arg, len) == OK)
2632 res = compile_load_scriptvar(cctx, name, *arg, &end,
2633 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002634
Bram Moolenaara5565e42020-05-09 15:44:01 +02002635 // When the name starts with an uppercase letter or "x:" it
2636 // can be a user defined function.
2637 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2638 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 }
2640 }
2641 if (gen_load)
2642 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002643 if (gen_load_outer)
2644 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 }
2646
2647 *arg = end;
2648
2649theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002650 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 semsg(_(e_var_notfound), name);
2652 vim_free(name);
2653 return res;
2654}
2655
2656/*
2657 * Compile the argument expressions.
2658 * "arg" points to just after the "(" and is advanced to after the ")"
2659 */
2660 static int
2661compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2662{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002663 char_u *p = *arg;
2664 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665
Bram Moolenaare6085c52020-04-12 20:19:16 +02002666 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002668 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002669 {
2670 p = next_line_from_context(cctx);
2671 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002672 goto failret;
2673 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002674 p = skipwhite(p);
2675 }
2676 if (*p == ')')
2677 {
2678 *arg = p + 1;
2679 return OK;
2680 }
2681
Bram Moolenaara5565e42020-05-09 15:44:01 +02002682 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 return FAIL;
2684 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002685
2686 if (*p != ',' && *skipwhite(p) == ',')
2687 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002688 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002689 p = skipwhite(p);
2690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002692 {
2693 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002694 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002695 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002696 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002697 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002698 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002700failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002701 emsg(_(e_missing_close));
2702 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703}
2704
2705/*
2706 * Compile a function call: name(arg1, arg2)
2707 * "arg" points to "name", "arg + varlen" to the "(".
2708 * "argcount_init" is 1 for "value->method()"
2709 * Instructions:
2710 * EVAL arg1
2711 * EVAL arg2
2712 * BCALL / DCALL / UCALL
2713 */
2714 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002715compile_call(
2716 char_u **arg,
2717 size_t varlen,
2718 cctx_T *cctx,
2719 ppconst_T *ppconst,
2720 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721{
2722 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002723 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 int argcount = argcount_init;
2725 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002726 char_u fname_buf[FLEN_FIXED + 1];
2727 char_u *tofree = NULL;
2728 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002730 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731
Bram Moolenaara5565e42020-05-09 15:44:01 +02002732 // we can evaluate "has('name')" at compile time
2733 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2734 {
2735 char_u *s = skipwhite(*arg + varlen + 1);
2736 typval_T argvars[2];
2737
2738 argvars[0].v_type = VAR_UNKNOWN;
2739 if (*s == '"')
2740 (void)get_string_tv(&s, &argvars[0], TRUE);
2741 else if (*s == '\'')
2742 (void)get_lit_string_tv(&s, &argvars[0], TRUE);
2743 s = skipwhite(s);
2744 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2745 {
2746 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2747
2748 *arg = s + 1;
2749 argvars[1].v_type = VAR_UNKNOWN;
2750 tv->v_type = VAR_NUMBER;
2751 tv->vval.v_number = 0;
2752 f_has(argvars, tv);
2753 clear_tv(&argvars[0]);
2754 ++ppconst->pp_used;
2755 return OK;
2756 }
2757 }
2758
2759 if (generate_ppconst(cctx, ppconst) == FAIL)
2760 return FAIL;
2761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 if (varlen >= sizeof(namebuf))
2763 {
2764 semsg(_("E1011: name too long: %s"), name);
2765 return FAIL;
2766 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002767 vim_strncpy(namebuf, *arg, varlen);
2768 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769
2770 *arg = skipwhite(*arg + varlen + 1);
2771 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002772 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002774 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 {
2776 int idx;
2777
2778 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002779 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002781 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002782 else
2783 semsg(_(e_unknownfunc), namebuf);
2784 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 }
2786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002788 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002790 {
2791 res = generate_CALL(cctx, ufunc, argcount);
2792 goto theend;
2793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794
2795 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002796 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002798 if (STRNCMP(namebuf, "g:", 2) != 0
2799 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002800 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002801 garray_T *stack = &cctx->ctx_type_stack;
2802 type_T *type;
2803
2804 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2805 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002806 goto theend;
2807 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002809 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002810 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002811 if (STRNCMP(namebuf, "g:", 2) == 0)
2812 res = generate_UCALL(cctx, name, argcount);
2813 else
2814 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002815
2816theend:
2817 vim_free(tofree);
2818 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819}
2820
2821// like NAMESPACE_CHAR but with 'a' and 'l'.
2822#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2823
2824/*
2825 * Find the end of a variable or function name. Unlike find_name_end() this
2826 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002827 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 * Return a pointer to just after the name. Equal to "arg" if there is no
2829 * valid name.
2830 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002831 static char_u *
2832to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833{
2834 char_u *p;
2835
2836 // Quick check for valid starting character.
2837 if (!eval_isnamec1(*arg))
2838 return arg;
2839
2840 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2841 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2842 // and can be used in slice "[n:]".
2843 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002844 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2846 break;
2847 return p;
2848}
2849
2850/*
2851 * Like to_name_end() but also skip over a list or dict constant.
2852 */
2853 char_u *
2854to_name_const_end(char_u *arg)
2855{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002856 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 typval_T rettv;
2858
2859 if (p == arg && *arg == '[')
2860 {
2861
2862 // Can be "[1, 2, 3]->Func()".
2863 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2864 p = arg;
2865 }
2866 else if (p == arg && *arg == '#' && arg[1] == '{')
2867 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002868 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 ++p;
2870 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2871 p = arg;
2872 }
2873 else if (p == arg && *arg == '{')
2874 {
2875 int ret = get_lambda_tv(&p, &rettv, FALSE);
2876
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002877 // Can be "{x -> ret}()".
2878 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 if (ret == NOTDONE)
2880 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2881 if (ret != OK)
2882 p = arg;
2883 }
2884
2885 return p;
2886}
2887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888/*
2889 * parse a list: [expr, expr]
2890 * "*arg" points to the '['.
2891 */
2892 static int
2893compile_list(char_u **arg, cctx_T *cctx)
2894{
2895 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002896 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 int count = 0;
2898
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002899 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002901 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002902 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002903 p = next_line_from_context(cctx);
2904 if (p == NULL)
2905 {
2906 semsg(_(e_list_end), *arg);
2907 return FAIL;
2908 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002909 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002910 p = skipwhite(p);
2911 }
2912 if (*p == ']')
2913 {
2914 ++p;
2915 // Allow for following comment, after at least one space.
2916 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2917 p += STRLEN(p);
2918 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002919 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002920 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 break;
2922 ++count;
2923 if (*p == ',')
2924 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002925 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 p = skipwhite(p);
2927 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002928 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929
2930 generate_NEWLIST(cctx, count);
2931 return OK;
2932}
2933
2934/*
2935 * parse a lambda: {arg, arg -> expr}
2936 * "*arg" points to the '{'.
2937 */
2938 static int
2939compile_lambda(char_u **arg, cctx_T *cctx)
2940{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 typval_T rettv;
2942 ufunc_T *ufunc;
2943
2944 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002945 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002949 ++ufunc->uf_refcount;
2950 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002951 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952
2953 // The function will have one line: "return {expr}".
2954 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002955 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956
2957 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002958 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 return FAIL;
2960}
2961
2962/*
2963 * Compile a lamda call: expr->{lambda}(args)
2964 * "arg" points to the "{".
2965 */
2966 static int
2967compile_lambda_call(char_u **arg, cctx_T *cctx)
2968{
2969 ufunc_T *ufunc;
2970 typval_T rettv;
2971 int argcount = 1;
2972 int ret = FAIL;
2973
2974 // Get the funcref in "rettv".
2975 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2976 return FAIL;
2977
2978 if (**arg != '(')
2979 {
2980 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002981 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 else
2983 semsg(_(e_missing_paren), "lambda");
2984 clear_tv(&rettv);
2985 return FAIL;
2986 }
2987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 ufunc = rettv.vval.v_partial->pt_func;
2989 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002990 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002991 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002992
2993 // The function will have one line: "return {expr}".
2994 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002995 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996
2997 // compile the arguments
2998 *arg = skipwhite(*arg + 1);
2999 if (compile_arguments(arg, cctx, &argcount) == OK)
3000 // call the compiled function
3001 ret = generate_CALL(cctx, ufunc, argcount);
3002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 return ret;
3004}
3005
3006/*
3007 * parse a dict: {'key': val} or #{key: val}
3008 * "*arg" points to the '{'.
3009 */
3010 static int
3011compile_dict(char_u **arg, cctx_T *cctx, int literal)
3012{
3013 garray_T *instr = &cctx->ctx_instr;
3014 int count = 0;
3015 dict_T *d = dict_alloc();
3016 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003017 char_u *whitep = *arg;
3018 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019
3020 if (d == NULL)
3021 return FAIL;
3022 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003023 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 {
3025 char_u *key = NULL;
3026
Bram Moolenaar2c330432020-04-13 14:41:35 +02003027 while (**arg == NUL || (literal && **arg == '"')
3028 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003029 {
3030 *arg = next_line_from_context(cctx);
3031 if (*arg == NULL)
3032 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003033 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003034 *arg = skipwhite(*arg);
3035 }
3036
3037 if (**arg == '}')
3038 break;
3039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 if (literal)
3041 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003042 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043
Bram Moolenaar2c330432020-04-13 14:41:35 +02003044 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 {
3046 semsg(_("E1014: Invalid key: %s"), *arg);
3047 return FAIL;
3048 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003049 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 if (generate_PUSHS(cctx, key) == FAIL)
3051 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003052 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 }
3054 else
3055 {
3056 isn_T *isn;
3057
Bram Moolenaara5565e42020-05-09 15:44:01 +02003058 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 return FAIL;
3060 // TODO: check type is string
3061 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3062 if (isn->isn_type == ISN_PUSHS)
3063 key = isn->isn_arg.string;
3064 }
3065
3066 // Check for duplicate keys, if using string keys.
3067 if (key != NULL)
3068 {
3069 item = dict_find(d, key, -1);
3070 if (item != NULL)
3071 {
3072 semsg(_(e_duplicate_key), key);
3073 goto failret;
3074 }
3075 item = dictitem_alloc(key);
3076 if (item != NULL)
3077 {
3078 item->di_tv.v_type = VAR_UNKNOWN;
3079 item->di_tv.v_lock = 0;
3080 if (dict_add(d, item) == FAIL)
3081 dictitem_free(item);
3082 }
3083 }
3084
3085 *arg = skipwhite(*arg);
3086 if (**arg != ':')
3087 {
3088 semsg(_(e_missing_dict_colon), *arg);
3089 return FAIL;
3090 }
3091
Bram Moolenaar2c330432020-04-13 14:41:35 +02003092 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003094 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003095 {
3096 *arg = next_line_from_context(cctx);
3097 if (*arg == NULL)
3098 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003099 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003100 *arg = skipwhite(*arg);
3101 }
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 ++count;
3106
Bram Moolenaar2c330432020-04-13 14:41:35 +02003107 whitep = *arg;
3108 p = skipwhite(*arg);
3109 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003110 {
3111 *arg = next_line_from_context(cctx);
3112 if (*arg == NULL)
3113 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003114 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003115 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003116 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 if (**arg == '}')
3119 break;
3120 if (**arg != ',')
3121 {
3122 semsg(_(e_missing_dict_comma), *arg);
3123 goto failret;
3124 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003125 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 *arg = skipwhite(*arg + 1);
3127 }
3128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 *arg = *arg + 1;
3130
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003131 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003132 p = skipwhite(*arg);
3133 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003134 *arg += STRLEN(*arg);
3135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 dict_unref(d);
3137 return generate_NEWDICT(cctx, count);
3138
3139failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003140 if (*arg == NULL)
3141 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 dict_unref(d);
3143 return FAIL;
3144}
3145
3146/*
3147 * Compile "&option".
3148 */
3149 static int
3150compile_get_option(char_u **arg, cctx_T *cctx)
3151{
3152 typval_T rettv;
3153 char_u *start = *arg;
3154 int ret;
3155
3156 // parse the option and get the current value to get the type.
3157 rettv.v_type = VAR_UNKNOWN;
3158 ret = get_option_tv(arg, &rettv, TRUE);
3159 if (ret == OK)
3160 {
3161 // include the '&' in the name, get_option_tv() expects it.
3162 char_u *name = vim_strnsave(start, *arg - start);
3163 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3164
3165 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3166 vim_free(name);
3167 }
3168 clear_tv(&rettv);
3169
3170 return ret;
3171}
3172
3173/*
3174 * Compile "$VAR".
3175 */
3176 static int
3177compile_get_env(char_u **arg, cctx_T *cctx)
3178{
3179 char_u *start = *arg;
3180 int len;
3181 int ret;
3182 char_u *name;
3183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 ++*arg;
3185 len = get_env_len(arg);
3186 if (len == 0)
3187 {
3188 semsg(_(e_syntax_at), start - 1);
3189 return FAIL;
3190 }
3191
3192 // include the '$' in the name, get_env_tv() expects it.
3193 name = vim_strnsave(start, len + 1);
3194 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3195 vim_free(name);
3196 return ret;
3197}
3198
3199/*
3200 * Compile "@r".
3201 */
3202 static int
3203compile_get_register(char_u **arg, cctx_T *cctx)
3204{
3205 int ret;
3206
3207 ++*arg;
3208 if (**arg == NUL)
3209 {
3210 semsg(_(e_syntax_at), *arg - 1);
3211 return FAIL;
3212 }
3213 if (!valid_yank_reg(**arg, TRUE))
3214 {
3215 emsg_invreg(**arg);
3216 return FAIL;
3217 }
3218 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3219 ++*arg;
3220 return ret;
3221}
3222
3223/*
3224 * Apply leading '!', '-' and '+' to constant "rettv".
3225 */
3226 static int
3227apply_leader(typval_T *rettv, char_u *start, char_u *end)
3228{
3229 char_u *p = end;
3230
3231 // this works from end to start
3232 while (p > start)
3233 {
3234 --p;
3235 if (*p == '-' || *p == '+')
3236 {
3237 // only '-' has an effect, for '+' we only check the type
3238#ifdef FEAT_FLOAT
3239 if (rettv->v_type == VAR_FLOAT)
3240 {
3241 if (*p == '-')
3242 rettv->vval.v_float = -rettv->vval.v_float;
3243 }
3244 else
3245#endif
3246 {
3247 varnumber_T val;
3248 int error = FALSE;
3249
3250 // tv_get_number_chk() accepts a string, but we don't want that
3251 // here
3252 if (check_not_string(rettv) == FAIL)
3253 return FAIL;
3254 val = tv_get_number_chk(rettv, &error);
3255 clear_tv(rettv);
3256 if (error)
3257 return FAIL;
3258 if (*p == '-')
3259 val = -val;
3260 rettv->v_type = VAR_NUMBER;
3261 rettv->vval.v_number = val;
3262 }
3263 }
3264 else
3265 {
3266 int v = tv2bool(rettv);
3267
3268 // '!' is permissive in the type.
3269 clear_tv(rettv);
3270 rettv->v_type = VAR_BOOL;
3271 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3272 }
3273 }
3274 return OK;
3275}
3276
3277/*
3278 * Recognize v: variables that are constants and set "rettv".
3279 */
3280 static void
3281get_vim_constant(char_u **arg, typval_T *rettv)
3282{
3283 if (STRNCMP(*arg, "v:true", 6) == 0)
3284 {
3285 rettv->v_type = VAR_BOOL;
3286 rettv->vval.v_number = VVAL_TRUE;
3287 *arg += 6;
3288 }
3289 else if (STRNCMP(*arg, "v:false", 7) == 0)
3290 {
3291 rettv->v_type = VAR_BOOL;
3292 rettv->vval.v_number = VVAL_FALSE;
3293 *arg += 7;
3294 }
3295 else if (STRNCMP(*arg, "v:null", 6) == 0)
3296 {
3297 rettv->v_type = VAR_SPECIAL;
3298 rettv->vval.v_number = VVAL_NULL;
3299 *arg += 6;
3300 }
3301 else if (STRNCMP(*arg, "v:none", 6) == 0)
3302 {
3303 rettv->v_type = VAR_SPECIAL;
3304 rettv->vval.v_number = VVAL_NONE;
3305 *arg += 6;
3306 }
3307}
3308
3309/*
Bram Moolenaar61a89812020-05-07 16:58:17 +02003310 * Evaluate an expression that is a constant:
3311 * has(arg)
3312 *
3313 * Also handle:
3314 * ! in front logical NOT
3315 *
3316 * Return FAIL if the expression is not a constant.
3317 */
3318 static int
3319evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3320{
3321 typval_T argvars[2];
3322 char_u *start_leader, *end_leader;
3323 int has_call = FALSE;
3324
3325 /*
3326 * Skip '!' characters. They are handled later.
3327 * TODO: '-' and '+' characters
3328 */
3329 start_leader = *arg;
3330 while (**arg == '!')
3331 *arg = skipwhite(*arg + 1);
3332 end_leader = *arg;
3333
3334 /*
3335 * Recognize only a few types of constants for now.
3336 */
3337 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
3338 {
3339 tv->v_type = VAR_BOOL;
3340 tv->vval.v_number = VVAL_TRUE;
3341 *arg += 4;
3342 return OK;
3343 }
3344 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
3345 {
3346 tv->v_type = VAR_BOOL;
3347 tv->vval.v_number = VVAL_FALSE;
3348 *arg += 5;
3349 return OK;
3350 }
3351
3352 if (STRNCMP("has(", *arg, 4) == 0)
3353 {
3354 has_call = TRUE;
3355 *arg = skipwhite(*arg + 4);
3356 }
3357
3358 if (**arg == '"')
3359 {
3360 if (get_string_tv(arg, tv, TRUE) == FAIL)
3361 return FAIL;
3362 }
3363 else if (**arg == '\'')
3364 {
3365 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3366 return FAIL;
3367 }
3368 else
3369 return FAIL;
3370
3371 if (has_call)
3372 {
3373 *arg = skipwhite(*arg);
3374 if (**arg != ')')
3375 return FAIL;
3376 *arg = *arg + 1;
3377
3378 argvars[0] = *tv;
3379 argvars[1].v_type = VAR_UNKNOWN;
3380 tv->v_type = VAR_NUMBER;
3381 tv->vval.v_number = 0;
3382 f_has(argvars, tv);
3383 clear_tv(&argvars[0]);
3384
3385 while (start_leader < end_leader)
3386 {
3387 if (*start_leader == '!')
3388 tv->vval.v_number = !tv->vval.v_number;
3389 ++start_leader;
3390 }
3391 }
3392 else if (end_leader > start_leader)
3393 {
3394 clear_tv(tv);
3395 return FAIL;
3396 }
3397
3398 return OK;
3399}
3400
3401/*
3402 * * number multiplication
3403 * / number division
3404 * % number modulo
3405 */
3406 static int
3407evaluate_const_expr6(char_u **arg, cctx_T *cctx, typval_T *tv)
3408{
3409 char_u *op;
3410
3411 // get the first variable
3412 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
3413 return FAIL;
3414
3415 /*
3416 * Repeat computing, until no "*", "/" or "%" is following.
3417 */
3418 for (;;)
3419 {
3420 op = skipwhite(*arg);
3421 if (*op != '*' && *op != '/' && *op != '%')
3422 break;
3423 // TODO: not implemented yet.
3424 clear_tv(tv);
3425 return FAIL;
3426 }
3427 return OK;
3428}
3429
3430/*
3431 * + number addition
3432 * - number subtraction
3433 * .. string concatenation
3434 */
3435 static int
3436evaluate_const_expr5(char_u **arg, cctx_T *cctx, typval_T *tv)
3437{
3438 char_u *op;
3439 int oplen;
3440
3441 // get the first variable
3442 if (evaluate_const_expr6(arg, cctx, tv) == FAIL)
3443 return FAIL;
3444
3445 /*
3446 * Repeat computing, until no "+", "-" or ".." is following.
3447 */
3448 for (;;)
3449 {
3450 op = skipwhite(*arg);
3451 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3452 break;
3453 oplen = (*op == '.' ? 2 : 1);
3454
3455 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3456 {
3457 clear_tv(tv);
3458 return FAIL;
3459 }
3460
3461 if (*op == '.' && tv->v_type == VAR_STRING)
3462 {
3463 typval_T tv2;
3464 size_t len1;
3465 char_u *s1, *s2;
3466
3467 tv2.v_type = VAR_UNKNOWN;
3468 *arg = skipwhite(op + oplen);
3469
3470 // TODO: what if we fail???
3471 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
3472 return FAIL;
3473
3474 // get the second variable
3475 if (evaluate_const_expr6(arg, cctx, &tv2) == FAIL)
3476 {
3477 clear_tv(tv);
3478 return FAIL;
3479 }
3480 if (tv2.v_type != VAR_STRING)
3481 {
3482 clear_tv(tv);
3483 clear_tv(&tv2);
3484 return FAIL;
3485 }
3486 s1 = tv->vval.v_string;
3487 len1 = STRLEN(s1);
3488 s2 = tv2.vval.v_string;
3489 tv->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3490 if (tv->vval.v_string == NULL)
3491 {
3492 vim_free(s1);
3493 vim_free(s2);
3494 return FAIL;
3495 }
3496 mch_memmove(tv->vval.v_string, s1, len1);
3497 STRCPY(tv->vval.v_string + len1, s2);
3498 continue;
3499 }
3500
3501 // TODO: Not implemented yet.
3502 clear_tv(tv);
3503 return FAIL;
3504 }
3505 return OK;
3506}
3507
3508 static exptype_T
3509get_compare_type(char_u *p, int *len, int *type_is)
3510{
3511 exptype_T type = EXPR_UNKNOWN;
3512 int i;
3513
3514 switch (p[0])
3515 {
3516 case '=': if (p[1] == '=')
3517 type = EXPR_EQUAL;
3518 else if (p[1] == '~')
3519 type = EXPR_MATCH;
3520 break;
3521 case '!': if (p[1] == '=')
3522 type = EXPR_NEQUAL;
3523 else if (p[1] == '~')
3524 type = EXPR_NOMATCH;
3525 break;
3526 case '>': if (p[1] != '=')
3527 {
3528 type = EXPR_GREATER;
3529 *len = 1;
3530 }
3531 else
3532 type = EXPR_GEQUAL;
3533 break;
3534 case '<': if (p[1] != '=')
3535 {
3536 type = EXPR_SMALLER;
3537 *len = 1;
3538 }
3539 else
3540 type = EXPR_SEQUAL;
3541 break;
3542 case 'i': if (p[1] == 's')
3543 {
3544 // "is" and "isnot"; but not a prefix of a name
3545 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3546 *len = 5;
3547 i = p[*len];
3548 if (!isalnum(i) && i != '_')
3549 {
3550 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3551 *type_is = TRUE;
3552 }
3553 }
3554 break;
3555 }
3556 return type;
3557}
3558
3559/*
3560 * Only comparing strings is supported right now.
3561 * expr5a == expr5b
3562 */
3563 static int
3564evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3565{
3566 exptype_T type = EXPR_UNKNOWN;
3567 char_u *p;
3568 int len = 2;
3569 int type_is = FALSE;
3570
3571 // get the first variable
3572 if (evaluate_const_expr5(arg, cctx, tv) == FAIL)
3573 return FAIL;
3574
3575 p = skipwhite(*arg);
3576 type = get_compare_type(p, &len, &type_is);
3577
3578 /*
3579 * If there is a comparative operator, use it.
3580 */
3581 if (type != EXPR_UNKNOWN)
3582 {
3583 typval_T tv2;
3584 char_u *s1, *s2;
3585 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3586 int n;
3587
3588 // TODO: Only string == string is supported now
3589 if (tv->v_type != VAR_STRING)
3590 return FAIL;
3591 if (type != EXPR_EQUAL)
3592 return FAIL;
3593
3594 // get the second variable
3595 init_tv(&tv2);
3596 *arg = skipwhite(p + len);
3597 if (evaluate_const_expr5(arg, cctx, &tv2) == FAIL
3598 || tv2.v_type != VAR_STRING)
3599 {
3600 clear_tv(&tv2);
3601 return FAIL;
3602 }
3603 s1 = tv_get_string_buf(tv, buf1);
3604 s2 = tv_get_string_buf(&tv2, buf2);
3605 n = STRCMP(s1, s2);
3606 clear_tv(tv);
3607 clear_tv(&tv2);
3608 tv->v_type = VAR_BOOL;
3609 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
3610 }
3611
3612 return OK;
3613}
3614
3615static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3616
3617/*
3618 * Compile constant || or &&.
3619 */
3620 static int
3621evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3622{
3623 char_u *p = skipwhite(*arg);
3624 int opchar = *op;
3625
3626 if (p[0] == opchar && p[1] == opchar)
3627 {
3628 int val = tv2bool(tv);
3629
3630 /*
3631 * Repeat until there is no following "||" or "&&"
3632 */
3633 while (p[0] == opchar && p[1] == opchar)
3634 {
3635 typval_T tv2;
3636
3637 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3638 return FAIL;
3639
3640 // eval the next expression
3641 *arg = skipwhite(p + 2);
3642 tv2.v_type = VAR_UNKNOWN;
3643 tv2.v_lock = 0;
3644 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
3645 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
3646 {
3647 clear_tv(&tv2);
3648 return FAIL;
3649 }
3650 if ((opchar == '&') == val)
3651 {
3652 // false || tv2 or true && tv2: use tv2
3653 clear_tv(tv);
3654 *tv = tv2;
3655 val = tv2bool(tv);
3656 }
3657 else
3658 clear_tv(&tv2);
3659 p = skipwhite(*arg);
3660 }
3661 }
3662
3663 return OK;
3664}
3665
3666/*
3667 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
3668 * Return FAIL if the expression is not a constant.
3669 */
3670 static int
3671evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
3672{
3673 // evaluate the first expression
3674 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
3675 return FAIL;
3676
3677 // || and && work almost the same
3678 return evaluate_const_and_or(arg, cctx, "&&", tv);
3679}
3680
3681/*
3682 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
3683 * Return FAIL if the expression is not a constant.
3684 */
3685 static int
3686evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
3687{
3688 // evaluate the first expression
3689 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
3690 return FAIL;
3691
3692 // || and && work almost the same
3693 return evaluate_const_and_or(arg, cctx, "||", tv);
3694}
3695
3696/*
3697 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
3698 * E.g. for "has('feature')".
3699 * This does not produce error messages. "tv" should be cleared afterwards.
3700 * Return FAIL if the expression is not a constant.
3701 */
3702 static int
3703evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
3704{
3705 char_u *p;
3706
3707 // evaluate the first expression
3708 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
3709 return FAIL;
3710
3711 p = skipwhite(*arg);
3712 if (*p == '?')
3713 {
3714 int val = tv2bool(tv);
3715 typval_T tv2;
3716
3717 // require space before and after the ?
3718 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3719 return FAIL;
3720
3721 // evaluate the second expression; any type is accepted
3722 clear_tv(tv);
3723 *arg = skipwhite(p + 1);
3724 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
3725 return FAIL;
3726
3727 // Check for the ":".
3728 p = skipwhite(*arg);
3729 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3730 return FAIL;
3731
3732 // evaluate the third expression
3733 *arg = skipwhite(p + 1);
3734 tv2.v_type = VAR_UNKNOWN;
3735 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
3736 {
3737 clear_tv(&tv2);
3738 return FAIL;
3739 }
3740 if (val)
3741 {
3742 // use the expr after "?"
3743 clear_tv(&tv2);
3744 }
3745 else
3746 {
3747 // use the expr after ":"
3748 clear_tv(tv);
3749 *tv = tv2;
3750 }
3751 }
3752 return OK;
3753}
3754
Bram Moolenaara5565e42020-05-09 15:44:01 +02003755static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003756
Bram Moolenaar61a89812020-05-07 16:58:17 +02003757/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 * Compile code to apply '-', '+' and '!'.
3759 */
3760 static int
3761compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3762{
3763 char_u *p = end;
3764
3765 // this works from end to start
3766 while (p > start)
3767 {
3768 --p;
3769 if (*p == '-' || *p == '+')
3770 {
3771 int negate = *p == '-';
3772 isn_T *isn;
3773
3774 // TODO: check type
3775 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3776 {
3777 --p;
3778 if (*p == '-')
3779 negate = !negate;
3780 }
3781 // only '-' has an effect, for '+' we only check the type
3782 if (negate)
3783 isn = generate_instr(cctx, ISN_NEGATENR);
3784 else
3785 isn = generate_instr(cctx, ISN_CHECKNR);
3786 if (isn == NULL)
3787 return FAIL;
3788 }
3789 else
3790 {
3791 int invert = TRUE;
3792
3793 while (p > start && p[-1] == '!')
3794 {
3795 --p;
3796 invert = !invert;
3797 }
3798 if (generate_2BOOL(cctx, invert) == FAIL)
3799 return FAIL;
3800 }
3801 }
3802 return OK;
3803}
3804
3805/*
3806 * Compile whatever comes after "name" or "name()".
3807 */
3808 static int
3809compile_subscript(
3810 char_u **arg,
3811 cctx_T *cctx,
3812 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003813 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003814 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815{
3816 for (;;)
3817 {
3818 if (**arg == '(')
3819 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003820 garray_T *stack = &cctx->ctx_type_stack;
3821 type_T *type;
3822 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003824 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003825 return FAIL;
3826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003828 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 *arg = skipwhite(*arg + 1);
3831 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3832 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003833 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 return FAIL;
3835 }
3836 else if (**arg == '-' && (*arg)[1] == '>')
3837 {
3838 char_u *p;
3839
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003840 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003841 return FAIL;
3842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 // something->method()
3844 // Apply the '!', '-' and '+' first:
3845 // -1.0->func() works like (-1.0)->func()
3846 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3847 return FAIL;
3848 *start_leader = end_leader; // don't apply again later
3849
3850 *arg = skipwhite(*arg + 2);
3851 if (**arg == '{')
3852 {
3853 // lambda call: list->{lambda}
3854 if (compile_lambda_call(arg, cctx) == FAIL)
3855 return FAIL;
3856 }
3857 else
3858 {
3859 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003860 p = *arg;
3861 if (ASCII_ISALPHA(*p) && p[1] == ':')
3862 p += 2;
3863 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 ;
3865 if (*p != '(')
3866 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003867 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 return FAIL;
3869 }
3870 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003871 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 return FAIL;
3873 }
3874 }
3875 else if (**arg == '[')
3876 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003877 garray_T *stack;
3878 type_T **typep;
3879
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003880 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003881 return FAIL;
3882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 // list index: list[123]
3884 // TODO: more arguments
3885 // TODO: dict member dict['name']
3886 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003887 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 return FAIL;
3889
3890 if (**arg != ']')
3891 {
3892 emsg(_(e_missbrac));
3893 return FAIL;
3894 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003895 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896
3897 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3898 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003899 stack = &cctx->ctx_type_stack;
3900 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3901 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3902 {
3903 emsg(_(e_listreq));
3904 return FAIL;
3905 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003906 if ((*typep)->tt_type == VAR_LIST)
3907 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 }
3909 else if (**arg == '.' && (*arg)[1] != '.')
3910 {
3911 char_u *p;
3912
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003913 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003914 return FAIL;
3915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 ++*arg;
3917 p = *arg;
3918 // dictionary member: dict.name
3919 if (eval_isnamec1(*p))
3920 while (eval_isnamec(*p))
3921 MB_PTR_ADV(p);
3922 if (p == *arg)
3923 {
3924 semsg(_(e_syntax_at), *arg);
3925 return FAIL;
3926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3928 return FAIL;
3929 *arg = p;
3930 }
3931 else
3932 break;
3933 }
3934
3935 // TODO - see handle_subscript():
3936 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3937 // Don't do this when "Func" is already a partial that was bound
3938 // explicitly (pt_auto is FALSE).
3939
3940 return OK;
3941}
3942
3943/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003944 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3945 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003947 * If the value is a constant "ppconst->pp_ret" will be set.
3948 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003949 *
3950 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 */
3952
3953/*
3954 * number number constant
3955 * 0zFFFFFFFF Blob constant
3956 * "string" string constant
3957 * 'string' literal string constant
3958 * &option-name option value
3959 * @r register contents
3960 * identifier variable value
3961 * function() function call
3962 * $VAR environment variable
3963 * (expression) nested expression
3964 * [expr, expr] List
3965 * {key: val, key: val} Dictionary
3966 * #{key: val, key: val} Dictionary with literal keys
3967 *
3968 * Also handle:
3969 * ! in front logical NOT
3970 * - in front unary minus
3971 * + in front unary plus (ignored)
3972 * trailing (arg) funcref/partial call
3973 * trailing [] subscript in String or List
3974 * trailing .name entry in Dictionary
3975 * trailing ->name() method call
3976 */
3977 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003978compile_expr7(
3979 char_u **arg,
3980 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003981 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 char_u *start_leader, *end_leader;
3984 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003985 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986
3987 /*
3988 * Skip '!', '-' and '+' characters. They are handled later.
3989 */
3990 start_leader = *arg;
3991 while (**arg == '!' || **arg == '-' || **arg == '+')
3992 *arg = skipwhite(*arg + 1);
3993 end_leader = *arg;
3994
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003995 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 switch (**arg)
3997 {
3998 /*
3999 * Number constant.
4000 */
4001 case '0': // also for blob starting with 0z
4002 case '1':
4003 case '2':
4004 case '3':
4005 case '4':
4006 case '5':
4007 case '6':
4008 case '7':
4009 case '8':
4010 case '9':
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004011 case '.': if (get_number_tv(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 return FAIL;
4013 break;
4014
4015 /*
4016 * String constant: "string".
4017 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004018 case '"': if (get_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 return FAIL;
4020 break;
4021
4022 /*
4023 * Literal string constant: 'str''ing'.
4024 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004025 case '\'': if (get_lit_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 return FAIL;
4027 break;
4028
4029 /*
4030 * Constant Vim variable.
4031 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004032 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 ret = NOTDONE;
4034 break;
4035
4036 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004037 * "true" constant
4038 */
4039 case 't': if (STRNCMP(*arg, "true", 4) == 0
4040 && !eval_isnamec((*arg)[4]))
4041 {
4042 *arg += 4;
4043 rettv->v_type = VAR_BOOL;
4044 rettv->vval.v_number = VVAL_TRUE;
4045 }
4046 else
4047 ret = NOTDONE;
4048 break;
4049
4050 /*
4051 * "false" constant
4052 */
4053 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4054 && !eval_isnamec((*arg)[5]))
4055 {
4056 *arg += 5;
4057 rettv->v_type = VAR_BOOL;
4058 rettv->vval.v_number = VVAL_FALSE;
4059 }
4060 else
4061 ret = NOTDONE;
4062 break;
4063
4064 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 * List: [expr, expr]
4066 */
4067 case '[': ret = compile_list(arg, cctx);
4068 break;
4069
4070 /*
4071 * Dictionary: #{key: val, key: val}
4072 */
4073 case '#': if ((*arg)[1] == '{')
4074 {
4075 ++*arg;
4076 ret = compile_dict(arg, cctx, TRUE);
4077 }
4078 else
4079 ret = NOTDONE;
4080 break;
4081
4082 /*
4083 * Lambda: {arg, arg -> expr}
4084 * Dictionary: {'key': val, 'key': val}
4085 */
4086 case '{': {
4087 char_u *start = skipwhite(*arg + 1);
4088
4089 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004090 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004092 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 if (ret != FAIL && *start == '>')
4094 ret = compile_lambda(arg, cctx);
4095 else
4096 ret = compile_dict(arg, cctx, FALSE);
4097 }
4098 break;
4099
4100 /*
4101 * Option value: &name
4102 */
4103 case '&': ret = compile_get_option(arg, cctx);
4104 break;
4105
4106 /*
4107 * Environment variable: $VAR.
4108 */
4109 case '$': ret = compile_get_env(arg, cctx);
4110 break;
4111
4112 /*
4113 * Register contents: @r.
4114 */
4115 case '@': ret = compile_get_register(arg, cctx);
4116 break;
4117 /*
4118 * nested expression: (expression).
4119 */
4120 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004121 ret = compile_expr0(arg, cctx); // recursive!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 *arg = skipwhite(*arg);
4123 if (**arg == ')')
4124 ++*arg;
4125 else if (ret == OK)
4126 {
4127 emsg(_(e_missing_close));
4128 ret = FAIL;
4129 }
4130 break;
4131
4132 default: ret = NOTDONE;
4133 break;
4134 }
4135 if (ret == FAIL)
4136 return FAIL;
4137
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004138 if (rettv->v_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 {
4140 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004141 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004143 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 return FAIL;
4145 }
4146 start_leader = end_leader; // don't apply again below
4147
Bram Moolenaara5565e42020-05-09 15:44:01 +02004148 if (cctx->ctx_skip == TRUE)
4149 clear_tv(rettv);
4150 else
4151 // A constant expression can possibly be handled compile time,
4152 // return the value instead of generating code.
4153 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 }
4155 else if (ret == NOTDONE)
4156 {
4157 char_u *p;
4158 int r;
4159
4160 if (!eval_isnamec1(**arg))
4161 {
4162 semsg(_("E1015: Name expected: %s"), *arg);
4163 return FAIL;
4164 }
4165
4166 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004167 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004169 {
4170 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004173 {
4174 if (generate_ppconst(cctx, ppconst) == FAIL)
4175 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 if (r == FAIL)
4179 return FAIL;
4180 }
4181
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004182 // Handle following "[]", ".member", etc.
4183 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004184 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004185 ppconst) == FAIL)
4186 return FAIL;
4187 if (ppconst->pp_used > 0)
4188 {
4189 // apply the '!', '-' and '+' before the constant
4190 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4191 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4192 return FAIL;
4193 return OK;
4194 }
4195 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004197 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198}
4199
4200/*
4201 * * number multiplication
4202 * / number division
4203 * % number modulo
4204 */
4205 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004206compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207{
4208 char_u *op;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004209 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004211 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004212 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 return FAIL;
4214
4215 /*
4216 * Repeat computing, until no "*", "/" or "%" is following.
4217 */
4218 for (;;)
4219 {
4220 op = skipwhite(*arg);
4221 if (*op != '*' && *op != '/' && *op != '%')
4222 break;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004223
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004224 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 {
4226 char_u buf[3];
4227
4228 vim_strncpy(buf, op, 1);
4229 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004230 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 }
4232 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004233 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004234 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004236 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004237 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004239
4240 if (ppconst->pp_used == ppconst_used + 2
4241 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4242 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004243 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004244 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4245 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004246 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004248 // both are numbers: compute the result
4249 switch (*op)
4250 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004251 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004252 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004253 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004254 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004255 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004256 break;
4257 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004258 tv1->vval.v_number = res;
4259 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004260 }
4261 else
4262 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004263 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004264 generate_two_op(cctx, op);
4265 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 }
4267
4268 return OK;
4269}
4270
4271/*
4272 * + number addition
4273 * - number subtraction
4274 * .. string concatenation
4275 */
4276 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004277compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278{
4279 char_u *op;
4280 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004281 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282
4283 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004284 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 return FAIL;
4286
4287 /*
4288 * Repeat computing, until no "+", "-" or ".." is following.
4289 */
4290 for (;;)
4291 {
4292 op = skipwhite(*arg);
4293 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
4294 break;
4295 oplen = (*op == '.' ? 2 : 1);
4296
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004297 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 {
4299 char_u buf[3];
4300
4301 vim_strncpy(buf, op, oplen);
4302 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004303 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 }
4305
4306 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004307 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004308 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004310 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004311 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 return FAIL;
4313
Bram Moolenaara5565e42020-05-09 15:44:01 +02004314 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004315 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4317 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4318 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4319 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004321 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4322 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004323
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004324 // concat/subtract/add constant numbers
4325 if (*op == '+')
4326 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4327 else if (*op == '-')
4328 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4329 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004330 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004331 // concatenate constant strings
4332 char_u *s1 = tv1->vval.v_string;
4333 char_u *s2 = tv2->vval.v_string;
4334 size_t len1 = STRLEN(s1);
4335
4336 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4337 if (tv1->vval.v_string == NULL)
4338 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004339 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004340 return FAIL;
4341 }
4342 mch_memmove(tv1->vval.v_string, s1, len1);
4343 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004344 vim_free(s1);
4345 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004346 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004347 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 }
4349 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004350 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004351 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004352 if (*op == '.')
4353 {
4354 if (may_generate_2STRING(-2, cctx) == FAIL
4355 || may_generate_2STRING(-1, cctx) == FAIL)
4356 return FAIL;
4357 generate_instr_drop(cctx, ISN_CONCAT, 1);
4358 }
4359 else
4360 generate_two_op(cctx, op);
4361 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 }
4363
4364 return OK;
4365}
4366
4367/*
4368 * expr5a == expr5b
4369 * expr5a =~ expr5b
4370 * expr5a != expr5b
4371 * expr5a !~ expr5b
4372 * expr5a > expr5b
4373 * expr5a >= expr5b
4374 * expr5a < expr5b
4375 * expr5a <= expr5b
4376 * expr5a is expr5b
4377 * expr5a isnot expr5b
4378 *
4379 * Produces instructions:
4380 * EVAL expr5a Push result of "expr5a"
4381 * EVAL expr5b Push result of "expr5b"
4382 * COMPARE one of the compare instructions
4383 */
4384 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004385compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386{
4387 exptype_T type = EXPR_UNKNOWN;
4388 char_u *p;
4389 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004391 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392
4393 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004394 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 return FAIL;
4396
4397 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004398 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399
4400 /*
4401 * If there is a comparative operator, use it.
4402 */
4403 if (type != EXPR_UNKNOWN)
4404 {
4405 int ic = FALSE; // Default: do not ignore case
4406
4407 if (type_is && (p[len] == '?' || p[len] == '#'))
4408 {
4409 semsg(_(e_invexpr2), *arg);
4410 return FAIL;
4411 }
4412 // extra question mark appended: ignore case
4413 if (p[len] == '?')
4414 {
4415 ic = TRUE;
4416 ++len;
4417 }
4418 // extra '#' appended: match case (ignored)
4419 else if (p[len] == '#')
4420 ++len;
4421 // nothing appended: match case
4422
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004423 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 {
4425 char_u buf[7];
4426
4427 vim_strncpy(buf, p, len);
4428 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004429 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 }
4431
4432 // get the second variable
4433 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004434 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004435 return FAIL;
4436
Bram Moolenaara5565e42020-05-09 15:44:01 +02004437 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 return FAIL;
4439
Bram Moolenaara5565e42020-05-09 15:44:01 +02004440 if (ppconst->pp_used == ppconst_used + 2)
4441 {
4442 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4443 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4444 int ret;
4445
4446 // Both sides are a constant, compute the result now.
4447 // First check for a valid combination of types, this is more
4448 // strict than typval_compare().
4449 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4450 ret = FAIL;
4451 else
4452 {
4453 ret = typval_compare(tv1, tv2, type, ic);
4454 tv1->v_type = VAR_BOOL;
4455 tv1->vval.v_number = tv1->vval.v_number
4456 ? VVAL_TRUE : VVAL_FALSE;
4457 clear_tv(tv2);
4458 --ppconst->pp_used;
4459 }
4460 return ret;
4461 }
4462
4463 generate_ppconst(cctx, ppconst);
4464 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 }
4466
4467 return OK;
4468}
4469
4470/*
4471 * Compile || or &&.
4472 */
4473 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004474compile_and_or(
4475 char_u **arg,
4476 cctx_T *cctx,
4477 char *op,
4478 ppconst_T *ppconst,
4479 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480{
4481 char_u *p = skipwhite(*arg);
4482 int opchar = *op;
4483
4484 if (p[0] == opchar && p[1] == opchar)
4485 {
4486 garray_T *instr = &cctx->ctx_instr;
4487 garray_T end_ga;
4488
4489 /*
4490 * Repeat until there is no following "||" or "&&"
4491 */
4492 ga_init2(&end_ga, sizeof(int), 10);
4493 while (p[0] == opchar && p[1] == opchar)
4494 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004495 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4496 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004498 return FAIL;
4499 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500
Bram Moolenaara5565e42020-05-09 15:44:01 +02004501 // TODO: use ppconst if the value is a constant
4502 generate_ppconst(cctx, ppconst);
4503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504 if (ga_grow(&end_ga, 1) == FAIL)
4505 {
4506 ga_clear(&end_ga);
4507 return FAIL;
4508 }
4509 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4510 ++end_ga.ga_len;
4511 generate_JUMP(cctx, opchar == '|'
4512 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4513
4514 // eval the next expression
4515 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004516 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004517 return FAIL;
4518
Bram Moolenaara5565e42020-05-09 15:44:01 +02004519 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4520 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 {
4522 ga_clear(&end_ga);
4523 return FAIL;
4524 }
4525 p = skipwhite(*arg);
4526 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004527 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528
4529 // Fill in the end label in all jumps.
4530 while (end_ga.ga_len > 0)
4531 {
4532 isn_T *isn;
4533
4534 --end_ga.ga_len;
4535 isn = ((isn_T *)instr->ga_data)
4536 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4537 isn->isn_arg.jump.jump_where = instr->ga_len;
4538 }
4539 ga_clear(&end_ga);
4540 }
4541
4542 return OK;
4543}
4544
4545/*
4546 * expr4a && expr4a && expr4a logical AND
4547 *
4548 * Produces instructions:
4549 * EVAL expr4a Push result of "expr4a"
4550 * JUMP_AND_KEEP_IF_FALSE end
4551 * EVAL expr4b Push result of "expr4b"
4552 * JUMP_AND_KEEP_IF_FALSE end
4553 * EVAL expr4c Push result of "expr4c"
4554 * end:
4555 */
4556 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004557compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004559 int ppconst_used = ppconst->pp_used;
4560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004562 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 return FAIL;
4564
4565 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004566 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567}
4568
4569/*
4570 * expr3a || expr3b || expr3c logical OR
4571 *
4572 * Produces instructions:
4573 * EVAL expr3a Push result of "expr3a"
4574 * JUMP_AND_KEEP_IF_TRUE end
4575 * EVAL expr3b Push result of "expr3b"
4576 * JUMP_AND_KEEP_IF_TRUE end
4577 * EVAL expr3c Push result of "expr3c"
4578 * end:
4579 */
4580 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004581compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004583 int ppconst_used = ppconst->pp_used;
4584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004586 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 return FAIL;
4588
4589 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004590 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591}
4592
4593/*
4594 * Toplevel expression: expr2 ? expr1a : expr1b
4595 *
4596 * Produces instructions:
4597 * EVAL expr2 Push result of "expr"
4598 * JUMP_IF_FALSE alt jump if false
4599 * EVAL expr1a
4600 * JUMP_ALWAYS end
4601 * alt: EVAL expr1b
4602 * end:
4603 */
4604 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004605compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606{
4607 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004608 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609
Bram Moolenaar61a89812020-05-07 16:58:17 +02004610 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004611 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 return FAIL;
4613
4614 p = skipwhite(*arg);
4615 if (*p == '?')
4616 {
4617 garray_T *instr = &cctx->ctx_instr;
4618 garray_T *stack = &cctx->ctx_type_stack;
4619 int alt_idx = instr->ga_len;
4620 int end_idx;
4621 isn_T *isn;
4622 type_T *type1;
4623 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004624 int has_const_expr = FALSE;
4625 int const_value = FALSE;
4626 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004628 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4629 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004631 return FAIL;
4632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633
Bram Moolenaara5565e42020-05-09 15:44:01 +02004634 if (ppconst->pp_used == ppconst_used + 1)
4635 {
4636 // the condition is a constant, we know whether the ? or the :
4637 // expression is to be evaluated.
4638 has_const_expr = TRUE;
4639 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4640 clear_tv(&ppconst->pp_tv[ppconst_used]);
4641 --ppconst->pp_used;
4642 cctx->ctx_skip = save_skip == TRUE || !const_value;
4643 }
4644 else
4645 {
4646 generate_ppconst(cctx, ppconst);
4647 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649
4650 // evaluate the second expression; any type is accepted
4651 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004652 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004653 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004654 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004655 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656
Bram Moolenaara5565e42020-05-09 15:44:01 +02004657 if (!has_const_expr)
4658 {
4659 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660
Bram Moolenaara5565e42020-05-09 15:44:01 +02004661 // remember the type and drop it
4662 --stack->ga_len;
4663 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664
Bram Moolenaara5565e42020-05-09 15:44:01 +02004665 end_idx = instr->ga_len;
4666 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4667
4668 // jump here from JUMP_IF_FALSE
4669 isn = ((isn_T *)instr->ga_data) + alt_idx;
4670 isn->isn_arg.jump.jump_where = instr->ga_len;
4671 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672
4673 // Check for the ":".
4674 p = skipwhite(*arg);
4675 if (*p != ':')
4676 {
4677 emsg(_(e_missing_colon));
4678 return FAIL;
4679 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004680 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4681 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004683 return FAIL;
4684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685
4686 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004687 if (has_const_expr)
4688 cctx->ctx_skip = save_skip == TRUE || const_value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004690 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004691 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004692 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004693 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694
Bram Moolenaara5565e42020-05-09 15:44:01 +02004695 if (!has_const_expr)
4696 {
4697 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698
Bram Moolenaara5565e42020-05-09 15:44:01 +02004699 // If the types differ, the result has a more generic type.
4700 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4701 common_type(type1, type2, &type2, cctx->ctx_type_list);
4702
4703 // jump here from JUMP_ALWAYS
4704 isn = ((isn_T *)instr->ga_data) + end_idx;
4705 isn->isn_arg.jump.jump_where = instr->ga_len;
4706 }
4707
4708 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 }
4710 return OK;
4711}
4712
4713/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004714 * Toplevel expression.
4715 */
4716 static int
4717compile_expr0(char_u **arg, cctx_T *cctx)
4718{
4719 ppconst_T ppconst;
4720
4721 CLEAR_FIELD(ppconst);
4722 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4723 {
4724 clear_ppconst(&ppconst);
4725 return FAIL;
4726 }
4727 if (generate_ppconst(cctx, &ppconst) == FAIL)
4728 return FAIL;
4729 return OK;
4730}
4731
4732/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733 * compile "return [expr]"
4734 */
4735 static char_u *
4736compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4737{
4738 char_u *p = arg;
4739 garray_T *stack = &cctx->ctx_type_stack;
4740 type_T *stack_type;
4741
4742 if (*p != NUL && *p != '|' && *p != '\n')
4743 {
4744 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004745 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 return NULL;
4747
4748 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4749 if (set_return_type)
4750 cctx->ctx_ufunc->uf_ret_type = stack_type;
4751 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4752 == FAIL)
4753 return NULL;
4754 }
4755 else
4756 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004757 // "set_return_type" cannot be TRUE, only used for a lambda which
4758 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004759 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4760 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 {
4762 emsg(_("E1003: Missing return value"));
4763 return NULL;
4764 }
4765
4766 // No argument, return zero.
4767 generate_PUSHNR(cctx, 0);
4768 }
4769
4770 if (generate_instr(cctx, ISN_RETURN) == NULL)
4771 return NULL;
4772
4773 // "return val | endif" is possible
4774 return skipwhite(p);
4775}
4776
4777/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004778 * Get a line from the compilation context, compatible with exarg_T getline().
4779 * Return a pointer to the line in allocated memory.
4780 * Return NULL for end-of-file or some error.
4781 */
4782 static char_u *
4783exarg_getline(
4784 int c UNUSED,
4785 void *cookie,
4786 int indent UNUSED,
4787 int do_concat UNUSED)
4788{
4789 cctx_T *cctx = (cctx_T *)cookie;
4790
4791 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4792 {
4793 iemsg("Heredoc got to end");
4794 return NULL;
4795 }
4796 ++cctx->ctx_lnum;
4797 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4798 [cctx->ctx_lnum]);
4799}
4800
4801/*
4802 * Compile a nested :def command.
4803 */
4804 static char_u *
4805compile_nested_function(exarg_T *eap, cctx_T *cctx)
4806{
4807 char_u *name_start = eap->arg;
4808 char_u *name_end = to_name_end(eap->arg, FALSE);
4809 char_u *name = get_lambda_name();
4810 lvar_T *lvar;
4811 ufunc_T *ufunc;
4812
4813 eap->arg = name_end;
4814 eap->getline = exarg_getline;
4815 eap->cookie = cctx;
4816 eap->skip = cctx->ctx_skip == TRUE;
4817 eap->forceit = FALSE;
4818 ufunc = def_function(eap, name, cctx);
4819
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004820 if (ufunc == NULL || ufunc->uf_dfunc_idx < 0)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004821 return NULL;
4822
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004823 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004824 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004825 TRUE, ufunc->uf_func_type);
4826
4827 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4828 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4829 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004830
Bram Moolenaar61a89812020-05-07 16:58:17 +02004831 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004832 return (char_u *)"";
4833}
4834
4835/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836 * Return the length of an assignment operator, or zero if there isn't one.
4837 */
4838 int
4839assignment_len(char_u *p, int *heredoc)
4840{
4841 if (*p == '=')
4842 {
4843 if (p[1] == '<' && p[2] == '<')
4844 {
4845 *heredoc = TRUE;
4846 return 3;
4847 }
4848 return 1;
4849 }
4850 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4851 return 2;
4852 if (STRNCMP(p, "..=", 3) == 0)
4853 return 3;
4854 return 0;
4855}
4856
4857// words that cannot be used as a variable
4858static char *reserved[] = {
4859 "true",
4860 "false",
4861 NULL
4862};
4863
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004864typedef enum {
4865 dest_local,
4866 dest_option,
4867 dest_env,
4868 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004869 dest_buffer,
4870 dest_window,
4871 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004872 dest_vimvar,
4873 dest_script,
4874 dest_reg,
4875} assign_dest_T;
4876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877/*
4878 * compile "let var [= expr]", "const var = expr" and "var = expr"
4879 * "arg" points to "var".
4880 */
4881 static char_u *
4882compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4883{
4884 char_u *p;
4885 char_u *ret = NULL;
4886 int var_count = 0;
4887 int semicolon = 0;
4888 size_t varlen;
4889 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004890 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004893 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004895 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 int oplen = 0;
4897 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004898 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004899 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 char_u *name;
4901 char_u *sp;
4902 int has_type = FALSE;
4903 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4904 int instr_count = -1;
4905
4906 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4907 if (p == NULL)
4908 return NULL;
4909 if (var_count > 0)
4910 {
4911 // TODO: let [var, var] = list
4912 emsg("Cannot handle a list yet");
4913 return NULL;
4914 }
4915
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004916 // "a: type" is declaring variable "a" with a type, not "a:".
4917 if (is_decl && p == arg + 2 && p[-1] == ':')
4918 --p;
4919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 varlen = p - arg;
4921 name = vim_strnsave(arg, (int)varlen);
4922 if (name == NULL)
4923 return NULL;
4924
Bram Moolenaar080457c2020-03-03 21:53:32 +01004925 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004927 if (*arg == '&')
4928 {
4929 int cc;
4930 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931
Bram Moolenaar080457c2020-03-03 21:53:32 +01004932 dest = dest_option;
4933 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004935 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004936 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 if (is_decl)
4939 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004940 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 goto theend;
4942 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004943 p = arg;
4944 p = find_option_end(&p, &opt_flags);
4945 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004947 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004948 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004949 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004950 }
4951 cc = *p;
4952 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004953 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004954 *p = cc;
4955 if (opt_type == -3)
4956 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004957 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004958 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004959 }
4960 if (opt_type == -2 || opt_type == 0)
4961 type = &t_string;
4962 else
4963 type = &t_number; // both number and boolean option
4964 }
4965 else if (*arg == '$')
4966 {
4967 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004968 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004969 if (is_decl)
4970 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004971 semsg(_("E1065: Cannot declare an environment variable: %s"),
4972 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004973 goto theend;
4974 }
4975 }
4976 else if (*arg == '@')
4977 {
4978 if (!valid_yank_reg(arg[1], TRUE))
4979 {
4980 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004981 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004982 }
4983 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004984 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004985 if (is_decl)
4986 {
4987 semsg(_("E1066: Cannot declare a register: %s"), name);
4988 goto theend;
4989 }
4990 }
4991 else if (STRNCMP(arg, "g:", 2) == 0)
4992 {
4993 dest = dest_global;
4994 if (is_decl)
4995 {
4996 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4997 goto theend;
4998 }
4999 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02005000 else if (STRNCMP(arg, "b:", 2) == 0)
5001 {
5002 dest = dest_buffer;
5003 if (is_decl)
5004 {
5005 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
5006 goto theend;
5007 }
5008 }
5009 else if (STRNCMP(arg, "w:", 2) == 0)
5010 {
5011 dest = dest_window;
5012 if (is_decl)
5013 {
5014 semsg(_("E1079: Cannot declare a window variable: %s"), name);
5015 goto theend;
5016 }
5017 }
5018 else if (STRNCMP(arg, "t:", 2) == 0)
5019 {
5020 dest = dest_tab;
5021 if (is_decl)
5022 {
5023 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
5024 goto theend;
5025 }
5026 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01005027 else if (STRNCMP(arg, "v:", 2) == 0)
5028 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02005029 typval_T *vtv;
5030 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005031
Bram Moolenaar5da356e2020-04-09 19:34:43 +02005032 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005033 if (vimvaridx < 0)
5034 {
5035 semsg(_(e_var_notfound), arg);
5036 goto theend;
5037 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02005038 // We use the current value of "sandbox" here, is that OK?
5039 if (var_check_ro(di_flags, name, FALSE))
5040 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01005041 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005042 vtv = get_vim_var_tv(vimvaridx);
5043 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005044 if (is_decl)
5045 {
5046 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
5047 goto theend;
5048 }
5049 }
5050 else
5051 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005052 int idx;
5053
Bram Moolenaar080457c2020-03-03 21:53:32 +01005054 for (idx = 0; reserved[idx] != NULL; ++idx)
5055 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01005057 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 goto theend;
5059 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01005060
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005061 lvar = lookup_local(arg, varlen, cctx);
5062 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01005064 if (is_decl)
5065 {
5066 semsg(_("E1017: Variable already declared: %s"), name);
5067 goto theend;
5068 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02005069 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01005070 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02005071 semsg(_("E1018: Cannot assign to a constant: %s"), name);
5072 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01005073 }
5074 }
5075 else if (STRNCMP(arg, "s:", 2) == 0
5076 || lookup_script(arg, varlen) == OK
5077 || find_imported(arg, varlen, cctx) != NULL)
5078 {
5079 dest = dest_script;
5080 if (is_decl)
5081 {
5082 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005083 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01005084 goto theend;
5085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005087 else if (name[1] == ':' && name[2] != NUL)
5088 {
5089 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
5090 goto theend;
5091 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092 }
5093 }
5094
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005095 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 {
5097 if (is_decl && *p == ':')
5098 {
5099 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005100 if (!VIM_ISWHITE(p[1]))
5101 {
5102 semsg(_(e_white_after), ":");
5103 goto theend;
5104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 p = skipwhite(p + 1);
5106 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 has_type = TRUE;
5108 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005109 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 }
5112
5113 sp = p;
5114 p = skipwhite(p);
5115 op = p;
5116 oplen = assignment_len(p, &heredoc);
5117 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5118 {
5119 char_u buf[4];
5120
5121 vim_strncpy(buf, op, oplen);
5122 semsg(_(e_white_both), buf);
5123 }
5124
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005125 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02005126 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005128 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129 goto theend;
5130 }
5131
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005132 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 {
5134 if (oplen > 1 && !heredoc)
5135 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005136 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5138 name);
5139 goto theend;
5140 }
5141
5142 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02005143 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005144 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005145 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
5146 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02005148 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149 }
5150
5151 if (heredoc)
5152 {
5153 list_T *l;
5154 listitem_T *li;
5155
5156 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005157 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005158 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005159 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005160
5161 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005162 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163 {
5164 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5165 li->li_tv.vval.v_string = NULL;
5166 }
5167 generate_NEWLIST(cctx, l->lv_len);
5168 type = &t_list_string;
5169 list_free(l);
5170 p += STRLEN(p);
5171 }
5172 else if (oplen > 0)
5173 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02005174 int r;
5175 type_T *stacktype;
5176 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178 // for "+=", "*=", "..=" etc. first load the current value
5179 if (*op != '=')
5180 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005181 switch (dest)
5182 {
5183 case dest_option:
5184 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02005185 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005186 break;
5187 case dest_global:
5188 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5189 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02005190 case dest_buffer:
5191 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5192 break;
5193 case dest_window:
5194 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5195 break;
5196 case dest_tab:
5197 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5198 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005199 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01005200 compile_load_scriptvar(cctx,
5201 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005202 break;
5203 case dest_env:
5204 // Include $ in the name here
5205 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5206 break;
5207 case dest_reg:
5208 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
5209 break;
5210 case dest_vimvar:
5211 generate_LOADV(cctx, name + 2, TRUE);
5212 break;
5213 case dest_local:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005214 if (lvar->lv_from_outer)
5215 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5216 NULL, type);
5217 else
5218 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005219 break;
5220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 }
5222
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005223 // Compile the expression. Temporarily hide the new local variable
5224 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02005225 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005226 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227 instr_count = instr->ga_len;
5228 p = skipwhite(p + oplen);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005229 r = compile_expr0(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02005230 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005231 ++cctx->ctx_locals.ga_len;
5232 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 goto theend;
5234
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005235 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005237 stack = &cctx->ctx_type_stack;
5238 stacktype = stack->ga_len == 0 ? &t_void
5239 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005240 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005242 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005244 if (stacktype->tt_type == VAR_VOID)
5245 {
5246 emsg(_("E1031: Cannot use void value"));
5247 goto theend;
5248 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005249 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005250 {
5251 // An empty list or dict has a &t_void member, for a
5252 // variable that implies &t_any.
5253 if (stacktype == &t_list_empty)
5254 lvar->lv_type = &t_list_any;
5255 else if (stacktype == &t_dict_empty)
5256 lvar->lv_type = &t_dict_any;
5257 else
5258 lvar->lv_type = stacktype;
5259 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005260 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005261 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
5262 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005264 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005265 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005266 }
5267 }
5268 else if (cmdidx == CMD_const)
5269 {
5270 emsg(_("E1021: const requires a value"));
5271 goto theend;
5272 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005273 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005274 {
5275 emsg(_("E1022: type or initialization required"));
5276 goto theend;
5277 }
5278 else
5279 {
5280 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005281 if (ga_grow(instr, 1) == FAIL)
5282 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005283 switch (type->tt_type)
5284 {
5285 case VAR_BOOL:
5286 generate_PUSHBOOL(cctx, VVAL_FALSE);
5287 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005288 case VAR_FLOAT:
5289#ifdef FEAT_FLOAT
5290 generate_PUSHF(cctx, 0.0);
5291#endif
5292 break;
5293 case VAR_STRING:
5294 generate_PUSHS(cctx, NULL);
5295 break;
5296 case VAR_BLOB:
5297 generate_PUSHBLOB(cctx, NULL);
5298 break;
5299 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005300 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005301 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005302 case VAR_LIST:
5303 generate_NEWLIST(cctx, 0);
5304 break;
5305 case VAR_DICT:
5306 generate_NEWDICT(cctx, 0);
5307 break;
5308 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005309 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005310 break;
5311 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005312 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005313 break;
5314 case VAR_NUMBER:
5315 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005316 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02005317 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01005318 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02005319 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01005320 generate_PUSHNR(cctx, 0);
5321 break;
5322 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005323 }
5324
5325 if (oplen > 0 && *op != '=')
5326 {
5327 type_T *expected = &t_number;
5328 garray_T *stack = &cctx->ctx_type_stack;
5329 type_T *stacktype;
5330
5331 // TODO: if type is known use float or any operation
5332
5333 if (*op == '.')
5334 expected = &t_string;
5335 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5336 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5337 goto theend;
5338
5339 if (*op == '.')
5340 generate_instr_drop(cctx, ISN_CONCAT, 1);
5341 else
5342 {
5343 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5344
5345 if (isn == NULL)
5346 goto theend;
5347 switch (*op)
5348 {
5349 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5350 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5351 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5352 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5353 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5354 }
5355 }
5356 }
5357
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005358 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005359 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005360 case dest_option:
5361 generate_STOREOPT(cctx, name + 1, opt_flags);
5362 break;
5363 case dest_global:
5364 // include g: with the name, easier to execute that way
5365 generate_STORE(cctx, ISN_STOREG, 0, name);
5366 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02005367 case dest_buffer:
5368 // include b: with the name, easier to execute that way
5369 generate_STORE(cctx, ISN_STOREB, 0, name);
5370 break;
5371 case dest_window:
5372 // include w: with the name, easier to execute that way
5373 generate_STORE(cctx, ISN_STOREW, 0, name);
5374 break;
5375 case dest_tab:
5376 // include t: with the name, easier to execute that way
5377 generate_STORE(cctx, ISN_STORET, 0, name);
5378 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005379 case dest_env:
5380 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5381 break;
5382 case dest_reg:
5383 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5384 break;
5385 case dest_vimvar:
5386 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5387 break;
5388 case dest_script:
5389 {
5390 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5391 imported_T *import = NULL;
5392 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005393 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005394
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005395 if (name[1] != ':')
5396 {
5397 import = find_imported(name, 0, cctx);
5398 if (import != NULL)
5399 sid = import->imp_sid;
5400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005401
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005402 idx = get_script_item_idx(sid, rawname, TRUE);
5403 // TODO: specific type
5404 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005405 {
5406 char_u *name_s = name;
5407
5408 // Include s: in the name for store_var()
5409 if (name[1] != ':')
5410 {
5411 int len = (int)STRLEN(name) + 3;
5412
5413 name_s = alloc(len);
5414 if (name_s == NULL)
5415 name_s = name;
5416 else
5417 vim_snprintf((char *)name_s, len, "s:%s", name);
5418 }
5419 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
5420 if (name_s != name)
5421 vim_free(name_s);
5422 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005423 else
5424 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5425 sid, idx, &t_any);
5426 }
5427 break;
5428 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005429 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005430 {
5431 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005432
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005433 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
5434 // into ISN_STORENR
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005435 if (!lvar->lv_from_outer && instr->ga_len == instr_count + 1
5436 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005437 {
5438 varnumber_T val = isn->isn_arg.number;
5439 garray_T *stack = &cctx->ctx_type_stack;
5440
5441 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005442 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01005443 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005444 if (stack->ga_len > 0)
5445 --stack->ga_len;
5446 }
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005447 else if (lvar->lv_from_outer)
5448 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005449 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005450 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005451 }
5452 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005453 }
5454 ret = p;
5455
5456theend:
5457 vim_free(name);
5458 return ret;
5459}
5460
5461/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005462 * Check if "name" can be "unlet".
5463 */
5464 int
5465check_vim9_unlet(char_u *name)
5466{
5467 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5468 {
5469 semsg(_("E1081: Cannot unlet %s"), name);
5470 return FAIL;
5471 }
5472 return OK;
5473}
5474
5475/*
5476 * Callback passed to ex_unletlock().
5477 */
5478 static int
5479compile_unlet(
5480 lval_T *lvp,
5481 char_u *name_end,
5482 exarg_T *eap,
5483 int deep UNUSED,
5484 void *coookie)
5485{
5486 cctx_T *cctx = coookie;
5487
5488 if (lvp->ll_tv == NULL)
5489 {
5490 char_u *p = lvp->ll_name;
5491 int cc = *name_end;
5492 int ret = OK;
5493
5494 // Normal name. Only supports g:, w:, t: and b: namespaces.
5495 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005496 if (*p == '$')
5497 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5498 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005499 ret = FAIL;
5500 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005501 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005502
5503 *name_end = cc;
5504 return ret;
5505 }
5506
5507 // TODO: unlet {list}[idx]
5508 // TODO: unlet {dict}[key]
5509 emsg("Sorry, :unlet not fully implemented yet");
5510 return FAIL;
5511}
5512
5513/*
5514 * compile "unlet var", "lock var" and "unlock var"
5515 * "arg" points to "var".
5516 */
5517 static char_u *
5518compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5519{
5520 char_u *p = arg;
5521
5522 if (eap->cmdidx != CMD_unlet)
5523 {
5524 emsg("Sorry, :lock and unlock not implemented yet");
5525 return NULL;
5526 }
5527
5528 if (*p == '!')
5529 {
5530 p = skipwhite(p + 1);
5531 eap->forceit = TRUE;
5532 }
5533
5534 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5535 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5536}
5537
5538/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005539 * Compile an :import command.
5540 */
5541 static char_u *
5542compile_import(char_u *arg, cctx_T *cctx)
5543{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005544 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005545}
5546
5547/*
5548 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5549 */
5550 static int
5551compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5552{
5553 garray_T *instr = &cctx->ctx_instr;
5554 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5555
5556 if (endlabel == NULL)
5557 return FAIL;
5558 endlabel->el_next = *el;
5559 *el = endlabel;
5560 endlabel->el_end_label = instr->ga_len;
5561
5562 generate_JUMP(cctx, when, 0);
5563 return OK;
5564}
5565
5566 static void
5567compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5568{
5569 garray_T *instr = &cctx->ctx_instr;
5570
5571 while (*el != NULL)
5572 {
5573 endlabel_T *cur = (*el);
5574 isn_T *isn;
5575
5576 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5577 isn->isn_arg.jump.jump_where = instr->ga_len;
5578 *el = cur->el_next;
5579 vim_free(cur);
5580 }
5581}
5582
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005583 static void
5584compile_free_jump_to_end(endlabel_T **el)
5585{
5586 while (*el != NULL)
5587 {
5588 endlabel_T *cur = (*el);
5589
5590 *el = cur->el_next;
5591 vim_free(cur);
5592 }
5593}
5594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005595/*
5596 * Create a new scope and set up the generic items.
5597 */
5598 static scope_T *
5599new_scope(cctx_T *cctx, scopetype_T type)
5600{
5601 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5602
5603 if (scope == NULL)
5604 return NULL;
5605 scope->se_outer = cctx->ctx_scope;
5606 cctx->ctx_scope = scope;
5607 scope->se_type = type;
5608 scope->se_local_count = cctx->ctx_locals.ga_len;
5609 return scope;
5610}
5611
5612/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005613 * Free the current scope and go back to the outer scope.
5614 */
5615 static void
5616drop_scope(cctx_T *cctx)
5617{
5618 scope_T *scope = cctx->ctx_scope;
5619
5620 if (scope == NULL)
5621 {
5622 iemsg("calling drop_scope() without a scope");
5623 return;
5624 }
5625 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005626 switch (scope->se_type)
5627 {
5628 case IF_SCOPE:
5629 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5630 case FOR_SCOPE:
5631 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5632 case WHILE_SCOPE:
5633 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5634 case TRY_SCOPE:
5635 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5636 case NO_SCOPE:
5637 case BLOCK_SCOPE:
5638 break;
5639 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005640 vim_free(scope);
5641}
5642
5643/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644 * compile "if expr"
5645 *
5646 * "if expr" Produces instructions:
5647 * EVAL expr Push result of "expr"
5648 * JUMP_IF_FALSE end
5649 * ... body ...
5650 * end:
5651 *
5652 * "if expr | else" Produces instructions:
5653 * EVAL expr Push result of "expr"
5654 * JUMP_IF_FALSE else
5655 * ... body ...
5656 * JUMP_ALWAYS end
5657 * else:
5658 * ... body ...
5659 * end:
5660 *
5661 * "if expr1 | elseif expr2 | else" Produces instructions:
5662 * EVAL expr Push result of "expr"
5663 * JUMP_IF_FALSE elseif
5664 * ... body ...
5665 * JUMP_ALWAYS end
5666 * elseif:
5667 * EVAL expr Push result of "expr"
5668 * JUMP_IF_FALSE else
5669 * ... body ...
5670 * JUMP_ALWAYS end
5671 * else:
5672 * ... body ...
5673 * end:
5674 */
5675 static char_u *
5676compile_if(char_u *arg, cctx_T *cctx)
5677{
5678 char_u *p = arg;
5679 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005680 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681 scope_T *scope;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005682 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005683
Bram Moolenaara5565e42020-05-09 15:44:01 +02005684 CLEAR_FIELD(ppconst);
5685 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005686 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005687 clear_ppconst(&ppconst);
5688 return NULL;
5689 }
5690 if (instr->ga_len == instr_count && ppconst.pp_used == 1)
5691 {
5692 // The expression results in a constant.
5693 // TODO: how about nesting?
5694 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? FALSE : TRUE;
5695 clear_ppconst(&ppconst);
5696 }
5697 else
5698 {
5699 // Not a constant, generate instructions for the expression.
5700 cctx->ctx_skip = MAYBE;
5701 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005702 return NULL;
5703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005704
5705 scope = new_scope(cctx, IF_SCOPE);
5706 if (scope == NULL)
5707 return NULL;
5708
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005709 if (cctx->ctx_skip == MAYBE)
5710 {
5711 // "where" is set when ":elseif", "else" or ":endif" is found
5712 scope->se_u.se_if.is_if_label = instr->ga_len;
5713 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5714 }
5715 else
5716 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005717
5718 return p;
5719}
5720
5721 static char_u *
5722compile_elseif(char_u *arg, cctx_T *cctx)
5723{
5724 char_u *p = arg;
5725 garray_T *instr = &cctx->ctx_instr;
5726 isn_T *isn;
5727 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005728 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005729
5730 if (scope == NULL || scope->se_type != IF_SCOPE)
5731 {
5732 emsg(_(e_elseif_without_if));
5733 return NULL;
5734 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005735 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005736
Bram Moolenaar158906c2020-02-06 20:39:45 +01005737 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005738 {
5739 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005740 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005741 return NULL;
5742 // previous "if" or "elseif" jumps here
5743 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5744 isn->isn_arg.jump.jump_where = instr->ga_len;
5745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005747 // compile "expr"; if we know it evaluates to FALSE skip the block
5748 tv.v_type = VAR_UNKNOWN;
5749 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5750 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5751 else
5752 cctx->ctx_skip = MAYBE;
5753 clear_tv(&tv);
5754 if (cctx->ctx_skip == MAYBE)
5755 {
5756 p = arg;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005757 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005758 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005760 // "where" is set when ":elseif", "else" or ":endif" is found
5761 scope->se_u.se_if.is_if_label = instr->ga_len;
5762 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5763 }
5764 else
5765 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005766
5767 return p;
5768}
5769
5770 static char_u *
5771compile_else(char_u *arg, cctx_T *cctx)
5772{
5773 char_u *p = arg;
5774 garray_T *instr = &cctx->ctx_instr;
5775 isn_T *isn;
5776 scope_T *scope = cctx->ctx_scope;
5777
5778 if (scope == NULL || scope->se_type != IF_SCOPE)
5779 {
5780 emsg(_(e_else_without_if));
5781 return NULL;
5782 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005783 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005784
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005785 // jump from previous block to the end, unless the else block is empty
5786 if (cctx->ctx_skip == MAYBE)
5787 {
5788 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005789 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005790 return NULL;
5791 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005792
Bram Moolenaar158906c2020-02-06 20:39:45 +01005793 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005794 {
5795 if (scope->se_u.se_if.is_if_label >= 0)
5796 {
5797 // previous "if" or "elseif" jumps here
5798 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5799 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005800 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005801 }
5802 }
5803
5804 if (cctx->ctx_skip != MAYBE)
5805 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005806
5807 return p;
5808}
5809
5810 static char_u *
5811compile_endif(char_u *arg, cctx_T *cctx)
5812{
5813 scope_T *scope = cctx->ctx_scope;
5814 ifscope_T *ifscope;
5815 garray_T *instr = &cctx->ctx_instr;
5816 isn_T *isn;
5817
5818 if (scope == NULL || scope->se_type != IF_SCOPE)
5819 {
5820 emsg(_(e_endif_without_if));
5821 return NULL;
5822 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005823 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005824 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005825
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005826 if (scope->se_u.se_if.is_if_label >= 0)
5827 {
5828 // previous "if" or "elseif" jumps here
5829 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5830 isn->isn_arg.jump.jump_where = instr->ga_len;
5831 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005832 // Fill in the "end" label in jumps at the end of the blocks.
5833 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005834 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005835
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005836 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005837 return arg;
5838}
5839
5840/*
5841 * compile "for var in expr"
5842 *
5843 * Produces instructions:
5844 * PUSHNR -1
5845 * STORE loop-idx Set index to -1
5846 * EVAL expr Push result of "expr"
5847 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5848 * - if beyond end, jump to "end"
5849 * - otherwise get item from list and push it
5850 * STORE var Store item in "var"
5851 * ... body ...
5852 * JUMP top Jump back to repeat
5853 * end: DROP Drop the result of "expr"
5854 *
5855 */
5856 static char_u *
5857compile_for(char_u *arg, cctx_T *cctx)
5858{
5859 char_u *p;
5860 size_t varlen;
5861 garray_T *instr = &cctx->ctx_instr;
5862 garray_T *stack = &cctx->ctx_type_stack;
5863 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005864 lvar_T *loop_lvar; // loop iteration variable
5865 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866 type_T *vartype;
5867
5868 // TODO: list of variables: "for [key, value] in dict"
5869 // parse "var"
5870 for (p = arg; eval_isnamec1(*p); ++p)
5871 ;
5872 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005873 var_lvar = lookup_local(arg, varlen, cctx);
5874 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005875 {
5876 semsg(_("E1023: variable already defined: %s"), arg);
5877 return NULL;
5878 }
5879
5880 // consume "in"
5881 p = skipwhite(p);
5882 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5883 {
5884 emsg(_(e_missing_in));
5885 return NULL;
5886 }
5887 p = skipwhite(p + 2);
5888
5889
5890 scope = new_scope(cctx, FOR_SCOPE);
5891 if (scope == NULL)
5892 return NULL;
5893
5894 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005895 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5896 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005897 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005898 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005899 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005900 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902
5903 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005904 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5905 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005906 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005907 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005908 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005909 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005910 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005911
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005912 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913
5914 // compile "expr", it remains on the stack until "endfor"
5915 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005916 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005917 {
5918 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005919 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005921
5922 // now we know the type of "var"
5923 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5924 if (vartype->tt_type != VAR_LIST)
5925 {
5926 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005927 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005928 return NULL;
5929 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005930 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005931 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005932
5933 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005934 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005935
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005936 generate_FOR(cctx, loop_lvar->lv_idx);
5937 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005938
5939 return arg;
5940}
5941
5942/*
5943 * compile "endfor"
5944 */
5945 static char_u *
5946compile_endfor(char_u *arg, cctx_T *cctx)
5947{
5948 garray_T *instr = &cctx->ctx_instr;
5949 scope_T *scope = cctx->ctx_scope;
5950 forscope_T *forscope;
5951 isn_T *isn;
5952
5953 if (scope == NULL || scope->se_type != FOR_SCOPE)
5954 {
5955 emsg(_(e_for));
5956 return NULL;
5957 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005958 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005959 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005960 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961
5962 // At end of ":for" scope jump back to the FOR instruction.
5963 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5964
5965 // Fill in the "end" label in the FOR statement so it can jump here
5966 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5967 isn->isn_arg.forloop.for_end = instr->ga_len;
5968
5969 // Fill in the "end" label any BREAK statements
5970 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5971
5972 // Below the ":for" scope drop the "expr" list from the stack.
5973 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5974 return NULL;
5975
5976 vim_free(scope);
5977
5978 return arg;
5979}
5980
5981/*
5982 * compile "while expr"
5983 *
5984 * Produces instructions:
5985 * top: EVAL expr Push result of "expr"
5986 * JUMP_IF_FALSE end jump if false
5987 * ... body ...
5988 * JUMP top Jump back to repeat
5989 * end:
5990 *
5991 */
5992 static char_u *
5993compile_while(char_u *arg, cctx_T *cctx)
5994{
5995 char_u *p = arg;
5996 garray_T *instr = &cctx->ctx_instr;
5997 scope_T *scope;
5998
5999 scope = new_scope(cctx, WHILE_SCOPE);
6000 if (scope == NULL)
6001 return NULL;
6002
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006003 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006004
6005 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006006 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007 return NULL;
6008
6009 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006010 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011 JUMP_IF_FALSE, cctx) == FAIL)
6012 return FAIL;
6013
6014 return p;
6015}
6016
6017/*
6018 * compile "endwhile"
6019 */
6020 static char_u *
6021compile_endwhile(char_u *arg, cctx_T *cctx)
6022{
6023 scope_T *scope = cctx->ctx_scope;
6024
6025 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6026 {
6027 emsg(_(e_while));
6028 return NULL;
6029 }
6030 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006031 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006032
6033 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006034 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035
6036 // Fill in the "end" label in the WHILE statement so it can jump here.
6037 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006038 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006039
6040 vim_free(scope);
6041
6042 return arg;
6043}
6044
6045/*
6046 * compile "continue"
6047 */
6048 static char_u *
6049compile_continue(char_u *arg, cctx_T *cctx)
6050{
6051 scope_T *scope = cctx->ctx_scope;
6052
6053 for (;;)
6054 {
6055 if (scope == NULL)
6056 {
6057 emsg(_(e_continue));
6058 return NULL;
6059 }
6060 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6061 break;
6062 scope = scope->se_outer;
6063 }
6064
6065 // Jump back to the FOR or WHILE instruction.
6066 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006067 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6068 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006069 return arg;
6070}
6071
6072/*
6073 * compile "break"
6074 */
6075 static char_u *
6076compile_break(char_u *arg, cctx_T *cctx)
6077{
6078 scope_T *scope = cctx->ctx_scope;
6079 endlabel_T **el;
6080
6081 for (;;)
6082 {
6083 if (scope == NULL)
6084 {
6085 emsg(_(e_break));
6086 return NULL;
6087 }
6088 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6089 break;
6090 scope = scope->se_outer;
6091 }
6092
6093 // Jump to the end of the FOR or WHILE loop.
6094 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006095 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006097 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006098 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6099 return FAIL;
6100
6101 return arg;
6102}
6103
6104/*
6105 * compile "{" start of block
6106 */
6107 static char_u *
6108compile_block(char_u *arg, cctx_T *cctx)
6109{
6110 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6111 return NULL;
6112 return skipwhite(arg + 1);
6113}
6114
6115/*
6116 * compile end of block: drop one scope
6117 */
6118 static void
6119compile_endblock(cctx_T *cctx)
6120{
6121 scope_T *scope = cctx->ctx_scope;
6122
6123 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006124 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125 vim_free(scope);
6126}
6127
6128/*
6129 * compile "try"
6130 * Creates a new scope for the try-endtry, pointing to the first catch and
6131 * finally.
6132 * Creates another scope for the "try" block itself.
6133 * TRY instruction sets up exception handling at runtime.
6134 *
6135 * "try"
6136 * TRY -> catch1, -> finally push trystack entry
6137 * ... try block
6138 * "throw {exception}"
6139 * EVAL {exception}
6140 * THROW create exception
6141 * ... try block
6142 * " catch {expr}"
6143 * JUMP -> finally
6144 * catch1: PUSH exeception
6145 * EVAL {expr}
6146 * MATCH
6147 * JUMP nomatch -> catch2
6148 * CATCH remove exception
6149 * ... catch block
6150 * " catch"
6151 * JUMP -> finally
6152 * catch2: CATCH remove exception
6153 * ... catch block
6154 * " finally"
6155 * finally:
6156 * ... finally block
6157 * " endtry"
6158 * ENDTRY pop trystack entry, may rethrow
6159 */
6160 static char_u *
6161compile_try(char_u *arg, cctx_T *cctx)
6162{
6163 garray_T *instr = &cctx->ctx_instr;
6164 scope_T *try_scope;
6165 scope_T *scope;
6166
6167 // scope that holds the jumps that go to catch/finally/endtry
6168 try_scope = new_scope(cctx, TRY_SCOPE);
6169 if (try_scope == NULL)
6170 return NULL;
6171
6172 // "catch" is set when the first ":catch" is found.
6173 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006174 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006175 if (generate_instr(cctx, ISN_TRY) == NULL)
6176 return NULL;
6177
6178 // scope for the try block itself
6179 scope = new_scope(cctx, BLOCK_SCOPE);
6180 if (scope == NULL)
6181 return NULL;
6182
6183 return arg;
6184}
6185
6186/*
6187 * compile "catch {expr}"
6188 */
6189 static char_u *
6190compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6191{
6192 scope_T *scope = cctx->ctx_scope;
6193 garray_T *instr = &cctx->ctx_instr;
6194 char_u *p;
6195 isn_T *isn;
6196
6197 // end block scope from :try or :catch
6198 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6199 compile_endblock(cctx);
6200 scope = cctx->ctx_scope;
6201
6202 // Error if not in a :try scope
6203 if (scope == NULL || scope->se_type != TRY_SCOPE)
6204 {
6205 emsg(_(e_catch));
6206 return NULL;
6207 }
6208
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006209 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006210 {
6211 emsg(_("E1033: catch unreachable after catch-all"));
6212 return NULL;
6213 }
6214
6215 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006216 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217 JUMP_ALWAYS, cctx) == FAIL)
6218 return NULL;
6219
6220 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006221 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 if (isn->isn_arg.try.try_catch == 0)
6223 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006224 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006225 {
6226 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006227 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006228 isn->isn_arg.jump.jump_where = instr->ga_len;
6229 }
6230
6231 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006232 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006234 scope->se_u.se_try.ts_caught_all = TRUE;
6235 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236 }
6237 else
6238 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006239 char_u *end;
6240 char_u *pat;
6241 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006242 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006243 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006244
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006245 // Push v:exception, push {expr} and MATCH
6246 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6247
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006248 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006249 if (*end != *p)
6250 {
6251 semsg(_("E1067: Separator mismatch: %s"), p);
6252 vim_free(tofree);
6253 return FAIL;
6254 }
6255 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006256 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006257 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006258 len = (int)(end - tofree);
6259 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006260 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006261 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006262 if (pat == NULL)
6263 return FAIL;
6264 if (generate_PUSHS(cctx, pat) == FAIL)
6265 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6268 return NULL;
6269
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006270 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6272 return NULL;
6273 }
6274
6275 if (generate_instr(cctx, ISN_CATCH) == NULL)
6276 return NULL;
6277
6278 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6279 return NULL;
6280 return p;
6281}
6282
6283 static char_u *
6284compile_finally(char_u *arg, cctx_T *cctx)
6285{
6286 scope_T *scope = cctx->ctx_scope;
6287 garray_T *instr = &cctx->ctx_instr;
6288 isn_T *isn;
6289
6290 // end block scope from :try or :catch
6291 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6292 compile_endblock(cctx);
6293 scope = cctx->ctx_scope;
6294
6295 // Error if not in a :try scope
6296 if (scope == NULL || scope->se_type != TRY_SCOPE)
6297 {
6298 emsg(_(e_finally));
6299 return NULL;
6300 }
6301
6302 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006303 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 if (isn->isn_arg.try.try_finally != 0)
6305 {
6306 emsg(_(e_finally_dup));
6307 return NULL;
6308 }
6309
6310 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006311 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312
Bram Moolenaar585fea72020-04-02 22:33:21 +02006313 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006314 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 {
6316 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006317 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 isn->isn_arg.jump.jump_where = instr->ga_len;
6319 }
6320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006321 // TODO: set index in ts_finally_label jumps
6322
6323 return arg;
6324}
6325
6326 static char_u *
6327compile_endtry(char_u *arg, cctx_T *cctx)
6328{
6329 scope_T *scope = cctx->ctx_scope;
6330 garray_T *instr = &cctx->ctx_instr;
6331 isn_T *isn;
6332
6333 // end block scope from :catch or :finally
6334 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6335 compile_endblock(cctx);
6336 scope = cctx->ctx_scope;
6337
6338 // Error if not in a :try scope
6339 if (scope == NULL || scope->se_type != TRY_SCOPE)
6340 {
6341 if (scope == NULL)
6342 emsg(_(e_no_endtry));
6343 else if (scope->se_type == WHILE_SCOPE)
6344 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006345 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346 emsg(_(e_endfor));
6347 else
6348 emsg(_(e_endif));
6349 return NULL;
6350 }
6351
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006352 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6354 {
6355 emsg(_("E1032: missing :catch or :finally"));
6356 return NULL;
6357 }
6358
6359 // Fill in the "end" label in jumps at the end of the blocks, if not done
6360 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006361 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006362
6363 // End :catch or :finally scope: set value in ISN_TRY instruction
6364 if (isn->isn_arg.try.try_finally == 0)
6365 isn->isn_arg.try.try_finally = instr->ga_len;
6366 compile_endblock(cctx);
6367
6368 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6369 return NULL;
6370 return arg;
6371}
6372
6373/*
6374 * compile "throw {expr}"
6375 */
6376 static char_u *
6377compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6378{
6379 char_u *p = skipwhite(arg);
6380
Bram Moolenaara5565e42020-05-09 15:44:01 +02006381 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382 return NULL;
6383 if (may_generate_2STRING(-1, cctx) == FAIL)
6384 return NULL;
6385 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6386 return NULL;
6387
6388 return p;
6389}
6390
6391/*
6392 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006393 * compile "echomsg expr"
6394 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006395 * compile "execute expr"
6396 */
6397 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006398compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006399{
6400 char_u *p = arg;
6401 int count = 0;
6402
6403 for (;;)
6404 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006405 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006406 return NULL;
6407 ++count;
6408 p = skipwhite(p);
6409 if (ends_excmd(*p))
6410 break;
6411 }
6412
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006413 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6414 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6415 else if (cmdidx == CMD_execute)
6416 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6417 else if (cmdidx == CMD_echomsg)
6418 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6419 else
6420 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006421 return p;
6422}
6423
6424/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006425 * A command that is not compiled, execute with legacy code.
6426 */
6427 static char_u *
6428compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6429{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006430 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006431 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006432
6433 if (cctx->ctx_skip == TRUE)
6434 goto theend;
6435
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006436 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6437 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006438 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6439 {
6440 // expand filename in "syntax include [@group] filename"
6441 has_expr = TRUE;
6442 eap->arg = skipwhite(eap->arg + 7);
6443 if (*eap->arg == '@')
6444 eap->arg = skiptowhite(eap->arg);
6445 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006446
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006447 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006448 {
6449 int count = 0;
6450 char_u *start = skipwhite(line);
6451
6452 // :cmd xxx`=expr1`yyy`=expr2`zzz
6453 // PUSHS ":cmd xxx"
6454 // eval expr1
6455 // PUSHS "yyy"
6456 // eval expr2
6457 // PUSHS "zzz"
6458 // EXECCONCAT 5
6459 for (;;)
6460 {
6461 if (p > start)
6462 {
6463 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
6464 ++count;
6465 }
6466 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006467 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006468 return NULL;
6469 may_generate_2STRING(-1, cctx);
6470 ++count;
6471 p = skipwhite(p);
6472 if (*p != '`')
6473 {
6474 emsg(_("E1083: missing backtick"));
6475 return NULL;
6476 }
6477 start = p + 1;
6478
6479 p = (char_u *)strstr((char *)start, "`=");
6480 if (p == NULL)
6481 {
6482 if (*skipwhite(start) != NUL)
6483 {
6484 generate_PUSHS(cctx, vim_strsave(start));
6485 ++count;
6486 }
6487 break;
6488 }
6489 }
6490 generate_EXECCONCAT(cctx, count);
6491 }
6492 else
6493 generate_EXEC(cctx, line);
6494
6495theend:
6496 return (char_u *)"";
6497}
6498
6499/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006500 * After ex_function() has collected all the function lines: parse and compile
6501 * the lines into instructions.
6502 * Adds the function to "def_functions".
6503 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6504 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006505 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006506 * This can be used recursively through compile_lambda(), which may reallocate
6507 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006508 */
6509 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006510compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006512 char_u *line = NULL;
6513 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006514 char *errormsg = NULL; // error message
6515 int had_return = FALSE;
6516 cctx_T cctx;
6517 garray_T *instr;
6518 int called_emsg_before = called_emsg;
6519 int ret = FAIL;
6520 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006521 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006524 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01006525
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006526 if (ufunc->uf_dfunc_idx >= 0)
6527 {
6528 // Redefining a function that was compiled before.
6529 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
6530
6531 // Free old instructions.
6532 delete_def_function_contents(dfunc);
6533 }
6534 else
6535 {
6536 // Add the function to "def_functions".
6537 if (ga_grow(&def_functions, 1) == FAIL)
6538 return;
6539 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006540 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006541 dfunc->df_idx = def_functions.ga_len;
6542 ufunc->uf_dfunc_idx = dfunc->df_idx;
6543 dfunc->df_ufunc = ufunc;
6544 ++def_functions.ga_len;
6545 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006546 }
6547
Bram Moolenaara80faa82020-04-12 19:37:17 +02006548 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006549 cctx.ctx_ufunc = ufunc;
6550 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006551 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6553 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6554 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6555 cctx.ctx_type_list = &ufunc->uf_type_list;
6556 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6557 instr = &cctx.ctx_instr;
6558
6559 // Most modern script version.
6560 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6561
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006562 if (ufunc->uf_def_args.ga_len > 0)
6563 {
6564 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006565 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006566 int i;
6567 char_u *arg;
6568 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6569
6570 // Produce instructions for the default values of optional arguments.
6571 // Store the instruction index in uf_def_arg_idx[] so that we know
6572 // where to start when the function is called, depending on the number
6573 // of arguments.
6574 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6575 if (ufunc->uf_def_arg_idx == NULL)
6576 goto erret;
6577 for (i = 0; i < count; ++i)
6578 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006579 garray_T *stack = &cctx.ctx_type_stack;
6580 type_T *val_type;
6581 int arg_idx = first_def_arg + i;
6582
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006583 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6584 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006585 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006586 goto erret;
6587
6588 // If no type specified use the type of the default value.
6589 // Otherwise check that the default value type matches the
6590 // specified type.
6591 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6592 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6593 ufunc->uf_arg_types[arg_idx] = val_type;
6594 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6595 == FAIL)
6596 {
6597 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6598 arg_idx + 1);
6599 goto erret;
6600 }
6601
6602 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006603 goto erret;
6604 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006605 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6606 }
6607
6608 /*
6609 * Loop over all the lines of the function and generate instructions.
6610 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 for (;;)
6612 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006613 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006614 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006615
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006616 // Bail out on the first error to avoid a flood of errors and report
6617 // the right line number when inside try/catch.
6618 if (emsg_before != called_emsg)
6619 goto erret;
6620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621 if (line != NULL && *line == '|')
6622 // the line continues after a '|'
6623 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006624 else if (line != NULL && *line != NUL
6625 && !(*line == '#' && (line == cctx.ctx_line_start
6626 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006627 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006628 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629 goto erret;
6630 }
6631 else
6632 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006633 line = next_line_from_context(&cctx);
6634 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006635 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006638 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639
6640 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006641 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642 ea.cmdlinep = &line;
6643 ea.cmd = skipwhite(line);
6644
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006645 // Some things can be recognized by the first character.
6646 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006647 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006648 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006649 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006650 if (ea.cmd[1] != '{')
6651 {
6652 line = (char_u *)"";
6653 continue;
6654 }
6655 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006657 case '}':
6658 {
6659 // "}" ends a block scope
6660 scopetype_T stype = cctx.ctx_scope == NULL
6661 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006662
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006663 if (stype == BLOCK_SCOPE)
6664 {
6665 compile_endblock(&cctx);
6666 line = ea.cmd;
6667 }
6668 else
6669 {
6670 emsg(_("E1025: using } outside of a block scope"));
6671 goto erret;
6672 }
6673 if (line != NULL)
6674 line = skipwhite(ea.cmd + 1);
6675 continue;
6676 }
6677
6678 case '{':
6679 // "{" starts a block scope
6680 // "{'a': 1}->func() is something else
6681 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6682 {
6683 line = compile_block(ea.cmd, &cctx);
6684 continue;
6685 }
6686 break;
6687
6688 case ':':
6689 is_ex_command = TRUE;
6690 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006691 }
6692
6693 /*
6694 * COMMAND MODIFIERS
6695 */
6696 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6697 {
6698 if (errormsg != NULL)
6699 goto erret;
6700 // empty line or comment
6701 line = (char_u *)"";
6702 continue;
6703 }
6704
6705 // Skip ":call" to get to the function name.
6706 if (checkforcmd(&ea.cmd, "call", 3))
6707 ea.cmd = skipwhite(ea.cmd);
6708
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006709 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006711 // Assuming the command starts with a variable or function name,
6712 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6713 // val".
6714 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6715 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006716 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006717 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006719 int oplen;
6720 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006722 oplen = assignment_len(skipwhite(p), &heredoc);
6723 if (oplen > 0)
6724 {
6725 // Recognize an assignment if we recognize the variable
6726 // name:
6727 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006728 // "local = expr" where "local" is a local var.
6729 // "script = expr" where "script" is a script-local var.
6730 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006731 // "&opt = expr"
6732 // "$ENV = expr"
6733 // "@r = expr"
6734 if (*ea.cmd == '&'
6735 || *ea.cmd == '$'
6736 || *ea.cmd == '@'
6737 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006738 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006739 || lookup_script(ea.cmd, p - ea.cmd) == OK
6740 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6741 {
6742 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6743 if (line == NULL)
6744 goto erret;
6745 continue;
6746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747 }
6748 }
6749 }
6750
6751 /*
6752 * COMMAND after range
6753 */
6754 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006755 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6756 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006757 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006758
6759 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6760 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006761 if (cctx.ctx_skip == TRUE)
6762 {
6763 line += STRLEN(line);
6764 continue;
6765 }
6766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767 // Expression or function call.
6768 if (ea.cmdidx == CMD_eval)
6769 {
6770 p = ea.cmd;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006771 if (compile_expr0(&p, &cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 goto erret;
6773
6774 // drop the return value
6775 generate_instr_drop(&cctx, ISN_DROP, 1);
6776 line = p;
6777 continue;
6778 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006779 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 iemsg("Command from find_ex_command() not handled");
6781 goto erret;
6782 }
6783
6784 p = skipwhite(p);
6785
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006786 if (cctx.ctx_skip == TRUE
6787 && ea.cmdidx != CMD_elseif
6788 && ea.cmdidx != CMD_else
6789 && ea.cmdidx != CMD_endif)
6790 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006791 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006792 continue;
6793 }
6794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 switch (ea.cmdidx)
6796 {
6797 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006798 ea.arg = p;
6799 line = compile_nested_function(&ea, &cctx);
6800 break;
6801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006802 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006803 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 goto erret;
6805
6806 case CMD_return:
6807 line = compile_return(p, set_return_type, &cctx);
6808 had_return = TRUE;
6809 break;
6810
6811 case CMD_let:
6812 case CMD_const:
6813 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6814 break;
6815
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006816 case CMD_unlet:
6817 case CMD_unlockvar:
6818 case CMD_lockvar:
6819 line = compile_unletlock(p, &ea, &cctx);
6820 break;
6821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006822 case CMD_import:
6823 line = compile_import(p, &cctx);
6824 break;
6825
6826 case CMD_if:
6827 line = compile_if(p, &cctx);
6828 break;
6829 case CMD_elseif:
6830 line = compile_elseif(p, &cctx);
6831 break;
6832 case CMD_else:
6833 line = compile_else(p, &cctx);
6834 break;
6835 case CMD_endif:
6836 line = compile_endif(p, &cctx);
6837 break;
6838
6839 case CMD_while:
6840 line = compile_while(p, &cctx);
6841 break;
6842 case CMD_endwhile:
6843 line = compile_endwhile(p, &cctx);
6844 break;
6845
6846 case CMD_for:
6847 line = compile_for(p, &cctx);
6848 break;
6849 case CMD_endfor:
6850 line = compile_endfor(p, &cctx);
6851 break;
6852 case CMD_continue:
6853 line = compile_continue(p, &cctx);
6854 break;
6855 case CMD_break:
6856 line = compile_break(p, &cctx);
6857 break;
6858
6859 case CMD_try:
6860 line = compile_try(p, &cctx);
6861 break;
6862 case CMD_catch:
6863 line = compile_catch(p, &cctx);
6864 break;
6865 case CMD_finally:
6866 line = compile_finally(p, &cctx);
6867 break;
6868 case CMD_endtry:
6869 line = compile_endtry(p, &cctx);
6870 break;
6871 case CMD_throw:
6872 line = compile_throw(p, &cctx);
6873 break;
6874
6875 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006876 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006877 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006878 case CMD_echomsg:
6879 case CMD_echoerr:
6880 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006881 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882
6883 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006884 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006885 // Not recognized, execute with do_cmdline_cmd().
6886 ea.arg = p;
6887 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006888 break;
6889 }
6890 if (line == NULL)
6891 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006892 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006893
6894 if (cctx.ctx_type_stack.ga_len < 0)
6895 {
6896 iemsg("Type stack underflow");
6897 goto erret;
6898 }
6899 }
6900
6901 if (cctx.ctx_scope != NULL)
6902 {
6903 if (cctx.ctx_scope->se_type == IF_SCOPE)
6904 emsg(_(e_endif));
6905 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6906 emsg(_(e_endwhile));
6907 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6908 emsg(_(e_endfor));
6909 else
6910 emsg(_("E1026: Missing }"));
6911 goto erret;
6912 }
6913
6914 if (!had_return)
6915 {
6916 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6917 {
6918 emsg(_("E1027: Missing return statement"));
6919 goto erret;
6920 }
6921
6922 // Return zero if there is no return at the end.
6923 generate_PUSHNR(&cctx, 0);
6924 generate_instr(&cctx, ISN_RETURN);
6925 }
6926
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006927 {
6928 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6929 + ufunc->uf_dfunc_idx;
6930 dfunc->df_deleted = FALSE;
6931 dfunc->df_instr = instr->ga_data;
6932 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006933 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006934 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006935 if (cctx.ctx_outer_used)
6936 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006937 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006939 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006940 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006941 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006942
6943 // Create a type for the function, with the return type and any
6944 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006945 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6946 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006947 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006948 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006949 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6950 argcount, &ufunc->uf_type_list);
6951 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006952 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006953 argcount + varargs,
6954 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006955 {
6956 ret = FAIL;
6957 goto erret;
6958 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006959 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6960 ufunc->uf_func_type->tt_min_argcount =
6961 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006962 if (ufunc->uf_arg_types == NULL)
6963 {
6964 int i;
6965
6966 // lambda does not have argument types.
6967 for (i = 0; i < argcount; ++i)
6968 ufunc->uf_func_type->tt_args[i] = &t_any;
6969 }
6970 else
6971 mch_memmove(ufunc->uf_func_type->tt_args,
6972 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006973 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006974 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006975 ufunc->uf_func_type->tt_args[argcount] =
6976 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006977 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6978 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006979 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006980 else
6981 // No arguments, can use a predefined type.
6982 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6983 argcount, &ufunc->uf_type_list);
6984
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006985 }
6986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987 ret = OK;
6988
6989erret:
6990 if (ret == FAIL)
6991 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006992 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006993 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6994 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006995
6996 for (idx = 0; idx < instr->ga_len; ++idx)
6997 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006998 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006999
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007001 if (!dfunc->df_deleted)
7002 --def_functions.ga_len;
7003
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007004 while (cctx.ctx_scope != NULL)
7005 drop_scope(&cctx);
7006
Bram Moolenaar20431c92020-03-20 18:39:46 +01007007 // Don't execute this function body.
7008 ga_clear_strings(&ufunc->uf_lines);
7009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010 if (errormsg != NULL)
7011 emsg(errormsg);
7012 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007013 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014 }
7015
7016 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007017 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007018 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020}
7021
7022/*
7023 * Delete an instruction, free what it contains.
7024 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007025 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026delete_instr(isn_T *isn)
7027{
7028 switch (isn->isn_type)
7029 {
7030 case ISN_EXEC:
7031 case ISN_LOADENV:
7032 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007033 case ISN_LOADB:
7034 case ISN_LOADW:
7035 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007036 case ISN_LOADOPT:
7037 case ISN_MEMBER:
7038 case ISN_PUSHEXC:
7039 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007040 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007042 case ISN_STOREB:
7043 case ISN_STOREW:
7044 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007045 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 vim_free(isn->isn_arg.string);
7047 break;
7048
7049 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007050 case ISN_STORES:
7051 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052 break;
7053
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007054 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007055 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007056 vim_free(isn->isn_arg.unlet.ul_name);
7057 break;
7058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 case ISN_STOREOPT:
7060 vim_free(isn->isn_arg.storeopt.so_name);
7061 break;
7062
7063 case ISN_PUSHBLOB: // push blob isn_arg.blob
7064 blob_unref(isn->isn_arg.blob);
7065 break;
7066
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007067 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007068#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007069 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007070#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007071 break;
7072
7073 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007074#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007075 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007076#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007077 break;
7078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079 case ISN_UCALL:
7080 vim_free(isn->isn_arg.ufunc.cuf_name);
7081 break;
7082
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007083 case ISN_FUNCREF:
7084 {
7085 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7086 + isn->isn_arg.funcref.fr_func;
7087 func_ptr_unref(dfunc->df_ufunc);
7088 }
7089 break;
7090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007091 case ISN_2BOOL:
7092 case ISN_2STRING:
7093 case ISN_ADDBLOB:
7094 case ISN_ADDLIST:
7095 case ISN_BCALL:
7096 case ISN_CATCH:
7097 case ISN_CHECKNR:
7098 case ISN_CHECKTYPE:
7099 case ISN_COMPAREANY:
7100 case ISN_COMPAREBLOB:
7101 case ISN_COMPAREBOOL:
7102 case ISN_COMPAREDICT:
7103 case ISN_COMPAREFLOAT:
7104 case ISN_COMPAREFUNC:
7105 case ISN_COMPARELIST:
7106 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 case ISN_COMPARESPECIAL:
7108 case ISN_COMPARESTRING:
7109 case ISN_CONCAT:
7110 case ISN_DCALL:
7111 case ISN_DROP:
7112 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007113 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007114 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007115 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007116 case ISN_EXECCONCAT:
7117 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007119 case ISN_INDEX:
7120 case ISN_JUMP:
7121 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007122 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123 case ISN_LOADSCRIPT:
7124 case ISN_LOADREG:
7125 case ISN_LOADV:
7126 case ISN_NEGATENR:
7127 case ISN_NEWDICT:
7128 case ISN_NEWLIST:
7129 case ISN_OPNR:
7130 case ISN_OPFLOAT:
7131 case ISN_OPANY:
7132 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007133 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007134 case ISN_PUSHF:
7135 case ISN_PUSHNR:
7136 case ISN_PUSHBOOL:
7137 case ISN_PUSHSPEC:
7138 case ISN_RETURN:
7139 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007140 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007141 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007143 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 case ISN_STORESCRIPT:
7145 case ISN_THROW:
7146 case ISN_TRY:
7147 // nothing allocated
7148 break;
7149 }
7150}
7151
7152/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007153 * Free all instructions for "dfunc".
7154 */
7155 static void
7156delete_def_function_contents(dfunc_T *dfunc)
7157{
7158 int idx;
7159
7160 ga_clear(&dfunc->df_def_args_isn);
7161
7162 if (dfunc->df_instr != NULL)
7163 {
7164 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7165 delete_instr(dfunc->df_instr + idx);
7166 VIM_CLEAR(dfunc->df_instr);
7167 }
7168
7169 dfunc->df_deleted = TRUE;
7170}
7171
7172/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173 * When a user function is deleted, delete any associated def function.
7174 */
7175 void
7176delete_def_function(ufunc_T *ufunc)
7177{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178 if (ufunc->uf_dfunc_idx >= 0)
7179 {
7180 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7181 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007182
Bram Moolenaar20431c92020-03-20 18:39:46 +01007183 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 }
7185}
7186
7187#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007188/*
7189 * Free all functions defined with ":def".
7190 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007191 void
7192free_def_functions(void)
7193{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007194 int idx;
7195
7196 for (idx = 0; idx < def_functions.ga_len; ++idx)
7197 {
7198 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7199
7200 delete_def_function_contents(dfunc);
7201 }
7202
7203 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007204}
7205#endif
7206
7207
7208#endif // FEAT_EVAL