blob: 64ebddfb99a0241df7c2dd18208cdda3be8f39af [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
26/*
27 * Chain of jump instructions where the end label needs to be set.
28 */
29typedef struct endlabel_S endlabel_T;
30struct endlabel_S {
31 endlabel_T *el_next; // chain end_label locations
32 int el_end_label; // instruction idx where to set end
33};
34
35/*
36 * info specific for the scope of :if / elseif / else
37 */
38typedef struct {
39 int is_if_label; // instruction idx at IF or ELSEIF
40 endlabel_T *is_end_label; // instructions to set end label
41} ifscope_T;
42
43/*
44 * info specific for the scope of :while
45 */
46typedef struct {
47 int ws_top_label; // instruction idx at WHILE
48 endlabel_T *ws_end_label; // instructions to set end
49} whilescope_T;
50
51/*
52 * info specific for the scope of :for
53 */
54typedef struct {
55 int fs_top_label; // instruction idx at FOR
56 endlabel_T *fs_end_label; // break instructions
57} forscope_T;
58
59/*
60 * info specific for the scope of :try
61 */
62typedef struct {
63 int ts_try_label; // instruction idx at TRY
64 endlabel_T *ts_end_label; // jump to :finally or :endtry
65 int ts_catch_label; // instruction idx of last CATCH
66 int ts_caught_all; // "catch" without argument encountered
67} tryscope_T;
68
69typedef enum {
70 NO_SCOPE,
71 IF_SCOPE,
72 WHILE_SCOPE,
73 FOR_SCOPE,
74 TRY_SCOPE,
75 BLOCK_SCOPE
76} scopetype_T;
77
78/*
79 * Info for one scope, pointed to by "ctx_scope".
80 */
81typedef struct scope_S scope_T;
82struct scope_S {
83 scope_T *se_outer; // scope containing this one
84 scopetype_T se_type;
85 int se_local_count; // ctx_locals.ga_len before scope
86 union {
87 ifscope_T se_if;
88 whilescope_T se_while;
89 forscope_T se_for;
90 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +010091 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092};
93
94/*
95 * Entry for "ctx_locals". Used for arguments and local variables.
96 */
97typedef struct {
98 char_u *lv_name;
99 type_T *lv_type;
100 int lv_const; // when TRUE cannot be assigned to
101 int lv_arg; // when TRUE this is an argument
102} lvar_T;
103
104/*
105 * Context for compiling lines of Vim script.
106 * Stores info about the local variables and condition stack.
107 */
108struct cctx_S {
109 ufunc_T *ctx_ufunc; // current function
110 int ctx_lnum; // line number in current function
111 garray_T ctx_instr; // generated instructions
112
113 garray_T ctx_locals; // currently visible local variables
114 int ctx_max_local; // maximum number of locals at one time
115
116 garray_T ctx_imports; // imported items
117
Bram Moolenaara259d8d2020-01-31 20:10:50 +0100118 int ctx_skip; // when TRUE skip commands, when FALSE skip
119 // commands after "else"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100120 scope_T *ctx_scope; // current scope, NULL at toplevel
121
122 garray_T ctx_type_stack; // type of each item on the stack
123 garray_T *ctx_type_list; // space for adding types
124};
125
126static char e_var_notfound[] = N_("E1001: variable not found: %s");
127static char e_syntax_at[] = N_("E1002: Syntax error at %s");
128
129static int compile_expr1(char_u **arg, cctx_T *cctx);
130static int compile_expr2(char_u **arg, cctx_T *cctx);
131static int compile_expr3(char_u **arg, cctx_T *cctx);
Bram Moolenaar20431c92020-03-20 18:39:46 +0100132static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133
134/*
135 * Lookup variable "name" in the local scope and return the index.
136 */
137 static int
138lookup_local(char_u *name, size_t len, cctx_T *cctx)
139{
140 int idx;
141
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100142 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 return -1;
144 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
145 {
146 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
147
148 if (STRNCMP(name, lvar->lv_name, len) == 0
149 && STRLEN(lvar->lv_name) == len)
150 return idx;
151 }
152 return -1;
153}
154
155/*
156 * Lookup an argument in the current function.
157 * Returns the argument index or -1 if not found.
158 */
159 static int
160lookup_arg(char_u *name, size_t len, cctx_T *cctx)
161{
162 int idx;
163
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100164 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 return -1;
166 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
167 {
168 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
169
170 if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
171 return idx;
172 }
173 return -1;
174}
175
176/*
177 * Lookup a vararg argument in the current function.
178 * Returns TRUE if there is a match.
179 */
180 static int
181lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
182{
183 char_u *va_name = cctx->ctx_ufunc->uf_va_name;
184
185 return len > 0 && va_name != NULL
186 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
187}
188
189/*
190 * Lookup a variable in the current script.
191 * Returns OK or FAIL.
192 */
193 static int
194lookup_script(char_u *name, size_t len)
195{
196 int cc;
197 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
198 dictitem_T *di;
199
200 cc = name[len];
201 name[len] = NUL;
202 di = find_var_in_ht(ht, 0, name, TRUE);
203 name[len] = cc;
204 return di == NULL ? FAIL: OK;
205}
206
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100207/*
208 * Check if "p[len]" is already defined, either in script "import_sid" or in
209 * compilation context "cctx".
210 * Return FAIL and give an error if it defined.
211 */
212 int
213check_defined(char_u *p, int len, cctx_T *cctx)
214{
215 if (lookup_script(p, len) == OK
216 || (cctx != NULL
217 && (lookup_local(p, len, cctx) >= 0
218 || find_imported(p, len, cctx) != NULL)))
219 {
220 semsg("E1073: imported name already defined: %s", p);
221 return FAIL;
222 }
223 return OK;
224}
225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 static type_T *
227get_list_type(type_T *member_type, garray_T *type_list)
228{
229 type_T *type;
230
231 // recognize commonly used types
232 if (member_type->tt_type == VAR_UNKNOWN)
233 return &t_list_any;
Bram Moolenaar436472f2020-02-20 22:54:43 +0100234 if (member_type->tt_type == VAR_VOID)
235 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100236 if (member_type->tt_type == VAR_BOOL)
237 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 if (member_type->tt_type == VAR_NUMBER)
239 return &t_list_number;
240 if (member_type->tt_type == VAR_STRING)
241 return &t_list_string;
242
243 // Not a common type, create a new entry.
244 if (ga_grow(type_list, 1) == FAIL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100245 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 type = ((type_T *)type_list->ga_data) + type_list->ga_len;
247 ++type_list->ga_len;
248 type->tt_type = VAR_LIST;
249 type->tt_member = member_type;
250 return type;
251}
252
253 static type_T *
254get_dict_type(type_T *member_type, garray_T *type_list)
255{
256 type_T *type;
257
258 // recognize commonly used types
259 if (member_type->tt_type == VAR_UNKNOWN)
260 return &t_dict_any;
Bram Moolenaar436472f2020-02-20 22:54:43 +0100261 if (member_type->tt_type == VAR_VOID)
262 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100263 if (member_type->tt_type == VAR_BOOL)
264 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 if (member_type->tt_type == VAR_NUMBER)
266 return &t_dict_number;
267 if (member_type->tt_type == VAR_STRING)
268 return &t_dict_string;
269
270 // Not a common type, create a new entry.
271 if (ga_grow(type_list, 1) == FAIL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100272 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273 type = ((type_T *)type_list->ga_data) + type_list->ga_len;
274 ++type_list->ga_len;
275 type->tt_type = VAR_DICT;
276 type->tt_member = member_type;
277 return type;
278}
279
280/////////////////////////////////////////////////////////////////////
281// Following generate_ functions expect the caller to call ga_grow().
282
Bram Moolenaar080457c2020-03-03 21:53:32 +0100283#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
284#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100286/*
287 * Generate an instruction without arguments.
288 * Returns a pointer to the new instruction, NULL if failed.
289 */
290 static isn_T *
291generate_instr(cctx_T *cctx, isntype_T isn_type)
292{
293 garray_T *instr = &cctx->ctx_instr;
294 isn_T *isn;
295
Bram Moolenaar080457c2020-03-03 21:53:32 +0100296 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 if (ga_grow(instr, 1) == FAIL)
298 return NULL;
299 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
300 isn->isn_type = isn_type;
301 isn->isn_lnum = cctx->ctx_lnum + 1;
302 ++instr->ga_len;
303
304 return isn;
305}
306
307/*
308 * Generate an instruction without arguments.
309 * "drop" will be removed from the stack.
310 * Returns a pointer to the new instruction, NULL if failed.
311 */
312 static isn_T *
313generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
314{
315 garray_T *stack = &cctx->ctx_type_stack;
316
Bram Moolenaar080457c2020-03-03 21:53:32 +0100317 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318 stack->ga_len -= drop;
319 return generate_instr(cctx, isn_type);
320}
321
322/*
323 * Generate instruction "isn_type" and put "type" on the type stack.
324 */
325 static isn_T *
326generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
327{
328 isn_T *isn;
329 garray_T *stack = &cctx->ctx_type_stack;
330
331 if ((isn = generate_instr(cctx, isn_type)) == NULL)
332 return NULL;
333
334 if (ga_grow(stack, 1) == FAIL)
335 return NULL;
336 ((type_T **)stack->ga_data)[stack->ga_len] = type;
337 ++stack->ga_len;
338
339 return isn;
340}
341
342/*
343 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
344 */
345 static int
346may_generate_2STRING(int offset, cctx_T *cctx)
347{
348 isn_T *isn;
349 garray_T *stack = &cctx->ctx_type_stack;
350 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
351
352 if ((*type)->tt_type == VAR_STRING)
353 return OK;
354 *type = &t_string;
355
356 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
357 return FAIL;
358 isn->isn_arg.number = offset;
359
360 return OK;
361}
362
363 static int
364check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
365{
366 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_UNKNOWN)
367 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
368 || type2 == VAR_UNKNOWN)))
369 {
370 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100371 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100372 else
373 semsg(_("E1036: %c requires number or float arguments"), *op);
374 return FAIL;
375 }
376 return OK;
377}
378
379/*
380 * Generate an instruction with two arguments. The instruction depends on the
381 * type of the arguments.
382 */
383 static int
384generate_two_op(cctx_T *cctx, char_u *op)
385{
386 garray_T *stack = &cctx->ctx_type_stack;
387 type_T *type1;
388 type_T *type2;
389 vartype_T vartype;
390 isn_T *isn;
391
Bram Moolenaar080457c2020-03-03 21:53:32 +0100392 RETURN_OK_IF_SKIP(cctx);
393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100394 // Get the known type of the two items on the stack. If they are matching
395 // use a type-specific instruction. Otherwise fall back to runtime type
396 // checking.
397 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
398 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
399 vartype = VAR_UNKNOWN;
400 if (type1->tt_type == type2->tt_type
401 && (type1->tt_type == VAR_NUMBER
402 || type1->tt_type == VAR_LIST
403#ifdef FEAT_FLOAT
404 || type1->tt_type == VAR_FLOAT
405#endif
406 || type1->tt_type == VAR_BLOB))
407 vartype = type1->tt_type;
408
409 switch (*op)
410 {
411 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100412 && type1->tt_type != VAR_UNKNOWN
413 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 && check_number_or_float(
415 type1->tt_type, type2->tt_type, op) == FAIL)
416 return FAIL;
417 isn = generate_instr_drop(cctx,
418 vartype == VAR_NUMBER ? ISN_OPNR
419 : vartype == VAR_LIST ? ISN_ADDLIST
420 : vartype == VAR_BLOB ? ISN_ADDBLOB
421#ifdef FEAT_FLOAT
422 : vartype == VAR_FLOAT ? ISN_OPFLOAT
423#endif
424 : ISN_OPANY, 1);
425 if (isn != NULL)
426 isn->isn_arg.op.op_type = EXPR_ADD;
427 break;
428
429 case '-':
430 case '*':
431 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
432 op) == FAIL)
433 return FAIL;
434 if (vartype == VAR_NUMBER)
435 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
436#ifdef FEAT_FLOAT
437 else if (vartype == VAR_FLOAT)
438 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
439#endif
440 else
441 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
442 if (isn != NULL)
443 isn->isn_arg.op.op_type = *op == '*'
444 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
445 break;
446
447 case '%': if ((type1->tt_type != VAR_UNKNOWN
448 && type1->tt_type != VAR_NUMBER)
449 || (type2->tt_type != VAR_UNKNOWN
450 && type2->tt_type != VAR_NUMBER))
451 {
452 emsg(_("E1035: % requires number arguments"));
453 return FAIL;
454 }
455 isn = generate_instr_drop(cctx,
456 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
457 if (isn != NULL)
458 isn->isn_arg.op.op_type = EXPR_REM;
459 break;
460 }
461
462 // correct type of result
463 if (vartype == VAR_UNKNOWN)
464 {
465 type_T *type = &t_any;
466
467#ifdef FEAT_FLOAT
468 // float+number and number+float results in float
469 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
470 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
471 type = &t_float;
472#endif
473 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
474 }
475
476 return OK;
477}
478
479/*
480 * Generate an ISN_COMPARE* instruction with a boolean result.
481 */
482 static int
483generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
484{
485 isntype_T isntype = ISN_DROP;
486 isn_T *isn;
487 garray_T *stack = &cctx->ctx_type_stack;
488 vartype_T type1;
489 vartype_T type2;
490
Bram Moolenaar080457c2020-03-03 21:53:32 +0100491 RETURN_OK_IF_SKIP(cctx);
492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493 // Get the known type of the two items on the stack. If they are matching
494 // use a type-specific instruction. Otherwise fall back to runtime type
495 // checking.
496 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
497 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
498 if (type1 == type2)
499 {
500 switch (type1)
501 {
502 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
503 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
504 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
505 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
506 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
507 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
508 case VAR_LIST: isntype = ISN_COMPARELIST; break;
509 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
510 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
511 case VAR_PARTIAL: isntype = ISN_COMPAREPARTIAL; break;
512 default: isntype = ISN_COMPAREANY; break;
513 }
514 }
515 else if (type1 == VAR_UNKNOWN || type2 == VAR_UNKNOWN
516 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
517 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
518 isntype = ISN_COMPAREANY;
519
520 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
521 && (isntype == ISN_COMPAREBOOL
522 || isntype == ISN_COMPARESPECIAL
523 || isntype == ISN_COMPARENR
524 || isntype == ISN_COMPAREFLOAT))
525 {
526 semsg(_("E1037: Cannot use \"%s\" with %s"),
527 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
528 return FAIL;
529 }
530 if (isntype == ISN_DROP
531 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
532 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
533 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
534 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
535 && exptype != EXPR_IS && exptype != EXPR_ISNOT
536 && (type1 == VAR_BLOB || type2 == VAR_BLOB
537 || type1 == VAR_LIST || type2 == VAR_LIST))))
538 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100539 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100540 vartype_name(type1), vartype_name(type2));
541 return FAIL;
542 }
543
544 if ((isn = generate_instr(cctx, isntype)) == NULL)
545 return FAIL;
546 isn->isn_arg.op.op_type = exptype;
547 isn->isn_arg.op.op_ic = ic;
548
549 // takes two arguments, puts one bool back
550 if (stack->ga_len >= 2)
551 {
552 --stack->ga_len;
553 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
554 }
555
556 return OK;
557}
558
559/*
560 * Generate an ISN_2BOOL instruction.
561 */
562 static int
563generate_2BOOL(cctx_T *cctx, int invert)
564{
565 isn_T *isn;
566 garray_T *stack = &cctx->ctx_type_stack;
567
Bram Moolenaar080457c2020-03-03 21:53:32 +0100568 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
570 return FAIL;
571 isn->isn_arg.number = invert;
572
573 // type becomes bool
574 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
575
576 return OK;
577}
578
579 static int
580generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
581{
582 isn_T *isn;
583 garray_T *stack = &cctx->ctx_type_stack;
584
Bram Moolenaar080457c2020-03-03 21:53:32 +0100585 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
587 return FAIL;
588 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
589 isn->isn_arg.type.ct_off = offset;
590
591 // type becomes vartype
592 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
593
594 return OK;
595}
596
597/*
598 * Generate an ISN_PUSHNR instruction.
599 */
600 static int
601generate_PUSHNR(cctx_T *cctx, varnumber_T number)
602{
603 isn_T *isn;
604
Bram Moolenaar080457c2020-03-03 21:53:32 +0100605 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
607 return FAIL;
608 isn->isn_arg.number = number;
609
610 return OK;
611}
612
613/*
614 * Generate an ISN_PUSHBOOL instruction.
615 */
616 static int
617generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
618{
619 isn_T *isn;
620
Bram Moolenaar080457c2020-03-03 21:53:32 +0100621 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
623 return FAIL;
624 isn->isn_arg.number = number;
625
626 return OK;
627}
628
629/*
630 * Generate an ISN_PUSHSPEC instruction.
631 */
632 static int
633generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
634{
635 isn_T *isn;
636
Bram Moolenaar080457c2020-03-03 21:53:32 +0100637 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
639 return FAIL;
640 isn->isn_arg.number = number;
641
642 return OK;
643}
644
645#ifdef FEAT_FLOAT
646/*
647 * Generate an ISN_PUSHF instruction.
648 */
649 static int
650generate_PUSHF(cctx_T *cctx, float_T fnumber)
651{
652 isn_T *isn;
653
Bram Moolenaar080457c2020-03-03 21:53:32 +0100654 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
656 return FAIL;
657 isn->isn_arg.fnumber = fnumber;
658
659 return OK;
660}
661#endif
662
663/*
664 * Generate an ISN_PUSHS instruction.
665 * Consumes "str".
666 */
667 static int
668generate_PUSHS(cctx_T *cctx, char_u *str)
669{
670 isn_T *isn;
671
Bram Moolenaar080457c2020-03-03 21:53:32 +0100672 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
674 return FAIL;
675 isn->isn_arg.string = str;
676
677 return OK;
678}
679
680/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100681 * Generate an ISN_PUSHCHANNEL instruction.
682 * Consumes "channel".
683 */
684 static int
685generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
686{
687 isn_T *isn;
688
Bram Moolenaar080457c2020-03-03 21:53:32 +0100689 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100690 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
691 return FAIL;
692 isn->isn_arg.channel = channel;
693
694 return OK;
695}
696
697/*
698 * Generate an ISN_PUSHJOB instruction.
699 * Consumes "job".
700 */
701 static int
702generate_PUSHJOB(cctx_T *cctx, job_T *job)
703{
704 isn_T *isn;
705
Bram Moolenaar080457c2020-03-03 21:53:32 +0100706 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100707 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100708 return FAIL;
709 isn->isn_arg.job = job;
710
711 return OK;
712}
713
714/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 * Generate an ISN_PUSHBLOB instruction.
716 * Consumes "blob".
717 */
718 static int
719generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
720{
721 isn_T *isn;
722
Bram Moolenaar080457c2020-03-03 21:53:32 +0100723 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
725 return FAIL;
726 isn->isn_arg.blob = blob;
727
728 return OK;
729}
730
731/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100732 * Generate an ISN_PUSHFUNC instruction with name "name".
733 * Consumes "name".
734 */
735 static int
736generate_PUSHFUNC(cctx_T *cctx, char_u *name)
737{
738 isn_T *isn;
739
Bram Moolenaar080457c2020-03-03 21:53:32 +0100740 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100741 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, &t_func_void)) == NULL)
742 return FAIL;
743 isn->isn_arg.string = name;
744
745 return OK;
746}
747
748/*
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100749 * Generate an ISN_PUSHPARTIAL instruction with partial "part".
750 * Consumes "name".
751 */
752 static int
753generate_PUSHPARTIAL(cctx_T *cctx, partial_T *part)
754{
755 isn_T *isn;
756
Bram Moolenaar080457c2020-03-03 21:53:32 +0100757 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100758 if ((isn = generate_instr_type(cctx, ISN_PUSHPARTIAL,
759 &t_partial_any)) == NULL)
760 return FAIL;
761 isn->isn_arg.partial = part;
762
763 return OK;
764}
765
766/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 * Generate an ISN_STORE instruction.
768 */
769 static int
770generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
771{
772 isn_T *isn;
773
Bram Moolenaar080457c2020-03-03 21:53:32 +0100774 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
776 return FAIL;
777 if (name != NULL)
778 isn->isn_arg.string = vim_strsave(name);
779 else
780 isn->isn_arg.number = idx;
781
782 return OK;
783}
784
785/*
786 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
787 */
788 static int
789generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
790{
791 isn_T *isn;
792
Bram Moolenaar080457c2020-03-03 21:53:32 +0100793 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
795 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100796 isn->isn_arg.storenr.stnr_idx = idx;
797 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798
799 return OK;
800}
801
802/*
803 * Generate an ISN_STOREOPT instruction
804 */
805 static int
806generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
807{
808 isn_T *isn;
809
Bram Moolenaar080457c2020-03-03 21:53:32 +0100810 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
812 return FAIL;
813 isn->isn_arg.storeopt.so_name = vim_strsave(name);
814 isn->isn_arg.storeopt.so_flags = opt_flags;
815
816 return OK;
817}
818
819/*
820 * Generate an ISN_LOAD or similar instruction.
821 */
822 static int
823generate_LOAD(
824 cctx_T *cctx,
825 isntype_T isn_type,
826 int idx,
827 char_u *name,
828 type_T *type)
829{
830 isn_T *isn;
831
Bram Moolenaar080457c2020-03-03 21:53:32 +0100832 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
834 return FAIL;
835 if (name != NULL)
836 isn->isn_arg.string = vim_strsave(name);
837 else
838 isn->isn_arg.number = idx;
839
840 return OK;
841}
842
843/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100844 * Generate an ISN_LOADV instruction.
845 */
846 static int
847generate_LOADV(
848 cctx_T *cctx,
849 char_u *name,
850 int error)
851{
852 // load v:var
853 int vidx = find_vim_var(name);
854
Bram Moolenaar080457c2020-03-03 21:53:32 +0100855 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100856 if (vidx < 0)
857 {
858 if (error)
859 semsg(_(e_var_notfound), name);
860 return FAIL;
861 }
862
863 // TODO: get actual type
864 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
865}
866
867/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 * Generate an ISN_LOADS instruction.
869 */
870 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100871generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100873 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100875 int sid,
876 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877{
878 isn_T *isn;
879
Bram Moolenaar080457c2020-03-03 21:53:32 +0100880 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100881 if (isn_type == ISN_LOADS)
882 isn = generate_instr_type(cctx, isn_type, type);
883 else
884 isn = generate_instr_drop(cctx, isn_type, 1);
885 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100887 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
888 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889
890 return OK;
891}
892
893/*
894 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
895 */
896 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100897generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 cctx_T *cctx,
899 isntype_T isn_type,
900 int sid,
901 int idx,
902 type_T *type)
903{
904 isn_T *isn;
905
Bram Moolenaar080457c2020-03-03 21:53:32 +0100906 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907 if (isn_type == ISN_LOADSCRIPT)
908 isn = generate_instr_type(cctx, isn_type, type);
909 else
910 isn = generate_instr_drop(cctx, isn_type, 1);
911 if (isn == NULL)
912 return FAIL;
913 isn->isn_arg.script.script_sid = sid;
914 isn->isn_arg.script.script_idx = idx;
915 return OK;
916}
917
918/*
919 * Generate an ISN_NEWLIST instruction.
920 */
921 static int
922generate_NEWLIST(cctx_T *cctx, int count)
923{
924 isn_T *isn;
925 garray_T *stack = &cctx->ctx_type_stack;
926 garray_T *type_list = cctx->ctx_type_list;
927 type_T *type;
928 type_T *member;
929
Bram Moolenaar080457c2020-03-03 21:53:32 +0100930 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
932 return FAIL;
933 isn->isn_arg.number = count;
934
935 // drop the value types
936 stack->ga_len -= count;
937
Bram Moolenaar436472f2020-02-20 22:54:43 +0100938 // Use the first value type for the list member type. Use "void" for an
939 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 if (count > 0)
941 member = ((type_T **)stack->ga_data)[stack->ga_len];
942 else
Bram Moolenaar436472f2020-02-20 22:54:43 +0100943 member = &t_void;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944 type = get_list_type(member, type_list);
945
946 // add the list type to the type stack
947 if (ga_grow(stack, 1) == FAIL)
948 return FAIL;
949 ((type_T **)stack->ga_data)[stack->ga_len] = type;
950 ++stack->ga_len;
951
952 return OK;
953}
954
955/*
956 * Generate an ISN_NEWDICT instruction.
957 */
958 static int
959generate_NEWDICT(cctx_T *cctx, int count)
960{
961 isn_T *isn;
962 garray_T *stack = &cctx->ctx_type_stack;
963 garray_T *type_list = cctx->ctx_type_list;
964 type_T *type;
965 type_T *member;
966
Bram Moolenaar080457c2020-03-03 21:53:32 +0100967 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
969 return FAIL;
970 isn->isn_arg.number = count;
971
972 // drop the key and value types
973 stack->ga_len -= 2 * count;
974
Bram Moolenaar436472f2020-02-20 22:54:43 +0100975 // Use the first value type for the list member type. Use "void" for an
976 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 if (count > 0)
978 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
979 else
Bram Moolenaar436472f2020-02-20 22:54:43 +0100980 member = &t_void;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 type = get_dict_type(member, type_list);
982
983 // add the dict type to the type stack
984 if (ga_grow(stack, 1) == FAIL)
985 return FAIL;
986 ((type_T **)stack->ga_data)[stack->ga_len] = type;
987 ++stack->ga_len;
988
989 return OK;
990}
991
992/*
993 * Generate an ISN_FUNCREF instruction.
994 */
995 static int
996generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
997{
998 isn_T *isn;
999 garray_T *stack = &cctx->ctx_type_stack;
1000
Bram Moolenaar080457c2020-03-03 21:53:32 +01001001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1003 return FAIL;
1004 isn->isn_arg.number = dfunc_idx;
1005
1006 if (ga_grow(stack, 1) == FAIL)
1007 return FAIL;
1008 ((type_T **)stack->ga_data)[stack->ga_len] = &t_partial_any;
1009 // TODO: argument and return types
1010 ++stack->ga_len;
1011
1012 return OK;
1013}
1014
1015/*
1016 * Generate an ISN_JUMP instruction.
1017 */
1018 static int
1019generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1020{
1021 isn_T *isn;
1022 garray_T *stack = &cctx->ctx_type_stack;
1023
Bram Moolenaar080457c2020-03-03 21:53:32 +01001024 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001025 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1026 return FAIL;
1027 isn->isn_arg.jump.jump_when = when;
1028 isn->isn_arg.jump.jump_where = where;
1029
1030 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1031 --stack->ga_len;
1032
1033 return OK;
1034}
1035
1036 static int
1037generate_FOR(cctx_T *cctx, int loop_idx)
1038{
1039 isn_T *isn;
1040 garray_T *stack = &cctx->ctx_type_stack;
1041
Bram Moolenaar080457c2020-03-03 21:53:32 +01001042 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1044 return FAIL;
1045 isn->isn_arg.forloop.for_idx = loop_idx;
1046
1047 if (ga_grow(stack, 1) == FAIL)
1048 return FAIL;
1049 // type doesn't matter, will be stored next
1050 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1051 ++stack->ga_len;
1052
1053 return OK;
1054}
1055
1056/*
1057 * Generate an ISN_BCALL instruction.
1058 * Return FAIL if the number of arguments is wrong.
1059 */
1060 static int
1061generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1062{
1063 isn_T *isn;
1064 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001065 type_T *argtypes[MAX_FUNC_ARGS];
1066 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067
Bram Moolenaar080457c2020-03-03 21:53:32 +01001068 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069 if (check_internal_func(func_idx, argcount) == FAIL)
1070 return FAIL;
1071
1072 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1073 return FAIL;
1074 isn->isn_arg.bfunc.cbf_idx = func_idx;
1075 isn->isn_arg.bfunc.cbf_argcount = argcount;
1076
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001077 for (i = 0; i < argcount; ++i)
1078 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 stack->ga_len -= argcount; // drop the arguments
1081 if (ga_grow(stack, 1) == FAIL)
1082 return FAIL;
1083 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001084 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 ++stack->ga_len; // add return value
1086
1087 return OK;
1088}
1089
1090/*
1091 * Generate an ISN_DCALL or ISN_UCALL instruction.
1092 * Return FAIL if the number of arguments is wrong.
1093 */
1094 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001095generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096{
1097 isn_T *isn;
1098 garray_T *stack = &cctx->ctx_type_stack;
1099 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001100 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101
Bram Moolenaar080457c2020-03-03 21:53:32 +01001102 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103 if (argcount > regular_args && !has_varargs(ufunc))
1104 {
1105 semsg(_(e_toomanyarg), ufunc->uf_name);
1106 return FAIL;
1107 }
1108 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1109 {
1110 semsg(_(e_toofewarg), ufunc->uf_name);
1111 return FAIL;
1112 }
1113
1114 // Turn varargs into a list.
1115 if (ufunc->uf_va_name != NULL)
1116 {
1117 int count = argcount - regular_args;
1118
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001119 // If count is negative an empty list will be added after evaluating
1120 // default values for missing optional arguments.
1121 if (count >= 0)
1122 {
1123 generate_NEWLIST(cctx, count);
1124 argcount = regular_args + 1;
1125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 }
1127
1128 if ((isn = generate_instr(cctx,
1129 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1130 return FAIL;
1131 if (ufunc->uf_dfunc_idx >= 0)
1132 {
1133 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1134 isn->isn_arg.dfunc.cdf_argcount = argcount;
1135 }
1136 else
1137 {
1138 // A user function may be deleted and redefined later, can't use the
1139 // ufunc pointer, need to look it up again at runtime.
1140 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1141 isn->isn_arg.ufunc.cuf_argcount = argcount;
1142 }
1143
1144 stack->ga_len -= argcount; // drop the arguments
1145 if (ga_grow(stack, 1) == FAIL)
1146 return FAIL;
1147 // add return value
1148 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1149 ++stack->ga_len;
1150
1151 return OK;
1152}
1153
1154/*
1155 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1156 */
1157 static int
1158generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1159{
1160 isn_T *isn;
1161 garray_T *stack = &cctx->ctx_type_stack;
1162
Bram Moolenaar080457c2020-03-03 21:53:32 +01001163 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1165 return FAIL;
1166 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1167 isn->isn_arg.ufunc.cuf_argcount = argcount;
1168
1169 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001170 if (ga_grow(stack, 1) == FAIL)
1171 return FAIL;
1172 // add return value
1173 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1174 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175
1176 return OK;
1177}
1178
1179/*
1180 * Generate an ISN_PCALL instruction.
1181 */
1182 static int
1183generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1184{
1185 isn_T *isn;
1186 garray_T *stack = &cctx->ctx_type_stack;
1187
Bram Moolenaar080457c2020-03-03 21:53:32 +01001188 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1190 return FAIL;
1191 isn->isn_arg.pfunc.cpf_top = at_top;
1192 isn->isn_arg.pfunc.cpf_argcount = argcount;
1193
1194 stack->ga_len -= argcount; // drop the arguments
1195
1196 // drop the funcref/partial, get back the return value
1197 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1198
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001199 // If partial is above the arguments it must be cleared and replaced with
1200 // the return value.
1201 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1202 return FAIL;
1203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 return OK;
1205}
1206
1207/*
1208 * Generate an ISN_MEMBER instruction.
1209 */
1210 static int
1211generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1212{
1213 isn_T *isn;
1214 garray_T *stack = &cctx->ctx_type_stack;
1215 type_T *type;
1216
Bram Moolenaar080457c2020-03-03 21:53:32 +01001217 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1219 return FAIL;
1220 isn->isn_arg.string = vim_strnsave(name, (int)len);
1221
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001222 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001224 if (type->tt_type != VAR_DICT && type != &t_any)
1225 {
1226 emsg(_(e_dictreq));
1227 return FAIL;
1228 }
1229 // change dict type to dict member type
1230 if (type->tt_type == VAR_DICT)
1231 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232
1233 return OK;
1234}
1235
1236/*
1237 * Generate an ISN_ECHO instruction.
1238 */
1239 static int
1240generate_ECHO(cctx_T *cctx, int with_white, int count)
1241{
1242 isn_T *isn;
1243
Bram Moolenaar080457c2020-03-03 21:53:32 +01001244 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1246 return FAIL;
1247 isn->isn_arg.echo.echo_with_white = with_white;
1248 isn->isn_arg.echo.echo_count = count;
1249
1250 return OK;
1251}
1252
Bram Moolenaarad39c092020-02-26 18:23:43 +01001253/*
1254 * Generate an ISN_EXECUTE instruction.
1255 */
1256 static int
1257generate_EXECUTE(cctx_T *cctx, int count)
1258{
1259 isn_T *isn;
1260
1261 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1262 return FAIL;
1263 isn->isn_arg.number = count;
1264
1265 return OK;
1266}
1267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 static int
1269generate_EXEC(cctx_T *cctx, char_u *line)
1270{
1271 isn_T *isn;
1272
Bram Moolenaar080457c2020-03-03 21:53:32 +01001273 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1275 return FAIL;
1276 isn->isn_arg.string = vim_strsave(line);
1277 return OK;
1278}
1279
1280static char e_white_both[] =
1281 N_("E1004: white space required before and after '%s'");
1282
1283/*
1284 * Reserve space for a local variable.
1285 * Return the index or -1 if it failed.
1286 */
1287 static int
1288reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1289{
1290 int idx;
1291 lvar_T *lvar;
1292
1293 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1294 {
1295 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1296 return -1;
1297 }
1298
1299 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1300 return -1;
1301 idx = cctx->ctx_locals.ga_len;
1302 if (cctx->ctx_max_local < idx + 1)
1303 cctx->ctx_max_local = idx + 1;
1304 ++cctx->ctx_locals.ga_len;
1305
1306 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1307 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1308 lvar->lv_const = isConst;
1309 lvar->lv_type = type;
1310
1311 return idx;
1312}
1313
1314/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001315 * Remove local variables above "new_top".
1316 */
1317 static void
1318unwind_locals(cctx_T *cctx, int new_top)
1319{
1320 if (cctx->ctx_locals.ga_len > new_top)
1321 {
1322 int idx;
1323 lvar_T *lvar;
1324
1325 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1326 {
1327 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1328 vim_free(lvar->lv_name);
1329 }
1330 }
1331 cctx->ctx_locals.ga_len = new_top;
1332}
1333
1334/*
1335 * Free all local variables.
1336 */
1337 static void
1338free_local(cctx_T *cctx)
1339{
1340 unwind_locals(cctx, 0);
1341 ga_clear(&cctx->ctx_locals);
1342}
1343
1344/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 * Skip over a type definition and return a pointer to just after it.
1346 */
1347 char_u *
1348skip_type(char_u *start)
1349{
1350 char_u *p = start;
1351
1352 while (ASCII_ISALNUM(*p) || *p == '_')
1353 ++p;
1354
1355 // Skip over "<type>"; this is permissive about white space.
1356 if (*skipwhite(p) == '<')
1357 {
1358 p = skipwhite(p);
1359 p = skip_type(skipwhite(p + 1));
1360 p = skipwhite(p);
1361 if (*p == '>')
1362 ++p;
1363 }
1364 return p;
1365}
1366
1367/*
1368 * Parse the member type: "<type>" and return "type" with the member set.
1369 * Use "type_list" if a new type needs to be added.
1370 * Returns NULL in case of failure.
1371 */
1372 static type_T *
1373parse_type_member(char_u **arg, type_T *type, garray_T *type_list)
1374{
1375 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001376 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377
1378 if (**arg != '<')
1379 {
1380 if (*skipwhite(*arg) == '<')
1381 emsg(_("E1007: No white space allowed before <"));
1382 else
1383 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001384 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 }
1386 *arg = skipwhite(*arg + 1);
1387
1388 member_type = parse_type(arg, type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389
1390 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001391 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 {
1393 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001394 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 }
1396 ++*arg;
1397
1398 if (type->tt_type == VAR_LIST)
1399 return get_list_type(member_type, type_list);
1400 return get_dict_type(member_type, type_list);
1401}
1402
1403/*
1404 * Parse a type at "arg" and advance over it.
1405 * Return NULL for failure.
1406 */
1407 type_T *
1408parse_type(char_u **arg, garray_T *type_list)
1409{
1410 char_u *p = *arg;
1411 size_t len;
1412
1413 // skip over the first word
1414 while (ASCII_ISALNUM(*p) || *p == '_')
1415 ++p;
1416 len = p - *arg;
1417
1418 switch (**arg)
1419 {
1420 case 'a':
1421 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1422 {
1423 *arg += len;
1424 return &t_any;
1425 }
1426 break;
1427 case 'b':
1428 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1429 {
1430 *arg += len;
1431 return &t_bool;
1432 }
1433 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1434 {
1435 *arg += len;
1436 return &t_blob;
1437 }
1438 break;
1439 case 'c':
1440 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1441 {
1442 *arg += len;
1443 return &t_channel;
1444 }
1445 break;
1446 case 'd':
1447 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1448 {
1449 *arg += len;
1450 return parse_type_member(arg, &t_dict_any, type_list);
1451 }
1452 break;
1453 case 'f':
1454 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1455 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001456#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 *arg += len;
1458 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001459#else
1460 emsg(_("E1055: This Vim is not compiled with float support"));
1461 return &t_any;
1462#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 }
1464 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1465 {
1466 *arg += len;
1467 // TODO: arguments and return type
1468 return &t_func_any;
1469 }
1470 break;
1471 case 'j':
1472 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1473 {
1474 *arg += len;
1475 return &t_job;
1476 }
1477 break;
1478 case 'l':
1479 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1480 {
1481 *arg += len;
1482 return parse_type_member(arg, &t_list_any, type_list);
1483 }
1484 break;
1485 case 'n':
1486 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1487 {
1488 *arg += len;
1489 return &t_number;
1490 }
1491 break;
1492 case 'p':
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001493 if (len == 7 && STRNCMP(*arg, "partial", len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 {
1495 *arg += len;
1496 // TODO: arguments and return type
1497 return &t_partial_any;
1498 }
1499 break;
1500 case 's':
1501 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1502 {
1503 *arg += len;
1504 return &t_string;
1505 }
1506 break;
1507 case 'v':
1508 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1509 {
1510 *arg += len;
1511 return &t_void;
1512 }
1513 break;
1514 }
1515
1516 semsg(_("E1010: Type not recognized: %s"), *arg);
1517 return &t_any;
1518}
1519
1520/*
1521 * Check if "type1" and "type2" are exactly the same.
1522 */
1523 static int
1524equal_type(type_T *type1, type_T *type2)
1525{
1526 if (type1->tt_type != type2->tt_type)
1527 return FALSE;
1528 switch (type1->tt_type)
1529 {
1530 case VAR_VOID:
1531 case VAR_UNKNOWN:
1532 case VAR_SPECIAL:
1533 case VAR_BOOL:
1534 case VAR_NUMBER:
1535 case VAR_FLOAT:
1536 case VAR_STRING:
1537 case VAR_BLOB:
1538 case VAR_JOB:
1539 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001540 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 case VAR_LIST:
1542 case VAR_DICT:
1543 return equal_type(type1->tt_member, type2->tt_member);
1544 case VAR_FUNC:
1545 case VAR_PARTIAL:
1546 // TODO; check argument types.
1547 return equal_type(type1->tt_member, type2->tt_member)
1548 && type1->tt_argcount == type2->tt_argcount;
1549 }
1550 return TRUE;
1551}
1552
1553/*
1554 * Find the common type of "type1" and "type2" and put it in "dest".
1555 * "type2" and "dest" may be the same.
1556 */
1557 static void
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001558common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_list)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559{
1560 if (equal_type(type1, type2))
1561 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001562 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 return;
1564 }
1565
1566 if (type1->tt_type == type2->tt_type)
1567 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1569 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001570 type_T *common;
1571
1572 common_type(type1->tt_member, type2->tt_member, &common, type_list);
1573 if (type1->tt_type == VAR_LIST)
1574 *dest = get_list_type(common, type_list);
1575 else
1576 *dest = get_dict_type(common, type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 return;
1578 }
1579 // TODO: VAR_FUNC and VAR_PARTIAL
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001580 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 }
1582
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001583 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584}
1585
1586 char *
1587vartype_name(vartype_T type)
1588{
1589 switch (type)
1590 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001591 case VAR_UNKNOWN: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 case VAR_SPECIAL: return "special";
1594 case VAR_BOOL: return "bool";
1595 case VAR_NUMBER: return "number";
1596 case VAR_FLOAT: return "float";
1597 case VAR_STRING: return "string";
1598 case VAR_BLOB: return "blob";
1599 case VAR_JOB: return "job";
1600 case VAR_CHANNEL: return "channel";
1601 case VAR_LIST: return "list";
1602 case VAR_DICT: return "dict";
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001603 case VAR_FUNC: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 case VAR_PARTIAL: return "partial";
1605 }
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001606 return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607}
1608
1609/*
1610 * Return the name of a type.
1611 * The result may be in allocated memory, in which case "tofree" is set.
1612 */
1613 char *
1614type_name(type_T *type, char **tofree)
1615{
1616 char *name = vartype_name(type->tt_type);
1617
1618 *tofree = NULL;
1619 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1620 {
1621 char *member_free;
1622 char *member_name = type_name(type->tt_member, &member_free);
1623 size_t len;
1624
1625 len = STRLEN(name) + STRLEN(member_name) + 3;
1626 *tofree = alloc(len);
1627 if (*tofree != NULL)
1628 {
1629 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1630 vim_free(member_free);
1631 return *tofree;
1632 }
1633 }
1634 // TODO: function and partial argument types
1635
1636 return name;
1637}
1638
1639/*
1640 * Find "name" in script-local items of script "sid".
1641 * Returns the index in "sn_var_vals" if found.
1642 * If found but not in "sn_var_vals" returns -1.
1643 * If not found returns -2.
1644 */
1645 int
1646get_script_item_idx(int sid, char_u *name, int check_writable)
1647{
1648 hashtab_T *ht;
1649 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001650 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651 int idx;
1652
1653 // First look the name up in the hashtable.
1654 if (sid <= 0 || sid > script_items.ga_len)
1655 return -1;
1656 ht = &SCRIPT_VARS(sid);
1657 di = find_var_in_ht(ht, 0, name, TRUE);
1658 if (di == NULL)
1659 return -2;
1660
1661 // Now find the svar_T index in sn_var_vals.
1662 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1663 {
1664 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1665
1666 if (sv->sv_tv == &di->di_tv)
1667 {
1668 if (check_writable && sv->sv_const)
1669 semsg(_(e_readonlyvar), name);
1670 return idx;
1671 }
1672 }
1673 return -1;
1674}
1675
1676/*
1677 * Find "name" in imported items of the current script/
1678 */
1679 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001680find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001682 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 int idx;
1684
1685 if (cctx != NULL)
1686 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1687 {
1688 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1689 + idx;
1690
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001691 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1692 : STRLEN(import->imp_name) == len
1693 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 return import;
1695 }
1696
1697 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1698 {
1699 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1700
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001701 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1702 : STRLEN(import->imp_name) == len
1703 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 return import;
1705 }
1706 return NULL;
1707}
1708
1709/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001710 * Free all imported variables.
1711 */
1712 static void
1713free_imported(cctx_T *cctx)
1714{
1715 int idx;
1716
1717 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1718 {
1719 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1720
1721 vim_free(import->imp_name);
1722 }
1723 ga_clear(&cctx->ctx_imports);
1724}
1725
1726/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 * Generate an instruction to load script-local variable "name".
1728 */
1729 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001730compile_load_scriptvar(
1731 cctx_T *cctx,
1732 char_u *name, // variable NUL terminated
1733 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001734 char_u **end, // end of variable
1735 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001737 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1739 imported_T *import;
1740
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001741 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001743 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001744 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1745 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746 }
1747 if (idx >= 0)
1748 {
1749 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1750
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001751 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 current_sctx.sc_sid, idx, sv->sv_type);
1753 return OK;
1754 }
1755
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001756 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 if (import != NULL)
1758 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001759 if (import->imp_all)
1760 {
1761 char_u *p = skipwhite(*end);
1762 int name_len;
1763 ufunc_T *ufunc;
1764 type_T *type;
1765
1766 // Used "import * as Name", need to lookup the member.
1767 if (*p != '.')
1768 {
1769 semsg(_("E1060: expected dot after name: %s"), start);
1770 return FAIL;
1771 }
1772 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001773 if (VIM_ISWHITE(*p))
1774 {
1775 emsg(_("E1074: no white space allowed after dot"));
1776 return FAIL;
1777 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001778
1779 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
1780 // TODO: what if it is a function?
1781 if (idx < 0)
1782 return FAIL;
1783 *end = p;
1784
1785 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1786 import->imp_sid,
1787 idx,
1788 type);
1789 }
1790 else
1791 {
1792 // TODO: check this is a variable, not a function
1793 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1794 import->imp_sid,
1795 import->imp_var_vals_idx,
1796 import->imp_type);
1797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 return OK;
1799 }
1800
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001801 if (error)
1802 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 return FAIL;
1804}
1805
1806/*
1807 * Compile a variable name into a load instruction.
1808 * "end" points to just after the name.
1809 * When "error" is FALSE do not give an error when not found.
1810 */
1811 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001812compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813{
1814 type_T *type;
1815 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001816 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001818 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819
1820 if (*(*arg + 1) == ':')
1821 {
1822 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001823 if (end <= *arg + 2)
1824 name = vim_strsave((char_u *)"[empty]");
1825 else
1826 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 if (name == NULL)
1828 return FAIL;
1829
1830 if (**arg == 'v')
1831 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001832 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 }
1834 else if (**arg == 'g')
1835 {
1836 // Global variables can be defined later, thus we don't check if it
1837 // exists, give error at runtime.
1838 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1839 }
1840 else if (**arg == 's')
1841 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001842 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001844 else if (**arg == 'b')
1845 {
1846 semsg("Namespace b: not supported yet: %s", *arg);
1847 goto theend;
1848 }
1849 else if (**arg == 'w')
1850 {
1851 semsg("Namespace w: not supported yet: %s", *arg);
1852 goto theend;
1853 }
1854 else if (**arg == 't')
1855 {
1856 semsg("Namespace t: not supported yet: %s", *arg);
1857 goto theend;
1858 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 else
1860 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01001861 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 goto theend;
1863 }
1864 }
1865 else
1866 {
1867 size_t len = end - *arg;
1868 int idx;
1869 int gen_load = FALSE;
1870
1871 name = vim_strnsave(*arg, end - *arg);
1872 if (name == NULL)
1873 return FAIL;
1874
1875 idx = lookup_arg(*arg, len, cctx);
1876 if (idx >= 0)
1877 {
1878 if (cctx->ctx_ufunc->uf_arg_types != NULL)
1879 type = cctx->ctx_ufunc->uf_arg_types[idx];
1880 else
1881 type = &t_any;
1882
1883 // Arguments are located above the frame pointer.
1884 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
1885 if (cctx->ctx_ufunc->uf_va_name != NULL)
1886 --idx;
1887 gen_load = TRUE;
1888 }
1889 else if (lookup_vararg(*arg, len, cctx))
1890 {
1891 // varargs is always the last argument
1892 idx = -STACK_FRAME_SIZE - 1;
1893 type = cctx->ctx_ufunc->uf_va_type;
1894 gen_load = TRUE;
1895 }
1896 else
1897 {
1898 idx = lookup_local(*arg, len, cctx);
1899 if (idx >= 0)
1900 {
1901 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
1902 gen_load = TRUE;
1903 }
1904 else
1905 {
1906 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
1907 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
1908 res = generate_PUSHBOOL(cctx, **arg == 't'
1909 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001910 else if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1911 == SCRIPT_VERSION_VIM9)
1912 // in Vim9 script "var" can be script-local.
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001913 res = compile_load_scriptvar(cctx, name, *arg, &end, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 }
1915 }
1916 if (gen_load)
1917 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1918 }
1919
1920 *arg = end;
1921
1922theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001923 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 semsg(_(e_var_notfound), name);
1925 vim_free(name);
1926 return res;
1927}
1928
1929/*
1930 * Compile the argument expressions.
1931 * "arg" points to just after the "(" and is advanced to after the ")"
1932 */
1933 static int
1934compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
1935{
1936 char_u *p = *arg;
1937
1938 while (*p != NUL && *p != ')')
1939 {
1940 if (compile_expr1(&p, cctx) == FAIL)
1941 return FAIL;
1942 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001943
1944 if (*p != ',' && *skipwhite(p) == ',')
1945 {
1946 emsg(_("E1068: No white space allowed before ,"));
1947 p = skipwhite(p);
1948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001950 {
1951 ++p;
1952 if (!VIM_ISWHITE(*p))
1953 emsg(_("E1069: white space required after ,"));
1954 }
1955 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01001957 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 if (*p != ')')
1959 {
1960 emsg(_(e_missing_close));
1961 return FAIL;
1962 }
1963 *arg = p + 1;
1964 return OK;
1965}
1966
1967/*
1968 * Compile a function call: name(arg1, arg2)
1969 * "arg" points to "name", "arg + varlen" to the "(".
1970 * "argcount_init" is 1 for "value->method()"
1971 * Instructions:
1972 * EVAL arg1
1973 * EVAL arg2
1974 * BCALL / DCALL / UCALL
1975 */
1976 static int
1977compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
1978{
1979 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01001980 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 int argcount = argcount_init;
1982 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001983 char_u fname_buf[FLEN_FIXED + 1];
1984 char_u *tofree = NULL;
1985 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001987 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988
1989 if (varlen >= sizeof(namebuf))
1990 {
1991 semsg(_("E1011: name too long: %s"), name);
1992 return FAIL;
1993 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001994 vim_strncpy(namebuf, *arg, varlen);
1995 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996
1997 *arg = skipwhite(*arg + varlen + 1);
1998 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001999 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002001 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 {
2003 int idx;
2004
2005 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002006 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002008 {
2009 res = generate_BCALL(cctx, idx, argcount);
2010 goto theend;
2011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 semsg(_(e_unknownfunc), namebuf);
2013 }
2014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002016 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002018 {
2019 res = generate_CALL(cctx, ufunc, argcount);
2020 goto theend;
2021 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022
2023 // If the name is a variable, load it and use PCALL.
2024 p = namebuf;
2025 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002026 {
2027 res = generate_PCALL(cctx, argcount, FALSE);
2028 goto theend;
2029 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030
2031 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002032 res = generate_UCALL(cctx, name, argcount);
2033
2034theend:
2035 vim_free(tofree);
2036 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037}
2038
2039// like NAMESPACE_CHAR but with 'a' and 'l'.
2040#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2041
2042/*
2043 * Find the end of a variable or function name. Unlike find_name_end() this
2044 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002045 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 * Return a pointer to just after the name. Equal to "arg" if there is no
2047 * valid name.
2048 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002049 static char_u *
2050to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051{
2052 char_u *p;
2053
2054 // Quick check for valid starting character.
2055 if (!eval_isnamec1(*arg))
2056 return arg;
2057
2058 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2059 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2060 // and can be used in slice "[n:]".
2061 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002062 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2064 break;
2065 return p;
2066}
2067
2068/*
2069 * Like to_name_end() but also skip over a list or dict constant.
2070 */
2071 char_u *
2072to_name_const_end(char_u *arg)
2073{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002074 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 typval_T rettv;
2076
2077 if (p == arg && *arg == '[')
2078 {
2079
2080 // Can be "[1, 2, 3]->Func()".
2081 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2082 p = arg;
2083 }
2084 else if (p == arg && *arg == '#' && arg[1] == '{')
2085 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002086 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 ++p;
2088 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2089 p = arg;
2090 }
2091 else if (p == arg && *arg == '{')
2092 {
2093 int ret = get_lambda_tv(&p, &rettv, FALSE);
2094
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002095 // Can be "{x -> ret}()".
2096 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 if (ret == NOTDONE)
2098 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2099 if (ret != OK)
2100 p = arg;
2101 }
2102
2103 return p;
2104}
2105
2106 static void
2107type_mismatch(type_T *expected, type_T *actual)
2108{
2109 char *tofree1, *tofree2;
2110
2111 semsg(_("E1013: type mismatch, expected %s but got %s"),
2112 type_name(expected, &tofree1), type_name(actual, &tofree2));
2113 vim_free(tofree1);
2114 vim_free(tofree2);
2115}
2116
2117/*
2118 * Check if the expected and actual types match.
2119 */
2120 static int
2121check_type(type_T *expected, type_T *actual, int give_msg)
2122{
2123 if (expected->tt_type != VAR_UNKNOWN)
2124 {
2125 if (expected->tt_type != actual->tt_type)
2126 {
2127 if (give_msg)
2128 type_mismatch(expected, actual);
2129 return FAIL;
2130 }
2131 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2132 {
Bram Moolenaar436472f2020-02-20 22:54:43 +01002133 int ret;
2134
2135 // void is used for an empty list or dict
2136 if (actual->tt_member == &t_void)
2137 ret = OK;
2138 else
2139 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 if (ret == FAIL && give_msg)
2141 type_mismatch(expected, actual);
2142 return ret;
2143 }
2144 }
2145 return OK;
2146}
2147
2148/*
2149 * Check that
2150 * - "actual" is "expected" type or
2151 * - "actual" is a type that can be "expected" type: add a runtime check; or
2152 * - return FAIL.
2153 */
2154 static int
2155need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2156{
Bram Moolenaar436472f2020-02-20 22:54:43 +01002157 if (check_type(expected, actual, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 return OK;
2159 if (actual->tt_type != VAR_UNKNOWN)
2160 {
2161 type_mismatch(expected, actual);
2162 return FAIL;
2163 }
2164 generate_TYPECHECK(cctx, expected, offset);
2165 return OK;
2166}
2167
2168/*
2169 * parse a list: [expr, expr]
2170 * "*arg" points to the '['.
2171 */
2172 static int
2173compile_list(char_u **arg, cctx_T *cctx)
2174{
2175 char_u *p = skipwhite(*arg + 1);
2176 int count = 0;
2177
2178 while (*p != ']')
2179 {
2180 if (*p == NUL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002181 {
2182 semsg(_(e_list_end), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183 return FAIL;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002184 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 if (compile_expr1(&p, cctx) == FAIL)
2186 break;
2187 ++count;
2188 if (*p == ',')
2189 ++p;
2190 p = skipwhite(p);
2191 }
2192 *arg = p + 1;
2193
2194 generate_NEWLIST(cctx, count);
2195 return OK;
2196}
2197
2198/*
2199 * parse a lambda: {arg, arg -> expr}
2200 * "*arg" points to the '{'.
2201 */
2202 static int
2203compile_lambda(char_u **arg, cctx_T *cctx)
2204{
2205 garray_T *instr = &cctx->ctx_instr;
2206 typval_T rettv;
2207 ufunc_T *ufunc;
2208
2209 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002210 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002214 ++ufunc->uf_refcount;
2215 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216
2217 // The function will have one line: "return {expr}".
2218 // Compile it into instructions.
2219 compile_def_function(ufunc, TRUE);
2220
2221 if (ufunc->uf_dfunc_idx >= 0)
2222 {
2223 if (ga_grow(instr, 1) == FAIL)
2224 return FAIL;
2225 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2226 return OK;
2227 }
2228 return FAIL;
2229}
2230
2231/*
2232 * Compile a lamda call: expr->{lambda}(args)
2233 * "arg" points to the "{".
2234 */
2235 static int
2236compile_lambda_call(char_u **arg, cctx_T *cctx)
2237{
2238 ufunc_T *ufunc;
2239 typval_T rettv;
2240 int argcount = 1;
2241 int ret = FAIL;
2242
2243 // Get the funcref in "rettv".
2244 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2245 return FAIL;
2246
2247 if (**arg != '(')
2248 {
2249 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002250 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 else
2252 semsg(_(e_missing_paren), "lambda");
2253 clear_tv(&rettv);
2254 return FAIL;
2255 }
2256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 ufunc = rettv.vval.v_partial->pt_func;
2258 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002259 clear_tv(&rettv);
2260
2261 // The function will have one line: "return {expr}".
2262 // Compile it into instructions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 compile_def_function(ufunc, TRUE);
2264
2265 // compile the arguments
2266 *arg = skipwhite(*arg + 1);
2267 if (compile_arguments(arg, cctx, &argcount) == OK)
2268 // call the compiled function
2269 ret = generate_CALL(cctx, ufunc, argcount);
2270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 return ret;
2272}
2273
2274/*
2275 * parse a dict: {'key': val} or #{key: val}
2276 * "*arg" points to the '{'.
2277 */
2278 static int
2279compile_dict(char_u **arg, cctx_T *cctx, int literal)
2280{
2281 garray_T *instr = &cctx->ctx_instr;
2282 int count = 0;
2283 dict_T *d = dict_alloc();
2284 dictitem_T *item;
2285
2286 if (d == NULL)
2287 return FAIL;
2288 *arg = skipwhite(*arg + 1);
2289 while (**arg != '}' && **arg != NUL)
2290 {
2291 char_u *key = NULL;
2292
2293 if (literal)
2294 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002295 char_u *p = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296
2297 if (p == *arg)
2298 {
2299 semsg(_("E1014: Invalid key: %s"), *arg);
2300 return FAIL;
2301 }
2302 key = vim_strnsave(*arg, p - *arg);
2303 if (generate_PUSHS(cctx, key) == FAIL)
2304 return FAIL;
2305 *arg = p;
2306 }
2307 else
2308 {
2309 isn_T *isn;
2310
2311 if (compile_expr1(arg, cctx) == FAIL)
2312 return FAIL;
2313 // TODO: check type is string
2314 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2315 if (isn->isn_type == ISN_PUSHS)
2316 key = isn->isn_arg.string;
2317 }
2318
2319 // Check for duplicate keys, if using string keys.
2320 if (key != NULL)
2321 {
2322 item = dict_find(d, key, -1);
2323 if (item != NULL)
2324 {
2325 semsg(_(e_duplicate_key), key);
2326 goto failret;
2327 }
2328 item = dictitem_alloc(key);
2329 if (item != NULL)
2330 {
2331 item->di_tv.v_type = VAR_UNKNOWN;
2332 item->di_tv.v_lock = 0;
2333 if (dict_add(d, item) == FAIL)
2334 dictitem_free(item);
2335 }
2336 }
2337
2338 *arg = skipwhite(*arg);
2339 if (**arg != ':')
2340 {
2341 semsg(_(e_missing_dict_colon), *arg);
2342 return FAIL;
2343 }
2344
2345 *arg = skipwhite(*arg + 1);
2346 if (compile_expr1(arg, cctx) == FAIL)
2347 return FAIL;
2348 ++count;
2349
2350 if (**arg == '}')
2351 break;
2352 if (**arg != ',')
2353 {
2354 semsg(_(e_missing_dict_comma), *arg);
2355 goto failret;
2356 }
2357 *arg = skipwhite(*arg + 1);
2358 }
2359
2360 if (**arg != '}')
2361 {
2362 semsg(_(e_missing_dict_end), *arg);
2363 goto failret;
2364 }
2365 *arg = *arg + 1;
2366
2367 dict_unref(d);
2368 return generate_NEWDICT(cctx, count);
2369
2370failret:
2371 dict_unref(d);
2372 return FAIL;
2373}
2374
2375/*
2376 * Compile "&option".
2377 */
2378 static int
2379compile_get_option(char_u **arg, cctx_T *cctx)
2380{
2381 typval_T rettv;
2382 char_u *start = *arg;
2383 int ret;
2384
2385 // parse the option and get the current value to get the type.
2386 rettv.v_type = VAR_UNKNOWN;
2387 ret = get_option_tv(arg, &rettv, TRUE);
2388 if (ret == OK)
2389 {
2390 // include the '&' in the name, get_option_tv() expects it.
2391 char_u *name = vim_strnsave(start, *arg - start);
2392 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2393
2394 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2395 vim_free(name);
2396 }
2397 clear_tv(&rettv);
2398
2399 return ret;
2400}
2401
2402/*
2403 * Compile "$VAR".
2404 */
2405 static int
2406compile_get_env(char_u **arg, cctx_T *cctx)
2407{
2408 char_u *start = *arg;
2409 int len;
2410 int ret;
2411 char_u *name;
2412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 ++*arg;
2414 len = get_env_len(arg);
2415 if (len == 0)
2416 {
2417 semsg(_(e_syntax_at), start - 1);
2418 return FAIL;
2419 }
2420
2421 // include the '$' in the name, get_env_tv() expects it.
2422 name = vim_strnsave(start, len + 1);
2423 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2424 vim_free(name);
2425 return ret;
2426}
2427
2428/*
2429 * Compile "@r".
2430 */
2431 static int
2432compile_get_register(char_u **arg, cctx_T *cctx)
2433{
2434 int ret;
2435
2436 ++*arg;
2437 if (**arg == NUL)
2438 {
2439 semsg(_(e_syntax_at), *arg - 1);
2440 return FAIL;
2441 }
2442 if (!valid_yank_reg(**arg, TRUE))
2443 {
2444 emsg_invreg(**arg);
2445 return FAIL;
2446 }
2447 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2448 ++*arg;
2449 return ret;
2450}
2451
2452/*
2453 * Apply leading '!', '-' and '+' to constant "rettv".
2454 */
2455 static int
2456apply_leader(typval_T *rettv, char_u *start, char_u *end)
2457{
2458 char_u *p = end;
2459
2460 // this works from end to start
2461 while (p > start)
2462 {
2463 --p;
2464 if (*p == '-' || *p == '+')
2465 {
2466 // only '-' has an effect, for '+' we only check the type
2467#ifdef FEAT_FLOAT
2468 if (rettv->v_type == VAR_FLOAT)
2469 {
2470 if (*p == '-')
2471 rettv->vval.v_float = -rettv->vval.v_float;
2472 }
2473 else
2474#endif
2475 {
2476 varnumber_T val;
2477 int error = FALSE;
2478
2479 // tv_get_number_chk() accepts a string, but we don't want that
2480 // here
2481 if (check_not_string(rettv) == FAIL)
2482 return FAIL;
2483 val = tv_get_number_chk(rettv, &error);
2484 clear_tv(rettv);
2485 if (error)
2486 return FAIL;
2487 if (*p == '-')
2488 val = -val;
2489 rettv->v_type = VAR_NUMBER;
2490 rettv->vval.v_number = val;
2491 }
2492 }
2493 else
2494 {
2495 int v = tv2bool(rettv);
2496
2497 // '!' is permissive in the type.
2498 clear_tv(rettv);
2499 rettv->v_type = VAR_BOOL;
2500 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2501 }
2502 }
2503 return OK;
2504}
2505
2506/*
2507 * Recognize v: variables that are constants and set "rettv".
2508 */
2509 static void
2510get_vim_constant(char_u **arg, typval_T *rettv)
2511{
2512 if (STRNCMP(*arg, "v:true", 6) == 0)
2513 {
2514 rettv->v_type = VAR_BOOL;
2515 rettv->vval.v_number = VVAL_TRUE;
2516 *arg += 6;
2517 }
2518 else if (STRNCMP(*arg, "v:false", 7) == 0)
2519 {
2520 rettv->v_type = VAR_BOOL;
2521 rettv->vval.v_number = VVAL_FALSE;
2522 *arg += 7;
2523 }
2524 else if (STRNCMP(*arg, "v:null", 6) == 0)
2525 {
2526 rettv->v_type = VAR_SPECIAL;
2527 rettv->vval.v_number = VVAL_NULL;
2528 *arg += 6;
2529 }
2530 else if (STRNCMP(*arg, "v:none", 6) == 0)
2531 {
2532 rettv->v_type = VAR_SPECIAL;
2533 rettv->vval.v_number = VVAL_NONE;
2534 *arg += 6;
2535 }
2536}
2537
2538/*
2539 * Compile code to apply '-', '+' and '!'.
2540 */
2541 static int
2542compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2543{
2544 char_u *p = end;
2545
2546 // this works from end to start
2547 while (p > start)
2548 {
2549 --p;
2550 if (*p == '-' || *p == '+')
2551 {
2552 int negate = *p == '-';
2553 isn_T *isn;
2554
2555 // TODO: check type
2556 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2557 {
2558 --p;
2559 if (*p == '-')
2560 negate = !negate;
2561 }
2562 // only '-' has an effect, for '+' we only check the type
2563 if (negate)
2564 isn = generate_instr(cctx, ISN_NEGATENR);
2565 else
2566 isn = generate_instr(cctx, ISN_CHECKNR);
2567 if (isn == NULL)
2568 return FAIL;
2569 }
2570 else
2571 {
2572 int invert = TRUE;
2573
2574 while (p > start && p[-1] == '!')
2575 {
2576 --p;
2577 invert = !invert;
2578 }
2579 if (generate_2BOOL(cctx, invert) == FAIL)
2580 return FAIL;
2581 }
2582 }
2583 return OK;
2584}
2585
2586/*
2587 * Compile whatever comes after "name" or "name()".
2588 */
2589 static int
2590compile_subscript(
2591 char_u **arg,
2592 cctx_T *cctx,
2593 char_u **start_leader,
2594 char_u *end_leader)
2595{
2596 for (;;)
2597 {
2598 if (**arg == '(')
2599 {
2600 int argcount = 0;
2601
2602 // funcref(arg)
2603 *arg = skipwhite(*arg + 1);
2604 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2605 return FAIL;
2606 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2607 return FAIL;
2608 }
2609 else if (**arg == '-' && (*arg)[1] == '>')
2610 {
2611 char_u *p;
2612
2613 // something->method()
2614 // Apply the '!', '-' and '+' first:
2615 // -1.0->func() works like (-1.0)->func()
2616 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2617 return FAIL;
2618 *start_leader = end_leader; // don't apply again later
2619
2620 *arg = skipwhite(*arg + 2);
2621 if (**arg == '{')
2622 {
2623 // lambda call: list->{lambda}
2624 if (compile_lambda_call(arg, cctx) == FAIL)
2625 return FAIL;
2626 }
2627 else
2628 {
2629 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002630 p = *arg;
2631 if (ASCII_ISALPHA(*p) && p[1] == ':')
2632 p += 2;
2633 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 ;
2635 if (*p != '(')
2636 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002637 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 return FAIL;
2639 }
2640 // TODO: base value may not be the first argument
2641 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2642 return FAIL;
2643 }
2644 }
2645 else if (**arg == '[')
2646 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002647 garray_T *stack;
2648 type_T **typep;
2649
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650 // list index: list[123]
2651 // TODO: more arguments
2652 // TODO: dict member dict['name']
2653 *arg = skipwhite(*arg + 1);
2654 if (compile_expr1(arg, cctx) == FAIL)
2655 return FAIL;
2656
2657 if (**arg != ']')
2658 {
2659 emsg(_(e_missbrac));
2660 return FAIL;
2661 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002662 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663
2664 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2665 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01002666 stack = &cctx->ctx_type_stack;
2667 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2668 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
2669 {
2670 emsg(_(e_listreq));
2671 return FAIL;
2672 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002673 if ((*typep)->tt_type == VAR_LIST)
2674 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 }
2676 else if (**arg == '.' && (*arg)[1] != '.')
2677 {
2678 char_u *p;
2679
2680 ++*arg;
2681 p = *arg;
2682 // dictionary member: dict.name
2683 if (eval_isnamec1(*p))
2684 while (eval_isnamec(*p))
2685 MB_PTR_ADV(p);
2686 if (p == *arg)
2687 {
2688 semsg(_(e_syntax_at), *arg);
2689 return FAIL;
2690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2692 return FAIL;
2693 *arg = p;
2694 }
2695 else
2696 break;
2697 }
2698
2699 // TODO - see handle_subscript():
2700 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2701 // Don't do this when "Func" is already a partial that was bound
2702 // explicitly (pt_auto is FALSE).
2703
2704 return OK;
2705}
2706
2707/*
2708 * Compile an expression at "*p" and add instructions to "instr".
2709 * "p" is advanced until after the expression, skipping white space.
2710 *
2711 * This is the equivalent of eval1(), eval2(), etc.
2712 */
2713
2714/*
2715 * number number constant
2716 * 0zFFFFFFFF Blob constant
2717 * "string" string constant
2718 * 'string' literal string constant
2719 * &option-name option value
2720 * @r register contents
2721 * identifier variable value
2722 * function() function call
2723 * $VAR environment variable
2724 * (expression) nested expression
2725 * [expr, expr] List
2726 * {key: val, key: val} Dictionary
2727 * #{key: val, key: val} Dictionary with literal keys
2728 *
2729 * Also handle:
2730 * ! in front logical NOT
2731 * - in front unary minus
2732 * + in front unary plus (ignored)
2733 * trailing (arg) funcref/partial call
2734 * trailing [] subscript in String or List
2735 * trailing .name entry in Dictionary
2736 * trailing ->name() method call
2737 */
2738 static int
2739compile_expr7(char_u **arg, cctx_T *cctx)
2740{
2741 typval_T rettv;
2742 char_u *start_leader, *end_leader;
2743 int ret = OK;
2744
2745 /*
2746 * Skip '!', '-' and '+' characters. They are handled later.
2747 */
2748 start_leader = *arg;
2749 while (**arg == '!' || **arg == '-' || **arg == '+')
2750 *arg = skipwhite(*arg + 1);
2751 end_leader = *arg;
2752
2753 rettv.v_type = VAR_UNKNOWN;
2754 switch (**arg)
2755 {
2756 /*
2757 * Number constant.
2758 */
2759 case '0': // also for blob starting with 0z
2760 case '1':
2761 case '2':
2762 case '3':
2763 case '4':
2764 case '5':
2765 case '6':
2766 case '7':
2767 case '8':
2768 case '9':
2769 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
2770 return FAIL;
2771 break;
2772
2773 /*
2774 * String constant: "string".
2775 */
2776 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
2777 return FAIL;
2778 break;
2779
2780 /*
2781 * Literal string constant: 'str''ing'.
2782 */
2783 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
2784 return FAIL;
2785 break;
2786
2787 /*
2788 * Constant Vim variable.
2789 */
2790 case 'v': get_vim_constant(arg, &rettv);
2791 ret = NOTDONE;
2792 break;
2793
2794 /*
2795 * List: [expr, expr]
2796 */
2797 case '[': ret = compile_list(arg, cctx);
2798 break;
2799
2800 /*
2801 * Dictionary: #{key: val, key: val}
2802 */
2803 case '#': if ((*arg)[1] == '{')
2804 {
2805 ++*arg;
2806 ret = compile_dict(arg, cctx, TRUE);
2807 }
2808 else
2809 ret = NOTDONE;
2810 break;
2811
2812 /*
2813 * Lambda: {arg, arg -> expr}
2814 * Dictionary: {'key': val, 'key': val}
2815 */
2816 case '{': {
2817 char_u *start = skipwhite(*arg + 1);
2818
2819 // Find out what comes after the arguments.
2820 ret = get_function_args(&start, '-', NULL,
2821 NULL, NULL, NULL, TRUE);
2822 if (ret != FAIL && *start == '>')
2823 ret = compile_lambda(arg, cctx);
2824 else
2825 ret = compile_dict(arg, cctx, FALSE);
2826 }
2827 break;
2828
2829 /*
2830 * Option value: &name
2831 */
2832 case '&': ret = compile_get_option(arg, cctx);
2833 break;
2834
2835 /*
2836 * Environment variable: $VAR.
2837 */
2838 case '$': ret = compile_get_env(arg, cctx);
2839 break;
2840
2841 /*
2842 * Register contents: @r.
2843 */
2844 case '@': ret = compile_get_register(arg, cctx);
2845 break;
2846 /*
2847 * nested expression: (expression).
2848 */
2849 case '(': *arg = skipwhite(*arg + 1);
2850 ret = compile_expr1(arg, cctx); // recursive!
2851 *arg = skipwhite(*arg);
2852 if (**arg == ')')
2853 ++*arg;
2854 else if (ret == OK)
2855 {
2856 emsg(_(e_missing_close));
2857 ret = FAIL;
2858 }
2859 break;
2860
2861 default: ret = NOTDONE;
2862 break;
2863 }
2864 if (ret == FAIL)
2865 return FAIL;
2866
2867 if (rettv.v_type != VAR_UNKNOWN)
2868 {
2869 // apply the '!', '-' and '+' before the constant
2870 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
2871 {
2872 clear_tv(&rettv);
2873 return FAIL;
2874 }
2875 start_leader = end_leader; // don't apply again below
2876
2877 // push constant
2878 switch (rettv.v_type)
2879 {
2880 case VAR_BOOL:
2881 generate_PUSHBOOL(cctx, rettv.vval.v_number);
2882 break;
2883 case VAR_SPECIAL:
2884 generate_PUSHSPEC(cctx, rettv.vval.v_number);
2885 break;
2886 case VAR_NUMBER:
2887 generate_PUSHNR(cctx, rettv.vval.v_number);
2888 break;
2889#ifdef FEAT_FLOAT
2890 case VAR_FLOAT:
2891 generate_PUSHF(cctx, rettv.vval.v_float);
2892 break;
2893#endif
2894 case VAR_BLOB:
2895 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
2896 rettv.vval.v_blob = NULL;
2897 break;
2898 case VAR_STRING:
2899 generate_PUSHS(cctx, rettv.vval.v_string);
2900 rettv.vval.v_string = NULL;
2901 break;
2902 default:
2903 iemsg("constant type missing");
2904 return FAIL;
2905 }
2906 }
2907 else if (ret == NOTDONE)
2908 {
2909 char_u *p;
2910 int r;
2911
2912 if (!eval_isnamec1(**arg))
2913 {
2914 semsg(_("E1015: Name expected: %s"), *arg);
2915 return FAIL;
2916 }
2917
2918 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002919 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 if (*p == '(')
2921 r = compile_call(arg, p - *arg, cctx, 0);
2922 else
2923 r = compile_load(arg, p, cctx, TRUE);
2924 if (r == FAIL)
2925 return FAIL;
2926 }
2927
2928 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
2929 return FAIL;
2930
2931 // Now deal with prefixed '-', '+' and '!', if not done already.
2932 return compile_leader(cctx, start_leader, end_leader);
2933}
2934
2935/*
2936 * * number multiplication
2937 * / number division
2938 * % number modulo
2939 */
2940 static int
2941compile_expr6(char_u **arg, cctx_T *cctx)
2942{
2943 char_u *op;
2944
2945 // get the first variable
2946 if (compile_expr7(arg, cctx) == FAIL)
2947 return FAIL;
2948
2949 /*
2950 * Repeat computing, until no "*", "/" or "%" is following.
2951 */
2952 for (;;)
2953 {
2954 op = skipwhite(*arg);
2955 if (*op != '*' && *op != '/' && *op != '%')
2956 break;
2957 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
2958 {
2959 char_u buf[3];
2960
2961 vim_strncpy(buf, op, 1);
2962 semsg(_(e_white_both), buf);
2963 }
2964 *arg = skipwhite(op + 1);
2965
2966 // get the second variable
2967 if (compile_expr7(arg, cctx) == FAIL)
2968 return FAIL;
2969
2970 generate_two_op(cctx, op);
2971 }
2972
2973 return OK;
2974}
2975
2976/*
2977 * + number addition
2978 * - number subtraction
2979 * .. string concatenation
2980 */
2981 static int
2982compile_expr5(char_u **arg, cctx_T *cctx)
2983{
2984 char_u *op;
2985 int oplen;
2986
2987 // get the first variable
2988 if (compile_expr6(arg, cctx) == FAIL)
2989 return FAIL;
2990
2991 /*
2992 * Repeat computing, until no "+", "-" or ".." is following.
2993 */
2994 for (;;)
2995 {
2996 op = skipwhite(*arg);
2997 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
2998 break;
2999 oplen = (*op == '.' ? 2 : 1);
3000
3001 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
3002 {
3003 char_u buf[3];
3004
3005 vim_strncpy(buf, op, oplen);
3006 semsg(_(e_white_both), buf);
3007 }
3008
3009 *arg = skipwhite(op + oplen);
3010
3011 // get the second variable
3012 if (compile_expr6(arg, cctx) == FAIL)
3013 return FAIL;
3014
3015 if (*op == '.')
3016 {
3017 if (may_generate_2STRING(-2, cctx) == FAIL
3018 || may_generate_2STRING(-1, cctx) == FAIL)
3019 return FAIL;
3020 generate_instr_drop(cctx, ISN_CONCAT, 1);
3021 }
3022 else
3023 generate_two_op(cctx, op);
3024 }
3025
3026 return OK;
3027}
3028
Bram Moolenaar080457c2020-03-03 21:53:32 +01003029 static exptype_T
3030get_compare_type(char_u *p, int *len, int *type_is)
3031{
3032 exptype_T type = EXPR_UNKNOWN;
3033 int i;
3034
3035 switch (p[0])
3036 {
3037 case '=': if (p[1] == '=')
3038 type = EXPR_EQUAL;
3039 else if (p[1] == '~')
3040 type = EXPR_MATCH;
3041 break;
3042 case '!': if (p[1] == '=')
3043 type = EXPR_NEQUAL;
3044 else if (p[1] == '~')
3045 type = EXPR_NOMATCH;
3046 break;
3047 case '>': if (p[1] != '=')
3048 {
3049 type = EXPR_GREATER;
3050 *len = 1;
3051 }
3052 else
3053 type = EXPR_GEQUAL;
3054 break;
3055 case '<': if (p[1] != '=')
3056 {
3057 type = EXPR_SMALLER;
3058 *len = 1;
3059 }
3060 else
3061 type = EXPR_SEQUAL;
3062 break;
3063 case 'i': if (p[1] == 's')
3064 {
3065 // "is" and "isnot"; but not a prefix of a name
3066 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3067 *len = 5;
3068 i = p[*len];
3069 if (!isalnum(i) && i != '_')
3070 {
3071 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3072 *type_is = TRUE;
3073 }
3074 }
3075 break;
3076 }
3077 return type;
3078}
3079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080/*
3081 * expr5a == expr5b
3082 * expr5a =~ expr5b
3083 * expr5a != expr5b
3084 * expr5a !~ expr5b
3085 * expr5a > expr5b
3086 * expr5a >= expr5b
3087 * expr5a < expr5b
3088 * expr5a <= expr5b
3089 * expr5a is expr5b
3090 * expr5a isnot expr5b
3091 *
3092 * Produces instructions:
3093 * EVAL expr5a Push result of "expr5a"
3094 * EVAL expr5b Push result of "expr5b"
3095 * COMPARE one of the compare instructions
3096 */
3097 static int
3098compile_expr4(char_u **arg, cctx_T *cctx)
3099{
3100 exptype_T type = EXPR_UNKNOWN;
3101 char_u *p;
3102 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 int type_is = FALSE;
3104
3105 // get the first variable
3106 if (compile_expr5(arg, cctx) == FAIL)
3107 return FAIL;
3108
3109 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003110 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111
3112 /*
3113 * If there is a comparative operator, use it.
3114 */
3115 if (type != EXPR_UNKNOWN)
3116 {
3117 int ic = FALSE; // Default: do not ignore case
3118
3119 if (type_is && (p[len] == '?' || p[len] == '#'))
3120 {
3121 semsg(_(e_invexpr2), *arg);
3122 return FAIL;
3123 }
3124 // extra question mark appended: ignore case
3125 if (p[len] == '?')
3126 {
3127 ic = TRUE;
3128 ++len;
3129 }
3130 // extra '#' appended: match case (ignored)
3131 else if (p[len] == '#')
3132 ++len;
3133 // nothing appended: match case
3134
3135 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
3136 {
3137 char_u buf[7];
3138
3139 vim_strncpy(buf, p, len);
3140 semsg(_(e_white_both), buf);
3141 }
3142
3143 // get the second variable
3144 *arg = skipwhite(p + len);
3145 if (compile_expr5(arg, cctx) == FAIL)
3146 return FAIL;
3147
3148 generate_COMPARE(cctx, type, ic);
3149 }
3150
3151 return OK;
3152}
3153
3154/*
3155 * Compile || or &&.
3156 */
3157 static int
3158compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3159{
3160 char_u *p = skipwhite(*arg);
3161 int opchar = *op;
3162
3163 if (p[0] == opchar && p[1] == opchar)
3164 {
3165 garray_T *instr = &cctx->ctx_instr;
3166 garray_T end_ga;
3167
3168 /*
3169 * Repeat until there is no following "||" or "&&"
3170 */
3171 ga_init2(&end_ga, sizeof(int), 10);
3172 while (p[0] == opchar && p[1] == opchar)
3173 {
3174 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3175 semsg(_(e_white_both), op);
3176
3177 if (ga_grow(&end_ga, 1) == FAIL)
3178 {
3179 ga_clear(&end_ga);
3180 return FAIL;
3181 }
3182 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3183 ++end_ga.ga_len;
3184 generate_JUMP(cctx, opchar == '|'
3185 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3186
3187 // eval the next expression
3188 *arg = skipwhite(p + 2);
3189 if ((opchar == '|' ? compile_expr3(arg, cctx)
3190 : compile_expr4(arg, cctx)) == FAIL)
3191 {
3192 ga_clear(&end_ga);
3193 return FAIL;
3194 }
3195 p = skipwhite(*arg);
3196 }
3197
3198 // Fill in the end label in all jumps.
3199 while (end_ga.ga_len > 0)
3200 {
3201 isn_T *isn;
3202
3203 --end_ga.ga_len;
3204 isn = ((isn_T *)instr->ga_data)
3205 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3206 isn->isn_arg.jump.jump_where = instr->ga_len;
3207 }
3208 ga_clear(&end_ga);
3209 }
3210
3211 return OK;
3212}
3213
3214/*
3215 * expr4a && expr4a && expr4a logical AND
3216 *
3217 * Produces instructions:
3218 * EVAL expr4a Push result of "expr4a"
3219 * JUMP_AND_KEEP_IF_FALSE end
3220 * EVAL expr4b Push result of "expr4b"
3221 * JUMP_AND_KEEP_IF_FALSE end
3222 * EVAL expr4c Push result of "expr4c"
3223 * end:
3224 */
3225 static int
3226compile_expr3(char_u **arg, cctx_T *cctx)
3227{
3228 // get the first variable
3229 if (compile_expr4(arg, cctx) == FAIL)
3230 return FAIL;
3231
3232 // || and && work almost the same
3233 return compile_and_or(arg, cctx, "&&");
3234}
3235
3236/*
3237 * expr3a || expr3b || expr3c logical OR
3238 *
3239 * Produces instructions:
3240 * EVAL expr3a Push result of "expr3a"
3241 * JUMP_AND_KEEP_IF_TRUE end
3242 * EVAL expr3b Push result of "expr3b"
3243 * JUMP_AND_KEEP_IF_TRUE end
3244 * EVAL expr3c Push result of "expr3c"
3245 * end:
3246 */
3247 static int
3248compile_expr2(char_u **arg, cctx_T *cctx)
3249{
3250 // eval the first expression
3251 if (compile_expr3(arg, cctx) == FAIL)
3252 return FAIL;
3253
3254 // || and && work almost the same
3255 return compile_and_or(arg, cctx, "||");
3256}
3257
3258/*
3259 * Toplevel expression: expr2 ? expr1a : expr1b
3260 *
3261 * Produces instructions:
3262 * EVAL expr2 Push result of "expr"
3263 * JUMP_IF_FALSE alt jump if false
3264 * EVAL expr1a
3265 * JUMP_ALWAYS end
3266 * alt: EVAL expr1b
3267 * end:
3268 */
3269 static int
3270compile_expr1(char_u **arg, cctx_T *cctx)
3271{
3272 char_u *p;
3273
3274 // evaluate the first expression
3275 if (compile_expr2(arg, cctx) == FAIL)
3276 return FAIL;
3277
3278 p = skipwhite(*arg);
3279 if (*p == '?')
3280 {
3281 garray_T *instr = &cctx->ctx_instr;
3282 garray_T *stack = &cctx->ctx_type_stack;
3283 int alt_idx = instr->ga_len;
3284 int end_idx;
3285 isn_T *isn;
3286 type_T *type1;
3287 type_T *type2;
3288
3289 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3290 semsg(_(e_white_both), "?");
3291
3292 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3293
3294 // evaluate the second expression; any type is accepted
3295 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003296 if (compile_expr1(arg, cctx) == FAIL)
3297 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298
3299 // remember the type and drop it
3300 --stack->ga_len;
3301 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3302
3303 end_idx = instr->ga_len;
3304 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3305
3306 // jump here from JUMP_IF_FALSE
3307 isn = ((isn_T *)instr->ga_data) + alt_idx;
3308 isn->isn_arg.jump.jump_where = instr->ga_len;
3309
3310 // Check for the ":".
3311 p = skipwhite(*arg);
3312 if (*p != ':')
3313 {
3314 emsg(_(e_missing_colon));
3315 return FAIL;
3316 }
3317 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3318 semsg(_(e_white_both), ":");
3319
3320 // evaluate the third expression
3321 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003322 if (compile_expr1(arg, cctx) == FAIL)
3323 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324
3325 // If the types differ, the result has a more generic type.
3326 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003327 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328
3329 // jump here from JUMP_ALWAYS
3330 isn = ((isn_T *)instr->ga_data) + end_idx;
3331 isn->isn_arg.jump.jump_where = instr->ga_len;
3332 }
3333 return OK;
3334}
3335
3336/*
3337 * compile "return [expr]"
3338 */
3339 static char_u *
3340compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3341{
3342 char_u *p = arg;
3343 garray_T *stack = &cctx->ctx_type_stack;
3344 type_T *stack_type;
3345
3346 if (*p != NUL && *p != '|' && *p != '\n')
3347 {
3348 // compile return argument into instructions
3349 if (compile_expr1(&p, cctx) == FAIL)
3350 return NULL;
3351
3352 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3353 if (set_return_type)
3354 cctx->ctx_ufunc->uf_ret_type = stack_type;
3355 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3356 == FAIL)
3357 return NULL;
3358 }
3359 else
3360 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003361 // "set_return_type" cannot be TRUE, only used for a lambda which
3362 // always has an argument.
3363 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 {
3365 emsg(_("E1003: Missing return value"));
3366 return NULL;
3367 }
3368
3369 // No argument, return zero.
3370 generate_PUSHNR(cctx, 0);
3371 }
3372
3373 if (generate_instr(cctx, ISN_RETURN) == NULL)
3374 return NULL;
3375
3376 // "return val | endif" is possible
3377 return skipwhite(p);
3378}
3379
3380/*
3381 * Return the length of an assignment operator, or zero if there isn't one.
3382 */
3383 int
3384assignment_len(char_u *p, int *heredoc)
3385{
3386 if (*p == '=')
3387 {
3388 if (p[1] == '<' && p[2] == '<')
3389 {
3390 *heredoc = TRUE;
3391 return 3;
3392 }
3393 return 1;
3394 }
3395 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3396 return 2;
3397 if (STRNCMP(p, "..=", 3) == 0)
3398 return 3;
3399 return 0;
3400}
3401
3402// words that cannot be used as a variable
3403static char *reserved[] = {
3404 "true",
3405 "false",
3406 NULL
3407};
3408
3409/*
3410 * Get a line for "=<<".
3411 * Return a pointer to the line in allocated memory.
3412 * Return NULL for end-of-file or some error.
3413 */
3414 static char_u *
3415heredoc_getline(
3416 int c UNUSED,
3417 void *cookie,
3418 int indent UNUSED,
3419 int do_concat UNUSED)
3420{
3421 cctx_T *cctx = (cctx_T *)cookie;
3422
3423 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003424 {
3425 iemsg("Heredoc got to end");
3426 return NULL;
3427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 ++cctx->ctx_lnum;
3429 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3430 [cctx->ctx_lnum]);
3431}
3432
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003433typedef enum {
3434 dest_local,
3435 dest_option,
3436 dest_env,
3437 dest_global,
3438 dest_vimvar,
3439 dest_script,
3440 dest_reg,
3441} assign_dest_T;
3442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443/*
3444 * compile "let var [= expr]", "const var = expr" and "var = expr"
3445 * "arg" points to "var".
3446 */
3447 static char_u *
3448compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3449{
3450 char_u *p;
3451 char_u *ret = NULL;
3452 int var_count = 0;
3453 int semicolon = 0;
3454 size_t varlen;
3455 garray_T *instr = &cctx->ctx_instr;
3456 int idx = -1;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003457 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003460 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003462 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 int oplen = 0;
3464 int heredoc = FALSE;
3465 type_T *type;
3466 lvar_T *lvar;
3467 char_u *name;
3468 char_u *sp;
3469 int has_type = FALSE;
3470 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3471 int instr_count = -1;
3472
3473 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3474 if (p == NULL)
3475 return NULL;
3476 if (var_count > 0)
3477 {
3478 // TODO: let [var, var] = list
3479 emsg("Cannot handle a list yet");
3480 return NULL;
3481 }
3482
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003483 // "a: type" is declaring variable "a" with a type, not "a:".
3484 if (is_decl && p == arg + 2 && p[-1] == ':')
3485 --p;
3486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 varlen = p - arg;
3488 name = vim_strnsave(arg, (int)varlen);
3489 if (name == NULL)
3490 return NULL;
3491
Bram Moolenaar080457c2020-03-03 21:53:32 +01003492 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003494 if (*arg == '&')
3495 {
3496 int cc;
3497 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498
Bram Moolenaar080457c2020-03-03 21:53:32 +01003499 dest = dest_option;
3500 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003502 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003503 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 if (is_decl)
3506 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003507 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 goto theend;
3509 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003510 p = arg;
3511 p = find_option_end(&p, &opt_flags);
3512 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003514 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01003515 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003516 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003517 }
3518 cc = *p;
3519 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003520 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003521 *p = cc;
3522 if (opt_type == -3)
3523 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003524 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003525 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003526 }
3527 if (opt_type == -2 || opt_type == 0)
3528 type = &t_string;
3529 else
3530 type = &t_number; // both number and boolean option
3531 }
3532 else if (*arg == '$')
3533 {
3534 dest = dest_env;
3535 if (is_decl)
3536 {
3537 semsg(_("E1065: Cannot declare an environment variable: %s"), name);
3538 goto theend;
3539 }
3540 }
3541 else if (*arg == '@')
3542 {
3543 if (!valid_yank_reg(arg[1], TRUE))
3544 {
3545 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003546 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003547 }
3548 dest = dest_reg;
3549 if (is_decl)
3550 {
3551 semsg(_("E1066: Cannot declare a register: %s"), name);
3552 goto theend;
3553 }
3554 }
3555 else if (STRNCMP(arg, "g:", 2) == 0)
3556 {
3557 dest = dest_global;
3558 if (is_decl)
3559 {
3560 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3561 goto theend;
3562 }
3563 }
3564 else if (STRNCMP(arg, "v:", 2) == 0)
3565 {
3566 vimvaridx = find_vim_var(name + 2);
3567 if (vimvaridx < 0)
3568 {
3569 semsg(_(e_var_notfound), arg);
3570 goto theend;
3571 }
3572 dest = dest_vimvar;
3573 if (is_decl)
3574 {
3575 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3576 goto theend;
3577 }
3578 }
3579 else
3580 {
3581 for (idx = 0; reserved[idx] != NULL; ++idx)
3582 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003584 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 goto theend;
3586 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003587
3588 idx = lookup_local(arg, varlen, cctx);
3589 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003591 if (is_decl)
3592 {
3593 semsg(_("E1017: Variable already declared: %s"), name);
3594 goto theend;
3595 }
3596 else
3597 {
3598 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3599 if (lvar->lv_const)
3600 {
3601 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3602 goto theend;
3603 }
3604 }
3605 }
3606 else if (STRNCMP(arg, "s:", 2) == 0
3607 || lookup_script(arg, varlen) == OK
3608 || find_imported(arg, varlen, cctx) != NULL)
3609 {
3610 dest = dest_script;
3611 if (is_decl)
3612 {
3613 semsg(_("E1054: Variable already declared in the script: %s"),
3614 name);
3615 goto theend;
3616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 }
3618 }
3619 }
3620
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003621 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 {
3623 if (is_decl && *p == ':')
3624 {
3625 // parse optional type: "let var: type = expr"
3626 p = skipwhite(p + 1);
3627 type = parse_type(&p, cctx->ctx_type_list);
3628 if (type == NULL)
3629 goto theend;
3630 has_type = TRUE;
3631 }
3632 else if (idx < 0)
3633 {
3634 // global and new local default to "any" type
3635 type = &t_any;
3636 }
3637 else
3638 {
3639 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3640 type = lvar->lv_type;
3641 }
3642 }
3643
3644 sp = p;
3645 p = skipwhite(p);
3646 op = p;
3647 oplen = assignment_len(p, &heredoc);
3648 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3649 {
3650 char_u buf[4];
3651
3652 vim_strncpy(buf, op, oplen);
3653 semsg(_(e_white_both), buf);
3654 }
3655
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003656 if (oplen == 3 && !heredoc && dest != dest_global
3657 && type->tt_type != VAR_STRING && type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01003659 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 goto theend;
3661 }
3662
3663 // +=, /=, etc. require an existing variable
Bram Moolenaar080457c2020-03-03 21:53:32 +01003664 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 {
3666 if (oplen > 1 && !heredoc)
3667 {
3668 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3669 name);
3670 goto theend;
3671 }
3672
3673 // new local variable
3674 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3675 if (idx < 0)
3676 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003677 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 }
3679
3680 if (heredoc)
3681 {
3682 list_T *l;
3683 listitem_T *li;
3684
3685 // [let] varname =<< [trim] {end}
3686 eap->getline = heredoc_getline;
3687 eap->cookie = cctx;
3688 l = heredoc_get(eap, op + 3);
3689
3690 // Push each line and the create the list.
3691 for (li = l->lv_first; li != NULL; li = li->li_next)
3692 {
3693 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3694 li->li_tv.vval.v_string = NULL;
3695 }
3696 generate_NEWLIST(cctx, l->lv_len);
3697 type = &t_list_string;
3698 list_free(l);
3699 p += STRLEN(p);
3700 }
3701 else if (oplen > 0)
3702 {
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003703 int r;
3704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 // for "+=", "*=", "..=" etc. first load the current value
3706 if (*op != '=')
3707 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003708 switch (dest)
3709 {
3710 case dest_option:
3711 // TODO: check the option exists
3712 generate_LOAD(cctx, ISN_LOADOPT, 0, name + 1, type);
3713 break;
3714 case dest_global:
3715 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3716 break;
3717 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003718 compile_load_scriptvar(cctx,
3719 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003720 break;
3721 case dest_env:
3722 // Include $ in the name here
3723 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
3724 break;
3725 case dest_reg:
3726 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
3727 break;
3728 case dest_vimvar:
3729 generate_LOADV(cctx, name + 2, TRUE);
3730 break;
3731 case dest_local:
3732 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3733 break;
3734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 }
3736
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003737 // Compile the expression. Temporarily hide the new local variable
3738 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02003739 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003740 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 instr_count = instr->ga_len;
3742 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003743 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02003744 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003745 ++cctx->ctx_locals.ga_len;
3746 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 goto theend;
3748
3749 if (idx >= 0 && (is_decl || !has_type))
3750 {
3751 garray_T *stack = &cctx->ctx_type_stack;
3752 type_T *stacktype =
3753 ((type_T **)stack->ga_data)[stack->ga_len - 1];
3754
3755 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3756 if (!has_type)
3757 {
3758 if (stacktype->tt_type == VAR_VOID)
3759 {
3760 emsg(_("E1031: Cannot use void value"));
3761 goto theend;
3762 }
3763 else
3764 lvar->lv_type = stacktype;
3765 }
3766 else
3767 if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
3768 goto theend;
3769 }
3770 }
3771 else if (cmdidx == CMD_const)
3772 {
3773 emsg(_("E1021: const requires a value"));
3774 goto theend;
3775 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003776 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 {
3778 emsg(_("E1022: type or initialization required"));
3779 goto theend;
3780 }
3781 else
3782 {
3783 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 if (ga_grow(instr, 1) == FAIL)
3785 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01003786 switch (type->tt_type)
3787 {
3788 case VAR_BOOL:
3789 generate_PUSHBOOL(cctx, VVAL_FALSE);
3790 break;
3791 case VAR_SPECIAL:
3792 generate_PUSHSPEC(cctx, VVAL_NONE);
3793 break;
3794 case VAR_FLOAT:
3795#ifdef FEAT_FLOAT
3796 generate_PUSHF(cctx, 0.0);
3797#endif
3798 break;
3799 case VAR_STRING:
3800 generate_PUSHS(cctx, NULL);
3801 break;
3802 case VAR_BLOB:
3803 generate_PUSHBLOB(cctx, NULL);
3804 break;
3805 case VAR_FUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003806 generate_PUSHFUNC(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003807 break;
3808 case VAR_PARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003809 generate_PUSHPARTIAL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003810 break;
3811 case VAR_LIST:
3812 generate_NEWLIST(cctx, 0);
3813 break;
3814 case VAR_DICT:
3815 generate_NEWDICT(cctx, 0);
3816 break;
3817 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003818 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003819 break;
3820 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003821 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01003822 break;
3823 case VAR_NUMBER:
3824 case VAR_UNKNOWN:
3825 case VAR_VOID:
3826 generate_PUSHNR(cctx, 0);
3827 break;
3828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 }
3830
3831 if (oplen > 0 && *op != '=')
3832 {
3833 type_T *expected = &t_number;
3834 garray_T *stack = &cctx->ctx_type_stack;
3835 type_T *stacktype;
3836
3837 // TODO: if type is known use float or any operation
3838
3839 if (*op == '.')
3840 expected = &t_string;
3841 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3842 if (need_type(stacktype, expected, -1, cctx) == FAIL)
3843 goto theend;
3844
3845 if (*op == '.')
3846 generate_instr_drop(cctx, ISN_CONCAT, 1);
3847 else
3848 {
3849 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3850
3851 if (isn == NULL)
3852 goto theend;
3853 switch (*op)
3854 {
3855 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3856 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3857 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3858 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3859 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3860 }
3861 }
3862 }
3863
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003864 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003866 case dest_option:
3867 generate_STOREOPT(cctx, name + 1, opt_flags);
3868 break;
3869 case dest_global:
3870 // include g: with the name, easier to execute that way
3871 generate_STORE(cctx, ISN_STOREG, 0, name);
3872 break;
3873 case dest_env:
3874 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
3875 break;
3876 case dest_reg:
3877 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
3878 break;
3879 case dest_vimvar:
3880 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
3881 break;
3882 case dest_script:
3883 {
3884 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
3885 imported_T *import = NULL;
3886 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003887
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003888 if (name[1] != ':')
3889 {
3890 import = find_imported(name, 0, cctx);
3891 if (import != NULL)
3892 sid = import->imp_sid;
3893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003895 idx = get_script_item_idx(sid, rawname, TRUE);
3896 // TODO: specific type
3897 if (idx < 0)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003898 generate_OLDSCRIPT(cctx, ISN_STORES, name, sid, &t_any);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003899 else
3900 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
3901 sid, idx, &t_any);
3902 }
3903 break;
3904 case dest_local:
3905 {
3906 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003908 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
3909 // into ISN_STORENR
3910 if (instr->ga_len == instr_count + 1
3911 && isn->isn_type == ISN_PUSHNR)
3912 {
3913 varnumber_T val = isn->isn_arg.number;
3914 garray_T *stack = &cctx->ctx_type_stack;
3915
3916 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003917 isn->isn_arg.storenr.stnr_idx = idx;
3918 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003919 if (stack->ga_len > 0)
3920 --stack->ga_len;
3921 }
3922 else
3923 generate_STORE(cctx, ISN_STORE, idx, NULL);
3924 }
3925 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 }
3927 ret = p;
3928
3929theend:
3930 vim_free(name);
3931 return ret;
3932}
3933
3934/*
3935 * Compile an :import command.
3936 */
3937 static char_u *
3938compile_import(char_u *arg, cctx_T *cctx)
3939{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01003940 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941}
3942
3943/*
3944 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3945 */
3946 static int
3947compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3948{
3949 garray_T *instr = &cctx->ctx_instr;
3950 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3951
3952 if (endlabel == NULL)
3953 return FAIL;
3954 endlabel->el_next = *el;
3955 *el = endlabel;
3956 endlabel->el_end_label = instr->ga_len;
3957
3958 generate_JUMP(cctx, when, 0);
3959 return OK;
3960}
3961
3962 static void
3963compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3964{
3965 garray_T *instr = &cctx->ctx_instr;
3966
3967 while (*el != NULL)
3968 {
3969 endlabel_T *cur = (*el);
3970 isn_T *isn;
3971
3972 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3973 isn->isn_arg.jump.jump_where = instr->ga_len;
3974 *el = cur->el_next;
3975 vim_free(cur);
3976 }
3977}
3978
3979/*
3980 * Create a new scope and set up the generic items.
3981 */
3982 static scope_T *
3983new_scope(cctx_T *cctx, scopetype_T type)
3984{
3985 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
3986
3987 if (scope == NULL)
3988 return NULL;
3989 scope->se_outer = cctx->ctx_scope;
3990 cctx->ctx_scope = scope;
3991 scope->se_type = type;
3992 scope->se_local_count = cctx->ctx_locals.ga_len;
3993 return scope;
3994}
3995
3996/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003997 * Free the current scope and go back to the outer scope.
3998 */
3999 static void
4000drop_scope(cctx_T *cctx)
4001{
4002 scope_T *scope = cctx->ctx_scope;
4003
4004 if (scope == NULL)
4005 {
4006 iemsg("calling drop_scope() without a scope");
4007 return;
4008 }
4009 cctx->ctx_scope = scope->se_outer;
4010 vim_free(scope);
4011}
4012
4013/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004014 * Evaluate an expression that is a constant:
4015 * has(arg)
4016 *
4017 * Also handle:
4018 * ! in front logical NOT
4019 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004020 * Return FAIL if the expression is not a constant.
4021 */
4022 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004023evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004024{
4025 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004026 char_u *start_leader, *end_leader;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004027
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004028 /*
4029 * Skip '!' characters. They are handled later.
4030 */
4031 start_leader = *arg;
4032 while (**arg == '!')
4033 *arg = skipwhite(*arg + 1);
4034 end_leader = *arg;
4035
4036 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004037 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004038 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004039 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4040 {
4041 tv->v_type = VAR_SPECIAL;
4042 tv->vval.v_number = VVAL_TRUE;
4043 *arg += 4;
4044 return OK;
4045 }
4046 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4047 {
4048 tv->v_type = VAR_SPECIAL;
4049 tv->vval.v_number = VVAL_FALSE;
4050 *arg += 5;
4051 return OK;
4052 }
4053
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004054 if (STRNCMP("has(", *arg, 4) != 0)
4055 return FAIL;
4056 *arg = skipwhite(*arg + 4);
4057
4058 if (**arg == '"')
4059 {
4060 if (get_string_tv(arg, tv, TRUE) == FAIL)
4061 return FAIL;
4062 }
4063 else if (**arg == '\'')
4064 {
4065 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4066 return FAIL;
4067 }
4068 else
4069 return FAIL;
4070
4071 *arg = skipwhite(*arg);
4072 if (**arg != ')')
4073 return FAIL;
4074 *arg = skipwhite(*arg + 1);
4075
4076 argvars[0] = *tv;
4077 argvars[1].v_type = VAR_UNKNOWN;
4078 tv->v_type = VAR_NUMBER;
4079 tv->vval.v_number = 0;
4080 f_has(argvars, tv);
4081 clear_tv(&argvars[0]);
4082
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004083 while (start_leader < end_leader)
4084 {
4085 if (*start_leader == '!')
4086 tv->vval.v_number = !tv->vval.v_number;
4087 ++start_leader;
4088 }
4089
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004090 return OK;
4091}
4092
Bram Moolenaar080457c2020-03-03 21:53:32 +01004093 static int
4094evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4095{
4096 exptype_T type = EXPR_UNKNOWN;
4097 char_u *p;
4098 int len = 2;
4099 int type_is = FALSE;
4100
4101 // get the first variable
4102 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4103 return FAIL;
4104
4105 p = skipwhite(*arg);
4106 type = get_compare_type(p, &len, &type_is);
4107
4108 /*
4109 * If there is a comparative operator, use it.
4110 */
4111 if (type != EXPR_UNKNOWN)
4112 {
4113 // TODO
4114 return FAIL;
4115 }
4116
4117 return OK;
4118}
4119
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004120static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4121
4122/*
4123 * Compile constant || or &&.
4124 */
4125 static int
4126evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4127{
4128 char_u *p = skipwhite(*arg);
4129 int opchar = *op;
4130
4131 if (p[0] == opchar && p[1] == opchar)
4132 {
4133 int val = tv2bool(tv);
4134
4135 /*
4136 * Repeat until there is no following "||" or "&&"
4137 */
4138 while (p[0] == opchar && p[1] == opchar)
4139 {
4140 typval_T tv2;
4141
4142 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4143 return FAIL;
4144
4145 // eval the next expression
4146 *arg = skipwhite(p + 2);
4147 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004148 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004149 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004150 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004151 {
4152 clear_tv(&tv2);
4153 return FAIL;
4154 }
4155 if ((opchar == '&') == val)
4156 {
4157 // false || tv2 or true && tv2: use tv2
4158 clear_tv(tv);
4159 *tv = tv2;
4160 val = tv2bool(tv);
4161 }
4162 else
4163 clear_tv(&tv2);
4164 p = skipwhite(*arg);
4165 }
4166 }
4167
4168 return OK;
4169}
4170
4171/*
4172 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4173 * Return FAIL if the expression is not a constant.
4174 */
4175 static int
4176evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4177{
4178 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004179 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004180 return FAIL;
4181
4182 // || and && work almost the same
4183 return evaluate_const_and_or(arg, cctx, "&&", tv);
4184}
4185
4186/*
4187 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4188 * Return FAIL if the expression is not a constant.
4189 */
4190 static int
4191evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4192{
4193 // evaluate the first expression
4194 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4195 return FAIL;
4196
4197 // || and && work almost the same
4198 return evaluate_const_and_or(arg, cctx, "||", tv);
4199}
4200
4201/*
4202 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4203 * E.g. for "has('feature')".
4204 * This does not produce error messages. "tv" should be cleared afterwards.
4205 * Return FAIL if the expression is not a constant.
4206 */
4207 static int
4208evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4209{
4210 char_u *p;
4211
4212 // evaluate the first expression
4213 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4214 return FAIL;
4215
4216 p = skipwhite(*arg);
4217 if (*p == '?')
4218 {
4219 int val = tv2bool(tv);
4220 typval_T tv2;
4221
4222 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4223 return FAIL;
4224
4225 // evaluate the second expression; any type is accepted
4226 clear_tv(tv);
4227 *arg = skipwhite(p + 1);
4228 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4229 return FAIL;
4230
4231 // Check for the ":".
4232 p = skipwhite(*arg);
4233 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4234 return FAIL;
4235
4236 // evaluate the third expression
4237 *arg = skipwhite(p + 1);
4238 tv2.v_type = VAR_UNKNOWN;
4239 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4240 {
4241 clear_tv(&tv2);
4242 return FAIL;
4243 }
4244 if (val)
4245 {
4246 // use the expr after "?"
4247 clear_tv(&tv2);
4248 }
4249 else
4250 {
4251 // use the expr after ":"
4252 clear_tv(tv);
4253 *tv = tv2;
4254 }
4255 }
4256 return OK;
4257}
4258
4259/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 * compile "if expr"
4261 *
4262 * "if expr" Produces instructions:
4263 * EVAL expr Push result of "expr"
4264 * JUMP_IF_FALSE end
4265 * ... body ...
4266 * end:
4267 *
4268 * "if expr | else" Produces instructions:
4269 * EVAL expr Push result of "expr"
4270 * JUMP_IF_FALSE else
4271 * ... body ...
4272 * JUMP_ALWAYS end
4273 * else:
4274 * ... body ...
4275 * end:
4276 *
4277 * "if expr1 | elseif expr2 | else" Produces instructions:
4278 * EVAL expr Push result of "expr"
4279 * JUMP_IF_FALSE elseif
4280 * ... body ...
4281 * JUMP_ALWAYS end
4282 * elseif:
4283 * EVAL expr Push result of "expr"
4284 * JUMP_IF_FALSE else
4285 * ... body ...
4286 * JUMP_ALWAYS end
4287 * else:
4288 * ... body ...
4289 * end:
4290 */
4291 static char_u *
4292compile_if(char_u *arg, cctx_T *cctx)
4293{
4294 char_u *p = arg;
4295 garray_T *instr = &cctx->ctx_instr;
4296 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004297 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004299 // compile "expr"; if we know it evaluates to FALSE skip the block
4300 tv.v_type = VAR_UNKNOWN;
4301 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4302 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4303 else
4304 cctx->ctx_skip = MAYBE;
4305 clear_tv(&tv);
4306 if (cctx->ctx_skip == MAYBE)
4307 {
4308 p = arg;
4309 if (compile_expr1(&p, cctx) == FAIL)
4310 return NULL;
4311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312
4313 scope = new_scope(cctx, IF_SCOPE);
4314 if (scope == NULL)
4315 return NULL;
4316
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004317 if (cctx->ctx_skip == MAYBE)
4318 {
4319 // "where" is set when ":elseif", "else" or ":endif" is found
4320 scope->se_u.se_if.is_if_label = instr->ga_len;
4321 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4322 }
4323 else
4324 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325
4326 return p;
4327}
4328
4329 static char_u *
4330compile_elseif(char_u *arg, cctx_T *cctx)
4331{
4332 char_u *p = arg;
4333 garray_T *instr = &cctx->ctx_instr;
4334 isn_T *isn;
4335 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004336 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337
4338 if (scope == NULL || scope->se_type != IF_SCOPE)
4339 {
4340 emsg(_(e_elseif_without_if));
4341 return NULL;
4342 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004343 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344
Bram Moolenaar158906c2020-02-06 20:39:45 +01004345 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004346 {
4347 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004349 return NULL;
4350 // previous "if" or "elseif" jumps here
4351 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4352 isn->isn_arg.jump.jump_where = instr->ga_len;
4353 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004355 // compile "expr"; if we know it evaluates to FALSE skip the block
4356 tv.v_type = VAR_UNKNOWN;
4357 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4358 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4359 else
4360 cctx->ctx_skip = MAYBE;
4361 clear_tv(&tv);
4362 if (cctx->ctx_skip == MAYBE)
4363 {
4364 p = arg;
4365 if (compile_expr1(&p, cctx) == FAIL)
4366 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004368 // "where" is set when ":elseif", "else" or ":endif" is found
4369 scope->se_u.se_if.is_if_label = instr->ga_len;
4370 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4371 }
4372 else
4373 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374
4375 return p;
4376}
4377
4378 static char_u *
4379compile_else(char_u *arg, cctx_T *cctx)
4380{
4381 char_u *p = arg;
4382 garray_T *instr = &cctx->ctx_instr;
4383 isn_T *isn;
4384 scope_T *scope = cctx->ctx_scope;
4385
4386 if (scope == NULL || scope->se_type != IF_SCOPE)
4387 {
4388 emsg(_(e_else_without_if));
4389 return NULL;
4390 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004391 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004393 // jump from previous block to the end, unless the else block is empty
4394 if (cctx->ctx_skip == MAYBE)
4395 {
4396 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004398 return NULL;
4399 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400
Bram Moolenaar158906c2020-02-06 20:39:45 +01004401 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004402 {
4403 if (scope->se_u.se_if.is_if_label >= 0)
4404 {
4405 // previous "if" or "elseif" jumps here
4406 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4407 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004408 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004409 }
4410 }
4411
4412 if (cctx->ctx_skip != MAYBE)
4413 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414
4415 return p;
4416}
4417
4418 static char_u *
4419compile_endif(char_u *arg, cctx_T *cctx)
4420{
4421 scope_T *scope = cctx->ctx_scope;
4422 ifscope_T *ifscope;
4423 garray_T *instr = &cctx->ctx_instr;
4424 isn_T *isn;
4425
4426 if (scope == NULL || scope->se_type != IF_SCOPE)
4427 {
4428 emsg(_(e_endif_without_if));
4429 return NULL;
4430 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004431 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004432 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004434 if (scope->se_u.se_if.is_if_label >= 0)
4435 {
4436 // previous "if" or "elseif" jumps here
4437 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4438 isn->isn_arg.jump.jump_where = instr->ga_len;
4439 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 // Fill in the "end" label in jumps at the end of the blocks.
4441 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004442 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004444 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 return arg;
4446}
4447
4448/*
4449 * compile "for var in expr"
4450 *
4451 * Produces instructions:
4452 * PUSHNR -1
4453 * STORE loop-idx Set index to -1
4454 * EVAL expr Push result of "expr"
4455 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4456 * - if beyond end, jump to "end"
4457 * - otherwise get item from list and push it
4458 * STORE var Store item in "var"
4459 * ... body ...
4460 * JUMP top Jump back to repeat
4461 * end: DROP Drop the result of "expr"
4462 *
4463 */
4464 static char_u *
4465compile_for(char_u *arg, cctx_T *cctx)
4466{
4467 char_u *p;
4468 size_t varlen;
4469 garray_T *instr = &cctx->ctx_instr;
4470 garray_T *stack = &cctx->ctx_type_stack;
4471 scope_T *scope;
4472 int loop_idx; // index of loop iteration variable
4473 int var_idx; // index of "var"
4474 type_T *vartype;
4475
4476 // TODO: list of variables: "for [key, value] in dict"
4477 // parse "var"
4478 for (p = arg; eval_isnamec1(*p); ++p)
4479 ;
4480 varlen = p - arg;
4481 var_idx = lookup_local(arg, varlen, cctx);
4482 if (var_idx >= 0)
4483 {
4484 semsg(_("E1023: variable already defined: %s"), arg);
4485 return NULL;
4486 }
4487
4488 // consume "in"
4489 p = skipwhite(p);
4490 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4491 {
4492 emsg(_(e_missing_in));
4493 return NULL;
4494 }
4495 p = skipwhite(p + 2);
4496
4497
4498 scope = new_scope(cctx, FOR_SCOPE);
4499 if (scope == NULL)
4500 return NULL;
4501
4502 // Reserve a variable to store the loop iteration counter.
4503 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4504 if (loop_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004505 {
4506 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509
4510 // Reserve a variable to store "var"
4511 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4512 if (var_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004513 {
4514 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517
4518 generate_STORENR(cctx, loop_idx, -1);
4519
4520 // compile "expr", it remains on the stack until "endfor"
4521 arg = p;
4522 if (compile_expr1(&arg, cctx) == FAIL)
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 // now we know the type of "var"
4529 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4530 if (vartype->tt_type != VAR_LIST)
4531 {
4532 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004533 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 return NULL;
4535 }
4536 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
4537 {
4538 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4539
4540 lvar->lv_type = vartype->tt_member;
4541 }
4542
4543 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004544 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545
4546 generate_FOR(cctx, loop_idx);
4547 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4548
4549 return arg;
4550}
4551
4552/*
4553 * compile "endfor"
4554 */
4555 static char_u *
4556compile_endfor(char_u *arg, cctx_T *cctx)
4557{
4558 garray_T *instr = &cctx->ctx_instr;
4559 scope_T *scope = cctx->ctx_scope;
4560 forscope_T *forscope;
4561 isn_T *isn;
4562
4563 if (scope == NULL || scope->se_type != FOR_SCOPE)
4564 {
4565 emsg(_(e_for));
4566 return NULL;
4567 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004568 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004570 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571
4572 // At end of ":for" scope jump back to the FOR instruction.
4573 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4574
4575 // Fill in the "end" label in the FOR statement so it can jump here
4576 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4577 isn->isn_arg.forloop.for_end = instr->ga_len;
4578
4579 // Fill in the "end" label any BREAK statements
4580 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4581
4582 // Below the ":for" scope drop the "expr" list from the stack.
4583 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4584 return NULL;
4585
4586 vim_free(scope);
4587
4588 return arg;
4589}
4590
4591/*
4592 * compile "while expr"
4593 *
4594 * Produces instructions:
4595 * top: EVAL expr Push result of "expr"
4596 * JUMP_IF_FALSE end jump if false
4597 * ... body ...
4598 * JUMP top Jump back to repeat
4599 * end:
4600 *
4601 */
4602 static char_u *
4603compile_while(char_u *arg, cctx_T *cctx)
4604{
4605 char_u *p = arg;
4606 garray_T *instr = &cctx->ctx_instr;
4607 scope_T *scope;
4608
4609 scope = new_scope(cctx, WHILE_SCOPE);
4610 if (scope == NULL)
4611 return NULL;
4612
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004613 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614
4615 // compile "expr"
4616 if (compile_expr1(&p, cctx) == FAIL)
4617 return NULL;
4618
4619 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004620 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 JUMP_IF_FALSE, cctx) == FAIL)
4622 return FAIL;
4623
4624 return p;
4625}
4626
4627/*
4628 * compile "endwhile"
4629 */
4630 static char_u *
4631compile_endwhile(char_u *arg, cctx_T *cctx)
4632{
4633 scope_T *scope = cctx->ctx_scope;
4634
4635 if (scope == NULL || scope->se_type != WHILE_SCOPE)
4636 {
4637 emsg(_(e_while));
4638 return NULL;
4639 }
4640 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004641 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642
4643 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004644 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645
4646 // Fill in the "end" label in the WHILE statement so it can jump here.
4647 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004648 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649
4650 vim_free(scope);
4651
4652 return arg;
4653}
4654
4655/*
4656 * compile "continue"
4657 */
4658 static char_u *
4659compile_continue(char_u *arg, cctx_T *cctx)
4660{
4661 scope_T *scope = cctx->ctx_scope;
4662
4663 for (;;)
4664 {
4665 if (scope == NULL)
4666 {
4667 emsg(_(e_continue));
4668 return NULL;
4669 }
4670 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4671 break;
4672 scope = scope->se_outer;
4673 }
4674
4675 // Jump back to the FOR or WHILE instruction.
4676 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004677 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
4678 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 return arg;
4680}
4681
4682/*
4683 * compile "break"
4684 */
4685 static char_u *
4686compile_break(char_u *arg, cctx_T *cctx)
4687{
4688 scope_T *scope = cctx->ctx_scope;
4689 endlabel_T **el;
4690
4691 for (;;)
4692 {
4693 if (scope == NULL)
4694 {
4695 emsg(_(e_break));
4696 return NULL;
4697 }
4698 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4699 break;
4700 scope = scope->se_outer;
4701 }
4702
4703 // Jump to the end of the FOR or WHILE loop.
4704 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004705 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004707 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
4709 return FAIL;
4710
4711 return arg;
4712}
4713
4714/*
4715 * compile "{" start of block
4716 */
4717 static char_u *
4718compile_block(char_u *arg, cctx_T *cctx)
4719{
4720 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4721 return NULL;
4722 return skipwhite(arg + 1);
4723}
4724
4725/*
4726 * compile end of block: drop one scope
4727 */
4728 static void
4729compile_endblock(cctx_T *cctx)
4730{
4731 scope_T *scope = cctx->ctx_scope;
4732
4733 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004734 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 vim_free(scope);
4736}
4737
4738/*
4739 * compile "try"
4740 * Creates a new scope for the try-endtry, pointing to the first catch and
4741 * finally.
4742 * Creates another scope for the "try" block itself.
4743 * TRY instruction sets up exception handling at runtime.
4744 *
4745 * "try"
4746 * TRY -> catch1, -> finally push trystack entry
4747 * ... try block
4748 * "throw {exception}"
4749 * EVAL {exception}
4750 * THROW create exception
4751 * ... try block
4752 * " catch {expr}"
4753 * JUMP -> finally
4754 * catch1: PUSH exeception
4755 * EVAL {expr}
4756 * MATCH
4757 * JUMP nomatch -> catch2
4758 * CATCH remove exception
4759 * ... catch block
4760 * " catch"
4761 * JUMP -> finally
4762 * catch2: CATCH remove exception
4763 * ... catch block
4764 * " finally"
4765 * finally:
4766 * ... finally block
4767 * " endtry"
4768 * ENDTRY pop trystack entry, may rethrow
4769 */
4770 static char_u *
4771compile_try(char_u *arg, cctx_T *cctx)
4772{
4773 garray_T *instr = &cctx->ctx_instr;
4774 scope_T *try_scope;
4775 scope_T *scope;
4776
4777 // scope that holds the jumps that go to catch/finally/endtry
4778 try_scope = new_scope(cctx, TRY_SCOPE);
4779 if (try_scope == NULL)
4780 return NULL;
4781
4782 // "catch" is set when the first ":catch" is found.
4783 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004784 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 if (generate_instr(cctx, ISN_TRY) == NULL)
4786 return NULL;
4787
4788 // scope for the try block itself
4789 scope = new_scope(cctx, BLOCK_SCOPE);
4790 if (scope == NULL)
4791 return NULL;
4792
4793 return arg;
4794}
4795
4796/*
4797 * compile "catch {expr}"
4798 */
4799 static char_u *
4800compile_catch(char_u *arg, cctx_T *cctx UNUSED)
4801{
4802 scope_T *scope = cctx->ctx_scope;
4803 garray_T *instr = &cctx->ctx_instr;
4804 char_u *p;
4805 isn_T *isn;
4806
4807 // end block scope from :try or :catch
4808 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4809 compile_endblock(cctx);
4810 scope = cctx->ctx_scope;
4811
4812 // Error if not in a :try scope
4813 if (scope == NULL || scope->se_type != TRY_SCOPE)
4814 {
4815 emsg(_(e_catch));
4816 return NULL;
4817 }
4818
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004819 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820 {
4821 emsg(_("E1033: catch unreachable after catch-all"));
4822 return NULL;
4823 }
4824
4825 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004826 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827 JUMP_ALWAYS, cctx) == FAIL)
4828 return NULL;
4829
4830 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004831 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 if (isn->isn_arg.try.try_catch == 0)
4833 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004834 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 {
4836 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004837 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 isn->isn_arg.jump.jump_where = instr->ga_len;
4839 }
4840
4841 p = skipwhite(arg);
4842 if (ends_excmd(*p))
4843 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004844 scope->se_u.se_try.ts_caught_all = TRUE;
4845 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 }
4847 else
4848 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004849 char_u *end;
4850 char_u *pat;
4851 char_u *tofree = NULL;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004852 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854 // Push v:exception, push {expr} and MATCH
4855 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
4856
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004857 end = skip_regexp(p + 1, *p, TRUE, &tofree);
4858 if (*end != *p)
4859 {
4860 semsg(_("E1067: Separator mismatch: %s"), p);
4861 vim_free(tofree);
4862 return FAIL;
4863 }
4864 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004865 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004866 else
Bram Moolenaar3dd64602020-02-13 20:31:28 +01004867 len = (int)(end - (tofree + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004868 pat = vim_strnsave(p + 1, len);
4869 vim_free(tofree);
4870 p += len + 2;
4871 if (pat == NULL)
4872 return FAIL;
4873 if (generate_PUSHS(cctx, pat) == FAIL)
4874 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4877 return NULL;
4878
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004879 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4881 return NULL;
4882 }
4883
4884 if (generate_instr(cctx, ISN_CATCH) == NULL)
4885 return NULL;
4886
4887 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4888 return NULL;
4889 return p;
4890}
4891
4892 static char_u *
4893compile_finally(char_u *arg, cctx_T *cctx)
4894{
4895 scope_T *scope = cctx->ctx_scope;
4896 garray_T *instr = &cctx->ctx_instr;
4897 isn_T *isn;
4898
4899 // end block scope from :try or :catch
4900 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4901 compile_endblock(cctx);
4902 scope = cctx->ctx_scope;
4903
4904 // Error if not in a :try scope
4905 if (scope == NULL || scope->se_type != TRY_SCOPE)
4906 {
4907 emsg(_(e_finally));
4908 return NULL;
4909 }
4910
4911 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004912 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913 if (isn->isn_arg.try.try_finally != 0)
4914 {
4915 emsg(_(e_finally_dup));
4916 return NULL;
4917 }
4918
4919 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004920 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004922 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923 {
4924 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004925 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 isn->isn_arg.jump.jump_where = instr->ga_len;
4927 }
4928
4929 isn->isn_arg.try.try_finally = instr->ga_len;
4930 // TODO: set index in ts_finally_label jumps
4931
4932 return arg;
4933}
4934
4935 static char_u *
4936compile_endtry(char_u *arg, cctx_T *cctx)
4937{
4938 scope_T *scope = cctx->ctx_scope;
4939 garray_T *instr = &cctx->ctx_instr;
4940 isn_T *isn;
4941
4942 // end block scope from :catch or :finally
4943 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4944 compile_endblock(cctx);
4945 scope = cctx->ctx_scope;
4946
4947 // Error if not in a :try scope
4948 if (scope == NULL || scope->se_type != TRY_SCOPE)
4949 {
4950 if (scope == NULL)
4951 emsg(_(e_no_endtry));
4952 else if (scope->se_type == WHILE_SCOPE)
4953 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01004954 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 emsg(_(e_endfor));
4956 else
4957 emsg(_(e_endif));
4958 return NULL;
4959 }
4960
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004961 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
4963 {
4964 emsg(_("E1032: missing :catch or :finally"));
4965 return NULL;
4966 }
4967
4968 // Fill in the "end" label in jumps at the end of the blocks, if not done
4969 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004970 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971
4972 // End :catch or :finally scope: set value in ISN_TRY instruction
4973 if (isn->isn_arg.try.try_finally == 0)
4974 isn->isn_arg.try.try_finally = instr->ga_len;
4975 compile_endblock(cctx);
4976
4977 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4978 return NULL;
4979 return arg;
4980}
4981
4982/*
4983 * compile "throw {expr}"
4984 */
4985 static char_u *
4986compile_throw(char_u *arg, cctx_T *cctx UNUSED)
4987{
4988 char_u *p = skipwhite(arg);
4989
4990 if (ends_excmd(*p))
4991 {
4992 emsg(_(e_argreq));
4993 return NULL;
4994 }
4995 if (compile_expr1(&p, cctx) == FAIL)
4996 return NULL;
4997 if (may_generate_2STRING(-1, cctx) == FAIL)
4998 return NULL;
4999 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5000 return NULL;
5001
5002 return p;
5003}
5004
5005/*
5006 * compile "echo expr"
5007 */
5008 static char_u *
5009compile_echo(char_u *arg, int with_white, cctx_T *cctx)
5010{
5011 char_u *p = arg;
5012 int count = 0;
5013
Bram Moolenaarad39c092020-02-26 18:23:43 +01005014 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 {
5016 if (compile_expr1(&p, cctx) == FAIL)
5017 return NULL;
5018 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005019 p = skipwhite(p);
5020 if (ends_excmd(*p))
5021 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022 }
5023
5024 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01005025 return p;
5026}
5027
5028/*
5029 * compile "execute expr"
5030 */
5031 static char_u *
5032compile_execute(char_u *arg, cctx_T *cctx)
5033{
5034 char_u *p = arg;
5035 int count = 0;
5036
5037 for (;;)
5038 {
5039 if (compile_expr1(&p, cctx) == FAIL)
5040 return NULL;
5041 ++count;
5042 p = skipwhite(p);
5043 if (ends_excmd(*p))
5044 break;
5045 }
5046
5047 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048
5049 return p;
5050}
5051
5052/*
5053 * After ex_function() has collected all the function lines: parse and compile
5054 * the lines into instructions.
5055 * Adds the function to "def_functions".
5056 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5057 * return statement (used for lambda).
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005058 * This can be used recursively through compile_lambda(), which may reallocate
5059 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 */
5061 void
5062compile_def_function(ufunc_T *ufunc, int set_return_type)
5063{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 char_u *line = NULL;
5065 char_u *p;
5066 exarg_T ea;
5067 char *errormsg = NULL; // error message
5068 int had_return = FALSE;
5069 cctx_T cctx;
5070 garray_T *instr;
5071 int called_emsg_before = called_emsg;
5072 int ret = FAIL;
5073 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005074 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005077 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005078
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005079 if (ufunc->uf_dfunc_idx >= 0)
5080 {
5081 // Redefining a function that was compiled before.
5082 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5083
5084 // Free old instructions.
5085 delete_def_function_contents(dfunc);
5086 }
5087 else
5088 {
5089 // Add the function to "def_functions".
5090 if (ga_grow(&def_functions, 1) == FAIL)
5091 return;
5092 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
5093 vim_memset(dfunc, 0, sizeof(dfunc_T));
5094 dfunc->df_idx = def_functions.ga_len;
5095 ufunc->uf_dfunc_idx = dfunc->df_idx;
5096 dfunc->df_ufunc = ufunc;
5097 ++def_functions.ga_len;
5098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099 }
5100
5101 vim_memset(&cctx, 0, sizeof(cctx));
5102 cctx.ctx_ufunc = ufunc;
5103 cctx.ctx_lnum = -1;
5104 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5105 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5106 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5107 cctx.ctx_type_list = &ufunc->uf_type_list;
5108 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5109 instr = &cctx.ctx_instr;
5110
5111 // Most modern script version.
5112 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5113
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005114 if (ufunc->uf_def_args.ga_len > 0)
5115 {
5116 int count = ufunc->uf_def_args.ga_len;
5117 int i;
5118 char_u *arg;
5119 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5120
5121 // Produce instructions for the default values of optional arguments.
5122 // Store the instruction index in uf_def_arg_idx[] so that we know
5123 // where to start when the function is called, depending on the number
5124 // of arguments.
5125 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5126 if (ufunc->uf_def_arg_idx == NULL)
5127 goto erret;
5128 for (i = 0; i < count; ++i)
5129 {
5130 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5131 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
5132 if (compile_expr1(&arg, &cctx) == FAIL
5133 || generate_STORE(&cctx, ISN_STORE,
5134 i - count - off, NULL) == FAIL)
5135 goto erret;
5136 }
5137
5138 // If a varargs is following, push an empty list.
5139 if (ufunc->uf_va_name != NULL)
5140 {
5141 if (generate_NEWLIST(&cctx, 0) == FAIL
5142 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
5143 goto erret;
5144 }
5145
5146 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5147 }
5148
5149 /*
5150 * Loop over all the lines of the function and generate instructions.
5151 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152 for (;;)
5153 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005154 int is_ex_command;
5155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005156 if (line != NULL && *line == '|')
5157 // the line continues after a '|'
5158 ++line;
5159 else if (line != NULL && *line != NUL)
5160 {
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005161 if (emsg_before == called_emsg)
5162 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163 goto erret;
5164 }
5165 else
5166 {
5167 do
5168 {
5169 ++cctx.ctx_lnum;
5170 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5171 break;
5172 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
5173 } while (line == NULL);
5174 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5175 break;
5176 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
5177 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005178 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179
5180 had_return = FALSE;
5181 vim_memset(&ea, 0, sizeof(ea));
5182 ea.cmdlinep = &line;
5183 ea.cmd = skipwhite(line);
5184
5185 // "}" ends a block scope
5186 if (*ea.cmd == '}')
5187 {
5188 scopetype_T stype = cctx.ctx_scope == NULL
5189 ? NO_SCOPE : cctx.ctx_scope->se_type;
5190
5191 if (stype == BLOCK_SCOPE)
5192 {
5193 compile_endblock(&cctx);
5194 line = ea.cmd;
5195 }
5196 else
5197 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005198 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199 goto erret;
5200 }
5201 if (line != NULL)
5202 line = skipwhite(ea.cmd + 1);
5203 continue;
5204 }
5205
5206 // "{" starts a block scope
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01005207 // "{'a': 1}->func() is something else
5208 if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209 {
5210 line = compile_block(ea.cmd, &cctx);
5211 continue;
5212 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005213 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214
5215 /*
5216 * COMMAND MODIFIERS
5217 */
5218 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5219 {
5220 if (errormsg != NULL)
5221 goto erret;
5222 // empty line or comment
5223 line = (char_u *)"";
5224 continue;
5225 }
5226
5227 // Skip ":call" to get to the function name.
5228 if (checkforcmd(&ea.cmd, "call", 3))
5229 ea.cmd = skipwhite(ea.cmd);
5230
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005231 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005232 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005233 // Assuming the command starts with a variable or function name,
5234 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5235 // val".
5236 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5237 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005238 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005239 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005241 int oplen;
5242 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005244 oplen = assignment_len(skipwhite(p), &heredoc);
5245 if (oplen > 0)
5246 {
5247 // Recognize an assignment if we recognize the variable
5248 // name:
5249 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005250 // "local = expr" where "local" is a local var.
5251 // "script = expr" where "script" is a script-local var.
5252 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005253 // "&opt = expr"
5254 // "$ENV = expr"
5255 // "@r = expr"
5256 if (*ea.cmd == '&'
5257 || *ea.cmd == '$'
5258 || *ea.cmd == '@'
5259 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5260 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5261 || lookup_script(ea.cmd, p - ea.cmd) == OK
5262 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5263 {
5264 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5265 if (line == NULL)
5266 goto erret;
5267 continue;
5268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269 }
5270 }
5271 }
5272
5273 /*
5274 * COMMAND after range
5275 */
5276 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005277 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5278 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279
5280 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5281 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005282 if (cctx.ctx_skip == TRUE)
5283 {
5284 line += STRLEN(line);
5285 continue;
5286 }
5287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288 // Expression or function call.
5289 if (ea.cmdidx == CMD_eval)
5290 {
5291 p = ea.cmd;
5292 if (compile_expr1(&p, &cctx) == FAIL)
5293 goto erret;
5294
5295 // drop the return value
5296 generate_instr_drop(&cctx, ISN_DROP, 1);
5297 line = p;
5298 continue;
5299 }
5300 if (ea.cmdidx == CMD_let)
5301 {
5302 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5303 if (line == NULL)
5304 goto erret;
5305 continue;
5306 }
5307 iemsg("Command from find_ex_command() not handled");
5308 goto erret;
5309 }
5310
5311 p = skipwhite(p);
5312
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005313 if (cctx.ctx_skip == TRUE
5314 && ea.cmdidx != CMD_elseif
5315 && ea.cmdidx != CMD_else
5316 && ea.cmdidx != CMD_endif)
5317 {
5318 line += STRLEN(line);
5319 continue;
5320 }
5321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005322 switch (ea.cmdidx)
5323 {
5324 case CMD_def:
5325 case CMD_function:
5326 // TODO: Nested function
5327 emsg("Nested function not implemented yet");
5328 goto erret;
5329
5330 case CMD_return:
5331 line = compile_return(p, set_return_type, &cctx);
5332 had_return = TRUE;
5333 break;
5334
5335 case CMD_let:
5336 case CMD_const:
5337 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5338 break;
5339
5340 case CMD_import:
5341 line = compile_import(p, &cctx);
5342 break;
5343
5344 case CMD_if:
5345 line = compile_if(p, &cctx);
5346 break;
5347 case CMD_elseif:
5348 line = compile_elseif(p, &cctx);
5349 break;
5350 case CMD_else:
5351 line = compile_else(p, &cctx);
5352 break;
5353 case CMD_endif:
5354 line = compile_endif(p, &cctx);
5355 break;
5356
5357 case CMD_while:
5358 line = compile_while(p, &cctx);
5359 break;
5360 case CMD_endwhile:
5361 line = compile_endwhile(p, &cctx);
5362 break;
5363
5364 case CMD_for:
5365 line = compile_for(p, &cctx);
5366 break;
5367 case CMD_endfor:
5368 line = compile_endfor(p, &cctx);
5369 break;
5370 case CMD_continue:
5371 line = compile_continue(p, &cctx);
5372 break;
5373 case CMD_break:
5374 line = compile_break(p, &cctx);
5375 break;
5376
5377 case CMD_try:
5378 line = compile_try(p, &cctx);
5379 break;
5380 case CMD_catch:
5381 line = compile_catch(p, &cctx);
5382 break;
5383 case CMD_finally:
5384 line = compile_finally(p, &cctx);
5385 break;
5386 case CMD_endtry:
5387 line = compile_endtry(p, &cctx);
5388 break;
5389 case CMD_throw:
5390 line = compile_throw(p, &cctx);
5391 break;
5392
5393 case CMD_echo:
5394 line = compile_echo(p, TRUE, &cctx);
5395 break;
5396 case CMD_echon:
5397 line = compile_echo(p, FALSE, &cctx);
5398 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005399 case CMD_execute:
5400 line = compile_execute(p, &cctx);
5401 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402
5403 default:
5404 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005405 // TODO:
5406 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005407 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005408 generate_EXEC(&cctx, line);
5409 line = (char_u *)"";
5410 break;
5411 }
5412 if (line == NULL)
5413 goto erret;
5414
5415 if (cctx.ctx_type_stack.ga_len < 0)
5416 {
5417 iemsg("Type stack underflow");
5418 goto erret;
5419 }
5420 }
5421
5422 if (cctx.ctx_scope != NULL)
5423 {
5424 if (cctx.ctx_scope->se_type == IF_SCOPE)
5425 emsg(_(e_endif));
5426 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5427 emsg(_(e_endwhile));
5428 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5429 emsg(_(e_endfor));
5430 else
5431 emsg(_("E1026: Missing }"));
5432 goto erret;
5433 }
5434
5435 if (!had_return)
5436 {
5437 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5438 {
5439 emsg(_("E1027: Missing return statement"));
5440 goto erret;
5441 }
5442
5443 // Return zero if there is no return at the end.
5444 generate_PUSHNR(&cctx, 0);
5445 generate_instr(&cctx, ISN_RETURN);
5446 }
5447
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005448 {
5449 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5450 + ufunc->uf_dfunc_idx;
5451 dfunc->df_deleted = FALSE;
5452 dfunc->df_instr = instr->ga_data;
5453 dfunc->df_instr_count = instr->ga_len;
5454 dfunc->df_varcount = cctx.ctx_max_local;
5455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456
5457 ret = OK;
5458
5459erret:
5460 if (ret == FAIL)
5461 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005462 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005463 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5464 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005465
5466 for (idx = 0; idx < instr->ga_len; ++idx)
5467 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005471 if (!dfunc->df_deleted)
5472 --def_functions.ga_len;
5473
5474 // Don't execute this function body.
5475 ga_clear_strings(&ufunc->uf_lines);
5476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005477 if (errormsg != NULL)
5478 emsg(errormsg);
5479 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005480 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005481 }
5482
5483 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005484 free_imported(&cctx);
5485 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005486 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005487}
5488
5489/*
5490 * Delete an instruction, free what it contains.
5491 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01005492 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493delete_instr(isn_T *isn)
5494{
5495 switch (isn->isn_type)
5496 {
5497 case ISN_EXEC:
5498 case ISN_LOADENV:
5499 case ISN_LOADG:
5500 case ISN_LOADOPT:
5501 case ISN_MEMBER:
5502 case ISN_PUSHEXC:
5503 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005504 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005505 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005506 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005507 vim_free(isn->isn_arg.string);
5508 break;
5509
5510 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005511 case ISN_STORES:
5512 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513 break;
5514
5515 case ISN_STOREOPT:
5516 vim_free(isn->isn_arg.storeopt.so_name);
5517 break;
5518
5519 case ISN_PUSHBLOB: // push blob isn_arg.blob
5520 blob_unref(isn->isn_arg.blob);
5521 break;
5522
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005523 case ISN_PUSHPARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01005524 partial_unref(isn->isn_arg.partial);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005525 break;
5526
5527 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005528#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005529 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005530#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005531 break;
5532
5533 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005534#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005535 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005536#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005537 break;
5538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005539 case ISN_UCALL:
5540 vim_free(isn->isn_arg.ufunc.cuf_name);
5541 break;
5542
5543 case ISN_2BOOL:
5544 case ISN_2STRING:
5545 case ISN_ADDBLOB:
5546 case ISN_ADDLIST:
5547 case ISN_BCALL:
5548 case ISN_CATCH:
5549 case ISN_CHECKNR:
5550 case ISN_CHECKTYPE:
5551 case ISN_COMPAREANY:
5552 case ISN_COMPAREBLOB:
5553 case ISN_COMPAREBOOL:
5554 case ISN_COMPAREDICT:
5555 case ISN_COMPAREFLOAT:
5556 case ISN_COMPAREFUNC:
5557 case ISN_COMPARELIST:
5558 case ISN_COMPARENR:
5559 case ISN_COMPAREPARTIAL:
5560 case ISN_COMPARESPECIAL:
5561 case ISN_COMPARESTRING:
5562 case ISN_CONCAT:
5563 case ISN_DCALL:
5564 case ISN_DROP:
5565 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01005566 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005567 case ISN_ENDTRY:
5568 case ISN_FOR:
5569 case ISN_FUNCREF:
5570 case ISN_INDEX:
5571 case ISN_JUMP:
5572 case ISN_LOAD:
5573 case ISN_LOADSCRIPT:
5574 case ISN_LOADREG:
5575 case ISN_LOADV:
5576 case ISN_NEGATENR:
5577 case ISN_NEWDICT:
5578 case ISN_NEWLIST:
5579 case ISN_OPNR:
5580 case ISN_OPFLOAT:
5581 case ISN_OPANY:
5582 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005583 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 case ISN_PUSHF:
5585 case ISN_PUSHNR:
5586 case ISN_PUSHBOOL:
5587 case ISN_PUSHSPEC:
5588 case ISN_RETURN:
5589 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005590 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005591 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005592 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005593 case ISN_STORESCRIPT:
5594 case ISN_THROW:
5595 case ISN_TRY:
5596 // nothing allocated
5597 break;
5598 }
5599}
5600
5601/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01005602 * Free all instructions for "dfunc".
5603 */
5604 static void
5605delete_def_function_contents(dfunc_T *dfunc)
5606{
5607 int idx;
5608
5609 ga_clear(&dfunc->df_def_args_isn);
5610
5611 if (dfunc->df_instr != NULL)
5612 {
5613 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5614 delete_instr(dfunc->df_instr + idx);
5615 VIM_CLEAR(dfunc->df_instr);
5616 }
5617
5618 dfunc->df_deleted = TRUE;
5619}
5620
5621/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005622 * When a user function is deleted, delete any associated def function.
5623 */
5624 void
5625delete_def_function(ufunc_T *ufunc)
5626{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005627 if (ufunc->uf_dfunc_idx >= 0)
5628 {
5629 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5630 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005631
Bram Moolenaar20431c92020-03-20 18:39:46 +01005632 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633 }
5634}
5635
5636#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005637/*
5638 * Free all functions defined with ":def".
5639 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640 void
5641free_def_functions(void)
5642{
Bram Moolenaar20431c92020-03-20 18:39:46 +01005643 int idx;
5644
5645 for (idx = 0; idx < def_functions.ga_len; ++idx)
5646 {
5647 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
5648
5649 delete_def_function_contents(dfunc);
5650 }
5651
5652 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005653}
5654#endif
5655
5656
5657#endif // FEAT_EVAL