blob: 2726e387f756a3098338e60e6caab891ff372815 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
26/*
27 * Chain of jump instructions where the end label needs to be set.
28 */
29typedef struct endlabel_S endlabel_T;
30struct endlabel_S {
31 endlabel_T *el_next; // chain end_label locations
32 int el_end_label; // instruction idx where to set end
33};
34
35/*
36 * info specific for the scope of :if / elseif / else
37 */
38typedef struct {
39 int is_if_label; // instruction idx at IF or ELSEIF
40 endlabel_T *is_end_label; // instructions to set end label
41} ifscope_T;
42
43/*
44 * info specific for the scope of :while
45 */
46typedef struct {
47 int ws_top_label; // instruction idx at WHILE
48 endlabel_T *ws_end_label; // instructions to set end
49} whilescope_T;
50
51/*
52 * info specific for the scope of :for
53 */
54typedef struct {
55 int fs_top_label; // instruction idx at FOR
56 endlabel_T *fs_end_label; // break instructions
57} forscope_T;
58
59/*
60 * info specific for the scope of :try
61 */
62typedef struct {
63 int ts_try_label; // instruction idx at TRY
64 endlabel_T *ts_end_label; // jump to :finally or :endtry
65 int ts_catch_label; // instruction idx of last CATCH
66 int ts_caught_all; // "catch" without argument encountered
67} tryscope_T;
68
69typedef enum {
70 NO_SCOPE,
71 IF_SCOPE,
72 WHILE_SCOPE,
73 FOR_SCOPE,
74 TRY_SCOPE,
75 BLOCK_SCOPE
76} scopetype_T;
77
78/*
79 * Info for one scope, pointed to by "ctx_scope".
80 */
81typedef struct scope_S scope_T;
82struct scope_S {
83 scope_T *se_outer; // scope containing this one
84 scopetype_T se_type;
85 int se_local_count; // ctx_locals.ga_len before scope
86 union {
87 ifscope_T se_if;
88 whilescope_T se_while;
89 forscope_T se_for;
90 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +010091 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092};
93
94/*
95 * Entry for "ctx_locals". Used for arguments and local variables.
96 */
97typedef struct {
98 char_u *lv_name;
99 type_T *lv_type;
100 int lv_const; // when TRUE cannot be assigned to
101 int lv_arg; // when TRUE this is an argument
102} lvar_T;
103
104/*
105 * Context for compiling lines of Vim script.
106 * Stores info about the local variables and condition stack.
107 */
108struct cctx_S {
109 ufunc_T *ctx_ufunc; // current function
110 int ctx_lnum; // line number in current function
111 garray_T ctx_instr; // generated instructions
112
113 garray_T ctx_locals; // currently visible local variables
114 int ctx_max_local; // maximum number of locals at one time
115
116 garray_T ctx_imports; // imported items
117
Bram Moolenaara259d8d2020-01-31 20:10:50 +0100118 int ctx_skip; // when TRUE skip commands, when FALSE skip
119 // commands after "else"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100120 scope_T *ctx_scope; // current scope, NULL at toplevel
121
122 garray_T ctx_type_stack; // type of each item on the stack
123 garray_T *ctx_type_list; // space for adding types
124};
125
126static char e_var_notfound[] = N_("E1001: variable not found: %s");
127static char e_syntax_at[] = N_("E1002: Syntax error at %s");
128
129static int compile_expr1(char_u **arg, cctx_T *cctx);
130static int compile_expr2(char_u **arg, cctx_T *cctx);
131static int compile_expr3(char_u **arg, cctx_T *cctx);
Bram Moolenaar20431c92020-03-20 18:39:46 +0100132static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133
134/*
135 * Lookup variable "name" in the local scope and return the index.
136 */
137 static int
138lookup_local(char_u *name, size_t len, cctx_T *cctx)
139{
140 int idx;
141
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100142 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 return -1;
144 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
145 {
146 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
147
148 if (STRNCMP(name, lvar->lv_name, len) == 0
149 && STRLEN(lvar->lv_name) == len)
150 return idx;
151 }
152 return -1;
153}
154
155/*
156 * Lookup an argument in the current function.
157 * Returns the argument index or -1 if not found.
158 */
159 static int
160lookup_arg(char_u *name, size_t len, cctx_T *cctx)
161{
162 int idx;
163
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100164 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 return -1;
166 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
167 {
168 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
169
170 if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
171 return idx;
172 }
173 return -1;
174}
175
176/*
177 * Lookup a vararg argument in the current function.
178 * Returns TRUE if there is a match.
179 */
180 static int
181lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
182{
183 char_u *va_name = cctx->ctx_ufunc->uf_va_name;
184
185 return len > 0 && va_name != NULL
186 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
187}
188
189/*
190 * Lookup a variable in the current script.
191 * Returns OK or FAIL.
192 */
193 static int
194lookup_script(char_u *name, size_t len)
195{
196 int cc;
197 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
198 dictitem_T *di;
199
200 cc = name[len];
201 name[len] = NUL;
202 di = find_var_in_ht(ht, 0, name, TRUE);
203 name[len] = cc;
204 return di == NULL ? FAIL: OK;
205}
206
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100207/*
208 * Check if "p[len]" is already defined, either in script "import_sid" or in
209 * compilation context "cctx".
210 * Return FAIL and give an error if it defined.
211 */
212 int
213check_defined(char_u *p, int len, cctx_T *cctx)
214{
215 if (lookup_script(p, len) == OK
216 || (cctx != NULL
217 && (lookup_local(p, len, cctx) >= 0
218 || find_imported(p, len, cctx) != NULL)))
219 {
220 semsg("E1073: imported name already defined: %s", p);
221 return FAIL;
222 }
223 return OK;
224}
225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 static type_T *
227get_list_type(type_T *member_type, garray_T *type_list)
228{
229 type_T *type;
230
231 // recognize commonly used types
232 if (member_type->tt_type == VAR_UNKNOWN)
233 return &t_list_any;
Bram Moolenaar436472f2020-02-20 22:54:43 +0100234 if (member_type->tt_type == VAR_VOID)
235 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100236 if (member_type->tt_type == VAR_BOOL)
237 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 if (member_type->tt_type == VAR_NUMBER)
239 return &t_list_number;
240 if (member_type->tt_type == VAR_STRING)
241 return &t_list_string;
242
243 // Not a common type, create a new entry.
244 if (ga_grow(type_list, 1) == FAIL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100245 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 type = ((type_T *)type_list->ga_data) + type_list->ga_len;
247 ++type_list->ga_len;
248 type->tt_type = VAR_LIST;
249 type->tt_member = member_type;
250 return type;
251}
252
253 static type_T *
254get_dict_type(type_T *member_type, garray_T *type_list)
255{
256 type_T *type;
257
258 // recognize commonly used types
259 if (member_type->tt_type == VAR_UNKNOWN)
260 return &t_dict_any;
Bram Moolenaar436472f2020-02-20 22:54:43 +0100261 if (member_type->tt_type == VAR_VOID)
262 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100263 if (member_type->tt_type == VAR_BOOL)
264 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 if (member_type->tt_type == VAR_NUMBER)
266 return &t_dict_number;
267 if (member_type->tt_type == VAR_STRING)
268 return &t_dict_string;
269
270 // Not a common type, create a new entry.
271 if (ga_grow(type_list, 1) == FAIL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100272 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273 type = ((type_T *)type_list->ga_data) + type_list->ga_len;
274 ++type_list->ga_len;
275 type->tt_type = VAR_DICT;
276 type->tt_member = member_type;
277 return type;
278}
279
Bram Moolenaara8c17702020-04-01 21:17:24 +0200280/*
281 * Return the type_T for a typval. Only for primitive types.
282 */
283 static type_T *
284typval2type(typval_T *tv)
285{
286 if (tv->v_type == VAR_NUMBER)
287 return &t_number;
288 if (tv->v_type == VAR_BOOL)
289 return &t_bool;
290 if (tv->v_type == VAR_STRING)
291 return &t_string;
292 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
293 return &t_list_string;
294 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
295 return &t_dict_any;
296 return &t_any;
297}
298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299/////////////////////////////////////////////////////////////////////
300// Following generate_ functions expect the caller to call ga_grow().
301
Bram Moolenaar080457c2020-03-03 21:53:32 +0100302#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
303#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305/*
306 * Generate an instruction without arguments.
307 * Returns a pointer to the new instruction, NULL if failed.
308 */
309 static isn_T *
310generate_instr(cctx_T *cctx, isntype_T isn_type)
311{
312 garray_T *instr = &cctx->ctx_instr;
313 isn_T *isn;
314
Bram Moolenaar080457c2020-03-03 21:53:32 +0100315 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100316 if (ga_grow(instr, 1) == FAIL)
317 return NULL;
318 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
319 isn->isn_type = isn_type;
320 isn->isn_lnum = cctx->ctx_lnum + 1;
321 ++instr->ga_len;
322
323 return isn;
324}
325
326/*
327 * Generate an instruction without arguments.
328 * "drop" will be removed from the stack.
329 * Returns a pointer to the new instruction, NULL if failed.
330 */
331 static isn_T *
332generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
333{
334 garray_T *stack = &cctx->ctx_type_stack;
335
Bram Moolenaar080457c2020-03-03 21:53:32 +0100336 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100337 stack->ga_len -= drop;
338 return generate_instr(cctx, isn_type);
339}
340
341/*
342 * Generate instruction "isn_type" and put "type" on the type stack.
343 */
344 static isn_T *
345generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
346{
347 isn_T *isn;
348 garray_T *stack = &cctx->ctx_type_stack;
349
350 if ((isn = generate_instr(cctx, isn_type)) == NULL)
351 return NULL;
352
353 if (ga_grow(stack, 1) == FAIL)
354 return NULL;
355 ((type_T **)stack->ga_data)[stack->ga_len] = type;
356 ++stack->ga_len;
357
358 return isn;
359}
360
361/*
362 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
363 */
364 static int
365may_generate_2STRING(int offset, cctx_T *cctx)
366{
367 isn_T *isn;
368 garray_T *stack = &cctx->ctx_type_stack;
369 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
370
371 if ((*type)->tt_type == VAR_STRING)
372 return OK;
373 *type = &t_string;
374
375 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
376 return FAIL;
377 isn->isn_arg.number = offset;
378
379 return OK;
380}
381
382 static int
383check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
384{
385 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_UNKNOWN)
386 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
387 || type2 == VAR_UNKNOWN)))
388 {
389 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100390 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 else
392 semsg(_("E1036: %c requires number or float arguments"), *op);
393 return FAIL;
394 }
395 return OK;
396}
397
398/*
399 * Generate an instruction with two arguments. The instruction depends on the
400 * type of the arguments.
401 */
402 static int
403generate_two_op(cctx_T *cctx, char_u *op)
404{
405 garray_T *stack = &cctx->ctx_type_stack;
406 type_T *type1;
407 type_T *type2;
408 vartype_T vartype;
409 isn_T *isn;
410
Bram Moolenaar080457c2020-03-03 21:53:32 +0100411 RETURN_OK_IF_SKIP(cctx);
412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413 // Get the known type of the two items on the stack. If they are matching
414 // use a type-specific instruction. Otherwise fall back to runtime type
415 // checking.
416 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
417 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
418 vartype = VAR_UNKNOWN;
419 if (type1->tt_type == type2->tt_type
420 && (type1->tt_type == VAR_NUMBER
421 || type1->tt_type == VAR_LIST
422#ifdef FEAT_FLOAT
423 || type1->tt_type == VAR_FLOAT
424#endif
425 || type1->tt_type == VAR_BLOB))
426 vartype = type1->tt_type;
427
428 switch (*op)
429 {
430 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100431 && type1->tt_type != VAR_UNKNOWN
432 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 && check_number_or_float(
434 type1->tt_type, type2->tt_type, op) == FAIL)
435 return FAIL;
436 isn = generate_instr_drop(cctx,
437 vartype == VAR_NUMBER ? ISN_OPNR
438 : vartype == VAR_LIST ? ISN_ADDLIST
439 : vartype == VAR_BLOB ? ISN_ADDBLOB
440#ifdef FEAT_FLOAT
441 : vartype == VAR_FLOAT ? ISN_OPFLOAT
442#endif
443 : ISN_OPANY, 1);
444 if (isn != NULL)
445 isn->isn_arg.op.op_type = EXPR_ADD;
446 break;
447
448 case '-':
449 case '*':
450 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
451 op) == FAIL)
452 return FAIL;
453 if (vartype == VAR_NUMBER)
454 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
455#ifdef FEAT_FLOAT
456 else if (vartype == VAR_FLOAT)
457 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
458#endif
459 else
460 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
461 if (isn != NULL)
462 isn->isn_arg.op.op_type = *op == '*'
463 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
464 break;
465
466 case '%': if ((type1->tt_type != VAR_UNKNOWN
467 && type1->tt_type != VAR_NUMBER)
468 || (type2->tt_type != VAR_UNKNOWN
469 && type2->tt_type != VAR_NUMBER))
470 {
471 emsg(_("E1035: % requires number arguments"));
472 return FAIL;
473 }
474 isn = generate_instr_drop(cctx,
475 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
476 if (isn != NULL)
477 isn->isn_arg.op.op_type = EXPR_REM;
478 break;
479 }
480
481 // correct type of result
482 if (vartype == VAR_UNKNOWN)
483 {
484 type_T *type = &t_any;
485
486#ifdef FEAT_FLOAT
487 // float+number and number+float results in float
488 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
489 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
490 type = &t_float;
491#endif
492 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
493 }
494
495 return OK;
496}
497
498/*
499 * Generate an ISN_COMPARE* instruction with a boolean result.
500 */
501 static int
502generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
503{
504 isntype_T isntype = ISN_DROP;
505 isn_T *isn;
506 garray_T *stack = &cctx->ctx_type_stack;
507 vartype_T type1;
508 vartype_T type2;
509
Bram Moolenaar080457c2020-03-03 21:53:32 +0100510 RETURN_OK_IF_SKIP(cctx);
511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 // Get the known type of the two items on the stack. If they are matching
513 // use a type-specific instruction. Otherwise fall back to runtime type
514 // checking.
515 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
516 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
517 if (type1 == type2)
518 {
519 switch (type1)
520 {
521 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
522 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
523 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
524 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
525 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
526 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
527 case VAR_LIST: isntype = ISN_COMPARELIST; break;
528 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
529 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
530 case VAR_PARTIAL: isntype = ISN_COMPAREPARTIAL; break;
531 default: isntype = ISN_COMPAREANY; break;
532 }
533 }
534 else if (type1 == VAR_UNKNOWN || type2 == VAR_UNKNOWN
535 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
536 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
537 isntype = ISN_COMPAREANY;
538
539 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
540 && (isntype == ISN_COMPAREBOOL
541 || isntype == ISN_COMPARESPECIAL
542 || isntype == ISN_COMPARENR
543 || isntype == ISN_COMPAREFLOAT))
544 {
545 semsg(_("E1037: Cannot use \"%s\" with %s"),
546 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
547 return FAIL;
548 }
549 if (isntype == ISN_DROP
550 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
551 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
552 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
553 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
554 && exptype != EXPR_IS && exptype != EXPR_ISNOT
555 && (type1 == VAR_BLOB || type2 == VAR_BLOB
556 || type1 == VAR_LIST || type2 == VAR_LIST))))
557 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100558 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 vartype_name(type1), vartype_name(type2));
560 return FAIL;
561 }
562
563 if ((isn = generate_instr(cctx, isntype)) == NULL)
564 return FAIL;
565 isn->isn_arg.op.op_type = exptype;
566 isn->isn_arg.op.op_ic = ic;
567
568 // takes two arguments, puts one bool back
569 if (stack->ga_len >= 2)
570 {
571 --stack->ga_len;
572 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
573 }
574
575 return OK;
576}
577
578/*
579 * Generate an ISN_2BOOL instruction.
580 */
581 static int
582generate_2BOOL(cctx_T *cctx, int invert)
583{
584 isn_T *isn;
585 garray_T *stack = &cctx->ctx_type_stack;
586
Bram Moolenaar080457c2020-03-03 21:53:32 +0100587 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
589 return FAIL;
590 isn->isn_arg.number = invert;
591
592 // type becomes bool
593 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
594
595 return OK;
596}
597
598 static int
599generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
600{
601 isn_T *isn;
602 garray_T *stack = &cctx->ctx_type_stack;
603
Bram Moolenaar080457c2020-03-03 21:53:32 +0100604 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
606 return FAIL;
607 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
608 isn->isn_arg.type.ct_off = offset;
609
610 // type becomes vartype
611 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
612
613 return OK;
614}
615
616/*
617 * Generate an ISN_PUSHNR instruction.
618 */
619 static int
620generate_PUSHNR(cctx_T *cctx, varnumber_T number)
621{
622 isn_T *isn;
623
Bram Moolenaar080457c2020-03-03 21:53:32 +0100624 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
626 return FAIL;
627 isn->isn_arg.number = number;
628
629 return OK;
630}
631
632/*
633 * Generate an ISN_PUSHBOOL instruction.
634 */
635 static int
636generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
637{
638 isn_T *isn;
639
Bram Moolenaar080457c2020-03-03 21:53:32 +0100640 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
642 return FAIL;
643 isn->isn_arg.number = number;
644
645 return OK;
646}
647
648/*
649 * Generate an ISN_PUSHSPEC instruction.
650 */
651 static int
652generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
653{
654 isn_T *isn;
655
Bram Moolenaar080457c2020-03-03 21:53:32 +0100656 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
658 return FAIL;
659 isn->isn_arg.number = number;
660
661 return OK;
662}
663
664#ifdef FEAT_FLOAT
665/*
666 * Generate an ISN_PUSHF instruction.
667 */
668 static int
669generate_PUSHF(cctx_T *cctx, float_T fnumber)
670{
671 isn_T *isn;
672
Bram Moolenaar080457c2020-03-03 21:53:32 +0100673 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
675 return FAIL;
676 isn->isn_arg.fnumber = fnumber;
677
678 return OK;
679}
680#endif
681
682/*
683 * Generate an ISN_PUSHS instruction.
684 * Consumes "str".
685 */
686 static int
687generate_PUSHS(cctx_T *cctx, char_u *str)
688{
689 isn_T *isn;
690
Bram Moolenaar080457c2020-03-03 21:53:32 +0100691 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
693 return FAIL;
694 isn->isn_arg.string = str;
695
696 return OK;
697}
698
699/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100700 * Generate an ISN_PUSHCHANNEL instruction.
701 * Consumes "channel".
702 */
703 static int
704generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
705{
706 isn_T *isn;
707
Bram Moolenaar080457c2020-03-03 21:53:32 +0100708 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100709 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
710 return FAIL;
711 isn->isn_arg.channel = channel;
712
713 return OK;
714}
715
716/*
717 * Generate an ISN_PUSHJOB instruction.
718 * Consumes "job".
719 */
720 static int
721generate_PUSHJOB(cctx_T *cctx, job_T *job)
722{
723 isn_T *isn;
724
Bram Moolenaar080457c2020-03-03 21:53:32 +0100725 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100726 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100727 return FAIL;
728 isn->isn_arg.job = job;
729
730 return OK;
731}
732
733/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734 * Generate an ISN_PUSHBLOB instruction.
735 * Consumes "blob".
736 */
737 static int
738generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
739{
740 isn_T *isn;
741
Bram Moolenaar080457c2020-03-03 21:53:32 +0100742 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
744 return FAIL;
745 isn->isn_arg.blob = blob;
746
747 return OK;
748}
749
750/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100751 * Generate an ISN_PUSHFUNC instruction with name "name".
752 * Consumes "name".
753 */
754 static int
755generate_PUSHFUNC(cctx_T *cctx, char_u *name)
756{
757 isn_T *isn;
758
Bram Moolenaar080457c2020-03-03 21:53:32 +0100759 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100760 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, &t_func_void)) == NULL)
761 return FAIL;
762 isn->isn_arg.string = name;
763
764 return OK;
765}
766
767/*
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100768 * Generate an ISN_PUSHPARTIAL instruction with partial "part".
769 * Consumes "name".
770 */
771 static int
772generate_PUSHPARTIAL(cctx_T *cctx, partial_T *part)
773{
774 isn_T *isn;
775
Bram Moolenaar080457c2020-03-03 21:53:32 +0100776 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100777 if ((isn = generate_instr_type(cctx, ISN_PUSHPARTIAL,
778 &t_partial_any)) == NULL)
779 return FAIL;
780 isn->isn_arg.partial = part;
781
782 return OK;
783}
784
785/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 * Generate an ISN_STORE instruction.
787 */
788 static int
789generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
790{
791 isn_T *isn;
792
Bram Moolenaar080457c2020-03-03 21:53:32 +0100793 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
795 return FAIL;
796 if (name != NULL)
797 isn->isn_arg.string = vim_strsave(name);
798 else
799 isn->isn_arg.number = idx;
800
801 return OK;
802}
803
804/*
805 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
806 */
807 static int
808generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
809{
810 isn_T *isn;
811
Bram Moolenaar080457c2020-03-03 21:53:32 +0100812 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
814 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100815 isn->isn_arg.storenr.stnr_idx = idx;
816 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817
818 return OK;
819}
820
821/*
822 * Generate an ISN_STOREOPT instruction
823 */
824 static int
825generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
826{
827 isn_T *isn;
828
Bram Moolenaar080457c2020-03-03 21:53:32 +0100829 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
831 return FAIL;
832 isn->isn_arg.storeopt.so_name = vim_strsave(name);
833 isn->isn_arg.storeopt.so_flags = opt_flags;
834
835 return OK;
836}
837
838/*
839 * Generate an ISN_LOAD or similar instruction.
840 */
841 static int
842generate_LOAD(
843 cctx_T *cctx,
844 isntype_T isn_type,
845 int idx,
846 char_u *name,
847 type_T *type)
848{
849 isn_T *isn;
850
Bram Moolenaar080457c2020-03-03 21:53:32 +0100851 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100852 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
853 return FAIL;
854 if (name != NULL)
855 isn->isn_arg.string = vim_strsave(name);
856 else
857 isn->isn_arg.number = idx;
858
859 return OK;
860}
861
862/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100863 * Generate an ISN_LOADV instruction.
864 */
865 static int
866generate_LOADV(
867 cctx_T *cctx,
868 char_u *name,
869 int error)
870{
871 // load v:var
872 int vidx = find_vim_var(name);
873
Bram Moolenaar080457c2020-03-03 21:53:32 +0100874 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100875 if (vidx < 0)
876 {
877 if (error)
878 semsg(_(e_var_notfound), name);
879 return FAIL;
880 }
881
882 // TODO: get actual type
883 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
884}
885
886/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 * Generate an ISN_LOADS instruction.
888 */
889 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100890generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100892 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100894 int sid,
895 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896{
897 isn_T *isn;
898
Bram Moolenaar080457c2020-03-03 21:53:32 +0100899 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100900 if (isn_type == ISN_LOADS)
901 isn = generate_instr_type(cctx, isn_type, type);
902 else
903 isn = generate_instr_drop(cctx, isn_type, 1);
904 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100906 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
907 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908
909 return OK;
910}
911
912/*
913 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
914 */
915 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100916generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 cctx_T *cctx,
918 isntype_T isn_type,
919 int sid,
920 int idx,
921 type_T *type)
922{
923 isn_T *isn;
924
Bram Moolenaar080457c2020-03-03 21:53:32 +0100925 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 if (isn_type == ISN_LOADSCRIPT)
927 isn = generate_instr_type(cctx, isn_type, type);
928 else
929 isn = generate_instr_drop(cctx, isn_type, 1);
930 if (isn == NULL)
931 return FAIL;
932 isn->isn_arg.script.script_sid = sid;
933 isn->isn_arg.script.script_idx = idx;
934 return OK;
935}
936
937/*
938 * Generate an ISN_NEWLIST instruction.
939 */
940 static int
941generate_NEWLIST(cctx_T *cctx, int count)
942{
943 isn_T *isn;
944 garray_T *stack = &cctx->ctx_type_stack;
945 garray_T *type_list = cctx->ctx_type_list;
946 type_T *type;
947 type_T *member;
948
Bram Moolenaar080457c2020-03-03 21:53:32 +0100949 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
951 return FAIL;
952 isn->isn_arg.number = count;
953
954 // drop the value types
955 stack->ga_len -= count;
956
Bram Moolenaar436472f2020-02-20 22:54:43 +0100957 // Use the first value type for the list member type. Use "void" for an
958 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 if (count > 0)
960 member = ((type_T **)stack->ga_data)[stack->ga_len];
961 else
Bram Moolenaar436472f2020-02-20 22:54:43 +0100962 member = &t_void;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 type = get_list_type(member, type_list);
964
965 // add the list type to the type stack
966 if (ga_grow(stack, 1) == FAIL)
967 return FAIL;
968 ((type_T **)stack->ga_data)[stack->ga_len] = type;
969 ++stack->ga_len;
970
971 return OK;
972}
973
974/*
975 * Generate an ISN_NEWDICT instruction.
976 */
977 static int
978generate_NEWDICT(cctx_T *cctx, int count)
979{
980 isn_T *isn;
981 garray_T *stack = &cctx->ctx_type_stack;
982 garray_T *type_list = cctx->ctx_type_list;
983 type_T *type;
984 type_T *member;
985
Bram Moolenaar080457c2020-03-03 21:53:32 +0100986 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
988 return FAIL;
989 isn->isn_arg.number = count;
990
991 // drop the key and value types
992 stack->ga_len -= 2 * count;
993
Bram Moolenaar436472f2020-02-20 22:54:43 +0100994 // Use the first value type for the list member type. Use "void" for an
995 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 if (count > 0)
997 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
998 else
Bram Moolenaar436472f2020-02-20 22:54:43 +0100999 member = &t_void;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000 type = get_dict_type(member, type_list);
1001
1002 // add the dict type to the type stack
1003 if (ga_grow(stack, 1) == FAIL)
1004 return FAIL;
1005 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1006 ++stack->ga_len;
1007
1008 return OK;
1009}
1010
1011/*
1012 * Generate an ISN_FUNCREF instruction.
1013 */
1014 static int
1015generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1016{
1017 isn_T *isn;
1018 garray_T *stack = &cctx->ctx_type_stack;
1019
Bram Moolenaar080457c2020-03-03 21:53:32 +01001020 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1022 return FAIL;
1023 isn->isn_arg.number = dfunc_idx;
1024
1025 if (ga_grow(stack, 1) == FAIL)
1026 return FAIL;
1027 ((type_T **)stack->ga_data)[stack->ga_len] = &t_partial_any;
1028 // TODO: argument and return types
1029 ++stack->ga_len;
1030
1031 return OK;
1032}
1033
1034/*
1035 * Generate an ISN_JUMP instruction.
1036 */
1037 static int
1038generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1039{
1040 isn_T *isn;
1041 garray_T *stack = &cctx->ctx_type_stack;
1042
Bram Moolenaar080457c2020-03-03 21:53:32 +01001043 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1045 return FAIL;
1046 isn->isn_arg.jump.jump_when = when;
1047 isn->isn_arg.jump.jump_where = where;
1048
1049 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1050 --stack->ga_len;
1051
1052 return OK;
1053}
1054
1055 static int
1056generate_FOR(cctx_T *cctx, int loop_idx)
1057{
1058 isn_T *isn;
1059 garray_T *stack = &cctx->ctx_type_stack;
1060
Bram Moolenaar080457c2020-03-03 21:53:32 +01001061 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1063 return FAIL;
1064 isn->isn_arg.forloop.for_idx = loop_idx;
1065
1066 if (ga_grow(stack, 1) == FAIL)
1067 return FAIL;
1068 // type doesn't matter, will be stored next
1069 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1070 ++stack->ga_len;
1071
1072 return OK;
1073}
1074
1075/*
1076 * Generate an ISN_BCALL instruction.
1077 * Return FAIL if the number of arguments is wrong.
1078 */
1079 static int
1080generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1081{
1082 isn_T *isn;
1083 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001084 type_T *argtypes[MAX_FUNC_ARGS];
1085 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086
Bram Moolenaar080457c2020-03-03 21:53:32 +01001087 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 if (check_internal_func(func_idx, argcount) == FAIL)
1089 return FAIL;
1090
1091 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1092 return FAIL;
1093 isn->isn_arg.bfunc.cbf_idx = func_idx;
1094 isn->isn_arg.bfunc.cbf_argcount = argcount;
1095
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001096 for (i = 0; i < argcount; ++i)
1097 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 stack->ga_len -= argcount; // drop the arguments
1100 if (ga_grow(stack, 1) == FAIL)
1101 return FAIL;
1102 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001103 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 ++stack->ga_len; // add return value
1105
1106 return OK;
1107}
1108
1109/*
1110 * Generate an ISN_DCALL or ISN_UCALL instruction.
1111 * Return FAIL if the number of arguments is wrong.
1112 */
1113 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001114generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115{
1116 isn_T *isn;
1117 garray_T *stack = &cctx->ctx_type_stack;
1118 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001119 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120
Bram Moolenaar080457c2020-03-03 21:53:32 +01001121 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 if (argcount > regular_args && !has_varargs(ufunc))
1123 {
1124 semsg(_(e_toomanyarg), ufunc->uf_name);
1125 return FAIL;
1126 }
1127 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1128 {
1129 semsg(_(e_toofewarg), ufunc->uf_name);
1130 return FAIL;
1131 }
1132
1133 // Turn varargs into a list.
1134 if (ufunc->uf_va_name != NULL)
1135 {
1136 int count = argcount - regular_args;
1137
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001138 // If count is negative an empty list will be added after evaluating
1139 // default values for missing optional arguments.
1140 if (count >= 0)
1141 {
1142 generate_NEWLIST(cctx, count);
1143 argcount = regular_args + 1;
1144 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 }
1146
1147 if ((isn = generate_instr(cctx,
1148 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1149 return FAIL;
1150 if (ufunc->uf_dfunc_idx >= 0)
1151 {
1152 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1153 isn->isn_arg.dfunc.cdf_argcount = argcount;
1154 }
1155 else
1156 {
1157 // A user function may be deleted and redefined later, can't use the
1158 // ufunc pointer, need to look it up again at runtime.
1159 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1160 isn->isn_arg.ufunc.cuf_argcount = argcount;
1161 }
1162
1163 stack->ga_len -= argcount; // drop the arguments
1164 if (ga_grow(stack, 1) == FAIL)
1165 return FAIL;
1166 // add return value
1167 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1168 ++stack->ga_len;
1169
1170 return OK;
1171}
1172
1173/*
1174 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1175 */
1176 static int
1177generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1178{
1179 isn_T *isn;
1180 garray_T *stack = &cctx->ctx_type_stack;
1181
Bram Moolenaar080457c2020-03-03 21:53:32 +01001182 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1184 return FAIL;
1185 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1186 isn->isn_arg.ufunc.cuf_argcount = argcount;
1187
1188 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001189 if (ga_grow(stack, 1) == FAIL)
1190 return FAIL;
1191 // add return value
1192 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1193 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194
1195 return OK;
1196}
1197
1198/*
1199 * Generate an ISN_PCALL instruction.
1200 */
1201 static int
1202generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1203{
1204 isn_T *isn;
1205 garray_T *stack = &cctx->ctx_type_stack;
1206
Bram Moolenaar080457c2020-03-03 21:53:32 +01001207 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1209 return FAIL;
1210 isn->isn_arg.pfunc.cpf_top = at_top;
1211 isn->isn_arg.pfunc.cpf_argcount = argcount;
1212
1213 stack->ga_len -= argcount; // drop the arguments
1214
1215 // drop the funcref/partial, get back the return value
1216 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1217
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001218 // If partial is above the arguments it must be cleared and replaced with
1219 // the return value.
1220 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1221 return FAIL;
1222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 return OK;
1224}
1225
1226/*
1227 * Generate an ISN_MEMBER instruction.
1228 */
1229 static int
1230generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1231{
1232 isn_T *isn;
1233 garray_T *stack = &cctx->ctx_type_stack;
1234 type_T *type;
1235
Bram Moolenaar080457c2020-03-03 21:53:32 +01001236 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1238 return FAIL;
1239 isn->isn_arg.string = vim_strnsave(name, (int)len);
1240
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001241 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001243 if (type->tt_type != VAR_DICT && type != &t_any)
1244 {
1245 emsg(_(e_dictreq));
1246 return FAIL;
1247 }
1248 // change dict type to dict member type
1249 if (type->tt_type == VAR_DICT)
1250 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251
1252 return OK;
1253}
1254
1255/*
1256 * Generate an ISN_ECHO instruction.
1257 */
1258 static int
1259generate_ECHO(cctx_T *cctx, int with_white, int count)
1260{
1261 isn_T *isn;
1262
Bram Moolenaar080457c2020-03-03 21:53:32 +01001263 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1265 return FAIL;
1266 isn->isn_arg.echo.echo_with_white = with_white;
1267 isn->isn_arg.echo.echo_count = count;
1268
1269 return OK;
1270}
1271
Bram Moolenaarad39c092020-02-26 18:23:43 +01001272/*
1273 * Generate an ISN_EXECUTE instruction.
1274 */
1275 static int
1276generate_EXECUTE(cctx_T *cctx, int count)
1277{
1278 isn_T *isn;
1279
1280 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1281 return FAIL;
1282 isn->isn_arg.number = count;
1283
1284 return OK;
1285}
1286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 static int
1288generate_EXEC(cctx_T *cctx, char_u *line)
1289{
1290 isn_T *isn;
1291
Bram Moolenaar080457c2020-03-03 21:53:32 +01001292 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1294 return FAIL;
1295 isn->isn_arg.string = vim_strsave(line);
1296 return OK;
1297}
1298
1299static char e_white_both[] =
1300 N_("E1004: white space required before and after '%s'");
1301
1302/*
1303 * Reserve space for a local variable.
1304 * Return the index or -1 if it failed.
1305 */
1306 static int
1307reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1308{
1309 int idx;
1310 lvar_T *lvar;
1311
1312 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1313 {
1314 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1315 return -1;
1316 }
1317
1318 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1319 return -1;
1320 idx = cctx->ctx_locals.ga_len;
1321 if (cctx->ctx_max_local < idx + 1)
1322 cctx->ctx_max_local = idx + 1;
1323 ++cctx->ctx_locals.ga_len;
1324
1325 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1326 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1327 lvar->lv_const = isConst;
1328 lvar->lv_type = type;
1329
1330 return idx;
1331}
1332
1333/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001334 * Remove local variables above "new_top".
1335 */
1336 static void
1337unwind_locals(cctx_T *cctx, int new_top)
1338{
1339 if (cctx->ctx_locals.ga_len > new_top)
1340 {
1341 int idx;
1342 lvar_T *lvar;
1343
1344 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1345 {
1346 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1347 vim_free(lvar->lv_name);
1348 }
1349 }
1350 cctx->ctx_locals.ga_len = new_top;
1351}
1352
1353/*
1354 * Free all local variables.
1355 */
1356 static void
1357free_local(cctx_T *cctx)
1358{
1359 unwind_locals(cctx, 0);
1360 ga_clear(&cctx->ctx_locals);
1361}
1362
1363/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 * Skip over a type definition and return a pointer to just after it.
1365 */
1366 char_u *
1367skip_type(char_u *start)
1368{
1369 char_u *p = start;
1370
1371 while (ASCII_ISALNUM(*p) || *p == '_')
1372 ++p;
1373
1374 // Skip over "<type>"; this is permissive about white space.
1375 if (*skipwhite(p) == '<')
1376 {
1377 p = skipwhite(p);
1378 p = skip_type(skipwhite(p + 1));
1379 p = skipwhite(p);
1380 if (*p == '>')
1381 ++p;
1382 }
1383 return p;
1384}
1385
1386/*
1387 * Parse the member type: "<type>" and return "type" with the member set.
1388 * Use "type_list" if a new type needs to be added.
1389 * Returns NULL in case of failure.
1390 */
1391 static type_T *
1392parse_type_member(char_u **arg, type_T *type, garray_T *type_list)
1393{
1394 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001395 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396
1397 if (**arg != '<')
1398 {
1399 if (*skipwhite(*arg) == '<')
1400 emsg(_("E1007: No white space allowed before <"));
1401 else
1402 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001403 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 }
1405 *arg = skipwhite(*arg + 1);
1406
1407 member_type = parse_type(arg, type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408
1409 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001410 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 {
1412 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001413 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 }
1415 ++*arg;
1416
1417 if (type->tt_type == VAR_LIST)
1418 return get_list_type(member_type, type_list);
1419 return get_dict_type(member_type, type_list);
1420}
1421
1422/*
1423 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001424 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 */
1426 type_T *
1427parse_type(char_u **arg, garray_T *type_list)
1428{
1429 char_u *p = *arg;
1430 size_t len;
1431
1432 // skip over the first word
1433 while (ASCII_ISALNUM(*p) || *p == '_')
1434 ++p;
1435 len = p - *arg;
1436
1437 switch (**arg)
1438 {
1439 case 'a':
1440 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1441 {
1442 *arg += len;
1443 return &t_any;
1444 }
1445 break;
1446 case 'b':
1447 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1448 {
1449 *arg += len;
1450 return &t_bool;
1451 }
1452 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1453 {
1454 *arg += len;
1455 return &t_blob;
1456 }
1457 break;
1458 case 'c':
1459 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1460 {
1461 *arg += len;
1462 return &t_channel;
1463 }
1464 break;
1465 case 'd':
1466 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1467 {
1468 *arg += len;
1469 return parse_type_member(arg, &t_dict_any, type_list);
1470 }
1471 break;
1472 case 'f':
1473 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1474 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001475#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 *arg += len;
1477 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001478#else
1479 emsg(_("E1055: This Vim is not compiled with float support"));
1480 return &t_any;
1481#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 }
1483 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1484 {
1485 *arg += len;
1486 // TODO: arguments and return type
1487 return &t_func_any;
1488 }
1489 break;
1490 case 'j':
1491 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1492 {
1493 *arg += len;
1494 return &t_job;
1495 }
1496 break;
1497 case 'l':
1498 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1499 {
1500 *arg += len;
1501 return parse_type_member(arg, &t_list_any, type_list);
1502 }
1503 break;
1504 case 'n':
1505 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1506 {
1507 *arg += len;
1508 return &t_number;
1509 }
1510 break;
1511 case 'p':
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001512 if (len == 7 && STRNCMP(*arg, "partial", len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 {
1514 *arg += len;
1515 // TODO: arguments and return type
1516 return &t_partial_any;
1517 }
1518 break;
1519 case 's':
1520 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1521 {
1522 *arg += len;
1523 return &t_string;
1524 }
1525 break;
1526 case 'v':
1527 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1528 {
1529 *arg += len;
1530 return &t_void;
1531 }
1532 break;
1533 }
1534
1535 semsg(_("E1010: Type not recognized: %s"), *arg);
1536 return &t_any;
1537}
1538
1539/*
1540 * Check if "type1" and "type2" are exactly the same.
1541 */
1542 static int
1543equal_type(type_T *type1, type_T *type2)
1544{
1545 if (type1->tt_type != type2->tt_type)
1546 return FALSE;
1547 switch (type1->tt_type)
1548 {
1549 case VAR_VOID:
1550 case VAR_UNKNOWN:
1551 case VAR_SPECIAL:
1552 case VAR_BOOL:
1553 case VAR_NUMBER:
1554 case VAR_FLOAT:
1555 case VAR_STRING:
1556 case VAR_BLOB:
1557 case VAR_JOB:
1558 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001559 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 case VAR_LIST:
1561 case VAR_DICT:
1562 return equal_type(type1->tt_member, type2->tt_member);
1563 case VAR_FUNC:
1564 case VAR_PARTIAL:
1565 // TODO; check argument types.
1566 return equal_type(type1->tt_member, type2->tt_member)
1567 && type1->tt_argcount == type2->tt_argcount;
1568 }
1569 return TRUE;
1570}
1571
1572/*
1573 * Find the common type of "type1" and "type2" and put it in "dest".
1574 * "type2" and "dest" may be the same.
1575 */
1576 static void
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001577common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_list)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578{
1579 if (equal_type(type1, type2))
1580 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001581 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 return;
1583 }
1584
1585 if (type1->tt_type == type2->tt_type)
1586 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1588 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001589 type_T *common;
1590
1591 common_type(type1->tt_member, type2->tt_member, &common, type_list);
1592 if (type1->tt_type == VAR_LIST)
1593 *dest = get_list_type(common, type_list);
1594 else
1595 *dest = get_dict_type(common, type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 return;
1597 }
1598 // TODO: VAR_FUNC and VAR_PARTIAL
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001599 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 }
1601
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001602 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603}
1604
1605 char *
1606vartype_name(vartype_T type)
1607{
1608 switch (type)
1609 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001610 case VAR_UNKNOWN: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 case VAR_SPECIAL: return "special";
1613 case VAR_BOOL: return "bool";
1614 case VAR_NUMBER: return "number";
1615 case VAR_FLOAT: return "float";
1616 case VAR_STRING: return "string";
1617 case VAR_BLOB: return "blob";
1618 case VAR_JOB: return "job";
1619 case VAR_CHANNEL: return "channel";
1620 case VAR_LIST: return "list";
1621 case VAR_DICT: return "dict";
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001622 case VAR_FUNC: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623 case VAR_PARTIAL: return "partial";
1624 }
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001625 return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626}
1627
1628/*
1629 * Return the name of a type.
1630 * The result may be in allocated memory, in which case "tofree" is set.
1631 */
1632 char *
1633type_name(type_T *type, char **tofree)
1634{
1635 char *name = vartype_name(type->tt_type);
1636
1637 *tofree = NULL;
1638 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1639 {
1640 char *member_free;
1641 char *member_name = type_name(type->tt_member, &member_free);
1642 size_t len;
1643
1644 len = STRLEN(name) + STRLEN(member_name) + 3;
1645 *tofree = alloc(len);
1646 if (*tofree != NULL)
1647 {
1648 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1649 vim_free(member_free);
1650 return *tofree;
1651 }
1652 }
1653 // TODO: function and partial argument types
1654
1655 return name;
1656}
1657
1658/*
1659 * Find "name" in script-local items of script "sid".
1660 * Returns the index in "sn_var_vals" if found.
1661 * If found but not in "sn_var_vals" returns -1.
1662 * If not found returns -2.
1663 */
1664 int
1665get_script_item_idx(int sid, char_u *name, int check_writable)
1666{
1667 hashtab_T *ht;
1668 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001669 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 int idx;
1671
1672 // First look the name up in the hashtable.
1673 if (sid <= 0 || sid > script_items.ga_len)
1674 return -1;
1675 ht = &SCRIPT_VARS(sid);
1676 di = find_var_in_ht(ht, 0, name, TRUE);
1677 if (di == NULL)
1678 return -2;
1679
1680 // Now find the svar_T index in sn_var_vals.
1681 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1682 {
1683 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1684
1685 if (sv->sv_tv == &di->di_tv)
1686 {
1687 if (check_writable && sv->sv_const)
1688 semsg(_(e_readonlyvar), name);
1689 return idx;
1690 }
1691 }
1692 return -1;
1693}
1694
1695/*
1696 * Find "name" in imported items of the current script/
1697 */
1698 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001699find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001701 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 int idx;
1703
1704 if (cctx != NULL)
1705 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1706 {
1707 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1708 + idx;
1709
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001710 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1711 : STRLEN(import->imp_name) == len
1712 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 return import;
1714 }
1715
1716 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1717 {
1718 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1719
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001720 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1721 : STRLEN(import->imp_name) == len
1722 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 return import;
1724 }
1725 return NULL;
1726}
1727
1728/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001729 * Free all imported variables.
1730 */
1731 static void
1732free_imported(cctx_T *cctx)
1733{
1734 int idx;
1735
1736 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1737 {
1738 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1739
1740 vim_free(import->imp_name);
1741 }
1742 ga_clear(&cctx->ctx_imports);
1743}
1744
1745/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746 * Generate an instruction to load script-local variable "name".
1747 */
1748 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001749compile_load_scriptvar(
1750 cctx_T *cctx,
1751 char_u *name, // variable NUL terminated
1752 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001753 char_u **end, // end of variable
1754 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001756 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1758 imported_T *import;
1759
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001760 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001762 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001763 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1764 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 }
1766 if (idx >= 0)
1767 {
1768 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1769
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001770 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 current_sctx.sc_sid, idx, sv->sv_type);
1772 return OK;
1773 }
1774
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001775 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 if (import != NULL)
1777 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001778 if (import->imp_all)
1779 {
1780 char_u *p = skipwhite(*end);
1781 int name_len;
1782 ufunc_T *ufunc;
1783 type_T *type;
1784
1785 // Used "import * as Name", need to lookup the member.
1786 if (*p != '.')
1787 {
1788 semsg(_("E1060: expected dot after name: %s"), start);
1789 return FAIL;
1790 }
1791 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001792 if (VIM_ISWHITE(*p))
1793 {
1794 emsg(_("E1074: no white space allowed after dot"));
1795 return FAIL;
1796 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001797
1798 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
1799 // TODO: what if it is a function?
1800 if (idx < 0)
1801 return FAIL;
1802 *end = p;
1803
1804 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1805 import->imp_sid,
1806 idx,
1807 type);
1808 }
1809 else
1810 {
1811 // TODO: check this is a variable, not a function
1812 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1813 import->imp_sid,
1814 import->imp_var_vals_idx,
1815 import->imp_type);
1816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 return OK;
1818 }
1819
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001820 if (error)
1821 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 return FAIL;
1823}
1824
1825/*
1826 * Compile a variable name into a load instruction.
1827 * "end" points to just after the name.
1828 * When "error" is FALSE do not give an error when not found.
1829 */
1830 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001831compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832{
1833 type_T *type;
1834 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001835 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001837 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838
1839 if (*(*arg + 1) == ':')
1840 {
1841 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001842 if (end <= *arg + 2)
1843 name = vim_strsave((char_u *)"[empty]");
1844 else
1845 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 if (name == NULL)
1847 return FAIL;
1848
1849 if (**arg == 'v')
1850 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001851 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 }
1853 else if (**arg == 'g')
1854 {
1855 // Global variables can be defined later, thus we don't check if it
1856 // exists, give error at runtime.
1857 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1858 }
1859 else if (**arg == 's')
1860 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001861 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001863 else if (**arg == 'b')
1864 {
1865 semsg("Namespace b: not supported yet: %s", *arg);
1866 goto theend;
1867 }
1868 else if (**arg == 'w')
1869 {
1870 semsg("Namespace w: not supported yet: %s", *arg);
1871 goto theend;
1872 }
1873 else if (**arg == 't')
1874 {
1875 semsg("Namespace t: not supported yet: %s", *arg);
1876 goto theend;
1877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 else
1879 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001880 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 goto theend;
1882 }
1883 }
1884 else
1885 {
1886 size_t len = end - *arg;
1887 int idx;
1888 int gen_load = FALSE;
1889
1890 name = vim_strnsave(*arg, end - *arg);
1891 if (name == NULL)
1892 return FAIL;
1893
1894 idx = lookup_arg(*arg, len, cctx);
1895 if (idx >= 0)
1896 {
1897 if (cctx->ctx_ufunc->uf_arg_types != NULL)
1898 type = cctx->ctx_ufunc->uf_arg_types[idx];
1899 else
1900 type = &t_any;
1901
1902 // Arguments are located above the frame pointer.
1903 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
1904 if (cctx->ctx_ufunc->uf_va_name != NULL)
1905 --idx;
1906 gen_load = TRUE;
1907 }
1908 else if (lookup_vararg(*arg, len, cctx))
1909 {
1910 // varargs is always the last argument
1911 idx = -STACK_FRAME_SIZE - 1;
1912 type = cctx->ctx_ufunc->uf_va_type;
1913 gen_load = TRUE;
1914 }
1915 else
1916 {
1917 idx = lookup_local(*arg, len, cctx);
1918 if (idx >= 0)
1919 {
1920 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
1921 gen_load = TRUE;
1922 }
1923 else
1924 {
1925 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
1926 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
1927 res = generate_PUSHBOOL(cctx, **arg == 't'
1928 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001929 else if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1930 == SCRIPT_VERSION_VIM9)
1931 // in Vim9 script "var" can be script-local.
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001932 res = compile_load_scriptvar(cctx, name, *arg, &end, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 }
1934 }
1935 if (gen_load)
1936 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1937 }
1938
1939 *arg = end;
1940
1941theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001942 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 semsg(_(e_var_notfound), name);
1944 vim_free(name);
1945 return res;
1946}
1947
1948/*
1949 * Compile the argument expressions.
1950 * "arg" points to just after the "(" and is advanced to after the ")"
1951 */
1952 static int
1953compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
1954{
1955 char_u *p = *arg;
1956
1957 while (*p != NUL && *p != ')')
1958 {
1959 if (compile_expr1(&p, cctx) == FAIL)
1960 return FAIL;
1961 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001962
1963 if (*p != ',' && *skipwhite(p) == ',')
1964 {
1965 emsg(_("E1068: No white space allowed before ,"));
1966 p = skipwhite(p);
1967 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001969 {
1970 ++p;
1971 if (!VIM_ISWHITE(*p))
1972 emsg(_("E1069: white space required after ,"));
1973 }
1974 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001976 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 if (*p != ')')
1978 {
1979 emsg(_(e_missing_close));
1980 return FAIL;
1981 }
1982 *arg = p + 1;
1983 return OK;
1984}
1985
1986/*
1987 * Compile a function call: name(arg1, arg2)
1988 * "arg" points to "name", "arg + varlen" to the "(".
1989 * "argcount_init" is 1 for "value->method()"
1990 * Instructions:
1991 * EVAL arg1
1992 * EVAL arg2
1993 * BCALL / DCALL / UCALL
1994 */
1995 static int
1996compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
1997{
1998 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01001999 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 int argcount = argcount_init;
2001 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002002 char_u fname_buf[FLEN_FIXED + 1];
2003 char_u *tofree = NULL;
2004 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002006 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007
2008 if (varlen >= sizeof(namebuf))
2009 {
2010 semsg(_("E1011: name too long: %s"), name);
2011 return FAIL;
2012 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002013 vim_strncpy(namebuf, *arg, varlen);
2014 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015
2016 *arg = skipwhite(*arg + varlen + 1);
2017 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002018 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002020 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 {
2022 int idx;
2023
2024 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002025 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002027 {
2028 res = generate_BCALL(cctx, idx, argcount);
2029 goto theend;
2030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 semsg(_(e_unknownfunc), namebuf);
2032 }
2033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002035 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002037 {
2038 res = generate_CALL(cctx, ufunc, argcount);
2039 goto theend;
2040 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041
2042 // If the name is a variable, load it and use PCALL.
2043 p = namebuf;
2044 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002045 {
2046 res = generate_PCALL(cctx, argcount, FALSE);
2047 goto theend;
2048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049
2050 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002051 res = generate_UCALL(cctx, name, argcount);
2052
2053theend:
2054 vim_free(tofree);
2055 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056}
2057
2058// like NAMESPACE_CHAR but with 'a' and 'l'.
2059#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2060
2061/*
2062 * Find the end of a variable or function name. Unlike find_name_end() this
2063 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002064 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 * Return a pointer to just after the name. Equal to "arg" if there is no
2066 * valid name.
2067 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002068 static char_u *
2069to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070{
2071 char_u *p;
2072
2073 // Quick check for valid starting character.
2074 if (!eval_isnamec1(*arg))
2075 return arg;
2076
2077 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2078 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2079 // and can be used in slice "[n:]".
2080 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002081 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2083 break;
2084 return p;
2085}
2086
2087/*
2088 * Like to_name_end() but also skip over a list or dict constant.
2089 */
2090 char_u *
2091to_name_const_end(char_u *arg)
2092{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002093 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 typval_T rettv;
2095
2096 if (p == arg && *arg == '[')
2097 {
2098
2099 // Can be "[1, 2, 3]->Func()".
2100 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2101 p = arg;
2102 }
2103 else if (p == arg && *arg == '#' && arg[1] == '{')
2104 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002105 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 ++p;
2107 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2108 p = arg;
2109 }
2110 else if (p == arg && *arg == '{')
2111 {
2112 int ret = get_lambda_tv(&p, &rettv, FALSE);
2113
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002114 // Can be "{x -> ret}()".
2115 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 if (ret == NOTDONE)
2117 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2118 if (ret != OK)
2119 p = arg;
2120 }
2121
2122 return p;
2123}
2124
2125 static void
2126type_mismatch(type_T *expected, type_T *actual)
2127{
2128 char *tofree1, *tofree2;
2129
2130 semsg(_("E1013: type mismatch, expected %s but got %s"),
2131 type_name(expected, &tofree1), type_name(actual, &tofree2));
2132 vim_free(tofree1);
2133 vim_free(tofree2);
2134}
2135
2136/*
2137 * Check if the expected and actual types match.
2138 */
2139 static int
2140check_type(type_T *expected, type_T *actual, int give_msg)
2141{
2142 if (expected->tt_type != VAR_UNKNOWN)
2143 {
2144 if (expected->tt_type != actual->tt_type)
2145 {
2146 if (give_msg)
2147 type_mismatch(expected, actual);
2148 return FAIL;
2149 }
2150 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2151 {
Bram Moolenaar436472f2020-02-20 22:54:43 +01002152 int ret;
2153
2154 // void is used for an empty list or dict
2155 if (actual->tt_member == &t_void)
2156 ret = OK;
2157 else
2158 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 if (ret == FAIL && give_msg)
2160 type_mismatch(expected, actual);
2161 return ret;
2162 }
2163 }
2164 return OK;
2165}
2166
2167/*
2168 * Check that
2169 * - "actual" is "expected" type or
2170 * - "actual" is a type that can be "expected" type: add a runtime check; or
2171 * - return FAIL.
2172 */
2173 static int
2174need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2175{
Bram Moolenaar436472f2020-02-20 22:54:43 +01002176 if (check_type(expected, actual, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 return OK;
2178 if (actual->tt_type != VAR_UNKNOWN)
2179 {
2180 type_mismatch(expected, actual);
2181 return FAIL;
2182 }
2183 generate_TYPECHECK(cctx, expected, offset);
2184 return OK;
2185}
2186
2187/*
2188 * parse a list: [expr, expr]
2189 * "*arg" points to the '['.
2190 */
2191 static int
2192compile_list(char_u **arg, cctx_T *cctx)
2193{
2194 char_u *p = skipwhite(*arg + 1);
2195 int count = 0;
2196
2197 while (*p != ']')
2198 {
2199 if (*p == NUL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002200 {
2201 semsg(_(e_list_end), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 return FAIL;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 if (compile_expr1(&p, cctx) == FAIL)
2205 break;
2206 ++count;
2207 if (*p == ',')
2208 ++p;
2209 p = skipwhite(p);
2210 }
2211 *arg = p + 1;
2212
2213 generate_NEWLIST(cctx, count);
2214 return OK;
2215}
2216
2217/*
2218 * parse a lambda: {arg, arg -> expr}
2219 * "*arg" points to the '{'.
2220 */
2221 static int
2222compile_lambda(char_u **arg, cctx_T *cctx)
2223{
2224 garray_T *instr = &cctx->ctx_instr;
2225 typval_T rettv;
2226 ufunc_T *ufunc;
2227
2228 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002229 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002233 ++ufunc->uf_refcount;
2234 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235
2236 // The function will have one line: "return {expr}".
2237 // Compile it into instructions.
2238 compile_def_function(ufunc, TRUE);
2239
2240 if (ufunc->uf_dfunc_idx >= 0)
2241 {
2242 if (ga_grow(instr, 1) == FAIL)
2243 return FAIL;
2244 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2245 return OK;
2246 }
2247 return FAIL;
2248}
2249
2250/*
2251 * Compile a lamda call: expr->{lambda}(args)
2252 * "arg" points to the "{".
2253 */
2254 static int
2255compile_lambda_call(char_u **arg, cctx_T *cctx)
2256{
2257 ufunc_T *ufunc;
2258 typval_T rettv;
2259 int argcount = 1;
2260 int ret = FAIL;
2261
2262 // Get the funcref in "rettv".
2263 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2264 return FAIL;
2265
2266 if (**arg != '(')
2267 {
2268 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002269 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 else
2271 semsg(_(e_missing_paren), "lambda");
2272 clear_tv(&rettv);
2273 return FAIL;
2274 }
2275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 ufunc = rettv.vval.v_partial->pt_func;
2277 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002278 clear_tv(&rettv);
2279
2280 // The function will have one line: "return {expr}".
2281 // Compile it into instructions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 compile_def_function(ufunc, TRUE);
2283
2284 // compile the arguments
2285 *arg = skipwhite(*arg + 1);
2286 if (compile_arguments(arg, cctx, &argcount) == OK)
2287 // call the compiled function
2288 ret = generate_CALL(cctx, ufunc, argcount);
2289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 return ret;
2291}
2292
2293/*
2294 * parse a dict: {'key': val} or #{key: val}
2295 * "*arg" points to the '{'.
2296 */
2297 static int
2298compile_dict(char_u **arg, cctx_T *cctx, int literal)
2299{
2300 garray_T *instr = &cctx->ctx_instr;
2301 int count = 0;
2302 dict_T *d = dict_alloc();
2303 dictitem_T *item;
2304
2305 if (d == NULL)
2306 return FAIL;
2307 *arg = skipwhite(*arg + 1);
2308 while (**arg != '}' && **arg != NUL)
2309 {
2310 char_u *key = NULL;
2311
2312 if (literal)
2313 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002314 char_u *p = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315
2316 if (p == *arg)
2317 {
2318 semsg(_("E1014: Invalid key: %s"), *arg);
2319 return FAIL;
2320 }
2321 key = vim_strnsave(*arg, p - *arg);
2322 if (generate_PUSHS(cctx, key) == FAIL)
2323 return FAIL;
2324 *arg = p;
2325 }
2326 else
2327 {
2328 isn_T *isn;
2329
2330 if (compile_expr1(arg, cctx) == FAIL)
2331 return FAIL;
2332 // TODO: check type is string
2333 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2334 if (isn->isn_type == ISN_PUSHS)
2335 key = isn->isn_arg.string;
2336 }
2337
2338 // Check for duplicate keys, if using string keys.
2339 if (key != NULL)
2340 {
2341 item = dict_find(d, key, -1);
2342 if (item != NULL)
2343 {
2344 semsg(_(e_duplicate_key), key);
2345 goto failret;
2346 }
2347 item = dictitem_alloc(key);
2348 if (item != NULL)
2349 {
2350 item->di_tv.v_type = VAR_UNKNOWN;
2351 item->di_tv.v_lock = 0;
2352 if (dict_add(d, item) == FAIL)
2353 dictitem_free(item);
2354 }
2355 }
2356
2357 *arg = skipwhite(*arg);
2358 if (**arg != ':')
2359 {
2360 semsg(_(e_missing_dict_colon), *arg);
2361 return FAIL;
2362 }
2363
2364 *arg = skipwhite(*arg + 1);
2365 if (compile_expr1(arg, cctx) == FAIL)
2366 return FAIL;
2367 ++count;
2368
2369 if (**arg == '}')
2370 break;
2371 if (**arg != ',')
2372 {
2373 semsg(_(e_missing_dict_comma), *arg);
2374 goto failret;
2375 }
2376 *arg = skipwhite(*arg + 1);
2377 }
2378
2379 if (**arg != '}')
2380 {
2381 semsg(_(e_missing_dict_end), *arg);
2382 goto failret;
2383 }
2384 *arg = *arg + 1;
2385
2386 dict_unref(d);
2387 return generate_NEWDICT(cctx, count);
2388
2389failret:
2390 dict_unref(d);
2391 return FAIL;
2392}
2393
2394/*
2395 * Compile "&option".
2396 */
2397 static int
2398compile_get_option(char_u **arg, cctx_T *cctx)
2399{
2400 typval_T rettv;
2401 char_u *start = *arg;
2402 int ret;
2403
2404 // parse the option and get the current value to get the type.
2405 rettv.v_type = VAR_UNKNOWN;
2406 ret = get_option_tv(arg, &rettv, TRUE);
2407 if (ret == OK)
2408 {
2409 // include the '&' in the name, get_option_tv() expects it.
2410 char_u *name = vim_strnsave(start, *arg - start);
2411 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2412
2413 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2414 vim_free(name);
2415 }
2416 clear_tv(&rettv);
2417
2418 return ret;
2419}
2420
2421/*
2422 * Compile "$VAR".
2423 */
2424 static int
2425compile_get_env(char_u **arg, cctx_T *cctx)
2426{
2427 char_u *start = *arg;
2428 int len;
2429 int ret;
2430 char_u *name;
2431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 ++*arg;
2433 len = get_env_len(arg);
2434 if (len == 0)
2435 {
2436 semsg(_(e_syntax_at), start - 1);
2437 return FAIL;
2438 }
2439
2440 // include the '$' in the name, get_env_tv() expects it.
2441 name = vim_strnsave(start, len + 1);
2442 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2443 vim_free(name);
2444 return ret;
2445}
2446
2447/*
2448 * Compile "@r".
2449 */
2450 static int
2451compile_get_register(char_u **arg, cctx_T *cctx)
2452{
2453 int ret;
2454
2455 ++*arg;
2456 if (**arg == NUL)
2457 {
2458 semsg(_(e_syntax_at), *arg - 1);
2459 return FAIL;
2460 }
2461 if (!valid_yank_reg(**arg, TRUE))
2462 {
2463 emsg_invreg(**arg);
2464 return FAIL;
2465 }
2466 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2467 ++*arg;
2468 return ret;
2469}
2470
2471/*
2472 * Apply leading '!', '-' and '+' to constant "rettv".
2473 */
2474 static int
2475apply_leader(typval_T *rettv, char_u *start, char_u *end)
2476{
2477 char_u *p = end;
2478
2479 // this works from end to start
2480 while (p > start)
2481 {
2482 --p;
2483 if (*p == '-' || *p == '+')
2484 {
2485 // only '-' has an effect, for '+' we only check the type
2486#ifdef FEAT_FLOAT
2487 if (rettv->v_type == VAR_FLOAT)
2488 {
2489 if (*p == '-')
2490 rettv->vval.v_float = -rettv->vval.v_float;
2491 }
2492 else
2493#endif
2494 {
2495 varnumber_T val;
2496 int error = FALSE;
2497
2498 // tv_get_number_chk() accepts a string, but we don't want that
2499 // here
2500 if (check_not_string(rettv) == FAIL)
2501 return FAIL;
2502 val = tv_get_number_chk(rettv, &error);
2503 clear_tv(rettv);
2504 if (error)
2505 return FAIL;
2506 if (*p == '-')
2507 val = -val;
2508 rettv->v_type = VAR_NUMBER;
2509 rettv->vval.v_number = val;
2510 }
2511 }
2512 else
2513 {
2514 int v = tv2bool(rettv);
2515
2516 // '!' is permissive in the type.
2517 clear_tv(rettv);
2518 rettv->v_type = VAR_BOOL;
2519 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2520 }
2521 }
2522 return OK;
2523}
2524
2525/*
2526 * Recognize v: variables that are constants and set "rettv".
2527 */
2528 static void
2529get_vim_constant(char_u **arg, typval_T *rettv)
2530{
2531 if (STRNCMP(*arg, "v:true", 6) == 0)
2532 {
2533 rettv->v_type = VAR_BOOL;
2534 rettv->vval.v_number = VVAL_TRUE;
2535 *arg += 6;
2536 }
2537 else if (STRNCMP(*arg, "v:false", 7) == 0)
2538 {
2539 rettv->v_type = VAR_BOOL;
2540 rettv->vval.v_number = VVAL_FALSE;
2541 *arg += 7;
2542 }
2543 else if (STRNCMP(*arg, "v:null", 6) == 0)
2544 {
2545 rettv->v_type = VAR_SPECIAL;
2546 rettv->vval.v_number = VVAL_NULL;
2547 *arg += 6;
2548 }
2549 else if (STRNCMP(*arg, "v:none", 6) == 0)
2550 {
2551 rettv->v_type = VAR_SPECIAL;
2552 rettv->vval.v_number = VVAL_NONE;
2553 *arg += 6;
2554 }
2555}
2556
2557/*
2558 * Compile code to apply '-', '+' and '!'.
2559 */
2560 static int
2561compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2562{
2563 char_u *p = end;
2564
2565 // this works from end to start
2566 while (p > start)
2567 {
2568 --p;
2569 if (*p == '-' || *p == '+')
2570 {
2571 int negate = *p == '-';
2572 isn_T *isn;
2573
2574 // TODO: check type
2575 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2576 {
2577 --p;
2578 if (*p == '-')
2579 negate = !negate;
2580 }
2581 // only '-' has an effect, for '+' we only check the type
2582 if (negate)
2583 isn = generate_instr(cctx, ISN_NEGATENR);
2584 else
2585 isn = generate_instr(cctx, ISN_CHECKNR);
2586 if (isn == NULL)
2587 return FAIL;
2588 }
2589 else
2590 {
2591 int invert = TRUE;
2592
2593 while (p > start && p[-1] == '!')
2594 {
2595 --p;
2596 invert = !invert;
2597 }
2598 if (generate_2BOOL(cctx, invert) == FAIL)
2599 return FAIL;
2600 }
2601 }
2602 return OK;
2603}
2604
2605/*
2606 * Compile whatever comes after "name" or "name()".
2607 */
2608 static int
2609compile_subscript(
2610 char_u **arg,
2611 cctx_T *cctx,
2612 char_u **start_leader,
2613 char_u *end_leader)
2614{
2615 for (;;)
2616 {
2617 if (**arg == '(')
2618 {
2619 int argcount = 0;
2620
2621 // funcref(arg)
2622 *arg = skipwhite(*arg + 1);
2623 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2624 return FAIL;
2625 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2626 return FAIL;
2627 }
2628 else if (**arg == '-' && (*arg)[1] == '>')
2629 {
2630 char_u *p;
2631
2632 // something->method()
2633 // Apply the '!', '-' and '+' first:
2634 // -1.0->func() works like (-1.0)->func()
2635 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2636 return FAIL;
2637 *start_leader = end_leader; // don't apply again later
2638
2639 *arg = skipwhite(*arg + 2);
2640 if (**arg == '{')
2641 {
2642 // lambda call: list->{lambda}
2643 if (compile_lambda_call(arg, cctx) == FAIL)
2644 return FAIL;
2645 }
2646 else
2647 {
2648 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002649 p = *arg;
2650 if (ASCII_ISALPHA(*p) && p[1] == ':')
2651 p += 2;
2652 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 ;
2654 if (*p != '(')
2655 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002656 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 return FAIL;
2658 }
2659 // TODO: base value may not be the first argument
2660 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2661 return FAIL;
2662 }
2663 }
2664 else if (**arg == '[')
2665 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002666 garray_T *stack;
2667 type_T **typep;
2668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 // list index: list[123]
2670 // TODO: more arguments
2671 // TODO: dict member dict['name']
2672 *arg = skipwhite(*arg + 1);
2673 if (compile_expr1(arg, cctx) == FAIL)
2674 return FAIL;
2675
2676 if (**arg != ']')
2677 {
2678 emsg(_(e_missbrac));
2679 return FAIL;
2680 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002681 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682
2683 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2684 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01002685 stack = &cctx->ctx_type_stack;
2686 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2687 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
2688 {
2689 emsg(_(e_listreq));
2690 return FAIL;
2691 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002692 if ((*typep)->tt_type == VAR_LIST)
2693 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 }
2695 else if (**arg == '.' && (*arg)[1] != '.')
2696 {
2697 char_u *p;
2698
2699 ++*arg;
2700 p = *arg;
2701 // dictionary member: dict.name
2702 if (eval_isnamec1(*p))
2703 while (eval_isnamec(*p))
2704 MB_PTR_ADV(p);
2705 if (p == *arg)
2706 {
2707 semsg(_(e_syntax_at), *arg);
2708 return FAIL;
2709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2711 return FAIL;
2712 *arg = p;
2713 }
2714 else
2715 break;
2716 }
2717
2718 // TODO - see handle_subscript():
2719 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2720 // Don't do this when "Func" is already a partial that was bound
2721 // explicitly (pt_auto is FALSE).
2722
2723 return OK;
2724}
2725
2726/*
2727 * Compile an expression at "*p" and add instructions to "instr".
2728 * "p" is advanced until after the expression, skipping white space.
2729 *
2730 * This is the equivalent of eval1(), eval2(), etc.
2731 */
2732
2733/*
2734 * number number constant
2735 * 0zFFFFFFFF Blob constant
2736 * "string" string constant
2737 * 'string' literal string constant
2738 * &option-name option value
2739 * @r register contents
2740 * identifier variable value
2741 * function() function call
2742 * $VAR environment variable
2743 * (expression) nested expression
2744 * [expr, expr] List
2745 * {key: val, key: val} Dictionary
2746 * #{key: val, key: val} Dictionary with literal keys
2747 *
2748 * Also handle:
2749 * ! in front logical NOT
2750 * - in front unary minus
2751 * + in front unary plus (ignored)
2752 * trailing (arg) funcref/partial call
2753 * trailing [] subscript in String or List
2754 * trailing .name entry in Dictionary
2755 * trailing ->name() method call
2756 */
2757 static int
2758compile_expr7(char_u **arg, cctx_T *cctx)
2759{
2760 typval_T rettv;
2761 char_u *start_leader, *end_leader;
2762 int ret = OK;
2763
2764 /*
2765 * Skip '!', '-' and '+' characters. They are handled later.
2766 */
2767 start_leader = *arg;
2768 while (**arg == '!' || **arg == '-' || **arg == '+')
2769 *arg = skipwhite(*arg + 1);
2770 end_leader = *arg;
2771
2772 rettv.v_type = VAR_UNKNOWN;
2773 switch (**arg)
2774 {
2775 /*
2776 * Number constant.
2777 */
2778 case '0': // also for blob starting with 0z
2779 case '1':
2780 case '2':
2781 case '3':
2782 case '4':
2783 case '5':
2784 case '6':
2785 case '7':
2786 case '8':
2787 case '9':
2788 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
2789 return FAIL;
2790 break;
2791
2792 /*
2793 * String constant: "string".
2794 */
2795 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
2796 return FAIL;
2797 break;
2798
2799 /*
2800 * Literal string constant: 'str''ing'.
2801 */
2802 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
2803 return FAIL;
2804 break;
2805
2806 /*
2807 * Constant Vim variable.
2808 */
2809 case 'v': get_vim_constant(arg, &rettv);
2810 ret = NOTDONE;
2811 break;
2812
2813 /*
2814 * List: [expr, expr]
2815 */
2816 case '[': ret = compile_list(arg, cctx);
2817 break;
2818
2819 /*
2820 * Dictionary: #{key: val, key: val}
2821 */
2822 case '#': if ((*arg)[1] == '{')
2823 {
2824 ++*arg;
2825 ret = compile_dict(arg, cctx, TRUE);
2826 }
2827 else
2828 ret = NOTDONE;
2829 break;
2830
2831 /*
2832 * Lambda: {arg, arg -> expr}
2833 * Dictionary: {'key': val, 'key': val}
2834 */
2835 case '{': {
2836 char_u *start = skipwhite(*arg + 1);
2837
2838 // Find out what comes after the arguments.
2839 ret = get_function_args(&start, '-', NULL,
2840 NULL, NULL, NULL, TRUE);
2841 if (ret != FAIL && *start == '>')
2842 ret = compile_lambda(arg, cctx);
2843 else
2844 ret = compile_dict(arg, cctx, FALSE);
2845 }
2846 break;
2847
2848 /*
2849 * Option value: &name
2850 */
2851 case '&': ret = compile_get_option(arg, cctx);
2852 break;
2853
2854 /*
2855 * Environment variable: $VAR.
2856 */
2857 case '$': ret = compile_get_env(arg, cctx);
2858 break;
2859
2860 /*
2861 * Register contents: @r.
2862 */
2863 case '@': ret = compile_get_register(arg, cctx);
2864 break;
2865 /*
2866 * nested expression: (expression).
2867 */
2868 case '(': *arg = skipwhite(*arg + 1);
2869 ret = compile_expr1(arg, cctx); // recursive!
2870 *arg = skipwhite(*arg);
2871 if (**arg == ')')
2872 ++*arg;
2873 else if (ret == OK)
2874 {
2875 emsg(_(e_missing_close));
2876 ret = FAIL;
2877 }
2878 break;
2879
2880 default: ret = NOTDONE;
2881 break;
2882 }
2883 if (ret == FAIL)
2884 return FAIL;
2885
2886 if (rettv.v_type != VAR_UNKNOWN)
2887 {
2888 // apply the '!', '-' and '+' before the constant
2889 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
2890 {
2891 clear_tv(&rettv);
2892 return FAIL;
2893 }
2894 start_leader = end_leader; // don't apply again below
2895
2896 // push constant
2897 switch (rettv.v_type)
2898 {
2899 case VAR_BOOL:
2900 generate_PUSHBOOL(cctx, rettv.vval.v_number);
2901 break;
2902 case VAR_SPECIAL:
2903 generate_PUSHSPEC(cctx, rettv.vval.v_number);
2904 break;
2905 case VAR_NUMBER:
2906 generate_PUSHNR(cctx, rettv.vval.v_number);
2907 break;
2908#ifdef FEAT_FLOAT
2909 case VAR_FLOAT:
2910 generate_PUSHF(cctx, rettv.vval.v_float);
2911 break;
2912#endif
2913 case VAR_BLOB:
2914 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
2915 rettv.vval.v_blob = NULL;
2916 break;
2917 case VAR_STRING:
2918 generate_PUSHS(cctx, rettv.vval.v_string);
2919 rettv.vval.v_string = NULL;
2920 break;
2921 default:
2922 iemsg("constant type missing");
2923 return FAIL;
2924 }
2925 }
2926 else if (ret == NOTDONE)
2927 {
2928 char_u *p;
2929 int r;
2930
2931 if (!eval_isnamec1(**arg))
2932 {
2933 semsg(_("E1015: Name expected: %s"), *arg);
2934 return FAIL;
2935 }
2936
2937 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002938 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 if (*p == '(')
2940 r = compile_call(arg, p - *arg, cctx, 0);
2941 else
2942 r = compile_load(arg, p, cctx, TRUE);
2943 if (r == FAIL)
2944 return FAIL;
2945 }
2946
2947 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
2948 return FAIL;
2949
2950 // Now deal with prefixed '-', '+' and '!', if not done already.
2951 return compile_leader(cctx, start_leader, end_leader);
2952}
2953
2954/*
2955 * * number multiplication
2956 * / number division
2957 * % number modulo
2958 */
2959 static int
2960compile_expr6(char_u **arg, cctx_T *cctx)
2961{
2962 char_u *op;
2963
2964 // get the first variable
2965 if (compile_expr7(arg, cctx) == FAIL)
2966 return FAIL;
2967
2968 /*
2969 * Repeat computing, until no "*", "/" or "%" is following.
2970 */
2971 for (;;)
2972 {
2973 op = skipwhite(*arg);
2974 if (*op != '*' && *op != '/' && *op != '%')
2975 break;
2976 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
2977 {
2978 char_u buf[3];
2979
2980 vim_strncpy(buf, op, 1);
2981 semsg(_(e_white_both), buf);
2982 }
2983 *arg = skipwhite(op + 1);
2984
2985 // get the second variable
2986 if (compile_expr7(arg, cctx) == FAIL)
2987 return FAIL;
2988
2989 generate_two_op(cctx, op);
2990 }
2991
2992 return OK;
2993}
2994
2995/*
2996 * + number addition
2997 * - number subtraction
2998 * .. string concatenation
2999 */
3000 static int
3001compile_expr5(char_u **arg, cctx_T *cctx)
3002{
3003 char_u *op;
3004 int oplen;
3005
3006 // get the first variable
3007 if (compile_expr6(arg, cctx) == FAIL)
3008 return FAIL;
3009
3010 /*
3011 * Repeat computing, until no "+", "-" or ".." is following.
3012 */
3013 for (;;)
3014 {
3015 op = skipwhite(*arg);
3016 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3017 break;
3018 oplen = (*op == '.' ? 2 : 1);
3019
3020 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
3021 {
3022 char_u buf[3];
3023
3024 vim_strncpy(buf, op, oplen);
3025 semsg(_(e_white_both), buf);
3026 }
3027
3028 *arg = skipwhite(op + oplen);
3029
3030 // get the second variable
3031 if (compile_expr6(arg, cctx) == FAIL)
3032 return FAIL;
3033
3034 if (*op == '.')
3035 {
3036 if (may_generate_2STRING(-2, cctx) == FAIL
3037 || may_generate_2STRING(-1, cctx) == FAIL)
3038 return FAIL;
3039 generate_instr_drop(cctx, ISN_CONCAT, 1);
3040 }
3041 else
3042 generate_two_op(cctx, op);
3043 }
3044
3045 return OK;
3046}
3047
Bram Moolenaar080457c2020-03-03 21:53:32 +01003048 static exptype_T
3049get_compare_type(char_u *p, int *len, int *type_is)
3050{
3051 exptype_T type = EXPR_UNKNOWN;
3052 int i;
3053
3054 switch (p[0])
3055 {
3056 case '=': if (p[1] == '=')
3057 type = EXPR_EQUAL;
3058 else if (p[1] == '~')
3059 type = EXPR_MATCH;
3060 break;
3061 case '!': if (p[1] == '=')
3062 type = EXPR_NEQUAL;
3063 else if (p[1] == '~')
3064 type = EXPR_NOMATCH;
3065 break;
3066 case '>': if (p[1] != '=')
3067 {
3068 type = EXPR_GREATER;
3069 *len = 1;
3070 }
3071 else
3072 type = EXPR_GEQUAL;
3073 break;
3074 case '<': if (p[1] != '=')
3075 {
3076 type = EXPR_SMALLER;
3077 *len = 1;
3078 }
3079 else
3080 type = EXPR_SEQUAL;
3081 break;
3082 case 'i': if (p[1] == 's')
3083 {
3084 // "is" and "isnot"; but not a prefix of a name
3085 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3086 *len = 5;
3087 i = p[*len];
3088 if (!isalnum(i) && i != '_')
3089 {
3090 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3091 *type_is = TRUE;
3092 }
3093 }
3094 break;
3095 }
3096 return type;
3097}
3098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099/*
3100 * expr5a == expr5b
3101 * expr5a =~ expr5b
3102 * expr5a != expr5b
3103 * expr5a !~ expr5b
3104 * expr5a > expr5b
3105 * expr5a >= expr5b
3106 * expr5a < expr5b
3107 * expr5a <= expr5b
3108 * expr5a is expr5b
3109 * expr5a isnot expr5b
3110 *
3111 * Produces instructions:
3112 * EVAL expr5a Push result of "expr5a"
3113 * EVAL expr5b Push result of "expr5b"
3114 * COMPARE one of the compare instructions
3115 */
3116 static int
3117compile_expr4(char_u **arg, cctx_T *cctx)
3118{
3119 exptype_T type = EXPR_UNKNOWN;
3120 char_u *p;
3121 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 int type_is = FALSE;
3123
3124 // get the first variable
3125 if (compile_expr5(arg, cctx) == FAIL)
3126 return FAIL;
3127
3128 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003129 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130
3131 /*
3132 * If there is a comparative operator, use it.
3133 */
3134 if (type != EXPR_UNKNOWN)
3135 {
3136 int ic = FALSE; // Default: do not ignore case
3137
3138 if (type_is && (p[len] == '?' || p[len] == '#'))
3139 {
3140 semsg(_(e_invexpr2), *arg);
3141 return FAIL;
3142 }
3143 // extra question mark appended: ignore case
3144 if (p[len] == '?')
3145 {
3146 ic = TRUE;
3147 ++len;
3148 }
3149 // extra '#' appended: match case (ignored)
3150 else if (p[len] == '#')
3151 ++len;
3152 // nothing appended: match case
3153
3154 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
3155 {
3156 char_u buf[7];
3157
3158 vim_strncpy(buf, p, len);
3159 semsg(_(e_white_both), buf);
3160 }
3161
3162 // get the second variable
3163 *arg = skipwhite(p + len);
3164 if (compile_expr5(arg, cctx) == FAIL)
3165 return FAIL;
3166
3167 generate_COMPARE(cctx, type, ic);
3168 }
3169
3170 return OK;
3171}
3172
3173/*
3174 * Compile || or &&.
3175 */
3176 static int
3177compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3178{
3179 char_u *p = skipwhite(*arg);
3180 int opchar = *op;
3181
3182 if (p[0] == opchar && p[1] == opchar)
3183 {
3184 garray_T *instr = &cctx->ctx_instr;
3185 garray_T end_ga;
3186
3187 /*
3188 * Repeat until there is no following "||" or "&&"
3189 */
3190 ga_init2(&end_ga, sizeof(int), 10);
3191 while (p[0] == opchar && p[1] == opchar)
3192 {
3193 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3194 semsg(_(e_white_both), op);
3195
3196 if (ga_grow(&end_ga, 1) == FAIL)
3197 {
3198 ga_clear(&end_ga);
3199 return FAIL;
3200 }
3201 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3202 ++end_ga.ga_len;
3203 generate_JUMP(cctx, opchar == '|'
3204 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3205
3206 // eval the next expression
3207 *arg = skipwhite(p + 2);
3208 if ((opchar == '|' ? compile_expr3(arg, cctx)
3209 : compile_expr4(arg, cctx)) == FAIL)
3210 {
3211 ga_clear(&end_ga);
3212 return FAIL;
3213 }
3214 p = skipwhite(*arg);
3215 }
3216
3217 // Fill in the end label in all jumps.
3218 while (end_ga.ga_len > 0)
3219 {
3220 isn_T *isn;
3221
3222 --end_ga.ga_len;
3223 isn = ((isn_T *)instr->ga_data)
3224 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3225 isn->isn_arg.jump.jump_where = instr->ga_len;
3226 }
3227 ga_clear(&end_ga);
3228 }
3229
3230 return OK;
3231}
3232
3233/*
3234 * expr4a && expr4a && expr4a logical AND
3235 *
3236 * Produces instructions:
3237 * EVAL expr4a Push result of "expr4a"
3238 * JUMP_AND_KEEP_IF_FALSE end
3239 * EVAL expr4b Push result of "expr4b"
3240 * JUMP_AND_KEEP_IF_FALSE end
3241 * EVAL expr4c Push result of "expr4c"
3242 * end:
3243 */
3244 static int
3245compile_expr3(char_u **arg, cctx_T *cctx)
3246{
3247 // get the first variable
3248 if (compile_expr4(arg, cctx) == FAIL)
3249 return FAIL;
3250
3251 // || and && work almost the same
3252 return compile_and_or(arg, cctx, "&&");
3253}
3254
3255/*
3256 * expr3a || expr3b || expr3c logical OR
3257 *
3258 * Produces instructions:
3259 * EVAL expr3a Push result of "expr3a"
3260 * JUMP_AND_KEEP_IF_TRUE end
3261 * EVAL expr3b Push result of "expr3b"
3262 * JUMP_AND_KEEP_IF_TRUE end
3263 * EVAL expr3c Push result of "expr3c"
3264 * end:
3265 */
3266 static int
3267compile_expr2(char_u **arg, cctx_T *cctx)
3268{
3269 // eval the first expression
3270 if (compile_expr3(arg, cctx) == FAIL)
3271 return FAIL;
3272
3273 // || and && work almost the same
3274 return compile_and_or(arg, cctx, "||");
3275}
3276
3277/*
3278 * Toplevel expression: expr2 ? expr1a : expr1b
3279 *
3280 * Produces instructions:
3281 * EVAL expr2 Push result of "expr"
3282 * JUMP_IF_FALSE alt jump if false
3283 * EVAL expr1a
3284 * JUMP_ALWAYS end
3285 * alt: EVAL expr1b
3286 * end:
3287 */
3288 static int
3289compile_expr1(char_u **arg, cctx_T *cctx)
3290{
3291 char_u *p;
3292
3293 // evaluate the first expression
3294 if (compile_expr2(arg, cctx) == FAIL)
3295 return FAIL;
3296
3297 p = skipwhite(*arg);
3298 if (*p == '?')
3299 {
3300 garray_T *instr = &cctx->ctx_instr;
3301 garray_T *stack = &cctx->ctx_type_stack;
3302 int alt_idx = instr->ga_len;
3303 int end_idx;
3304 isn_T *isn;
3305 type_T *type1;
3306 type_T *type2;
3307
3308 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3309 semsg(_(e_white_both), "?");
3310
3311 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3312
3313 // evaluate the second expression; any type is accepted
3314 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003315 if (compile_expr1(arg, cctx) == FAIL)
3316 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317
3318 // remember the type and drop it
3319 --stack->ga_len;
3320 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3321
3322 end_idx = instr->ga_len;
3323 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3324
3325 // jump here from JUMP_IF_FALSE
3326 isn = ((isn_T *)instr->ga_data) + alt_idx;
3327 isn->isn_arg.jump.jump_where = instr->ga_len;
3328
3329 // Check for the ":".
3330 p = skipwhite(*arg);
3331 if (*p != ':')
3332 {
3333 emsg(_(e_missing_colon));
3334 return FAIL;
3335 }
3336 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3337 semsg(_(e_white_both), ":");
3338
3339 // evaluate the third expression
3340 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003341 if (compile_expr1(arg, cctx) == FAIL)
3342 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343
3344 // If the types differ, the result has a more generic type.
3345 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003346 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347
3348 // jump here from JUMP_ALWAYS
3349 isn = ((isn_T *)instr->ga_data) + end_idx;
3350 isn->isn_arg.jump.jump_where = instr->ga_len;
3351 }
3352 return OK;
3353}
3354
3355/*
3356 * compile "return [expr]"
3357 */
3358 static char_u *
3359compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3360{
3361 char_u *p = arg;
3362 garray_T *stack = &cctx->ctx_type_stack;
3363 type_T *stack_type;
3364
3365 if (*p != NUL && *p != '|' && *p != '\n')
3366 {
3367 // compile return argument into instructions
3368 if (compile_expr1(&p, cctx) == FAIL)
3369 return NULL;
3370
3371 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3372 if (set_return_type)
3373 cctx->ctx_ufunc->uf_ret_type = stack_type;
3374 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3375 == FAIL)
3376 return NULL;
3377 }
3378 else
3379 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003380 // "set_return_type" cannot be TRUE, only used for a lambda which
3381 // always has an argument.
3382 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 {
3384 emsg(_("E1003: Missing return value"));
3385 return NULL;
3386 }
3387
3388 // No argument, return zero.
3389 generate_PUSHNR(cctx, 0);
3390 }
3391
3392 if (generate_instr(cctx, ISN_RETURN) == NULL)
3393 return NULL;
3394
3395 // "return val | endif" is possible
3396 return skipwhite(p);
3397}
3398
3399/*
3400 * Return the length of an assignment operator, or zero if there isn't one.
3401 */
3402 int
3403assignment_len(char_u *p, int *heredoc)
3404{
3405 if (*p == '=')
3406 {
3407 if (p[1] == '<' && p[2] == '<')
3408 {
3409 *heredoc = TRUE;
3410 return 3;
3411 }
3412 return 1;
3413 }
3414 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3415 return 2;
3416 if (STRNCMP(p, "..=", 3) == 0)
3417 return 3;
3418 return 0;
3419}
3420
3421// words that cannot be used as a variable
3422static char *reserved[] = {
3423 "true",
3424 "false",
3425 NULL
3426};
3427
3428/*
3429 * Get a line for "=<<".
3430 * Return a pointer to the line in allocated memory.
3431 * Return NULL for end-of-file or some error.
3432 */
3433 static char_u *
3434heredoc_getline(
3435 int c UNUSED,
3436 void *cookie,
3437 int indent UNUSED,
3438 int do_concat UNUSED)
3439{
3440 cctx_T *cctx = (cctx_T *)cookie;
3441
3442 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003443 {
3444 iemsg("Heredoc got to end");
3445 return NULL;
3446 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 ++cctx->ctx_lnum;
3448 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3449 [cctx->ctx_lnum]);
3450}
3451
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003452typedef enum {
3453 dest_local,
3454 dest_option,
3455 dest_env,
3456 dest_global,
3457 dest_vimvar,
3458 dest_script,
3459 dest_reg,
3460} assign_dest_T;
3461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462/*
3463 * compile "let var [= expr]", "const var = expr" and "var = expr"
3464 * "arg" points to "var".
3465 */
3466 static char_u *
3467compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3468{
3469 char_u *p;
3470 char_u *ret = NULL;
3471 int var_count = 0;
3472 int semicolon = 0;
3473 size_t varlen;
3474 garray_T *instr = &cctx->ctx_instr;
3475 int idx = -1;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003476 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003479 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003481 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 int oplen = 0;
3483 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003484 type_T *type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 lvar_T *lvar;
3486 char_u *name;
3487 char_u *sp;
3488 int has_type = FALSE;
3489 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3490 int instr_count = -1;
3491
3492 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3493 if (p == NULL)
3494 return NULL;
3495 if (var_count > 0)
3496 {
3497 // TODO: let [var, var] = list
3498 emsg("Cannot handle a list yet");
3499 return NULL;
3500 }
3501
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003502 // "a: type" is declaring variable "a" with a type, not "a:".
3503 if (is_decl && p == arg + 2 && p[-1] == ':')
3504 --p;
3505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 varlen = p - arg;
3507 name = vim_strnsave(arg, (int)varlen);
3508 if (name == NULL)
3509 return NULL;
3510
Bram Moolenaar080457c2020-03-03 21:53:32 +01003511 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003513 if (*arg == '&')
3514 {
3515 int cc;
3516 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517
Bram Moolenaar080457c2020-03-03 21:53:32 +01003518 dest = dest_option;
3519 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003521 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003522 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 if (is_decl)
3525 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003526 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 goto theend;
3528 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003529 p = arg;
3530 p = find_option_end(&p, &opt_flags);
3531 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003533 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01003534 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003535 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003536 }
3537 cc = *p;
3538 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003539 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003540 *p = cc;
3541 if (opt_type == -3)
3542 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003543 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003544 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003545 }
3546 if (opt_type == -2 || opt_type == 0)
3547 type = &t_string;
3548 else
3549 type = &t_number; // both number and boolean option
3550 }
3551 else if (*arg == '$')
3552 {
3553 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003554 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003555 if (is_decl)
3556 {
3557 semsg(_("E1065: Cannot declare an environment variable: %s"), name);
3558 goto theend;
3559 }
3560 }
3561 else if (*arg == '@')
3562 {
3563 if (!valid_yank_reg(arg[1], TRUE))
3564 {
3565 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003566 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003567 }
3568 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003569 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003570 if (is_decl)
3571 {
3572 semsg(_("E1066: Cannot declare a register: %s"), name);
3573 goto theend;
3574 }
3575 }
3576 else if (STRNCMP(arg, "g:", 2) == 0)
3577 {
3578 dest = dest_global;
3579 if (is_decl)
3580 {
3581 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3582 goto theend;
3583 }
3584 }
3585 else if (STRNCMP(arg, "v:", 2) == 0)
3586 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02003587 typval_T *vtv;
3588
Bram Moolenaar080457c2020-03-03 21:53:32 +01003589 vimvaridx = find_vim_var(name + 2);
3590 if (vimvaridx < 0)
3591 {
3592 semsg(_(e_var_notfound), arg);
3593 goto theend;
3594 }
3595 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003596 vtv = get_vim_var_tv(vimvaridx);
3597 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003598 if (is_decl)
3599 {
3600 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3601 goto theend;
3602 }
3603 }
3604 else
3605 {
3606 for (idx = 0; reserved[idx] != NULL; ++idx)
3607 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003609 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 goto theend;
3611 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003612
3613 idx = lookup_local(arg, varlen, cctx);
3614 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003616 if (is_decl)
3617 {
3618 semsg(_("E1017: Variable already declared: %s"), name);
3619 goto theend;
3620 }
3621 else
3622 {
3623 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3624 if (lvar->lv_const)
3625 {
3626 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3627 goto theend;
3628 }
3629 }
3630 }
3631 else if (STRNCMP(arg, "s:", 2) == 0
3632 || lookup_script(arg, varlen) == OK
3633 || find_imported(arg, varlen, cctx) != NULL)
3634 {
3635 dest = dest_script;
3636 if (is_decl)
3637 {
3638 semsg(_("E1054: Variable already declared in the script: %s"),
3639 name);
3640 goto theend;
3641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 }
3643 }
3644 }
3645
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003646 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 {
3648 if (is_decl && *p == ':')
3649 {
3650 // parse optional type: "let var: type = expr"
3651 p = skipwhite(p + 1);
3652 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 has_type = TRUE;
3654 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02003655 else if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 {
3657 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3658 type = lvar->lv_type;
3659 }
3660 }
3661
3662 sp = p;
3663 p = skipwhite(p);
3664 op = p;
3665 oplen = assignment_len(p, &heredoc);
3666 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3667 {
3668 char_u buf[4];
3669
3670 vim_strncpy(buf, op, oplen);
3671 semsg(_(e_white_both), buf);
3672 }
3673
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003674 if (oplen == 3 && !heredoc && dest != dest_global
3675 && type->tt_type != VAR_STRING && type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01003677 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 goto theend;
3679 }
3680
3681 // +=, /=, etc. require an existing variable
Bram Moolenaar080457c2020-03-03 21:53:32 +01003682 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 {
3684 if (oplen > 1 && !heredoc)
3685 {
3686 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3687 name);
3688 goto theend;
3689 }
3690
3691 // new local variable
3692 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3693 if (idx < 0)
3694 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003695 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 }
3697
3698 if (heredoc)
3699 {
3700 list_T *l;
3701 listitem_T *li;
3702
3703 // [let] varname =<< [trim] {end}
3704 eap->getline = heredoc_getline;
3705 eap->cookie = cctx;
3706 l = heredoc_get(eap, op + 3);
3707
3708 // Push each line and the create the list.
3709 for (li = l->lv_first; li != NULL; li = li->li_next)
3710 {
3711 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3712 li->li_tv.vval.v_string = NULL;
3713 }
3714 generate_NEWLIST(cctx, l->lv_len);
3715 type = &t_list_string;
3716 list_free(l);
3717 p += STRLEN(p);
3718 }
3719 else if (oplen > 0)
3720 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02003721 int r;
3722 type_T *stacktype;
3723 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 // for "+=", "*=", "..=" etc. first load the current value
3726 if (*op != '=')
3727 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003728 switch (dest)
3729 {
3730 case dest_option:
3731 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02003732 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003733 break;
3734 case dest_global:
3735 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3736 break;
3737 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003738 compile_load_scriptvar(cctx,
3739 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003740 break;
3741 case dest_env:
3742 // Include $ in the name here
3743 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
3744 break;
3745 case dest_reg:
3746 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
3747 break;
3748 case dest_vimvar:
3749 generate_LOADV(cctx, name + 2, TRUE);
3750 break;
3751 case dest_local:
3752 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3753 break;
3754 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 }
3756
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003757 // Compile the expression. Temporarily hide the new local variable
3758 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02003759 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003760 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 instr_count = instr->ga_len;
3762 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003763 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02003764 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003765 ++cctx->ctx_locals.ga_len;
3766 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 goto theend;
3768
Bram Moolenaara8c17702020-04-01 21:17:24 +02003769 stack = &cctx->ctx_type_stack;
3770 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 if (idx >= 0 && (is_decl || !has_type))
3772 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3774 if (!has_type)
3775 {
3776 if (stacktype->tt_type == VAR_VOID)
3777 {
3778 emsg(_("E1031: Cannot use void value"));
3779 goto theend;
3780 }
3781 else
3782 lvar->lv_type = stacktype;
3783 }
3784 else
3785 if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
3786 goto theend;
3787 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02003788 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
3789 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 }
3791 else if (cmdidx == CMD_const)
3792 {
3793 emsg(_("E1021: const requires a value"));
3794 goto theend;
3795 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003796 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 {
3798 emsg(_("E1022: type or initialization required"));
3799 goto theend;
3800 }
3801 else
3802 {
3803 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 if (ga_grow(instr, 1) == FAIL)
3805 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01003806 switch (type->tt_type)
3807 {
3808 case VAR_BOOL:
3809 generate_PUSHBOOL(cctx, VVAL_FALSE);
3810 break;
3811 case VAR_SPECIAL:
3812 generate_PUSHSPEC(cctx, VVAL_NONE);
3813 break;
3814 case VAR_FLOAT:
3815#ifdef FEAT_FLOAT
3816 generate_PUSHF(cctx, 0.0);
3817#endif
3818 break;
3819 case VAR_STRING:
3820 generate_PUSHS(cctx, NULL);
3821 break;
3822 case VAR_BLOB:
3823 generate_PUSHBLOB(cctx, NULL);
3824 break;
3825 case VAR_FUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003826 generate_PUSHFUNC(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003827 break;
3828 case VAR_PARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003829 generate_PUSHPARTIAL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003830 break;
3831 case VAR_LIST:
3832 generate_NEWLIST(cctx, 0);
3833 break;
3834 case VAR_DICT:
3835 generate_NEWDICT(cctx, 0);
3836 break;
3837 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003838 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003839 break;
3840 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003841 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003842 break;
3843 case VAR_NUMBER:
3844 case VAR_UNKNOWN:
3845 case VAR_VOID:
3846 generate_PUSHNR(cctx, 0);
3847 break;
3848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 }
3850
3851 if (oplen > 0 && *op != '=')
3852 {
3853 type_T *expected = &t_number;
3854 garray_T *stack = &cctx->ctx_type_stack;
3855 type_T *stacktype;
3856
3857 // TODO: if type is known use float or any operation
3858
3859 if (*op == '.')
3860 expected = &t_string;
3861 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3862 if (need_type(stacktype, expected, -1, cctx) == FAIL)
3863 goto theend;
3864
3865 if (*op == '.')
3866 generate_instr_drop(cctx, ISN_CONCAT, 1);
3867 else
3868 {
3869 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3870
3871 if (isn == NULL)
3872 goto theend;
3873 switch (*op)
3874 {
3875 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3876 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3877 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3878 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3879 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3880 }
3881 }
3882 }
3883
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003884 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003886 case dest_option:
3887 generate_STOREOPT(cctx, name + 1, opt_flags);
3888 break;
3889 case dest_global:
3890 // include g: with the name, easier to execute that way
3891 generate_STORE(cctx, ISN_STOREG, 0, name);
3892 break;
3893 case dest_env:
3894 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
3895 break;
3896 case dest_reg:
3897 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
3898 break;
3899 case dest_vimvar:
3900 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
3901 break;
3902 case dest_script:
3903 {
3904 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
3905 imported_T *import = NULL;
3906 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003907
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003908 if (name[1] != ':')
3909 {
3910 import = find_imported(name, 0, cctx);
3911 if (import != NULL)
3912 sid = import->imp_sid;
3913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003915 idx = get_script_item_idx(sid, rawname, TRUE);
3916 // TODO: specific type
3917 if (idx < 0)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003918 generate_OLDSCRIPT(cctx, ISN_STORES, name, sid, &t_any);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003919 else
3920 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
3921 sid, idx, &t_any);
3922 }
3923 break;
3924 case dest_local:
3925 {
3926 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003928 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
3929 // into ISN_STORENR
3930 if (instr->ga_len == instr_count + 1
3931 && isn->isn_type == ISN_PUSHNR)
3932 {
3933 varnumber_T val = isn->isn_arg.number;
3934 garray_T *stack = &cctx->ctx_type_stack;
3935
3936 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003937 isn->isn_arg.storenr.stnr_idx = idx;
3938 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003939 if (stack->ga_len > 0)
3940 --stack->ga_len;
3941 }
3942 else
3943 generate_STORE(cctx, ISN_STORE, idx, NULL);
3944 }
3945 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 }
3947 ret = p;
3948
3949theend:
3950 vim_free(name);
3951 return ret;
3952}
3953
3954/*
3955 * Compile an :import command.
3956 */
3957 static char_u *
3958compile_import(char_u *arg, cctx_T *cctx)
3959{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01003960 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961}
3962
3963/*
3964 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3965 */
3966 static int
3967compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3968{
3969 garray_T *instr = &cctx->ctx_instr;
3970 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3971
3972 if (endlabel == NULL)
3973 return FAIL;
3974 endlabel->el_next = *el;
3975 *el = endlabel;
3976 endlabel->el_end_label = instr->ga_len;
3977
3978 generate_JUMP(cctx, when, 0);
3979 return OK;
3980}
3981
3982 static void
3983compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3984{
3985 garray_T *instr = &cctx->ctx_instr;
3986
3987 while (*el != NULL)
3988 {
3989 endlabel_T *cur = (*el);
3990 isn_T *isn;
3991
3992 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3993 isn->isn_arg.jump.jump_where = instr->ga_len;
3994 *el = cur->el_next;
3995 vim_free(cur);
3996 }
3997}
3998
3999/*
4000 * Create a new scope and set up the generic items.
4001 */
4002 static scope_T *
4003new_scope(cctx_T *cctx, scopetype_T type)
4004{
4005 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4006
4007 if (scope == NULL)
4008 return NULL;
4009 scope->se_outer = cctx->ctx_scope;
4010 cctx->ctx_scope = scope;
4011 scope->se_type = type;
4012 scope->se_local_count = cctx->ctx_locals.ga_len;
4013 return scope;
4014}
4015
4016/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004017 * Free the current scope and go back to the outer scope.
4018 */
4019 static void
4020drop_scope(cctx_T *cctx)
4021{
4022 scope_T *scope = cctx->ctx_scope;
4023
4024 if (scope == NULL)
4025 {
4026 iemsg("calling drop_scope() without a scope");
4027 return;
4028 }
4029 cctx->ctx_scope = scope->se_outer;
4030 vim_free(scope);
4031}
4032
4033/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004034 * Evaluate an expression that is a constant:
4035 * has(arg)
4036 *
4037 * Also handle:
4038 * ! in front logical NOT
4039 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004040 * Return FAIL if the expression is not a constant.
4041 */
4042 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004043evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004044{
4045 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004046 char_u *start_leader, *end_leader;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004047
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004048 /*
4049 * Skip '!' characters. They are handled later.
4050 */
4051 start_leader = *arg;
4052 while (**arg == '!')
4053 *arg = skipwhite(*arg + 1);
4054 end_leader = *arg;
4055
4056 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004057 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004058 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004059 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4060 {
4061 tv->v_type = VAR_SPECIAL;
4062 tv->vval.v_number = VVAL_TRUE;
4063 *arg += 4;
4064 return OK;
4065 }
4066 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4067 {
4068 tv->v_type = VAR_SPECIAL;
4069 tv->vval.v_number = VVAL_FALSE;
4070 *arg += 5;
4071 return OK;
4072 }
4073
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004074 if (STRNCMP("has(", *arg, 4) != 0)
4075 return FAIL;
4076 *arg = skipwhite(*arg + 4);
4077
4078 if (**arg == '"')
4079 {
4080 if (get_string_tv(arg, tv, TRUE) == FAIL)
4081 return FAIL;
4082 }
4083 else if (**arg == '\'')
4084 {
4085 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4086 return FAIL;
4087 }
4088 else
4089 return FAIL;
4090
4091 *arg = skipwhite(*arg);
4092 if (**arg != ')')
4093 return FAIL;
4094 *arg = skipwhite(*arg + 1);
4095
4096 argvars[0] = *tv;
4097 argvars[1].v_type = VAR_UNKNOWN;
4098 tv->v_type = VAR_NUMBER;
4099 tv->vval.v_number = 0;
4100 f_has(argvars, tv);
4101 clear_tv(&argvars[0]);
4102
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004103 while (start_leader < end_leader)
4104 {
4105 if (*start_leader == '!')
4106 tv->vval.v_number = !tv->vval.v_number;
4107 ++start_leader;
4108 }
4109
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004110 return OK;
4111}
4112
Bram Moolenaar080457c2020-03-03 21:53:32 +01004113 static int
4114evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4115{
4116 exptype_T type = EXPR_UNKNOWN;
4117 char_u *p;
4118 int len = 2;
4119 int type_is = FALSE;
4120
4121 // get the first variable
4122 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4123 return FAIL;
4124
4125 p = skipwhite(*arg);
4126 type = get_compare_type(p, &len, &type_is);
4127
4128 /*
4129 * If there is a comparative operator, use it.
4130 */
4131 if (type != EXPR_UNKNOWN)
4132 {
4133 // TODO
4134 return FAIL;
4135 }
4136
4137 return OK;
4138}
4139
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004140static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4141
4142/*
4143 * Compile constant || or &&.
4144 */
4145 static int
4146evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4147{
4148 char_u *p = skipwhite(*arg);
4149 int opchar = *op;
4150
4151 if (p[0] == opchar && p[1] == opchar)
4152 {
4153 int val = tv2bool(tv);
4154
4155 /*
4156 * Repeat until there is no following "||" or "&&"
4157 */
4158 while (p[0] == opchar && p[1] == opchar)
4159 {
4160 typval_T tv2;
4161
4162 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4163 return FAIL;
4164
4165 // eval the next expression
4166 *arg = skipwhite(p + 2);
4167 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004168 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004169 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004170 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004171 {
4172 clear_tv(&tv2);
4173 return FAIL;
4174 }
4175 if ((opchar == '&') == val)
4176 {
4177 // false || tv2 or true && tv2: use tv2
4178 clear_tv(tv);
4179 *tv = tv2;
4180 val = tv2bool(tv);
4181 }
4182 else
4183 clear_tv(&tv2);
4184 p = skipwhite(*arg);
4185 }
4186 }
4187
4188 return OK;
4189}
4190
4191/*
4192 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4193 * Return FAIL if the expression is not a constant.
4194 */
4195 static int
4196evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4197{
4198 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004199 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004200 return FAIL;
4201
4202 // || and && work almost the same
4203 return evaluate_const_and_or(arg, cctx, "&&", tv);
4204}
4205
4206/*
4207 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4208 * Return FAIL if the expression is not a constant.
4209 */
4210 static int
4211evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4212{
4213 // evaluate the first expression
4214 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4215 return FAIL;
4216
4217 // || and && work almost the same
4218 return evaluate_const_and_or(arg, cctx, "||", tv);
4219}
4220
4221/*
4222 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4223 * E.g. for "has('feature')".
4224 * This does not produce error messages. "tv" should be cleared afterwards.
4225 * Return FAIL if the expression is not a constant.
4226 */
4227 static int
4228evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4229{
4230 char_u *p;
4231
4232 // evaluate the first expression
4233 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4234 return FAIL;
4235
4236 p = skipwhite(*arg);
4237 if (*p == '?')
4238 {
4239 int val = tv2bool(tv);
4240 typval_T tv2;
4241
4242 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4243 return FAIL;
4244
4245 // evaluate the second expression; any type is accepted
4246 clear_tv(tv);
4247 *arg = skipwhite(p + 1);
4248 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4249 return FAIL;
4250
4251 // Check for the ":".
4252 p = skipwhite(*arg);
4253 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4254 return FAIL;
4255
4256 // evaluate the third expression
4257 *arg = skipwhite(p + 1);
4258 tv2.v_type = VAR_UNKNOWN;
4259 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4260 {
4261 clear_tv(&tv2);
4262 return FAIL;
4263 }
4264 if (val)
4265 {
4266 // use the expr after "?"
4267 clear_tv(&tv2);
4268 }
4269 else
4270 {
4271 // use the expr after ":"
4272 clear_tv(tv);
4273 *tv = tv2;
4274 }
4275 }
4276 return OK;
4277}
4278
4279/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 * compile "if expr"
4281 *
4282 * "if expr" Produces instructions:
4283 * EVAL expr Push result of "expr"
4284 * JUMP_IF_FALSE end
4285 * ... body ...
4286 * end:
4287 *
4288 * "if expr | else" Produces instructions:
4289 * EVAL expr Push result of "expr"
4290 * JUMP_IF_FALSE else
4291 * ... body ...
4292 * JUMP_ALWAYS end
4293 * else:
4294 * ... body ...
4295 * end:
4296 *
4297 * "if expr1 | elseif expr2 | else" Produces instructions:
4298 * EVAL expr Push result of "expr"
4299 * JUMP_IF_FALSE elseif
4300 * ... body ...
4301 * JUMP_ALWAYS end
4302 * elseif:
4303 * EVAL expr Push result of "expr"
4304 * JUMP_IF_FALSE else
4305 * ... body ...
4306 * JUMP_ALWAYS end
4307 * else:
4308 * ... body ...
4309 * end:
4310 */
4311 static char_u *
4312compile_if(char_u *arg, cctx_T *cctx)
4313{
4314 char_u *p = arg;
4315 garray_T *instr = &cctx->ctx_instr;
4316 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004317 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004319 // compile "expr"; if we know it evaluates to FALSE skip the block
4320 tv.v_type = VAR_UNKNOWN;
4321 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4322 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4323 else
4324 cctx->ctx_skip = MAYBE;
4325 clear_tv(&tv);
4326 if (cctx->ctx_skip == MAYBE)
4327 {
4328 p = arg;
4329 if (compile_expr1(&p, cctx) == FAIL)
4330 return NULL;
4331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332
4333 scope = new_scope(cctx, IF_SCOPE);
4334 if (scope == NULL)
4335 return NULL;
4336
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004337 if (cctx->ctx_skip == MAYBE)
4338 {
4339 // "where" is set when ":elseif", "else" or ":endif" is found
4340 scope->se_u.se_if.is_if_label = instr->ga_len;
4341 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4342 }
4343 else
4344 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345
4346 return p;
4347}
4348
4349 static char_u *
4350compile_elseif(char_u *arg, cctx_T *cctx)
4351{
4352 char_u *p = arg;
4353 garray_T *instr = &cctx->ctx_instr;
4354 isn_T *isn;
4355 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004356 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357
4358 if (scope == NULL || scope->se_type != IF_SCOPE)
4359 {
4360 emsg(_(e_elseif_without_if));
4361 return NULL;
4362 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004363 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364
Bram Moolenaar158906c2020-02-06 20:39:45 +01004365 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004366 {
4367 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004369 return NULL;
4370 // previous "if" or "elseif" jumps here
4371 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4372 isn->isn_arg.jump.jump_where = instr->ga_len;
4373 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004375 // compile "expr"; if we know it evaluates to FALSE skip the block
4376 tv.v_type = VAR_UNKNOWN;
4377 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4378 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4379 else
4380 cctx->ctx_skip = MAYBE;
4381 clear_tv(&tv);
4382 if (cctx->ctx_skip == MAYBE)
4383 {
4384 p = arg;
4385 if (compile_expr1(&p, cctx) == FAIL)
4386 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004388 // "where" is set when ":elseif", "else" or ":endif" is found
4389 scope->se_u.se_if.is_if_label = instr->ga_len;
4390 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4391 }
4392 else
4393 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394
4395 return p;
4396}
4397
4398 static char_u *
4399compile_else(char_u *arg, cctx_T *cctx)
4400{
4401 char_u *p = arg;
4402 garray_T *instr = &cctx->ctx_instr;
4403 isn_T *isn;
4404 scope_T *scope = cctx->ctx_scope;
4405
4406 if (scope == NULL || scope->se_type != IF_SCOPE)
4407 {
4408 emsg(_(e_else_without_if));
4409 return NULL;
4410 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004411 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004413 // jump from previous block to the end, unless the else block is empty
4414 if (cctx->ctx_skip == MAYBE)
4415 {
4416 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004418 return NULL;
4419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420
Bram Moolenaar158906c2020-02-06 20:39:45 +01004421 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004422 {
4423 if (scope->se_u.se_if.is_if_label >= 0)
4424 {
4425 // previous "if" or "elseif" jumps here
4426 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4427 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004428 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004429 }
4430 }
4431
4432 if (cctx->ctx_skip != MAYBE)
4433 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434
4435 return p;
4436}
4437
4438 static char_u *
4439compile_endif(char_u *arg, cctx_T *cctx)
4440{
4441 scope_T *scope = cctx->ctx_scope;
4442 ifscope_T *ifscope;
4443 garray_T *instr = &cctx->ctx_instr;
4444 isn_T *isn;
4445
4446 if (scope == NULL || scope->se_type != IF_SCOPE)
4447 {
4448 emsg(_(e_endif_without_if));
4449 return NULL;
4450 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004451 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004452 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004454 if (scope->se_u.se_if.is_if_label >= 0)
4455 {
4456 // previous "if" or "elseif" jumps here
4457 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4458 isn->isn_arg.jump.jump_where = instr->ga_len;
4459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 // Fill in the "end" label in jumps at the end of the blocks.
4461 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004462 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004464 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 return arg;
4466}
4467
4468/*
4469 * compile "for var in expr"
4470 *
4471 * Produces instructions:
4472 * PUSHNR -1
4473 * STORE loop-idx Set index to -1
4474 * EVAL expr Push result of "expr"
4475 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4476 * - if beyond end, jump to "end"
4477 * - otherwise get item from list and push it
4478 * STORE var Store item in "var"
4479 * ... body ...
4480 * JUMP top Jump back to repeat
4481 * end: DROP Drop the result of "expr"
4482 *
4483 */
4484 static char_u *
4485compile_for(char_u *arg, cctx_T *cctx)
4486{
4487 char_u *p;
4488 size_t varlen;
4489 garray_T *instr = &cctx->ctx_instr;
4490 garray_T *stack = &cctx->ctx_type_stack;
4491 scope_T *scope;
4492 int loop_idx; // index of loop iteration variable
4493 int var_idx; // index of "var"
4494 type_T *vartype;
4495
4496 // TODO: list of variables: "for [key, value] in dict"
4497 // parse "var"
4498 for (p = arg; eval_isnamec1(*p); ++p)
4499 ;
4500 varlen = p - arg;
4501 var_idx = lookup_local(arg, varlen, cctx);
4502 if (var_idx >= 0)
4503 {
4504 semsg(_("E1023: variable already defined: %s"), arg);
4505 return NULL;
4506 }
4507
4508 // consume "in"
4509 p = skipwhite(p);
4510 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4511 {
4512 emsg(_(e_missing_in));
4513 return NULL;
4514 }
4515 p = skipwhite(p + 2);
4516
4517
4518 scope = new_scope(cctx, FOR_SCOPE);
4519 if (scope == NULL)
4520 return NULL;
4521
4522 // Reserve a variable to store the loop iteration counter.
4523 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4524 if (loop_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004525 {
4526 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004528 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529
4530 // Reserve a variable to store "var"
4531 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4532 if (var_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004533 {
4534 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004536 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537
4538 generate_STORENR(cctx, loop_idx, -1);
4539
4540 // compile "expr", it remains on the stack until "endfor"
4541 arg = p;
4542 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004543 {
4544 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004546 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547
4548 // now we know the type of "var"
4549 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4550 if (vartype->tt_type != VAR_LIST)
4551 {
4552 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004553 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 return NULL;
4555 }
4556 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
4557 {
4558 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4559
4560 lvar->lv_type = vartype->tt_member;
4561 }
4562
4563 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004564 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565
4566 generate_FOR(cctx, loop_idx);
4567 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4568
4569 return arg;
4570}
4571
4572/*
4573 * compile "endfor"
4574 */
4575 static char_u *
4576compile_endfor(char_u *arg, cctx_T *cctx)
4577{
4578 garray_T *instr = &cctx->ctx_instr;
4579 scope_T *scope = cctx->ctx_scope;
4580 forscope_T *forscope;
4581 isn_T *isn;
4582
4583 if (scope == NULL || scope->se_type != FOR_SCOPE)
4584 {
4585 emsg(_(e_for));
4586 return NULL;
4587 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004588 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004590 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591
4592 // At end of ":for" scope jump back to the FOR instruction.
4593 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4594
4595 // Fill in the "end" label in the FOR statement so it can jump here
4596 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4597 isn->isn_arg.forloop.for_end = instr->ga_len;
4598
4599 // Fill in the "end" label any BREAK statements
4600 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4601
4602 // Below the ":for" scope drop the "expr" list from the stack.
4603 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4604 return NULL;
4605
4606 vim_free(scope);
4607
4608 return arg;
4609}
4610
4611/*
4612 * compile "while expr"
4613 *
4614 * Produces instructions:
4615 * top: EVAL expr Push result of "expr"
4616 * JUMP_IF_FALSE end jump if false
4617 * ... body ...
4618 * JUMP top Jump back to repeat
4619 * end:
4620 *
4621 */
4622 static char_u *
4623compile_while(char_u *arg, cctx_T *cctx)
4624{
4625 char_u *p = arg;
4626 garray_T *instr = &cctx->ctx_instr;
4627 scope_T *scope;
4628
4629 scope = new_scope(cctx, WHILE_SCOPE);
4630 if (scope == NULL)
4631 return NULL;
4632
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004633 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634
4635 // compile "expr"
4636 if (compile_expr1(&p, cctx) == FAIL)
4637 return NULL;
4638
4639 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004640 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 JUMP_IF_FALSE, cctx) == FAIL)
4642 return FAIL;
4643
4644 return p;
4645}
4646
4647/*
4648 * compile "endwhile"
4649 */
4650 static char_u *
4651compile_endwhile(char_u *arg, cctx_T *cctx)
4652{
4653 scope_T *scope = cctx->ctx_scope;
4654
4655 if (scope == NULL || scope->se_type != WHILE_SCOPE)
4656 {
4657 emsg(_(e_while));
4658 return NULL;
4659 }
4660 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004661 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662
4663 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004664 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665
4666 // Fill in the "end" label in the WHILE statement so it can jump here.
4667 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004668 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669
4670 vim_free(scope);
4671
4672 return arg;
4673}
4674
4675/*
4676 * compile "continue"
4677 */
4678 static char_u *
4679compile_continue(char_u *arg, cctx_T *cctx)
4680{
4681 scope_T *scope = cctx->ctx_scope;
4682
4683 for (;;)
4684 {
4685 if (scope == NULL)
4686 {
4687 emsg(_(e_continue));
4688 return NULL;
4689 }
4690 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4691 break;
4692 scope = scope->se_outer;
4693 }
4694
4695 // Jump back to the FOR or WHILE instruction.
4696 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004697 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
4698 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 return arg;
4700}
4701
4702/*
4703 * compile "break"
4704 */
4705 static char_u *
4706compile_break(char_u *arg, cctx_T *cctx)
4707{
4708 scope_T *scope = cctx->ctx_scope;
4709 endlabel_T **el;
4710
4711 for (;;)
4712 {
4713 if (scope == NULL)
4714 {
4715 emsg(_(e_break));
4716 return NULL;
4717 }
4718 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4719 break;
4720 scope = scope->se_outer;
4721 }
4722
4723 // Jump to the end of the FOR or WHILE loop.
4724 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004725 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004727 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
4729 return FAIL;
4730
4731 return arg;
4732}
4733
4734/*
4735 * compile "{" start of block
4736 */
4737 static char_u *
4738compile_block(char_u *arg, cctx_T *cctx)
4739{
4740 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4741 return NULL;
4742 return skipwhite(arg + 1);
4743}
4744
4745/*
4746 * compile end of block: drop one scope
4747 */
4748 static void
4749compile_endblock(cctx_T *cctx)
4750{
4751 scope_T *scope = cctx->ctx_scope;
4752
4753 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004754 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 vim_free(scope);
4756}
4757
4758/*
4759 * compile "try"
4760 * Creates a new scope for the try-endtry, pointing to the first catch and
4761 * finally.
4762 * Creates another scope for the "try" block itself.
4763 * TRY instruction sets up exception handling at runtime.
4764 *
4765 * "try"
4766 * TRY -> catch1, -> finally push trystack entry
4767 * ... try block
4768 * "throw {exception}"
4769 * EVAL {exception}
4770 * THROW create exception
4771 * ... try block
4772 * " catch {expr}"
4773 * JUMP -> finally
4774 * catch1: PUSH exeception
4775 * EVAL {expr}
4776 * MATCH
4777 * JUMP nomatch -> catch2
4778 * CATCH remove exception
4779 * ... catch block
4780 * " catch"
4781 * JUMP -> finally
4782 * catch2: CATCH remove exception
4783 * ... catch block
4784 * " finally"
4785 * finally:
4786 * ... finally block
4787 * " endtry"
4788 * ENDTRY pop trystack entry, may rethrow
4789 */
4790 static char_u *
4791compile_try(char_u *arg, cctx_T *cctx)
4792{
4793 garray_T *instr = &cctx->ctx_instr;
4794 scope_T *try_scope;
4795 scope_T *scope;
4796
4797 // scope that holds the jumps that go to catch/finally/endtry
4798 try_scope = new_scope(cctx, TRY_SCOPE);
4799 if (try_scope == NULL)
4800 return NULL;
4801
4802 // "catch" is set when the first ":catch" is found.
4803 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004804 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 if (generate_instr(cctx, ISN_TRY) == NULL)
4806 return NULL;
4807
4808 // scope for the try block itself
4809 scope = new_scope(cctx, BLOCK_SCOPE);
4810 if (scope == NULL)
4811 return NULL;
4812
4813 return arg;
4814}
4815
4816/*
4817 * compile "catch {expr}"
4818 */
4819 static char_u *
4820compile_catch(char_u *arg, cctx_T *cctx UNUSED)
4821{
4822 scope_T *scope = cctx->ctx_scope;
4823 garray_T *instr = &cctx->ctx_instr;
4824 char_u *p;
4825 isn_T *isn;
4826
4827 // end block scope from :try or :catch
4828 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4829 compile_endblock(cctx);
4830 scope = cctx->ctx_scope;
4831
4832 // Error if not in a :try scope
4833 if (scope == NULL || scope->se_type != TRY_SCOPE)
4834 {
4835 emsg(_(e_catch));
4836 return NULL;
4837 }
4838
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004839 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 {
4841 emsg(_("E1033: catch unreachable after catch-all"));
4842 return NULL;
4843 }
4844
4845 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004846 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 JUMP_ALWAYS, cctx) == FAIL)
4848 return NULL;
4849
4850 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004851 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 if (isn->isn_arg.try.try_catch == 0)
4853 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004854 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855 {
4856 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004857 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 isn->isn_arg.jump.jump_where = instr->ga_len;
4859 }
4860
4861 p = skipwhite(arg);
4862 if (ends_excmd(*p))
4863 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004864 scope->se_u.se_try.ts_caught_all = TRUE;
4865 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 }
4867 else
4868 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004869 char_u *end;
4870 char_u *pat;
4871 char_u *tofree = NULL;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004872 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 // Push v:exception, push {expr} and MATCH
4875 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
4876
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004877 end = skip_regexp(p + 1, *p, TRUE, &tofree);
4878 if (*end != *p)
4879 {
4880 semsg(_("E1067: Separator mismatch: %s"), p);
4881 vim_free(tofree);
4882 return FAIL;
4883 }
4884 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004885 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004886 else
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004887 len = (int)(end - (tofree + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004888 pat = vim_strnsave(p + 1, len);
4889 vim_free(tofree);
4890 p += len + 2;
4891 if (pat == NULL)
4892 return FAIL;
4893 if (generate_PUSHS(cctx, pat) == FAIL)
4894 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4897 return NULL;
4898
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004899 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4901 return NULL;
4902 }
4903
4904 if (generate_instr(cctx, ISN_CATCH) == NULL)
4905 return NULL;
4906
4907 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4908 return NULL;
4909 return p;
4910}
4911
4912 static char_u *
4913compile_finally(char_u *arg, cctx_T *cctx)
4914{
4915 scope_T *scope = cctx->ctx_scope;
4916 garray_T *instr = &cctx->ctx_instr;
4917 isn_T *isn;
4918
4919 // end block scope from :try or :catch
4920 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4921 compile_endblock(cctx);
4922 scope = cctx->ctx_scope;
4923
4924 // Error if not in a :try scope
4925 if (scope == NULL || scope->se_type != TRY_SCOPE)
4926 {
4927 emsg(_(e_finally));
4928 return NULL;
4929 }
4930
4931 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004932 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 if (isn->isn_arg.try.try_finally != 0)
4934 {
4935 emsg(_(e_finally_dup));
4936 return NULL;
4937 }
4938
4939 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004940 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004942 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 {
4944 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004945 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946 isn->isn_arg.jump.jump_where = instr->ga_len;
4947 }
4948
4949 isn->isn_arg.try.try_finally = instr->ga_len;
4950 // TODO: set index in ts_finally_label jumps
4951
4952 return arg;
4953}
4954
4955 static char_u *
4956compile_endtry(char_u *arg, cctx_T *cctx)
4957{
4958 scope_T *scope = cctx->ctx_scope;
4959 garray_T *instr = &cctx->ctx_instr;
4960 isn_T *isn;
4961
4962 // end block scope from :catch or :finally
4963 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4964 compile_endblock(cctx);
4965 scope = cctx->ctx_scope;
4966
4967 // Error if not in a :try scope
4968 if (scope == NULL || scope->se_type != TRY_SCOPE)
4969 {
4970 if (scope == NULL)
4971 emsg(_(e_no_endtry));
4972 else if (scope->se_type == WHILE_SCOPE)
4973 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01004974 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 emsg(_(e_endfor));
4976 else
4977 emsg(_(e_endif));
4978 return NULL;
4979 }
4980
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004981 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
4983 {
4984 emsg(_("E1032: missing :catch or :finally"));
4985 return NULL;
4986 }
4987
4988 // Fill in the "end" label in jumps at the end of the blocks, if not done
4989 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004990 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991
4992 // End :catch or :finally scope: set value in ISN_TRY instruction
4993 if (isn->isn_arg.try.try_finally == 0)
4994 isn->isn_arg.try.try_finally = instr->ga_len;
4995 compile_endblock(cctx);
4996
4997 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4998 return NULL;
4999 return arg;
5000}
5001
5002/*
5003 * compile "throw {expr}"
5004 */
5005 static char_u *
5006compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5007{
5008 char_u *p = skipwhite(arg);
5009
5010 if (ends_excmd(*p))
5011 {
5012 emsg(_(e_argreq));
5013 return NULL;
5014 }
5015 if (compile_expr1(&p, cctx) == FAIL)
5016 return NULL;
5017 if (may_generate_2STRING(-1, cctx) == FAIL)
5018 return NULL;
5019 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5020 return NULL;
5021
5022 return p;
5023}
5024
5025/*
5026 * compile "echo expr"
5027 */
5028 static char_u *
5029compile_echo(char_u *arg, int with_white, cctx_T *cctx)
5030{
5031 char_u *p = arg;
5032 int count = 0;
5033
Bram Moolenaarad39c092020-02-26 18:23:43 +01005034 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 {
5036 if (compile_expr1(&p, cctx) == FAIL)
5037 return NULL;
5038 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005039 p = skipwhite(p);
5040 if (ends_excmd(*p))
5041 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 }
5043
5044 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01005045 return p;
5046}
5047
5048/*
5049 * compile "execute expr"
5050 */
5051 static char_u *
5052compile_execute(char_u *arg, cctx_T *cctx)
5053{
5054 char_u *p = arg;
5055 int count = 0;
5056
5057 for (;;)
5058 {
5059 if (compile_expr1(&p, cctx) == FAIL)
5060 return NULL;
5061 ++count;
5062 p = skipwhite(p);
5063 if (ends_excmd(*p))
5064 break;
5065 }
5066
5067 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068
5069 return p;
5070}
5071
5072/*
5073 * After ex_function() has collected all the function lines: parse and compile
5074 * the lines into instructions.
5075 * Adds the function to "def_functions".
5076 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5077 * return statement (used for lambda).
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005078 * This can be used recursively through compile_lambda(), which may reallocate
5079 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 */
5081 void
5082compile_def_function(ufunc_T *ufunc, int set_return_type)
5083{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084 char_u *line = NULL;
5085 char_u *p;
5086 exarg_T ea;
5087 char *errormsg = NULL; // error message
5088 int had_return = FALSE;
5089 cctx_T cctx;
5090 garray_T *instr;
5091 int called_emsg_before = called_emsg;
5092 int ret = FAIL;
5093 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005094 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005097 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005098
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005099 if (ufunc->uf_dfunc_idx >= 0)
5100 {
5101 // Redefining a function that was compiled before.
5102 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5103
5104 // Free old instructions.
5105 delete_def_function_contents(dfunc);
5106 }
5107 else
5108 {
5109 // Add the function to "def_functions".
5110 if (ga_grow(&def_functions, 1) == FAIL)
5111 return;
5112 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
5113 vim_memset(dfunc, 0, sizeof(dfunc_T));
5114 dfunc->df_idx = def_functions.ga_len;
5115 ufunc->uf_dfunc_idx = dfunc->df_idx;
5116 dfunc->df_ufunc = ufunc;
5117 ++def_functions.ga_len;
5118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 }
5120
5121 vim_memset(&cctx, 0, sizeof(cctx));
5122 cctx.ctx_ufunc = ufunc;
5123 cctx.ctx_lnum = -1;
5124 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5125 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5126 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5127 cctx.ctx_type_list = &ufunc->uf_type_list;
5128 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5129 instr = &cctx.ctx_instr;
5130
5131 // Most modern script version.
5132 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5133
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005134 if (ufunc->uf_def_args.ga_len > 0)
5135 {
5136 int count = ufunc->uf_def_args.ga_len;
5137 int i;
5138 char_u *arg;
5139 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5140
5141 // Produce instructions for the default values of optional arguments.
5142 // Store the instruction index in uf_def_arg_idx[] so that we know
5143 // where to start when the function is called, depending on the number
5144 // of arguments.
5145 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5146 if (ufunc->uf_def_arg_idx == NULL)
5147 goto erret;
5148 for (i = 0; i < count; ++i)
5149 {
5150 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5151 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
5152 if (compile_expr1(&arg, &cctx) == FAIL
5153 || generate_STORE(&cctx, ISN_STORE,
5154 i - count - off, NULL) == FAIL)
5155 goto erret;
5156 }
5157
5158 // If a varargs is following, push an empty list.
5159 if (ufunc->uf_va_name != NULL)
5160 {
5161 if (generate_NEWLIST(&cctx, 0) == FAIL
5162 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
5163 goto erret;
5164 }
5165
5166 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5167 }
5168
5169 /*
5170 * Loop over all the lines of the function and generate instructions.
5171 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172 for (;;)
5173 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005174 int is_ex_command;
5175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 if (line != NULL && *line == '|')
5177 // the line continues after a '|'
5178 ++line;
5179 else if (line != NULL && *line != NUL)
5180 {
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005181 if (emsg_before == called_emsg)
5182 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183 goto erret;
5184 }
5185 else
5186 {
5187 do
5188 {
5189 ++cctx.ctx_lnum;
5190 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5191 break;
5192 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
5193 } while (line == NULL);
5194 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5195 break;
5196 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
5197 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005198 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199
5200 had_return = FALSE;
5201 vim_memset(&ea, 0, sizeof(ea));
5202 ea.cmdlinep = &line;
5203 ea.cmd = skipwhite(line);
5204
5205 // "}" ends a block scope
5206 if (*ea.cmd == '}')
5207 {
5208 scopetype_T stype = cctx.ctx_scope == NULL
5209 ? NO_SCOPE : cctx.ctx_scope->se_type;
5210
5211 if (stype == BLOCK_SCOPE)
5212 {
5213 compile_endblock(&cctx);
5214 line = ea.cmd;
5215 }
5216 else
5217 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005218 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 goto erret;
5220 }
5221 if (line != NULL)
5222 line = skipwhite(ea.cmd + 1);
5223 continue;
5224 }
5225
5226 // "{" starts a block scope
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01005227 // "{'a': 1}->func() is something else
5228 if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005229 {
5230 line = compile_block(ea.cmd, &cctx);
5231 continue;
5232 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005233 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234
5235 /*
5236 * COMMAND MODIFIERS
5237 */
5238 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5239 {
5240 if (errormsg != NULL)
5241 goto erret;
5242 // empty line or comment
5243 line = (char_u *)"";
5244 continue;
5245 }
5246
5247 // Skip ":call" to get to the function name.
5248 if (checkforcmd(&ea.cmd, "call", 3))
5249 ea.cmd = skipwhite(ea.cmd);
5250
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005251 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005253 // Assuming the command starts with a variable or function name,
5254 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5255 // val".
5256 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5257 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005258 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005259 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005260 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005261 int oplen;
5262 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005264 oplen = assignment_len(skipwhite(p), &heredoc);
5265 if (oplen > 0)
5266 {
5267 // Recognize an assignment if we recognize the variable
5268 // name:
5269 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005270 // "local = expr" where "local" is a local var.
5271 // "script = expr" where "script" is a script-local var.
5272 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005273 // "&opt = expr"
5274 // "$ENV = expr"
5275 // "@r = expr"
5276 if (*ea.cmd == '&'
5277 || *ea.cmd == '$'
5278 || *ea.cmd == '@'
5279 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5280 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5281 || lookup_script(ea.cmd, p - ea.cmd) == OK
5282 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5283 {
5284 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5285 if (line == NULL)
5286 goto erret;
5287 continue;
5288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289 }
5290 }
5291 }
5292
5293 /*
5294 * COMMAND after range
5295 */
5296 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005297 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5298 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005299
5300 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5301 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005302 if (cctx.ctx_skip == TRUE)
5303 {
5304 line += STRLEN(line);
5305 continue;
5306 }
5307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308 // Expression or function call.
5309 if (ea.cmdidx == CMD_eval)
5310 {
5311 p = ea.cmd;
5312 if (compile_expr1(&p, &cctx) == FAIL)
5313 goto erret;
5314
5315 // drop the return value
5316 generate_instr_drop(&cctx, ISN_DROP, 1);
5317 line = p;
5318 continue;
5319 }
5320 if (ea.cmdidx == CMD_let)
5321 {
5322 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5323 if (line == NULL)
5324 goto erret;
5325 continue;
5326 }
5327 iemsg("Command from find_ex_command() not handled");
5328 goto erret;
5329 }
5330
5331 p = skipwhite(p);
5332
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005333 if (cctx.ctx_skip == TRUE
5334 && ea.cmdidx != CMD_elseif
5335 && ea.cmdidx != CMD_else
5336 && ea.cmdidx != CMD_endif)
5337 {
5338 line += STRLEN(line);
5339 continue;
5340 }
5341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 switch (ea.cmdidx)
5343 {
5344 case CMD_def:
5345 case CMD_function:
5346 // TODO: Nested function
5347 emsg("Nested function not implemented yet");
5348 goto erret;
5349
5350 case CMD_return:
5351 line = compile_return(p, set_return_type, &cctx);
5352 had_return = TRUE;
5353 break;
5354
5355 case CMD_let:
5356 case CMD_const:
5357 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5358 break;
5359
5360 case CMD_import:
5361 line = compile_import(p, &cctx);
5362 break;
5363
5364 case CMD_if:
5365 line = compile_if(p, &cctx);
5366 break;
5367 case CMD_elseif:
5368 line = compile_elseif(p, &cctx);
5369 break;
5370 case CMD_else:
5371 line = compile_else(p, &cctx);
5372 break;
5373 case CMD_endif:
5374 line = compile_endif(p, &cctx);
5375 break;
5376
5377 case CMD_while:
5378 line = compile_while(p, &cctx);
5379 break;
5380 case CMD_endwhile:
5381 line = compile_endwhile(p, &cctx);
5382 break;
5383
5384 case CMD_for:
5385 line = compile_for(p, &cctx);
5386 break;
5387 case CMD_endfor:
5388 line = compile_endfor(p, &cctx);
5389 break;
5390 case CMD_continue:
5391 line = compile_continue(p, &cctx);
5392 break;
5393 case CMD_break:
5394 line = compile_break(p, &cctx);
5395 break;
5396
5397 case CMD_try:
5398 line = compile_try(p, &cctx);
5399 break;
5400 case CMD_catch:
5401 line = compile_catch(p, &cctx);
5402 break;
5403 case CMD_finally:
5404 line = compile_finally(p, &cctx);
5405 break;
5406 case CMD_endtry:
5407 line = compile_endtry(p, &cctx);
5408 break;
5409 case CMD_throw:
5410 line = compile_throw(p, &cctx);
5411 break;
5412
5413 case CMD_echo:
5414 line = compile_echo(p, TRUE, &cctx);
5415 break;
5416 case CMD_echon:
5417 line = compile_echo(p, FALSE, &cctx);
5418 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005419 case CMD_execute:
5420 line = compile_execute(p, &cctx);
5421 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422
5423 default:
5424 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005425 // TODO:
5426 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005427 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428 generate_EXEC(&cctx, line);
5429 line = (char_u *)"";
5430 break;
5431 }
5432 if (line == NULL)
5433 goto erret;
5434
5435 if (cctx.ctx_type_stack.ga_len < 0)
5436 {
5437 iemsg("Type stack underflow");
5438 goto erret;
5439 }
5440 }
5441
5442 if (cctx.ctx_scope != NULL)
5443 {
5444 if (cctx.ctx_scope->se_type == IF_SCOPE)
5445 emsg(_(e_endif));
5446 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5447 emsg(_(e_endwhile));
5448 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5449 emsg(_(e_endfor));
5450 else
5451 emsg(_("E1026: Missing }"));
5452 goto erret;
5453 }
5454
5455 if (!had_return)
5456 {
5457 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5458 {
5459 emsg(_("E1027: Missing return statement"));
5460 goto erret;
5461 }
5462
5463 // Return zero if there is no return at the end.
5464 generate_PUSHNR(&cctx, 0);
5465 generate_instr(&cctx, ISN_RETURN);
5466 }
5467
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005468 {
5469 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5470 + ufunc->uf_dfunc_idx;
5471 dfunc->df_deleted = FALSE;
5472 dfunc->df_instr = instr->ga_data;
5473 dfunc->df_instr_count = instr->ga_len;
5474 dfunc->df_varcount = cctx.ctx_max_local;
5475 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476
5477 ret = OK;
5478
5479erret:
5480 if (ret == FAIL)
5481 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005482 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005483 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5484 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005485
5486 for (idx = 0; idx < instr->ga_len; ++idx)
5487 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005488 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005491 if (!dfunc->df_deleted)
5492 --def_functions.ga_len;
5493
5494 // Don't execute this function body.
5495 ga_clear_strings(&ufunc->uf_lines);
5496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 if (errormsg != NULL)
5498 emsg(errormsg);
5499 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005500 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005501 }
5502
5503 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005504 free_imported(&cctx);
5505 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005506 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005507}
5508
5509/*
5510 * Delete an instruction, free what it contains.
5511 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01005512 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513delete_instr(isn_T *isn)
5514{
5515 switch (isn->isn_type)
5516 {
5517 case ISN_EXEC:
5518 case ISN_LOADENV:
5519 case ISN_LOADG:
5520 case ISN_LOADOPT:
5521 case ISN_MEMBER:
5522 case ISN_PUSHEXC:
5523 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005524 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005526 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005527 vim_free(isn->isn_arg.string);
5528 break;
5529
5530 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005531 case ISN_STORES:
5532 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533 break;
5534
5535 case ISN_STOREOPT:
5536 vim_free(isn->isn_arg.storeopt.so_name);
5537 break;
5538
5539 case ISN_PUSHBLOB: // push blob isn_arg.blob
5540 blob_unref(isn->isn_arg.blob);
5541 break;
5542
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005543 case ISN_PUSHPARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01005544 partial_unref(isn->isn_arg.partial);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005545 break;
5546
5547 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005548#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005549 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005550#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005551 break;
5552
5553 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005554#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005555 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005556#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005557 break;
5558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005559 case ISN_UCALL:
5560 vim_free(isn->isn_arg.ufunc.cuf_name);
5561 break;
5562
5563 case ISN_2BOOL:
5564 case ISN_2STRING:
5565 case ISN_ADDBLOB:
5566 case ISN_ADDLIST:
5567 case ISN_BCALL:
5568 case ISN_CATCH:
5569 case ISN_CHECKNR:
5570 case ISN_CHECKTYPE:
5571 case ISN_COMPAREANY:
5572 case ISN_COMPAREBLOB:
5573 case ISN_COMPAREBOOL:
5574 case ISN_COMPAREDICT:
5575 case ISN_COMPAREFLOAT:
5576 case ISN_COMPAREFUNC:
5577 case ISN_COMPARELIST:
5578 case ISN_COMPARENR:
5579 case ISN_COMPAREPARTIAL:
5580 case ISN_COMPARESPECIAL:
5581 case ISN_COMPARESTRING:
5582 case ISN_CONCAT:
5583 case ISN_DCALL:
5584 case ISN_DROP:
5585 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01005586 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005587 case ISN_ENDTRY:
5588 case ISN_FOR:
5589 case ISN_FUNCREF:
5590 case ISN_INDEX:
5591 case ISN_JUMP:
5592 case ISN_LOAD:
5593 case ISN_LOADSCRIPT:
5594 case ISN_LOADREG:
5595 case ISN_LOADV:
5596 case ISN_NEGATENR:
5597 case ISN_NEWDICT:
5598 case ISN_NEWLIST:
5599 case ISN_OPNR:
5600 case ISN_OPFLOAT:
5601 case ISN_OPANY:
5602 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005603 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005604 case ISN_PUSHF:
5605 case ISN_PUSHNR:
5606 case ISN_PUSHBOOL:
5607 case ISN_PUSHSPEC:
5608 case ISN_RETURN:
5609 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005610 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005611 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005612 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613 case ISN_STORESCRIPT:
5614 case ISN_THROW:
5615 case ISN_TRY:
5616 // nothing allocated
5617 break;
5618 }
5619}
5620
5621/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01005622 * Free all instructions for "dfunc".
5623 */
5624 static void
5625delete_def_function_contents(dfunc_T *dfunc)
5626{
5627 int idx;
5628
5629 ga_clear(&dfunc->df_def_args_isn);
5630
5631 if (dfunc->df_instr != NULL)
5632 {
5633 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5634 delete_instr(dfunc->df_instr + idx);
5635 VIM_CLEAR(dfunc->df_instr);
5636 }
5637
5638 dfunc->df_deleted = TRUE;
5639}
5640
5641/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642 * When a user function is deleted, delete any associated def function.
5643 */
5644 void
5645delete_def_function(ufunc_T *ufunc)
5646{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647 if (ufunc->uf_dfunc_idx >= 0)
5648 {
5649 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5650 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005651
Bram Moolenaar20431c92020-03-20 18:39:46 +01005652 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005653 }
5654}
5655
5656#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005657/*
5658 * Free all functions defined with ":def".
5659 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005660 void
5661free_def_functions(void)
5662{
Bram Moolenaar20431c92020-03-20 18:39:46 +01005663 int idx;
5664
5665 for (idx = 0; idx < def_functions.ga_len; ++idx)
5666 {
5667 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
5668
5669 delete_def_function_contents(dfunc);
5670 }
5671
5672 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005673}
5674#endif
5675
5676
5677#endif // FEAT_EVAL