blob: a8a7647468bd2e70d3181fa4d28a7eac8c25b55c [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;
100 int lv_const; // when TRUE cannot be assigned to
101 int lv_arg; // when TRUE this is an argument
102} lvar_T;
103
104/*
105 * Context for compiling lines of Vim script.
106 * Stores info about the local variables and condition stack.
107 */
108struct cctx_S {
109 ufunc_T *ctx_ufunc; // current function
110 int ctx_lnum; // line number in current function
111 garray_T ctx_instr; // generated instructions
112
113 garray_T ctx_locals; // currently visible local variables
114 int ctx_max_local; // maximum number of locals at one time
115
116 garray_T ctx_imports; // imported items
117
Bram Moolenaara259d8d2020-01-31 20:10:50 +0100118 int ctx_skip; // when TRUE skip commands, when FALSE skip
119 // commands after "else"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100120 scope_T *ctx_scope; // current scope, NULL at toplevel
121
122 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200123 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124};
125
126static char e_var_notfound[] = N_("E1001: variable not found: %s");
127static char e_syntax_at[] = N_("E1002: Syntax error at %s");
128
129static int compile_expr1(char_u **arg, cctx_T *cctx);
130static int compile_expr2(char_u **arg, cctx_T *cctx);
131static int compile_expr3(char_u **arg, cctx_T *cctx);
Bram Moolenaar20431c92020-03-20 18:39:46 +0100132static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200133static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
134static int check_type(type_T *expected, type_T *actual, int give_msg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135
136/*
137 * Lookup variable "name" in the local scope and return the index.
138 */
139 static int
140lookup_local(char_u *name, size_t len, cctx_T *cctx)
141{
142 int idx;
143
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100144 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 return -1;
146 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
147 {
148 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
149
150 if (STRNCMP(name, lvar->lv_name, len) == 0
151 && STRLEN(lvar->lv_name) == len)
152 return idx;
153 }
154 return -1;
155}
156
157/*
158 * Lookup an argument in the current function.
159 * Returns the argument index or -1 if not found.
160 */
161 static int
162lookup_arg(char_u *name, size_t len, cctx_T *cctx)
163{
164 int idx;
165
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100166 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167 return -1;
168 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
169 {
170 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
171
172 if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
173 return idx;
174 }
175 return -1;
176}
177
178/*
179 * Lookup a vararg argument in the current function.
180 * Returns TRUE if there is a match.
181 */
182 static int
183lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
184{
185 char_u *va_name = cctx->ctx_ufunc->uf_va_name;
186
187 return len > 0 && va_name != NULL
188 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
189}
190
191/*
192 * Lookup a variable in the current script.
193 * Returns OK or FAIL.
194 */
195 static int
196lookup_script(char_u *name, size_t len)
197{
198 int cc;
199 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
200 dictitem_T *di;
201
202 cc = name[len];
203 name[len] = NUL;
204 di = find_var_in_ht(ht, 0, name, TRUE);
205 name[len] = cc;
206 return di == NULL ? FAIL: OK;
207}
208
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100209/*
210 * Check if "p[len]" is already defined, either in script "import_sid" or in
211 * compilation context "cctx".
212 * Return FAIL and give an error if it defined.
213 */
214 int
215check_defined(char_u *p, int len, cctx_T *cctx)
216{
217 if (lookup_script(p, len) == OK
218 || (cctx != NULL
219 && (lookup_local(p, len, cctx) >= 0
220 || find_imported(p, len, cctx) != NULL)))
221 {
222 semsg("E1073: imported name already defined: %s", p);
223 return FAIL;
224 }
225 return OK;
226}
227
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200228/*
229 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
230 * be freed later.
231 */
232 static type_T *
233alloc_type(garray_T *type_gap)
234{
235 type_T *type;
236
237 if (ga_grow(type_gap, 1) == FAIL)
238 return NULL;
239 type = ALLOC_CLEAR_ONE(type_T);
240 if (type != NULL)
241 {
242 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
243 ++type_gap->ga_len;
244 }
245 return type;
246}
247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200249get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250{
251 type_T *type;
252
253 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200254 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200256 if (member_type->tt_type == VAR_VOID
257 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100258 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100259 if (member_type->tt_type == VAR_BOOL)
260 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261 if (member_type->tt_type == VAR_NUMBER)
262 return &t_list_number;
263 if (member_type->tt_type == VAR_STRING)
264 return &t_list_string;
265
266 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200267 type = alloc_type(type_gap);
268 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100269 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270 type->tt_type = VAR_LIST;
271 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200272 type->tt_argcount = 0;
273 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 return type;
275}
276
277 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200278get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279{
280 type_T *type;
281
282 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200283 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200285 if (member_type->tt_type == VAR_VOID
286 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100287 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100288 if (member_type->tt_type == VAR_BOOL)
289 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 if (member_type->tt_type == VAR_NUMBER)
291 return &t_dict_number;
292 if (member_type->tt_type == VAR_STRING)
293 return &t_dict_string;
294
295 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200296 type = alloc_type(type_gap);
297 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100298 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299 type->tt_type = VAR_DICT;
300 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200301 type->tt_argcount = 0;
302 type->tt_args = NULL;
303 return type;
304}
305
306/*
307 * Get a function type, based on the return type "ret_type".
308 * If "argcount" is -1 or 0 a predefined type can be used.
309 * If "argcount" > 0 always create a new type, so that arguments can be added.
310 */
311 static type_T *
312get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
313{
314 type_T *type;
315
316 // recognize commonly used types
317 if (argcount <= 0)
318 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200319 if (ret_type == &t_unknown)
320 {
321 // (argcount == 0) is not possible
322 return &t_func_unknown;
323 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200324 if (ret_type == &t_void)
325 {
326 if (argcount == 0)
327 return &t_func_0_void;
328 else
329 return &t_func_void;
330 }
331 if (ret_type == &t_any)
332 {
333 if (argcount == 0)
334 return &t_func_0_any;
335 else
336 return &t_func_any;
337 }
338 if (ret_type == &t_number)
339 {
340 if (argcount == 0)
341 return &t_func_0_number;
342 else
343 return &t_func_number;
344 }
345 if (ret_type == &t_string)
346 {
347 if (argcount == 0)
348 return &t_func_0_string;
349 else
350 return &t_func_string;
351 }
352 }
353
354 // Not a common type or has arguments, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200355 type = alloc_type(type_gap);
356 if (type == NULL)
Bram Moolenaard77a8522020-04-03 21:59:57 +0200357 return &t_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200358 type->tt_type = VAR_FUNC;
359 type->tt_member = ret_type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200360 type->tt_argcount = argcount;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200361 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 return type;
363}
364
Bram Moolenaara8c17702020-04-01 21:17:24 +0200365/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200366 * For a function type, reserve space for "argcount" argument types (including
367 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200368 */
369 static int
370func_type_add_arg_types(
371 type_T *functype,
372 int argcount,
373 int min_argcount,
374 garray_T *type_gap)
375{
376 if (ga_grow(type_gap, 1) == FAIL)
377 return FAIL;
378 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
379 if (functype->tt_args == NULL)
380 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200381 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
382 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200383 ++type_gap->ga_len;
384
385 functype->tt_argcount = argcount;
386 functype->tt_min_argcount = min_argcount;
387 return OK;
388}
389
390/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200391 * Return the type_T for a typval. Only for primitive types.
392 */
393 static type_T *
394typval2type(typval_T *tv)
395{
396 if (tv->v_type == VAR_NUMBER)
397 return &t_number;
398 if (tv->v_type == VAR_BOOL)
399 return &t_bool;
400 if (tv->v_type == VAR_STRING)
401 return &t_string;
402 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
403 return &t_list_string;
404 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
405 return &t_dict_any;
406 return &t_any;
407}
408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409/////////////////////////////////////////////////////////////////////
410// Following generate_ functions expect the caller to call ga_grow().
411
Bram Moolenaar080457c2020-03-03 21:53:32 +0100412#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
413#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100415/*
416 * Generate an instruction without arguments.
417 * Returns a pointer to the new instruction, NULL if failed.
418 */
419 static isn_T *
420generate_instr(cctx_T *cctx, isntype_T isn_type)
421{
422 garray_T *instr = &cctx->ctx_instr;
423 isn_T *isn;
424
Bram Moolenaar080457c2020-03-03 21:53:32 +0100425 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426 if (ga_grow(instr, 1) == FAIL)
427 return NULL;
428 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
429 isn->isn_type = isn_type;
430 isn->isn_lnum = cctx->ctx_lnum + 1;
431 ++instr->ga_len;
432
433 return isn;
434}
435
436/*
437 * Generate an instruction without arguments.
438 * "drop" will be removed from the stack.
439 * Returns a pointer to the new instruction, NULL if failed.
440 */
441 static isn_T *
442generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
443{
444 garray_T *stack = &cctx->ctx_type_stack;
445
Bram Moolenaar080457c2020-03-03 21:53:32 +0100446 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100447 stack->ga_len -= drop;
448 return generate_instr(cctx, isn_type);
449}
450
451/*
452 * Generate instruction "isn_type" and put "type" on the type stack.
453 */
454 static isn_T *
455generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
456{
457 isn_T *isn;
458 garray_T *stack = &cctx->ctx_type_stack;
459
460 if ((isn = generate_instr(cctx, isn_type)) == NULL)
461 return NULL;
462
463 if (ga_grow(stack, 1) == FAIL)
464 return NULL;
465 ((type_T **)stack->ga_data)[stack->ga_len] = type;
466 ++stack->ga_len;
467
468 return isn;
469}
470
471/*
472 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
473 */
474 static int
475may_generate_2STRING(int offset, cctx_T *cctx)
476{
477 isn_T *isn;
478 garray_T *stack = &cctx->ctx_type_stack;
479 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
480
481 if ((*type)->tt_type == VAR_STRING)
482 return OK;
483 *type = &t_string;
484
485 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
486 return FAIL;
487 isn->isn_arg.number = offset;
488
489 return OK;
490}
491
492 static int
493check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
494{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200495 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200497 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100498 {
499 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100500 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501 else
502 semsg(_("E1036: %c requires number or float arguments"), *op);
503 return FAIL;
504 }
505 return OK;
506}
507
508/*
509 * Generate an instruction with two arguments. The instruction depends on the
510 * type of the arguments.
511 */
512 static int
513generate_two_op(cctx_T *cctx, char_u *op)
514{
515 garray_T *stack = &cctx->ctx_type_stack;
516 type_T *type1;
517 type_T *type2;
518 vartype_T vartype;
519 isn_T *isn;
520
Bram Moolenaar080457c2020-03-03 21:53:32 +0100521 RETURN_OK_IF_SKIP(cctx);
522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 // Get the known type of the two items on the stack. If they are matching
524 // use a type-specific instruction. Otherwise fall back to runtime type
525 // checking.
526 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
527 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200528 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 if (type1->tt_type == type2->tt_type
530 && (type1->tt_type == VAR_NUMBER
531 || type1->tt_type == VAR_LIST
532#ifdef FEAT_FLOAT
533 || type1->tt_type == VAR_FLOAT
534#endif
535 || type1->tt_type == VAR_BLOB))
536 vartype = type1->tt_type;
537
538 switch (*op)
539 {
540 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200541 && type1->tt_type != VAR_ANY
542 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543 && check_number_or_float(
544 type1->tt_type, type2->tt_type, op) == FAIL)
545 return FAIL;
546 isn = generate_instr_drop(cctx,
547 vartype == VAR_NUMBER ? ISN_OPNR
548 : vartype == VAR_LIST ? ISN_ADDLIST
549 : vartype == VAR_BLOB ? ISN_ADDBLOB
550#ifdef FEAT_FLOAT
551 : vartype == VAR_FLOAT ? ISN_OPFLOAT
552#endif
553 : ISN_OPANY, 1);
554 if (isn != NULL)
555 isn->isn_arg.op.op_type = EXPR_ADD;
556 break;
557
558 case '-':
559 case '*':
560 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
561 op) == FAIL)
562 return FAIL;
563 if (vartype == VAR_NUMBER)
564 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
565#ifdef FEAT_FLOAT
566 else if (vartype == VAR_FLOAT)
567 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
568#endif
569 else
570 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
571 if (isn != NULL)
572 isn->isn_arg.op.op_type = *op == '*'
573 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
574 break;
575
Bram Moolenaar4c683752020-04-05 21:38:23 +0200576 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200578 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 && type2->tt_type != VAR_NUMBER))
580 {
581 emsg(_("E1035: % requires number arguments"));
582 return FAIL;
583 }
584 isn = generate_instr_drop(cctx,
585 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
586 if (isn != NULL)
587 isn->isn_arg.op.op_type = EXPR_REM;
588 break;
589 }
590
591 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200592 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 {
594 type_T *type = &t_any;
595
596#ifdef FEAT_FLOAT
597 // float+number and number+float results in float
598 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
599 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
600 type = &t_float;
601#endif
602 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
603 }
604
605 return OK;
606}
607
608/*
609 * Generate an ISN_COMPARE* instruction with a boolean result.
610 */
611 static int
612generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
613{
614 isntype_T isntype = ISN_DROP;
615 isn_T *isn;
616 garray_T *stack = &cctx->ctx_type_stack;
617 vartype_T type1;
618 vartype_T type2;
619
Bram Moolenaar080457c2020-03-03 21:53:32 +0100620 RETURN_OK_IF_SKIP(cctx);
621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622 // Get the known type of the two items on the stack. If they are matching
623 // use a type-specific instruction. Otherwise fall back to runtime type
624 // checking.
625 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
626 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200627 if (type1 == VAR_UNKNOWN)
628 type1 = VAR_ANY;
629 if (type2 == VAR_UNKNOWN)
630 type2 = VAR_ANY;
631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632 if (type1 == type2)
633 {
634 switch (type1)
635 {
636 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
637 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
638 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
639 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
640 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
641 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
642 case VAR_LIST: isntype = ISN_COMPARELIST; break;
643 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
644 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
645 case VAR_PARTIAL: isntype = ISN_COMPAREPARTIAL; break;
646 default: isntype = ISN_COMPAREANY; break;
647 }
648 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200649 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
651 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
652 isntype = ISN_COMPAREANY;
653
654 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
655 && (isntype == ISN_COMPAREBOOL
656 || isntype == ISN_COMPARESPECIAL
657 || isntype == ISN_COMPARENR
658 || isntype == ISN_COMPAREFLOAT))
659 {
660 semsg(_("E1037: Cannot use \"%s\" with %s"),
661 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
662 return FAIL;
663 }
664 if (isntype == ISN_DROP
665 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
666 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
667 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
668 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
669 && exptype != EXPR_IS && exptype != EXPR_ISNOT
670 && (type1 == VAR_BLOB || type2 == VAR_BLOB
671 || type1 == VAR_LIST || type2 == VAR_LIST))))
672 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100673 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674 vartype_name(type1), vartype_name(type2));
675 return FAIL;
676 }
677
678 if ((isn = generate_instr(cctx, isntype)) == NULL)
679 return FAIL;
680 isn->isn_arg.op.op_type = exptype;
681 isn->isn_arg.op.op_ic = ic;
682
683 // takes two arguments, puts one bool back
684 if (stack->ga_len >= 2)
685 {
686 --stack->ga_len;
687 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
688 }
689
690 return OK;
691}
692
693/*
694 * Generate an ISN_2BOOL instruction.
695 */
696 static int
697generate_2BOOL(cctx_T *cctx, int invert)
698{
699 isn_T *isn;
700 garray_T *stack = &cctx->ctx_type_stack;
701
Bram Moolenaar080457c2020-03-03 21:53:32 +0100702 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
704 return FAIL;
705 isn->isn_arg.number = invert;
706
707 // type becomes bool
708 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
709
710 return OK;
711}
712
713 static int
714generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
715{
716 isn_T *isn;
717 garray_T *stack = &cctx->ctx_type_stack;
718
Bram Moolenaar080457c2020-03-03 21:53:32 +0100719 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
721 return FAIL;
722 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
723 isn->isn_arg.type.ct_off = offset;
724
725 // type becomes vartype
726 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
727
728 return OK;
729}
730
731/*
732 * Generate an ISN_PUSHNR instruction.
733 */
734 static int
735generate_PUSHNR(cctx_T *cctx, varnumber_T number)
736{
737 isn_T *isn;
738
Bram Moolenaar080457c2020-03-03 21:53:32 +0100739 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100740 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
741 return FAIL;
742 isn->isn_arg.number = number;
743
744 return OK;
745}
746
747/*
748 * Generate an ISN_PUSHBOOL instruction.
749 */
750 static int
751generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
752{
753 isn_T *isn;
754
Bram Moolenaar080457c2020-03-03 21:53:32 +0100755 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
757 return FAIL;
758 isn->isn_arg.number = number;
759
760 return OK;
761}
762
763/*
764 * Generate an ISN_PUSHSPEC instruction.
765 */
766 static int
767generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
768{
769 isn_T *isn;
770
Bram Moolenaar080457c2020-03-03 21:53:32 +0100771 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
773 return FAIL;
774 isn->isn_arg.number = number;
775
776 return OK;
777}
778
779#ifdef FEAT_FLOAT
780/*
781 * Generate an ISN_PUSHF instruction.
782 */
783 static int
784generate_PUSHF(cctx_T *cctx, float_T fnumber)
785{
786 isn_T *isn;
787
Bram Moolenaar080457c2020-03-03 21:53:32 +0100788 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
790 return FAIL;
791 isn->isn_arg.fnumber = fnumber;
792
793 return OK;
794}
795#endif
796
797/*
798 * Generate an ISN_PUSHS instruction.
799 * Consumes "str".
800 */
801 static int
802generate_PUSHS(cctx_T *cctx, char_u *str)
803{
804 isn_T *isn;
805
Bram Moolenaar080457c2020-03-03 21:53:32 +0100806 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
808 return FAIL;
809 isn->isn_arg.string = str;
810
811 return OK;
812}
813
814/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100815 * Generate an ISN_PUSHCHANNEL instruction.
816 * Consumes "channel".
817 */
818 static int
819generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
820{
821 isn_T *isn;
822
Bram Moolenaar080457c2020-03-03 21:53:32 +0100823 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100824 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
825 return FAIL;
826 isn->isn_arg.channel = channel;
827
828 return OK;
829}
830
831/*
832 * Generate an ISN_PUSHJOB instruction.
833 * Consumes "job".
834 */
835 static int
836generate_PUSHJOB(cctx_T *cctx, job_T *job)
837{
838 isn_T *isn;
839
Bram Moolenaar080457c2020-03-03 21:53:32 +0100840 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100841 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100842 return FAIL;
843 isn->isn_arg.job = job;
844
845 return OK;
846}
847
848/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849 * Generate an ISN_PUSHBLOB instruction.
850 * Consumes "blob".
851 */
852 static int
853generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
854{
855 isn_T *isn;
856
Bram Moolenaar080457c2020-03-03 21:53:32 +0100857 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
859 return FAIL;
860 isn->isn_arg.blob = blob;
861
862 return OK;
863}
864
865/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100866 * Generate an ISN_PUSHFUNC instruction with name "name".
867 * Consumes "name".
868 */
869 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200870generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100871{
872 isn_T *isn;
873
Bram Moolenaar080457c2020-03-03 21:53:32 +0100874 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200875 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100876 return FAIL;
877 isn->isn_arg.string = name;
878
879 return OK;
880}
881
882/*
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100883 * Generate an ISN_PUSHPARTIAL instruction with partial "part".
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200884 * Consumes "part".
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100885 */
886 static int
887generate_PUSHPARTIAL(cctx_T *cctx, partial_T *part)
888{
889 isn_T *isn;
890
Bram Moolenaar080457c2020-03-03 21:53:32 +0100891 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaard77a8522020-04-03 21:59:57 +0200892 if ((isn = generate_instr_type(cctx, ISN_PUSHPARTIAL, &t_func_any)) == NULL)
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100893 return FAIL;
894 isn->isn_arg.partial = part;
895
896 return OK;
897}
898
899/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 * Generate an ISN_STORE instruction.
901 */
902 static int
903generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
904{
905 isn_T *isn;
906
Bram Moolenaar080457c2020-03-03 21:53:32 +0100907 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
909 return FAIL;
910 if (name != NULL)
911 isn->isn_arg.string = vim_strsave(name);
912 else
913 isn->isn_arg.number = idx;
914
915 return OK;
916}
917
918/*
919 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
920 */
921 static int
922generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
923{
924 isn_T *isn;
925
Bram Moolenaar080457c2020-03-03 21:53:32 +0100926 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
928 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100929 isn->isn_arg.storenr.stnr_idx = idx;
930 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931
932 return OK;
933}
934
935/*
936 * Generate an ISN_STOREOPT instruction
937 */
938 static int
939generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
940{
941 isn_T *isn;
942
Bram Moolenaar080457c2020-03-03 21:53:32 +0100943 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
945 return FAIL;
946 isn->isn_arg.storeopt.so_name = vim_strsave(name);
947 isn->isn_arg.storeopt.so_flags = opt_flags;
948
949 return OK;
950}
951
952/*
953 * Generate an ISN_LOAD or similar instruction.
954 */
955 static int
956generate_LOAD(
957 cctx_T *cctx,
958 isntype_T isn_type,
959 int idx,
960 char_u *name,
961 type_T *type)
962{
963 isn_T *isn;
964
Bram Moolenaar080457c2020-03-03 21:53:32 +0100965 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
967 return FAIL;
968 if (name != NULL)
969 isn->isn_arg.string = vim_strsave(name);
970 else
971 isn->isn_arg.number = idx;
972
973 return OK;
974}
975
976/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100977 * Generate an ISN_LOADV instruction.
978 */
979 static int
980generate_LOADV(
981 cctx_T *cctx,
982 char_u *name,
983 int error)
984{
985 // load v:var
986 int vidx = find_vim_var(name);
987
Bram Moolenaar080457c2020-03-03 21:53:32 +0100988 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100989 if (vidx < 0)
990 {
991 if (error)
992 semsg(_(e_var_notfound), name);
993 return FAIL;
994 }
995
996 // TODO: get actual type
997 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
998}
999
1000/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 * Generate an ISN_LOADS instruction.
1002 */
1003 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001004generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001006 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001008 int sid,
1009 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010{
1011 isn_T *isn;
1012
Bram Moolenaar080457c2020-03-03 21:53:32 +01001013 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001014 if (isn_type == ISN_LOADS)
1015 isn = generate_instr_type(cctx, isn_type, type);
1016 else
1017 isn = generate_instr_drop(cctx, isn_type, 1);
1018 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001020 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1021 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022
1023 return OK;
1024}
1025
1026/*
1027 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1028 */
1029 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001030generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 cctx_T *cctx,
1032 isntype_T isn_type,
1033 int sid,
1034 int idx,
1035 type_T *type)
1036{
1037 isn_T *isn;
1038
Bram Moolenaar080457c2020-03-03 21:53:32 +01001039 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040 if (isn_type == ISN_LOADSCRIPT)
1041 isn = generate_instr_type(cctx, isn_type, type);
1042 else
1043 isn = generate_instr_drop(cctx, isn_type, 1);
1044 if (isn == NULL)
1045 return FAIL;
1046 isn->isn_arg.script.script_sid = sid;
1047 isn->isn_arg.script.script_idx = idx;
1048 return OK;
1049}
1050
1051/*
1052 * Generate an ISN_NEWLIST instruction.
1053 */
1054 static int
1055generate_NEWLIST(cctx_T *cctx, int count)
1056{
1057 isn_T *isn;
1058 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 type_T *type;
1060 type_T *member;
1061
Bram Moolenaar080457c2020-03-03 21:53:32 +01001062 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1064 return FAIL;
1065 isn->isn_arg.number = count;
1066
1067 // drop the value types
1068 stack->ga_len -= count;
1069
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001070 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001071 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 if (count > 0)
1073 member = ((type_T **)stack->ga_data)[stack->ga_len];
1074 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001075 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001076 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077
1078 // add the list type to the type stack
1079 if (ga_grow(stack, 1) == FAIL)
1080 return FAIL;
1081 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1082 ++stack->ga_len;
1083
1084 return OK;
1085}
1086
1087/*
1088 * Generate an ISN_NEWDICT instruction.
1089 */
1090 static int
1091generate_NEWDICT(cctx_T *cctx, int count)
1092{
1093 isn_T *isn;
1094 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 type_T *type;
1096 type_T *member;
1097
Bram Moolenaar080457c2020-03-03 21:53:32 +01001098 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1100 return FAIL;
1101 isn->isn_arg.number = count;
1102
1103 // drop the key and value types
1104 stack->ga_len -= 2 * count;
1105
Bram Moolenaar436472f2020-02-20 22:54:43 +01001106 // Use the first value type for the list member type. Use "void" for an
1107 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 if (count > 0)
1109 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1110 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001111 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001112 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113
1114 // add the dict type to the type stack
1115 if (ga_grow(stack, 1) == FAIL)
1116 return FAIL;
1117 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1118 ++stack->ga_len;
1119
1120 return OK;
1121}
1122
1123/*
1124 * Generate an ISN_FUNCREF instruction.
1125 */
1126 static int
1127generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1128{
1129 isn_T *isn;
1130 garray_T *stack = &cctx->ctx_type_stack;
1131
Bram Moolenaar080457c2020-03-03 21:53:32 +01001132 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1134 return FAIL;
1135 isn->isn_arg.number = dfunc_idx;
1136
1137 if (ga_grow(stack, 1) == FAIL)
1138 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001139 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 // TODO: argument and return types
1141 ++stack->ga_len;
1142
1143 return OK;
1144}
1145
1146/*
1147 * Generate an ISN_JUMP instruction.
1148 */
1149 static int
1150generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1151{
1152 isn_T *isn;
1153 garray_T *stack = &cctx->ctx_type_stack;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1157 return FAIL;
1158 isn->isn_arg.jump.jump_when = when;
1159 isn->isn_arg.jump.jump_where = where;
1160
1161 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1162 --stack->ga_len;
1163
1164 return OK;
1165}
1166
1167 static int
1168generate_FOR(cctx_T *cctx, int loop_idx)
1169{
1170 isn_T *isn;
1171 garray_T *stack = &cctx->ctx_type_stack;
1172
Bram Moolenaar080457c2020-03-03 21:53:32 +01001173 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1175 return FAIL;
1176 isn->isn_arg.forloop.for_idx = loop_idx;
1177
1178 if (ga_grow(stack, 1) == FAIL)
1179 return FAIL;
1180 // type doesn't matter, will be stored next
1181 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1182 ++stack->ga_len;
1183
1184 return OK;
1185}
1186
1187/*
1188 * Generate an ISN_BCALL instruction.
1189 * Return FAIL if the number of arguments is wrong.
1190 */
1191 static int
1192generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1193{
1194 isn_T *isn;
1195 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001196 type_T *argtypes[MAX_FUNC_ARGS];
1197 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198
Bram Moolenaar080457c2020-03-03 21:53:32 +01001199 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 if (check_internal_func(func_idx, argcount) == FAIL)
1201 return FAIL;
1202
1203 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1204 return FAIL;
1205 isn->isn_arg.bfunc.cbf_idx = func_idx;
1206 isn->isn_arg.bfunc.cbf_argcount = argcount;
1207
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001208 for (i = 0; i < argcount; ++i)
1209 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 stack->ga_len -= argcount; // drop the arguments
1212 if (ga_grow(stack, 1) == FAIL)
1213 return FAIL;
1214 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001215 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 ++stack->ga_len; // add return value
1217
1218 return OK;
1219}
1220
1221/*
1222 * Generate an ISN_DCALL or ISN_UCALL instruction.
1223 * Return FAIL if the number of arguments is wrong.
1224 */
1225 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001226generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227{
1228 isn_T *isn;
1229 garray_T *stack = &cctx->ctx_type_stack;
1230 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001231 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232
Bram Moolenaar080457c2020-03-03 21:53:32 +01001233 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 if (argcount > regular_args && !has_varargs(ufunc))
1235 {
1236 semsg(_(e_toomanyarg), ufunc->uf_name);
1237 return FAIL;
1238 }
1239 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1240 {
1241 semsg(_(e_toofewarg), ufunc->uf_name);
1242 return FAIL;
1243 }
1244
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001245 if (ufunc->uf_dfunc_idx >= 0)
1246 {
1247 int i;
1248
1249 for (i = 0; i < argcount; ++i)
1250 {
1251 type_T *expected;
1252 type_T *actual;
1253
1254 if (i < regular_args)
1255 {
1256 if (ufunc->uf_arg_types == NULL)
1257 continue;
1258 expected = ufunc->uf_arg_types[i];
1259 }
1260 else
1261 expected = ufunc->uf_va_type->tt_member;
1262 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1263 if (check_type(expected, actual, FALSE) == FAIL)
1264 {
1265 arg_type_mismatch(expected, actual, i + 1);
1266 return FAIL;
1267 }
1268 }
1269 }
1270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 // Turn varargs into a list.
1272 if (ufunc->uf_va_name != NULL)
1273 {
1274 int count = argcount - regular_args;
1275
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001276 // If count is negative an empty list will be added after evaluating
1277 // default values for missing optional arguments.
1278 if (count >= 0)
1279 {
1280 generate_NEWLIST(cctx, count);
1281 argcount = regular_args + 1;
1282 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 }
1284
1285 if ((isn = generate_instr(cctx,
1286 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1287 return FAIL;
1288 if (ufunc->uf_dfunc_idx >= 0)
1289 {
1290 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1291 isn->isn_arg.dfunc.cdf_argcount = argcount;
1292 }
1293 else
1294 {
1295 // A user function may be deleted and redefined later, can't use the
1296 // ufunc pointer, need to look it up again at runtime.
1297 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1298 isn->isn_arg.ufunc.cuf_argcount = argcount;
1299 }
1300
1301 stack->ga_len -= argcount; // drop the arguments
1302 if (ga_grow(stack, 1) == FAIL)
1303 return FAIL;
1304 // add return value
1305 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1306 ++stack->ga_len;
1307
1308 return OK;
1309}
1310
1311/*
1312 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1313 */
1314 static int
1315generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1316{
1317 isn_T *isn;
1318 garray_T *stack = &cctx->ctx_type_stack;
1319
Bram Moolenaar080457c2020-03-03 21:53:32 +01001320 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1322 return FAIL;
1323 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1324 isn->isn_arg.ufunc.cuf_argcount = argcount;
1325
1326 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001327 if (ga_grow(stack, 1) == FAIL)
1328 return FAIL;
1329 // add return value
1330 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1331 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332
1333 return OK;
1334}
1335
1336/*
1337 * Generate an ISN_PCALL instruction.
1338 */
1339 static int
1340generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1341{
1342 isn_T *isn;
1343 garray_T *stack = &cctx->ctx_type_stack;
1344
Bram Moolenaar080457c2020-03-03 21:53:32 +01001345 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1347 return FAIL;
1348 isn->isn_arg.pfunc.cpf_top = at_top;
1349 isn->isn_arg.pfunc.cpf_argcount = argcount;
1350
1351 stack->ga_len -= argcount; // drop the arguments
1352
1353 // drop the funcref/partial, get back the return value
1354 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1355
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001356 // If partial is above the arguments it must be cleared and replaced with
1357 // the return value.
1358 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1359 return FAIL;
1360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 return OK;
1362}
1363
1364/*
1365 * Generate an ISN_MEMBER instruction.
1366 */
1367 static int
1368generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1369{
1370 isn_T *isn;
1371 garray_T *stack = &cctx->ctx_type_stack;
1372 type_T *type;
1373
Bram Moolenaar080457c2020-03-03 21:53:32 +01001374 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1376 return FAIL;
1377 isn->isn_arg.string = vim_strnsave(name, (int)len);
1378
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001379 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001381 if (type->tt_type != VAR_DICT && type != &t_any)
1382 {
1383 emsg(_(e_dictreq));
1384 return FAIL;
1385 }
1386 // change dict type to dict member type
1387 if (type->tt_type == VAR_DICT)
1388 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389
1390 return OK;
1391}
1392
1393/*
1394 * Generate an ISN_ECHO instruction.
1395 */
1396 static int
1397generate_ECHO(cctx_T *cctx, int with_white, int count)
1398{
1399 isn_T *isn;
1400
Bram Moolenaar080457c2020-03-03 21:53:32 +01001401 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1403 return FAIL;
1404 isn->isn_arg.echo.echo_with_white = with_white;
1405 isn->isn_arg.echo.echo_count = count;
1406
1407 return OK;
1408}
1409
Bram Moolenaarad39c092020-02-26 18:23:43 +01001410/*
1411 * Generate an ISN_EXECUTE instruction.
1412 */
1413 static int
1414generate_EXECUTE(cctx_T *cctx, int count)
1415{
1416 isn_T *isn;
1417
1418 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1419 return FAIL;
1420 isn->isn_arg.number = count;
1421
1422 return OK;
1423}
1424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 static int
1426generate_EXEC(cctx_T *cctx, char_u *line)
1427{
1428 isn_T *isn;
1429
Bram Moolenaar080457c2020-03-03 21:53:32 +01001430 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1432 return FAIL;
1433 isn->isn_arg.string = vim_strsave(line);
1434 return OK;
1435}
1436
1437static char e_white_both[] =
1438 N_("E1004: white space required before and after '%s'");
Bram Moolenaard77a8522020-04-03 21:59:57 +02001439static char e_white_after[] = N_("E1069: white space required after '%s'");
1440static char e_no_white_before[] = N_("E1068: No white space allowed before '%s'");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441
1442/*
1443 * Reserve space for a local variable.
1444 * Return the index or -1 if it failed.
1445 */
1446 static int
1447reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1448{
1449 int idx;
1450 lvar_T *lvar;
1451
1452 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1453 {
1454 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1455 return -1;
1456 }
1457
1458 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1459 return -1;
1460 idx = cctx->ctx_locals.ga_len;
1461 if (cctx->ctx_max_local < idx + 1)
1462 cctx->ctx_max_local = idx + 1;
1463 ++cctx->ctx_locals.ga_len;
1464
1465 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1466 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1467 lvar->lv_const = isConst;
1468 lvar->lv_type = type;
1469
1470 return idx;
1471}
1472
1473/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001474 * Remove local variables above "new_top".
1475 */
1476 static void
1477unwind_locals(cctx_T *cctx, int new_top)
1478{
1479 if (cctx->ctx_locals.ga_len > new_top)
1480 {
1481 int idx;
1482 lvar_T *lvar;
1483
1484 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1485 {
1486 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1487 vim_free(lvar->lv_name);
1488 }
1489 }
1490 cctx->ctx_locals.ga_len = new_top;
1491}
1492
1493/*
1494 * Free all local variables.
1495 */
1496 static void
1497free_local(cctx_T *cctx)
1498{
1499 unwind_locals(cctx, 0);
1500 ga_clear(&cctx->ctx_locals);
1501}
1502
1503/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 * Skip over a type definition and return a pointer to just after it.
1505 */
1506 char_u *
1507skip_type(char_u *start)
1508{
1509 char_u *p = start;
1510
1511 while (ASCII_ISALNUM(*p) || *p == '_')
1512 ++p;
1513
1514 // Skip over "<type>"; this is permissive about white space.
1515 if (*skipwhite(p) == '<')
1516 {
1517 p = skipwhite(p);
1518 p = skip_type(skipwhite(p + 1));
1519 p = skipwhite(p);
1520 if (*p == '>')
1521 ++p;
1522 }
1523 return p;
1524}
1525
1526/*
1527 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001528 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529 * Returns NULL in case of failure.
1530 */
1531 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001532parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533{
1534 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001535 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536
1537 if (**arg != '<')
1538 {
1539 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001540 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 else
1542 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001543 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 }
1545 *arg = skipwhite(*arg + 1);
1546
Bram Moolenaard77a8522020-04-03 21:59:57 +02001547 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548
1549 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001550 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 {
1552 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001553 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 }
1555 ++*arg;
1556
1557 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001558 return get_list_type(member_type, type_gap);
1559 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560}
1561
1562/*
1563 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001564 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 */
1566 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001567parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568{
1569 char_u *p = *arg;
1570 size_t len;
1571
1572 // skip over the first word
1573 while (ASCII_ISALNUM(*p) || *p == '_')
1574 ++p;
1575 len = p - *arg;
1576
1577 switch (**arg)
1578 {
1579 case 'a':
1580 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1581 {
1582 *arg += len;
1583 return &t_any;
1584 }
1585 break;
1586 case 'b':
1587 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1588 {
1589 *arg += len;
1590 return &t_bool;
1591 }
1592 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1593 {
1594 *arg += len;
1595 return &t_blob;
1596 }
1597 break;
1598 case 'c':
1599 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1600 {
1601 *arg += len;
1602 return &t_channel;
1603 }
1604 break;
1605 case 'd':
1606 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1607 {
1608 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001609 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 }
1611 break;
1612 case 'f':
1613 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1614 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001615#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 *arg += len;
1617 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001618#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001619 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001620 return &t_any;
1621#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 }
1623 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1624 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001625 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001626 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001627 int argcount = -1;
1628 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001629 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001630 type_T *arg_type[MAX_FUNC_ARGS + 1];
1631
1632 // func({type}, ...): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001634 if (**arg == '(')
1635 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001636 // "func" may or may not return a value, "func()" does
1637 // not return a value.
1638 ret_type = &t_void;
1639
Bram Moolenaard77a8522020-04-03 21:59:57 +02001640 p = ++*arg;
1641 argcount = 0;
1642 while (*p != NUL && *p != ')')
1643 {
1644 if (STRNCMP(p, "...", 3) == 0)
1645 {
1646 flags |= TTFLAG_VARARGS;
1647 break;
1648 }
1649 arg_type[argcount++] = parse_type(&p, type_gap);
1650
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001651 if (*p == '?')
1652 {
1653 if (first_optional == -1)
1654 first_optional = argcount;
1655 ++p;
1656 }
1657 else if (first_optional != -1)
1658 {
1659 emsg(_("E1007: mandatory argument after optional argument"));
1660 return &t_any;
1661 }
1662
Bram Moolenaard77a8522020-04-03 21:59:57 +02001663 if (*p != ',' && *skipwhite(p) == ',')
1664 {
1665 semsg(_(e_no_white_before), ",");
1666 return &t_any;
1667 }
1668 if (*p == ',')
1669 {
1670 ++p;
1671 if (!VIM_ISWHITE(*p))
1672 semsg(_(e_white_after), ",");
1673 }
1674 p = skipwhite(p);
1675 if (argcount == MAX_FUNC_ARGS)
1676 {
1677 emsg(_("E740: Too many argument types"));
1678 return &t_any;
1679 }
1680 }
1681
1682 p = skipwhite(p);
1683 if (*p != ')')
1684 {
1685 emsg(_(e_missing_close));
1686 return &t_any;
1687 }
1688 *arg = p + 1;
1689 }
1690 if (**arg == ':')
1691 {
1692 // parse return type
1693 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001694 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001695 semsg(_(e_white_after), ":");
1696 *arg = skipwhite(*arg);
1697 ret_type = parse_type(arg, type_gap);
1698 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001699 type = get_func_type(ret_type,
1700 flags == 0 && first_optional == -1 ? argcount : 99,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001701 type_gap);
1702 if (flags != 0)
1703 type->tt_flags = flags;
1704 if (argcount > 0)
1705 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001706 if (func_type_add_arg_types(type, argcount,
1707 first_optional == -1 ? argcount : first_optional,
1708 type_gap) == FAIL)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001709 return &t_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001710 mch_memmove(type->tt_args, arg_type,
1711 sizeof(type_T *) * argcount);
1712 }
1713 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 }
1715 break;
1716 case 'j':
1717 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1718 {
1719 *arg += len;
1720 return &t_job;
1721 }
1722 break;
1723 case 'l':
1724 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1725 {
1726 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001727 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 }
1729 break;
1730 case 'n':
1731 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1732 {
1733 *arg += len;
1734 return &t_number;
1735 }
1736 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 case 's':
1738 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1739 {
1740 *arg += len;
1741 return &t_string;
1742 }
1743 break;
1744 case 'v':
1745 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1746 {
1747 *arg += len;
1748 return &t_void;
1749 }
1750 break;
1751 }
1752
1753 semsg(_("E1010: Type not recognized: %s"), *arg);
1754 return &t_any;
1755}
1756
1757/*
1758 * Check if "type1" and "type2" are exactly the same.
1759 */
1760 static int
1761equal_type(type_T *type1, type_T *type2)
1762{
1763 if (type1->tt_type != type2->tt_type)
1764 return FALSE;
1765 switch (type1->tt_type)
1766 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001768 case VAR_ANY:
1769 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 case VAR_SPECIAL:
1771 case VAR_BOOL:
1772 case VAR_NUMBER:
1773 case VAR_FLOAT:
1774 case VAR_STRING:
1775 case VAR_BLOB:
1776 case VAR_JOB:
1777 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001778 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 case VAR_LIST:
1780 case VAR_DICT:
1781 return equal_type(type1->tt_member, type2->tt_member);
1782 case VAR_FUNC:
1783 case VAR_PARTIAL:
1784 // TODO; check argument types.
1785 return equal_type(type1->tt_member, type2->tt_member)
1786 && type1->tt_argcount == type2->tt_argcount;
1787 }
1788 return TRUE;
1789}
1790
1791/*
1792 * Find the common type of "type1" and "type2" and put it in "dest".
1793 * "type2" and "dest" may be the same.
1794 */
1795 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02001796common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797{
1798 if (equal_type(type1, type2))
1799 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001800 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 return;
1802 }
1803
1804 if (type1->tt_type == type2->tt_type)
1805 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1807 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001808 type_T *common;
1809
Bram Moolenaard77a8522020-04-03 21:59:57 +02001810 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001811 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001812 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001813 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001814 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 return;
1816 }
1817 // TODO: VAR_FUNC and VAR_PARTIAL
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001818 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 }
1820
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001821 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822}
1823
1824 char *
1825vartype_name(vartype_T type)
1826{
1827 switch (type)
1828 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001829 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02001830 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 case VAR_SPECIAL: return "special";
1833 case VAR_BOOL: return "bool";
1834 case VAR_NUMBER: return "number";
1835 case VAR_FLOAT: return "float";
1836 case VAR_STRING: return "string";
1837 case VAR_BLOB: return "blob";
1838 case VAR_JOB: return "job";
1839 case VAR_CHANNEL: return "channel";
1840 case VAR_LIST: return "list";
1841 case VAR_DICT: return "dict";
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001842 case VAR_FUNC: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 case VAR_PARTIAL: return "partial";
1844 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02001845 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846}
1847
1848/*
1849 * Return the name of a type.
1850 * The result may be in allocated memory, in which case "tofree" is set.
1851 */
1852 char *
1853type_name(type_T *type, char **tofree)
1854{
1855 char *name = vartype_name(type->tt_type);
1856
1857 *tofree = NULL;
1858 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1859 {
1860 char *member_free;
1861 char *member_name = type_name(type->tt_member, &member_free);
1862 size_t len;
1863
1864 len = STRLEN(name) + STRLEN(member_name) + 3;
1865 *tofree = alloc(len);
1866 if (*tofree != NULL)
1867 {
1868 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1869 vim_free(member_free);
1870 return *tofree;
1871 }
1872 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001873 if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1874 {
1875 garray_T ga;
1876 int i;
1877
1878 ga_init2(&ga, 1, 100);
1879 if (ga_grow(&ga, 20) == FAIL)
1880 return "[unknown]";
1881 *tofree = ga.ga_data;
1882 STRCPY(ga.ga_data, "func(");
1883 ga.ga_len += 5;
1884
1885 for (i = 0; i < type->tt_argcount; ++i)
1886 {
1887 char *arg_free;
1888 char *arg_type = type_name(type->tt_args[i], &arg_free);
1889 int len;
1890
1891 if (i > 0)
1892 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001893 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001894 ga.ga_len += 2;
1895 }
1896 len = (int)STRLEN(arg_type);
1897 if (ga_grow(&ga, len + 6) == FAIL)
1898 {
1899 vim_free(arg_free);
1900 return "[unknown]";
1901 }
1902 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001903 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001904 ga.ga_len += len;
1905 vim_free(arg_free);
1906 }
1907
1908 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001909 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001910 else
1911 {
1912 char *ret_free;
1913 char *ret_name = type_name(type->tt_member, &ret_free);
1914 int len;
1915
1916 len = (int)STRLEN(ret_name) + 4;
1917 if (ga_grow(&ga, len) == FAIL)
1918 {
1919 vim_free(ret_free);
1920 return "[unknown]";
1921 }
1922 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001923 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
1924 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001925 vim_free(ret_free);
1926 }
1927 return ga.ga_data;
1928 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929
1930 return name;
1931}
1932
1933/*
1934 * Find "name" in script-local items of script "sid".
1935 * Returns the index in "sn_var_vals" if found.
1936 * If found but not in "sn_var_vals" returns -1.
1937 * If not found returns -2.
1938 */
1939 int
1940get_script_item_idx(int sid, char_u *name, int check_writable)
1941{
1942 hashtab_T *ht;
1943 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001944 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 int idx;
1946
1947 // First look the name up in the hashtable.
1948 if (sid <= 0 || sid > script_items.ga_len)
1949 return -1;
1950 ht = &SCRIPT_VARS(sid);
1951 di = find_var_in_ht(ht, 0, name, TRUE);
1952 if (di == NULL)
1953 return -2;
1954
1955 // Now find the svar_T index in sn_var_vals.
1956 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1957 {
1958 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1959
1960 if (sv->sv_tv == &di->di_tv)
1961 {
1962 if (check_writable && sv->sv_const)
1963 semsg(_(e_readonlyvar), name);
1964 return idx;
1965 }
1966 }
1967 return -1;
1968}
1969
1970/*
1971 * Find "name" in imported items of the current script/
1972 */
1973 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001974find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001976 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 int idx;
1978
1979 if (cctx != NULL)
1980 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1981 {
1982 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1983 + idx;
1984
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001985 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1986 : STRLEN(import->imp_name) == len
1987 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 return import;
1989 }
1990
1991 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1992 {
1993 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1994
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001995 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1996 : STRLEN(import->imp_name) == len
1997 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 return import;
1999 }
2000 return NULL;
2001}
2002
2003/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002004 * Free all imported variables.
2005 */
2006 static void
2007free_imported(cctx_T *cctx)
2008{
2009 int idx;
2010
2011 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2012 {
2013 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2014
2015 vim_free(import->imp_name);
2016 }
2017 ga_clear(&cctx->ctx_imports);
2018}
2019
2020/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002021 * Generate an instruction to load script-local variable "name", without the
2022 * leading "s:".
2023 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 */
2025 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002026compile_load_scriptvar(
2027 cctx_T *cctx,
2028 char_u *name, // variable NUL terminated
2029 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002030 char_u **end, // end of variable
2031 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002033 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2035 imported_T *import;
2036
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002037 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002039 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002040 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2041 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 }
2043 if (idx >= 0)
2044 {
2045 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2046
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002047 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 current_sctx.sc_sid, idx, sv->sv_type);
2049 return OK;
2050 }
2051
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002052 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 if (import != NULL)
2054 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002055 if (import->imp_all)
2056 {
2057 char_u *p = skipwhite(*end);
2058 int name_len;
2059 ufunc_T *ufunc;
2060 type_T *type;
2061
2062 // Used "import * as Name", need to lookup the member.
2063 if (*p != '.')
2064 {
2065 semsg(_("E1060: expected dot after name: %s"), start);
2066 return FAIL;
2067 }
2068 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002069 if (VIM_ISWHITE(*p))
2070 {
2071 emsg(_("E1074: no white space allowed after dot"));
2072 return FAIL;
2073 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002074
2075 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2076 // TODO: what if it is a function?
2077 if (idx < 0)
2078 return FAIL;
2079 *end = p;
2080
2081 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2082 import->imp_sid,
2083 idx,
2084 type);
2085 }
2086 else
2087 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002088 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002089 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2090 import->imp_sid,
2091 import->imp_var_vals_idx,
2092 import->imp_type);
2093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 return OK;
2095 }
2096
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002097 if (error)
2098 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 return FAIL;
2100}
2101
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002102 static int
2103generate_funcref(cctx_T *cctx, char_u *name)
2104{
2105 ufunc_T *ufunc = find_func(name, cctx);
2106
2107 if (ufunc == NULL)
2108 return FAIL;
2109
2110 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2111}
2112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113/*
2114 * Compile a variable name into a load instruction.
2115 * "end" points to just after the name.
2116 * When "error" is FALSE do not give an error when not found.
2117 */
2118 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002119compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120{
2121 type_T *type;
2122 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002123 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002125 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126
2127 if (*(*arg + 1) == ':')
2128 {
2129 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002130 if (end <= *arg + 2)
2131 name = vim_strsave((char_u *)"[empty]");
2132 else
2133 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 if (name == NULL)
2135 return FAIL;
2136
2137 if (**arg == 'v')
2138 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002139 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 }
2141 else if (**arg == 'g')
2142 {
2143 // Global variables can be defined later, thus we don't check if it
2144 // exists, give error at runtime.
2145 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2146 }
2147 else if (**arg == 's')
2148 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002149 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002151 else if (**arg == 'b')
2152 {
2153 semsg("Namespace b: not supported yet: %s", *arg);
2154 goto theend;
2155 }
2156 else if (**arg == 'w')
2157 {
2158 semsg("Namespace w: not supported yet: %s", *arg);
2159 goto theend;
2160 }
2161 else if (**arg == 't')
2162 {
2163 semsg("Namespace t: not supported yet: %s", *arg);
2164 goto theend;
2165 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 else
2167 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002168 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 goto theend;
2170 }
2171 }
2172 else
2173 {
2174 size_t len = end - *arg;
2175 int idx;
2176 int gen_load = FALSE;
2177
2178 name = vim_strnsave(*arg, end - *arg);
2179 if (name == NULL)
2180 return FAIL;
2181
2182 idx = lookup_arg(*arg, len, cctx);
2183 if (idx >= 0)
2184 {
2185 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2186 type = cctx->ctx_ufunc->uf_arg_types[idx];
2187 else
2188 type = &t_any;
2189
2190 // Arguments are located above the frame pointer.
2191 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2192 if (cctx->ctx_ufunc->uf_va_name != NULL)
2193 --idx;
2194 gen_load = TRUE;
2195 }
2196 else if (lookup_vararg(*arg, len, cctx))
2197 {
2198 // varargs is always the last argument
2199 idx = -STACK_FRAME_SIZE - 1;
2200 type = cctx->ctx_ufunc->uf_va_type;
2201 gen_load = TRUE;
2202 }
2203 else
2204 {
2205 idx = lookup_local(*arg, len, cctx);
2206 if (idx >= 0)
2207 {
2208 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
2209 gen_load = TRUE;
2210 }
2211 else
2212 {
2213 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2214 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2215 res = generate_PUSHBOOL(cctx, **arg == 't'
2216 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002217 else
2218 {
2219 // "var" can be script-local even without using "s:" if it
2220 // already exists.
2221 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2222 == SCRIPT_VERSION_VIM9
2223 || lookup_script(*arg, len) == OK)
2224 res = compile_load_scriptvar(cctx, name, *arg, &end,
2225 FALSE);
2226
2227 // When the name starts with an uppercase letter or "x:" it
2228 // can be a user defined function.
2229 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2230 res = generate_funcref(cctx, name);
2231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 }
2233 }
2234 if (gen_load)
2235 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
2236 }
2237
2238 *arg = end;
2239
2240theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002241 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 semsg(_(e_var_notfound), name);
2243 vim_free(name);
2244 return res;
2245}
2246
2247/*
2248 * Compile the argument expressions.
2249 * "arg" points to just after the "(" and is advanced to after the ")"
2250 */
2251 static int
2252compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2253{
2254 char_u *p = *arg;
2255
2256 while (*p != NUL && *p != ')')
2257 {
2258 if (compile_expr1(&p, cctx) == FAIL)
2259 return FAIL;
2260 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002261
2262 if (*p != ',' && *skipwhite(p) == ',')
2263 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002264 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002265 p = skipwhite(p);
2266 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002268 {
2269 ++p;
2270 if (!VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002271 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002272 }
2273 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002275 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 if (*p != ')')
2277 {
2278 emsg(_(e_missing_close));
2279 return FAIL;
2280 }
2281 *arg = p + 1;
2282 return OK;
2283}
2284
2285/*
2286 * Compile a function call: name(arg1, arg2)
2287 * "arg" points to "name", "arg + varlen" to the "(".
2288 * "argcount_init" is 1 for "value->method()"
2289 * Instructions:
2290 * EVAL arg1
2291 * EVAL arg2
2292 * BCALL / DCALL / UCALL
2293 */
2294 static int
2295compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2296{
2297 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002298 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 int argcount = argcount_init;
2300 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002301 char_u fname_buf[FLEN_FIXED + 1];
2302 char_u *tofree = NULL;
2303 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002305 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306
2307 if (varlen >= sizeof(namebuf))
2308 {
2309 semsg(_("E1011: name too long: %s"), name);
2310 return FAIL;
2311 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002312 vim_strncpy(namebuf, *arg, varlen);
2313 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314
2315 *arg = skipwhite(*arg + varlen + 1);
2316 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002317 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002319 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 {
2321 int idx;
2322
2323 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002324 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002326 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002327 else
2328 semsg(_(e_unknownfunc), namebuf);
2329 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 }
2331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002333 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002335 {
2336 res = generate_CALL(cctx, ufunc, argcount);
2337 goto theend;
2338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339
2340 // If the name is a variable, load it and use PCALL.
2341 p = namebuf;
2342 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002343 {
2344 res = generate_PCALL(cctx, argcount, FALSE);
2345 goto theend;
2346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347
2348 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002349 res = generate_UCALL(cctx, name, argcount);
2350
2351theend:
2352 vim_free(tofree);
2353 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354}
2355
2356// like NAMESPACE_CHAR but with 'a' and 'l'.
2357#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2358
2359/*
2360 * Find the end of a variable or function name. Unlike find_name_end() this
2361 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002362 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 * Return a pointer to just after the name. Equal to "arg" if there is no
2364 * valid name.
2365 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002366 static char_u *
2367to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368{
2369 char_u *p;
2370
2371 // Quick check for valid starting character.
2372 if (!eval_isnamec1(*arg))
2373 return arg;
2374
2375 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2376 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2377 // and can be used in slice "[n:]".
2378 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002379 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2381 break;
2382 return p;
2383}
2384
2385/*
2386 * Like to_name_end() but also skip over a list or dict constant.
2387 */
2388 char_u *
2389to_name_const_end(char_u *arg)
2390{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002391 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 typval_T rettv;
2393
2394 if (p == arg && *arg == '[')
2395 {
2396
2397 // Can be "[1, 2, 3]->Func()".
2398 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2399 p = arg;
2400 }
2401 else if (p == arg && *arg == '#' && arg[1] == '{')
2402 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002403 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 ++p;
2405 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2406 p = arg;
2407 }
2408 else if (p == arg && *arg == '{')
2409 {
2410 int ret = get_lambda_tv(&p, &rettv, FALSE);
2411
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002412 // Can be "{x -> ret}()".
2413 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 if (ret == NOTDONE)
2415 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2416 if (ret != OK)
2417 p = arg;
2418 }
2419
2420 return p;
2421}
2422
2423 static void
2424type_mismatch(type_T *expected, type_T *actual)
2425{
2426 char *tofree1, *tofree2;
2427
2428 semsg(_("E1013: type mismatch, expected %s but got %s"),
2429 type_name(expected, &tofree1), type_name(actual, &tofree2));
2430 vim_free(tofree1);
2431 vim_free(tofree2);
2432}
2433
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002434 static void
2435arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
2436{
2437 char *tofree1, *tofree2;
2438
2439 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
2440 argidx,
2441 type_name(expected, &tofree1), type_name(actual, &tofree2));
2442 vim_free(tofree1);
2443 vim_free(tofree2);
2444}
2445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446/*
2447 * Check if the expected and actual types match.
2448 */
2449 static int
2450check_type(type_T *expected, type_T *actual, int give_msg)
2451{
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002452 int ret = OK;
2453
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002454 // When expected is "unknown" we accept any actual type.
2455 // When expected is "any" we accept any actual type except "void".
2456 if (expected->tt_type != VAR_UNKNOWN
2457 && (expected->tt_type != VAR_ANY || actual->tt_type == VAR_VOID))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 {
2459 if (expected->tt_type != actual->tt_type)
2460 {
2461 if (give_msg)
2462 type_mismatch(expected, actual);
2463 return FAIL;
2464 }
2465 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2466 {
Bram Moolenaar4c683752020-04-05 21:38:23 +02002467 // "unknown" is used for an empty list or dict
2468 if (actual->tt_member != &t_unknown)
Bram Moolenaar436472f2020-02-20 22:54:43 +01002469 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002471 else if (expected->tt_type == VAR_FUNC)
2472 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002473 if (expected->tt_member != &t_unknown)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002474 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
2475 if (ret == OK && expected->tt_argcount != -1
2476 && (actual->tt_argcount < expected->tt_min_argcount
2477 || actual->tt_argcount > expected->tt_argcount))
2478 ret = FAIL;
2479 }
2480 if (ret == FAIL && give_msg)
2481 type_mismatch(expected, actual);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 }
Bram Moolenaar89228602020-04-05 22:14:54 +02002483 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484}
2485
2486/*
2487 * Check that
2488 * - "actual" is "expected" type or
2489 * - "actual" is a type that can be "expected" type: add a runtime check; or
2490 * - return FAIL.
2491 */
2492 static int
2493need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2494{
Bram Moolenaar89228602020-04-05 22:14:54 +02002495 if (check_type(expected, actual, FALSE) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 return OK;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002497 if (actual->tt_type != VAR_ANY && actual->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 {
2499 type_mismatch(expected, actual);
2500 return FAIL;
2501 }
2502 generate_TYPECHECK(cctx, expected, offset);
2503 return OK;
2504}
2505
2506/*
2507 * parse a list: [expr, expr]
2508 * "*arg" points to the '['.
2509 */
2510 static int
2511compile_list(char_u **arg, cctx_T *cctx)
2512{
2513 char_u *p = skipwhite(*arg + 1);
2514 int count = 0;
2515
2516 while (*p != ']')
2517 {
2518 if (*p == NUL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002519 {
2520 semsg(_(e_list_end), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 return FAIL;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 if (compile_expr1(&p, cctx) == FAIL)
2524 break;
2525 ++count;
2526 if (*p == ',')
2527 ++p;
2528 p = skipwhite(p);
2529 }
2530 *arg = p + 1;
2531
2532 generate_NEWLIST(cctx, count);
2533 return OK;
2534}
2535
2536/*
2537 * parse a lambda: {arg, arg -> expr}
2538 * "*arg" points to the '{'.
2539 */
2540 static int
2541compile_lambda(char_u **arg, cctx_T *cctx)
2542{
2543 garray_T *instr = &cctx->ctx_instr;
2544 typval_T rettv;
2545 ufunc_T *ufunc;
2546
2547 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002548 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002552 ++ufunc->uf_refcount;
2553 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002554 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555
2556 // The function will have one line: "return {expr}".
2557 // Compile it into instructions.
2558 compile_def_function(ufunc, TRUE);
2559
2560 if (ufunc->uf_dfunc_idx >= 0)
2561 {
2562 if (ga_grow(instr, 1) == FAIL)
2563 return FAIL;
2564 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2565 return OK;
2566 }
2567 return FAIL;
2568}
2569
2570/*
2571 * Compile a lamda call: expr->{lambda}(args)
2572 * "arg" points to the "{".
2573 */
2574 static int
2575compile_lambda_call(char_u **arg, cctx_T *cctx)
2576{
2577 ufunc_T *ufunc;
2578 typval_T rettv;
2579 int argcount = 1;
2580 int ret = FAIL;
2581
2582 // Get the funcref in "rettv".
2583 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2584 return FAIL;
2585
2586 if (**arg != '(')
2587 {
2588 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002589 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 else
2591 semsg(_(e_missing_paren), "lambda");
2592 clear_tv(&rettv);
2593 return FAIL;
2594 }
2595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 ufunc = rettv.vval.v_partial->pt_func;
2597 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002598 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002599 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002600
2601 // The function will have one line: "return {expr}".
2602 // Compile it into instructions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 compile_def_function(ufunc, TRUE);
2604
2605 // compile the arguments
2606 *arg = skipwhite(*arg + 1);
2607 if (compile_arguments(arg, cctx, &argcount) == OK)
2608 // call the compiled function
2609 ret = generate_CALL(cctx, ufunc, argcount);
2610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 return ret;
2612}
2613
2614/*
2615 * parse a dict: {'key': val} or #{key: val}
2616 * "*arg" points to the '{'.
2617 */
2618 static int
2619compile_dict(char_u **arg, cctx_T *cctx, int literal)
2620{
2621 garray_T *instr = &cctx->ctx_instr;
2622 int count = 0;
2623 dict_T *d = dict_alloc();
2624 dictitem_T *item;
2625
2626 if (d == NULL)
2627 return FAIL;
2628 *arg = skipwhite(*arg + 1);
2629 while (**arg != '}' && **arg != NUL)
2630 {
2631 char_u *key = NULL;
2632
2633 if (literal)
2634 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002635 char_u *p = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636
2637 if (p == *arg)
2638 {
2639 semsg(_("E1014: Invalid key: %s"), *arg);
2640 return FAIL;
2641 }
2642 key = vim_strnsave(*arg, p - *arg);
2643 if (generate_PUSHS(cctx, key) == FAIL)
2644 return FAIL;
2645 *arg = p;
2646 }
2647 else
2648 {
2649 isn_T *isn;
2650
2651 if (compile_expr1(arg, cctx) == FAIL)
2652 return FAIL;
2653 // TODO: check type is string
2654 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2655 if (isn->isn_type == ISN_PUSHS)
2656 key = isn->isn_arg.string;
2657 }
2658
2659 // Check for duplicate keys, if using string keys.
2660 if (key != NULL)
2661 {
2662 item = dict_find(d, key, -1);
2663 if (item != NULL)
2664 {
2665 semsg(_(e_duplicate_key), key);
2666 goto failret;
2667 }
2668 item = dictitem_alloc(key);
2669 if (item != NULL)
2670 {
2671 item->di_tv.v_type = VAR_UNKNOWN;
2672 item->di_tv.v_lock = 0;
2673 if (dict_add(d, item) == FAIL)
2674 dictitem_free(item);
2675 }
2676 }
2677
2678 *arg = skipwhite(*arg);
2679 if (**arg != ':')
2680 {
2681 semsg(_(e_missing_dict_colon), *arg);
2682 return FAIL;
2683 }
2684
2685 *arg = skipwhite(*arg + 1);
2686 if (compile_expr1(arg, cctx) == FAIL)
2687 return FAIL;
2688 ++count;
2689
2690 if (**arg == '}')
2691 break;
2692 if (**arg != ',')
2693 {
2694 semsg(_(e_missing_dict_comma), *arg);
2695 goto failret;
2696 }
2697 *arg = skipwhite(*arg + 1);
2698 }
2699
2700 if (**arg != '}')
2701 {
2702 semsg(_(e_missing_dict_end), *arg);
2703 goto failret;
2704 }
2705 *arg = *arg + 1;
2706
2707 dict_unref(d);
2708 return generate_NEWDICT(cctx, count);
2709
2710failret:
2711 dict_unref(d);
2712 return FAIL;
2713}
2714
2715/*
2716 * Compile "&option".
2717 */
2718 static int
2719compile_get_option(char_u **arg, cctx_T *cctx)
2720{
2721 typval_T rettv;
2722 char_u *start = *arg;
2723 int ret;
2724
2725 // parse the option and get the current value to get the type.
2726 rettv.v_type = VAR_UNKNOWN;
2727 ret = get_option_tv(arg, &rettv, TRUE);
2728 if (ret == OK)
2729 {
2730 // include the '&' in the name, get_option_tv() expects it.
2731 char_u *name = vim_strnsave(start, *arg - start);
2732 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2733
2734 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2735 vim_free(name);
2736 }
2737 clear_tv(&rettv);
2738
2739 return ret;
2740}
2741
2742/*
2743 * Compile "$VAR".
2744 */
2745 static int
2746compile_get_env(char_u **arg, cctx_T *cctx)
2747{
2748 char_u *start = *arg;
2749 int len;
2750 int ret;
2751 char_u *name;
2752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 ++*arg;
2754 len = get_env_len(arg);
2755 if (len == 0)
2756 {
2757 semsg(_(e_syntax_at), start - 1);
2758 return FAIL;
2759 }
2760
2761 // include the '$' in the name, get_env_tv() expects it.
2762 name = vim_strnsave(start, len + 1);
2763 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2764 vim_free(name);
2765 return ret;
2766}
2767
2768/*
2769 * Compile "@r".
2770 */
2771 static int
2772compile_get_register(char_u **arg, cctx_T *cctx)
2773{
2774 int ret;
2775
2776 ++*arg;
2777 if (**arg == NUL)
2778 {
2779 semsg(_(e_syntax_at), *arg - 1);
2780 return FAIL;
2781 }
2782 if (!valid_yank_reg(**arg, TRUE))
2783 {
2784 emsg_invreg(**arg);
2785 return FAIL;
2786 }
2787 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2788 ++*arg;
2789 return ret;
2790}
2791
2792/*
2793 * Apply leading '!', '-' and '+' to constant "rettv".
2794 */
2795 static int
2796apply_leader(typval_T *rettv, char_u *start, char_u *end)
2797{
2798 char_u *p = end;
2799
2800 // this works from end to start
2801 while (p > start)
2802 {
2803 --p;
2804 if (*p == '-' || *p == '+')
2805 {
2806 // only '-' has an effect, for '+' we only check the type
2807#ifdef FEAT_FLOAT
2808 if (rettv->v_type == VAR_FLOAT)
2809 {
2810 if (*p == '-')
2811 rettv->vval.v_float = -rettv->vval.v_float;
2812 }
2813 else
2814#endif
2815 {
2816 varnumber_T val;
2817 int error = FALSE;
2818
2819 // tv_get_number_chk() accepts a string, but we don't want that
2820 // here
2821 if (check_not_string(rettv) == FAIL)
2822 return FAIL;
2823 val = tv_get_number_chk(rettv, &error);
2824 clear_tv(rettv);
2825 if (error)
2826 return FAIL;
2827 if (*p == '-')
2828 val = -val;
2829 rettv->v_type = VAR_NUMBER;
2830 rettv->vval.v_number = val;
2831 }
2832 }
2833 else
2834 {
2835 int v = tv2bool(rettv);
2836
2837 // '!' is permissive in the type.
2838 clear_tv(rettv);
2839 rettv->v_type = VAR_BOOL;
2840 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2841 }
2842 }
2843 return OK;
2844}
2845
2846/*
2847 * Recognize v: variables that are constants and set "rettv".
2848 */
2849 static void
2850get_vim_constant(char_u **arg, typval_T *rettv)
2851{
2852 if (STRNCMP(*arg, "v:true", 6) == 0)
2853 {
2854 rettv->v_type = VAR_BOOL;
2855 rettv->vval.v_number = VVAL_TRUE;
2856 *arg += 6;
2857 }
2858 else if (STRNCMP(*arg, "v:false", 7) == 0)
2859 {
2860 rettv->v_type = VAR_BOOL;
2861 rettv->vval.v_number = VVAL_FALSE;
2862 *arg += 7;
2863 }
2864 else if (STRNCMP(*arg, "v:null", 6) == 0)
2865 {
2866 rettv->v_type = VAR_SPECIAL;
2867 rettv->vval.v_number = VVAL_NULL;
2868 *arg += 6;
2869 }
2870 else if (STRNCMP(*arg, "v:none", 6) == 0)
2871 {
2872 rettv->v_type = VAR_SPECIAL;
2873 rettv->vval.v_number = VVAL_NONE;
2874 *arg += 6;
2875 }
2876}
2877
2878/*
2879 * Compile code to apply '-', '+' and '!'.
2880 */
2881 static int
2882compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2883{
2884 char_u *p = end;
2885
2886 // this works from end to start
2887 while (p > start)
2888 {
2889 --p;
2890 if (*p == '-' || *p == '+')
2891 {
2892 int negate = *p == '-';
2893 isn_T *isn;
2894
2895 // TODO: check type
2896 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2897 {
2898 --p;
2899 if (*p == '-')
2900 negate = !negate;
2901 }
2902 // only '-' has an effect, for '+' we only check the type
2903 if (negate)
2904 isn = generate_instr(cctx, ISN_NEGATENR);
2905 else
2906 isn = generate_instr(cctx, ISN_CHECKNR);
2907 if (isn == NULL)
2908 return FAIL;
2909 }
2910 else
2911 {
2912 int invert = TRUE;
2913
2914 while (p > start && p[-1] == '!')
2915 {
2916 --p;
2917 invert = !invert;
2918 }
2919 if (generate_2BOOL(cctx, invert) == FAIL)
2920 return FAIL;
2921 }
2922 }
2923 return OK;
2924}
2925
2926/*
2927 * Compile whatever comes after "name" or "name()".
2928 */
2929 static int
2930compile_subscript(
2931 char_u **arg,
2932 cctx_T *cctx,
2933 char_u **start_leader,
2934 char_u *end_leader)
2935{
2936 for (;;)
2937 {
2938 if (**arg == '(')
2939 {
2940 int argcount = 0;
2941
2942 // funcref(arg)
2943 *arg = skipwhite(*arg + 1);
2944 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2945 return FAIL;
2946 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2947 return FAIL;
2948 }
2949 else if (**arg == '-' && (*arg)[1] == '>')
2950 {
2951 char_u *p;
2952
2953 // something->method()
2954 // Apply the '!', '-' and '+' first:
2955 // -1.0->func() works like (-1.0)->func()
2956 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2957 return FAIL;
2958 *start_leader = end_leader; // don't apply again later
2959
2960 *arg = skipwhite(*arg + 2);
2961 if (**arg == '{')
2962 {
2963 // lambda call: list->{lambda}
2964 if (compile_lambda_call(arg, cctx) == FAIL)
2965 return FAIL;
2966 }
2967 else
2968 {
2969 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002970 p = *arg;
2971 if (ASCII_ISALPHA(*p) && p[1] == ':')
2972 p += 2;
2973 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 ;
2975 if (*p != '(')
2976 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002977 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 return FAIL;
2979 }
2980 // TODO: base value may not be the first argument
2981 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2982 return FAIL;
2983 }
2984 }
2985 else if (**arg == '[')
2986 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002987 garray_T *stack;
2988 type_T **typep;
2989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 // list index: list[123]
2991 // TODO: more arguments
2992 // TODO: dict member dict['name']
2993 *arg = skipwhite(*arg + 1);
2994 if (compile_expr1(arg, cctx) == FAIL)
2995 return FAIL;
2996
2997 if (**arg != ']')
2998 {
2999 emsg(_(e_missbrac));
3000 return FAIL;
3001 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003002 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003
3004 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3005 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003006 stack = &cctx->ctx_type_stack;
3007 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3008 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3009 {
3010 emsg(_(e_listreq));
3011 return FAIL;
3012 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003013 if ((*typep)->tt_type == VAR_LIST)
3014 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 }
3016 else if (**arg == '.' && (*arg)[1] != '.')
3017 {
3018 char_u *p;
3019
3020 ++*arg;
3021 p = *arg;
3022 // dictionary member: dict.name
3023 if (eval_isnamec1(*p))
3024 while (eval_isnamec(*p))
3025 MB_PTR_ADV(p);
3026 if (p == *arg)
3027 {
3028 semsg(_(e_syntax_at), *arg);
3029 return FAIL;
3030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3032 return FAIL;
3033 *arg = p;
3034 }
3035 else
3036 break;
3037 }
3038
3039 // TODO - see handle_subscript():
3040 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3041 // Don't do this when "Func" is already a partial that was bound
3042 // explicitly (pt_auto is FALSE).
3043
3044 return OK;
3045}
3046
3047/*
3048 * Compile an expression at "*p" and add instructions to "instr".
3049 * "p" is advanced until after the expression, skipping white space.
3050 *
3051 * This is the equivalent of eval1(), eval2(), etc.
3052 */
3053
3054/*
3055 * number number constant
3056 * 0zFFFFFFFF Blob constant
3057 * "string" string constant
3058 * 'string' literal string constant
3059 * &option-name option value
3060 * @r register contents
3061 * identifier variable value
3062 * function() function call
3063 * $VAR environment variable
3064 * (expression) nested expression
3065 * [expr, expr] List
3066 * {key: val, key: val} Dictionary
3067 * #{key: val, key: val} Dictionary with literal keys
3068 *
3069 * Also handle:
3070 * ! in front logical NOT
3071 * - in front unary minus
3072 * + in front unary plus (ignored)
3073 * trailing (arg) funcref/partial call
3074 * trailing [] subscript in String or List
3075 * trailing .name entry in Dictionary
3076 * trailing ->name() method call
3077 */
3078 static int
3079compile_expr7(char_u **arg, cctx_T *cctx)
3080{
3081 typval_T rettv;
3082 char_u *start_leader, *end_leader;
3083 int ret = OK;
3084
3085 /*
3086 * Skip '!', '-' and '+' characters. They are handled later.
3087 */
3088 start_leader = *arg;
3089 while (**arg == '!' || **arg == '-' || **arg == '+')
3090 *arg = skipwhite(*arg + 1);
3091 end_leader = *arg;
3092
3093 rettv.v_type = VAR_UNKNOWN;
3094 switch (**arg)
3095 {
3096 /*
3097 * Number constant.
3098 */
3099 case '0': // also for blob starting with 0z
3100 case '1':
3101 case '2':
3102 case '3':
3103 case '4':
3104 case '5':
3105 case '6':
3106 case '7':
3107 case '8':
3108 case '9':
3109 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3110 return FAIL;
3111 break;
3112
3113 /*
3114 * String constant: "string".
3115 */
3116 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3117 return FAIL;
3118 break;
3119
3120 /*
3121 * Literal string constant: 'str''ing'.
3122 */
3123 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3124 return FAIL;
3125 break;
3126
3127 /*
3128 * Constant Vim variable.
3129 */
3130 case 'v': get_vim_constant(arg, &rettv);
3131 ret = NOTDONE;
3132 break;
3133
3134 /*
3135 * List: [expr, expr]
3136 */
3137 case '[': ret = compile_list(arg, cctx);
3138 break;
3139
3140 /*
3141 * Dictionary: #{key: val, key: val}
3142 */
3143 case '#': if ((*arg)[1] == '{')
3144 {
3145 ++*arg;
3146 ret = compile_dict(arg, cctx, TRUE);
3147 }
3148 else
3149 ret = NOTDONE;
3150 break;
3151
3152 /*
3153 * Lambda: {arg, arg -> expr}
3154 * Dictionary: {'key': val, 'key': val}
3155 */
3156 case '{': {
3157 char_u *start = skipwhite(*arg + 1);
3158
3159 // Find out what comes after the arguments.
3160 ret = get_function_args(&start, '-', NULL,
3161 NULL, NULL, NULL, TRUE);
3162 if (ret != FAIL && *start == '>')
3163 ret = compile_lambda(arg, cctx);
3164 else
3165 ret = compile_dict(arg, cctx, FALSE);
3166 }
3167 break;
3168
3169 /*
3170 * Option value: &name
3171 */
3172 case '&': ret = compile_get_option(arg, cctx);
3173 break;
3174
3175 /*
3176 * Environment variable: $VAR.
3177 */
3178 case '$': ret = compile_get_env(arg, cctx);
3179 break;
3180
3181 /*
3182 * Register contents: @r.
3183 */
3184 case '@': ret = compile_get_register(arg, cctx);
3185 break;
3186 /*
3187 * nested expression: (expression).
3188 */
3189 case '(': *arg = skipwhite(*arg + 1);
3190 ret = compile_expr1(arg, cctx); // recursive!
3191 *arg = skipwhite(*arg);
3192 if (**arg == ')')
3193 ++*arg;
3194 else if (ret == OK)
3195 {
3196 emsg(_(e_missing_close));
3197 ret = FAIL;
3198 }
3199 break;
3200
3201 default: ret = NOTDONE;
3202 break;
3203 }
3204 if (ret == FAIL)
3205 return FAIL;
3206
3207 if (rettv.v_type != VAR_UNKNOWN)
3208 {
3209 // apply the '!', '-' and '+' before the constant
3210 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3211 {
3212 clear_tv(&rettv);
3213 return FAIL;
3214 }
3215 start_leader = end_leader; // don't apply again below
3216
3217 // push constant
3218 switch (rettv.v_type)
3219 {
3220 case VAR_BOOL:
3221 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3222 break;
3223 case VAR_SPECIAL:
3224 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3225 break;
3226 case VAR_NUMBER:
3227 generate_PUSHNR(cctx, rettv.vval.v_number);
3228 break;
3229#ifdef FEAT_FLOAT
3230 case VAR_FLOAT:
3231 generate_PUSHF(cctx, rettv.vval.v_float);
3232 break;
3233#endif
3234 case VAR_BLOB:
3235 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3236 rettv.vval.v_blob = NULL;
3237 break;
3238 case VAR_STRING:
3239 generate_PUSHS(cctx, rettv.vval.v_string);
3240 rettv.vval.v_string = NULL;
3241 break;
3242 default:
3243 iemsg("constant type missing");
3244 return FAIL;
3245 }
3246 }
3247 else if (ret == NOTDONE)
3248 {
3249 char_u *p;
3250 int r;
3251
3252 if (!eval_isnamec1(**arg))
3253 {
3254 semsg(_("E1015: Name expected: %s"), *arg);
3255 return FAIL;
3256 }
3257
3258 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003259 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 if (*p == '(')
3261 r = compile_call(arg, p - *arg, cctx, 0);
3262 else
3263 r = compile_load(arg, p, cctx, TRUE);
3264 if (r == FAIL)
3265 return FAIL;
3266 }
3267
3268 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3269 return FAIL;
3270
3271 // Now deal with prefixed '-', '+' and '!', if not done already.
3272 return compile_leader(cctx, start_leader, end_leader);
3273}
3274
3275/*
3276 * * number multiplication
3277 * / number division
3278 * % number modulo
3279 */
3280 static int
3281compile_expr6(char_u **arg, cctx_T *cctx)
3282{
3283 char_u *op;
3284
3285 // get the first variable
3286 if (compile_expr7(arg, cctx) == FAIL)
3287 return FAIL;
3288
3289 /*
3290 * Repeat computing, until no "*", "/" or "%" is following.
3291 */
3292 for (;;)
3293 {
3294 op = skipwhite(*arg);
3295 if (*op != '*' && *op != '/' && *op != '%')
3296 break;
3297 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
3298 {
3299 char_u buf[3];
3300
3301 vim_strncpy(buf, op, 1);
3302 semsg(_(e_white_both), buf);
3303 }
3304 *arg = skipwhite(op + 1);
3305
3306 // get the second variable
3307 if (compile_expr7(arg, cctx) == FAIL)
3308 return FAIL;
3309
3310 generate_two_op(cctx, op);
3311 }
3312
3313 return OK;
3314}
3315
3316/*
3317 * + number addition
3318 * - number subtraction
3319 * .. string concatenation
3320 */
3321 static int
3322compile_expr5(char_u **arg, cctx_T *cctx)
3323{
3324 char_u *op;
3325 int oplen;
3326
3327 // get the first variable
3328 if (compile_expr6(arg, cctx) == FAIL)
3329 return FAIL;
3330
3331 /*
3332 * Repeat computing, until no "+", "-" or ".." is following.
3333 */
3334 for (;;)
3335 {
3336 op = skipwhite(*arg);
3337 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3338 break;
3339 oplen = (*op == '.' ? 2 : 1);
3340
3341 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
3342 {
3343 char_u buf[3];
3344
3345 vim_strncpy(buf, op, oplen);
3346 semsg(_(e_white_both), buf);
3347 }
3348
3349 *arg = skipwhite(op + oplen);
3350
3351 // get the second variable
3352 if (compile_expr6(arg, cctx) == FAIL)
3353 return FAIL;
3354
3355 if (*op == '.')
3356 {
3357 if (may_generate_2STRING(-2, cctx) == FAIL
3358 || may_generate_2STRING(-1, cctx) == FAIL)
3359 return FAIL;
3360 generate_instr_drop(cctx, ISN_CONCAT, 1);
3361 }
3362 else
3363 generate_two_op(cctx, op);
3364 }
3365
3366 return OK;
3367}
3368
Bram Moolenaar080457c2020-03-03 21:53:32 +01003369 static exptype_T
3370get_compare_type(char_u *p, int *len, int *type_is)
3371{
3372 exptype_T type = EXPR_UNKNOWN;
3373 int i;
3374
3375 switch (p[0])
3376 {
3377 case '=': if (p[1] == '=')
3378 type = EXPR_EQUAL;
3379 else if (p[1] == '~')
3380 type = EXPR_MATCH;
3381 break;
3382 case '!': if (p[1] == '=')
3383 type = EXPR_NEQUAL;
3384 else if (p[1] == '~')
3385 type = EXPR_NOMATCH;
3386 break;
3387 case '>': if (p[1] != '=')
3388 {
3389 type = EXPR_GREATER;
3390 *len = 1;
3391 }
3392 else
3393 type = EXPR_GEQUAL;
3394 break;
3395 case '<': if (p[1] != '=')
3396 {
3397 type = EXPR_SMALLER;
3398 *len = 1;
3399 }
3400 else
3401 type = EXPR_SEQUAL;
3402 break;
3403 case 'i': if (p[1] == 's')
3404 {
3405 // "is" and "isnot"; but not a prefix of a name
3406 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3407 *len = 5;
3408 i = p[*len];
3409 if (!isalnum(i) && i != '_')
3410 {
3411 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3412 *type_is = TRUE;
3413 }
3414 }
3415 break;
3416 }
3417 return type;
3418}
3419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420/*
3421 * expr5a == expr5b
3422 * expr5a =~ expr5b
3423 * expr5a != expr5b
3424 * expr5a !~ expr5b
3425 * expr5a > expr5b
3426 * expr5a >= expr5b
3427 * expr5a < expr5b
3428 * expr5a <= expr5b
3429 * expr5a is expr5b
3430 * expr5a isnot expr5b
3431 *
3432 * Produces instructions:
3433 * EVAL expr5a Push result of "expr5a"
3434 * EVAL expr5b Push result of "expr5b"
3435 * COMPARE one of the compare instructions
3436 */
3437 static int
3438compile_expr4(char_u **arg, cctx_T *cctx)
3439{
3440 exptype_T type = EXPR_UNKNOWN;
3441 char_u *p;
3442 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 int type_is = FALSE;
3444
3445 // get the first variable
3446 if (compile_expr5(arg, cctx) == FAIL)
3447 return FAIL;
3448
3449 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003450 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451
3452 /*
3453 * If there is a comparative operator, use it.
3454 */
3455 if (type != EXPR_UNKNOWN)
3456 {
3457 int ic = FALSE; // Default: do not ignore case
3458
3459 if (type_is && (p[len] == '?' || p[len] == '#'))
3460 {
3461 semsg(_(e_invexpr2), *arg);
3462 return FAIL;
3463 }
3464 // extra question mark appended: ignore case
3465 if (p[len] == '?')
3466 {
3467 ic = TRUE;
3468 ++len;
3469 }
3470 // extra '#' appended: match case (ignored)
3471 else if (p[len] == '#')
3472 ++len;
3473 // nothing appended: match case
3474
3475 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
3476 {
3477 char_u buf[7];
3478
3479 vim_strncpy(buf, p, len);
3480 semsg(_(e_white_both), buf);
3481 }
3482
3483 // get the second variable
3484 *arg = skipwhite(p + len);
3485 if (compile_expr5(arg, cctx) == FAIL)
3486 return FAIL;
3487
3488 generate_COMPARE(cctx, type, ic);
3489 }
3490
3491 return OK;
3492}
3493
3494/*
3495 * Compile || or &&.
3496 */
3497 static int
3498compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3499{
3500 char_u *p = skipwhite(*arg);
3501 int opchar = *op;
3502
3503 if (p[0] == opchar && p[1] == opchar)
3504 {
3505 garray_T *instr = &cctx->ctx_instr;
3506 garray_T end_ga;
3507
3508 /*
3509 * Repeat until there is no following "||" or "&&"
3510 */
3511 ga_init2(&end_ga, sizeof(int), 10);
3512 while (p[0] == opchar && p[1] == opchar)
3513 {
3514 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3515 semsg(_(e_white_both), op);
3516
3517 if (ga_grow(&end_ga, 1) == FAIL)
3518 {
3519 ga_clear(&end_ga);
3520 return FAIL;
3521 }
3522 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3523 ++end_ga.ga_len;
3524 generate_JUMP(cctx, opchar == '|'
3525 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3526
3527 // eval the next expression
3528 *arg = skipwhite(p + 2);
3529 if ((opchar == '|' ? compile_expr3(arg, cctx)
3530 : compile_expr4(arg, cctx)) == FAIL)
3531 {
3532 ga_clear(&end_ga);
3533 return FAIL;
3534 }
3535 p = skipwhite(*arg);
3536 }
3537
3538 // Fill in the end label in all jumps.
3539 while (end_ga.ga_len > 0)
3540 {
3541 isn_T *isn;
3542
3543 --end_ga.ga_len;
3544 isn = ((isn_T *)instr->ga_data)
3545 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3546 isn->isn_arg.jump.jump_where = instr->ga_len;
3547 }
3548 ga_clear(&end_ga);
3549 }
3550
3551 return OK;
3552}
3553
3554/*
3555 * expr4a && expr4a && expr4a logical AND
3556 *
3557 * Produces instructions:
3558 * EVAL expr4a Push result of "expr4a"
3559 * JUMP_AND_KEEP_IF_FALSE end
3560 * EVAL expr4b Push result of "expr4b"
3561 * JUMP_AND_KEEP_IF_FALSE end
3562 * EVAL expr4c Push result of "expr4c"
3563 * end:
3564 */
3565 static int
3566compile_expr3(char_u **arg, cctx_T *cctx)
3567{
3568 // get the first variable
3569 if (compile_expr4(arg, cctx) == FAIL)
3570 return FAIL;
3571
3572 // || and && work almost the same
3573 return compile_and_or(arg, cctx, "&&");
3574}
3575
3576/*
3577 * expr3a || expr3b || expr3c logical OR
3578 *
3579 * Produces instructions:
3580 * EVAL expr3a Push result of "expr3a"
3581 * JUMP_AND_KEEP_IF_TRUE end
3582 * EVAL expr3b Push result of "expr3b"
3583 * JUMP_AND_KEEP_IF_TRUE end
3584 * EVAL expr3c Push result of "expr3c"
3585 * end:
3586 */
3587 static int
3588compile_expr2(char_u **arg, cctx_T *cctx)
3589{
3590 // eval the first expression
3591 if (compile_expr3(arg, cctx) == FAIL)
3592 return FAIL;
3593
3594 // || and && work almost the same
3595 return compile_and_or(arg, cctx, "||");
3596}
3597
3598/*
3599 * Toplevel expression: expr2 ? expr1a : expr1b
3600 *
3601 * Produces instructions:
3602 * EVAL expr2 Push result of "expr"
3603 * JUMP_IF_FALSE alt jump if false
3604 * EVAL expr1a
3605 * JUMP_ALWAYS end
3606 * alt: EVAL expr1b
3607 * end:
3608 */
3609 static int
3610compile_expr1(char_u **arg, cctx_T *cctx)
3611{
3612 char_u *p;
3613
3614 // evaluate the first expression
3615 if (compile_expr2(arg, cctx) == FAIL)
3616 return FAIL;
3617
3618 p = skipwhite(*arg);
3619 if (*p == '?')
3620 {
3621 garray_T *instr = &cctx->ctx_instr;
3622 garray_T *stack = &cctx->ctx_type_stack;
3623 int alt_idx = instr->ga_len;
3624 int end_idx;
3625 isn_T *isn;
3626 type_T *type1;
3627 type_T *type2;
3628
3629 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3630 semsg(_(e_white_both), "?");
3631
3632 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3633
3634 // evaluate the second expression; any type is accepted
3635 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003636 if (compile_expr1(arg, cctx) == FAIL)
3637 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638
3639 // remember the type and drop it
3640 --stack->ga_len;
3641 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3642
3643 end_idx = instr->ga_len;
3644 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3645
3646 // jump here from JUMP_IF_FALSE
3647 isn = ((isn_T *)instr->ga_data) + alt_idx;
3648 isn->isn_arg.jump.jump_where = instr->ga_len;
3649
3650 // Check for the ":".
3651 p = skipwhite(*arg);
3652 if (*p != ':')
3653 {
3654 emsg(_(e_missing_colon));
3655 return FAIL;
3656 }
3657 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3658 semsg(_(e_white_both), ":");
3659
3660 // evaluate the third expression
3661 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003662 if (compile_expr1(arg, cctx) == FAIL)
3663 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664
3665 // If the types differ, the result has a more generic type.
3666 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003667 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668
3669 // jump here from JUMP_ALWAYS
3670 isn = ((isn_T *)instr->ga_data) + end_idx;
3671 isn->isn_arg.jump.jump_where = instr->ga_len;
3672 }
3673 return OK;
3674}
3675
3676/*
3677 * compile "return [expr]"
3678 */
3679 static char_u *
3680compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3681{
3682 char_u *p = arg;
3683 garray_T *stack = &cctx->ctx_type_stack;
3684 type_T *stack_type;
3685
3686 if (*p != NUL && *p != '|' && *p != '\n')
3687 {
3688 // compile return argument into instructions
3689 if (compile_expr1(&p, cctx) == FAIL)
3690 return NULL;
3691
3692 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3693 if (set_return_type)
3694 cctx->ctx_ufunc->uf_ret_type = stack_type;
3695 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3696 == FAIL)
3697 return NULL;
3698 }
3699 else
3700 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003701 // "set_return_type" cannot be TRUE, only used for a lambda which
3702 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02003703 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
3704 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 {
3706 emsg(_("E1003: Missing return value"));
3707 return NULL;
3708 }
3709
3710 // No argument, return zero.
3711 generate_PUSHNR(cctx, 0);
3712 }
3713
3714 if (generate_instr(cctx, ISN_RETURN) == NULL)
3715 return NULL;
3716
3717 // "return val | endif" is possible
3718 return skipwhite(p);
3719}
3720
3721/*
3722 * Return the length of an assignment operator, or zero if there isn't one.
3723 */
3724 int
3725assignment_len(char_u *p, int *heredoc)
3726{
3727 if (*p == '=')
3728 {
3729 if (p[1] == '<' && p[2] == '<')
3730 {
3731 *heredoc = TRUE;
3732 return 3;
3733 }
3734 return 1;
3735 }
3736 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3737 return 2;
3738 if (STRNCMP(p, "..=", 3) == 0)
3739 return 3;
3740 return 0;
3741}
3742
3743// words that cannot be used as a variable
3744static char *reserved[] = {
3745 "true",
3746 "false",
3747 NULL
3748};
3749
3750/*
3751 * Get a line for "=<<".
3752 * Return a pointer to the line in allocated memory.
3753 * Return NULL for end-of-file or some error.
3754 */
3755 static char_u *
3756heredoc_getline(
3757 int c UNUSED,
3758 void *cookie,
3759 int indent UNUSED,
3760 int do_concat UNUSED)
3761{
3762 cctx_T *cctx = (cctx_T *)cookie;
3763
3764 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003765 {
3766 iemsg("Heredoc got to end");
3767 return NULL;
3768 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 ++cctx->ctx_lnum;
3770 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3771 [cctx->ctx_lnum]);
3772}
3773
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003774typedef enum {
3775 dest_local,
3776 dest_option,
3777 dest_env,
3778 dest_global,
3779 dest_vimvar,
3780 dest_script,
3781 dest_reg,
3782} assign_dest_T;
3783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784/*
3785 * compile "let var [= expr]", "const var = expr" and "var = expr"
3786 * "arg" points to "var".
3787 */
3788 static char_u *
3789compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3790{
3791 char_u *p;
3792 char_u *ret = NULL;
3793 int var_count = 0;
3794 int semicolon = 0;
3795 size_t varlen;
3796 garray_T *instr = &cctx->ctx_instr;
3797 int idx = -1;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003798 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003801 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003803 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 int oplen = 0;
3805 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003806 type_T *type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 lvar_T *lvar;
3808 char_u *name;
3809 char_u *sp;
3810 int has_type = FALSE;
3811 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3812 int instr_count = -1;
3813
3814 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3815 if (p == NULL)
3816 return NULL;
3817 if (var_count > 0)
3818 {
3819 // TODO: let [var, var] = list
3820 emsg("Cannot handle a list yet");
3821 return NULL;
3822 }
3823
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003824 // "a: type" is declaring variable "a" with a type, not "a:".
3825 if (is_decl && p == arg + 2 && p[-1] == ':')
3826 --p;
3827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 varlen = p - arg;
3829 name = vim_strnsave(arg, (int)varlen);
3830 if (name == NULL)
3831 return NULL;
3832
Bram Moolenaar080457c2020-03-03 21:53:32 +01003833 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003835 if (*arg == '&')
3836 {
3837 int cc;
3838 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839
Bram Moolenaar080457c2020-03-03 21:53:32 +01003840 dest = dest_option;
3841 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003843 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003844 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 if (is_decl)
3847 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003848 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 goto theend;
3850 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003851 p = arg;
3852 p = find_option_end(&p, &opt_flags);
3853 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003855 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01003856 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003857 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003858 }
3859 cc = *p;
3860 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003861 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003862 *p = cc;
3863 if (opt_type == -3)
3864 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003865 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003866 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003867 }
3868 if (opt_type == -2 || opt_type == 0)
3869 type = &t_string;
3870 else
3871 type = &t_number; // both number and boolean option
3872 }
3873 else if (*arg == '$')
3874 {
3875 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003876 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003877 if (is_decl)
3878 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003879 semsg(_("E1065: Cannot declare an environment variable: %s"),
3880 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003881 goto theend;
3882 }
3883 }
3884 else if (*arg == '@')
3885 {
3886 if (!valid_yank_reg(arg[1], TRUE))
3887 {
3888 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003889 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003890 }
3891 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003892 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003893 if (is_decl)
3894 {
3895 semsg(_("E1066: Cannot declare a register: %s"), name);
3896 goto theend;
3897 }
3898 }
3899 else if (STRNCMP(arg, "g:", 2) == 0)
3900 {
3901 dest = dest_global;
3902 if (is_decl)
3903 {
3904 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3905 goto theend;
3906 }
3907 }
3908 else if (STRNCMP(arg, "v:", 2) == 0)
3909 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02003910 typval_T *vtv;
3911
Bram Moolenaar080457c2020-03-03 21:53:32 +01003912 vimvaridx = find_vim_var(name + 2);
3913 if (vimvaridx < 0)
3914 {
3915 semsg(_(e_var_notfound), arg);
3916 goto theend;
3917 }
3918 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003919 vtv = get_vim_var_tv(vimvaridx);
3920 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003921 if (is_decl)
3922 {
3923 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3924 goto theend;
3925 }
3926 }
3927 else
3928 {
3929 for (idx = 0; reserved[idx] != NULL; ++idx)
3930 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003932 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 goto theend;
3934 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003935
3936 idx = lookup_local(arg, varlen, cctx);
3937 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003939 if (is_decl)
3940 {
3941 semsg(_("E1017: Variable already declared: %s"), name);
3942 goto theend;
3943 }
3944 else
3945 {
3946 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3947 if (lvar->lv_const)
3948 {
3949 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3950 goto theend;
3951 }
3952 }
3953 }
3954 else if (STRNCMP(arg, "s:", 2) == 0
3955 || lookup_script(arg, varlen) == OK
3956 || find_imported(arg, varlen, cctx) != NULL)
3957 {
3958 dest = dest_script;
3959 if (is_decl)
3960 {
3961 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003962 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003963 goto theend;
3964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 }
3966 }
3967 }
3968
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003969 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 {
3971 if (is_decl && *p == ':')
3972 {
3973 // parse optional type: "let var: type = expr"
3974 p = skipwhite(p + 1);
3975 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 has_type = TRUE;
3977 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02003978 else if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 {
3980 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3981 type = lvar->lv_type;
3982 }
3983 }
3984
3985 sp = p;
3986 p = skipwhite(p);
3987 op = p;
3988 oplen = assignment_len(p, &heredoc);
3989 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3990 {
3991 char_u buf[4];
3992
3993 vim_strncpy(buf, op, oplen);
3994 semsg(_(e_white_both), buf);
3995 }
3996
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003997 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02003998 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004000 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 goto theend;
4002 }
4003
Bram Moolenaar080457c2020-03-03 21:53:32 +01004004 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 {
4006 if (oplen > 1 && !heredoc)
4007 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004008 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4010 name);
4011 goto theend;
4012 }
4013
4014 // new local variable
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004015 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4016 && var_check_func_name(name, TRUE))
4017 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4019 if (idx < 0)
4020 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004021 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 }
4023
4024 if (heredoc)
4025 {
4026 list_T *l;
4027 listitem_T *li;
4028
4029 // [let] varname =<< [trim] {end}
4030 eap->getline = heredoc_getline;
4031 eap->cookie = cctx;
4032 l = heredoc_get(eap, op + 3);
4033
4034 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004035 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 {
4037 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4038 li->li_tv.vval.v_string = NULL;
4039 }
4040 generate_NEWLIST(cctx, l->lv_len);
4041 type = &t_list_string;
4042 list_free(l);
4043 p += STRLEN(p);
4044 }
4045 else if (oplen > 0)
4046 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004047 int r;
4048 type_T *stacktype;
4049 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 // for "+=", "*=", "..=" etc. first load the current value
4052 if (*op != '=')
4053 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004054 switch (dest)
4055 {
4056 case dest_option:
4057 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004058 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004059 break;
4060 case dest_global:
4061 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4062 break;
4063 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004064 compile_load_scriptvar(cctx,
4065 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004066 break;
4067 case dest_env:
4068 // Include $ in the name here
4069 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4070 break;
4071 case dest_reg:
4072 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4073 break;
4074 case dest_vimvar:
4075 generate_LOADV(cctx, name + 2, TRUE);
4076 break;
4077 case dest_local:
4078 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
4079 break;
4080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 }
4082
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004083 // Compile the expression. Temporarily hide the new local variable
4084 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004085 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004086 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 instr_count = instr->ga_len;
4088 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004089 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004090 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004091 ++cctx->ctx_locals.ga_len;
4092 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 goto theend;
4094
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004095 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004097 stack = &cctx->ctx_type_stack;
4098 stacktype = stack->ga_len == 0 ? &t_void
4099 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
4100 if (idx >= 0 && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004102 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
4103 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004105 if (stacktype->tt_type == VAR_VOID)
4106 {
4107 emsg(_("E1031: Cannot use void value"));
4108 goto theend;
4109 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004110 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004111 {
4112 // An empty list or dict has a &t_void member, for a
4113 // variable that implies &t_any.
4114 if (stacktype == &t_list_empty)
4115 lvar->lv_type = &t_list_any;
4116 else if (stacktype == &t_dict_empty)
4117 lvar->lv_type = &t_dict_any;
4118 else
4119 lvar->lv_type = stacktype;
4120 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004121 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004122 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4123 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004125 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004126 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 }
4128 }
4129 else if (cmdidx == CMD_const)
4130 {
4131 emsg(_("E1021: const requires a value"));
4132 goto theend;
4133 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004134 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 {
4136 emsg(_("E1022: type or initialization required"));
4137 goto theend;
4138 }
4139 else
4140 {
4141 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 if (ga_grow(instr, 1) == FAIL)
4143 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004144 switch (type->tt_type)
4145 {
4146 case VAR_BOOL:
4147 generate_PUSHBOOL(cctx, VVAL_FALSE);
4148 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004149 case VAR_FLOAT:
4150#ifdef FEAT_FLOAT
4151 generate_PUSHF(cctx, 0.0);
4152#endif
4153 break;
4154 case VAR_STRING:
4155 generate_PUSHS(cctx, NULL);
4156 break;
4157 case VAR_BLOB:
4158 generate_PUSHBLOB(cctx, NULL);
4159 break;
4160 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004161 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004162 break;
4163 case VAR_PARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004164 generate_PUSHPARTIAL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004165 break;
4166 case VAR_LIST:
4167 generate_NEWLIST(cctx, 0);
4168 break;
4169 case VAR_DICT:
4170 generate_NEWDICT(cctx, 0);
4171 break;
4172 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004173 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004174 break;
4175 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004176 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004177 break;
4178 case VAR_NUMBER:
4179 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004180 case VAR_ANY:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004181 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004182 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004183 generate_PUSHNR(cctx, 0);
4184 break;
4185 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 }
4187
4188 if (oplen > 0 && *op != '=')
4189 {
4190 type_T *expected = &t_number;
4191 garray_T *stack = &cctx->ctx_type_stack;
4192 type_T *stacktype;
4193
4194 // TODO: if type is known use float or any operation
4195
4196 if (*op == '.')
4197 expected = &t_string;
4198 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4199 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4200 goto theend;
4201
4202 if (*op == '.')
4203 generate_instr_drop(cctx, ISN_CONCAT, 1);
4204 else
4205 {
4206 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4207
4208 if (isn == NULL)
4209 goto theend;
4210 switch (*op)
4211 {
4212 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4213 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4214 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4215 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4216 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4217 }
4218 }
4219 }
4220
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004221 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004223 case dest_option:
4224 generate_STOREOPT(cctx, name + 1, opt_flags);
4225 break;
4226 case dest_global:
4227 // include g: with the name, easier to execute that way
4228 generate_STORE(cctx, ISN_STOREG, 0, name);
4229 break;
4230 case dest_env:
4231 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4232 break;
4233 case dest_reg:
4234 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4235 break;
4236 case dest_vimvar:
4237 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4238 break;
4239 case dest_script:
4240 {
4241 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4242 imported_T *import = NULL;
4243 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004244
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004245 if (name[1] != ':')
4246 {
4247 import = find_imported(name, 0, cctx);
4248 if (import != NULL)
4249 sid = import->imp_sid;
4250 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004252 idx = get_script_item_idx(sid, rawname, TRUE);
4253 // TODO: specific type
4254 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004255 {
4256 char_u *name_s = name;
4257
4258 // Include s: in the name for store_var()
4259 if (name[1] != ':')
4260 {
4261 int len = (int)STRLEN(name) + 3;
4262
4263 name_s = alloc(len);
4264 if (name_s == NULL)
4265 name_s = name;
4266 else
4267 vim_snprintf((char *)name_s, len, "s:%s", name);
4268 }
4269 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4270 if (name_s != name)
4271 vim_free(name_s);
4272 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004273 else
4274 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4275 sid, idx, &t_any);
4276 }
4277 break;
4278 case dest_local:
4279 {
4280 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004282 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4283 // into ISN_STORENR
4284 if (instr->ga_len == instr_count + 1
4285 && isn->isn_type == ISN_PUSHNR)
4286 {
4287 varnumber_T val = isn->isn_arg.number;
4288 garray_T *stack = &cctx->ctx_type_stack;
4289
4290 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004291 isn->isn_arg.storenr.stnr_idx = idx;
4292 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004293 if (stack->ga_len > 0)
4294 --stack->ga_len;
4295 }
4296 else
4297 generate_STORE(cctx, ISN_STORE, idx, NULL);
4298 }
4299 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 }
4301 ret = p;
4302
4303theend:
4304 vim_free(name);
4305 return ret;
4306}
4307
4308/*
4309 * Compile an :import command.
4310 */
4311 static char_u *
4312compile_import(char_u *arg, cctx_T *cctx)
4313{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004314 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315}
4316
4317/*
4318 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4319 */
4320 static int
4321compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4322{
4323 garray_T *instr = &cctx->ctx_instr;
4324 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4325
4326 if (endlabel == NULL)
4327 return FAIL;
4328 endlabel->el_next = *el;
4329 *el = endlabel;
4330 endlabel->el_end_label = instr->ga_len;
4331
4332 generate_JUMP(cctx, when, 0);
4333 return OK;
4334}
4335
4336 static void
4337compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4338{
4339 garray_T *instr = &cctx->ctx_instr;
4340
4341 while (*el != NULL)
4342 {
4343 endlabel_T *cur = (*el);
4344 isn_T *isn;
4345
4346 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4347 isn->isn_arg.jump.jump_where = instr->ga_len;
4348 *el = cur->el_next;
4349 vim_free(cur);
4350 }
4351}
4352
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004353 static void
4354compile_free_jump_to_end(endlabel_T **el)
4355{
4356 while (*el != NULL)
4357 {
4358 endlabel_T *cur = (*el);
4359
4360 *el = cur->el_next;
4361 vim_free(cur);
4362 }
4363}
4364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365/*
4366 * Create a new scope and set up the generic items.
4367 */
4368 static scope_T *
4369new_scope(cctx_T *cctx, scopetype_T type)
4370{
4371 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4372
4373 if (scope == NULL)
4374 return NULL;
4375 scope->se_outer = cctx->ctx_scope;
4376 cctx->ctx_scope = scope;
4377 scope->se_type = type;
4378 scope->se_local_count = cctx->ctx_locals.ga_len;
4379 return scope;
4380}
4381
4382/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004383 * Free the current scope and go back to the outer scope.
4384 */
4385 static void
4386drop_scope(cctx_T *cctx)
4387{
4388 scope_T *scope = cctx->ctx_scope;
4389
4390 if (scope == NULL)
4391 {
4392 iemsg("calling drop_scope() without a scope");
4393 return;
4394 }
4395 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004396 switch (scope->se_type)
4397 {
4398 case IF_SCOPE:
4399 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4400 case FOR_SCOPE:
4401 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4402 case WHILE_SCOPE:
4403 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4404 case TRY_SCOPE:
4405 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4406 case NO_SCOPE:
4407 case BLOCK_SCOPE:
4408 break;
4409 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004410 vim_free(scope);
4411}
4412
4413/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004414 * Evaluate an expression that is a constant:
4415 * has(arg)
4416 *
4417 * Also handle:
4418 * ! in front logical NOT
4419 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004420 * Return FAIL if the expression is not a constant.
4421 */
4422 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004423evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004424{
4425 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004426 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004427 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004428
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004429 /*
4430 * Skip '!' characters. They are handled later.
4431 */
4432 start_leader = *arg;
4433 while (**arg == '!')
4434 *arg = skipwhite(*arg + 1);
4435 end_leader = *arg;
4436
4437 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004438 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004439 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004440 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4441 {
4442 tv->v_type = VAR_SPECIAL;
4443 tv->vval.v_number = VVAL_TRUE;
4444 *arg += 4;
4445 return OK;
4446 }
4447 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4448 {
4449 tv->v_type = VAR_SPECIAL;
4450 tv->vval.v_number = VVAL_FALSE;
4451 *arg += 5;
4452 return OK;
4453 }
4454
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004455 if (STRNCMP("has(", *arg, 4) == 0)
4456 {
4457 has_call = TRUE;
4458 *arg = skipwhite(*arg + 4);
4459 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004460
4461 if (**arg == '"')
4462 {
4463 if (get_string_tv(arg, tv, TRUE) == FAIL)
4464 return FAIL;
4465 }
4466 else if (**arg == '\'')
4467 {
4468 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4469 return FAIL;
4470 }
4471 else
4472 return FAIL;
4473
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004474 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004475 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004476 *arg = skipwhite(*arg);
4477 if (**arg != ')')
4478 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004479 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004480
4481 argvars[0] = *tv;
4482 argvars[1].v_type = VAR_UNKNOWN;
4483 tv->v_type = VAR_NUMBER;
4484 tv->vval.v_number = 0;
4485 f_has(argvars, tv);
4486 clear_tv(&argvars[0]);
4487
4488 while (start_leader < end_leader)
4489 {
4490 if (*start_leader == '!')
4491 tv->vval.v_number = !tv->vval.v_number;
4492 ++start_leader;
4493 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004494 }
4495
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004496 return OK;
4497}
4498
Bram Moolenaar080457c2020-03-03 21:53:32 +01004499 static int
4500evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4501{
4502 exptype_T type = EXPR_UNKNOWN;
4503 char_u *p;
4504 int len = 2;
4505 int type_is = FALSE;
4506
4507 // get the first variable
4508 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4509 return FAIL;
4510
4511 p = skipwhite(*arg);
4512 type = get_compare_type(p, &len, &type_is);
4513
4514 /*
4515 * If there is a comparative operator, use it.
4516 */
4517 if (type != EXPR_UNKNOWN)
4518 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004519 typval_T tv2;
4520 char_u *s1, *s2;
4521 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4522 int n;
4523
4524 // TODO: Only string == string is supported now
4525 if (tv->v_type != VAR_STRING)
4526 return FAIL;
4527 if (type != EXPR_EQUAL)
4528 return FAIL;
4529
4530 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004531 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004532 *arg = skipwhite(p + len);
4533 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4534 || tv2.v_type != VAR_STRING)
4535 {
4536 clear_tv(&tv2);
4537 return FAIL;
4538 }
4539 s1 = tv_get_string_buf(tv, buf1);
4540 s2 = tv_get_string_buf(&tv2, buf2);
4541 n = STRCMP(s1, s2);
4542 clear_tv(tv);
4543 clear_tv(&tv2);
4544 tv->v_type = VAR_BOOL;
4545 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004546 }
4547
4548 return OK;
4549}
4550
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004551static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4552
4553/*
4554 * Compile constant || or &&.
4555 */
4556 static int
4557evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4558{
4559 char_u *p = skipwhite(*arg);
4560 int opchar = *op;
4561
4562 if (p[0] == opchar && p[1] == opchar)
4563 {
4564 int val = tv2bool(tv);
4565
4566 /*
4567 * Repeat until there is no following "||" or "&&"
4568 */
4569 while (p[0] == opchar && p[1] == opchar)
4570 {
4571 typval_T tv2;
4572
4573 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4574 return FAIL;
4575
4576 // eval the next expression
4577 *arg = skipwhite(p + 2);
4578 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004579 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004580 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004581 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004582 {
4583 clear_tv(&tv2);
4584 return FAIL;
4585 }
4586 if ((opchar == '&') == val)
4587 {
4588 // false || tv2 or true && tv2: use tv2
4589 clear_tv(tv);
4590 *tv = tv2;
4591 val = tv2bool(tv);
4592 }
4593 else
4594 clear_tv(&tv2);
4595 p = skipwhite(*arg);
4596 }
4597 }
4598
4599 return OK;
4600}
4601
4602/*
4603 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4604 * Return FAIL if the expression is not a constant.
4605 */
4606 static int
4607evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4608{
4609 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004610 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004611 return FAIL;
4612
4613 // || and && work almost the same
4614 return evaluate_const_and_or(arg, cctx, "&&", tv);
4615}
4616
4617/*
4618 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4619 * Return FAIL if the expression is not a constant.
4620 */
4621 static int
4622evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4623{
4624 // evaluate the first expression
4625 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4626 return FAIL;
4627
4628 // || and && work almost the same
4629 return evaluate_const_and_or(arg, cctx, "||", tv);
4630}
4631
4632/*
4633 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4634 * E.g. for "has('feature')".
4635 * This does not produce error messages. "tv" should be cleared afterwards.
4636 * Return FAIL if the expression is not a constant.
4637 */
4638 static int
4639evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4640{
4641 char_u *p;
4642
4643 // evaluate the first expression
4644 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4645 return FAIL;
4646
4647 p = skipwhite(*arg);
4648 if (*p == '?')
4649 {
4650 int val = tv2bool(tv);
4651 typval_T tv2;
4652
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004653 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004654 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4655 return FAIL;
4656
4657 // evaluate the second expression; any type is accepted
4658 clear_tv(tv);
4659 *arg = skipwhite(p + 1);
4660 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4661 return FAIL;
4662
4663 // Check for the ":".
4664 p = skipwhite(*arg);
4665 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4666 return FAIL;
4667
4668 // evaluate the third expression
4669 *arg = skipwhite(p + 1);
4670 tv2.v_type = VAR_UNKNOWN;
4671 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4672 {
4673 clear_tv(&tv2);
4674 return FAIL;
4675 }
4676 if (val)
4677 {
4678 // use the expr after "?"
4679 clear_tv(&tv2);
4680 }
4681 else
4682 {
4683 // use the expr after ":"
4684 clear_tv(tv);
4685 *tv = tv2;
4686 }
4687 }
4688 return OK;
4689}
4690
4691/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 * compile "if expr"
4693 *
4694 * "if expr" Produces instructions:
4695 * EVAL expr Push result of "expr"
4696 * JUMP_IF_FALSE end
4697 * ... body ...
4698 * end:
4699 *
4700 * "if expr | else" Produces instructions:
4701 * EVAL expr Push result of "expr"
4702 * JUMP_IF_FALSE else
4703 * ... body ...
4704 * JUMP_ALWAYS end
4705 * else:
4706 * ... body ...
4707 * end:
4708 *
4709 * "if expr1 | elseif expr2 | else" Produces instructions:
4710 * EVAL expr Push result of "expr"
4711 * JUMP_IF_FALSE elseif
4712 * ... body ...
4713 * JUMP_ALWAYS end
4714 * elseif:
4715 * EVAL expr Push result of "expr"
4716 * JUMP_IF_FALSE else
4717 * ... body ...
4718 * JUMP_ALWAYS end
4719 * else:
4720 * ... body ...
4721 * end:
4722 */
4723 static char_u *
4724compile_if(char_u *arg, cctx_T *cctx)
4725{
4726 char_u *p = arg;
4727 garray_T *instr = &cctx->ctx_instr;
4728 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004729 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004731 // compile "expr"; if we know it evaluates to FALSE skip the block
4732 tv.v_type = VAR_UNKNOWN;
4733 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4734 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4735 else
4736 cctx->ctx_skip = MAYBE;
4737 clear_tv(&tv);
4738 if (cctx->ctx_skip == MAYBE)
4739 {
4740 p = arg;
4741 if (compile_expr1(&p, cctx) == FAIL)
4742 return NULL;
4743 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744
4745 scope = new_scope(cctx, IF_SCOPE);
4746 if (scope == NULL)
4747 return NULL;
4748
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004749 if (cctx->ctx_skip == MAYBE)
4750 {
4751 // "where" is set when ":elseif", "else" or ":endif" is found
4752 scope->se_u.se_if.is_if_label = instr->ga_len;
4753 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4754 }
4755 else
4756 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757
4758 return p;
4759}
4760
4761 static char_u *
4762compile_elseif(char_u *arg, cctx_T *cctx)
4763{
4764 char_u *p = arg;
4765 garray_T *instr = &cctx->ctx_instr;
4766 isn_T *isn;
4767 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004768 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769
4770 if (scope == NULL || scope->se_type != IF_SCOPE)
4771 {
4772 emsg(_(e_elseif_without_if));
4773 return NULL;
4774 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004775 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776
Bram Moolenaar158906c2020-02-06 20:39:45 +01004777 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004778 {
4779 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004781 return NULL;
4782 // previous "if" or "elseif" jumps here
4783 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4784 isn->isn_arg.jump.jump_where = instr->ga_len;
4785 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004787 // compile "expr"; if we know it evaluates to FALSE skip the block
4788 tv.v_type = VAR_UNKNOWN;
4789 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4790 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4791 else
4792 cctx->ctx_skip = MAYBE;
4793 clear_tv(&tv);
4794 if (cctx->ctx_skip == MAYBE)
4795 {
4796 p = arg;
4797 if (compile_expr1(&p, cctx) == FAIL)
4798 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004800 // "where" is set when ":elseif", "else" or ":endif" is found
4801 scope->se_u.se_if.is_if_label = instr->ga_len;
4802 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4803 }
4804 else
4805 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806
4807 return p;
4808}
4809
4810 static char_u *
4811compile_else(char_u *arg, cctx_T *cctx)
4812{
4813 char_u *p = arg;
4814 garray_T *instr = &cctx->ctx_instr;
4815 isn_T *isn;
4816 scope_T *scope = cctx->ctx_scope;
4817
4818 if (scope == NULL || scope->se_type != IF_SCOPE)
4819 {
4820 emsg(_(e_else_without_if));
4821 return NULL;
4822 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004823 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004825 // jump from previous block to the end, unless the else block is empty
4826 if (cctx->ctx_skip == MAYBE)
4827 {
4828 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004830 return NULL;
4831 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832
Bram Moolenaar158906c2020-02-06 20:39:45 +01004833 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004834 {
4835 if (scope->se_u.se_if.is_if_label >= 0)
4836 {
4837 // previous "if" or "elseif" jumps here
4838 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4839 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004840 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004841 }
4842 }
4843
4844 if (cctx->ctx_skip != MAYBE)
4845 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846
4847 return p;
4848}
4849
4850 static char_u *
4851compile_endif(char_u *arg, cctx_T *cctx)
4852{
4853 scope_T *scope = cctx->ctx_scope;
4854 ifscope_T *ifscope;
4855 garray_T *instr = &cctx->ctx_instr;
4856 isn_T *isn;
4857
4858 if (scope == NULL || scope->se_type != IF_SCOPE)
4859 {
4860 emsg(_(e_endif_without_if));
4861 return NULL;
4862 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004863 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004864 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004866 if (scope->se_u.se_if.is_if_label >= 0)
4867 {
4868 // previous "if" or "elseif" jumps here
4869 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4870 isn->isn_arg.jump.jump_where = instr->ga_len;
4871 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 // Fill in the "end" label in jumps at the end of the blocks.
4873 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004874 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004876 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 return arg;
4878}
4879
4880/*
4881 * compile "for var in expr"
4882 *
4883 * Produces instructions:
4884 * PUSHNR -1
4885 * STORE loop-idx Set index to -1
4886 * EVAL expr Push result of "expr"
4887 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4888 * - if beyond end, jump to "end"
4889 * - otherwise get item from list and push it
4890 * STORE var Store item in "var"
4891 * ... body ...
4892 * JUMP top Jump back to repeat
4893 * end: DROP Drop the result of "expr"
4894 *
4895 */
4896 static char_u *
4897compile_for(char_u *arg, cctx_T *cctx)
4898{
4899 char_u *p;
4900 size_t varlen;
4901 garray_T *instr = &cctx->ctx_instr;
4902 garray_T *stack = &cctx->ctx_type_stack;
4903 scope_T *scope;
4904 int loop_idx; // index of loop iteration variable
4905 int var_idx; // index of "var"
4906 type_T *vartype;
4907
4908 // TODO: list of variables: "for [key, value] in dict"
4909 // parse "var"
4910 for (p = arg; eval_isnamec1(*p); ++p)
4911 ;
4912 varlen = p - arg;
4913 var_idx = lookup_local(arg, varlen, cctx);
4914 if (var_idx >= 0)
4915 {
4916 semsg(_("E1023: variable already defined: %s"), arg);
4917 return NULL;
4918 }
4919
4920 // consume "in"
4921 p = skipwhite(p);
4922 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4923 {
4924 emsg(_(e_missing_in));
4925 return NULL;
4926 }
4927 p = skipwhite(p + 2);
4928
4929
4930 scope = new_scope(cctx, FOR_SCOPE);
4931 if (scope == NULL)
4932 return NULL;
4933
4934 // Reserve a variable to store the loop iteration counter.
4935 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4936 if (loop_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004937 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004938 // only happens when out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004939 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942
4943 // Reserve a variable to store "var"
4944 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4945 if (var_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004946 {
4947 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004949 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950
4951 generate_STORENR(cctx, loop_idx, -1);
4952
4953 // compile "expr", it remains on the stack until "endfor"
4954 arg = p;
4955 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004956 {
4957 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004959 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960
4961 // now we know the type of "var"
4962 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4963 if (vartype->tt_type != VAR_LIST)
4964 {
4965 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004966 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 return NULL;
4968 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02004969 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 {
4971 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4972
4973 lvar->lv_type = vartype->tt_member;
4974 }
4975
4976 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004977 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978
4979 generate_FOR(cctx, loop_idx);
4980 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4981
4982 return arg;
4983}
4984
4985/*
4986 * compile "endfor"
4987 */
4988 static char_u *
4989compile_endfor(char_u *arg, cctx_T *cctx)
4990{
4991 garray_T *instr = &cctx->ctx_instr;
4992 scope_T *scope = cctx->ctx_scope;
4993 forscope_T *forscope;
4994 isn_T *isn;
4995
4996 if (scope == NULL || scope->se_type != FOR_SCOPE)
4997 {
4998 emsg(_(e_for));
4999 return NULL;
5000 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005001 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005003 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004
5005 // At end of ":for" scope jump back to the FOR instruction.
5006 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5007
5008 // Fill in the "end" label in the FOR statement so it can jump here
5009 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5010 isn->isn_arg.forloop.for_end = instr->ga_len;
5011
5012 // Fill in the "end" label any BREAK statements
5013 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5014
5015 // Below the ":for" scope drop the "expr" list from the stack.
5016 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5017 return NULL;
5018
5019 vim_free(scope);
5020
5021 return arg;
5022}
5023
5024/*
5025 * compile "while expr"
5026 *
5027 * Produces instructions:
5028 * top: EVAL expr Push result of "expr"
5029 * JUMP_IF_FALSE end jump if false
5030 * ... body ...
5031 * JUMP top Jump back to repeat
5032 * end:
5033 *
5034 */
5035 static char_u *
5036compile_while(char_u *arg, cctx_T *cctx)
5037{
5038 char_u *p = arg;
5039 garray_T *instr = &cctx->ctx_instr;
5040 scope_T *scope;
5041
5042 scope = new_scope(cctx, WHILE_SCOPE);
5043 if (scope == NULL)
5044 return NULL;
5045
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005046 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047
5048 // compile "expr"
5049 if (compile_expr1(&p, cctx) == FAIL)
5050 return NULL;
5051
5052 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005053 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 JUMP_IF_FALSE, cctx) == FAIL)
5055 return FAIL;
5056
5057 return p;
5058}
5059
5060/*
5061 * compile "endwhile"
5062 */
5063 static char_u *
5064compile_endwhile(char_u *arg, cctx_T *cctx)
5065{
5066 scope_T *scope = cctx->ctx_scope;
5067
5068 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5069 {
5070 emsg(_(e_while));
5071 return NULL;
5072 }
5073 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005074 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075
5076 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005077 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078
5079 // Fill in the "end" label in the WHILE statement so it can jump here.
5080 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005081 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082
5083 vim_free(scope);
5084
5085 return arg;
5086}
5087
5088/*
5089 * compile "continue"
5090 */
5091 static char_u *
5092compile_continue(char_u *arg, cctx_T *cctx)
5093{
5094 scope_T *scope = cctx->ctx_scope;
5095
5096 for (;;)
5097 {
5098 if (scope == NULL)
5099 {
5100 emsg(_(e_continue));
5101 return NULL;
5102 }
5103 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5104 break;
5105 scope = scope->se_outer;
5106 }
5107
5108 // Jump back to the FOR or WHILE instruction.
5109 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005110 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5111 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 return arg;
5113}
5114
5115/*
5116 * compile "break"
5117 */
5118 static char_u *
5119compile_break(char_u *arg, cctx_T *cctx)
5120{
5121 scope_T *scope = cctx->ctx_scope;
5122 endlabel_T **el;
5123
5124 for (;;)
5125 {
5126 if (scope == NULL)
5127 {
5128 emsg(_(e_break));
5129 return NULL;
5130 }
5131 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5132 break;
5133 scope = scope->se_outer;
5134 }
5135
5136 // Jump to the end of the FOR or WHILE loop.
5137 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005138 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005140 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5142 return FAIL;
5143
5144 return arg;
5145}
5146
5147/*
5148 * compile "{" start of block
5149 */
5150 static char_u *
5151compile_block(char_u *arg, cctx_T *cctx)
5152{
5153 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5154 return NULL;
5155 return skipwhite(arg + 1);
5156}
5157
5158/*
5159 * compile end of block: drop one scope
5160 */
5161 static void
5162compile_endblock(cctx_T *cctx)
5163{
5164 scope_T *scope = cctx->ctx_scope;
5165
5166 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005167 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168 vim_free(scope);
5169}
5170
5171/*
5172 * compile "try"
5173 * Creates a new scope for the try-endtry, pointing to the first catch and
5174 * finally.
5175 * Creates another scope for the "try" block itself.
5176 * TRY instruction sets up exception handling at runtime.
5177 *
5178 * "try"
5179 * TRY -> catch1, -> finally push trystack entry
5180 * ... try block
5181 * "throw {exception}"
5182 * EVAL {exception}
5183 * THROW create exception
5184 * ... try block
5185 * " catch {expr}"
5186 * JUMP -> finally
5187 * catch1: PUSH exeception
5188 * EVAL {expr}
5189 * MATCH
5190 * JUMP nomatch -> catch2
5191 * CATCH remove exception
5192 * ... catch block
5193 * " catch"
5194 * JUMP -> finally
5195 * catch2: CATCH remove exception
5196 * ... catch block
5197 * " finally"
5198 * finally:
5199 * ... finally block
5200 * " endtry"
5201 * ENDTRY pop trystack entry, may rethrow
5202 */
5203 static char_u *
5204compile_try(char_u *arg, cctx_T *cctx)
5205{
5206 garray_T *instr = &cctx->ctx_instr;
5207 scope_T *try_scope;
5208 scope_T *scope;
5209
5210 // scope that holds the jumps that go to catch/finally/endtry
5211 try_scope = new_scope(cctx, TRY_SCOPE);
5212 if (try_scope == NULL)
5213 return NULL;
5214
5215 // "catch" is set when the first ":catch" is found.
5216 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005217 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 if (generate_instr(cctx, ISN_TRY) == NULL)
5219 return NULL;
5220
5221 // scope for the try block itself
5222 scope = new_scope(cctx, BLOCK_SCOPE);
5223 if (scope == NULL)
5224 return NULL;
5225
5226 return arg;
5227}
5228
5229/*
5230 * compile "catch {expr}"
5231 */
5232 static char_u *
5233compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5234{
5235 scope_T *scope = cctx->ctx_scope;
5236 garray_T *instr = &cctx->ctx_instr;
5237 char_u *p;
5238 isn_T *isn;
5239
5240 // end block scope from :try or :catch
5241 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5242 compile_endblock(cctx);
5243 scope = cctx->ctx_scope;
5244
5245 // Error if not in a :try scope
5246 if (scope == NULL || scope->se_type != TRY_SCOPE)
5247 {
5248 emsg(_(e_catch));
5249 return NULL;
5250 }
5251
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005252 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253 {
5254 emsg(_("E1033: catch unreachable after catch-all"));
5255 return NULL;
5256 }
5257
5258 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005259 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005260 JUMP_ALWAYS, cctx) == FAIL)
5261 return NULL;
5262
5263 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005264 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265 if (isn->isn_arg.try.try_catch == 0)
5266 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005267 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005268 {
5269 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005270 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 isn->isn_arg.jump.jump_where = instr->ga_len;
5272 }
5273
5274 p = skipwhite(arg);
5275 if (ends_excmd(*p))
5276 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005277 scope->se_u.se_try.ts_caught_all = TRUE;
5278 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279 }
5280 else
5281 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005282 char_u *end;
5283 char_u *pat;
5284 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005285 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005286 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288 // Push v:exception, push {expr} and MATCH
5289 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5290
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005291 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005292 if (*end != *p)
5293 {
5294 semsg(_("E1067: Separator mismatch: %s"), p);
5295 vim_free(tofree);
5296 return FAIL;
5297 }
5298 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005299 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005300 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005301 len = (int)(end - tofree);
5302 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005303 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005304 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005305 if (pat == NULL)
5306 return FAIL;
5307 if (generate_PUSHS(cctx, pat) == FAIL)
5308 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5311 return NULL;
5312
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005313 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005314 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5315 return NULL;
5316 }
5317
5318 if (generate_instr(cctx, ISN_CATCH) == NULL)
5319 return NULL;
5320
5321 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5322 return NULL;
5323 return p;
5324}
5325
5326 static char_u *
5327compile_finally(char_u *arg, cctx_T *cctx)
5328{
5329 scope_T *scope = cctx->ctx_scope;
5330 garray_T *instr = &cctx->ctx_instr;
5331 isn_T *isn;
5332
5333 // end block scope from :try or :catch
5334 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5335 compile_endblock(cctx);
5336 scope = cctx->ctx_scope;
5337
5338 // Error if not in a :try scope
5339 if (scope == NULL || scope->se_type != TRY_SCOPE)
5340 {
5341 emsg(_(e_finally));
5342 return NULL;
5343 }
5344
5345 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005346 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005347 if (isn->isn_arg.try.try_finally != 0)
5348 {
5349 emsg(_(e_finally_dup));
5350 return NULL;
5351 }
5352
5353 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005354 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355
Bram Moolenaar585fea72020-04-02 22:33:21 +02005356 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005357 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005358 {
5359 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005360 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 isn->isn_arg.jump.jump_where = instr->ga_len;
5362 }
5363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005364 // TODO: set index in ts_finally_label jumps
5365
5366 return arg;
5367}
5368
5369 static char_u *
5370compile_endtry(char_u *arg, cctx_T *cctx)
5371{
5372 scope_T *scope = cctx->ctx_scope;
5373 garray_T *instr = &cctx->ctx_instr;
5374 isn_T *isn;
5375
5376 // end block scope from :catch or :finally
5377 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5378 compile_endblock(cctx);
5379 scope = cctx->ctx_scope;
5380
5381 // Error if not in a :try scope
5382 if (scope == NULL || scope->se_type != TRY_SCOPE)
5383 {
5384 if (scope == NULL)
5385 emsg(_(e_no_endtry));
5386 else if (scope->se_type == WHILE_SCOPE)
5387 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005388 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389 emsg(_(e_endfor));
5390 else
5391 emsg(_(e_endif));
5392 return NULL;
5393 }
5394
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005395 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5397 {
5398 emsg(_("E1032: missing :catch or :finally"));
5399 return NULL;
5400 }
5401
5402 // Fill in the "end" label in jumps at the end of the blocks, if not done
5403 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005404 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405
5406 // End :catch or :finally scope: set value in ISN_TRY instruction
5407 if (isn->isn_arg.try.try_finally == 0)
5408 isn->isn_arg.try.try_finally = instr->ga_len;
5409 compile_endblock(cctx);
5410
5411 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5412 return NULL;
5413 return arg;
5414}
5415
5416/*
5417 * compile "throw {expr}"
5418 */
5419 static char_u *
5420compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5421{
5422 char_u *p = skipwhite(arg);
5423
5424 if (ends_excmd(*p))
5425 {
5426 emsg(_(e_argreq));
5427 return NULL;
5428 }
5429 if (compile_expr1(&p, cctx) == FAIL)
5430 return NULL;
5431 if (may_generate_2STRING(-1, cctx) == FAIL)
5432 return NULL;
5433 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5434 return NULL;
5435
5436 return p;
5437}
5438
5439/*
5440 * compile "echo expr"
5441 */
5442 static char_u *
5443compile_echo(char_u *arg, int with_white, cctx_T *cctx)
5444{
5445 char_u *p = arg;
5446 int count = 0;
5447
Bram Moolenaarad39c092020-02-26 18:23:43 +01005448 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449 {
5450 if (compile_expr1(&p, cctx) == FAIL)
5451 return NULL;
5452 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005453 p = skipwhite(p);
5454 if (ends_excmd(*p))
5455 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456 }
5457
5458 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01005459 return p;
5460}
5461
5462/*
5463 * compile "execute expr"
5464 */
5465 static char_u *
5466compile_execute(char_u *arg, cctx_T *cctx)
5467{
5468 char_u *p = arg;
5469 int count = 0;
5470
5471 for (;;)
5472 {
5473 if (compile_expr1(&p, cctx) == FAIL)
5474 return NULL;
5475 ++count;
5476 p = skipwhite(p);
5477 if (ends_excmd(*p))
5478 break;
5479 }
5480
5481 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482
5483 return p;
5484}
5485
5486/*
5487 * After ex_function() has collected all the function lines: parse and compile
5488 * the lines into instructions.
5489 * Adds the function to "def_functions".
5490 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5491 * return statement (used for lambda).
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005492 * This can be used recursively through compile_lambda(), which may reallocate
5493 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494 */
5495 void
5496compile_def_function(ufunc_T *ufunc, int set_return_type)
5497{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005498 char_u *line = NULL;
5499 char_u *p;
5500 exarg_T ea;
5501 char *errormsg = NULL; // error message
5502 int had_return = FALSE;
5503 cctx_T cctx;
5504 garray_T *instr;
5505 int called_emsg_before = called_emsg;
5506 int ret = FAIL;
5507 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005508 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005510 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005511 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005512
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005513 if (ufunc->uf_dfunc_idx >= 0)
5514 {
5515 // Redefining a function that was compiled before.
5516 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5517
5518 // Free old instructions.
5519 delete_def_function_contents(dfunc);
5520 }
5521 else
5522 {
5523 // Add the function to "def_functions".
5524 if (ga_grow(&def_functions, 1) == FAIL)
5525 return;
5526 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
5527 vim_memset(dfunc, 0, sizeof(dfunc_T));
5528 dfunc->df_idx = def_functions.ga_len;
5529 ufunc->uf_dfunc_idx = dfunc->df_idx;
5530 dfunc->df_ufunc = ufunc;
5531 ++def_functions.ga_len;
5532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533 }
5534
5535 vim_memset(&cctx, 0, sizeof(cctx));
5536 cctx.ctx_ufunc = ufunc;
5537 cctx.ctx_lnum = -1;
5538 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5539 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5540 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5541 cctx.ctx_type_list = &ufunc->uf_type_list;
5542 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5543 instr = &cctx.ctx_instr;
5544
5545 // Most modern script version.
5546 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5547
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005548 if (ufunc->uf_def_args.ga_len > 0)
5549 {
5550 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005551 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005552 int i;
5553 char_u *arg;
5554 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5555
5556 // Produce instructions for the default values of optional arguments.
5557 // Store the instruction index in uf_def_arg_idx[] so that we know
5558 // where to start when the function is called, depending on the number
5559 // of arguments.
5560 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5561 if (ufunc->uf_def_arg_idx == NULL)
5562 goto erret;
5563 for (i = 0; i < count; ++i)
5564 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005565 garray_T *stack = &cctx.ctx_type_stack;
5566 type_T *val_type;
5567 int arg_idx = first_def_arg + i;
5568
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005569 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5570 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005571 if (compile_expr1(&arg, &cctx) == FAIL)
5572 goto erret;
5573
5574 // If no type specified use the type of the default value.
5575 // Otherwise check that the default value type matches the
5576 // specified type.
5577 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5578 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
5579 ufunc->uf_arg_types[arg_idx] = val_type;
5580 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
5581 == FAIL)
5582 {
5583 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
5584 arg_idx + 1);
5585 goto erret;
5586 }
5587
5588 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005589 goto erret;
5590 }
5591
5592 // If a varargs is following, push an empty list.
5593 if (ufunc->uf_va_name != NULL)
5594 {
5595 if (generate_NEWLIST(&cctx, 0) == FAIL
5596 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
5597 goto erret;
5598 }
5599
5600 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5601 }
5602
5603 /*
5604 * Loop over all the lines of the function and generate instructions.
5605 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005606 for (;;)
5607 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005608 int is_ex_command;
5609
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005610 // Bail out on the first error to avoid a flood of errors and report
5611 // the right line number when inside try/catch.
5612 if (emsg_before != called_emsg)
5613 goto erret;
5614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005615 if (line != NULL && *line == '|')
5616 // the line continues after a '|'
5617 ++line;
5618 else if (line != NULL && *line != NUL)
5619 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005620 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005621 goto erret;
5622 }
5623 else
5624 {
5625 do
5626 {
5627 ++cctx.ctx_lnum;
5628 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5629 break;
5630 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
5631 } while (line == NULL);
5632 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5633 break;
5634 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
5635 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005636 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005637
5638 had_return = FALSE;
5639 vim_memset(&ea, 0, sizeof(ea));
5640 ea.cmdlinep = &line;
5641 ea.cmd = skipwhite(line);
5642
5643 // "}" ends a block scope
5644 if (*ea.cmd == '}')
5645 {
5646 scopetype_T stype = cctx.ctx_scope == NULL
5647 ? NO_SCOPE : cctx.ctx_scope->se_type;
5648
5649 if (stype == BLOCK_SCOPE)
5650 {
5651 compile_endblock(&cctx);
5652 line = ea.cmd;
5653 }
5654 else
5655 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005656 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657 goto erret;
5658 }
5659 if (line != NULL)
5660 line = skipwhite(ea.cmd + 1);
5661 continue;
5662 }
5663
5664 // "{" starts a block scope
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01005665 // "{'a': 1}->func() is something else
5666 if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005667 {
5668 line = compile_block(ea.cmd, &cctx);
5669 continue;
5670 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005671 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672
5673 /*
5674 * COMMAND MODIFIERS
5675 */
5676 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5677 {
5678 if (errormsg != NULL)
5679 goto erret;
5680 // empty line or comment
5681 line = (char_u *)"";
5682 continue;
5683 }
5684
5685 // Skip ":call" to get to the function name.
5686 if (checkforcmd(&ea.cmd, "call", 3))
5687 ea.cmd = skipwhite(ea.cmd);
5688
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005689 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005690 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005691 // Assuming the command starts with a variable or function name,
5692 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5693 // val".
5694 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5695 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005696 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005697 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005698 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005699 int oplen;
5700 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005701
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005702 oplen = assignment_len(skipwhite(p), &heredoc);
5703 if (oplen > 0)
5704 {
5705 // Recognize an assignment if we recognize the variable
5706 // name:
5707 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005708 // "local = expr" where "local" is a local var.
5709 // "script = expr" where "script" is a script-local var.
5710 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005711 // "&opt = expr"
5712 // "$ENV = expr"
5713 // "@r = expr"
5714 if (*ea.cmd == '&'
5715 || *ea.cmd == '$'
5716 || *ea.cmd == '@'
5717 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5718 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5719 || lookup_script(ea.cmd, p - ea.cmd) == OK
5720 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5721 {
5722 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5723 if (line == NULL)
5724 goto erret;
5725 continue;
5726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005727 }
5728 }
5729 }
5730
5731 /*
5732 * COMMAND after range
5733 */
5734 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005735 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5736 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737
5738 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5739 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005740 if (cctx.ctx_skip == TRUE)
5741 {
5742 line += STRLEN(line);
5743 continue;
5744 }
5745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746 // Expression or function call.
5747 if (ea.cmdidx == CMD_eval)
5748 {
5749 p = ea.cmd;
5750 if (compile_expr1(&p, &cctx) == FAIL)
5751 goto erret;
5752
5753 // drop the return value
5754 generate_instr_drop(&cctx, ISN_DROP, 1);
5755 line = p;
5756 continue;
5757 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02005758 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759 iemsg("Command from find_ex_command() not handled");
5760 goto erret;
5761 }
5762
5763 p = skipwhite(p);
5764
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005765 if (cctx.ctx_skip == TRUE
5766 && ea.cmdidx != CMD_elseif
5767 && ea.cmdidx != CMD_else
5768 && ea.cmdidx != CMD_endif)
5769 {
5770 line += STRLEN(line);
5771 continue;
5772 }
5773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005774 switch (ea.cmdidx)
5775 {
5776 case CMD_def:
5777 case CMD_function:
5778 // TODO: Nested function
5779 emsg("Nested function not implemented yet");
5780 goto erret;
5781
5782 case CMD_return:
5783 line = compile_return(p, set_return_type, &cctx);
5784 had_return = TRUE;
5785 break;
5786
5787 case CMD_let:
5788 case CMD_const:
5789 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5790 break;
5791
5792 case CMD_import:
5793 line = compile_import(p, &cctx);
5794 break;
5795
5796 case CMD_if:
5797 line = compile_if(p, &cctx);
5798 break;
5799 case CMD_elseif:
5800 line = compile_elseif(p, &cctx);
5801 break;
5802 case CMD_else:
5803 line = compile_else(p, &cctx);
5804 break;
5805 case CMD_endif:
5806 line = compile_endif(p, &cctx);
5807 break;
5808
5809 case CMD_while:
5810 line = compile_while(p, &cctx);
5811 break;
5812 case CMD_endwhile:
5813 line = compile_endwhile(p, &cctx);
5814 break;
5815
5816 case CMD_for:
5817 line = compile_for(p, &cctx);
5818 break;
5819 case CMD_endfor:
5820 line = compile_endfor(p, &cctx);
5821 break;
5822 case CMD_continue:
5823 line = compile_continue(p, &cctx);
5824 break;
5825 case CMD_break:
5826 line = compile_break(p, &cctx);
5827 break;
5828
5829 case CMD_try:
5830 line = compile_try(p, &cctx);
5831 break;
5832 case CMD_catch:
5833 line = compile_catch(p, &cctx);
5834 break;
5835 case CMD_finally:
5836 line = compile_finally(p, &cctx);
5837 break;
5838 case CMD_endtry:
5839 line = compile_endtry(p, &cctx);
5840 break;
5841 case CMD_throw:
5842 line = compile_throw(p, &cctx);
5843 break;
5844
5845 case CMD_echo:
5846 line = compile_echo(p, TRUE, &cctx);
5847 break;
5848 case CMD_echon:
5849 line = compile_echo(p, FALSE, &cctx);
5850 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005851 case CMD_execute:
5852 line = compile_execute(p, &cctx);
5853 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005854
5855 default:
5856 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005857 // TODO:
5858 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005859 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005860 generate_EXEC(&cctx, line);
5861 line = (char_u *)"";
5862 break;
5863 }
5864 if (line == NULL)
5865 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02005866 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005867
5868 if (cctx.ctx_type_stack.ga_len < 0)
5869 {
5870 iemsg("Type stack underflow");
5871 goto erret;
5872 }
5873 }
5874
5875 if (cctx.ctx_scope != NULL)
5876 {
5877 if (cctx.ctx_scope->se_type == IF_SCOPE)
5878 emsg(_(e_endif));
5879 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5880 emsg(_(e_endwhile));
5881 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5882 emsg(_(e_endfor));
5883 else
5884 emsg(_("E1026: Missing }"));
5885 goto erret;
5886 }
5887
5888 if (!had_return)
5889 {
5890 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5891 {
5892 emsg(_("E1027: Missing return statement"));
5893 goto erret;
5894 }
5895
5896 // Return zero if there is no return at the end.
5897 generate_PUSHNR(&cctx, 0);
5898 generate_instr(&cctx, ISN_RETURN);
5899 }
5900
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005901 {
5902 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5903 + ufunc->uf_dfunc_idx;
5904 dfunc->df_deleted = FALSE;
5905 dfunc->df_instr = instr->ga_data;
5906 dfunc->df_instr_count = instr->ga_len;
5907 dfunc->df_varcount = cctx.ctx_max_local;
5908 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005909
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005910 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005911 int varargs = ufunc->uf_va_name != NULL;
5912 int argcount = ufunc->uf_args.ga_len - (varargs ? 1 : 0);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005913
5914 // Create a type for the function, with the return type and any
5915 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005916 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5917 // The type is included in "tt_args".
5918 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5919 ufunc->uf_args.ga_len, &ufunc->uf_type_list);
5920 if (ufunc->uf_args.ga_len > 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005921 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005922 if (func_type_add_arg_types(ufunc->uf_func_type,
5923 ufunc->uf_args.ga_len,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005924 argcount - ufunc->uf_def_args.ga_len,
5925 &ufunc->uf_type_list) == FAIL)
5926 {
5927 ret = FAIL;
5928 goto erret;
5929 }
5930 if (ufunc->uf_arg_types == NULL)
5931 {
5932 int i;
5933
5934 // lambda does not have argument types.
5935 for (i = 0; i < argcount; ++i)
5936 ufunc->uf_func_type->tt_args[i] = &t_any;
5937 }
5938 else
5939 mch_memmove(ufunc->uf_func_type->tt_args,
5940 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005941 if (varargs)
5942 ufunc->uf_func_type->tt_args[argcount] =
5943 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005944 }
5945 }
5946
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005947 ret = OK;
5948
5949erret:
5950 if (ret == FAIL)
5951 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005952 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005953 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5954 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005955
5956 for (idx = 0; idx < instr->ga_len; ++idx)
5957 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005958 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005960 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005961 if (!dfunc->df_deleted)
5962 --def_functions.ga_len;
5963
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005964 while (cctx.ctx_scope != NULL)
5965 drop_scope(&cctx);
5966
Bram Moolenaar20431c92020-03-20 18:39:46 +01005967 // Don't execute this function body.
5968 ga_clear_strings(&ufunc->uf_lines);
5969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 if (errormsg != NULL)
5971 emsg(errormsg);
5972 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005973 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 }
5975
5976 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005977 free_imported(&cctx);
5978 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980}
5981
5982/*
5983 * Delete an instruction, free what it contains.
5984 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01005985 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986delete_instr(isn_T *isn)
5987{
5988 switch (isn->isn_type)
5989 {
5990 case ISN_EXEC:
5991 case ISN_LOADENV:
5992 case ISN_LOADG:
5993 case ISN_LOADOPT:
5994 case ISN_MEMBER:
5995 case ISN_PUSHEXC:
5996 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005997 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005999 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000 vim_free(isn->isn_arg.string);
6001 break;
6002
6003 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006004 case ISN_STORES:
6005 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006006 break;
6007
6008 case ISN_STOREOPT:
6009 vim_free(isn->isn_arg.storeopt.so_name);
6010 break;
6011
6012 case ISN_PUSHBLOB: // push blob isn_arg.blob
6013 blob_unref(isn->isn_arg.blob);
6014 break;
6015
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006016 case ISN_PUSHPARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01006017 partial_unref(isn->isn_arg.partial);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006018 break;
6019
6020 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006021#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006022 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006023#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006024 break;
6025
6026 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006027#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006028 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006029#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006030 break;
6031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006032 case ISN_UCALL:
6033 vim_free(isn->isn_arg.ufunc.cuf_name);
6034 break;
6035
6036 case ISN_2BOOL:
6037 case ISN_2STRING:
6038 case ISN_ADDBLOB:
6039 case ISN_ADDLIST:
6040 case ISN_BCALL:
6041 case ISN_CATCH:
6042 case ISN_CHECKNR:
6043 case ISN_CHECKTYPE:
6044 case ISN_COMPAREANY:
6045 case ISN_COMPAREBLOB:
6046 case ISN_COMPAREBOOL:
6047 case ISN_COMPAREDICT:
6048 case ISN_COMPAREFLOAT:
6049 case ISN_COMPAREFUNC:
6050 case ISN_COMPARELIST:
6051 case ISN_COMPARENR:
6052 case ISN_COMPAREPARTIAL:
6053 case ISN_COMPARESPECIAL:
6054 case ISN_COMPARESTRING:
6055 case ISN_CONCAT:
6056 case ISN_DCALL:
6057 case ISN_DROP:
6058 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006059 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060 case ISN_ENDTRY:
6061 case ISN_FOR:
6062 case ISN_FUNCREF:
6063 case ISN_INDEX:
6064 case ISN_JUMP:
6065 case ISN_LOAD:
6066 case ISN_LOADSCRIPT:
6067 case ISN_LOADREG:
6068 case ISN_LOADV:
6069 case ISN_NEGATENR:
6070 case ISN_NEWDICT:
6071 case ISN_NEWLIST:
6072 case ISN_OPNR:
6073 case ISN_OPFLOAT:
6074 case ISN_OPANY:
6075 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006076 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006077 case ISN_PUSHF:
6078 case ISN_PUSHNR:
6079 case ISN_PUSHBOOL:
6080 case ISN_PUSHSPEC:
6081 case ISN_RETURN:
6082 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006083 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006084 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006085 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086 case ISN_STORESCRIPT:
6087 case ISN_THROW:
6088 case ISN_TRY:
6089 // nothing allocated
6090 break;
6091 }
6092}
6093
6094/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006095 * Free all instructions for "dfunc".
6096 */
6097 static void
6098delete_def_function_contents(dfunc_T *dfunc)
6099{
6100 int idx;
6101
6102 ga_clear(&dfunc->df_def_args_isn);
6103
6104 if (dfunc->df_instr != NULL)
6105 {
6106 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6107 delete_instr(dfunc->df_instr + idx);
6108 VIM_CLEAR(dfunc->df_instr);
6109 }
6110
6111 dfunc->df_deleted = TRUE;
6112}
6113
6114/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006115 * When a user function is deleted, delete any associated def function.
6116 */
6117 void
6118delete_def_function(ufunc_T *ufunc)
6119{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006120 if (ufunc->uf_dfunc_idx >= 0)
6121 {
6122 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6123 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006124
Bram Moolenaar20431c92020-03-20 18:39:46 +01006125 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006126 }
6127}
6128
6129#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006130/*
6131 * Free all functions defined with ":def".
6132 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006133 void
6134free_def_functions(void)
6135{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006136 int idx;
6137
6138 for (idx = 0; idx < def_functions.ga_len; ++idx)
6139 {
6140 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6141
6142 delete_def_function_contents(dfunc);
6143 }
6144
6145 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146}
6147#endif
6148
6149
6150#endif // FEAT_EVAL