blob: 7d302da83a17d19941390f305270a0c385e442ea [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
26/*
27 * Chain of jump instructions where the end label needs to be set.
28 */
29typedef struct endlabel_S endlabel_T;
30struct endlabel_S {
31 endlabel_T *el_next; // chain end_label locations
32 int el_end_label; // instruction idx where to set end
33};
34
35/*
36 * info specific for the scope of :if / elseif / else
37 */
38typedef struct {
39 int is_if_label; // instruction idx at IF or ELSEIF
40 endlabel_T *is_end_label; // instructions to set end label
41} ifscope_T;
42
43/*
44 * info specific for the scope of :while
45 */
46typedef struct {
47 int ws_top_label; // instruction idx at WHILE
48 endlabel_T *ws_end_label; // instructions to set end
49} whilescope_T;
50
51/*
52 * info specific for the scope of :for
53 */
54typedef struct {
55 int fs_top_label; // instruction idx at FOR
56 endlabel_T *fs_end_label; // break instructions
57} forscope_T;
58
59/*
60 * info specific for the scope of :try
61 */
62typedef struct {
63 int ts_try_label; // instruction idx at TRY
64 endlabel_T *ts_end_label; // jump to :finally or :endtry
65 int ts_catch_label; // instruction idx of last CATCH
66 int ts_caught_all; // "catch" without argument encountered
67} tryscope_T;
68
69typedef enum {
70 NO_SCOPE,
71 IF_SCOPE,
72 WHILE_SCOPE,
73 FOR_SCOPE,
74 TRY_SCOPE,
75 BLOCK_SCOPE
76} scopetype_T;
77
78/*
79 * Info for one scope, pointed to by "ctx_scope".
80 */
81typedef struct scope_S scope_T;
82struct scope_S {
83 scope_T *se_outer; // scope containing this one
84 scopetype_T se_type;
85 int se_local_count; // ctx_locals.ga_len before scope
86 union {
87 ifscope_T se_if;
88 whilescope_T se_while;
89 forscope_T se_for;
90 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +010091 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092};
93
94/*
95 * Entry for "ctx_locals". Used for arguments and local variables.
96 */
97typedef struct {
98 char_u *lv_name;
99 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200100 int lv_idx; // index of the variable on the stack
101 int lv_from_outer; // when TRUE using ctx_outer scope
102 int lv_const; // when TRUE cannot be assigned to
103 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104} lvar_T;
105
106/*
107 * Context for compiling lines of Vim script.
108 * Stores info about the local variables and condition stack.
109 */
110struct cctx_S {
111 ufunc_T *ctx_ufunc; // current function
112 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200113 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114 garray_T ctx_instr; // generated instructions
115
116 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200117 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100118
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200119 int ctx_closure_count; // number of closures created in the
120 // function
121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100122 garray_T ctx_imports; // imported items
123
Bram Moolenaara259d8d2020-01-31 20:10:50 +0100124 int ctx_skip; // when TRUE skip commands, when FALSE skip
125 // commands after "else"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126 scope_T *ctx_scope; // current scope, NULL at toplevel
127
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200128 cctx_T *ctx_outer; // outer scope for lambda or nested
129 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200130 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200133 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100134};
135
136static char e_var_notfound[] = N_("E1001: variable not found: %s");
137static char e_syntax_at[] = N_("E1002: Syntax error at %s");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200138static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139
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 Moolenaar822ba242020-05-24 23:00:18 +02001421 if (ufunc->uf_dfunc_idx != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001422 {
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 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001445 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED)
1446 if (compile_def_function(ufunc, TRUE, cctx) == FAIL)
1447 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001448 }
1449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 if ((isn = generate_instr(cctx,
Bram Moolenaar822ba242020-05-24 23:00:18 +02001451 ufunc->uf_dfunc_idx != UF_NOT_COMPILED ? ISN_DCALL
1452 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 return FAIL;
Bram Moolenaar822ba242020-05-24 23:00:18 +02001454 if (ufunc->uf_dfunc_idx != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 {
1456 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1457 isn->isn_arg.dfunc.cdf_argcount = argcount;
1458 }
1459 else
1460 {
1461 // A user function may be deleted and redefined later, can't use the
1462 // ufunc pointer, need to look it up again at runtime.
1463 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1464 isn->isn_arg.ufunc.cuf_argcount = argcount;
1465 }
1466
1467 stack->ga_len -= argcount; // drop the arguments
1468 if (ga_grow(stack, 1) == FAIL)
1469 return FAIL;
1470 // add return value
1471 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1472 ++stack->ga_len;
1473
1474 return OK;
1475}
1476
1477/*
1478 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1479 */
1480 static int
1481generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1482{
1483 isn_T *isn;
1484 garray_T *stack = &cctx->ctx_type_stack;
1485
Bram Moolenaar080457c2020-03-03 21:53:32 +01001486 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1488 return FAIL;
1489 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1490 isn->isn_arg.ufunc.cuf_argcount = argcount;
1491
1492 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001493 if (ga_grow(stack, 1) == FAIL)
1494 return FAIL;
1495 // add return value
1496 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1497 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498
1499 return OK;
1500}
1501
1502/*
1503 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001504 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 */
1506 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001507generate_PCALL(
1508 cctx_T *cctx,
1509 int argcount,
1510 char_u *name,
1511 type_T *type,
1512 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513{
1514 isn_T *isn;
1515 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001516 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517
Bram Moolenaar080457c2020-03-03 21:53:32 +01001518 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001519
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001520 if (type->tt_type == VAR_ANY)
1521 ret_type = &t_any;
1522 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001523 {
1524 if (type->tt_argcount != -1)
1525 {
1526 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1527
1528 if (argcount < type->tt_min_argcount - varargs)
1529 {
1530 semsg(_(e_toofewarg), "[reference]");
1531 return FAIL;
1532 }
1533 if (!varargs && argcount > type->tt_argcount)
1534 {
1535 semsg(_(e_toomanyarg), "[reference]");
1536 return FAIL;
1537 }
1538 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001539 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001540 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001541 else
1542 {
1543 semsg(_("E1085: Not a callable type: %s"), name);
1544 return FAIL;
1545 }
1546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1548 return FAIL;
1549 isn->isn_arg.pfunc.cpf_top = at_top;
1550 isn->isn_arg.pfunc.cpf_argcount = argcount;
1551
1552 stack->ga_len -= argcount; // drop the arguments
1553
1554 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001555 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001557 // If partial is above the arguments it must be cleared and replaced with
1558 // the return value.
1559 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1560 return FAIL;
1561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 return OK;
1563}
1564
1565/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001566 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567 */
1568 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001569generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570{
1571 isn_T *isn;
1572 garray_T *stack = &cctx->ctx_type_stack;
1573 type_T *type;
1574
Bram Moolenaar080457c2020-03-03 21:53:32 +01001575 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001576 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 return FAIL;
1578 isn->isn_arg.string = vim_strnsave(name, (int)len);
1579
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001580 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001582 if (type->tt_type != VAR_DICT && type != &t_any)
1583 {
1584 emsg(_(e_dictreq));
1585 return FAIL;
1586 }
1587 // change dict type to dict member type
1588 if (type->tt_type == VAR_DICT)
1589 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590
1591 return OK;
1592}
1593
1594/*
1595 * Generate an ISN_ECHO instruction.
1596 */
1597 static int
1598generate_ECHO(cctx_T *cctx, int with_white, int count)
1599{
1600 isn_T *isn;
1601
Bram Moolenaar080457c2020-03-03 21:53:32 +01001602 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1604 return FAIL;
1605 isn->isn_arg.echo.echo_with_white = with_white;
1606 isn->isn_arg.echo.echo_count = count;
1607
1608 return OK;
1609}
1610
Bram Moolenaarad39c092020-02-26 18:23:43 +01001611/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001612 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001613 */
1614 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001615generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001616{
1617 isn_T *isn;
1618
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001619 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001620 return FAIL;
1621 isn->isn_arg.number = count;
1622
1623 return OK;
1624}
1625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 static int
1627generate_EXEC(cctx_T *cctx, char_u *line)
1628{
1629 isn_T *isn;
1630
Bram Moolenaar080457c2020-03-03 21:53:32 +01001631 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1633 return FAIL;
1634 isn->isn_arg.string = vim_strsave(line);
1635 return OK;
1636}
1637
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001638 static int
1639generate_EXECCONCAT(cctx_T *cctx, int count)
1640{
1641 isn_T *isn;
1642
1643 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1644 return FAIL;
1645 isn->isn_arg.number = count;
1646 return OK;
1647}
1648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649/*
1650 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001651 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001653 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1655{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 lvar_T *lvar;
1657
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001658 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001660 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001661 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 }
1663
1664 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001665 return NULL;
1666 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001668 // Every local variable uses the next entry on the stack. We could re-use
1669 // the last ones when leaving a scope, but then variables used in a closure
1670 // might get overwritten. To keep things simple do not re-use stack
1671 // entries. This is less efficient, but memory is cheap these days.
1672 lvar->lv_idx = cctx->ctx_locals_count++;
1673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1675 lvar->lv_const = isConst;
1676 lvar->lv_type = type;
1677
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001678 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679}
1680
1681/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001682 * Remove local variables above "new_top".
1683 */
1684 static void
1685unwind_locals(cctx_T *cctx, int new_top)
1686{
1687 if (cctx->ctx_locals.ga_len > new_top)
1688 {
1689 int idx;
1690 lvar_T *lvar;
1691
1692 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1693 {
1694 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1695 vim_free(lvar->lv_name);
1696 }
1697 }
1698 cctx->ctx_locals.ga_len = new_top;
1699}
1700
1701/*
1702 * Free all local variables.
1703 */
1704 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001705free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001706{
1707 unwind_locals(cctx, 0);
1708 ga_clear(&cctx->ctx_locals);
1709}
1710
1711/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 * Skip over a type definition and return a pointer to just after it.
1713 */
1714 char_u *
1715skip_type(char_u *start)
1716{
1717 char_u *p = start;
1718
1719 while (ASCII_ISALNUM(*p) || *p == '_')
1720 ++p;
1721
1722 // Skip over "<type>"; this is permissive about white space.
1723 if (*skipwhite(p) == '<')
1724 {
1725 p = skipwhite(p);
1726 p = skip_type(skipwhite(p + 1));
1727 p = skipwhite(p);
1728 if (*p == '>')
1729 ++p;
1730 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001731 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1732 {
1733 // handle func(args): type
1734 ++p;
1735 while (*p != ')' && *p != NUL)
1736 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001737 char_u *sp = p;
1738
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001739 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001740 if (p == sp)
1741 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001742 if (*p == ',')
1743 p = skipwhite(p + 1);
1744 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001745 if (*p == ')')
1746 {
1747 if (p[1] == ':')
1748 p = skip_type(skipwhite(p + 2));
1749 else
1750 p = skipwhite(p + 1);
1751 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001752 }
1753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 return p;
1755}
1756
1757/*
1758 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001759 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 * Returns NULL in case of failure.
1761 */
1762 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001763parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764{
1765 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001766 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767
1768 if (**arg != '<')
1769 {
1770 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001771 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 else
1773 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001774 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 }
1776 *arg = skipwhite(*arg + 1);
1777
Bram Moolenaard77a8522020-04-03 21:59:57 +02001778 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779
1780 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001781 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 {
1783 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001784 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 }
1786 ++*arg;
1787
1788 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001789 return get_list_type(member_type, type_gap);
1790 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791}
1792
1793/*
1794 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001795 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 */
1797 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001798parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799{
1800 char_u *p = *arg;
1801 size_t len;
1802
1803 // skip over the first word
1804 while (ASCII_ISALNUM(*p) || *p == '_')
1805 ++p;
1806 len = p - *arg;
1807
1808 switch (**arg)
1809 {
1810 case 'a':
1811 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1812 {
1813 *arg += len;
1814 return &t_any;
1815 }
1816 break;
1817 case 'b':
1818 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1819 {
1820 *arg += len;
1821 return &t_bool;
1822 }
1823 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1824 {
1825 *arg += len;
1826 return &t_blob;
1827 }
1828 break;
1829 case 'c':
1830 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1831 {
1832 *arg += len;
1833 return &t_channel;
1834 }
1835 break;
1836 case 'd':
1837 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1838 {
1839 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001840 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 }
1842 break;
1843 case 'f':
1844 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1845 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001846#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 *arg += len;
1848 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001849#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001850 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001851 return &t_any;
1852#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 }
1854 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1855 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001856 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001857 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001858 int argcount = -1;
1859 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001860 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001861 type_T *arg_type[MAX_FUNC_ARGS + 1];
1862
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001863 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001865 if (**arg == '(')
1866 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001867 // "func" may or may not return a value, "func()" does
1868 // not return a value.
1869 ret_type = &t_void;
1870
Bram Moolenaard77a8522020-04-03 21:59:57 +02001871 p = ++*arg;
1872 argcount = 0;
1873 while (*p != NUL && *p != ')')
1874 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001875 if (*p == '?')
1876 {
1877 if (first_optional == -1)
1878 first_optional = argcount;
1879 ++p;
1880 }
1881 else if (first_optional != -1)
1882 {
1883 emsg(_("E1007: mandatory argument after optional argument"));
1884 return &t_any;
1885 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001886 else if (STRNCMP(p, "...", 3) == 0)
1887 {
1888 flags |= TTFLAG_VARARGS;
1889 p += 3;
1890 }
1891
1892 arg_type[argcount++] = parse_type(&p, type_gap);
1893
1894 // Nothing comes after "...{type}".
1895 if (flags & TTFLAG_VARARGS)
1896 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001897
Bram Moolenaard77a8522020-04-03 21:59:57 +02001898 if (*p != ',' && *skipwhite(p) == ',')
1899 {
1900 semsg(_(e_no_white_before), ",");
1901 return &t_any;
1902 }
1903 if (*p == ',')
1904 {
1905 ++p;
1906 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001907 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001908 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001909 return &t_any;
1910 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001911 }
1912 p = skipwhite(p);
1913 if (argcount == MAX_FUNC_ARGS)
1914 {
1915 emsg(_("E740: Too many argument types"));
1916 return &t_any;
1917 }
1918 }
1919
1920 p = skipwhite(p);
1921 if (*p != ')')
1922 {
1923 emsg(_(e_missing_close));
1924 return &t_any;
1925 }
1926 *arg = p + 1;
1927 }
1928 if (**arg == ':')
1929 {
1930 // parse return type
1931 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001932 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001933 semsg(_(e_white_after), ":");
1934 *arg = skipwhite(*arg);
1935 ret_type = parse_type(arg, type_gap);
1936 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001937 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001938 type = get_func_type(ret_type, argcount, type_gap);
1939 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001940 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001941 type = alloc_func_type(ret_type, argcount, type_gap);
1942 type->tt_flags = flags;
1943 if (argcount > 0)
1944 {
1945 type->tt_argcount = argcount;
1946 type->tt_min_argcount = first_optional == -1
1947 ? argcount : first_optional;
1948 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001949 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001950 return &t_any;
1951 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001952 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001953 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001954 }
1955 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 }
1957 break;
1958 case 'j':
1959 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1960 {
1961 *arg += len;
1962 return &t_job;
1963 }
1964 break;
1965 case 'l':
1966 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1967 {
1968 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001969 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 }
1971 break;
1972 case 'n':
1973 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1974 {
1975 *arg += len;
1976 return &t_number;
1977 }
1978 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 case 's':
1980 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1981 {
1982 *arg += len;
1983 return &t_string;
1984 }
1985 break;
1986 case 'v':
1987 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1988 {
1989 *arg += len;
1990 return &t_void;
1991 }
1992 break;
1993 }
1994
1995 semsg(_("E1010: Type not recognized: %s"), *arg);
1996 return &t_any;
1997}
1998
1999/*
2000 * Check if "type1" and "type2" are exactly the same.
2001 */
2002 static int
2003equal_type(type_T *type1, type_T *type2)
2004{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002005 int i;
2006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 if (type1->tt_type != type2->tt_type)
2008 return FALSE;
2009 switch (type1->tt_type)
2010 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002012 case VAR_ANY:
2013 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 case VAR_SPECIAL:
2015 case VAR_BOOL:
2016 case VAR_NUMBER:
2017 case VAR_FLOAT:
2018 case VAR_STRING:
2019 case VAR_BLOB:
2020 case VAR_JOB:
2021 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002022 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 case VAR_LIST:
2024 case VAR_DICT:
2025 return equal_type(type1->tt_member, type2->tt_member);
2026 case VAR_FUNC:
2027 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002028 if (!equal_type(type1->tt_member, type2->tt_member)
2029 || type1->tt_argcount != type2->tt_argcount)
2030 return FALSE;
2031 if (type1->tt_argcount < 0
2032 || type1->tt_args == NULL || type2->tt_args == NULL)
2033 return TRUE;
2034 for (i = 0; i < type1->tt_argcount; ++i)
2035 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2036 return FALSE;
2037 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 }
2039 return TRUE;
2040}
2041
2042/*
2043 * Find the common type of "type1" and "type2" and put it in "dest".
2044 * "type2" and "dest" may be the same.
2045 */
2046 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002047common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048{
2049 if (equal_type(type1, type2))
2050 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002051 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 return;
2053 }
2054
2055 if (type1->tt_type == type2->tt_type)
2056 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2058 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002059 type_T *common;
2060
Bram Moolenaard77a8522020-04-03 21:59:57 +02002061 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002062 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002063 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002064 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002065 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 return;
2067 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002068 if (type1->tt_type == VAR_FUNC)
2069 {
2070 type_T *common;
2071
2072 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2073 if (type1->tt_argcount == type2->tt_argcount
2074 && type1->tt_argcount >= 0)
2075 {
2076 int argcount = type1->tt_argcount;
2077 int i;
2078
2079 *dest = alloc_func_type(common, argcount, type_gap);
2080 if (type1->tt_args != NULL && type2->tt_args != NULL)
2081 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002082 if (func_type_add_arg_types(*dest, argcount,
2083 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002084 for (i = 0; i < argcount; ++i)
2085 common_type(type1->tt_args[i], type2->tt_args[i],
2086 &(*dest)->tt_args[i], type_gap);
2087 }
2088 }
2089 else
2090 *dest = alloc_func_type(common, -1, type_gap);
2091 return;
2092 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 }
2094
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002095 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096}
2097
2098 char *
2099vartype_name(vartype_T type)
2100{
2101 switch (type)
2102 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002103 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002104 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 case VAR_SPECIAL: return "special";
2107 case VAR_BOOL: return "bool";
2108 case VAR_NUMBER: return "number";
2109 case VAR_FLOAT: return "float";
2110 case VAR_STRING: return "string";
2111 case VAR_BLOB: return "blob";
2112 case VAR_JOB: return "job";
2113 case VAR_CHANNEL: return "channel";
2114 case VAR_LIST: return "list";
2115 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002116
2117 case VAR_FUNC:
2118 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002120 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121}
2122
2123/*
2124 * Return the name of a type.
2125 * The result may be in allocated memory, in which case "tofree" is set.
2126 */
2127 char *
2128type_name(type_T *type, char **tofree)
2129{
2130 char *name = vartype_name(type->tt_type);
2131
2132 *tofree = NULL;
2133 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2134 {
2135 char *member_free;
2136 char *member_name = type_name(type->tt_member, &member_free);
2137 size_t len;
2138
2139 len = STRLEN(name) + STRLEN(member_name) + 3;
2140 *tofree = alloc(len);
2141 if (*tofree != NULL)
2142 {
2143 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2144 vim_free(member_free);
2145 return *tofree;
2146 }
2147 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002148 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002149 {
2150 garray_T ga;
2151 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002152 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002153
2154 ga_init2(&ga, 1, 100);
2155 if (ga_grow(&ga, 20) == FAIL)
2156 return "[unknown]";
2157 *tofree = ga.ga_data;
2158 STRCPY(ga.ga_data, "func(");
2159 ga.ga_len += 5;
2160
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002161 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002162 {
2163 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002164 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002165 int len;
2166
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002167 if (type->tt_args == NULL)
2168 arg_type = "[unknown]";
2169 else
2170 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002171 if (i > 0)
2172 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002173 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002174 ga.ga_len += 2;
2175 }
2176 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002177 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002178 {
2179 vim_free(arg_free);
2180 return "[unknown]";
2181 }
2182 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002183 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002184 {
2185 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2186 ga.ga_len += 3;
2187 }
2188 else if (i >= type->tt_min_argcount)
2189 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002190 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002191 ga.ga_len += len;
2192 vim_free(arg_free);
2193 }
2194
2195 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002196 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002197 else
2198 {
2199 char *ret_free;
2200 char *ret_name = type_name(type->tt_member, &ret_free);
2201 int len;
2202
2203 len = (int)STRLEN(ret_name) + 4;
2204 if (ga_grow(&ga, len) == FAIL)
2205 {
2206 vim_free(ret_free);
2207 return "[unknown]";
2208 }
2209 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002210 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2211 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002212 vim_free(ret_free);
2213 }
2214 return ga.ga_data;
2215 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216
2217 return name;
2218}
2219
2220/*
2221 * Find "name" in script-local items of script "sid".
2222 * Returns the index in "sn_var_vals" if found.
2223 * If found but not in "sn_var_vals" returns -1.
2224 * If not found returns -2.
2225 */
2226 int
2227get_script_item_idx(int sid, char_u *name, int check_writable)
2228{
2229 hashtab_T *ht;
2230 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002231 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 int idx;
2233
2234 // First look the name up in the hashtable.
2235 if (sid <= 0 || sid > script_items.ga_len)
2236 return -1;
2237 ht = &SCRIPT_VARS(sid);
2238 di = find_var_in_ht(ht, 0, name, TRUE);
2239 if (di == NULL)
2240 return -2;
2241
2242 // Now find the svar_T index in sn_var_vals.
2243 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2244 {
2245 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2246
2247 if (sv->sv_tv == &di->di_tv)
2248 {
2249 if (check_writable && sv->sv_const)
2250 semsg(_(e_readonlyvar), name);
2251 return idx;
2252 }
2253 }
2254 return -1;
2255}
2256
2257/*
2258 * Find "name" in imported items of the current script/
2259 */
2260 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002261find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002263 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 int idx;
2265
2266 if (cctx != NULL)
2267 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2268 {
2269 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2270 + idx;
2271
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002272 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2273 : STRLEN(import->imp_name) == len
2274 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 return import;
2276 }
2277
2278 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2279 {
2280 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2281
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002282 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2283 : STRLEN(import->imp_name) == len
2284 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 return import;
2286 }
2287 return NULL;
2288}
2289
2290/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002291 * Free all imported variables.
2292 */
2293 static void
2294free_imported(cctx_T *cctx)
2295{
2296 int idx;
2297
2298 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2299 {
2300 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2301
2302 vim_free(import->imp_name);
2303 }
2304 ga_clear(&cctx->ctx_imports);
2305}
2306
2307/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002308 * Get the next line of the function from "cctx".
2309 * Returns NULL when at the end.
2310 */
2311 static char_u *
2312next_line_from_context(cctx_T *cctx)
2313{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002314 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002315
2316 do
2317 {
2318 ++cctx->ctx_lnum;
2319 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002320 {
2321 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002322 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002323 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002324 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002325 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002326 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2327 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002328 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002329 return line;
2330}
2331
2332/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002333 * Return TRUE if "p" points at a "#" but not at "#{".
2334 */
2335 static int
2336comment_start(char_u *p)
2337{
2338 return p[0] == '#' && p[1] != '{';
2339}
2340
2341/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002342 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002343 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002344 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2345 */
2346 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002347may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002348{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002349 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002350 {
2351 char_u *next = next_line_from_context(cctx);
2352
2353 if (next == NULL)
2354 return FAIL;
2355 *arg = skipwhite(next);
2356 }
2357 return OK;
2358}
2359
Bram Moolenaara5565e42020-05-09 15:44:01 +02002360// Structure passed between the compile_expr* functions to keep track of
2361// constants that have been parsed but for which no code was produced yet. If
2362// possible expressions on these constants are applied at compile time. If
2363// that is not possible, the code to push the constants needs to be generated
2364// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002365// Using 50 should be more than enough of 5 levels of ().
2366#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002367typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002368 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002369 int pp_used; // active entries in pp_tv[]
2370} ppconst_T;
2371
Bram Moolenaar1c747212020-05-09 18:28:34 +02002372static int compile_expr0(char_u **arg, cctx_T *cctx);
2373static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2374
Bram Moolenaara5565e42020-05-09 15:44:01 +02002375/*
2376 * Generate a PUSH instruction for "tv".
2377 * "tv" will be consumed or cleared.
2378 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2379 */
2380 static int
2381generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2382{
2383 if (tv != NULL)
2384 {
2385 switch (tv->v_type)
2386 {
2387 case VAR_UNKNOWN:
2388 break;
2389 case VAR_BOOL:
2390 generate_PUSHBOOL(cctx, tv->vval.v_number);
2391 break;
2392 case VAR_SPECIAL:
2393 generate_PUSHSPEC(cctx, tv->vval.v_number);
2394 break;
2395 case VAR_NUMBER:
2396 generate_PUSHNR(cctx, tv->vval.v_number);
2397 break;
2398#ifdef FEAT_FLOAT
2399 case VAR_FLOAT:
2400 generate_PUSHF(cctx, tv->vval.v_float);
2401 break;
2402#endif
2403 case VAR_BLOB:
2404 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2405 tv->vval.v_blob = NULL;
2406 break;
2407 case VAR_STRING:
2408 generate_PUSHS(cctx, tv->vval.v_string);
2409 tv->vval.v_string = NULL;
2410 break;
2411 default:
2412 iemsg("constant type not supported");
2413 clear_tv(tv);
2414 return FAIL;
2415 }
2416 tv->v_type = VAR_UNKNOWN;
2417 }
2418 return OK;
2419}
2420
2421/*
2422 * Generate code for any ppconst entries.
2423 */
2424 static int
2425generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2426{
2427 int i;
2428 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002429 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002430
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002431 cctx->ctx_skip = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002432 for (i = 0; i < ppconst->pp_used; ++i)
2433 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2434 ret = FAIL;
2435 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002436 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002437 return ret;
2438}
2439
2440/*
2441 * Clear ppconst constants. Used when failing.
2442 */
2443 static void
2444clear_ppconst(ppconst_T *ppconst)
2445{
2446 int i;
2447
2448 for (i = 0; i < ppconst->pp_used; ++i)
2449 clear_tv(&ppconst->pp_tv[i]);
2450 ppconst->pp_used = 0;
2451}
2452
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002453/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002454 * Generate an instruction to load script-local variable "name", without the
2455 * leading "s:".
2456 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 */
2458 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002459compile_load_scriptvar(
2460 cctx_T *cctx,
2461 char_u *name, // variable NUL terminated
2462 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002463 char_u **end, // end of variable
2464 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002466 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2468 imported_T *import;
2469
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002470 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002472 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002473 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2474 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 }
2476 if (idx >= 0)
2477 {
2478 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2479
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002480 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 current_sctx.sc_sid, idx, sv->sv_type);
2482 return OK;
2483 }
2484
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002485 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 if (import != NULL)
2487 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002488 if (import->imp_all)
2489 {
2490 char_u *p = skipwhite(*end);
2491 int name_len;
2492 ufunc_T *ufunc;
2493 type_T *type;
2494
2495 // Used "import * as Name", need to lookup the member.
2496 if (*p != '.')
2497 {
2498 semsg(_("E1060: expected dot after name: %s"), start);
2499 return FAIL;
2500 }
2501 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002502 if (VIM_ISWHITE(*p))
2503 {
2504 emsg(_("E1074: no white space allowed after dot"));
2505 return FAIL;
2506 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002507
2508 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2509 // TODO: what if it is a function?
2510 if (idx < 0)
2511 return FAIL;
2512 *end = p;
2513
2514 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2515 import->imp_sid,
2516 idx,
2517 type);
2518 }
2519 else
2520 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002521 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002522 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2523 import->imp_sid,
2524 import->imp_var_vals_idx,
2525 import->imp_type);
2526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 return OK;
2528 }
2529
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002530 if (error)
2531 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 return FAIL;
2533}
2534
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002535 static int
2536generate_funcref(cctx_T *cctx, char_u *name)
2537{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002538 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002539
2540 if (ufunc == NULL)
2541 return FAIL;
2542
2543 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2544}
2545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546/*
2547 * Compile a variable name into a load instruction.
2548 * "end" points to just after the name.
2549 * When "error" is FALSE do not give an error when not found.
2550 */
2551 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002552compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553{
2554 type_T *type;
2555 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002556 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002558 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559
2560 if (*(*arg + 1) == ':')
2561 {
2562 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002563 if (end <= *arg + 2)
2564 name = vim_strsave((char_u *)"[empty]");
2565 else
2566 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 if (name == NULL)
2568 return FAIL;
2569
2570 if (**arg == 'v')
2571 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002572 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 }
2574 else if (**arg == 'g')
2575 {
2576 // Global variables can be defined later, thus we don't check if it
2577 // exists, give error at runtime.
2578 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2579 }
2580 else if (**arg == 's')
2581 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002582 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002584 else if (**arg == 'b')
2585 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002586 // Buffer-local variables can be defined later, thus we don't check
2587 // if it exists, give error at runtime.
2588 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002589 }
2590 else if (**arg == 'w')
2591 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002592 // Window-local variables can be defined later, thus we don't check
2593 // if it exists, give error at runtime.
2594 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002595 }
2596 else if (**arg == 't')
2597 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002598 // Tabpage-local variables can be defined later, thus we don't
2599 // check if it exists, give error at runtime.
2600 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 else
2603 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002604 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 goto theend;
2606 }
2607 }
2608 else
2609 {
2610 size_t len = end - *arg;
2611 int idx;
2612 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002613 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614
2615 name = vim_strnsave(*arg, end - *arg);
2616 if (name == NULL)
2617 return FAIL;
2618
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002619 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002621 if (!gen_load_outer)
2622 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 }
2624 else
2625 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002626 lvar_T *lvar = lookup_local(*arg, len, cctx);
2627
2628 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002630 type = lvar->lv_type;
2631 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002632 if (lvar->lv_from_outer)
2633 gen_load_outer = TRUE;
2634 else
2635 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 }
2637 else
2638 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002639 // "var" can be script-local even without using "s:" if it
2640 // already exists.
2641 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2642 == SCRIPT_VERSION_VIM9
2643 || lookup_script(*arg, len) == OK)
2644 res = compile_load_scriptvar(cctx, name, *arg, &end,
2645 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002646
Bram Moolenaara5565e42020-05-09 15:44:01 +02002647 // When the name starts with an uppercase letter or "x:" it
2648 // can be a user defined function.
2649 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2650 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 }
2652 }
2653 if (gen_load)
2654 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002655 if (gen_load_outer)
2656 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 }
2658
2659 *arg = end;
2660
2661theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002662 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 semsg(_(e_var_notfound), name);
2664 vim_free(name);
2665 return res;
2666}
2667
2668/*
2669 * Compile the argument expressions.
2670 * "arg" points to just after the "(" and is advanced to after the ")"
2671 */
2672 static int
2673compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2674{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002675 char_u *p = *arg;
2676 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677
Bram Moolenaare6085c52020-04-12 20:19:16 +02002678 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002680 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002681 {
2682 p = next_line_from_context(cctx);
2683 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002684 goto failret;
2685 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002686 p = skipwhite(p);
2687 }
2688 if (*p == ')')
2689 {
2690 *arg = p + 1;
2691 return OK;
2692 }
2693
Bram Moolenaara5565e42020-05-09 15:44:01 +02002694 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 return FAIL;
2696 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002697
2698 if (*p != ',' && *skipwhite(p) == ',')
2699 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002700 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002701 p = skipwhite(p);
2702 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002704 {
2705 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002706 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002707 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002708 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002709 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002710 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002712failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002713 emsg(_(e_missing_close));
2714 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715}
2716
2717/*
2718 * Compile a function call: name(arg1, arg2)
2719 * "arg" points to "name", "arg + varlen" to the "(".
2720 * "argcount_init" is 1 for "value->method()"
2721 * Instructions:
2722 * EVAL arg1
2723 * EVAL arg2
2724 * BCALL / DCALL / UCALL
2725 */
2726 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002727compile_call(
2728 char_u **arg,
2729 size_t varlen,
2730 cctx_T *cctx,
2731 ppconst_T *ppconst,
2732 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733{
2734 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002735 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 int argcount = argcount_init;
2737 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002738 char_u fname_buf[FLEN_FIXED + 1];
2739 char_u *tofree = NULL;
2740 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002742 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743
Bram Moolenaara5565e42020-05-09 15:44:01 +02002744 // we can evaluate "has('name')" at compile time
2745 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2746 {
2747 char_u *s = skipwhite(*arg + varlen + 1);
2748 typval_T argvars[2];
2749
2750 argvars[0].v_type = VAR_UNKNOWN;
2751 if (*s == '"')
2752 (void)get_string_tv(&s, &argvars[0], TRUE);
2753 else if (*s == '\'')
2754 (void)get_lit_string_tv(&s, &argvars[0], TRUE);
2755 s = skipwhite(s);
2756 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2757 {
2758 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2759
2760 *arg = s + 1;
2761 argvars[1].v_type = VAR_UNKNOWN;
2762 tv->v_type = VAR_NUMBER;
2763 tv->vval.v_number = 0;
2764 f_has(argvars, tv);
2765 clear_tv(&argvars[0]);
2766 ++ppconst->pp_used;
2767 return OK;
2768 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002769 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002770 }
2771
2772 if (generate_ppconst(cctx, ppconst) == FAIL)
2773 return FAIL;
2774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 if (varlen >= sizeof(namebuf))
2776 {
2777 semsg(_("E1011: name too long: %s"), name);
2778 return FAIL;
2779 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002780 vim_strncpy(namebuf, *arg, varlen);
2781 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782
2783 *arg = skipwhite(*arg + varlen + 1);
2784 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002785 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002787 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 {
2789 int idx;
2790
2791 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002792 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002794 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002795 else
2796 semsg(_(e_unknownfunc), namebuf);
2797 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 }
2799
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002801 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002803 {
2804 res = generate_CALL(cctx, ufunc, argcount);
2805 goto theend;
2806 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807
2808 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002809 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002811 if (STRNCMP(namebuf, "g:", 2) != 0
2812 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002813 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002814 garray_T *stack = &cctx->ctx_type_stack;
2815 type_T *type;
2816
2817 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2818 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002819 goto theend;
2820 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002822 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002823 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002824 if (STRNCMP(namebuf, "g:", 2) == 0)
2825 res = generate_UCALL(cctx, name, argcount);
2826 else
2827 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002828
2829theend:
2830 vim_free(tofree);
2831 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832}
2833
2834// like NAMESPACE_CHAR but with 'a' and 'l'.
2835#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2836
2837/*
2838 * Find the end of a variable or function name. Unlike find_name_end() this
2839 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002840 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 * Return a pointer to just after the name. Equal to "arg" if there is no
2842 * valid name.
2843 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002844 static char_u *
2845to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846{
2847 char_u *p;
2848
2849 // Quick check for valid starting character.
2850 if (!eval_isnamec1(*arg))
2851 return arg;
2852
2853 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2854 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2855 // and can be used in slice "[n:]".
2856 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002857 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2859 break;
2860 return p;
2861}
2862
2863/*
2864 * Like to_name_end() but also skip over a list or dict constant.
2865 */
2866 char_u *
2867to_name_const_end(char_u *arg)
2868{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002869 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 typval_T rettv;
2871
2872 if (p == arg && *arg == '[')
2873 {
2874
2875 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar32e35112020-05-14 22:41:15 +02002876 if (get_list_tv(&p, &rettv, 0, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 p = arg;
2878 }
2879 else if (p == arg && *arg == '#' && arg[1] == '{')
2880 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002881 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 ++p;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002883 if (eval_dict(&p, &rettv, 0, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 p = arg;
2885 }
2886 else if (p == arg && *arg == '{')
2887 {
2888 int ret = get_lambda_tv(&p, &rettv, FALSE);
2889
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002890 // Can be "{x -> ret}()".
2891 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 if (ret == NOTDONE)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002893 ret = eval_dict(&p, &rettv, 0, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 if (ret != OK)
2895 p = arg;
2896 }
2897
2898 return p;
2899}
2900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901/*
2902 * parse a list: [expr, expr]
2903 * "*arg" points to the '['.
2904 */
2905 static int
2906compile_list(char_u **arg, cctx_T *cctx)
2907{
2908 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002909 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 int count = 0;
2911
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002912 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002914 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002915 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002916 p = next_line_from_context(cctx);
2917 if (p == NULL)
2918 {
2919 semsg(_(e_list_end), *arg);
2920 return FAIL;
2921 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002922 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002923 p = skipwhite(p);
2924 }
2925 if (*p == ']')
2926 {
2927 ++p;
2928 // Allow for following comment, after at least one space.
2929 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2930 p += STRLEN(p);
2931 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002932 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002933 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 break;
2935 ++count;
2936 if (*p == ',')
2937 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002938 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 p = skipwhite(p);
2940 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002941 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942
2943 generate_NEWLIST(cctx, count);
2944 return OK;
2945}
2946
2947/*
2948 * parse a lambda: {arg, arg -> expr}
2949 * "*arg" points to the '{'.
2950 */
2951 static int
2952compile_lambda(char_u **arg, cctx_T *cctx)
2953{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 typval_T rettv;
2955 ufunc_T *ufunc;
2956
2957 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002958 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002962 ++ufunc->uf_refcount;
2963 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002964 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965
2966 // The function will have one line: "return {expr}".
2967 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002968 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969
2970 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002971 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 return FAIL;
2973}
2974
2975/*
2976 * Compile a lamda call: expr->{lambda}(args)
2977 * "arg" points to the "{".
2978 */
2979 static int
2980compile_lambda_call(char_u **arg, cctx_T *cctx)
2981{
2982 ufunc_T *ufunc;
2983 typval_T rettv;
2984 int argcount = 1;
2985 int ret = FAIL;
2986
2987 // Get the funcref in "rettv".
2988 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2989 return FAIL;
2990
2991 if (**arg != '(')
2992 {
2993 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002994 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 else
2996 semsg(_(e_missing_paren), "lambda");
2997 clear_tv(&rettv);
2998 return FAIL;
2999 }
3000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 ufunc = rettv.vval.v_partial->pt_func;
3002 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003003 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003004 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003005
3006 // The function will have one line: "return {expr}".
3007 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003008 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009
3010 // compile the arguments
3011 *arg = skipwhite(*arg + 1);
3012 if (compile_arguments(arg, cctx, &argcount) == OK)
3013 // call the compiled function
3014 ret = generate_CALL(cctx, ufunc, argcount);
3015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 return ret;
3017}
3018
3019/*
3020 * parse a dict: {'key': val} or #{key: val}
3021 * "*arg" points to the '{'.
3022 */
3023 static int
3024compile_dict(char_u **arg, cctx_T *cctx, int literal)
3025{
3026 garray_T *instr = &cctx->ctx_instr;
3027 int count = 0;
3028 dict_T *d = dict_alloc();
3029 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003030 char_u *whitep = *arg;
3031 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032
3033 if (d == NULL)
3034 return FAIL;
3035 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003036 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 {
3038 char_u *key = NULL;
3039
Bram Moolenaar2c330432020-04-13 14:41:35 +02003040 while (**arg == NUL || (literal && **arg == '"')
3041 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003042 {
3043 *arg = next_line_from_context(cctx);
3044 if (*arg == NULL)
3045 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003046 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003047 *arg = skipwhite(*arg);
3048 }
3049
3050 if (**arg == '}')
3051 break;
3052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 if (literal)
3054 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003055 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056
Bram Moolenaar2c330432020-04-13 14:41:35 +02003057 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 {
3059 semsg(_("E1014: Invalid key: %s"), *arg);
3060 return FAIL;
3061 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003062 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 if (generate_PUSHS(cctx, key) == FAIL)
3064 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003065 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 }
3067 else
3068 {
3069 isn_T *isn;
3070
Bram Moolenaara5565e42020-05-09 15:44:01 +02003071 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 return FAIL;
3073 // TODO: check type is string
3074 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3075 if (isn->isn_type == ISN_PUSHS)
3076 key = isn->isn_arg.string;
3077 }
3078
3079 // Check for duplicate keys, if using string keys.
3080 if (key != NULL)
3081 {
3082 item = dict_find(d, key, -1);
3083 if (item != NULL)
3084 {
3085 semsg(_(e_duplicate_key), key);
3086 goto failret;
3087 }
3088 item = dictitem_alloc(key);
3089 if (item != NULL)
3090 {
3091 item->di_tv.v_type = VAR_UNKNOWN;
3092 item->di_tv.v_lock = 0;
3093 if (dict_add(d, item) == FAIL)
3094 dictitem_free(item);
3095 }
3096 }
3097
3098 *arg = skipwhite(*arg);
3099 if (**arg != ':')
3100 {
3101 semsg(_(e_missing_dict_colon), *arg);
3102 return FAIL;
3103 }
3104
Bram Moolenaar2c330432020-04-13 14:41:35 +02003105 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003107 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003108 {
3109 *arg = next_line_from_context(cctx);
3110 if (*arg == NULL)
3111 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003112 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003113 *arg = skipwhite(*arg);
3114 }
3115
Bram Moolenaara5565e42020-05-09 15:44:01 +02003116 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 return FAIL;
3118 ++count;
3119
Bram Moolenaar2c330432020-04-13 14:41:35 +02003120 whitep = *arg;
3121 p = skipwhite(*arg);
3122 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003123 {
3124 *arg = next_line_from_context(cctx);
3125 if (*arg == NULL)
3126 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003127 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003128 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003129 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003130 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 if (**arg == '}')
3132 break;
3133 if (**arg != ',')
3134 {
3135 semsg(_(e_missing_dict_comma), *arg);
3136 goto failret;
3137 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003138 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 *arg = skipwhite(*arg + 1);
3140 }
3141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 *arg = *arg + 1;
3143
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003144 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003145 p = skipwhite(*arg);
3146 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003147 *arg += STRLEN(*arg);
3148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 dict_unref(d);
3150 return generate_NEWDICT(cctx, count);
3151
3152failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003153 if (*arg == NULL)
3154 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 dict_unref(d);
3156 return FAIL;
3157}
3158
3159/*
3160 * Compile "&option".
3161 */
3162 static int
3163compile_get_option(char_u **arg, cctx_T *cctx)
3164{
3165 typval_T rettv;
3166 char_u *start = *arg;
3167 int ret;
3168
3169 // parse the option and get the current value to get the type.
3170 rettv.v_type = VAR_UNKNOWN;
3171 ret = get_option_tv(arg, &rettv, TRUE);
3172 if (ret == OK)
3173 {
3174 // include the '&' in the name, get_option_tv() expects it.
3175 char_u *name = vim_strnsave(start, *arg - start);
3176 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3177
3178 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3179 vim_free(name);
3180 }
3181 clear_tv(&rettv);
3182
3183 return ret;
3184}
3185
3186/*
3187 * Compile "$VAR".
3188 */
3189 static int
3190compile_get_env(char_u **arg, cctx_T *cctx)
3191{
3192 char_u *start = *arg;
3193 int len;
3194 int ret;
3195 char_u *name;
3196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 ++*arg;
3198 len = get_env_len(arg);
3199 if (len == 0)
3200 {
3201 semsg(_(e_syntax_at), start - 1);
3202 return FAIL;
3203 }
3204
3205 // include the '$' in the name, get_env_tv() expects it.
3206 name = vim_strnsave(start, len + 1);
3207 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3208 vim_free(name);
3209 return ret;
3210}
3211
3212/*
3213 * Compile "@r".
3214 */
3215 static int
3216compile_get_register(char_u **arg, cctx_T *cctx)
3217{
3218 int ret;
3219
3220 ++*arg;
3221 if (**arg == NUL)
3222 {
3223 semsg(_(e_syntax_at), *arg - 1);
3224 return FAIL;
3225 }
3226 if (!valid_yank_reg(**arg, TRUE))
3227 {
3228 emsg_invreg(**arg);
3229 return FAIL;
3230 }
3231 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3232 ++*arg;
3233 return ret;
3234}
3235
3236/*
3237 * Apply leading '!', '-' and '+' to constant "rettv".
3238 */
3239 static int
3240apply_leader(typval_T *rettv, char_u *start, char_u *end)
3241{
3242 char_u *p = end;
3243
3244 // this works from end to start
3245 while (p > start)
3246 {
3247 --p;
3248 if (*p == '-' || *p == '+')
3249 {
3250 // only '-' has an effect, for '+' we only check the type
3251#ifdef FEAT_FLOAT
3252 if (rettv->v_type == VAR_FLOAT)
3253 {
3254 if (*p == '-')
3255 rettv->vval.v_float = -rettv->vval.v_float;
3256 }
3257 else
3258#endif
3259 {
3260 varnumber_T val;
3261 int error = FALSE;
3262
3263 // tv_get_number_chk() accepts a string, but we don't want that
3264 // here
3265 if (check_not_string(rettv) == FAIL)
3266 return FAIL;
3267 val = tv_get_number_chk(rettv, &error);
3268 clear_tv(rettv);
3269 if (error)
3270 return FAIL;
3271 if (*p == '-')
3272 val = -val;
3273 rettv->v_type = VAR_NUMBER;
3274 rettv->vval.v_number = val;
3275 }
3276 }
3277 else
3278 {
3279 int v = tv2bool(rettv);
3280
3281 // '!' is permissive in the type.
3282 clear_tv(rettv);
3283 rettv->v_type = VAR_BOOL;
3284 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3285 }
3286 }
3287 return OK;
3288}
3289
3290/*
3291 * Recognize v: variables that are constants and set "rettv".
3292 */
3293 static void
3294get_vim_constant(char_u **arg, typval_T *rettv)
3295{
3296 if (STRNCMP(*arg, "v:true", 6) == 0)
3297 {
3298 rettv->v_type = VAR_BOOL;
3299 rettv->vval.v_number = VVAL_TRUE;
3300 *arg += 6;
3301 }
3302 else if (STRNCMP(*arg, "v:false", 7) == 0)
3303 {
3304 rettv->v_type = VAR_BOOL;
3305 rettv->vval.v_number = VVAL_FALSE;
3306 *arg += 7;
3307 }
3308 else if (STRNCMP(*arg, "v:null", 6) == 0)
3309 {
3310 rettv->v_type = VAR_SPECIAL;
3311 rettv->vval.v_number = VVAL_NULL;
3312 *arg += 6;
3313 }
3314 else if (STRNCMP(*arg, "v:none", 6) == 0)
3315 {
3316 rettv->v_type = VAR_SPECIAL;
3317 rettv->vval.v_number = VVAL_NONE;
3318 *arg += 6;
3319 }
3320}
3321
Bram Moolenaar61a89812020-05-07 16:58:17 +02003322 static exptype_T
3323get_compare_type(char_u *p, int *len, int *type_is)
3324{
3325 exptype_T type = EXPR_UNKNOWN;
3326 int i;
3327
3328 switch (p[0])
3329 {
3330 case '=': if (p[1] == '=')
3331 type = EXPR_EQUAL;
3332 else if (p[1] == '~')
3333 type = EXPR_MATCH;
3334 break;
3335 case '!': if (p[1] == '=')
3336 type = EXPR_NEQUAL;
3337 else if (p[1] == '~')
3338 type = EXPR_NOMATCH;
3339 break;
3340 case '>': if (p[1] != '=')
3341 {
3342 type = EXPR_GREATER;
3343 *len = 1;
3344 }
3345 else
3346 type = EXPR_GEQUAL;
3347 break;
3348 case '<': if (p[1] != '=')
3349 {
3350 type = EXPR_SMALLER;
3351 *len = 1;
3352 }
3353 else
3354 type = EXPR_SEQUAL;
3355 break;
3356 case 'i': if (p[1] == 's')
3357 {
3358 // "is" and "isnot"; but not a prefix of a name
3359 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3360 *len = 5;
3361 i = p[*len];
3362 if (!isalnum(i) && i != '_')
3363 {
3364 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3365 *type_is = TRUE;
3366 }
3367 }
3368 break;
3369 }
3370 return type;
3371}
3372
3373/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 * Compile code to apply '-', '+' and '!'.
3375 */
3376 static int
3377compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3378{
3379 char_u *p = end;
3380
3381 // this works from end to start
3382 while (p > start)
3383 {
3384 --p;
3385 if (*p == '-' || *p == '+')
3386 {
3387 int negate = *p == '-';
3388 isn_T *isn;
3389
3390 // TODO: check type
3391 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3392 {
3393 --p;
3394 if (*p == '-')
3395 negate = !negate;
3396 }
3397 // only '-' has an effect, for '+' we only check the type
3398 if (negate)
3399 isn = generate_instr(cctx, ISN_NEGATENR);
3400 else
3401 isn = generate_instr(cctx, ISN_CHECKNR);
3402 if (isn == NULL)
3403 return FAIL;
3404 }
3405 else
3406 {
3407 int invert = TRUE;
3408
3409 while (p > start && p[-1] == '!')
3410 {
3411 --p;
3412 invert = !invert;
3413 }
3414 if (generate_2BOOL(cctx, invert) == FAIL)
3415 return FAIL;
3416 }
3417 }
3418 return OK;
3419}
3420
3421/*
3422 * Compile whatever comes after "name" or "name()".
3423 */
3424 static int
3425compile_subscript(
3426 char_u **arg,
3427 cctx_T *cctx,
3428 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003429 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003430 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431{
3432 for (;;)
3433 {
3434 if (**arg == '(')
3435 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003436 garray_T *stack = &cctx->ctx_type_stack;
3437 type_T *type;
3438 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003440 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003441 return FAIL;
3442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003444 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 *arg = skipwhite(*arg + 1);
3447 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3448 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003449 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 return FAIL;
3451 }
3452 else if (**arg == '-' && (*arg)[1] == '>')
3453 {
3454 char_u *p;
3455
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003456 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003457 return FAIL;
3458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 // something->method()
3460 // Apply the '!', '-' and '+' first:
3461 // -1.0->func() works like (-1.0)->func()
3462 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3463 return FAIL;
3464 *start_leader = end_leader; // don't apply again later
3465
3466 *arg = skipwhite(*arg + 2);
3467 if (**arg == '{')
3468 {
3469 // lambda call: list->{lambda}
3470 if (compile_lambda_call(arg, cctx) == FAIL)
3471 return FAIL;
3472 }
3473 else
3474 {
3475 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003476 p = *arg;
3477 if (ASCII_ISALPHA(*p) && p[1] == ':')
3478 p += 2;
3479 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 ;
3481 if (*p != '(')
3482 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003483 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 return FAIL;
3485 }
3486 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003487 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 return FAIL;
3489 }
3490 }
3491 else if (**arg == '[')
3492 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003493 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003494 type_T **typep;
3495
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003496 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003497 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003498 // TODO: blob index
3499 // TODO: more arguments
3500 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003501 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003502 return FAIL;
3503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003505 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 return FAIL;
3507
3508 if (**arg != ']')
3509 {
3510 emsg(_(e_missbrac));
3511 return FAIL;
3512 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003513 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003515 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3516 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003517 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003518 if ((*typep)->tt_type == VAR_LIST)
3519 *typep = (*typep)->tt_member;
3520 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3521 return FAIL;
3522 }
3523 else if ((*typep)->tt_type == VAR_DICT)
3524 {
3525 *typep = (*typep)->tt_member;
3526 if (may_generate_2STRING(-1, cctx) == FAIL)
3527 return FAIL;
3528 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3529 return FAIL;
3530 }
3531 else
3532 {
3533 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003534 return FAIL;
3535 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 }
3537 else if (**arg == '.' && (*arg)[1] != '.')
3538 {
3539 char_u *p;
3540
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003541 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003542 return FAIL;
3543
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 ++*arg;
3545 p = *arg;
3546 // dictionary member: dict.name
3547 if (eval_isnamec1(*p))
3548 while (eval_isnamec(*p))
3549 MB_PTR_ADV(p);
3550 if (p == *arg)
3551 {
3552 semsg(_(e_syntax_at), *arg);
3553 return FAIL;
3554 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003555 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 return FAIL;
3557 *arg = p;
3558 }
3559 else
3560 break;
3561 }
3562
3563 // TODO - see handle_subscript():
3564 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3565 // Don't do this when "Func" is already a partial that was bound
3566 // explicitly (pt_auto is FALSE).
3567
3568 return OK;
3569}
3570
3571/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003572 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3573 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003575 * If the value is a constant "ppconst->pp_ret" will be set.
3576 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003577 *
3578 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 */
3580
3581/*
3582 * number number constant
3583 * 0zFFFFFFFF Blob constant
3584 * "string" string constant
3585 * 'string' literal string constant
3586 * &option-name option value
3587 * @r register contents
3588 * identifier variable value
3589 * function() function call
3590 * $VAR environment variable
3591 * (expression) nested expression
3592 * [expr, expr] List
3593 * {key: val, key: val} Dictionary
3594 * #{key: val, key: val} Dictionary with literal keys
3595 *
3596 * Also handle:
3597 * ! in front logical NOT
3598 * - in front unary minus
3599 * + in front unary plus (ignored)
3600 * trailing (arg) funcref/partial call
3601 * trailing [] subscript in String or List
3602 * trailing .name entry in Dictionary
3603 * trailing ->name() method call
3604 */
3605 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003606compile_expr7(
3607 char_u **arg,
3608 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003609 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 char_u *start_leader, *end_leader;
3612 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003613 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003614 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615
3616 /*
3617 * Skip '!', '-' and '+' characters. They are handled later.
3618 */
3619 start_leader = *arg;
3620 while (**arg == '!' || **arg == '-' || **arg == '+')
3621 *arg = skipwhite(*arg + 1);
3622 end_leader = *arg;
3623
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003624 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 switch (**arg)
3626 {
3627 /*
3628 * Number constant.
3629 */
3630 case '0': // also for blob starting with 0z
3631 case '1':
3632 case '2':
3633 case '3':
3634 case '4':
3635 case '5':
3636 case '6':
3637 case '7':
3638 case '8':
3639 case '9':
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003640 case '.': if (get_number_tv(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 return FAIL;
3642 break;
3643
3644 /*
3645 * String constant: "string".
3646 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003647 case '"': if (get_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 return FAIL;
3649 break;
3650
3651 /*
3652 * Literal string constant: 'str''ing'.
3653 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003654 case '\'': if (get_lit_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 return FAIL;
3656 break;
3657
3658 /*
3659 * Constant Vim variable.
3660 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003661 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 ret = NOTDONE;
3663 break;
3664
3665 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003666 * "true" constant
3667 */
3668 case 't': if (STRNCMP(*arg, "true", 4) == 0
3669 && !eval_isnamec((*arg)[4]))
3670 {
3671 *arg += 4;
3672 rettv->v_type = VAR_BOOL;
3673 rettv->vval.v_number = VVAL_TRUE;
3674 }
3675 else
3676 ret = NOTDONE;
3677 break;
3678
3679 /*
3680 * "false" constant
3681 */
3682 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3683 && !eval_isnamec((*arg)[5]))
3684 {
3685 *arg += 5;
3686 rettv->v_type = VAR_BOOL;
3687 rettv->vval.v_number = VVAL_FALSE;
3688 }
3689 else
3690 ret = NOTDONE;
3691 break;
3692
3693 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 * List: [expr, expr]
3695 */
3696 case '[': ret = compile_list(arg, cctx);
3697 break;
3698
3699 /*
3700 * Dictionary: #{key: val, key: val}
3701 */
3702 case '#': if ((*arg)[1] == '{')
3703 {
3704 ++*arg;
3705 ret = compile_dict(arg, cctx, TRUE);
3706 }
3707 else
3708 ret = NOTDONE;
3709 break;
3710
3711 /*
3712 * Lambda: {arg, arg -> expr}
3713 * Dictionary: {'key': val, 'key': val}
3714 */
3715 case '{': {
3716 char_u *start = skipwhite(*arg + 1);
3717
3718 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003719 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003721 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 if (ret != FAIL && *start == '>')
3723 ret = compile_lambda(arg, cctx);
3724 else
3725 ret = compile_dict(arg, cctx, FALSE);
3726 }
3727 break;
3728
3729 /*
3730 * Option value: &name
3731 */
3732 case '&': ret = compile_get_option(arg, cctx);
3733 break;
3734
3735 /*
3736 * Environment variable: $VAR.
3737 */
3738 case '$': ret = compile_get_env(arg, cctx);
3739 break;
3740
3741 /*
3742 * Register contents: @r.
3743 */
3744 case '@': ret = compile_get_register(arg, cctx);
3745 break;
3746 /*
3747 * nested expression: (expression).
3748 */
3749 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003750
3751 // recursive!
3752 if (ppconst->pp_used <= PPSIZE - 10)
3753 {
3754 ret = compile_expr1(arg, cctx, ppconst);
3755 }
3756 else
3757 {
3758 // Not enough space in ppconst, flush constants.
3759 if (generate_ppconst(cctx, ppconst) == FAIL)
3760 return FAIL;
3761 ret = compile_expr0(arg, cctx);
3762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 *arg = skipwhite(*arg);
3764 if (**arg == ')')
3765 ++*arg;
3766 else if (ret == OK)
3767 {
3768 emsg(_(e_missing_close));
3769 ret = FAIL;
3770 }
3771 break;
3772
3773 default: ret = NOTDONE;
3774 break;
3775 }
3776 if (ret == FAIL)
3777 return FAIL;
3778
Bram Moolenaar1c747212020-05-09 18:28:34 +02003779 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 {
3781 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003782 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003784 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 return FAIL;
3786 }
3787 start_leader = end_leader; // don't apply again below
3788
Bram Moolenaara5565e42020-05-09 15:44:01 +02003789 if (cctx->ctx_skip == TRUE)
3790 clear_tv(rettv);
3791 else
3792 // A constant expression can possibly be handled compile time,
3793 // return the value instead of generating code.
3794 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 }
3796 else if (ret == NOTDONE)
3797 {
3798 char_u *p;
3799 int r;
3800
3801 if (!eval_isnamec1(**arg))
3802 {
3803 semsg(_("E1015: Name expected: %s"), *arg);
3804 return FAIL;
3805 }
3806
3807 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003808 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003810 {
3811 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3812 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003814 {
3815 if (generate_ppconst(cctx, ppconst) == FAIL)
3816 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003818 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 if (r == FAIL)
3820 return FAIL;
3821 }
3822
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003823 // Handle following "[]", ".member", etc.
3824 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003825 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003826 ppconst) == FAIL)
3827 return FAIL;
3828 if (ppconst->pp_used > 0)
3829 {
3830 // apply the '!', '-' and '+' before the constant
3831 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3832 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3833 return FAIL;
3834 return OK;
3835 }
3836 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003838 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839}
3840
3841/*
3842 * * number multiplication
3843 * / number division
3844 * % number modulo
3845 */
3846 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003847compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848{
3849 char_u *op;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003850 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003852 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003853 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 return FAIL;
3855
3856 /*
3857 * Repeat computing, until no "*", "/" or "%" is following.
3858 */
3859 for (;;)
3860 {
3861 op = skipwhite(*arg);
3862 if (*op != '*' && *op != '/' && *op != '%')
3863 break;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003864
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003865 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 {
3867 char_u buf[3];
3868
3869 vim_strncpy(buf, op, 1);
3870 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003871 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 }
3873 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003874 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003875 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003877 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003878 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003880
3881 if (ppconst->pp_used == ppconst_used + 2
3882 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3883 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003884 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003885 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3886 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003887 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003889 // both are numbers: compute the result
3890 switch (*op)
3891 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003892 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003893 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003894 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003895 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003896 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003897 break;
3898 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003899 tv1->vval.v_number = res;
3900 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003901 }
3902 else
3903 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003904 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003905 generate_two_op(cctx, op);
3906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 }
3908
3909 return OK;
3910}
3911
3912/*
3913 * + number addition
3914 * - number subtraction
3915 * .. string concatenation
3916 */
3917 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003918compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919{
3920 char_u *op;
3921 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003922 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923
3924 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003925 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 return FAIL;
3927
3928 /*
3929 * Repeat computing, until no "+", "-" or ".." is following.
3930 */
3931 for (;;)
3932 {
3933 op = skipwhite(*arg);
3934 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3935 break;
3936 oplen = (*op == '.' ? 2 : 1);
3937
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003938 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 {
3940 char_u buf[3];
3941
3942 vim_strncpy(buf, op, oplen);
3943 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003944 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 }
3946
3947 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003948 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003949 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003951 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003952 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 return FAIL;
3954
Bram Moolenaara5565e42020-05-09 15:44:01 +02003955 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003956 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003957 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3958 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3959 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3960 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003962 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3963 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003964
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003965 // concat/subtract/add constant numbers
3966 if (*op == '+')
3967 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3968 else if (*op == '-')
3969 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3970 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003971 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003972 // concatenate constant strings
3973 char_u *s1 = tv1->vval.v_string;
3974 char_u *s2 = tv2->vval.v_string;
3975 size_t len1 = STRLEN(s1);
3976
3977 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3978 if (tv1->vval.v_string == NULL)
3979 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003980 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003981 return FAIL;
3982 }
3983 mch_memmove(tv1->vval.v_string, s1, len1);
3984 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003985 vim_free(s1);
3986 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003987 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003988 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 }
3990 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003991 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003992 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003993 if (*op == '.')
3994 {
3995 if (may_generate_2STRING(-2, cctx) == FAIL
3996 || may_generate_2STRING(-1, cctx) == FAIL)
3997 return FAIL;
3998 generate_instr_drop(cctx, ISN_CONCAT, 1);
3999 }
4000 else
4001 generate_two_op(cctx, op);
4002 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 }
4004
4005 return OK;
4006}
4007
4008/*
4009 * expr5a == expr5b
4010 * expr5a =~ expr5b
4011 * expr5a != expr5b
4012 * expr5a !~ expr5b
4013 * expr5a > expr5b
4014 * expr5a >= expr5b
4015 * expr5a < expr5b
4016 * expr5a <= expr5b
4017 * expr5a is expr5b
4018 * expr5a isnot expr5b
4019 *
4020 * Produces instructions:
4021 * EVAL expr5a Push result of "expr5a"
4022 * EVAL expr5b Push result of "expr5b"
4023 * COMPARE one of the compare instructions
4024 */
4025 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004026compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027{
4028 exptype_T type = EXPR_UNKNOWN;
4029 char_u *p;
4030 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004032 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033
4034 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004035 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 return FAIL;
4037
4038 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004039 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040
4041 /*
4042 * If there is a comparative operator, use it.
4043 */
4044 if (type != EXPR_UNKNOWN)
4045 {
4046 int ic = FALSE; // Default: do not ignore case
4047
4048 if (type_is && (p[len] == '?' || p[len] == '#'))
4049 {
4050 semsg(_(e_invexpr2), *arg);
4051 return FAIL;
4052 }
4053 // extra question mark appended: ignore case
4054 if (p[len] == '?')
4055 {
4056 ic = TRUE;
4057 ++len;
4058 }
4059 // extra '#' appended: match case (ignored)
4060 else if (p[len] == '#')
4061 ++len;
4062 // nothing appended: match case
4063
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004064 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 {
4066 char_u buf[7];
4067
4068 vim_strncpy(buf, p, len);
4069 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004070 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 }
4072
4073 // get the second variable
4074 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004075 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004076 return FAIL;
4077
Bram Moolenaara5565e42020-05-09 15:44:01 +02004078 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 return FAIL;
4080
Bram Moolenaara5565e42020-05-09 15:44:01 +02004081 if (ppconst->pp_used == ppconst_used + 2)
4082 {
4083 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4084 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4085 int ret;
4086
4087 // Both sides are a constant, compute the result now.
4088 // First check for a valid combination of types, this is more
4089 // strict than typval_compare().
4090 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4091 ret = FAIL;
4092 else
4093 {
4094 ret = typval_compare(tv1, tv2, type, ic);
4095 tv1->v_type = VAR_BOOL;
4096 tv1->vval.v_number = tv1->vval.v_number
4097 ? VVAL_TRUE : VVAL_FALSE;
4098 clear_tv(tv2);
4099 --ppconst->pp_used;
4100 }
4101 return ret;
4102 }
4103
4104 generate_ppconst(cctx, ppconst);
4105 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 }
4107
4108 return OK;
4109}
4110
Bram Moolenaar7f141552020-05-09 17:35:53 +02004111static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113/*
4114 * Compile || or &&.
4115 */
4116 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004117compile_and_or(
4118 char_u **arg,
4119 cctx_T *cctx,
4120 char *op,
4121 ppconst_T *ppconst,
4122 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123{
4124 char_u *p = skipwhite(*arg);
4125 int opchar = *op;
4126
4127 if (p[0] == opchar && p[1] == opchar)
4128 {
4129 garray_T *instr = &cctx->ctx_instr;
4130 garray_T end_ga;
4131
4132 /*
4133 * Repeat until there is no following "||" or "&&"
4134 */
4135 ga_init2(&end_ga, sizeof(int), 10);
4136 while (p[0] == opchar && p[1] == opchar)
4137 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004138 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4139 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004141 return FAIL;
4142 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143
Bram Moolenaara5565e42020-05-09 15:44:01 +02004144 // TODO: use ppconst if the value is a constant
4145 generate_ppconst(cctx, ppconst);
4146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 if (ga_grow(&end_ga, 1) == FAIL)
4148 {
4149 ga_clear(&end_ga);
4150 return FAIL;
4151 }
4152 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4153 ++end_ga.ga_len;
4154 generate_JUMP(cctx, opchar == '|'
4155 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4156
4157 // eval the next expression
4158 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004159 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004160 return FAIL;
4161
Bram Moolenaara5565e42020-05-09 15:44:01 +02004162 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4163 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 {
4165 ga_clear(&end_ga);
4166 return FAIL;
4167 }
4168 p = skipwhite(*arg);
4169 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004170 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171
4172 // Fill in the end label in all jumps.
4173 while (end_ga.ga_len > 0)
4174 {
4175 isn_T *isn;
4176
4177 --end_ga.ga_len;
4178 isn = ((isn_T *)instr->ga_data)
4179 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4180 isn->isn_arg.jump.jump_where = instr->ga_len;
4181 }
4182 ga_clear(&end_ga);
4183 }
4184
4185 return OK;
4186}
4187
4188/*
4189 * expr4a && expr4a && expr4a logical AND
4190 *
4191 * Produces instructions:
4192 * EVAL expr4a Push result of "expr4a"
4193 * JUMP_AND_KEEP_IF_FALSE end
4194 * EVAL expr4b Push result of "expr4b"
4195 * JUMP_AND_KEEP_IF_FALSE end
4196 * EVAL expr4c Push result of "expr4c"
4197 * end:
4198 */
4199 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004200compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004202 int ppconst_used = ppconst->pp_used;
4203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004205 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 return FAIL;
4207
4208 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004209 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210}
4211
4212/*
4213 * expr3a || expr3b || expr3c logical OR
4214 *
4215 * Produces instructions:
4216 * EVAL expr3a Push result of "expr3a"
4217 * JUMP_AND_KEEP_IF_TRUE end
4218 * EVAL expr3b Push result of "expr3b"
4219 * JUMP_AND_KEEP_IF_TRUE end
4220 * EVAL expr3c Push result of "expr3c"
4221 * end:
4222 */
4223 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004224compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004226 int ppconst_used = ppconst->pp_used;
4227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004229 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 return FAIL;
4231
4232 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004233 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234}
4235
4236/*
4237 * Toplevel expression: expr2 ? expr1a : expr1b
4238 *
4239 * Produces instructions:
4240 * EVAL expr2 Push result of "expr"
4241 * JUMP_IF_FALSE alt jump if false
4242 * EVAL expr1a
4243 * JUMP_ALWAYS end
4244 * alt: EVAL expr1b
4245 * end:
4246 */
4247 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004248compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249{
4250 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004251 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252
Bram Moolenaar61a89812020-05-07 16:58:17 +02004253 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004254 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 return FAIL;
4256
4257 p = skipwhite(*arg);
4258 if (*p == '?')
4259 {
4260 garray_T *instr = &cctx->ctx_instr;
4261 garray_T *stack = &cctx->ctx_type_stack;
4262 int alt_idx = instr->ga_len;
4263 int end_idx;
4264 isn_T *isn;
4265 type_T *type1;
4266 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004267 int has_const_expr = FALSE;
4268 int const_value = FALSE;
4269 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004271 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4272 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004274 return FAIL;
4275 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276
Bram Moolenaara5565e42020-05-09 15:44:01 +02004277 if (ppconst->pp_used == ppconst_used + 1)
4278 {
4279 // the condition is a constant, we know whether the ? or the :
4280 // expression is to be evaluated.
4281 has_const_expr = TRUE;
4282 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4283 clear_tv(&ppconst->pp_tv[ppconst_used]);
4284 --ppconst->pp_used;
4285 cctx->ctx_skip = save_skip == TRUE || !const_value;
4286 }
4287 else
4288 {
4289 generate_ppconst(cctx, ppconst);
4290 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4291 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292
4293 // evaluate the second expression; any type is accepted
4294 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004295 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004296 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004297 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004298 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299
Bram Moolenaara5565e42020-05-09 15:44:01 +02004300 if (!has_const_expr)
4301 {
4302 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303
Bram Moolenaara5565e42020-05-09 15:44:01 +02004304 // remember the type and drop it
4305 --stack->ga_len;
4306 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307
Bram Moolenaara5565e42020-05-09 15:44:01 +02004308 end_idx = instr->ga_len;
4309 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4310
4311 // jump here from JUMP_IF_FALSE
4312 isn = ((isn_T *)instr->ga_data) + alt_idx;
4313 isn->isn_arg.jump.jump_where = instr->ga_len;
4314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315
4316 // Check for the ":".
4317 p = skipwhite(*arg);
4318 if (*p != ':')
4319 {
4320 emsg(_(e_missing_colon));
4321 return FAIL;
4322 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004323 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4324 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004326 return FAIL;
4327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328
4329 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004330 if (has_const_expr)
4331 cctx->ctx_skip = save_skip == TRUE || const_value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004333 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004334 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004335 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004336 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337
Bram Moolenaara5565e42020-05-09 15:44:01 +02004338 if (!has_const_expr)
4339 {
4340 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341
Bram Moolenaara5565e42020-05-09 15:44:01 +02004342 // If the types differ, the result has a more generic type.
4343 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4344 common_type(type1, type2, &type2, cctx->ctx_type_list);
4345
4346 // jump here from JUMP_ALWAYS
4347 isn = ((isn_T *)instr->ga_data) + end_idx;
4348 isn->isn_arg.jump.jump_where = instr->ga_len;
4349 }
4350
4351 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352 }
4353 return OK;
4354}
4355
4356/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004357 * Toplevel expression.
4358 */
4359 static int
4360compile_expr0(char_u **arg, cctx_T *cctx)
4361{
4362 ppconst_T ppconst;
4363
4364 CLEAR_FIELD(ppconst);
4365 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4366 {
4367 clear_ppconst(&ppconst);
4368 return FAIL;
4369 }
4370 if (generate_ppconst(cctx, &ppconst) == FAIL)
4371 return FAIL;
4372 return OK;
4373}
4374
4375/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 * compile "return [expr]"
4377 */
4378 static char_u *
4379compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4380{
4381 char_u *p = arg;
4382 garray_T *stack = &cctx->ctx_type_stack;
4383 type_T *stack_type;
4384
4385 if (*p != NUL && *p != '|' && *p != '\n')
4386 {
4387 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004388 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 return NULL;
4390
4391 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4392 if (set_return_type)
4393 cctx->ctx_ufunc->uf_ret_type = stack_type;
4394 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4395 == FAIL)
4396 return NULL;
4397 }
4398 else
4399 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004400 // "set_return_type" cannot be TRUE, only used for a lambda which
4401 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004402 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4403 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 {
4405 emsg(_("E1003: Missing return value"));
4406 return NULL;
4407 }
4408
4409 // No argument, return zero.
4410 generate_PUSHNR(cctx, 0);
4411 }
4412
4413 if (generate_instr(cctx, ISN_RETURN) == NULL)
4414 return NULL;
4415
4416 // "return val | endif" is possible
4417 return skipwhite(p);
4418}
4419
4420/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004421 * Get a line from the compilation context, compatible with exarg_T getline().
4422 * Return a pointer to the line in allocated memory.
4423 * Return NULL for end-of-file or some error.
4424 */
4425 static char_u *
4426exarg_getline(
4427 int c UNUSED,
4428 void *cookie,
4429 int indent UNUSED,
4430 int do_concat UNUSED)
4431{
4432 cctx_T *cctx = (cctx_T *)cookie;
4433
4434 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4435 {
4436 iemsg("Heredoc got to end");
4437 return NULL;
4438 }
4439 ++cctx->ctx_lnum;
4440 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4441 [cctx->ctx_lnum]);
4442}
4443
4444/*
4445 * Compile a nested :def command.
4446 */
4447 static char_u *
4448compile_nested_function(exarg_T *eap, cctx_T *cctx)
4449{
4450 char_u *name_start = eap->arg;
4451 char_u *name_end = to_name_end(eap->arg, FALSE);
4452 char_u *name = get_lambda_name();
4453 lvar_T *lvar;
4454 ufunc_T *ufunc;
4455
4456 eap->arg = name_end;
4457 eap->getline = exarg_getline;
4458 eap->cookie = cctx;
4459 eap->skip = cctx->ctx_skip == TRUE;
4460 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004461 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004462
Bram Moolenaar822ba242020-05-24 23:00:18 +02004463 if (ufunc == NULL)
4464 return NULL;
4465 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED
4466 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004467 return NULL;
4468
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004469 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004470 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004471 TRUE, ufunc->uf_func_type);
4472
4473 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4474 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4475 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004476
Bram Moolenaar61a89812020-05-07 16:58:17 +02004477 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004478 return (char_u *)"";
4479}
4480
4481/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 * Return the length of an assignment operator, or zero if there isn't one.
4483 */
4484 int
4485assignment_len(char_u *p, int *heredoc)
4486{
4487 if (*p == '=')
4488 {
4489 if (p[1] == '<' && p[2] == '<')
4490 {
4491 *heredoc = TRUE;
4492 return 3;
4493 }
4494 return 1;
4495 }
4496 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4497 return 2;
4498 if (STRNCMP(p, "..=", 3) == 0)
4499 return 3;
4500 return 0;
4501}
4502
4503// words that cannot be used as a variable
4504static char *reserved[] = {
4505 "true",
4506 "false",
4507 NULL
4508};
4509
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004510typedef enum {
4511 dest_local,
4512 dest_option,
4513 dest_env,
4514 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004515 dest_buffer,
4516 dest_window,
4517 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004518 dest_vimvar,
4519 dest_script,
4520 dest_reg,
4521} assign_dest_T;
4522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004524 * Generate the load instruction for "name".
4525 */
4526 static void
4527generate_loadvar(
4528 cctx_T *cctx,
4529 assign_dest_T dest,
4530 char_u *name,
4531 lvar_T *lvar,
4532 type_T *type)
4533{
4534 switch (dest)
4535 {
4536 case dest_option:
4537 // TODO: check the option exists
4538 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4539 break;
4540 case dest_global:
4541 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4542 break;
4543 case dest_buffer:
4544 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4545 break;
4546 case dest_window:
4547 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4548 break;
4549 case dest_tab:
4550 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4551 break;
4552 case dest_script:
4553 compile_load_scriptvar(cctx,
4554 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4555 break;
4556 case dest_env:
4557 // Include $ in the name here
4558 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4559 break;
4560 case dest_reg:
4561 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4562 break;
4563 case dest_vimvar:
4564 generate_LOADV(cctx, name + 2, TRUE);
4565 break;
4566 case dest_local:
4567 if (lvar->lv_from_outer)
4568 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4569 NULL, type);
4570 else
4571 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4572 break;
4573 }
4574}
4575
4576/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 * compile "let var [= expr]", "const var = expr" and "var = expr"
4578 * "arg" points to "var".
4579 */
4580 static char_u *
4581compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4582{
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004583 char_u *var_end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004585 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 char_u *ret = NULL;
4587 int var_count = 0;
4588 int semicolon = 0;
4589 size_t varlen;
4590 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004591 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004592 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004595 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004597 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 int oplen = 0;
4599 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004600 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004601 type_T *member_type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004602 lvar_T *lvar = NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004603 lvar_T arg_lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 char_u *name;
4605 char_u *sp;
4606 int has_type = FALSE;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004607 int has_index = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4609 int instr_count = -1;
4610
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004611 var_end = skip_var_list(arg, FALSE, &var_count, &semicolon);
4612 if (var_end == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 return NULL;
4614 if (var_count > 0)
4615 {
4616 // TODO: let [var, var] = list
4617 emsg("Cannot handle a list yet");
4618 return NULL;
4619 }
4620
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004621 p = (*arg == '&' || *arg == '$' || *arg == '@') ? arg + 1 : arg;
4622 p = to_name_end(p, TRUE);
4623
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004624 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004625 if (is_decl && var_end == arg + 2 && var_end[-1] == ':')
4626 --var_end;
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004627 if (is_decl && p == arg + 2 && p[-1] == ':')
4628 --p;
4629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 varlen = p - arg;
4631 name = vim_strnsave(arg, (int)varlen);
4632 if (name == NULL)
4633 return NULL;
4634
Bram Moolenaar080457c2020-03-03 21:53:32 +01004635 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004637 if (*arg == '&')
4638 {
4639 int cc;
4640 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641
Bram Moolenaar080457c2020-03-03 21:53:32 +01004642 dest = dest_option;
4643 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004645 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004646 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 if (is_decl)
4649 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004650 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 goto theend;
4652 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004653 p = arg;
4654 p = find_option_end(&p, &opt_flags);
4655 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004657 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004658 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004659 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004660 }
4661 cc = *p;
4662 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004663 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004664 *p = cc;
4665 if (opt_type == -3)
4666 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004667 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004668 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004669 }
4670 if (opt_type == -2 || opt_type == 0)
4671 type = &t_string;
4672 else
4673 type = &t_number; // both number and boolean option
4674 }
4675 else if (*arg == '$')
4676 {
4677 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004678 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004679 if (is_decl)
4680 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004681 semsg(_("E1065: Cannot declare an environment variable: %s"),
4682 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004683 goto theend;
4684 }
4685 }
4686 else if (*arg == '@')
4687 {
4688 if (!valid_yank_reg(arg[1], TRUE))
4689 {
4690 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004691 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004692 }
4693 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004694 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004695 if (is_decl)
4696 {
4697 semsg(_("E1066: Cannot declare a register: %s"), name);
4698 goto theend;
4699 }
4700 }
4701 else if (STRNCMP(arg, "g:", 2) == 0)
4702 {
4703 dest = dest_global;
4704 if (is_decl)
4705 {
4706 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4707 goto theend;
4708 }
4709 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004710 else if (STRNCMP(arg, "b:", 2) == 0)
4711 {
4712 dest = dest_buffer;
4713 if (is_decl)
4714 {
4715 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4716 goto theend;
4717 }
4718 }
4719 else if (STRNCMP(arg, "w:", 2) == 0)
4720 {
4721 dest = dest_window;
4722 if (is_decl)
4723 {
4724 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4725 goto theend;
4726 }
4727 }
4728 else if (STRNCMP(arg, "t:", 2) == 0)
4729 {
4730 dest = dest_tab;
4731 if (is_decl)
4732 {
4733 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4734 goto theend;
4735 }
4736 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004737 else if (STRNCMP(arg, "v:", 2) == 0)
4738 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004739 typval_T *vtv;
4740 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004741
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004742 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004743 if (vimvaridx < 0)
4744 {
4745 semsg(_(e_var_notfound), arg);
4746 goto theend;
4747 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004748 // We use the current value of "sandbox" here, is that OK?
4749 if (var_check_ro(di_flags, name, FALSE))
4750 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004751 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004752 vtv = get_vim_var_tv(vimvaridx);
4753 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004754 if (is_decl)
4755 {
4756 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4757 goto theend;
4758 }
4759 }
4760 else
4761 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004762 int idx;
4763
Bram Moolenaar080457c2020-03-03 21:53:32 +01004764 for (idx = 0; reserved[idx] != NULL; ++idx)
4765 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004767 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768 goto theend;
4769 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004770
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004771 lvar = lookup_local(arg, varlen, cctx);
Bram Moolenaarbc38f252020-05-10 23:20:06 +02004772 if (lvar == NULL)
4773 {
4774 CLEAR_FIELD(arg_lvar);
4775 if (lookup_arg(arg, varlen,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004776 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4777 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004778 {
Bram Moolenaarbc38f252020-05-10 23:20:06 +02004779 if (is_decl)
4780 {
4781 semsg(_(e_used_as_arg), name);
4782 goto theend;
4783 }
4784 lvar = &arg_lvar;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004785 }
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004786 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004787 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004789 if (is_decl)
4790 {
4791 semsg(_("E1017: Variable already declared: %s"), name);
4792 goto theend;
4793 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004794 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004795 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004796 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4797 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004798 }
4799 }
4800 else if (STRNCMP(arg, "s:", 2) == 0
4801 || lookup_script(arg, varlen) == OK
4802 || find_imported(arg, varlen, cctx) != NULL)
4803 {
4804 dest = dest_script;
4805 if (is_decl)
4806 {
4807 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004808 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004809 goto theend;
4810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004812 else if (name[1] == ':' && name[2] != NUL)
4813 {
4814 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
4815 goto theend;
4816 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004817 else if (!is_decl)
4818 {
4819 semsg(_("E1089: unknown variable: %s"), name);
4820 goto theend;
4821 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 }
4823 }
4824
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004825 // handle "a:name" as a name, not index "name" on "a"
4826 if (varlen > 1 || arg[varlen] != ':')
4827 p = var_end;
4828
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004829 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 {
4831 if (is_decl && *p == ':')
4832 {
4833 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004834 if (!VIM_ISWHITE(p[1]))
4835 {
4836 semsg(_(e_white_after), ":");
4837 goto theend;
4838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 p = skipwhite(p + 1);
4840 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841 has_type = TRUE;
4842 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004843 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 }
4846
4847 sp = p;
4848 p = skipwhite(p);
4849 op = p;
4850 oplen = assignment_len(p, &heredoc);
4851 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4852 {
4853 char_u buf[4];
4854
4855 vim_strncpy(buf, op, oplen);
4856 semsg(_(e_white_both), buf);
4857 }
4858
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004859 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004860 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004862 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 goto theend;
4864 }
4865
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004866 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 {
4868 if (oplen > 1 && !heredoc)
4869 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004870 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4872 name);
4873 goto theend;
4874 }
4875
4876 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004877 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004878 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004879 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4880 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004882 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 }
4884
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004885 member_type = type;
4886 if (var_end > arg + varlen)
4887 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004888 // Something follows after the variable: "var[idx]".
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004889 if (is_decl)
4890 {
4891 emsg(_("E1087: cannot use an index when declaring a variable"));
4892 goto theend;
4893 }
4894
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004895 if (arg[varlen] == '[')
4896 {
4897 has_index = TRUE;
4898 if (type->tt_member == NULL)
4899 {
4900 semsg(_("E1088: cannot use an index on %s"), name);
4901 goto theend;
4902 }
4903 member_type = type->tt_member;
4904 }
4905 else
4906 {
4907 semsg("Not supported yet: %s", arg);
4908 goto theend;
4909 }
4910 }
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004911 else if (lvar == &arg_lvar)
4912 {
4913 semsg(_("E1090: Cannot assign to argument %s"), name);
4914 goto theend;
4915 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 if (heredoc)
4918 {
4919 list_T *l;
4920 listitem_T *li;
4921
4922 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004923 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004925 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926
4927 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004928 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929 {
4930 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4931 li->li_tv.vval.v_string = NULL;
4932 }
4933 generate_NEWLIST(cctx, l->lv_len);
4934 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004935 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 list_free(l);
4937 p += STRLEN(p);
4938 }
4939 else if (oplen > 0)
4940 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004941 int r;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004942
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 // for "+=", "*=", "..=" etc. first load the current value
4944 if (*op != '=')
4945 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004946 generate_loadvar(cctx, dest, name, lvar, type);
4947
4948 if (has_index)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004949 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004950 // TODO: get member from list or dict
4951 emsg("Index with operation not supported yet");
4952 goto theend;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 }
4955
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004956 // Compile the expression. Temporarily hide the new local variable
4957 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004958 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004959 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 instr_count = instr->ga_len;
4961 p = skipwhite(p + oplen);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004962 r = compile_expr0(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004963 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004964 ++cctx->ctx_locals.ga_len;
4965 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 goto theend;
4967
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004968 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004970 type_T *stacktype;
4971
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004972 stacktype = stack->ga_len == 0 ? &t_void
4973 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004974 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004976 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004978 if (stacktype->tt_type == VAR_VOID)
4979 {
4980 emsg(_("E1031: Cannot use void value"));
4981 goto theend;
4982 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004983 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004984 {
4985 // An empty list or dict has a &t_void member, for a
4986 // variable that implies &t_any.
4987 if (stacktype == &t_list_empty)
4988 lvar->lv_type = &t_list_any;
4989 else if (stacktype == &t_dict_empty)
4990 lvar->lv_type = &t_dict_any;
4991 else
4992 lvar->lv_type = stacktype;
4993 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004994 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004995 else
4996 {
4997 type_T *use_type = lvar->lv_type;
4998
4999 if (has_index)
5000 {
5001 use_type = use_type->tt_member;
5002 if (use_type == NULL)
5003 use_type = &t_void;
5004 }
5005 if (need_type(stacktype, use_type, -1, cctx) == FAIL)
5006 goto theend;
5007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 }
Bram Moolenaara6e67e42020-05-15 23:36:40 +02005009 else if (*p != '=' && need_type(stacktype, member_type, -1,
5010 cctx) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005011 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 }
5013 }
5014 else if (cmdidx == CMD_const)
5015 {
5016 emsg(_("E1021: const requires a value"));
5017 goto theend;
5018 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005019 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 {
5021 emsg(_("E1022: type or initialization required"));
5022 goto theend;
5023 }
5024 else
5025 {
5026 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027 if (ga_grow(instr, 1) == FAIL)
5028 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005029 switch (member_type->tt_type)
Bram Moolenaar04d05222020-02-06 22:06:54 +01005030 {
5031 case VAR_BOOL:
5032 generate_PUSHBOOL(cctx, VVAL_FALSE);
5033 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005034 case VAR_FLOAT:
5035#ifdef FEAT_FLOAT
5036 generate_PUSHF(cctx, 0.0);
5037#endif
5038 break;
5039 case VAR_STRING:
5040 generate_PUSHS(cctx, NULL);
5041 break;
5042 case VAR_BLOB:
5043 generate_PUSHBLOB(cctx, NULL);
5044 break;
5045 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005046 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005047 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005048 case VAR_LIST:
5049 generate_NEWLIST(cctx, 0);
5050 break;
5051 case VAR_DICT:
5052 generate_NEWDICT(cctx, 0);
5053 break;
5054 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005055 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005056 break;
5057 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005058 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005059 break;
5060 case VAR_NUMBER:
5061 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005062 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02005063 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01005064 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02005065 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01005066 generate_PUSHNR(cctx, 0);
5067 break;
5068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005070 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071
5072 if (oplen > 0 && *op != '=')
5073 {
5074 type_T *expected = &t_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 type_T *stacktype;
5076
5077 // TODO: if type is known use float or any operation
5078
5079 if (*op == '.')
5080 expected = &t_string;
5081 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5082 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5083 goto theend;
5084
5085 if (*op == '.')
5086 generate_instr_drop(cctx, ISN_CONCAT, 1);
5087 else
5088 {
5089 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5090
5091 if (isn == NULL)
5092 goto theend;
5093 switch (*op)
5094 {
5095 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5096 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5097 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5098 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5099 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5100 }
5101 }
5102 }
5103
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005104 if (has_index)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005106 int r;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005107
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005108 // Compile the "idx" in "var[idx]".
5109 if (new_local)
5110 --cctx->ctx_locals.ga_len;
5111 p = skipwhite(arg + varlen + 1);
5112 r = compile_expr0(&p, cctx);
5113 if (new_local)
5114 ++cctx->ctx_locals.ga_len;
5115 if (r == FAIL)
5116 goto theend;
5117 if (*skipwhite(p) != ']')
5118 {
5119 emsg(_(e_missbrac));
5120 goto theend;
5121 }
5122 if (type->tt_type == VAR_DICT
5123 && may_generate_2STRING(-1, cctx) == FAIL)
5124 goto theend;
5125 if (type->tt_type == VAR_LIST
5126 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
5127 != VAR_NUMBER)
5128 {
5129 emsg(_(e_number_exp));
5130 goto theend;
5131 }
5132
5133 // Load the dict or list. On the stack we then have:
5134 // - value
5135 // - index
5136 // - variable
5137 generate_loadvar(cctx, dest, name, lvar, type);
5138
5139 if (type->tt_type == VAR_LIST)
5140 {
5141 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5142 return FAIL;
5143 }
5144 else if (type->tt_type == VAR_DICT)
5145 {
5146 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5147 return FAIL;
5148 }
5149 else
5150 {
5151 emsg(_(e_listreq));
5152 goto theend;
5153 }
5154 }
5155 else
5156 {
5157 switch (dest)
5158 {
5159 case dest_option:
5160 generate_STOREOPT(cctx, name + 1, opt_flags);
5161 break;
5162 case dest_global:
5163 // include g: with the name, easier to execute that way
5164 generate_STORE(cctx, ISN_STOREG, 0, name);
5165 break;
5166 case dest_buffer:
5167 // include b: with the name, easier to execute that way
5168 generate_STORE(cctx, ISN_STOREB, 0, name);
5169 break;
5170 case dest_window:
5171 // include w: with the name, easier to execute that way
5172 generate_STORE(cctx, ISN_STOREW, 0, name);
5173 break;
5174 case dest_tab:
5175 // include t: with the name, easier to execute that way
5176 generate_STORE(cctx, ISN_STORET, 0, name);
5177 break;
5178 case dest_env:
5179 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5180 break;
5181 case dest_reg:
5182 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5183 break;
5184 case dest_vimvar:
5185 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5186 break;
5187 case dest_script:
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005188 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005189 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5190 imported_T *import = NULL;
5191 int sid = current_sctx.sc_sid;
5192 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005194 if (name[1] != ':')
5195 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005196 import = find_imported(name, 0, cctx);
5197 if (import != NULL)
5198 sid = import->imp_sid;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005199 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005200
5201 idx = get_script_item_idx(sid, rawname, TRUE);
5202 // TODO: specific type
5203 if (idx < 0)
5204 {
5205 char_u *name_s = name;
5206
5207 // Include s: in the name for store_var()
5208 if (name[1] != ':')
5209 {
5210 int len = (int)STRLEN(name) + 3;
5211
5212 name_s = alloc(len);
5213 if (name_s == NULL)
5214 name_s = name;
5215 else
5216 vim_snprintf((char *)name_s, len, "s:%s", name);
5217 }
5218 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
5219 &t_any);
5220 if (name_s != name)
5221 vim_free(name_s);
5222 }
5223 else
5224 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005225 sid, idx, &t_any);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005226 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005227 break;
5228 case dest_local:
5229 if (lvar != NULL)
5230 {
5231 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5232
5233 // optimization: turn "var = 123" from ISN_PUSHNR +
5234 // ISN_STORE into ISN_STORENR
5235 if (!lvar->lv_from_outer && instr->ga_len == instr_count + 1
5236 && isn->isn_type == ISN_PUSHNR)
5237 {
5238 varnumber_T val = isn->isn_arg.number;
5239
5240 isn->isn_type = ISN_STORENR;
5241 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5242 isn->isn_arg.storenr.stnr_val = val;
5243 if (stack->ga_len > 0)
5244 --stack->ga_len;
5245 }
5246 else if (lvar->lv_from_outer)
5247 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
5248 NULL);
5249 else
5250 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5251 }
5252 break;
5253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005255 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256
5257theend:
5258 vim_free(name);
5259 return ret;
5260}
5261
5262/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005263 * Check if "name" can be "unlet".
5264 */
5265 int
5266check_vim9_unlet(char_u *name)
5267{
5268 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5269 {
5270 semsg(_("E1081: Cannot unlet %s"), name);
5271 return FAIL;
5272 }
5273 return OK;
5274}
5275
5276/*
5277 * Callback passed to ex_unletlock().
5278 */
5279 static int
5280compile_unlet(
5281 lval_T *lvp,
5282 char_u *name_end,
5283 exarg_T *eap,
5284 int deep UNUSED,
5285 void *coookie)
5286{
5287 cctx_T *cctx = coookie;
5288
5289 if (lvp->ll_tv == NULL)
5290 {
5291 char_u *p = lvp->ll_name;
5292 int cc = *name_end;
5293 int ret = OK;
5294
5295 // Normal name. Only supports g:, w:, t: and b: namespaces.
5296 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005297 if (*p == '$')
5298 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5299 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005300 ret = FAIL;
5301 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005302 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005303
5304 *name_end = cc;
5305 return ret;
5306 }
5307
5308 // TODO: unlet {list}[idx]
5309 // TODO: unlet {dict}[key]
5310 emsg("Sorry, :unlet not fully implemented yet");
5311 return FAIL;
5312}
5313
5314/*
5315 * compile "unlet var", "lock var" and "unlock var"
5316 * "arg" points to "var".
5317 */
5318 static char_u *
5319compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5320{
5321 char_u *p = arg;
5322
5323 if (eap->cmdidx != CMD_unlet)
5324 {
5325 emsg("Sorry, :lock and unlock not implemented yet");
5326 return NULL;
5327 }
5328
5329 if (*p == '!')
5330 {
5331 p = skipwhite(p + 1);
5332 eap->forceit = TRUE;
5333 }
5334
5335 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5336 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5337}
5338
5339/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005340 * Compile an :import command.
5341 */
5342 static char_u *
5343compile_import(char_u *arg, cctx_T *cctx)
5344{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005345 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346}
5347
5348/*
5349 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5350 */
5351 static int
5352compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5353{
5354 garray_T *instr = &cctx->ctx_instr;
5355 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5356
5357 if (endlabel == NULL)
5358 return FAIL;
5359 endlabel->el_next = *el;
5360 *el = endlabel;
5361 endlabel->el_end_label = instr->ga_len;
5362
5363 generate_JUMP(cctx, when, 0);
5364 return OK;
5365}
5366
5367 static void
5368compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5369{
5370 garray_T *instr = &cctx->ctx_instr;
5371
5372 while (*el != NULL)
5373 {
5374 endlabel_T *cur = (*el);
5375 isn_T *isn;
5376
5377 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5378 isn->isn_arg.jump.jump_where = instr->ga_len;
5379 *el = cur->el_next;
5380 vim_free(cur);
5381 }
5382}
5383
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005384 static void
5385compile_free_jump_to_end(endlabel_T **el)
5386{
5387 while (*el != NULL)
5388 {
5389 endlabel_T *cur = (*el);
5390
5391 *el = cur->el_next;
5392 vim_free(cur);
5393 }
5394}
5395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396/*
5397 * Create a new scope and set up the generic items.
5398 */
5399 static scope_T *
5400new_scope(cctx_T *cctx, scopetype_T type)
5401{
5402 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5403
5404 if (scope == NULL)
5405 return NULL;
5406 scope->se_outer = cctx->ctx_scope;
5407 cctx->ctx_scope = scope;
5408 scope->se_type = type;
5409 scope->se_local_count = cctx->ctx_locals.ga_len;
5410 return scope;
5411}
5412
5413/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005414 * Free the current scope and go back to the outer scope.
5415 */
5416 static void
5417drop_scope(cctx_T *cctx)
5418{
5419 scope_T *scope = cctx->ctx_scope;
5420
5421 if (scope == NULL)
5422 {
5423 iemsg("calling drop_scope() without a scope");
5424 return;
5425 }
5426 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005427 switch (scope->se_type)
5428 {
5429 case IF_SCOPE:
5430 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5431 case FOR_SCOPE:
5432 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5433 case WHILE_SCOPE:
5434 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5435 case TRY_SCOPE:
5436 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5437 case NO_SCOPE:
5438 case BLOCK_SCOPE:
5439 break;
5440 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005441 vim_free(scope);
5442}
5443
5444/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005445 * compile "if expr"
5446 *
5447 * "if expr" Produces instructions:
5448 * EVAL expr Push result of "expr"
5449 * JUMP_IF_FALSE end
5450 * ... body ...
5451 * end:
5452 *
5453 * "if expr | else" Produces instructions:
5454 * EVAL expr Push result of "expr"
5455 * JUMP_IF_FALSE else
5456 * ... body ...
5457 * JUMP_ALWAYS end
5458 * else:
5459 * ... body ...
5460 * end:
5461 *
5462 * "if expr1 | elseif expr2 | else" Produces instructions:
5463 * EVAL expr Push result of "expr"
5464 * JUMP_IF_FALSE elseif
5465 * ... body ...
5466 * JUMP_ALWAYS end
5467 * elseif:
5468 * EVAL expr Push result of "expr"
5469 * JUMP_IF_FALSE else
5470 * ... body ...
5471 * JUMP_ALWAYS end
5472 * else:
5473 * ... body ...
5474 * end:
5475 */
5476 static char_u *
5477compile_if(char_u *arg, cctx_T *cctx)
5478{
5479 char_u *p = arg;
5480 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005481 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482 scope_T *scope;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005483 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005484
Bram Moolenaara5565e42020-05-09 15:44:01 +02005485 CLEAR_FIELD(ppconst);
5486 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005487 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005488 clear_ppconst(&ppconst);
5489 return NULL;
5490 }
5491 if (instr->ga_len == instr_count && ppconst.pp_used == 1)
5492 {
5493 // The expression results in a constant.
5494 // TODO: how about nesting?
5495 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? FALSE : TRUE;
5496 clear_ppconst(&ppconst);
5497 }
5498 else
5499 {
5500 // Not a constant, generate instructions for the expression.
5501 cctx->ctx_skip = MAYBE;
5502 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005503 return NULL;
5504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005505
5506 scope = new_scope(cctx, IF_SCOPE);
5507 if (scope == NULL)
5508 return NULL;
5509
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005510 if (cctx->ctx_skip == MAYBE)
5511 {
5512 // "where" is set when ":elseif", "else" or ":endif" is found
5513 scope->se_u.se_if.is_if_label = instr->ga_len;
5514 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5515 }
5516 else
5517 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005518
5519 return p;
5520}
5521
5522 static char_u *
5523compile_elseif(char_u *arg, cctx_T *cctx)
5524{
5525 char_u *p = arg;
5526 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005527 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005528 isn_T *isn;
5529 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005530 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531
5532 if (scope == NULL || scope->se_type != IF_SCOPE)
5533 {
5534 emsg(_(e_elseif_without_if));
5535 return NULL;
5536 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005537 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005538
Bram Moolenaar158906c2020-02-06 20:39:45 +01005539 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005540 {
5541 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005543 return NULL;
5544 // previous "if" or "elseif" jumps here
5545 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5546 isn->isn_arg.jump.jump_where = instr->ga_len;
5547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005549 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005550 CLEAR_FIELD(ppconst);
5551 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005552 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005553 clear_ppconst(&ppconst);
5554 return NULL;
5555 }
5556 if (instr->ga_len == instr_count && ppconst.pp_used == 1)
5557 {
5558 // The expression results in a constant.
5559 // TODO: how about nesting?
5560 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? FALSE : TRUE;
5561 clear_ppconst(&ppconst);
5562 scope->se_u.se_if.is_if_label = -1;
5563 }
5564 else
5565 {
5566 // Not a constant, generate instructions for the expression.
5567 cctx->ctx_skip = MAYBE;
5568 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005569 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005570
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005571 // "where" is set when ":elseif", "else" or ":endif" is found
5572 scope->se_u.se_if.is_if_label = instr->ga_len;
5573 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005575
5576 return p;
5577}
5578
5579 static char_u *
5580compile_else(char_u *arg, cctx_T *cctx)
5581{
5582 char_u *p = arg;
5583 garray_T *instr = &cctx->ctx_instr;
5584 isn_T *isn;
5585 scope_T *scope = cctx->ctx_scope;
5586
5587 if (scope == NULL || scope->se_type != IF_SCOPE)
5588 {
5589 emsg(_(e_else_without_if));
5590 return NULL;
5591 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005592 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005593
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005594 // jump from previous block to the end, unless the else block is empty
5595 if (cctx->ctx_skip == MAYBE)
5596 {
5597 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005598 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005599 return NULL;
5600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005601
Bram Moolenaar158906c2020-02-06 20:39:45 +01005602 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005603 {
5604 if (scope->se_u.se_if.is_if_label >= 0)
5605 {
5606 // previous "if" or "elseif" jumps here
5607 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5608 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005609 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005610 }
5611 }
5612
5613 if (cctx->ctx_skip != MAYBE)
5614 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005615
5616 return p;
5617}
5618
5619 static char_u *
5620compile_endif(char_u *arg, cctx_T *cctx)
5621{
5622 scope_T *scope = cctx->ctx_scope;
5623 ifscope_T *ifscope;
5624 garray_T *instr = &cctx->ctx_instr;
5625 isn_T *isn;
5626
5627 if (scope == NULL || scope->se_type != IF_SCOPE)
5628 {
5629 emsg(_(e_endif_without_if));
5630 return NULL;
5631 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005632 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005633 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005634
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005635 if (scope->se_u.se_if.is_if_label >= 0)
5636 {
5637 // previous "if" or "elseif" jumps here
5638 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5639 isn->isn_arg.jump.jump_where = instr->ga_len;
5640 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641 // Fill in the "end" label in jumps at the end of the blocks.
5642 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005643 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005645 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005646 return arg;
5647}
5648
5649/*
5650 * compile "for var in expr"
5651 *
5652 * Produces instructions:
5653 * PUSHNR -1
5654 * STORE loop-idx Set index to -1
5655 * EVAL expr Push result of "expr"
5656 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5657 * - if beyond end, jump to "end"
5658 * - otherwise get item from list and push it
5659 * STORE var Store item in "var"
5660 * ... body ...
5661 * JUMP top Jump back to repeat
5662 * end: DROP Drop the result of "expr"
5663 *
5664 */
5665 static char_u *
5666compile_for(char_u *arg, cctx_T *cctx)
5667{
5668 char_u *p;
5669 size_t varlen;
5670 garray_T *instr = &cctx->ctx_instr;
5671 garray_T *stack = &cctx->ctx_type_stack;
5672 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005673 lvar_T *loop_lvar; // loop iteration variable
5674 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005675 type_T *vartype;
5676
5677 // TODO: list of variables: "for [key, value] in dict"
5678 // parse "var"
5679 for (p = arg; eval_isnamec1(*p); ++p)
5680 ;
5681 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005682 var_lvar = lookup_local(arg, varlen, cctx);
5683 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005684 {
5685 semsg(_("E1023: variable already defined: %s"), arg);
5686 return NULL;
5687 }
5688
5689 // consume "in"
5690 p = skipwhite(p);
5691 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5692 {
5693 emsg(_(e_missing_in));
5694 return NULL;
5695 }
5696 p = skipwhite(p + 2);
5697
5698
5699 scope = new_scope(cctx, FOR_SCOPE);
5700 if (scope == NULL)
5701 return NULL;
5702
5703 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005704 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5705 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005706 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005707 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005708 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005709 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711
5712 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005713 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5714 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005715 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005716 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005717 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005718 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005719 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005720
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005721 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005722
5723 // compile "expr", it remains on the stack until "endfor"
5724 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005725 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005726 {
5727 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005728 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005730
5731 // now we know the type of "var"
5732 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5733 if (vartype->tt_type != VAR_LIST)
5734 {
5735 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005736 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737 return NULL;
5738 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005739 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005740 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005741
5742 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005743 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005744
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005745 generate_FOR(cctx, loop_lvar->lv_idx);
5746 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005747
5748 return arg;
5749}
5750
5751/*
5752 * compile "endfor"
5753 */
5754 static char_u *
5755compile_endfor(char_u *arg, cctx_T *cctx)
5756{
5757 garray_T *instr = &cctx->ctx_instr;
5758 scope_T *scope = cctx->ctx_scope;
5759 forscope_T *forscope;
5760 isn_T *isn;
5761
5762 if (scope == NULL || scope->se_type != FOR_SCOPE)
5763 {
5764 emsg(_(e_for));
5765 return NULL;
5766 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005767 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005768 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005769 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005770
5771 // At end of ":for" scope jump back to the FOR instruction.
5772 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5773
5774 // Fill in the "end" label in the FOR statement so it can jump here
5775 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5776 isn->isn_arg.forloop.for_end = instr->ga_len;
5777
5778 // Fill in the "end" label any BREAK statements
5779 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5780
5781 // Below the ":for" scope drop the "expr" list from the stack.
5782 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5783 return NULL;
5784
5785 vim_free(scope);
5786
5787 return arg;
5788}
5789
5790/*
5791 * compile "while expr"
5792 *
5793 * Produces instructions:
5794 * top: EVAL expr Push result of "expr"
5795 * JUMP_IF_FALSE end jump if false
5796 * ... body ...
5797 * JUMP top Jump back to repeat
5798 * end:
5799 *
5800 */
5801 static char_u *
5802compile_while(char_u *arg, cctx_T *cctx)
5803{
5804 char_u *p = arg;
5805 garray_T *instr = &cctx->ctx_instr;
5806 scope_T *scope;
5807
5808 scope = new_scope(cctx, WHILE_SCOPE);
5809 if (scope == NULL)
5810 return NULL;
5811
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005812 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005813
5814 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005815 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005816 return NULL;
5817
5818 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005819 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820 JUMP_IF_FALSE, cctx) == FAIL)
5821 return FAIL;
5822
5823 return p;
5824}
5825
5826/*
5827 * compile "endwhile"
5828 */
5829 static char_u *
5830compile_endwhile(char_u *arg, cctx_T *cctx)
5831{
5832 scope_T *scope = cctx->ctx_scope;
5833
5834 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5835 {
5836 emsg(_(e_while));
5837 return NULL;
5838 }
5839 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005840 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005841
5842 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005843 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844
5845 // Fill in the "end" label in the WHILE statement so it can jump here.
5846 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005847 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848
5849 vim_free(scope);
5850
5851 return arg;
5852}
5853
5854/*
5855 * compile "continue"
5856 */
5857 static char_u *
5858compile_continue(char_u *arg, cctx_T *cctx)
5859{
5860 scope_T *scope = cctx->ctx_scope;
5861
5862 for (;;)
5863 {
5864 if (scope == NULL)
5865 {
5866 emsg(_(e_continue));
5867 return NULL;
5868 }
5869 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5870 break;
5871 scope = scope->se_outer;
5872 }
5873
5874 // Jump back to the FOR or WHILE instruction.
5875 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005876 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5877 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005878 return arg;
5879}
5880
5881/*
5882 * compile "break"
5883 */
5884 static char_u *
5885compile_break(char_u *arg, cctx_T *cctx)
5886{
5887 scope_T *scope = cctx->ctx_scope;
5888 endlabel_T **el;
5889
5890 for (;;)
5891 {
5892 if (scope == NULL)
5893 {
5894 emsg(_(e_break));
5895 return NULL;
5896 }
5897 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5898 break;
5899 scope = scope->se_outer;
5900 }
5901
5902 // Jump to the end of the FOR or WHILE loop.
5903 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005904 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005906 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005907 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5908 return FAIL;
5909
5910 return arg;
5911}
5912
5913/*
5914 * compile "{" start of block
5915 */
5916 static char_u *
5917compile_block(char_u *arg, cctx_T *cctx)
5918{
5919 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5920 return NULL;
5921 return skipwhite(arg + 1);
5922}
5923
5924/*
5925 * compile end of block: drop one scope
5926 */
5927 static void
5928compile_endblock(cctx_T *cctx)
5929{
5930 scope_T *scope = cctx->ctx_scope;
5931
5932 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005933 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005934 vim_free(scope);
5935}
5936
5937/*
5938 * compile "try"
5939 * Creates a new scope for the try-endtry, pointing to the first catch and
5940 * finally.
5941 * Creates another scope for the "try" block itself.
5942 * TRY instruction sets up exception handling at runtime.
5943 *
5944 * "try"
5945 * TRY -> catch1, -> finally push trystack entry
5946 * ... try block
5947 * "throw {exception}"
5948 * EVAL {exception}
5949 * THROW create exception
5950 * ... try block
5951 * " catch {expr}"
5952 * JUMP -> finally
5953 * catch1: PUSH exeception
5954 * EVAL {expr}
5955 * MATCH
5956 * JUMP nomatch -> catch2
5957 * CATCH remove exception
5958 * ... catch block
5959 * " catch"
5960 * JUMP -> finally
5961 * catch2: CATCH remove exception
5962 * ... catch block
5963 * " finally"
5964 * finally:
5965 * ... finally block
5966 * " endtry"
5967 * ENDTRY pop trystack entry, may rethrow
5968 */
5969 static char_u *
5970compile_try(char_u *arg, cctx_T *cctx)
5971{
5972 garray_T *instr = &cctx->ctx_instr;
5973 scope_T *try_scope;
5974 scope_T *scope;
5975
5976 // scope that holds the jumps that go to catch/finally/endtry
5977 try_scope = new_scope(cctx, TRY_SCOPE);
5978 if (try_scope == NULL)
5979 return NULL;
5980
5981 // "catch" is set when the first ":catch" is found.
5982 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005983 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005984 if (generate_instr(cctx, ISN_TRY) == NULL)
5985 return NULL;
5986
5987 // scope for the try block itself
5988 scope = new_scope(cctx, BLOCK_SCOPE);
5989 if (scope == NULL)
5990 return NULL;
5991
5992 return arg;
5993}
5994
5995/*
5996 * compile "catch {expr}"
5997 */
5998 static char_u *
5999compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6000{
6001 scope_T *scope = cctx->ctx_scope;
6002 garray_T *instr = &cctx->ctx_instr;
6003 char_u *p;
6004 isn_T *isn;
6005
6006 // end block scope from :try or :catch
6007 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6008 compile_endblock(cctx);
6009 scope = cctx->ctx_scope;
6010
6011 // Error if not in a :try scope
6012 if (scope == NULL || scope->se_type != TRY_SCOPE)
6013 {
6014 emsg(_(e_catch));
6015 return NULL;
6016 }
6017
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006018 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006019 {
6020 emsg(_("E1033: catch unreachable after catch-all"));
6021 return NULL;
6022 }
6023
6024 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006025 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026 JUMP_ALWAYS, cctx) == FAIL)
6027 return NULL;
6028
6029 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006030 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031 if (isn->isn_arg.try.try_catch == 0)
6032 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006033 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034 {
6035 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006036 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006037 isn->isn_arg.jump.jump_where = instr->ga_len;
6038 }
6039
6040 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006041 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006043 scope->se_u.se_try.ts_caught_all = TRUE;
6044 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006045 }
6046 else
6047 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006048 char_u *end;
6049 char_u *pat;
6050 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006051 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006052 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006054 // Push v:exception, push {expr} and MATCH
6055 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6056
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006057 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006058 if (*end != *p)
6059 {
6060 semsg(_("E1067: Separator mismatch: %s"), p);
6061 vim_free(tofree);
6062 return FAIL;
6063 }
6064 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006065 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006066 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006067 len = (int)(end - tofree);
6068 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006069 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006070 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006071 if (pat == NULL)
6072 return FAIL;
6073 if (generate_PUSHS(cctx, pat) == FAIL)
6074 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6077 return NULL;
6078
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006079 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6081 return NULL;
6082 }
6083
6084 if (generate_instr(cctx, ISN_CATCH) == NULL)
6085 return NULL;
6086
6087 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6088 return NULL;
6089 return p;
6090}
6091
6092 static char_u *
6093compile_finally(char_u *arg, cctx_T *cctx)
6094{
6095 scope_T *scope = cctx->ctx_scope;
6096 garray_T *instr = &cctx->ctx_instr;
6097 isn_T *isn;
6098
6099 // end block scope from :try or :catch
6100 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6101 compile_endblock(cctx);
6102 scope = cctx->ctx_scope;
6103
6104 // Error if not in a :try scope
6105 if (scope == NULL || scope->se_type != TRY_SCOPE)
6106 {
6107 emsg(_(e_finally));
6108 return NULL;
6109 }
6110
6111 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006112 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113 if (isn->isn_arg.try.try_finally != 0)
6114 {
6115 emsg(_(e_finally_dup));
6116 return NULL;
6117 }
6118
6119 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006120 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006121
Bram Moolenaar585fea72020-04-02 22:33:21 +02006122 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006123 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006124 {
6125 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006126 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127 isn->isn_arg.jump.jump_where = instr->ga_len;
6128 }
6129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130 // TODO: set index in ts_finally_label jumps
6131
6132 return arg;
6133}
6134
6135 static char_u *
6136compile_endtry(char_u *arg, cctx_T *cctx)
6137{
6138 scope_T *scope = cctx->ctx_scope;
6139 garray_T *instr = &cctx->ctx_instr;
6140 isn_T *isn;
6141
6142 // end block scope from :catch or :finally
6143 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6144 compile_endblock(cctx);
6145 scope = cctx->ctx_scope;
6146
6147 // Error if not in a :try scope
6148 if (scope == NULL || scope->se_type != TRY_SCOPE)
6149 {
6150 if (scope == NULL)
6151 emsg(_(e_no_endtry));
6152 else if (scope->se_type == WHILE_SCOPE)
6153 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006154 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006155 emsg(_(e_endfor));
6156 else
6157 emsg(_(e_endif));
6158 return NULL;
6159 }
6160
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006161 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006162 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6163 {
6164 emsg(_("E1032: missing :catch or :finally"));
6165 return NULL;
6166 }
6167
6168 // Fill in the "end" label in jumps at the end of the blocks, if not done
6169 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006170 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006171
6172 // End :catch or :finally scope: set value in ISN_TRY instruction
6173 if (isn->isn_arg.try.try_finally == 0)
6174 isn->isn_arg.try.try_finally = instr->ga_len;
6175 compile_endblock(cctx);
6176
6177 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6178 return NULL;
6179 return arg;
6180}
6181
6182/*
6183 * compile "throw {expr}"
6184 */
6185 static char_u *
6186compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6187{
6188 char_u *p = skipwhite(arg);
6189
Bram Moolenaara5565e42020-05-09 15:44:01 +02006190 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006191 return NULL;
6192 if (may_generate_2STRING(-1, cctx) == FAIL)
6193 return NULL;
6194 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6195 return NULL;
6196
6197 return p;
6198}
6199
6200/*
6201 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006202 * compile "echomsg expr"
6203 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006204 * compile "execute expr"
6205 */
6206 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006207compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006208{
6209 char_u *p = arg;
6210 int count = 0;
6211
6212 for (;;)
6213 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006214 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006215 return NULL;
6216 ++count;
6217 p = skipwhite(p);
6218 if (ends_excmd(*p))
6219 break;
6220 }
6221
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006222 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6223 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6224 else if (cmdidx == CMD_execute)
6225 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6226 else if (cmdidx == CMD_echomsg)
6227 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6228 else
6229 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 return p;
6231}
6232
6233/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006234 * A command that is not compiled, execute with legacy code.
6235 */
6236 static char_u *
6237compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6238{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006239 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006240 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006241
6242 if (cctx->ctx_skip == TRUE)
6243 goto theend;
6244
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006245 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6246 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006247 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6248 {
6249 // expand filename in "syntax include [@group] filename"
6250 has_expr = TRUE;
6251 eap->arg = skipwhite(eap->arg + 7);
6252 if (*eap->arg == '@')
6253 eap->arg = skiptowhite(eap->arg);
6254 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006255
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006256 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006257 {
6258 int count = 0;
6259 char_u *start = skipwhite(line);
6260
6261 // :cmd xxx`=expr1`yyy`=expr2`zzz
6262 // PUSHS ":cmd xxx"
6263 // eval expr1
6264 // PUSHS "yyy"
6265 // eval expr2
6266 // PUSHS "zzz"
6267 // EXECCONCAT 5
6268 for (;;)
6269 {
6270 if (p > start)
6271 {
6272 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
6273 ++count;
6274 }
6275 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006276 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006277 return NULL;
6278 may_generate_2STRING(-1, cctx);
6279 ++count;
6280 p = skipwhite(p);
6281 if (*p != '`')
6282 {
6283 emsg(_("E1083: missing backtick"));
6284 return NULL;
6285 }
6286 start = p + 1;
6287
6288 p = (char_u *)strstr((char *)start, "`=");
6289 if (p == NULL)
6290 {
6291 if (*skipwhite(start) != NUL)
6292 {
6293 generate_PUSHS(cctx, vim_strsave(start));
6294 ++count;
6295 }
6296 break;
6297 }
6298 }
6299 generate_EXECCONCAT(cctx, count);
6300 }
6301 else
6302 generate_EXEC(cctx, line);
6303
6304theend:
6305 return (char_u *)"";
6306}
6307
6308/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006309 * Add a function to the list of :def functions.
6310 * This "sets ufunc->uf_dfunc_idx" but the function isn't compiled yet.
6311 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006312 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006313add_def_function(ufunc_T *ufunc)
6314{
6315 dfunc_T *dfunc;
6316
6317 // Add the function to "def_functions".
6318 if (ga_grow(&def_functions, 1) == FAIL)
6319 return FAIL;
6320 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6321 CLEAR_POINTER(dfunc);
6322 dfunc->df_idx = def_functions.ga_len;
6323 ufunc->uf_dfunc_idx = dfunc->df_idx;
6324 dfunc->df_ufunc = ufunc;
6325 ++def_functions.ga_len;
6326 return OK;
6327}
6328
6329/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330 * After ex_function() has collected all the function lines: parse and compile
6331 * the lines into instructions.
6332 * Adds the function to "def_functions".
6333 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6334 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006335 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006336 * This can be used recursively through compile_lambda(), which may reallocate
6337 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006338 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006339 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006340 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006341compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006342{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 char_u *line = NULL;
6344 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345 char *errormsg = NULL; // error message
6346 int had_return = FALSE;
6347 cctx_T cctx;
6348 garray_T *instr;
6349 int called_emsg_before = called_emsg;
6350 int ret = FAIL;
6351 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006352 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353
Bram Moolenaar09689a02020-05-09 22:50:08 +02006354 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006356 // Redefining a function that was compiled before.
6357 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6358 + ufunc->uf_dfunc_idx;
6359 // Free old instructions.
6360 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006362 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006363 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006364
Bram Moolenaara80faa82020-04-12 19:37:17 +02006365 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366 cctx.ctx_ufunc = ufunc;
6367 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006368 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006369 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6370 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6371 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6372 cctx.ctx_type_list = &ufunc->uf_type_list;
6373 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6374 instr = &cctx.ctx_instr;
6375
6376 // Most modern script version.
6377 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6378
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006379 if (ufunc->uf_def_args.ga_len > 0)
6380 {
6381 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006382 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006383 int i;
6384 char_u *arg;
6385 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6386
6387 // Produce instructions for the default values of optional arguments.
6388 // Store the instruction index in uf_def_arg_idx[] so that we know
6389 // where to start when the function is called, depending on the number
6390 // of arguments.
6391 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6392 if (ufunc->uf_def_arg_idx == NULL)
6393 goto erret;
6394 for (i = 0; i < count; ++i)
6395 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006396 garray_T *stack = &cctx.ctx_type_stack;
6397 type_T *val_type;
6398 int arg_idx = first_def_arg + i;
6399
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006400 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6401 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006402 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006403 goto erret;
6404
6405 // If no type specified use the type of the default value.
6406 // Otherwise check that the default value type matches the
6407 // specified type.
6408 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6409 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6410 ufunc->uf_arg_types[arg_idx] = val_type;
6411 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6412 == FAIL)
6413 {
6414 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6415 arg_idx + 1);
6416 goto erret;
6417 }
6418
6419 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006420 goto erret;
6421 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006422 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6423 }
6424
6425 /*
6426 * Loop over all the lines of the function and generate instructions.
6427 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006428 for (;;)
6429 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006430 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006431 int starts_with_colon = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006432
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006433 // Bail out on the first error to avoid a flood of errors and report
6434 // the right line number when inside try/catch.
6435 if (emsg_before != called_emsg)
6436 goto erret;
6437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006438 if (line != NULL && *line == '|')
6439 // the line continues after a '|'
6440 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006441 else if (line != NULL && *line != NUL
6442 && !(*line == '#' && (line == cctx.ctx_line_start
6443 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006444 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006445 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446 goto erret;
6447 }
6448 else
6449 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006450 line = next_line_from_context(&cctx);
6451 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006452 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006453 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006455 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006456
6457 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006458 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006459 ea.cmdlinep = &line;
6460 ea.cmd = skipwhite(line);
6461
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006462 // Some things can be recognized by the first character.
6463 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006465 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006466 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006467 if (ea.cmd[1] != '{')
6468 {
6469 line = (char_u *)"";
6470 continue;
6471 }
6472 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006473
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006474 case '}':
6475 {
6476 // "}" ends a block scope
6477 scopetype_T stype = cctx.ctx_scope == NULL
6478 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006479
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006480 if (stype == BLOCK_SCOPE)
6481 {
6482 compile_endblock(&cctx);
6483 line = ea.cmd;
6484 }
6485 else
6486 {
6487 emsg(_("E1025: using } outside of a block scope"));
6488 goto erret;
6489 }
6490 if (line != NULL)
6491 line = skipwhite(ea.cmd + 1);
6492 continue;
6493 }
6494
6495 case '{':
6496 // "{" starts a block scope
6497 // "{'a': 1}->func() is something else
6498 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6499 {
6500 line = compile_block(ea.cmd, &cctx);
6501 continue;
6502 }
6503 break;
6504
6505 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006506 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006507 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006508 }
6509
6510 /*
6511 * COMMAND MODIFIERS
6512 */
6513 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6514 {
6515 if (errormsg != NULL)
6516 goto erret;
6517 // empty line or comment
6518 line = (char_u *)"";
6519 continue;
6520 }
6521
6522 // Skip ":call" to get to the function name.
6523 if (checkforcmd(&ea.cmd, "call", 3))
6524 ea.cmd = skipwhite(ea.cmd);
6525
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006526 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006528 char_u *pskip;
6529
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006530 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006531 // find what follows.
6532 // Skip over "var.member", "var[idx]" and the like.
6533 // Also "&opt = val", "$ENV = val" and "@r = val".
6534 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006535 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006536 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006537 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006539 char_u *var_end;
6540 int oplen;
6541 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006543 var_end = find_name_end(pskip, NULL, NULL,
6544 FNE_CHECK_START | FNE_INCL_BR);
6545 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006546 if (oplen > 0)
6547 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006548 size_t len = p - ea.cmd;
6549
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006550 // Recognize an assignment if we recognize the variable
6551 // name:
6552 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006553 // "local = expr" where "local" is a local var.
6554 // "script = expr" where "script" is a script-local var.
6555 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006556 // "&opt = expr"
6557 // "$ENV = expr"
6558 // "@r = expr"
6559 if (*ea.cmd == '&'
6560 || *ea.cmd == '$'
6561 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006562 || ((len) > 2 && ea.cmd[1] == ':')
6563 || lookup_local(ea.cmd, len, &cctx) != NULL
6564 || lookup_arg(ea.cmd, len, NULL, NULL,
6565 NULL, &cctx) == OK
6566 || lookup_script(ea.cmd, len) == OK
6567 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006568 {
6569 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6570 if (line == NULL)
6571 goto erret;
6572 continue;
6573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006574 }
6575 }
6576 }
6577
6578 /*
6579 * COMMAND after range
6580 */
6581 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006582 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006583 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006584 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006585
6586 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6587 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006588 if (cctx.ctx_skip == TRUE)
6589 {
6590 line += STRLEN(line);
6591 continue;
6592 }
6593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 // Expression or function call.
6595 if (ea.cmdidx == CMD_eval)
6596 {
6597 p = ea.cmd;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006598 if (compile_expr0(&p, &cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 goto erret;
6600
6601 // drop the return value
6602 generate_instr_drop(&cctx, ISN_DROP, 1);
6603 line = p;
6604 continue;
6605 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006606 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 iemsg("Command from find_ex_command() not handled");
6608 goto erret;
6609 }
6610
6611 p = skipwhite(p);
6612
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006613 if (cctx.ctx_skip == TRUE
6614 && ea.cmdidx != CMD_elseif
6615 && ea.cmdidx != CMD_else
6616 && ea.cmdidx != CMD_endif)
6617 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006618 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006619 continue;
6620 }
6621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 switch (ea.cmdidx)
6623 {
6624 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006625 ea.arg = p;
6626 line = compile_nested_function(&ea, &cctx);
6627 break;
6628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006630 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631 goto erret;
6632
6633 case CMD_return:
6634 line = compile_return(p, set_return_type, &cctx);
6635 had_return = TRUE;
6636 break;
6637
6638 case CMD_let:
6639 case CMD_const:
6640 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6641 break;
6642
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006643 case CMD_unlet:
6644 case CMD_unlockvar:
6645 case CMD_lockvar:
6646 line = compile_unletlock(p, &ea, &cctx);
6647 break;
6648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006649 case CMD_import:
6650 line = compile_import(p, &cctx);
6651 break;
6652
6653 case CMD_if:
6654 line = compile_if(p, &cctx);
6655 break;
6656 case CMD_elseif:
6657 line = compile_elseif(p, &cctx);
6658 break;
6659 case CMD_else:
6660 line = compile_else(p, &cctx);
6661 break;
6662 case CMD_endif:
6663 line = compile_endif(p, &cctx);
6664 break;
6665
6666 case CMD_while:
6667 line = compile_while(p, &cctx);
6668 break;
6669 case CMD_endwhile:
6670 line = compile_endwhile(p, &cctx);
6671 break;
6672
6673 case CMD_for:
6674 line = compile_for(p, &cctx);
6675 break;
6676 case CMD_endfor:
6677 line = compile_endfor(p, &cctx);
6678 break;
6679 case CMD_continue:
6680 line = compile_continue(p, &cctx);
6681 break;
6682 case CMD_break:
6683 line = compile_break(p, &cctx);
6684 break;
6685
6686 case CMD_try:
6687 line = compile_try(p, &cctx);
6688 break;
6689 case CMD_catch:
6690 line = compile_catch(p, &cctx);
6691 break;
6692 case CMD_finally:
6693 line = compile_finally(p, &cctx);
6694 break;
6695 case CMD_endtry:
6696 line = compile_endtry(p, &cctx);
6697 break;
6698 case CMD_throw:
6699 line = compile_throw(p, &cctx);
6700 break;
6701
6702 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006704 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006705 case CMD_echomsg:
6706 case CMD_echoerr:
6707 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006708 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709
6710 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006711 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006712 // Not recognized, execute with do_cmdline_cmd().
6713 ea.arg = p;
6714 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715 break;
6716 }
6717 if (line == NULL)
6718 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006719 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006720
6721 if (cctx.ctx_type_stack.ga_len < 0)
6722 {
6723 iemsg("Type stack underflow");
6724 goto erret;
6725 }
6726 }
6727
6728 if (cctx.ctx_scope != NULL)
6729 {
6730 if (cctx.ctx_scope->se_type == IF_SCOPE)
6731 emsg(_(e_endif));
6732 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6733 emsg(_(e_endwhile));
6734 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6735 emsg(_(e_endfor));
6736 else
6737 emsg(_("E1026: Missing }"));
6738 goto erret;
6739 }
6740
6741 if (!had_return)
6742 {
6743 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6744 {
6745 emsg(_("E1027: Missing return statement"));
6746 goto erret;
6747 }
6748
6749 // Return zero if there is no return at the end.
6750 generate_PUSHNR(&cctx, 0);
6751 generate_instr(&cctx, ISN_RETURN);
6752 }
6753
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006754 {
6755 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6756 + ufunc->uf_dfunc_idx;
6757 dfunc->df_deleted = FALSE;
6758 dfunc->df_instr = instr->ga_data;
6759 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006760 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006761 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006762 if (cctx.ctx_outer_used)
6763 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006765
6766 ret = OK;
6767
6768erret:
6769 if (ret == FAIL)
6770 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006771 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006772 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6773 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006774
6775 for (idx = 0; idx < instr->ga_len; ++idx)
6776 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006778
Bram Moolenaar822ba242020-05-24 23:00:18 +02006779 ufunc->uf_dfunc_idx = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006780 if (!dfunc->df_deleted)
6781 --def_functions.ga_len;
6782
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006783 while (cctx.ctx_scope != NULL)
6784 drop_scope(&cctx);
6785
Bram Moolenaar20431c92020-03-20 18:39:46 +01006786 // Don't execute this function body.
6787 ga_clear_strings(&ufunc->uf_lines);
6788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789 if (errormsg != NULL)
6790 emsg(errormsg);
6791 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006792 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 }
6794
6795 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006796 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006797 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006798 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006799 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006800}
6801
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006802 void
6803set_function_type(ufunc_T *ufunc)
6804{
6805 int varargs = ufunc->uf_va_name != NULL;
6806 int argcount = ufunc->uf_args.ga_len;
6807
6808 // Create a type for the function, with the return type and any
6809 // argument types.
6810 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6811 // The type is included in "tt_args".
6812 if (argcount > 0 || varargs)
6813 {
6814 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6815 argcount, &ufunc->uf_type_list);
6816 // Add argument types to the function type.
6817 if (func_type_add_arg_types(ufunc->uf_func_type,
6818 argcount + varargs,
6819 &ufunc->uf_type_list) == FAIL)
6820 return;
6821 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6822 ufunc->uf_func_type->tt_min_argcount =
6823 argcount - ufunc->uf_def_args.ga_len;
6824 if (ufunc->uf_arg_types == NULL)
6825 {
6826 int i;
6827
6828 // lambda does not have argument types.
6829 for (i = 0; i < argcount; ++i)
6830 ufunc->uf_func_type->tt_args[i] = &t_any;
6831 }
6832 else
6833 mch_memmove(ufunc->uf_func_type->tt_args,
6834 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6835 if (varargs)
6836 {
6837 ufunc->uf_func_type->tt_args[argcount] =
6838 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
6839 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6840 }
6841 }
6842 else
6843 // No arguments, can use a predefined type.
6844 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6845 argcount, &ufunc->uf_type_list);
6846}
6847
6848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006849/*
6850 * Delete an instruction, free what it contains.
6851 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006852 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853delete_instr(isn_T *isn)
6854{
6855 switch (isn->isn_type)
6856 {
6857 case ISN_EXEC:
6858 case ISN_LOADENV:
6859 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006860 case ISN_LOADB:
6861 case ISN_LOADW:
6862 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006863 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006864 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006865 case ISN_PUSHEXC:
6866 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006867 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006869 case ISN_STOREB:
6870 case ISN_STOREW:
6871 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006872 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006873 vim_free(isn->isn_arg.string);
6874 break;
6875
6876 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006877 case ISN_STORES:
6878 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 break;
6880
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006881 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006882 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006883 vim_free(isn->isn_arg.unlet.ul_name);
6884 break;
6885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886 case ISN_STOREOPT:
6887 vim_free(isn->isn_arg.storeopt.so_name);
6888 break;
6889
6890 case ISN_PUSHBLOB: // push blob isn_arg.blob
6891 blob_unref(isn->isn_arg.blob);
6892 break;
6893
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006894 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006895#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006896 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006897#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006898 break;
6899
6900 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006901#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006902 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006903#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006904 break;
6905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906 case ISN_UCALL:
6907 vim_free(isn->isn_arg.ufunc.cuf_name);
6908 break;
6909
Bram Moolenaar221fcc72020-05-05 19:46:20 +02006910 case ISN_FUNCREF:
6911 {
6912 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6913 + isn->isn_arg.funcref.fr_func;
6914 func_ptr_unref(dfunc->df_ufunc);
6915 }
6916 break;
6917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918 case ISN_2BOOL:
6919 case ISN_2STRING:
6920 case ISN_ADDBLOB:
6921 case ISN_ADDLIST:
6922 case ISN_BCALL:
6923 case ISN_CATCH:
6924 case ISN_CHECKNR:
6925 case ISN_CHECKTYPE:
6926 case ISN_COMPAREANY:
6927 case ISN_COMPAREBLOB:
6928 case ISN_COMPAREBOOL:
6929 case ISN_COMPAREDICT:
6930 case ISN_COMPAREFLOAT:
6931 case ISN_COMPAREFUNC:
6932 case ISN_COMPARELIST:
6933 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 case ISN_COMPARESPECIAL:
6935 case ISN_COMPARESTRING:
6936 case ISN_CONCAT:
6937 case ISN_DCALL:
6938 case ISN_DROP:
6939 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006940 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006941 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006943 case ISN_EXECCONCAT:
6944 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006946 case ISN_INDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006947 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 case ISN_JUMP:
6949 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006950 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006951 case ISN_LOADSCRIPT:
6952 case ISN_LOADREG:
6953 case ISN_LOADV:
6954 case ISN_NEGATENR:
6955 case ISN_NEWDICT:
6956 case ISN_NEWLIST:
6957 case ISN_OPNR:
6958 case ISN_OPFLOAT:
6959 case ISN_OPANY:
6960 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006961 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006962 case ISN_PUSHF:
6963 case ISN_PUSHNR:
6964 case ISN_PUSHBOOL:
6965 case ISN_PUSHSPEC:
6966 case ISN_RETURN:
6967 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02006968 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006969 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006971 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006972 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006973 case ISN_STOREDICT:
6974 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975 case ISN_THROW:
6976 case ISN_TRY:
6977 // nothing allocated
6978 break;
6979 }
6980}
6981
6982/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006983 * Free all instructions for "dfunc".
6984 */
6985 static void
6986delete_def_function_contents(dfunc_T *dfunc)
6987{
6988 int idx;
6989
6990 ga_clear(&dfunc->df_def_args_isn);
6991
6992 if (dfunc->df_instr != NULL)
6993 {
6994 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6995 delete_instr(dfunc->df_instr + idx);
6996 VIM_CLEAR(dfunc->df_instr);
6997 }
6998
6999 dfunc->df_deleted = TRUE;
7000}
7001
7002/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003 * When a user function is deleted, delete any associated def function.
7004 */
7005 void
7006delete_def_function(ufunc_T *ufunc)
7007{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007008 if (ufunc->uf_dfunc_idx >= 0)
7009 {
7010 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7011 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007012
Bram Moolenaar20431c92020-03-20 18:39:46 +01007013 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014 }
7015}
7016
7017#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007018/*
7019 * Free all functions defined with ":def".
7020 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021 void
7022free_def_functions(void)
7023{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007024 int idx;
7025
7026 for (idx = 0; idx < def_functions.ga_len; ++idx)
7027 {
7028 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7029
7030 delete_def_function_contents(dfunc);
7031 }
7032
7033 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034}
7035#endif
7036
7037
7038#endif // FEAT_EVAL