blob: 18fbdd36ed882a758bb79cb0fc01e363f43a9bfd [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 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200317 if (ret_type == &t_unknown)
318 {
319 // (argcount == 0) is not possible
320 return &t_func_unknown;
321 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200322 if (ret_type == &t_void)
323 {
324 if (argcount == 0)
325 return &t_func_0_void;
326 else
327 return &t_func_void;
328 }
329 if (ret_type == &t_any)
330 {
331 if (argcount == 0)
332 return &t_func_0_any;
333 else
334 return &t_func_any;
335 }
336 if (ret_type == &t_number)
337 {
338 if (argcount == 0)
339 return &t_func_0_number;
340 else
341 return &t_func_number;
342 }
343 if (ret_type == &t_string)
344 {
345 if (argcount == 0)
346 return &t_func_0_string;
347 else
348 return &t_func_string;
349 }
350 }
351
352 // Not a common type or has arguments, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200353 type = alloc_type(type_gap);
354 if (type == NULL)
Bram Moolenaard77a8522020-04-03 21:59:57 +0200355 return &t_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200356 type->tt_type = VAR_FUNC;
357 type->tt_member = ret_type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200358 type->tt_argcount = argcount;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200359 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100360 return type;
361}
362
Bram Moolenaara8c17702020-04-01 21:17:24 +0200363/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200364 * For a function type, reserve space for "argcount" argument types (including
365 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200366 */
367 static int
368func_type_add_arg_types(
369 type_T *functype,
370 int argcount,
371 int min_argcount,
372 garray_T *type_gap)
373{
374 if (ga_grow(type_gap, 1) == FAIL)
375 return FAIL;
376 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
377 if (functype->tt_args == NULL)
378 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200379 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
380 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200381 ++type_gap->ga_len;
382
383 functype->tt_argcount = argcount;
384 functype->tt_min_argcount = min_argcount;
385 return OK;
386}
387
388/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200389 * Return the type_T for a typval. Only for primitive types.
390 */
391 static type_T *
392typval2type(typval_T *tv)
393{
394 if (tv->v_type == VAR_NUMBER)
395 return &t_number;
396 if (tv->v_type == VAR_BOOL)
397 return &t_bool;
398 if (tv->v_type == VAR_STRING)
399 return &t_string;
400 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
401 return &t_list_string;
402 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
403 return &t_dict_any;
404 return &t_any;
405}
406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407/////////////////////////////////////////////////////////////////////
408// Following generate_ functions expect the caller to call ga_grow().
409
Bram Moolenaar080457c2020-03-03 21:53:32 +0100410#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
411#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413/*
414 * Generate an instruction without arguments.
415 * Returns a pointer to the new instruction, NULL if failed.
416 */
417 static isn_T *
418generate_instr(cctx_T *cctx, isntype_T isn_type)
419{
420 garray_T *instr = &cctx->ctx_instr;
421 isn_T *isn;
422
Bram Moolenaar080457c2020-03-03 21:53:32 +0100423 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100424 if (ga_grow(instr, 1) == FAIL)
425 return NULL;
426 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
427 isn->isn_type = isn_type;
428 isn->isn_lnum = cctx->ctx_lnum + 1;
429 ++instr->ga_len;
430
431 return isn;
432}
433
434/*
435 * Generate an instruction without arguments.
436 * "drop" will be removed from the stack.
437 * Returns a pointer to the new instruction, NULL if failed.
438 */
439 static isn_T *
440generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
441{
442 garray_T *stack = &cctx->ctx_type_stack;
443
Bram Moolenaar080457c2020-03-03 21:53:32 +0100444 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100445 stack->ga_len -= drop;
446 return generate_instr(cctx, isn_type);
447}
448
449/*
450 * Generate instruction "isn_type" and put "type" on the type stack.
451 */
452 static isn_T *
453generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
454{
455 isn_T *isn;
456 garray_T *stack = &cctx->ctx_type_stack;
457
458 if ((isn = generate_instr(cctx, isn_type)) == NULL)
459 return NULL;
460
461 if (ga_grow(stack, 1) == FAIL)
462 return NULL;
463 ((type_T **)stack->ga_data)[stack->ga_len] = type;
464 ++stack->ga_len;
465
466 return isn;
467}
468
469/*
470 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
471 */
472 static int
473may_generate_2STRING(int offset, cctx_T *cctx)
474{
475 isn_T *isn;
476 garray_T *stack = &cctx->ctx_type_stack;
477 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
478
479 if ((*type)->tt_type == VAR_STRING)
480 return OK;
481 *type = &t_string;
482
483 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
484 return FAIL;
485 isn->isn_arg.number = offset;
486
487 return OK;
488}
489
490 static int
491check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
492{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200493 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200495 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 {
497 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100498 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100499 else
500 semsg(_("E1036: %c requires number or float arguments"), *op);
501 return FAIL;
502 }
503 return OK;
504}
505
506/*
507 * Generate an instruction with two arguments. The instruction depends on the
508 * type of the arguments.
509 */
510 static int
511generate_two_op(cctx_T *cctx, char_u *op)
512{
513 garray_T *stack = &cctx->ctx_type_stack;
514 type_T *type1;
515 type_T *type2;
516 vartype_T vartype;
517 isn_T *isn;
518
Bram Moolenaar080457c2020-03-03 21:53:32 +0100519 RETURN_OK_IF_SKIP(cctx);
520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 // Get the known type of the two items on the stack. If they are matching
522 // use a type-specific instruction. Otherwise fall back to runtime type
523 // checking.
524 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
525 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200526 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 if (type1->tt_type == type2->tt_type
528 && (type1->tt_type == VAR_NUMBER
529 || type1->tt_type == VAR_LIST
530#ifdef FEAT_FLOAT
531 || type1->tt_type == VAR_FLOAT
532#endif
533 || type1->tt_type == VAR_BLOB))
534 vartype = type1->tt_type;
535
536 switch (*op)
537 {
538 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200539 && type1->tt_type != VAR_ANY
540 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541 && check_number_or_float(
542 type1->tt_type, type2->tt_type, op) == FAIL)
543 return FAIL;
544 isn = generate_instr_drop(cctx,
545 vartype == VAR_NUMBER ? ISN_OPNR
546 : vartype == VAR_LIST ? ISN_ADDLIST
547 : vartype == VAR_BLOB ? ISN_ADDBLOB
548#ifdef FEAT_FLOAT
549 : vartype == VAR_FLOAT ? ISN_OPFLOAT
550#endif
551 : ISN_OPANY, 1);
552 if (isn != NULL)
553 isn->isn_arg.op.op_type = EXPR_ADD;
554 break;
555
556 case '-':
557 case '*':
558 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
559 op) == FAIL)
560 return FAIL;
561 if (vartype == VAR_NUMBER)
562 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
563#ifdef FEAT_FLOAT
564 else if (vartype == VAR_FLOAT)
565 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
566#endif
567 else
568 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
569 if (isn != NULL)
570 isn->isn_arg.op.op_type = *op == '*'
571 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
572 break;
573
Bram Moolenaar4c683752020-04-05 21:38:23 +0200574 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200576 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 && type2->tt_type != VAR_NUMBER))
578 {
579 emsg(_("E1035: % requires number arguments"));
580 return FAIL;
581 }
582 isn = generate_instr_drop(cctx,
583 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
584 if (isn != NULL)
585 isn->isn_arg.op.op_type = EXPR_REM;
586 break;
587 }
588
589 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200590 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 {
592 type_T *type = &t_any;
593
594#ifdef FEAT_FLOAT
595 // float+number and number+float results in float
596 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
597 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
598 type = &t_float;
599#endif
600 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
601 }
602
603 return OK;
604}
605
606/*
607 * Generate an ISN_COMPARE* instruction with a boolean result.
608 */
609 static int
610generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
611{
612 isntype_T isntype = ISN_DROP;
613 isn_T *isn;
614 garray_T *stack = &cctx->ctx_type_stack;
615 vartype_T type1;
616 vartype_T type2;
617
Bram Moolenaar080457c2020-03-03 21:53:32 +0100618 RETURN_OK_IF_SKIP(cctx);
619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620 // Get the known type of the two items on the stack. If they are matching
621 // use a type-specific instruction. Otherwise fall back to runtime type
622 // checking.
623 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
624 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200625 if (type1 == VAR_UNKNOWN)
626 type1 = VAR_ANY;
627 if (type2 == VAR_UNKNOWN)
628 type2 = VAR_ANY;
629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100630 if (type1 == type2)
631 {
632 switch (type1)
633 {
634 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
635 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
636 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
637 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
638 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
639 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
640 case VAR_LIST: isntype = ISN_COMPARELIST; break;
641 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
642 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
643 case VAR_PARTIAL: isntype = ISN_COMPAREPARTIAL; break;
644 default: isntype = ISN_COMPAREANY; break;
645 }
646 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200647 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100648 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
649 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
650 isntype = ISN_COMPAREANY;
651
652 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
653 && (isntype == ISN_COMPAREBOOL
654 || isntype == ISN_COMPARESPECIAL
655 || isntype == ISN_COMPARENR
656 || isntype == ISN_COMPAREFLOAT))
657 {
658 semsg(_("E1037: Cannot use \"%s\" with %s"),
659 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
660 return FAIL;
661 }
662 if (isntype == ISN_DROP
663 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
664 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
665 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
666 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
667 && exptype != EXPR_IS && exptype != EXPR_ISNOT
668 && (type1 == VAR_BLOB || type2 == VAR_BLOB
669 || type1 == VAR_LIST || type2 == VAR_LIST))))
670 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100671 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 vartype_name(type1), vartype_name(type2));
673 return FAIL;
674 }
675
676 if ((isn = generate_instr(cctx, isntype)) == NULL)
677 return FAIL;
678 isn->isn_arg.op.op_type = exptype;
679 isn->isn_arg.op.op_ic = ic;
680
681 // takes two arguments, puts one bool back
682 if (stack->ga_len >= 2)
683 {
684 --stack->ga_len;
685 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
686 }
687
688 return OK;
689}
690
691/*
692 * Generate an ISN_2BOOL instruction.
693 */
694 static int
695generate_2BOOL(cctx_T *cctx, int invert)
696{
697 isn_T *isn;
698 garray_T *stack = &cctx->ctx_type_stack;
699
Bram Moolenaar080457c2020-03-03 21:53:32 +0100700 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
702 return FAIL;
703 isn->isn_arg.number = invert;
704
705 // type becomes bool
706 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
707
708 return OK;
709}
710
711 static int
712generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
713{
714 isn_T *isn;
715 garray_T *stack = &cctx->ctx_type_stack;
716
Bram Moolenaar080457c2020-03-03 21:53:32 +0100717 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
719 return FAIL;
720 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
721 isn->isn_arg.type.ct_off = offset;
722
723 // type becomes vartype
724 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
725
726 return OK;
727}
728
729/*
730 * Generate an ISN_PUSHNR instruction.
731 */
732 static int
733generate_PUSHNR(cctx_T *cctx, varnumber_T number)
734{
735 isn_T *isn;
736
Bram Moolenaar080457c2020-03-03 21:53:32 +0100737 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
739 return FAIL;
740 isn->isn_arg.number = number;
741
742 return OK;
743}
744
745/*
746 * Generate an ISN_PUSHBOOL instruction.
747 */
748 static int
749generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
750{
751 isn_T *isn;
752
Bram Moolenaar080457c2020-03-03 21:53:32 +0100753 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
755 return FAIL;
756 isn->isn_arg.number = number;
757
758 return OK;
759}
760
761/*
762 * Generate an ISN_PUSHSPEC instruction.
763 */
764 static int
765generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
766{
767 isn_T *isn;
768
Bram Moolenaar080457c2020-03-03 21:53:32 +0100769 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
771 return FAIL;
772 isn->isn_arg.number = number;
773
774 return OK;
775}
776
777#ifdef FEAT_FLOAT
778/*
779 * Generate an ISN_PUSHF instruction.
780 */
781 static int
782generate_PUSHF(cctx_T *cctx, float_T fnumber)
783{
784 isn_T *isn;
785
Bram Moolenaar080457c2020-03-03 21:53:32 +0100786 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
788 return FAIL;
789 isn->isn_arg.fnumber = fnumber;
790
791 return OK;
792}
793#endif
794
795/*
796 * Generate an ISN_PUSHS instruction.
797 * Consumes "str".
798 */
799 static int
800generate_PUSHS(cctx_T *cctx, char_u *str)
801{
802 isn_T *isn;
803
Bram Moolenaar080457c2020-03-03 21:53:32 +0100804 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
806 return FAIL;
807 isn->isn_arg.string = str;
808
809 return OK;
810}
811
812/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100813 * Generate an ISN_PUSHCHANNEL instruction.
814 * Consumes "channel".
815 */
816 static int
817generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
818{
819 isn_T *isn;
820
Bram Moolenaar080457c2020-03-03 21:53:32 +0100821 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100822 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
823 return FAIL;
824 isn->isn_arg.channel = channel;
825
826 return OK;
827}
828
829/*
830 * Generate an ISN_PUSHJOB instruction.
831 * Consumes "job".
832 */
833 static int
834generate_PUSHJOB(cctx_T *cctx, job_T *job)
835{
836 isn_T *isn;
837
Bram Moolenaar080457c2020-03-03 21:53:32 +0100838 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100839 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100840 return FAIL;
841 isn->isn_arg.job = job;
842
843 return OK;
844}
845
846/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 * Generate an ISN_PUSHBLOB instruction.
848 * Consumes "blob".
849 */
850 static int
851generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
852{
853 isn_T *isn;
854
Bram Moolenaar080457c2020-03-03 21:53:32 +0100855 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
857 return FAIL;
858 isn->isn_arg.blob = blob;
859
860 return OK;
861}
862
863/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100864 * Generate an ISN_PUSHFUNC instruction with name "name".
865 * Consumes "name".
866 */
867 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200868generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100869{
870 isn_T *isn;
871
Bram Moolenaar080457c2020-03-03 21:53:32 +0100872 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200873 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100874 return FAIL;
875 isn->isn_arg.string = name;
876
877 return OK;
878}
879
880/*
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100881 * Generate an ISN_PUSHPARTIAL instruction with partial "part".
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200882 * Consumes "part".
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100883 */
884 static int
885generate_PUSHPARTIAL(cctx_T *cctx, partial_T *part)
886{
887 isn_T *isn;
888
Bram Moolenaar080457c2020-03-03 21:53:32 +0100889 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaard77a8522020-04-03 21:59:57 +0200890 if ((isn = generate_instr_type(cctx, ISN_PUSHPARTIAL, &t_func_any)) == NULL)
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100891 return FAIL;
892 isn->isn_arg.partial = part;
893
894 return OK;
895}
896
897/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 * Generate an ISN_STORE instruction.
899 */
900 static int
901generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
902{
903 isn_T *isn;
904
Bram Moolenaar080457c2020-03-03 21:53:32 +0100905 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
907 return FAIL;
908 if (name != NULL)
909 isn->isn_arg.string = vim_strsave(name);
910 else
911 isn->isn_arg.number = idx;
912
913 return OK;
914}
915
916/*
917 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
918 */
919 static int
920generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
921{
922 isn_T *isn;
923
Bram Moolenaar080457c2020-03-03 21:53:32 +0100924 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
926 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100927 isn->isn_arg.storenr.stnr_idx = idx;
928 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929
930 return OK;
931}
932
933/*
934 * Generate an ISN_STOREOPT instruction
935 */
936 static int
937generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
938{
939 isn_T *isn;
940
Bram Moolenaar080457c2020-03-03 21:53:32 +0100941 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
943 return FAIL;
944 isn->isn_arg.storeopt.so_name = vim_strsave(name);
945 isn->isn_arg.storeopt.so_flags = opt_flags;
946
947 return OK;
948}
949
950/*
951 * Generate an ISN_LOAD or similar instruction.
952 */
953 static int
954generate_LOAD(
955 cctx_T *cctx,
956 isntype_T isn_type,
957 int idx,
958 char_u *name,
959 type_T *type)
960{
961 isn_T *isn;
962
Bram Moolenaar080457c2020-03-03 21:53:32 +0100963 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
965 return FAIL;
966 if (name != NULL)
967 isn->isn_arg.string = vim_strsave(name);
968 else
969 isn->isn_arg.number = idx;
970
971 return OK;
972}
973
974/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100975 * Generate an ISN_LOADV instruction.
976 */
977 static int
978generate_LOADV(
979 cctx_T *cctx,
980 char_u *name,
981 int error)
982{
983 // load v:var
984 int vidx = find_vim_var(name);
985
Bram Moolenaar080457c2020-03-03 21:53:32 +0100986 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100987 if (vidx < 0)
988 {
989 if (error)
990 semsg(_(e_var_notfound), name);
991 return FAIL;
992 }
993
994 // TODO: get actual type
995 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
996}
997
998/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 * Generate an ISN_LOADS instruction.
1000 */
1001 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001002generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001004 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001006 int sid,
1007 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008{
1009 isn_T *isn;
1010
Bram Moolenaar080457c2020-03-03 21:53:32 +01001011 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001012 if (isn_type == ISN_LOADS)
1013 isn = generate_instr_type(cctx, isn_type, type);
1014 else
1015 isn = generate_instr_drop(cctx, isn_type, 1);
1016 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001018 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1019 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020
1021 return OK;
1022}
1023
1024/*
1025 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1026 */
1027 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001028generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 cctx_T *cctx,
1030 isntype_T isn_type,
1031 int sid,
1032 int idx,
1033 type_T *type)
1034{
1035 isn_T *isn;
1036
Bram Moolenaar080457c2020-03-03 21:53:32 +01001037 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038 if (isn_type == ISN_LOADSCRIPT)
1039 isn = generate_instr_type(cctx, isn_type, type);
1040 else
1041 isn = generate_instr_drop(cctx, isn_type, 1);
1042 if (isn == NULL)
1043 return FAIL;
1044 isn->isn_arg.script.script_sid = sid;
1045 isn->isn_arg.script.script_idx = idx;
1046 return OK;
1047}
1048
1049/*
1050 * Generate an ISN_NEWLIST instruction.
1051 */
1052 static int
1053generate_NEWLIST(cctx_T *cctx, int count)
1054{
1055 isn_T *isn;
1056 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 type_T *type;
1058 type_T *member;
1059
Bram Moolenaar080457c2020-03-03 21:53:32 +01001060 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1062 return FAIL;
1063 isn->isn_arg.number = count;
1064
1065 // drop the value types
1066 stack->ga_len -= count;
1067
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001068 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001069 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 if (count > 0)
1071 member = ((type_T **)stack->ga_data)[stack->ga_len];
1072 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001073 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001074 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075
1076 // add the list type to the type stack
1077 if (ga_grow(stack, 1) == FAIL)
1078 return FAIL;
1079 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1080 ++stack->ga_len;
1081
1082 return OK;
1083}
1084
1085/*
1086 * Generate an ISN_NEWDICT instruction.
1087 */
1088 static int
1089generate_NEWDICT(cctx_T *cctx, int count)
1090{
1091 isn_T *isn;
1092 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 type_T *type;
1094 type_T *member;
1095
Bram Moolenaar080457c2020-03-03 21:53:32 +01001096 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1098 return FAIL;
1099 isn->isn_arg.number = count;
1100
1101 // drop the key and value types
1102 stack->ga_len -= 2 * count;
1103
Bram Moolenaar436472f2020-02-20 22:54:43 +01001104 // Use the first value type for the list member type. Use "void" for an
1105 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 if (count > 0)
1107 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1108 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001109 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001110 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111
1112 // add the dict type to the type stack
1113 if (ga_grow(stack, 1) == FAIL)
1114 return FAIL;
1115 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1116 ++stack->ga_len;
1117
1118 return OK;
1119}
1120
1121/*
1122 * Generate an ISN_FUNCREF instruction.
1123 */
1124 static int
1125generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1126{
1127 isn_T *isn;
1128 garray_T *stack = &cctx->ctx_type_stack;
1129
Bram Moolenaar080457c2020-03-03 21:53:32 +01001130 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1132 return FAIL;
1133 isn->isn_arg.number = dfunc_idx;
1134
1135 if (ga_grow(stack, 1) == FAIL)
1136 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001137 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 // TODO: argument and return types
1139 ++stack->ga_len;
1140
1141 return OK;
1142}
1143
1144/*
1145 * Generate an ISN_JUMP instruction.
1146 */
1147 static int
1148generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1149{
1150 isn_T *isn;
1151 garray_T *stack = &cctx->ctx_type_stack;
1152
Bram Moolenaar080457c2020-03-03 21:53:32 +01001153 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1155 return FAIL;
1156 isn->isn_arg.jump.jump_when = when;
1157 isn->isn_arg.jump.jump_where = where;
1158
1159 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1160 --stack->ga_len;
1161
1162 return OK;
1163}
1164
1165 static int
1166generate_FOR(cctx_T *cctx, int loop_idx)
1167{
1168 isn_T *isn;
1169 garray_T *stack = &cctx->ctx_type_stack;
1170
Bram Moolenaar080457c2020-03-03 21:53:32 +01001171 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1173 return FAIL;
1174 isn->isn_arg.forloop.for_idx = loop_idx;
1175
1176 if (ga_grow(stack, 1) == FAIL)
1177 return FAIL;
1178 // type doesn't matter, will be stored next
1179 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1180 ++stack->ga_len;
1181
1182 return OK;
1183}
1184
1185/*
1186 * Generate an ISN_BCALL instruction.
1187 * Return FAIL if the number of arguments is wrong.
1188 */
1189 static int
1190generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1191{
1192 isn_T *isn;
1193 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001194 type_T *argtypes[MAX_FUNC_ARGS];
1195 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196
Bram Moolenaar080457c2020-03-03 21:53:32 +01001197 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 if (check_internal_func(func_idx, argcount) == FAIL)
1199 return FAIL;
1200
1201 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1202 return FAIL;
1203 isn->isn_arg.bfunc.cbf_idx = func_idx;
1204 isn->isn_arg.bfunc.cbf_argcount = argcount;
1205
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001206 for (i = 0; i < argcount; ++i)
1207 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 stack->ga_len -= argcount; // drop the arguments
1210 if (ga_grow(stack, 1) == FAIL)
1211 return FAIL;
1212 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001213 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 ++stack->ga_len; // add return value
1215
1216 return OK;
1217}
1218
1219/*
1220 * Generate an ISN_DCALL or ISN_UCALL instruction.
1221 * Return FAIL if the number of arguments is wrong.
1222 */
1223 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001224generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225{
1226 isn_T *isn;
1227 garray_T *stack = &cctx->ctx_type_stack;
1228 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001229 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230
Bram Moolenaar080457c2020-03-03 21:53:32 +01001231 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232 if (argcount > regular_args && !has_varargs(ufunc))
1233 {
1234 semsg(_(e_toomanyarg), ufunc->uf_name);
1235 return FAIL;
1236 }
1237 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1238 {
1239 semsg(_(e_toofewarg), ufunc->uf_name);
1240 return FAIL;
1241 }
1242
1243 // Turn varargs into a list.
1244 if (ufunc->uf_va_name != NULL)
1245 {
1246 int count = argcount - regular_args;
1247
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001248 // If count is negative an empty list will be added after evaluating
1249 // default values for missing optional arguments.
1250 if (count >= 0)
1251 {
1252 generate_NEWLIST(cctx, count);
1253 argcount = regular_args + 1;
1254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 }
1256
1257 if ((isn = generate_instr(cctx,
1258 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1259 return FAIL;
1260 if (ufunc->uf_dfunc_idx >= 0)
1261 {
1262 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1263 isn->isn_arg.dfunc.cdf_argcount = argcount;
1264 }
1265 else
1266 {
1267 // A user function may be deleted and redefined later, can't use the
1268 // ufunc pointer, need to look it up again at runtime.
1269 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1270 isn->isn_arg.ufunc.cuf_argcount = argcount;
1271 }
1272
1273 stack->ga_len -= argcount; // drop the arguments
1274 if (ga_grow(stack, 1) == FAIL)
1275 return FAIL;
1276 // add return value
1277 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1278 ++stack->ga_len;
1279
1280 return OK;
1281}
1282
1283/*
1284 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1285 */
1286 static int
1287generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1288{
1289 isn_T *isn;
1290 garray_T *stack = &cctx->ctx_type_stack;
1291
Bram Moolenaar080457c2020-03-03 21:53:32 +01001292 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1294 return FAIL;
1295 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1296 isn->isn_arg.ufunc.cuf_argcount = argcount;
1297
1298 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001299 if (ga_grow(stack, 1) == FAIL)
1300 return FAIL;
1301 // add return value
1302 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1303 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304
1305 return OK;
1306}
1307
1308/*
1309 * Generate an ISN_PCALL instruction.
1310 */
1311 static int
1312generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1313{
1314 isn_T *isn;
1315 garray_T *stack = &cctx->ctx_type_stack;
1316
Bram Moolenaar080457c2020-03-03 21:53:32 +01001317 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1319 return FAIL;
1320 isn->isn_arg.pfunc.cpf_top = at_top;
1321 isn->isn_arg.pfunc.cpf_argcount = argcount;
1322
1323 stack->ga_len -= argcount; // drop the arguments
1324
1325 // drop the funcref/partial, get back the return value
1326 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1327
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001328 // If partial is above the arguments it must be cleared and replaced with
1329 // the return value.
1330 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1331 return FAIL;
1332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 return OK;
1334}
1335
1336/*
1337 * Generate an ISN_MEMBER instruction.
1338 */
1339 static int
1340generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1341{
1342 isn_T *isn;
1343 garray_T *stack = &cctx->ctx_type_stack;
1344 type_T *type;
1345
Bram Moolenaar080457c2020-03-03 21:53:32 +01001346 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1348 return FAIL;
1349 isn->isn_arg.string = vim_strnsave(name, (int)len);
1350
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001351 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001353 if (type->tt_type != VAR_DICT && type != &t_any)
1354 {
1355 emsg(_(e_dictreq));
1356 return FAIL;
1357 }
1358 // change dict type to dict member type
1359 if (type->tt_type == VAR_DICT)
1360 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361
1362 return OK;
1363}
1364
1365/*
1366 * Generate an ISN_ECHO instruction.
1367 */
1368 static int
1369generate_ECHO(cctx_T *cctx, int with_white, int count)
1370{
1371 isn_T *isn;
1372
Bram Moolenaar080457c2020-03-03 21:53:32 +01001373 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1375 return FAIL;
1376 isn->isn_arg.echo.echo_with_white = with_white;
1377 isn->isn_arg.echo.echo_count = count;
1378
1379 return OK;
1380}
1381
Bram Moolenaarad39c092020-02-26 18:23:43 +01001382/*
1383 * Generate an ISN_EXECUTE instruction.
1384 */
1385 static int
1386generate_EXECUTE(cctx_T *cctx, int count)
1387{
1388 isn_T *isn;
1389
1390 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1391 return FAIL;
1392 isn->isn_arg.number = count;
1393
1394 return OK;
1395}
1396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 static int
1398generate_EXEC(cctx_T *cctx, char_u *line)
1399{
1400 isn_T *isn;
1401
Bram Moolenaar080457c2020-03-03 21:53:32 +01001402 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1404 return FAIL;
1405 isn->isn_arg.string = vim_strsave(line);
1406 return OK;
1407}
1408
1409static char e_white_both[] =
1410 N_("E1004: white space required before and after '%s'");
Bram Moolenaard77a8522020-04-03 21:59:57 +02001411static char e_white_after[] = N_("E1069: white space required after '%s'");
1412static char e_no_white_before[] = N_("E1068: No white space allowed before '%s'");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413
1414/*
1415 * Reserve space for a local variable.
1416 * Return the index or -1 if it failed.
1417 */
1418 static int
1419reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1420{
1421 int idx;
1422 lvar_T *lvar;
1423
1424 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1425 {
1426 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1427 return -1;
1428 }
1429
1430 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1431 return -1;
1432 idx = cctx->ctx_locals.ga_len;
1433 if (cctx->ctx_max_local < idx + 1)
1434 cctx->ctx_max_local = idx + 1;
1435 ++cctx->ctx_locals.ga_len;
1436
1437 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1438 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1439 lvar->lv_const = isConst;
1440 lvar->lv_type = type;
1441
1442 return idx;
1443}
1444
1445/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001446 * Remove local variables above "new_top".
1447 */
1448 static void
1449unwind_locals(cctx_T *cctx, int new_top)
1450{
1451 if (cctx->ctx_locals.ga_len > new_top)
1452 {
1453 int idx;
1454 lvar_T *lvar;
1455
1456 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1457 {
1458 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1459 vim_free(lvar->lv_name);
1460 }
1461 }
1462 cctx->ctx_locals.ga_len = new_top;
1463}
1464
1465/*
1466 * Free all local variables.
1467 */
1468 static void
1469free_local(cctx_T *cctx)
1470{
1471 unwind_locals(cctx, 0);
1472 ga_clear(&cctx->ctx_locals);
1473}
1474
1475/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 * Skip over a type definition and return a pointer to just after it.
1477 */
1478 char_u *
1479skip_type(char_u *start)
1480{
1481 char_u *p = start;
1482
1483 while (ASCII_ISALNUM(*p) || *p == '_')
1484 ++p;
1485
1486 // Skip over "<type>"; this is permissive about white space.
1487 if (*skipwhite(p) == '<')
1488 {
1489 p = skipwhite(p);
1490 p = skip_type(skipwhite(p + 1));
1491 p = skipwhite(p);
1492 if (*p == '>')
1493 ++p;
1494 }
1495 return p;
1496}
1497
1498/*
1499 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001500 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 * Returns NULL in case of failure.
1502 */
1503 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001504parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505{
1506 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001507 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508
1509 if (**arg != '<')
1510 {
1511 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001512 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 else
1514 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001515 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516 }
1517 *arg = skipwhite(*arg + 1);
1518
Bram Moolenaard77a8522020-04-03 21:59:57 +02001519 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520
1521 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001522 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 {
1524 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001525 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 }
1527 ++*arg;
1528
1529 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001530 return get_list_type(member_type, type_gap);
1531 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532}
1533
1534/*
1535 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001536 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 */
1538 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001539parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540{
1541 char_u *p = *arg;
1542 size_t len;
1543
1544 // skip over the first word
1545 while (ASCII_ISALNUM(*p) || *p == '_')
1546 ++p;
1547 len = p - *arg;
1548
1549 switch (**arg)
1550 {
1551 case 'a':
1552 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1553 {
1554 *arg += len;
1555 return &t_any;
1556 }
1557 break;
1558 case 'b':
1559 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1560 {
1561 *arg += len;
1562 return &t_bool;
1563 }
1564 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1565 {
1566 *arg += len;
1567 return &t_blob;
1568 }
1569 break;
1570 case 'c':
1571 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1572 {
1573 *arg += len;
1574 return &t_channel;
1575 }
1576 break;
1577 case 'd':
1578 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1579 {
1580 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001581 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 }
1583 break;
1584 case 'f':
1585 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1586 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001587#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 *arg += len;
1589 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001590#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001591 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001592 return &t_any;
1593#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 }
1595 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1596 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001597 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001598 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001599 int argcount = -1;
1600 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001601 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001602 type_T *arg_type[MAX_FUNC_ARGS + 1];
1603
1604 // func({type}, ...): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001606 if (**arg == '(')
1607 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001608 // "func" may or may not return a value, "func()" does
1609 // not return a value.
1610 ret_type = &t_void;
1611
Bram Moolenaard77a8522020-04-03 21:59:57 +02001612 p = ++*arg;
1613 argcount = 0;
1614 while (*p != NUL && *p != ')')
1615 {
1616 if (STRNCMP(p, "...", 3) == 0)
1617 {
1618 flags |= TTFLAG_VARARGS;
1619 break;
1620 }
1621 arg_type[argcount++] = parse_type(&p, type_gap);
1622
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001623 if (*p == '?')
1624 {
1625 if (first_optional == -1)
1626 first_optional = argcount;
1627 ++p;
1628 }
1629 else if (first_optional != -1)
1630 {
1631 emsg(_("E1007: mandatory argument after optional argument"));
1632 return &t_any;
1633 }
1634
Bram Moolenaard77a8522020-04-03 21:59:57 +02001635 if (*p != ',' && *skipwhite(p) == ',')
1636 {
1637 semsg(_(e_no_white_before), ",");
1638 return &t_any;
1639 }
1640 if (*p == ',')
1641 {
1642 ++p;
1643 if (!VIM_ISWHITE(*p))
1644 semsg(_(e_white_after), ",");
1645 }
1646 p = skipwhite(p);
1647 if (argcount == MAX_FUNC_ARGS)
1648 {
1649 emsg(_("E740: Too many argument types"));
1650 return &t_any;
1651 }
1652 }
1653
1654 p = skipwhite(p);
1655 if (*p != ')')
1656 {
1657 emsg(_(e_missing_close));
1658 return &t_any;
1659 }
1660 *arg = p + 1;
1661 }
1662 if (**arg == ':')
1663 {
1664 // parse return type
1665 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001666 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001667 semsg(_(e_white_after), ":");
1668 *arg = skipwhite(*arg);
1669 ret_type = parse_type(arg, type_gap);
1670 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001671 type = get_func_type(ret_type,
1672 flags == 0 && first_optional == -1 ? argcount : 99,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001673 type_gap);
1674 if (flags != 0)
1675 type->tt_flags = flags;
1676 if (argcount > 0)
1677 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001678 if (func_type_add_arg_types(type, argcount,
1679 first_optional == -1 ? argcount : first_optional,
1680 type_gap) == FAIL)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001681 return &t_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001682 mch_memmove(type->tt_args, arg_type,
1683 sizeof(type_T *) * argcount);
1684 }
1685 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 }
1687 break;
1688 case 'j':
1689 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1690 {
1691 *arg += len;
1692 return &t_job;
1693 }
1694 break;
1695 case 'l':
1696 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1697 {
1698 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001699 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 }
1701 break;
1702 case 'n':
1703 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1704 {
1705 *arg += len;
1706 return &t_number;
1707 }
1708 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 case 's':
1710 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1711 {
1712 *arg += len;
1713 return &t_string;
1714 }
1715 break;
1716 case 'v':
1717 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1718 {
1719 *arg += len;
1720 return &t_void;
1721 }
1722 break;
1723 }
1724
1725 semsg(_("E1010: Type not recognized: %s"), *arg);
1726 return &t_any;
1727}
1728
1729/*
1730 * Check if "type1" and "type2" are exactly the same.
1731 */
1732 static int
1733equal_type(type_T *type1, type_T *type2)
1734{
1735 if (type1->tt_type != type2->tt_type)
1736 return FALSE;
1737 switch (type1->tt_type)
1738 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001740 case VAR_ANY:
1741 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 case VAR_SPECIAL:
1743 case VAR_BOOL:
1744 case VAR_NUMBER:
1745 case VAR_FLOAT:
1746 case VAR_STRING:
1747 case VAR_BLOB:
1748 case VAR_JOB:
1749 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001750 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 case VAR_LIST:
1752 case VAR_DICT:
1753 return equal_type(type1->tt_member, type2->tt_member);
1754 case VAR_FUNC:
1755 case VAR_PARTIAL:
1756 // TODO; check argument types.
1757 return equal_type(type1->tt_member, type2->tt_member)
1758 && type1->tt_argcount == type2->tt_argcount;
1759 }
1760 return TRUE;
1761}
1762
1763/*
1764 * Find the common type of "type1" and "type2" and put it in "dest".
1765 * "type2" and "dest" may be the same.
1766 */
1767 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02001768common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769{
1770 if (equal_type(type1, type2))
1771 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001772 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 return;
1774 }
1775
1776 if (type1->tt_type == type2->tt_type)
1777 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1779 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001780 type_T *common;
1781
Bram Moolenaard77a8522020-04-03 21:59:57 +02001782 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001783 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001784 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001785 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001786 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 return;
1788 }
1789 // TODO: VAR_FUNC and VAR_PARTIAL
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001790 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 }
1792
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001793 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794}
1795
1796 char *
1797vartype_name(vartype_T type)
1798{
1799 switch (type)
1800 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001801 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02001802 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 case VAR_SPECIAL: return "special";
1805 case VAR_BOOL: return "bool";
1806 case VAR_NUMBER: return "number";
1807 case VAR_FLOAT: return "float";
1808 case VAR_STRING: return "string";
1809 case VAR_BLOB: return "blob";
1810 case VAR_JOB: return "job";
1811 case VAR_CHANNEL: return "channel";
1812 case VAR_LIST: return "list";
1813 case VAR_DICT: return "dict";
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001814 case VAR_FUNC: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 case VAR_PARTIAL: return "partial";
1816 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02001817 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818}
1819
1820/*
1821 * Return the name of a type.
1822 * The result may be in allocated memory, in which case "tofree" is set.
1823 */
1824 char *
1825type_name(type_T *type, char **tofree)
1826{
1827 char *name = vartype_name(type->tt_type);
1828
1829 *tofree = NULL;
1830 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1831 {
1832 char *member_free;
1833 char *member_name = type_name(type->tt_member, &member_free);
1834 size_t len;
1835
1836 len = STRLEN(name) + STRLEN(member_name) + 3;
1837 *tofree = alloc(len);
1838 if (*tofree != NULL)
1839 {
1840 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1841 vim_free(member_free);
1842 return *tofree;
1843 }
1844 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001845 if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1846 {
1847 garray_T ga;
1848 int i;
1849
1850 ga_init2(&ga, 1, 100);
1851 if (ga_grow(&ga, 20) == FAIL)
1852 return "[unknown]";
1853 *tofree = ga.ga_data;
1854 STRCPY(ga.ga_data, "func(");
1855 ga.ga_len += 5;
1856
1857 for (i = 0; i < type->tt_argcount; ++i)
1858 {
1859 char *arg_free;
1860 char *arg_type = type_name(type->tt_args[i], &arg_free);
1861 int len;
1862
1863 if (i > 0)
1864 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001865 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001866 ga.ga_len += 2;
1867 }
1868 len = (int)STRLEN(arg_type);
1869 if (ga_grow(&ga, len + 6) == FAIL)
1870 {
1871 vim_free(arg_free);
1872 return "[unknown]";
1873 }
1874 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001875 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001876 ga.ga_len += len;
1877 vim_free(arg_free);
1878 }
1879
1880 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001881 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001882 else
1883 {
1884 char *ret_free;
1885 char *ret_name = type_name(type->tt_member, &ret_free);
1886 int len;
1887
1888 len = (int)STRLEN(ret_name) + 4;
1889 if (ga_grow(&ga, len) == FAIL)
1890 {
1891 vim_free(ret_free);
1892 return "[unknown]";
1893 }
1894 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001895 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
1896 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001897 vim_free(ret_free);
1898 }
1899 return ga.ga_data;
1900 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901
1902 return name;
1903}
1904
1905/*
1906 * Find "name" in script-local items of script "sid".
1907 * Returns the index in "sn_var_vals" if found.
1908 * If found but not in "sn_var_vals" returns -1.
1909 * If not found returns -2.
1910 */
1911 int
1912get_script_item_idx(int sid, char_u *name, int check_writable)
1913{
1914 hashtab_T *ht;
1915 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001916 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 int idx;
1918
1919 // First look the name up in the hashtable.
1920 if (sid <= 0 || sid > script_items.ga_len)
1921 return -1;
1922 ht = &SCRIPT_VARS(sid);
1923 di = find_var_in_ht(ht, 0, name, TRUE);
1924 if (di == NULL)
1925 return -2;
1926
1927 // Now find the svar_T index in sn_var_vals.
1928 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1929 {
1930 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1931
1932 if (sv->sv_tv == &di->di_tv)
1933 {
1934 if (check_writable && sv->sv_const)
1935 semsg(_(e_readonlyvar), name);
1936 return idx;
1937 }
1938 }
1939 return -1;
1940}
1941
1942/*
1943 * Find "name" in imported items of the current script/
1944 */
1945 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001946find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001948 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 int idx;
1950
1951 if (cctx != NULL)
1952 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1953 {
1954 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1955 + idx;
1956
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001957 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1958 : STRLEN(import->imp_name) == len
1959 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 return import;
1961 }
1962
1963 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1964 {
1965 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1966
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001967 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1968 : STRLEN(import->imp_name) == len
1969 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 return import;
1971 }
1972 return NULL;
1973}
1974
1975/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001976 * Free all imported variables.
1977 */
1978 static void
1979free_imported(cctx_T *cctx)
1980{
1981 int idx;
1982
1983 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1984 {
1985 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1986
1987 vim_free(import->imp_name);
1988 }
1989 ga_clear(&cctx->ctx_imports);
1990}
1991
1992/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001993 * Generate an instruction to load script-local variable "name", without the
1994 * leading "s:".
1995 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 */
1997 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001998compile_load_scriptvar(
1999 cctx_T *cctx,
2000 char_u *name, // variable NUL terminated
2001 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002002 char_u **end, // end of variable
2003 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002005 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2007 imported_T *import;
2008
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002009 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002011 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002012 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2013 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 }
2015 if (idx >= 0)
2016 {
2017 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2018
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002019 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 current_sctx.sc_sid, idx, sv->sv_type);
2021 return OK;
2022 }
2023
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002024 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 if (import != NULL)
2026 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002027 if (import->imp_all)
2028 {
2029 char_u *p = skipwhite(*end);
2030 int name_len;
2031 ufunc_T *ufunc;
2032 type_T *type;
2033
2034 // Used "import * as Name", need to lookup the member.
2035 if (*p != '.')
2036 {
2037 semsg(_("E1060: expected dot after name: %s"), start);
2038 return FAIL;
2039 }
2040 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002041 if (VIM_ISWHITE(*p))
2042 {
2043 emsg(_("E1074: no white space allowed after dot"));
2044 return FAIL;
2045 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002046
2047 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2048 // TODO: what if it is a function?
2049 if (idx < 0)
2050 return FAIL;
2051 *end = p;
2052
2053 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2054 import->imp_sid,
2055 idx,
2056 type);
2057 }
2058 else
2059 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002060 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002061 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2062 import->imp_sid,
2063 import->imp_var_vals_idx,
2064 import->imp_type);
2065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 return OK;
2067 }
2068
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002069 if (error)
2070 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 return FAIL;
2072}
2073
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002074 static int
2075generate_funcref(cctx_T *cctx, char_u *name)
2076{
2077 ufunc_T *ufunc = find_func(name, cctx);
2078
2079 if (ufunc == NULL)
2080 return FAIL;
2081
2082 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2083}
2084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085/*
2086 * Compile a variable name into a load instruction.
2087 * "end" points to just after the name.
2088 * When "error" is FALSE do not give an error when not found.
2089 */
2090 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002091compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092{
2093 type_T *type;
2094 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002095 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002097 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098
2099 if (*(*arg + 1) == ':')
2100 {
2101 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002102 if (end <= *arg + 2)
2103 name = vim_strsave((char_u *)"[empty]");
2104 else
2105 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 if (name == NULL)
2107 return FAIL;
2108
2109 if (**arg == 'v')
2110 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002111 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 }
2113 else if (**arg == 'g')
2114 {
2115 // Global variables can be defined later, thus we don't check if it
2116 // exists, give error at runtime.
2117 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2118 }
2119 else if (**arg == 's')
2120 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002121 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002123 else if (**arg == 'b')
2124 {
2125 semsg("Namespace b: not supported yet: %s", *arg);
2126 goto theend;
2127 }
2128 else if (**arg == 'w')
2129 {
2130 semsg("Namespace w: not supported yet: %s", *arg);
2131 goto theend;
2132 }
2133 else if (**arg == 't')
2134 {
2135 semsg("Namespace t: not supported yet: %s", *arg);
2136 goto theend;
2137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 else
2139 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002140 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 goto theend;
2142 }
2143 }
2144 else
2145 {
2146 size_t len = end - *arg;
2147 int idx;
2148 int gen_load = FALSE;
2149
2150 name = vim_strnsave(*arg, end - *arg);
2151 if (name == NULL)
2152 return FAIL;
2153
2154 idx = lookup_arg(*arg, len, cctx);
2155 if (idx >= 0)
2156 {
2157 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2158 type = cctx->ctx_ufunc->uf_arg_types[idx];
2159 else
2160 type = &t_any;
2161
2162 // Arguments are located above the frame pointer.
2163 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2164 if (cctx->ctx_ufunc->uf_va_name != NULL)
2165 --idx;
2166 gen_load = TRUE;
2167 }
2168 else if (lookup_vararg(*arg, len, cctx))
2169 {
2170 // varargs is always the last argument
2171 idx = -STACK_FRAME_SIZE - 1;
2172 type = cctx->ctx_ufunc->uf_va_type;
2173 gen_load = TRUE;
2174 }
2175 else
2176 {
2177 idx = lookup_local(*arg, len, cctx);
2178 if (idx >= 0)
2179 {
2180 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
2181 gen_load = TRUE;
2182 }
2183 else
2184 {
2185 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2186 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2187 res = generate_PUSHBOOL(cctx, **arg == 't'
2188 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002189 else
2190 {
2191 // "var" can be script-local even without using "s:" if it
2192 // already exists.
2193 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2194 == SCRIPT_VERSION_VIM9
2195 || lookup_script(*arg, len) == OK)
2196 res = compile_load_scriptvar(cctx, name, *arg, &end,
2197 FALSE);
2198
2199 // When the name starts with an uppercase letter or "x:" it
2200 // can be a user defined function.
2201 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2202 res = generate_funcref(cctx, name);
2203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 }
2205 }
2206 if (gen_load)
2207 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
2208 }
2209
2210 *arg = end;
2211
2212theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002213 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 semsg(_(e_var_notfound), name);
2215 vim_free(name);
2216 return res;
2217}
2218
2219/*
2220 * Compile the argument expressions.
2221 * "arg" points to just after the "(" and is advanced to after the ")"
2222 */
2223 static int
2224compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2225{
2226 char_u *p = *arg;
2227
2228 while (*p != NUL && *p != ')')
2229 {
2230 if (compile_expr1(&p, cctx) == FAIL)
2231 return FAIL;
2232 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002233
2234 if (*p != ',' && *skipwhite(p) == ',')
2235 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002236 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002237 p = skipwhite(p);
2238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002240 {
2241 ++p;
2242 if (!VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002243 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002244 }
2245 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002247 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 if (*p != ')')
2249 {
2250 emsg(_(e_missing_close));
2251 return FAIL;
2252 }
2253 *arg = p + 1;
2254 return OK;
2255}
2256
2257/*
2258 * Compile a function call: name(arg1, arg2)
2259 * "arg" points to "name", "arg + varlen" to the "(".
2260 * "argcount_init" is 1 for "value->method()"
2261 * Instructions:
2262 * EVAL arg1
2263 * EVAL arg2
2264 * BCALL / DCALL / UCALL
2265 */
2266 static int
2267compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2268{
2269 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002270 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 int argcount = argcount_init;
2272 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002273 char_u fname_buf[FLEN_FIXED + 1];
2274 char_u *tofree = NULL;
2275 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002277 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278
2279 if (varlen >= sizeof(namebuf))
2280 {
2281 semsg(_("E1011: name too long: %s"), name);
2282 return FAIL;
2283 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002284 vim_strncpy(namebuf, *arg, varlen);
2285 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286
2287 *arg = skipwhite(*arg + varlen + 1);
2288 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002289 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002291 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 {
2293 int idx;
2294
2295 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002296 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002298 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002299 else
2300 semsg(_(e_unknownfunc), namebuf);
2301 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 }
2303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002305 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002307 {
2308 res = generate_CALL(cctx, ufunc, argcount);
2309 goto theend;
2310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311
2312 // If the name is a variable, load it and use PCALL.
2313 p = namebuf;
2314 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002315 {
2316 res = generate_PCALL(cctx, argcount, FALSE);
2317 goto theend;
2318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319
2320 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002321 res = generate_UCALL(cctx, name, argcount);
2322
2323theend:
2324 vim_free(tofree);
2325 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326}
2327
2328// like NAMESPACE_CHAR but with 'a' and 'l'.
2329#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2330
2331/*
2332 * Find the end of a variable or function name. Unlike find_name_end() this
2333 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002334 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 * Return a pointer to just after the name. Equal to "arg" if there is no
2336 * valid name.
2337 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002338 static char_u *
2339to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340{
2341 char_u *p;
2342
2343 // Quick check for valid starting character.
2344 if (!eval_isnamec1(*arg))
2345 return arg;
2346
2347 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2348 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2349 // and can be used in slice "[n:]".
2350 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002351 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2353 break;
2354 return p;
2355}
2356
2357/*
2358 * Like to_name_end() but also skip over a list or dict constant.
2359 */
2360 char_u *
2361to_name_const_end(char_u *arg)
2362{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002363 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 typval_T rettv;
2365
2366 if (p == arg && *arg == '[')
2367 {
2368
2369 // Can be "[1, 2, 3]->Func()".
2370 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2371 p = arg;
2372 }
2373 else if (p == arg && *arg == '#' && arg[1] == '{')
2374 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002375 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 ++p;
2377 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2378 p = arg;
2379 }
2380 else if (p == arg && *arg == '{')
2381 {
2382 int ret = get_lambda_tv(&p, &rettv, FALSE);
2383
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002384 // Can be "{x -> ret}()".
2385 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 if (ret == NOTDONE)
2387 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2388 if (ret != OK)
2389 p = arg;
2390 }
2391
2392 return p;
2393}
2394
2395 static void
2396type_mismatch(type_T *expected, type_T *actual)
2397{
2398 char *tofree1, *tofree2;
2399
2400 semsg(_("E1013: type mismatch, expected %s but got %s"),
2401 type_name(expected, &tofree1), type_name(actual, &tofree2));
2402 vim_free(tofree1);
2403 vim_free(tofree2);
2404}
2405
2406/*
2407 * Check if the expected and actual types match.
2408 */
2409 static int
2410check_type(type_T *expected, type_T *actual, int give_msg)
2411{
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002412 int ret = OK;
2413
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002414 // When expected is "unknown" we accept any actual type.
2415 // When expected is "any" we accept any actual type except "void".
2416 if (expected->tt_type != VAR_UNKNOWN
2417 && (expected->tt_type != VAR_ANY || actual->tt_type == VAR_VOID))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 {
2419 if (expected->tt_type != actual->tt_type)
2420 {
2421 if (give_msg)
2422 type_mismatch(expected, actual);
2423 return FAIL;
2424 }
2425 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2426 {
Bram Moolenaar4c683752020-04-05 21:38:23 +02002427 // "unknown" is used for an empty list or dict
2428 if (actual->tt_member != &t_unknown)
Bram Moolenaar436472f2020-02-20 22:54:43 +01002429 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002431 else if (expected->tt_type == VAR_FUNC)
2432 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002433 if (expected->tt_member != &t_unknown)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002434 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
2435 if (ret == OK && expected->tt_argcount != -1
2436 && (actual->tt_argcount < expected->tt_min_argcount
2437 || actual->tt_argcount > expected->tt_argcount))
2438 ret = FAIL;
2439 }
2440 if (ret == FAIL && give_msg)
2441 type_mismatch(expected, actual);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 }
Bram Moolenaar89228602020-04-05 22:14:54 +02002443 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444}
2445
2446/*
2447 * Check that
2448 * - "actual" is "expected" type or
2449 * - "actual" is a type that can be "expected" type: add a runtime check; or
2450 * - return FAIL.
2451 */
2452 static int
2453need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2454{
Bram Moolenaar89228602020-04-05 22:14:54 +02002455 if (check_type(expected, actual, FALSE) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 return OK;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002457 if (actual->tt_type != VAR_ANY && actual->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 {
2459 type_mismatch(expected, actual);
2460 return FAIL;
2461 }
2462 generate_TYPECHECK(cctx, expected, offset);
2463 return OK;
2464}
2465
2466/*
2467 * parse a list: [expr, expr]
2468 * "*arg" points to the '['.
2469 */
2470 static int
2471compile_list(char_u **arg, cctx_T *cctx)
2472{
2473 char_u *p = skipwhite(*arg + 1);
2474 int count = 0;
2475
2476 while (*p != ']')
2477 {
2478 if (*p == NUL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002479 {
2480 semsg(_(e_list_end), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 return FAIL;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002482 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 if (compile_expr1(&p, cctx) == FAIL)
2484 break;
2485 ++count;
2486 if (*p == ',')
2487 ++p;
2488 p = skipwhite(p);
2489 }
2490 *arg = p + 1;
2491
2492 generate_NEWLIST(cctx, count);
2493 return OK;
2494}
2495
2496/*
2497 * parse a lambda: {arg, arg -> expr}
2498 * "*arg" points to the '{'.
2499 */
2500 static int
2501compile_lambda(char_u **arg, cctx_T *cctx)
2502{
2503 garray_T *instr = &cctx->ctx_instr;
2504 typval_T rettv;
2505 ufunc_T *ufunc;
2506
2507 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002508 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002512 ++ufunc->uf_refcount;
2513 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002514 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515
2516 // The function will have one line: "return {expr}".
2517 // Compile it into instructions.
2518 compile_def_function(ufunc, TRUE);
2519
2520 if (ufunc->uf_dfunc_idx >= 0)
2521 {
2522 if (ga_grow(instr, 1) == FAIL)
2523 return FAIL;
2524 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2525 return OK;
2526 }
2527 return FAIL;
2528}
2529
2530/*
2531 * Compile a lamda call: expr->{lambda}(args)
2532 * "arg" points to the "{".
2533 */
2534 static int
2535compile_lambda_call(char_u **arg, cctx_T *cctx)
2536{
2537 ufunc_T *ufunc;
2538 typval_T rettv;
2539 int argcount = 1;
2540 int ret = FAIL;
2541
2542 // Get the funcref in "rettv".
2543 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2544 return FAIL;
2545
2546 if (**arg != '(')
2547 {
2548 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002549 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 else
2551 semsg(_(e_missing_paren), "lambda");
2552 clear_tv(&rettv);
2553 return FAIL;
2554 }
2555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 ufunc = rettv.vval.v_partial->pt_func;
2557 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002558 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002559 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002560
2561 // The function will have one line: "return {expr}".
2562 // Compile it into instructions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 compile_def_function(ufunc, TRUE);
2564
2565 // compile the arguments
2566 *arg = skipwhite(*arg + 1);
2567 if (compile_arguments(arg, cctx, &argcount) == OK)
2568 // call the compiled function
2569 ret = generate_CALL(cctx, ufunc, argcount);
2570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 return ret;
2572}
2573
2574/*
2575 * parse a dict: {'key': val} or #{key: val}
2576 * "*arg" points to the '{'.
2577 */
2578 static int
2579compile_dict(char_u **arg, cctx_T *cctx, int literal)
2580{
2581 garray_T *instr = &cctx->ctx_instr;
2582 int count = 0;
2583 dict_T *d = dict_alloc();
2584 dictitem_T *item;
2585
2586 if (d == NULL)
2587 return FAIL;
2588 *arg = skipwhite(*arg + 1);
2589 while (**arg != '}' && **arg != NUL)
2590 {
2591 char_u *key = NULL;
2592
2593 if (literal)
2594 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002595 char_u *p = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596
2597 if (p == *arg)
2598 {
2599 semsg(_("E1014: Invalid key: %s"), *arg);
2600 return FAIL;
2601 }
2602 key = vim_strnsave(*arg, p - *arg);
2603 if (generate_PUSHS(cctx, key) == FAIL)
2604 return FAIL;
2605 *arg = p;
2606 }
2607 else
2608 {
2609 isn_T *isn;
2610
2611 if (compile_expr1(arg, cctx) == FAIL)
2612 return FAIL;
2613 // TODO: check type is string
2614 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2615 if (isn->isn_type == ISN_PUSHS)
2616 key = isn->isn_arg.string;
2617 }
2618
2619 // Check for duplicate keys, if using string keys.
2620 if (key != NULL)
2621 {
2622 item = dict_find(d, key, -1);
2623 if (item != NULL)
2624 {
2625 semsg(_(e_duplicate_key), key);
2626 goto failret;
2627 }
2628 item = dictitem_alloc(key);
2629 if (item != NULL)
2630 {
2631 item->di_tv.v_type = VAR_UNKNOWN;
2632 item->di_tv.v_lock = 0;
2633 if (dict_add(d, item) == FAIL)
2634 dictitem_free(item);
2635 }
2636 }
2637
2638 *arg = skipwhite(*arg);
2639 if (**arg != ':')
2640 {
2641 semsg(_(e_missing_dict_colon), *arg);
2642 return FAIL;
2643 }
2644
2645 *arg = skipwhite(*arg + 1);
2646 if (compile_expr1(arg, cctx) == FAIL)
2647 return FAIL;
2648 ++count;
2649
2650 if (**arg == '}')
2651 break;
2652 if (**arg != ',')
2653 {
2654 semsg(_(e_missing_dict_comma), *arg);
2655 goto failret;
2656 }
2657 *arg = skipwhite(*arg + 1);
2658 }
2659
2660 if (**arg != '}')
2661 {
2662 semsg(_(e_missing_dict_end), *arg);
2663 goto failret;
2664 }
2665 *arg = *arg + 1;
2666
2667 dict_unref(d);
2668 return generate_NEWDICT(cctx, count);
2669
2670failret:
2671 dict_unref(d);
2672 return FAIL;
2673}
2674
2675/*
2676 * Compile "&option".
2677 */
2678 static int
2679compile_get_option(char_u **arg, cctx_T *cctx)
2680{
2681 typval_T rettv;
2682 char_u *start = *arg;
2683 int ret;
2684
2685 // parse the option and get the current value to get the type.
2686 rettv.v_type = VAR_UNKNOWN;
2687 ret = get_option_tv(arg, &rettv, TRUE);
2688 if (ret == OK)
2689 {
2690 // include the '&' in the name, get_option_tv() expects it.
2691 char_u *name = vim_strnsave(start, *arg - start);
2692 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2693
2694 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2695 vim_free(name);
2696 }
2697 clear_tv(&rettv);
2698
2699 return ret;
2700}
2701
2702/*
2703 * Compile "$VAR".
2704 */
2705 static int
2706compile_get_env(char_u **arg, cctx_T *cctx)
2707{
2708 char_u *start = *arg;
2709 int len;
2710 int ret;
2711 char_u *name;
2712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 ++*arg;
2714 len = get_env_len(arg);
2715 if (len == 0)
2716 {
2717 semsg(_(e_syntax_at), start - 1);
2718 return FAIL;
2719 }
2720
2721 // include the '$' in the name, get_env_tv() expects it.
2722 name = vim_strnsave(start, len + 1);
2723 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2724 vim_free(name);
2725 return ret;
2726}
2727
2728/*
2729 * Compile "@r".
2730 */
2731 static int
2732compile_get_register(char_u **arg, cctx_T *cctx)
2733{
2734 int ret;
2735
2736 ++*arg;
2737 if (**arg == NUL)
2738 {
2739 semsg(_(e_syntax_at), *arg - 1);
2740 return FAIL;
2741 }
2742 if (!valid_yank_reg(**arg, TRUE))
2743 {
2744 emsg_invreg(**arg);
2745 return FAIL;
2746 }
2747 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2748 ++*arg;
2749 return ret;
2750}
2751
2752/*
2753 * Apply leading '!', '-' and '+' to constant "rettv".
2754 */
2755 static int
2756apply_leader(typval_T *rettv, char_u *start, char_u *end)
2757{
2758 char_u *p = end;
2759
2760 // this works from end to start
2761 while (p > start)
2762 {
2763 --p;
2764 if (*p == '-' || *p == '+')
2765 {
2766 // only '-' has an effect, for '+' we only check the type
2767#ifdef FEAT_FLOAT
2768 if (rettv->v_type == VAR_FLOAT)
2769 {
2770 if (*p == '-')
2771 rettv->vval.v_float = -rettv->vval.v_float;
2772 }
2773 else
2774#endif
2775 {
2776 varnumber_T val;
2777 int error = FALSE;
2778
2779 // tv_get_number_chk() accepts a string, but we don't want that
2780 // here
2781 if (check_not_string(rettv) == FAIL)
2782 return FAIL;
2783 val = tv_get_number_chk(rettv, &error);
2784 clear_tv(rettv);
2785 if (error)
2786 return FAIL;
2787 if (*p == '-')
2788 val = -val;
2789 rettv->v_type = VAR_NUMBER;
2790 rettv->vval.v_number = val;
2791 }
2792 }
2793 else
2794 {
2795 int v = tv2bool(rettv);
2796
2797 // '!' is permissive in the type.
2798 clear_tv(rettv);
2799 rettv->v_type = VAR_BOOL;
2800 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2801 }
2802 }
2803 return OK;
2804}
2805
2806/*
2807 * Recognize v: variables that are constants and set "rettv".
2808 */
2809 static void
2810get_vim_constant(char_u **arg, typval_T *rettv)
2811{
2812 if (STRNCMP(*arg, "v:true", 6) == 0)
2813 {
2814 rettv->v_type = VAR_BOOL;
2815 rettv->vval.v_number = VVAL_TRUE;
2816 *arg += 6;
2817 }
2818 else if (STRNCMP(*arg, "v:false", 7) == 0)
2819 {
2820 rettv->v_type = VAR_BOOL;
2821 rettv->vval.v_number = VVAL_FALSE;
2822 *arg += 7;
2823 }
2824 else if (STRNCMP(*arg, "v:null", 6) == 0)
2825 {
2826 rettv->v_type = VAR_SPECIAL;
2827 rettv->vval.v_number = VVAL_NULL;
2828 *arg += 6;
2829 }
2830 else if (STRNCMP(*arg, "v:none", 6) == 0)
2831 {
2832 rettv->v_type = VAR_SPECIAL;
2833 rettv->vval.v_number = VVAL_NONE;
2834 *arg += 6;
2835 }
2836}
2837
2838/*
2839 * Compile code to apply '-', '+' and '!'.
2840 */
2841 static int
2842compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2843{
2844 char_u *p = end;
2845
2846 // this works from end to start
2847 while (p > start)
2848 {
2849 --p;
2850 if (*p == '-' || *p == '+')
2851 {
2852 int negate = *p == '-';
2853 isn_T *isn;
2854
2855 // TODO: check type
2856 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2857 {
2858 --p;
2859 if (*p == '-')
2860 negate = !negate;
2861 }
2862 // only '-' has an effect, for '+' we only check the type
2863 if (negate)
2864 isn = generate_instr(cctx, ISN_NEGATENR);
2865 else
2866 isn = generate_instr(cctx, ISN_CHECKNR);
2867 if (isn == NULL)
2868 return FAIL;
2869 }
2870 else
2871 {
2872 int invert = TRUE;
2873
2874 while (p > start && p[-1] == '!')
2875 {
2876 --p;
2877 invert = !invert;
2878 }
2879 if (generate_2BOOL(cctx, invert) == FAIL)
2880 return FAIL;
2881 }
2882 }
2883 return OK;
2884}
2885
2886/*
2887 * Compile whatever comes after "name" or "name()".
2888 */
2889 static int
2890compile_subscript(
2891 char_u **arg,
2892 cctx_T *cctx,
2893 char_u **start_leader,
2894 char_u *end_leader)
2895{
2896 for (;;)
2897 {
2898 if (**arg == '(')
2899 {
2900 int argcount = 0;
2901
2902 // funcref(arg)
2903 *arg = skipwhite(*arg + 1);
2904 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2905 return FAIL;
2906 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2907 return FAIL;
2908 }
2909 else if (**arg == '-' && (*arg)[1] == '>')
2910 {
2911 char_u *p;
2912
2913 // something->method()
2914 // Apply the '!', '-' and '+' first:
2915 // -1.0->func() works like (-1.0)->func()
2916 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2917 return FAIL;
2918 *start_leader = end_leader; // don't apply again later
2919
2920 *arg = skipwhite(*arg + 2);
2921 if (**arg == '{')
2922 {
2923 // lambda call: list->{lambda}
2924 if (compile_lambda_call(arg, cctx) == FAIL)
2925 return FAIL;
2926 }
2927 else
2928 {
2929 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002930 p = *arg;
2931 if (ASCII_ISALPHA(*p) && p[1] == ':')
2932 p += 2;
2933 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 ;
2935 if (*p != '(')
2936 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002937 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 return FAIL;
2939 }
2940 // TODO: base value may not be the first argument
2941 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2942 return FAIL;
2943 }
2944 }
2945 else if (**arg == '[')
2946 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002947 garray_T *stack;
2948 type_T **typep;
2949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 // list index: list[123]
2951 // TODO: more arguments
2952 // TODO: dict member dict['name']
2953 *arg = skipwhite(*arg + 1);
2954 if (compile_expr1(arg, cctx) == FAIL)
2955 return FAIL;
2956
2957 if (**arg != ']')
2958 {
2959 emsg(_(e_missbrac));
2960 return FAIL;
2961 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002962 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963
2964 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2965 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01002966 stack = &cctx->ctx_type_stack;
2967 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2968 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
2969 {
2970 emsg(_(e_listreq));
2971 return FAIL;
2972 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002973 if ((*typep)->tt_type == VAR_LIST)
2974 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 }
2976 else if (**arg == '.' && (*arg)[1] != '.')
2977 {
2978 char_u *p;
2979
2980 ++*arg;
2981 p = *arg;
2982 // dictionary member: dict.name
2983 if (eval_isnamec1(*p))
2984 while (eval_isnamec(*p))
2985 MB_PTR_ADV(p);
2986 if (p == *arg)
2987 {
2988 semsg(_(e_syntax_at), *arg);
2989 return FAIL;
2990 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2992 return FAIL;
2993 *arg = p;
2994 }
2995 else
2996 break;
2997 }
2998
2999 // TODO - see handle_subscript():
3000 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3001 // Don't do this when "Func" is already a partial that was bound
3002 // explicitly (pt_auto is FALSE).
3003
3004 return OK;
3005}
3006
3007/*
3008 * Compile an expression at "*p" and add instructions to "instr".
3009 * "p" is advanced until after the expression, skipping white space.
3010 *
3011 * This is the equivalent of eval1(), eval2(), etc.
3012 */
3013
3014/*
3015 * number number constant
3016 * 0zFFFFFFFF Blob constant
3017 * "string" string constant
3018 * 'string' literal string constant
3019 * &option-name option value
3020 * @r register contents
3021 * identifier variable value
3022 * function() function call
3023 * $VAR environment variable
3024 * (expression) nested expression
3025 * [expr, expr] List
3026 * {key: val, key: val} Dictionary
3027 * #{key: val, key: val} Dictionary with literal keys
3028 *
3029 * Also handle:
3030 * ! in front logical NOT
3031 * - in front unary minus
3032 * + in front unary plus (ignored)
3033 * trailing (arg) funcref/partial call
3034 * trailing [] subscript in String or List
3035 * trailing .name entry in Dictionary
3036 * trailing ->name() method call
3037 */
3038 static int
3039compile_expr7(char_u **arg, cctx_T *cctx)
3040{
3041 typval_T rettv;
3042 char_u *start_leader, *end_leader;
3043 int ret = OK;
3044
3045 /*
3046 * Skip '!', '-' and '+' characters. They are handled later.
3047 */
3048 start_leader = *arg;
3049 while (**arg == '!' || **arg == '-' || **arg == '+')
3050 *arg = skipwhite(*arg + 1);
3051 end_leader = *arg;
3052
3053 rettv.v_type = VAR_UNKNOWN;
3054 switch (**arg)
3055 {
3056 /*
3057 * Number constant.
3058 */
3059 case '0': // also for blob starting with 0z
3060 case '1':
3061 case '2':
3062 case '3':
3063 case '4':
3064 case '5':
3065 case '6':
3066 case '7':
3067 case '8':
3068 case '9':
3069 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3070 return FAIL;
3071 break;
3072
3073 /*
3074 * String constant: "string".
3075 */
3076 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3077 return FAIL;
3078 break;
3079
3080 /*
3081 * Literal string constant: 'str''ing'.
3082 */
3083 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3084 return FAIL;
3085 break;
3086
3087 /*
3088 * Constant Vim variable.
3089 */
3090 case 'v': get_vim_constant(arg, &rettv);
3091 ret = NOTDONE;
3092 break;
3093
3094 /*
3095 * List: [expr, expr]
3096 */
3097 case '[': ret = compile_list(arg, cctx);
3098 break;
3099
3100 /*
3101 * Dictionary: #{key: val, key: val}
3102 */
3103 case '#': if ((*arg)[1] == '{')
3104 {
3105 ++*arg;
3106 ret = compile_dict(arg, cctx, TRUE);
3107 }
3108 else
3109 ret = NOTDONE;
3110 break;
3111
3112 /*
3113 * Lambda: {arg, arg -> expr}
3114 * Dictionary: {'key': val, 'key': val}
3115 */
3116 case '{': {
3117 char_u *start = skipwhite(*arg + 1);
3118
3119 // Find out what comes after the arguments.
3120 ret = get_function_args(&start, '-', NULL,
3121 NULL, NULL, NULL, TRUE);
3122 if (ret != FAIL && *start == '>')
3123 ret = compile_lambda(arg, cctx);
3124 else
3125 ret = compile_dict(arg, cctx, FALSE);
3126 }
3127 break;
3128
3129 /*
3130 * Option value: &name
3131 */
3132 case '&': ret = compile_get_option(arg, cctx);
3133 break;
3134
3135 /*
3136 * Environment variable: $VAR.
3137 */
3138 case '$': ret = compile_get_env(arg, cctx);
3139 break;
3140
3141 /*
3142 * Register contents: @r.
3143 */
3144 case '@': ret = compile_get_register(arg, cctx);
3145 break;
3146 /*
3147 * nested expression: (expression).
3148 */
3149 case '(': *arg = skipwhite(*arg + 1);
3150 ret = compile_expr1(arg, cctx); // recursive!
3151 *arg = skipwhite(*arg);
3152 if (**arg == ')')
3153 ++*arg;
3154 else if (ret == OK)
3155 {
3156 emsg(_(e_missing_close));
3157 ret = FAIL;
3158 }
3159 break;
3160
3161 default: ret = NOTDONE;
3162 break;
3163 }
3164 if (ret == FAIL)
3165 return FAIL;
3166
3167 if (rettv.v_type != VAR_UNKNOWN)
3168 {
3169 // apply the '!', '-' and '+' before the constant
3170 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3171 {
3172 clear_tv(&rettv);
3173 return FAIL;
3174 }
3175 start_leader = end_leader; // don't apply again below
3176
3177 // push constant
3178 switch (rettv.v_type)
3179 {
3180 case VAR_BOOL:
3181 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3182 break;
3183 case VAR_SPECIAL:
3184 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3185 break;
3186 case VAR_NUMBER:
3187 generate_PUSHNR(cctx, rettv.vval.v_number);
3188 break;
3189#ifdef FEAT_FLOAT
3190 case VAR_FLOAT:
3191 generate_PUSHF(cctx, rettv.vval.v_float);
3192 break;
3193#endif
3194 case VAR_BLOB:
3195 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3196 rettv.vval.v_blob = NULL;
3197 break;
3198 case VAR_STRING:
3199 generate_PUSHS(cctx, rettv.vval.v_string);
3200 rettv.vval.v_string = NULL;
3201 break;
3202 default:
3203 iemsg("constant type missing");
3204 return FAIL;
3205 }
3206 }
3207 else if (ret == NOTDONE)
3208 {
3209 char_u *p;
3210 int r;
3211
3212 if (!eval_isnamec1(**arg))
3213 {
3214 semsg(_("E1015: Name expected: %s"), *arg);
3215 return FAIL;
3216 }
3217
3218 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003219 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 if (*p == '(')
3221 r = compile_call(arg, p - *arg, cctx, 0);
3222 else
3223 r = compile_load(arg, p, cctx, TRUE);
3224 if (r == FAIL)
3225 return FAIL;
3226 }
3227
3228 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3229 return FAIL;
3230
3231 // Now deal with prefixed '-', '+' and '!', if not done already.
3232 return compile_leader(cctx, start_leader, end_leader);
3233}
3234
3235/*
3236 * * number multiplication
3237 * / number division
3238 * % number modulo
3239 */
3240 static int
3241compile_expr6(char_u **arg, cctx_T *cctx)
3242{
3243 char_u *op;
3244
3245 // get the first variable
3246 if (compile_expr7(arg, cctx) == FAIL)
3247 return FAIL;
3248
3249 /*
3250 * Repeat computing, until no "*", "/" or "%" is following.
3251 */
3252 for (;;)
3253 {
3254 op = skipwhite(*arg);
3255 if (*op != '*' && *op != '/' && *op != '%')
3256 break;
3257 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
3258 {
3259 char_u buf[3];
3260
3261 vim_strncpy(buf, op, 1);
3262 semsg(_(e_white_both), buf);
3263 }
3264 *arg = skipwhite(op + 1);
3265
3266 // get the second variable
3267 if (compile_expr7(arg, cctx) == FAIL)
3268 return FAIL;
3269
3270 generate_two_op(cctx, op);
3271 }
3272
3273 return OK;
3274}
3275
3276/*
3277 * + number addition
3278 * - number subtraction
3279 * .. string concatenation
3280 */
3281 static int
3282compile_expr5(char_u **arg, cctx_T *cctx)
3283{
3284 char_u *op;
3285 int oplen;
3286
3287 // get the first variable
3288 if (compile_expr6(arg, cctx) == FAIL)
3289 return FAIL;
3290
3291 /*
3292 * Repeat computing, until no "+", "-" or ".." is following.
3293 */
3294 for (;;)
3295 {
3296 op = skipwhite(*arg);
3297 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3298 break;
3299 oplen = (*op == '.' ? 2 : 1);
3300
3301 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
3302 {
3303 char_u buf[3];
3304
3305 vim_strncpy(buf, op, oplen);
3306 semsg(_(e_white_both), buf);
3307 }
3308
3309 *arg = skipwhite(op + oplen);
3310
3311 // get the second variable
3312 if (compile_expr6(arg, cctx) == FAIL)
3313 return FAIL;
3314
3315 if (*op == '.')
3316 {
3317 if (may_generate_2STRING(-2, cctx) == FAIL
3318 || may_generate_2STRING(-1, cctx) == FAIL)
3319 return FAIL;
3320 generate_instr_drop(cctx, ISN_CONCAT, 1);
3321 }
3322 else
3323 generate_two_op(cctx, op);
3324 }
3325
3326 return OK;
3327}
3328
Bram Moolenaar080457c2020-03-03 21:53:32 +01003329 static exptype_T
3330get_compare_type(char_u *p, int *len, int *type_is)
3331{
3332 exptype_T type = EXPR_UNKNOWN;
3333 int i;
3334
3335 switch (p[0])
3336 {
3337 case '=': if (p[1] == '=')
3338 type = EXPR_EQUAL;
3339 else if (p[1] == '~')
3340 type = EXPR_MATCH;
3341 break;
3342 case '!': if (p[1] == '=')
3343 type = EXPR_NEQUAL;
3344 else if (p[1] == '~')
3345 type = EXPR_NOMATCH;
3346 break;
3347 case '>': if (p[1] != '=')
3348 {
3349 type = EXPR_GREATER;
3350 *len = 1;
3351 }
3352 else
3353 type = EXPR_GEQUAL;
3354 break;
3355 case '<': if (p[1] != '=')
3356 {
3357 type = EXPR_SMALLER;
3358 *len = 1;
3359 }
3360 else
3361 type = EXPR_SEQUAL;
3362 break;
3363 case 'i': if (p[1] == 's')
3364 {
3365 // "is" and "isnot"; but not a prefix of a name
3366 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3367 *len = 5;
3368 i = p[*len];
3369 if (!isalnum(i) && i != '_')
3370 {
3371 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3372 *type_is = TRUE;
3373 }
3374 }
3375 break;
3376 }
3377 return type;
3378}
3379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380/*
3381 * expr5a == expr5b
3382 * expr5a =~ expr5b
3383 * expr5a != expr5b
3384 * expr5a !~ expr5b
3385 * expr5a > expr5b
3386 * expr5a >= expr5b
3387 * expr5a < expr5b
3388 * expr5a <= expr5b
3389 * expr5a is expr5b
3390 * expr5a isnot expr5b
3391 *
3392 * Produces instructions:
3393 * EVAL expr5a Push result of "expr5a"
3394 * EVAL expr5b Push result of "expr5b"
3395 * COMPARE one of the compare instructions
3396 */
3397 static int
3398compile_expr4(char_u **arg, cctx_T *cctx)
3399{
3400 exptype_T type = EXPR_UNKNOWN;
3401 char_u *p;
3402 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 int type_is = FALSE;
3404
3405 // get the first variable
3406 if (compile_expr5(arg, cctx) == FAIL)
3407 return FAIL;
3408
3409 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003410 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411
3412 /*
3413 * If there is a comparative operator, use it.
3414 */
3415 if (type != EXPR_UNKNOWN)
3416 {
3417 int ic = FALSE; // Default: do not ignore case
3418
3419 if (type_is && (p[len] == '?' || p[len] == '#'))
3420 {
3421 semsg(_(e_invexpr2), *arg);
3422 return FAIL;
3423 }
3424 // extra question mark appended: ignore case
3425 if (p[len] == '?')
3426 {
3427 ic = TRUE;
3428 ++len;
3429 }
3430 // extra '#' appended: match case (ignored)
3431 else if (p[len] == '#')
3432 ++len;
3433 // nothing appended: match case
3434
3435 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
3436 {
3437 char_u buf[7];
3438
3439 vim_strncpy(buf, p, len);
3440 semsg(_(e_white_both), buf);
3441 }
3442
3443 // get the second variable
3444 *arg = skipwhite(p + len);
3445 if (compile_expr5(arg, cctx) == FAIL)
3446 return FAIL;
3447
3448 generate_COMPARE(cctx, type, ic);
3449 }
3450
3451 return OK;
3452}
3453
3454/*
3455 * Compile || or &&.
3456 */
3457 static int
3458compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3459{
3460 char_u *p = skipwhite(*arg);
3461 int opchar = *op;
3462
3463 if (p[0] == opchar && p[1] == opchar)
3464 {
3465 garray_T *instr = &cctx->ctx_instr;
3466 garray_T end_ga;
3467
3468 /*
3469 * Repeat until there is no following "||" or "&&"
3470 */
3471 ga_init2(&end_ga, sizeof(int), 10);
3472 while (p[0] == opchar && p[1] == opchar)
3473 {
3474 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3475 semsg(_(e_white_both), op);
3476
3477 if (ga_grow(&end_ga, 1) == FAIL)
3478 {
3479 ga_clear(&end_ga);
3480 return FAIL;
3481 }
3482 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3483 ++end_ga.ga_len;
3484 generate_JUMP(cctx, opchar == '|'
3485 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3486
3487 // eval the next expression
3488 *arg = skipwhite(p + 2);
3489 if ((opchar == '|' ? compile_expr3(arg, cctx)
3490 : compile_expr4(arg, cctx)) == FAIL)
3491 {
3492 ga_clear(&end_ga);
3493 return FAIL;
3494 }
3495 p = skipwhite(*arg);
3496 }
3497
3498 // Fill in the end label in all jumps.
3499 while (end_ga.ga_len > 0)
3500 {
3501 isn_T *isn;
3502
3503 --end_ga.ga_len;
3504 isn = ((isn_T *)instr->ga_data)
3505 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3506 isn->isn_arg.jump.jump_where = instr->ga_len;
3507 }
3508 ga_clear(&end_ga);
3509 }
3510
3511 return OK;
3512}
3513
3514/*
3515 * expr4a && expr4a && expr4a logical AND
3516 *
3517 * Produces instructions:
3518 * EVAL expr4a Push result of "expr4a"
3519 * JUMP_AND_KEEP_IF_FALSE end
3520 * EVAL expr4b Push result of "expr4b"
3521 * JUMP_AND_KEEP_IF_FALSE end
3522 * EVAL expr4c Push result of "expr4c"
3523 * end:
3524 */
3525 static int
3526compile_expr3(char_u **arg, cctx_T *cctx)
3527{
3528 // get the first variable
3529 if (compile_expr4(arg, cctx) == FAIL)
3530 return FAIL;
3531
3532 // || and && work almost the same
3533 return compile_and_or(arg, cctx, "&&");
3534}
3535
3536/*
3537 * expr3a || expr3b || expr3c logical OR
3538 *
3539 * Produces instructions:
3540 * EVAL expr3a Push result of "expr3a"
3541 * JUMP_AND_KEEP_IF_TRUE end
3542 * EVAL expr3b Push result of "expr3b"
3543 * JUMP_AND_KEEP_IF_TRUE end
3544 * EVAL expr3c Push result of "expr3c"
3545 * end:
3546 */
3547 static int
3548compile_expr2(char_u **arg, cctx_T *cctx)
3549{
3550 // eval the first expression
3551 if (compile_expr3(arg, cctx) == FAIL)
3552 return FAIL;
3553
3554 // || and && work almost the same
3555 return compile_and_or(arg, cctx, "||");
3556}
3557
3558/*
3559 * Toplevel expression: expr2 ? expr1a : expr1b
3560 *
3561 * Produces instructions:
3562 * EVAL expr2 Push result of "expr"
3563 * JUMP_IF_FALSE alt jump if false
3564 * EVAL expr1a
3565 * JUMP_ALWAYS end
3566 * alt: EVAL expr1b
3567 * end:
3568 */
3569 static int
3570compile_expr1(char_u **arg, cctx_T *cctx)
3571{
3572 char_u *p;
3573
3574 // evaluate the first expression
3575 if (compile_expr2(arg, cctx) == FAIL)
3576 return FAIL;
3577
3578 p = skipwhite(*arg);
3579 if (*p == '?')
3580 {
3581 garray_T *instr = &cctx->ctx_instr;
3582 garray_T *stack = &cctx->ctx_type_stack;
3583 int alt_idx = instr->ga_len;
3584 int end_idx;
3585 isn_T *isn;
3586 type_T *type1;
3587 type_T *type2;
3588
3589 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3590 semsg(_(e_white_both), "?");
3591
3592 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3593
3594 // evaluate the second expression; any type is accepted
3595 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003596 if (compile_expr1(arg, cctx) == FAIL)
3597 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598
3599 // remember the type and drop it
3600 --stack->ga_len;
3601 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3602
3603 end_idx = instr->ga_len;
3604 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3605
3606 // jump here from JUMP_IF_FALSE
3607 isn = ((isn_T *)instr->ga_data) + alt_idx;
3608 isn->isn_arg.jump.jump_where = instr->ga_len;
3609
3610 // Check for the ":".
3611 p = skipwhite(*arg);
3612 if (*p != ':')
3613 {
3614 emsg(_(e_missing_colon));
3615 return FAIL;
3616 }
3617 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3618 semsg(_(e_white_both), ":");
3619
3620 // evaluate the third expression
3621 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003622 if (compile_expr1(arg, cctx) == FAIL)
3623 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624
3625 // If the types differ, the result has a more generic type.
3626 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003627 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628
3629 // jump here from JUMP_ALWAYS
3630 isn = ((isn_T *)instr->ga_data) + end_idx;
3631 isn->isn_arg.jump.jump_where = instr->ga_len;
3632 }
3633 return OK;
3634}
3635
3636/*
3637 * compile "return [expr]"
3638 */
3639 static char_u *
3640compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3641{
3642 char_u *p = arg;
3643 garray_T *stack = &cctx->ctx_type_stack;
3644 type_T *stack_type;
3645
3646 if (*p != NUL && *p != '|' && *p != '\n')
3647 {
3648 // compile return argument into instructions
3649 if (compile_expr1(&p, cctx) == FAIL)
3650 return NULL;
3651
3652 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3653 if (set_return_type)
3654 cctx->ctx_ufunc->uf_ret_type = stack_type;
3655 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3656 == FAIL)
3657 return NULL;
3658 }
3659 else
3660 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003661 // "set_return_type" cannot be TRUE, only used for a lambda which
3662 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02003663 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
3664 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 {
3666 emsg(_("E1003: Missing return value"));
3667 return NULL;
3668 }
3669
3670 // No argument, return zero.
3671 generate_PUSHNR(cctx, 0);
3672 }
3673
3674 if (generate_instr(cctx, ISN_RETURN) == NULL)
3675 return NULL;
3676
3677 // "return val | endif" is possible
3678 return skipwhite(p);
3679}
3680
3681/*
3682 * Return the length of an assignment operator, or zero if there isn't one.
3683 */
3684 int
3685assignment_len(char_u *p, int *heredoc)
3686{
3687 if (*p == '=')
3688 {
3689 if (p[1] == '<' && p[2] == '<')
3690 {
3691 *heredoc = TRUE;
3692 return 3;
3693 }
3694 return 1;
3695 }
3696 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3697 return 2;
3698 if (STRNCMP(p, "..=", 3) == 0)
3699 return 3;
3700 return 0;
3701}
3702
3703// words that cannot be used as a variable
3704static char *reserved[] = {
3705 "true",
3706 "false",
3707 NULL
3708};
3709
3710/*
3711 * Get a line for "=<<".
3712 * Return a pointer to the line in allocated memory.
3713 * Return NULL for end-of-file or some error.
3714 */
3715 static char_u *
3716heredoc_getline(
3717 int c UNUSED,
3718 void *cookie,
3719 int indent UNUSED,
3720 int do_concat UNUSED)
3721{
3722 cctx_T *cctx = (cctx_T *)cookie;
3723
3724 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003725 {
3726 iemsg("Heredoc got to end");
3727 return NULL;
3728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 ++cctx->ctx_lnum;
3730 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3731 [cctx->ctx_lnum]);
3732}
3733
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003734typedef enum {
3735 dest_local,
3736 dest_option,
3737 dest_env,
3738 dest_global,
3739 dest_vimvar,
3740 dest_script,
3741 dest_reg,
3742} assign_dest_T;
3743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744/*
3745 * compile "let var [= expr]", "const var = expr" and "var = expr"
3746 * "arg" points to "var".
3747 */
3748 static char_u *
3749compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3750{
3751 char_u *p;
3752 char_u *ret = NULL;
3753 int var_count = 0;
3754 int semicolon = 0;
3755 size_t varlen;
3756 garray_T *instr = &cctx->ctx_instr;
3757 int idx = -1;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003758 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003761 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003763 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 int oplen = 0;
3765 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003766 type_T *type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 lvar_T *lvar;
3768 char_u *name;
3769 char_u *sp;
3770 int has_type = FALSE;
3771 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3772 int instr_count = -1;
3773
3774 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3775 if (p == NULL)
3776 return NULL;
3777 if (var_count > 0)
3778 {
3779 // TODO: let [var, var] = list
3780 emsg("Cannot handle a list yet");
3781 return NULL;
3782 }
3783
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003784 // "a: type" is declaring variable "a" with a type, not "a:".
3785 if (is_decl && p == arg + 2 && p[-1] == ':')
3786 --p;
3787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 varlen = p - arg;
3789 name = vim_strnsave(arg, (int)varlen);
3790 if (name == NULL)
3791 return NULL;
3792
Bram Moolenaar080457c2020-03-03 21:53:32 +01003793 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003795 if (*arg == '&')
3796 {
3797 int cc;
3798 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799
Bram Moolenaar080457c2020-03-03 21:53:32 +01003800 dest = dest_option;
3801 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003803 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003804 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 if (is_decl)
3807 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003808 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 goto theend;
3810 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003811 p = arg;
3812 p = find_option_end(&p, &opt_flags);
3813 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003815 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01003816 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003817 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003818 }
3819 cc = *p;
3820 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003821 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003822 *p = cc;
3823 if (opt_type == -3)
3824 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003825 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003826 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003827 }
3828 if (opt_type == -2 || opt_type == 0)
3829 type = &t_string;
3830 else
3831 type = &t_number; // both number and boolean option
3832 }
3833 else if (*arg == '$')
3834 {
3835 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003836 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003837 if (is_decl)
3838 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003839 semsg(_("E1065: Cannot declare an environment variable: %s"),
3840 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003841 goto theend;
3842 }
3843 }
3844 else if (*arg == '@')
3845 {
3846 if (!valid_yank_reg(arg[1], TRUE))
3847 {
3848 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003849 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003850 }
3851 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003852 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003853 if (is_decl)
3854 {
3855 semsg(_("E1066: Cannot declare a register: %s"), name);
3856 goto theend;
3857 }
3858 }
3859 else if (STRNCMP(arg, "g:", 2) == 0)
3860 {
3861 dest = dest_global;
3862 if (is_decl)
3863 {
3864 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3865 goto theend;
3866 }
3867 }
3868 else if (STRNCMP(arg, "v:", 2) == 0)
3869 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02003870 typval_T *vtv;
3871
Bram Moolenaar080457c2020-03-03 21:53:32 +01003872 vimvaridx = find_vim_var(name + 2);
3873 if (vimvaridx < 0)
3874 {
3875 semsg(_(e_var_notfound), arg);
3876 goto theend;
3877 }
3878 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003879 vtv = get_vim_var_tv(vimvaridx);
3880 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003881 if (is_decl)
3882 {
3883 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3884 goto theend;
3885 }
3886 }
3887 else
3888 {
3889 for (idx = 0; reserved[idx] != NULL; ++idx)
3890 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003892 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 goto theend;
3894 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003895
3896 idx = lookup_local(arg, varlen, cctx);
3897 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003899 if (is_decl)
3900 {
3901 semsg(_("E1017: Variable already declared: %s"), name);
3902 goto theend;
3903 }
3904 else
3905 {
3906 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3907 if (lvar->lv_const)
3908 {
3909 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3910 goto theend;
3911 }
3912 }
3913 }
3914 else if (STRNCMP(arg, "s:", 2) == 0
3915 || lookup_script(arg, varlen) == OK
3916 || find_imported(arg, varlen, cctx) != NULL)
3917 {
3918 dest = dest_script;
3919 if (is_decl)
3920 {
3921 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003922 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003923 goto theend;
3924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 }
3926 }
3927 }
3928
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003929 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 {
3931 if (is_decl && *p == ':')
3932 {
3933 // parse optional type: "let var: type = expr"
3934 p = skipwhite(p + 1);
3935 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 has_type = TRUE;
3937 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02003938 else if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 {
3940 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3941 type = lvar->lv_type;
3942 }
3943 }
3944
3945 sp = p;
3946 p = skipwhite(p);
3947 op = p;
3948 oplen = assignment_len(p, &heredoc);
3949 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3950 {
3951 char_u buf[4];
3952
3953 vim_strncpy(buf, op, oplen);
3954 semsg(_(e_white_both), buf);
3955 }
3956
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003957 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02003958 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01003960 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 goto theend;
3962 }
3963
Bram Moolenaar080457c2020-03-03 21:53:32 +01003964 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 {
3966 if (oplen > 1 && !heredoc)
3967 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003968 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3970 name);
3971 goto theend;
3972 }
3973
3974 // new local variable
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003975 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
3976 && var_check_func_name(name, TRUE))
3977 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3979 if (idx < 0)
3980 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003981 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 }
3983
3984 if (heredoc)
3985 {
3986 list_T *l;
3987 listitem_T *li;
3988
3989 // [let] varname =<< [trim] {end}
3990 eap->getline = heredoc_getline;
3991 eap->cookie = cctx;
3992 l = heredoc_get(eap, op + 3);
3993
3994 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003995 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 {
3997 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3998 li->li_tv.vval.v_string = NULL;
3999 }
4000 generate_NEWLIST(cctx, l->lv_len);
4001 type = &t_list_string;
4002 list_free(l);
4003 p += STRLEN(p);
4004 }
4005 else if (oplen > 0)
4006 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004007 int r;
4008 type_T *stacktype;
4009 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 // for "+=", "*=", "..=" etc. first load the current value
4012 if (*op != '=')
4013 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004014 switch (dest)
4015 {
4016 case dest_option:
4017 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004018 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004019 break;
4020 case dest_global:
4021 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4022 break;
4023 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004024 compile_load_scriptvar(cctx,
4025 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004026 break;
4027 case dest_env:
4028 // Include $ in the name here
4029 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4030 break;
4031 case dest_reg:
4032 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4033 break;
4034 case dest_vimvar:
4035 generate_LOADV(cctx, name + 2, TRUE);
4036 break;
4037 case dest_local:
4038 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
4039 break;
4040 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 }
4042
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004043 // Compile the expression. Temporarily hide the new local variable
4044 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004045 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004046 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 instr_count = instr->ga_len;
4048 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004049 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004050 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004051 ++cctx->ctx_locals.ga_len;
4052 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 goto theend;
4054
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004055 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004057 stack = &cctx->ctx_type_stack;
4058 stacktype = stack->ga_len == 0 ? &t_void
4059 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
4060 if (idx >= 0 && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004062 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
4063 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004065 if (stacktype->tt_type == VAR_VOID)
4066 {
4067 emsg(_("E1031: Cannot use void value"));
4068 goto theend;
4069 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004070 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004071 {
4072 // An empty list or dict has a &t_void member, for a
4073 // variable that implies &t_any.
4074 if (stacktype == &t_list_empty)
4075 lvar->lv_type = &t_list_any;
4076 else if (stacktype == &t_dict_empty)
4077 lvar->lv_type = &t_dict_any;
4078 else
4079 lvar->lv_type = stacktype;
4080 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004081 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004082 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4083 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004085 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004086 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 }
4088 }
4089 else if (cmdidx == CMD_const)
4090 {
4091 emsg(_("E1021: const requires a value"));
4092 goto theend;
4093 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004094 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 {
4096 emsg(_("E1022: type or initialization required"));
4097 goto theend;
4098 }
4099 else
4100 {
4101 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 if (ga_grow(instr, 1) == FAIL)
4103 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004104 switch (type->tt_type)
4105 {
4106 case VAR_BOOL:
4107 generate_PUSHBOOL(cctx, VVAL_FALSE);
4108 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004109 case VAR_FLOAT:
4110#ifdef FEAT_FLOAT
4111 generate_PUSHF(cctx, 0.0);
4112#endif
4113 break;
4114 case VAR_STRING:
4115 generate_PUSHS(cctx, NULL);
4116 break;
4117 case VAR_BLOB:
4118 generate_PUSHBLOB(cctx, NULL);
4119 break;
4120 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004121 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004122 break;
4123 case VAR_PARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004124 generate_PUSHPARTIAL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004125 break;
4126 case VAR_LIST:
4127 generate_NEWLIST(cctx, 0);
4128 break;
4129 case VAR_DICT:
4130 generate_NEWDICT(cctx, 0);
4131 break;
4132 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004133 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004134 break;
4135 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004136 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004137 break;
4138 case VAR_NUMBER:
4139 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004140 case VAR_ANY:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004141 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004142 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004143 generate_PUSHNR(cctx, 0);
4144 break;
4145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 }
4147
4148 if (oplen > 0 && *op != '=')
4149 {
4150 type_T *expected = &t_number;
4151 garray_T *stack = &cctx->ctx_type_stack;
4152 type_T *stacktype;
4153
4154 // TODO: if type is known use float or any operation
4155
4156 if (*op == '.')
4157 expected = &t_string;
4158 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4159 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4160 goto theend;
4161
4162 if (*op == '.')
4163 generate_instr_drop(cctx, ISN_CONCAT, 1);
4164 else
4165 {
4166 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4167
4168 if (isn == NULL)
4169 goto theend;
4170 switch (*op)
4171 {
4172 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4173 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4174 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4175 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4176 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4177 }
4178 }
4179 }
4180
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004181 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004183 case dest_option:
4184 generate_STOREOPT(cctx, name + 1, opt_flags);
4185 break;
4186 case dest_global:
4187 // include g: with the name, easier to execute that way
4188 generate_STORE(cctx, ISN_STOREG, 0, name);
4189 break;
4190 case dest_env:
4191 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4192 break;
4193 case dest_reg:
4194 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4195 break;
4196 case dest_vimvar:
4197 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4198 break;
4199 case dest_script:
4200 {
4201 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4202 imported_T *import = NULL;
4203 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004204
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004205 if (name[1] != ':')
4206 {
4207 import = find_imported(name, 0, cctx);
4208 if (import != NULL)
4209 sid = import->imp_sid;
4210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004212 idx = get_script_item_idx(sid, rawname, TRUE);
4213 // TODO: specific type
4214 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004215 {
4216 char_u *name_s = name;
4217
4218 // Include s: in the name for store_var()
4219 if (name[1] != ':')
4220 {
4221 int len = (int)STRLEN(name) + 3;
4222
4223 name_s = alloc(len);
4224 if (name_s == NULL)
4225 name_s = name;
4226 else
4227 vim_snprintf((char *)name_s, len, "s:%s", name);
4228 }
4229 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4230 if (name_s != name)
4231 vim_free(name_s);
4232 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004233 else
4234 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4235 sid, idx, &t_any);
4236 }
4237 break;
4238 case dest_local:
4239 {
4240 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004242 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4243 // into ISN_STORENR
4244 if (instr->ga_len == instr_count + 1
4245 && isn->isn_type == ISN_PUSHNR)
4246 {
4247 varnumber_T val = isn->isn_arg.number;
4248 garray_T *stack = &cctx->ctx_type_stack;
4249
4250 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004251 isn->isn_arg.storenr.stnr_idx = idx;
4252 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004253 if (stack->ga_len > 0)
4254 --stack->ga_len;
4255 }
4256 else
4257 generate_STORE(cctx, ISN_STORE, idx, NULL);
4258 }
4259 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 }
4261 ret = p;
4262
4263theend:
4264 vim_free(name);
4265 return ret;
4266}
4267
4268/*
4269 * Compile an :import command.
4270 */
4271 static char_u *
4272compile_import(char_u *arg, cctx_T *cctx)
4273{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004274 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275}
4276
4277/*
4278 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4279 */
4280 static int
4281compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4282{
4283 garray_T *instr = &cctx->ctx_instr;
4284 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4285
4286 if (endlabel == NULL)
4287 return FAIL;
4288 endlabel->el_next = *el;
4289 *el = endlabel;
4290 endlabel->el_end_label = instr->ga_len;
4291
4292 generate_JUMP(cctx, when, 0);
4293 return OK;
4294}
4295
4296 static void
4297compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4298{
4299 garray_T *instr = &cctx->ctx_instr;
4300
4301 while (*el != NULL)
4302 {
4303 endlabel_T *cur = (*el);
4304 isn_T *isn;
4305
4306 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4307 isn->isn_arg.jump.jump_where = instr->ga_len;
4308 *el = cur->el_next;
4309 vim_free(cur);
4310 }
4311}
4312
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004313 static void
4314compile_free_jump_to_end(endlabel_T **el)
4315{
4316 while (*el != NULL)
4317 {
4318 endlabel_T *cur = (*el);
4319
4320 *el = cur->el_next;
4321 vim_free(cur);
4322 }
4323}
4324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325/*
4326 * Create a new scope and set up the generic items.
4327 */
4328 static scope_T *
4329new_scope(cctx_T *cctx, scopetype_T type)
4330{
4331 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4332
4333 if (scope == NULL)
4334 return NULL;
4335 scope->se_outer = cctx->ctx_scope;
4336 cctx->ctx_scope = scope;
4337 scope->se_type = type;
4338 scope->se_local_count = cctx->ctx_locals.ga_len;
4339 return scope;
4340}
4341
4342/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004343 * Free the current scope and go back to the outer scope.
4344 */
4345 static void
4346drop_scope(cctx_T *cctx)
4347{
4348 scope_T *scope = cctx->ctx_scope;
4349
4350 if (scope == NULL)
4351 {
4352 iemsg("calling drop_scope() without a scope");
4353 return;
4354 }
4355 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004356 switch (scope->se_type)
4357 {
4358 case IF_SCOPE:
4359 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4360 case FOR_SCOPE:
4361 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4362 case WHILE_SCOPE:
4363 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4364 case TRY_SCOPE:
4365 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4366 case NO_SCOPE:
4367 case BLOCK_SCOPE:
4368 break;
4369 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004370 vim_free(scope);
4371}
4372
4373/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004374 * Evaluate an expression that is a constant:
4375 * has(arg)
4376 *
4377 * Also handle:
4378 * ! in front logical NOT
4379 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004380 * Return FAIL if the expression is not a constant.
4381 */
4382 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004383evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004384{
4385 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004386 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004387 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004388
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004389 /*
4390 * Skip '!' characters. They are handled later.
4391 */
4392 start_leader = *arg;
4393 while (**arg == '!')
4394 *arg = skipwhite(*arg + 1);
4395 end_leader = *arg;
4396
4397 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004398 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004399 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004400 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4401 {
4402 tv->v_type = VAR_SPECIAL;
4403 tv->vval.v_number = VVAL_TRUE;
4404 *arg += 4;
4405 return OK;
4406 }
4407 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4408 {
4409 tv->v_type = VAR_SPECIAL;
4410 tv->vval.v_number = VVAL_FALSE;
4411 *arg += 5;
4412 return OK;
4413 }
4414
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004415 if (STRNCMP("has(", *arg, 4) == 0)
4416 {
4417 has_call = TRUE;
4418 *arg = skipwhite(*arg + 4);
4419 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004420
4421 if (**arg == '"')
4422 {
4423 if (get_string_tv(arg, tv, TRUE) == FAIL)
4424 return FAIL;
4425 }
4426 else if (**arg == '\'')
4427 {
4428 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4429 return FAIL;
4430 }
4431 else
4432 return FAIL;
4433
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004434 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004435 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004436 *arg = skipwhite(*arg);
4437 if (**arg != ')')
4438 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004439 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004440
4441 argvars[0] = *tv;
4442 argvars[1].v_type = VAR_UNKNOWN;
4443 tv->v_type = VAR_NUMBER;
4444 tv->vval.v_number = 0;
4445 f_has(argvars, tv);
4446 clear_tv(&argvars[0]);
4447
4448 while (start_leader < end_leader)
4449 {
4450 if (*start_leader == '!')
4451 tv->vval.v_number = !tv->vval.v_number;
4452 ++start_leader;
4453 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004454 }
4455
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004456 return OK;
4457}
4458
Bram Moolenaar080457c2020-03-03 21:53:32 +01004459 static int
4460evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4461{
4462 exptype_T type = EXPR_UNKNOWN;
4463 char_u *p;
4464 int len = 2;
4465 int type_is = FALSE;
4466
4467 // get the first variable
4468 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4469 return FAIL;
4470
4471 p = skipwhite(*arg);
4472 type = get_compare_type(p, &len, &type_is);
4473
4474 /*
4475 * If there is a comparative operator, use it.
4476 */
4477 if (type != EXPR_UNKNOWN)
4478 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004479 typval_T tv2;
4480 char_u *s1, *s2;
4481 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4482 int n;
4483
4484 // TODO: Only string == string is supported now
4485 if (tv->v_type != VAR_STRING)
4486 return FAIL;
4487 if (type != EXPR_EQUAL)
4488 return FAIL;
4489
4490 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004491 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004492 *arg = skipwhite(p + len);
4493 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4494 || tv2.v_type != VAR_STRING)
4495 {
4496 clear_tv(&tv2);
4497 return FAIL;
4498 }
4499 s1 = tv_get_string_buf(tv, buf1);
4500 s2 = tv_get_string_buf(&tv2, buf2);
4501 n = STRCMP(s1, s2);
4502 clear_tv(tv);
4503 clear_tv(&tv2);
4504 tv->v_type = VAR_BOOL;
4505 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004506 }
4507
4508 return OK;
4509}
4510
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004511static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4512
4513/*
4514 * Compile constant || or &&.
4515 */
4516 static int
4517evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4518{
4519 char_u *p = skipwhite(*arg);
4520 int opchar = *op;
4521
4522 if (p[0] == opchar && p[1] == opchar)
4523 {
4524 int val = tv2bool(tv);
4525
4526 /*
4527 * Repeat until there is no following "||" or "&&"
4528 */
4529 while (p[0] == opchar && p[1] == opchar)
4530 {
4531 typval_T tv2;
4532
4533 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4534 return FAIL;
4535
4536 // eval the next expression
4537 *arg = skipwhite(p + 2);
4538 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004539 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004540 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004541 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004542 {
4543 clear_tv(&tv2);
4544 return FAIL;
4545 }
4546 if ((opchar == '&') == val)
4547 {
4548 // false || tv2 or true && tv2: use tv2
4549 clear_tv(tv);
4550 *tv = tv2;
4551 val = tv2bool(tv);
4552 }
4553 else
4554 clear_tv(&tv2);
4555 p = skipwhite(*arg);
4556 }
4557 }
4558
4559 return OK;
4560}
4561
4562/*
4563 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4564 * Return FAIL if the expression is not a constant.
4565 */
4566 static int
4567evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4568{
4569 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004570 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004571 return FAIL;
4572
4573 // || and && work almost the same
4574 return evaluate_const_and_or(arg, cctx, "&&", tv);
4575}
4576
4577/*
4578 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4579 * Return FAIL if the expression is not a constant.
4580 */
4581 static int
4582evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4583{
4584 // evaluate the first expression
4585 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4586 return FAIL;
4587
4588 // || and && work almost the same
4589 return evaluate_const_and_or(arg, cctx, "||", tv);
4590}
4591
4592/*
4593 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4594 * E.g. for "has('feature')".
4595 * This does not produce error messages. "tv" should be cleared afterwards.
4596 * Return FAIL if the expression is not a constant.
4597 */
4598 static int
4599evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4600{
4601 char_u *p;
4602
4603 // evaluate the first expression
4604 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4605 return FAIL;
4606
4607 p = skipwhite(*arg);
4608 if (*p == '?')
4609 {
4610 int val = tv2bool(tv);
4611 typval_T tv2;
4612
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004613 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004614 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4615 return FAIL;
4616
4617 // evaluate the second expression; any type is accepted
4618 clear_tv(tv);
4619 *arg = skipwhite(p + 1);
4620 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4621 return FAIL;
4622
4623 // Check for the ":".
4624 p = skipwhite(*arg);
4625 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4626 return FAIL;
4627
4628 // evaluate the third expression
4629 *arg = skipwhite(p + 1);
4630 tv2.v_type = VAR_UNKNOWN;
4631 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4632 {
4633 clear_tv(&tv2);
4634 return FAIL;
4635 }
4636 if (val)
4637 {
4638 // use the expr after "?"
4639 clear_tv(&tv2);
4640 }
4641 else
4642 {
4643 // use the expr after ":"
4644 clear_tv(tv);
4645 *tv = tv2;
4646 }
4647 }
4648 return OK;
4649}
4650
4651/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 * compile "if expr"
4653 *
4654 * "if expr" Produces instructions:
4655 * EVAL expr Push result of "expr"
4656 * JUMP_IF_FALSE end
4657 * ... body ...
4658 * end:
4659 *
4660 * "if expr | else" Produces instructions:
4661 * EVAL expr Push result of "expr"
4662 * JUMP_IF_FALSE else
4663 * ... body ...
4664 * JUMP_ALWAYS end
4665 * else:
4666 * ... body ...
4667 * end:
4668 *
4669 * "if expr1 | elseif expr2 | else" Produces instructions:
4670 * EVAL expr Push result of "expr"
4671 * JUMP_IF_FALSE elseif
4672 * ... body ...
4673 * JUMP_ALWAYS end
4674 * elseif:
4675 * EVAL expr Push result of "expr"
4676 * JUMP_IF_FALSE else
4677 * ... body ...
4678 * JUMP_ALWAYS end
4679 * else:
4680 * ... body ...
4681 * end:
4682 */
4683 static char_u *
4684compile_if(char_u *arg, cctx_T *cctx)
4685{
4686 char_u *p = arg;
4687 garray_T *instr = &cctx->ctx_instr;
4688 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004689 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004691 // compile "expr"; if we know it evaluates to FALSE skip the block
4692 tv.v_type = VAR_UNKNOWN;
4693 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4694 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4695 else
4696 cctx->ctx_skip = MAYBE;
4697 clear_tv(&tv);
4698 if (cctx->ctx_skip == MAYBE)
4699 {
4700 p = arg;
4701 if (compile_expr1(&p, cctx) == FAIL)
4702 return NULL;
4703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704
4705 scope = new_scope(cctx, IF_SCOPE);
4706 if (scope == NULL)
4707 return NULL;
4708
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004709 if (cctx->ctx_skip == MAYBE)
4710 {
4711 // "where" is set when ":elseif", "else" or ":endif" is found
4712 scope->se_u.se_if.is_if_label = instr->ga_len;
4713 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4714 }
4715 else
4716 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717
4718 return p;
4719}
4720
4721 static char_u *
4722compile_elseif(char_u *arg, cctx_T *cctx)
4723{
4724 char_u *p = arg;
4725 garray_T *instr = &cctx->ctx_instr;
4726 isn_T *isn;
4727 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004728 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729
4730 if (scope == NULL || scope->se_type != IF_SCOPE)
4731 {
4732 emsg(_(e_elseif_without_if));
4733 return NULL;
4734 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004735 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736
Bram Moolenaar158906c2020-02-06 20:39:45 +01004737 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004738 {
4739 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004741 return NULL;
4742 // previous "if" or "elseif" jumps here
4743 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4744 isn->isn_arg.jump.jump_where = instr->ga_len;
4745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004747 // compile "expr"; if we know it evaluates to FALSE skip the block
4748 tv.v_type = VAR_UNKNOWN;
4749 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4750 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4751 else
4752 cctx->ctx_skip = MAYBE;
4753 clear_tv(&tv);
4754 if (cctx->ctx_skip == MAYBE)
4755 {
4756 p = arg;
4757 if (compile_expr1(&p, cctx) == FAIL)
4758 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004760 // "where" is set when ":elseif", "else" or ":endif" is found
4761 scope->se_u.se_if.is_if_label = instr->ga_len;
4762 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4763 }
4764 else
4765 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766
4767 return p;
4768}
4769
4770 static char_u *
4771compile_else(char_u *arg, cctx_T *cctx)
4772{
4773 char_u *p = arg;
4774 garray_T *instr = &cctx->ctx_instr;
4775 isn_T *isn;
4776 scope_T *scope = cctx->ctx_scope;
4777
4778 if (scope == NULL || scope->se_type != IF_SCOPE)
4779 {
4780 emsg(_(e_else_without_if));
4781 return NULL;
4782 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004783 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004785 // jump from previous block to the end, unless the else block is empty
4786 if (cctx->ctx_skip == MAYBE)
4787 {
4788 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004790 return NULL;
4791 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792
Bram Moolenaar158906c2020-02-06 20:39:45 +01004793 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004794 {
4795 if (scope->se_u.se_if.is_if_label >= 0)
4796 {
4797 // previous "if" or "elseif" jumps here
4798 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4799 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004800 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004801 }
4802 }
4803
4804 if (cctx->ctx_skip != MAYBE)
4805 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806
4807 return p;
4808}
4809
4810 static char_u *
4811compile_endif(char_u *arg, cctx_T *cctx)
4812{
4813 scope_T *scope = cctx->ctx_scope;
4814 ifscope_T *ifscope;
4815 garray_T *instr = &cctx->ctx_instr;
4816 isn_T *isn;
4817
4818 if (scope == NULL || scope->se_type != IF_SCOPE)
4819 {
4820 emsg(_(e_endif_without_if));
4821 return NULL;
4822 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004823 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004824 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004826 if (scope->se_u.se_if.is_if_label >= 0)
4827 {
4828 // previous "if" or "elseif" jumps here
4829 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4830 isn->isn_arg.jump.jump_where = instr->ga_len;
4831 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 // Fill in the "end" label in jumps at the end of the blocks.
4833 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004834 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004836 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 return arg;
4838}
4839
4840/*
4841 * compile "for var in expr"
4842 *
4843 * Produces instructions:
4844 * PUSHNR -1
4845 * STORE loop-idx Set index to -1
4846 * EVAL expr Push result of "expr"
4847 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4848 * - if beyond end, jump to "end"
4849 * - otherwise get item from list and push it
4850 * STORE var Store item in "var"
4851 * ... body ...
4852 * JUMP top Jump back to repeat
4853 * end: DROP Drop the result of "expr"
4854 *
4855 */
4856 static char_u *
4857compile_for(char_u *arg, cctx_T *cctx)
4858{
4859 char_u *p;
4860 size_t varlen;
4861 garray_T *instr = &cctx->ctx_instr;
4862 garray_T *stack = &cctx->ctx_type_stack;
4863 scope_T *scope;
4864 int loop_idx; // index of loop iteration variable
4865 int var_idx; // index of "var"
4866 type_T *vartype;
4867
4868 // TODO: list of variables: "for [key, value] in dict"
4869 // parse "var"
4870 for (p = arg; eval_isnamec1(*p); ++p)
4871 ;
4872 varlen = p - arg;
4873 var_idx = lookup_local(arg, varlen, cctx);
4874 if (var_idx >= 0)
4875 {
4876 semsg(_("E1023: variable already defined: %s"), arg);
4877 return NULL;
4878 }
4879
4880 // consume "in"
4881 p = skipwhite(p);
4882 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4883 {
4884 emsg(_(e_missing_in));
4885 return NULL;
4886 }
4887 p = skipwhite(p + 2);
4888
4889
4890 scope = new_scope(cctx, FOR_SCOPE);
4891 if (scope == NULL)
4892 return NULL;
4893
4894 // Reserve a variable to store the loop iteration counter.
4895 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4896 if (loop_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004897 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004898 // only happens when out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004899 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902
4903 // Reserve a variable to store "var"
4904 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4905 if (var_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004906 {
4907 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004909 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910
4911 generate_STORENR(cctx, loop_idx, -1);
4912
4913 // compile "expr", it remains on the stack until "endfor"
4914 arg = p;
4915 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004916 {
4917 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920
4921 // now we know the type of "var"
4922 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4923 if (vartype->tt_type != VAR_LIST)
4924 {
4925 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004926 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004927 return NULL;
4928 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02004929 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 {
4931 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4932
4933 lvar->lv_type = vartype->tt_member;
4934 }
4935
4936 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004937 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938
4939 generate_FOR(cctx, loop_idx);
4940 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4941
4942 return arg;
4943}
4944
4945/*
4946 * compile "endfor"
4947 */
4948 static char_u *
4949compile_endfor(char_u *arg, cctx_T *cctx)
4950{
4951 garray_T *instr = &cctx->ctx_instr;
4952 scope_T *scope = cctx->ctx_scope;
4953 forscope_T *forscope;
4954 isn_T *isn;
4955
4956 if (scope == NULL || scope->se_type != FOR_SCOPE)
4957 {
4958 emsg(_(e_for));
4959 return NULL;
4960 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004961 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004963 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964
4965 // At end of ":for" scope jump back to the FOR instruction.
4966 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4967
4968 // Fill in the "end" label in the FOR statement so it can jump here
4969 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4970 isn->isn_arg.forloop.for_end = instr->ga_len;
4971
4972 // Fill in the "end" label any BREAK statements
4973 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4974
4975 // Below the ":for" scope drop the "expr" list from the stack.
4976 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4977 return NULL;
4978
4979 vim_free(scope);
4980
4981 return arg;
4982}
4983
4984/*
4985 * compile "while expr"
4986 *
4987 * Produces instructions:
4988 * top: EVAL expr Push result of "expr"
4989 * JUMP_IF_FALSE end jump if false
4990 * ... body ...
4991 * JUMP top Jump back to repeat
4992 * end:
4993 *
4994 */
4995 static char_u *
4996compile_while(char_u *arg, cctx_T *cctx)
4997{
4998 char_u *p = arg;
4999 garray_T *instr = &cctx->ctx_instr;
5000 scope_T *scope;
5001
5002 scope = new_scope(cctx, WHILE_SCOPE);
5003 if (scope == NULL)
5004 return NULL;
5005
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005006 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007
5008 // compile "expr"
5009 if (compile_expr1(&p, cctx) == FAIL)
5010 return NULL;
5011
5012 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005013 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 JUMP_IF_FALSE, cctx) == FAIL)
5015 return FAIL;
5016
5017 return p;
5018}
5019
5020/*
5021 * compile "endwhile"
5022 */
5023 static char_u *
5024compile_endwhile(char_u *arg, cctx_T *cctx)
5025{
5026 scope_T *scope = cctx->ctx_scope;
5027
5028 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5029 {
5030 emsg(_(e_while));
5031 return NULL;
5032 }
5033 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005034 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035
5036 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005037 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038
5039 // Fill in the "end" label in the WHILE statement so it can jump here.
5040 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005041 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042
5043 vim_free(scope);
5044
5045 return arg;
5046}
5047
5048/*
5049 * compile "continue"
5050 */
5051 static char_u *
5052compile_continue(char_u *arg, cctx_T *cctx)
5053{
5054 scope_T *scope = cctx->ctx_scope;
5055
5056 for (;;)
5057 {
5058 if (scope == NULL)
5059 {
5060 emsg(_(e_continue));
5061 return NULL;
5062 }
5063 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5064 break;
5065 scope = scope->se_outer;
5066 }
5067
5068 // Jump back to the FOR or WHILE instruction.
5069 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005070 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5071 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 return arg;
5073}
5074
5075/*
5076 * compile "break"
5077 */
5078 static char_u *
5079compile_break(char_u *arg, cctx_T *cctx)
5080{
5081 scope_T *scope = cctx->ctx_scope;
5082 endlabel_T **el;
5083
5084 for (;;)
5085 {
5086 if (scope == NULL)
5087 {
5088 emsg(_(e_break));
5089 return NULL;
5090 }
5091 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5092 break;
5093 scope = scope->se_outer;
5094 }
5095
5096 // Jump to the end of the FOR or WHILE loop.
5097 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005098 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005100 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5102 return FAIL;
5103
5104 return arg;
5105}
5106
5107/*
5108 * compile "{" start of block
5109 */
5110 static char_u *
5111compile_block(char_u *arg, cctx_T *cctx)
5112{
5113 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5114 return NULL;
5115 return skipwhite(arg + 1);
5116}
5117
5118/*
5119 * compile end of block: drop one scope
5120 */
5121 static void
5122compile_endblock(cctx_T *cctx)
5123{
5124 scope_T *scope = cctx->ctx_scope;
5125
5126 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005127 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 vim_free(scope);
5129}
5130
5131/*
5132 * compile "try"
5133 * Creates a new scope for the try-endtry, pointing to the first catch and
5134 * finally.
5135 * Creates another scope for the "try" block itself.
5136 * TRY instruction sets up exception handling at runtime.
5137 *
5138 * "try"
5139 * TRY -> catch1, -> finally push trystack entry
5140 * ... try block
5141 * "throw {exception}"
5142 * EVAL {exception}
5143 * THROW create exception
5144 * ... try block
5145 * " catch {expr}"
5146 * JUMP -> finally
5147 * catch1: PUSH exeception
5148 * EVAL {expr}
5149 * MATCH
5150 * JUMP nomatch -> catch2
5151 * CATCH remove exception
5152 * ... catch block
5153 * " catch"
5154 * JUMP -> finally
5155 * catch2: CATCH remove exception
5156 * ... catch block
5157 * " finally"
5158 * finally:
5159 * ... finally block
5160 * " endtry"
5161 * ENDTRY pop trystack entry, may rethrow
5162 */
5163 static char_u *
5164compile_try(char_u *arg, cctx_T *cctx)
5165{
5166 garray_T *instr = &cctx->ctx_instr;
5167 scope_T *try_scope;
5168 scope_T *scope;
5169
5170 // scope that holds the jumps that go to catch/finally/endtry
5171 try_scope = new_scope(cctx, TRY_SCOPE);
5172 if (try_scope == NULL)
5173 return NULL;
5174
5175 // "catch" is set when the first ":catch" is found.
5176 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005177 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178 if (generate_instr(cctx, ISN_TRY) == NULL)
5179 return NULL;
5180
5181 // scope for the try block itself
5182 scope = new_scope(cctx, BLOCK_SCOPE);
5183 if (scope == NULL)
5184 return NULL;
5185
5186 return arg;
5187}
5188
5189/*
5190 * compile "catch {expr}"
5191 */
5192 static char_u *
5193compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5194{
5195 scope_T *scope = cctx->ctx_scope;
5196 garray_T *instr = &cctx->ctx_instr;
5197 char_u *p;
5198 isn_T *isn;
5199
5200 // end block scope from :try or :catch
5201 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5202 compile_endblock(cctx);
5203 scope = cctx->ctx_scope;
5204
5205 // Error if not in a :try scope
5206 if (scope == NULL || scope->se_type != TRY_SCOPE)
5207 {
5208 emsg(_(e_catch));
5209 return NULL;
5210 }
5211
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005212 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213 {
5214 emsg(_("E1033: catch unreachable after catch-all"));
5215 return NULL;
5216 }
5217
5218 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005219 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 JUMP_ALWAYS, cctx) == FAIL)
5221 return NULL;
5222
5223 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005224 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 if (isn->isn_arg.try.try_catch == 0)
5226 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005227 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 {
5229 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005230 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231 isn->isn_arg.jump.jump_where = instr->ga_len;
5232 }
5233
5234 p = skipwhite(arg);
5235 if (ends_excmd(*p))
5236 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005237 scope->se_u.se_try.ts_caught_all = TRUE;
5238 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239 }
5240 else
5241 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005242 char_u *end;
5243 char_u *pat;
5244 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005245 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005246 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248 // Push v:exception, push {expr} and MATCH
5249 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5250
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005251 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005252 if (*end != *p)
5253 {
5254 semsg(_("E1067: Separator mismatch: %s"), p);
5255 vim_free(tofree);
5256 return FAIL;
5257 }
5258 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005259 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005260 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005261 len = (int)(end - tofree);
5262 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005263 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005264 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005265 if (pat == NULL)
5266 return FAIL;
5267 if (generate_PUSHS(cctx, pat) == FAIL)
5268 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005270 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5271 return NULL;
5272
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005273 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005274 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5275 return NULL;
5276 }
5277
5278 if (generate_instr(cctx, ISN_CATCH) == NULL)
5279 return NULL;
5280
5281 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5282 return NULL;
5283 return p;
5284}
5285
5286 static char_u *
5287compile_finally(char_u *arg, cctx_T *cctx)
5288{
5289 scope_T *scope = cctx->ctx_scope;
5290 garray_T *instr = &cctx->ctx_instr;
5291 isn_T *isn;
5292
5293 // end block scope from :try or :catch
5294 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5295 compile_endblock(cctx);
5296 scope = cctx->ctx_scope;
5297
5298 // Error if not in a :try scope
5299 if (scope == NULL || scope->se_type != TRY_SCOPE)
5300 {
5301 emsg(_(e_finally));
5302 return NULL;
5303 }
5304
5305 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005306 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005307 if (isn->isn_arg.try.try_finally != 0)
5308 {
5309 emsg(_(e_finally_dup));
5310 return NULL;
5311 }
5312
5313 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005314 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315
Bram Moolenaar585fea72020-04-02 22:33:21 +02005316 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005317 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 {
5319 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005320 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005321 isn->isn_arg.jump.jump_where = instr->ga_len;
5322 }
5323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324 // TODO: set index in ts_finally_label jumps
5325
5326 return arg;
5327}
5328
5329 static char_u *
5330compile_endtry(char_u *arg, cctx_T *cctx)
5331{
5332 scope_T *scope = cctx->ctx_scope;
5333 garray_T *instr = &cctx->ctx_instr;
5334 isn_T *isn;
5335
5336 // end block scope from :catch or :finally
5337 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5338 compile_endblock(cctx);
5339 scope = cctx->ctx_scope;
5340
5341 // Error if not in a :try scope
5342 if (scope == NULL || scope->se_type != TRY_SCOPE)
5343 {
5344 if (scope == NULL)
5345 emsg(_(e_no_endtry));
5346 else if (scope->se_type == WHILE_SCOPE)
5347 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005348 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005349 emsg(_(e_endfor));
5350 else
5351 emsg(_(e_endif));
5352 return NULL;
5353 }
5354
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005355 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005356 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5357 {
5358 emsg(_("E1032: missing :catch or :finally"));
5359 return NULL;
5360 }
5361
5362 // Fill in the "end" label in jumps at the end of the blocks, if not done
5363 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005364 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365
5366 // End :catch or :finally scope: set value in ISN_TRY instruction
5367 if (isn->isn_arg.try.try_finally == 0)
5368 isn->isn_arg.try.try_finally = instr->ga_len;
5369 compile_endblock(cctx);
5370
5371 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5372 return NULL;
5373 return arg;
5374}
5375
5376/*
5377 * compile "throw {expr}"
5378 */
5379 static char_u *
5380compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5381{
5382 char_u *p = skipwhite(arg);
5383
5384 if (ends_excmd(*p))
5385 {
5386 emsg(_(e_argreq));
5387 return NULL;
5388 }
5389 if (compile_expr1(&p, cctx) == FAIL)
5390 return NULL;
5391 if (may_generate_2STRING(-1, cctx) == FAIL)
5392 return NULL;
5393 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5394 return NULL;
5395
5396 return p;
5397}
5398
5399/*
5400 * compile "echo expr"
5401 */
5402 static char_u *
5403compile_echo(char_u *arg, int with_white, cctx_T *cctx)
5404{
5405 char_u *p = arg;
5406 int count = 0;
5407
Bram Moolenaarad39c092020-02-26 18:23:43 +01005408 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409 {
5410 if (compile_expr1(&p, cctx) == FAIL)
5411 return NULL;
5412 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005413 p = skipwhite(p);
5414 if (ends_excmd(*p))
5415 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005416 }
5417
5418 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01005419 return p;
5420}
5421
5422/*
5423 * compile "execute expr"
5424 */
5425 static char_u *
5426compile_execute(char_u *arg, cctx_T *cctx)
5427{
5428 char_u *p = arg;
5429 int count = 0;
5430
5431 for (;;)
5432 {
5433 if (compile_expr1(&p, cctx) == FAIL)
5434 return NULL;
5435 ++count;
5436 p = skipwhite(p);
5437 if (ends_excmd(*p))
5438 break;
5439 }
5440
5441 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442
5443 return p;
5444}
5445
5446/*
5447 * After ex_function() has collected all the function lines: parse and compile
5448 * the lines into instructions.
5449 * Adds the function to "def_functions".
5450 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5451 * return statement (used for lambda).
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005452 * This can be used recursively through compile_lambda(), which may reallocate
5453 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454 */
5455 void
5456compile_def_function(ufunc_T *ufunc, int set_return_type)
5457{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458 char_u *line = NULL;
5459 char_u *p;
5460 exarg_T ea;
5461 char *errormsg = NULL; // error message
5462 int had_return = FALSE;
5463 cctx_T cctx;
5464 garray_T *instr;
5465 int called_emsg_before = called_emsg;
5466 int ret = FAIL;
5467 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005468 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005471 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005472
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005473 if (ufunc->uf_dfunc_idx >= 0)
5474 {
5475 // Redefining a function that was compiled before.
5476 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5477
5478 // Free old instructions.
5479 delete_def_function_contents(dfunc);
5480 }
5481 else
5482 {
5483 // Add the function to "def_functions".
5484 if (ga_grow(&def_functions, 1) == FAIL)
5485 return;
5486 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
5487 vim_memset(dfunc, 0, sizeof(dfunc_T));
5488 dfunc->df_idx = def_functions.ga_len;
5489 ufunc->uf_dfunc_idx = dfunc->df_idx;
5490 dfunc->df_ufunc = ufunc;
5491 ++def_functions.ga_len;
5492 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493 }
5494
5495 vim_memset(&cctx, 0, sizeof(cctx));
5496 cctx.ctx_ufunc = ufunc;
5497 cctx.ctx_lnum = -1;
5498 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5499 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5500 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5501 cctx.ctx_type_list = &ufunc->uf_type_list;
5502 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5503 instr = &cctx.ctx_instr;
5504
5505 // Most modern script version.
5506 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5507
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005508 if (ufunc->uf_def_args.ga_len > 0)
5509 {
5510 int count = ufunc->uf_def_args.ga_len;
5511 int i;
5512 char_u *arg;
5513 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5514
5515 // Produce instructions for the default values of optional arguments.
5516 // Store the instruction index in uf_def_arg_idx[] so that we know
5517 // where to start when the function is called, depending on the number
5518 // of arguments.
5519 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5520 if (ufunc->uf_def_arg_idx == NULL)
5521 goto erret;
5522 for (i = 0; i < count; ++i)
5523 {
5524 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5525 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
5526 if (compile_expr1(&arg, &cctx) == FAIL
5527 || generate_STORE(&cctx, ISN_STORE,
5528 i - count - off, NULL) == FAIL)
5529 goto erret;
5530 }
5531
5532 // If a varargs is following, push an empty list.
5533 if (ufunc->uf_va_name != NULL)
5534 {
5535 if (generate_NEWLIST(&cctx, 0) == FAIL
5536 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
5537 goto erret;
5538 }
5539
5540 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5541 }
5542
5543 /*
5544 * Loop over all the lines of the function and generate instructions.
5545 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 for (;;)
5547 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005548 int is_ex_command;
5549
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005550 // Bail out on the first error to avoid a flood of errors and report
5551 // the right line number when inside try/catch.
5552 if (emsg_before != called_emsg)
5553 goto erret;
5554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005555 if (line != NULL && *line == '|')
5556 // the line continues after a '|'
5557 ++line;
5558 else if (line != NULL && *line != NUL)
5559 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005560 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005561 goto erret;
5562 }
5563 else
5564 {
5565 do
5566 {
5567 ++cctx.ctx_lnum;
5568 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5569 break;
5570 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
5571 } while (line == NULL);
5572 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5573 break;
5574 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
5575 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005576 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005577
5578 had_return = FALSE;
5579 vim_memset(&ea, 0, sizeof(ea));
5580 ea.cmdlinep = &line;
5581 ea.cmd = skipwhite(line);
5582
5583 // "}" ends a block scope
5584 if (*ea.cmd == '}')
5585 {
5586 scopetype_T stype = cctx.ctx_scope == NULL
5587 ? NO_SCOPE : cctx.ctx_scope->se_type;
5588
5589 if (stype == BLOCK_SCOPE)
5590 {
5591 compile_endblock(&cctx);
5592 line = ea.cmd;
5593 }
5594 else
5595 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005596 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005597 goto erret;
5598 }
5599 if (line != NULL)
5600 line = skipwhite(ea.cmd + 1);
5601 continue;
5602 }
5603
5604 // "{" starts a block scope
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01005605 // "{'a': 1}->func() is something else
5606 if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607 {
5608 line = compile_block(ea.cmd, &cctx);
5609 continue;
5610 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005611 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612
5613 /*
5614 * COMMAND MODIFIERS
5615 */
5616 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5617 {
5618 if (errormsg != NULL)
5619 goto erret;
5620 // empty line or comment
5621 line = (char_u *)"";
5622 continue;
5623 }
5624
5625 // Skip ":call" to get to the function name.
5626 if (checkforcmd(&ea.cmd, "call", 3))
5627 ea.cmd = skipwhite(ea.cmd);
5628
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005629 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005630 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005631 // Assuming the command starts with a variable or function name,
5632 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5633 // val".
5634 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5635 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005636 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005637 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005638 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005639 int oplen;
5640 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005642 oplen = assignment_len(skipwhite(p), &heredoc);
5643 if (oplen > 0)
5644 {
5645 // Recognize an assignment if we recognize the variable
5646 // name:
5647 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005648 // "local = expr" where "local" is a local var.
5649 // "script = expr" where "script" is a script-local var.
5650 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005651 // "&opt = expr"
5652 // "$ENV = expr"
5653 // "@r = expr"
5654 if (*ea.cmd == '&'
5655 || *ea.cmd == '$'
5656 || *ea.cmd == '@'
5657 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5658 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5659 || lookup_script(ea.cmd, p - ea.cmd) == OK
5660 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5661 {
5662 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5663 if (line == NULL)
5664 goto erret;
5665 continue;
5666 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005667 }
5668 }
5669 }
5670
5671 /*
5672 * COMMAND after range
5673 */
5674 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005675 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5676 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005677
5678 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5679 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005680 if (cctx.ctx_skip == TRUE)
5681 {
5682 line += STRLEN(line);
5683 continue;
5684 }
5685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005686 // Expression or function call.
5687 if (ea.cmdidx == CMD_eval)
5688 {
5689 p = ea.cmd;
5690 if (compile_expr1(&p, &cctx) == FAIL)
5691 goto erret;
5692
5693 // drop the return value
5694 generate_instr_drop(&cctx, ISN_DROP, 1);
5695 line = p;
5696 continue;
5697 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02005698 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005699 iemsg("Command from find_ex_command() not handled");
5700 goto erret;
5701 }
5702
5703 p = skipwhite(p);
5704
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005705 if (cctx.ctx_skip == TRUE
5706 && ea.cmdidx != CMD_elseif
5707 && ea.cmdidx != CMD_else
5708 && ea.cmdidx != CMD_endif)
5709 {
5710 line += STRLEN(line);
5711 continue;
5712 }
5713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005714 switch (ea.cmdidx)
5715 {
5716 case CMD_def:
5717 case CMD_function:
5718 // TODO: Nested function
5719 emsg("Nested function not implemented yet");
5720 goto erret;
5721
5722 case CMD_return:
5723 line = compile_return(p, set_return_type, &cctx);
5724 had_return = TRUE;
5725 break;
5726
5727 case CMD_let:
5728 case CMD_const:
5729 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5730 break;
5731
5732 case CMD_import:
5733 line = compile_import(p, &cctx);
5734 break;
5735
5736 case CMD_if:
5737 line = compile_if(p, &cctx);
5738 break;
5739 case CMD_elseif:
5740 line = compile_elseif(p, &cctx);
5741 break;
5742 case CMD_else:
5743 line = compile_else(p, &cctx);
5744 break;
5745 case CMD_endif:
5746 line = compile_endif(p, &cctx);
5747 break;
5748
5749 case CMD_while:
5750 line = compile_while(p, &cctx);
5751 break;
5752 case CMD_endwhile:
5753 line = compile_endwhile(p, &cctx);
5754 break;
5755
5756 case CMD_for:
5757 line = compile_for(p, &cctx);
5758 break;
5759 case CMD_endfor:
5760 line = compile_endfor(p, &cctx);
5761 break;
5762 case CMD_continue:
5763 line = compile_continue(p, &cctx);
5764 break;
5765 case CMD_break:
5766 line = compile_break(p, &cctx);
5767 break;
5768
5769 case CMD_try:
5770 line = compile_try(p, &cctx);
5771 break;
5772 case CMD_catch:
5773 line = compile_catch(p, &cctx);
5774 break;
5775 case CMD_finally:
5776 line = compile_finally(p, &cctx);
5777 break;
5778 case CMD_endtry:
5779 line = compile_endtry(p, &cctx);
5780 break;
5781 case CMD_throw:
5782 line = compile_throw(p, &cctx);
5783 break;
5784
5785 case CMD_echo:
5786 line = compile_echo(p, TRUE, &cctx);
5787 break;
5788 case CMD_echon:
5789 line = compile_echo(p, FALSE, &cctx);
5790 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005791 case CMD_execute:
5792 line = compile_execute(p, &cctx);
5793 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005794
5795 default:
5796 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005797 // TODO:
5798 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005799 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005800 generate_EXEC(&cctx, line);
5801 line = (char_u *)"";
5802 break;
5803 }
5804 if (line == NULL)
5805 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02005806 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005807
5808 if (cctx.ctx_type_stack.ga_len < 0)
5809 {
5810 iemsg("Type stack underflow");
5811 goto erret;
5812 }
5813 }
5814
5815 if (cctx.ctx_scope != NULL)
5816 {
5817 if (cctx.ctx_scope->se_type == IF_SCOPE)
5818 emsg(_(e_endif));
5819 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5820 emsg(_(e_endwhile));
5821 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5822 emsg(_(e_endfor));
5823 else
5824 emsg(_("E1026: Missing }"));
5825 goto erret;
5826 }
5827
5828 if (!had_return)
5829 {
5830 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5831 {
5832 emsg(_("E1027: Missing return statement"));
5833 goto erret;
5834 }
5835
5836 // Return zero if there is no return at the end.
5837 generate_PUSHNR(&cctx, 0);
5838 generate_instr(&cctx, ISN_RETURN);
5839 }
5840
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005841 {
5842 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5843 + ufunc->uf_dfunc_idx;
5844 dfunc->df_deleted = FALSE;
5845 dfunc->df_instr = instr->ga_data;
5846 dfunc->df_instr_count = instr->ga_len;
5847 dfunc->df_varcount = cctx.ctx_max_local;
5848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005849
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005850 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005851 int varargs = ufunc->uf_va_name != NULL;
5852 int argcount = ufunc->uf_args.ga_len - (varargs ? 1 : 0);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005853
5854 // Create a type for the function, with the return type and any
5855 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005856 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5857 // The type is included in "tt_args".
5858 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5859 ufunc->uf_args.ga_len, &ufunc->uf_type_list);
5860 if (ufunc->uf_args.ga_len > 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005861 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005862 if (func_type_add_arg_types(ufunc->uf_func_type,
5863 ufunc->uf_args.ga_len,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005864 argcount - ufunc->uf_def_args.ga_len,
5865 &ufunc->uf_type_list) == FAIL)
5866 {
5867 ret = FAIL;
5868 goto erret;
5869 }
5870 if (ufunc->uf_arg_types == NULL)
5871 {
5872 int i;
5873
5874 // lambda does not have argument types.
5875 for (i = 0; i < argcount; ++i)
5876 ufunc->uf_func_type->tt_args[i] = &t_any;
5877 }
5878 else
5879 mch_memmove(ufunc->uf_func_type->tt_args,
5880 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005881 if (varargs)
5882 ufunc->uf_func_type->tt_args[argcount] =
5883 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005884 }
5885 }
5886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005887 ret = OK;
5888
5889erret:
5890 if (ret == FAIL)
5891 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005892 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005893 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5894 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005895
5896 for (idx = 0; idx < instr->ga_len; ++idx)
5897 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005900 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005901 if (!dfunc->df_deleted)
5902 --def_functions.ga_len;
5903
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005904 while (cctx.ctx_scope != NULL)
5905 drop_scope(&cctx);
5906
Bram Moolenaar20431c92020-03-20 18:39:46 +01005907 // Don't execute this function body.
5908 ga_clear_strings(&ufunc->uf_lines);
5909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005910 if (errormsg != NULL)
5911 emsg(errormsg);
5912 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005913 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005914 }
5915
5916 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005917 free_imported(&cctx);
5918 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005919 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005920}
5921
5922/*
5923 * Delete an instruction, free what it contains.
5924 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01005925 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005926delete_instr(isn_T *isn)
5927{
5928 switch (isn->isn_type)
5929 {
5930 case ISN_EXEC:
5931 case ISN_LOADENV:
5932 case ISN_LOADG:
5933 case ISN_LOADOPT:
5934 case ISN_MEMBER:
5935 case ISN_PUSHEXC:
5936 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005937 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005938 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005939 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940 vim_free(isn->isn_arg.string);
5941 break;
5942
5943 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005944 case ISN_STORES:
5945 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005946 break;
5947
5948 case ISN_STOREOPT:
5949 vim_free(isn->isn_arg.storeopt.so_name);
5950 break;
5951
5952 case ISN_PUSHBLOB: // push blob isn_arg.blob
5953 blob_unref(isn->isn_arg.blob);
5954 break;
5955
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005956 case ISN_PUSHPARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01005957 partial_unref(isn->isn_arg.partial);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005958 break;
5959
5960 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005961#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005962 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005963#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005964 break;
5965
5966 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005967#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005968 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005969#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005970 break;
5971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005972 case ISN_UCALL:
5973 vim_free(isn->isn_arg.ufunc.cuf_name);
5974 break;
5975
5976 case ISN_2BOOL:
5977 case ISN_2STRING:
5978 case ISN_ADDBLOB:
5979 case ISN_ADDLIST:
5980 case ISN_BCALL:
5981 case ISN_CATCH:
5982 case ISN_CHECKNR:
5983 case ISN_CHECKTYPE:
5984 case ISN_COMPAREANY:
5985 case ISN_COMPAREBLOB:
5986 case ISN_COMPAREBOOL:
5987 case ISN_COMPAREDICT:
5988 case ISN_COMPAREFLOAT:
5989 case ISN_COMPAREFUNC:
5990 case ISN_COMPARELIST:
5991 case ISN_COMPARENR:
5992 case ISN_COMPAREPARTIAL:
5993 case ISN_COMPARESPECIAL:
5994 case ISN_COMPARESTRING:
5995 case ISN_CONCAT:
5996 case ISN_DCALL:
5997 case ISN_DROP:
5998 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01005999 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000 case ISN_ENDTRY:
6001 case ISN_FOR:
6002 case ISN_FUNCREF:
6003 case ISN_INDEX:
6004 case ISN_JUMP:
6005 case ISN_LOAD:
6006 case ISN_LOADSCRIPT:
6007 case ISN_LOADREG:
6008 case ISN_LOADV:
6009 case ISN_NEGATENR:
6010 case ISN_NEWDICT:
6011 case ISN_NEWLIST:
6012 case ISN_OPNR:
6013 case ISN_OPFLOAT:
6014 case ISN_OPANY:
6015 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006016 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006017 case ISN_PUSHF:
6018 case ISN_PUSHNR:
6019 case ISN_PUSHBOOL:
6020 case ISN_PUSHSPEC:
6021 case ISN_RETURN:
6022 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006023 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006025 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026 case ISN_STORESCRIPT:
6027 case ISN_THROW:
6028 case ISN_TRY:
6029 // nothing allocated
6030 break;
6031 }
6032}
6033
6034/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006035 * Free all instructions for "dfunc".
6036 */
6037 static void
6038delete_def_function_contents(dfunc_T *dfunc)
6039{
6040 int idx;
6041
6042 ga_clear(&dfunc->df_def_args_isn);
6043
6044 if (dfunc->df_instr != NULL)
6045 {
6046 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6047 delete_instr(dfunc->df_instr + idx);
6048 VIM_CLEAR(dfunc->df_instr);
6049 }
6050
6051 dfunc->df_deleted = TRUE;
6052}
6053
6054/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055 * When a user function is deleted, delete any associated def function.
6056 */
6057 void
6058delete_def_function(ufunc_T *ufunc)
6059{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060 if (ufunc->uf_dfunc_idx >= 0)
6061 {
6062 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6063 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064
Bram Moolenaar20431c92020-03-20 18:39:46 +01006065 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066 }
6067}
6068
6069#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006070/*
6071 * Free all functions defined with ":def".
6072 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006073 void
6074free_def_functions(void)
6075{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006076 int idx;
6077
6078 for (idx = 0; idx < def_functions.ga_len; ++idx)
6079 {
6080 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6081
6082 delete_def_function_contents(dfunc);
6083 }
6084
6085 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086}
6087#endif
6088
6089
6090#endif // FEAT_EVAL