blob: 9fdfdb3b64ee175dcaf4638da83909245360b5f5 [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
123 garray_T *ctx_type_list; // space for adding types
124};
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 Moolenaar8a7d6542020-01-26 15:56:19 +0100226 static type_T *
227get_list_type(type_T *member_type, garray_T *type_list)
228{
229 type_T *type;
230
231 // recognize commonly used types
232 if (member_type->tt_type == VAR_UNKNOWN)
233 return &t_list_any;
Bram Moolenaar436472f2020-02-20 22:54:43 +0100234 if (member_type->tt_type == VAR_VOID)
235 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100236 if (member_type->tt_type == VAR_BOOL)
237 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 if (member_type->tt_type == VAR_NUMBER)
239 return &t_list_number;
240 if (member_type->tt_type == VAR_STRING)
241 return &t_list_string;
242
243 // Not a common type, create a new entry.
244 if (ga_grow(type_list, 1) == FAIL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100245 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 type = ((type_T *)type_list->ga_data) + type_list->ga_len;
247 ++type_list->ga_len;
248 type->tt_type = VAR_LIST;
249 type->tt_member = member_type;
250 return type;
251}
252
253 static type_T *
254get_dict_type(type_T *member_type, garray_T *type_list)
255{
256 type_T *type;
257
258 // recognize commonly used types
259 if (member_type->tt_type == VAR_UNKNOWN)
260 return &t_dict_any;
Bram Moolenaar436472f2020-02-20 22:54:43 +0100261 if (member_type->tt_type == VAR_VOID)
262 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100263 if (member_type->tt_type == VAR_BOOL)
264 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 if (member_type->tt_type == VAR_NUMBER)
266 return &t_dict_number;
267 if (member_type->tt_type == VAR_STRING)
268 return &t_dict_string;
269
270 // Not a common type, create a new entry.
271 if (ga_grow(type_list, 1) == FAIL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100272 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273 type = ((type_T *)type_list->ga_data) + type_list->ga_len;
274 ++type_list->ga_len;
275 type->tt_type = VAR_DICT;
276 type->tt_member = member_type;
277 return type;
278}
279
280/////////////////////////////////////////////////////////////////////
281// Following generate_ functions expect the caller to call ga_grow().
282
Bram Moolenaar080457c2020-03-03 21:53:32 +0100283#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
284#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100286/*
287 * Generate an instruction without arguments.
288 * Returns a pointer to the new instruction, NULL if failed.
289 */
290 static isn_T *
291generate_instr(cctx_T *cctx, isntype_T isn_type)
292{
293 garray_T *instr = &cctx->ctx_instr;
294 isn_T *isn;
295
Bram Moolenaar080457c2020-03-03 21:53:32 +0100296 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 if (ga_grow(instr, 1) == FAIL)
298 return NULL;
299 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
300 isn->isn_type = isn_type;
301 isn->isn_lnum = cctx->ctx_lnum + 1;
302 ++instr->ga_len;
303
304 return isn;
305}
306
307/*
308 * Generate an instruction without arguments.
309 * "drop" will be removed from the stack.
310 * Returns a pointer to the new instruction, NULL if failed.
311 */
312 static isn_T *
313generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
314{
315 garray_T *stack = &cctx->ctx_type_stack;
316
Bram Moolenaar080457c2020-03-03 21:53:32 +0100317 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318 stack->ga_len -= drop;
319 return generate_instr(cctx, isn_type);
320}
321
322/*
323 * Generate instruction "isn_type" and put "type" on the type stack.
324 */
325 static isn_T *
326generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
327{
328 isn_T *isn;
329 garray_T *stack = &cctx->ctx_type_stack;
330
331 if ((isn = generate_instr(cctx, isn_type)) == NULL)
332 return NULL;
333
334 if (ga_grow(stack, 1) == FAIL)
335 return NULL;
336 ((type_T **)stack->ga_data)[stack->ga_len] = type;
337 ++stack->ga_len;
338
339 return isn;
340}
341
342/*
343 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
344 */
345 static int
346may_generate_2STRING(int offset, cctx_T *cctx)
347{
348 isn_T *isn;
349 garray_T *stack = &cctx->ctx_type_stack;
350 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
351
352 if ((*type)->tt_type == VAR_STRING)
353 return OK;
354 *type = &t_string;
355
356 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
357 return FAIL;
358 isn->isn_arg.number = offset;
359
360 return OK;
361}
362
363 static int
364check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
365{
366 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_UNKNOWN)
367 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
368 || type2 == VAR_UNKNOWN)))
369 {
370 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100371 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100372 else
373 semsg(_("E1036: %c requires number or float arguments"), *op);
374 return FAIL;
375 }
376 return OK;
377}
378
379/*
380 * Generate an instruction with two arguments. The instruction depends on the
381 * type of the arguments.
382 */
383 static int
384generate_two_op(cctx_T *cctx, char_u *op)
385{
386 garray_T *stack = &cctx->ctx_type_stack;
387 type_T *type1;
388 type_T *type2;
389 vartype_T vartype;
390 isn_T *isn;
391
Bram Moolenaar080457c2020-03-03 21:53:32 +0100392 RETURN_OK_IF_SKIP(cctx);
393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100394 // Get the known type of the two items on the stack. If they are matching
395 // use a type-specific instruction. Otherwise fall back to runtime type
396 // checking.
397 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
398 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
399 vartype = VAR_UNKNOWN;
400 if (type1->tt_type == type2->tt_type
401 && (type1->tt_type == VAR_NUMBER
402 || type1->tt_type == VAR_LIST
403#ifdef FEAT_FLOAT
404 || type1->tt_type == VAR_FLOAT
405#endif
406 || type1->tt_type == VAR_BLOB))
407 vartype = type1->tt_type;
408
409 switch (*op)
410 {
411 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100412 && type1->tt_type != VAR_UNKNOWN
413 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 && check_number_or_float(
415 type1->tt_type, type2->tt_type, op) == FAIL)
416 return FAIL;
417 isn = generate_instr_drop(cctx,
418 vartype == VAR_NUMBER ? ISN_OPNR
419 : vartype == VAR_LIST ? ISN_ADDLIST
420 : vartype == VAR_BLOB ? ISN_ADDBLOB
421#ifdef FEAT_FLOAT
422 : vartype == VAR_FLOAT ? ISN_OPFLOAT
423#endif
424 : ISN_OPANY, 1);
425 if (isn != NULL)
426 isn->isn_arg.op.op_type = EXPR_ADD;
427 break;
428
429 case '-':
430 case '*':
431 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
432 op) == FAIL)
433 return FAIL;
434 if (vartype == VAR_NUMBER)
435 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
436#ifdef FEAT_FLOAT
437 else if (vartype == VAR_FLOAT)
438 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
439#endif
440 else
441 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
442 if (isn != NULL)
443 isn->isn_arg.op.op_type = *op == '*'
444 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
445 break;
446
447 case '%': if ((type1->tt_type != VAR_UNKNOWN
448 && type1->tt_type != VAR_NUMBER)
449 || (type2->tt_type != VAR_UNKNOWN
450 && type2->tt_type != VAR_NUMBER))
451 {
452 emsg(_("E1035: % requires number arguments"));
453 return FAIL;
454 }
455 isn = generate_instr_drop(cctx,
456 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
457 if (isn != NULL)
458 isn->isn_arg.op.op_type = EXPR_REM;
459 break;
460 }
461
462 // correct type of result
463 if (vartype == VAR_UNKNOWN)
464 {
465 type_T *type = &t_any;
466
467#ifdef FEAT_FLOAT
468 // float+number and number+float results in float
469 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
470 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
471 type = &t_float;
472#endif
473 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
474 }
475
476 return OK;
477}
478
479/*
480 * Generate an ISN_COMPARE* instruction with a boolean result.
481 */
482 static int
483generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
484{
485 isntype_T isntype = ISN_DROP;
486 isn_T *isn;
487 garray_T *stack = &cctx->ctx_type_stack;
488 vartype_T type1;
489 vartype_T type2;
490
Bram Moolenaar080457c2020-03-03 21:53:32 +0100491 RETURN_OK_IF_SKIP(cctx);
492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493 // Get the known type of the two items on the stack. If they are matching
494 // use a type-specific instruction. Otherwise fall back to runtime type
495 // checking.
496 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
497 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
498 if (type1 == type2)
499 {
500 switch (type1)
501 {
502 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
503 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
504 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
505 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
506 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
507 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
508 case VAR_LIST: isntype = ISN_COMPARELIST; break;
509 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
510 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
511 case VAR_PARTIAL: isntype = ISN_COMPAREPARTIAL; break;
512 default: isntype = ISN_COMPAREANY; break;
513 }
514 }
515 else if (type1 == VAR_UNKNOWN || type2 == VAR_UNKNOWN
516 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
517 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
518 isntype = ISN_COMPAREANY;
519
520 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
521 && (isntype == ISN_COMPAREBOOL
522 || isntype == ISN_COMPARESPECIAL
523 || isntype == ISN_COMPARENR
524 || isntype == ISN_COMPAREFLOAT))
525 {
526 semsg(_("E1037: Cannot use \"%s\" with %s"),
527 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
528 return FAIL;
529 }
530 if (isntype == ISN_DROP
531 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
532 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
533 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
534 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
535 && exptype != EXPR_IS && exptype != EXPR_ISNOT
536 && (type1 == VAR_BLOB || type2 == VAR_BLOB
537 || type1 == VAR_LIST || type2 == VAR_LIST))))
538 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100539 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100540 vartype_name(type1), vartype_name(type2));
541 return FAIL;
542 }
543
544 if ((isn = generate_instr(cctx, isntype)) == NULL)
545 return FAIL;
546 isn->isn_arg.op.op_type = exptype;
547 isn->isn_arg.op.op_ic = ic;
548
549 // takes two arguments, puts one bool back
550 if (stack->ga_len >= 2)
551 {
552 --stack->ga_len;
553 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
554 }
555
556 return OK;
557}
558
559/*
560 * Generate an ISN_2BOOL instruction.
561 */
562 static int
563generate_2BOOL(cctx_T *cctx, int invert)
564{
565 isn_T *isn;
566 garray_T *stack = &cctx->ctx_type_stack;
567
Bram Moolenaar080457c2020-03-03 21:53:32 +0100568 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
570 return FAIL;
571 isn->isn_arg.number = invert;
572
573 // type becomes bool
574 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
575
576 return OK;
577}
578
579 static int
580generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
581{
582 isn_T *isn;
583 garray_T *stack = &cctx->ctx_type_stack;
584
Bram Moolenaar080457c2020-03-03 21:53:32 +0100585 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
587 return FAIL;
588 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
589 isn->isn_arg.type.ct_off = offset;
590
591 // type becomes vartype
592 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
593
594 return OK;
595}
596
597/*
598 * Generate an ISN_PUSHNR instruction.
599 */
600 static int
601generate_PUSHNR(cctx_T *cctx, varnumber_T number)
602{
603 isn_T *isn;
604
Bram Moolenaar080457c2020-03-03 21:53:32 +0100605 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
607 return FAIL;
608 isn->isn_arg.number = number;
609
610 return OK;
611}
612
613/*
614 * Generate an ISN_PUSHBOOL instruction.
615 */
616 static int
617generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
618{
619 isn_T *isn;
620
Bram Moolenaar080457c2020-03-03 21:53:32 +0100621 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
623 return FAIL;
624 isn->isn_arg.number = number;
625
626 return OK;
627}
628
629/*
630 * Generate an ISN_PUSHSPEC instruction.
631 */
632 static int
633generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
634{
635 isn_T *isn;
636
Bram Moolenaar080457c2020-03-03 21:53:32 +0100637 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
639 return FAIL;
640 isn->isn_arg.number = number;
641
642 return OK;
643}
644
645#ifdef FEAT_FLOAT
646/*
647 * Generate an ISN_PUSHF instruction.
648 */
649 static int
650generate_PUSHF(cctx_T *cctx, float_T fnumber)
651{
652 isn_T *isn;
653
Bram Moolenaar080457c2020-03-03 21:53:32 +0100654 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
656 return FAIL;
657 isn->isn_arg.fnumber = fnumber;
658
659 return OK;
660}
661#endif
662
663/*
664 * Generate an ISN_PUSHS instruction.
665 * Consumes "str".
666 */
667 static int
668generate_PUSHS(cctx_T *cctx, char_u *str)
669{
670 isn_T *isn;
671
Bram Moolenaar080457c2020-03-03 21:53:32 +0100672 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
674 return FAIL;
675 isn->isn_arg.string = str;
676
677 return OK;
678}
679
680/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100681 * Generate an ISN_PUSHCHANNEL instruction.
682 * Consumes "channel".
683 */
684 static int
685generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
686{
687 isn_T *isn;
688
Bram Moolenaar080457c2020-03-03 21:53:32 +0100689 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100690 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
691 return FAIL;
692 isn->isn_arg.channel = channel;
693
694 return OK;
695}
696
697/*
698 * Generate an ISN_PUSHJOB instruction.
699 * Consumes "job".
700 */
701 static int
702generate_PUSHJOB(cctx_T *cctx, job_T *job)
703{
704 isn_T *isn;
705
Bram Moolenaar080457c2020-03-03 21:53:32 +0100706 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100707 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100708 return FAIL;
709 isn->isn_arg.job = job;
710
711 return OK;
712}
713
714/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 * Generate an ISN_PUSHBLOB instruction.
716 * Consumes "blob".
717 */
718 static int
719generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
720{
721 isn_T *isn;
722
Bram Moolenaar080457c2020-03-03 21:53:32 +0100723 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
725 return FAIL;
726 isn->isn_arg.blob = blob;
727
728 return OK;
729}
730
731/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100732 * Generate an ISN_PUSHFUNC instruction with name "name".
733 * Consumes "name".
734 */
735 static int
736generate_PUSHFUNC(cctx_T *cctx, char_u *name)
737{
738 isn_T *isn;
739
Bram Moolenaar080457c2020-03-03 21:53:32 +0100740 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100741 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, &t_func_void)) == NULL)
742 return FAIL;
743 isn->isn_arg.string = name;
744
745 return OK;
746}
747
748/*
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100749 * Generate an ISN_PUSHPARTIAL instruction with partial "part".
750 * Consumes "name".
751 */
752 static int
753generate_PUSHPARTIAL(cctx_T *cctx, partial_T *part)
754{
755 isn_T *isn;
756
Bram Moolenaar080457c2020-03-03 21:53:32 +0100757 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100758 if ((isn = generate_instr_type(cctx, ISN_PUSHPARTIAL,
759 &t_partial_any)) == NULL)
760 return FAIL;
761 isn->isn_arg.partial = part;
762
763 return OK;
764}
765
766/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 * Generate an ISN_STORE instruction.
768 */
769 static int
770generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
771{
772 isn_T *isn;
773
Bram Moolenaar080457c2020-03-03 21:53:32 +0100774 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
776 return FAIL;
777 if (name != NULL)
778 isn->isn_arg.string = vim_strsave(name);
779 else
780 isn->isn_arg.number = idx;
781
782 return OK;
783}
784
785/*
786 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
787 */
788 static int
789generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
790{
791 isn_T *isn;
792
Bram Moolenaar080457c2020-03-03 21:53:32 +0100793 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
795 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100796 isn->isn_arg.storenr.stnr_idx = idx;
797 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798
799 return OK;
800}
801
802/*
803 * Generate an ISN_STOREOPT instruction
804 */
805 static int
806generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
807{
808 isn_T *isn;
809
Bram Moolenaar080457c2020-03-03 21:53:32 +0100810 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
812 return FAIL;
813 isn->isn_arg.storeopt.so_name = vim_strsave(name);
814 isn->isn_arg.storeopt.so_flags = opt_flags;
815
816 return OK;
817}
818
819/*
820 * Generate an ISN_LOAD or similar instruction.
821 */
822 static int
823generate_LOAD(
824 cctx_T *cctx,
825 isntype_T isn_type,
826 int idx,
827 char_u *name,
828 type_T *type)
829{
830 isn_T *isn;
831
Bram Moolenaar080457c2020-03-03 21:53:32 +0100832 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
834 return FAIL;
835 if (name != NULL)
836 isn->isn_arg.string = vim_strsave(name);
837 else
838 isn->isn_arg.number = idx;
839
840 return OK;
841}
842
843/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100844 * Generate an ISN_LOADV instruction.
845 */
846 static int
847generate_LOADV(
848 cctx_T *cctx,
849 char_u *name,
850 int error)
851{
852 // load v:var
853 int vidx = find_vim_var(name);
854
Bram Moolenaar080457c2020-03-03 21:53:32 +0100855 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100856 if (vidx < 0)
857 {
858 if (error)
859 semsg(_(e_var_notfound), name);
860 return FAIL;
861 }
862
863 // TODO: get actual type
864 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
865}
866
867/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 * Generate an ISN_LOADS instruction.
869 */
870 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100871generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100873 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100875 int sid,
876 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877{
878 isn_T *isn;
879
Bram Moolenaar080457c2020-03-03 21:53:32 +0100880 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100881 if (isn_type == ISN_LOADS)
882 isn = generate_instr_type(cctx, isn_type, type);
883 else
884 isn = generate_instr_drop(cctx, isn_type, 1);
885 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100887 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
888 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889
890 return OK;
891}
892
893/*
894 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
895 */
896 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100897generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 cctx_T *cctx,
899 isntype_T isn_type,
900 int sid,
901 int idx,
902 type_T *type)
903{
904 isn_T *isn;
905
Bram Moolenaar080457c2020-03-03 21:53:32 +0100906 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907 if (isn_type == ISN_LOADSCRIPT)
908 isn = generate_instr_type(cctx, isn_type, type);
909 else
910 isn = generate_instr_drop(cctx, isn_type, 1);
911 if (isn == NULL)
912 return FAIL;
913 isn->isn_arg.script.script_sid = sid;
914 isn->isn_arg.script.script_idx = idx;
915 return OK;
916}
917
918/*
919 * Generate an ISN_NEWLIST instruction.
920 */
921 static int
922generate_NEWLIST(cctx_T *cctx, int count)
923{
924 isn_T *isn;
925 garray_T *stack = &cctx->ctx_type_stack;
926 garray_T *type_list = cctx->ctx_type_list;
927 type_T *type;
928 type_T *member;
929
Bram Moolenaar080457c2020-03-03 21:53:32 +0100930 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
932 return FAIL;
933 isn->isn_arg.number = count;
934
935 // drop the value types
936 stack->ga_len -= count;
937
Bram Moolenaar436472f2020-02-20 22:54:43 +0100938 // Use the first value type for the list member type. Use "void" for an
939 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 if (count > 0)
941 member = ((type_T **)stack->ga_data)[stack->ga_len];
942 else
Bram Moolenaar436472f2020-02-20 22:54:43 +0100943 member = &t_void;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944 type = get_list_type(member, type_list);
945
946 // add the list type to the type stack
947 if (ga_grow(stack, 1) == FAIL)
948 return FAIL;
949 ((type_T **)stack->ga_data)[stack->ga_len] = type;
950 ++stack->ga_len;
951
952 return OK;
953}
954
955/*
956 * Generate an ISN_NEWDICT instruction.
957 */
958 static int
959generate_NEWDICT(cctx_T *cctx, int count)
960{
961 isn_T *isn;
962 garray_T *stack = &cctx->ctx_type_stack;
963 garray_T *type_list = cctx->ctx_type_list;
964 type_T *type;
965 type_T *member;
966
Bram Moolenaar080457c2020-03-03 21:53:32 +0100967 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
969 return FAIL;
970 isn->isn_arg.number = count;
971
972 // drop the key and value types
973 stack->ga_len -= 2 * count;
974
Bram Moolenaar436472f2020-02-20 22:54:43 +0100975 // Use the first value type for the list member type. Use "void" for an
976 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 if (count > 0)
978 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
979 else
Bram Moolenaar436472f2020-02-20 22:54:43 +0100980 member = &t_void;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 type = get_dict_type(member, type_list);
982
983 // add the dict type to the type stack
984 if (ga_grow(stack, 1) == FAIL)
985 return FAIL;
986 ((type_T **)stack->ga_data)[stack->ga_len] = type;
987 ++stack->ga_len;
988
989 return OK;
990}
991
992/*
993 * Generate an ISN_FUNCREF instruction.
994 */
995 static int
996generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
997{
998 isn_T *isn;
999 garray_T *stack = &cctx->ctx_type_stack;
1000
Bram Moolenaar080457c2020-03-03 21:53:32 +01001001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1003 return FAIL;
1004 isn->isn_arg.number = dfunc_idx;
1005
1006 if (ga_grow(stack, 1) == FAIL)
1007 return FAIL;
1008 ((type_T **)stack->ga_data)[stack->ga_len] = &t_partial_any;
1009 // TODO: argument and return types
1010 ++stack->ga_len;
1011
1012 return OK;
1013}
1014
1015/*
1016 * Generate an ISN_JUMP instruction.
1017 */
1018 static int
1019generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1020{
1021 isn_T *isn;
1022 garray_T *stack = &cctx->ctx_type_stack;
1023
Bram Moolenaar080457c2020-03-03 21:53:32 +01001024 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001025 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1026 return FAIL;
1027 isn->isn_arg.jump.jump_when = when;
1028 isn->isn_arg.jump.jump_where = where;
1029
1030 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1031 --stack->ga_len;
1032
1033 return OK;
1034}
1035
1036 static int
1037generate_FOR(cctx_T *cctx, int loop_idx)
1038{
1039 isn_T *isn;
1040 garray_T *stack = &cctx->ctx_type_stack;
1041
Bram Moolenaar080457c2020-03-03 21:53:32 +01001042 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1044 return FAIL;
1045 isn->isn_arg.forloop.for_idx = loop_idx;
1046
1047 if (ga_grow(stack, 1) == FAIL)
1048 return FAIL;
1049 // type doesn't matter, will be stored next
1050 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1051 ++stack->ga_len;
1052
1053 return OK;
1054}
1055
1056/*
1057 * Generate an ISN_BCALL instruction.
1058 * Return FAIL if the number of arguments is wrong.
1059 */
1060 static int
1061generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1062{
1063 isn_T *isn;
1064 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001065 type_T *argtypes[MAX_FUNC_ARGS];
1066 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067
Bram Moolenaar080457c2020-03-03 21:53:32 +01001068 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069 if (check_internal_func(func_idx, argcount) == FAIL)
1070 return FAIL;
1071
1072 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1073 return FAIL;
1074 isn->isn_arg.bfunc.cbf_idx = func_idx;
1075 isn->isn_arg.bfunc.cbf_argcount = argcount;
1076
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001077 for (i = 0; i < argcount; ++i)
1078 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 stack->ga_len -= argcount; // drop the arguments
1081 if (ga_grow(stack, 1) == FAIL)
1082 return FAIL;
1083 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001084 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 ++stack->ga_len; // add return value
1086
1087 return OK;
1088}
1089
1090/*
1091 * Generate an ISN_DCALL or ISN_UCALL instruction.
1092 * Return FAIL if the number of arguments is wrong.
1093 */
1094 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001095generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096{
1097 isn_T *isn;
1098 garray_T *stack = &cctx->ctx_type_stack;
1099 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001100 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101
Bram Moolenaar080457c2020-03-03 21:53:32 +01001102 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103 if (argcount > regular_args && !has_varargs(ufunc))
1104 {
1105 semsg(_(e_toomanyarg), ufunc->uf_name);
1106 return FAIL;
1107 }
1108 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1109 {
1110 semsg(_(e_toofewarg), ufunc->uf_name);
1111 return FAIL;
1112 }
1113
1114 // Turn varargs into a list.
1115 if (ufunc->uf_va_name != NULL)
1116 {
1117 int count = argcount - regular_args;
1118
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001119 // If count is negative an empty list will be added after evaluating
1120 // default values for missing optional arguments.
1121 if (count >= 0)
1122 {
1123 generate_NEWLIST(cctx, count);
1124 argcount = regular_args + 1;
1125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 }
1127
1128 if ((isn = generate_instr(cctx,
1129 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1130 return FAIL;
1131 if (ufunc->uf_dfunc_idx >= 0)
1132 {
1133 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1134 isn->isn_arg.dfunc.cdf_argcount = argcount;
1135 }
1136 else
1137 {
1138 // A user function may be deleted and redefined later, can't use the
1139 // ufunc pointer, need to look it up again at runtime.
1140 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1141 isn->isn_arg.ufunc.cuf_argcount = argcount;
1142 }
1143
1144 stack->ga_len -= argcount; // drop the arguments
1145 if (ga_grow(stack, 1) == FAIL)
1146 return FAIL;
1147 // add return value
1148 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1149 ++stack->ga_len;
1150
1151 return OK;
1152}
1153
1154/*
1155 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1156 */
1157 static int
1158generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1159{
1160 isn_T *isn;
1161 garray_T *stack = &cctx->ctx_type_stack;
1162
Bram Moolenaar080457c2020-03-03 21:53:32 +01001163 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1165 return FAIL;
1166 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1167 isn->isn_arg.ufunc.cuf_argcount = argcount;
1168
1169 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001170 if (ga_grow(stack, 1) == FAIL)
1171 return FAIL;
1172 // add return value
1173 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1174 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175
1176 return OK;
1177}
1178
1179/*
1180 * Generate an ISN_PCALL instruction.
1181 */
1182 static int
1183generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1184{
1185 isn_T *isn;
1186 garray_T *stack = &cctx->ctx_type_stack;
1187
Bram Moolenaar080457c2020-03-03 21:53:32 +01001188 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1190 return FAIL;
1191 isn->isn_arg.pfunc.cpf_top = at_top;
1192 isn->isn_arg.pfunc.cpf_argcount = argcount;
1193
1194 stack->ga_len -= argcount; // drop the arguments
1195
1196 // drop the funcref/partial, get back the return value
1197 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1198
1199 return OK;
1200}
1201
1202/*
1203 * Generate an ISN_MEMBER instruction.
1204 */
1205 static int
1206generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1207{
1208 isn_T *isn;
1209 garray_T *stack = &cctx->ctx_type_stack;
1210 type_T *type;
1211
Bram Moolenaar080457c2020-03-03 21:53:32 +01001212 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1214 return FAIL;
1215 isn->isn_arg.string = vim_strnsave(name, (int)len);
1216
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001217 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001219 if (type->tt_type != VAR_DICT && type != &t_any)
1220 {
1221 emsg(_(e_dictreq));
1222 return FAIL;
1223 }
1224 // change dict type to dict member type
1225 if (type->tt_type == VAR_DICT)
1226 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227
1228 return OK;
1229}
1230
1231/*
1232 * Generate an ISN_ECHO instruction.
1233 */
1234 static int
1235generate_ECHO(cctx_T *cctx, int with_white, int count)
1236{
1237 isn_T *isn;
1238
Bram Moolenaar080457c2020-03-03 21:53:32 +01001239 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1241 return FAIL;
1242 isn->isn_arg.echo.echo_with_white = with_white;
1243 isn->isn_arg.echo.echo_count = count;
1244
1245 return OK;
1246}
1247
Bram Moolenaarad39c092020-02-26 18:23:43 +01001248/*
1249 * Generate an ISN_EXECUTE instruction.
1250 */
1251 static int
1252generate_EXECUTE(cctx_T *cctx, int count)
1253{
1254 isn_T *isn;
1255
1256 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1257 return FAIL;
1258 isn->isn_arg.number = count;
1259
1260 return OK;
1261}
1262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 static int
1264generate_EXEC(cctx_T *cctx, char_u *line)
1265{
1266 isn_T *isn;
1267
Bram Moolenaar080457c2020-03-03 21:53:32 +01001268 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1270 return FAIL;
1271 isn->isn_arg.string = vim_strsave(line);
1272 return OK;
1273}
1274
1275static char e_white_both[] =
1276 N_("E1004: white space required before and after '%s'");
1277
1278/*
1279 * Reserve space for a local variable.
1280 * Return the index or -1 if it failed.
1281 */
1282 static int
1283reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1284{
1285 int idx;
1286 lvar_T *lvar;
1287
1288 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1289 {
1290 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1291 return -1;
1292 }
1293
1294 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1295 return -1;
1296 idx = cctx->ctx_locals.ga_len;
1297 if (cctx->ctx_max_local < idx + 1)
1298 cctx->ctx_max_local = idx + 1;
1299 ++cctx->ctx_locals.ga_len;
1300
1301 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1302 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1303 lvar->lv_const = isConst;
1304 lvar->lv_type = type;
1305
1306 return idx;
1307}
1308
1309/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001310 * Remove local variables above "new_top".
1311 */
1312 static void
1313unwind_locals(cctx_T *cctx, int new_top)
1314{
1315 if (cctx->ctx_locals.ga_len > new_top)
1316 {
1317 int idx;
1318 lvar_T *lvar;
1319
1320 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1321 {
1322 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1323 vim_free(lvar->lv_name);
1324 }
1325 }
1326 cctx->ctx_locals.ga_len = new_top;
1327}
1328
1329/*
1330 * Free all local variables.
1331 */
1332 static void
1333free_local(cctx_T *cctx)
1334{
1335 unwind_locals(cctx, 0);
1336 ga_clear(&cctx->ctx_locals);
1337}
1338
1339/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 * Skip over a type definition and return a pointer to just after it.
1341 */
1342 char_u *
1343skip_type(char_u *start)
1344{
1345 char_u *p = start;
1346
1347 while (ASCII_ISALNUM(*p) || *p == '_')
1348 ++p;
1349
1350 // Skip over "<type>"; this is permissive about white space.
1351 if (*skipwhite(p) == '<')
1352 {
1353 p = skipwhite(p);
1354 p = skip_type(skipwhite(p + 1));
1355 p = skipwhite(p);
1356 if (*p == '>')
1357 ++p;
1358 }
1359 return p;
1360}
1361
1362/*
1363 * Parse the member type: "<type>" and return "type" with the member set.
1364 * Use "type_list" if a new type needs to be added.
1365 * Returns NULL in case of failure.
1366 */
1367 static type_T *
1368parse_type_member(char_u **arg, type_T *type, garray_T *type_list)
1369{
1370 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001371 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372
1373 if (**arg != '<')
1374 {
1375 if (*skipwhite(*arg) == '<')
1376 emsg(_("E1007: No white space allowed before <"));
1377 else
1378 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001379 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 }
1381 *arg = skipwhite(*arg + 1);
1382
1383 member_type = parse_type(arg, type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384
1385 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001386 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 {
1388 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001389 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390 }
1391 ++*arg;
1392
1393 if (type->tt_type == VAR_LIST)
1394 return get_list_type(member_type, type_list);
1395 return get_dict_type(member_type, type_list);
1396}
1397
1398/*
1399 * Parse a type at "arg" and advance over it.
1400 * Return NULL for failure.
1401 */
1402 type_T *
1403parse_type(char_u **arg, garray_T *type_list)
1404{
1405 char_u *p = *arg;
1406 size_t len;
1407
1408 // skip over the first word
1409 while (ASCII_ISALNUM(*p) || *p == '_')
1410 ++p;
1411 len = p - *arg;
1412
1413 switch (**arg)
1414 {
1415 case 'a':
1416 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1417 {
1418 *arg += len;
1419 return &t_any;
1420 }
1421 break;
1422 case 'b':
1423 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1424 {
1425 *arg += len;
1426 return &t_bool;
1427 }
1428 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1429 {
1430 *arg += len;
1431 return &t_blob;
1432 }
1433 break;
1434 case 'c':
1435 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1436 {
1437 *arg += len;
1438 return &t_channel;
1439 }
1440 break;
1441 case 'd':
1442 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1443 {
1444 *arg += len;
1445 return parse_type_member(arg, &t_dict_any, type_list);
1446 }
1447 break;
1448 case 'f':
1449 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1450 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001451#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452 *arg += len;
1453 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001454#else
1455 emsg(_("E1055: This Vim is not compiled with float support"));
1456 return &t_any;
1457#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 }
1459 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1460 {
1461 *arg += len;
1462 // TODO: arguments and return type
1463 return &t_func_any;
1464 }
1465 break;
1466 case 'j':
1467 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1468 {
1469 *arg += len;
1470 return &t_job;
1471 }
1472 break;
1473 case 'l':
1474 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1475 {
1476 *arg += len;
1477 return parse_type_member(arg, &t_list_any, type_list);
1478 }
1479 break;
1480 case 'n':
1481 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1482 {
1483 *arg += len;
1484 return &t_number;
1485 }
1486 break;
1487 case 'p':
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001488 if (len == 7 && STRNCMP(*arg, "partial", len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 {
1490 *arg += len;
1491 // TODO: arguments and return type
1492 return &t_partial_any;
1493 }
1494 break;
1495 case 's':
1496 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1497 {
1498 *arg += len;
1499 return &t_string;
1500 }
1501 break;
1502 case 'v':
1503 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1504 {
1505 *arg += len;
1506 return &t_void;
1507 }
1508 break;
1509 }
1510
1511 semsg(_("E1010: Type not recognized: %s"), *arg);
1512 return &t_any;
1513}
1514
1515/*
1516 * Check if "type1" and "type2" are exactly the same.
1517 */
1518 static int
1519equal_type(type_T *type1, type_T *type2)
1520{
1521 if (type1->tt_type != type2->tt_type)
1522 return FALSE;
1523 switch (type1->tt_type)
1524 {
1525 case VAR_VOID:
1526 case VAR_UNKNOWN:
1527 case VAR_SPECIAL:
1528 case VAR_BOOL:
1529 case VAR_NUMBER:
1530 case VAR_FLOAT:
1531 case VAR_STRING:
1532 case VAR_BLOB:
1533 case VAR_JOB:
1534 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001535 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 case VAR_LIST:
1537 case VAR_DICT:
1538 return equal_type(type1->tt_member, type2->tt_member);
1539 case VAR_FUNC:
1540 case VAR_PARTIAL:
1541 // TODO; check argument types.
1542 return equal_type(type1->tt_member, type2->tt_member)
1543 && type1->tt_argcount == type2->tt_argcount;
1544 }
1545 return TRUE;
1546}
1547
1548/*
1549 * Find the common type of "type1" and "type2" and put it in "dest".
1550 * "type2" and "dest" may be the same.
1551 */
1552 static void
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001553common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_list)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554{
1555 if (equal_type(type1, type2))
1556 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001557 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 return;
1559 }
1560
1561 if (type1->tt_type == type2->tt_type)
1562 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1564 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001565 type_T *common;
1566
1567 common_type(type1->tt_member, type2->tt_member, &common, type_list);
1568 if (type1->tt_type == VAR_LIST)
1569 *dest = get_list_type(common, type_list);
1570 else
1571 *dest = get_dict_type(common, type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 return;
1573 }
1574 // TODO: VAR_FUNC and VAR_PARTIAL
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001575 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 }
1577
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001578 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579}
1580
1581 char *
1582vartype_name(vartype_T type)
1583{
1584 switch (type)
1585 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001586 case VAR_UNKNOWN: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 case VAR_SPECIAL: return "special";
1589 case VAR_BOOL: return "bool";
1590 case VAR_NUMBER: return "number";
1591 case VAR_FLOAT: return "float";
1592 case VAR_STRING: return "string";
1593 case VAR_BLOB: return "blob";
1594 case VAR_JOB: return "job";
1595 case VAR_CHANNEL: return "channel";
1596 case VAR_LIST: return "list";
1597 case VAR_DICT: return "dict";
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001598 case VAR_FUNC: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 case VAR_PARTIAL: return "partial";
1600 }
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001601 return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602}
1603
1604/*
1605 * Return the name of a type.
1606 * The result may be in allocated memory, in which case "tofree" is set.
1607 */
1608 char *
1609type_name(type_T *type, char **tofree)
1610{
1611 char *name = vartype_name(type->tt_type);
1612
1613 *tofree = NULL;
1614 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1615 {
1616 char *member_free;
1617 char *member_name = type_name(type->tt_member, &member_free);
1618 size_t len;
1619
1620 len = STRLEN(name) + STRLEN(member_name) + 3;
1621 *tofree = alloc(len);
1622 if (*tofree != NULL)
1623 {
1624 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1625 vim_free(member_free);
1626 return *tofree;
1627 }
1628 }
1629 // TODO: function and partial argument types
1630
1631 return name;
1632}
1633
1634/*
1635 * Find "name" in script-local items of script "sid".
1636 * Returns the index in "sn_var_vals" if found.
1637 * If found but not in "sn_var_vals" returns -1.
1638 * If not found returns -2.
1639 */
1640 int
1641get_script_item_idx(int sid, char_u *name, int check_writable)
1642{
1643 hashtab_T *ht;
1644 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001645 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 int idx;
1647
1648 // First look the name up in the hashtable.
1649 if (sid <= 0 || sid > script_items.ga_len)
1650 return -1;
1651 ht = &SCRIPT_VARS(sid);
1652 di = find_var_in_ht(ht, 0, name, TRUE);
1653 if (di == NULL)
1654 return -2;
1655
1656 // Now find the svar_T index in sn_var_vals.
1657 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1658 {
1659 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1660
1661 if (sv->sv_tv == &di->di_tv)
1662 {
1663 if (check_writable && sv->sv_const)
1664 semsg(_(e_readonlyvar), name);
1665 return idx;
1666 }
1667 }
1668 return -1;
1669}
1670
1671/*
1672 * Find "name" in imported items of the current script/
1673 */
1674 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001675find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001677 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 int idx;
1679
1680 if (cctx != NULL)
1681 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1682 {
1683 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1684 + idx;
1685
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001686 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1687 : STRLEN(import->imp_name) == len
1688 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 return import;
1690 }
1691
1692 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1693 {
1694 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1695
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001696 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1697 : STRLEN(import->imp_name) == len
1698 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 return import;
1700 }
1701 return NULL;
1702}
1703
1704/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001705 * Free all imported variables.
1706 */
1707 static void
1708free_imported(cctx_T *cctx)
1709{
1710 int idx;
1711
1712 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1713 {
1714 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1715
1716 vim_free(import->imp_name);
1717 }
1718 ga_clear(&cctx->ctx_imports);
1719}
1720
1721/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 * Generate an instruction to load script-local variable "name".
1723 */
1724 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001725compile_load_scriptvar(
1726 cctx_T *cctx,
1727 char_u *name, // variable NUL terminated
1728 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001729 char_u **end, // end of variable
1730 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001732 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1734 imported_T *import;
1735
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001736 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001738 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001739 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1740 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 }
1742 if (idx >= 0)
1743 {
1744 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1745
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001746 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 current_sctx.sc_sid, idx, sv->sv_type);
1748 return OK;
1749 }
1750
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001751 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 if (import != NULL)
1753 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001754 if (import->imp_all)
1755 {
1756 char_u *p = skipwhite(*end);
1757 int name_len;
1758 ufunc_T *ufunc;
1759 type_T *type;
1760
1761 // Used "import * as Name", need to lookup the member.
1762 if (*p != '.')
1763 {
1764 semsg(_("E1060: expected dot after name: %s"), start);
1765 return FAIL;
1766 }
1767 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001768 if (VIM_ISWHITE(*p))
1769 {
1770 emsg(_("E1074: no white space allowed after dot"));
1771 return FAIL;
1772 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001773
1774 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
1775 // TODO: what if it is a function?
1776 if (idx < 0)
1777 return FAIL;
1778 *end = p;
1779
1780 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1781 import->imp_sid,
1782 idx,
1783 type);
1784 }
1785 else
1786 {
1787 // TODO: check this is a variable, not a function
1788 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1789 import->imp_sid,
1790 import->imp_var_vals_idx,
1791 import->imp_type);
1792 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 return OK;
1794 }
1795
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001796 if (error)
1797 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 return FAIL;
1799}
1800
1801/*
1802 * Compile a variable name into a load instruction.
1803 * "end" points to just after the name.
1804 * When "error" is FALSE do not give an error when not found.
1805 */
1806 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001807compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808{
1809 type_T *type;
1810 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001811 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001813 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814
1815 if (*(*arg + 1) == ':')
1816 {
1817 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001818 if (end <= *arg + 2)
1819 name = vim_strsave((char_u *)"[empty]");
1820 else
1821 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 if (name == NULL)
1823 return FAIL;
1824
1825 if (**arg == 'v')
1826 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001827 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 }
1829 else if (**arg == 'g')
1830 {
1831 // Global variables can be defined later, thus we don't check if it
1832 // exists, give error at runtime.
1833 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1834 }
1835 else if (**arg == 's')
1836 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001837 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001839 else if (**arg == 'b')
1840 {
1841 semsg("Namespace b: not supported yet: %s", *arg);
1842 goto theend;
1843 }
1844 else if (**arg == 'w')
1845 {
1846 semsg("Namespace w: not supported yet: %s", *arg);
1847 goto theend;
1848 }
1849 else if (**arg == 't')
1850 {
1851 semsg("Namespace t: not supported yet: %s", *arg);
1852 goto theend;
1853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 else
1855 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001856 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 goto theend;
1858 }
1859 }
1860 else
1861 {
1862 size_t len = end - *arg;
1863 int idx;
1864 int gen_load = FALSE;
1865
1866 name = vim_strnsave(*arg, end - *arg);
1867 if (name == NULL)
1868 return FAIL;
1869
1870 idx = lookup_arg(*arg, len, cctx);
1871 if (idx >= 0)
1872 {
1873 if (cctx->ctx_ufunc->uf_arg_types != NULL)
1874 type = cctx->ctx_ufunc->uf_arg_types[idx];
1875 else
1876 type = &t_any;
1877
1878 // Arguments are located above the frame pointer.
1879 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
1880 if (cctx->ctx_ufunc->uf_va_name != NULL)
1881 --idx;
1882 gen_load = TRUE;
1883 }
1884 else if (lookup_vararg(*arg, len, cctx))
1885 {
1886 // varargs is always the last argument
1887 idx = -STACK_FRAME_SIZE - 1;
1888 type = cctx->ctx_ufunc->uf_va_type;
1889 gen_load = TRUE;
1890 }
1891 else
1892 {
1893 idx = lookup_local(*arg, len, cctx);
1894 if (idx >= 0)
1895 {
1896 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
1897 gen_load = TRUE;
1898 }
1899 else
1900 {
1901 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
1902 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
1903 res = generate_PUSHBOOL(cctx, **arg == 't'
1904 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001905 else if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1906 == SCRIPT_VERSION_VIM9)
1907 // in Vim9 script "var" can be script-local.
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001908 res = compile_load_scriptvar(cctx, name, *arg, &end, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 }
1910 }
1911 if (gen_load)
1912 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1913 }
1914
1915 *arg = end;
1916
1917theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001918 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 semsg(_(e_var_notfound), name);
1920 vim_free(name);
1921 return res;
1922}
1923
1924/*
1925 * Compile the argument expressions.
1926 * "arg" points to just after the "(" and is advanced to after the ")"
1927 */
1928 static int
1929compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
1930{
1931 char_u *p = *arg;
1932
1933 while (*p != NUL && *p != ')')
1934 {
1935 if (compile_expr1(&p, cctx) == FAIL)
1936 return FAIL;
1937 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001938
1939 if (*p != ',' && *skipwhite(p) == ',')
1940 {
1941 emsg(_("E1068: No white space allowed before ,"));
1942 p = skipwhite(p);
1943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001945 {
1946 ++p;
1947 if (!VIM_ISWHITE(*p))
1948 emsg(_("E1069: white space required after ,"));
1949 }
1950 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001952 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 if (*p != ')')
1954 {
1955 emsg(_(e_missing_close));
1956 return FAIL;
1957 }
1958 *arg = p + 1;
1959 return OK;
1960}
1961
1962/*
1963 * Compile a function call: name(arg1, arg2)
1964 * "arg" points to "name", "arg + varlen" to the "(".
1965 * "argcount_init" is 1 for "value->method()"
1966 * Instructions:
1967 * EVAL arg1
1968 * EVAL arg2
1969 * BCALL / DCALL / UCALL
1970 */
1971 static int
1972compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
1973{
1974 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01001975 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 int argcount = argcount_init;
1977 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001978 char_u fname_buf[FLEN_FIXED + 1];
1979 char_u *tofree = NULL;
1980 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001982 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983
1984 if (varlen >= sizeof(namebuf))
1985 {
1986 semsg(_("E1011: name too long: %s"), name);
1987 return FAIL;
1988 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001989 vim_strncpy(namebuf, *arg, varlen);
1990 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991
1992 *arg = skipwhite(*arg + varlen + 1);
1993 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001994 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001996 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 {
1998 int idx;
1999
2000 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002001 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002003 {
2004 res = generate_BCALL(cctx, idx, argcount);
2005 goto theend;
2006 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 semsg(_(e_unknownfunc), namebuf);
2008 }
2009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002011 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002013 {
2014 res = generate_CALL(cctx, ufunc, argcount);
2015 goto theend;
2016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017
2018 // If the name is a variable, load it and use PCALL.
2019 p = namebuf;
2020 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002021 {
2022 res = generate_PCALL(cctx, argcount, FALSE);
2023 goto theend;
2024 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025
2026 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002027 res = generate_UCALL(cctx, name, argcount);
2028
2029theend:
2030 vim_free(tofree);
2031 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032}
2033
2034// like NAMESPACE_CHAR but with 'a' and 'l'.
2035#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2036
2037/*
2038 * Find the end of a variable or function name. Unlike find_name_end() this
2039 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002040 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 * Return a pointer to just after the name. Equal to "arg" if there is no
2042 * valid name.
2043 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002044 static char_u *
2045to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046{
2047 char_u *p;
2048
2049 // Quick check for valid starting character.
2050 if (!eval_isnamec1(*arg))
2051 return arg;
2052
2053 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2054 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2055 // and can be used in slice "[n:]".
2056 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002057 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2059 break;
2060 return p;
2061}
2062
2063/*
2064 * Like to_name_end() but also skip over a list or dict constant.
2065 */
2066 char_u *
2067to_name_const_end(char_u *arg)
2068{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002069 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 typval_T rettv;
2071
2072 if (p == arg && *arg == '[')
2073 {
2074
2075 // Can be "[1, 2, 3]->Func()".
2076 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2077 p = arg;
2078 }
2079 else if (p == arg && *arg == '#' && arg[1] == '{')
2080 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002081 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 ++p;
2083 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2084 p = arg;
2085 }
2086 else if (p == arg && *arg == '{')
2087 {
2088 int ret = get_lambda_tv(&p, &rettv, FALSE);
2089
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002090 // Can be "{x -> ret}()".
2091 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 if (ret == NOTDONE)
2093 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2094 if (ret != OK)
2095 p = arg;
2096 }
2097
2098 return p;
2099}
2100
2101 static void
2102type_mismatch(type_T *expected, type_T *actual)
2103{
2104 char *tofree1, *tofree2;
2105
2106 semsg(_("E1013: type mismatch, expected %s but got %s"),
2107 type_name(expected, &tofree1), type_name(actual, &tofree2));
2108 vim_free(tofree1);
2109 vim_free(tofree2);
2110}
2111
2112/*
2113 * Check if the expected and actual types match.
2114 */
2115 static int
2116check_type(type_T *expected, type_T *actual, int give_msg)
2117{
2118 if (expected->tt_type != VAR_UNKNOWN)
2119 {
2120 if (expected->tt_type != actual->tt_type)
2121 {
2122 if (give_msg)
2123 type_mismatch(expected, actual);
2124 return FAIL;
2125 }
2126 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2127 {
Bram Moolenaar436472f2020-02-20 22:54:43 +01002128 int ret;
2129
2130 // void is used for an empty list or dict
2131 if (actual->tt_member == &t_void)
2132 ret = OK;
2133 else
2134 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 if (ret == FAIL && give_msg)
2136 type_mismatch(expected, actual);
2137 return ret;
2138 }
2139 }
2140 return OK;
2141}
2142
2143/*
2144 * Check that
2145 * - "actual" is "expected" type or
2146 * - "actual" is a type that can be "expected" type: add a runtime check; or
2147 * - return FAIL.
2148 */
2149 static int
2150need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2151{
Bram Moolenaar436472f2020-02-20 22:54:43 +01002152 if (check_type(expected, actual, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 return OK;
2154 if (actual->tt_type != VAR_UNKNOWN)
2155 {
2156 type_mismatch(expected, actual);
2157 return FAIL;
2158 }
2159 generate_TYPECHECK(cctx, expected, offset);
2160 return OK;
2161}
2162
2163/*
2164 * parse a list: [expr, expr]
2165 * "*arg" points to the '['.
2166 */
2167 static int
2168compile_list(char_u **arg, cctx_T *cctx)
2169{
2170 char_u *p = skipwhite(*arg + 1);
2171 int count = 0;
2172
2173 while (*p != ']')
2174 {
2175 if (*p == NUL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002176 {
2177 semsg(_(e_list_end), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002178 return FAIL;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 if (compile_expr1(&p, cctx) == FAIL)
2181 break;
2182 ++count;
2183 if (*p == ',')
2184 ++p;
2185 p = skipwhite(p);
2186 }
2187 *arg = p + 1;
2188
2189 generate_NEWLIST(cctx, count);
2190 return OK;
2191}
2192
2193/*
2194 * parse a lambda: {arg, arg -> expr}
2195 * "*arg" points to the '{'.
2196 */
2197 static int
2198compile_lambda(char_u **arg, cctx_T *cctx)
2199{
2200 garray_T *instr = &cctx->ctx_instr;
2201 typval_T rettv;
2202 ufunc_T *ufunc;
2203
2204 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002205 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002209 ++ufunc->uf_refcount;
2210 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211
2212 // The function will have one line: "return {expr}".
2213 // Compile it into instructions.
2214 compile_def_function(ufunc, TRUE);
2215
2216 if (ufunc->uf_dfunc_idx >= 0)
2217 {
2218 if (ga_grow(instr, 1) == FAIL)
2219 return FAIL;
2220 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2221 return OK;
2222 }
2223 return FAIL;
2224}
2225
2226/*
2227 * Compile a lamda call: expr->{lambda}(args)
2228 * "arg" points to the "{".
2229 */
2230 static int
2231compile_lambda_call(char_u **arg, cctx_T *cctx)
2232{
2233 ufunc_T *ufunc;
2234 typval_T rettv;
2235 int argcount = 1;
2236 int ret = FAIL;
2237
2238 // Get the funcref in "rettv".
2239 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2240 return FAIL;
2241
2242 if (**arg != '(')
2243 {
2244 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002245 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 else
2247 semsg(_(e_missing_paren), "lambda");
2248 clear_tv(&rettv);
2249 return FAIL;
2250 }
2251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 ufunc = rettv.vval.v_partial->pt_func;
2253 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002254 clear_tv(&rettv);
2255
2256 // The function will have one line: "return {expr}".
2257 // Compile it into instructions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 compile_def_function(ufunc, TRUE);
2259
2260 // compile the arguments
2261 *arg = skipwhite(*arg + 1);
2262 if (compile_arguments(arg, cctx, &argcount) == OK)
2263 // call the compiled function
2264 ret = generate_CALL(cctx, ufunc, argcount);
2265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 return ret;
2267}
2268
2269/*
2270 * parse a dict: {'key': val} or #{key: val}
2271 * "*arg" points to the '{'.
2272 */
2273 static int
2274compile_dict(char_u **arg, cctx_T *cctx, int literal)
2275{
2276 garray_T *instr = &cctx->ctx_instr;
2277 int count = 0;
2278 dict_T *d = dict_alloc();
2279 dictitem_T *item;
2280
2281 if (d == NULL)
2282 return FAIL;
2283 *arg = skipwhite(*arg + 1);
2284 while (**arg != '}' && **arg != NUL)
2285 {
2286 char_u *key = NULL;
2287
2288 if (literal)
2289 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002290 char_u *p = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291
2292 if (p == *arg)
2293 {
2294 semsg(_("E1014: Invalid key: %s"), *arg);
2295 return FAIL;
2296 }
2297 key = vim_strnsave(*arg, p - *arg);
2298 if (generate_PUSHS(cctx, key) == FAIL)
2299 return FAIL;
2300 *arg = p;
2301 }
2302 else
2303 {
2304 isn_T *isn;
2305
2306 if (compile_expr1(arg, cctx) == FAIL)
2307 return FAIL;
2308 // TODO: check type is string
2309 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2310 if (isn->isn_type == ISN_PUSHS)
2311 key = isn->isn_arg.string;
2312 }
2313
2314 // Check for duplicate keys, if using string keys.
2315 if (key != NULL)
2316 {
2317 item = dict_find(d, key, -1);
2318 if (item != NULL)
2319 {
2320 semsg(_(e_duplicate_key), key);
2321 goto failret;
2322 }
2323 item = dictitem_alloc(key);
2324 if (item != NULL)
2325 {
2326 item->di_tv.v_type = VAR_UNKNOWN;
2327 item->di_tv.v_lock = 0;
2328 if (dict_add(d, item) == FAIL)
2329 dictitem_free(item);
2330 }
2331 }
2332
2333 *arg = skipwhite(*arg);
2334 if (**arg != ':')
2335 {
2336 semsg(_(e_missing_dict_colon), *arg);
2337 return FAIL;
2338 }
2339
2340 *arg = skipwhite(*arg + 1);
2341 if (compile_expr1(arg, cctx) == FAIL)
2342 return FAIL;
2343 ++count;
2344
2345 if (**arg == '}')
2346 break;
2347 if (**arg != ',')
2348 {
2349 semsg(_(e_missing_dict_comma), *arg);
2350 goto failret;
2351 }
2352 *arg = skipwhite(*arg + 1);
2353 }
2354
2355 if (**arg != '}')
2356 {
2357 semsg(_(e_missing_dict_end), *arg);
2358 goto failret;
2359 }
2360 *arg = *arg + 1;
2361
2362 dict_unref(d);
2363 return generate_NEWDICT(cctx, count);
2364
2365failret:
2366 dict_unref(d);
2367 return FAIL;
2368}
2369
2370/*
2371 * Compile "&option".
2372 */
2373 static int
2374compile_get_option(char_u **arg, cctx_T *cctx)
2375{
2376 typval_T rettv;
2377 char_u *start = *arg;
2378 int ret;
2379
2380 // parse the option and get the current value to get the type.
2381 rettv.v_type = VAR_UNKNOWN;
2382 ret = get_option_tv(arg, &rettv, TRUE);
2383 if (ret == OK)
2384 {
2385 // include the '&' in the name, get_option_tv() expects it.
2386 char_u *name = vim_strnsave(start, *arg - start);
2387 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2388
2389 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2390 vim_free(name);
2391 }
2392 clear_tv(&rettv);
2393
2394 return ret;
2395}
2396
2397/*
2398 * Compile "$VAR".
2399 */
2400 static int
2401compile_get_env(char_u **arg, cctx_T *cctx)
2402{
2403 char_u *start = *arg;
2404 int len;
2405 int ret;
2406 char_u *name;
2407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 ++*arg;
2409 len = get_env_len(arg);
2410 if (len == 0)
2411 {
2412 semsg(_(e_syntax_at), start - 1);
2413 return FAIL;
2414 }
2415
2416 // include the '$' in the name, get_env_tv() expects it.
2417 name = vim_strnsave(start, len + 1);
2418 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2419 vim_free(name);
2420 return ret;
2421}
2422
2423/*
2424 * Compile "@r".
2425 */
2426 static int
2427compile_get_register(char_u **arg, cctx_T *cctx)
2428{
2429 int ret;
2430
2431 ++*arg;
2432 if (**arg == NUL)
2433 {
2434 semsg(_(e_syntax_at), *arg - 1);
2435 return FAIL;
2436 }
2437 if (!valid_yank_reg(**arg, TRUE))
2438 {
2439 emsg_invreg(**arg);
2440 return FAIL;
2441 }
2442 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2443 ++*arg;
2444 return ret;
2445}
2446
2447/*
2448 * Apply leading '!', '-' and '+' to constant "rettv".
2449 */
2450 static int
2451apply_leader(typval_T *rettv, char_u *start, char_u *end)
2452{
2453 char_u *p = end;
2454
2455 // this works from end to start
2456 while (p > start)
2457 {
2458 --p;
2459 if (*p == '-' || *p == '+')
2460 {
2461 // only '-' has an effect, for '+' we only check the type
2462#ifdef FEAT_FLOAT
2463 if (rettv->v_type == VAR_FLOAT)
2464 {
2465 if (*p == '-')
2466 rettv->vval.v_float = -rettv->vval.v_float;
2467 }
2468 else
2469#endif
2470 {
2471 varnumber_T val;
2472 int error = FALSE;
2473
2474 // tv_get_number_chk() accepts a string, but we don't want that
2475 // here
2476 if (check_not_string(rettv) == FAIL)
2477 return FAIL;
2478 val = tv_get_number_chk(rettv, &error);
2479 clear_tv(rettv);
2480 if (error)
2481 return FAIL;
2482 if (*p == '-')
2483 val = -val;
2484 rettv->v_type = VAR_NUMBER;
2485 rettv->vval.v_number = val;
2486 }
2487 }
2488 else
2489 {
2490 int v = tv2bool(rettv);
2491
2492 // '!' is permissive in the type.
2493 clear_tv(rettv);
2494 rettv->v_type = VAR_BOOL;
2495 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2496 }
2497 }
2498 return OK;
2499}
2500
2501/*
2502 * Recognize v: variables that are constants and set "rettv".
2503 */
2504 static void
2505get_vim_constant(char_u **arg, typval_T *rettv)
2506{
2507 if (STRNCMP(*arg, "v:true", 6) == 0)
2508 {
2509 rettv->v_type = VAR_BOOL;
2510 rettv->vval.v_number = VVAL_TRUE;
2511 *arg += 6;
2512 }
2513 else if (STRNCMP(*arg, "v:false", 7) == 0)
2514 {
2515 rettv->v_type = VAR_BOOL;
2516 rettv->vval.v_number = VVAL_FALSE;
2517 *arg += 7;
2518 }
2519 else if (STRNCMP(*arg, "v:null", 6) == 0)
2520 {
2521 rettv->v_type = VAR_SPECIAL;
2522 rettv->vval.v_number = VVAL_NULL;
2523 *arg += 6;
2524 }
2525 else if (STRNCMP(*arg, "v:none", 6) == 0)
2526 {
2527 rettv->v_type = VAR_SPECIAL;
2528 rettv->vval.v_number = VVAL_NONE;
2529 *arg += 6;
2530 }
2531}
2532
2533/*
2534 * Compile code to apply '-', '+' and '!'.
2535 */
2536 static int
2537compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2538{
2539 char_u *p = end;
2540
2541 // this works from end to start
2542 while (p > start)
2543 {
2544 --p;
2545 if (*p == '-' || *p == '+')
2546 {
2547 int negate = *p == '-';
2548 isn_T *isn;
2549
2550 // TODO: check type
2551 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2552 {
2553 --p;
2554 if (*p == '-')
2555 negate = !negate;
2556 }
2557 // only '-' has an effect, for '+' we only check the type
2558 if (negate)
2559 isn = generate_instr(cctx, ISN_NEGATENR);
2560 else
2561 isn = generate_instr(cctx, ISN_CHECKNR);
2562 if (isn == NULL)
2563 return FAIL;
2564 }
2565 else
2566 {
2567 int invert = TRUE;
2568
2569 while (p > start && p[-1] == '!')
2570 {
2571 --p;
2572 invert = !invert;
2573 }
2574 if (generate_2BOOL(cctx, invert) == FAIL)
2575 return FAIL;
2576 }
2577 }
2578 return OK;
2579}
2580
2581/*
2582 * Compile whatever comes after "name" or "name()".
2583 */
2584 static int
2585compile_subscript(
2586 char_u **arg,
2587 cctx_T *cctx,
2588 char_u **start_leader,
2589 char_u *end_leader)
2590{
2591 for (;;)
2592 {
2593 if (**arg == '(')
2594 {
2595 int argcount = 0;
2596
2597 // funcref(arg)
2598 *arg = skipwhite(*arg + 1);
2599 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2600 return FAIL;
2601 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2602 return FAIL;
2603 }
2604 else if (**arg == '-' && (*arg)[1] == '>')
2605 {
2606 char_u *p;
2607
2608 // something->method()
2609 // Apply the '!', '-' and '+' first:
2610 // -1.0->func() works like (-1.0)->func()
2611 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2612 return FAIL;
2613 *start_leader = end_leader; // don't apply again later
2614
2615 *arg = skipwhite(*arg + 2);
2616 if (**arg == '{')
2617 {
2618 // lambda call: list->{lambda}
2619 if (compile_lambda_call(arg, cctx) == FAIL)
2620 return FAIL;
2621 }
2622 else
2623 {
2624 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002625 p = *arg;
2626 if (ASCII_ISALPHA(*p) && p[1] == ':')
2627 p += 2;
2628 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 ;
2630 if (*p != '(')
2631 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002632 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 return FAIL;
2634 }
2635 // TODO: base value may not be the first argument
2636 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2637 return FAIL;
2638 }
2639 }
2640 else if (**arg == '[')
2641 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002642 garray_T *stack;
2643 type_T **typep;
2644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 // list index: list[123]
2646 // TODO: more arguments
2647 // TODO: dict member dict['name']
2648 *arg = skipwhite(*arg + 1);
2649 if (compile_expr1(arg, cctx) == FAIL)
2650 return FAIL;
2651
2652 if (**arg != ']')
2653 {
2654 emsg(_(e_missbrac));
2655 return FAIL;
2656 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002657 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658
2659 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2660 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01002661 stack = &cctx->ctx_type_stack;
2662 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2663 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
2664 {
2665 emsg(_(e_listreq));
2666 return FAIL;
2667 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002668 if ((*typep)->tt_type == VAR_LIST)
2669 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 }
2671 else if (**arg == '.' && (*arg)[1] != '.')
2672 {
2673 char_u *p;
2674
2675 ++*arg;
2676 p = *arg;
2677 // dictionary member: dict.name
2678 if (eval_isnamec1(*p))
2679 while (eval_isnamec(*p))
2680 MB_PTR_ADV(p);
2681 if (p == *arg)
2682 {
2683 semsg(_(e_syntax_at), *arg);
2684 return FAIL;
2685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2687 return FAIL;
2688 *arg = p;
2689 }
2690 else
2691 break;
2692 }
2693
2694 // TODO - see handle_subscript():
2695 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2696 // Don't do this when "Func" is already a partial that was bound
2697 // explicitly (pt_auto is FALSE).
2698
2699 return OK;
2700}
2701
2702/*
2703 * Compile an expression at "*p" and add instructions to "instr".
2704 * "p" is advanced until after the expression, skipping white space.
2705 *
2706 * This is the equivalent of eval1(), eval2(), etc.
2707 */
2708
2709/*
2710 * number number constant
2711 * 0zFFFFFFFF Blob constant
2712 * "string" string constant
2713 * 'string' literal string constant
2714 * &option-name option value
2715 * @r register contents
2716 * identifier variable value
2717 * function() function call
2718 * $VAR environment variable
2719 * (expression) nested expression
2720 * [expr, expr] List
2721 * {key: val, key: val} Dictionary
2722 * #{key: val, key: val} Dictionary with literal keys
2723 *
2724 * Also handle:
2725 * ! in front logical NOT
2726 * - in front unary minus
2727 * + in front unary plus (ignored)
2728 * trailing (arg) funcref/partial call
2729 * trailing [] subscript in String or List
2730 * trailing .name entry in Dictionary
2731 * trailing ->name() method call
2732 */
2733 static int
2734compile_expr7(char_u **arg, cctx_T *cctx)
2735{
2736 typval_T rettv;
2737 char_u *start_leader, *end_leader;
2738 int ret = OK;
2739
2740 /*
2741 * Skip '!', '-' and '+' characters. They are handled later.
2742 */
2743 start_leader = *arg;
2744 while (**arg == '!' || **arg == '-' || **arg == '+')
2745 *arg = skipwhite(*arg + 1);
2746 end_leader = *arg;
2747
2748 rettv.v_type = VAR_UNKNOWN;
2749 switch (**arg)
2750 {
2751 /*
2752 * Number constant.
2753 */
2754 case '0': // also for blob starting with 0z
2755 case '1':
2756 case '2':
2757 case '3':
2758 case '4':
2759 case '5':
2760 case '6':
2761 case '7':
2762 case '8':
2763 case '9':
2764 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
2765 return FAIL;
2766 break;
2767
2768 /*
2769 * String constant: "string".
2770 */
2771 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
2772 return FAIL;
2773 break;
2774
2775 /*
2776 * Literal string constant: 'str''ing'.
2777 */
2778 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
2779 return FAIL;
2780 break;
2781
2782 /*
2783 * Constant Vim variable.
2784 */
2785 case 'v': get_vim_constant(arg, &rettv);
2786 ret = NOTDONE;
2787 break;
2788
2789 /*
2790 * List: [expr, expr]
2791 */
2792 case '[': ret = compile_list(arg, cctx);
2793 break;
2794
2795 /*
2796 * Dictionary: #{key: val, key: val}
2797 */
2798 case '#': if ((*arg)[1] == '{')
2799 {
2800 ++*arg;
2801 ret = compile_dict(arg, cctx, TRUE);
2802 }
2803 else
2804 ret = NOTDONE;
2805 break;
2806
2807 /*
2808 * Lambda: {arg, arg -> expr}
2809 * Dictionary: {'key': val, 'key': val}
2810 */
2811 case '{': {
2812 char_u *start = skipwhite(*arg + 1);
2813
2814 // Find out what comes after the arguments.
2815 ret = get_function_args(&start, '-', NULL,
2816 NULL, NULL, NULL, TRUE);
2817 if (ret != FAIL && *start == '>')
2818 ret = compile_lambda(arg, cctx);
2819 else
2820 ret = compile_dict(arg, cctx, FALSE);
2821 }
2822 break;
2823
2824 /*
2825 * Option value: &name
2826 */
2827 case '&': ret = compile_get_option(arg, cctx);
2828 break;
2829
2830 /*
2831 * Environment variable: $VAR.
2832 */
2833 case '$': ret = compile_get_env(arg, cctx);
2834 break;
2835
2836 /*
2837 * Register contents: @r.
2838 */
2839 case '@': ret = compile_get_register(arg, cctx);
2840 break;
2841 /*
2842 * nested expression: (expression).
2843 */
2844 case '(': *arg = skipwhite(*arg + 1);
2845 ret = compile_expr1(arg, cctx); // recursive!
2846 *arg = skipwhite(*arg);
2847 if (**arg == ')')
2848 ++*arg;
2849 else if (ret == OK)
2850 {
2851 emsg(_(e_missing_close));
2852 ret = FAIL;
2853 }
2854 break;
2855
2856 default: ret = NOTDONE;
2857 break;
2858 }
2859 if (ret == FAIL)
2860 return FAIL;
2861
2862 if (rettv.v_type != VAR_UNKNOWN)
2863 {
2864 // apply the '!', '-' and '+' before the constant
2865 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
2866 {
2867 clear_tv(&rettv);
2868 return FAIL;
2869 }
2870 start_leader = end_leader; // don't apply again below
2871
2872 // push constant
2873 switch (rettv.v_type)
2874 {
2875 case VAR_BOOL:
2876 generate_PUSHBOOL(cctx, rettv.vval.v_number);
2877 break;
2878 case VAR_SPECIAL:
2879 generate_PUSHSPEC(cctx, rettv.vval.v_number);
2880 break;
2881 case VAR_NUMBER:
2882 generate_PUSHNR(cctx, rettv.vval.v_number);
2883 break;
2884#ifdef FEAT_FLOAT
2885 case VAR_FLOAT:
2886 generate_PUSHF(cctx, rettv.vval.v_float);
2887 break;
2888#endif
2889 case VAR_BLOB:
2890 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
2891 rettv.vval.v_blob = NULL;
2892 break;
2893 case VAR_STRING:
2894 generate_PUSHS(cctx, rettv.vval.v_string);
2895 rettv.vval.v_string = NULL;
2896 break;
2897 default:
2898 iemsg("constant type missing");
2899 return FAIL;
2900 }
2901 }
2902 else if (ret == NOTDONE)
2903 {
2904 char_u *p;
2905 int r;
2906
2907 if (!eval_isnamec1(**arg))
2908 {
2909 semsg(_("E1015: Name expected: %s"), *arg);
2910 return FAIL;
2911 }
2912
2913 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002914 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 if (*p == '(')
2916 r = compile_call(arg, p - *arg, cctx, 0);
2917 else
2918 r = compile_load(arg, p, cctx, TRUE);
2919 if (r == FAIL)
2920 return FAIL;
2921 }
2922
2923 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
2924 return FAIL;
2925
2926 // Now deal with prefixed '-', '+' and '!', if not done already.
2927 return compile_leader(cctx, start_leader, end_leader);
2928}
2929
2930/*
2931 * * number multiplication
2932 * / number division
2933 * % number modulo
2934 */
2935 static int
2936compile_expr6(char_u **arg, cctx_T *cctx)
2937{
2938 char_u *op;
2939
2940 // get the first variable
2941 if (compile_expr7(arg, cctx) == FAIL)
2942 return FAIL;
2943
2944 /*
2945 * Repeat computing, until no "*", "/" or "%" is following.
2946 */
2947 for (;;)
2948 {
2949 op = skipwhite(*arg);
2950 if (*op != '*' && *op != '/' && *op != '%')
2951 break;
2952 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
2953 {
2954 char_u buf[3];
2955
2956 vim_strncpy(buf, op, 1);
2957 semsg(_(e_white_both), buf);
2958 }
2959 *arg = skipwhite(op + 1);
2960
2961 // get the second variable
2962 if (compile_expr7(arg, cctx) == FAIL)
2963 return FAIL;
2964
2965 generate_two_op(cctx, op);
2966 }
2967
2968 return OK;
2969}
2970
2971/*
2972 * + number addition
2973 * - number subtraction
2974 * .. string concatenation
2975 */
2976 static int
2977compile_expr5(char_u **arg, cctx_T *cctx)
2978{
2979 char_u *op;
2980 int oplen;
2981
2982 // get the first variable
2983 if (compile_expr6(arg, cctx) == FAIL)
2984 return FAIL;
2985
2986 /*
2987 * Repeat computing, until no "+", "-" or ".." is following.
2988 */
2989 for (;;)
2990 {
2991 op = skipwhite(*arg);
2992 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
2993 break;
2994 oplen = (*op == '.' ? 2 : 1);
2995
2996 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
2997 {
2998 char_u buf[3];
2999
3000 vim_strncpy(buf, op, oplen);
3001 semsg(_(e_white_both), buf);
3002 }
3003
3004 *arg = skipwhite(op + oplen);
3005
3006 // get the second variable
3007 if (compile_expr6(arg, cctx) == FAIL)
3008 return FAIL;
3009
3010 if (*op == '.')
3011 {
3012 if (may_generate_2STRING(-2, cctx) == FAIL
3013 || may_generate_2STRING(-1, cctx) == FAIL)
3014 return FAIL;
3015 generate_instr_drop(cctx, ISN_CONCAT, 1);
3016 }
3017 else
3018 generate_two_op(cctx, op);
3019 }
3020
3021 return OK;
3022}
3023
Bram Moolenaar080457c2020-03-03 21:53:32 +01003024 static exptype_T
3025get_compare_type(char_u *p, int *len, int *type_is)
3026{
3027 exptype_T type = EXPR_UNKNOWN;
3028 int i;
3029
3030 switch (p[0])
3031 {
3032 case '=': if (p[1] == '=')
3033 type = EXPR_EQUAL;
3034 else if (p[1] == '~')
3035 type = EXPR_MATCH;
3036 break;
3037 case '!': if (p[1] == '=')
3038 type = EXPR_NEQUAL;
3039 else if (p[1] == '~')
3040 type = EXPR_NOMATCH;
3041 break;
3042 case '>': if (p[1] != '=')
3043 {
3044 type = EXPR_GREATER;
3045 *len = 1;
3046 }
3047 else
3048 type = EXPR_GEQUAL;
3049 break;
3050 case '<': if (p[1] != '=')
3051 {
3052 type = EXPR_SMALLER;
3053 *len = 1;
3054 }
3055 else
3056 type = EXPR_SEQUAL;
3057 break;
3058 case 'i': if (p[1] == 's')
3059 {
3060 // "is" and "isnot"; but not a prefix of a name
3061 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3062 *len = 5;
3063 i = p[*len];
3064 if (!isalnum(i) && i != '_')
3065 {
3066 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3067 *type_is = TRUE;
3068 }
3069 }
3070 break;
3071 }
3072 return type;
3073}
3074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075/*
3076 * expr5a == expr5b
3077 * expr5a =~ expr5b
3078 * expr5a != expr5b
3079 * expr5a !~ expr5b
3080 * expr5a > expr5b
3081 * expr5a >= expr5b
3082 * expr5a < expr5b
3083 * expr5a <= expr5b
3084 * expr5a is expr5b
3085 * expr5a isnot expr5b
3086 *
3087 * Produces instructions:
3088 * EVAL expr5a Push result of "expr5a"
3089 * EVAL expr5b Push result of "expr5b"
3090 * COMPARE one of the compare instructions
3091 */
3092 static int
3093compile_expr4(char_u **arg, cctx_T *cctx)
3094{
3095 exptype_T type = EXPR_UNKNOWN;
3096 char_u *p;
3097 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 int type_is = FALSE;
3099
3100 // get the first variable
3101 if (compile_expr5(arg, cctx) == FAIL)
3102 return FAIL;
3103
3104 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003105 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106
3107 /*
3108 * If there is a comparative operator, use it.
3109 */
3110 if (type != EXPR_UNKNOWN)
3111 {
3112 int ic = FALSE; // Default: do not ignore case
3113
3114 if (type_is && (p[len] == '?' || p[len] == '#'))
3115 {
3116 semsg(_(e_invexpr2), *arg);
3117 return FAIL;
3118 }
3119 // extra question mark appended: ignore case
3120 if (p[len] == '?')
3121 {
3122 ic = TRUE;
3123 ++len;
3124 }
3125 // extra '#' appended: match case (ignored)
3126 else if (p[len] == '#')
3127 ++len;
3128 // nothing appended: match case
3129
3130 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
3131 {
3132 char_u buf[7];
3133
3134 vim_strncpy(buf, p, len);
3135 semsg(_(e_white_both), buf);
3136 }
3137
3138 // get the second variable
3139 *arg = skipwhite(p + len);
3140 if (compile_expr5(arg, cctx) == FAIL)
3141 return FAIL;
3142
3143 generate_COMPARE(cctx, type, ic);
3144 }
3145
3146 return OK;
3147}
3148
3149/*
3150 * Compile || or &&.
3151 */
3152 static int
3153compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3154{
3155 char_u *p = skipwhite(*arg);
3156 int opchar = *op;
3157
3158 if (p[0] == opchar && p[1] == opchar)
3159 {
3160 garray_T *instr = &cctx->ctx_instr;
3161 garray_T end_ga;
3162
3163 /*
3164 * Repeat until there is no following "||" or "&&"
3165 */
3166 ga_init2(&end_ga, sizeof(int), 10);
3167 while (p[0] == opchar && p[1] == opchar)
3168 {
3169 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3170 semsg(_(e_white_both), op);
3171
3172 if (ga_grow(&end_ga, 1) == FAIL)
3173 {
3174 ga_clear(&end_ga);
3175 return FAIL;
3176 }
3177 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3178 ++end_ga.ga_len;
3179 generate_JUMP(cctx, opchar == '|'
3180 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3181
3182 // eval the next expression
3183 *arg = skipwhite(p + 2);
3184 if ((opchar == '|' ? compile_expr3(arg, cctx)
3185 : compile_expr4(arg, cctx)) == FAIL)
3186 {
3187 ga_clear(&end_ga);
3188 return FAIL;
3189 }
3190 p = skipwhite(*arg);
3191 }
3192
3193 // Fill in the end label in all jumps.
3194 while (end_ga.ga_len > 0)
3195 {
3196 isn_T *isn;
3197
3198 --end_ga.ga_len;
3199 isn = ((isn_T *)instr->ga_data)
3200 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3201 isn->isn_arg.jump.jump_where = instr->ga_len;
3202 }
3203 ga_clear(&end_ga);
3204 }
3205
3206 return OK;
3207}
3208
3209/*
3210 * expr4a && expr4a && expr4a logical AND
3211 *
3212 * Produces instructions:
3213 * EVAL expr4a Push result of "expr4a"
3214 * JUMP_AND_KEEP_IF_FALSE end
3215 * EVAL expr4b Push result of "expr4b"
3216 * JUMP_AND_KEEP_IF_FALSE end
3217 * EVAL expr4c Push result of "expr4c"
3218 * end:
3219 */
3220 static int
3221compile_expr3(char_u **arg, cctx_T *cctx)
3222{
3223 // get the first variable
3224 if (compile_expr4(arg, cctx) == FAIL)
3225 return FAIL;
3226
3227 // || and && work almost the same
3228 return compile_and_or(arg, cctx, "&&");
3229}
3230
3231/*
3232 * expr3a || expr3b || expr3c logical OR
3233 *
3234 * Produces instructions:
3235 * EVAL expr3a Push result of "expr3a"
3236 * JUMP_AND_KEEP_IF_TRUE end
3237 * EVAL expr3b Push result of "expr3b"
3238 * JUMP_AND_KEEP_IF_TRUE end
3239 * EVAL expr3c Push result of "expr3c"
3240 * end:
3241 */
3242 static int
3243compile_expr2(char_u **arg, cctx_T *cctx)
3244{
3245 // eval the first expression
3246 if (compile_expr3(arg, cctx) == FAIL)
3247 return FAIL;
3248
3249 // || and && work almost the same
3250 return compile_and_or(arg, cctx, "||");
3251}
3252
3253/*
3254 * Toplevel expression: expr2 ? expr1a : expr1b
3255 *
3256 * Produces instructions:
3257 * EVAL expr2 Push result of "expr"
3258 * JUMP_IF_FALSE alt jump if false
3259 * EVAL expr1a
3260 * JUMP_ALWAYS end
3261 * alt: EVAL expr1b
3262 * end:
3263 */
3264 static int
3265compile_expr1(char_u **arg, cctx_T *cctx)
3266{
3267 char_u *p;
3268
3269 // evaluate the first expression
3270 if (compile_expr2(arg, cctx) == FAIL)
3271 return FAIL;
3272
3273 p = skipwhite(*arg);
3274 if (*p == '?')
3275 {
3276 garray_T *instr = &cctx->ctx_instr;
3277 garray_T *stack = &cctx->ctx_type_stack;
3278 int alt_idx = instr->ga_len;
3279 int end_idx;
3280 isn_T *isn;
3281 type_T *type1;
3282 type_T *type2;
3283
3284 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3285 semsg(_(e_white_both), "?");
3286
3287 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3288
3289 // evaluate the second expression; any type is accepted
3290 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003291 if (compile_expr1(arg, cctx) == FAIL)
3292 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293
3294 // remember the type and drop it
3295 --stack->ga_len;
3296 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3297
3298 end_idx = instr->ga_len;
3299 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3300
3301 // jump here from JUMP_IF_FALSE
3302 isn = ((isn_T *)instr->ga_data) + alt_idx;
3303 isn->isn_arg.jump.jump_where = instr->ga_len;
3304
3305 // Check for the ":".
3306 p = skipwhite(*arg);
3307 if (*p != ':')
3308 {
3309 emsg(_(e_missing_colon));
3310 return FAIL;
3311 }
3312 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3313 semsg(_(e_white_both), ":");
3314
3315 // evaluate the third expression
3316 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003317 if (compile_expr1(arg, cctx) == FAIL)
3318 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319
3320 // If the types differ, the result has a more generic type.
3321 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003322 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323
3324 // jump here from JUMP_ALWAYS
3325 isn = ((isn_T *)instr->ga_data) + end_idx;
3326 isn->isn_arg.jump.jump_where = instr->ga_len;
3327 }
3328 return OK;
3329}
3330
3331/*
3332 * compile "return [expr]"
3333 */
3334 static char_u *
3335compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3336{
3337 char_u *p = arg;
3338 garray_T *stack = &cctx->ctx_type_stack;
3339 type_T *stack_type;
3340
3341 if (*p != NUL && *p != '|' && *p != '\n')
3342 {
3343 // compile return argument into instructions
3344 if (compile_expr1(&p, cctx) == FAIL)
3345 return NULL;
3346
3347 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3348 if (set_return_type)
3349 cctx->ctx_ufunc->uf_ret_type = stack_type;
3350 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3351 == FAIL)
3352 return NULL;
3353 }
3354 else
3355 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003356 // "set_return_type" cannot be TRUE, only used for a lambda which
3357 // always has an argument.
3358 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 {
3360 emsg(_("E1003: Missing return value"));
3361 return NULL;
3362 }
3363
3364 // No argument, return zero.
3365 generate_PUSHNR(cctx, 0);
3366 }
3367
3368 if (generate_instr(cctx, ISN_RETURN) == NULL)
3369 return NULL;
3370
3371 // "return val | endif" is possible
3372 return skipwhite(p);
3373}
3374
3375/*
3376 * Return the length of an assignment operator, or zero if there isn't one.
3377 */
3378 int
3379assignment_len(char_u *p, int *heredoc)
3380{
3381 if (*p == '=')
3382 {
3383 if (p[1] == '<' && p[2] == '<')
3384 {
3385 *heredoc = TRUE;
3386 return 3;
3387 }
3388 return 1;
3389 }
3390 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3391 return 2;
3392 if (STRNCMP(p, "..=", 3) == 0)
3393 return 3;
3394 return 0;
3395}
3396
3397// words that cannot be used as a variable
3398static char *reserved[] = {
3399 "true",
3400 "false",
3401 NULL
3402};
3403
3404/*
3405 * Get a line for "=<<".
3406 * Return a pointer to the line in allocated memory.
3407 * Return NULL for end-of-file or some error.
3408 */
3409 static char_u *
3410heredoc_getline(
3411 int c UNUSED,
3412 void *cookie,
3413 int indent UNUSED,
3414 int do_concat UNUSED)
3415{
3416 cctx_T *cctx = (cctx_T *)cookie;
3417
3418 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003419 {
3420 iemsg("Heredoc got to end");
3421 return NULL;
3422 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 ++cctx->ctx_lnum;
3424 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3425 [cctx->ctx_lnum]);
3426}
3427
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003428typedef enum {
3429 dest_local,
3430 dest_option,
3431 dest_env,
3432 dest_global,
3433 dest_vimvar,
3434 dest_script,
3435 dest_reg,
3436} assign_dest_T;
3437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438/*
3439 * compile "let var [= expr]", "const var = expr" and "var = expr"
3440 * "arg" points to "var".
3441 */
3442 static char_u *
3443compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3444{
3445 char_u *p;
3446 char_u *ret = NULL;
3447 int var_count = 0;
3448 int semicolon = 0;
3449 size_t varlen;
3450 garray_T *instr = &cctx->ctx_instr;
3451 int idx = -1;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003452 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003455 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003457 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 int oplen = 0;
3459 int heredoc = FALSE;
3460 type_T *type;
3461 lvar_T *lvar;
3462 char_u *name;
3463 char_u *sp;
3464 int has_type = FALSE;
3465 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3466 int instr_count = -1;
3467
3468 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3469 if (p == NULL)
3470 return NULL;
3471 if (var_count > 0)
3472 {
3473 // TODO: let [var, var] = list
3474 emsg("Cannot handle a list yet");
3475 return NULL;
3476 }
3477
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003478 // "a: type" is declaring variable "a" with a type, not "a:".
3479 if (is_decl && p == arg + 2 && p[-1] == ':')
3480 --p;
3481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 varlen = p - arg;
3483 name = vim_strnsave(arg, (int)varlen);
3484 if (name == NULL)
3485 return NULL;
3486
Bram Moolenaar080457c2020-03-03 21:53:32 +01003487 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003489 if (*arg == '&')
3490 {
3491 int cc;
3492 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493
Bram Moolenaar080457c2020-03-03 21:53:32 +01003494 dest = dest_option;
3495 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003497 emsg(_(e_const_option));
3498 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 if (is_decl)
3501 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003502 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 goto theend;
3504 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003505 p = arg;
3506 p = find_option_end(&p, &opt_flags);
3507 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003509 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01003510 emsg(_(e_letunexp));
3511 return NULL;
3512 }
3513 cc = *p;
3514 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003515 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003516 *p = cc;
3517 if (opt_type == -3)
3518 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003519 semsg(_(e_unknown_option), arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003520 return NULL;
3521 }
3522 if (opt_type == -2 || opt_type == 0)
3523 type = &t_string;
3524 else
3525 type = &t_number; // both number and boolean option
3526 }
3527 else if (*arg == '$')
3528 {
3529 dest = dest_env;
3530 if (is_decl)
3531 {
3532 semsg(_("E1065: Cannot declare an environment variable: %s"), name);
3533 goto theend;
3534 }
3535 }
3536 else if (*arg == '@')
3537 {
3538 if (!valid_yank_reg(arg[1], TRUE))
3539 {
3540 emsg_invreg(arg[1]);
3541 return FAIL;
3542 }
3543 dest = dest_reg;
3544 if (is_decl)
3545 {
3546 semsg(_("E1066: Cannot declare a register: %s"), name);
3547 goto theend;
3548 }
3549 }
3550 else if (STRNCMP(arg, "g:", 2) == 0)
3551 {
3552 dest = dest_global;
3553 if (is_decl)
3554 {
3555 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3556 goto theend;
3557 }
3558 }
3559 else if (STRNCMP(arg, "v:", 2) == 0)
3560 {
3561 vimvaridx = find_vim_var(name + 2);
3562 if (vimvaridx < 0)
3563 {
3564 semsg(_(e_var_notfound), arg);
3565 goto theend;
3566 }
3567 dest = dest_vimvar;
3568 if (is_decl)
3569 {
3570 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3571 goto theend;
3572 }
3573 }
3574 else
3575 {
3576 for (idx = 0; reserved[idx] != NULL; ++idx)
3577 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003579 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 goto theend;
3581 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003582
3583 idx = lookup_local(arg, varlen, cctx);
3584 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003586 if (is_decl)
3587 {
3588 semsg(_("E1017: Variable already declared: %s"), name);
3589 goto theend;
3590 }
3591 else
3592 {
3593 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3594 if (lvar->lv_const)
3595 {
3596 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3597 goto theend;
3598 }
3599 }
3600 }
3601 else if (STRNCMP(arg, "s:", 2) == 0
3602 || lookup_script(arg, varlen) == OK
3603 || find_imported(arg, varlen, cctx) != NULL)
3604 {
3605 dest = dest_script;
3606 if (is_decl)
3607 {
3608 semsg(_("E1054: Variable already declared in the script: %s"),
3609 name);
3610 goto theend;
3611 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 }
3613 }
3614 }
3615
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003616 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 {
3618 if (is_decl && *p == ':')
3619 {
3620 // parse optional type: "let var: type = expr"
3621 p = skipwhite(p + 1);
3622 type = parse_type(&p, cctx->ctx_type_list);
3623 if (type == NULL)
3624 goto theend;
3625 has_type = TRUE;
3626 }
3627 else if (idx < 0)
3628 {
3629 // global and new local default to "any" type
3630 type = &t_any;
3631 }
3632 else
3633 {
3634 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3635 type = lvar->lv_type;
3636 }
3637 }
3638
3639 sp = p;
3640 p = skipwhite(p);
3641 op = p;
3642 oplen = assignment_len(p, &heredoc);
3643 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3644 {
3645 char_u buf[4];
3646
3647 vim_strncpy(buf, op, oplen);
3648 semsg(_(e_white_both), buf);
3649 }
3650
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003651 if (oplen == 3 && !heredoc && dest != dest_global
3652 && type->tt_type != VAR_STRING && type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01003654 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 goto theend;
3656 }
3657
3658 // +=, /=, etc. require an existing variable
Bram Moolenaar080457c2020-03-03 21:53:32 +01003659 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 {
3661 if (oplen > 1 && !heredoc)
3662 {
3663 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3664 name);
3665 goto theend;
3666 }
3667
3668 // new local variable
3669 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3670 if (idx < 0)
3671 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003672 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 }
3674
3675 if (heredoc)
3676 {
3677 list_T *l;
3678 listitem_T *li;
3679
3680 // [let] varname =<< [trim] {end}
3681 eap->getline = heredoc_getline;
3682 eap->cookie = cctx;
3683 l = heredoc_get(eap, op + 3);
3684
3685 // Push each line and the create the list.
3686 for (li = l->lv_first; li != NULL; li = li->li_next)
3687 {
3688 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3689 li->li_tv.vval.v_string = NULL;
3690 }
3691 generate_NEWLIST(cctx, l->lv_len);
3692 type = &t_list_string;
3693 list_free(l);
3694 p += STRLEN(p);
3695 }
3696 else if (oplen > 0)
3697 {
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003698 int r;
3699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 // for "+=", "*=", "..=" etc. first load the current value
3701 if (*op != '=')
3702 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003703 switch (dest)
3704 {
3705 case dest_option:
3706 // TODO: check the option exists
3707 generate_LOAD(cctx, ISN_LOADOPT, 0, name + 1, type);
3708 break;
3709 case dest_global:
3710 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3711 break;
3712 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003713 compile_load_scriptvar(cctx,
3714 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003715 break;
3716 case dest_env:
3717 // Include $ in the name here
3718 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
3719 break;
3720 case dest_reg:
3721 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
3722 break;
3723 case dest_vimvar:
3724 generate_LOADV(cctx, name + 2, TRUE);
3725 break;
3726 case dest_local:
3727 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3728 break;
3729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 }
3731
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003732 // Compile the expression. Temporarily hide the new local variable
3733 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02003734 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003735 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 instr_count = instr->ga_len;
3737 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003738 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02003739 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003740 ++cctx->ctx_locals.ga_len;
3741 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 goto theend;
3743
3744 if (idx >= 0 && (is_decl || !has_type))
3745 {
3746 garray_T *stack = &cctx->ctx_type_stack;
3747 type_T *stacktype =
3748 ((type_T **)stack->ga_data)[stack->ga_len - 1];
3749
3750 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3751 if (!has_type)
3752 {
3753 if (stacktype->tt_type == VAR_VOID)
3754 {
3755 emsg(_("E1031: Cannot use void value"));
3756 goto theend;
3757 }
3758 else
3759 lvar->lv_type = stacktype;
3760 }
3761 else
3762 if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
3763 goto theend;
3764 }
3765 }
3766 else if (cmdidx == CMD_const)
3767 {
3768 emsg(_("E1021: const requires a value"));
3769 goto theend;
3770 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003771 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 {
3773 emsg(_("E1022: type or initialization required"));
3774 goto theend;
3775 }
3776 else
3777 {
3778 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 if (ga_grow(instr, 1) == FAIL)
3780 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01003781 switch (type->tt_type)
3782 {
3783 case VAR_BOOL:
3784 generate_PUSHBOOL(cctx, VVAL_FALSE);
3785 break;
3786 case VAR_SPECIAL:
3787 generate_PUSHSPEC(cctx, VVAL_NONE);
3788 break;
3789 case VAR_FLOAT:
3790#ifdef FEAT_FLOAT
3791 generate_PUSHF(cctx, 0.0);
3792#endif
3793 break;
3794 case VAR_STRING:
3795 generate_PUSHS(cctx, NULL);
3796 break;
3797 case VAR_BLOB:
3798 generate_PUSHBLOB(cctx, NULL);
3799 break;
3800 case VAR_FUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003801 generate_PUSHFUNC(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003802 break;
3803 case VAR_PARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003804 generate_PUSHPARTIAL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003805 break;
3806 case VAR_LIST:
3807 generate_NEWLIST(cctx, 0);
3808 break;
3809 case VAR_DICT:
3810 generate_NEWDICT(cctx, 0);
3811 break;
3812 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003813 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003814 break;
3815 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003816 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003817 break;
3818 case VAR_NUMBER:
3819 case VAR_UNKNOWN:
3820 case VAR_VOID:
3821 generate_PUSHNR(cctx, 0);
3822 break;
3823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 }
3825
3826 if (oplen > 0 && *op != '=')
3827 {
3828 type_T *expected = &t_number;
3829 garray_T *stack = &cctx->ctx_type_stack;
3830 type_T *stacktype;
3831
3832 // TODO: if type is known use float or any operation
3833
3834 if (*op == '.')
3835 expected = &t_string;
3836 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3837 if (need_type(stacktype, expected, -1, cctx) == FAIL)
3838 goto theend;
3839
3840 if (*op == '.')
3841 generate_instr_drop(cctx, ISN_CONCAT, 1);
3842 else
3843 {
3844 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3845
3846 if (isn == NULL)
3847 goto theend;
3848 switch (*op)
3849 {
3850 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3851 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3852 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3853 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3854 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3855 }
3856 }
3857 }
3858
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003859 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003861 case dest_option:
3862 generate_STOREOPT(cctx, name + 1, opt_flags);
3863 break;
3864 case dest_global:
3865 // include g: with the name, easier to execute that way
3866 generate_STORE(cctx, ISN_STOREG, 0, name);
3867 break;
3868 case dest_env:
3869 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
3870 break;
3871 case dest_reg:
3872 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
3873 break;
3874 case dest_vimvar:
3875 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
3876 break;
3877 case dest_script:
3878 {
3879 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
3880 imported_T *import = NULL;
3881 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003882
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003883 if (name[1] != ':')
3884 {
3885 import = find_imported(name, 0, cctx);
3886 if (import != NULL)
3887 sid = import->imp_sid;
3888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003890 idx = get_script_item_idx(sid, rawname, TRUE);
3891 // TODO: specific type
3892 if (idx < 0)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003893 generate_OLDSCRIPT(cctx, ISN_STORES, name, sid, &t_any);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003894 else
3895 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
3896 sid, idx, &t_any);
3897 }
3898 break;
3899 case dest_local:
3900 {
3901 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003903 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
3904 // into ISN_STORENR
3905 if (instr->ga_len == instr_count + 1
3906 && isn->isn_type == ISN_PUSHNR)
3907 {
3908 varnumber_T val = isn->isn_arg.number;
3909 garray_T *stack = &cctx->ctx_type_stack;
3910
3911 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003912 isn->isn_arg.storenr.stnr_idx = idx;
3913 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003914 if (stack->ga_len > 0)
3915 --stack->ga_len;
3916 }
3917 else
3918 generate_STORE(cctx, ISN_STORE, idx, NULL);
3919 }
3920 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 }
3922 ret = p;
3923
3924theend:
3925 vim_free(name);
3926 return ret;
3927}
3928
3929/*
3930 * Compile an :import command.
3931 */
3932 static char_u *
3933compile_import(char_u *arg, cctx_T *cctx)
3934{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01003935 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936}
3937
3938/*
3939 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3940 */
3941 static int
3942compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3943{
3944 garray_T *instr = &cctx->ctx_instr;
3945 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3946
3947 if (endlabel == NULL)
3948 return FAIL;
3949 endlabel->el_next = *el;
3950 *el = endlabel;
3951 endlabel->el_end_label = instr->ga_len;
3952
3953 generate_JUMP(cctx, when, 0);
3954 return OK;
3955}
3956
3957 static void
3958compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3959{
3960 garray_T *instr = &cctx->ctx_instr;
3961
3962 while (*el != NULL)
3963 {
3964 endlabel_T *cur = (*el);
3965 isn_T *isn;
3966
3967 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3968 isn->isn_arg.jump.jump_where = instr->ga_len;
3969 *el = cur->el_next;
3970 vim_free(cur);
3971 }
3972}
3973
3974/*
3975 * Create a new scope and set up the generic items.
3976 */
3977 static scope_T *
3978new_scope(cctx_T *cctx, scopetype_T type)
3979{
3980 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
3981
3982 if (scope == NULL)
3983 return NULL;
3984 scope->se_outer = cctx->ctx_scope;
3985 cctx->ctx_scope = scope;
3986 scope->se_type = type;
3987 scope->se_local_count = cctx->ctx_locals.ga_len;
3988 return scope;
3989}
3990
3991/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003992 * Evaluate an expression that is a constant:
3993 * has(arg)
3994 *
3995 * Also handle:
3996 * ! in front logical NOT
3997 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003998 * Return FAIL if the expression is not a constant.
3999 */
4000 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004001evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004002{
4003 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004004 char_u *start_leader, *end_leader;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004005
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004006 /*
4007 * Skip '!' characters. They are handled later.
4008 */
4009 start_leader = *arg;
4010 while (**arg == '!')
4011 *arg = skipwhite(*arg + 1);
4012 end_leader = *arg;
4013
4014 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004015 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004016 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004017 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4018 {
4019 tv->v_type = VAR_SPECIAL;
4020 tv->vval.v_number = VVAL_TRUE;
4021 *arg += 4;
4022 return OK;
4023 }
4024 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4025 {
4026 tv->v_type = VAR_SPECIAL;
4027 tv->vval.v_number = VVAL_FALSE;
4028 *arg += 5;
4029 return OK;
4030 }
4031
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004032 if (STRNCMP("has(", *arg, 4) != 0)
4033 return FAIL;
4034 *arg = skipwhite(*arg + 4);
4035
4036 if (**arg == '"')
4037 {
4038 if (get_string_tv(arg, tv, TRUE) == FAIL)
4039 return FAIL;
4040 }
4041 else if (**arg == '\'')
4042 {
4043 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4044 return FAIL;
4045 }
4046 else
4047 return FAIL;
4048
4049 *arg = skipwhite(*arg);
4050 if (**arg != ')')
4051 return FAIL;
4052 *arg = skipwhite(*arg + 1);
4053
4054 argvars[0] = *tv;
4055 argvars[1].v_type = VAR_UNKNOWN;
4056 tv->v_type = VAR_NUMBER;
4057 tv->vval.v_number = 0;
4058 f_has(argvars, tv);
4059 clear_tv(&argvars[0]);
4060
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004061 while (start_leader < end_leader)
4062 {
4063 if (*start_leader == '!')
4064 tv->vval.v_number = !tv->vval.v_number;
4065 ++start_leader;
4066 }
4067
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004068 return OK;
4069}
4070
Bram Moolenaar080457c2020-03-03 21:53:32 +01004071 static int
4072evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4073{
4074 exptype_T type = EXPR_UNKNOWN;
4075 char_u *p;
4076 int len = 2;
4077 int type_is = FALSE;
4078
4079 // get the first variable
4080 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4081 return FAIL;
4082
4083 p = skipwhite(*arg);
4084 type = get_compare_type(p, &len, &type_is);
4085
4086 /*
4087 * If there is a comparative operator, use it.
4088 */
4089 if (type != EXPR_UNKNOWN)
4090 {
4091 // TODO
4092 return FAIL;
4093 }
4094
4095 return OK;
4096}
4097
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004098static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4099
4100/*
4101 * Compile constant || or &&.
4102 */
4103 static int
4104evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4105{
4106 char_u *p = skipwhite(*arg);
4107 int opchar = *op;
4108
4109 if (p[0] == opchar && p[1] == opchar)
4110 {
4111 int val = tv2bool(tv);
4112
4113 /*
4114 * Repeat until there is no following "||" or "&&"
4115 */
4116 while (p[0] == opchar && p[1] == opchar)
4117 {
4118 typval_T tv2;
4119
4120 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4121 return FAIL;
4122
4123 // eval the next expression
4124 *arg = skipwhite(p + 2);
4125 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004126 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004127 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004128 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004129 {
4130 clear_tv(&tv2);
4131 return FAIL;
4132 }
4133 if ((opchar == '&') == val)
4134 {
4135 // false || tv2 or true && tv2: use tv2
4136 clear_tv(tv);
4137 *tv = tv2;
4138 val = tv2bool(tv);
4139 }
4140 else
4141 clear_tv(&tv2);
4142 p = skipwhite(*arg);
4143 }
4144 }
4145
4146 return OK;
4147}
4148
4149/*
4150 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4151 * Return FAIL if the expression is not a constant.
4152 */
4153 static int
4154evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4155{
4156 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004157 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004158 return FAIL;
4159
4160 // || and && work almost the same
4161 return evaluate_const_and_or(arg, cctx, "&&", tv);
4162}
4163
4164/*
4165 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4166 * Return FAIL if the expression is not a constant.
4167 */
4168 static int
4169evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4170{
4171 // evaluate the first expression
4172 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4173 return FAIL;
4174
4175 // || and && work almost the same
4176 return evaluate_const_and_or(arg, cctx, "||", tv);
4177}
4178
4179/*
4180 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4181 * E.g. for "has('feature')".
4182 * This does not produce error messages. "tv" should be cleared afterwards.
4183 * Return FAIL if the expression is not a constant.
4184 */
4185 static int
4186evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4187{
4188 char_u *p;
4189
4190 // evaluate the first expression
4191 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4192 return FAIL;
4193
4194 p = skipwhite(*arg);
4195 if (*p == '?')
4196 {
4197 int val = tv2bool(tv);
4198 typval_T tv2;
4199
4200 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4201 return FAIL;
4202
4203 // evaluate the second expression; any type is accepted
4204 clear_tv(tv);
4205 *arg = skipwhite(p + 1);
4206 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4207 return FAIL;
4208
4209 // Check for the ":".
4210 p = skipwhite(*arg);
4211 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4212 return FAIL;
4213
4214 // evaluate the third expression
4215 *arg = skipwhite(p + 1);
4216 tv2.v_type = VAR_UNKNOWN;
4217 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4218 {
4219 clear_tv(&tv2);
4220 return FAIL;
4221 }
4222 if (val)
4223 {
4224 // use the expr after "?"
4225 clear_tv(&tv2);
4226 }
4227 else
4228 {
4229 // use the expr after ":"
4230 clear_tv(tv);
4231 *tv = tv2;
4232 }
4233 }
4234 return OK;
4235}
4236
4237/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 * compile "if expr"
4239 *
4240 * "if expr" Produces instructions:
4241 * EVAL expr Push result of "expr"
4242 * JUMP_IF_FALSE end
4243 * ... body ...
4244 * end:
4245 *
4246 * "if expr | else" Produces instructions:
4247 * EVAL expr Push result of "expr"
4248 * JUMP_IF_FALSE else
4249 * ... body ...
4250 * JUMP_ALWAYS end
4251 * else:
4252 * ... body ...
4253 * end:
4254 *
4255 * "if expr1 | elseif expr2 | else" Produces instructions:
4256 * EVAL expr Push result of "expr"
4257 * JUMP_IF_FALSE elseif
4258 * ... body ...
4259 * JUMP_ALWAYS end
4260 * elseif:
4261 * EVAL expr Push result of "expr"
4262 * JUMP_IF_FALSE else
4263 * ... body ...
4264 * JUMP_ALWAYS end
4265 * else:
4266 * ... body ...
4267 * end:
4268 */
4269 static char_u *
4270compile_if(char_u *arg, cctx_T *cctx)
4271{
4272 char_u *p = arg;
4273 garray_T *instr = &cctx->ctx_instr;
4274 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004275 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004277 // compile "expr"; if we know it evaluates to FALSE skip the block
4278 tv.v_type = VAR_UNKNOWN;
4279 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4280 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4281 else
4282 cctx->ctx_skip = MAYBE;
4283 clear_tv(&tv);
4284 if (cctx->ctx_skip == MAYBE)
4285 {
4286 p = arg;
4287 if (compile_expr1(&p, cctx) == FAIL)
4288 return NULL;
4289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290
4291 scope = new_scope(cctx, IF_SCOPE);
4292 if (scope == NULL)
4293 return NULL;
4294
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004295 if (cctx->ctx_skip == MAYBE)
4296 {
4297 // "where" is set when ":elseif", "else" or ":endif" is found
4298 scope->se_u.se_if.is_if_label = instr->ga_len;
4299 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4300 }
4301 else
4302 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303
4304 return p;
4305}
4306
4307 static char_u *
4308compile_elseif(char_u *arg, cctx_T *cctx)
4309{
4310 char_u *p = arg;
4311 garray_T *instr = &cctx->ctx_instr;
4312 isn_T *isn;
4313 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004314 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315
4316 if (scope == NULL || scope->se_type != IF_SCOPE)
4317 {
4318 emsg(_(e_elseif_without_if));
4319 return NULL;
4320 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004321 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322
Bram Moolenaar158906c2020-02-06 20:39:45 +01004323 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004324 {
4325 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004327 return NULL;
4328 // previous "if" or "elseif" jumps here
4329 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4330 isn->isn_arg.jump.jump_where = instr->ga_len;
4331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004333 // compile "expr"; if we know it evaluates to FALSE skip the block
4334 tv.v_type = VAR_UNKNOWN;
4335 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4336 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4337 else
4338 cctx->ctx_skip = MAYBE;
4339 clear_tv(&tv);
4340 if (cctx->ctx_skip == MAYBE)
4341 {
4342 p = arg;
4343 if (compile_expr1(&p, cctx) == FAIL)
4344 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004346 // "where" is set when ":elseif", "else" or ":endif" is found
4347 scope->se_u.se_if.is_if_label = instr->ga_len;
4348 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4349 }
4350 else
4351 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352
4353 return p;
4354}
4355
4356 static char_u *
4357compile_else(char_u *arg, cctx_T *cctx)
4358{
4359 char_u *p = arg;
4360 garray_T *instr = &cctx->ctx_instr;
4361 isn_T *isn;
4362 scope_T *scope = cctx->ctx_scope;
4363
4364 if (scope == NULL || scope->se_type != IF_SCOPE)
4365 {
4366 emsg(_(e_else_without_if));
4367 return NULL;
4368 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004369 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004371 // jump from previous block to the end, unless the else block is empty
4372 if (cctx->ctx_skip == MAYBE)
4373 {
4374 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004376 return NULL;
4377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378
Bram Moolenaar158906c2020-02-06 20:39:45 +01004379 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004380 {
4381 if (scope->se_u.se_if.is_if_label >= 0)
4382 {
4383 // previous "if" or "elseif" jumps here
4384 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4385 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004386 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004387 }
4388 }
4389
4390 if (cctx->ctx_skip != MAYBE)
4391 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392
4393 return p;
4394}
4395
4396 static char_u *
4397compile_endif(char_u *arg, cctx_T *cctx)
4398{
4399 scope_T *scope = cctx->ctx_scope;
4400 ifscope_T *ifscope;
4401 garray_T *instr = &cctx->ctx_instr;
4402 isn_T *isn;
4403
4404 if (scope == NULL || scope->se_type != IF_SCOPE)
4405 {
4406 emsg(_(e_endif_without_if));
4407 return NULL;
4408 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004409 ifscope = &scope->se_u.se_if;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004411 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004413 if (scope->se_u.se_if.is_if_label >= 0)
4414 {
4415 // previous "if" or "elseif" jumps here
4416 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4417 isn->isn_arg.jump.jump_where = instr->ga_len;
4418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 // Fill in the "end" label in jumps at the end of the blocks.
4420 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004421 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422
4423 vim_free(scope);
4424 return arg;
4425}
4426
4427/*
4428 * compile "for var in expr"
4429 *
4430 * Produces instructions:
4431 * PUSHNR -1
4432 * STORE loop-idx Set index to -1
4433 * EVAL expr Push result of "expr"
4434 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4435 * - if beyond end, jump to "end"
4436 * - otherwise get item from list and push it
4437 * STORE var Store item in "var"
4438 * ... body ...
4439 * JUMP top Jump back to repeat
4440 * end: DROP Drop the result of "expr"
4441 *
4442 */
4443 static char_u *
4444compile_for(char_u *arg, cctx_T *cctx)
4445{
4446 char_u *p;
4447 size_t varlen;
4448 garray_T *instr = &cctx->ctx_instr;
4449 garray_T *stack = &cctx->ctx_type_stack;
4450 scope_T *scope;
4451 int loop_idx; // index of loop iteration variable
4452 int var_idx; // index of "var"
4453 type_T *vartype;
4454
4455 // TODO: list of variables: "for [key, value] in dict"
4456 // parse "var"
4457 for (p = arg; eval_isnamec1(*p); ++p)
4458 ;
4459 varlen = p - arg;
4460 var_idx = lookup_local(arg, varlen, cctx);
4461 if (var_idx >= 0)
4462 {
4463 semsg(_("E1023: variable already defined: %s"), arg);
4464 return NULL;
4465 }
4466
4467 // consume "in"
4468 p = skipwhite(p);
4469 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4470 {
4471 emsg(_(e_missing_in));
4472 return NULL;
4473 }
4474 p = skipwhite(p + 2);
4475
4476
4477 scope = new_scope(cctx, FOR_SCOPE);
4478 if (scope == NULL)
4479 return NULL;
4480
4481 // Reserve a variable to store the loop iteration counter.
4482 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4483 if (loop_idx < 0)
4484 return NULL;
4485
4486 // Reserve a variable to store "var"
4487 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4488 if (var_idx < 0)
4489 return NULL;
4490
4491 generate_STORENR(cctx, loop_idx, -1);
4492
4493 // compile "expr", it remains on the stack until "endfor"
4494 arg = p;
4495 if (compile_expr1(&arg, cctx) == FAIL)
4496 return NULL;
4497
4498 // now we know the type of "var"
4499 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4500 if (vartype->tt_type != VAR_LIST)
4501 {
4502 emsg(_("E1024: need a List to iterate over"));
4503 return NULL;
4504 }
4505 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
4506 {
4507 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4508
4509 lvar->lv_type = vartype->tt_member;
4510 }
4511
4512 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004513 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514
4515 generate_FOR(cctx, loop_idx);
4516 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4517
4518 return arg;
4519}
4520
4521/*
4522 * compile "endfor"
4523 */
4524 static char_u *
4525compile_endfor(char_u *arg, cctx_T *cctx)
4526{
4527 garray_T *instr = &cctx->ctx_instr;
4528 scope_T *scope = cctx->ctx_scope;
4529 forscope_T *forscope;
4530 isn_T *isn;
4531
4532 if (scope == NULL || scope->se_type != FOR_SCOPE)
4533 {
4534 emsg(_(e_for));
4535 return NULL;
4536 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004537 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004539 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540
4541 // At end of ":for" scope jump back to the FOR instruction.
4542 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4543
4544 // Fill in the "end" label in the FOR statement so it can jump here
4545 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4546 isn->isn_arg.forloop.for_end = instr->ga_len;
4547
4548 // Fill in the "end" label any BREAK statements
4549 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4550
4551 // Below the ":for" scope drop the "expr" list from the stack.
4552 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4553 return NULL;
4554
4555 vim_free(scope);
4556
4557 return arg;
4558}
4559
4560/*
4561 * compile "while expr"
4562 *
4563 * Produces instructions:
4564 * top: EVAL expr Push result of "expr"
4565 * JUMP_IF_FALSE end jump if false
4566 * ... body ...
4567 * JUMP top Jump back to repeat
4568 * end:
4569 *
4570 */
4571 static char_u *
4572compile_while(char_u *arg, cctx_T *cctx)
4573{
4574 char_u *p = arg;
4575 garray_T *instr = &cctx->ctx_instr;
4576 scope_T *scope;
4577
4578 scope = new_scope(cctx, WHILE_SCOPE);
4579 if (scope == NULL)
4580 return NULL;
4581
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004582 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583
4584 // compile "expr"
4585 if (compile_expr1(&p, cctx) == FAIL)
4586 return NULL;
4587
4588 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004589 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 JUMP_IF_FALSE, cctx) == FAIL)
4591 return FAIL;
4592
4593 return p;
4594}
4595
4596/*
4597 * compile "endwhile"
4598 */
4599 static char_u *
4600compile_endwhile(char_u *arg, cctx_T *cctx)
4601{
4602 scope_T *scope = cctx->ctx_scope;
4603
4604 if (scope == NULL || scope->se_type != WHILE_SCOPE)
4605 {
4606 emsg(_(e_while));
4607 return NULL;
4608 }
4609 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004610 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611
4612 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004613 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614
4615 // Fill in the "end" label in the WHILE statement so it can jump here.
4616 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004617 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618
4619 vim_free(scope);
4620
4621 return arg;
4622}
4623
4624/*
4625 * compile "continue"
4626 */
4627 static char_u *
4628compile_continue(char_u *arg, cctx_T *cctx)
4629{
4630 scope_T *scope = cctx->ctx_scope;
4631
4632 for (;;)
4633 {
4634 if (scope == NULL)
4635 {
4636 emsg(_(e_continue));
4637 return NULL;
4638 }
4639 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4640 break;
4641 scope = scope->se_outer;
4642 }
4643
4644 // Jump back to the FOR or WHILE instruction.
4645 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004646 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
4647 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 return arg;
4649}
4650
4651/*
4652 * compile "break"
4653 */
4654 static char_u *
4655compile_break(char_u *arg, cctx_T *cctx)
4656{
4657 scope_T *scope = cctx->ctx_scope;
4658 endlabel_T **el;
4659
4660 for (;;)
4661 {
4662 if (scope == NULL)
4663 {
4664 emsg(_(e_break));
4665 return NULL;
4666 }
4667 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4668 break;
4669 scope = scope->se_outer;
4670 }
4671
4672 // Jump to the end of the FOR or WHILE loop.
4673 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004674 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004676 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
4678 return FAIL;
4679
4680 return arg;
4681}
4682
4683/*
4684 * compile "{" start of block
4685 */
4686 static char_u *
4687compile_block(char_u *arg, cctx_T *cctx)
4688{
4689 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4690 return NULL;
4691 return skipwhite(arg + 1);
4692}
4693
4694/*
4695 * compile end of block: drop one scope
4696 */
4697 static void
4698compile_endblock(cctx_T *cctx)
4699{
4700 scope_T *scope = cctx->ctx_scope;
4701
4702 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004703 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 vim_free(scope);
4705}
4706
4707/*
4708 * compile "try"
4709 * Creates a new scope for the try-endtry, pointing to the first catch and
4710 * finally.
4711 * Creates another scope for the "try" block itself.
4712 * TRY instruction sets up exception handling at runtime.
4713 *
4714 * "try"
4715 * TRY -> catch1, -> finally push trystack entry
4716 * ... try block
4717 * "throw {exception}"
4718 * EVAL {exception}
4719 * THROW create exception
4720 * ... try block
4721 * " catch {expr}"
4722 * JUMP -> finally
4723 * catch1: PUSH exeception
4724 * EVAL {expr}
4725 * MATCH
4726 * JUMP nomatch -> catch2
4727 * CATCH remove exception
4728 * ... catch block
4729 * " catch"
4730 * JUMP -> finally
4731 * catch2: CATCH remove exception
4732 * ... catch block
4733 * " finally"
4734 * finally:
4735 * ... finally block
4736 * " endtry"
4737 * ENDTRY pop trystack entry, may rethrow
4738 */
4739 static char_u *
4740compile_try(char_u *arg, cctx_T *cctx)
4741{
4742 garray_T *instr = &cctx->ctx_instr;
4743 scope_T *try_scope;
4744 scope_T *scope;
4745
4746 // scope that holds the jumps that go to catch/finally/endtry
4747 try_scope = new_scope(cctx, TRY_SCOPE);
4748 if (try_scope == NULL)
4749 return NULL;
4750
4751 // "catch" is set when the first ":catch" is found.
4752 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004753 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754 if (generate_instr(cctx, ISN_TRY) == NULL)
4755 return NULL;
4756
4757 // scope for the try block itself
4758 scope = new_scope(cctx, BLOCK_SCOPE);
4759 if (scope == NULL)
4760 return NULL;
4761
4762 return arg;
4763}
4764
4765/*
4766 * compile "catch {expr}"
4767 */
4768 static char_u *
4769compile_catch(char_u *arg, cctx_T *cctx UNUSED)
4770{
4771 scope_T *scope = cctx->ctx_scope;
4772 garray_T *instr = &cctx->ctx_instr;
4773 char_u *p;
4774 isn_T *isn;
4775
4776 // end block scope from :try or :catch
4777 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4778 compile_endblock(cctx);
4779 scope = cctx->ctx_scope;
4780
4781 // Error if not in a :try scope
4782 if (scope == NULL || scope->se_type != TRY_SCOPE)
4783 {
4784 emsg(_(e_catch));
4785 return NULL;
4786 }
4787
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004788 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 {
4790 emsg(_("E1033: catch unreachable after catch-all"));
4791 return NULL;
4792 }
4793
4794 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004795 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 JUMP_ALWAYS, cctx) == FAIL)
4797 return NULL;
4798
4799 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004800 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 if (isn->isn_arg.try.try_catch == 0)
4802 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004803 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 {
4805 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004806 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807 isn->isn_arg.jump.jump_where = instr->ga_len;
4808 }
4809
4810 p = skipwhite(arg);
4811 if (ends_excmd(*p))
4812 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004813 scope->se_u.se_try.ts_caught_all = TRUE;
4814 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 }
4816 else
4817 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004818 char_u *end;
4819 char_u *pat;
4820 char_u *tofree = NULL;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004821 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 // Push v:exception, push {expr} and MATCH
4824 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
4825
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004826 end = skip_regexp(p + 1, *p, TRUE, &tofree);
4827 if (*end != *p)
4828 {
4829 semsg(_("E1067: Separator mismatch: %s"), p);
4830 vim_free(tofree);
4831 return FAIL;
4832 }
4833 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004834 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004835 else
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004836 len = (int)(end - (tofree + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004837 pat = vim_strnsave(p + 1, len);
4838 vim_free(tofree);
4839 p += len + 2;
4840 if (pat == NULL)
4841 return FAIL;
4842 if (generate_PUSHS(cctx, pat) == FAIL)
4843 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4846 return NULL;
4847
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004848 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4850 return NULL;
4851 }
4852
4853 if (generate_instr(cctx, ISN_CATCH) == NULL)
4854 return NULL;
4855
4856 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4857 return NULL;
4858 return p;
4859}
4860
4861 static char_u *
4862compile_finally(char_u *arg, cctx_T *cctx)
4863{
4864 scope_T *scope = cctx->ctx_scope;
4865 garray_T *instr = &cctx->ctx_instr;
4866 isn_T *isn;
4867
4868 // end block scope from :try or :catch
4869 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4870 compile_endblock(cctx);
4871 scope = cctx->ctx_scope;
4872
4873 // Error if not in a :try scope
4874 if (scope == NULL || scope->se_type != TRY_SCOPE)
4875 {
4876 emsg(_(e_finally));
4877 return NULL;
4878 }
4879
4880 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004881 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882 if (isn->isn_arg.try.try_finally != 0)
4883 {
4884 emsg(_(e_finally_dup));
4885 return NULL;
4886 }
4887
4888 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004889 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004891 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 {
4893 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004894 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 isn->isn_arg.jump.jump_where = instr->ga_len;
4896 }
4897
4898 isn->isn_arg.try.try_finally = instr->ga_len;
4899 // TODO: set index in ts_finally_label jumps
4900
4901 return arg;
4902}
4903
4904 static char_u *
4905compile_endtry(char_u *arg, cctx_T *cctx)
4906{
4907 scope_T *scope = cctx->ctx_scope;
4908 garray_T *instr = &cctx->ctx_instr;
4909 isn_T *isn;
4910
4911 // end block scope from :catch or :finally
4912 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4913 compile_endblock(cctx);
4914 scope = cctx->ctx_scope;
4915
4916 // Error if not in a :try scope
4917 if (scope == NULL || scope->se_type != TRY_SCOPE)
4918 {
4919 if (scope == NULL)
4920 emsg(_(e_no_endtry));
4921 else if (scope->se_type == WHILE_SCOPE)
4922 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01004923 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 emsg(_(e_endfor));
4925 else
4926 emsg(_(e_endif));
4927 return NULL;
4928 }
4929
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004930 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
4932 {
4933 emsg(_("E1032: missing :catch or :finally"));
4934 return NULL;
4935 }
4936
4937 // Fill in the "end" label in jumps at the end of the blocks, if not done
4938 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004939 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940
4941 // End :catch or :finally scope: set value in ISN_TRY instruction
4942 if (isn->isn_arg.try.try_finally == 0)
4943 isn->isn_arg.try.try_finally = instr->ga_len;
4944 compile_endblock(cctx);
4945
4946 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4947 return NULL;
4948 return arg;
4949}
4950
4951/*
4952 * compile "throw {expr}"
4953 */
4954 static char_u *
4955compile_throw(char_u *arg, cctx_T *cctx UNUSED)
4956{
4957 char_u *p = skipwhite(arg);
4958
4959 if (ends_excmd(*p))
4960 {
4961 emsg(_(e_argreq));
4962 return NULL;
4963 }
4964 if (compile_expr1(&p, cctx) == FAIL)
4965 return NULL;
4966 if (may_generate_2STRING(-1, cctx) == FAIL)
4967 return NULL;
4968 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
4969 return NULL;
4970
4971 return p;
4972}
4973
4974/*
4975 * compile "echo expr"
4976 */
4977 static char_u *
4978compile_echo(char_u *arg, int with_white, cctx_T *cctx)
4979{
4980 char_u *p = arg;
4981 int count = 0;
4982
Bram Moolenaarad39c092020-02-26 18:23:43 +01004983 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 {
4985 if (compile_expr1(&p, cctx) == FAIL)
4986 return NULL;
4987 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004988 p = skipwhite(p);
4989 if (ends_excmd(*p))
4990 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 }
4992
4993 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01004994 return p;
4995}
4996
4997/*
4998 * compile "execute expr"
4999 */
5000 static char_u *
5001compile_execute(char_u *arg, cctx_T *cctx)
5002{
5003 char_u *p = arg;
5004 int count = 0;
5005
5006 for (;;)
5007 {
5008 if (compile_expr1(&p, cctx) == FAIL)
5009 return NULL;
5010 ++count;
5011 p = skipwhite(p);
5012 if (ends_excmd(*p))
5013 break;
5014 }
5015
5016 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017
5018 return p;
5019}
5020
5021/*
5022 * After ex_function() has collected all the function lines: parse and compile
5023 * the lines into instructions.
5024 * Adds the function to "def_functions".
5025 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5026 * return statement (used for lambda).
5027 */
5028 void
5029compile_def_function(ufunc_T *ufunc, int set_return_type)
5030{
5031 dfunc_T *dfunc;
5032 char_u *line = NULL;
5033 char_u *p;
5034 exarg_T ea;
5035 char *errormsg = NULL; // error message
5036 int had_return = FALSE;
5037 cctx_T cctx;
5038 garray_T *instr;
5039 int called_emsg_before = called_emsg;
5040 int ret = FAIL;
5041 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005042 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043
5044 if (ufunc->uf_dfunc_idx >= 0)
5045 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005046 // Redefining a function that was compiled before.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005048
5049 // Free old instructions.
5050 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 }
5052 else
5053 {
5054 // Add the function to "def_functions".
5055 if (ga_grow(&def_functions, 1) == FAIL)
5056 return;
5057 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
5058 vim_memset(dfunc, 0, sizeof(dfunc_T));
5059 dfunc->df_idx = def_functions.ga_len;
5060 ufunc->uf_dfunc_idx = dfunc->df_idx;
5061 dfunc->df_ufunc = ufunc;
5062 ++def_functions.ga_len;
5063 }
5064
5065 vim_memset(&cctx, 0, sizeof(cctx));
5066 cctx.ctx_ufunc = ufunc;
5067 cctx.ctx_lnum = -1;
5068 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5069 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5070 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5071 cctx.ctx_type_list = &ufunc->uf_type_list;
5072 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5073 instr = &cctx.ctx_instr;
5074
5075 // Most modern script version.
5076 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5077
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005078 if (ufunc->uf_def_args.ga_len > 0)
5079 {
5080 int count = ufunc->uf_def_args.ga_len;
5081 int i;
5082 char_u *arg;
5083 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5084
5085 // Produce instructions for the default values of optional arguments.
5086 // Store the instruction index in uf_def_arg_idx[] so that we know
5087 // where to start when the function is called, depending on the number
5088 // of arguments.
5089 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5090 if (ufunc->uf_def_arg_idx == NULL)
5091 goto erret;
5092 for (i = 0; i < count; ++i)
5093 {
5094 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5095 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
5096 if (compile_expr1(&arg, &cctx) == FAIL
5097 || generate_STORE(&cctx, ISN_STORE,
5098 i - count - off, NULL) == FAIL)
5099 goto erret;
5100 }
5101
5102 // If a varargs is following, push an empty list.
5103 if (ufunc->uf_va_name != NULL)
5104 {
5105 if (generate_NEWLIST(&cctx, 0) == FAIL
5106 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
5107 goto erret;
5108 }
5109
5110 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5111 }
5112
5113 /*
5114 * Loop over all the lines of the function and generate instructions.
5115 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 for (;;)
5117 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005118 int is_ex_command;
5119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120 if (line != NULL && *line == '|')
5121 // the line continues after a '|'
5122 ++line;
5123 else if (line != NULL && *line != NUL)
5124 {
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005125 if (emsg_before == called_emsg)
5126 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127 goto erret;
5128 }
5129 else
5130 {
5131 do
5132 {
5133 ++cctx.ctx_lnum;
5134 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5135 break;
5136 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
5137 } while (line == NULL);
5138 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5139 break;
5140 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
5141 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005142 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005143
5144 had_return = FALSE;
5145 vim_memset(&ea, 0, sizeof(ea));
5146 ea.cmdlinep = &line;
5147 ea.cmd = skipwhite(line);
5148
5149 // "}" ends a block scope
5150 if (*ea.cmd == '}')
5151 {
5152 scopetype_T stype = cctx.ctx_scope == NULL
5153 ? NO_SCOPE : cctx.ctx_scope->se_type;
5154
5155 if (stype == BLOCK_SCOPE)
5156 {
5157 compile_endblock(&cctx);
5158 line = ea.cmd;
5159 }
5160 else
5161 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005162 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163 goto erret;
5164 }
5165 if (line != NULL)
5166 line = skipwhite(ea.cmd + 1);
5167 continue;
5168 }
5169
5170 // "{" starts a block scope
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01005171 // "{'a': 1}->func() is something else
5172 if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 {
5174 line = compile_block(ea.cmd, &cctx);
5175 continue;
5176 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005177 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178
5179 /*
5180 * COMMAND MODIFIERS
5181 */
5182 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5183 {
5184 if (errormsg != NULL)
5185 goto erret;
5186 // empty line or comment
5187 line = (char_u *)"";
5188 continue;
5189 }
5190
5191 // Skip ":call" to get to the function name.
5192 if (checkforcmd(&ea.cmd, "call", 3))
5193 ea.cmd = skipwhite(ea.cmd);
5194
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005195 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005197 // Assuming the command starts with a variable or function name,
5198 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5199 // val".
5200 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5201 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005202 p = to_name_end(p, TRUE);
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01005203 if ((p > ea.cmd && *p != NUL) || *p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005205 int oplen;
5206 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005208 oplen = assignment_len(skipwhite(p), &heredoc);
5209 if (oplen > 0)
5210 {
5211 // Recognize an assignment if we recognize the variable
5212 // name:
5213 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005214 // "local = expr" where "local" is a local var.
5215 // "script = expr" where "script" is a script-local var.
5216 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005217 // "&opt = expr"
5218 // "$ENV = expr"
5219 // "@r = expr"
5220 if (*ea.cmd == '&'
5221 || *ea.cmd == '$'
5222 || *ea.cmd == '@'
5223 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5224 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5225 || lookup_script(ea.cmd, p - ea.cmd) == OK
5226 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5227 {
5228 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5229 if (line == NULL)
5230 goto erret;
5231 continue;
5232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 }
5234 }
5235 }
5236
5237 /*
5238 * COMMAND after range
5239 */
5240 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005241 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5242 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243
5244 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5245 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005246 if (cctx.ctx_skip == TRUE)
5247 {
5248 line += STRLEN(line);
5249 continue;
5250 }
5251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252 // Expression or function call.
5253 if (ea.cmdidx == CMD_eval)
5254 {
5255 p = ea.cmd;
5256 if (compile_expr1(&p, &cctx) == FAIL)
5257 goto erret;
5258
5259 // drop the return value
5260 generate_instr_drop(&cctx, ISN_DROP, 1);
5261 line = p;
5262 continue;
5263 }
5264 if (ea.cmdidx == CMD_let)
5265 {
5266 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5267 if (line == NULL)
5268 goto erret;
5269 continue;
5270 }
5271 iemsg("Command from find_ex_command() not handled");
5272 goto erret;
5273 }
5274
5275 p = skipwhite(p);
5276
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005277 if (cctx.ctx_skip == TRUE
5278 && ea.cmdidx != CMD_elseif
5279 && ea.cmdidx != CMD_else
5280 && ea.cmdidx != CMD_endif)
5281 {
5282 line += STRLEN(line);
5283 continue;
5284 }
5285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286 switch (ea.cmdidx)
5287 {
5288 case CMD_def:
5289 case CMD_function:
5290 // TODO: Nested function
5291 emsg("Nested function not implemented yet");
5292 goto erret;
5293
5294 case CMD_return:
5295 line = compile_return(p, set_return_type, &cctx);
5296 had_return = TRUE;
5297 break;
5298
5299 case CMD_let:
5300 case CMD_const:
5301 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5302 break;
5303
5304 case CMD_import:
5305 line = compile_import(p, &cctx);
5306 break;
5307
5308 case CMD_if:
5309 line = compile_if(p, &cctx);
5310 break;
5311 case CMD_elseif:
5312 line = compile_elseif(p, &cctx);
5313 break;
5314 case CMD_else:
5315 line = compile_else(p, &cctx);
5316 break;
5317 case CMD_endif:
5318 line = compile_endif(p, &cctx);
5319 break;
5320
5321 case CMD_while:
5322 line = compile_while(p, &cctx);
5323 break;
5324 case CMD_endwhile:
5325 line = compile_endwhile(p, &cctx);
5326 break;
5327
5328 case CMD_for:
5329 line = compile_for(p, &cctx);
5330 break;
5331 case CMD_endfor:
5332 line = compile_endfor(p, &cctx);
5333 break;
5334 case CMD_continue:
5335 line = compile_continue(p, &cctx);
5336 break;
5337 case CMD_break:
5338 line = compile_break(p, &cctx);
5339 break;
5340
5341 case CMD_try:
5342 line = compile_try(p, &cctx);
5343 break;
5344 case CMD_catch:
5345 line = compile_catch(p, &cctx);
5346 break;
5347 case CMD_finally:
5348 line = compile_finally(p, &cctx);
5349 break;
5350 case CMD_endtry:
5351 line = compile_endtry(p, &cctx);
5352 break;
5353 case CMD_throw:
5354 line = compile_throw(p, &cctx);
5355 break;
5356
5357 case CMD_echo:
5358 line = compile_echo(p, TRUE, &cctx);
5359 break;
5360 case CMD_echon:
5361 line = compile_echo(p, FALSE, &cctx);
5362 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005363 case CMD_execute:
5364 line = compile_execute(p, &cctx);
5365 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005366
5367 default:
5368 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005369 // TODO:
5370 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005371 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005372 generate_EXEC(&cctx, line);
5373 line = (char_u *)"";
5374 break;
5375 }
5376 if (line == NULL)
5377 goto erret;
5378
5379 if (cctx.ctx_type_stack.ga_len < 0)
5380 {
5381 iemsg("Type stack underflow");
5382 goto erret;
5383 }
5384 }
5385
5386 if (cctx.ctx_scope != NULL)
5387 {
5388 if (cctx.ctx_scope->se_type == IF_SCOPE)
5389 emsg(_(e_endif));
5390 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5391 emsg(_(e_endwhile));
5392 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5393 emsg(_(e_endfor));
5394 else
5395 emsg(_("E1026: Missing }"));
5396 goto erret;
5397 }
5398
5399 if (!had_return)
5400 {
5401 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5402 {
5403 emsg(_("E1027: Missing return statement"));
5404 goto erret;
5405 }
5406
5407 // Return zero if there is no return at the end.
5408 generate_PUSHNR(&cctx, 0);
5409 generate_instr(&cctx, ISN_RETURN);
5410 }
5411
Bram Moolenaar20431c92020-03-20 18:39:46 +01005412 dfunc->df_deleted = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005413 dfunc->df_instr = instr->ga_data;
5414 dfunc->df_instr_count = instr->ga_len;
5415 dfunc->df_varcount = cctx.ctx_max_local;
5416
5417 ret = OK;
5418
5419erret:
5420 if (ret == FAIL)
5421 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005422 int idx;
5423
5424 for (idx = 0; idx < instr->ga_len; ++idx)
5425 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005429 if (!dfunc->df_deleted)
5430 --def_functions.ga_len;
5431
5432 // Don't execute this function body.
5433 ga_clear_strings(&ufunc->uf_lines);
5434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005435 if (errormsg != NULL)
5436 emsg(errormsg);
5437 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005438 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439 }
5440
5441 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005442 free_imported(&cctx);
5443 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005444 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005445}
5446
5447/*
5448 * Delete an instruction, free what it contains.
5449 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01005450 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005451delete_instr(isn_T *isn)
5452{
5453 switch (isn->isn_type)
5454 {
5455 case ISN_EXEC:
5456 case ISN_LOADENV:
5457 case ISN_LOADG:
5458 case ISN_LOADOPT:
5459 case ISN_MEMBER:
5460 case ISN_PUSHEXC:
5461 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005462 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005464 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005465 vim_free(isn->isn_arg.string);
5466 break;
5467
5468 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005469 case ISN_STORES:
5470 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005471 break;
5472
5473 case ISN_STOREOPT:
5474 vim_free(isn->isn_arg.storeopt.so_name);
5475 break;
5476
5477 case ISN_PUSHBLOB: // push blob isn_arg.blob
5478 blob_unref(isn->isn_arg.blob);
5479 break;
5480
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005481 case ISN_PUSHPARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01005482 partial_unref(isn->isn_arg.partial);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005483 break;
5484
5485 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005486#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005487 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005488#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005489 break;
5490
5491 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005492#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005493 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005494#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005495 break;
5496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 case ISN_UCALL:
5498 vim_free(isn->isn_arg.ufunc.cuf_name);
5499 break;
5500
5501 case ISN_2BOOL:
5502 case ISN_2STRING:
5503 case ISN_ADDBLOB:
5504 case ISN_ADDLIST:
5505 case ISN_BCALL:
5506 case ISN_CATCH:
5507 case ISN_CHECKNR:
5508 case ISN_CHECKTYPE:
5509 case ISN_COMPAREANY:
5510 case ISN_COMPAREBLOB:
5511 case ISN_COMPAREBOOL:
5512 case ISN_COMPAREDICT:
5513 case ISN_COMPAREFLOAT:
5514 case ISN_COMPAREFUNC:
5515 case ISN_COMPARELIST:
5516 case ISN_COMPARENR:
5517 case ISN_COMPAREPARTIAL:
5518 case ISN_COMPARESPECIAL:
5519 case ISN_COMPARESTRING:
5520 case ISN_CONCAT:
5521 case ISN_DCALL:
5522 case ISN_DROP:
5523 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01005524 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525 case ISN_ENDTRY:
5526 case ISN_FOR:
5527 case ISN_FUNCREF:
5528 case ISN_INDEX:
5529 case ISN_JUMP:
5530 case ISN_LOAD:
5531 case ISN_LOADSCRIPT:
5532 case ISN_LOADREG:
5533 case ISN_LOADV:
5534 case ISN_NEGATENR:
5535 case ISN_NEWDICT:
5536 case ISN_NEWLIST:
5537 case ISN_OPNR:
5538 case ISN_OPFLOAT:
5539 case ISN_OPANY:
5540 case ISN_PCALL:
5541 case ISN_PUSHF:
5542 case ISN_PUSHNR:
5543 case ISN_PUSHBOOL:
5544 case ISN_PUSHSPEC:
5545 case ISN_RETURN:
5546 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005547 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005549 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550 case ISN_STORESCRIPT:
5551 case ISN_THROW:
5552 case ISN_TRY:
5553 // nothing allocated
5554 break;
5555 }
5556}
5557
5558/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01005559 * Free all instructions for "dfunc".
5560 */
5561 static void
5562delete_def_function_contents(dfunc_T *dfunc)
5563{
5564 int idx;
5565
5566 ga_clear(&dfunc->df_def_args_isn);
5567
5568 if (dfunc->df_instr != NULL)
5569 {
5570 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5571 delete_instr(dfunc->df_instr + idx);
5572 VIM_CLEAR(dfunc->df_instr);
5573 }
5574
5575 dfunc->df_deleted = TRUE;
5576}
5577
5578/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579 * When a user function is deleted, delete any associated def function.
5580 */
5581 void
5582delete_def_function(ufunc_T *ufunc)
5583{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 if (ufunc->uf_dfunc_idx >= 0)
5585 {
5586 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5587 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588
Bram Moolenaar20431c92020-03-20 18:39:46 +01005589 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590 }
5591}
5592
5593#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005594/*
5595 * Free all functions defined with ":def".
5596 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005597 void
5598free_def_functions(void)
5599{
Bram Moolenaar20431c92020-03-20 18:39:46 +01005600 int idx;
5601
5602 for (idx = 0; idx < def_functions.ga_len; ++idx)
5603 {
5604 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
5605
5606 delete_def_function_contents(dfunc);
5607 }
5608
5609 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005610}
5611#endif
5612
5613
5614#endif // FEAT_EVAL