blob: ce1ca76c24b51fa6296565a3e3a9de2b77d91ce0 [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".
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200769 * Consumes "part".
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100770 */
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;
Bram Moolenaar04d05222020-02-06 22:06:54 +01003811 case VAR_FLOAT:
3812#ifdef FEAT_FLOAT
3813 generate_PUSHF(cctx, 0.0);
3814#endif
3815 break;
3816 case VAR_STRING:
3817 generate_PUSHS(cctx, NULL);
3818 break;
3819 case VAR_BLOB:
3820 generate_PUSHBLOB(cctx, NULL);
3821 break;
3822 case VAR_FUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003823 generate_PUSHFUNC(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003824 break;
3825 case VAR_PARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003826 generate_PUSHPARTIAL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003827 break;
3828 case VAR_LIST:
3829 generate_NEWLIST(cctx, 0);
3830 break;
3831 case VAR_DICT:
3832 generate_NEWDICT(cctx, 0);
3833 break;
3834 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003835 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003836 break;
3837 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003838 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003839 break;
3840 case VAR_NUMBER:
3841 case VAR_UNKNOWN:
3842 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003843 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01003844 generate_PUSHNR(cctx, 0);
3845 break;
3846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 }
3848
3849 if (oplen > 0 && *op != '=')
3850 {
3851 type_T *expected = &t_number;
3852 garray_T *stack = &cctx->ctx_type_stack;
3853 type_T *stacktype;
3854
3855 // TODO: if type is known use float or any operation
3856
3857 if (*op == '.')
3858 expected = &t_string;
3859 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3860 if (need_type(stacktype, expected, -1, cctx) == FAIL)
3861 goto theend;
3862
3863 if (*op == '.')
3864 generate_instr_drop(cctx, ISN_CONCAT, 1);
3865 else
3866 {
3867 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3868
3869 if (isn == NULL)
3870 goto theend;
3871 switch (*op)
3872 {
3873 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3874 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3875 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3876 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3877 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3878 }
3879 }
3880 }
3881
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003882 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003884 case dest_option:
3885 generate_STOREOPT(cctx, name + 1, opt_flags);
3886 break;
3887 case dest_global:
3888 // include g: with the name, easier to execute that way
3889 generate_STORE(cctx, ISN_STOREG, 0, name);
3890 break;
3891 case dest_env:
3892 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
3893 break;
3894 case dest_reg:
3895 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
3896 break;
3897 case dest_vimvar:
3898 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
3899 break;
3900 case dest_script:
3901 {
3902 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
3903 imported_T *import = NULL;
3904 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003905
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003906 if (name[1] != ':')
3907 {
3908 import = find_imported(name, 0, cctx);
3909 if (import != NULL)
3910 sid = import->imp_sid;
3911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003913 idx = get_script_item_idx(sid, rawname, TRUE);
3914 // TODO: specific type
3915 if (idx < 0)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003916 generate_OLDSCRIPT(cctx, ISN_STORES, name, sid, &t_any);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003917 else
3918 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
3919 sid, idx, &t_any);
3920 }
3921 break;
3922 case dest_local:
3923 {
3924 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003926 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
3927 // into ISN_STORENR
3928 if (instr->ga_len == instr_count + 1
3929 && isn->isn_type == ISN_PUSHNR)
3930 {
3931 varnumber_T val = isn->isn_arg.number;
3932 garray_T *stack = &cctx->ctx_type_stack;
3933
3934 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003935 isn->isn_arg.storenr.stnr_idx = idx;
3936 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003937 if (stack->ga_len > 0)
3938 --stack->ga_len;
3939 }
3940 else
3941 generate_STORE(cctx, ISN_STORE, idx, NULL);
3942 }
3943 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 }
3945 ret = p;
3946
3947theend:
3948 vim_free(name);
3949 return ret;
3950}
3951
3952/*
3953 * Compile an :import command.
3954 */
3955 static char_u *
3956compile_import(char_u *arg, cctx_T *cctx)
3957{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01003958 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959}
3960
3961/*
3962 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3963 */
3964 static int
3965compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3966{
3967 garray_T *instr = &cctx->ctx_instr;
3968 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3969
3970 if (endlabel == NULL)
3971 return FAIL;
3972 endlabel->el_next = *el;
3973 *el = endlabel;
3974 endlabel->el_end_label = instr->ga_len;
3975
3976 generate_JUMP(cctx, when, 0);
3977 return OK;
3978}
3979
3980 static void
3981compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3982{
3983 garray_T *instr = &cctx->ctx_instr;
3984
3985 while (*el != NULL)
3986 {
3987 endlabel_T *cur = (*el);
3988 isn_T *isn;
3989
3990 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3991 isn->isn_arg.jump.jump_where = instr->ga_len;
3992 *el = cur->el_next;
3993 vim_free(cur);
3994 }
3995}
3996
3997/*
3998 * Create a new scope and set up the generic items.
3999 */
4000 static scope_T *
4001new_scope(cctx_T *cctx, scopetype_T type)
4002{
4003 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4004
4005 if (scope == NULL)
4006 return NULL;
4007 scope->se_outer = cctx->ctx_scope;
4008 cctx->ctx_scope = scope;
4009 scope->se_type = type;
4010 scope->se_local_count = cctx->ctx_locals.ga_len;
4011 return scope;
4012}
4013
4014/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004015 * Free the current scope and go back to the outer scope.
4016 */
4017 static void
4018drop_scope(cctx_T *cctx)
4019{
4020 scope_T *scope = cctx->ctx_scope;
4021
4022 if (scope == NULL)
4023 {
4024 iemsg("calling drop_scope() without a scope");
4025 return;
4026 }
4027 cctx->ctx_scope = scope->se_outer;
4028 vim_free(scope);
4029}
4030
4031/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004032 * Evaluate an expression that is a constant:
4033 * has(arg)
4034 *
4035 * Also handle:
4036 * ! in front logical NOT
4037 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004038 * Return FAIL if the expression is not a constant.
4039 */
4040 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004041evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004042{
4043 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004044 char_u *start_leader, *end_leader;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004045
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004046 /*
4047 * Skip '!' characters. They are handled later.
4048 */
4049 start_leader = *arg;
4050 while (**arg == '!')
4051 *arg = skipwhite(*arg + 1);
4052 end_leader = *arg;
4053
4054 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004055 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004056 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004057 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4058 {
4059 tv->v_type = VAR_SPECIAL;
4060 tv->vval.v_number = VVAL_TRUE;
4061 *arg += 4;
4062 return OK;
4063 }
4064 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4065 {
4066 tv->v_type = VAR_SPECIAL;
4067 tv->vval.v_number = VVAL_FALSE;
4068 *arg += 5;
4069 return OK;
4070 }
4071
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004072 if (STRNCMP("has(", *arg, 4) != 0)
4073 return FAIL;
4074 *arg = skipwhite(*arg + 4);
4075
4076 if (**arg == '"')
4077 {
4078 if (get_string_tv(arg, tv, TRUE) == FAIL)
4079 return FAIL;
4080 }
4081 else if (**arg == '\'')
4082 {
4083 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4084 return FAIL;
4085 }
4086 else
4087 return FAIL;
4088
4089 *arg = skipwhite(*arg);
4090 if (**arg != ')')
4091 return FAIL;
4092 *arg = skipwhite(*arg + 1);
4093
4094 argvars[0] = *tv;
4095 argvars[1].v_type = VAR_UNKNOWN;
4096 tv->v_type = VAR_NUMBER;
4097 tv->vval.v_number = 0;
4098 f_has(argvars, tv);
4099 clear_tv(&argvars[0]);
4100
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004101 while (start_leader < end_leader)
4102 {
4103 if (*start_leader == '!')
4104 tv->vval.v_number = !tv->vval.v_number;
4105 ++start_leader;
4106 }
4107
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004108 return OK;
4109}
4110
Bram Moolenaar080457c2020-03-03 21:53:32 +01004111 static int
4112evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4113{
4114 exptype_T type = EXPR_UNKNOWN;
4115 char_u *p;
4116 int len = 2;
4117 int type_is = FALSE;
4118
4119 // get the first variable
4120 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4121 return FAIL;
4122
4123 p = skipwhite(*arg);
4124 type = get_compare_type(p, &len, &type_is);
4125
4126 /*
4127 * If there is a comparative operator, use it.
4128 */
4129 if (type != EXPR_UNKNOWN)
4130 {
4131 // TODO
4132 return FAIL;
4133 }
4134
4135 return OK;
4136}
4137
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004138static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4139
4140/*
4141 * Compile constant || or &&.
4142 */
4143 static int
4144evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4145{
4146 char_u *p = skipwhite(*arg);
4147 int opchar = *op;
4148
4149 if (p[0] == opchar && p[1] == opchar)
4150 {
4151 int val = tv2bool(tv);
4152
4153 /*
4154 * Repeat until there is no following "||" or "&&"
4155 */
4156 while (p[0] == opchar && p[1] == opchar)
4157 {
4158 typval_T tv2;
4159
4160 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4161 return FAIL;
4162
4163 // eval the next expression
4164 *arg = skipwhite(p + 2);
4165 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004166 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004167 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004168 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004169 {
4170 clear_tv(&tv2);
4171 return FAIL;
4172 }
4173 if ((opchar == '&') == val)
4174 {
4175 // false || tv2 or true && tv2: use tv2
4176 clear_tv(tv);
4177 *tv = tv2;
4178 val = tv2bool(tv);
4179 }
4180 else
4181 clear_tv(&tv2);
4182 p = skipwhite(*arg);
4183 }
4184 }
4185
4186 return OK;
4187}
4188
4189/*
4190 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4191 * Return FAIL if the expression is not a constant.
4192 */
4193 static int
4194evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4195{
4196 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004197 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004198 return FAIL;
4199
4200 // || and && work almost the same
4201 return evaluate_const_and_or(arg, cctx, "&&", tv);
4202}
4203
4204/*
4205 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4206 * Return FAIL if the expression is not a constant.
4207 */
4208 static int
4209evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4210{
4211 // evaluate the first expression
4212 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4213 return FAIL;
4214
4215 // || and && work almost the same
4216 return evaluate_const_and_or(arg, cctx, "||", tv);
4217}
4218
4219/*
4220 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4221 * E.g. for "has('feature')".
4222 * This does not produce error messages. "tv" should be cleared afterwards.
4223 * Return FAIL if the expression is not a constant.
4224 */
4225 static int
4226evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4227{
4228 char_u *p;
4229
4230 // evaluate the first expression
4231 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4232 return FAIL;
4233
4234 p = skipwhite(*arg);
4235 if (*p == '?')
4236 {
4237 int val = tv2bool(tv);
4238 typval_T tv2;
4239
4240 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4241 return FAIL;
4242
4243 // evaluate the second expression; any type is accepted
4244 clear_tv(tv);
4245 *arg = skipwhite(p + 1);
4246 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4247 return FAIL;
4248
4249 // Check for the ":".
4250 p = skipwhite(*arg);
4251 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4252 return FAIL;
4253
4254 // evaluate the third expression
4255 *arg = skipwhite(p + 1);
4256 tv2.v_type = VAR_UNKNOWN;
4257 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4258 {
4259 clear_tv(&tv2);
4260 return FAIL;
4261 }
4262 if (val)
4263 {
4264 // use the expr after "?"
4265 clear_tv(&tv2);
4266 }
4267 else
4268 {
4269 // use the expr after ":"
4270 clear_tv(tv);
4271 *tv = tv2;
4272 }
4273 }
4274 return OK;
4275}
4276
4277/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 * compile "if expr"
4279 *
4280 * "if expr" Produces instructions:
4281 * EVAL expr Push result of "expr"
4282 * JUMP_IF_FALSE end
4283 * ... body ...
4284 * end:
4285 *
4286 * "if expr | else" Produces instructions:
4287 * EVAL expr Push result of "expr"
4288 * JUMP_IF_FALSE else
4289 * ... body ...
4290 * JUMP_ALWAYS end
4291 * else:
4292 * ... body ...
4293 * end:
4294 *
4295 * "if expr1 | elseif expr2 | else" Produces instructions:
4296 * EVAL expr Push result of "expr"
4297 * JUMP_IF_FALSE elseif
4298 * ... body ...
4299 * JUMP_ALWAYS end
4300 * elseif:
4301 * EVAL expr Push result of "expr"
4302 * JUMP_IF_FALSE else
4303 * ... body ...
4304 * JUMP_ALWAYS end
4305 * else:
4306 * ... body ...
4307 * end:
4308 */
4309 static char_u *
4310compile_if(char_u *arg, cctx_T *cctx)
4311{
4312 char_u *p = arg;
4313 garray_T *instr = &cctx->ctx_instr;
4314 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004315 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004317 // compile "expr"; if we know it evaluates to FALSE skip the block
4318 tv.v_type = VAR_UNKNOWN;
4319 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4320 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4321 else
4322 cctx->ctx_skip = MAYBE;
4323 clear_tv(&tv);
4324 if (cctx->ctx_skip == MAYBE)
4325 {
4326 p = arg;
4327 if (compile_expr1(&p, cctx) == FAIL)
4328 return NULL;
4329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330
4331 scope = new_scope(cctx, IF_SCOPE);
4332 if (scope == NULL)
4333 return NULL;
4334
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004335 if (cctx->ctx_skip == MAYBE)
4336 {
4337 // "where" is set when ":elseif", "else" or ":endif" is found
4338 scope->se_u.se_if.is_if_label = instr->ga_len;
4339 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4340 }
4341 else
4342 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343
4344 return p;
4345}
4346
4347 static char_u *
4348compile_elseif(char_u *arg, cctx_T *cctx)
4349{
4350 char_u *p = arg;
4351 garray_T *instr = &cctx->ctx_instr;
4352 isn_T *isn;
4353 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004354 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355
4356 if (scope == NULL || scope->se_type != IF_SCOPE)
4357 {
4358 emsg(_(e_elseif_without_if));
4359 return NULL;
4360 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004361 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362
Bram Moolenaar158906c2020-02-06 20:39:45 +01004363 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004364 {
4365 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004367 return NULL;
4368 // previous "if" or "elseif" jumps here
4369 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4370 isn->isn_arg.jump.jump_where = instr->ga_len;
4371 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004373 // compile "expr"; if we know it evaluates to FALSE skip the block
4374 tv.v_type = VAR_UNKNOWN;
4375 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4376 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4377 else
4378 cctx->ctx_skip = MAYBE;
4379 clear_tv(&tv);
4380 if (cctx->ctx_skip == MAYBE)
4381 {
4382 p = arg;
4383 if (compile_expr1(&p, cctx) == FAIL)
4384 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004386 // "where" is set when ":elseif", "else" or ":endif" is found
4387 scope->se_u.se_if.is_if_label = instr->ga_len;
4388 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4389 }
4390 else
4391 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392
4393 return p;
4394}
4395
4396 static char_u *
4397compile_else(char_u *arg, cctx_T *cctx)
4398{
4399 char_u *p = arg;
4400 garray_T *instr = &cctx->ctx_instr;
4401 isn_T *isn;
4402 scope_T *scope = cctx->ctx_scope;
4403
4404 if (scope == NULL || scope->se_type != IF_SCOPE)
4405 {
4406 emsg(_(e_else_without_if));
4407 return NULL;
4408 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004409 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004411 // jump from previous block to the end, unless the else block is empty
4412 if (cctx->ctx_skip == MAYBE)
4413 {
4414 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004416 return NULL;
4417 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418
Bram Moolenaar158906c2020-02-06 20:39:45 +01004419 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004420 {
4421 if (scope->se_u.se_if.is_if_label >= 0)
4422 {
4423 // previous "if" or "elseif" jumps here
4424 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4425 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004426 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004427 }
4428 }
4429
4430 if (cctx->ctx_skip != MAYBE)
4431 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432
4433 return p;
4434}
4435
4436 static char_u *
4437compile_endif(char_u *arg, cctx_T *cctx)
4438{
4439 scope_T *scope = cctx->ctx_scope;
4440 ifscope_T *ifscope;
4441 garray_T *instr = &cctx->ctx_instr;
4442 isn_T *isn;
4443
4444 if (scope == NULL || scope->se_type != IF_SCOPE)
4445 {
4446 emsg(_(e_endif_without_if));
4447 return NULL;
4448 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004449 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004450 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004452 if (scope->se_u.se_if.is_if_label >= 0)
4453 {
4454 // previous "if" or "elseif" jumps here
4455 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4456 isn->isn_arg.jump.jump_where = instr->ga_len;
4457 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 // Fill in the "end" label in jumps at the end of the blocks.
4459 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004460 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004462 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 return arg;
4464}
4465
4466/*
4467 * compile "for var in expr"
4468 *
4469 * Produces instructions:
4470 * PUSHNR -1
4471 * STORE loop-idx Set index to -1
4472 * EVAL expr Push result of "expr"
4473 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4474 * - if beyond end, jump to "end"
4475 * - otherwise get item from list and push it
4476 * STORE var Store item in "var"
4477 * ... body ...
4478 * JUMP top Jump back to repeat
4479 * end: DROP Drop the result of "expr"
4480 *
4481 */
4482 static char_u *
4483compile_for(char_u *arg, cctx_T *cctx)
4484{
4485 char_u *p;
4486 size_t varlen;
4487 garray_T *instr = &cctx->ctx_instr;
4488 garray_T *stack = &cctx->ctx_type_stack;
4489 scope_T *scope;
4490 int loop_idx; // index of loop iteration variable
4491 int var_idx; // index of "var"
4492 type_T *vartype;
4493
4494 // TODO: list of variables: "for [key, value] in dict"
4495 // parse "var"
4496 for (p = arg; eval_isnamec1(*p); ++p)
4497 ;
4498 varlen = p - arg;
4499 var_idx = lookup_local(arg, varlen, cctx);
4500 if (var_idx >= 0)
4501 {
4502 semsg(_("E1023: variable already defined: %s"), arg);
4503 return NULL;
4504 }
4505
4506 // consume "in"
4507 p = skipwhite(p);
4508 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4509 {
4510 emsg(_(e_missing_in));
4511 return NULL;
4512 }
4513 p = skipwhite(p + 2);
4514
4515
4516 scope = new_scope(cctx, FOR_SCOPE);
4517 if (scope == NULL)
4518 return NULL;
4519
4520 // Reserve a variable to store the loop iteration counter.
4521 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4522 if (loop_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004523 {
4524 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527
4528 // Reserve a variable to store "var"
4529 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4530 if (var_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004531 {
4532 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535
4536 generate_STORENR(cctx, loop_idx, -1);
4537
4538 // compile "expr", it remains on the stack until "endfor"
4539 arg = p;
4540 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004541 {
4542 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545
4546 // now we know the type of "var"
4547 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4548 if (vartype->tt_type != VAR_LIST)
4549 {
4550 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004551 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 return NULL;
4553 }
4554 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
4555 {
4556 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4557
4558 lvar->lv_type = vartype->tt_member;
4559 }
4560
4561 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004562 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563
4564 generate_FOR(cctx, loop_idx);
4565 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4566
4567 return arg;
4568}
4569
4570/*
4571 * compile "endfor"
4572 */
4573 static char_u *
4574compile_endfor(char_u *arg, cctx_T *cctx)
4575{
4576 garray_T *instr = &cctx->ctx_instr;
4577 scope_T *scope = cctx->ctx_scope;
4578 forscope_T *forscope;
4579 isn_T *isn;
4580
4581 if (scope == NULL || scope->se_type != FOR_SCOPE)
4582 {
4583 emsg(_(e_for));
4584 return NULL;
4585 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004586 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004588 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589
4590 // At end of ":for" scope jump back to the FOR instruction.
4591 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4592
4593 // Fill in the "end" label in the FOR statement so it can jump here
4594 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4595 isn->isn_arg.forloop.for_end = instr->ga_len;
4596
4597 // Fill in the "end" label any BREAK statements
4598 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4599
4600 // Below the ":for" scope drop the "expr" list from the stack.
4601 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4602 return NULL;
4603
4604 vim_free(scope);
4605
4606 return arg;
4607}
4608
4609/*
4610 * compile "while expr"
4611 *
4612 * Produces instructions:
4613 * top: EVAL expr Push result of "expr"
4614 * JUMP_IF_FALSE end jump if false
4615 * ... body ...
4616 * JUMP top Jump back to repeat
4617 * end:
4618 *
4619 */
4620 static char_u *
4621compile_while(char_u *arg, cctx_T *cctx)
4622{
4623 char_u *p = arg;
4624 garray_T *instr = &cctx->ctx_instr;
4625 scope_T *scope;
4626
4627 scope = new_scope(cctx, WHILE_SCOPE);
4628 if (scope == NULL)
4629 return NULL;
4630
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004631 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632
4633 // compile "expr"
4634 if (compile_expr1(&p, cctx) == FAIL)
4635 return NULL;
4636
4637 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004638 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 JUMP_IF_FALSE, cctx) == FAIL)
4640 return FAIL;
4641
4642 return p;
4643}
4644
4645/*
4646 * compile "endwhile"
4647 */
4648 static char_u *
4649compile_endwhile(char_u *arg, cctx_T *cctx)
4650{
4651 scope_T *scope = cctx->ctx_scope;
4652
4653 if (scope == NULL || scope->se_type != WHILE_SCOPE)
4654 {
4655 emsg(_(e_while));
4656 return NULL;
4657 }
4658 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004659 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660
4661 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004662 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663
4664 // Fill in the "end" label in the WHILE statement so it can jump here.
4665 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004666 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667
4668 vim_free(scope);
4669
4670 return arg;
4671}
4672
4673/*
4674 * compile "continue"
4675 */
4676 static char_u *
4677compile_continue(char_u *arg, cctx_T *cctx)
4678{
4679 scope_T *scope = cctx->ctx_scope;
4680
4681 for (;;)
4682 {
4683 if (scope == NULL)
4684 {
4685 emsg(_(e_continue));
4686 return NULL;
4687 }
4688 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4689 break;
4690 scope = scope->se_outer;
4691 }
4692
4693 // Jump back to the FOR or WHILE instruction.
4694 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004695 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
4696 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 return arg;
4698}
4699
4700/*
4701 * compile "break"
4702 */
4703 static char_u *
4704compile_break(char_u *arg, cctx_T *cctx)
4705{
4706 scope_T *scope = cctx->ctx_scope;
4707 endlabel_T **el;
4708
4709 for (;;)
4710 {
4711 if (scope == NULL)
4712 {
4713 emsg(_(e_break));
4714 return NULL;
4715 }
4716 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4717 break;
4718 scope = scope->se_outer;
4719 }
4720
4721 // Jump to the end of the FOR or WHILE loop.
4722 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004723 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004725 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
4727 return FAIL;
4728
4729 return arg;
4730}
4731
4732/*
4733 * compile "{" start of block
4734 */
4735 static char_u *
4736compile_block(char_u *arg, cctx_T *cctx)
4737{
4738 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4739 return NULL;
4740 return skipwhite(arg + 1);
4741}
4742
4743/*
4744 * compile end of block: drop one scope
4745 */
4746 static void
4747compile_endblock(cctx_T *cctx)
4748{
4749 scope_T *scope = cctx->ctx_scope;
4750
4751 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004752 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 vim_free(scope);
4754}
4755
4756/*
4757 * compile "try"
4758 * Creates a new scope for the try-endtry, pointing to the first catch and
4759 * finally.
4760 * Creates another scope for the "try" block itself.
4761 * TRY instruction sets up exception handling at runtime.
4762 *
4763 * "try"
4764 * TRY -> catch1, -> finally push trystack entry
4765 * ... try block
4766 * "throw {exception}"
4767 * EVAL {exception}
4768 * THROW create exception
4769 * ... try block
4770 * " catch {expr}"
4771 * JUMP -> finally
4772 * catch1: PUSH exeception
4773 * EVAL {expr}
4774 * MATCH
4775 * JUMP nomatch -> catch2
4776 * CATCH remove exception
4777 * ... catch block
4778 * " catch"
4779 * JUMP -> finally
4780 * catch2: CATCH remove exception
4781 * ... catch block
4782 * " finally"
4783 * finally:
4784 * ... finally block
4785 * " endtry"
4786 * ENDTRY pop trystack entry, may rethrow
4787 */
4788 static char_u *
4789compile_try(char_u *arg, cctx_T *cctx)
4790{
4791 garray_T *instr = &cctx->ctx_instr;
4792 scope_T *try_scope;
4793 scope_T *scope;
4794
4795 // scope that holds the jumps that go to catch/finally/endtry
4796 try_scope = new_scope(cctx, TRY_SCOPE);
4797 if (try_scope == NULL)
4798 return NULL;
4799
4800 // "catch" is set when the first ":catch" is found.
4801 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004802 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803 if (generate_instr(cctx, ISN_TRY) == NULL)
4804 return NULL;
4805
4806 // scope for the try block itself
4807 scope = new_scope(cctx, BLOCK_SCOPE);
4808 if (scope == NULL)
4809 return NULL;
4810
4811 return arg;
4812}
4813
4814/*
4815 * compile "catch {expr}"
4816 */
4817 static char_u *
4818compile_catch(char_u *arg, cctx_T *cctx UNUSED)
4819{
4820 scope_T *scope = cctx->ctx_scope;
4821 garray_T *instr = &cctx->ctx_instr;
4822 char_u *p;
4823 isn_T *isn;
4824
4825 // end block scope from :try or :catch
4826 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4827 compile_endblock(cctx);
4828 scope = cctx->ctx_scope;
4829
4830 // Error if not in a :try scope
4831 if (scope == NULL || scope->se_type != TRY_SCOPE)
4832 {
4833 emsg(_(e_catch));
4834 return NULL;
4835 }
4836
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004837 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 {
4839 emsg(_("E1033: catch unreachable after catch-all"));
4840 return NULL;
4841 }
4842
4843 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004844 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 JUMP_ALWAYS, cctx) == FAIL)
4846 return NULL;
4847
4848 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004849 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 if (isn->isn_arg.try.try_catch == 0)
4851 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004852 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 {
4854 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004855 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 isn->isn_arg.jump.jump_where = instr->ga_len;
4857 }
4858
4859 p = skipwhite(arg);
4860 if (ends_excmd(*p))
4861 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004862 scope->se_u.se_try.ts_caught_all = TRUE;
4863 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 }
4865 else
4866 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004867 char_u *end;
4868 char_u *pat;
4869 char_u *tofree = NULL;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004870 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 // Push v:exception, push {expr} and MATCH
4873 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
4874
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004875 end = skip_regexp(p + 1, *p, TRUE, &tofree);
4876 if (*end != *p)
4877 {
4878 semsg(_("E1067: Separator mismatch: %s"), p);
4879 vim_free(tofree);
4880 return FAIL;
4881 }
4882 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004883 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004884 else
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004885 len = (int)(end - (tofree + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004886 pat = vim_strnsave(p + 1, len);
4887 vim_free(tofree);
4888 p += len + 2;
4889 if (pat == NULL)
4890 return FAIL;
4891 if (generate_PUSHS(cctx, pat) == FAIL)
4892 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4895 return NULL;
4896
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004897 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4899 return NULL;
4900 }
4901
4902 if (generate_instr(cctx, ISN_CATCH) == NULL)
4903 return NULL;
4904
4905 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4906 return NULL;
4907 return p;
4908}
4909
4910 static char_u *
4911compile_finally(char_u *arg, cctx_T *cctx)
4912{
4913 scope_T *scope = cctx->ctx_scope;
4914 garray_T *instr = &cctx->ctx_instr;
4915 isn_T *isn;
4916
4917 // end block scope from :try or :catch
4918 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4919 compile_endblock(cctx);
4920 scope = cctx->ctx_scope;
4921
4922 // Error if not in a :try scope
4923 if (scope == NULL || scope->se_type != TRY_SCOPE)
4924 {
4925 emsg(_(e_finally));
4926 return NULL;
4927 }
4928
4929 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004930 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 if (isn->isn_arg.try.try_finally != 0)
4932 {
4933 emsg(_(e_finally_dup));
4934 return NULL;
4935 }
4936
4937 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004938 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004940 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 {
4942 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004943 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 isn->isn_arg.jump.jump_where = instr->ga_len;
4945 }
4946
4947 isn->isn_arg.try.try_finally = instr->ga_len;
4948 // TODO: set index in ts_finally_label jumps
4949
4950 return arg;
4951}
4952
4953 static char_u *
4954compile_endtry(char_u *arg, cctx_T *cctx)
4955{
4956 scope_T *scope = cctx->ctx_scope;
4957 garray_T *instr = &cctx->ctx_instr;
4958 isn_T *isn;
4959
4960 // end block scope from :catch or :finally
4961 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4962 compile_endblock(cctx);
4963 scope = cctx->ctx_scope;
4964
4965 // Error if not in a :try scope
4966 if (scope == NULL || scope->se_type != TRY_SCOPE)
4967 {
4968 if (scope == NULL)
4969 emsg(_(e_no_endtry));
4970 else if (scope->se_type == WHILE_SCOPE)
4971 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01004972 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 emsg(_(e_endfor));
4974 else
4975 emsg(_(e_endif));
4976 return NULL;
4977 }
4978
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004979 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
4981 {
4982 emsg(_("E1032: missing :catch or :finally"));
4983 return NULL;
4984 }
4985
4986 // Fill in the "end" label in jumps at the end of the blocks, if not done
4987 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004988 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989
4990 // End :catch or :finally scope: set value in ISN_TRY instruction
4991 if (isn->isn_arg.try.try_finally == 0)
4992 isn->isn_arg.try.try_finally = instr->ga_len;
4993 compile_endblock(cctx);
4994
4995 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4996 return NULL;
4997 return arg;
4998}
4999
5000/*
5001 * compile "throw {expr}"
5002 */
5003 static char_u *
5004compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5005{
5006 char_u *p = skipwhite(arg);
5007
5008 if (ends_excmd(*p))
5009 {
5010 emsg(_(e_argreq));
5011 return NULL;
5012 }
5013 if (compile_expr1(&p, cctx) == FAIL)
5014 return NULL;
5015 if (may_generate_2STRING(-1, cctx) == FAIL)
5016 return NULL;
5017 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5018 return NULL;
5019
5020 return p;
5021}
5022
5023/*
5024 * compile "echo expr"
5025 */
5026 static char_u *
5027compile_echo(char_u *arg, int with_white, cctx_T *cctx)
5028{
5029 char_u *p = arg;
5030 int count = 0;
5031
Bram Moolenaarad39c092020-02-26 18:23:43 +01005032 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 {
5034 if (compile_expr1(&p, cctx) == FAIL)
5035 return NULL;
5036 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005037 p = skipwhite(p);
5038 if (ends_excmd(*p))
5039 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040 }
5041
5042 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01005043 return p;
5044}
5045
5046/*
5047 * compile "execute expr"
5048 */
5049 static char_u *
5050compile_execute(char_u *arg, cctx_T *cctx)
5051{
5052 char_u *p = arg;
5053 int count = 0;
5054
5055 for (;;)
5056 {
5057 if (compile_expr1(&p, cctx) == FAIL)
5058 return NULL;
5059 ++count;
5060 p = skipwhite(p);
5061 if (ends_excmd(*p))
5062 break;
5063 }
5064
5065 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066
5067 return p;
5068}
5069
5070/*
5071 * After ex_function() has collected all the function lines: parse and compile
5072 * the lines into instructions.
5073 * Adds the function to "def_functions".
5074 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5075 * return statement (used for lambda).
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005076 * This can be used recursively through compile_lambda(), which may reallocate
5077 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 */
5079 void
5080compile_def_function(ufunc_T *ufunc, int set_return_type)
5081{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082 char_u *line = NULL;
5083 char_u *p;
5084 exarg_T ea;
5085 char *errormsg = NULL; // error message
5086 int had_return = FALSE;
5087 cctx_T cctx;
5088 garray_T *instr;
5089 int called_emsg_before = called_emsg;
5090 int ret = FAIL;
5091 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005092 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005095 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005096
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005097 if (ufunc->uf_dfunc_idx >= 0)
5098 {
5099 // Redefining a function that was compiled before.
5100 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5101
5102 // Free old instructions.
5103 delete_def_function_contents(dfunc);
5104 }
5105 else
5106 {
5107 // Add the function to "def_functions".
5108 if (ga_grow(&def_functions, 1) == FAIL)
5109 return;
5110 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
5111 vim_memset(dfunc, 0, sizeof(dfunc_T));
5112 dfunc->df_idx = def_functions.ga_len;
5113 ufunc->uf_dfunc_idx = dfunc->df_idx;
5114 dfunc->df_ufunc = ufunc;
5115 ++def_functions.ga_len;
5116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117 }
5118
5119 vim_memset(&cctx, 0, sizeof(cctx));
5120 cctx.ctx_ufunc = ufunc;
5121 cctx.ctx_lnum = -1;
5122 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5123 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5124 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5125 cctx.ctx_type_list = &ufunc->uf_type_list;
5126 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5127 instr = &cctx.ctx_instr;
5128
5129 // Most modern script version.
5130 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5131
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005132 if (ufunc->uf_def_args.ga_len > 0)
5133 {
5134 int count = ufunc->uf_def_args.ga_len;
5135 int i;
5136 char_u *arg;
5137 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5138
5139 // Produce instructions for the default values of optional arguments.
5140 // Store the instruction index in uf_def_arg_idx[] so that we know
5141 // where to start when the function is called, depending on the number
5142 // of arguments.
5143 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5144 if (ufunc->uf_def_arg_idx == NULL)
5145 goto erret;
5146 for (i = 0; i < count; ++i)
5147 {
5148 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5149 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
5150 if (compile_expr1(&arg, &cctx) == FAIL
5151 || generate_STORE(&cctx, ISN_STORE,
5152 i - count - off, NULL) == FAIL)
5153 goto erret;
5154 }
5155
5156 // If a varargs is following, push an empty list.
5157 if (ufunc->uf_va_name != NULL)
5158 {
5159 if (generate_NEWLIST(&cctx, 0) == FAIL
5160 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
5161 goto erret;
5162 }
5163
5164 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5165 }
5166
5167 /*
5168 * Loop over all the lines of the function and generate instructions.
5169 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 for (;;)
5171 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005172 int is_ex_command;
5173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005174 if (line != NULL && *line == '|')
5175 // the line continues after a '|'
5176 ++line;
5177 else if (line != NULL && *line != NUL)
5178 {
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005179 if (emsg_before == called_emsg)
5180 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 goto erret;
5182 }
5183 else
5184 {
5185 do
5186 {
5187 ++cctx.ctx_lnum;
5188 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5189 break;
5190 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
5191 } while (line == NULL);
5192 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5193 break;
5194 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
5195 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005196 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197
5198 had_return = FALSE;
5199 vim_memset(&ea, 0, sizeof(ea));
5200 ea.cmdlinep = &line;
5201 ea.cmd = skipwhite(line);
5202
5203 // "}" ends a block scope
5204 if (*ea.cmd == '}')
5205 {
5206 scopetype_T stype = cctx.ctx_scope == NULL
5207 ? NO_SCOPE : cctx.ctx_scope->se_type;
5208
5209 if (stype == BLOCK_SCOPE)
5210 {
5211 compile_endblock(&cctx);
5212 line = ea.cmd;
5213 }
5214 else
5215 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005216 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217 goto erret;
5218 }
5219 if (line != NULL)
5220 line = skipwhite(ea.cmd + 1);
5221 continue;
5222 }
5223
5224 // "{" starts a block scope
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01005225 // "{'a': 1}->func() is something else
5226 if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227 {
5228 line = compile_block(ea.cmd, &cctx);
5229 continue;
5230 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005231 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005232
5233 /*
5234 * COMMAND MODIFIERS
5235 */
5236 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5237 {
5238 if (errormsg != NULL)
5239 goto erret;
5240 // empty line or comment
5241 line = (char_u *)"";
5242 continue;
5243 }
5244
5245 // Skip ":call" to get to the function name.
5246 if (checkforcmd(&ea.cmd, "call", 3))
5247 ea.cmd = skipwhite(ea.cmd);
5248
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005249 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005250 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005251 // Assuming the command starts with a variable or function name,
5252 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5253 // val".
5254 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5255 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005256 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005257 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005259 int oplen;
5260 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005262 oplen = assignment_len(skipwhite(p), &heredoc);
5263 if (oplen > 0)
5264 {
5265 // Recognize an assignment if we recognize the variable
5266 // name:
5267 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005268 // "local = expr" where "local" is a local var.
5269 // "script = expr" where "script" is a script-local var.
5270 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005271 // "&opt = expr"
5272 // "$ENV = expr"
5273 // "@r = expr"
5274 if (*ea.cmd == '&'
5275 || *ea.cmd == '$'
5276 || *ea.cmd == '@'
5277 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5278 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5279 || lookup_script(ea.cmd, p - ea.cmd) == OK
5280 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5281 {
5282 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5283 if (line == NULL)
5284 goto erret;
5285 continue;
5286 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005287 }
5288 }
5289 }
5290
5291 /*
5292 * COMMAND after range
5293 */
5294 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005295 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5296 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297
5298 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5299 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005300 if (cctx.ctx_skip == TRUE)
5301 {
5302 line += STRLEN(line);
5303 continue;
5304 }
5305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306 // Expression or function call.
5307 if (ea.cmdidx == CMD_eval)
5308 {
5309 p = ea.cmd;
5310 if (compile_expr1(&p, &cctx) == FAIL)
5311 goto erret;
5312
5313 // drop the return value
5314 generate_instr_drop(&cctx, ISN_DROP, 1);
5315 line = p;
5316 continue;
5317 }
5318 if (ea.cmdidx == CMD_let)
5319 {
5320 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5321 if (line == NULL)
5322 goto erret;
5323 continue;
5324 }
5325 iemsg("Command from find_ex_command() not handled");
5326 goto erret;
5327 }
5328
5329 p = skipwhite(p);
5330
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005331 if (cctx.ctx_skip == TRUE
5332 && ea.cmdidx != CMD_elseif
5333 && ea.cmdidx != CMD_else
5334 && ea.cmdidx != CMD_endif)
5335 {
5336 line += STRLEN(line);
5337 continue;
5338 }
5339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005340 switch (ea.cmdidx)
5341 {
5342 case CMD_def:
5343 case CMD_function:
5344 // TODO: Nested function
5345 emsg("Nested function not implemented yet");
5346 goto erret;
5347
5348 case CMD_return:
5349 line = compile_return(p, set_return_type, &cctx);
5350 had_return = TRUE;
5351 break;
5352
5353 case CMD_let:
5354 case CMD_const:
5355 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5356 break;
5357
5358 case CMD_import:
5359 line = compile_import(p, &cctx);
5360 break;
5361
5362 case CMD_if:
5363 line = compile_if(p, &cctx);
5364 break;
5365 case CMD_elseif:
5366 line = compile_elseif(p, &cctx);
5367 break;
5368 case CMD_else:
5369 line = compile_else(p, &cctx);
5370 break;
5371 case CMD_endif:
5372 line = compile_endif(p, &cctx);
5373 break;
5374
5375 case CMD_while:
5376 line = compile_while(p, &cctx);
5377 break;
5378 case CMD_endwhile:
5379 line = compile_endwhile(p, &cctx);
5380 break;
5381
5382 case CMD_for:
5383 line = compile_for(p, &cctx);
5384 break;
5385 case CMD_endfor:
5386 line = compile_endfor(p, &cctx);
5387 break;
5388 case CMD_continue:
5389 line = compile_continue(p, &cctx);
5390 break;
5391 case CMD_break:
5392 line = compile_break(p, &cctx);
5393 break;
5394
5395 case CMD_try:
5396 line = compile_try(p, &cctx);
5397 break;
5398 case CMD_catch:
5399 line = compile_catch(p, &cctx);
5400 break;
5401 case CMD_finally:
5402 line = compile_finally(p, &cctx);
5403 break;
5404 case CMD_endtry:
5405 line = compile_endtry(p, &cctx);
5406 break;
5407 case CMD_throw:
5408 line = compile_throw(p, &cctx);
5409 break;
5410
5411 case CMD_echo:
5412 line = compile_echo(p, TRUE, &cctx);
5413 break;
5414 case CMD_echon:
5415 line = compile_echo(p, FALSE, &cctx);
5416 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005417 case CMD_execute:
5418 line = compile_execute(p, &cctx);
5419 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005420
5421 default:
5422 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005423 // TODO:
5424 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005425 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 generate_EXEC(&cctx, line);
5427 line = (char_u *)"";
5428 break;
5429 }
5430 if (line == NULL)
5431 goto erret;
5432
5433 if (cctx.ctx_type_stack.ga_len < 0)
5434 {
5435 iemsg("Type stack underflow");
5436 goto erret;
5437 }
5438 }
5439
5440 if (cctx.ctx_scope != NULL)
5441 {
5442 if (cctx.ctx_scope->se_type == IF_SCOPE)
5443 emsg(_(e_endif));
5444 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5445 emsg(_(e_endwhile));
5446 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5447 emsg(_(e_endfor));
5448 else
5449 emsg(_("E1026: Missing }"));
5450 goto erret;
5451 }
5452
5453 if (!had_return)
5454 {
5455 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5456 {
5457 emsg(_("E1027: Missing return statement"));
5458 goto erret;
5459 }
5460
5461 // Return zero if there is no return at the end.
5462 generate_PUSHNR(&cctx, 0);
5463 generate_instr(&cctx, ISN_RETURN);
5464 }
5465
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005466 {
5467 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5468 + ufunc->uf_dfunc_idx;
5469 dfunc->df_deleted = FALSE;
5470 dfunc->df_instr = instr->ga_data;
5471 dfunc->df_instr_count = instr->ga_len;
5472 dfunc->df_varcount = cctx.ctx_max_local;
5473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474
5475 ret = OK;
5476
5477erret:
5478 if (ret == FAIL)
5479 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005480 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005481 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5482 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005483
5484 for (idx = 0; idx < instr->ga_len; ++idx)
5485 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005486 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005488 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005489 if (!dfunc->df_deleted)
5490 --def_functions.ga_len;
5491
5492 // Don't execute this function body.
5493 ga_clear_strings(&ufunc->uf_lines);
5494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005495 if (errormsg != NULL)
5496 emsg(errormsg);
5497 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005498 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005499 }
5500
5501 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005502 free_imported(&cctx);
5503 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005504 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005505}
5506
5507/*
5508 * Delete an instruction, free what it contains.
5509 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01005510 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005511delete_instr(isn_T *isn)
5512{
5513 switch (isn->isn_type)
5514 {
5515 case ISN_EXEC:
5516 case ISN_LOADENV:
5517 case ISN_LOADG:
5518 case ISN_LOADOPT:
5519 case ISN_MEMBER:
5520 case ISN_PUSHEXC:
5521 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005522 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005523 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005524 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525 vim_free(isn->isn_arg.string);
5526 break;
5527
5528 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005529 case ISN_STORES:
5530 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531 break;
5532
5533 case ISN_STOREOPT:
5534 vim_free(isn->isn_arg.storeopt.so_name);
5535 break;
5536
5537 case ISN_PUSHBLOB: // push blob isn_arg.blob
5538 blob_unref(isn->isn_arg.blob);
5539 break;
5540
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005541 case ISN_PUSHPARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01005542 partial_unref(isn->isn_arg.partial);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005543 break;
5544
5545 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005546#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005547 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005548#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005549 break;
5550
5551 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005552#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005553 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005554#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005555 break;
5556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557 case ISN_UCALL:
5558 vim_free(isn->isn_arg.ufunc.cuf_name);
5559 break;
5560
5561 case ISN_2BOOL:
5562 case ISN_2STRING:
5563 case ISN_ADDBLOB:
5564 case ISN_ADDLIST:
5565 case ISN_BCALL:
5566 case ISN_CATCH:
5567 case ISN_CHECKNR:
5568 case ISN_CHECKTYPE:
5569 case ISN_COMPAREANY:
5570 case ISN_COMPAREBLOB:
5571 case ISN_COMPAREBOOL:
5572 case ISN_COMPAREDICT:
5573 case ISN_COMPAREFLOAT:
5574 case ISN_COMPAREFUNC:
5575 case ISN_COMPARELIST:
5576 case ISN_COMPARENR:
5577 case ISN_COMPAREPARTIAL:
5578 case ISN_COMPARESPECIAL:
5579 case ISN_COMPARESTRING:
5580 case ISN_CONCAT:
5581 case ISN_DCALL:
5582 case ISN_DROP:
5583 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01005584 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585 case ISN_ENDTRY:
5586 case ISN_FOR:
5587 case ISN_FUNCREF:
5588 case ISN_INDEX:
5589 case ISN_JUMP:
5590 case ISN_LOAD:
5591 case ISN_LOADSCRIPT:
5592 case ISN_LOADREG:
5593 case ISN_LOADV:
5594 case ISN_NEGATENR:
5595 case ISN_NEWDICT:
5596 case ISN_NEWLIST:
5597 case ISN_OPNR:
5598 case ISN_OPFLOAT:
5599 case ISN_OPANY:
5600 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005601 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005602 case ISN_PUSHF:
5603 case ISN_PUSHNR:
5604 case ISN_PUSHBOOL:
5605 case ISN_PUSHSPEC:
5606 case ISN_RETURN:
5607 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005608 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005610 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005611 case ISN_STORESCRIPT:
5612 case ISN_THROW:
5613 case ISN_TRY:
5614 // nothing allocated
5615 break;
5616 }
5617}
5618
5619/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01005620 * Free all instructions for "dfunc".
5621 */
5622 static void
5623delete_def_function_contents(dfunc_T *dfunc)
5624{
5625 int idx;
5626
5627 ga_clear(&dfunc->df_def_args_isn);
5628
5629 if (dfunc->df_instr != NULL)
5630 {
5631 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5632 delete_instr(dfunc->df_instr + idx);
5633 VIM_CLEAR(dfunc->df_instr);
5634 }
5635
5636 dfunc->df_deleted = TRUE;
5637}
5638
5639/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640 * When a user function is deleted, delete any associated def function.
5641 */
5642 void
5643delete_def_function(ufunc_T *ufunc)
5644{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005645 if (ufunc->uf_dfunc_idx >= 0)
5646 {
5647 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5648 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005649
Bram Moolenaar20431c92020-03-20 18:39:46 +01005650 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005651 }
5652}
5653
5654#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005655/*
5656 * Free all functions defined with ":def".
5657 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658 void
5659free_def_functions(void)
5660{
Bram Moolenaar20431c92020-03-20 18:39:46 +01005661 int idx;
5662
5663 for (idx = 0; idx < def_functions.ga_len; ++idx)
5664 {
5665 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
5666
5667 delete_def_function_contents(dfunc);
5668 }
5669
5670 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005671}
5672#endif
5673
5674
5675#endif // FEAT_EVAL