blob: 526f25b80dfb5ef0d6bcaa94b22805b87b186d71 [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
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200123 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124};
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 Moolenaar5deeb3f2020-04-05 17:08:17 +0200226/*
227 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
228 * be freed later.
229 */
230 static type_T *
231alloc_type(garray_T *type_gap)
232{
233 type_T *type;
234
235 if (ga_grow(type_gap, 1) == FAIL)
236 return NULL;
237 type = ALLOC_CLEAR_ONE(type_T);
238 if (type != NULL)
239 {
240 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
241 ++type_gap->ga_len;
242 }
243 return type;
244}
245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200247get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248{
249 type_T *type;
250
251 // recognize commonly used types
252 if (member_type->tt_type == VAR_UNKNOWN)
253 return &t_list_any;
Bram Moolenaar436472f2020-02-20 22:54:43 +0100254 if (member_type->tt_type == VAR_VOID)
255 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100256 if (member_type->tt_type == VAR_BOOL)
257 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 if (member_type->tt_type == VAR_NUMBER)
259 return &t_list_number;
260 if (member_type->tt_type == VAR_STRING)
261 return &t_list_string;
262
263 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200264 type = alloc_type(type_gap);
265 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100266 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 type->tt_type = VAR_LIST;
268 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200269 type->tt_argcount = 0;
270 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 return type;
272}
273
274 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200275get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276{
277 type_T *type;
278
279 // recognize commonly used types
280 if (member_type->tt_type == VAR_UNKNOWN)
281 return &t_dict_any;
Bram Moolenaar436472f2020-02-20 22:54:43 +0100282 if (member_type->tt_type == VAR_VOID)
283 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100284 if (member_type->tt_type == VAR_BOOL)
285 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100286 if (member_type->tt_type == VAR_NUMBER)
287 return &t_dict_number;
288 if (member_type->tt_type == VAR_STRING)
289 return &t_dict_string;
290
291 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200292 type = alloc_type(type_gap);
293 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100294 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 type->tt_type = VAR_DICT;
296 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200297 type->tt_argcount = 0;
298 type->tt_args = NULL;
299 return type;
300}
301
302/*
303 * Get a function type, based on the return type "ret_type".
304 * If "argcount" is -1 or 0 a predefined type can be used.
305 * If "argcount" > 0 always create a new type, so that arguments can be added.
306 */
307 static type_T *
308get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
309{
310 type_T *type;
311
312 // recognize commonly used types
313 if (argcount <= 0)
314 {
315 if (ret_type == &t_void)
316 {
317 if (argcount == 0)
318 return &t_func_0_void;
319 else
320 return &t_func_void;
321 }
322 if (ret_type == &t_any)
323 {
324 if (argcount == 0)
325 return &t_func_0_any;
326 else
327 return &t_func_any;
328 }
329 if (ret_type == &t_number)
330 {
331 if (argcount == 0)
332 return &t_func_0_number;
333 else
334 return &t_func_number;
335 }
336 if (ret_type == &t_string)
337 {
338 if (argcount == 0)
339 return &t_func_0_string;
340 else
341 return &t_func_string;
342 }
343 }
344
345 // Not a common type or has arguments, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200346 type = alloc_type(type_gap);
347 if (type == NULL)
Bram Moolenaard77a8522020-04-03 21:59:57 +0200348 return &t_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200349 type->tt_type = VAR_FUNC;
350 type->tt_member = ret_type;
351 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352 return type;
353}
354
Bram Moolenaara8c17702020-04-01 21:17:24 +0200355/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200356 * For a function type, reserve space for "argcount" argument types (including
357 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200358 */
359 static int
360func_type_add_arg_types(
361 type_T *functype,
362 int argcount,
363 int min_argcount,
364 garray_T *type_gap)
365{
366 if (ga_grow(type_gap, 1) == FAIL)
367 return FAIL;
368 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
369 if (functype->tt_args == NULL)
370 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200371 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
372 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200373 ++type_gap->ga_len;
374
375 functype->tt_argcount = argcount;
376 functype->tt_min_argcount = min_argcount;
377 return OK;
378}
379
380/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200381 * Return the type_T for a typval. Only for primitive types.
382 */
383 static type_T *
384typval2type(typval_T *tv)
385{
386 if (tv->v_type == VAR_NUMBER)
387 return &t_number;
388 if (tv->v_type == VAR_BOOL)
389 return &t_bool;
390 if (tv->v_type == VAR_STRING)
391 return &t_string;
392 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
393 return &t_list_string;
394 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
395 return &t_dict_any;
396 return &t_any;
397}
398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399/////////////////////////////////////////////////////////////////////
400// Following generate_ functions expect the caller to call ga_grow().
401
Bram Moolenaar080457c2020-03-03 21:53:32 +0100402#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
403#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/*
406 * Generate an instruction without arguments.
407 * Returns a pointer to the new instruction, NULL if failed.
408 */
409 static isn_T *
410generate_instr(cctx_T *cctx, isntype_T isn_type)
411{
412 garray_T *instr = &cctx->ctx_instr;
413 isn_T *isn;
414
Bram Moolenaar080457c2020-03-03 21:53:32 +0100415 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416 if (ga_grow(instr, 1) == FAIL)
417 return NULL;
418 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
419 isn->isn_type = isn_type;
420 isn->isn_lnum = cctx->ctx_lnum + 1;
421 ++instr->ga_len;
422
423 return isn;
424}
425
426/*
427 * Generate an instruction without arguments.
428 * "drop" will be removed from the stack.
429 * Returns a pointer to the new instruction, NULL if failed.
430 */
431 static isn_T *
432generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
433{
434 garray_T *stack = &cctx->ctx_type_stack;
435
Bram Moolenaar080457c2020-03-03 21:53:32 +0100436 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437 stack->ga_len -= drop;
438 return generate_instr(cctx, isn_type);
439}
440
441/*
442 * Generate instruction "isn_type" and put "type" on the type stack.
443 */
444 static isn_T *
445generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
446{
447 isn_T *isn;
448 garray_T *stack = &cctx->ctx_type_stack;
449
450 if ((isn = generate_instr(cctx, isn_type)) == NULL)
451 return NULL;
452
453 if (ga_grow(stack, 1) == FAIL)
454 return NULL;
455 ((type_T **)stack->ga_data)[stack->ga_len] = type;
456 ++stack->ga_len;
457
458 return isn;
459}
460
461/*
462 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
463 */
464 static int
465may_generate_2STRING(int offset, cctx_T *cctx)
466{
467 isn_T *isn;
468 garray_T *stack = &cctx->ctx_type_stack;
469 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
470
471 if ((*type)->tt_type == VAR_STRING)
472 return OK;
473 *type = &t_string;
474
475 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
476 return FAIL;
477 isn->isn_arg.number = offset;
478
479 return OK;
480}
481
482 static int
483check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
484{
485 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_UNKNOWN)
486 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
487 || type2 == VAR_UNKNOWN)))
488 {
489 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100490 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491 else
492 semsg(_("E1036: %c requires number or float arguments"), *op);
493 return FAIL;
494 }
495 return OK;
496}
497
498/*
499 * Generate an instruction with two arguments. The instruction depends on the
500 * type of the arguments.
501 */
502 static int
503generate_two_op(cctx_T *cctx, char_u *op)
504{
505 garray_T *stack = &cctx->ctx_type_stack;
506 type_T *type1;
507 type_T *type2;
508 vartype_T vartype;
509 isn_T *isn;
510
Bram Moolenaar080457c2020-03-03 21:53:32 +0100511 RETURN_OK_IF_SKIP(cctx);
512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513 // Get the known type of the two items on the stack. If they are matching
514 // use a type-specific instruction. Otherwise fall back to runtime type
515 // checking.
516 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
517 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
518 vartype = VAR_UNKNOWN;
519 if (type1->tt_type == type2->tt_type
520 && (type1->tt_type == VAR_NUMBER
521 || type1->tt_type == VAR_LIST
522#ifdef FEAT_FLOAT
523 || type1->tt_type == VAR_FLOAT
524#endif
525 || type1->tt_type == VAR_BLOB))
526 vartype = type1->tt_type;
527
528 switch (*op)
529 {
530 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar0062c2d2020-02-20 22:14:31 +0100531 && type1->tt_type != VAR_UNKNOWN
532 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 && check_number_or_float(
534 type1->tt_type, type2->tt_type, op) == FAIL)
535 return FAIL;
536 isn = generate_instr_drop(cctx,
537 vartype == VAR_NUMBER ? ISN_OPNR
538 : vartype == VAR_LIST ? ISN_ADDLIST
539 : vartype == VAR_BLOB ? ISN_ADDBLOB
540#ifdef FEAT_FLOAT
541 : vartype == VAR_FLOAT ? ISN_OPFLOAT
542#endif
543 : ISN_OPANY, 1);
544 if (isn != NULL)
545 isn->isn_arg.op.op_type = EXPR_ADD;
546 break;
547
548 case '-':
549 case '*':
550 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
551 op) == FAIL)
552 return FAIL;
553 if (vartype == VAR_NUMBER)
554 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
555#ifdef FEAT_FLOAT
556 else if (vartype == VAR_FLOAT)
557 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
558#endif
559 else
560 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
561 if (isn != NULL)
562 isn->isn_arg.op.op_type = *op == '*'
563 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
564 break;
565
566 case '%': if ((type1->tt_type != VAR_UNKNOWN
567 && type1->tt_type != VAR_NUMBER)
568 || (type2->tt_type != VAR_UNKNOWN
569 && type2->tt_type != VAR_NUMBER))
570 {
571 emsg(_("E1035: % requires number arguments"));
572 return FAIL;
573 }
574 isn = generate_instr_drop(cctx,
575 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
576 if (isn != NULL)
577 isn->isn_arg.op.op_type = EXPR_REM;
578 break;
579 }
580
581 // correct type of result
582 if (vartype == VAR_UNKNOWN)
583 {
584 type_T *type = &t_any;
585
586#ifdef FEAT_FLOAT
587 // float+number and number+float results in float
588 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
589 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
590 type = &t_float;
591#endif
592 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
593 }
594
595 return OK;
596}
597
598/*
599 * Generate an ISN_COMPARE* instruction with a boolean result.
600 */
601 static int
602generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
603{
604 isntype_T isntype = ISN_DROP;
605 isn_T *isn;
606 garray_T *stack = &cctx->ctx_type_stack;
607 vartype_T type1;
608 vartype_T type2;
609
Bram Moolenaar080457c2020-03-03 21:53:32 +0100610 RETURN_OK_IF_SKIP(cctx);
611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100612 // Get the known type of the two items on the stack. If they are matching
613 // use a type-specific instruction. Otherwise fall back to runtime type
614 // checking.
615 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
616 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
617 if (type1 == type2)
618 {
619 switch (type1)
620 {
621 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
622 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
623 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
624 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
625 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
626 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
627 case VAR_LIST: isntype = ISN_COMPARELIST; break;
628 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
629 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
630 case VAR_PARTIAL: isntype = ISN_COMPAREPARTIAL; break;
631 default: isntype = ISN_COMPAREANY; break;
632 }
633 }
634 else if (type1 == VAR_UNKNOWN || type2 == VAR_UNKNOWN
635 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
636 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
637 isntype = ISN_COMPAREANY;
638
639 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
640 && (isntype == ISN_COMPAREBOOL
641 || isntype == ISN_COMPARESPECIAL
642 || isntype == ISN_COMPARENR
643 || isntype == ISN_COMPAREFLOAT))
644 {
645 semsg(_("E1037: Cannot use \"%s\" with %s"),
646 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
647 return FAIL;
648 }
649 if (isntype == ISN_DROP
650 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
651 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
652 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
653 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
654 && exptype != EXPR_IS && exptype != EXPR_ISNOT
655 && (type1 == VAR_BLOB || type2 == VAR_BLOB
656 || type1 == VAR_LIST || type2 == VAR_LIST))))
657 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100658 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659 vartype_name(type1), vartype_name(type2));
660 return FAIL;
661 }
662
663 if ((isn = generate_instr(cctx, isntype)) == NULL)
664 return FAIL;
665 isn->isn_arg.op.op_type = exptype;
666 isn->isn_arg.op.op_ic = ic;
667
668 // takes two arguments, puts one bool back
669 if (stack->ga_len >= 2)
670 {
671 --stack->ga_len;
672 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
673 }
674
675 return OK;
676}
677
678/*
679 * Generate an ISN_2BOOL instruction.
680 */
681 static int
682generate_2BOOL(cctx_T *cctx, int invert)
683{
684 isn_T *isn;
685 garray_T *stack = &cctx->ctx_type_stack;
686
Bram Moolenaar080457c2020-03-03 21:53:32 +0100687 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
689 return FAIL;
690 isn->isn_arg.number = invert;
691
692 // type becomes bool
693 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
694
695 return OK;
696}
697
698 static int
699generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
700{
701 isn_T *isn;
702 garray_T *stack = &cctx->ctx_type_stack;
703
Bram Moolenaar080457c2020-03-03 21:53:32 +0100704 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
706 return FAIL;
707 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
708 isn->isn_arg.type.ct_off = offset;
709
710 // type becomes vartype
711 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
712
713 return OK;
714}
715
716/*
717 * Generate an ISN_PUSHNR instruction.
718 */
719 static int
720generate_PUSHNR(cctx_T *cctx, varnumber_T number)
721{
722 isn_T *isn;
723
Bram Moolenaar080457c2020-03-03 21:53:32 +0100724 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
726 return FAIL;
727 isn->isn_arg.number = number;
728
729 return OK;
730}
731
732/*
733 * Generate an ISN_PUSHBOOL instruction.
734 */
735 static int
736generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
737{
738 isn_T *isn;
739
Bram Moolenaar080457c2020-03-03 21:53:32 +0100740 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
742 return FAIL;
743 isn->isn_arg.number = number;
744
745 return OK;
746}
747
748/*
749 * Generate an ISN_PUSHSPEC instruction.
750 */
751 static int
752generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
753{
754 isn_T *isn;
755
Bram Moolenaar080457c2020-03-03 21:53:32 +0100756 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
758 return FAIL;
759 isn->isn_arg.number = number;
760
761 return OK;
762}
763
764#ifdef FEAT_FLOAT
765/*
766 * Generate an ISN_PUSHF instruction.
767 */
768 static int
769generate_PUSHF(cctx_T *cctx, float_T fnumber)
770{
771 isn_T *isn;
772
Bram Moolenaar080457c2020-03-03 21:53:32 +0100773 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
775 return FAIL;
776 isn->isn_arg.fnumber = fnumber;
777
778 return OK;
779}
780#endif
781
782/*
783 * Generate an ISN_PUSHS instruction.
784 * Consumes "str".
785 */
786 static int
787generate_PUSHS(cctx_T *cctx, char_u *str)
788{
789 isn_T *isn;
790
Bram Moolenaar080457c2020-03-03 21:53:32 +0100791 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
793 return FAIL;
794 isn->isn_arg.string = str;
795
796 return OK;
797}
798
799/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100800 * Generate an ISN_PUSHCHANNEL instruction.
801 * Consumes "channel".
802 */
803 static int
804generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
805{
806 isn_T *isn;
807
Bram Moolenaar080457c2020-03-03 21:53:32 +0100808 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100809 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
810 return FAIL;
811 isn->isn_arg.channel = channel;
812
813 return OK;
814}
815
816/*
817 * Generate an ISN_PUSHJOB instruction.
818 * Consumes "job".
819 */
820 static int
821generate_PUSHJOB(cctx_T *cctx, job_T *job)
822{
823 isn_T *isn;
824
Bram Moolenaar080457c2020-03-03 21:53:32 +0100825 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100826 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100827 return FAIL;
828 isn->isn_arg.job = job;
829
830 return OK;
831}
832
833/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834 * Generate an ISN_PUSHBLOB instruction.
835 * Consumes "blob".
836 */
837 static int
838generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
839{
840 isn_T *isn;
841
Bram Moolenaar080457c2020-03-03 21:53:32 +0100842 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
844 return FAIL;
845 isn->isn_arg.blob = blob;
846
847 return OK;
848}
849
850/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100851 * Generate an ISN_PUSHFUNC instruction with name "name".
852 * Consumes "name".
853 */
854 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200855generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100856{
857 isn_T *isn;
858
Bram Moolenaar080457c2020-03-03 21:53:32 +0100859 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200860 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100861 return FAIL;
862 isn->isn_arg.string = name;
863
864 return OK;
865}
866
867/*
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100868 * Generate an ISN_PUSHPARTIAL instruction with partial "part".
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200869 * Consumes "part".
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100870 */
871 static int
872generate_PUSHPARTIAL(cctx_T *cctx, partial_T *part)
873{
874 isn_T *isn;
875
Bram Moolenaar080457c2020-03-03 21:53:32 +0100876 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaard77a8522020-04-03 21:59:57 +0200877 if ((isn = generate_instr_type(cctx, ISN_PUSHPARTIAL, &t_func_any)) == NULL)
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100878 return FAIL;
879 isn->isn_arg.partial = part;
880
881 return OK;
882}
883
884/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 * Generate an ISN_STORE instruction.
886 */
887 static int
888generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
889{
890 isn_T *isn;
891
Bram Moolenaar080457c2020-03-03 21:53:32 +0100892 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
894 return FAIL;
895 if (name != NULL)
896 isn->isn_arg.string = vim_strsave(name);
897 else
898 isn->isn_arg.number = idx;
899
900 return OK;
901}
902
903/*
904 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
905 */
906 static int
907generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
908{
909 isn_T *isn;
910
Bram Moolenaar080457c2020-03-03 21:53:32 +0100911 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
913 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100914 isn->isn_arg.storenr.stnr_idx = idx;
915 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916
917 return OK;
918}
919
920/*
921 * Generate an ISN_STOREOPT instruction
922 */
923 static int
924generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
925{
926 isn_T *isn;
927
Bram Moolenaar080457c2020-03-03 21:53:32 +0100928 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
930 return FAIL;
931 isn->isn_arg.storeopt.so_name = vim_strsave(name);
932 isn->isn_arg.storeopt.so_flags = opt_flags;
933
934 return OK;
935}
936
937/*
938 * Generate an ISN_LOAD or similar instruction.
939 */
940 static int
941generate_LOAD(
942 cctx_T *cctx,
943 isntype_T isn_type,
944 int idx,
945 char_u *name,
946 type_T *type)
947{
948 isn_T *isn;
949
Bram Moolenaar080457c2020-03-03 21:53:32 +0100950 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
952 return FAIL;
953 if (name != NULL)
954 isn->isn_arg.string = vim_strsave(name);
955 else
956 isn->isn_arg.number = idx;
957
958 return OK;
959}
960
961/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100962 * Generate an ISN_LOADV instruction.
963 */
964 static int
965generate_LOADV(
966 cctx_T *cctx,
967 char_u *name,
968 int error)
969{
970 // load v:var
971 int vidx = find_vim_var(name);
972
Bram Moolenaar080457c2020-03-03 21:53:32 +0100973 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100974 if (vidx < 0)
975 {
976 if (error)
977 semsg(_(e_var_notfound), name);
978 return FAIL;
979 }
980
981 // TODO: get actual type
982 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
983}
984
985/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 * Generate an ISN_LOADS instruction.
987 */
988 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100989generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100991 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100993 int sid,
994 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995{
996 isn_T *isn;
997
Bram Moolenaar080457c2020-03-03 21:53:32 +0100998 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100999 if (isn_type == ISN_LOADS)
1000 isn = generate_instr_type(cctx, isn_type, type);
1001 else
1002 isn = generate_instr_drop(cctx, isn_type, 1);
1003 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001005 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1006 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007
1008 return OK;
1009}
1010
1011/*
1012 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1013 */
1014 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001015generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001016 cctx_T *cctx,
1017 isntype_T isn_type,
1018 int sid,
1019 int idx,
1020 type_T *type)
1021{
1022 isn_T *isn;
1023
Bram Moolenaar080457c2020-03-03 21:53:32 +01001024 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001025 if (isn_type == ISN_LOADSCRIPT)
1026 isn = generate_instr_type(cctx, isn_type, type);
1027 else
1028 isn = generate_instr_drop(cctx, isn_type, 1);
1029 if (isn == NULL)
1030 return FAIL;
1031 isn->isn_arg.script.script_sid = sid;
1032 isn->isn_arg.script.script_idx = idx;
1033 return OK;
1034}
1035
1036/*
1037 * Generate an ISN_NEWLIST instruction.
1038 */
1039 static int
1040generate_NEWLIST(cctx_T *cctx, int count)
1041{
1042 isn_T *isn;
1043 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 type_T *type;
1045 type_T *member;
1046
Bram Moolenaar080457c2020-03-03 21:53:32 +01001047 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1049 return FAIL;
1050 isn->isn_arg.number = count;
1051
1052 // drop the value types
1053 stack->ga_len -= count;
1054
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001055 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001056 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 if (count > 0)
1058 member = ((type_T **)stack->ga_data)[stack->ga_len];
1059 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001060 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001061 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062
1063 // add the list type to the type stack
1064 if (ga_grow(stack, 1) == FAIL)
1065 return FAIL;
1066 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1067 ++stack->ga_len;
1068
1069 return OK;
1070}
1071
1072/*
1073 * Generate an ISN_NEWDICT instruction.
1074 */
1075 static int
1076generate_NEWDICT(cctx_T *cctx, int count)
1077{
1078 isn_T *isn;
1079 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 type_T *type;
1081 type_T *member;
1082
Bram Moolenaar080457c2020-03-03 21:53:32 +01001083 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1085 return FAIL;
1086 isn->isn_arg.number = count;
1087
1088 // drop the key and value types
1089 stack->ga_len -= 2 * count;
1090
Bram Moolenaar436472f2020-02-20 22:54:43 +01001091 // Use the first value type for the list member type. Use "void" for an
1092 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 if (count > 0)
1094 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1095 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001096 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001097 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098
1099 // add the dict type to the type stack
1100 if (ga_grow(stack, 1) == FAIL)
1101 return FAIL;
1102 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1103 ++stack->ga_len;
1104
1105 return OK;
1106}
1107
1108/*
1109 * Generate an ISN_FUNCREF instruction.
1110 */
1111 static int
1112generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1113{
1114 isn_T *isn;
1115 garray_T *stack = &cctx->ctx_type_stack;
1116
Bram Moolenaar080457c2020-03-03 21:53:32 +01001117 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1119 return FAIL;
1120 isn->isn_arg.number = dfunc_idx;
1121
1122 if (ga_grow(stack, 1) == FAIL)
1123 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001124 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 // TODO: argument and return types
1126 ++stack->ga_len;
1127
1128 return OK;
1129}
1130
1131/*
1132 * Generate an ISN_JUMP instruction.
1133 */
1134 static int
1135generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1136{
1137 isn_T *isn;
1138 garray_T *stack = &cctx->ctx_type_stack;
1139
Bram Moolenaar080457c2020-03-03 21:53:32 +01001140 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1142 return FAIL;
1143 isn->isn_arg.jump.jump_when = when;
1144 isn->isn_arg.jump.jump_where = where;
1145
1146 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1147 --stack->ga_len;
1148
1149 return OK;
1150}
1151
1152 static int
1153generate_FOR(cctx_T *cctx, int loop_idx)
1154{
1155 isn_T *isn;
1156 garray_T *stack = &cctx->ctx_type_stack;
1157
Bram Moolenaar080457c2020-03-03 21:53:32 +01001158 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1160 return FAIL;
1161 isn->isn_arg.forloop.for_idx = loop_idx;
1162
1163 if (ga_grow(stack, 1) == FAIL)
1164 return FAIL;
1165 // type doesn't matter, will be stored next
1166 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1167 ++stack->ga_len;
1168
1169 return OK;
1170}
1171
1172/*
1173 * Generate an ISN_BCALL instruction.
1174 * Return FAIL if the number of arguments is wrong.
1175 */
1176 static int
1177generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1178{
1179 isn_T *isn;
1180 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001181 type_T *argtypes[MAX_FUNC_ARGS];
1182 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183
Bram Moolenaar080457c2020-03-03 21:53:32 +01001184 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 if (check_internal_func(func_idx, argcount) == FAIL)
1186 return FAIL;
1187
1188 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1189 return FAIL;
1190 isn->isn_arg.bfunc.cbf_idx = func_idx;
1191 isn->isn_arg.bfunc.cbf_argcount = argcount;
1192
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001193 for (i = 0; i < argcount; ++i)
1194 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 stack->ga_len -= argcount; // drop the arguments
1197 if (ga_grow(stack, 1) == FAIL)
1198 return FAIL;
1199 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001200 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 ++stack->ga_len; // add return value
1202
1203 return OK;
1204}
1205
1206/*
1207 * Generate an ISN_DCALL or ISN_UCALL instruction.
1208 * Return FAIL if the number of arguments is wrong.
1209 */
1210 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001211generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212{
1213 isn_T *isn;
1214 garray_T *stack = &cctx->ctx_type_stack;
1215 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001216 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217
Bram Moolenaar080457c2020-03-03 21:53:32 +01001218 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 if (argcount > regular_args && !has_varargs(ufunc))
1220 {
1221 semsg(_(e_toomanyarg), ufunc->uf_name);
1222 return FAIL;
1223 }
1224 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1225 {
1226 semsg(_(e_toofewarg), ufunc->uf_name);
1227 return FAIL;
1228 }
1229
1230 // Turn varargs into a list.
1231 if (ufunc->uf_va_name != NULL)
1232 {
1233 int count = argcount - regular_args;
1234
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001235 // If count is negative an empty list will be added after evaluating
1236 // default values for missing optional arguments.
1237 if (count >= 0)
1238 {
1239 generate_NEWLIST(cctx, count);
1240 argcount = regular_args + 1;
1241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 }
1243
1244 if ((isn = generate_instr(cctx,
1245 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1246 return FAIL;
1247 if (ufunc->uf_dfunc_idx >= 0)
1248 {
1249 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1250 isn->isn_arg.dfunc.cdf_argcount = argcount;
1251 }
1252 else
1253 {
1254 // A user function may be deleted and redefined later, can't use the
1255 // ufunc pointer, need to look it up again at runtime.
1256 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1257 isn->isn_arg.ufunc.cuf_argcount = argcount;
1258 }
1259
1260 stack->ga_len -= argcount; // drop the arguments
1261 if (ga_grow(stack, 1) == FAIL)
1262 return FAIL;
1263 // add return value
1264 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1265 ++stack->ga_len;
1266
1267 return OK;
1268}
1269
1270/*
1271 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1272 */
1273 static int
1274generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1275{
1276 isn_T *isn;
1277 garray_T *stack = &cctx->ctx_type_stack;
1278
Bram Moolenaar080457c2020-03-03 21:53:32 +01001279 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1281 return FAIL;
1282 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1283 isn->isn_arg.ufunc.cuf_argcount = argcount;
1284
1285 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001286 if (ga_grow(stack, 1) == FAIL)
1287 return FAIL;
1288 // add return value
1289 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1290 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291
1292 return OK;
1293}
1294
1295/*
1296 * Generate an ISN_PCALL instruction.
1297 */
1298 static int
1299generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1300{
1301 isn_T *isn;
1302 garray_T *stack = &cctx->ctx_type_stack;
1303
Bram Moolenaar080457c2020-03-03 21:53:32 +01001304 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1306 return FAIL;
1307 isn->isn_arg.pfunc.cpf_top = at_top;
1308 isn->isn_arg.pfunc.cpf_argcount = argcount;
1309
1310 stack->ga_len -= argcount; // drop the arguments
1311
1312 // drop the funcref/partial, get back the return value
1313 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1314
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001315 // If partial is above the arguments it must be cleared and replaced with
1316 // the return value.
1317 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1318 return FAIL;
1319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 return OK;
1321}
1322
1323/*
1324 * Generate an ISN_MEMBER instruction.
1325 */
1326 static int
1327generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1328{
1329 isn_T *isn;
1330 garray_T *stack = &cctx->ctx_type_stack;
1331 type_T *type;
1332
Bram Moolenaar080457c2020-03-03 21:53:32 +01001333 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1335 return FAIL;
1336 isn->isn_arg.string = vim_strnsave(name, (int)len);
1337
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001338 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001340 if (type->tt_type != VAR_DICT && type != &t_any)
1341 {
1342 emsg(_(e_dictreq));
1343 return FAIL;
1344 }
1345 // change dict type to dict member type
1346 if (type->tt_type == VAR_DICT)
1347 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348
1349 return OK;
1350}
1351
1352/*
1353 * Generate an ISN_ECHO instruction.
1354 */
1355 static int
1356generate_ECHO(cctx_T *cctx, int with_white, int count)
1357{
1358 isn_T *isn;
1359
Bram Moolenaar080457c2020-03-03 21:53:32 +01001360 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1362 return FAIL;
1363 isn->isn_arg.echo.echo_with_white = with_white;
1364 isn->isn_arg.echo.echo_count = count;
1365
1366 return OK;
1367}
1368
Bram Moolenaarad39c092020-02-26 18:23:43 +01001369/*
1370 * Generate an ISN_EXECUTE instruction.
1371 */
1372 static int
1373generate_EXECUTE(cctx_T *cctx, int count)
1374{
1375 isn_T *isn;
1376
1377 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1378 return FAIL;
1379 isn->isn_arg.number = count;
1380
1381 return OK;
1382}
1383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 static int
1385generate_EXEC(cctx_T *cctx, char_u *line)
1386{
1387 isn_T *isn;
1388
Bram Moolenaar080457c2020-03-03 21:53:32 +01001389 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1391 return FAIL;
1392 isn->isn_arg.string = vim_strsave(line);
1393 return OK;
1394}
1395
1396static char e_white_both[] =
1397 N_("E1004: white space required before and after '%s'");
Bram Moolenaard77a8522020-04-03 21:59:57 +02001398static char e_white_after[] = N_("E1069: white space required after '%s'");
1399static char e_no_white_before[] = N_("E1068: No white space allowed before '%s'");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400
1401/*
1402 * Reserve space for a local variable.
1403 * Return the index or -1 if it failed.
1404 */
1405 static int
1406reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1407{
1408 int idx;
1409 lvar_T *lvar;
1410
1411 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1412 {
1413 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1414 return -1;
1415 }
1416
1417 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1418 return -1;
1419 idx = cctx->ctx_locals.ga_len;
1420 if (cctx->ctx_max_local < idx + 1)
1421 cctx->ctx_max_local = idx + 1;
1422 ++cctx->ctx_locals.ga_len;
1423
1424 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1425 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1426 lvar->lv_const = isConst;
1427 lvar->lv_type = type;
1428
1429 return idx;
1430}
1431
1432/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001433 * Remove local variables above "new_top".
1434 */
1435 static void
1436unwind_locals(cctx_T *cctx, int new_top)
1437{
1438 if (cctx->ctx_locals.ga_len > new_top)
1439 {
1440 int idx;
1441 lvar_T *lvar;
1442
1443 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1444 {
1445 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1446 vim_free(lvar->lv_name);
1447 }
1448 }
1449 cctx->ctx_locals.ga_len = new_top;
1450}
1451
1452/*
1453 * Free all local variables.
1454 */
1455 static void
1456free_local(cctx_T *cctx)
1457{
1458 unwind_locals(cctx, 0);
1459 ga_clear(&cctx->ctx_locals);
1460}
1461
1462/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 * Skip over a type definition and return a pointer to just after it.
1464 */
1465 char_u *
1466skip_type(char_u *start)
1467{
1468 char_u *p = start;
1469
1470 while (ASCII_ISALNUM(*p) || *p == '_')
1471 ++p;
1472
1473 // Skip over "<type>"; this is permissive about white space.
1474 if (*skipwhite(p) == '<')
1475 {
1476 p = skipwhite(p);
1477 p = skip_type(skipwhite(p + 1));
1478 p = skipwhite(p);
1479 if (*p == '>')
1480 ++p;
1481 }
1482 return p;
1483}
1484
1485/*
1486 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001487 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 * Returns NULL in case of failure.
1489 */
1490 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001491parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492{
1493 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001494 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495
1496 if (**arg != '<')
1497 {
1498 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001499 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 else
1501 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001502 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 }
1504 *arg = skipwhite(*arg + 1);
1505
Bram Moolenaard77a8522020-04-03 21:59:57 +02001506 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507
1508 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001509 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 {
1511 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001512 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 }
1514 ++*arg;
1515
1516 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001517 return get_list_type(member_type, type_gap);
1518 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519}
1520
1521/*
1522 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001523 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001524 */
1525 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001526parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527{
1528 char_u *p = *arg;
1529 size_t len;
1530
1531 // skip over the first word
1532 while (ASCII_ISALNUM(*p) || *p == '_')
1533 ++p;
1534 len = p - *arg;
1535
1536 switch (**arg)
1537 {
1538 case 'a':
1539 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1540 {
1541 *arg += len;
1542 return &t_any;
1543 }
1544 break;
1545 case 'b':
1546 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1547 {
1548 *arg += len;
1549 return &t_bool;
1550 }
1551 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1552 {
1553 *arg += len;
1554 return &t_blob;
1555 }
1556 break;
1557 case 'c':
1558 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1559 {
1560 *arg += len;
1561 return &t_channel;
1562 }
1563 break;
1564 case 'd':
1565 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1566 {
1567 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001568 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 }
1570 break;
1571 case 'f':
1572 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1573 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001574#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 *arg += len;
1576 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001577#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001578 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001579 return &t_any;
1580#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 }
1582 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1583 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001584 type_T *type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001585 type_T *ret_type = &t_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001586 int argcount = -1;
1587 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001588 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001589 type_T *arg_type[MAX_FUNC_ARGS + 1];
1590
1591 // func({type}, ...): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001593 if (**arg == '(')
1594 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001595 // "func" may or may not return a value, "func()" does
1596 // not return a value.
1597 ret_type = &t_void;
1598
Bram Moolenaard77a8522020-04-03 21:59:57 +02001599 p = ++*arg;
1600 argcount = 0;
1601 while (*p != NUL && *p != ')')
1602 {
1603 if (STRNCMP(p, "...", 3) == 0)
1604 {
1605 flags |= TTFLAG_VARARGS;
1606 break;
1607 }
1608 arg_type[argcount++] = parse_type(&p, type_gap);
1609
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001610 if (*p == '?')
1611 {
1612 if (first_optional == -1)
1613 first_optional = argcount;
1614 ++p;
1615 }
1616 else if (first_optional != -1)
1617 {
1618 emsg(_("E1007: mandatory argument after optional argument"));
1619 return &t_any;
1620 }
1621
Bram Moolenaard77a8522020-04-03 21:59:57 +02001622 if (*p != ',' && *skipwhite(p) == ',')
1623 {
1624 semsg(_(e_no_white_before), ",");
1625 return &t_any;
1626 }
1627 if (*p == ',')
1628 {
1629 ++p;
1630 if (!VIM_ISWHITE(*p))
1631 semsg(_(e_white_after), ",");
1632 }
1633 p = skipwhite(p);
1634 if (argcount == MAX_FUNC_ARGS)
1635 {
1636 emsg(_("E740: Too many argument types"));
1637 return &t_any;
1638 }
1639 }
1640
1641 p = skipwhite(p);
1642 if (*p != ')')
1643 {
1644 emsg(_(e_missing_close));
1645 return &t_any;
1646 }
1647 *arg = p + 1;
1648 }
1649 if (**arg == ':')
1650 {
1651 // parse return type
1652 ++*arg;
1653 if (!VIM_ISWHITE(*p))
1654 semsg(_(e_white_after), ":");
1655 *arg = skipwhite(*arg);
1656 ret_type = parse_type(arg, type_gap);
1657 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001658 type = get_func_type(ret_type,
1659 flags == 0 && first_optional == -1 ? argcount : 99,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001660 type_gap);
1661 if (flags != 0)
1662 type->tt_flags = flags;
1663 if (argcount > 0)
1664 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001665 if (func_type_add_arg_types(type, argcount,
1666 first_optional == -1 ? argcount : first_optional,
1667 type_gap) == FAIL)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001668 return &t_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001669 mch_memmove(type->tt_args, arg_type,
1670 sizeof(type_T *) * argcount);
1671 }
1672 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673 }
1674 break;
1675 case 'j':
1676 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1677 {
1678 *arg += len;
1679 return &t_job;
1680 }
1681 break;
1682 case 'l':
1683 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1684 {
1685 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001686 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 }
1688 break;
1689 case 'n':
1690 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1691 {
1692 *arg += len;
1693 return &t_number;
1694 }
1695 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696 case 's':
1697 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1698 {
1699 *arg += len;
1700 return &t_string;
1701 }
1702 break;
1703 case 'v':
1704 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1705 {
1706 *arg += len;
1707 return &t_void;
1708 }
1709 break;
1710 }
1711
1712 semsg(_("E1010: Type not recognized: %s"), *arg);
1713 return &t_any;
1714}
1715
1716/*
1717 * Check if "type1" and "type2" are exactly the same.
1718 */
1719 static int
1720equal_type(type_T *type1, type_T *type2)
1721{
1722 if (type1->tt_type != type2->tt_type)
1723 return FALSE;
1724 switch (type1->tt_type)
1725 {
1726 case VAR_VOID:
1727 case VAR_UNKNOWN:
1728 case VAR_SPECIAL:
1729 case VAR_BOOL:
1730 case VAR_NUMBER:
1731 case VAR_FLOAT:
1732 case VAR_STRING:
1733 case VAR_BLOB:
1734 case VAR_JOB:
1735 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001736 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 case VAR_LIST:
1738 case VAR_DICT:
1739 return equal_type(type1->tt_member, type2->tt_member);
1740 case VAR_FUNC:
1741 case VAR_PARTIAL:
1742 // TODO; check argument types.
1743 return equal_type(type1->tt_member, type2->tt_member)
1744 && type1->tt_argcount == type2->tt_argcount;
1745 }
1746 return TRUE;
1747}
1748
1749/*
1750 * Find the common type of "type1" and "type2" and put it in "dest".
1751 * "type2" and "dest" may be the same.
1752 */
1753 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02001754common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755{
1756 if (equal_type(type1, type2))
1757 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001758 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759 return;
1760 }
1761
1762 if (type1->tt_type == type2->tt_type)
1763 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1765 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001766 type_T *common;
1767
Bram Moolenaard77a8522020-04-03 21:59:57 +02001768 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001769 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001770 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001771 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001772 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 return;
1774 }
1775 // TODO: VAR_FUNC and VAR_PARTIAL
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001776 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 }
1778
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001779 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780}
1781
1782 char *
1783vartype_name(vartype_T type)
1784{
1785 switch (type)
1786 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001787 case VAR_UNKNOWN: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 case VAR_SPECIAL: return "special";
1790 case VAR_BOOL: return "bool";
1791 case VAR_NUMBER: return "number";
1792 case VAR_FLOAT: return "float";
1793 case VAR_STRING: return "string";
1794 case VAR_BLOB: return "blob";
1795 case VAR_JOB: return "job";
1796 case VAR_CHANNEL: return "channel";
1797 case VAR_LIST: return "list";
1798 case VAR_DICT: return "dict";
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001799 case VAR_FUNC: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 case VAR_PARTIAL: return "partial";
1801 }
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001802 return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803}
1804
1805/*
1806 * Return the name of a type.
1807 * The result may be in allocated memory, in which case "tofree" is set.
1808 */
1809 char *
1810type_name(type_T *type, char **tofree)
1811{
1812 char *name = vartype_name(type->tt_type);
1813
1814 *tofree = NULL;
1815 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1816 {
1817 char *member_free;
1818 char *member_name = type_name(type->tt_member, &member_free);
1819 size_t len;
1820
1821 len = STRLEN(name) + STRLEN(member_name) + 3;
1822 *tofree = alloc(len);
1823 if (*tofree != NULL)
1824 {
1825 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1826 vim_free(member_free);
1827 return *tofree;
1828 }
1829 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001830 if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1831 {
1832 garray_T ga;
1833 int i;
1834
1835 ga_init2(&ga, 1, 100);
1836 if (ga_grow(&ga, 20) == FAIL)
1837 return "[unknown]";
1838 *tofree = ga.ga_data;
1839 STRCPY(ga.ga_data, "func(");
1840 ga.ga_len += 5;
1841
1842 for (i = 0; i < type->tt_argcount; ++i)
1843 {
1844 char *arg_free;
1845 char *arg_type = type_name(type->tt_args[i], &arg_free);
1846 int len;
1847
1848 if (i > 0)
1849 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001850 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001851 ga.ga_len += 2;
1852 }
1853 len = (int)STRLEN(arg_type);
1854 if (ga_grow(&ga, len + 6) == FAIL)
1855 {
1856 vim_free(arg_free);
1857 return "[unknown]";
1858 }
1859 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001860 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001861 ga.ga_len += len;
1862 vim_free(arg_free);
1863 }
1864
1865 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001866 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001867 else
1868 {
1869 char *ret_free;
1870 char *ret_name = type_name(type->tt_member, &ret_free);
1871 int len;
1872
1873 len = (int)STRLEN(ret_name) + 4;
1874 if (ga_grow(&ga, len) == FAIL)
1875 {
1876 vim_free(ret_free);
1877 return "[unknown]";
1878 }
1879 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001880 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
1881 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001882 vim_free(ret_free);
1883 }
1884 return ga.ga_data;
1885 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886
1887 return name;
1888}
1889
1890/*
1891 * Find "name" in script-local items of script "sid".
1892 * Returns the index in "sn_var_vals" if found.
1893 * If found but not in "sn_var_vals" returns -1.
1894 * If not found returns -2.
1895 */
1896 int
1897get_script_item_idx(int sid, char_u *name, int check_writable)
1898{
1899 hashtab_T *ht;
1900 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001901 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 int idx;
1903
1904 // First look the name up in the hashtable.
1905 if (sid <= 0 || sid > script_items.ga_len)
1906 return -1;
1907 ht = &SCRIPT_VARS(sid);
1908 di = find_var_in_ht(ht, 0, name, TRUE);
1909 if (di == NULL)
1910 return -2;
1911
1912 // Now find the svar_T index in sn_var_vals.
1913 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1914 {
1915 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1916
1917 if (sv->sv_tv == &di->di_tv)
1918 {
1919 if (check_writable && sv->sv_const)
1920 semsg(_(e_readonlyvar), name);
1921 return idx;
1922 }
1923 }
1924 return -1;
1925}
1926
1927/*
1928 * Find "name" in imported items of the current script/
1929 */
1930 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001931find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001933 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 int idx;
1935
1936 if (cctx != NULL)
1937 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1938 {
1939 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1940 + idx;
1941
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001942 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1943 : STRLEN(import->imp_name) == len
1944 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 return import;
1946 }
1947
1948 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1949 {
1950 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1951
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001952 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1953 : STRLEN(import->imp_name) == len
1954 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 return import;
1956 }
1957 return NULL;
1958}
1959
1960/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001961 * Free all imported variables.
1962 */
1963 static void
1964free_imported(cctx_T *cctx)
1965{
1966 int idx;
1967
1968 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1969 {
1970 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1971
1972 vim_free(import->imp_name);
1973 }
1974 ga_clear(&cctx->ctx_imports);
1975}
1976
1977/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001978 * Generate an instruction to load script-local variable "name", without the
1979 * leading "s:".
1980 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 */
1982 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001983compile_load_scriptvar(
1984 cctx_T *cctx,
1985 char_u *name, // variable NUL terminated
1986 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001987 char_u **end, // end of variable
1988 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001990 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1992 imported_T *import;
1993
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001994 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001996 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001997 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1998 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 }
2000 if (idx >= 0)
2001 {
2002 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2003
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002004 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 current_sctx.sc_sid, idx, sv->sv_type);
2006 return OK;
2007 }
2008
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002009 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 if (import != NULL)
2011 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002012 if (import->imp_all)
2013 {
2014 char_u *p = skipwhite(*end);
2015 int name_len;
2016 ufunc_T *ufunc;
2017 type_T *type;
2018
2019 // Used "import * as Name", need to lookup the member.
2020 if (*p != '.')
2021 {
2022 semsg(_("E1060: expected dot after name: %s"), start);
2023 return FAIL;
2024 }
2025 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002026 if (VIM_ISWHITE(*p))
2027 {
2028 emsg(_("E1074: no white space allowed after dot"));
2029 return FAIL;
2030 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002031
2032 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2033 // TODO: what if it is a function?
2034 if (idx < 0)
2035 return FAIL;
2036 *end = p;
2037
2038 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2039 import->imp_sid,
2040 idx,
2041 type);
2042 }
2043 else
2044 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002045 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002046 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2047 import->imp_sid,
2048 import->imp_var_vals_idx,
2049 import->imp_type);
2050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051 return OK;
2052 }
2053
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002054 if (error)
2055 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 return FAIL;
2057}
2058
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002059 static int
2060generate_funcref(cctx_T *cctx, char_u *name)
2061{
2062 ufunc_T *ufunc = find_func(name, cctx);
2063
2064 if (ufunc == NULL)
2065 return FAIL;
2066
2067 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2068}
2069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070/*
2071 * Compile a variable name into a load instruction.
2072 * "end" points to just after the name.
2073 * When "error" is FALSE do not give an error when not found.
2074 */
2075 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002076compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077{
2078 type_T *type;
2079 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002080 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002082 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083
2084 if (*(*arg + 1) == ':')
2085 {
2086 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002087 if (end <= *arg + 2)
2088 name = vim_strsave((char_u *)"[empty]");
2089 else
2090 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091 if (name == NULL)
2092 return FAIL;
2093
2094 if (**arg == 'v')
2095 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002096 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 }
2098 else if (**arg == 'g')
2099 {
2100 // Global variables can be defined later, thus we don't check if it
2101 // exists, give error at runtime.
2102 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2103 }
2104 else if (**arg == 's')
2105 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002106 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002108 else if (**arg == 'b')
2109 {
2110 semsg("Namespace b: not supported yet: %s", *arg);
2111 goto theend;
2112 }
2113 else if (**arg == 'w')
2114 {
2115 semsg("Namespace w: not supported yet: %s", *arg);
2116 goto theend;
2117 }
2118 else if (**arg == 't')
2119 {
2120 semsg("Namespace t: not supported yet: %s", *arg);
2121 goto theend;
2122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 else
2124 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002125 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 goto theend;
2127 }
2128 }
2129 else
2130 {
2131 size_t len = end - *arg;
2132 int idx;
2133 int gen_load = FALSE;
2134
2135 name = vim_strnsave(*arg, end - *arg);
2136 if (name == NULL)
2137 return FAIL;
2138
2139 idx = lookup_arg(*arg, len, cctx);
2140 if (idx >= 0)
2141 {
2142 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2143 type = cctx->ctx_ufunc->uf_arg_types[idx];
2144 else
2145 type = &t_any;
2146
2147 // Arguments are located above the frame pointer.
2148 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2149 if (cctx->ctx_ufunc->uf_va_name != NULL)
2150 --idx;
2151 gen_load = TRUE;
2152 }
2153 else if (lookup_vararg(*arg, len, cctx))
2154 {
2155 // varargs is always the last argument
2156 idx = -STACK_FRAME_SIZE - 1;
2157 type = cctx->ctx_ufunc->uf_va_type;
2158 gen_load = TRUE;
2159 }
2160 else
2161 {
2162 idx = lookup_local(*arg, len, cctx);
2163 if (idx >= 0)
2164 {
2165 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
2166 gen_load = TRUE;
2167 }
2168 else
2169 {
2170 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2171 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2172 res = generate_PUSHBOOL(cctx, **arg == 't'
2173 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002174 else
2175 {
2176 // "var" can be script-local even without using "s:" if it
2177 // already exists.
2178 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2179 == SCRIPT_VERSION_VIM9
2180 || lookup_script(*arg, len) == OK)
2181 res = compile_load_scriptvar(cctx, name, *arg, &end,
2182 FALSE);
2183
2184 // When the name starts with an uppercase letter or "x:" it
2185 // can be a user defined function.
2186 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2187 res = generate_funcref(cctx, name);
2188 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189 }
2190 }
2191 if (gen_load)
2192 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
2193 }
2194
2195 *arg = end;
2196
2197theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002198 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 semsg(_(e_var_notfound), name);
2200 vim_free(name);
2201 return res;
2202}
2203
2204/*
2205 * Compile the argument expressions.
2206 * "arg" points to just after the "(" and is advanced to after the ")"
2207 */
2208 static int
2209compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2210{
2211 char_u *p = *arg;
2212
2213 while (*p != NUL && *p != ')')
2214 {
2215 if (compile_expr1(&p, cctx) == FAIL)
2216 return FAIL;
2217 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002218
2219 if (*p != ',' && *skipwhite(p) == ',')
2220 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002221 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002222 p = skipwhite(p);
2223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002225 {
2226 ++p;
2227 if (!VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002228 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002229 }
2230 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002232 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 if (*p != ')')
2234 {
2235 emsg(_(e_missing_close));
2236 return FAIL;
2237 }
2238 *arg = p + 1;
2239 return OK;
2240}
2241
2242/*
2243 * Compile a function call: name(arg1, arg2)
2244 * "arg" points to "name", "arg + varlen" to the "(".
2245 * "argcount_init" is 1 for "value->method()"
2246 * Instructions:
2247 * EVAL arg1
2248 * EVAL arg2
2249 * BCALL / DCALL / UCALL
2250 */
2251 static int
2252compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2253{
2254 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002255 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 int argcount = argcount_init;
2257 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002258 char_u fname_buf[FLEN_FIXED + 1];
2259 char_u *tofree = NULL;
2260 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002262 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263
2264 if (varlen >= sizeof(namebuf))
2265 {
2266 semsg(_("E1011: name too long: %s"), name);
2267 return FAIL;
2268 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002269 vim_strncpy(namebuf, *arg, varlen);
2270 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271
2272 *arg = skipwhite(*arg + varlen + 1);
2273 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002274 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002276 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 {
2278 int idx;
2279
2280 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002281 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002283 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002284 else
2285 semsg(_(e_unknownfunc), namebuf);
2286 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 }
2288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002290 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002292 {
2293 res = generate_CALL(cctx, ufunc, argcount);
2294 goto theend;
2295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296
2297 // If the name is a variable, load it and use PCALL.
2298 p = namebuf;
2299 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002300 {
2301 res = generate_PCALL(cctx, argcount, FALSE);
2302 goto theend;
2303 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304
2305 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002306 res = generate_UCALL(cctx, name, argcount);
2307
2308theend:
2309 vim_free(tofree);
2310 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311}
2312
2313// like NAMESPACE_CHAR but with 'a' and 'l'.
2314#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2315
2316/*
2317 * Find the end of a variable or function name. Unlike find_name_end() this
2318 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002319 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 * Return a pointer to just after the name. Equal to "arg" if there is no
2321 * valid name.
2322 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002323 static char_u *
2324to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325{
2326 char_u *p;
2327
2328 // Quick check for valid starting character.
2329 if (!eval_isnamec1(*arg))
2330 return arg;
2331
2332 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2333 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2334 // and can be used in slice "[n:]".
2335 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002336 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2338 break;
2339 return p;
2340}
2341
2342/*
2343 * Like to_name_end() but also skip over a list or dict constant.
2344 */
2345 char_u *
2346to_name_const_end(char_u *arg)
2347{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002348 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349 typval_T rettv;
2350
2351 if (p == arg && *arg == '[')
2352 {
2353
2354 // Can be "[1, 2, 3]->Func()".
2355 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2356 p = arg;
2357 }
2358 else if (p == arg && *arg == '#' && arg[1] == '{')
2359 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002360 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 ++p;
2362 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2363 p = arg;
2364 }
2365 else if (p == arg && *arg == '{')
2366 {
2367 int ret = get_lambda_tv(&p, &rettv, FALSE);
2368
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002369 // Can be "{x -> ret}()".
2370 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 if (ret == NOTDONE)
2372 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2373 if (ret != OK)
2374 p = arg;
2375 }
2376
2377 return p;
2378}
2379
2380 static void
2381type_mismatch(type_T *expected, type_T *actual)
2382{
2383 char *tofree1, *tofree2;
2384
2385 semsg(_("E1013: type mismatch, expected %s but got %s"),
2386 type_name(expected, &tofree1), type_name(actual, &tofree2));
2387 vim_free(tofree1);
2388 vim_free(tofree2);
2389}
2390
2391/*
2392 * Check if the expected and actual types match.
2393 */
2394 static int
2395check_type(type_T *expected, type_T *actual, int give_msg)
2396{
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002397 int ret = OK;
2398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 if (expected->tt_type != VAR_UNKNOWN)
2400 {
2401 if (expected->tt_type != actual->tt_type)
2402 {
2403 if (give_msg)
2404 type_mismatch(expected, actual);
2405 return FAIL;
2406 }
2407 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2408 {
Bram Moolenaar436472f2020-02-20 22:54:43 +01002409 // void is used for an empty list or dict
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002410 if (actual->tt_member != &t_void)
Bram Moolenaar436472f2020-02-20 22:54:43 +01002411 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002413 else if (expected->tt_type == VAR_FUNC)
2414 {
2415 if (expected->tt_member != &t_any)
2416 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
2417 if (ret == OK && expected->tt_argcount != -1
2418 && (actual->tt_argcount < expected->tt_min_argcount
2419 || actual->tt_argcount > expected->tt_argcount))
2420 ret = FAIL;
2421 }
2422 if (ret == FAIL && give_msg)
2423 type_mismatch(expected, actual);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 }
2425 return OK;
2426}
2427
2428/*
2429 * Check that
2430 * - "actual" is "expected" type or
2431 * - "actual" is a type that can be "expected" type: add a runtime check; or
2432 * - return FAIL.
2433 */
2434 static int
2435need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2436{
Bram Moolenaar436472f2020-02-20 22:54:43 +01002437 if (check_type(expected, actual, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 return OK;
2439 if (actual->tt_type != VAR_UNKNOWN)
2440 {
2441 type_mismatch(expected, actual);
2442 return FAIL;
2443 }
2444 generate_TYPECHECK(cctx, expected, offset);
2445 return OK;
2446}
2447
2448/*
2449 * parse a list: [expr, expr]
2450 * "*arg" points to the '['.
2451 */
2452 static int
2453compile_list(char_u **arg, cctx_T *cctx)
2454{
2455 char_u *p = skipwhite(*arg + 1);
2456 int count = 0;
2457
2458 while (*p != ']')
2459 {
2460 if (*p == NUL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002461 {
2462 semsg(_(e_list_end), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 return FAIL;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002464 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 if (compile_expr1(&p, cctx) == FAIL)
2466 break;
2467 ++count;
2468 if (*p == ',')
2469 ++p;
2470 p = skipwhite(p);
2471 }
2472 *arg = p + 1;
2473
2474 generate_NEWLIST(cctx, count);
2475 return OK;
2476}
2477
2478/*
2479 * parse a lambda: {arg, arg -> expr}
2480 * "*arg" points to the '{'.
2481 */
2482 static int
2483compile_lambda(char_u **arg, cctx_T *cctx)
2484{
2485 garray_T *instr = &cctx->ctx_instr;
2486 typval_T rettv;
2487 ufunc_T *ufunc;
2488
2489 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002490 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002494 ++ufunc->uf_refcount;
2495 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002496 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497
2498 // The function will have one line: "return {expr}".
2499 // Compile it into instructions.
2500 compile_def_function(ufunc, TRUE);
2501
2502 if (ufunc->uf_dfunc_idx >= 0)
2503 {
2504 if (ga_grow(instr, 1) == FAIL)
2505 return FAIL;
2506 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2507 return OK;
2508 }
2509 return FAIL;
2510}
2511
2512/*
2513 * Compile a lamda call: expr->{lambda}(args)
2514 * "arg" points to the "{".
2515 */
2516 static int
2517compile_lambda_call(char_u **arg, cctx_T *cctx)
2518{
2519 ufunc_T *ufunc;
2520 typval_T rettv;
2521 int argcount = 1;
2522 int ret = FAIL;
2523
2524 // Get the funcref in "rettv".
2525 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2526 return FAIL;
2527
2528 if (**arg != '(')
2529 {
2530 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002531 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 else
2533 semsg(_(e_missing_paren), "lambda");
2534 clear_tv(&rettv);
2535 return FAIL;
2536 }
2537
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 ufunc = rettv.vval.v_partial->pt_func;
2539 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002540 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002541 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002542
2543 // The function will have one line: "return {expr}".
2544 // Compile it into instructions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 compile_def_function(ufunc, TRUE);
2546
2547 // compile the arguments
2548 *arg = skipwhite(*arg + 1);
2549 if (compile_arguments(arg, cctx, &argcount) == OK)
2550 // call the compiled function
2551 ret = generate_CALL(cctx, ufunc, argcount);
2552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 return ret;
2554}
2555
2556/*
2557 * parse a dict: {'key': val} or #{key: val}
2558 * "*arg" points to the '{'.
2559 */
2560 static int
2561compile_dict(char_u **arg, cctx_T *cctx, int literal)
2562{
2563 garray_T *instr = &cctx->ctx_instr;
2564 int count = 0;
2565 dict_T *d = dict_alloc();
2566 dictitem_T *item;
2567
2568 if (d == NULL)
2569 return FAIL;
2570 *arg = skipwhite(*arg + 1);
2571 while (**arg != '}' && **arg != NUL)
2572 {
2573 char_u *key = NULL;
2574
2575 if (literal)
2576 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002577 char_u *p = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578
2579 if (p == *arg)
2580 {
2581 semsg(_("E1014: Invalid key: %s"), *arg);
2582 return FAIL;
2583 }
2584 key = vim_strnsave(*arg, p - *arg);
2585 if (generate_PUSHS(cctx, key) == FAIL)
2586 return FAIL;
2587 *arg = p;
2588 }
2589 else
2590 {
2591 isn_T *isn;
2592
2593 if (compile_expr1(arg, cctx) == FAIL)
2594 return FAIL;
2595 // TODO: check type is string
2596 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2597 if (isn->isn_type == ISN_PUSHS)
2598 key = isn->isn_arg.string;
2599 }
2600
2601 // Check for duplicate keys, if using string keys.
2602 if (key != NULL)
2603 {
2604 item = dict_find(d, key, -1);
2605 if (item != NULL)
2606 {
2607 semsg(_(e_duplicate_key), key);
2608 goto failret;
2609 }
2610 item = dictitem_alloc(key);
2611 if (item != NULL)
2612 {
2613 item->di_tv.v_type = VAR_UNKNOWN;
2614 item->di_tv.v_lock = 0;
2615 if (dict_add(d, item) == FAIL)
2616 dictitem_free(item);
2617 }
2618 }
2619
2620 *arg = skipwhite(*arg);
2621 if (**arg != ':')
2622 {
2623 semsg(_(e_missing_dict_colon), *arg);
2624 return FAIL;
2625 }
2626
2627 *arg = skipwhite(*arg + 1);
2628 if (compile_expr1(arg, cctx) == FAIL)
2629 return FAIL;
2630 ++count;
2631
2632 if (**arg == '}')
2633 break;
2634 if (**arg != ',')
2635 {
2636 semsg(_(e_missing_dict_comma), *arg);
2637 goto failret;
2638 }
2639 *arg = skipwhite(*arg + 1);
2640 }
2641
2642 if (**arg != '}')
2643 {
2644 semsg(_(e_missing_dict_end), *arg);
2645 goto failret;
2646 }
2647 *arg = *arg + 1;
2648
2649 dict_unref(d);
2650 return generate_NEWDICT(cctx, count);
2651
2652failret:
2653 dict_unref(d);
2654 return FAIL;
2655}
2656
2657/*
2658 * Compile "&option".
2659 */
2660 static int
2661compile_get_option(char_u **arg, cctx_T *cctx)
2662{
2663 typval_T rettv;
2664 char_u *start = *arg;
2665 int ret;
2666
2667 // parse the option and get the current value to get the type.
2668 rettv.v_type = VAR_UNKNOWN;
2669 ret = get_option_tv(arg, &rettv, TRUE);
2670 if (ret == OK)
2671 {
2672 // include the '&' in the name, get_option_tv() expects it.
2673 char_u *name = vim_strnsave(start, *arg - start);
2674 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2675
2676 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2677 vim_free(name);
2678 }
2679 clear_tv(&rettv);
2680
2681 return ret;
2682}
2683
2684/*
2685 * Compile "$VAR".
2686 */
2687 static int
2688compile_get_env(char_u **arg, cctx_T *cctx)
2689{
2690 char_u *start = *arg;
2691 int len;
2692 int ret;
2693 char_u *name;
2694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 ++*arg;
2696 len = get_env_len(arg);
2697 if (len == 0)
2698 {
2699 semsg(_(e_syntax_at), start - 1);
2700 return FAIL;
2701 }
2702
2703 // include the '$' in the name, get_env_tv() expects it.
2704 name = vim_strnsave(start, len + 1);
2705 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2706 vim_free(name);
2707 return ret;
2708}
2709
2710/*
2711 * Compile "@r".
2712 */
2713 static int
2714compile_get_register(char_u **arg, cctx_T *cctx)
2715{
2716 int ret;
2717
2718 ++*arg;
2719 if (**arg == NUL)
2720 {
2721 semsg(_(e_syntax_at), *arg - 1);
2722 return FAIL;
2723 }
2724 if (!valid_yank_reg(**arg, TRUE))
2725 {
2726 emsg_invreg(**arg);
2727 return FAIL;
2728 }
2729 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2730 ++*arg;
2731 return ret;
2732}
2733
2734/*
2735 * Apply leading '!', '-' and '+' to constant "rettv".
2736 */
2737 static int
2738apply_leader(typval_T *rettv, char_u *start, char_u *end)
2739{
2740 char_u *p = end;
2741
2742 // this works from end to start
2743 while (p > start)
2744 {
2745 --p;
2746 if (*p == '-' || *p == '+')
2747 {
2748 // only '-' has an effect, for '+' we only check the type
2749#ifdef FEAT_FLOAT
2750 if (rettv->v_type == VAR_FLOAT)
2751 {
2752 if (*p == '-')
2753 rettv->vval.v_float = -rettv->vval.v_float;
2754 }
2755 else
2756#endif
2757 {
2758 varnumber_T val;
2759 int error = FALSE;
2760
2761 // tv_get_number_chk() accepts a string, but we don't want that
2762 // here
2763 if (check_not_string(rettv) == FAIL)
2764 return FAIL;
2765 val = tv_get_number_chk(rettv, &error);
2766 clear_tv(rettv);
2767 if (error)
2768 return FAIL;
2769 if (*p == '-')
2770 val = -val;
2771 rettv->v_type = VAR_NUMBER;
2772 rettv->vval.v_number = val;
2773 }
2774 }
2775 else
2776 {
2777 int v = tv2bool(rettv);
2778
2779 // '!' is permissive in the type.
2780 clear_tv(rettv);
2781 rettv->v_type = VAR_BOOL;
2782 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2783 }
2784 }
2785 return OK;
2786}
2787
2788/*
2789 * Recognize v: variables that are constants and set "rettv".
2790 */
2791 static void
2792get_vim_constant(char_u **arg, typval_T *rettv)
2793{
2794 if (STRNCMP(*arg, "v:true", 6) == 0)
2795 {
2796 rettv->v_type = VAR_BOOL;
2797 rettv->vval.v_number = VVAL_TRUE;
2798 *arg += 6;
2799 }
2800 else if (STRNCMP(*arg, "v:false", 7) == 0)
2801 {
2802 rettv->v_type = VAR_BOOL;
2803 rettv->vval.v_number = VVAL_FALSE;
2804 *arg += 7;
2805 }
2806 else if (STRNCMP(*arg, "v:null", 6) == 0)
2807 {
2808 rettv->v_type = VAR_SPECIAL;
2809 rettv->vval.v_number = VVAL_NULL;
2810 *arg += 6;
2811 }
2812 else if (STRNCMP(*arg, "v:none", 6) == 0)
2813 {
2814 rettv->v_type = VAR_SPECIAL;
2815 rettv->vval.v_number = VVAL_NONE;
2816 *arg += 6;
2817 }
2818}
2819
2820/*
2821 * Compile code to apply '-', '+' and '!'.
2822 */
2823 static int
2824compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2825{
2826 char_u *p = end;
2827
2828 // this works from end to start
2829 while (p > start)
2830 {
2831 --p;
2832 if (*p == '-' || *p == '+')
2833 {
2834 int negate = *p == '-';
2835 isn_T *isn;
2836
2837 // TODO: check type
2838 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2839 {
2840 --p;
2841 if (*p == '-')
2842 negate = !negate;
2843 }
2844 // only '-' has an effect, for '+' we only check the type
2845 if (negate)
2846 isn = generate_instr(cctx, ISN_NEGATENR);
2847 else
2848 isn = generate_instr(cctx, ISN_CHECKNR);
2849 if (isn == NULL)
2850 return FAIL;
2851 }
2852 else
2853 {
2854 int invert = TRUE;
2855
2856 while (p > start && p[-1] == '!')
2857 {
2858 --p;
2859 invert = !invert;
2860 }
2861 if (generate_2BOOL(cctx, invert) == FAIL)
2862 return FAIL;
2863 }
2864 }
2865 return OK;
2866}
2867
2868/*
2869 * Compile whatever comes after "name" or "name()".
2870 */
2871 static int
2872compile_subscript(
2873 char_u **arg,
2874 cctx_T *cctx,
2875 char_u **start_leader,
2876 char_u *end_leader)
2877{
2878 for (;;)
2879 {
2880 if (**arg == '(')
2881 {
2882 int argcount = 0;
2883
2884 // funcref(arg)
2885 *arg = skipwhite(*arg + 1);
2886 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2887 return FAIL;
2888 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2889 return FAIL;
2890 }
2891 else if (**arg == '-' && (*arg)[1] == '>')
2892 {
2893 char_u *p;
2894
2895 // something->method()
2896 // Apply the '!', '-' and '+' first:
2897 // -1.0->func() works like (-1.0)->func()
2898 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2899 return FAIL;
2900 *start_leader = end_leader; // don't apply again later
2901
2902 *arg = skipwhite(*arg + 2);
2903 if (**arg == '{')
2904 {
2905 // lambda call: list->{lambda}
2906 if (compile_lambda_call(arg, cctx) == FAIL)
2907 return FAIL;
2908 }
2909 else
2910 {
2911 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002912 p = *arg;
2913 if (ASCII_ISALPHA(*p) && p[1] == ':')
2914 p += 2;
2915 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 ;
2917 if (*p != '(')
2918 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002919 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 return FAIL;
2921 }
2922 // TODO: base value may not be the first argument
2923 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2924 return FAIL;
2925 }
2926 }
2927 else if (**arg == '[')
2928 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002929 garray_T *stack;
2930 type_T **typep;
2931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 // list index: list[123]
2933 // TODO: more arguments
2934 // TODO: dict member dict['name']
2935 *arg = skipwhite(*arg + 1);
2936 if (compile_expr1(arg, cctx) == FAIL)
2937 return FAIL;
2938
2939 if (**arg != ']')
2940 {
2941 emsg(_(e_missbrac));
2942 return FAIL;
2943 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002944 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945
2946 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2947 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01002948 stack = &cctx->ctx_type_stack;
2949 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2950 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
2951 {
2952 emsg(_(e_listreq));
2953 return FAIL;
2954 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002955 if ((*typep)->tt_type == VAR_LIST)
2956 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 }
2958 else if (**arg == '.' && (*arg)[1] != '.')
2959 {
2960 char_u *p;
2961
2962 ++*arg;
2963 p = *arg;
2964 // dictionary member: dict.name
2965 if (eval_isnamec1(*p))
2966 while (eval_isnamec(*p))
2967 MB_PTR_ADV(p);
2968 if (p == *arg)
2969 {
2970 semsg(_(e_syntax_at), *arg);
2971 return FAIL;
2972 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2974 return FAIL;
2975 *arg = p;
2976 }
2977 else
2978 break;
2979 }
2980
2981 // TODO - see handle_subscript():
2982 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2983 // Don't do this when "Func" is already a partial that was bound
2984 // explicitly (pt_auto is FALSE).
2985
2986 return OK;
2987}
2988
2989/*
2990 * Compile an expression at "*p" and add instructions to "instr".
2991 * "p" is advanced until after the expression, skipping white space.
2992 *
2993 * This is the equivalent of eval1(), eval2(), etc.
2994 */
2995
2996/*
2997 * number number constant
2998 * 0zFFFFFFFF Blob constant
2999 * "string" string constant
3000 * 'string' literal string constant
3001 * &option-name option value
3002 * @r register contents
3003 * identifier variable value
3004 * function() function call
3005 * $VAR environment variable
3006 * (expression) nested expression
3007 * [expr, expr] List
3008 * {key: val, key: val} Dictionary
3009 * #{key: val, key: val} Dictionary with literal keys
3010 *
3011 * Also handle:
3012 * ! in front logical NOT
3013 * - in front unary minus
3014 * + in front unary plus (ignored)
3015 * trailing (arg) funcref/partial call
3016 * trailing [] subscript in String or List
3017 * trailing .name entry in Dictionary
3018 * trailing ->name() method call
3019 */
3020 static int
3021compile_expr7(char_u **arg, cctx_T *cctx)
3022{
3023 typval_T rettv;
3024 char_u *start_leader, *end_leader;
3025 int ret = OK;
3026
3027 /*
3028 * Skip '!', '-' and '+' characters. They are handled later.
3029 */
3030 start_leader = *arg;
3031 while (**arg == '!' || **arg == '-' || **arg == '+')
3032 *arg = skipwhite(*arg + 1);
3033 end_leader = *arg;
3034
3035 rettv.v_type = VAR_UNKNOWN;
3036 switch (**arg)
3037 {
3038 /*
3039 * Number constant.
3040 */
3041 case '0': // also for blob starting with 0z
3042 case '1':
3043 case '2':
3044 case '3':
3045 case '4':
3046 case '5':
3047 case '6':
3048 case '7':
3049 case '8':
3050 case '9':
3051 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3052 return FAIL;
3053 break;
3054
3055 /*
3056 * String constant: "string".
3057 */
3058 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3059 return FAIL;
3060 break;
3061
3062 /*
3063 * Literal string constant: 'str''ing'.
3064 */
3065 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3066 return FAIL;
3067 break;
3068
3069 /*
3070 * Constant Vim variable.
3071 */
3072 case 'v': get_vim_constant(arg, &rettv);
3073 ret = NOTDONE;
3074 break;
3075
3076 /*
3077 * List: [expr, expr]
3078 */
3079 case '[': ret = compile_list(arg, cctx);
3080 break;
3081
3082 /*
3083 * Dictionary: #{key: val, key: val}
3084 */
3085 case '#': if ((*arg)[1] == '{')
3086 {
3087 ++*arg;
3088 ret = compile_dict(arg, cctx, TRUE);
3089 }
3090 else
3091 ret = NOTDONE;
3092 break;
3093
3094 /*
3095 * Lambda: {arg, arg -> expr}
3096 * Dictionary: {'key': val, 'key': val}
3097 */
3098 case '{': {
3099 char_u *start = skipwhite(*arg + 1);
3100
3101 // Find out what comes after the arguments.
3102 ret = get_function_args(&start, '-', NULL,
3103 NULL, NULL, NULL, TRUE);
3104 if (ret != FAIL && *start == '>')
3105 ret = compile_lambda(arg, cctx);
3106 else
3107 ret = compile_dict(arg, cctx, FALSE);
3108 }
3109 break;
3110
3111 /*
3112 * Option value: &name
3113 */
3114 case '&': ret = compile_get_option(arg, cctx);
3115 break;
3116
3117 /*
3118 * Environment variable: $VAR.
3119 */
3120 case '$': ret = compile_get_env(arg, cctx);
3121 break;
3122
3123 /*
3124 * Register contents: @r.
3125 */
3126 case '@': ret = compile_get_register(arg, cctx);
3127 break;
3128 /*
3129 * nested expression: (expression).
3130 */
3131 case '(': *arg = skipwhite(*arg + 1);
3132 ret = compile_expr1(arg, cctx); // recursive!
3133 *arg = skipwhite(*arg);
3134 if (**arg == ')')
3135 ++*arg;
3136 else if (ret == OK)
3137 {
3138 emsg(_(e_missing_close));
3139 ret = FAIL;
3140 }
3141 break;
3142
3143 default: ret = NOTDONE;
3144 break;
3145 }
3146 if (ret == FAIL)
3147 return FAIL;
3148
3149 if (rettv.v_type != VAR_UNKNOWN)
3150 {
3151 // apply the '!', '-' and '+' before the constant
3152 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3153 {
3154 clear_tv(&rettv);
3155 return FAIL;
3156 }
3157 start_leader = end_leader; // don't apply again below
3158
3159 // push constant
3160 switch (rettv.v_type)
3161 {
3162 case VAR_BOOL:
3163 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3164 break;
3165 case VAR_SPECIAL:
3166 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3167 break;
3168 case VAR_NUMBER:
3169 generate_PUSHNR(cctx, rettv.vval.v_number);
3170 break;
3171#ifdef FEAT_FLOAT
3172 case VAR_FLOAT:
3173 generate_PUSHF(cctx, rettv.vval.v_float);
3174 break;
3175#endif
3176 case VAR_BLOB:
3177 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3178 rettv.vval.v_blob = NULL;
3179 break;
3180 case VAR_STRING:
3181 generate_PUSHS(cctx, rettv.vval.v_string);
3182 rettv.vval.v_string = NULL;
3183 break;
3184 default:
3185 iemsg("constant type missing");
3186 return FAIL;
3187 }
3188 }
3189 else if (ret == NOTDONE)
3190 {
3191 char_u *p;
3192 int r;
3193
3194 if (!eval_isnamec1(**arg))
3195 {
3196 semsg(_("E1015: Name expected: %s"), *arg);
3197 return FAIL;
3198 }
3199
3200 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003201 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 if (*p == '(')
3203 r = compile_call(arg, p - *arg, cctx, 0);
3204 else
3205 r = compile_load(arg, p, cctx, TRUE);
3206 if (r == FAIL)
3207 return FAIL;
3208 }
3209
3210 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3211 return FAIL;
3212
3213 // Now deal with prefixed '-', '+' and '!', if not done already.
3214 return compile_leader(cctx, start_leader, end_leader);
3215}
3216
3217/*
3218 * * number multiplication
3219 * / number division
3220 * % number modulo
3221 */
3222 static int
3223compile_expr6(char_u **arg, cctx_T *cctx)
3224{
3225 char_u *op;
3226
3227 // get the first variable
3228 if (compile_expr7(arg, cctx) == FAIL)
3229 return FAIL;
3230
3231 /*
3232 * Repeat computing, until no "*", "/" or "%" is following.
3233 */
3234 for (;;)
3235 {
3236 op = skipwhite(*arg);
3237 if (*op != '*' && *op != '/' && *op != '%')
3238 break;
3239 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
3240 {
3241 char_u buf[3];
3242
3243 vim_strncpy(buf, op, 1);
3244 semsg(_(e_white_both), buf);
3245 }
3246 *arg = skipwhite(op + 1);
3247
3248 // get the second variable
3249 if (compile_expr7(arg, cctx) == FAIL)
3250 return FAIL;
3251
3252 generate_two_op(cctx, op);
3253 }
3254
3255 return OK;
3256}
3257
3258/*
3259 * + number addition
3260 * - number subtraction
3261 * .. string concatenation
3262 */
3263 static int
3264compile_expr5(char_u **arg, cctx_T *cctx)
3265{
3266 char_u *op;
3267 int oplen;
3268
3269 // get the first variable
3270 if (compile_expr6(arg, cctx) == FAIL)
3271 return FAIL;
3272
3273 /*
3274 * Repeat computing, until no "+", "-" or ".." is following.
3275 */
3276 for (;;)
3277 {
3278 op = skipwhite(*arg);
3279 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3280 break;
3281 oplen = (*op == '.' ? 2 : 1);
3282
3283 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
3284 {
3285 char_u buf[3];
3286
3287 vim_strncpy(buf, op, oplen);
3288 semsg(_(e_white_both), buf);
3289 }
3290
3291 *arg = skipwhite(op + oplen);
3292
3293 // get the second variable
3294 if (compile_expr6(arg, cctx) == FAIL)
3295 return FAIL;
3296
3297 if (*op == '.')
3298 {
3299 if (may_generate_2STRING(-2, cctx) == FAIL
3300 || may_generate_2STRING(-1, cctx) == FAIL)
3301 return FAIL;
3302 generate_instr_drop(cctx, ISN_CONCAT, 1);
3303 }
3304 else
3305 generate_two_op(cctx, op);
3306 }
3307
3308 return OK;
3309}
3310
Bram Moolenaar080457c2020-03-03 21:53:32 +01003311 static exptype_T
3312get_compare_type(char_u *p, int *len, int *type_is)
3313{
3314 exptype_T type = EXPR_UNKNOWN;
3315 int i;
3316
3317 switch (p[0])
3318 {
3319 case '=': if (p[1] == '=')
3320 type = EXPR_EQUAL;
3321 else if (p[1] == '~')
3322 type = EXPR_MATCH;
3323 break;
3324 case '!': if (p[1] == '=')
3325 type = EXPR_NEQUAL;
3326 else if (p[1] == '~')
3327 type = EXPR_NOMATCH;
3328 break;
3329 case '>': if (p[1] != '=')
3330 {
3331 type = EXPR_GREATER;
3332 *len = 1;
3333 }
3334 else
3335 type = EXPR_GEQUAL;
3336 break;
3337 case '<': if (p[1] != '=')
3338 {
3339 type = EXPR_SMALLER;
3340 *len = 1;
3341 }
3342 else
3343 type = EXPR_SEQUAL;
3344 break;
3345 case 'i': if (p[1] == 's')
3346 {
3347 // "is" and "isnot"; but not a prefix of a name
3348 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3349 *len = 5;
3350 i = p[*len];
3351 if (!isalnum(i) && i != '_')
3352 {
3353 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3354 *type_is = TRUE;
3355 }
3356 }
3357 break;
3358 }
3359 return type;
3360}
3361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362/*
3363 * expr5a == expr5b
3364 * expr5a =~ expr5b
3365 * expr5a != expr5b
3366 * expr5a !~ expr5b
3367 * expr5a > expr5b
3368 * expr5a >= expr5b
3369 * expr5a < expr5b
3370 * expr5a <= expr5b
3371 * expr5a is expr5b
3372 * expr5a isnot expr5b
3373 *
3374 * Produces instructions:
3375 * EVAL expr5a Push result of "expr5a"
3376 * EVAL expr5b Push result of "expr5b"
3377 * COMPARE one of the compare instructions
3378 */
3379 static int
3380compile_expr4(char_u **arg, cctx_T *cctx)
3381{
3382 exptype_T type = EXPR_UNKNOWN;
3383 char_u *p;
3384 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 int type_is = FALSE;
3386
3387 // get the first variable
3388 if (compile_expr5(arg, cctx) == FAIL)
3389 return FAIL;
3390
3391 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003392 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393
3394 /*
3395 * If there is a comparative operator, use it.
3396 */
3397 if (type != EXPR_UNKNOWN)
3398 {
3399 int ic = FALSE; // Default: do not ignore case
3400
3401 if (type_is && (p[len] == '?' || p[len] == '#'))
3402 {
3403 semsg(_(e_invexpr2), *arg);
3404 return FAIL;
3405 }
3406 // extra question mark appended: ignore case
3407 if (p[len] == '?')
3408 {
3409 ic = TRUE;
3410 ++len;
3411 }
3412 // extra '#' appended: match case (ignored)
3413 else if (p[len] == '#')
3414 ++len;
3415 // nothing appended: match case
3416
3417 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
3418 {
3419 char_u buf[7];
3420
3421 vim_strncpy(buf, p, len);
3422 semsg(_(e_white_both), buf);
3423 }
3424
3425 // get the second variable
3426 *arg = skipwhite(p + len);
3427 if (compile_expr5(arg, cctx) == FAIL)
3428 return FAIL;
3429
3430 generate_COMPARE(cctx, type, ic);
3431 }
3432
3433 return OK;
3434}
3435
3436/*
3437 * Compile || or &&.
3438 */
3439 static int
3440compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3441{
3442 char_u *p = skipwhite(*arg);
3443 int opchar = *op;
3444
3445 if (p[0] == opchar && p[1] == opchar)
3446 {
3447 garray_T *instr = &cctx->ctx_instr;
3448 garray_T end_ga;
3449
3450 /*
3451 * Repeat until there is no following "||" or "&&"
3452 */
3453 ga_init2(&end_ga, sizeof(int), 10);
3454 while (p[0] == opchar && p[1] == opchar)
3455 {
3456 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3457 semsg(_(e_white_both), op);
3458
3459 if (ga_grow(&end_ga, 1) == FAIL)
3460 {
3461 ga_clear(&end_ga);
3462 return FAIL;
3463 }
3464 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3465 ++end_ga.ga_len;
3466 generate_JUMP(cctx, opchar == '|'
3467 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3468
3469 // eval the next expression
3470 *arg = skipwhite(p + 2);
3471 if ((opchar == '|' ? compile_expr3(arg, cctx)
3472 : compile_expr4(arg, cctx)) == FAIL)
3473 {
3474 ga_clear(&end_ga);
3475 return FAIL;
3476 }
3477 p = skipwhite(*arg);
3478 }
3479
3480 // Fill in the end label in all jumps.
3481 while (end_ga.ga_len > 0)
3482 {
3483 isn_T *isn;
3484
3485 --end_ga.ga_len;
3486 isn = ((isn_T *)instr->ga_data)
3487 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3488 isn->isn_arg.jump.jump_where = instr->ga_len;
3489 }
3490 ga_clear(&end_ga);
3491 }
3492
3493 return OK;
3494}
3495
3496/*
3497 * expr4a && expr4a && expr4a logical AND
3498 *
3499 * Produces instructions:
3500 * EVAL expr4a Push result of "expr4a"
3501 * JUMP_AND_KEEP_IF_FALSE end
3502 * EVAL expr4b Push result of "expr4b"
3503 * JUMP_AND_KEEP_IF_FALSE end
3504 * EVAL expr4c Push result of "expr4c"
3505 * end:
3506 */
3507 static int
3508compile_expr3(char_u **arg, cctx_T *cctx)
3509{
3510 // get the first variable
3511 if (compile_expr4(arg, cctx) == FAIL)
3512 return FAIL;
3513
3514 // || and && work almost the same
3515 return compile_and_or(arg, cctx, "&&");
3516}
3517
3518/*
3519 * expr3a || expr3b || expr3c logical OR
3520 *
3521 * Produces instructions:
3522 * EVAL expr3a Push result of "expr3a"
3523 * JUMP_AND_KEEP_IF_TRUE end
3524 * EVAL expr3b Push result of "expr3b"
3525 * JUMP_AND_KEEP_IF_TRUE end
3526 * EVAL expr3c Push result of "expr3c"
3527 * end:
3528 */
3529 static int
3530compile_expr2(char_u **arg, cctx_T *cctx)
3531{
3532 // eval the first expression
3533 if (compile_expr3(arg, cctx) == FAIL)
3534 return FAIL;
3535
3536 // || and && work almost the same
3537 return compile_and_or(arg, cctx, "||");
3538}
3539
3540/*
3541 * Toplevel expression: expr2 ? expr1a : expr1b
3542 *
3543 * Produces instructions:
3544 * EVAL expr2 Push result of "expr"
3545 * JUMP_IF_FALSE alt jump if false
3546 * EVAL expr1a
3547 * JUMP_ALWAYS end
3548 * alt: EVAL expr1b
3549 * end:
3550 */
3551 static int
3552compile_expr1(char_u **arg, cctx_T *cctx)
3553{
3554 char_u *p;
3555
3556 // evaluate the first expression
3557 if (compile_expr2(arg, cctx) == FAIL)
3558 return FAIL;
3559
3560 p = skipwhite(*arg);
3561 if (*p == '?')
3562 {
3563 garray_T *instr = &cctx->ctx_instr;
3564 garray_T *stack = &cctx->ctx_type_stack;
3565 int alt_idx = instr->ga_len;
3566 int end_idx;
3567 isn_T *isn;
3568 type_T *type1;
3569 type_T *type2;
3570
3571 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3572 semsg(_(e_white_both), "?");
3573
3574 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3575
3576 // evaluate the second expression; any type is accepted
3577 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003578 if (compile_expr1(arg, cctx) == FAIL)
3579 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580
3581 // remember the type and drop it
3582 --stack->ga_len;
3583 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3584
3585 end_idx = instr->ga_len;
3586 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3587
3588 // jump here from JUMP_IF_FALSE
3589 isn = ((isn_T *)instr->ga_data) + alt_idx;
3590 isn->isn_arg.jump.jump_where = instr->ga_len;
3591
3592 // Check for the ":".
3593 p = skipwhite(*arg);
3594 if (*p != ':')
3595 {
3596 emsg(_(e_missing_colon));
3597 return FAIL;
3598 }
3599 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3600 semsg(_(e_white_both), ":");
3601
3602 // evaluate the third expression
3603 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003604 if (compile_expr1(arg, cctx) == FAIL)
3605 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606
3607 // If the types differ, the result has a more generic type.
3608 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003609 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610
3611 // jump here from JUMP_ALWAYS
3612 isn = ((isn_T *)instr->ga_data) + end_idx;
3613 isn->isn_arg.jump.jump_where = instr->ga_len;
3614 }
3615 return OK;
3616}
3617
3618/*
3619 * compile "return [expr]"
3620 */
3621 static char_u *
3622compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3623{
3624 char_u *p = arg;
3625 garray_T *stack = &cctx->ctx_type_stack;
3626 type_T *stack_type;
3627
3628 if (*p != NUL && *p != '|' && *p != '\n')
3629 {
3630 // compile return argument into instructions
3631 if (compile_expr1(&p, cctx) == FAIL)
3632 return NULL;
3633
3634 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3635 if (set_return_type)
3636 cctx->ctx_ufunc->uf_ret_type = stack_type;
3637 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3638 == FAIL)
3639 return NULL;
3640 }
3641 else
3642 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003643 // "set_return_type" cannot be TRUE, only used for a lambda which
3644 // always has an argument.
3645 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 {
3647 emsg(_("E1003: Missing return value"));
3648 return NULL;
3649 }
3650
3651 // No argument, return zero.
3652 generate_PUSHNR(cctx, 0);
3653 }
3654
3655 if (generate_instr(cctx, ISN_RETURN) == NULL)
3656 return NULL;
3657
3658 // "return val | endif" is possible
3659 return skipwhite(p);
3660}
3661
3662/*
3663 * Return the length of an assignment operator, or zero if there isn't one.
3664 */
3665 int
3666assignment_len(char_u *p, int *heredoc)
3667{
3668 if (*p == '=')
3669 {
3670 if (p[1] == '<' && p[2] == '<')
3671 {
3672 *heredoc = TRUE;
3673 return 3;
3674 }
3675 return 1;
3676 }
3677 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3678 return 2;
3679 if (STRNCMP(p, "..=", 3) == 0)
3680 return 3;
3681 return 0;
3682}
3683
3684// words that cannot be used as a variable
3685static char *reserved[] = {
3686 "true",
3687 "false",
3688 NULL
3689};
3690
3691/*
3692 * Get a line for "=<<".
3693 * Return a pointer to the line in allocated memory.
3694 * Return NULL for end-of-file or some error.
3695 */
3696 static char_u *
3697heredoc_getline(
3698 int c UNUSED,
3699 void *cookie,
3700 int indent UNUSED,
3701 int do_concat UNUSED)
3702{
3703 cctx_T *cctx = (cctx_T *)cookie;
3704
3705 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003706 {
3707 iemsg("Heredoc got to end");
3708 return NULL;
3709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 ++cctx->ctx_lnum;
3711 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3712 [cctx->ctx_lnum]);
3713}
3714
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003715typedef enum {
3716 dest_local,
3717 dest_option,
3718 dest_env,
3719 dest_global,
3720 dest_vimvar,
3721 dest_script,
3722 dest_reg,
3723} assign_dest_T;
3724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725/*
3726 * compile "let var [= expr]", "const var = expr" and "var = expr"
3727 * "arg" points to "var".
3728 */
3729 static char_u *
3730compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3731{
3732 char_u *p;
3733 char_u *ret = NULL;
3734 int var_count = 0;
3735 int semicolon = 0;
3736 size_t varlen;
3737 garray_T *instr = &cctx->ctx_instr;
3738 int idx = -1;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003739 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003742 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003744 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 int oplen = 0;
3746 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003747 type_T *type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 lvar_T *lvar;
3749 char_u *name;
3750 char_u *sp;
3751 int has_type = FALSE;
3752 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3753 int instr_count = -1;
3754
3755 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3756 if (p == NULL)
3757 return NULL;
3758 if (var_count > 0)
3759 {
3760 // TODO: let [var, var] = list
3761 emsg("Cannot handle a list yet");
3762 return NULL;
3763 }
3764
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003765 // "a: type" is declaring variable "a" with a type, not "a:".
3766 if (is_decl && p == arg + 2 && p[-1] == ':')
3767 --p;
3768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 varlen = p - arg;
3770 name = vim_strnsave(arg, (int)varlen);
3771 if (name == NULL)
3772 return NULL;
3773
Bram Moolenaar080457c2020-03-03 21:53:32 +01003774 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003776 if (*arg == '&')
3777 {
3778 int cc;
3779 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780
Bram Moolenaar080457c2020-03-03 21:53:32 +01003781 dest = dest_option;
3782 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003784 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003785 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 if (is_decl)
3788 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003789 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 goto theend;
3791 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003792 p = arg;
3793 p = find_option_end(&p, &opt_flags);
3794 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003796 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01003797 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003798 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003799 }
3800 cc = *p;
3801 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003802 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003803 *p = cc;
3804 if (opt_type == -3)
3805 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003806 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003807 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003808 }
3809 if (opt_type == -2 || opt_type == 0)
3810 type = &t_string;
3811 else
3812 type = &t_number; // both number and boolean option
3813 }
3814 else if (*arg == '$')
3815 {
3816 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003817 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003818 if (is_decl)
3819 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003820 semsg(_("E1065: Cannot declare an environment variable: %s"),
3821 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003822 goto theend;
3823 }
3824 }
3825 else if (*arg == '@')
3826 {
3827 if (!valid_yank_reg(arg[1], TRUE))
3828 {
3829 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003830 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003831 }
3832 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003833 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003834 if (is_decl)
3835 {
3836 semsg(_("E1066: Cannot declare a register: %s"), name);
3837 goto theend;
3838 }
3839 }
3840 else if (STRNCMP(arg, "g:", 2) == 0)
3841 {
3842 dest = dest_global;
3843 if (is_decl)
3844 {
3845 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3846 goto theend;
3847 }
3848 }
3849 else if (STRNCMP(arg, "v:", 2) == 0)
3850 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02003851 typval_T *vtv;
3852
Bram Moolenaar080457c2020-03-03 21:53:32 +01003853 vimvaridx = find_vim_var(name + 2);
3854 if (vimvaridx < 0)
3855 {
3856 semsg(_(e_var_notfound), arg);
3857 goto theend;
3858 }
3859 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003860 vtv = get_vim_var_tv(vimvaridx);
3861 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003862 if (is_decl)
3863 {
3864 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3865 goto theend;
3866 }
3867 }
3868 else
3869 {
3870 for (idx = 0; reserved[idx] != NULL; ++idx)
3871 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003873 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 goto theend;
3875 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003876
3877 idx = lookup_local(arg, varlen, cctx);
3878 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003880 if (is_decl)
3881 {
3882 semsg(_("E1017: Variable already declared: %s"), name);
3883 goto theend;
3884 }
3885 else
3886 {
3887 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3888 if (lvar->lv_const)
3889 {
3890 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3891 goto theend;
3892 }
3893 }
3894 }
3895 else if (STRNCMP(arg, "s:", 2) == 0
3896 || lookup_script(arg, varlen) == OK
3897 || find_imported(arg, varlen, cctx) != NULL)
3898 {
3899 dest = dest_script;
3900 if (is_decl)
3901 {
3902 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003903 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003904 goto theend;
3905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 }
3907 }
3908 }
3909
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003910 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 {
3912 if (is_decl && *p == ':')
3913 {
3914 // parse optional type: "let var: type = expr"
3915 p = skipwhite(p + 1);
3916 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 has_type = TRUE;
3918 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02003919 else if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 {
3921 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3922 type = lvar->lv_type;
3923 }
3924 }
3925
3926 sp = p;
3927 p = skipwhite(p);
3928 op = p;
3929 oplen = assignment_len(p, &heredoc);
3930 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3931 {
3932 char_u buf[4];
3933
3934 vim_strncpy(buf, op, oplen);
3935 semsg(_(e_white_both), buf);
3936 }
3937
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003938 if (oplen == 3 && !heredoc && dest != dest_global
3939 && type->tt_type != VAR_STRING && type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01003941 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 goto theend;
3943 }
3944
Bram Moolenaar080457c2020-03-03 21:53:32 +01003945 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 {
3947 if (oplen > 1 && !heredoc)
3948 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003949 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3951 name);
3952 goto theend;
3953 }
3954
3955 // new local variable
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003956 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
3957 && var_check_func_name(name, TRUE))
3958 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3960 if (idx < 0)
3961 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003962 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 }
3964
3965 if (heredoc)
3966 {
3967 list_T *l;
3968 listitem_T *li;
3969
3970 // [let] varname =<< [trim] {end}
3971 eap->getline = heredoc_getline;
3972 eap->cookie = cctx;
3973 l = heredoc_get(eap, op + 3);
3974
3975 // Push each line and the create the list.
3976 for (li = l->lv_first; li != NULL; li = li->li_next)
3977 {
3978 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3979 li->li_tv.vval.v_string = NULL;
3980 }
3981 generate_NEWLIST(cctx, l->lv_len);
3982 type = &t_list_string;
3983 list_free(l);
3984 p += STRLEN(p);
3985 }
3986 else if (oplen > 0)
3987 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02003988 int r;
3989 type_T *stacktype;
3990 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02003991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 // for "+=", "*=", "..=" etc. first load the current value
3993 if (*op != '=')
3994 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003995 switch (dest)
3996 {
3997 case dest_option:
3998 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02003999 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004000 break;
4001 case dest_global:
4002 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4003 break;
4004 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004005 compile_load_scriptvar(cctx,
4006 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004007 break;
4008 case dest_env:
4009 // Include $ in the name here
4010 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4011 break;
4012 case dest_reg:
4013 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4014 break;
4015 case dest_vimvar:
4016 generate_LOADV(cctx, name + 2, TRUE);
4017 break;
4018 case dest_local:
4019 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
4020 break;
4021 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 }
4023
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004024 // Compile the expression. Temporarily hide the new local variable
4025 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004026 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004027 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 instr_count = instr->ga_len;
4029 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004030 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004031 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004032 ++cctx->ctx_locals.ga_len;
4033 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 goto theend;
4035
Bram Moolenaara8c17702020-04-01 21:17:24 +02004036 stack = &cctx->ctx_type_stack;
Bram Moolenaarea94fbe2020-04-01 22:36:49 +02004037 stacktype = stack->ga_len == 0 ? &t_void
4038 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 if (idx >= 0 && (is_decl || !has_type))
4040 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004042 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 {
4044 if (stacktype->tt_type == VAR_VOID)
4045 {
4046 emsg(_("E1031: Cannot use void value"));
4047 goto theend;
4048 }
4049 else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004050 {
4051 // An empty list or dict has a &t_void member, for a
4052 // variable that implies &t_any.
4053 if (stacktype == &t_list_empty)
4054 lvar->lv_type = &t_list_any;
4055 else if (stacktype == &t_dict_empty)
4056 lvar->lv_type = &t_dict_any;
4057 else
4058 lvar->lv_type = stacktype;
4059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004061 else if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
4062 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02004064 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
4065 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 }
4067 else if (cmdidx == CMD_const)
4068 {
4069 emsg(_("E1021: const requires a value"));
4070 goto theend;
4071 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004072 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 {
4074 emsg(_("E1022: type or initialization required"));
4075 goto theend;
4076 }
4077 else
4078 {
4079 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 if (ga_grow(instr, 1) == FAIL)
4081 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004082 switch (type->tt_type)
4083 {
4084 case VAR_BOOL:
4085 generate_PUSHBOOL(cctx, VVAL_FALSE);
4086 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004087 case VAR_FLOAT:
4088#ifdef FEAT_FLOAT
4089 generate_PUSHF(cctx, 0.0);
4090#endif
4091 break;
4092 case VAR_STRING:
4093 generate_PUSHS(cctx, NULL);
4094 break;
4095 case VAR_BLOB:
4096 generate_PUSHBLOB(cctx, NULL);
4097 break;
4098 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004099 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004100 break;
4101 case VAR_PARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004102 generate_PUSHPARTIAL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004103 break;
4104 case VAR_LIST:
4105 generate_NEWLIST(cctx, 0);
4106 break;
4107 case VAR_DICT:
4108 generate_NEWDICT(cctx, 0);
4109 break;
4110 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004111 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004112 break;
4113 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004114 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004115 break;
4116 case VAR_NUMBER:
4117 case VAR_UNKNOWN:
4118 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004119 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004120 generate_PUSHNR(cctx, 0);
4121 break;
4122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 }
4124
4125 if (oplen > 0 && *op != '=')
4126 {
4127 type_T *expected = &t_number;
4128 garray_T *stack = &cctx->ctx_type_stack;
4129 type_T *stacktype;
4130
4131 // TODO: if type is known use float or any operation
4132
4133 if (*op == '.')
4134 expected = &t_string;
4135 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4136 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4137 goto theend;
4138
4139 if (*op == '.')
4140 generate_instr_drop(cctx, ISN_CONCAT, 1);
4141 else
4142 {
4143 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4144
4145 if (isn == NULL)
4146 goto theend;
4147 switch (*op)
4148 {
4149 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4150 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4151 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4152 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4153 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4154 }
4155 }
4156 }
4157
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004158 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004160 case dest_option:
4161 generate_STOREOPT(cctx, name + 1, opt_flags);
4162 break;
4163 case dest_global:
4164 // include g: with the name, easier to execute that way
4165 generate_STORE(cctx, ISN_STOREG, 0, name);
4166 break;
4167 case dest_env:
4168 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4169 break;
4170 case dest_reg:
4171 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4172 break;
4173 case dest_vimvar:
4174 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4175 break;
4176 case dest_script:
4177 {
4178 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4179 imported_T *import = NULL;
4180 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004181
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004182 if (name[1] != ':')
4183 {
4184 import = find_imported(name, 0, cctx);
4185 if (import != NULL)
4186 sid = import->imp_sid;
4187 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004189 idx = get_script_item_idx(sid, rawname, TRUE);
4190 // TODO: specific type
4191 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004192 {
4193 char_u *name_s = name;
4194
4195 // Include s: in the name for store_var()
4196 if (name[1] != ':')
4197 {
4198 int len = (int)STRLEN(name) + 3;
4199
4200 name_s = alloc(len);
4201 if (name_s == NULL)
4202 name_s = name;
4203 else
4204 vim_snprintf((char *)name_s, len, "s:%s", name);
4205 }
4206 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4207 if (name_s != name)
4208 vim_free(name_s);
4209 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004210 else
4211 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4212 sid, idx, &t_any);
4213 }
4214 break;
4215 case dest_local:
4216 {
4217 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004219 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4220 // into ISN_STORENR
4221 if (instr->ga_len == instr_count + 1
4222 && isn->isn_type == ISN_PUSHNR)
4223 {
4224 varnumber_T val = isn->isn_arg.number;
4225 garray_T *stack = &cctx->ctx_type_stack;
4226
4227 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004228 isn->isn_arg.storenr.stnr_idx = idx;
4229 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004230 if (stack->ga_len > 0)
4231 --stack->ga_len;
4232 }
4233 else
4234 generate_STORE(cctx, ISN_STORE, idx, NULL);
4235 }
4236 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 }
4238 ret = p;
4239
4240theend:
4241 vim_free(name);
4242 return ret;
4243}
4244
4245/*
4246 * Compile an :import command.
4247 */
4248 static char_u *
4249compile_import(char_u *arg, cctx_T *cctx)
4250{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004251 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252}
4253
4254/*
4255 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4256 */
4257 static int
4258compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4259{
4260 garray_T *instr = &cctx->ctx_instr;
4261 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4262
4263 if (endlabel == NULL)
4264 return FAIL;
4265 endlabel->el_next = *el;
4266 *el = endlabel;
4267 endlabel->el_end_label = instr->ga_len;
4268
4269 generate_JUMP(cctx, when, 0);
4270 return OK;
4271}
4272
4273 static void
4274compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4275{
4276 garray_T *instr = &cctx->ctx_instr;
4277
4278 while (*el != NULL)
4279 {
4280 endlabel_T *cur = (*el);
4281 isn_T *isn;
4282
4283 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4284 isn->isn_arg.jump.jump_where = instr->ga_len;
4285 *el = cur->el_next;
4286 vim_free(cur);
4287 }
4288}
4289
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004290 static void
4291compile_free_jump_to_end(endlabel_T **el)
4292{
4293 while (*el != NULL)
4294 {
4295 endlabel_T *cur = (*el);
4296
4297 *el = cur->el_next;
4298 vim_free(cur);
4299 }
4300}
4301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302/*
4303 * Create a new scope and set up the generic items.
4304 */
4305 static scope_T *
4306new_scope(cctx_T *cctx, scopetype_T type)
4307{
4308 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4309
4310 if (scope == NULL)
4311 return NULL;
4312 scope->se_outer = cctx->ctx_scope;
4313 cctx->ctx_scope = scope;
4314 scope->se_type = type;
4315 scope->se_local_count = cctx->ctx_locals.ga_len;
4316 return scope;
4317}
4318
4319/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004320 * Free the current scope and go back to the outer scope.
4321 */
4322 static void
4323drop_scope(cctx_T *cctx)
4324{
4325 scope_T *scope = cctx->ctx_scope;
4326
4327 if (scope == NULL)
4328 {
4329 iemsg("calling drop_scope() without a scope");
4330 return;
4331 }
4332 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004333 switch (scope->se_type)
4334 {
4335 case IF_SCOPE:
4336 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4337 case FOR_SCOPE:
4338 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4339 case WHILE_SCOPE:
4340 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4341 case TRY_SCOPE:
4342 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4343 case NO_SCOPE:
4344 case BLOCK_SCOPE:
4345 break;
4346 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004347 vim_free(scope);
4348}
4349
4350/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004351 * Evaluate an expression that is a constant:
4352 * has(arg)
4353 *
4354 * Also handle:
4355 * ! in front logical NOT
4356 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004357 * Return FAIL if the expression is not a constant.
4358 */
4359 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004360evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004361{
4362 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004363 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004364 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004365
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004366 /*
4367 * Skip '!' characters. They are handled later.
4368 */
4369 start_leader = *arg;
4370 while (**arg == '!')
4371 *arg = skipwhite(*arg + 1);
4372 end_leader = *arg;
4373
4374 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004375 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004376 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004377 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4378 {
4379 tv->v_type = VAR_SPECIAL;
4380 tv->vval.v_number = VVAL_TRUE;
4381 *arg += 4;
4382 return OK;
4383 }
4384 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4385 {
4386 tv->v_type = VAR_SPECIAL;
4387 tv->vval.v_number = VVAL_FALSE;
4388 *arg += 5;
4389 return OK;
4390 }
4391
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004392 if (STRNCMP("has(", *arg, 4) == 0)
4393 {
4394 has_call = TRUE;
4395 *arg = skipwhite(*arg + 4);
4396 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004397
4398 if (**arg == '"')
4399 {
4400 if (get_string_tv(arg, tv, TRUE) == FAIL)
4401 return FAIL;
4402 }
4403 else if (**arg == '\'')
4404 {
4405 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4406 return FAIL;
4407 }
4408 else
4409 return FAIL;
4410
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004411 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004412 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004413 *arg = skipwhite(*arg);
4414 if (**arg != ')')
4415 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004416 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004417
4418 argvars[0] = *tv;
4419 argvars[1].v_type = VAR_UNKNOWN;
4420 tv->v_type = VAR_NUMBER;
4421 tv->vval.v_number = 0;
4422 f_has(argvars, tv);
4423 clear_tv(&argvars[0]);
4424
4425 while (start_leader < end_leader)
4426 {
4427 if (*start_leader == '!')
4428 tv->vval.v_number = !tv->vval.v_number;
4429 ++start_leader;
4430 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004431 }
4432
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004433 return OK;
4434}
4435
Bram Moolenaar080457c2020-03-03 21:53:32 +01004436 static int
4437evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4438{
4439 exptype_T type = EXPR_UNKNOWN;
4440 char_u *p;
4441 int len = 2;
4442 int type_is = FALSE;
4443
4444 // get the first variable
4445 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4446 return FAIL;
4447
4448 p = skipwhite(*arg);
4449 type = get_compare_type(p, &len, &type_is);
4450
4451 /*
4452 * If there is a comparative operator, use it.
4453 */
4454 if (type != EXPR_UNKNOWN)
4455 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004456 typval_T tv2;
4457 char_u *s1, *s2;
4458 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4459 int n;
4460
4461 // TODO: Only string == string is supported now
4462 if (tv->v_type != VAR_STRING)
4463 return FAIL;
4464 if (type != EXPR_EQUAL)
4465 return FAIL;
4466
4467 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004468 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004469 *arg = skipwhite(p + len);
4470 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4471 || tv2.v_type != VAR_STRING)
4472 {
4473 clear_tv(&tv2);
4474 return FAIL;
4475 }
4476 s1 = tv_get_string_buf(tv, buf1);
4477 s2 = tv_get_string_buf(&tv2, buf2);
4478 n = STRCMP(s1, s2);
4479 clear_tv(tv);
4480 clear_tv(&tv2);
4481 tv->v_type = VAR_BOOL;
4482 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004483 }
4484
4485 return OK;
4486}
4487
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004488static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4489
4490/*
4491 * Compile constant || or &&.
4492 */
4493 static int
4494evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4495{
4496 char_u *p = skipwhite(*arg);
4497 int opchar = *op;
4498
4499 if (p[0] == opchar && p[1] == opchar)
4500 {
4501 int val = tv2bool(tv);
4502
4503 /*
4504 * Repeat until there is no following "||" or "&&"
4505 */
4506 while (p[0] == opchar && p[1] == opchar)
4507 {
4508 typval_T tv2;
4509
4510 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4511 return FAIL;
4512
4513 // eval the next expression
4514 *arg = skipwhite(p + 2);
4515 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004516 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004517 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004518 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004519 {
4520 clear_tv(&tv2);
4521 return FAIL;
4522 }
4523 if ((opchar == '&') == val)
4524 {
4525 // false || tv2 or true && tv2: use tv2
4526 clear_tv(tv);
4527 *tv = tv2;
4528 val = tv2bool(tv);
4529 }
4530 else
4531 clear_tv(&tv2);
4532 p = skipwhite(*arg);
4533 }
4534 }
4535
4536 return OK;
4537}
4538
4539/*
4540 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4541 * Return FAIL if the expression is not a constant.
4542 */
4543 static int
4544evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4545{
4546 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004547 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004548 return FAIL;
4549
4550 // || and && work almost the same
4551 return evaluate_const_and_or(arg, cctx, "&&", tv);
4552}
4553
4554/*
4555 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4556 * Return FAIL if the expression is not a constant.
4557 */
4558 static int
4559evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4560{
4561 // evaluate the first expression
4562 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4563 return FAIL;
4564
4565 // || and && work almost the same
4566 return evaluate_const_and_or(arg, cctx, "||", tv);
4567}
4568
4569/*
4570 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4571 * E.g. for "has('feature')".
4572 * This does not produce error messages. "tv" should be cleared afterwards.
4573 * Return FAIL if the expression is not a constant.
4574 */
4575 static int
4576evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4577{
4578 char_u *p;
4579
4580 // evaluate the first expression
4581 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4582 return FAIL;
4583
4584 p = skipwhite(*arg);
4585 if (*p == '?')
4586 {
4587 int val = tv2bool(tv);
4588 typval_T tv2;
4589
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004590 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004591 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4592 return FAIL;
4593
4594 // evaluate the second expression; any type is accepted
4595 clear_tv(tv);
4596 *arg = skipwhite(p + 1);
4597 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4598 return FAIL;
4599
4600 // Check for the ":".
4601 p = skipwhite(*arg);
4602 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4603 return FAIL;
4604
4605 // evaluate the third expression
4606 *arg = skipwhite(p + 1);
4607 tv2.v_type = VAR_UNKNOWN;
4608 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4609 {
4610 clear_tv(&tv2);
4611 return FAIL;
4612 }
4613 if (val)
4614 {
4615 // use the expr after "?"
4616 clear_tv(&tv2);
4617 }
4618 else
4619 {
4620 // use the expr after ":"
4621 clear_tv(tv);
4622 *tv = tv2;
4623 }
4624 }
4625 return OK;
4626}
4627
4628/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 * compile "if expr"
4630 *
4631 * "if expr" Produces instructions:
4632 * EVAL expr Push result of "expr"
4633 * JUMP_IF_FALSE end
4634 * ... body ...
4635 * end:
4636 *
4637 * "if expr | else" Produces instructions:
4638 * EVAL expr Push result of "expr"
4639 * JUMP_IF_FALSE else
4640 * ... body ...
4641 * JUMP_ALWAYS end
4642 * else:
4643 * ... body ...
4644 * end:
4645 *
4646 * "if expr1 | elseif expr2 | else" Produces instructions:
4647 * EVAL expr Push result of "expr"
4648 * JUMP_IF_FALSE elseif
4649 * ... body ...
4650 * JUMP_ALWAYS end
4651 * elseif:
4652 * EVAL expr Push result of "expr"
4653 * JUMP_IF_FALSE else
4654 * ... body ...
4655 * JUMP_ALWAYS end
4656 * else:
4657 * ... body ...
4658 * end:
4659 */
4660 static char_u *
4661compile_if(char_u *arg, cctx_T *cctx)
4662{
4663 char_u *p = arg;
4664 garray_T *instr = &cctx->ctx_instr;
4665 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004666 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004668 // compile "expr"; if we know it evaluates to FALSE skip the block
4669 tv.v_type = VAR_UNKNOWN;
4670 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4671 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4672 else
4673 cctx->ctx_skip = MAYBE;
4674 clear_tv(&tv);
4675 if (cctx->ctx_skip == MAYBE)
4676 {
4677 p = arg;
4678 if (compile_expr1(&p, cctx) == FAIL)
4679 return NULL;
4680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681
4682 scope = new_scope(cctx, IF_SCOPE);
4683 if (scope == NULL)
4684 return NULL;
4685
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004686 if (cctx->ctx_skip == MAYBE)
4687 {
4688 // "where" is set when ":elseif", "else" or ":endif" is found
4689 scope->se_u.se_if.is_if_label = instr->ga_len;
4690 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4691 }
4692 else
4693 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694
4695 return p;
4696}
4697
4698 static char_u *
4699compile_elseif(char_u *arg, cctx_T *cctx)
4700{
4701 char_u *p = arg;
4702 garray_T *instr = &cctx->ctx_instr;
4703 isn_T *isn;
4704 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004705 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706
4707 if (scope == NULL || scope->se_type != IF_SCOPE)
4708 {
4709 emsg(_(e_elseif_without_if));
4710 return NULL;
4711 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004712 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713
Bram Moolenaar158906c2020-02-06 20:39:45 +01004714 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004715 {
4716 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004718 return NULL;
4719 // previous "if" or "elseif" jumps here
4720 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4721 isn->isn_arg.jump.jump_where = instr->ga_len;
4722 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004724 // compile "expr"; if we know it evaluates to FALSE skip the block
4725 tv.v_type = VAR_UNKNOWN;
4726 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4727 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4728 else
4729 cctx->ctx_skip = MAYBE;
4730 clear_tv(&tv);
4731 if (cctx->ctx_skip == MAYBE)
4732 {
4733 p = arg;
4734 if (compile_expr1(&p, cctx) == FAIL)
4735 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004737 // "where" is set when ":elseif", "else" or ":endif" is found
4738 scope->se_u.se_if.is_if_label = instr->ga_len;
4739 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4740 }
4741 else
4742 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743
4744 return p;
4745}
4746
4747 static char_u *
4748compile_else(char_u *arg, cctx_T *cctx)
4749{
4750 char_u *p = arg;
4751 garray_T *instr = &cctx->ctx_instr;
4752 isn_T *isn;
4753 scope_T *scope = cctx->ctx_scope;
4754
4755 if (scope == NULL || scope->se_type != IF_SCOPE)
4756 {
4757 emsg(_(e_else_without_if));
4758 return NULL;
4759 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004760 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004762 // jump from previous block to the end, unless the else block is empty
4763 if (cctx->ctx_skip == MAYBE)
4764 {
4765 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004767 return NULL;
4768 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769
Bram Moolenaar158906c2020-02-06 20:39:45 +01004770 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004771 {
4772 if (scope->se_u.se_if.is_if_label >= 0)
4773 {
4774 // previous "if" or "elseif" jumps here
4775 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4776 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004777 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004778 }
4779 }
4780
4781 if (cctx->ctx_skip != MAYBE)
4782 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783
4784 return p;
4785}
4786
4787 static char_u *
4788compile_endif(char_u *arg, cctx_T *cctx)
4789{
4790 scope_T *scope = cctx->ctx_scope;
4791 ifscope_T *ifscope;
4792 garray_T *instr = &cctx->ctx_instr;
4793 isn_T *isn;
4794
4795 if (scope == NULL || scope->se_type != IF_SCOPE)
4796 {
4797 emsg(_(e_endif_without_if));
4798 return NULL;
4799 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004800 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004801 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004803 if (scope->se_u.se_if.is_if_label >= 0)
4804 {
4805 // previous "if" or "elseif" jumps here
4806 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4807 isn->isn_arg.jump.jump_where = instr->ga_len;
4808 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 // Fill in the "end" label in jumps at the end of the blocks.
4810 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004811 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004813 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 return arg;
4815}
4816
4817/*
4818 * compile "for var in expr"
4819 *
4820 * Produces instructions:
4821 * PUSHNR -1
4822 * STORE loop-idx Set index to -1
4823 * EVAL expr Push result of "expr"
4824 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4825 * - if beyond end, jump to "end"
4826 * - otherwise get item from list and push it
4827 * STORE var Store item in "var"
4828 * ... body ...
4829 * JUMP top Jump back to repeat
4830 * end: DROP Drop the result of "expr"
4831 *
4832 */
4833 static char_u *
4834compile_for(char_u *arg, cctx_T *cctx)
4835{
4836 char_u *p;
4837 size_t varlen;
4838 garray_T *instr = &cctx->ctx_instr;
4839 garray_T *stack = &cctx->ctx_type_stack;
4840 scope_T *scope;
4841 int loop_idx; // index of loop iteration variable
4842 int var_idx; // index of "var"
4843 type_T *vartype;
4844
4845 // TODO: list of variables: "for [key, value] in dict"
4846 // parse "var"
4847 for (p = arg; eval_isnamec1(*p); ++p)
4848 ;
4849 varlen = p - arg;
4850 var_idx = lookup_local(arg, varlen, cctx);
4851 if (var_idx >= 0)
4852 {
4853 semsg(_("E1023: variable already defined: %s"), arg);
4854 return NULL;
4855 }
4856
4857 // consume "in"
4858 p = skipwhite(p);
4859 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4860 {
4861 emsg(_(e_missing_in));
4862 return NULL;
4863 }
4864 p = skipwhite(p + 2);
4865
4866
4867 scope = new_scope(cctx, FOR_SCOPE);
4868 if (scope == NULL)
4869 return NULL;
4870
4871 // Reserve a variable to store the loop iteration counter.
4872 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4873 if (loop_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004874 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004875 // only happens when out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004876 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879
4880 // Reserve a variable to store "var"
4881 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4882 if (var_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004883 {
4884 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887
4888 generate_STORENR(cctx, loop_idx, -1);
4889
4890 // compile "expr", it remains on the stack until "endfor"
4891 arg = p;
4892 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004893 {
4894 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897
4898 // now we know the type of "var"
4899 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4900 if (vartype->tt_type != VAR_LIST)
4901 {
4902 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004903 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 return NULL;
4905 }
4906 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
4907 {
4908 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4909
4910 lvar->lv_type = vartype->tt_member;
4911 }
4912
4913 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004914 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915
4916 generate_FOR(cctx, loop_idx);
4917 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4918
4919 return arg;
4920}
4921
4922/*
4923 * compile "endfor"
4924 */
4925 static char_u *
4926compile_endfor(char_u *arg, cctx_T *cctx)
4927{
4928 garray_T *instr = &cctx->ctx_instr;
4929 scope_T *scope = cctx->ctx_scope;
4930 forscope_T *forscope;
4931 isn_T *isn;
4932
4933 if (scope == NULL || scope->se_type != FOR_SCOPE)
4934 {
4935 emsg(_(e_for));
4936 return NULL;
4937 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004938 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004940 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941
4942 // At end of ":for" scope jump back to the FOR instruction.
4943 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
4944
4945 // Fill in the "end" label in the FOR statement so it can jump here
4946 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
4947 isn->isn_arg.forloop.for_end = instr->ga_len;
4948
4949 // Fill in the "end" label any BREAK statements
4950 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
4951
4952 // Below the ":for" scope drop the "expr" list from the stack.
4953 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
4954 return NULL;
4955
4956 vim_free(scope);
4957
4958 return arg;
4959}
4960
4961/*
4962 * compile "while expr"
4963 *
4964 * Produces instructions:
4965 * top: EVAL expr Push result of "expr"
4966 * JUMP_IF_FALSE end jump if false
4967 * ... body ...
4968 * JUMP top Jump back to repeat
4969 * end:
4970 *
4971 */
4972 static char_u *
4973compile_while(char_u *arg, cctx_T *cctx)
4974{
4975 char_u *p = arg;
4976 garray_T *instr = &cctx->ctx_instr;
4977 scope_T *scope;
4978
4979 scope = new_scope(cctx, WHILE_SCOPE);
4980 if (scope == NULL)
4981 return NULL;
4982
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004983 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984
4985 // compile "expr"
4986 if (compile_expr1(&p, cctx) == FAIL)
4987 return NULL;
4988
4989 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004990 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 JUMP_IF_FALSE, cctx) == FAIL)
4992 return FAIL;
4993
4994 return p;
4995}
4996
4997/*
4998 * compile "endwhile"
4999 */
5000 static char_u *
5001compile_endwhile(char_u *arg, cctx_T *cctx)
5002{
5003 scope_T *scope = cctx->ctx_scope;
5004
5005 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5006 {
5007 emsg(_(e_while));
5008 return NULL;
5009 }
5010 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005011 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012
5013 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005014 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015
5016 // Fill in the "end" label in the WHILE statement so it can jump here.
5017 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005018 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019
5020 vim_free(scope);
5021
5022 return arg;
5023}
5024
5025/*
5026 * compile "continue"
5027 */
5028 static char_u *
5029compile_continue(char_u *arg, cctx_T *cctx)
5030{
5031 scope_T *scope = cctx->ctx_scope;
5032
5033 for (;;)
5034 {
5035 if (scope == NULL)
5036 {
5037 emsg(_(e_continue));
5038 return NULL;
5039 }
5040 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5041 break;
5042 scope = scope->se_outer;
5043 }
5044
5045 // Jump back to the FOR or WHILE instruction.
5046 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005047 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5048 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 return arg;
5050}
5051
5052/*
5053 * compile "break"
5054 */
5055 static char_u *
5056compile_break(char_u *arg, cctx_T *cctx)
5057{
5058 scope_T *scope = cctx->ctx_scope;
5059 endlabel_T **el;
5060
5061 for (;;)
5062 {
5063 if (scope == NULL)
5064 {
5065 emsg(_(e_break));
5066 return NULL;
5067 }
5068 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5069 break;
5070 scope = scope->se_outer;
5071 }
5072
5073 // Jump to the end of the FOR or WHILE loop.
5074 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005075 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005077 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5079 return FAIL;
5080
5081 return arg;
5082}
5083
5084/*
5085 * compile "{" start of block
5086 */
5087 static char_u *
5088compile_block(char_u *arg, cctx_T *cctx)
5089{
5090 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5091 return NULL;
5092 return skipwhite(arg + 1);
5093}
5094
5095/*
5096 * compile end of block: drop one scope
5097 */
5098 static void
5099compile_endblock(cctx_T *cctx)
5100{
5101 scope_T *scope = cctx->ctx_scope;
5102
5103 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005104 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 vim_free(scope);
5106}
5107
5108/*
5109 * compile "try"
5110 * Creates a new scope for the try-endtry, pointing to the first catch and
5111 * finally.
5112 * Creates another scope for the "try" block itself.
5113 * TRY instruction sets up exception handling at runtime.
5114 *
5115 * "try"
5116 * TRY -> catch1, -> finally push trystack entry
5117 * ... try block
5118 * "throw {exception}"
5119 * EVAL {exception}
5120 * THROW create exception
5121 * ... try block
5122 * " catch {expr}"
5123 * JUMP -> finally
5124 * catch1: PUSH exeception
5125 * EVAL {expr}
5126 * MATCH
5127 * JUMP nomatch -> catch2
5128 * CATCH remove exception
5129 * ... catch block
5130 * " catch"
5131 * JUMP -> finally
5132 * catch2: CATCH remove exception
5133 * ... catch block
5134 * " finally"
5135 * finally:
5136 * ... finally block
5137 * " endtry"
5138 * ENDTRY pop trystack entry, may rethrow
5139 */
5140 static char_u *
5141compile_try(char_u *arg, cctx_T *cctx)
5142{
5143 garray_T *instr = &cctx->ctx_instr;
5144 scope_T *try_scope;
5145 scope_T *scope;
5146
5147 // scope that holds the jumps that go to catch/finally/endtry
5148 try_scope = new_scope(cctx, TRY_SCOPE);
5149 if (try_scope == NULL)
5150 return NULL;
5151
5152 // "catch" is set when the first ":catch" is found.
5153 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005154 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155 if (generate_instr(cctx, ISN_TRY) == NULL)
5156 return NULL;
5157
5158 // scope for the try block itself
5159 scope = new_scope(cctx, BLOCK_SCOPE);
5160 if (scope == NULL)
5161 return NULL;
5162
5163 return arg;
5164}
5165
5166/*
5167 * compile "catch {expr}"
5168 */
5169 static char_u *
5170compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5171{
5172 scope_T *scope = cctx->ctx_scope;
5173 garray_T *instr = &cctx->ctx_instr;
5174 char_u *p;
5175 isn_T *isn;
5176
5177 // end block scope from :try or :catch
5178 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5179 compile_endblock(cctx);
5180 scope = cctx->ctx_scope;
5181
5182 // Error if not in a :try scope
5183 if (scope == NULL || scope->se_type != TRY_SCOPE)
5184 {
5185 emsg(_(e_catch));
5186 return NULL;
5187 }
5188
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005189 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 {
5191 emsg(_("E1033: catch unreachable after catch-all"));
5192 return NULL;
5193 }
5194
5195 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005196 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 JUMP_ALWAYS, cctx) == FAIL)
5198 return NULL;
5199
5200 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005201 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 if (isn->isn_arg.try.try_catch == 0)
5203 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005204 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 {
5206 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005207 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 isn->isn_arg.jump.jump_where = instr->ga_len;
5209 }
5210
5211 p = skipwhite(arg);
5212 if (ends_excmd(*p))
5213 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005214 scope->se_u.se_try.ts_caught_all = TRUE;
5215 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216 }
5217 else
5218 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005219 char_u *end;
5220 char_u *pat;
5221 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005222 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005223 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 // Push v:exception, push {expr} and MATCH
5226 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5227
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005228 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005229 if (*end != *p)
5230 {
5231 semsg(_("E1067: Separator mismatch: %s"), p);
5232 vim_free(tofree);
5233 return FAIL;
5234 }
5235 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005236 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005237 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005238 len = (int)(end - tofree);
5239 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005240 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005241 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005242 if (pat == NULL)
5243 return FAIL;
5244 if (generate_PUSHS(cctx, pat) == FAIL)
5245 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5248 return NULL;
5249
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005250 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5252 return NULL;
5253 }
5254
5255 if (generate_instr(cctx, ISN_CATCH) == NULL)
5256 return NULL;
5257
5258 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5259 return NULL;
5260 return p;
5261}
5262
5263 static char_u *
5264compile_finally(char_u *arg, cctx_T *cctx)
5265{
5266 scope_T *scope = cctx->ctx_scope;
5267 garray_T *instr = &cctx->ctx_instr;
5268 isn_T *isn;
5269
5270 // end block scope from :try or :catch
5271 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5272 compile_endblock(cctx);
5273 scope = cctx->ctx_scope;
5274
5275 // Error if not in a :try scope
5276 if (scope == NULL || scope->se_type != TRY_SCOPE)
5277 {
5278 emsg(_(e_finally));
5279 return NULL;
5280 }
5281
5282 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005283 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284 if (isn->isn_arg.try.try_finally != 0)
5285 {
5286 emsg(_(e_finally_dup));
5287 return NULL;
5288 }
5289
5290 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005291 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292
Bram Moolenaar585fea72020-04-02 22:33:21 +02005293 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005294 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295 {
5296 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005297 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005298 isn->isn_arg.jump.jump_where = instr->ga_len;
5299 }
5300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301 // TODO: set index in ts_finally_label jumps
5302
5303 return arg;
5304}
5305
5306 static char_u *
5307compile_endtry(char_u *arg, cctx_T *cctx)
5308{
5309 scope_T *scope = cctx->ctx_scope;
5310 garray_T *instr = &cctx->ctx_instr;
5311 isn_T *isn;
5312
5313 // end block scope from :catch or :finally
5314 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5315 compile_endblock(cctx);
5316 scope = cctx->ctx_scope;
5317
5318 // Error if not in a :try scope
5319 if (scope == NULL || scope->se_type != TRY_SCOPE)
5320 {
5321 if (scope == NULL)
5322 emsg(_(e_no_endtry));
5323 else if (scope->se_type == WHILE_SCOPE)
5324 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005325 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005326 emsg(_(e_endfor));
5327 else
5328 emsg(_(e_endif));
5329 return NULL;
5330 }
5331
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005332 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5334 {
5335 emsg(_("E1032: missing :catch or :finally"));
5336 return NULL;
5337 }
5338
5339 // Fill in the "end" label in jumps at the end of the blocks, if not done
5340 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005341 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342
5343 // End :catch or :finally scope: set value in ISN_TRY instruction
5344 if (isn->isn_arg.try.try_finally == 0)
5345 isn->isn_arg.try.try_finally = instr->ga_len;
5346 compile_endblock(cctx);
5347
5348 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5349 return NULL;
5350 return arg;
5351}
5352
5353/*
5354 * compile "throw {expr}"
5355 */
5356 static char_u *
5357compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5358{
5359 char_u *p = skipwhite(arg);
5360
5361 if (ends_excmd(*p))
5362 {
5363 emsg(_(e_argreq));
5364 return NULL;
5365 }
5366 if (compile_expr1(&p, cctx) == FAIL)
5367 return NULL;
5368 if (may_generate_2STRING(-1, cctx) == FAIL)
5369 return NULL;
5370 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5371 return NULL;
5372
5373 return p;
5374}
5375
5376/*
5377 * compile "echo expr"
5378 */
5379 static char_u *
5380compile_echo(char_u *arg, int with_white, cctx_T *cctx)
5381{
5382 char_u *p = arg;
5383 int count = 0;
5384
Bram Moolenaarad39c092020-02-26 18:23:43 +01005385 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386 {
5387 if (compile_expr1(&p, cctx) == FAIL)
5388 return NULL;
5389 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005390 p = skipwhite(p);
5391 if (ends_excmd(*p))
5392 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393 }
5394
5395 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01005396 return p;
5397}
5398
5399/*
5400 * compile "execute expr"
5401 */
5402 static char_u *
5403compile_execute(char_u *arg, cctx_T *cctx)
5404{
5405 char_u *p = arg;
5406 int count = 0;
5407
5408 for (;;)
5409 {
5410 if (compile_expr1(&p, cctx) == FAIL)
5411 return NULL;
5412 ++count;
5413 p = skipwhite(p);
5414 if (ends_excmd(*p))
5415 break;
5416 }
5417
5418 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005419
5420 return p;
5421}
5422
5423/*
5424 * After ex_function() has collected all the function lines: parse and compile
5425 * the lines into instructions.
5426 * Adds the function to "def_functions".
5427 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5428 * return statement (used for lambda).
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005429 * This can be used recursively through compile_lambda(), which may reallocate
5430 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 */
5432 void
5433compile_def_function(ufunc_T *ufunc, int set_return_type)
5434{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005435 char_u *line = NULL;
5436 char_u *p;
5437 exarg_T ea;
5438 char *errormsg = NULL; // error message
5439 int had_return = FALSE;
5440 cctx_T cctx;
5441 garray_T *instr;
5442 int called_emsg_before = called_emsg;
5443 int ret = FAIL;
5444 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005445 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005448 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005449
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005450 if (ufunc->uf_dfunc_idx >= 0)
5451 {
5452 // Redefining a function that was compiled before.
5453 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5454
5455 // Free old instructions.
5456 delete_def_function_contents(dfunc);
5457 }
5458 else
5459 {
5460 // Add the function to "def_functions".
5461 if (ga_grow(&def_functions, 1) == FAIL)
5462 return;
5463 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
5464 vim_memset(dfunc, 0, sizeof(dfunc_T));
5465 dfunc->df_idx = def_functions.ga_len;
5466 ufunc->uf_dfunc_idx = dfunc->df_idx;
5467 dfunc->df_ufunc = ufunc;
5468 ++def_functions.ga_len;
5469 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470 }
5471
5472 vim_memset(&cctx, 0, sizeof(cctx));
5473 cctx.ctx_ufunc = ufunc;
5474 cctx.ctx_lnum = -1;
5475 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5476 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5477 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5478 cctx.ctx_type_list = &ufunc->uf_type_list;
5479 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5480 instr = &cctx.ctx_instr;
5481
5482 // Most modern script version.
5483 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5484
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005485 if (ufunc->uf_def_args.ga_len > 0)
5486 {
5487 int count = ufunc->uf_def_args.ga_len;
5488 int i;
5489 char_u *arg;
5490 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5491
5492 // Produce instructions for the default values of optional arguments.
5493 // Store the instruction index in uf_def_arg_idx[] so that we know
5494 // where to start when the function is called, depending on the number
5495 // of arguments.
5496 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5497 if (ufunc->uf_def_arg_idx == NULL)
5498 goto erret;
5499 for (i = 0; i < count; ++i)
5500 {
5501 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5502 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
5503 if (compile_expr1(&arg, &cctx) == FAIL
5504 || generate_STORE(&cctx, ISN_STORE,
5505 i - count - off, NULL) == FAIL)
5506 goto erret;
5507 }
5508
5509 // If a varargs is following, push an empty list.
5510 if (ufunc->uf_va_name != NULL)
5511 {
5512 if (generate_NEWLIST(&cctx, 0) == FAIL
5513 || generate_STORE(&cctx, ISN_STORE, -off, NULL) == FAIL)
5514 goto erret;
5515 }
5516
5517 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5518 }
5519
5520 /*
5521 * Loop over all the lines of the function and generate instructions.
5522 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005523 for (;;)
5524 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005525 int is_ex_command;
5526
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005527 // Bail out on the first error to avoid a flood of errors and report
5528 // the right line number when inside try/catch.
5529 if (emsg_before != called_emsg)
5530 goto erret;
5531
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005532 if (line != NULL && *line == '|')
5533 // the line continues after a '|'
5534 ++line;
5535 else if (line != NULL && *line != NUL)
5536 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005537 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005538 goto erret;
5539 }
5540 else
5541 {
5542 do
5543 {
5544 ++cctx.ctx_lnum;
5545 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5546 break;
5547 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
5548 } while (line == NULL);
5549 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5550 break;
5551 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
5552 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005553 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005554
5555 had_return = FALSE;
5556 vim_memset(&ea, 0, sizeof(ea));
5557 ea.cmdlinep = &line;
5558 ea.cmd = skipwhite(line);
5559
5560 // "}" ends a block scope
5561 if (*ea.cmd == '}')
5562 {
5563 scopetype_T stype = cctx.ctx_scope == NULL
5564 ? NO_SCOPE : cctx.ctx_scope->se_type;
5565
5566 if (stype == BLOCK_SCOPE)
5567 {
5568 compile_endblock(&cctx);
5569 line = ea.cmd;
5570 }
5571 else
5572 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005573 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005574 goto erret;
5575 }
5576 if (line != NULL)
5577 line = skipwhite(ea.cmd + 1);
5578 continue;
5579 }
5580
5581 // "{" starts a block scope
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01005582 // "{'a': 1}->func() is something else
5583 if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 {
5585 line = compile_block(ea.cmd, &cctx);
5586 continue;
5587 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005588 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005589
5590 /*
5591 * COMMAND MODIFIERS
5592 */
5593 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5594 {
5595 if (errormsg != NULL)
5596 goto erret;
5597 // empty line or comment
5598 line = (char_u *)"";
5599 continue;
5600 }
5601
5602 // Skip ":call" to get to the function name.
5603 if (checkforcmd(&ea.cmd, "call", 3))
5604 ea.cmd = skipwhite(ea.cmd);
5605
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005606 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005608 // Assuming the command starts with a variable or function name,
5609 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5610 // val".
5611 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5612 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005613 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005614 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005615 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005616 int oplen;
5617 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005619 oplen = assignment_len(skipwhite(p), &heredoc);
5620 if (oplen > 0)
5621 {
5622 // Recognize an assignment if we recognize the variable
5623 // name:
5624 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005625 // "local = expr" where "local" is a local var.
5626 // "script = expr" where "script" is a script-local var.
5627 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005628 // "&opt = expr"
5629 // "$ENV = expr"
5630 // "@r = expr"
5631 if (*ea.cmd == '&'
5632 || *ea.cmd == '$'
5633 || *ea.cmd == '@'
5634 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5635 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5636 || lookup_script(ea.cmd, p - ea.cmd) == OK
5637 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5638 {
5639 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5640 if (line == NULL)
5641 goto erret;
5642 continue;
5643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644 }
5645 }
5646 }
5647
5648 /*
5649 * COMMAND after range
5650 */
5651 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005652 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5653 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005654
5655 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5656 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005657 if (cctx.ctx_skip == TRUE)
5658 {
5659 line += STRLEN(line);
5660 continue;
5661 }
5662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005663 // Expression or function call.
5664 if (ea.cmdidx == CMD_eval)
5665 {
5666 p = ea.cmd;
5667 if (compile_expr1(&p, &cctx) == FAIL)
5668 goto erret;
5669
5670 // drop the return value
5671 generate_instr_drop(&cctx, ISN_DROP, 1);
5672 line = p;
5673 continue;
5674 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02005675 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005676 iemsg("Command from find_ex_command() not handled");
5677 goto erret;
5678 }
5679
5680 p = skipwhite(p);
5681
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005682 if (cctx.ctx_skip == TRUE
5683 && ea.cmdidx != CMD_elseif
5684 && ea.cmdidx != CMD_else
5685 && ea.cmdidx != CMD_endif)
5686 {
5687 line += STRLEN(line);
5688 continue;
5689 }
5690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005691 switch (ea.cmdidx)
5692 {
5693 case CMD_def:
5694 case CMD_function:
5695 // TODO: Nested function
5696 emsg("Nested function not implemented yet");
5697 goto erret;
5698
5699 case CMD_return:
5700 line = compile_return(p, set_return_type, &cctx);
5701 had_return = TRUE;
5702 break;
5703
5704 case CMD_let:
5705 case CMD_const:
5706 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5707 break;
5708
5709 case CMD_import:
5710 line = compile_import(p, &cctx);
5711 break;
5712
5713 case CMD_if:
5714 line = compile_if(p, &cctx);
5715 break;
5716 case CMD_elseif:
5717 line = compile_elseif(p, &cctx);
5718 break;
5719 case CMD_else:
5720 line = compile_else(p, &cctx);
5721 break;
5722 case CMD_endif:
5723 line = compile_endif(p, &cctx);
5724 break;
5725
5726 case CMD_while:
5727 line = compile_while(p, &cctx);
5728 break;
5729 case CMD_endwhile:
5730 line = compile_endwhile(p, &cctx);
5731 break;
5732
5733 case CMD_for:
5734 line = compile_for(p, &cctx);
5735 break;
5736 case CMD_endfor:
5737 line = compile_endfor(p, &cctx);
5738 break;
5739 case CMD_continue:
5740 line = compile_continue(p, &cctx);
5741 break;
5742 case CMD_break:
5743 line = compile_break(p, &cctx);
5744 break;
5745
5746 case CMD_try:
5747 line = compile_try(p, &cctx);
5748 break;
5749 case CMD_catch:
5750 line = compile_catch(p, &cctx);
5751 break;
5752 case CMD_finally:
5753 line = compile_finally(p, &cctx);
5754 break;
5755 case CMD_endtry:
5756 line = compile_endtry(p, &cctx);
5757 break;
5758 case CMD_throw:
5759 line = compile_throw(p, &cctx);
5760 break;
5761
5762 case CMD_echo:
5763 line = compile_echo(p, TRUE, &cctx);
5764 break;
5765 case CMD_echon:
5766 line = compile_echo(p, FALSE, &cctx);
5767 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005768 case CMD_execute:
5769 line = compile_execute(p, &cctx);
5770 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771
5772 default:
5773 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005774 // TODO:
5775 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005776 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777 generate_EXEC(&cctx, line);
5778 line = (char_u *)"";
5779 break;
5780 }
5781 if (line == NULL)
5782 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02005783 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005784
5785 if (cctx.ctx_type_stack.ga_len < 0)
5786 {
5787 iemsg("Type stack underflow");
5788 goto erret;
5789 }
5790 }
5791
5792 if (cctx.ctx_scope != NULL)
5793 {
5794 if (cctx.ctx_scope->se_type == IF_SCOPE)
5795 emsg(_(e_endif));
5796 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5797 emsg(_(e_endwhile));
5798 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5799 emsg(_(e_endfor));
5800 else
5801 emsg(_("E1026: Missing }"));
5802 goto erret;
5803 }
5804
5805 if (!had_return)
5806 {
5807 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5808 {
5809 emsg(_("E1027: Missing return statement"));
5810 goto erret;
5811 }
5812
5813 // Return zero if there is no return at the end.
5814 generate_PUSHNR(&cctx, 0);
5815 generate_instr(&cctx, ISN_RETURN);
5816 }
5817
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005818 {
5819 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5820 + ufunc->uf_dfunc_idx;
5821 dfunc->df_deleted = FALSE;
5822 dfunc->df_instr = instr->ga_data;
5823 dfunc->df_instr_count = instr->ga_len;
5824 dfunc->df_varcount = cctx.ctx_max_local;
5825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005826
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005827 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005828 int varargs = ufunc->uf_va_name != NULL;
5829 int argcount = ufunc->uf_args.ga_len - (varargs ? 1 : 0);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005830
5831 // Create a type for the function, with the return type and any
5832 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005833 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5834 // The type is included in "tt_args".
5835 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5836 ufunc->uf_args.ga_len, &ufunc->uf_type_list);
5837 if (ufunc->uf_args.ga_len > 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005838 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005839 if (func_type_add_arg_types(ufunc->uf_func_type,
5840 ufunc->uf_args.ga_len,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005841 argcount - ufunc->uf_def_args.ga_len,
5842 &ufunc->uf_type_list) == FAIL)
5843 {
5844 ret = FAIL;
5845 goto erret;
5846 }
5847 if (ufunc->uf_arg_types == NULL)
5848 {
5849 int i;
5850
5851 // lambda does not have argument types.
5852 for (i = 0; i < argcount; ++i)
5853 ufunc->uf_func_type->tt_args[i] = &t_any;
5854 }
5855 else
5856 mch_memmove(ufunc->uf_func_type->tt_args,
5857 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005858 if (varargs)
5859 ufunc->uf_func_type->tt_args[argcount] =
5860 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005861 }
5862 }
5863
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005864 ret = OK;
5865
5866erret:
5867 if (ret == FAIL)
5868 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005869 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005870 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5871 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005872
5873 for (idx = 0; idx < instr->ga_len; ++idx)
5874 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005875 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005877 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005878 if (!dfunc->df_deleted)
5879 --def_functions.ga_len;
5880
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005881 while (cctx.ctx_scope != NULL)
5882 drop_scope(&cctx);
5883
Bram Moolenaar20431c92020-03-20 18:39:46 +01005884 // Don't execute this function body.
5885 ga_clear_strings(&ufunc->uf_lines);
5886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005887 if (errormsg != NULL)
5888 emsg(errormsg);
5889 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005890 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005891 }
5892
5893 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005894 free_imported(&cctx);
5895 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897}
5898
5899/*
5900 * Delete an instruction, free what it contains.
5901 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01005902 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005903delete_instr(isn_T *isn)
5904{
5905 switch (isn->isn_type)
5906 {
5907 case ISN_EXEC:
5908 case ISN_LOADENV:
5909 case ISN_LOADG:
5910 case ISN_LOADOPT:
5911 case ISN_MEMBER:
5912 case ISN_PUSHEXC:
5913 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005914 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005916 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005917 vim_free(isn->isn_arg.string);
5918 break;
5919
5920 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005921 case ISN_STORES:
5922 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005923 break;
5924
5925 case ISN_STOREOPT:
5926 vim_free(isn->isn_arg.storeopt.so_name);
5927 break;
5928
5929 case ISN_PUSHBLOB: // push blob isn_arg.blob
5930 blob_unref(isn->isn_arg.blob);
5931 break;
5932
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005933 case ISN_PUSHPARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01005934 partial_unref(isn->isn_arg.partial);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005935 break;
5936
5937 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005938#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005939 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005940#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005941 break;
5942
5943 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005944#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005945 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01005946#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005947 break;
5948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005949 case ISN_UCALL:
5950 vim_free(isn->isn_arg.ufunc.cuf_name);
5951 break;
5952
5953 case ISN_2BOOL:
5954 case ISN_2STRING:
5955 case ISN_ADDBLOB:
5956 case ISN_ADDLIST:
5957 case ISN_BCALL:
5958 case ISN_CATCH:
5959 case ISN_CHECKNR:
5960 case ISN_CHECKTYPE:
5961 case ISN_COMPAREANY:
5962 case ISN_COMPAREBLOB:
5963 case ISN_COMPAREBOOL:
5964 case ISN_COMPAREDICT:
5965 case ISN_COMPAREFLOAT:
5966 case ISN_COMPAREFUNC:
5967 case ISN_COMPARELIST:
5968 case ISN_COMPARENR:
5969 case ISN_COMPAREPARTIAL:
5970 case ISN_COMPARESPECIAL:
5971 case ISN_COMPARESTRING:
5972 case ISN_CONCAT:
5973 case ISN_DCALL:
5974 case ISN_DROP:
5975 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01005976 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977 case ISN_ENDTRY:
5978 case ISN_FOR:
5979 case ISN_FUNCREF:
5980 case ISN_INDEX:
5981 case ISN_JUMP:
5982 case ISN_LOAD:
5983 case ISN_LOADSCRIPT:
5984 case ISN_LOADREG:
5985 case ISN_LOADV:
5986 case ISN_NEGATENR:
5987 case ISN_NEWDICT:
5988 case ISN_NEWLIST:
5989 case ISN_OPNR:
5990 case ISN_OPFLOAT:
5991 case ISN_OPANY:
5992 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005993 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994 case ISN_PUSHF:
5995 case ISN_PUSHNR:
5996 case ISN_PUSHBOOL:
5997 case ISN_PUSHSPEC:
5998 case ISN_RETURN:
5999 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006000 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006002 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 case ISN_STORESCRIPT:
6004 case ISN_THROW:
6005 case ISN_TRY:
6006 // nothing allocated
6007 break;
6008 }
6009}
6010
6011/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006012 * Free all instructions for "dfunc".
6013 */
6014 static void
6015delete_def_function_contents(dfunc_T *dfunc)
6016{
6017 int idx;
6018
6019 ga_clear(&dfunc->df_def_args_isn);
6020
6021 if (dfunc->df_instr != NULL)
6022 {
6023 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6024 delete_instr(dfunc->df_instr + idx);
6025 VIM_CLEAR(dfunc->df_instr);
6026 }
6027
6028 dfunc->df_deleted = TRUE;
6029}
6030
6031/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006032 * When a user function is deleted, delete any associated def function.
6033 */
6034 void
6035delete_def_function(ufunc_T *ufunc)
6036{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006037 if (ufunc->uf_dfunc_idx >= 0)
6038 {
6039 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6040 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006041
Bram Moolenaar20431c92020-03-20 18:39:46 +01006042 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006043 }
6044}
6045
6046#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006047/*
6048 * Free all functions defined with ":def".
6049 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050 void
6051free_def_functions(void)
6052{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006053 int idx;
6054
6055 for (idx = 0; idx < def_functions.ga_len; ++idx)
6056 {
6057 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6058
6059 delete_def_function_contents(dfunc);
6060 }
6061
6062 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063}
6064#endif
6065
6066
6067#endif // FEAT_EVAL