blob: 98936dd66068954ac7b1c8d65e5c8b23fa002e8f [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100133
134/*
135 * Lookup variable "name" in the local scope and return the index.
136 */
137 static int
138lookup_local(char_u *name, size_t len, cctx_T *cctx)
139{
140 int idx;
141
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100142 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 return -1;
144 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
145 {
146 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
147
148 if (STRNCMP(name, lvar->lv_name, len) == 0
149 && STRLEN(lvar->lv_name) == len)
150 return idx;
151 }
152 return -1;
153}
154
155/*
156 * Lookup an argument in the current function.
157 * Returns the argument index or -1 if not found.
158 */
159 static int
160lookup_arg(char_u *name, size_t len, cctx_T *cctx)
161{
162 int idx;
163
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100164 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 return -1;
166 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
167 {
168 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
169
170 if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
171 return idx;
172 }
173 return -1;
174}
175
176/*
177 * Lookup a vararg argument in the current function.
178 * Returns TRUE if there is a match.
179 */
180 static int
181lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
182{
183 char_u *va_name = cctx->ctx_ufunc->uf_va_name;
184
185 return len > 0 && va_name != NULL
186 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
187}
188
189/*
190 * Lookup a variable in the current script.
191 * Returns OK or FAIL.
192 */
193 static int
194lookup_script(char_u *name, size_t len)
195{
196 int cc;
197 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
198 dictitem_T *di;
199
200 cc = name[len];
201 name[len] = NUL;
202 di = find_var_in_ht(ht, 0, name, TRUE);
203 name[len] = cc;
204 return di == NULL ? FAIL: OK;
205}
206
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100207/*
208 * Check if "p[len]" is already defined, either in script "import_sid" or in
209 * compilation context "cctx".
210 * Return FAIL and give an error if it defined.
211 */
212 int
213check_defined(char_u *p, int len, cctx_T *cctx)
214{
215 if (lookup_script(p, len) == OK
216 || (cctx != NULL
217 && (lookup_local(p, len, cctx) >= 0
218 || find_imported(p, len, cctx) != NULL)))
219 {
220 semsg("E1073: imported name already defined: %s", p);
221 return FAIL;
222 }
223 return OK;
224}
225
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200226/*
227 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
228 * be freed later.
229 */
230 static type_T *
231alloc_type(garray_T *type_gap)
232{
233 type_T *type;
234
235 if (ga_grow(type_gap, 1) == FAIL)
236 return NULL;
237 type = ALLOC_CLEAR_ONE(type_T);
238 if (type != NULL)
239 {
240 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
241 ++type_gap->ga_len;
242 }
243 return type;
244}
245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200247get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248{
249 type_T *type;
250
251 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200252 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200254 if (member_type->tt_type == VAR_VOID
255 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100256 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100257 if (member_type->tt_type == VAR_BOOL)
258 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259 if (member_type->tt_type == VAR_NUMBER)
260 return &t_list_number;
261 if (member_type->tt_type == VAR_STRING)
262 return &t_list_string;
263
264 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200265 type = alloc_type(type_gap);
266 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100267 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 type->tt_type = VAR_LIST;
269 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200270 type->tt_argcount = 0;
271 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272 return type;
273}
274
275 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200276get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277{
278 type_T *type;
279
280 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200281 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200283 if (member_type->tt_type == VAR_VOID
284 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100285 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100286 if (member_type->tt_type == VAR_BOOL)
287 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288 if (member_type->tt_type == VAR_NUMBER)
289 return &t_dict_number;
290 if (member_type->tt_type == VAR_STRING)
291 return &t_dict_string;
292
293 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200294 type = alloc_type(type_gap);
295 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100296 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 type->tt_type = VAR_DICT;
298 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200299 type->tt_argcount = 0;
300 type->tt_args = NULL;
301 return type;
302}
303
304/*
305 * Get a function type, based on the return type "ret_type".
306 * If "argcount" is -1 or 0 a predefined type can be used.
307 * If "argcount" > 0 always create a new type, so that arguments can be added.
308 */
309 static type_T *
310get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
311{
312 type_T *type;
313
314 // recognize commonly used types
315 if (argcount <= 0)
316 {
317 if (ret_type == &t_void)
318 {
319 if (argcount == 0)
320 return &t_func_0_void;
321 else
322 return &t_func_void;
323 }
324 if (ret_type == &t_any)
325 {
326 if (argcount == 0)
327 return &t_func_0_any;
328 else
329 return &t_func_any;
330 }
331 if (ret_type == &t_number)
332 {
333 if (argcount == 0)
334 return &t_func_0_number;
335 else
336 return &t_func_number;
337 }
338 if (ret_type == &t_string)
339 {
340 if (argcount == 0)
341 return &t_func_0_string;
342 else
343 return &t_func_string;
344 }
345 }
346
347 // Not a common type or has arguments, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200348 type = alloc_type(type_gap);
349 if (type == NULL)
Bram Moolenaard77a8522020-04-03 21:59:57 +0200350 return &t_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200351 type->tt_type = VAR_FUNC;
352 type->tt_member = ret_type;
353 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100354 return type;
355}
356
Bram Moolenaara8c17702020-04-01 21:17:24 +0200357/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200358 * For a function type, reserve space for "argcount" argument types (including
359 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200360 */
361 static int
362func_type_add_arg_types(
363 type_T *functype,
364 int argcount,
365 int min_argcount,
366 garray_T *type_gap)
367{
368 if (ga_grow(type_gap, 1) == FAIL)
369 return FAIL;
370 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
371 if (functype->tt_args == NULL)
372 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200373 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
374 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200375 ++type_gap->ga_len;
376
377 functype->tt_argcount = argcount;
378 functype->tt_min_argcount = min_argcount;
379 return OK;
380}
381
382/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200383 * Return the type_T for a typval. Only for primitive types.
384 */
385 static type_T *
386typval2type(typval_T *tv)
387{
388 if (tv->v_type == VAR_NUMBER)
389 return &t_number;
390 if (tv->v_type == VAR_BOOL)
391 return &t_bool;
392 if (tv->v_type == VAR_STRING)
393 return &t_string;
394 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
395 return &t_list_string;
396 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
397 return &t_dict_any;
398 return &t_any;
399}
400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100401/////////////////////////////////////////////////////////////////////
402// Following generate_ functions expect the caller to call ga_grow().
403
Bram Moolenaar080457c2020-03-03 21:53:32 +0100404#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
405#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407/*
408 * Generate an instruction without arguments.
409 * Returns a pointer to the new instruction, NULL if failed.
410 */
411 static isn_T *
412generate_instr(cctx_T *cctx, isntype_T isn_type)
413{
414 garray_T *instr = &cctx->ctx_instr;
415 isn_T *isn;
416
Bram Moolenaar080457c2020-03-03 21:53:32 +0100417 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 if (ga_grow(instr, 1) == FAIL)
419 return NULL;
420 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
421 isn->isn_type = isn_type;
422 isn->isn_lnum = cctx->ctx_lnum + 1;
423 ++instr->ga_len;
424
425 return isn;
426}
427
428/*
429 * Generate an instruction without arguments.
430 * "drop" will be removed from the stack.
431 * Returns a pointer to the new instruction, NULL if failed.
432 */
433 static isn_T *
434generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
435{
436 garray_T *stack = &cctx->ctx_type_stack;
437
Bram Moolenaar080457c2020-03-03 21:53:32 +0100438 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439 stack->ga_len -= drop;
440 return generate_instr(cctx, isn_type);
441}
442
443/*
444 * Generate instruction "isn_type" and put "type" on the type stack.
445 */
446 static isn_T *
447generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
448{
449 isn_T *isn;
450 garray_T *stack = &cctx->ctx_type_stack;
451
452 if ((isn = generate_instr(cctx, isn_type)) == NULL)
453 return NULL;
454
455 if (ga_grow(stack, 1) == FAIL)
456 return NULL;
457 ((type_T **)stack->ga_data)[stack->ga_len] = type;
458 ++stack->ga_len;
459
460 return isn;
461}
462
463/*
464 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
465 */
466 static int
467may_generate_2STRING(int offset, cctx_T *cctx)
468{
469 isn_T *isn;
470 garray_T *stack = &cctx->ctx_type_stack;
471 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
472
473 if ((*type)->tt_type == VAR_STRING)
474 return OK;
475 *type = &t_string;
476
477 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
478 return FAIL;
479 isn->isn_arg.number = offset;
480
481 return OK;
482}
483
484 static int
485check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
486{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200487 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100488 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200489 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100490 {
491 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100492 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493 else
494 semsg(_("E1036: %c requires number or float arguments"), *op);
495 return FAIL;
496 }
497 return OK;
498}
499
500/*
501 * Generate an instruction with two arguments. The instruction depends on the
502 * type of the arguments.
503 */
504 static int
505generate_two_op(cctx_T *cctx, char_u *op)
506{
507 garray_T *stack = &cctx->ctx_type_stack;
508 type_T *type1;
509 type_T *type2;
510 vartype_T vartype;
511 isn_T *isn;
512
Bram Moolenaar080457c2020-03-03 21:53:32 +0100513 RETURN_OK_IF_SKIP(cctx);
514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515 // Get the known type of the two items on the stack. If they are matching
516 // use a type-specific instruction. Otherwise fall back to runtime type
517 // checking.
518 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
519 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200520 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 if (type1->tt_type == type2->tt_type
522 && (type1->tt_type == VAR_NUMBER
523 || type1->tt_type == VAR_LIST
524#ifdef FEAT_FLOAT
525 || type1->tt_type == VAR_FLOAT
526#endif
527 || type1->tt_type == VAR_BLOB))
528 vartype = type1->tt_type;
529
530 switch (*op)
531 {
532 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200533 && type1->tt_type != VAR_ANY
534 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 && check_number_or_float(
536 type1->tt_type, type2->tt_type, op) == FAIL)
537 return FAIL;
538 isn = generate_instr_drop(cctx,
539 vartype == VAR_NUMBER ? ISN_OPNR
540 : vartype == VAR_LIST ? ISN_ADDLIST
541 : vartype == VAR_BLOB ? ISN_ADDBLOB
542#ifdef FEAT_FLOAT
543 : vartype == VAR_FLOAT ? ISN_OPFLOAT
544#endif
545 : ISN_OPANY, 1);
546 if (isn != NULL)
547 isn->isn_arg.op.op_type = EXPR_ADD;
548 break;
549
550 case '-':
551 case '*':
552 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
553 op) == FAIL)
554 return FAIL;
555 if (vartype == VAR_NUMBER)
556 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
557#ifdef FEAT_FLOAT
558 else if (vartype == VAR_FLOAT)
559 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
560#endif
561 else
562 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
563 if (isn != NULL)
564 isn->isn_arg.op.op_type = *op == '*'
565 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
566 break;
567
Bram Moolenaar4c683752020-04-05 21:38:23 +0200568 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200570 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 && type2->tt_type != VAR_NUMBER))
572 {
573 emsg(_("E1035: % requires number arguments"));
574 return FAIL;
575 }
576 isn = generate_instr_drop(cctx,
577 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
578 if (isn != NULL)
579 isn->isn_arg.op.op_type = EXPR_REM;
580 break;
581 }
582
583 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200584 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 {
586 type_T *type = &t_any;
587
588#ifdef FEAT_FLOAT
589 // float+number and number+float results in float
590 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
591 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
592 type = &t_float;
593#endif
594 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
595 }
596
597 return OK;
598}
599
600/*
601 * Generate an ISN_COMPARE* instruction with a boolean result.
602 */
603 static int
604generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
605{
606 isntype_T isntype = ISN_DROP;
607 isn_T *isn;
608 garray_T *stack = &cctx->ctx_type_stack;
609 vartype_T type1;
610 vartype_T type2;
611
Bram Moolenaar080457c2020-03-03 21:53:32 +0100612 RETURN_OK_IF_SKIP(cctx);
613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 // Get the known type of the two items on the stack. If they are matching
615 // use a type-specific instruction. Otherwise fall back to runtime type
616 // checking.
617 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
618 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200619 if (type1 == VAR_UNKNOWN)
620 type1 = VAR_ANY;
621 if (type2 == VAR_UNKNOWN)
622 type2 = VAR_ANY;
623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 if (type1 == type2)
625 {
626 switch (type1)
627 {
628 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
629 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
630 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
631 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
632 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
633 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
634 case VAR_LIST: isntype = ISN_COMPARELIST; break;
635 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
636 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
637 case VAR_PARTIAL: isntype = ISN_COMPAREPARTIAL; break;
638 default: isntype = ISN_COMPAREANY; break;
639 }
640 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200641 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
643 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
644 isntype = ISN_COMPAREANY;
645
646 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
647 && (isntype == ISN_COMPAREBOOL
648 || isntype == ISN_COMPARESPECIAL
649 || isntype == ISN_COMPARENR
650 || isntype == ISN_COMPAREFLOAT))
651 {
652 semsg(_("E1037: Cannot use \"%s\" with %s"),
653 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
654 return FAIL;
655 }
656 if (isntype == ISN_DROP
657 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
658 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
659 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
660 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
661 && exptype != EXPR_IS && exptype != EXPR_ISNOT
662 && (type1 == VAR_BLOB || type2 == VAR_BLOB
663 || type1 == VAR_LIST || type2 == VAR_LIST))))
664 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100665 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 vartype_name(type1), vartype_name(type2));
667 return FAIL;
668 }
669
670 if ((isn = generate_instr(cctx, isntype)) == NULL)
671 return FAIL;
672 isn->isn_arg.op.op_type = exptype;
673 isn->isn_arg.op.op_ic = ic;
674
675 // takes two arguments, puts one bool back
676 if (stack->ga_len >= 2)
677 {
678 --stack->ga_len;
679 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
680 }
681
682 return OK;
683}
684
685/*
686 * Generate an ISN_2BOOL instruction.
687 */
688 static int
689generate_2BOOL(cctx_T *cctx, int invert)
690{
691 isn_T *isn;
692 garray_T *stack = &cctx->ctx_type_stack;
693
Bram Moolenaar080457c2020-03-03 21:53:32 +0100694 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
696 return FAIL;
697 isn->isn_arg.number = invert;
698
699 // type becomes bool
700 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
701
702 return OK;
703}
704
705 static int
706generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
707{
708 isn_T *isn;
709 garray_T *stack = &cctx->ctx_type_stack;
710
Bram Moolenaar080457c2020-03-03 21:53:32 +0100711 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100712 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
713 return FAIL;
714 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
715 isn->isn_arg.type.ct_off = offset;
716
717 // type becomes vartype
718 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
719
720 return OK;
721}
722
723/*
724 * Generate an ISN_PUSHNR instruction.
725 */
726 static int
727generate_PUSHNR(cctx_T *cctx, varnumber_T number)
728{
729 isn_T *isn;
730
Bram Moolenaar080457c2020-03-03 21:53:32 +0100731 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
733 return FAIL;
734 isn->isn_arg.number = number;
735
736 return OK;
737}
738
739/*
740 * Generate an ISN_PUSHBOOL instruction.
741 */
742 static int
743generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
744{
745 isn_T *isn;
746
Bram Moolenaar080457c2020-03-03 21:53:32 +0100747 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
749 return FAIL;
750 isn->isn_arg.number = number;
751
752 return OK;
753}
754
755/*
756 * Generate an ISN_PUSHSPEC instruction.
757 */
758 static int
759generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
760{
761 isn_T *isn;
762
Bram Moolenaar080457c2020-03-03 21:53:32 +0100763 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
765 return FAIL;
766 isn->isn_arg.number = number;
767
768 return OK;
769}
770
771#ifdef FEAT_FLOAT
772/*
773 * Generate an ISN_PUSHF instruction.
774 */
775 static int
776generate_PUSHF(cctx_T *cctx, float_T fnumber)
777{
778 isn_T *isn;
779
Bram Moolenaar080457c2020-03-03 21:53:32 +0100780 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
782 return FAIL;
783 isn->isn_arg.fnumber = fnumber;
784
785 return OK;
786}
787#endif
788
789/*
790 * Generate an ISN_PUSHS instruction.
791 * Consumes "str".
792 */
793 static int
794generate_PUSHS(cctx_T *cctx, char_u *str)
795{
796 isn_T *isn;
797
Bram Moolenaar080457c2020-03-03 21:53:32 +0100798 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
800 return FAIL;
801 isn->isn_arg.string = str;
802
803 return OK;
804}
805
806/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100807 * Generate an ISN_PUSHCHANNEL instruction.
808 * Consumes "channel".
809 */
810 static int
811generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
812{
813 isn_T *isn;
814
Bram Moolenaar080457c2020-03-03 21:53:32 +0100815 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100816 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
817 return FAIL;
818 isn->isn_arg.channel = channel;
819
820 return OK;
821}
822
823/*
824 * Generate an ISN_PUSHJOB instruction.
825 * Consumes "job".
826 */
827 static int
828generate_PUSHJOB(cctx_T *cctx, job_T *job)
829{
830 isn_T *isn;
831
Bram Moolenaar080457c2020-03-03 21:53:32 +0100832 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100833 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100834 return FAIL;
835 isn->isn_arg.job = job;
836
837 return OK;
838}
839
840/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 * Generate an ISN_PUSHBLOB instruction.
842 * Consumes "blob".
843 */
844 static int
845generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
846{
847 isn_T *isn;
848
Bram Moolenaar080457c2020-03-03 21:53:32 +0100849 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
851 return FAIL;
852 isn->isn_arg.blob = blob;
853
854 return OK;
855}
856
857/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100858 * Generate an ISN_PUSHFUNC instruction with name "name".
859 * Consumes "name".
860 */
861 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200862generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100863{
864 isn_T *isn;
865
Bram Moolenaar080457c2020-03-03 21:53:32 +0100866 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200867 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100868 return FAIL;
869 isn->isn_arg.string = name;
870
871 return OK;
872}
873
874/*
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100875 * Generate an ISN_PUSHPARTIAL instruction with partial "part".
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200876 * Consumes "part".
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100877 */
878 static int
879generate_PUSHPARTIAL(cctx_T *cctx, partial_T *part)
880{
881 isn_T *isn;
882
Bram Moolenaar080457c2020-03-03 21:53:32 +0100883 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaard77a8522020-04-03 21:59:57 +0200884 if ((isn = generate_instr_type(cctx, ISN_PUSHPARTIAL, &t_func_any)) == NULL)
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100885 return FAIL;
886 isn->isn_arg.partial = part;
887
888 return OK;
889}
890
891/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 * Generate an ISN_STORE instruction.
893 */
894 static int
895generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
896{
897 isn_T *isn;
898
Bram Moolenaar080457c2020-03-03 21:53:32 +0100899 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
901 return FAIL;
902 if (name != NULL)
903 isn->isn_arg.string = vim_strsave(name);
904 else
905 isn->isn_arg.number = idx;
906
907 return OK;
908}
909
910/*
911 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
912 */
913 static int
914generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
915{
916 isn_T *isn;
917
Bram Moolenaar080457c2020-03-03 21:53:32 +0100918 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
920 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100921 isn->isn_arg.storenr.stnr_idx = idx;
922 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923
924 return OK;
925}
926
927/*
928 * Generate an ISN_STOREOPT instruction
929 */
930 static int
931generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
932{
933 isn_T *isn;
934
Bram Moolenaar080457c2020-03-03 21:53:32 +0100935 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
937 return FAIL;
938 isn->isn_arg.storeopt.so_name = vim_strsave(name);
939 isn->isn_arg.storeopt.so_flags = opt_flags;
940
941 return OK;
942}
943
944/*
945 * Generate an ISN_LOAD or similar instruction.
946 */
947 static int
948generate_LOAD(
949 cctx_T *cctx,
950 isntype_T isn_type,
951 int idx,
952 char_u *name,
953 type_T *type)
954{
955 isn_T *isn;
956
Bram Moolenaar080457c2020-03-03 21:53:32 +0100957 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
959 return FAIL;
960 if (name != NULL)
961 isn->isn_arg.string = vim_strsave(name);
962 else
963 isn->isn_arg.number = idx;
964
965 return OK;
966}
967
968/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100969 * Generate an ISN_LOADV instruction.
970 */
971 static int
972generate_LOADV(
973 cctx_T *cctx,
974 char_u *name,
975 int error)
976{
977 // load v:var
978 int vidx = find_vim_var(name);
979
Bram Moolenaar080457c2020-03-03 21:53:32 +0100980 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100981 if (vidx < 0)
982 {
983 if (error)
984 semsg(_(e_var_notfound), name);
985 return FAIL;
986 }
987
988 // TODO: get actual type
989 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
990}
991
992/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 * Generate an ISN_LOADS instruction.
994 */
995 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100996generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100998 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001000 int sid,
1001 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002{
1003 isn_T *isn;
1004
Bram Moolenaar080457c2020-03-03 21:53:32 +01001005 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001006 if (isn_type == ISN_LOADS)
1007 isn = generate_instr_type(cctx, isn_type, type);
1008 else
1009 isn = generate_instr_drop(cctx, isn_type, 1);
1010 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001012 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1013 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014
1015 return OK;
1016}
1017
1018/*
1019 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1020 */
1021 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001022generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 cctx_T *cctx,
1024 isntype_T isn_type,
1025 int sid,
1026 int idx,
1027 type_T *type)
1028{
1029 isn_T *isn;
1030
Bram Moolenaar080457c2020-03-03 21:53:32 +01001031 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 if (isn_type == ISN_LOADSCRIPT)
1033 isn = generate_instr_type(cctx, isn_type, type);
1034 else
1035 isn = generate_instr_drop(cctx, isn_type, 1);
1036 if (isn == NULL)
1037 return FAIL;
1038 isn->isn_arg.script.script_sid = sid;
1039 isn->isn_arg.script.script_idx = idx;
1040 return OK;
1041}
1042
1043/*
1044 * Generate an ISN_NEWLIST instruction.
1045 */
1046 static int
1047generate_NEWLIST(cctx_T *cctx, int count)
1048{
1049 isn_T *isn;
1050 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 type_T *type;
1052 type_T *member;
1053
Bram Moolenaar080457c2020-03-03 21:53:32 +01001054 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1056 return FAIL;
1057 isn->isn_arg.number = count;
1058
1059 // drop the value types
1060 stack->ga_len -= count;
1061
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001062 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001063 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 if (count > 0)
1065 member = ((type_T **)stack->ga_data)[stack->ga_len];
1066 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001067 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001068 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069
1070 // add the list type to the type stack
1071 if (ga_grow(stack, 1) == FAIL)
1072 return FAIL;
1073 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1074 ++stack->ga_len;
1075
1076 return OK;
1077}
1078
1079/*
1080 * Generate an ISN_NEWDICT instruction.
1081 */
1082 static int
1083generate_NEWDICT(cctx_T *cctx, int count)
1084{
1085 isn_T *isn;
1086 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 type_T *type;
1088 type_T *member;
1089
Bram Moolenaar080457c2020-03-03 21:53:32 +01001090 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1092 return FAIL;
1093 isn->isn_arg.number = count;
1094
1095 // drop the key and value types
1096 stack->ga_len -= 2 * count;
1097
Bram Moolenaar436472f2020-02-20 22:54:43 +01001098 // Use the first value type for the list member type. Use "void" for an
1099 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 if (count > 0)
1101 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1102 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001103 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001104 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105
1106 // add the dict type to the type stack
1107 if (ga_grow(stack, 1) == FAIL)
1108 return FAIL;
1109 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1110 ++stack->ga_len;
1111
1112 return OK;
1113}
1114
1115/*
1116 * Generate an ISN_FUNCREF instruction.
1117 */
1118 static int
1119generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1120{
1121 isn_T *isn;
1122 garray_T *stack = &cctx->ctx_type_stack;
1123
Bram Moolenaar080457c2020-03-03 21:53:32 +01001124 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1126 return FAIL;
1127 isn->isn_arg.number = dfunc_idx;
1128
1129 if (ga_grow(stack, 1) == FAIL)
1130 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001131 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 // TODO: argument and return types
1133 ++stack->ga_len;
1134
1135 return OK;
1136}
1137
1138/*
1139 * Generate an ISN_JUMP instruction.
1140 */
1141 static int
1142generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1143{
1144 isn_T *isn;
1145 garray_T *stack = &cctx->ctx_type_stack;
1146
Bram Moolenaar080457c2020-03-03 21:53:32 +01001147 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1149 return FAIL;
1150 isn->isn_arg.jump.jump_when = when;
1151 isn->isn_arg.jump.jump_where = where;
1152
1153 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1154 --stack->ga_len;
1155
1156 return OK;
1157}
1158
1159 static int
1160generate_FOR(cctx_T *cctx, int loop_idx)
1161{
1162 isn_T *isn;
1163 garray_T *stack = &cctx->ctx_type_stack;
1164
Bram Moolenaar080457c2020-03-03 21:53:32 +01001165 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1167 return FAIL;
1168 isn->isn_arg.forloop.for_idx = loop_idx;
1169
1170 if (ga_grow(stack, 1) == FAIL)
1171 return FAIL;
1172 // type doesn't matter, will be stored next
1173 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1174 ++stack->ga_len;
1175
1176 return OK;
1177}
1178
1179/*
1180 * Generate an ISN_BCALL instruction.
1181 * Return FAIL if the number of arguments is wrong.
1182 */
1183 static int
1184generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1185{
1186 isn_T *isn;
1187 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001188 type_T *argtypes[MAX_FUNC_ARGS];
1189 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190
Bram Moolenaar080457c2020-03-03 21:53:32 +01001191 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 if (check_internal_func(func_idx, argcount) == FAIL)
1193 return FAIL;
1194
1195 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1196 return FAIL;
1197 isn->isn_arg.bfunc.cbf_idx = func_idx;
1198 isn->isn_arg.bfunc.cbf_argcount = argcount;
1199
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001200 for (i = 0; i < argcount; ++i)
1201 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1202
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 stack->ga_len -= argcount; // drop the arguments
1204 if (ga_grow(stack, 1) == FAIL)
1205 return FAIL;
1206 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001207 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 ++stack->ga_len; // add return value
1209
1210 return OK;
1211}
1212
1213/*
1214 * Generate an ISN_DCALL or ISN_UCALL instruction.
1215 * Return FAIL if the number of arguments is wrong.
1216 */
1217 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001218generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219{
1220 isn_T *isn;
1221 garray_T *stack = &cctx->ctx_type_stack;
1222 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001223 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224
Bram Moolenaar080457c2020-03-03 21:53:32 +01001225 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226 if (argcount > regular_args && !has_varargs(ufunc))
1227 {
1228 semsg(_(e_toomanyarg), ufunc->uf_name);
1229 return FAIL;
1230 }
1231 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1232 {
1233 semsg(_(e_toofewarg), ufunc->uf_name);
1234 return FAIL;
1235 }
1236
1237 // Turn varargs into a list.
1238 if (ufunc->uf_va_name != NULL)
1239 {
1240 int count = argcount - regular_args;
1241
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001242 // If count is negative an empty list will be added after evaluating
1243 // default values for missing optional arguments.
1244 if (count >= 0)
1245 {
1246 generate_NEWLIST(cctx, count);
1247 argcount = regular_args + 1;
1248 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 }
1250
1251 if ((isn = generate_instr(cctx,
1252 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1253 return FAIL;
1254 if (ufunc->uf_dfunc_idx >= 0)
1255 {
1256 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1257 isn->isn_arg.dfunc.cdf_argcount = argcount;
1258 }
1259 else
1260 {
1261 // A user function may be deleted and redefined later, can't use the
1262 // ufunc pointer, need to look it up again at runtime.
1263 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1264 isn->isn_arg.ufunc.cuf_argcount = argcount;
1265 }
1266
1267 stack->ga_len -= argcount; // drop the arguments
1268 if (ga_grow(stack, 1) == FAIL)
1269 return FAIL;
1270 // add return value
1271 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1272 ++stack->ga_len;
1273
1274 return OK;
1275}
1276
1277/*
1278 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1279 */
1280 static int
1281generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1282{
1283 isn_T *isn;
1284 garray_T *stack = &cctx->ctx_type_stack;
1285
Bram Moolenaar080457c2020-03-03 21:53:32 +01001286 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1288 return FAIL;
1289 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1290 isn->isn_arg.ufunc.cuf_argcount = argcount;
1291
1292 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001293 if (ga_grow(stack, 1) == FAIL)
1294 return FAIL;
1295 // add return value
1296 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1297 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298
1299 return OK;
1300}
1301
1302/*
1303 * Generate an ISN_PCALL instruction.
1304 */
1305 static int
1306generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1307{
1308 isn_T *isn;
1309 garray_T *stack = &cctx->ctx_type_stack;
1310
Bram Moolenaar080457c2020-03-03 21:53:32 +01001311 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1313 return FAIL;
1314 isn->isn_arg.pfunc.cpf_top = at_top;
1315 isn->isn_arg.pfunc.cpf_argcount = argcount;
1316
1317 stack->ga_len -= argcount; // drop the arguments
1318
1319 // drop the funcref/partial, get back the return value
1320 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1321
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001322 // If partial is above the arguments it must be cleared and replaced with
1323 // the return value.
1324 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1325 return FAIL;
1326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327 return OK;
1328}
1329
1330/*
1331 * Generate an ISN_MEMBER instruction.
1332 */
1333 static int
1334generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1335{
1336 isn_T *isn;
1337 garray_T *stack = &cctx->ctx_type_stack;
1338 type_T *type;
1339
Bram Moolenaar080457c2020-03-03 21:53:32 +01001340 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1342 return FAIL;
1343 isn->isn_arg.string = vim_strnsave(name, (int)len);
1344
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001345 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001347 if (type->tt_type != VAR_DICT && type != &t_any)
1348 {
1349 emsg(_(e_dictreq));
1350 return FAIL;
1351 }
1352 // change dict type to dict member type
1353 if (type->tt_type == VAR_DICT)
1354 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355
1356 return OK;
1357}
1358
1359/*
1360 * Generate an ISN_ECHO instruction.
1361 */
1362 static int
1363generate_ECHO(cctx_T *cctx, int with_white, int count)
1364{
1365 isn_T *isn;
1366
Bram Moolenaar080457c2020-03-03 21:53:32 +01001367 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1369 return FAIL;
1370 isn->isn_arg.echo.echo_with_white = with_white;
1371 isn->isn_arg.echo.echo_count = count;
1372
1373 return OK;
1374}
1375
Bram Moolenaarad39c092020-02-26 18:23:43 +01001376/*
1377 * Generate an ISN_EXECUTE instruction.
1378 */
1379 static int
1380generate_EXECUTE(cctx_T *cctx, int count)
1381{
1382 isn_T *isn;
1383
1384 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1385 return FAIL;
1386 isn->isn_arg.number = count;
1387
1388 return OK;
1389}
1390
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 static int
1392generate_EXEC(cctx_T *cctx, char_u *line)
1393{
1394 isn_T *isn;
1395
Bram Moolenaar080457c2020-03-03 21:53:32 +01001396 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1398 return FAIL;
1399 isn->isn_arg.string = vim_strsave(line);
1400 return OK;
1401}
1402
1403static char e_white_both[] =
1404 N_("E1004: white space required before and after '%s'");
Bram Moolenaard77a8522020-04-03 21:59:57 +02001405static char e_white_after[] = N_("E1069: white space required after '%s'");
1406static char e_no_white_before[] = N_("E1068: No white space allowed before '%s'");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407
1408/*
1409 * Reserve space for a local variable.
1410 * Return the index or -1 if it failed.
1411 */
1412 static int
1413reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1414{
1415 int idx;
1416 lvar_T *lvar;
1417
1418 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1419 {
1420 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1421 return -1;
1422 }
1423
1424 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1425 return -1;
1426 idx = cctx->ctx_locals.ga_len;
1427 if (cctx->ctx_max_local < idx + 1)
1428 cctx->ctx_max_local = idx + 1;
1429 ++cctx->ctx_locals.ga_len;
1430
1431 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1432 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1433 lvar->lv_const = isConst;
1434 lvar->lv_type = type;
1435
1436 return idx;
1437}
1438
1439/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001440 * Remove local variables above "new_top".
1441 */
1442 static void
1443unwind_locals(cctx_T *cctx, int new_top)
1444{
1445 if (cctx->ctx_locals.ga_len > new_top)
1446 {
1447 int idx;
1448 lvar_T *lvar;
1449
1450 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1451 {
1452 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1453 vim_free(lvar->lv_name);
1454 }
1455 }
1456 cctx->ctx_locals.ga_len = new_top;
1457}
1458
1459/*
1460 * Free all local variables.
1461 */
1462 static void
1463free_local(cctx_T *cctx)
1464{
1465 unwind_locals(cctx, 0);
1466 ga_clear(&cctx->ctx_locals);
1467}
1468
1469/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 * Skip over a type definition and return a pointer to just after it.
1471 */
1472 char_u *
1473skip_type(char_u *start)
1474{
1475 char_u *p = start;
1476
1477 while (ASCII_ISALNUM(*p) || *p == '_')
1478 ++p;
1479
1480 // Skip over "<type>"; this is permissive about white space.
1481 if (*skipwhite(p) == '<')
1482 {
1483 p = skipwhite(p);
1484 p = skip_type(skipwhite(p + 1));
1485 p = skipwhite(p);
1486 if (*p == '>')
1487 ++p;
1488 }
1489 return p;
1490}
1491
1492/*
1493 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001494 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 * Returns NULL in case of failure.
1496 */
1497 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001498parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499{
1500 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001501 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502
1503 if (**arg != '<')
1504 {
1505 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001506 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 else
1508 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001509 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 }
1511 *arg = skipwhite(*arg + 1);
1512
Bram Moolenaard77a8522020-04-03 21:59:57 +02001513 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514
1515 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001516 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517 {
1518 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001519 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 }
1521 ++*arg;
1522
1523 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001524 return get_list_type(member_type, type_gap);
1525 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526}
1527
1528/*
1529 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001530 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531 */
1532 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001533parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001534{
1535 char_u *p = *arg;
1536 size_t len;
1537
1538 // skip over the first word
1539 while (ASCII_ISALNUM(*p) || *p == '_')
1540 ++p;
1541 len = p - *arg;
1542
1543 switch (**arg)
1544 {
1545 case 'a':
1546 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1547 {
1548 *arg += len;
1549 return &t_any;
1550 }
1551 break;
1552 case 'b':
1553 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1554 {
1555 *arg += len;
1556 return &t_bool;
1557 }
1558 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1559 {
1560 *arg += len;
1561 return &t_blob;
1562 }
1563 break;
1564 case 'c':
1565 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1566 {
1567 *arg += len;
1568 return &t_channel;
1569 }
1570 break;
1571 case 'd':
1572 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1573 {
1574 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001575 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 }
1577 break;
1578 case 'f':
1579 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1580 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001581#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 *arg += len;
1583 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001584#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001585 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001586 return &t_any;
1587#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 }
1589 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1590 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001591 type_T *type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001592 type_T *ret_type = &t_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001593 int argcount = -1;
1594 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001595 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001596 type_T *arg_type[MAX_FUNC_ARGS + 1];
1597
1598 // func({type}, ...): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001600 if (**arg == '(')
1601 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001602 // "func" may or may not return a value, "func()" does
1603 // not return a value.
1604 ret_type = &t_void;
1605
Bram Moolenaard77a8522020-04-03 21:59:57 +02001606 p = ++*arg;
1607 argcount = 0;
1608 while (*p != NUL && *p != ')')
1609 {
1610 if (STRNCMP(p, "...", 3) == 0)
1611 {
1612 flags |= TTFLAG_VARARGS;
1613 break;
1614 }
1615 arg_type[argcount++] = parse_type(&p, type_gap);
1616
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001617 if (*p == '?')
1618 {
1619 if (first_optional == -1)
1620 first_optional = argcount;
1621 ++p;
1622 }
1623 else if (first_optional != -1)
1624 {
1625 emsg(_("E1007: mandatory argument after optional argument"));
1626 return &t_any;
1627 }
1628
Bram Moolenaard77a8522020-04-03 21:59:57 +02001629 if (*p != ',' && *skipwhite(p) == ',')
1630 {
1631 semsg(_(e_no_white_before), ",");
1632 return &t_any;
1633 }
1634 if (*p == ',')
1635 {
1636 ++p;
1637 if (!VIM_ISWHITE(*p))
1638 semsg(_(e_white_after), ",");
1639 }
1640 p = skipwhite(p);
1641 if (argcount == MAX_FUNC_ARGS)
1642 {
1643 emsg(_("E740: Too many argument types"));
1644 return &t_any;
1645 }
1646 }
1647
1648 p = skipwhite(p);
1649 if (*p != ')')
1650 {
1651 emsg(_(e_missing_close));
1652 return &t_any;
1653 }
1654 *arg = p + 1;
1655 }
1656 if (**arg == ':')
1657 {
1658 // parse return type
1659 ++*arg;
1660 if (!VIM_ISWHITE(*p))
1661 semsg(_(e_white_after), ":");
1662 *arg = skipwhite(*arg);
1663 ret_type = parse_type(arg, type_gap);
1664 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001665 type = get_func_type(ret_type,
1666 flags == 0 && first_optional == -1 ? argcount : 99,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001667 type_gap);
1668 if (flags != 0)
1669 type->tt_flags = flags;
1670 if (argcount > 0)
1671 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001672 if (func_type_add_arg_types(type, argcount,
1673 first_optional == -1 ? argcount : first_optional,
1674 type_gap) == FAIL)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001675 return &t_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001676 mch_memmove(type->tt_args, arg_type,
1677 sizeof(type_T *) * argcount);
1678 }
1679 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 }
1681 break;
1682 case 'j':
1683 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1684 {
1685 *arg += len;
1686 return &t_job;
1687 }
1688 break;
1689 case 'l':
1690 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1691 {
1692 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001693 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 }
1695 break;
1696 case 'n':
1697 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1698 {
1699 *arg += len;
1700 return &t_number;
1701 }
1702 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 case 's':
1704 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1705 {
1706 *arg += len;
1707 return &t_string;
1708 }
1709 break;
1710 case 'v':
1711 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1712 {
1713 *arg += len;
1714 return &t_void;
1715 }
1716 break;
1717 }
1718
1719 semsg(_("E1010: Type not recognized: %s"), *arg);
1720 return &t_any;
1721}
1722
1723/*
1724 * Check if "type1" and "type2" are exactly the same.
1725 */
1726 static int
1727equal_type(type_T *type1, type_T *type2)
1728{
1729 if (type1->tt_type != type2->tt_type)
1730 return FALSE;
1731 switch (type1->tt_type)
1732 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001734 case VAR_ANY:
1735 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 case VAR_SPECIAL:
1737 case VAR_BOOL:
1738 case VAR_NUMBER:
1739 case VAR_FLOAT:
1740 case VAR_STRING:
1741 case VAR_BLOB:
1742 case VAR_JOB:
1743 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001744 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 case VAR_LIST:
1746 case VAR_DICT:
1747 return equal_type(type1->tt_member, type2->tt_member);
1748 case VAR_FUNC:
1749 case VAR_PARTIAL:
1750 // TODO; check argument types.
1751 return equal_type(type1->tt_member, type2->tt_member)
1752 && type1->tt_argcount == type2->tt_argcount;
1753 }
1754 return TRUE;
1755}
1756
1757/*
1758 * Find the common type of "type1" and "type2" and put it in "dest".
1759 * "type2" and "dest" may be the same.
1760 */
1761 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02001762common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763{
1764 if (equal_type(type1, type2))
1765 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001766 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 return;
1768 }
1769
1770 if (type1->tt_type == type2->tt_type)
1771 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1773 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001774 type_T *common;
1775
Bram Moolenaard77a8522020-04-03 21:59:57 +02001776 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001777 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001778 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001779 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001780 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 return;
1782 }
1783 // TODO: VAR_FUNC and VAR_PARTIAL
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001784 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 }
1786
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001787 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788}
1789
1790 char *
1791vartype_name(vartype_T type)
1792{
1793 switch (type)
1794 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001795 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02001796 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 case VAR_SPECIAL: return "special";
1799 case VAR_BOOL: return "bool";
1800 case VAR_NUMBER: return "number";
1801 case VAR_FLOAT: return "float";
1802 case VAR_STRING: return "string";
1803 case VAR_BLOB: return "blob";
1804 case VAR_JOB: return "job";
1805 case VAR_CHANNEL: return "channel";
1806 case VAR_LIST: return "list";
1807 case VAR_DICT: return "dict";
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001808 case VAR_FUNC: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 case VAR_PARTIAL: return "partial";
1810 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02001811 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812}
1813
1814/*
1815 * Return the name of a type.
1816 * The result may be in allocated memory, in which case "tofree" is set.
1817 */
1818 char *
1819type_name(type_T *type, char **tofree)
1820{
1821 char *name = vartype_name(type->tt_type);
1822
1823 *tofree = NULL;
1824 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1825 {
1826 char *member_free;
1827 char *member_name = type_name(type->tt_member, &member_free);
1828 size_t len;
1829
1830 len = STRLEN(name) + STRLEN(member_name) + 3;
1831 *tofree = alloc(len);
1832 if (*tofree != NULL)
1833 {
1834 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1835 vim_free(member_free);
1836 return *tofree;
1837 }
1838 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001839 if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1840 {
1841 garray_T ga;
1842 int i;
1843
1844 ga_init2(&ga, 1, 100);
1845 if (ga_grow(&ga, 20) == FAIL)
1846 return "[unknown]";
1847 *tofree = ga.ga_data;
1848 STRCPY(ga.ga_data, "func(");
1849 ga.ga_len += 5;
1850
1851 for (i = 0; i < type->tt_argcount; ++i)
1852 {
1853 char *arg_free;
1854 char *arg_type = type_name(type->tt_args[i], &arg_free);
1855 int len;
1856
1857 if (i > 0)
1858 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001859 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001860 ga.ga_len += 2;
1861 }
1862 len = (int)STRLEN(arg_type);
1863 if (ga_grow(&ga, len + 6) == FAIL)
1864 {
1865 vim_free(arg_free);
1866 return "[unknown]";
1867 }
1868 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001869 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001870 ga.ga_len += len;
1871 vim_free(arg_free);
1872 }
1873
1874 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001875 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001876 else
1877 {
1878 char *ret_free;
1879 char *ret_name = type_name(type->tt_member, &ret_free);
1880 int len;
1881
1882 len = (int)STRLEN(ret_name) + 4;
1883 if (ga_grow(&ga, len) == FAIL)
1884 {
1885 vim_free(ret_free);
1886 return "[unknown]";
1887 }
1888 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001889 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
1890 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001891 vim_free(ret_free);
1892 }
1893 return ga.ga_data;
1894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895
1896 return name;
1897}
1898
1899/*
1900 * Find "name" in script-local items of script "sid".
1901 * Returns the index in "sn_var_vals" if found.
1902 * If found but not in "sn_var_vals" returns -1.
1903 * If not found returns -2.
1904 */
1905 int
1906get_script_item_idx(int sid, char_u *name, int check_writable)
1907{
1908 hashtab_T *ht;
1909 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001910 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001911 int idx;
1912
1913 // First look the name up in the hashtable.
1914 if (sid <= 0 || sid > script_items.ga_len)
1915 return -1;
1916 ht = &SCRIPT_VARS(sid);
1917 di = find_var_in_ht(ht, 0, name, TRUE);
1918 if (di == NULL)
1919 return -2;
1920
1921 // Now find the svar_T index in sn_var_vals.
1922 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1923 {
1924 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1925
1926 if (sv->sv_tv == &di->di_tv)
1927 {
1928 if (check_writable && sv->sv_const)
1929 semsg(_(e_readonlyvar), name);
1930 return idx;
1931 }
1932 }
1933 return -1;
1934}
1935
1936/*
1937 * Find "name" in imported items of the current script/
1938 */
1939 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001940find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001942 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 int idx;
1944
1945 if (cctx != NULL)
1946 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1947 {
1948 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1949 + idx;
1950
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001951 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1952 : STRLEN(import->imp_name) == len
1953 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954 return import;
1955 }
1956
1957 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1958 {
1959 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1960
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001961 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1962 : STRLEN(import->imp_name) == len
1963 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 return import;
1965 }
1966 return NULL;
1967}
1968
1969/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001970 * Free all imported variables.
1971 */
1972 static void
1973free_imported(cctx_T *cctx)
1974{
1975 int idx;
1976
1977 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1978 {
1979 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1980
1981 vim_free(import->imp_name);
1982 }
1983 ga_clear(&cctx->ctx_imports);
1984}
1985
1986/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001987 * Generate an instruction to load script-local variable "name", without the
1988 * leading "s:".
1989 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 */
1991 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001992compile_load_scriptvar(
1993 cctx_T *cctx,
1994 char_u *name, // variable NUL terminated
1995 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001996 char_u **end, // end of variable
1997 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001999 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2001 imported_T *import;
2002
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002003 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002005 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002006 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2007 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 }
2009 if (idx >= 0)
2010 {
2011 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2012
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002013 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 current_sctx.sc_sid, idx, sv->sv_type);
2015 return OK;
2016 }
2017
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002018 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 if (import != NULL)
2020 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002021 if (import->imp_all)
2022 {
2023 char_u *p = skipwhite(*end);
2024 int name_len;
2025 ufunc_T *ufunc;
2026 type_T *type;
2027
2028 // Used "import * as Name", need to lookup the member.
2029 if (*p != '.')
2030 {
2031 semsg(_("E1060: expected dot after name: %s"), start);
2032 return FAIL;
2033 }
2034 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002035 if (VIM_ISWHITE(*p))
2036 {
2037 emsg(_("E1074: no white space allowed after dot"));
2038 return FAIL;
2039 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002040
2041 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2042 // TODO: what if it is a function?
2043 if (idx < 0)
2044 return FAIL;
2045 *end = p;
2046
2047 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2048 import->imp_sid,
2049 idx,
2050 type);
2051 }
2052 else
2053 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002054 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002055 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2056 import->imp_sid,
2057 import->imp_var_vals_idx,
2058 import->imp_type);
2059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 return OK;
2061 }
2062
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002063 if (error)
2064 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 return FAIL;
2066}
2067
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002068 static int
2069generate_funcref(cctx_T *cctx, char_u *name)
2070{
2071 ufunc_T *ufunc = find_func(name, cctx);
2072
2073 if (ufunc == NULL)
2074 return FAIL;
2075
2076 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2077}
2078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079/*
2080 * Compile a variable name into a load instruction.
2081 * "end" points to just after the name.
2082 * When "error" is FALSE do not give an error when not found.
2083 */
2084 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002085compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086{
2087 type_T *type;
2088 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002089 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002091 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092
2093 if (*(*arg + 1) == ':')
2094 {
2095 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002096 if (end <= *arg + 2)
2097 name = vim_strsave((char_u *)"[empty]");
2098 else
2099 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 if (name == NULL)
2101 return FAIL;
2102
2103 if (**arg == 'v')
2104 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002105 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 }
2107 else if (**arg == 'g')
2108 {
2109 // Global variables can be defined later, thus we don't check if it
2110 // exists, give error at runtime.
2111 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2112 }
2113 else if (**arg == 's')
2114 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002115 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002117 else if (**arg == 'b')
2118 {
2119 semsg("Namespace b: not supported yet: %s", *arg);
2120 goto theend;
2121 }
2122 else if (**arg == 'w')
2123 {
2124 semsg("Namespace w: not supported yet: %s", *arg);
2125 goto theend;
2126 }
2127 else if (**arg == 't')
2128 {
2129 semsg("Namespace t: not supported yet: %s", *arg);
2130 goto theend;
2131 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 else
2133 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002134 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 goto theend;
2136 }
2137 }
2138 else
2139 {
2140 size_t len = end - *arg;
2141 int idx;
2142 int gen_load = FALSE;
2143
2144 name = vim_strnsave(*arg, end - *arg);
2145 if (name == NULL)
2146 return FAIL;
2147
2148 idx = lookup_arg(*arg, len, cctx);
2149 if (idx >= 0)
2150 {
2151 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2152 type = cctx->ctx_ufunc->uf_arg_types[idx];
2153 else
2154 type = &t_any;
2155
2156 // Arguments are located above the frame pointer.
2157 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2158 if (cctx->ctx_ufunc->uf_va_name != NULL)
2159 --idx;
2160 gen_load = TRUE;
2161 }
2162 else if (lookup_vararg(*arg, len, cctx))
2163 {
2164 // varargs is always the last argument
2165 idx = -STACK_FRAME_SIZE - 1;
2166 type = cctx->ctx_ufunc->uf_va_type;
2167 gen_load = TRUE;
2168 }
2169 else
2170 {
2171 idx = lookup_local(*arg, len, cctx);
2172 if (idx >= 0)
2173 {
2174 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
2175 gen_load = TRUE;
2176 }
2177 else
2178 {
2179 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2180 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2181 res = generate_PUSHBOOL(cctx, **arg == 't'
2182 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002183 else
2184 {
2185 // "var" can be script-local even without using "s:" if it
2186 // already exists.
2187 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2188 == SCRIPT_VERSION_VIM9
2189 || lookup_script(*arg, len) == OK)
2190 res = compile_load_scriptvar(cctx, name, *arg, &end,
2191 FALSE);
2192
2193 // When the name starts with an uppercase letter or "x:" it
2194 // can be a user defined function.
2195 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2196 res = generate_funcref(cctx, name);
2197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 }
2199 }
2200 if (gen_load)
2201 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
2202 }
2203
2204 *arg = end;
2205
2206theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002207 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 semsg(_(e_var_notfound), name);
2209 vim_free(name);
2210 return res;
2211}
2212
2213/*
2214 * Compile the argument expressions.
2215 * "arg" points to just after the "(" and is advanced to after the ")"
2216 */
2217 static int
2218compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2219{
2220 char_u *p = *arg;
2221
2222 while (*p != NUL && *p != ')')
2223 {
2224 if (compile_expr1(&p, cctx) == FAIL)
2225 return FAIL;
2226 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002227
2228 if (*p != ',' && *skipwhite(p) == ',')
2229 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002230 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002231 p = skipwhite(p);
2232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002234 {
2235 ++p;
2236 if (!VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002237 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002238 }
2239 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002241 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 if (*p != ')')
2243 {
2244 emsg(_(e_missing_close));
2245 return FAIL;
2246 }
2247 *arg = p + 1;
2248 return OK;
2249}
2250
2251/*
2252 * Compile a function call: name(arg1, arg2)
2253 * "arg" points to "name", "arg + varlen" to the "(".
2254 * "argcount_init" is 1 for "value->method()"
2255 * Instructions:
2256 * EVAL arg1
2257 * EVAL arg2
2258 * BCALL / DCALL / UCALL
2259 */
2260 static int
2261compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2262{
2263 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002264 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265 int argcount = argcount_init;
2266 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002267 char_u fname_buf[FLEN_FIXED + 1];
2268 char_u *tofree = NULL;
2269 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002271 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272
2273 if (varlen >= sizeof(namebuf))
2274 {
2275 semsg(_("E1011: name too long: %s"), name);
2276 return FAIL;
2277 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002278 vim_strncpy(namebuf, *arg, varlen);
2279 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280
2281 *arg = skipwhite(*arg + varlen + 1);
2282 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002283 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002285 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 {
2287 int idx;
2288
2289 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002290 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002292 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002293 else
2294 semsg(_(e_unknownfunc), namebuf);
2295 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 }
2297
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002299 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002301 {
2302 res = generate_CALL(cctx, ufunc, argcount);
2303 goto theend;
2304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305
2306 // If the name is a variable, load it and use PCALL.
2307 p = namebuf;
2308 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002309 {
2310 res = generate_PCALL(cctx, argcount, FALSE);
2311 goto theend;
2312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313
2314 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002315 res = generate_UCALL(cctx, name, argcount);
2316
2317theend:
2318 vim_free(tofree);
2319 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320}
2321
2322// like NAMESPACE_CHAR but with 'a' and 'l'.
2323#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2324
2325/*
2326 * Find the end of a variable or function name. Unlike find_name_end() this
2327 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002328 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 * Return a pointer to just after the name. Equal to "arg" if there is no
2330 * valid name.
2331 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002332 static char_u *
2333to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334{
2335 char_u *p;
2336
2337 // Quick check for valid starting character.
2338 if (!eval_isnamec1(*arg))
2339 return arg;
2340
2341 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2342 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2343 // and can be used in slice "[n:]".
2344 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002345 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2347 break;
2348 return p;
2349}
2350
2351/*
2352 * Like to_name_end() but also skip over a list or dict constant.
2353 */
2354 char_u *
2355to_name_const_end(char_u *arg)
2356{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002357 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358 typval_T rettv;
2359
2360 if (p == arg && *arg == '[')
2361 {
2362
2363 // Can be "[1, 2, 3]->Func()".
2364 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2365 p = arg;
2366 }
2367 else if (p == arg && *arg == '#' && arg[1] == '{')
2368 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002369 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 ++p;
2371 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2372 p = arg;
2373 }
2374 else if (p == arg && *arg == '{')
2375 {
2376 int ret = get_lambda_tv(&p, &rettv, FALSE);
2377
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002378 // Can be "{x -> ret}()".
2379 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 if (ret == NOTDONE)
2381 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2382 if (ret != OK)
2383 p = arg;
2384 }
2385
2386 return p;
2387}
2388
2389 static void
2390type_mismatch(type_T *expected, type_T *actual)
2391{
2392 char *tofree1, *tofree2;
2393
2394 semsg(_("E1013: type mismatch, expected %s but got %s"),
2395 type_name(expected, &tofree1), type_name(actual, &tofree2));
2396 vim_free(tofree1);
2397 vim_free(tofree2);
2398}
2399
2400/*
2401 * Check if the expected and actual types match.
2402 */
2403 static int
2404check_type(type_T *expected, type_T *actual, int give_msg)
2405{
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002406 int ret = OK;
2407
Bram Moolenaar4c683752020-04-05 21:38:23 +02002408 if (expected->tt_type != VAR_UNKNOWN && expected->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 {
2410 if (expected->tt_type != actual->tt_type)
2411 {
2412 if (give_msg)
2413 type_mismatch(expected, actual);
2414 return FAIL;
2415 }
2416 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2417 {
Bram Moolenaar4c683752020-04-05 21:38:23 +02002418 // "unknown" is used for an empty list or dict
2419 if (actual->tt_member != &t_unknown)
Bram Moolenaar436472f2020-02-20 22:54:43 +01002420 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002422 else if (expected->tt_type == VAR_FUNC)
2423 {
Bram Moolenaar4c683752020-04-05 21:38:23 +02002424 if (expected->tt_member != &t_any
2425 && expected->tt_member != &t_unknown)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002426 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
2427 if (ret == OK && expected->tt_argcount != -1
2428 && (actual->tt_argcount < expected->tt_min_argcount
2429 || actual->tt_argcount > expected->tt_argcount))
2430 ret = FAIL;
2431 }
2432 if (ret == FAIL && give_msg)
2433 type_mismatch(expected, actual);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 }
2435 return OK;
2436}
2437
2438/*
2439 * Check that
2440 * - "actual" is "expected" type or
2441 * - "actual" is a type that can be "expected" type: add a runtime check; or
2442 * - return FAIL.
2443 */
2444 static int
2445need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2446{
Bram Moolenaar436472f2020-02-20 22:54:43 +01002447 if (check_type(expected, actual, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 return OK;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002449 if (actual->tt_type != VAR_ANY && actual->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 {
2451 type_mismatch(expected, actual);
2452 return FAIL;
2453 }
2454 generate_TYPECHECK(cctx, expected, offset);
2455 return OK;
2456}
2457
2458/*
2459 * parse a list: [expr, expr]
2460 * "*arg" points to the '['.
2461 */
2462 static int
2463compile_list(char_u **arg, cctx_T *cctx)
2464{
2465 char_u *p = skipwhite(*arg + 1);
2466 int count = 0;
2467
2468 while (*p != ']')
2469 {
2470 if (*p == NUL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002471 {
2472 semsg(_(e_list_end), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 return FAIL;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002474 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 if (compile_expr1(&p, cctx) == FAIL)
2476 break;
2477 ++count;
2478 if (*p == ',')
2479 ++p;
2480 p = skipwhite(p);
2481 }
2482 *arg = p + 1;
2483
2484 generate_NEWLIST(cctx, count);
2485 return OK;
2486}
2487
2488/*
2489 * parse a lambda: {arg, arg -> expr}
2490 * "*arg" points to the '{'.
2491 */
2492 static int
2493compile_lambda(char_u **arg, cctx_T *cctx)
2494{
2495 garray_T *instr = &cctx->ctx_instr;
2496 typval_T rettv;
2497 ufunc_T *ufunc;
2498
2499 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002500 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002504 ++ufunc->uf_refcount;
2505 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002506 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507
2508 // The function will have one line: "return {expr}".
2509 // Compile it into instructions.
2510 compile_def_function(ufunc, TRUE);
2511
2512 if (ufunc->uf_dfunc_idx >= 0)
2513 {
2514 if (ga_grow(instr, 1) == FAIL)
2515 return FAIL;
2516 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2517 return OK;
2518 }
2519 return FAIL;
2520}
2521
2522/*
2523 * Compile a lamda call: expr->{lambda}(args)
2524 * "arg" points to the "{".
2525 */
2526 static int
2527compile_lambda_call(char_u **arg, cctx_T *cctx)
2528{
2529 ufunc_T *ufunc;
2530 typval_T rettv;
2531 int argcount = 1;
2532 int ret = FAIL;
2533
2534 // Get the funcref in "rettv".
2535 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2536 return FAIL;
2537
2538 if (**arg != '(')
2539 {
2540 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002541 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 else
2543 semsg(_(e_missing_paren), "lambda");
2544 clear_tv(&rettv);
2545 return FAIL;
2546 }
2547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 ufunc = rettv.vval.v_partial->pt_func;
2549 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002550 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002551 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002552
2553 // The function will have one line: "return {expr}".
2554 // Compile it into instructions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 compile_def_function(ufunc, TRUE);
2556
2557 // compile the arguments
2558 *arg = skipwhite(*arg + 1);
2559 if (compile_arguments(arg, cctx, &argcount) == OK)
2560 // call the compiled function
2561 ret = generate_CALL(cctx, ufunc, argcount);
2562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 return ret;
2564}
2565
2566/*
2567 * parse a dict: {'key': val} or #{key: val}
2568 * "*arg" points to the '{'.
2569 */
2570 static int
2571compile_dict(char_u **arg, cctx_T *cctx, int literal)
2572{
2573 garray_T *instr = &cctx->ctx_instr;
2574 int count = 0;
2575 dict_T *d = dict_alloc();
2576 dictitem_T *item;
2577
2578 if (d == NULL)
2579 return FAIL;
2580 *arg = skipwhite(*arg + 1);
2581 while (**arg != '}' && **arg != NUL)
2582 {
2583 char_u *key = NULL;
2584
2585 if (literal)
2586 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002587 char_u *p = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588
2589 if (p == *arg)
2590 {
2591 semsg(_("E1014: Invalid key: %s"), *arg);
2592 return FAIL;
2593 }
2594 key = vim_strnsave(*arg, p - *arg);
2595 if (generate_PUSHS(cctx, key) == FAIL)
2596 return FAIL;
2597 *arg = p;
2598 }
2599 else
2600 {
2601 isn_T *isn;
2602
2603 if (compile_expr1(arg, cctx) == FAIL)
2604 return FAIL;
2605 // TODO: check type is string
2606 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2607 if (isn->isn_type == ISN_PUSHS)
2608 key = isn->isn_arg.string;
2609 }
2610
2611 // Check for duplicate keys, if using string keys.
2612 if (key != NULL)
2613 {
2614 item = dict_find(d, key, -1);
2615 if (item != NULL)
2616 {
2617 semsg(_(e_duplicate_key), key);
2618 goto failret;
2619 }
2620 item = dictitem_alloc(key);
2621 if (item != NULL)
2622 {
2623 item->di_tv.v_type = VAR_UNKNOWN;
2624 item->di_tv.v_lock = 0;
2625 if (dict_add(d, item) == FAIL)
2626 dictitem_free(item);
2627 }
2628 }
2629
2630 *arg = skipwhite(*arg);
2631 if (**arg != ':')
2632 {
2633 semsg(_(e_missing_dict_colon), *arg);
2634 return FAIL;
2635 }
2636
2637 *arg = skipwhite(*arg + 1);
2638 if (compile_expr1(arg, cctx) == FAIL)
2639 return FAIL;
2640 ++count;
2641
2642 if (**arg == '}')
2643 break;
2644 if (**arg != ',')
2645 {
2646 semsg(_(e_missing_dict_comma), *arg);
2647 goto failret;
2648 }
2649 *arg = skipwhite(*arg + 1);
2650 }
2651
2652 if (**arg != '}')
2653 {
2654 semsg(_(e_missing_dict_end), *arg);
2655 goto failret;
2656 }
2657 *arg = *arg + 1;
2658
2659 dict_unref(d);
2660 return generate_NEWDICT(cctx, count);
2661
2662failret:
2663 dict_unref(d);
2664 return FAIL;
2665}
2666
2667/*
2668 * Compile "&option".
2669 */
2670 static int
2671compile_get_option(char_u **arg, cctx_T *cctx)
2672{
2673 typval_T rettv;
2674 char_u *start = *arg;
2675 int ret;
2676
2677 // parse the option and get the current value to get the type.
2678 rettv.v_type = VAR_UNKNOWN;
2679 ret = get_option_tv(arg, &rettv, TRUE);
2680 if (ret == OK)
2681 {
2682 // include the '&' in the name, get_option_tv() expects it.
2683 char_u *name = vim_strnsave(start, *arg - start);
2684 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2685
2686 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2687 vim_free(name);
2688 }
2689 clear_tv(&rettv);
2690
2691 return ret;
2692}
2693
2694/*
2695 * Compile "$VAR".
2696 */
2697 static int
2698compile_get_env(char_u **arg, cctx_T *cctx)
2699{
2700 char_u *start = *arg;
2701 int len;
2702 int ret;
2703 char_u *name;
2704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 ++*arg;
2706 len = get_env_len(arg);
2707 if (len == 0)
2708 {
2709 semsg(_(e_syntax_at), start - 1);
2710 return FAIL;
2711 }
2712
2713 // include the '$' in the name, get_env_tv() expects it.
2714 name = vim_strnsave(start, len + 1);
2715 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2716 vim_free(name);
2717 return ret;
2718}
2719
2720/*
2721 * Compile "@r".
2722 */
2723 static int
2724compile_get_register(char_u **arg, cctx_T *cctx)
2725{
2726 int ret;
2727
2728 ++*arg;
2729 if (**arg == NUL)
2730 {
2731 semsg(_(e_syntax_at), *arg - 1);
2732 return FAIL;
2733 }
2734 if (!valid_yank_reg(**arg, TRUE))
2735 {
2736 emsg_invreg(**arg);
2737 return FAIL;
2738 }
2739 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2740 ++*arg;
2741 return ret;
2742}
2743
2744/*
2745 * Apply leading '!', '-' and '+' to constant "rettv".
2746 */
2747 static int
2748apply_leader(typval_T *rettv, char_u *start, char_u *end)
2749{
2750 char_u *p = end;
2751
2752 // this works from end to start
2753 while (p > start)
2754 {
2755 --p;
2756 if (*p == '-' || *p == '+')
2757 {
2758 // only '-' has an effect, for '+' we only check the type
2759#ifdef FEAT_FLOAT
2760 if (rettv->v_type == VAR_FLOAT)
2761 {
2762 if (*p == '-')
2763 rettv->vval.v_float = -rettv->vval.v_float;
2764 }
2765 else
2766#endif
2767 {
2768 varnumber_T val;
2769 int error = FALSE;
2770
2771 // tv_get_number_chk() accepts a string, but we don't want that
2772 // here
2773 if (check_not_string(rettv) == FAIL)
2774 return FAIL;
2775 val = tv_get_number_chk(rettv, &error);
2776 clear_tv(rettv);
2777 if (error)
2778 return FAIL;
2779 if (*p == '-')
2780 val = -val;
2781 rettv->v_type = VAR_NUMBER;
2782 rettv->vval.v_number = val;
2783 }
2784 }
2785 else
2786 {
2787 int v = tv2bool(rettv);
2788
2789 // '!' is permissive in the type.
2790 clear_tv(rettv);
2791 rettv->v_type = VAR_BOOL;
2792 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2793 }
2794 }
2795 return OK;
2796}
2797
2798/*
2799 * Recognize v: variables that are constants and set "rettv".
2800 */
2801 static void
2802get_vim_constant(char_u **arg, typval_T *rettv)
2803{
2804 if (STRNCMP(*arg, "v:true", 6) == 0)
2805 {
2806 rettv->v_type = VAR_BOOL;
2807 rettv->vval.v_number = VVAL_TRUE;
2808 *arg += 6;
2809 }
2810 else if (STRNCMP(*arg, "v:false", 7) == 0)
2811 {
2812 rettv->v_type = VAR_BOOL;
2813 rettv->vval.v_number = VVAL_FALSE;
2814 *arg += 7;
2815 }
2816 else if (STRNCMP(*arg, "v:null", 6) == 0)
2817 {
2818 rettv->v_type = VAR_SPECIAL;
2819 rettv->vval.v_number = VVAL_NULL;
2820 *arg += 6;
2821 }
2822 else if (STRNCMP(*arg, "v:none", 6) == 0)
2823 {
2824 rettv->v_type = VAR_SPECIAL;
2825 rettv->vval.v_number = VVAL_NONE;
2826 *arg += 6;
2827 }
2828}
2829
2830/*
2831 * Compile code to apply '-', '+' and '!'.
2832 */
2833 static int
2834compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2835{
2836 char_u *p = end;
2837
2838 // this works from end to start
2839 while (p > start)
2840 {
2841 --p;
2842 if (*p == '-' || *p == '+')
2843 {
2844 int negate = *p == '-';
2845 isn_T *isn;
2846
2847 // TODO: check type
2848 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2849 {
2850 --p;
2851 if (*p == '-')
2852 negate = !negate;
2853 }
2854 // only '-' has an effect, for '+' we only check the type
2855 if (negate)
2856 isn = generate_instr(cctx, ISN_NEGATENR);
2857 else
2858 isn = generate_instr(cctx, ISN_CHECKNR);
2859 if (isn == NULL)
2860 return FAIL;
2861 }
2862 else
2863 {
2864 int invert = TRUE;
2865
2866 while (p > start && p[-1] == '!')
2867 {
2868 --p;
2869 invert = !invert;
2870 }
2871 if (generate_2BOOL(cctx, invert) == FAIL)
2872 return FAIL;
2873 }
2874 }
2875 return OK;
2876}
2877
2878/*
2879 * Compile whatever comes after "name" or "name()".
2880 */
2881 static int
2882compile_subscript(
2883 char_u **arg,
2884 cctx_T *cctx,
2885 char_u **start_leader,
2886 char_u *end_leader)
2887{
2888 for (;;)
2889 {
2890 if (**arg == '(')
2891 {
2892 int argcount = 0;
2893
2894 // funcref(arg)
2895 *arg = skipwhite(*arg + 1);
2896 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2897 return FAIL;
2898 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2899 return FAIL;
2900 }
2901 else if (**arg == '-' && (*arg)[1] == '>')
2902 {
2903 char_u *p;
2904
2905 // something->method()
2906 // Apply the '!', '-' and '+' first:
2907 // -1.0->func() works like (-1.0)->func()
2908 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2909 return FAIL;
2910 *start_leader = end_leader; // don't apply again later
2911
2912 *arg = skipwhite(*arg + 2);
2913 if (**arg == '{')
2914 {
2915 // lambda call: list->{lambda}
2916 if (compile_lambda_call(arg, cctx) == FAIL)
2917 return FAIL;
2918 }
2919 else
2920 {
2921 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002922 p = *arg;
2923 if (ASCII_ISALPHA(*p) && p[1] == ':')
2924 p += 2;
2925 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 ;
2927 if (*p != '(')
2928 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002929 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 return FAIL;
2931 }
2932 // TODO: base value may not be the first argument
2933 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2934 return FAIL;
2935 }
2936 }
2937 else if (**arg == '[')
2938 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002939 garray_T *stack;
2940 type_T **typep;
2941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 // list index: list[123]
2943 // TODO: more arguments
2944 // TODO: dict member dict['name']
2945 *arg = skipwhite(*arg + 1);
2946 if (compile_expr1(arg, cctx) == FAIL)
2947 return FAIL;
2948
2949 if (**arg != ']')
2950 {
2951 emsg(_(e_missbrac));
2952 return FAIL;
2953 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002954 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955
2956 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2957 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01002958 stack = &cctx->ctx_type_stack;
2959 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2960 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
2961 {
2962 emsg(_(e_listreq));
2963 return FAIL;
2964 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002965 if ((*typep)->tt_type == VAR_LIST)
2966 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 }
2968 else if (**arg == '.' && (*arg)[1] != '.')
2969 {
2970 char_u *p;
2971
2972 ++*arg;
2973 p = *arg;
2974 // dictionary member: dict.name
2975 if (eval_isnamec1(*p))
2976 while (eval_isnamec(*p))
2977 MB_PTR_ADV(p);
2978 if (p == *arg)
2979 {
2980 semsg(_(e_syntax_at), *arg);
2981 return FAIL;
2982 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2984 return FAIL;
2985 *arg = p;
2986 }
2987 else
2988 break;
2989 }
2990
2991 // TODO - see handle_subscript():
2992 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2993 // Don't do this when "Func" is already a partial that was bound
2994 // explicitly (pt_auto is FALSE).
2995
2996 return OK;
2997}
2998
2999/*
3000 * Compile an expression at "*p" and add instructions to "instr".
3001 * "p" is advanced until after the expression, skipping white space.
3002 *
3003 * This is the equivalent of eval1(), eval2(), etc.
3004 */
3005
3006/*
3007 * number number constant
3008 * 0zFFFFFFFF Blob constant
3009 * "string" string constant
3010 * 'string' literal string constant
3011 * &option-name option value
3012 * @r register contents
3013 * identifier variable value
3014 * function() function call
3015 * $VAR environment variable
3016 * (expression) nested expression
3017 * [expr, expr] List
3018 * {key: val, key: val} Dictionary
3019 * #{key: val, key: val} Dictionary with literal keys
3020 *
3021 * Also handle:
3022 * ! in front logical NOT
3023 * - in front unary minus
3024 * + in front unary plus (ignored)
3025 * trailing (arg) funcref/partial call
3026 * trailing [] subscript in String or List
3027 * trailing .name entry in Dictionary
3028 * trailing ->name() method call
3029 */
3030 static int
3031compile_expr7(char_u **arg, cctx_T *cctx)
3032{
3033 typval_T rettv;
3034 char_u *start_leader, *end_leader;
3035 int ret = OK;
3036
3037 /*
3038 * Skip '!', '-' and '+' characters. They are handled later.
3039 */
3040 start_leader = *arg;
3041 while (**arg == '!' || **arg == '-' || **arg == '+')
3042 *arg = skipwhite(*arg + 1);
3043 end_leader = *arg;
3044
3045 rettv.v_type = VAR_UNKNOWN;
3046 switch (**arg)
3047 {
3048 /*
3049 * Number constant.
3050 */
3051 case '0': // also for blob starting with 0z
3052 case '1':
3053 case '2':
3054 case '3':
3055 case '4':
3056 case '5':
3057 case '6':
3058 case '7':
3059 case '8':
3060 case '9':
3061 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3062 return FAIL;
3063 break;
3064
3065 /*
3066 * String constant: "string".
3067 */
3068 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3069 return FAIL;
3070 break;
3071
3072 /*
3073 * Literal string constant: 'str''ing'.
3074 */
3075 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3076 return FAIL;
3077 break;
3078
3079 /*
3080 * Constant Vim variable.
3081 */
3082 case 'v': get_vim_constant(arg, &rettv);
3083 ret = NOTDONE;
3084 break;
3085
3086 /*
3087 * List: [expr, expr]
3088 */
3089 case '[': ret = compile_list(arg, cctx);
3090 break;
3091
3092 /*
3093 * Dictionary: #{key: val, key: val}
3094 */
3095 case '#': if ((*arg)[1] == '{')
3096 {
3097 ++*arg;
3098 ret = compile_dict(arg, cctx, TRUE);
3099 }
3100 else
3101 ret = NOTDONE;
3102 break;
3103
3104 /*
3105 * Lambda: {arg, arg -> expr}
3106 * Dictionary: {'key': val, 'key': val}
3107 */
3108 case '{': {
3109 char_u *start = skipwhite(*arg + 1);
3110
3111 // Find out what comes after the arguments.
3112 ret = get_function_args(&start, '-', NULL,
3113 NULL, NULL, NULL, TRUE);
3114 if (ret != FAIL && *start == '>')
3115 ret = compile_lambda(arg, cctx);
3116 else
3117 ret = compile_dict(arg, cctx, FALSE);
3118 }
3119 break;
3120
3121 /*
3122 * Option value: &name
3123 */
3124 case '&': ret = compile_get_option(arg, cctx);
3125 break;
3126
3127 /*
3128 * Environment variable: $VAR.
3129 */
3130 case '$': ret = compile_get_env(arg, cctx);
3131 break;
3132
3133 /*
3134 * Register contents: @r.
3135 */
3136 case '@': ret = compile_get_register(arg, cctx);
3137 break;
3138 /*
3139 * nested expression: (expression).
3140 */
3141 case '(': *arg = skipwhite(*arg + 1);
3142 ret = compile_expr1(arg, cctx); // recursive!
3143 *arg = skipwhite(*arg);
3144 if (**arg == ')')
3145 ++*arg;
3146 else if (ret == OK)
3147 {
3148 emsg(_(e_missing_close));
3149 ret = FAIL;
3150 }
3151 break;
3152
3153 default: ret = NOTDONE;
3154 break;
3155 }
3156 if (ret == FAIL)
3157 return FAIL;
3158
3159 if (rettv.v_type != VAR_UNKNOWN)
3160 {
3161 // apply the '!', '-' and '+' before the constant
3162 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3163 {
3164 clear_tv(&rettv);
3165 return FAIL;
3166 }
3167 start_leader = end_leader; // don't apply again below
3168
3169 // push constant
3170 switch (rettv.v_type)
3171 {
3172 case VAR_BOOL:
3173 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3174 break;
3175 case VAR_SPECIAL:
3176 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3177 break;
3178 case VAR_NUMBER:
3179 generate_PUSHNR(cctx, rettv.vval.v_number);
3180 break;
3181#ifdef FEAT_FLOAT
3182 case VAR_FLOAT:
3183 generate_PUSHF(cctx, rettv.vval.v_float);
3184 break;
3185#endif
3186 case VAR_BLOB:
3187 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3188 rettv.vval.v_blob = NULL;
3189 break;
3190 case VAR_STRING:
3191 generate_PUSHS(cctx, rettv.vval.v_string);
3192 rettv.vval.v_string = NULL;
3193 break;
3194 default:
3195 iemsg("constant type missing");
3196 return FAIL;
3197 }
3198 }
3199 else if (ret == NOTDONE)
3200 {
3201 char_u *p;
3202 int r;
3203
3204 if (!eval_isnamec1(**arg))
3205 {
3206 semsg(_("E1015: Name expected: %s"), *arg);
3207 return FAIL;
3208 }
3209
3210 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003211 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 if (*p == '(')
3213 r = compile_call(arg, p - *arg, cctx, 0);
3214 else
3215 r = compile_load(arg, p, cctx, TRUE);
3216 if (r == FAIL)
3217 return FAIL;
3218 }
3219
3220 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3221 return FAIL;
3222
3223 // Now deal with prefixed '-', '+' and '!', if not done already.
3224 return compile_leader(cctx, start_leader, end_leader);
3225}
3226
3227/*
3228 * * number multiplication
3229 * / number division
3230 * % number modulo
3231 */
3232 static int
3233compile_expr6(char_u **arg, cctx_T *cctx)
3234{
3235 char_u *op;
3236
3237 // get the first variable
3238 if (compile_expr7(arg, cctx) == FAIL)
3239 return FAIL;
3240
3241 /*
3242 * Repeat computing, until no "*", "/" or "%" is following.
3243 */
3244 for (;;)
3245 {
3246 op = skipwhite(*arg);
3247 if (*op != '*' && *op != '/' && *op != '%')
3248 break;
3249 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
3250 {
3251 char_u buf[3];
3252
3253 vim_strncpy(buf, op, 1);
3254 semsg(_(e_white_both), buf);
3255 }
3256 *arg = skipwhite(op + 1);
3257
3258 // get the second variable
3259 if (compile_expr7(arg, cctx) == FAIL)
3260 return FAIL;
3261
3262 generate_two_op(cctx, op);
3263 }
3264
3265 return OK;
3266}
3267
3268/*
3269 * + number addition
3270 * - number subtraction
3271 * .. string concatenation
3272 */
3273 static int
3274compile_expr5(char_u **arg, cctx_T *cctx)
3275{
3276 char_u *op;
3277 int oplen;
3278
3279 // get the first variable
3280 if (compile_expr6(arg, cctx) == FAIL)
3281 return FAIL;
3282
3283 /*
3284 * Repeat computing, until no "+", "-" or ".." is following.
3285 */
3286 for (;;)
3287 {
3288 op = skipwhite(*arg);
3289 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3290 break;
3291 oplen = (*op == '.' ? 2 : 1);
3292
3293 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
3294 {
3295 char_u buf[3];
3296
3297 vim_strncpy(buf, op, oplen);
3298 semsg(_(e_white_both), buf);
3299 }
3300
3301 *arg = skipwhite(op + oplen);
3302
3303 // get the second variable
3304 if (compile_expr6(arg, cctx) == FAIL)
3305 return FAIL;
3306
3307 if (*op == '.')
3308 {
3309 if (may_generate_2STRING(-2, cctx) == FAIL
3310 || may_generate_2STRING(-1, cctx) == FAIL)
3311 return FAIL;
3312 generate_instr_drop(cctx, ISN_CONCAT, 1);
3313 }
3314 else
3315 generate_two_op(cctx, op);
3316 }
3317
3318 return OK;
3319}
3320
Bram Moolenaar080457c2020-03-03 21:53:32 +01003321 static exptype_T
3322get_compare_type(char_u *p, int *len, int *type_is)
3323{
3324 exptype_T type = EXPR_UNKNOWN;
3325 int i;
3326
3327 switch (p[0])
3328 {
3329 case '=': if (p[1] == '=')
3330 type = EXPR_EQUAL;
3331 else if (p[1] == '~')
3332 type = EXPR_MATCH;
3333 break;
3334 case '!': if (p[1] == '=')
3335 type = EXPR_NEQUAL;
3336 else if (p[1] == '~')
3337 type = EXPR_NOMATCH;
3338 break;
3339 case '>': if (p[1] != '=')
3340 {
3341 type = EXPR_GREATER;
3342 *len = 1;
3343 }
3344 else
3345 type = EXPR_GEQUAL;
3346 break;
3347 case '<': if (p[1] != '=')
3348 {
3349 type = EXPR_SMALLER;
3350 *len = 1;
3351 }
3352 else
3353 type = EXPR_SEQUAL;
3354 break;
3355 case 'i': if (p[1] == 's')
3356 {
3357 // "is" and "isnot"; but not a prefix of a name
3358 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3359 *len = 5;
3360 i = p[*len];
3361 if (!isalnum(i) && i != '_')
3362 {
3363 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3364 *type_is = TRUE;
3365 }
3366 }
3367 break;
3368 }
3369 return type;
3370}
3371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372/*
3373 * expr5a == expr5b
3374 * expr5a =~ expr5b
3375 * expr5a != expr5b
3376 * expr5a !~ expr5b
3377 * expr5a > expr5b
3378 * expr5a >= expr5b
3379 * expr5a < expr5b
3380 * expr5a <= expr5b
3381 * expr5a is expr5b
3382 * expr5a isnot expr5b
3383 *
3384 * Produces instructions:
3385 * EVAL expr5a Push result of "expr5a"
3386 * EVAL expr5b Push result of "expr5b"
3387 * COMPARE one of the compare instructions
3388 */
3389 static int
3390compile_expr4(char_u **arg, cctx_T *cctx)
3391{
3392 exptype_T type = EXPR_UNKNOWN;
3393 char_u *p;
3394 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 int type_is = FALSE;
3396
3397 // get the first variable
3398 if (compile_expr5(arg, cctx) == FAIL)
3399 return FAIL;
3400
3401 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003402 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403
3404 /*
3405 * If there is a comparative operator, use it.
3406 */
3407 if (type != EXPR_UNKNOWN)
3408 {
3409 int ic = FALSE; // Default: do not ignore case
3410
3411 if (type_is && (p[len] == '?' || p[len] == '#'))
3412 {
3413 semsg(_(e_invexpr2), *arg);
3414 return FAIL;
3415 }
3416 // extra question mark appended: ignore case
3417 if (p[len] == '?')
3418 {
3419 ic = TRUE;
3420 ++len;
3421 }
3422 // extra '#' appended: match case (ignored)
3423 else if (p[len] == '#')
3424 ++len;
3425 // nothing appended: match case
3426
3427 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
3428 {
3429 char_u buf[7];
3430
3431 vim_strncpy(buf, p, len);
3432 semsg(_(e_white_both), buf);
3433 }
3434
3435 // get the second variable
3436 *arg = skipwhite(p + len);
3437 if (compile_expr5(arg, cctx) == FAIL)
3438 return FAIL;
3439
3440 generate_COMPARE(cctx, type, ic);
3441 }
3442
3443 return OK;
3444}
3445
3446/*
3447 * Compile || or &&.
3448 */
3449 static int
3450compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3451{
3452 char_u *p = skipwhite(*arg);
3453 int opchar = *op;
3454
3455 if (p[0] == opchar && p[1] == opchar)
3456 {
3457 garray_T *instr = &cctx->ctx_instr;
3458 garray_T end_ga;
3459
3460 /*
3461 * Repeat until there is no following "||" or "&&"
3462 */
3463 ga_init2(&end_ga, sizeof(int), 10);
3464 while (p[0] == opchar && p[1] == opchar)
3465 {
3466 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3467 semsg(_(e_white_both), op);
3468
3469 if (ga_grow(&end_ga, 1) == FAIL)
3470 {
3471 ga_clear(&end_ga);
3472 return FAIL;
3473 }
3474 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3475 ++end_ga.ga_len;
3476 generate_JUMP(cctx, opchar == '|'
3477 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3478
3479 // eval the next expression
3480 *arg = skipwhite(p + 2);
3481 if ((opchar == '|' ? compile_expr3(arg, cctx)
3482 : compile_expr4(arg, cctx)) == FAIL)
3483 {
3484 ga_clear(&end_ga);
3485 return FAIL;
3486 }
3487 p = skipwhite(*arg);
3488 }
3489
3490 // Fill in the end label in all jumps.
3491 while (end_ga.ga_len > 0)
3492 {
3493 isn_T *isn;
3494
3495 --end_ga.ga_len;
3496 isn = ((isn_T *)instr->ga_data)
3497 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3498 isn->isn_arg.jump.jump_where = instr->ga_len;
3499 }
3500 ga_clear(&end_ga);
3501 }
3502
3503 return OK;
3504}
3505
3506/*
3507 * expr4a && expr4a && expr4a logical AND
3508 *
3509 * Produces instructions:
3510 * EVAL expr4a Push result of "expr4a"
3511 * JUMP_AND_KEEP_IF_FALSE end
3512 * EVAL expr4b Push result of "expr4b"
3513 * JUMP_AND_KEEP_IF_FALSE end
3514 * EVAL expr4c Push result of "expr4c"
3515 * end:
3516 */
3517 static int
3518compile_expr3(char_u **arg, cctx_T *cctx)
3519{
3520 // get the first variable
3521 if (compile_expr4(arg, cctx) == FAIL)
3522 return FAIL;
3523
3524 // || and && work almost the same
3525 return compile_and_or(arg, cctx, "&&");
3526}
3527
3528/*
3529 * expr3a || expr3b || expr3c logical OR
3530 *
3531 * Produces instructions:
3532 * EVAL expr3a Push result of "expr3a"
3533 * JUMP_AND_KEEP_IF_TRUE end
3534 * EVAL expr3b Push result of "expr3b"
3535 * JUMP_AND_KEEP_IF_TRUE end
3536 * EVAL expr3c Push result of "expr3c"
3537 * end:
3538 */
3539 static int
3540compile_expr2(char_u **arg, cctx_T *cctx)
3541{
3542 // eval the first expression
3543 if (compile_expr3(arg, cctx) == FAIL)
3544 return FAIL;
3545
3546 // || and && work almost the same
3547 return compile_and_or(arg, cctx, "||");
3548}
3549
3550/*
3551 * Toplevel expression: expr2 ? expr1a : expr1b
3552 *
3553 * Produces instructions:
3554 * EVAL expr2 Push result of "expr"
3555 * JUMP_IF_FALSE alt jump if false
3556 * EVAL expr1a
3557 * JUMP_ALWAYS end
3558 * alt: EVAL expr1b
3559 * end:
3560 */
3561 static int
3562compile_expr1(char_u **arg, cctx_T *cctx)
3563{
3564 char_u *p;
3565
3566 // evaluate the first expression
3567 if (compile_expr2(arg, cctx) == FAIL)
3568 return FAIL;
3569
3570 p = skipwhite(*arg);
3571 if (*p == '?')
3572 {
3573 garray_T *instr = &cctx->ctx_instr;
3574 garray_T *stack = &cctx->ctx_type_stack;
3575 int alt_idx = instr->ga_len;
3576 int end_idx;
3577 isn_T *isn;
3578 type_T *type1;
3579 type_T *type2;
3580
3581 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3582 semsg(_(e_white_both), "?");
3583
3584 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3585
3586 // evaluate the second expression; any type is accepted
3587 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003588 if (compile_expr1(arg, cctx) == FAIL)
3589 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590
3591 // remember the type and drop it
3592 --stack->ga_len;
3593 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3594
3595 end_idx = instr->ga_len;
3596 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3597
3598 // jump here from JUMP_IF_FALSE
3599 isn = ((isn_T *)instr->ga_data) + alt_idx;
3600 isn->isn_arg.jump.jump_where = instr->ga_len;
3601
3602 // Check for the ":".
3603 p = skipwhite(*arg);
3604 if (*p != ':')
3605 {
3606 emsg(_(e_missing_colon));
3607 return FAIL;
3608 }
3609 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3610 semsg(_(e_white_both), ":");
3611
3612 // evaluate the third expression
3613 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003614 if (compile_expr1(arg, cctx) == FAIL)
3615 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616
3617 // If the types differ, the result has a more generic type.
3618 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003619 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620
3621 // jump here from JUMP_ALWAYS
3622 isn = ((isn_T *)instr->ga_data) + end_idx;
3623 isn->isn_arg.jump.jump_where = instr->ga_len;
3624 }
3625 return OK;
3626}
3627
3628/*
3629 * compile "return [expr]"
3630 */
3631 static char_u *
3632compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3633{
3634 char_u *p = arg;
3635 garray_T *stack = &cctx->ctx_type_stack;
3636 type_T *stack_type;
3637
3638 if (*p != NUL && *p != '|' && *p != '\n')
3639 {
3640 // compile return argument into instructions
3641 if (compile_expr1(&p, cctx) == FAIL)
3642 return NULL;
3643
3644 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3645 if (set_return_type)
3646 cctx->ctx_ufunc->uf_ret_type = stack_type;
3647 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3648 == FAIL)
3649 return NULL;
3650 }
3651 else
3652 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003653 // "set_return_type" cannot be TRUE, only used for a lambda which
3654 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02003655 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
3656 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 {
3658 emsg(_("E1003: Missing return value"));
3659 return NULL;
3660 }
3661
3662 // No argument, return zero.
3663 generate_PUSHNR(cctx, 0);
3664 }
3665
3666 if (generate_instr(cctx, ISN_RETURN) == NULL)
3667 return NULL;
3668
3669 // "return val | endif" is possible
3670 return skipwhite(p);
3671}
3672
3673/*
3674 * Return the length of an assignment operator, or zero if there isn't one.
3675 */
3676 int
3677assignment_len(char_u *p, int *heredoc)
3678{
3679 if (*p == '=')
3680 {
3681 if (p[1] == '<' && p[2] == '<')
3682 {
3683 *heredoc = TRUE;
3684 return 3;
3685 }
3686 return 1;
3687 }
3688 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3689 return 2;
3690 if (STRNCMP(p, "..=", 3) == 0)
3691 return 3;
3692 return 0;
3693}
3694
3695// words that cannot be used as a variable
3696static char *reserved[] = {
3697 "true",
3698 "false",
3699 NULL
3700};
3701
3702/*
3703 * Get a line for "=<<".
3704 * Return a pointer to the line in allocated memory.
3705 * Return NULL for end-of-file or some error.
3706 */
3707 static char_u *
3708heredoc_getline(
3709 int c UNUSED,
3710 void *cookie,
3711 int indent UNUSED,
3712 int do_concat UNUSED)
3713{
3714 cctx_T *cctx = (cctx_T *)cookie;
3715
3716 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003717 {
3718 iemsg("Heredoc got to end");
3719 return NULL;
3720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 ++cctx->ctx_lnum;
3722 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3723 [cctx->ctx_lnum]);
3724}
3725
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003726typedef enum {
3727 dest_local,
3728 dest_option,
3729 dest_env,
3730 dest_global,
3731 dest_vimvar,
3732 dest_script,
3733 dest_reg,
3734} assign_dest_T;
3735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736/*
3737 * compile "let var [= expr]", "const var = expr" and "var = expr"
3738 * "arg" points to "var".
3739 */
3740 static char_u *
3741compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3742{
3743 char_u *p;
3744 char_u *ret = NULL;
3745 int var_count = 0;
3746 int semicolon = 0;
3747 size_t varlen;
3748 garray_T *instr = &cctx->ctx_instr;
3749 int idx = -1;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003750 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003753 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003755 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 int oplen = 0;
3757 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003758 type_T *type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 lvar_T *lvar;
3760 char_u *name;
3761 char_u *sp;
3762 int has_type = FALSE;
3763 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3764 int instr_count = -1;
3765
3766 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3767 if (p == NULL)
3768 return NULL;
3769 if (var_count > 0)
3770 {
3771 // TODO: let [var, var] = list
3772 emsg("Cannot handle a list yet");
3773 return NULL;
3774 }
3775
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003776 // "a: type" is declaring variable "a" with a type, not "a:".
3777 if (is_decl && p == arg + 2 && p[-1] == ':')
3778 --p;
3779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 varlen = p - arg;
3781 name = vim_strnsave(arg, (int)varlen);
3782 if (name == NULL)
3783 return NULL;
3784
Bram Moolenaar080457c2020-03-03 21:53:32 +01003785 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003787 if (*arg == '&')
3788 {
3789 int cc;
3790 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791
Bram Moolenaar080457c2020-03-03 21:53:32 +01003792 dest = dest_option;
3793 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003795 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003796 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 if (is_decl)
3799 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003800 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 goto theend;
3802 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003803 p = arg;
3804 p = find_option_end(&p, &opt_flags);
3805 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003807 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01003808 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003809 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003810 }
3811 cc = *p;
3812 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003813 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003814 *p = cc;
3815 if (opt_type == -3)
3816 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003817 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003818 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003819 }
3820 if (opt_type == -2 || opt_type == 0)
3821 type = &t_string;
3822 else
3823 type = &t_number; // both number and boolean option
3824 }
3825 else if (*arg == '$')
3826 {
3827 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003828 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003829 if (is_decl)
3830 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003831 semsg(_("E1065: Cannot declare an environment variable: %s"),
3832 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003833 goto theend;
3834 }
3835 }
3836 else if (*arg == '@')
3837 {
3838 if (!valid_yank_reg(arg[1], TRUE))
3839 {
3840 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003841 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003842 }
3843 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003844 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003845 if (is_decl)
3846 {
3847 semsg(_("E1066: Cannot declare a register: %s"), name);
3848 goto theend;
3849 }
3850 }
3851 else if (STRNCMP(arg, "g:", 2) == 0)
3852 {
3853 dest = dest_global;
3854 if (is_decl)
3855 {
3856 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3857 goto theend;
3858 }
3859 }
3860 else if (STRNCMP(arg, "v:", 2) == 0)
3861 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02003862 typval_T *vtv;
3863
Bram Moolenaar080457c2020-03-03 21:53:32 +01003864 vimvaridx = find_vim_var(name + 2);
3865 if (vimvaridx < 0)
3866 {
3867 semsg(_(e_var_notfound), arg);
3868 goto theend;
3869 }
3870 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003871 vtv = get_vim_var_tv(vimvaridx);
3872 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003873 if (is_decl)
3874 {
3875 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3876 goto theend;
3877 }
3878 }
3879 else
3880 {
3881 for (idx = 0; reserved[idx] != NULL; ++idx)
3882 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003884 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 goto theend;
3886 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003887
3888 idx = lookup_local(arg, varlen, cctx);
3889 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003891 if (is_decl)
3892 {
3893 semsg(_("E1017: Variable already declared: %s"), name);
3894 goto theend;
3895 }
3896 else
3897 {
3898 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3899 if (lvar->lv_const)
3900 {
3901 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3902 goto theend;
3903 }
3904 }
3905 }
3906 else if (STRNCMP(arg, "s:", 2) == 0
3907 || lookup_script(arg, varlen) == OK
3908 || find_imported(arg, varlen, cctx) != NULL)
3909 {
3910 dest = dest_script;
3911 if (is_decl)
3912 {
3913 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003914 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003915 goto theend;
3916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 }
3918 }
3919 }
3920
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003921 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 {
3923 if (is_decl && *p == ':')
3924 {
3925 // parse optional type: "let var: type = expr"
3926 p = skipwhite(p + 1);
3927 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 has_type = TRUE;
3929 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02003930 else if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 {
3932 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3933 type = lvar->lv_type;
3934 }
3935 }
3936
3937 sp = p;
3938 p = skipwhite(p);
3939 op = p;
3940 oplen = assignment_len(p, &heredoc);
3941 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3942 {
3943 char_u buf[4];
3944
3945 vim_strncpy(buf, op, oplen);
3946 semsg(_(e_white_both), buf);
3947 }
3948
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003949 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02003950 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01003952 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 goto theend;
3954 }
3955
Bram Moolenaar080457c2020-03-03 21:53:32 +01003956 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 {
3958 if (oplen > 1 && !heredoc)
3959 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003960 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3962 name);
3963 goto theend;
3964 }
3965
3966 // new local variable
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003967 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
3968 && var_check_func_name(name, TRUE))
3969 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3971 if (idx < 0)
3972 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003973 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 }
3975
3976 if (heredoc)
3977 {
3978 list_T *l;
3979 listitem_T *li;
3980
3981 // [let] varname =<< [trim] {end}
3982 eap->getline = heredoc_getline;
3983 eap->cookie = cctx;
3984 l = heredoc_get(eap, op + 3);
3985
3986 // Push each line and the create the list.
3987 for (li = l->lv_first; li != NULL; li = li->li_next)
3988 {
3989 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3990 li->li_tv.vval.v_string = NULL;
3991 }
3992 generate_NEWLIST(cctx, l->lv_len);
3993 type = &t_list_string;
3994 list_free(l);
3995 p += STRLEN(p);
3996 }
3997 else if (oplen > 0)
3998 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02003999 int r;
4000 type_T *stacktype;
4001 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 // for "+=", "*=", "..=" etc. first load the current value
4004 if (*op != '=')
4005 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004006 switch (dest)
4007 {
4008 case dest_option:
4009 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004010 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004011 break;
4012 case dest_global:
4013 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4014 break;
4015 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004016 compile_load_scriptvar(cctx,
4017 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004018 break;
4019 case dest_env:
4020 // Include $ in the name here
4021 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4022 break;
4023 case dest_reg:
4024 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4025 break;
4026 case dest_vimvar:
4027 generate_LOADV(cctx, name + 2, TRUE);
4028 break;
4029 case dest_local:
4030 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
4031 break;
4032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 }
4034
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004035 // Compile the expression. Temporarily hide the new local variable
4036 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004037 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004038 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 instr_count = instr->ga_len;
4040 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004041 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004042 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004043 ++cctx->ctx_locals.ga_len;
4044 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 goto theend;
4046
Bram Moolenaara8c17702020-04-01 21:17:24 +02004047 stack = &cctx->ctx_type_stack;
Bram Moolenaarea94fbe2020-04-01 22:36:49 +02004048 stacktype = stack->ga_len == 0 ? &t_void
4049 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 if (idx >= 0 && (is_decl || !has_type))
4051 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004053 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 {
4055 if (stacktype->tt_type == VAR_VOID)
4056 {
4057 emsg(_("E1031: Cannot use void value"));
4058 goto theend;
4059 }
4060 else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004061 {
4062 // An empty list or dict has a &t_void member, for a
4063 // variable that implies &t_any.
4064 if (stacktype == &t_list_empty)
4065 lvar->lv_type = &t_list_any;
4066 else if (stacktype == &t_dict_empty)
4067 lvar->lv_type = &t_dict_any;
4068 else
4069 lvar->lv_type = stacktype;
4070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004072 else if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
4073 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02004075 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
4076 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 }
4078 else if (cmdidx == CMD_const)
4079 {
4080 emsg(_("E1021: const requires a value"));
4081 goto theend;
4082 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004083 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 {
4085 emsg(_("E1022: type or initialization required"));
4086 goto theend;
4087 }
4088 else
4089 {
4090 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 if (ga_grow(instr, 1) == FAIL)
4092 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004093 switch (type->tt_type)
4094 {
4095 case VAR_BOOL:
4096 generate_PUSHBOOL(cctx, VVAL_FALSE);
4097 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004098 case VAR_FLOAT:
4099#ifdef FEAT_FLOAT
4100 generate_PUSHF(cctx, 0.0);
4101#endif
4102 break;
4103 case VAR_STRING:
4104 generate_PUSHS(cctx, NULL);
4105 break;
4106 case VAR_BLOB:
4107 generate_PUSHBLOB(cctx, NULL);
4108 break;
4109 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004110 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004111 break;
4112 case VAR_PARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004113 generate_PUSHPARTIAL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004114 break;
4115 case VAR_LIST:
4116 generate_NEWLIST(cctx, 0);
4117 break;
4118 case VAR_DICT:
4119 generate_NEWDICT(cctx, 0);
4120 break;
4121 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004122 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004123 break;
4124 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004125 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004126 break;
4127 case VAR_NUMBER:
4128 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004129 case VAR_ANY:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004130 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004131 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004132 generate_PUSHNR(cctx, 0);
4133 break;
4134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 }
4136
4137 if (oplen > 0 && *op != '=')
4138 {
4139 type_T *expected = &t_number;
4140 garray_T *stack = &cctx->ctx_type_stack;
4141 type_T *stacktype;
4142
4143 // TODO: if type is known use float or any operation
4144
4145 if (*op == '.')
4146 expected = &t_string;
4147 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4148 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4149 goto theend;
4150
4151 if (*op == '.')
4152 generate_instr_drop(cctx, ISN_CONCAT, 1);
4153 else
4154 {
4155 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4156
4157 if (isn == NULL)
4158 goto theend;
4159 switch (*op)
4160 {
4161 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4162 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4163 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4164 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4165 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4166 }
4167 }
4168 }
4169
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004170 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004172 case dest_option:
4173 generate_STOREOPT(cctx, name + 1, opt_flags);
4174 break;
4175 case dest_global:
4176 // include g: with the name, easier to execute that way
4177 generate_STORE(cctx, ISN_STOREG, 0, name);
4178 break;
4179 case dest_env:
4180 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4181 break;
4182 case dest_reg:
4183 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4184 break;
4185 case dest_vimvar:
4186 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4187 break;
4188 case dest_script:
4189 {
4190 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4191 imported_T *import = NULL;
4192 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004193
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004194 if (name[1] != ':')
4195 {
4196 import = find_imported(name, 0, cctx);
4197 if (import != NULL)
4198 sid = import->imp_sid;
4199 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004201 idx = get_script_item_idx(sid, rawname, TRUE);
4202 // TODO: specific type
4203 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004204 {
4205 char_u *name_s = name;
4206
4207 // Include s: in the name for store_var()
4208 if (name[1] != ':')
4209 {
4210 int len = (int)STRLEN(name) + 3;
4211
4212 name_s = alloc(len);
4213 if (name_s == NULL)
4214 name_s = name;
4215 else
4216 vim_snprintf((char *)name_s, len, "s:%s", name);
4217 }
4218 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4219 if (name_s != name)
4220 vim_free(name_s);
4221 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004222 else
4223 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4224 sid, idx, &t_any);
4225 }
4226 break;
4227 case dest_local:
4228 {
4229 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004231 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4232 // into ISN_STORENR
4233 if (instr->ga_len == instr_count + 1
4234 && isn->isn_type == ISN_PUSHNR)
4235 {
4236 varnumber_T val = isn->isn_arg.number;
4237 garray_T *stack = &cctx->ctx_type_stack;
4238
4239 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004240 isn->isn_arg.storenr.stnr_idx = idx;
4241 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004242 if (stack->ga_len > 0)
4243 --stack->ga_len;
4244 }
4245 else
4246 generate_STORE(cctx, ISN_STORE, idx, NULL);
4247 }
4248 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 }
4250 ret = p;
4251
4252theend:
4253 vim_free(name);
4254 return ret;
4255}
4256
4257/*
4258 * Compile an :import command.
4259 */
4260 static char_u *
4261compile_import(char_u *arg, cctx_T *cctx)
4262{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004263 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264}
4265
4266/*
4267 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4268 */
4269 static int
4270compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4271{
4272 garray_T *instr = &cctx->ctx_instr;
4273 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4274
4275 if (endlabel == NULL)
4276 return FAIL;
4277 endlabel->el_next = *el;
4278 *el = endlabel;
4279 endlabel->el_end_label = instr->ga_len;
4280
4281 generate_JUMP(cctx, when, 0);
4282 return OK;
4283}
4284
4285 static void
4286compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4287{
4288 garray_T *instr = &cctx->ctx_instr;
4289
4290 while (*el != NULL)
4291 {
4292 endlabel_T *cur = (*el);
4293 isn_T *isn;
4294
4295 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4296 isn->isn_arg.jump.jump_where = instr->ga_len;
4297 *el = cur->el_next;
4298 vim_free(cur);
4299 }
4300}
4301
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004302 static void
4303compile_free_jump_to_end(endlabel_T **el)
4304{
4305 while (*el != NULL)
4306 {
4307 endlabel_T *cur = (*el);
4308
4309 *el = cur->el_next;
4310 vim_free(cur);
4311 }
4312}
4313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314/*
4315 * Create a new scope and set up the generic items.
4316 */
4317 static scope_T *
4318new_scope(cctx_T *cctx, scopetype_T type)
4319{
4320 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4321
4322 if (scope == NULL)
4323 return NULL;
4324 scope->se_outer = cctx->ctx_scope;
4325 cctx->ctx_scope = scope;
4326 scope->se_type = type;
4327 scope->se_local_count = cctx->ctx_locals.ga_len;
4328 return scope;
4329}
4330
4331/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004332 * Free the current scope and go back to the outer scope.
4333 */
4334 static void
4335drop_scope(cctx_T *cctx)
4336{
4337 scope_T *scope = cctx->ctx_scope;
4338
4339 if (scope == NULL)
4340 {
4341 iemsg("calling drop_scope() without a scope");
4342 return;
4343 }
4344 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004345 switch (scope->se_type)
4346 {
4347 case IF_SCOPE:
4348 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4349 case FOR_SCOPE:
4350 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4351 case WHILE_SCOPE:
4352 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4353 case TRY_SCOPE:
4354 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4355 case NO_SCOPE:
4356 case BLOCK_SCOPE:
4357 break;
4358 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004359 vim_free(scope);
4360}
4361
4362/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004363 * Evaluate an expression that is a constant:
4364 * has(arg)
4365 *
4366 * Also handle:
4367 * ! in front logical NOT
4368 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004369 * Return FAIL if the expression is not a constant.
4370 */
4371 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004372evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004373{
4374 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004375 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004376 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004377
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004378 /*
4379 * Skip '!' characters. They are handled later.
4380 */
4381 start_leader = *arg;
4382 while (**arg == '!')
4383 *arg = skipwhite(*arg + 1);
4384 end_leader = *arg;
4385
4386 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004387 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004388 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004389 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4390 {
4391 tv->v_type = VAR_SPECIAL;
4392 tv->vval.v_number = VVAL_TRUE;
4393 *arg += 4;
4394 return OK;
4395 }
4396 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4397 {
4398 tv->v_type = VAR_SPECIAL;
4399 tv->vval.v_number = VVAL_FALSE;
4400 *arg += 5;
4401 return OK;
4402 }
4403
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004404 if (STRNCMP("has(", *arg, 4) == 0)
4405 {
4406 has_call = TRUE;
4407 *arg = skipwhite(*arg + 4);
4408 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004409
4410 if (**arg == '"')
4411 {
4412 if (get_string_tv(arg, tv, TRUE) == FAIL)
4413 return FAIL;
4414 }
4415 else if (**arg == '\'')
4416 {
4417 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4418 return FAIL;
4419 }
4420 else
4421 return FAIL;
4422
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004423 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004424 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004425 *arg = skipwhite(*arg);
4426 if (**arg != ')')
4427 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004428 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004429
4430 argvars[0] = *tv;
4431 argvars[1].v_type = VAR_UNKNOWN;
4432 tv->v_type = VAR_NUMBER;
4433 tv->vval.v_number = 0;
4434 f_has(argvars, tv);
4435 clear_tv(&argvars[0]);
4436
4437 while (start_leader < end_leader)
4438 {
4439 if (*start_leader == '!')
4440 tv->vval.v_number = !tv->vval.v_number;
4441 ++start_leader;
4442 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004443 }
4444
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004445 return OK;
4446}
4447
Bram Moolenaar080457c2020-03-03 21:53:32 +01004448 static int
4449evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4450{
4451 exptype_T type = EXPR_UNKNOWN;
4452 char_u *p;
4453 int len = 2;
4454 int type_is = FALSE;
4455
4456 // get the first variable
4457 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4458 return FAIL;
4459
4460 p = skipwhite(*arg);
4461 type = get_compare_type(p, &len, &type_is);
4462
4463 /*
4464 * If there is a comparative operator, use it.
4465 */
4466 if (type != EXPR_UNKNOWN)
4467 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004468 typval_T tv2;
4469 char_u *s1, *s2;
4470 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4471 int n;
4472
4473 // TODO: Only string == string is supported now
4474 if (tv->v_type != VAR_STRING)
4475 return FAIL;
4476 if (type != EXPR_EQUAL)
4477 return FAIL;
4478
4479 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004480 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004481 *arg = skipwhite(p + len);
4482 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4483 || tv2.v_type != VAR_STRING)
4484 {
4485 clear_tv(&tv2);
4486 return FAIL;
4487 }
4488 s1 = tv_get_string_buf(tv, buf1);
4489 s2 = tv_get_string_buf(&tv2, buf2);
4490 n = STRCMP(s1, s2);
4491 clear_tv(tv);
4492 clear_tv(&tv2);
4493 tv->v_type = VAR_BOOL;
4494 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004495 }
4496
4497 return OK;
4498}
4499
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004500static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4501
4502/*
4503 * Compile constant || or &&.
4504 */
4505 static int
4506evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4507{
4508 char_u *p = skipwhite(*arg);
4509 int opchar = *op;
4510
4511 if (p[0] == opchar && p[1] == opchar)
4512 {
4513 int val = tv2bool(tv);
4514
4515 /*
4516 * Repeat until there is no following "||" or "&&"
4517 */
4518 while (p[0] == opchar && p[1] == opchar)
4519 {
4520 typval_T tv2;
4521
4522 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4523 return FAIL;
4524
4525 // eval the next expression
4526 *arg = skipwhite(p + 2);
4527 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004528 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004529 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004530 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004531 {
4532 clear_tv(&tv2);
4533 return FAIL;
4534 }
4535 if ((opchar == '&') == val)
4536 {
4537 // false || tv2 or true && tv2: use tv2
4538 clear_tv(tv);
4539 *tv = tv2;
4540 val = tv2bool(tv);
4541 }
4542 else
4543 clear_tv(&tv2);
4544 p = skipwhite(*arg);
4545 }
4546 }
4547
4548 return OK;
4549}
4550
4551/*
4552 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4553 * Return FAIL if the expression is not a constant.
4554 */
4555 static int
4556evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4557{
4558 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004559 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004560 return FAIL;
4561
4562 // || and && work almost the same
4563 return evaluate_const_and_or(arg, cctx, "&&", tv);
4564}
4565
4566/*
4567 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4568 * Return FAIL if the expression is not a constant.
4569 */
4570 static int
4571evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4572{
4573 // evaluate the first expression
4574 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4575 return FAIL;
4576
4577 // || and && work almost the same
4578 return evaluate_const_and_or(arg, cctx, "||", tv);
4579}
4580
4581/*
4582 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4583 * E.g. for "has('feature')".
4584 * This does not produce error messages. "tv" should be cleared afterwards.
4585 * Return FAIL if the expression is not a constant.
4586 */
4587 static int
4588evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4589{
4590 char_u *p;
4591
4592 // evaluate the first expression
4593 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4594 return FAIL;
4595
4596 p = skipwhite(*arg);
4597 if (*p == '?')
4598 {
4599 int val = tv2bool(tv);
4600 typval_T tv2;
4601
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004602 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004603 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4604 return FAIL;
4605
4606 // evaluate the second expression; any type is accepted
4607 clear_tv(tv);
4608 *arg = skipwhite(p + 1);
4609 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4610 return FAIL;
4611
4612 // Check for the ":".
4613 p = skipwhite(*arg);
4614 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4615 return FAIL;
4616
4617 // evaluate the third expression
4618 *arg = skipwhite(p + 1);
4619 tv2.v_type = VAR_UNKNOWN;
4620 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4621 {
4622 clear_tv(&tv2);
4623 return FAIL;
4624 }
4625 if (val)
4626 {
4627 // use the expr after "?"
4628 clear_tv(&tv2);
4629 }
4630 else
4631 {
4632 // use the expr after ":"
4633 clear_tv(tv);
4634 *tv = tv2;
4635 }
4636 }
4637 return OK;
4638}
4639
4640/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 * compile "if expr"
4642 *
4643 * "if expr" Produces instructions:
4644 * EVAL expr Push result of "expr"
4645 * JUMP_IF_FALSE end
4646 * ... body ...
4647 * end:
4648 *
4649 * "if expr | else" Produces instructions:
4650 * EVAL expr Push result of "expr"
4651 * JUMP_IF_FALSE else
4652 * ... body ...
4653 * JUMP_ALWAYS end
4654 * else:
4655 * ... body ...
4656 * end:
4657 *
4658 * "if expr1 | elseif expr2 | else" Produces instructions:
4659 * EVAL expr Push result of "expr"
4660 * JUMP_IF_FALSE elseif
4661 * ... body ...
4662 * JUMP_ALWAYS end
4663 * elseif:
4664 * EVAL expr Push result of "expr"
4665 * JUMP_IF_FALSE else
4666 * ... body ...
4667 * JUMP_ALWAYS end
4668 * else:
4669 * ... body ...
4670 * end:
4671 */
4672 static char_u *
4673compile_if(char_u *arg, cctx_T *cctx)
4674{
4675 char_u *p = arg;
4676 garray_T *instr = &cctx->ctx_instr;
4677 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004678 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004680 // compile "expr"; if we know it evaluates to FALSE skip the block
4681 tv.v_type = VAR_UNKNOWN;
4682 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4683 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4684 else
4685 cctx->ctx_skip = MAYBE;
4686 clear_tv(&tv);
4687 if (cctx->ctx_skip == MAYBE)
4688 {
4689 p = arg;
4690 if (compile_expr1(&p, cctx) == FAIL)
4691 return NULL;
4692 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693
4694 scope = new_scope(cctx, IF_SCOPE);
4695 if (scope == NULL)
4696 return NULL;
4697
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004698 if (cctx->ctx_skip == MAYBE)
4699 {
4700 // "where" is set when ":elseif", "else" or ":endif" is found
4701 scope->se_u.se_if.is_if_label = instr->ga_len;
4702 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4703 }
4704 else
4705 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706
4707 return p;
4708}
4709
4710 static char_u *
4711compile_elseif(char_u *arg, cctx_T *cctx)
4712{
4713 char_u *p = arg;
4714 garray_T *instr = &cctx->ctx_instr;
4715 isn_T *isn;
4716 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004717 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718
4719 if (scope == NULL || scope->se_type != IF_SCOPE)
4720 {
4721 emsg(_(e_elseif_without_if));
4722 return NULL;
4723 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004724 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725
Bram Moolenaar158906c2020-02-06 20:39:45 +01004726 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004727 {
4728 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004730 return NULL;
4731 // previous "if" or "elseif" jumps here
4732 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4733 isn->isn_arg.jump.jump_where = instr->ga_len;
4734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004736 // compile "expr"; if we know it evaluates to FALSE skip the block
4737 tv.v_type = VAR_UNKNOWN;
4738 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4739 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4740 else
4741 cctx->ctx_skip = MAYBE;
4742 clear_tv(&tv);
4743 if (cctx->ctx_skip == MAYBE)
4744 {
4745 p = arg;
4746 if (compile_expr1(&p, cctx) == FAIL)
4747 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004749 // "where" is set when ":elseif", "else" or ":endif" is found
4750 scope->se_u.se_if.is_if_label = instr->ga_len;
4751 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4752 }
4753 else
4754 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755
4756 return p;
4757}
4758
4759 static char_u *
4760compile_else(char_u *arg, cctx_T *cctx)
4761{
4762 char_u *p = arg;
4763 garray_T *instr = &cctx->ctx_instr;
4764 isn_T *isn;
4765 scope_T *scope = cctx->ctx_scope;
4766
4767 if (scope == NULL || scope->se_type != IF_SCOPE)
4768 {
4769 emsg(_(e_else_without_if));
4770 return NULL;
4771 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004772 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004774 // jump from previous block to the end, unless the else block is empty
4775 if (cctx->ctx_skip == MAYBE)
4776 {
4777 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004779 return NULL;
4780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781
Bram Moolenaar158906c2020-02-06 20:39:45 +01004782 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004783 {
4784 if (scope->se_u.se_if.is_if_label >= 0)
4785 {
4786 // previous "if" or "elseif" jumps here
4787 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4788 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004789 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004790 }
4791 }
4792
4793 if (cctx->ctx_skip != MAYBE)
4794 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795
4796 return p;
4797}
4798
4799 static char_u *
4800compile_endif(char_u *arg, cctx_T *cctx)
4801{
4802 scope_T *scope = cctx->ctx_scope;
4803 ifscope_T *ifscope;
4804 garray_T *instr = &cctx->ctx_instr;
4805 isn_T *isn;
4806
4807 if (scope == NULL || scope->se_type != IF_SCOPE)
4808 {
4809 emsg(_(e_endif_without_if));
4810 return NULL;
4811 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004812 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004813 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004815 if (scope->se_u.se_if.is_if_label >= 0)
4816 {
4817 // previous "if" or "elseif" jumps here
4818 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4819 isn->isn_arg.jump.jump_where = instr->ga_len;
4820 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 // Fill in the "end" label in jumps at the end of the blocks.
4822 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004823 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004825 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826 return arg;
4827}
4828
4829/*
4830 * compile "for var in expr"
4831 *
4832 * Produces instructions:
4833 * PUSHNR -1
4834 * STORE loop-idx Set index to -1
4835 * EVAL expr Push result of "expr"
4836 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4837 * - if beyond end, jump to "end"
4838 * - otherwise get item from list and push it
4839 * STORE var Store item in "var"
4840 * ... body ...
4841 * JUMP top Jump back to repeat
4842 * end: DROP Drop the result of "expr"
4843 *
4844 */
4845 static char_u *
4846compile_for(char_u *arg, cctx_T *cctx)
4847{
4848 char_u *p;
4849 size_t varlen;
4850 garray_T *instr = &cctx->ctx_instr;
4851 garray_T *stack = &cctx->ctx_type_stack;
4852 scope_T *scope;
4853 int loop_idx; // index of loop iteration variable
4854 int var_idx; // index of "var"
4855 type_T *vartype;
4856
4857 // TODO: list of variables: "for [key, value] in dict"
4858 // parse "var"
4859 for (p = arg; eval_isnamec1(*p); ++p)
4860 ;
4861 varlen = p - arg;
4862 var_idx = lookup_local(arg, varlen, cctx);
4863 if (var_idx >= 0)
4864 {
4865 semsg(_("E1023: variable already defined: %s"), arg);
4866 return NULL;
4867 }
4868
4869 // consume "in"
4870 p = skipwhite(p);
4871 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4872 {
4873 emsg(_(e_missing_in));
4874 return NULL;
4875 }
4876 p = skipwhite(p + 2);
4877
4878
4879 scope = new_scope(cctx, FOR_SCOPE);
4880 if (scope == NULL)
4881 return NULL;
4882
4883 // Reserve a variable to store the loop iteration counter.
4884 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4885 if (loop_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004886 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004887 // only happens when out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004888 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004890 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891
4892 // Reserve a variable to store "var"
4893 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4894 if (var_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004895 {
4896 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004898 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899
4900 generate_STORENR(cctx, loop_idx, -1);
4901
4902 // compile "expr", it remains on the stack until "endfor"
4903 arg = p;
4904 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004905 {
4906 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004908 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909
4910 // now we know the type of "var"
4911 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4912 if (vartype->tt_type != VAR_LIST)
4913 {
4914 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004915 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 return NULL;
4917 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02004918 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919 {
4920 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4921
4922 lvar->lv_type = vartype->tt_member;
4923 }
4924
4925 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004926 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004927
4928 generate_FOR(cctx, loop_idx);
4929 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4930
4931 return arg;
4932}
4933
4934/*
4935 * compile "endfor"
4936 */
4937 static char_u *
4938compile_endfor(char_u *arg, cctx_T *cctx)
4939{
4940 garray_T *instr = &cctx->ctx_instr;
4941 scope_T *scope = cctx->ctx_scope;
4942 forscope_T *forscope;
4943 isn_T *isn;
4944
4945 if (scope == NULL || scope->se_type != FOR_SCOPE)
4946 {
4947 emsg(_(e_for));
4948 return NULL;
4949 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004950 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004952 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953
4954 // At end of ":for" scope jump back to the FOR instruction.
4955 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4956
4957 // Fill in the "end" label in the FOR statement so it can jump here
4958 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4959 isn->isn_arg.forloop.for_end = instr->ga_len;
4960
4961 // Fill in the "end" label any BREAK statements
4962 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4963
4964 // Below the ":for" scope drop the "expr" list from the stack.
4965 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4966 return NULL;
4967
4968 vim_free(scope);
4969
4970 return arg;
4971}
4972
4973/*
4974 * compile "while expr"
4975 *
4976 * Produces instructions:
4977 * top: EVAL expr Push result of "expr"
4978 * JUMP_IF_FALSE end jump if false
4979 * ... body ...
4980 * JUMP top Jump back to repeat
4981 * end:
4982 *
4983 */
4984 static char_u *
4985compile_while(char_u *arg, cctx_T *cctx)
4986{
4987 char_u *p = arg;
4988 garray_T *instr = &cctx->ctx_instr;
4989 scope_T *scope;
4990
4991 scope = new_scope(cctx, WHILE_SCOPE);
4992 if (scope == NULL)
4993 return NULL;
4994
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004995 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996
4997 // compile "expr"
4998 if (compile_expr1(&p, cctx) == FAIL)
4999 return NULL;
5000
5001 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005002 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 JUMP_IF_FALSE, cctx) == FAIL)
5004 return FAIL;
5005
5006 return p;
5007}
5008
5009/*
5010 * compile "endwhile"
5011 */
5012 static char_u *
5013compile_endwhile(char_u *arg, cctx_T *cctx)
5014{
5015 scope_T *scope = cctx->ctx_scope;
5016
5017 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5018 {
5019 emsg(_(e_while));
5020 return NULL;
5021 }
5022 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005023 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024
5025 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005026 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027
5028 // Fill in the "end" label in the WHILE statement so it can jump here.
5029 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005030 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031
5032 vim_free(scope);
5033
5034 return arg;
5035}
5036
5037/*
5038 * compile "continue"
5039 */
5040 static char_u *
5041compile_continue(char_u *arg, cctx_T *cctx)
5042{
5043 scope_T *scope = cctx->ctx_scope;
5044
5045 for (;;)
5046 {
5047 if (scope == NULL)
5048 {
5049 emsg(_(e_continue));
5050 return NULL;
5051 }
5052 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5053 break;
5054 scope = scope->se_outer;
5055 }
5056
5057 // Jump back to the FOR or WHILE instruction.
5058 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005059 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5060 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061 return arg;
5062}
5063
5064/*
5065 * compile "break"
5066 */
5067 static char_u *
5068compile_break(char_u *arg, cctx_T *cctx)
5069{
5070 scope_T *scope = cctx->ctx_scope;
5071 endlabel_T **el;
5072
5073 for (;;)
5074 {
5075 if (scope == NULL)
5076 {
5077 emsg(_(e_break));
5078 return NULL;
5079 }
5080 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5081 break;
5082 scope = scope->se_outer;
5083 }
5084
5085 // Jump to the end of the FOR or WHILE loop.
5086 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005087 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005089 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5091 return FAIL;
5092
5093 return arg;
5094}
5095
5096/*
5097 * compile "{" start of block
5098 */
5099 static char_u *
5100compile_block(char_u *arg, cctx_T *cctx)
5101{
5102 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5103 return NULL;
5104 return skipwhite(arg + 1);
5105}
5106
5107/*
5108 * compile end of block: drop one scope
5109 */
5110 static void
5111compile_endblock(cctx_T *cctx)
5112{
5113 scope_T *scope = cctx->ctx_scope;
5114
5115 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005116 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117 vim_free(scope);
5118}
5119
5120/*
5121 * compile "try"
5122 * Creates a new scope for the try-endtry, pointing to the first catch and
5123 * finally.
5124 * Creates another scope for the "try" block itself.
5125 * TRY instruction sets up exception handling at runtime.
5126 *
5127 * "try"
5128 * TRY -> catch1, -> finally push trystack entry
5129 * ... try block
5130 * "throw {exception}"
5131 * EVAL {exception}
5132 * THROW create exception
5133 * ... try block
5134 * " catch {expr}"
5135 * JUMP -> finally
5136 * catch1: PUSH exeception
5137 * EVAL {expr}
5138 * MATCH
5139 * JUMP nomatch -> catch2
5140 * CATCH remove exception
5141 * ... catch block
5142 * " catch"
5143 * JUMP -> finally
5144 * catch2: CATCH remove exception
5145 * ... catch block
5146 * " finally"
5147 * finally:
5148 * ... finally block
5149 * " endtry"
5150 * ENDTRY pop trystack entry, may rethrow
5151 */
5152 static char_u *
5153compile_try(char_u *arg, cctx_T *cctx)
5154{
5155 garray_T *instr = &cctx->ctx_instr;
5156 scope_T *try_scope;
5157 scope_T *scope;
5158
5159 // scope that holds the jumps that go to catch/finally/endtry
5160 try_scope = new_scope(cctx, TRY_SCOPE);
5161 if (try_scope == NULL)
5162 return NULL;
5163
5164 // "catch" is set when the first ":catch" is found.
5165 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005166 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 if (generate_instr(cctx, ISN_TRY) == NULL)
5168 return NULL;
5169
5170 // scope for the try block itself
5171 scope = new_scope(cctx, BLOCK_SCOPE);
5172 if (scope == NULL)
5173 return NULL;
5174
5175 return arg;
5176}
5177
5178/*
5179 * compile "catch {expr}"
5180 */
5181 static char_u *
5182compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5183{
5184 scope_T *scope = cctx->ctx_scope;
5185 garray_T *instr = &cctx->ctx_instr;
5186 char_u *p;
5187 isn_T *isn;
5188
5189 // end block scope from :try or :catch
5190 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5191 compile_endblock(cctx);
5192 scope = cctx->ctx_scope;
5193
5194 // Error if not in a :try scope
5195 if (scope == NULL || scope->se_type != TRY_SCOPE)
5196 {
5197 emsg(_(e_catch));
5198 return NULL;
5199 }
5200
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005201 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 {
5203 emsg(_("E1033: catch unreachable after catch-all"));
5204 return NULL;
5205 }
5206
5207 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005208 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209 JUMP_ALWAYS, cctx) == FAIL)
5210 return NULL;
5211
5212 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005213 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 if (isn->isn_arg.try.try_catch == 0)
5215 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005216 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217 {
5218 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005219 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 isn->isn_arg.jump.jump_where = instr->ga_len;
5221 }
5222
5223 p = skipwhite(arg);
5224 if (ends_excmd(*p))
5225 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005226 scope->se_u.se_try.ts_caught_all = TRUE;
5227 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 }
5229 else
5230 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005231 char_u *end;
5232 char_u *pat;
5233 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005234 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005235 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 // Push v:exception, push {expr} and MATCH
5238 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5239
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005240 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005241 if (*end != *p)
5242 {
5243 semsg(_("E1067: Separator mismatch: %s"), p);
5244 vim_free(tofree);
5245 return FAIL;
5246 }
5247 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005248 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005249 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005250 len = (int)(end - tofree);
5251 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005252 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005253 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005254 if (pat == NULL)
5255 return FAIL;
5256 if (generate_PUSHS(cctx, pat) == FAIL)
5257 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5260 return NULL;
5261
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005262 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5264 return NULL;
5265 }
5266
5267 if (generate_instr(cctx, ISN_CATCH) == NULL)
5268 return NULL;
5269
5270 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5271 return NULL;
5272 return p;
5273}
5274
5275 static char_u *
5276compile_finally(char_u *arg, cctx_T *cctx)
5277{
5278 scope_T *scope = cctx->ctx_scope;
5279 garray_T *instr = &cctx->ctx_instr;
5280 isn_T *isn;
5281
5282 // end block scope from :try or :catch
5283 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5284 compile_endblock(cctx);
5285 scope = cctx->ctx_scope;
5286
5287 // Error if not in a :try scope
5288 if (scope == NULL || scope->se_type != TRY_SCOPE)
5289 {
5290 emsg(_(e_finally));
5291 return NULL;
5292 }
5293
5294 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005295 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296 if (isn->isn_arg.try.try_finally != 0)
5297 {
5298 emsg(_(e_finally_dup));
5299 return NULL;
5300 }
5301
5302 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005303 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304
Bram Moolenaar585fea72020-04-02 22:33:21 +02005305 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005306 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005307 {
5308 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005309 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310 isn->isn_arg.jump.jump_where = instr->ga_len;
5311 }
5312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005313 // TODO: set index in ts_finally_label jumps
5314
5315 return arg;
5316}
5317
5318 static char_u *
5319compile_endtry(char_u *arg, cctx_T *cctx)
5320{
5321 scope_T *scope = cctx->ctx_scope;
5322 garray_T *instr = &cctx->ctx_instr;
5323 isn_T *isn;
5324
5325 // end block scope from :catch or :finally
5326 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5327 compile_endblock(cctx);
5328 scope = cctx->ctx_scope;
5329
5330 // Error if not in a :try scope
5331 if (scope == NULL || scope->se_type != TRY_SCOPE)
5332 {
5333 if (scope == NULL)
5334 emsg(_(e_no_endtry));
5335 else if (scope->se_type == WHILE_SCOPE)
5336 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005337 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338 emsg(_(e_endfor));
5339 else
5340 emsg(_(e_endif));
5341 return NULL;
5342 }
5343
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005344 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5346 {
5347 emsg(_("E1032: missing :catch or :finally"));
5348 return NULL;
5349 }
5350
5351 // Fill in the "end" label in jumps at the end of the blocks, if not done
5352 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005353 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354
5355 // End :catch or :finally scope: set value in ISN_TRY instruction
5356 if (isn->isn_arg.try.try_finally == 0)
5357 isn->isn_arg.try.try_finally = instr->ga_len;
5358 compile_endblock(cctx);
5359
5360 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5361 return NULL;
5362 return arg;
5363}
5364
5365/*
5366 * compile "throw {expr}"
5367 */
5368 static char_u *
5369compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5370{
5371 char_u *p = skipwhite(arg);
5372
5373 if (ends_excmd(*p))
5374 {
5375 emsg(_(e_argreq));
5376 return NULL;
5377 }
5378 if (compile_expr1(&p, cctx) == FAIL)
5379 return NULL;
5380 if (may_generate_2STRING(-1, cctx) == FAIL)
5381 return NULL;
5382 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5383 return NULL;
5384
5385 return p;
5386}
5387
5388/*
5389 * compile "echo expr"
5390 */
5391 static char_u *
5392compile_echo(char_u *arg, int with_white, cctx_T *cctx)
5393{
5394 char_u *p = arg;
5395 int count = 0;
5396
Bram Moolenaarad39c092020-02-26 18:23:43 +01005397 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398 {
5399 if (compile_expr1(&p, cctx) == FAIL)
5400 return NULL;
5401 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005402 p = skipwhite(p);
5403 if (ends_excmd(*p))
5404 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405 }
5406
5407 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01005408 return p;
5409}
5410
5411/*
5412 * compile "execute expr"
5413 */
5414 static char_u *
5415compile_execute(char_u *arg, cctx_T *cctx)
5416{
5417 char_u *p = arg;
5418 int count = 0;
5419
5420 for (;;)
5421 {
5422 if (compile_expr1(&p, cctx) == FAIL)
5423 return NULL;
5424 ++count;
5425 p = skipwhite(p);
5426 if (ends_excmd(*p))
5427 break;
5428 }
5429
5430 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431
5432 return p;
5433}
5434
5435/*
5436 * After ex_function() has collected all the function lines: parse and compile
5437 * the lines into instructions.
5438 * Adds the function to "def_functions".
5439 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5440 * return statement (used for lambda).
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005441 * This can be used recursively through compile_lambda(), which may reallocate
5442 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005443 */
5444 void
5445compile_def_function(ufunc_T *ufunc, int set_return_type)
5446{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447 char_u *line = NULL;
5448 char_u *p;
5449 exarg_T ea;
5450 char *errormsg = NULL; // error message
5451 int had_return = FALSE;
5452 cctx_T cctx;
5453 garray_T *instr;
5454 int called_emsg_before = called_emsg;
5455 int ret = FAIL;
5456 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005457 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005460 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005461
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005462 if (ufunc->uf_dfunc_idx >= 0)
5463 {
5464 // Redefining a function that was compiled before.
5465 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5466
5467 // Free old instructions.
5468 delete_def_function_contents(dfunc);
5469 }
5470 else
5471 {
5472 // Add the function to "def_functions".
5473 if (ga_grow(&def_functions, 1) == FAIL)
5474 return;
5475 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
5476 vim_memset(dfunc, 0, sizeof(dfunc_T));
5477 dfunc->df_idx = def_functions.ga_len;
5478 ufunc->uf_dfunc_idx = dfunc->df_idx;
5479 dfunc->df_ufunc = ufunc;
5480 ++def_functions.ga_len;
5481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482 }
5483
5484 vim_memset(&cctx, 0, sizeof(cctx));
5485 cctx.ctx_ufunc = ufunc;
5486 cctx.ctx_lnum = -1;
5487 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5488 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5489 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5490 cctx.ctx_type_list = &ufunc->uf_type_list;
5491 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5492 instr = &cctx.ctx_instr;
5493
5494 // Most modern script version.
5495 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5496
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005497 if (ufunc->uf_def_args.ga_len > 0)
5498 {
5499 int count = ufunc->uf_def_args.ga_len;
5500 int i;
5501 char_u *arg;
5502 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5503
5504 // Produce instructions for the default values of optional arguments.
5505 // Store the instruction index in uf_def_arg_idx[] so that we know
5506 // where to start when the function is called, depending on the number
5507 // of arguments.
5508 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5509 if (ufunc->uf_def_arg_idx == NULL)
5510 goto erret;
5511 for (i = 0; i < count; ++i)
5512 {
5513 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5514 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
5515 if (compile_expr1(&arg, &cctx) == FAIL
5516 || generate_STORE(&cctx, ISN_STORE,
5517 i - count - off, NULL) == FAIL)
5518 goto erret;
5519 }
5520
5521 // If a varargs is following, push an empty list.
5522 if (ufunc->uf_va_name != NULL)
5523 {
5524 if (generate_NEWLIST(&cctx, 0) == FAIL
5525 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
5526 goto erret;
5527 }
5528
5529 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5530 }
5531
5532 /*
5533 * Loop over all the lines of the function and generate instructions.
5534 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535 for (;;)
5536 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005537 int is_ex_command;
5538
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005539 // Bail out on the first error to avoid a flood of errors and report
5540 // the right line number when inside try/catch.
5541 if (emsg_before != called_emsg)
5542 goto erret;
5543
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544 if (line != NULL && *line == '|')
5545 // the line continues after a '|'
5546 ++line;
5547 else if (line != NULL && *line != NUL)
5548 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005549 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550 goto erret;
5551 }
5552 else
5553 {
5554 do
5555 {
5556 ++cctx.ctx_lnum;
5557 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5558 break;
5559 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
5560 } while (line == NULL);
5561 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5562 break;
5563 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
5564 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005565 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005566
5567 had_return = FALSE;
5568 vim_memset(&ea, 0, sizeof(ea));
5569 ea.cmdlinep = &line;
5570 ea.cmd = skipwhite(line);
5571
5572 // "}" ends a block scope
5573 if (*ea.cmd == '}')
5574 {
5575 scopetype_T stype = cctx.ctx_scope == NULL
5576 ? NO_SCOPE : cctx.ctx_scope->se_type;
5577
5578 if (stype == BLOCK_SCOPE)
5579 {
5580 compile_endblock(&cctx);
5581 line = ea.cmd;
5582 }
5583 else
5584 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005585 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005586 goto erret;
5587 }
5588 if (line != NULL)
5589 line = skipwhite(ea.cmd + 1);
5590 continue;
5591 }
5592
5593 // "{" starts a block scope
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01005594 // "{'a': 1}->func() is something else
5595 if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005596 {
5597 line = compile_block(ea.cmd, &cctx);
5598 continue;
5599 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005600 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005601
5602 /*
5603 * COMMAND MODIFIERS
5604 */
5605 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5606 {
5607 if (errormsg != NULL)
5608 goto erret;
5609 // empty line or comment
5610 line = (char_u *)"";
5611 continue;
5612 }
5613
5614 // Skip ":call" to get to the function name.
5615 if (checkforcmd(&ea.cmd, "call", 3))
5616 ea.cmd = skipwhite(ea.cmd);
5617
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005618 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005619 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005620 // Assuming the command starts with a variable or function name,
5621 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5622 // val".
5623 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5624 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005625 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005626 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005627 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005628 int oplen;
5629 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005630
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005631 oplen = assignment_len(skipwhite(p), &heredoc);
5632 if (oplen > 0)
5633 {
5634 // Recognize an assignment if we recognize the variable
5635 // name:
5636 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005637 // "local = expr" where "local" is a local var.
5638 // "script = expr" where "script" is a script-local var.
5639 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005640 // "&opt = expr"
5641 // "$ENV = expr"
5642 // "@r = expr"
5643 if (*ea.cmd == '&'
5644 || *ea.cmd == '$'
5645 || *ea.cmd == '@'
5646 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5647 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5648 || lookup_script(ea.cmd, p - ea.cmd) == OK
5649 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5650 {
5651 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5652 if (line == NULL)
5653 goto erret;
5654 continue;
5655 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005656 }
5657 }
5658 }
5659
5660 /*
5661 * COMMAND after range
5662 */
5663 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005664 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5665 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666
5667 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5668 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005669 if (cctx.ctx_skip == TRUE)
5670 {
5671 line += STRLEN(line);
5672 continue;
5673 }
5674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005675 // Expression or function call.
5676 if (ea.cmdidx == CMD_eval)
5677 {
5678 p = ea.cmd;
5679 if (compile_expr1(&p, &cctx) == FAIL)
5680 goto erret;
5681
5682 // drop the return value
5683 generate_instr_drop(&cctx, ISN_DROP, 1);
5684 line = p;
5685 continue;
5686 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02005687 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005688 iemsg("Command from find_ex_command() not handled");
5689 goto erret;
5690 }
5691
5692 p = skipwhite(p);
5693
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005694 if (cctx.ctx_skip == TRUE
5695 && ea.cmdidx != CMD_elseif
5696 && ea.cmdidx != CMD_else
5697 && ea.cmdidx != CMD_endif)
5698 {
5699 line += STRLEN(line);
5700 continue;
5701 }
5702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005703 switch (ea.cmdidx)
5704 {
5705 case CMD_def:
5706 case CMD_function:
5707 // TODO: Nested function
5708 emsg("Nested function not implemented yet");
5709 goto erret;
5710
5711 case CMD_return:
5712 line = compile_return(p, set_return_type, &cctx);
5713 had_return = TRUE;
5714 break;
5715
5716 case CMD_let:
5717 case CMD_const:
5718 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5719 break;
5720
5721 case CMD_import:
5722 line = compile_import(p, &cctx);
5723 break;
5724
5725 case CMD_if:
5726 line = compile_if(p, &cctx);
5727 break;
5728 case CMD_elseif:
5729 line = compile_elseif(p, &cctx);
5730 break;
5731 case CMD_else:
5732 line = compile_else(p, &cctx);
5733 break;
5734 case CMD_endif:
5735 line = compile_endif(p, &cctx);
5736 break;
5737
5738 case CMD_while:
5739 line = compile_while(p, &cctx);
5740 break;
5741 case CMD_endwhile:
5742 line = compile_endwhile(p, &cctx);
5743 break;
5744
5745 case CMD_for:
5746 line = compile_for(p, &cctx);
5747 break;
5748 case CMD_endfor:
5749 line = compile_endfor(p, &cctx);
5750 break;
5751 case CMD_continue:
5752 line = compile_continue(p, &cctx);
5753 break;
5754 case CMD_break:
5755 line = compile_break(p, &cctx);
5756 break;
5757
5758 case CMD_try:
5759 line = compile_try(p, &cctx);
5760 break;
5761 case CMD_catch:
5762 line = compile_catch(p, &cctx);
5763 break;
5764 case CMD_finally:
5765 line = compile_finally(p, &cctx);
5766 break;
5767 case CMD_endtry:
5768 line = compile_endtry(p, &cctx);
5769 break;
5770 case CMD_throw:
5771 line = compile_throw(p, &cctx);
5772 break;
5773
5774 case CMD_echo:
5775 line = compile_echo(p, TRUE, &cctx);
5776 break;
5777 case CMD_echon:
5778 line = compile_echo(p, FALSE, &cctx);
5779 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005780 case CMD_execute:
5781 line = compile_execute(p, &cctx);
5782 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005783
5784 default:
5785 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005786 // TODO:
5787 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005788 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005789 generate_EXEC(&cctx, line);
5790 line = (char_u *)"";
5791 break;
5792 }
5793 if (line == NULL)
5794 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02005795 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005796
5797 if (cctx.ctx_type_stack.ga_len < 0)
5798 {
5799 iemsg("Type stack underflow");
5800 goto erret;
5801 }
5802 }
5803
5804 if (cctx.ctx_scope != NULL)
5805 {
5806 if (cctx.ctx_scope->se_type == IF_SCOPE)
5807 emsg(_(e_endif));
5808 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5809 emsg(_(e_endwhile));
5810 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5811 emsg(_(e_endfor));
5812 else
5813 emsg(_("E1026: Missing }"));
5814 goto erret;
5815 }
5816
5817 if (!had_return)
5818 {
5819 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5820 {
5821 emsg(_("E1027: Missing return statement"));
5822 goto erret;
5823 }
5824
5825 // Return zero if there is no return at the end.
5826 generate_PUSHNR(&cctx, 0);
5827 generate_instr(&cctx, ISN_RETURN);
5828 }
5829
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005830 {
5831 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5832 + ufunc->uf_dfunc_idx;
5833 dfunc->df_deleted = FALSE;
5834 dfunc->df_instr = instr->ga_data;
5835 dfunc->df_instr_count = instr->ga_len;
5836 dfunc->df_varcount = cctx.ctx_max_local;
5837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005838
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005839 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005840 int varargs = ufunc->uf_va_name != NULL;
5841 int argcount = ufunc->uf_args.ga_len - (varargs ? 1 : 0);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005842
5843 // Create a type for the function, with the return type and any
5844 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005845 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5846 // The type is included in "tt_args".
5847 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5848 ufunc->uf_args.ga_len, &ufunc->uf_type_list);
5849 if (ufunc->uf_args.ga_len > 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005850 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005851 if (func_type_add_arg_types(ufunc->uf_func_type,
5852 ufunc->uf_args.ga_len,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005853 argcount - ufunc->uf_def_args.ga_len,
5854 &ufunc->uf_type_list) == FAIL)
5855 {
5856 ret = FAIL;
5857 goto erret;
5858 }
5859 if (ufunc->uf_arg_types == NULL)
5860 {
5861 int i;
5862
5863 // lambda does not have argument types.
5864 for (i = 0; i < argcount; ++i)
5865 ufunc->uf_func_type->tt_args[i] = &t_any;
5866 }
5867 else
5868 mch_memmove(ufunc->uf_func_type->tt_args,
5869 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005870 if (varargs)
5871 ufunc->uf_func_type->tt_args[argcount] =
5872 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005873 }
5874 }
5875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005876 ret = OK;
5877
5878erret:
5879 if (ret == FAIL)
5880 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005881 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005882 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5883 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005884
5885 for (idx = 0; idx < instr->ga_len; ++idx)
5886 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005887 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005889 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005890 if (!dfunc->df_deleted)
5891 --def_functions.ga_len;
5892
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005893 while (cctx.ctx_scope != NULL)
5894 drop_scope(&cctx);
5895
Bram Moolenaar20431c92020-03-20 18:39:46 +01005896 // Don't execute this function body.
5897 ga_clear_strings(&ufunc->uf_lines);
5898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005899 if (errormsg != NULL)
5900 emsg(errormsg);
5901 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005902 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005903 }
5904
5905 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005906 free_imported(&cctx);
5907 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005908 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005909}
5910
5911/*
5912 * Delete an instruction, free what it contains.
5913 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01005914 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915delete_instr(isn_T *isn)
5916{
5917 switch (isn->isn_type)
5918 {
5919 case ISN_EXEC:
5920 case ISN_LOADENV:
5921 case ISN_LOADG:
5922 case ISN_LOADOPT:
5923 case ISN_MEMBER:
5924 case ISN_PUSHEXC:
5925 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005926 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005927 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005928 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005929 vim_free(isn->isn_arg.string);
5930 break;
5931
5932 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005933 case ISN_STORES:
5934 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005935 break;
5936
5937 case ISN_STOREOPT:
5938 vim_free(isn->isn_arg.storeopt.so_name);
5939 break;
5940
5941 case ISN_PUSHBLOB: // push blob isn_arg.blob
5942 blob_unref(isn->isn_arg.blob);
5943 break;
5944
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005945 case ISN_PUSHPARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01005946 partial_unref(isn->isn_arg.partial);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005947 break;
5948
5949 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005950#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005951 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005952#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005953 break;
5954
5955 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005956#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005957 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005958#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005959 break;
5960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961 case ISN_UCALL:
5962 vim_free(isn->isn_arg.ufunc.cuf_name);
5963 break;
5964
5965 case ISN_2BOOL:
5966 case ISN_2STRING:
5967 case ISN_ADDBLOB:
5968 case ISN_ADDLIST:
5969 case ISN_BCALL:
5970 case ISN_CATCH:
5971 case ISN_CHECKNR:
5972 case ISN_CHECKTYPE:
5973 case ISN_COMPAREANY:
5974 case ISN_COMPAREBLOB:
5975 case ISN_COMPAREBOOL:
5976 case ISN_COMPAREDICT:
5977 case ISN_COMPAREFLOAT:
5978 case ISN_COMPAREFUNC:
5979 case ISN_COMPARELIST:
5980 case ISN_COMPARENR:
5981 case ISN_COMPAREPARTIAL:
5982 case ISN_COMPARESPECIAL:
5983 case ISN_COMPARESTRING:
5984 case ISN_CONCAT:
5985 case ISN_DCALL:
5986 case ISN_DROP:
5987 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01005988 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005989 case ISN_ENDTRY:
5990 case ISN_FOR:
5991 case ISN_FUNCREF:
5992 case ISN_INDEX:
5993 case ISN_JUMP:
5994 case ISN_LOAD:
5995 case ISN_LOADSCRIPT:
5996 case ISN_LOADREG:
5997 case ISN_LOADV:
5998 case ISN_NEGATENR:
5999 case ISN_NEWDICT:
6000 case ISN_NEWLIST:
6001 case ISN_OPNR:
6002 case ISN_OPFLOAT:
6003 case ISN_OPANY:
6004 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006005 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006006 case ISN_PUSHF:
6007 case ISN_PUSHNR:
6008 case ISN_PUSHBOOL:
6009 case ISN_PUSHSPEC:
6010 case ISN_RETURN:
6011 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006012 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006013 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006014 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 case ISN_STORESCRIPT:
6016 case ISN_THROW:
6017 case ISN_TRY:
6018 // nothing allocated
6019 break;
6020 }
6021}
6022
6023/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006024 * Free all instructions for "dfunc".
6025 */
6026 static void
6027delete_def_function_contents(dfunc_T *dfunc)
6028{
6029 int idx;
6030
6031 ga_clear(&dfunc->df_def_args_isn);
6032
6033 if (dfunc->df_instr != NULL)
6034 {
6035 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6036 delete_instr(dfunc->df_instr + idx);
6037 VIM_CLEAR(dfunc->df_instr);
6038 }
6039
6040 dfunc->df_deleted = TRUE;
6041}
6042
6043/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006044 * When a user function is deleted, delete any associated def function.
6045 */
6046 void
6047delete_def_function(ufunc_T *ufunc)
6048{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006049 if (ufunc->uf_dfunc_idx >= 0)
6050 {
6051 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6052 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053
Bram Moolenaar20431c92020-03-20 18:39:46 +01006054 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055 }
6056}
6057
6058#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006059/*
6060 * Free all functions defined with ":def".
6061 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062 void
6063free_def_functions(void)
6064{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006065 int idx;
6066
6067 for (idx = 0; idx < def_functions.ga_len; ++idx)
6068 {
6069 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6070
6071 delete_def_function_contents(dfunc);
6072 }
6073
6074 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006075}
6076#endif
6077
6078
6079#endif // FEAT_EVAL