blob: 5d8dc90025ee07ea55e1735cf1dd71cd2347c06c [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);
132
133/*
134 * Lookup variable "name" in the local scope and return the index.
135 */
136 static int
137lookup_local(char_u *name, size_t len, cctx_T *cctx)
138{
139 int idx;
140
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100141 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 return -1;
143 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
144 {
145 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
146
147 if (STRNCMP(name, lvar->lv_name, len) == 0
148 && STRLEN(lvar->lv_name) == len)
149 return idx;
150 }
151 return -1;
152}
153
154/*
155 * Lookup an argument in the current function.
156 * Returns the argument index or -1 if not found.
157 */
158 static int
159lookup_arg(char_u *name, size_t len, cctx_T *cctx)
160{
161 int idx;
162
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100163 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 return -1;
165 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
166 {
167 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
168
169 if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
170 return idx;
171 }
172 return -1;
173}
174
175/*
176 * Lookup a vararg argument in the current function.
177 * Returns TRUE if there is a match.
178 */
179 static int
180lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
181{
182 char_u *va_name = cctx->ctx_ufunc->uf_va_name;
183
184 return len > 0 && va_name != NULL
185 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
186}
187
188/*
189 * Lookup a variable in the current script.
190 * Returns OK or FAIL.
191 */
192 static int
193lookup_script(char_u *name, size_t len)
194{
195 int cc;
196 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
197 dictitem_T *di;
198
199 cc = name[len];
200 name[len] = NUL;
201 di = find_var_in_ht(ht, 0, name, TRUE);
202 name[len] = cc;
203 return di == NULL ? FAIL: OK;
204}
205
206 static type_T *
207get_list_type(type_T *member_type, garray_T *type_list)
208{
209 type_T *type;
210
211 // recognize commonly used types
212 if (member_type->tt_type == VAR_UNKNOWN)
213 return &t_list_any;
Bram Moolenaar436472f2020-02-20 22:54:43 +0100214 if (member_type->tt_type == VAR_VOID)
215 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100216 if (member_type->tt_type == VAR_BOOL)
217 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218 if (member_type->tt_type == VAR_NUMBER)
219 return &t_list_number;
220 if (member_type->tt_type == VAR_STRING)
221 return &t_list_string;
222
223 // Not a common type, create a new entry.
224 if (ga_grow(type_list, 1) == FAIL)
225 return FAIL;
226 type = ((type_T *)type_list->ga_data) + type_list->ga_len;
227 ++type_list->ga_len;
228 type->tt_type = VAR_LIST;
229 type->tt_member = member_type;
230 return type;
231}
232
233 static type_T *
234get_dict_type(type_T *member_type, garray_T *type_list)
235{
236 type_T *type;
237
238 // recognize commonly used types
239 if (member_type->tt_type == VAR_UNKNOWN)
240 return &t_dict_any;
Bram Moolenaar436472f2020-02-20 22:54:43 +0100241 if (member_type->tt_type == VAR_VOID)
242 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100243 if (member_type->tt_type == VAR_BOOL)
244 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245 if (member_type->tt_type == VAR_NUMBER)
246 return &t_dict_number;
247 if (member_type->tt_type == VAR_STRING)
248 return &t_dict_string;
249
250 // Not a common type, create a new entry.
251 if (ga_grow(type_list, 1) == FAIL)
252 return FAIL;
253 type = ((type_T *)type_list->ga_data) + type_list->ga_len;
254 ++type_list->ga_len;
255 type->tt_type = VAR_DICT;
256 type->tt_member = member_type;
257 return type;
258}
259
260/////////////////////////////////////////////////////////////////////
261// Following generate_ functions expect the caller to call ga_grow().
262
263/*
264 * Generate an instruction without arguments.
265 * Returns a pointer to the new instruction, NULL if failed.
266 */
267 static isn_T *
268generate_instr(cctx_T *cctx, isntype_T isn_type)
269{
270 garray_T *instr = &cctx->ctx_instr;
271 isn_T *isn;
272
273 if (ga_grow(instr, 1) == FAIL)
274 return NULL;
275 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
276 isn->isn_type = isn_type;
277 isn->isn_lnum = cctx->ctx_lnum + 1;
278 ++instr->ga_len;
279
280 return isn;
281}
282
283/*
284 * Generate an instruction without arguments.
285 * "drop" will be removed from the stack.
286 * Returns a pointer to the new instruction, NULL if failed.
287 */
288 static isn_T *
289generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
290{
291 garray_T *stack = &cctx->ctx_type_stack;
292
293 stack->ga_len -= drop;
294 return generate_instr(cctx, isn_type);
295}
296
297/*
298 * Generate instruction "isn_type" and put "type" on the type stack.
299 */
300 static isn_T *
301generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
302{
303 isn_T *isn;
304 garray_T *stack = &cctx->ctx_type_stack;
305
306 if ((isn = generate_instr(cctx, isn_type)) == NULL)
307 return NULL;
308
309 if (ga_grow(stack, 1) == FAIL)
310 return NULL;
311 ((type_T **)stack->ga_data)[stack->ga_len] = type;
312 ++stack->ga_len;
313
314 return isn;
315}
316
317/*
318 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
319 */
320 static int
321may_generate_2STRING(int offset, cctx_T *cctx)
322{
323 isn_T *isn;
324 garray_T *stack = &cctx->ctx_type_stack;
325 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
326
327 if ((*type)->tt_type == VAR_STRING)
328 return OK;
329 *type = &t_string;
330
331 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
332 return FAIL;
333 isn->isn_arg.number = offset;
334
335 return OK;
336}
337
338 static int
339check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
340{
341 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_UNKNOWN)
342 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
343 || type2 == VAR_UNKNOWN)))
344 {
345 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100346 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100347 else
348 semsg(_("E1036: %c requires number or float arguments"), *op);
349 return FAIL;
350 }
351 return OK;
352}
353
354/*
355 * Generate an instruction with two arguments. The instruction depends on the
356 * type of the arguments.
357 */
358 static int
359generate_two_op(cctx_T *cctx, char_u *op)
360{
361 garray_T *stack = &cctx->ctx_type_stack;
362 type_T *type1;
363 type_T *type2;
364 vartype_T vartype;
365 isn_T *isn;
366
367 // Get the known type of the two items on the stack. If they are matching
368 // use a type-specific instruction. Otherwise fall back to runtime type
369 // checking.
370 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
371 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
372 vartype = VAR_UNKNOWN;
373 if (type1->tt_type == type2->tt_type
374 && (type1->tt_type == VAR_NUMBER
375 || type1->tt_type == VAR_LIST
376#ifdef FEAT_FLOAT
377 || type1->tt_type == VAR_FLOAT
378#endif
379 || type1->tt_type == VAR_BLOB))
380 vartype = type1->tt_type;
381
382 switch (*op)
383 {
384 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100385 && type1->tt_type != VAR_UNKNOWN
386 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387 && check_number_or_float(
388 type1->tt_type, type2->tt_type, op) == FAIL)
389 return FAIL;
390 isn = generate_instr_drop(cctx,
391 vartype == VAR_NUMBER ? ISN_OPNR
392 : vartype == VAR_LIST ? ISN_ADDLIST
393 : vartype == VAR_BLOB ? ISN_ADDBLOB
394#ifdef FEAT_FLOAT
395 : vartype == VAR_FLOAT ? ISN_OPFLOAT
396#endif
397 : ISN_OPANY, 1);
398 if (isn != NULL)
399 isn->isn_arg.op.op_type = EXPR_ADD;
400 break;
401
402 case '-':
403 case '*':
404 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
405 op) == FAIL)
406 return FAIL;
407 if (vartype == VAR_NUMBER)
408 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
409#ifdef FEAT_FLOAT
410 else if (vartype == VAR_FLOAT)
411 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
412#endif
413 else
414 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
415 if (isn != NULL)
416 isn->isn_arg.op.op_type = *op == '*'
417 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
418 break;
419
420 case '%': if ((type1->tt_type != VAR_UNKNOWN
421 && type1->tt_type != VAR_NUMBER)
422 || (type2->tt_type != VAR_UNKNOWN
423 && type2->tt_type != VAR_NUMBER))
424 {
425 emsg(_("E1035: % requires number arguments"));
426 return FAIL;
427 }
428 isn = generate_instr_drop(cctx,
429 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
430 if (isn != NULL)
431 isn->isn_arg.op.op_type = EXPR_REM;
432 break;
433 }
434
435 // correct type of result
436 if (vartype == VAR_UNKNOWN)
437 {
438 type_T *type = &t_any;
439
440#ifdef FEAT_FLOAT
441 // float+number and number+float results in float
442 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
443 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
444 type = &t_float;
445#endif
446 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
447 }
448
449 return OK;
450}
451
452/*
453 * Generate an ISN_COMPARE* instruction with a boolean result.
454 */
455 static int
456generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
457{
458 isntype_T isntype = ISN_DROP;
459 isn_T *isn;
460 garray_T *stack = &cctx->ctx_type_stack;
461 vartype_T type1;
462 vartype_T type2;
463
464 // Get the known type of the two items on the stack. If they are matching
465 // use a type-specific instruction. Otherwise fall back to runtime type
466 // checking.
467 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
468 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
469 if (type1 == type2)
470 {
471 switch (type1)
472 {
473 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
474 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
475 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
476 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
477 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
478 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
479 case VAR_LIST: isntype = ISN_COMPARELIST; break;
480 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
481 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
482 case VAR_PARTIAL: isntype = ISN_COMPAREPARTIAL; break;
483 default: isntype = ISN_COMPAREANY; break;
484 }
485 }
486 else if (type1 == VAR_UNKNOWN || type2 == VAR_UNKNOWN
487 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
488 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
489 isntype = ISN_COMPAREANY;
490
491 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
492 && (isntype == ISN_COMPAREBOOL
493 || isntype == ISN_COMPARESPECIAL
494 || isntype == ISN_COMPARENR
495 || isntype == ISN_COMPAREFLOAT))
496 {
497 semsg(_("E1037: Cannot use \"%s\" with %s"),
498 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
499 return FAIL;
500 }
501 if (isntype == ISN_DROP
502 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
503 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
504 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
505 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
506 && exptype != EXPR_IS && exptype != EXPR_ISNOT
507 && (type1 == VAR_BLOB || type2 == VAR_BLOB
508 || type1 == VAR_LIST || type2 == VAR_LIST))))
509 {
510 semsg(_("E1037: Cannot compare %s with %s"),
511 vartype_name(type1), vartype_name(type2));
512 return FAIL;
513 }
514
515 if ((isn = generate_instr(cctx, isntype)) == NULL)
516 return FAIL;
517 isn->isn_arg.op.op_type = exptype;
518 isn->isn_arg.op.op_ic = ic;
519
520 // takes two arguments, puts one bool back
521 if (stack->ga_len >= 2)
522 {
523 --stack->ga_len;
524 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
525 }
526
527 return OK;
528}
529
530/*
531 * Generate an ISN_2BOOL instruction.
532 */
533 static int
534generate_2BOOL(cctx_T *cctx, int invert)
535{
536 isn_T *isn;
537 garray_T *stack = &cctx->ctx_type_stack;
538
539 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
540 return FAIL;
541 isn->isn_arg.number = invert;
542
543 // type becomes bool
544 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
545
546 return OK;
547}
548
549 static int
550generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
551{
552 isn_T *isn;
553 garray_T *stack = &cctx->ctx_type_stack;
554
555 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
556 return FAIL;
557 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
558 isn->isn_arg.type.ct_off = offset;
559
560 // type becomes vartype
561 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
562
563 return OK;
564}
565
566/*
567 * Generate an ISN_PUSHNR instruction.
568 */
569 static int
570generate_PUSHNR(cctx_T *cctx, varnumber_T number)
571{
572 isn_T *isn;
573
574 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
575 return FAIL;
576 isn->isn_arg.number = number;
577
578 return OK;
579}
580
581/*
582 * Generate an ISN_PUSHBOOL instruction.
583 */
584 static int
585generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
586{
587 isn_T *isn;
588
589 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
590 return FAIL;
591 isn->isn_arg.number = number;
592
593 return OK;
594}
595
596/*
597 * Generate an ISN_PUSHSPEC instruction.
598 */
599 static int
600generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
601{
602 isn_T *isn;
603
604 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
605 return FAIL;
606 isn->isn_arg.number = number;
607
608 return OK;
609}
610
611#ifdef FEAT_FLOAT
612/*
613 * Generate an ISN_PUSHF instruction.
614 */
615 static int
616generate_PUSHF(cctx_T *cctx, float_T fnumber)
617{
618 isn_T *isn;
619
620 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
621 return FAIL;
622 isn->isn_arg.fnumber = fnumber;
623
624 return OK;
625}
626#endif
627
628/*
629 * Generate an ISN_PUSHS instruction.
630 * Consumes "str".
631 */
632 static int
633generate_PUSHS(cctx_T *cctx, char_u *str)
634{
635 isn_T *isn;
636
637 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
638 return FAIL;
639 isn->isn_arg.string = str;
640
641 return OK;
642}
643
644/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100645 * Generate an ISN_PUSHCHANNEL instruction.
646 * Consumes "channel".
647 */
648 static int
649generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
650{
651 isn_T *isn;
652
653 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
654 return FAIL;
655 isn->isn_arg.channel = channel;
656
657 return OK;
658}
659
660/*
661 * Generate an ISN_PUSHJOB instruction.
662 * Consumes "job".
663 */
664 static int
665generate_PUSHJOB(cctx_T *cctx, job_T *job)
666{
667 isn_T *isn;
668
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100669 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100670 return FAIL;
671 isn->isn_arg.job = job;
672
673 return OK;
674}
675
676/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 * Generate an ISN_PUSHBLOB instruction.
678 * Consumes "blob".
679 */
680 static int
681generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
682{
683 isn_T *isn;
684
685 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
686 return FAIL;
687 isn->isn_arg.blob = blob;
688
689 return OK;
690}
691
692/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100693 * Generate an ISN_PUSHFUNC instruction with name "name".
694 * Consumes "name".
695 */
696 static int
697generate_PUSHFUNC(cctx_T *cctx, char_u *name)
698{
699 isn_T *isn;
700
701 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, &t_func_void)) == NULL)
702 return FAIL;
703 isn->isn_arg.string = name;
704
705 return OK;
706}
707
708/*
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100709 * Generate an ISN_PUSHPARTIAL instruction with partial "part".
710 * Consumes "name".
711 */
712 static int
713generate_PUSHPARTIAL(cctx_T *cctx, partial_T *part)
714{
715 isn_T *isn;
716
717 if ((isn = generate_instr_type(cctx, ISN_PUSHPARTIAL,
718 &t_partial_any)) == NULL)
719 return FAIL;
720 isn->isn_arg.partial = part;
721
722 return OK;
723}
724
725/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726 * Generate an ISN_STORE instruction.
727 */
728 static int
729generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
730{
731 isn_T *isn;
732
733 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
734 return FAIL;
735 if (name != NULL)
736 isn->isn_arg.string = vim_strsave(name);
737 else
738 isn->isn_arg.number = idx;
739
740 return OK;
741}
742
743/*
744 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
745 */
746 static int
747generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
748{
749 isn_T *isn;
750
751 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
752 return FAIL;
753 isn->isn_arg.storenr.str_idx = idx;
754 isn->isn_arg.storenr.str_val = value;
755
756 return OK;
757}
758
759/*
760 * Generate an ISN_STOREOPT instruction
761 */
762 static int
763generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
764{
765 isn_T *isn;
766
767 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
768 return FAIL;
769 isn->isn_arg.storeopt.so_name = vim_strsave(name);
770 isn->isn_arg.storeopt.so_flags = opt_flags;
771
772 return OK;
773}
774
775/*
776 * Generate an ISN_LOAD or similar instruction.
777 */
778 static int
779generate_LOAD(
780 cctx_T *cctx,
781 isntype_T isn_type,
782 int idx,
783 char_u *name,
784 type_T *type)
785{
786 isn_T *isn;
787
788 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
789 return FAIL;
790 if (name != NULL)
791 isn->isn_arg.string = vim_strsave(name);
792 else
793 isn->isn_arg.number = idx;
794
795 return OK;
796}
797
798/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100799 * Generate an ISN_LOADV instruction.
800 */
801 static int
802generate_LOADV(
803 cctx_T *cctx,
804 char_u *name,
805 int error)
806{
807 // load v:var
808 int vidx = find_vim_var(name);
809
810 if (vidx < 0)
811 {
812 if (error)
813 semsg(_(e_var_notfound), name);
814 return FAIL;
815 }
816
817 // TODO: get actual type
818 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
819}
820
821/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100822 * Generate an ISN_LOADS instruction.
823 */
824 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100825generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100827 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100829 int sid,
830 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831{
832 isn_T *isn;
833
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100834 if (isn_type == ISN_LOADS)
835 isn = generate_instr_type(cctx, isn_type, type);
836 else
837 isn = generate_instr_drop(cctx, isn_type, 1);
838 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100840 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
841 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842
843 return OK;
844}
845
846/*
847 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
848 */
849 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100850generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 cctx_T *cctx,
852 isntype_T isn_type,
853 int sid,
854 int idx,
855 type_T *type)
856{
857 isn_T *isn;
858
859 if (isn_type == ISN_LOADSCRIPT)
860 isn = generate_instr_type(cctx, isn_type, type);
861 else
862 isn = generate_instr_drop(cctx, isn_type, 1);
863 if (isn == NULL)
864 return FAIL;
865 isn->isn_arg.script.script_sid = sid;
866 isn->isn_arg.script.script_idx = idx;
867 return OK;
868}
869
870/*
871 * Generate an ISN_NEWLIST instruction.
872 */
873 static int
874generate_NEWLIST(cctx_T *cctx, int count)
875{
876 isn_T *isn;
877 garray_T *stack = &cctx->ctx_type_stack;
878 garray_T *type_list = cctx->ctx_type_list;
879 type_T *type;
880 type_T *member;
881
882 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
883 return FAIL;
884 isn->isn_arg.number = count;
885
886 // drop the value types
887 stack->ga_len -= count;
888
Bram Moolenaar436472f2020-02-20 22:54:43 +0100889 // Use the first value type for the list member type. Use "void" for an
890 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891 if (count > 0)
892 member = ((type_T **)stack->ga_data)[stack->ga_len];
893 else
Bram Moolenaar436472f2020-02-20 22:54:43 +0100894 member = &t_void;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 type = get_list_type(member, type_list);
896
897 // add the list type to the type stack
898 if (ga_grow(stack, 1) == FAIL)
899 return FAIL;
900 ((type_T **)stack->ga_data)[stack->ga_len] = type;
901 ++stack->ga_len;
902
903 return OK;
904}
905
906/*
907 * Generate an ISN_NEWDICT instruction.
908 */
909 static int
910generate_NEWDICT(cctx_T *cctx, int count)
911{
912 isn_T *isn;
913 garray_T *stack = &cctx->ctx_type_stack;
914 garray_T *type_list = cctx->ctx_type_list;
915 type_T *type;
916 type_T *member;
917
918 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
919 return FAIL;
920 isn->isn_arg.number = count;
921
922 // drop the key and value types
923 stack->ga_len -= 2 * count;
924
Bram Moolenaar436472f2020-02-20 22:54:43 +0100925 // Use the first value type for the list member type. Use "void" for an
926 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 if (count > 0)
928 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
929 else
Bram Moolenaar436472f2020-02-20 22:54:43 +0100930 member = &t_void;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 type = get_dict_type(member, type_list);
932
933 // add the dict type to the type stack
934 if (ga_grow(stack, 1) == FAIL)
935 return FAIL;
936 ((type_T **)stack->ga_data)[stack->ga_len] = type;
937 ++stack->ga_len;
938
939 return OK;
940}
941
942/*
943 * Generate an ISN_FUNCREF instruction.
944 */
945 static int
946generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
947{
948 isn_T *isn;
949 garray_T *stack = &cctx->ctx_type_stack;
950
951 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
952 return FAIL;
953 isn->isn_arg.number = dfunc_idx;
954
955 if (ga_grow(stack, 1) == FAIL)
956 return FAIL;
957 ((type_T **)stack->ga_data)[stack->ga_len] = &t_partial_any;
958 // TODO: argument and return types
959 ++stack->ga_len;
960
961 return OK;
962}
963
964/*
965 * Generate an ISN_JUMP instruction.
966 */
967 static int
968generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
969{
970 isn_T *isn;
971 garray_T *stack = &cctx->ctx_type_stack;
972
973 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
974 return FAIL;
975 isn->isn_arg.jump.jump_when = when;
976 isn->isn_arg.jump.jump_where = where;
977
978 if (when != JUMP_ALWAYS && stack->ga_len > 0)
979 --stack->ga_len;
980
981 return OK;
982}
983
984 static int
985generate_FOR(cctx_T *cctx, int loop_idx)
986{
987 isn_T *isn;
988 garray_T *stack = &cctx->ctx_type_stack;
989
990 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
991 return FAIL;
992 isn->isn_arg.forloop.for_idx = loop_idx;
993
994 if (ga_grow(stack, 1) == FAIL)
995 return FAIL;
996 // type doesn't matter, will be stored next
997 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
998 ++stack->ga_len;
999
1000 return OK;
1001}
1002
1003/*
1004 * Generate an ISN_BCALL instruction.
1005 * Return FAIL if the number of arguments is wrong.
1006 */
1007 static int
1008generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1009{
1010 isn_T *isn;
1011 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001012 type_T *argtypes[MAX_FUNC_ARGS];
1013 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014
1015 if (check_internal_func(func_idx, argcount) == FAIL)
1016 return FAIL;
1017
1018 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1019 return FAIL;
1020 isn->isn_arg.bfunc.cbf_idx = func_idx;
1021 isn->isn_arg.bfunc.cbf_argcount = argcount;
1022
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001023 for (i = 0; i < argcount; ++i)
1024 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 stack->ga_len -= argcount; // drop the arguments
1027 if (ga_grow(stack, 1) == FAIL)
1028 return FAIL;
1029 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001030 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 ++stack->ga_len; // add return value
1032
1033 return OK;
1034}
1035
1036/*
1037 * Generate an ISN_DCALL or ISN_UCALL instruction.
1038 * Return FAIL if the number of arguments is wrong.
1039 */
1040 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001041generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042{
1043 isn_T *isn;
1044 garray_T *stack = &cctx->ctx_type_stack;
1045 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001046 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047
1048 if (argcount > regular_args && !has_varargs(ufunc))
1049 {
1050 semsg(_(e_toomanyarg), ufunc->uf_name);
1051 return FAIL;
1052 }
1053 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1054 {
1055 semsg(_(e_toofewarg), ufunc->uf_name);
1056 return FAIL;
1057 }
1058
1059 // Turn varargs into a list.
1060 if (ufunc->uf_va_name != NULL)
1061 {
1062 int count = argcount - regular_args;
1063
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001064 // If count is negative an empty list will be added after evaluating
1065 // default values for missing optional arguments.
1066 if (count >= 0)
1067 {
1068 generate_NEWLIST(cctx, count);
1069 argcount = regular_args + 1;
1070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071 }
1072
1073 if ((isn = generate_instr(cctx,
1074 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1075 return FAIL;
1076 if (ufunc->uf_dfunc_idx >= 0)
1077 {
1078 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1079 isn->isn_arg.dfunc.cdf_argcount = argcount;
1080 }
1081 else
1082 {
1083 // A user function may be deleted and redefined later, can't use the
1084 // ufunc pointer, need to look it up again at runtime.
1085 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1086 isn->isn_arg.ufunc.cuf_argcount = argcount;
1087 }
1088
1089 stack->ga_len -= argcount; // drop the arguments
1090 if (ga_grow(stack, 1) == FAIL)
1091 return FAIL;
1092 // add return value
1093 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1094 ++stack->ga_len;
1095
1096 return OK;
1097}
1098
1099/*
1100 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1101 */
1102 static int
1103generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1104{
1105 isn_T *isn;
1106 garray_T *stack = &cctx->ctx_type_stack;
1107
1108 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1109 return FAIL;
1110 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1111 isn->isn_arg.ufunc.cuf_argcount = argcount;
1112
1113 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001114 if (ga_grow(stack, 1) == FAIL)
1115 return FAIL;
1116 // add return value
1117 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1118 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119
1120 return OK;
1121}
1122
1123/*
1124 * Generate an ISN_PCALL instruction.
1125 */
1126 static int
1127generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1128{
1129 isn_T *isn;
1130 garray_T *stack = &cctx->ctx_type_stack;
1131
1132 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1133 return FAIL;
1134 isn->isn_arg.pfunc.cpf_top = at_top;
1135 isn->isn_arg.pfunc.cpf_argcount = argcount;
1136
1137 stack->ga_len -= argcount; // drop the arguments
1138
1139 // drop the funcref/partial, get back the return value
1140 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1141
1142 return OK;
1143}
1144
1145/*
1146 * Generate an ISN_MEMBER instruction.
1147 */
1148 static int
1149generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1150{
1151 isn_T *isn;
1152 garray_T *stack = &cctx->ctx_type_stack;
1153 type_T *type;
1154
1155 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1156 return FAIL;
1157 isn->isn_arg.string = vim_strnsave(name, (int)len);
1158
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001159 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001161 if (type->tt_type != VAR_DICT && type != &t_any)
1162 {
1163 emsg(_(e_dictreq));
1164 return FAIL;
1165 }
1166 // change dict type to dict member type
1167 if (type->tt_type == VAR_DICT)
1168 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169
1170 return OK;
1171}
1172
1173/*
1174 * Generate an ISN_ECHO instruction.
1175 */
1176 static int
1177generate_ECHO(cctx_T *cctx, int with_white, int count)
1178{
1179 isn_T *isn;
1180
1181 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1182 return FAIL;
1183 isn->isn_arg.echo.echo_with_white = with_white;
1184 isn->isn_arg.echo.echo_count = count;
1185
1186 return OK;
1187}
1188
Bram Moolenaarad39c092020-02-26 18:23:43 +01001189/*
1190 * Generate an ISN_EXECUTE instruction.
1191 */
1192 static int
1193generate_EXECUTE(cctx_T *cctx, int count)
1194{
1195 isn_T *isn;
1196
1197 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1198 return FAIL;
1199 isn->isn_arg.number = count;
1200
1201 return OK;
1202}
1203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 static int
1205generate_EXEC(cctx_T *cctx, char_u *line)
1206{
1207 isn_T *isn;
1208
1209 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1210 return FAIL;
1211 isn->isn_arg.string = vim_strsave(line);
1212 return OK;
1213}
1214
1215static char e_white_both[] =
1216 N_("E1004: white space required before and after '%s'");
1217
1218/*
1219 * Reserve space for a local variable.
1220 * Return the index or -1 if it failed.
1221 */
1222 static int
1223reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1224{
1225 int idx;
1226 lvar_T *lvar;
1227
1228 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1229 {
1230 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1231 return -1;
1232 }
1233
1234 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1235 return -1;
1236 idx = cctx->ctx_locals.ga_len;
1237 if (cctx->ctx_max_local < idx + 1)
1238 cctx->ctx_max_local = idx + 1;
1239 ++cctx->ctx_locals.ga_len;
1240
1241 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1242 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1243 lvar->lv_const = isConst;
1244 lvar->lv_type = type;
1245
1246 return idx;
1247}
1248
1249/*
1250 * Skip over a type definition and return a pointer to just after it.
1251 */
1252 char_u *
1253skip_type(char_u *start)
1254{
1255 char_u *p = start;
1256
1257 while (ASCII_ISALNUM(*p) || *p == '_')
1258 ++p;
1259
1260 // Skip over "<type>"; this is permissive about white space.
1261 if (*skipwhite(p) == '<')
1262 {
1263 p = skipwhite(p);
1264 p = skip_type(skipwhite(p + 1));
1265 p = skipwhite(p);
1266 if (*p == '>')
1267 ++p;
1268 }
1269 return p;
1270}
1271
1272/*
1273 * Parse the member type: "<type>" and return "type" with the member set.
1274 * Use "type_list" if a new type needs to be added.
1275 * Returns NULL in case of failure.
1276 */
1277 static type_T *
1278parse_type_member(char_u **arg, type_T *type, garray_T *type_list)
1279{
1280 type_T *member_type;
1281
1282 if (**arg != '<')
1283 {
1284 if (*skipwhite(*arg) == '<')
1285 emsg(_("E1007: No white space allowed before <"));
1286 else
1287 emsg(_("E1008: Missing <type>"));
1288 return NULL;
1289 }
1290 *arg = skipwhite(*arg + 1);
1291
1292 member_type = parse_type(arg, type_list);
1293 if (member_type == NULL)
1294 return NULL;
1295
1296 *arg = skipwhite(*arg);
1297 if (**arg != '>')
1298 {
1299 emsg(_("E1009: Missing > after type"));
1300 return NULL;
1301 }
1302 ++*arg;
1303
1304 if (type->tt_type == VAR_LIST)
1305 return get_list_type(member_type, type_list);
1306 return get_dict_type(member_type, type_list);
1307}
1308
1309/*
1310 * Parse a type at "arg" and advance over it.
1311 * Return NULL for failure.
1312 */
1313 type_T *
1314parse_type(char_u **arg, garray_T *type_list)
1315{
1316 char_u *p = *arg;
1317 size_t len;
1318
1319 // skip over the first word
1320 while (ASCII_ISALNUM(*p) || *p == '_')
1321 ++p;
1322 len = p - *arg;
1323
1324 switch (**arg)
1325 {
1326 case 'a':
1327 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1328 {
1329 *arg += len;
1330 return &t_any;
1331 }
1332 break;
1333 case 'b':
1334 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1335 {
1336 *arg += len;
1337 return &t_bool;
1338 }
1339 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1340 {
1341 *arg += len;
1342 return &t_blob;
1343 }
1344 break;
1345 case 'c':
1346 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1347 {
1348 *arg += len;
1349 return &t_channel;
1350 }
1351 break;
1352 case 'd':
1353 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1354 {
1355 *arg += len;
1356 return parse_type_member(arg, &t_dict_any, type_list);
1357 }
1358 break;
1359 case 'f':
1360 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1361 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001362#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 *arg += len;
1364 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001365#else
1366 emsg(_("E1055: This Vim is not compiled with float support"));
1367 return &t_any;
1368#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 }
1370 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1371 {
1372 *arg += len;
1373 // TODO: arguments and return type
1374 return &t_func_any;
1375 }
1376 break;
1377 case 'j':
1378 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1379 {
1380 *arg += len;
1381 return &t_job;
1382 }
1383 break;
1384 case 'l':
1385 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1386 {
1387 *arg += len;
1388 return parse_type_member(arg, &t_list_any, type_list);
1389 }
1390 break;
1391 case 'n':
1392 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1393 {
1394 *arg += len;
1395 return &t_number;
1396 }
1397 break;
1398 case 'p':
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001399 if (len == 7 && STRNCMP(*arg, "partial", len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 {
1401 *arg += len;
1402 // TODO: arguments and return type
1403 return &t_partial_any;
1404 }
1405 break;
1406 case 's':
1407 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1408 {
1409 *arg += len;
1410 return &t_string;
1411 }
1412 break;
1413 case 'v':
1414 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1415 {
1416 *arg += len;
1417 return &t_void;
1418 }
1419 break;
1420 }
1421
1422 semsg(_("E1010: Type not recognized: %s"), *arg);
1423 return &t_any;
1424}
1425
1426/*
1427 * Check if "type1" and "type2" are exactly the same.
1428 */
1429 static int
1430equal_type(type_T *type1, type_T *type2)
1431{
1432 if (type1->tt_type != type2->tt_type)
1433 return FALSE;
1434 switch (type1->tt_type)
1435 {
1436 case VAR_VOID:
1437 case VAR_UNKNOWN:
1438 case VAR_SPECIAL:
1439 case VAR_BOOL:
1440 case VAR_NUMBER:
1441 case VAR_FLOAT:
1442 case VAR_STRING:
1443 case VAR_BLOB:
1444 case VAR_JOB:
1445 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001446 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 case VAR_LIST:
1448 case VAR_DICT:
1449 return equal_type(type1->tt_member, type2->tt_member);
1450 case VAR_FUNC:
1451 case VAR_PARTIAL:
1452 // TODO; check argument types.
1453 return equal_type(type1->tt_member, type2->tt_member)
1454 && type1->tt_argcount == type2->tt_argcount;
1455 }
1456 return TRUE;
1457}
1458
1459/*
1460 * Find the common type of "type1" and "type2" and put it in "dest".
1461 * "type2" and "dest" may be the same.
1462 */
1463 static void
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001464common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_list)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465{
1466 if (equal_type(type1, type2))
1467 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001468 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 return;
1470 }
1471
1472 if (type1->tt_type == type2->tt_type)
1473 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1475 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001476 type_T *common;
1477
1478 common_type(type1->tt_member, type2->tt_member, &common, type_list);
1479 if (type1->tt_type == VAR_LIST)
1480 *dest = get_list_type(common, type_list);
1481 else
1482 *dest = get_dict_type(common, type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 return;
1484 }
1485 // TODO: VAR_FUNC and VAR_PARTIAL
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001486 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 }
1488
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001489 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490}
1491
1492 char *
1493vartype_name(vartype_T type)
1494{
1495 switch (type)
1496 {
1497 case VAR_VOID: return "void";
1498 case VAR_UNKNOWN: return "any";
1499 case VAR_SPECIAL: return "special";
1500 case VAR_BOOL: return "bool";
1501 case VAR_NUMBER: return "number";
1502 case VAR_FLOAT: return "float";
1503 case VAR_STRING: return "string";
1504 case VAR_BLOB: return "blob";
1505 case VAR_JOB: return "job";
1506 case VAR_CHANNEL: return "channel";
1507 case VAR_LIST: return "list";
1508 case VAR_DICT: return "dict";
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001509 case VAR_FUNC: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 case VAR_PARTIAL: return "partial";
1511 }
1512 return "???";
1513}
1514
1515/*
1516 * Return the name of a type.
1517 * The result may be in allocated memory, in which case "tofree" is set.
1518 */
1519 char *
1520type_name(type_T *type, char **tofree)
1521{
1522 char *name = vartype_name(type->tt_type);
1523
1524 *tofree = NULL;
1525 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1526 {
1527 char *member_free;
1528 char *member_name = type_name(type->tt_member, &member_free);
1529 size_t len;
1530
1531 len = STRLEN(name) + STRLEN(member_name) + 3;
1532 *tofree = alloc(len);
1533 if (*tofree != NULL)
1534 {
1535 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1536 vim_free(member_free);
1537 return *tofree;
1538 }
1539 }
1540 // TODO: function and partial argument types
1541
1542 return name;
1543}
1544
1545/*
1546 * Find "name" in script-local items of script "sid".
1547 * Returns the index in "sn_var_vals" if found.
1548 * If found but not in "sn_var_vals" returns -1.
1549 * If not found returns -2.
1550 */
1551 int
1552get_script_item_idx(int sid, char_u *name, int check_writable)
1553{
1554 hashtab_T *ht;
1555 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001556 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 int idx;
1558
1559 // First look the name up in the hashtable.
1560 if (sid <= 0 || sid > script_items.ga_len)
1561 return -1;
1562 ht = &SCRIPT_VARS(sid);
1563 di = find_var_in_ht(ht, 0, name, TRUE);
1564 if (di == NULL)
1565 return -2;
1566
1567 // Now find the svar_T index in sn_var_vals.
1568 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1569 {
1570 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1571
1572 if (sv->sv_tv == &di->di_tv)
1573 {
1574 if (check_writable && sv->sv_const)
1575 semsg(_(e_readonlyvar), name);
1576 return idx;
1577 }
1578 }
1579 return -1;
1580}
1581
1582/*
1583 * Find "name" in imported items of the current script/
1584 */
1585 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001586find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001588 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 int idx;
1590
1591 if (cctx != NULL)
1592 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1593 {
1594 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1595 + idx;
1596
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001597 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1598 : STRLEN(import->imp_name) == len
1599 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 return import;
1601 }
1602
1603 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1604 {
1605 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1606
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001607 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1608 : STRLEN(import->imp_name) == len
1609 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 return import;
1611 }
1612 return NULL;
1613}
1614
1615/*
1616 * Generate an instruction to load script-local variable "name".
1617 */
1618 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001619compile_load_scriptvar(
1620 cctx_T *cctx,
1621 char_u *name, // variable NUL terminated
1622 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001623 char_u **end, // end of variable
1624 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001626 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1628 imported_T *import;
1629
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001630 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001632 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001633 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1634 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 }
1636 if (idx >= 0)
1637 {
1638 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1639
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001640 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 current_sctx.sc_sid, idx, sv->sv_type);
1642 return OK;
1643 }
1644
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001645 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 if (import != NULL)
1647 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001648 if (import->imp_all)
1649 {
1650 char_u *p = skipwhite(*end);
1651 int name_len;
1652 ufunc_T *ufunc;
1653 type_T *type;
1654
1655 // Used "import * as Name", need to lookup the member.
1656 if (*p != '.')
1657 {
1658 semsg(_("E1060: expected dot after name: %s"), start);
1659 return FAIL;
1660 }
1661 ++p;
1662
1663 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
1664 // TODO: what if it is a function?
1665 if (idx < 0)
1666 return FAIL;
1667 *end = p;
1668
1669 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1670 import->imp_sid,
1671 idx,
1672 type);
1673 }
1674 else
1675 {
1676 // TODO: check this is a variable, not a function
1677 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1678 import->imp_sid,
1679 import->imp_var_vals_idx,
1680 import->imp_type);
1681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 return OK;
1683 }
1684
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001685 if (error)
1686 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 return FAIL;
1688}
1689
1690/*
1691 * Compile a variable name into a load instruction.
1692 * "end" points to just after the name.
1693 * When "error" is FALSE do not give an error when not found.
1694 */
1695 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001696compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697{
1698 type_T *type;
1699 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001700 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 int res = FAIL;
1702
1703 if (*(*arg + 1) == ':')
1704 {
1705 // load namespaced variable
1706 name = vim_strnsave(*arg + 2, end - (*arg + 2));
1707 if (name == NULL)
1708 return FAIL;
1709
1710 if (**arg == 'v')
1711 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001712 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 }
1714 else if (**arg == 'g')
1715 {
1716 // Global variables can be defined later, thus we don't check if it
1717 // exists, give error at runtime.
1718 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1719 }
1720 else if (**arg == 's')
1721 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001722 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 }
1724 else
1725 {
1726 semsg("Namespace not supported yet: %s", **arg);
1727 goto theend;
1728 }
1729 }
1730 else
1731 {
1732 size_t len = end - *arg;
1733 int idx;
1734 int gen_load = FALSE;
1735
1736 name = vim_strnsave(*arg, end - *arg);
1737 if (name == NULL)
1738 return FAIL;
1739
1740 idx = lookup_arg(*arg, len, cctx);
1741 if (idx >= 0)
1742 {
1743 if (cctx->ctx_ufunc->uf_arg_types != NULL)
1744 type = cctx->ctx_ufunc->uf_arg_types[idx];
1745 else
1746 type = &t_any;
1747
1748 // Arguments are located above the frame pointer.
1749 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
1750 if (cctx->ctx_ufunc->uf_va_name != NULL)
1751 --idx;
1752 gen_load = TRUE;
1753 }
1754 else if (lookup_vararg(*arg, len, cctx))
1755 {
1756 // varargs is always the last argument
1757 idx = -STACK_FRAME_SIZE - 1;
1758 type = cctx->ctx_ufunc->uf_va_type;
1759 gen_load = TRUE;
1760 }
1761 else
1762 {
1763 idx = lookup_local(*arg, len, cctx);
1764 if (idx >= 0)
1765 {
1766 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
1767 gen_load = TRUE;
1768 }
1769 else
1770 {
1771 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
1772 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
1773 res = generate_PUSHBOOL(cctx, **arg == 't'
1774 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001775 else if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1776 == SCRIPT_VERSION_VIM9)
1777 // in Vim9 script "var" can be script-local.
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001778 res = compile_load_scriptvar(cctx, name, *arg, &end, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 }
1780 }
1781 if (gen_load)
1782 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1783 }
1784
1785 *arg = end;
1786
1787theend:
1788 if (res == FAIL && error)
1789 semsg(_(e_var_notfound), name);
1790 vim_free(name);
1791 return res;
1792}
1793
1794/*
1795 * Compile the argument expressions.
1796 * "arg" points to just after the "(" and is advanced to after the ")"
1797 */
1798 static int
1799compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
1800{
1801 char_u *p = *arg;
1802
1803 while (*p != NUL && *p != ')')
1804 {
1805 if (compile_expr1(&p, cctx) == FAIL)
1806 return FAIL;
1807 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001808
1809 if (*p != ',' && *skipwhite(p) == ',')
1810 {
1811 emsg(_("E1068: No white space allowed before ,"));
1812 p = skipwhite(p);
1813 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001815 {
1816 ++p;
1817 if (!VIM_ISWHITE(*p))
1818 emsg(_("E1069: white space required after ,"));
1819 }
1820 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001822 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 if (*p != ')')
1824 {
1825 emsg(_(e_missing_close));
1826 return FAIL;
1827 }
1828 *arg = p + 1;
1829 return OK;
1830}
1831
1832/*
1833 * Compile a function call: name(arg1, arg2)
1834 * "arg" points to "name", "arg + varlen" to the "(".
1835 * "argcount_init" is 1 for "value->method()"
1836 * Instructions:
1837 * EVAL arg1
1838 * EVAL arg2
1839 * BCALL / DCALL / UCALL
1840 */
1841 static int
1842compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
1843{
1844 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01001845 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 int argcount = argcount_init;
1847 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001848 char_u fname_buf[FLEN_FIXED + 1];
1849 char_u *tofree = NULL;
1850 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001852 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853
1854 if (varlen >= sizeof(namebuf))
1855 {
1856 semsg(_("E1011: name too long: %s"), name);
1857 return FAIL;
1858 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001859 vim_strncpy(namebuf, *arg, varlen);
1860 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861
1862 *arg = skipwhite(*arg + varlen + 1);
1863 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001864 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001866 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867 {
1868 int idx;
1869
1870 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001871 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001873 {
1874 res = generate_BCALL(cctx, idx, argcount);
1875 goto theend;
1876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 semsg(_(e_unknownfunc), namebuf);
1878 }
1879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001881 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001883 {
1884 res = generate_CALL(cctx, ufunc, argcount);
1885 goto theend;
1886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887
1888 // If the name is a variable, load it and use PCALL.
1889 p = namebuf;
1890 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001891 {
1892 res = generate_PCALL(cctx, argcount, FALSE);
1893 goto theend;
1894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895
1896 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001897 res = generate_UCALL(cctx, name, argcount);
1898
1899theend:
1900 vim_free(tofree);
1901 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902}
1903
1904// like NAMESPACE_CHAR but with 'a' and 'l'.
1905#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1906
1907/*
1908 * Find the end of a variable or function name. Unlike find_name_end() this
1909 * does not recognize magic braces.
1910 * Return a pointer to just after the name. Equal to "arg" if there is no
1911 * valid name.
1912 */
1913 char_u *
1914to_name_end(char_u *arg)
1915{
1916 char_u *p;
1917
1918 // Quick check for valid starting character.
1919 if (!eval_isnamec1(*arg))
1920 return arg;
1921
1922 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1923 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1924 // and can be used in slice "[n:]".
1925 if (*p == ':' && (p != arg + 1
1926 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1927 break;
1928 return p;
1929}
1930
1931/*
1932 * Like to_name_end() but also skip over a list or dict constant.
1933 */
1934 char_u *
1935to_name_const_end(char_u *arg)
1936{
1937 char_u *p = to_name_end(arg);
1938 typval_T rettv;
1939
1940 if (p == arg && *arg == '[')
1941 {
1942
1943 // Can be "[1, 2, 3]->Func()".
1944 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
1945 p = arg;
1946 }
1947 else if (p == arg && *arg == '#' && arg[1] == '{')
1948 {
1949 ++p;
1950 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
1951 p = arg;
1952 }
1953 else if (p == arg && *arg == '{')
1954 {
1955 int ret = get_lambda_tv(&p, &rettv, FALSE);
1956
1957 if (ret == NOTDONE)
1958 ret = eval_dict(&p, &rettv, FALSE, FALSE);
1959 if (ret != OK)
1960 p = arg;
1961 }
1962
1963 return p;
1964}
1965
1966 static void
1967type_mismatch(type_T *expected, type_T *actual)
1968{
1969 char *tofree1, *tofree2;
1970
1971 semsg(_("E1013: type mismatch, expected %s but got %s"),
1972 type_name(expected, &tofree1), type_name(actual, &tofree2));
1973 vim_free(tofree1);
1974 vim_free(tofree2);
1975}
1976
1977/*
1978 * Check if the expected and actual types match.
1979 */
1980 static int
1981check_type(type_T *expected, type_T *actual, int give_msg)
1982{
1983 if (expected->tt_type != VAR_UNKNOWN)
1984 {
1985 if (expected->tt_type != actual->tt_type)
1986 {
1987 if (give_msg)
1988 type_mismatch(expected, actual);
1989 return FAIL;
1990 }
1991 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
1992 {
Bram Moolenaar436472f2020-02-20 22:54:43 +01001993 int ret;
1994
1995 // void is used for an empty list or dict
1996 if (actual->tt_member == &t_void)
1997 ret = OK;
1998 else
1999 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 if (ret == FAIL && give_msg)
2001 type_mismatch(expected, actual);
2002 return ret;
2003 }
2004 }
2005 return OK;
2006}
2007
2008/*
2009 * Check that
2010 * - "actual" is "expected" type or
2011 * - "actual" is a type that can be "expected" type: add a runtime check; or
2012 * - return FAIL.
2013 */
2014 static int
2015need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2016{
Bram Moolenaar436472f2020-02-20 22:54:43 +01002017 if (check_type(expected, actual, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 return OK;
2019 if (actual->tt_type != VAR_UNKNOWN)
2020 {
2021 type_mismatch(expected, actual);
2022 return FAIL;
2023 }
2024 generate_TYPECHECK(cctx, expected, offset);
2025 return OK;
2026}
2027
2028/*
2029 * parse a list: [expr, expr]
2030 * "*arg" points to the '['.
2031 */
2032 static int
2033compile_list(char_u **arg, cctx_T *cctx)
2034{
2035 char_u *p = skipwhite(*arg + 1);
2036 int count = 0;
2037
2038 while (*p != ']')
2039 {
2040 if (*p == NUL)
2041 return FAIL;
2042 if (compile_expr1(&p, cctx) == FAIL)
2043 break;
2044 ++count;
2045 if (*p == ',')
2046 ++p;
2047 p = skipwhite(p);
2048 }
2049 *arg = p + 1;
2050
2051 generate_NEWLIST(cctx, count);
2052 return OK;
2053}
2054
2055/*
2056 * parse a lambda: {arg, arg -> expr}
2057 * "*arg" points to the '{'.
2058 */
2059 static int
2060compile_lambda(char_u **arg, cctx_T *cctx)
2061{
2062 garray_T *instr = &cctx->ctx_instr;
2063 typval_T rettv;
2064 ufunc_T *ufunc;
2065
2066 // Get the funcref in "rettv".
2067 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2068 return FAIL;
2069 ufunc = rettv.vval.v_partial->pt_func;
2070
2071 // The function will have one line: "return {expr}".
2072 // Compile it into instructions.
2073 compile_def_function(ufunc, TRUE);
2074
2075 if (ufunc->uf_dfunc_idx >= 0)
2076 {
2077 if (ga_grow(instr, 1) == FAIL)
2078 return FAIL;
2079 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2080 return OK;
2081 }
2082 return FAIL;
2083}
2084
2085/*
2086 * Compile a lamda call: expr->{lambda}(args)
2087 * "arg" points to the "{".
2088 */
2089 static int
2090compile_lambda_call(char_u **arg, cctx_T *cctx)
2091{
2092 ufunc_T *ufunc;
2093 typval_T rettv;
2094 int argcount = 1;
2095 int ret = FAIL;
2096
2097 // Get the funcref in "rettv".
2098 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2099 return FAIL;
2100
2101 if (**arg != '(')
2102 {
2103 if (*skipwhite(*arg) == '(')
2104 semsg(_(e_nowhitespace));
2105 else
2106 semsg(_(e_missing_paren), "lambda");
2107 clear_tv(&rettv);
2108 return FAIL;
2109 }
2110
2111 // The function will have one line: "return {expr}".
2112 // Compile it into instructions.
2113 ufunc = rettv.vval.v_partial->pt_func;
2114 ++ufunc->uf_refcount;
2115 compile_def_function(ufunc, TRUE);
2116
2117 // compile the arguments
2118 *arg = skipwhite(*arg + 1);
2119 if (compile_arguments(arg, cctx, &argcount) == OK)
2120 // call the compiled function
2121 ret = generate_CALL(cctx, ufunc, argcount);
2122
2123 clear_tv(&rettv);
2124 return ret;
2125}
2126
2127/*
2128 * parse a dict: {'key': val} or #{key: val}
2129 * "*arg" points to the '{'.
2130 */
2131 static int
2132compile_dict(char_u **arg, cctx_T *cctx, int literal)
2133{
2134 garray_T *instr = &cctx->ctx_instr;
2135 int count = 0;
2136 dict_T *d = dict_alloc();
2137 dictitem_T *item;
2138
2139 if (d == NULL)
2140 return FAIL;
2141 *arg = skipwhite(*arg + 1);
2142 while (**arg != '}' && **arg != NUL)
2143 {
2144 char_u *key = NULL;
2145
2146 if (literal)
2147 {
2148 char_u *p = to_name_end(*arg);
2149
2150 if (p == *arg)
2151 {
2152 semsg(_("E1014: Invalid key: %s"), *arg);
2153 return FAIL;
2154 }
2155 key = vim_strnsave(*arg, p - *arg);
2156 if (generate_PUSHS(cctx, key) == FAIL)
2157 return FAIL;
2158 *arg = p;
2159 }
2160 else
2161 {
2162 isn_T *isn;
2163
2164 if (compile_expr1(arg, cctx) == FAIL)
2165 return FAIL;
2166 // TODO: check type is string
2167 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2168 if (isn->isn_type == ISN_PUSHS)
2169 key = isn->isn_arg.string;
2170 }
2171
2172 // Check for duplicate keys, if using string keys.
2173 if (key != NULL)
2174 {
2175 item = dict_find(d, key, -1);
2176 if (item != NULL)
2177 {
2178 semsg(_(e_duplicate_key), key);
2179 goto failret;
2180 }
2181 item = dictitem_alloc(key);
2182 if (item != NULL)
2183 {
2184 item->di_tv.v_type = VAR_UNKNOWN;
2185 item->di_tv.v_lock = 0;
2186 if (dict_add(d, item) == FAIL)
2187 dictitem_free(item);
2188 }
2189 }
2190
2191 *arg = skipwhite(*arg);
2192 if (**arg != ':')
2193 {
2194 semsg(_(e_missing_dict_colon), *arg);
2195 return FAIL;
2196 }
2197
2198 *arg = skipwhite(*arg + 1);
2199 if (compile_expr1(arg, cctx) == FAIL)
2200 return FAIL;
2201 ++count;
2202
2203 if (**arg == '}')
2204 break;
2205 if (**arg != ',')
2206 {
2207 semsg(_(e_missing_dict_comma), *arg);
2208 goto failret;
2209 }
2210 *arg = skipwhite(*arg + 1);
2211 }
2212
2213 if (**arg != '}')
2214 {
2215 semsg(_(e_missing_dict_end), *arg);
2216 goto failret;
2217 }
2218 *arg = *arg + 1;
2219
2220 dict_unref(d);
2221 return generate_NEWDICT(cctx, count);
2222
2223failret:
2224 dict_unref(d);
2225 return FAIL;
2226}
2227
2228/*
2229 * Compile "&option".
2230 */
2231 static int
2232compile_get_option(char_u **arg, cctx_T *cctx)
2233{
2234 typval_T rettv;
2235 char_u *start = *arg;
2236 int ret;
2237
2238 // parse the option and get the current value to get the type.
2239 rettv.v_type = VAR_UNKNOWN;
2240 ret = get_option_tv(arg, &rettv, TRUE);
2241 if (ret == OK)
2242 {
2243 // include the '&' in the name, get_option_tv() expects it.
2244 char_u *name = vim_strnsave(start, *arg - start);
2245 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2246
2247 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2248 vim_free(name);
2249 }
2250 clear_tv(&rettv);
2251
2252 return ret;
2253}
2254
2255/*
2256 * Compile "$VAR".
2257 */
2258 static int
2259compile_get_env(char_u **arg, cctx_T *cctx)
2260{
2261 char_u *start = *arg;
2262 int len;
2263 int ret;
2264 char_u *name;
2265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 ++*arg;
2267 len = get_env_len(arg);
2268 if (len == 0)
2269 {
2270 semsg(_(e_syntax_at), start - 1);
2271 return FAIL;
2272 }
2273
2274 // include the '$' in the name, get_env_tv() expects it.
2275 name = vim_strnsave(start, len + 1);
2276 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2277 vim_free(name);
2278 return ret;
2279}
2280
2281/*
2282 * Compile "@r".
2283 */
2284 static int
2285compile_get_register(char_u **arg, cctx_T *cctx)
2286{
2287 int ret;
2288
2289 ++*arg;
2290 if (**arg == NUL)
2291 {
2292 semsg(_(e_syntax_at), *arg - 1);
2293 return FAIL;
2294 }
2295 if (!valid_yank_reg(**arg, TRUE))
2296 {
2297 emsg_invreg(**arg);
2298 return FAIL;
2299 }
2300 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2301 ++*arg;
2302 return ret;
2303}
2304
2305/*
2306 * Apply leading '!', '-' and '+' to constant "rettv".
2307 */
2308 static int
2309apply_leader(typval_T *rettv, char_u *start, char_u *end)
2310{
2311 char_u *p = end;
2312
2313 // this works from end to start
2314 while (p > start)
2315 {
2316 --p;
2317 if (*p == '-' || *p == '+')
2318 {
2319 // only '-' has an effect, for '+' we only check the type
2320#ifdef FEAT_FLOAT
2321 if (rettv->v_type == VAR_FLOAT)
2322 {
2323 if (*p == '-')
2324 rettv->vval.v_float = -rettv->vval.v_float;
2325 }
2326 else
2327#endif
2328 {
2329 varnumber_T val;
2330 int error = FALSE;
2331
2332 // tv_get_number_chk() accepts a string, but we don't want that
2333 // here
2334 if (check_not_string(rettv) == FAIL)
2335 return FAIL;
2336 val = tv_get_number_chk(rettv, &error);
2337 clear_tv(rettv);
2338 if (error)
2339 return FAIL;
2340 if (*p == '-')
2341 val = -val;
2342 rettv->v_type = VAR_NUMBER;
2343 rettv->vval.v_number = val;
2344 }
2345 }
2346 else
2347 {
2348 int v = tv2bool(rettv);
2349
2350 // '!' is permissive in the type.
2351 clear_tv(rettv);
2352 rettv->v_type = VAR_BOOL;
2353 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2354 }
2355 }
2356 return OK;
2357}
2358
2359/*
2360 * Recognize v: variables that are constants and set "rettv".
2361 */
2362 static void
2363get_vim_constant(char_u **arg, typval_T *rettv)
2364{
2365 if (STRNCMP(*arg, "v:true", 6) == 0)
2366 {
2367 rettv->v_type = VAR_BOOL;
2368 rettv->vval.v_number = VVAL_TRUE;
2369 *arg += 6;
2370 }
2371 else if (STRNCMP(*arg, "v:false", 7) == 0)
2372 {
2373 rettv->v_type = VAR_BOOL;
2374 rettv->vval.v_number = VVAL_FALSE;
2375 *arg += 7;
2376 }
2377 else if (STRNCMP(*arg, "v:null", 6) == 0)
2378 {
2379 rettv->v_type = VAR_SPECIAL;
2380 rettv->vval.v_number = VVAL_NULL;
2381 *arg += 6;
2382 }
2383 else if (STRNCMP(*arg, "v:none", 6) == 0)
2384 {
2385 rettv->v_type = VAR_SPECIAL;
2386 rettv->vval.v_number = VVAL_NONE;
2387 *arg += 6;
2388 }
2389}
2390
2391/*
2392 * Compile code to apply '-', '+' and '!'.
2393 */
2394 static int
2395compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2396{
2397 char_u *p = end;
2398
2399 // this works from end to start
2400 while (p > start)
2401 {
2402 --p;
2403 if (*p == '-' || *p == '+')
2404 {
2405 int negate = *p == '-';
2406 isn_T *isn;
2407
2408 // TODO: check type
2409 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2410 {
2411 --p;
2412 if (*p == '-')
2413 negate = !negate;
2414 }
2415 // only '-' has an effect, for '+' we only check the type
2416 if (negate)
2417 isn = generate_instr(cctx, ISN_NEGATENR);
2418 else
2419 isn = generate_instr(cctx, ISN_CHECKNR);
2420 if (isn == NULL)
2421 return FAIL;
2422 }
2423 else
2424 {
2425 int invert = TRUE;
2426
2427 while (p > start && p[-1] == '!')
2428 {
2429 --p;
2430 invert = !invert;
2431 }
2432 if (generate_2BOOL(cctx, invert) == FAIL)
2433 return FAIL;
2434 }
2435 }
2436 return OK;
2437}
2438
2439/*
2440 * Compile whatever comes after "name" or "name()".
2441 */
2442 static int
2443compile_subscript(
2444 char_u **arg,
2445 cctx_T *cctx,
2446 char_u **start_leader,
2447 char_u *end_leader)
2448{
2449 for (;;)
2450 {
2451 if (**arg == '(')
2452 {
2453 int argcount = 0;
2454
2455 // funcref(arg)
2456 *arg = skipwhite(*arg + 1);
2457 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2458 return FAIL;
2459 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2460 return FAIL;
2461 }
2462 else if (**arg == '-' && (*arg)[1] == '>')
2463 {
2464 char_u *p;
2465
2466 // something->method()
2467 // Apply the '!', '-' and '+' first:
2468 // -1.0->func() works like (-1.0)->func()
2469 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2470 return FAIL;
2471 *start_leader = end_leader; // don't apply again later
2472
2473 *arg = skipwhite(*arg + 2);
2474 if (**arg == '{')
2475 {
2476 // lambda call: list->{lambda}
2477 if (compile_lambda_call(arg, cctx) == FAIL)
2478 return FAIL;
2479 }
2480 else
2481 {
2482 // method call: list->method()
2483 for (p = *arg; eval_isnamec1(*p); ++p)
2484 ;
2485 if (*p != '(')
2486 {
2487 semsg(_(e_missing_paren), arg);
2488 return FAIL;
2489 }
2490 // TODO: base value may not be the first argument
2491 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2492 return FAIL;
2493 }
2494 }
2495 else if (**arg == '[')
2496 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002497 garray_T *stack;
2498 type_T **typep;
2499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 // list index: list[123]
2501 // TODO: more arguments
2502 // TODO: dict member dict['name']
2503 *arg = skipwhite(*arg + 1);
2504 if (compile_expr1(arg, cctx) == FAIL)
2505 return FAIL;
2506
2507 if (**arg != ']')
2508 {
2509 emsg(_(e_missbrac));
2510 return FAIL;
2511 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002512 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513
2514 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2515 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01002516 stack = &cctx->ctx_type_stack;
2517 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2518 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
2519 {
2520 emsg(_(e_listreq));
2521 return FAIL;
2522 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002523 if ((*typep)->tt_type == VAR_LIST)
2524 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 }
2526 else if (**arg == '.' && (*arg)[1] != '.')
2527 {
2528 char_u *p;
2529
2530 ++*arg;
2531 p = *arg;
2532 // dictionary member: dict.name
2533 if (eval_isnamec1(*p))
2534 while (eval_isnamec(*p))
2535 MB_PTR_ADV(p);
2536 if (p == *arg)
2537 {
2538 semsg(_(e_syntax_at), *arg);
2539 return FAIL;
2540 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2542 return FAIL;
2543 *arg = p;
2544 }
2545 else
2546 break;
2547 }
2548
2549 // TODO - see handle_subscript():
2550 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2551 // Don't do this when "Func" is already a partial that was bound
2552 // explicitly (pt_auto is FALSE).
2553
2554 return OK;
2555}
2556
2557/*
2558 * Compile an expression at "*p" and add instructions to "instr".
2559 * "p" is advanced until after the expression, skipping white space.
2560 *
2561 * This is the equivalent of eval1(), eval2(), etc.
2562 */
2563
2564/*
2565 * number number constant
2566 * 0zFFFFFFFF Blob constant
2567 * "string" string constant
2568 * 'string' literal string constant
2569 * &option-name option value
2570 * @r register contents
2571 * identifier variable value
2572 * function() function call
2573 * $VAR environment variable
2574 * (expression) nested expression
2575 * [expr, expr] List
2576 * {key: val, key: val} Dictionary
2577 * #{key: val, key: val} Dictionary with literal keys
2578 *
2579 * Also handle:
2580 * ! in front logical NOT
2581 * - in front unary minus
2582 * + in front unary plus (ignored)
2583 * trailing (arg) funcref/partial call
2584 * trailing [] subscript in String or List
2585 * trailing .name entry in Dictionary
2586 * trailing ->name() method call
2587 */
2588 static int
2589compile_expr7(char_u **arg, cctx_T *cctx)
2590{
2591 typval_T rettv;
2592 char_u *start_leader, *end_leader;
2593 int ret = OK;
2594
2595 /*
2596 * Skip '!', '-' and '+' characters. They are handled later.
2597 */
2598 start_leader = *arg;
2599 while (**arg == '!' || **arg == '-' || **arg == '+')
2600 *arg = skipwhite(*arg + 1);
2601 end_leader = *arg;
2602
2603 rettv.v_type = VAR_UNKNOWN;
2604 switch (**arg)
2605 {
2606 /*
2607 * Number constant.
2608 */
2609 case '0': // also for blob starting with 0z
2610 case '1':
2611 case '2':
2612 case '3':
2613 case '4':
2614 case '5':
2615 case '6':
2616 case '7':
2617 case '8':
2618 case '9':
2619 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
2620 return FAIL;
2621 break;
2622
2623 /*
2624 * String constant: "string".
2625 */
2626 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
2627 return FAIL;
2628 break;
2629
2630 /*
2631 * Literal string constant: 'str''ing'.
2632 */
2633 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
2634 return FAIL;
2635 break;
2636
2637 /*
2638 * Constant Vim variable.
2639 */
2640 case 'v': get_vim_constant(arg, &rettv);
2641 ret = NOTDONE;
2642 break;
2643
2644 /*
2645 * List: [expr, expr]
2646 */
2647 case '[': ret = compile_list(arg, cctx);
2648 break;
2649
2650 /*
2651 * Dictionary: #{key: val, key: val}
2652 */
2653 case '#': if ((*arg)[1] == '{')
2654 {
2655 ++*arg;
2656 ret = compile_dict(arg, cctx, TRUE);
2657 }
2658 else
2659 ret = NOTDONE;
2660 break;
2661
2662 /*
2663 * Lambda: {arg, arg -> expr}
2664 * Dictionary: {'key': val, 'key': val}
2665 */
2666 case '{': {
2667 char_u *start = skipwhite(*arg + 1);
2668
2669 // Find out what comes after the arguments.
2670 ret = get_function_args(&start, '-', NULL,
2671 NULL, NULL, NULL, TRUE);
2672 if (ret != FAIL && *start == '>')
2673 ret = compile_lambda(arg, cctx);
2674 else
2675 ret = compile_dict(arg, cctx, FALSE);
2676 }
2677 break;
2678
2679 /*
2680 * Option value: &name
2681 */
2682 case '&': ret = compile_get_option(arg, cctx);
2683 break;
2684
2685 /*
2686 * Environment variable: $VAR.
2687 */
2688 case '$': ret = compile_get_env(arg, cctx);
2689 break;
2690
2691 /*
2692 * Register contents: @r.
2693 */
2694 case '@': ret = compile_get_register(arg, cctx);
2695 break;
2696 /*
2697 * nested expression: (expression).
2698 */
2699 case '(': *arg = skipwhite(*arg + 1);
2700 ret = compile_expr1(arg, cctx); // recursive!
2701 *arg = skipwhite(*arg);
2702 if (**arg == ')')
2703 ++*arg;
2704 else if (ret == OK)
2705 {
2706 emsg(_(e_missing_close));
2707 ret = FAIL;
2708 }
2709 break;
2710
2711 default: ret = NOTDONE;
2712 break;
2713 }
2714 if (ret == FAIL)
2715 return FAIL;
2716
2717 if (rettv.v_type != VAR_UNKNOWN)
2718 {
2719 // apply the '!', '-' and '+' before the constant
2720 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
2721 {
2722 clear_tv(&rettv);
2723 return FAIL;
2724 }
2725 start_leader = end_leader; // don't apply again below
2726
2727 // push constant
2728 switch (rettv.v_type)
2729 {
2730 case VAR_BOOL:
2731 generate_PUSHBOOL(cctx, rettv.vval.v_number);
2732 break;
2733 case VAR_SPECIAL:
2734 generate_PUSHSPEC(cctx, rettv.vval.v_number);
2735 break;
2736 case VAR_NUMBER:
2737 generate_PUSHNR(cctx, rettv.vval.v_number);
2738 break;
2739#ifdef FEAT_FLOAT
2740 case VAR_FLOAT:
2741 generate_PUSHF(cctx, rettv.vval.v_float);
2742 break;
2743#endif
2744 case VAR_BLOB:
2745 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
2746 rettv.vval.v_blob = NULL;
2747 break;
2748 case VAR_STRING:
2749 generate_PUSHS(cctx, rettv.vval.v_string);
2750 rettv.vval.v_string = NULL;
2751 break;
2752 default:
2753 iemsg("constant type missing");
2754 return FAIL;
2755 }
2756 }
2757 else if (ret == NOTDONE)
2758 {
2759 char_u *p;
2760 int r;
2761
2762 if (!eval_isnamec1(**arg))
2763 {
2764 semsg(_("E1015: Name expected: %s"), *arg);
2765 return FAIL;
2766 }
2767
2768 // "name" or "name()"
2769 p = to_name_end(*arg);
2770 if (*p == '(')
2771 r = compile_call(arg, p - *arg, cctx, 0);
2772 else
2773 r = compile_load(arg, p, cctx, TRUE);
2774 if (r == FAIL)
2775 return FAIL;
2776 }
2777
2778 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
2779 return FAIL;
2780
2781 // Now deal with prefixed '-', '+' and '!', if not done already.
2782 return compile_leader(cctx, start_leader, end_leader);
2783}
2784
2785/*
2786 * * number multiplication
2787 * / number division
2788 * % number modulo
2789 */
2790 static int
2791compile_expr6(char_u **arg, cctx_T *cctx)
2792{
2793 char_u *op;
2794
2795 // get the first variable
2796 if (compile_expr7(arg, cctx) == FAIL)
2797 return FAIL;
2798
2799 /*
2800 * Repeat computing, until no "*", "/" or "%" is following.
2801 */
2802 for (;;)
2803 {
2804 op = skipwhite(*arg);
2805 if (*op != '*' && *op != '/' && *op != '%')
2806 break;
2807 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
2808 {
2809 char_u buf[3];
2810
2811 vim_strncpy(buf, op, 1);
2812 semsg(_(e_white_both), buf);
2813 }
2814 *arg = skipwhite(op + 1);
2815
2816 // get the second variable
2817 if (compile_expr7(arg, cctx) == FAIL)
2818 return FAIL;
2819
2820 generate_two_op(cctx, op);
2821 }
2822
2823 return OK;
2824}
2825
2826/*
2827 * + number addition
2828 * - number subtraction
2829 * .. string concatenation
2830 */
2831 static int
2832compile_expr5(char_u **arg, cctx_T *cctx)
2833{
2834 char_u *op;
2835 int oplen;
2836
2837 // get the first variable
2838 if (compile_expr6(arg, cctx) == FAIL)
2839 return FAIL;
2840
2841 /*
2842 * Repeat computing, until no "+", "-" or ".." is following.
2843 */
2844 for (;;)
2845 {
2846 op = skipwhite(*arg);
2847 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
2848 break;
2849 oplen = (*op == '.' ? 2 : 1);
2850
2851 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
2852 {
2853 char_u buf[3];
2854
2855 vim_strncpy(buf, op, oplen);
2856 semsg(_(e_white_both), buf);
2857 }
2858
2859 *arg = skipwhite(op + oplen);
2860
2861 // get the second variable
2862 if (compile_expr6(arg, cctx) == FAIL)
2863 return FAIL;
2864
2865 if (*op == '.')
2866 {
2867 if (may_generate_2STRING(-2, cctx) == FAIL
2868 || may_generate_2STRING(-1, cctx) == FAIL)
2869 return FAIL;
2870 generate_instr_drop(cctx, ISN_CONCAT, 1);
2871 }
2872 else
2873 generate_two_op(cctx, op);
2874 }
2875
2876 return OK;
2877}
2878
2879/*
2880 * expr5a == expr5b
2881 * expr5a =~ expr5b
2882 * expr5a != expr5b
2883 * expr5a !~ expr5b
2884 * expr5a > expr5b
2885 * expr5a >= expr5b
2886 * expr5a < expr5b
2887 * expr5a <= expr5b
2888 * expr5a is expr5b
2889 * expr5a isnot expr5b
2890 *
2891 * Produces instructions:
2892 * EVAL expr5a Push result of "expr5a"
2893 * EVAL expr5b Push result of "expr5b"
2894 * COMPARE one of the compare instructions
2895 */
2896 static int
2897compile_expr4(char_u **arg, cctx_T *cctx)
2898{
2899 exptype_T type = EXPR_UNKNOWN;
2900 char_u *p;
2901 int len = 2;
2902 int i;
2903 int type_is = FALSE;
2904
2905 // get the first variable
2906 if (compile_expr5(arg, cctx) == FAIL)
2907 return FAIL;
2908
2909 p = skipwhite(*arg);
2910 switch (p[0])
2911 {
2912 case '=': if (p[1] == '=')
2913 type = EXPR_EQUAL;
2914 else if (p[1] == '~')
2915 type = EXPR_MATCH;
2916 break;
2917 case '!': if (p[1] == '=')
2918 type = EXPR_NEQUAL;
2919 else if (p[1] == '~')
2920 type = EXPR_NOMATCH;
2921 break;
2922 case '>': if (p[1] != '=')
2923 {
2924 type = EXPR_GREATER;
2925 len = 1;
2926 }
2927 else
2928 type = EXPR_GEQUAL;
2929 break;
2930 case '<': if (p[1] != '=')
2931 {
2932 type = EXPR_SMALLER;
2933 len = 1;
2934 }
2935 else
2936 type = EXPR_SEQUAL;
2937 break;
2938 case 'i': if (p[1] == 's')
2939 {
2940 // "is" and "isnot"; but not a prefix of a name
2941 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2942 len = 5;
2943 i = p[len];
2944 if (!isalnum(i) && i != '_')
2945 {
2946 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
2947 type_is = TRUE;
2948 }
2949 }
2950 break;
2951 }
2952
2953 /*
2954 * If there is a comparative operator, use it.
2955 */
2956 if (type != EXPR_UNKNOWN)
2957 {
2958 int ic = FALSE; // Default: do not ignore case
2959
2960 if (type_is && (p[len] == '?' || p[len] == '#'))
2961 {
2962 semsg(_(e_invexpr2), *arg);
2963 return FAIL;
2964 }
2965 // extra question mark appended: ignore case
2966 if (p[len] == '?')
2967 {
2968 ic = TRUE;
2969 ++len;
2970 }
2971 // extra '#' appended: match case (ignored)
2972 else if (p[len] == '#')
2973 ++len;
2974 // nothing appended: match case
2975
2976 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
2977 {
2978 char_u buf[7];
2979
2980 vim_strncpy(buf, p, len);
2981 semsg(_(e_white_both), buf);
2982 }
2983
2984 // get the second variable
2985 *arg = skipwhite(p + len);
2986 if (compile_expr5(arg, cctx) == FAIL)
2987 return FAIL;
2988
2989 generate_COMPARE(cctx, type, ic);
2990 }
2991
2992 return OK;
2993}
2994
2995/*
2996 * Compile || or &&.
2997 */
2998 static int
2999compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3000{
3001 char_u *p = skipwhite(*arg);
3002 int opchar = *op;
3003
3004 if (p[0] == opchar && p[1] == opchar)
3005 {
3006 garray_T *instr = &cctx->ctx_instr;
3007 garray_T end_ga;
3008
3009 /*
3010 * Repeat until there is no following "||" or "&&"
3011 */
3012 ga_init2(&end_ga, sizeof(int), 10);
3013 while (p[0] == opchar && p[1] == opchar)
3014 {
3015 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3016 semsg(_(e_white_both), op);
3017
3018 if (ga_grow(&end_ga, 1) == FAIL)
3019 {
3020 ga_clear(&end_ga);
3021 return FAIL;
3022 }
3023 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3024 ++end_ga.ga_len;
3025 generate_JUMP(cctx, opchar == '|'
3026 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3027
3028 // eval the next expression
3029 *arg = skipwhite(p + 2);
3030 if ((opchar == '|' ? compile_expr3(arg, cctx)
3031 : compile_expr4(arg, cctx)) == FAIL)
3032 {
3033 ga_clear(&end_ga);
3034 return FAIL;
3035 }
3036 p = skipwhite(*arg);
3037 }
3038
3039 // Fill in the end label in all jumps.
3040 while (end_ga.ga_len > 0)
3041 {
3042 isn_T *isn;
3043
3044 --end_ga.ga_len;
3045 isn = ((isn_T *)instr->ga_data)
3046 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3047 isn->isn_arg.jump.jump_where = instr->ga_len;
3048 }
3049 ga_clear(&end_ga);
3050 }
3051
3052 return OK;
3053}
3054
3055/*
3056 * expr4a && expr4a && expr4a logical AND
3057 *
3058 * Produces instructions:
3059 * EVAL expr4a Push result of "expr4a"
3060 * JUMP_AND_KEEP_IF_FALSE end
3061 * EVAL expr4b Push result of "expr4b"
3062 * JUMP_AND_KEEP_IF_FALSE end
3063 * EVAL expr4c Push result of "expr4c"
3064 * end:
3065 */
3066 static int
3067compile_expr3(char_u **arg, cctx_T *cctx)
3068{
3069 // get the first variable
3070 if (compile_expr4(arg, cctx) == FAIL)
3071 return FAIL;
3072
3073 // || and && work almost the same
3074 return compile_and_or(arg, cctx, "&&");
3075}
3076
3077/*
3078 * expr3a || expr3b || expr3c logical OR
3079 *
3080 * Produces instructions:
3081 * EVAL expr3a Push result of "expr3a"
3082 * JUMP_AND_KEEP_IF_TRUE end
3083 * EVAL expr3b Push result of "expr3b"
3084 * JUMP_AND_KEEP_IF_TRUE end
3085 * EVAL expr3c Push result of "expr3c"
3086 * end:
3087 */
3088 static int
3089compile_expr2(char_u **arg, cctx_T *cctx)
3090{
3091 // eval the first expression
3092 if (compile_expr3(arg, cctx) == FAIL)
3093 return FAIL;
3094
3095 // || and && work almost the same
3096 return compile_and_or(arg, cctx, "||");
3097}
3098
3099/*
3100 * Toplevel expression: expr2 ? expr1a : expr1b
3101 *
3102 * Produces instructions:
3103 * EVAL expr2 Push result of "expr"
3104 * JUMP_IF_FALSE alt jump if false
3105 * EVAL expr1a
3106 * JUMP_ALWAYS end
3107 * alt: EVAL expr1b
3108 * end:
3109 */
3110 static int
3111compile_expr1(char_u **arg, cctx_T *cctx)
3112{
3113 char_u *p;
3114
3115 // evaluate the first expression
3116 if (compile_expr2(arg, cctx) == FAIL)
3117 return FAIL;
3118
3119 p = skipwhite(*arg);
3120 if (*p == '?')
3121 {
3122 garray_T *instr = &cctx->ctx_instr;
3123 garray_T *stack = &cctx->ctx_type_stack;
3124 int alt_idx = instr->ga_len;
3125 int end_idx;
3126 isn_T *isn;
3127 type_T *type1;
3128 type_T *type2;
3129
3130 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3131 semsg(_(e_white_both), "?");
3132
3133 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3134
3135 // evaluate the second expression; any type is accepted
3136 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003137 if (compile_expr1(arg, cctx) == FAIL)
3138 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139
3140 // remember the type and drop it
3141 --stack->ga_len;
3142 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3143
3144 end_idx = instr->ga_len;
3145 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3146
3147 // jump here from JUMP_IF_FALSE
3148 isn = ((isn_T *)instr->ga_data) + alt_idx;
3149 isn->isn_arg.jump.jump_where = instr->ga_len;
3150
3151 // Check for the ":".
3152 p = skipwhite(*arg);
3153 if (*p != ':')
3154 {
3155 emsg(_(e_missing_colon));
3156 return FAIL;
3157 }
3158 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3159 semsg(_(e_white_both), ":");
3160
3161 // evaluate the third expression
3162 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003163 if (compile_expr1(arg, cctx) == FAIL)
3164 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165
3166 // If the types differ, the result has a more generic type.
3167 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003168 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169
3170 // jump here from JUMP_ALWAYS
3171 isn = ((isn_T *)instr->ga_data) + end_idx;
3172 isn->isn_arg.jump.jump_where = instr->ga_len;
3173 }
3174 return OK;
3175}
3176
3177/*
3178 * compile "return [expr]"
3179 */
3180 static char_u *
3181compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3182{
3183 char_u *p = arg;
3184 garray_T *stack = &cctx->ctx_type_stack;
3185 type_T *stack_type;
3186
3187 if (*p != NUL && *p != '|' && *p != '\n')
3188 {
3189 // compile return argument into instructions
3190 if (compile_expr1(&p, cctx) == FAIL)
3191 return NULL;
3192
3193 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3194 if (set_return_type)
3195 cctx->ctx_ufunc->uf_ret_type = stack_type;
3196 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3197 == FAIL)
3198 return NULL;
3199 }
3200 else
3201 {
3202 if (set_return_type)
3203 cctx->ctx_ufunc->uf_ret_type = &t_void;
3204 else if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
3205 {
3206 emsg(_("E1003: Missing return value"));
3207 return NULL;
3208 }
3209
3210 // No argument, return zero.
3211 generate_PUSHNR(cctx, 0);
3212 }
3213
3214 if (generate_instr(cctx, ISN_RETURN) == NULL)
3215 return NULL;
3216
3217 // "return val | endif" is possible
3218 return skipwhite(p);
3219}
3220
3221/*
3222 * Return the length of an assignment operator, or zero if there isn't one.
3223 */
3224 int
3225assignment_len(char_u *p, int *heredoc)
3226{
3227 if (*p == '=')
3228 {
3229 if (p[1] == '<' && p[2] == '<')
3230 {
3231 *heredoc = TRUE;
3232 return 3;
3233 }
3234 return 1;
3235 }
3236 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3237 return 2;
3238 if (STRNCMP(p, "..=", 3) == 0)
3239 return 3;
3240 return 0;
3241}
3242
3243// words that cannot be used as a variable
3244static char *reserved[] = {
3245 "true",
3246 "false",
3247 NULL
3248};
3249
3250/*
3251 * Get a line for "=<<".
3252 * Return a pointer to the line in allocated memory.
3253 * Return NULL for end-of-file or some error.
3254 */
3255 static char_u *
3256heredoc_getline(
3257 int c UNUSED,
3258 void *cookie,
3259 int indent UNUSED,
3260 int do_concat UNUSED)
3261{
3262 cctx_T *cctx = (cctx_T *)cookie;
3263
3264 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
3265 NULL;
3266 ++cctx->ctx_lnum;
3267 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3268 [cctx->ctx_lnum]);
3269}
3270
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003271typedef enum {
3272 dest_local,
3273 dest_option,
3274 dest_env,
3275 dest_global,
3276 dest_vimvar,
3277 dest_script,
3278 dest_reg,
3279} assign_dest_T;
3280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281/*
3282 * compile "let var [= expr]", "const var = expr" and "var = expr"
3283 * "arg" points to "var".
3284 */
3285 static char_u *
3286compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3287{
3288 char_u *p;
3289 char_u *ret = NULL;
3290 int var_count = 0;
3291 int semicolon = 0;
3292 size_t varlen;
3293 garray_T *instr = &cctx->ctx_instr;
3294 int idx = -1;
3295 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003297 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003299 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 int oplen = 0;
3301 int heredoc = FALSE;
3302 type_T *type;
3303 lvar_T *lvar;
3304 char_u *name;
3305 char_u *sp;
3306 int has_type = FALSE;
3307 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3308 int instr_count = -1;
3309
3310 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3311 if (p == NULL)
3312 return NULL;
3313 if (var_count > 0)
3314 {
3315 // TODO: let [var, var] = list
3316 emsg("Cannot handle a list yet");
3317 return NULL;
3318 }
3319
3320 varlen = p - arg;
3321 name = vim_strnsave(arg, (int)varlen);
3322 if (name == NULL)
3323 return NULL;
3324
3325 if (*arg == '&')
3326 {
3327 int cc;
3328 long numval;
3329 char_u *stringval = NULL;
3330
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003331 dest = dest_option;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 if (cmdidx == CMD_const)
3333 {
3334 emsg(_(e_const_option));
3335 return NULL;
3336 }
3337 if (is_decl)
3338 {
3339 semsg(_("E1052: Cannot declare an option: %s"), arg);
3340 goto theend;
3341 }
3342 p = arg;
3343 p = find_option_end(&p, &opt_flags);
3344 if (p == NULL)
3345 {
3346 emsg(_(e_letunexp));
3347 return NULL;
3348 }
3349 cc = *p;
3350 *p = NUL;
3351 opt_type = get_option_value(arg + 1, &numval, &stringval, opt_flags);
3352 *p = cc;
3353 if (opt_type == -3)
3354 {
3355 semsg(_(e_unknown_option), *arg);
3356 return NULL;
3357 }
3358 if (opt_type == -2 || opt_type == 0)
3359 type = &t_string;
3360 else
3361 type = &t_number; // both number and boolean option
3362 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003363 else if (*arg == '$')
3364 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003365 dest = dest_env;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003366 if (is_decl)
3367 {
3368 semsg(_("E1065: Cannot declare an environment variable: %s"), name);
3369 goto theend;
3370 }
3371 }
3372 else if (*arg == '@')
3373 {
3374 if (!valid_yank_reg(arg[1], TRUE))
3375 {
3376 emsg_invreg(arg[1]);
3377 return FAIL;
3378 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003379 dest = dest_reg;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003380 if (is_decl)
3381 {
3382 semsg(_("E1066: Cannot declare a register: %s"), name);
3383 goto theend;
3384 }
3385 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 else if (STRNCMP(arg, "g:", 2) == 0)
3387 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003388 dest = dest_global;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 if (is_decl)
3390 {
3391 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3392 goto theend;
3393 }
3394 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003395 else if (STRNCMP(arg, "v:", 2) == 0)
3396 {
3397 vimvaridx = find_vim_var(name + 2);
3398 if (vimvaridx < 0)
3399 {
3400 semsg(_(e_var_notfound), arg);
3401 goto theend;
3402 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003403 dest = dest_vimvar;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003404 if (is_decl)
3405 {
3406 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3407 goto theend;
3408 }
3409 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 else
3411 {
3412 for (idx = 0; reserved[idx] != NULL; ++idx)
3413 if (STRCMP(reserved[idx], name) == 0)
3414 {
3415 semsg(_("E1034: Cannot use reserved name %s"), name);
3416 goto theend;
3417 }
3418
3419 idx = lookup_local(arg, varlen, cctx);
3420 if (idx >= 0)
3421 {
3422 if (is_decl)
3423 {
3424 semsg(_("E1017: Variable already declared: %s"), name);
3425 goto theend;
3426 }
3427 else
3428 {
3429 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3430 if (lvar->lv_const)
3431 {
3432 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3433 goto theend;
3434 }
3435 }
3436 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003437 else if (STRNCMP(arg, "s:", 2) == 0
3438 || lookup_script(arg, varlen) == OK
3439 || find_imported(arg, varlen, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003441 dest = dest_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 if (is_decl)
3443 {
3444 semsg(_("E1054: Variable already declared in the script: %s"),
3445 name);
3446 goto theend;
3447 }
3448 }
3449 }
3450
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003451 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 {
3453 if (is_decl && *p == ':')
3454 {
3455 // parse optional type: "let var: type = expr"
3456 p = skipwhite(p + 1);
3457 type = parse_type(&p, cctx->ctx_type_list);
3458 if (type == NULL)
3459 goto theend;
3460 has_type = TRUE;
3461 }
3462 else if (idx < 0)
3463 {
3464 // global and new local default to "any" type
3465 type = &t_any;
3466 }
3467 else
3468 {
3469 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3470 type = lvar->lv_type;
3471 }
3472 }
3473
3474 sp = p;
3475 p = skipwhite(p);
3476 op = p;
3477 oplen = assignment_len(p, &heredoc);
3478 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3479 {
3480 char_u buf[4];
3481
3482 vim_strncpy(buf, op, oplen);
3483 semsg(_(e_white_both), buf);
3484 }
3485
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003486 if (oplen == 3 && !heredoc && dest != dest_global
3487 && type->tt_type != VAR_STRING && type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01003489 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490 goto theend;
3491 }
3492
3493 // +=, /=, etc. require an existing variable
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003494 if (idx < 0 && dest == dest_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 {
3496 if (oplen > 1 && !heredoc)
3497 {
3498 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3499 name);
3500 goto theend;
3501 }
3502
3503 // new local variable
3504 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3505 if (idx < 0)
3506 goto theend;
3507 }
3508
3509 if (heredoc)
3510 {
3511 list_T *l;
3512 listitem_T *li;
3513
3514 // [let] varname =<< [trim] {end}
3515 eap->getline = heredoc_getline;
3516 eap->cookie = cctx;
3517 l = heredoc_get(eap, op + 3);
3518
3519 // Push each line and the create the list.
3520 for (li = l->lv_first; li != NULL; li = li->li_next)
3521 {
3522 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3523 li->li_tv.vval.v_string = NULL;
3524 }
3525 generate_NEWLIST(cctx, l->lv_len);
3526 type = &t_list_string;
3527 list_free(l);
3528 p += STRLEN(p);
3529 }
3530 else if (oplen > 0)
3531 {
3532 // for "+=", "*=", "..=" etc. first load the current value
3533 if (*op != '=')
3534 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003535 switch (dest)
3536 {
3537 case dest_option:
3538 // TODO: check the option exists
3539 generate_LOAD(cctx, ISN_LOADOPT, 0, name + 1, type);
3540 break;
3541 case dest_global:
3542 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3543 break;
3544 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003545 compile_load_scriptvar(cctx,
3546 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003547 break;
3548 case dest_env:
3549 // Include $ in the name here
3550 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
3551 break;
3552 case dest_reg:
3553 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
3554 break;
3555 case dest_vimvar:
3556 generate_LOADV(cctx, name + 2, TRUE);
3557 break;
3558 case dest_local:
3559 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3560 break;
3561 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 }
3563
3564 // compile the expression
3565 instr_count = instr->ga_len;
3566 p = skipwhite(p + oplen);
3567 if (compile_expr1(&p, cctx) == FAIL)
3568 goto theend;
3569
3570 if (idx >= 0 && (is_decl || !has_type))
3571 {
3572 garray_T *stack = &cctx->ctx_type_stack;
3573 type_T *stacktype =
3574 ((type_T **)stack->ga_data)[stack->ga_len - 1];
3575
3576 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3577 if (!has_type)
3578 {
3579 if (stacktype->tt_type == VAR_VOID)
3580 {
3581 emsg(_("E1031: Cannot use void value"));
3582 goto theend;
3583 }
3584 else
3585 lvar->lv_type = stacktype;
3586 }
3587 else
3588 if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
3589 goto theend;
3590 }
3591 }
3592 else if (cmdidx == CMD_const)
3593 {
3594 emsg(_("E1021: const requires a value"));
3595 goto theend;
3596 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003597 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 {
3599 emsg(_("E1022: type or initialization required"));
3600 goto theend;
3601 }
3602 else
3603 {
3604 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 if (ga_grow(instr, 1) == FAIL)
3606 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01003607 switch (type->tt_type)
3608 {
3609 case VAR_BOOL:
3610 generate_PUSHBOOL(cctx, VVAL_FALSE);
3611 break;
3612 case VAR_SPECIAL:
3613 generate_PUSHSPEC(cctx, VVAL_NONE);
3614 break;
3615 case VAR_FLOAT:
3616#ifdef FEAT_FLOAT
3617 generate_PUSHF(cctx, 0.0);
3618#endif
3619 break;
3620 case VAR_STRING:
3621 generate_PUSHS(cctx, NULL);
3622 break;
3623 case VAR_BLOB:
3624 generate_PUSHBLOB(cctx, NULL);
3625 break;
3626 case VAR_FUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003627 generate_PUSHFUNC(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003628 break;
3629 case VAR_PARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003630 generate_PUSHPARTIAL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003631 break;
3632 case VAR_LIST:
3633 generate_NEWLIST(cctx, 0);
3634 break;
3635 case VAR_DICT:
3636 generate_NEWDICT(cctx, 0);
3637 break;
3638 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003639 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003640 break;
3641 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003642 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003643 break;
3644 case VAR_NUMBER:
3645 case VAR_UNKNOWN:
3646 case VAR_VOID:
3647 generate_PUSHNR(cctx, 0);
3648 break;
3649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 }
3651
3652 if (oplen > 0 && *op != '=')
3653 {
3654 type_T *expected = &t_number;
3655 garray_T *stack = &cctx->ctx_type_stack;
3656 type_T *stacktype;
3657
3658 // TODO: if type is known use float or any operation
3659
3660 if (*op == '.')
3661 expected = &t_string;
3662 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3663 if (need_type(stacktype, expected, -1, cctx) == FAIL)
3664 goto theend;
3665
3666 if (*op == '.')
3667 generate_instr_drop(cctx, ISN_CONCAT, 1);
3668 else
3669 {
3670 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3671
3672 if (isn == NULL)
3673 goto theend;
3674 switch (*op)
3675 {
3676 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3677 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3678 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3679 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3680 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3681 }
3682 }
3683 }
3684
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003685 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003687 case dest_option:
3688 generate_STOREOPT(cctx, name + 1, opt_flags);
3689 break;
3690 case dest_global:
3691 // include g: with the name, easier to execute that way
3692 generate_STORE(cctx, ISN_STOREG, 0, name);
3693 break;
3694 case dest_env:
3695 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
3696 break;
3697 case dest_reg:
3698 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
3699 break;
3700 case dest_vimvar:
3701 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
3702 break;
3703 case dest_script:
3704 {
3705 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
3706 imported_T *import = NULL;
3707 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003708
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003709 if (name[1] != ':')
3710 {
3711 import = find_imported(name, 0, cctx);
3712 if (import != NULL)
3713 sid = import->imp_sid;
3714 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003716 idx = get_script_item_idx(sid, rawname, TRUE);
3717 // TODO: specific type
3718 if (idx < 0)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003719 generate_OLDSCRIPT(cctx, ISN_STORES, name, sid, &t_any);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003720 else
3721 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
3722 sid, idx, &t_any);
3723 }
3724 break;
3725 case dest_local:
3726 {
3727 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003729 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
3730 // into ISN_STORENR
3731 if (instr->ga_len == instr_count + 1
3732 && isn->isn_type == ISN_PUSHNR)
3733 {
3734 varnumber_T val = isn->isn_arg.number;
3735 garray_T *stack = &cctx->ctx_type_stack;
3736
3737 isn->isn_type = ISN_STORENR;
3738 isn->isn_arg.storenr.str_idx = idx;
3739 isn->isn_arg.storenr.str_val = val;
3740 if (stack->ga_len > 0)
3741 --stack->ga_len;
3742 }
3743 else
3744 generate_STORE(cctx, ISN_STORE, idx, NULL);
3745 }
3746 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 }
3748 ret = p;
3749
3750theend:
3751 vim_free(name);
3752 return ret;
3753}
3754
3755/*
3756 * Compile an :import command.
3757 */
3758 static char_u *
3759compile_import(char_u *arg, cctx_T *cctx)
3760{
3761 return handle_import(arg, &cctx->ctx_imports, 0);
3762}
3763
3764/*
3765 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3766 */
3767 static int
3768compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3769{
3770 garray_T *instr = &cctx->ctx_instr;
3771 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3772
3773 if (endlabel == NULL)
3774 return FAIL;
3775 endlabel->el_next = *el;
3776 *el = endlabel;
3777 endlabel->el_end_label = instr->ga_len;
3778
3779 generate_JUMP(cctx, when, 0);
3780 return OK;
3781}
3782
3783 static void
3784compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3785{
3786 garray_T *instr = &cctx->ctx_instr;
3787
3788 while (*el != NULL)
3789 {
3790 endlabel_T *cur = (*el);
3791 isn_T *isn;
3792
3793 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3794 isn->isn_arg.jump.jump_where = instr->ga_len;
3795 *el = cur->el_next;
3796 vim_free(cur);
3797 }
3798}
3799
3800/*
3801 * Create a new scope and set up the generic items.
3802 */
3803 static scope_T *
3804new_scope(cctx_T *cctx, scopetype_T type)
3805{
3806 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
3807
3808 if (scope == NULL)
3809 return NULL;
3810 scope->se_outer = cctx->ctx_scope;
3811 cctx->ctx_scope = scope;
3812 scope->se_type = type;
3813 scope->se_local_count = cctx->ctx_locals.ga_len;
3814 return scope;
3815}
3816
3817/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003818 * Evaluate an expression that is a constant:
3819 * has(arg)
3820 *
3821 * Also handle:
3822 * ! in front logical NOT
3823 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003824 * Return FAIL if the expression is not a constant.
3825 */
3826 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003827evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003828{
3829 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003830 char_u *start_leader, *end_leader;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003831
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003832 /*
3833 * Skip '!' characters. They are handled later.
3834 */
3835 start_leader = *arg;
3836 while (**arg == '!')
3837 *arg = skipwhite(*arg + 1);
3838 end_leader = *arg;
3839
3840 /*
3841 * Recognize only has() for now.
3842 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003843 if (STRNCMP("has(", *arg, 4) != 0)
3844 return FAIL;
3845 *arg = skipwhite(*arg + 4);
3846
3847 if (**arg == '"')
3848 {
3849 if (get_string_tv(arg, tv, TRUE) == FAIL)
3850 return FAIL;
3851 }
3852 else if (**arg == '\'')
3853 {
3854 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3855 return FAIL;
3856 }
3857 else
3858 return FAIL;
3859
3860 *arg = skipwhite(*arg);
3861 if (**arg != ')')
3862 return FAIL;
3863 *arg = skipwhite(*arg + 1);
3864
3865 argvars[0] = *tv;
3866 argvars[1].v_type = VAR_UNKNOWN;
3867 tv->v_type = VAR_NUMBER;
3868 tv->vval.v_number = 0;
3869 f_has(argvars, tv);
3870 clear_tv(&argvars[0]);
3871
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003872 while (start_leader < end_leader)
3873 {
3874 if (*start_leader == '!')
3875 tv->vval.v_number = !tv->vval.v_number;
3876 ++start_leader;
3877 }
3878
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003879 return OK;
3880}
3881
3882static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3883
3884/*
3885 * Compile constant || or &&.
3886 */
3887 static int
3888evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3889{
3890 char_u *p = skipwhite(*arg);
3891 int opchar = *op;
3892
3893 if (p[0] == opchar && p[1] == opchar)
3894 {
3895 int val = tv2bool(tv);
3896
3897 /*
3898 * Repeat until there is no following "||" or "&&"
3899 */
3900 while (p[0] == opchar && p[1] == opchar)
3901 {
3902 typval_T tv2;
3903
3904 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3905 return FAIL;
3906
3907 // eval the next expression
3908 *arg = skipwhite(p + 2);
3909 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01003910 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003911 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003912 : evaluate_const_expr7(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003913 {
3914 clear_tv(&tv2);
3915 return FAIL;
3916 }
3917 if ((opchar == '&') == val)
3918 {
3919 // false || tv2 or true && tv2: use tv2
3920 clear_tv(tv);
3921 *tv = tv2;
3922 val = tv2bool(tv);
3923 }
3924 else
3925 clear_tv(&tv2);
3926 p = skipwhite(*arg);
3927 }
3928 }
3929
3930 return OK;
3931}
3932
3933/*
3934 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
3935 * Return FAIL if the expression is not a constant.
3936 */
3937 static int
3938evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
3939{
3940 // evaluate the first expression
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01003941 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003942 return FAIL;
3943
3944 // || and && work almost the same
3945 return evaluate_const_and_or(arg, cctx, "&&", tv);
3946}
3947
3948/*
3949 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
3950 * Return FAIL if the expression is not a constant.
3951 */
3952 static int
3953evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
3954{
3955 // evaluate the first expression
3956 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
3957 return FAIL;
3958
3959 // || and && work almost the same
3960 return evaluate_const_and_or(arg, cctx, "||", tv);
3961}
3962
3963/*
3964 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
3965 * E.g. for "has('feature')".
3966 * This does not produce error messages. "tv" should be cleared afterwards.
3967 * Return FAIL if the expression is not a constant.
3968 */
3969 static int
3970evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
3971{
3972 char_u *p;
3973
3974 // evaluate the first expression
3975 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
3976 return FAIL;
3977
3978 p = skipwhite(*arg);
3979 if (*p == '?')
3980 {
3981 int val = tv2bool(tv);
3982 typval_T tv2;
3983
3984 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3985 return FAIL;
3986
3987 // evaluate the second expression; any type is accepted
3988 clear_tv(tv);
3989 *arg = skipwhite(p + 1);
3990 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
3991 return FAIL;
3992
3993 // Check for the ":".
3994 p = skipwhite(*arg);
3995 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3996 return FAIL;
3997
3998 // evaluate the third expression
3999 *arg = skipwhite(p + 1);
4000 tv2.v_type = VAR_UNKNOWN;
4001 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4002 {
4003 clear_tv(&tv2);
4004 return FAIL;
4005 }
4006 if (val)
4007 {
4008 // use the expr after "?"
4009 clear_tv(&tv2);
4010 }
4011 else
4012 {
4013 // use the expr after ":"
4014 clear_tv(tv);
4015 *tv = tv2;
4016 }
4017 }
4018 return OK;
4019}
4020
4021/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 * compile "if expr"
4023 *
4024 * "if expr" Produces instructions:
4025 * EVAL expr Push result of "expr"
4026 * JUMP_IF_FALSE end
4027 * ... body ...
4028 * end:
4029 *
4030 * "if expr | else" Produces instructions:
4031 * EVAL expr Push result of "expr"
4032 * JUMP_IF_FALSE else
4033 * ... body ...
4034 * JUMP_ALWAYS end
4035 * else:
4036 * ... body ...
4037 * end:
4038 *
4039 * "if expr1 | elseif expr2 | else" Produces instructions:
4040 * EVAL expr Push result of "expr"
4041 * JUMP_IF_FALSE elseif
4042 * ... body ...
4043 * JUMP_ALWAYS end
4044 * elseif:
4045 * EVAL expr Push result of "expr"
4046 * JUMP_IF_FALSE else
4047 * ... body ...
4048 * JUMP_ALWAYS end
4049 * else:
4050 * ... body ...
4051 * end:
4052 */
4053 static char_u *
4054compile_if(char_u *arg, cctx_T *cctx)
4055{
4056 char_u *p = arg;
4057 garray_T *instr = &cctx->ctx_instr;
4058 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004059 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004061 // compile "expr"; if we know it evaluates to FALSE skip the block
4062 tv.v_type = VAR_UNKNOWN;
4063 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4064 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4065 else
4066 cctx->ctx_skip = MAYBE;
4067 clear_tv(&tv);
4068 if (cctx->ctx_skip == MAYBE)
4069 {
4070 p = arg;
4071 if (compile_expr1(&p, cctx) == FAIL)
4072 return NULL;
4073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074
4075 scope = new_scope(cctx, IF_SCOPE);
4076 if (scope == NULL)
4077 return NULL;
4078
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004079 if (cctx->ctx_skip == MAYBE)
4080 {
4081 // "where" is set when ":elseif", "else" or ":endif" is found
4082 scope->se_u.se_if.is_if_label = instr->ga_len;
4083 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4084 }
4085 else
4086 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087
4088 return p;
4089}
4090
4091 static char_u *
4092compile_elseif(char_u *arg, cctx_T *cctx)
4093{
4094 char_u *p = arg;
4095 garray_T *instr = &cctx->ctx_instr;
4096 isn_T *isn;
4097 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004098 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099
4100 if (scope == NULL || scope->se_type != IF_SCOPE)
4101 {
4102 emsg(_(e_elseif_without_if));
4103 return NULL;
4104 }
4105 cctx->ctx_locals.ga_len = scope->se_local_count;
4106
Bram Moolenaar158906c2020-02-06 20:39:45 +01004107 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004108 {
4109 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004111 return NULL;
4112 // previous "if" or "elseif" jumps here
4113 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4114 isn->isn_arg.jump.jump_where = instr->ga_len;
4115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004117 // compile "expr"; if we know it evaluates to FALSE skip the block
4118 tv.v_type = VAR_UNKNOWN;
4119 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4120 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4121 else
4122 cctx->ctx_skip = MAYBE;
4123 clear_tv(&tv);
4124 if (cctx->ctx_skip == MAYBE)
4125 {
4126 p = arg;
4127 if (compile_expr1(&p, cctx) == FAIL)
4128 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004130 // "where" is set when ":elseif", "else" or ":endif" is found
4131 scope->se_u.se_if.is_if_label = instr->ga_len;
4132 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4133 }
4134 else
4135 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136
4137 return p;
4138}
4139
4140 static char_u *
4141compile_else(char_u *arg, cctx_T *cctx)
4142{
4143 char_u *p = arg;
4144 garray_T *instr = &cctx->ctx_instr;
4145 isn_T *isn;
4146 scope_T *scope = cctx->ctx_scope;
4147
4148 if (scope == NULL || scope->se_type != IF_SCOPE)
4149 {
4150 emsg(_(e_else_without_if));
4151 return NULL;
4152 }
4153 cctx->ctx_locals.ga_len = scope->se_local_count;
4154
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004155 // jump from previous block to the end, unless the else block is empty
4156 if (cctx->ctx_skip == MAYBE)
4157 {
4158 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004160 return NULL;
4161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162
Bram Moolenaar158906c2020-02-06 20:39:45 +01004163 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004164 {
4165 if (scope->se_u.se_if.is_if_label >= 0)
4166 {
4167 // previous "if" or "elseif" jumps here
4168 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4169 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004170 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004171 }
4172 }
4173
4174 if (cctx->ctx_skip != MAYBE)
4175 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176
4177 return p;
4178}
4179
4180 static char_u *
4181compile_endif(char_u *arg, cctx_T *cctx)
4182{
4183 scope_T *scope = cctx->ctx_scope;
4184 ifscope_T *ifscope;
4185 garray_T *instr = &cctx->ctx_instr;
4186 isn_T *isn;
4187
4188 if (scope == NULL || scope->se_type != IF_SCOPE)
4189 {
4190 emsg(_(e_endif_without_if));
4191 return NULL;
4192 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004193 ifscope = &scope->se_u.se_if;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 cctx->ctx_scope = scope->se_outer;
4195 cctx->ctx_locals.ga_len = scope->se_local_count;
4196
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004197 if (scope->se_u.se_if.is_if_label >= 0)
4198 {
4199 // previous "if" or "elseif" jumps here
4200 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4201 isn->isn_arg.jump.jump_where = instr->ga_len;
4202 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 // Fill in the "end" label in jumps at the end of the blocks.
4204 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004205 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206
4207 vim_free(scope);
4208 return arg;
4209}
4210
4211/*
4212 * compile "for var in expr"
4213 *
4214 * Produces instructions:
4215 * PUSHNR -1
4216 * STORE loop-idx Set index to -1
4217 * EVAL expr Push result of "expr"
4218 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4219 * - if beyond end, jump to "end"
4220 * - otherwise get item from list and push it
4221 * STORE var Store item in "var"
4222 * ... body ...
4223 * JUMP top Jump back to repeat
4224 * end: DROP Drop the result of "expr"
4225 *
4226 */
4227 static char_u *
4228compile_for(char_u *arg, cctx_T *cctx)
4229{
4230 char_u *p;
4231 size_t varlen;
4232 garray_T *instr = &cctx->ctx_instr;
4233 garray_T *stack = &cctx->ctx_type_stack;
4234 scope_T *scope;
4235 int loop_idx; // index of loop iteration variable
4236 int var_idx; // index of "var"
4237 type_T *vartype;
4238
4239 // TODO: list of variables: "for [key, value] in dict"
4240 // parse "var"
4241 for (p = arg; eval_isnamec1(*p); ++p)
4242 ;
4243 varlen = p - arg;
4244 var_idx = lookup_local(arg, varlen, cctx);
4245 if (var_idx >= 0)
4246 {
4247 semsg(_("E1023: variable already defined: %s"), arg);
4248 return NULL;
4249 }
4250
4251 // consume "in"
4252 p = skipwhite(p);
4253 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4254 {
4255 emsg(_(e_missing_in));
4256 return NULL;
4257 }
4258 p = skipwhite(p + 2);
4259
4260
4261 scope = new_scope(cctx, FOR_SCOPE);
4262 if (scope == NULL)
4263 return NULL;
4264
4265 // Reserve a variable to store the loop iteration counter.
4266 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4267 if (loop_idx < 0)
4268 return NULL;
4269
4270 // Reserve a variable to store "var"
4271 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4272 if (var_idx < 0)
4273 return NULL;
4274
4275 generate_STORENR(cctx, loop_idx, -1);
4276
4277 // compile "expr", it remains on the stack until "endfor"
4278 arg = p;
4279 if (compile_expr1(&arg, cctx) == FAIL)
4280 return NULL;
4281
4282 // now we know the type of "var"
4283 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4284 if (vartype->tt_type != VAR_LIST)
4285 {
4286 emsg(_("E1024: need a List to iterate over"));
4287 return NULL;
4288 }
4289 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
4290 {
4291 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4292
4293 lvar->lv_type = vartype->tt_member;
4294 }
4295
4296 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004297 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298
4299 generate_FOR(cctx, loop_idx);
4300 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4301
4302 return arg;
4303}
4304
4305/*
4306 * compile "endfor"
4307 */
4308 static char_u *
4309compile_endfor(char_u *arg, cctx_T *cctx)
4310{
4311 garray_T *instr = &cctx->ctx_instr;
4312 scope_T *scope = cctx->ctx_scope;
4313 forscope_T *forscope;
4314 isn_T *isn;
4315
4316 if (scope == NULL || scope->se_type != FOR_SCOPE)
4317 {
4318 emsg(_(e_for));
4319 return NULL;
4320 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004321 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 cctx->ctx_scope = scope->se_outer;
4323 cctx->ctx_locals.ga_len = scope->se_local_count;
4324
4325 // At end of ":for" scope jump back to the FOR instruction.
4326 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4327
4328 // Fill in the "end" label in the FOR statement so it can jump here
4329 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4330 isn->isn_arg.forloop.for_end = instr->ga_len;
4331
4332 // Fill in the "end" label any BREAK statements
4333 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4334
4335 // Below the ":for" scope drop the "expr" list from the stack.
4336 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4337 return NULL;
4338
4339 vim_free(scope);
4340
4341 return arg;
4342}
4343
4344/*
4345 * compile "while expr"
4346 *
4347 * Produces instructions:
4348 * top: EVAL expr Push result of "expr"
4349 * JUMP_IF_FALSE end jump if false
4350 * ... body ...
4351 * JUMP top Jump back to repeat
4352 * end:
4353 *
4354 */
4355 static char_u *
4356compile_while(char_u *arg, cctx_T *cctx)
4357{
4358 char_u *p = arg;
4359 garray_T *instr = &cctx->ctx_instr;
4360 scope_T *scope;
4361
4362 scope = new_scope(cctx, WHILE_SCOPE);
4363 if (scope == NULL)
4364 return NULL;
4365
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004366 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367
4368 // compile "expr"
4369 if (compile_expr1(&p, cctx) == FAIL)
4370 return NULL;
4371
4372 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004373 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 JUMP_IF_FALSE, cctx) == FAIL)
4375 return FAIL;
4376
4377 return p;
4378}
4379
4380/*
4381 * compile "endwhile"
4382 */
4383 static char_u *
4384compile_endwhile(char_u *arg, cctx_T *cctx)
4385{
4386 scope_T *scope = cctx->ctx_scope;
4387
4388 if (scope == NULL || scope->se_type != WHILE_SCOPE)
4389 {
4390 emsg(_(e_while));
4391 return NULL;
4392 }
4393 cctx->ctx_scope = scope->se_outer;
4394 cctx->ctx_locals.ga_len = scope->se_local_count;
4395
4396 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004397 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398
4399 // Fill in the "end" label in the WHILE statement so it can jump here.
4400 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004401 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402
4403 vim_free(scope);
4404
4405 return arg;
4406}
4407
4408/*
4409 * compile "continue"
4410 */
4411 static char_u *
4412compile_continue(char_u *arg, cctx_T *cctx)
4413{
4414 scope_T *scope = cctx->ctx_scope;
4415
4416 for (;;)
4417 {
4418 if (scope == NULL)
4419 {
4420 emsg(_(e_continue));
4421 return NULL;
4422 }
4423 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4424 break;
4425 scope = scope->se_outer;
4426 }
4427
4428 // Jump back to the FOR or WHILE instruction.
4429 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004430 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
4431 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 return arg;
4433}
4434
4435/*
4436 * compile "break"
4437 */
4438 static char_u *
4439compile_break(char_u *arg, cctx_T *cctx)
4440{
4441 scope_T *scope = cctx->ctx_scope;
4442 endlabel_T **el;
4443
4444 for (;;)
4445 {
4446 if (scope == NULL)
4447 {
4448 emsg(_(e_break));
4449 return NULL;
4450 }
4451 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4452 break;
4453 scope = scope->se_outer;
4454 }
4455
4456 // Jump to the end of the FOR or WHILE loop.
4457 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004458 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004460 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
4462 return FAIL;
4463
4464 return arg;
4465}
4466
4467/*
4468 * compile "{" start of block
4469 */
4470 static char_u *
4471compile_block(char_u *arg, cctx_T *cctx)
4472{
4473 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4474 return NULL;
4475 return skipwhite(arg + 1);
4476}
4477
4478/*
4479 * compile end of block: drop one scope
4480 */
4481 static void
4482compile_endblock(cctx_T *cctx)
4483{
4484 scope_T *scope = cctx->ctx_scope;
4485
4486 cctx->ctx_scope = scope->se_outer;
4487 cctx->ctx_locals.ga_len = scope->se_local_count;
4488 vim_free(scope);
4489}
4490
4491/*
4492 * compile "try"
4493 * Creates a new scope for the try-endtry, pointing to the first catch and
4494 * finally.
4495 * Creates another scope for the "try" block itself.
4496 * TRY instruction sets up exception handling at runtime.
4497 *
4498 * "try"
4499 * TRY -> catch1, -> finally push trystack entry
4500 * ... try block
4501 * "throw {exception}"
4502 * EVAL {exception}
4503 * THROW create exception
4504 * ... try block
4505 * " catch {expr}"
4506 * JUMP -> finally
4507 * catch1: PUSH exeception
4508 * EVAL {expr}
4509 * MATCH
4510 * JUMP nomatch -> catch2
4511 * CATCH remove exception
4512 * ... catch block
4513 * " catch"
4514 * JUMP -> finally
4515 * catch2: CATCH remove exception
4516 * ... catch block
4517 * " finally"
4518 * finally:
4519 * ... finally block
4520 * " endtry"
4521 * ENDTRY pop trystack entry, may rethrow
4522 */
4523 static char_u *
4524compile_try(char_u *arg, cctx_T *cctx)
4525{
4526 garray_T *instr = &cctx->ctx_instr;
4527 scope_T *try_scope;
4528 scope_T *scope;
4529
4530 // scope that holds the jumps that go to catch/finally/endtry
4531 try_scope = new_scope(cctx, TRY_SCOPE);
4532 if (try_scope == NULL)
4533 return NULL;
4534
4535 // "catch" is set when the first ":catch" is found.
4536 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004537 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 if (generate_instr(cctx, ISN_TRY) == NULL)
4539 return NULL;
4540
4541 // scope for the try block itself
4542 scope = new_scope(cctx, BLOCK_SCOPE);
4543 if (scope == NULL)
4544 return NULL;
4545
4546 return arg;
4547}
4548
4549/*
4550 * compile "catch {expr}"
4551 */
4552 static char_u *
4553compile_catch(char_u *arg, cctx_T *cctx UNUSED)
4554{
4555 scope_T *scope = cctx->ctx_scope;
4556 garray_T *instr = &cctx->ctx_instr;
4557 char_u *p;
4558 isn_T *isn;
4559
4560 // end block scope from :try or :catch
4561 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4562 compile_endblock(cctx);
4563 scope = cctx->ctx_scope;
4564
4565 // Error if not in a :try scope
4566 if (scope == NULL || scope->se_type != TRY_SCOPE)
4567 {
4568 emsg(_(e_catch));
4569 return NULL;
4570 }
4571
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004572 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 {
4574 emsg(_("E1033: catch unreachable after catch-all"));
4575 return NULL;
4576 }
4577
4578 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004579 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 JUMP_ALWAYS, cctx) == FAIL)
4581 return NULL;
4582
4583 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004584 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 if (isn->isn_arg.try.try_catch == 0)
4586 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004587 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 {
4589 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004590 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 isn->isn_arg.jump.jump_where = instr->ga_len;
4592 }
4593
4594 p = skipwhite(arg);
4595 if (ends_excmd(*p))
4596 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004597 scope->se_u.se_try.ts_caught_all = TRUE;
4598 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 }
4600 else
4601 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004602 char_u *end;
4603 char_u *pat;
4604 char_u *tofree = NULL;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004605 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 // Push v:exception, push {expr} and MATCH
4608 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
4609
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004610 end = skip_regexp(p + 1, *p, TRUE, &tofree);
4611 if (*end != *p)
4612 {
4613 semsg(_("E1067: Separator mismatch: %s"), p);
4614 vim_free(tofree);
4615 return FAIL;
4616 }
4617 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004618 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004619 else
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004620 len = (int)(end - (tofree + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004621 pat = vim_strnsave(p + 1, len);
4622 vim_free(tofree);
4623 p += len + 2;
4624 if (pat == NULL)
4625 return FAIL;
4626 if (generate_PUSHS(cctx, pat) == FAIL)
4627 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4630 return NULL;
4631
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004632 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4634 return NULL;
4635 }
4636
4637 if (generate_instr(cctx, ISN_CATCH) == NULL)
4638 return NULL;
4639
4640 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4641 return NULL;
4642 return p;
4643}
4644
4645 static char_u *
4646compile_finally(char_u *arg, cctx_T *cctx)
4647{
4648 scope_T *scope = cctx->ctx_scope;
4649 garray_T *instr = &cctx->ctx_instr;
4650 isn_T *isn;
4651
4652 // end block scope from :try or :catch
4653 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4654 compile_endblock(cctx);
4655 scope = cctx->ctx_scope;
4656
4657 // Error if not in a :try scope
4658 if (scope == NULL || scope->se_type != TRY_SCOPE)
4659 {
4660 emsg(_(e_finally));
4661 return NULL;
4662 }
4663
4664 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004665 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 if (isn->isn_arg.try.try_finally != 0)
4667 {
4668 emsg(_(e_finally_dup));
4669 return NULL;
4670 }
4671
4672 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004673 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004675 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 {
4677 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004678 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 isn->isn_arg.jump.jump_where = instr->ga_len;
4680 }
4681
4682 isn->isn_arg.try.try_finally = instr->ga_len;
4683 // TODO: set index in ts_finally_label jumps
4684
4685 return arg;
4686}
4687
4688 static char_u *
4689compile_endtry(char_u *arg, cctx_T *cctx)
4690{
4691 scope_T *scope = cctx->ctx_scope;
4692 garray_T *instr = &cctx->ctx_instr;
4693 isn_T *isn;
4694
4695 // end block scope from :catch or :finally
4696 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4697 compile_endblock(cctx);
4698 scope = cctx->ctx_scope;
4699
4700 // Error if not in a :try scope
4701 if (scope == NULL || scope->se_type != TRY_SCOPE)
4702 {
4703 if (scope == NULL)
4704 emsg(_(e_no_endtry));
4705 else if (scope->se_type == WHILE_SCOPE)
4706 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01004707 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 emsg(_(e_endfor));
4709 else
4710 emsg(_(e_endif));
4711 return NULL;
4712 }
4713
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004714 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
4716 {
4717 emsg(_("E1032: missing :catch or :finally"));
4718 return NULL;
4719 }
4720
4721 // Fill in the "end" label in jumps at the end of the blocks, if not done
4722 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004723 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724
4725 // End :catch or :finally scope: set value in ISN_TRY instruction
4726 if (isn->isn_arg.try.try_finally == 0)
4727 isn->isn_arg.try.try_finally = instr->ga_len;
4728 compile_endblock(cctx);
4729
4730 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4731 return NULL;
4732 return arg;
4733}
4734
4735/*
4736 * compile "throw {expr}"
4737 */
4738 static char_u *
4739compile_throw(char_u *arg, cctx_T *cctx UNUSED)
4740{
4741 char_u *p = skipwhite(arg);
4742
4743 if (ends_excmd(*p))
4744 {
4745 emsg(_(e_argreq));
4746 return NULL;
4747 }
4748 if (compile_expr1(&p, cctx) == FAIL)
4749 return NULL;
4750 if (may_generate_2STRING(-1, cctx) == FAIL)
4751 return NULL;
4752 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
4753 return NULL;
4754
4755 return p;
4756}
4757
4758/*
4759 * compile "echo expr"
4760 */
4761 static char_u *
4762compile_echo(char_u *arg, int with_white, cctx_T *cctx)
4763{
4764 char_u *p = arg;
4765 int count = 0;
4766
Bram Moolenaarad39c092020-02-26 18:23:43 +01004767 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768 {
4769 if (compile_expr1(&p, cctx) == FAIL)
4770 return NULL;
4771 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004772 p = skipwhite(p);
4773 if (ends_excmd(*p))
4774 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 }
4776
4777 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01004778 return p;
4779}
4780
4781/*
4782 * compile "execute expr"
4783 */
4784 static char_u *
4785compile_execute(char_u *arg, cctx_T *cctx)
4786{
4787 char_u *p = arg;
4788 int count = 0;
4789
4790 for (;;)
4791 {
4792 if (compile_expr1(&p, cctx) == FAIL)
4793 return NULL;
4794 ++count;
4795 p = skipwhite(p);
4796 if (ends_excmd(*p))
4797 break;
4798 }
4799
4800 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801
4802 return p;
4803}
4804
4805/*
4806 * After ex_function() has collected all the function lines: parse and compile
4807 * the lines into instructions.
4808 * Adds the function to "def_functions".
4809 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
4810 * return statement (used for lambda).
4811 */
4812 void
4813compile_def_function(ufunc_T *ufunc, int set_return_type)
4814{
4815 dfunc_T *dfunc;
4816 char_u *line = NULL;
4817 char_u *p;
4818 exarg_T ea;
4819 char *errormsg = NULL; // error message
4820 int had_return = FALSE;
4821 cctx_T cctx;
4822 garray_T *instr;
4823 int called_emsg_before = called_emsg;
4824 int ret = FAIL;
4825 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004826 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827
4828 if (ufunc->uf_dfunc_idx >= 0)
4829 {
4830 // redefining a function that was compiled before
4831 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4832 dfunc->df_deleted = FALSE;
4833 }
4834 else
4835 {
4836 // Add the function to "def_functions".
4837 if (ga_grow(&def_functions, 1) == FAIL)
4838 return;
4839 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
4840 vim_memset(dfunc, 0, sizeof(dfunc_T));
4841 dfunc->df_idx = def_functions.ga_len;
4842 ufunc->uf_dfunc_idx = dfunc->df_idx;
4843 dfunc->df_ufunc = ufunc;
4844 ++def_functions.ga_len;
4845 }
4846
4847 vim_memset(&cctx, 0, sizeof(cctx));
4848 cctx.ctx_ufunc = ufunc;
4849 cctx.ctx_lnum = -1;
4850 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
4851 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
4852 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
4853 cctx.ctx_type_list = &ufunc->uf_type_list;
4854 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
4855 instr = &cctx.ctx_instr;
4856
4857 // Most modern script version.
4858 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4859
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004860 if (ufunc->uf_def_args.ga_len > 0)
4861 {
4862 int count = ufunc->uf_def_args.ga_len;
4863 int i;
4864 char_u *arg;
4865 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
4866
4867 // Produce instructions for the default values of optional arguments.
4868 // Store the instruction index in uf_def_arg_idx[] so that we know
4869 // where to start when the function is called, depending on the number
4870 // of arguments.
4871 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
4872 if (ufunc->uf_def_arg_idx == NULL)
4873 goto erret;
4874 for (i = 0; i < count; ++i)
4875 {
4876 ufunc->uf_def_arg_idx[i] = instr->ga_len;
4877 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
4878 if (compile_expr1(&arg, &cctx) == FAIL
4879 || generate_STORE(&cctx, ISN_STORE,
4880 i - count - off, NULL) == FAIL)
4881 goto erret;
4882 }
4883
4884 // If a varargs is following, push an empty list.
4885 if (ufunc->uf_va_name != NULL)
4886 {
4887 if (generate_NEWLIST(&cctx, 0) == FAIL
4888 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
4889 goto erret;
4890 }
4891
4892 ufunc->uf_def_arg_idx[count] = instr->ga_len;
4893 }
4894
4895 /*
4896 * Loop over all the lines of the function and generate instructions.
4897 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 for (;;)
4899 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004900 int is_ex_command;
4901
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 if (line != NULL && *line == '|')
4903 // the line continues after a '|'
4904 ++line;
4905 else if (line != NULL && *line != NUL)
4906 {
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004907 if (emsg_before == called_emsg)
4908 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 goto erret;
4910 }
4911 else
4912 {
4913 do
4914 {
4915 ++cctx.ctx_lnum;
4916 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4917 break;
4918 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
4919 } while (line == NULL);
4920 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4921 break;
4922 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
4923 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004924 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925
4926 had_return = FALSE;
4927 vim_memset(&ea, 0, sizeof(ea));
4928 ea.cmdlinep = &line;
4929 ea.cmd = skipwhite(line);
4930
4931 // "}" ends a block scope
4932 if (*ea.cmd == '}')
4933 {
4934 scopetype_T stype = cctx.ctx_scope == NULL
4935 ? NO_SCOPE : cctx.ctx_scope->se_type;
4936
4937 if (stype == BLOCK_SCOPE)
4938 {
4939 compile_endblock(&cctx);
4940 line = ea.cmd;
4941 }
4942 else
4943 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004944 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 goto erret;
4946 }
4947 if (line != NULL)
4948 line = skipwhite(ea.cmd + 1);
4949 continue;
4950 }
4951
4952 // "{" starts a block scope
4953 if (*ea.cmd == '{')
4954 {
4955 line = compile_block(ea.cmd, &cctx);
4956 continue;
4957 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004958 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959
4960 /*
4961 * COMMAND MODIFIERS
4962 */
4963 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
4964 {
4965 if (errormsg != NULL)
4966 goto erret;
4967 // empty line or comment
4968 line = (char_u *)"";
4969 continue;
4970 }
4971
4972 // Skip ":call" to get to the function name.
4973 if (checkforcmd(&ea.cmd, "call", 3))
4974 ea.cmd = skipwhite(ea.cmd);
4975
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004976 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004978 // Assuming the command starts with a variable or function name,
4979 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
4980 // val".
4981 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
4982 ? ea.cmd + 1 : ea.cmd;
4983 p = to_name_end(p);
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01004984 if ((p > ea.cmd && *p != NUL) || *p == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004986 int oplen;
4987 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01004989 oplen = assignment_len(skipwhite(p), &heredoc);
4990 if (oplen > 0)
4991 {
4992 // Recognize an assignment if we recognize the variable
4993 // name:
4994 // "g:var = expr"
4995 // "var = expr" where "var" is a local var name.
4996 // "&opt = expr"
4997 // "$ENV = expr"
4998 // "@r = expr"
4999 if (*ea.cmd == '&'
5000 || *ea.cmd == '$'
5001 || *ea.cmd == '@'
5002 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5003 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5004 || lookup_script(ea.cmd, p - ea.cmd) == OK
5005 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5006 {
5007 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5008 if (line == NULL)
5009 goto erret;
5010 continue;
5011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 }
5013 }
5014 }
5015
5016 /*
5017 * COMMAND after range
5018 */
5019 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005020 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5021 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022
5023 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5024 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005025 if (cctx.ctx_skip == TRUE)
5026 {
5027 line += STRLEN(line);
5028 continue;
5029 }
5030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 // Expression or function call.
5032 if (ea.cmdidx == CMD_eval)
5033 {
5034 p = ea.cmd;
5035 if (compile_expr1(&p, &cctx) == FAIL)
5036 goto erret;
5037
5038 // drop the return value
5039 generate_instr_drop(&cctx, ISN_DROP, 1);
5040 line = p;
5041 continue;
5042 }
5043 if (ea.cmdidx == CMD_let)
5044 {
5045 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5046 if (line == NULL)
5047 goto erret;
5048 continue;
5049 }
5050 iemsg("Command from find_ex_command() not handled");
5051 goto erret;
5052 }
5053
5054 p = skipwhite(p);
5055
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005056 if (cctx.ctx_skip == TRUE
5057 && ea.cmdidx != CMD_elseif
5058 && ea.cmdidx != CMD_else
5059 && ea.cmdidx != CMD_endif)
5060 {
5061 line += STRLEN(line);
5062 continue;
5063 }
5064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 switch (ea.cmdidx)
5066 {
5067 case CMD_def:
5068 case CMD_function:
5069 // TODO: Nested function
5070 emsg("Nested function not implemented yet");
5071 goto erret;
5072
5073 case CMD_return:
5074 line = compile_return(p, set_return_type, &cctx);
5075 had_return = TRUE;
5076 break;
5077
5078 case CMD_let:
5079 case CMD_const:
5080 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5081 break;
5082
5083 case CMD_import:
5084 line = compile_import(p, &cctx);
5085 break;
5086
5087 case CMD_if:
5088 line = compile_if(p, &cctx);
5089 break;
5090 case CMD_elseif:
5091 line = compile_elseif(p, &cctx);
5092 break;
5093 case CMD_else:
5094 line = compile_else(p, &cctx);
5095 break;
5096 case CMD_endif:
5097 line = compile_endif(p, &cctx);
5098 break;
5099
5100 case CMD_while:
5101 line = compile_while(p, &cctx);
5102 break;
5103 case CMD_endwhile:
5104 line = compile_endwhile(p, &cctx);
5105 break;
5106
5107 case CMD_for:
5108 line = compile_for(p, &cctx);
5109 break;
5110 case CMD_endfor:
5111 line = compile_endfor(p, &cctx);
5112 break;
5113 case CMD_continue:
5114 line = compile_continue(p, &cctx);
5115 break;
5116 case CMD_break:
5117 line = compile_break(p, &cctx);
5118 break;
5119
5120 case CMD_try:
5121 line = compile_try(p, &cctx);
5122 break;
5123 case CMD_catch:
5124 line = compile_catch(p, &cctx);
5125 break;
5126 case CMD_finally:
5127 line = compile_finally(p, &cctx);
5128 break;
5129 case CMD_endtry:
5130 line = compile_endtry(p, &cctx);
5131 break;
5132 case CMD_throw:
5133 line = compile_throw(p, &cctx);
5134 break;
5135
5136 case CMD_echo:
5137 line = compile_echo(p, TRUE, &cctx);
5138 break;
5139 case CMD_echon:
5140 line = compile_echo(p, FALSE, &cctx);
5141 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005142 case CMD_execute:
5143 line = compile_execute(p, &cctx);
5144 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145
5146 default:
5147 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005148 // TODO:
5149 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005150 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151 generate_EXEC(&cctx, line);
5152 line = (char_u *)"";
5153 break;
5154 }
5155 if (line == NULL)
5156 goto erret;
5157
5158 if (cctx.ctx_type_stack.ga_len < 0)
5159 {
5160 iemsg("Type stack underflow");
5161 goto erret;
5162 }
5163 }
5164
5165 if (cctx.ctx_scope != NULL)
5166 {
5167 if (cctx.ctx_scope->se_type == IF_SCOPE)
5168 emsg(_(e_endif));
5169 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5170 emsg(_(e_endwhile));
5171 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5172 emsg(_(e_endfor));
5173 else
5174 emsg(_("E1026: Missing }"));
5175 goto erret;
5176 }
5177
5178 if (!had_return)
5179 {
5180 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5181 {
5182 emsg(_("E1027: Missing return statement"));
5183 goto erret;
5184 }
5185
5186 // Return zero if there is no return at the end.
5187 generate_PUSHNR(&cctx, 0);
5188 generate_instr(&cctx, ISN_RETURN);
5189 }
5190
5191 dfunc->df_instr = instr->ga_data;
5192 dfunc->df_instr_count = instr->ga_len;
5193 dfunc->df_varcount = cctx.ctx_max_local;
5194
5195 ret = OK;
5196
5197erret:
5198 if (ret == FAIL)
5199 {
5200 ga_clear(instr);
5201 ufunc->uf_dfunc_idx = -1;
5202 --def_functions.ga_len;
5203 if (errormsg != NULL)
5204 emsg(errormsg);
5205 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005206 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207
5208 // don't execute this function body
5209 ufunc->uf_lines.ga_len = 0;
5210 }
5211
5212 current_sctx = save_current_sctx;
5213 ga_clear(&cctx.ctx_type_stack);
5214 ga_clear(&cctx.ctx_locals);
5215}
5216
5217/*
5218 * Delete an instruction, free what it contains.
5219 */
5220 static void
5221delete_instr(isn_T *isn)
5222{
5223 switch (isn->isn_type)
5224 {
5225 case ISN_EXEC:
5226 case ISN_LOADENV:
5227 case ISN_LOADG:
5228 case ISN_LOADOPT:
5229 case ISN_MEMBER:
5230 case ISN_PUSHEXC:
5231 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005232 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005234 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 vim_free(isn->isn_arg.string);
5236 break;
5237
5238 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005239 case ISN_STORES:
5240 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241 break;
5242
5243 case ISN_STOREOPT:
5244 vim_free(isn->isn_arg.storeopt.so_name);
5245 break;
5246
5247 case ISN_PUSHBLOB: // push blob isn_arg.blob
5248 blob_unref(isn->isn_arg.blob);
5249 break;
5250
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005251 case ISN_PUSHPARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01005252 partial_unref(isn->isn_arg.partial);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005253 break;
5254
5255 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005256#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005257 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005258#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005259 break;
5260
5261 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005262#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005263 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005264#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005265 break;
5266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267 case ISN_UCALL:
5268 vim_free(isn->isn_arg.ufunc.cuf_name);
5269 break;
5270
5271 case ISN_2BOOL:
5272 case ISN_2STRING:
5273 case ISN_ADDBLOB:
5274 case ISN_ADDLIST:
5275 case ISN_BCALL:
5276 case ISN_CATCH:
5277 case ISN_CHECKNR:
5278 case ISN_CHECKTYPE:
5279 case ISN_COMPAREANY:
5280 case ISN_COMPAREBLOB:
5281 case ISN_COMPAREBOOL:
5282 case ISN_COMPAREDICT:
5283 case ISN_COMPAREFLOAT:
5284 case ISN_COMPAREFUNC:
5285 case ISN_COMPARELIST:
5286 case ISN_COMPARENR:
5287 case ISN_COMPAREPARTIAL:
5288 case ISN_COMPARESPECIAL:
5289 case ISN_COMPARESTRING:
5290 case ISN_CONCAT:
5291 case ISN_DCALL:
5292 case ISN_DROP:
5293 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01005294 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295 case ISN_ENDTRY:
5296 case ISN_FOR:
5297 case ISN_FUNCREF:
5298 case ISN_INDEX:
5299 case ISN_JUMP:
5300 case ISN_LOAD:
5301 case ISN_LOADSCRIPT:
5302 case ISN_LOADREG:
5303 case ISN_LOADV:
5304 case ISN_NEGATENR:
5305 case ISN_NEWDICT:
5306 case ISN_NEWLIST:
5307 case ISN_OPNR:
5308 case ISN_OPFLOAT:
5309 case ISN_OPANY:
5310 case ISN_PCALL:
5311 case ISN_PUSHF:
5312 case ISN_PUSHNR:
5313 case ISN_PUSHBOOL:
5314 case ISN_PUSHSPEC:
5315 case ISN_RETURN:
5316 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005317 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005319 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320 case ISN_STORESCRIPT:
5321 case ISN_THROW:
5322 case ISN_TRY:
5323 // nothing allocated
5324 break;
5325 }
5326}
5327
5328/*
5329 * When a user function is deleted, delete any associated def function.
5330 */
5331 void
5332delete_def_function(ufunc_T *ufunc)
5333{
5334 int idx;
5335
5336 if (ufunc->uf_dfunc_idx >= 0)
5337 {
5338 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5339 + ufunc->uf_dfunc_idx;
5340 ga_clear(&dfunc->df_def_args_isn);
5341
5342 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5343 delete_instr(dfunc->df_instr + idx);
5344 VIM_CLEAR(dfunc->df_instr);
5345
5346 dfunc->df_deleted = TRUE;
5347 }
5348}
5349
5350#if defined(EXITFREE) || defined(PROTO)
5351 void
5352free_def_functions(void)
5353{
5354 vim_free(def_functions.ga_data);
5355}
5356#endif
5357
5358
5359#endif // FEAT_EVAL