blob: 0d245d2ea3179c4d329de9607528288fcf3385b0 [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 Moolenaar0b76b422020-04-07 22:05:08 +0200133static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
134static int check_type(type_T *expected, type_T *actual, int give_msg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135
136/*
137 * Lookup variable "name" in the local scope and return the index.
138 */
139 static int
140lookup_local(char_u *name, size_t len, cctx_T *cctx)
141{
142 int idx;
143
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100144 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 return -1;
146 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
147 {
148 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
149
150 if (STRNCMP(name, lvar->lv_name, len) == 0
151 && STRLEN(lvar->lv_name) == len)
152 return idx;
153 }
154 return -1;
155}
156
157/*
158 * Lookup an argument in the current function.
159 * Returns the argument index or -1 if not found.
160 */
161 static int
162lookup_arg(char_u *name, size_t len, cctx_T *cctx)
163{
164 int idx;
165
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100166 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167 return -1;
168 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
169 {
170 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
171
172 if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
173 return idx;
174 }
175 return -1;
176}
177
178/*
179 * Lookup a vararg argument in the current function.
180 * Returns TRUE if there is a match.
181 */
182 static int
183lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
184{
185 char_u *va_name = cctx->ctx_ufunc->uf_va_name;
186
187 return len > 0 && va_name != NULL
188 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
189}
190
191/*
192 * Lookup a variable in the current script.
193 * Returns OK or FAIL.
194 */
195 static int
196lookup_script(char_u *name, size_t len)
197{
198 int cc;
199 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
200 dictitem_T *di;
201
202 cc = name[len];
203 name[len] = NUL;
204 di = find_var_in_ht(ht, 0, name, TRUE);
205 name[len] = cc;
206 return di == NULL ? FAIL: OK;
207}
208
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100209/*
210 * Check if "p[len]" is already defined, either in script "import_sid" or in
211 * compilation context "cctx".
212 * Return FAIL and give an error if it defined.
213 */
214 int
215check_defined(char_u *p, int len, cctx_T *cctx)
216{
217 if (lookup_script(p, len) == OK
218 || (cctx != NULL
219 && (lookup_local(p, len, cctx) >= 0
220 || find_imported(p, len, cctx) != NULL)))
221 {
222 semsg("E1073: imported name already defined: %s", p);
223 return FAIL;
224 }
225 return OK;
226}
227
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200228/*
229 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
230 * be freed later.
231 */
232 static type_T *
233alloc_type(garray_T *type_gap)
234{
235 type_T *type;
236
237 if (ga_grow(type_gap, 1) == FAIL)
238 return NULL;
239 type = ALLOC_CLEAR_ONE(type_T);
240 if (type != NULL)
241 {
242 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
243 ++type_gap->ga_len;
244 }
245 return type;
246}
247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200249get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250{
251 type_T *type;
252
253 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200254 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200256 if (member_type->tt_type == VAR_VOID
257 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100258 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100259 if (member_type->tt_type == VAR_BOOL)
260 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261 if (member_type->tt_type == VAR_NUMBER)
262 return &t_list_number;
263 if (member_type->tt_type == VAR_STRING)
264 return &t_list_string;
265
266 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200267 type = alloc_type(type_gap);
268 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100269 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270 type->tt_type = VAR_LIST;
271 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200272 type->tt_argcount = 0;
273 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 return type;
275}
276
277 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200278get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279{
280 type_T *type;
281
282 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200283 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200285 if (member_type->tt_type == VAR_VOID
286 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100287 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100288 if (member_type->tt_type == VAR_BOOL)
289 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 if (member_type->tt_type == VAR_NUMBER)
291 return &t_dict_number;
292 if (member_type->tt_type == VAR_STRING)
293 return &t_dict_string;
294
295 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200296 type = alloc_type(type_gap);
297 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100298 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299 type->tt_type = VAR_DICT;
300 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200301 type->tt_argcount = 0;
302 type->tt_args = NULL;
303 return type;
304}
305
306/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200307 * Allocate a new type for a function.
308 */
309 static type_T *
310alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
311{
312 type_T *type = alloc_type(type_gap);
313
314 if (type == NULL)
315 return &t_any;
316 type->tt_type = VAR_FUNC;
317 type->tt_member = ret_type;
318 type->tt_argcount = argcount;
319 type->tt_args = NULL;
320 return type;
321}
322
323/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200324 * Get a function type, based on the return type "ret_type".
325 * If "argcount" is -1 or 0 a predefined type can be used.
326 * If "argcount" > 0 always create a new type, so that arguments can be added.
327 */
328 static type_T *
329get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
330{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200331 // recognize commonly used types
332 if (argcount <= 0)
333 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200334 if (ret_type == &t_unknown)
335 {
336 // (argcount == 0) is not possible
337 return &t_func_unknown;
338 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200339 if (ret_type == &t_void)
340 {
341 if (argcount == 0)
342 return &t_func_0_void;
343 else
344 return &t_func_void;
345 }
346 if (ret_type == &t_any)
347 {
348 if (argcount == 0)
349 return &t_func_0_any;
350 else
351 return &t_func_any;
352 }
353 if (ret_type == &t_number)
354 {
355 if (argcount == 0)
356 return &t_func_0_number;
357 else
358 return &t_func_number;
359 }
360 if (ret_type == &t_string)
361 {
362 if (argcount == 0)
363 return &t_func_0_string;
364 else
365 return &t_func_string;
366 }
367 }
368
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200369 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370}
371
Bram Moolenaara8c17702020-04-01 21:17:24 +0200372/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200373 * For a function type, reserve space for "argcount" argument types (including
374 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200375 */
376 static int
377func_type_add_arg_types(
378 type_T *functype,
379 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200380 garray_T *type_gap)
381{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200382 // To make it easy to free the space needed for the argument types, add the
383 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200384 if (ga_grow(type_gap, 1) == FAIL)
385 return FAIL;
386 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
387 if (functype->tt_args == NULL)
388 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200389 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
390 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200391 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200392 return OK;
393}
394
395/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200396 * Return the type_T for a typval. Only for primitive types.
397 */
398 static type_T *
399typval2type(typval_T *tv)
400{
401 if (tv->v_type == VAR_NUMBER)
402 return &t_number;
403 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200404 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200405 if (tv->v_type == VAR_STRING)
406 return &t_string;
407 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
408 return &t_list_string;
409 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
410 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200411 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200412}
413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414/////////////////////////////////////////////////////////////////////
415// Following generate_ functions expect the caller to call ga_grow().
416
Bram Moolenaar080457c2020-03-03 21:53:32 +0100417#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
418#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420/*
421 * Generate an instruction without arguments.
422 * Returns a pointer to the new instruction, NULL if failed.
423 */
424 static isn_T *
425generate_instr(cctx_T *cctx, isntype_T isn_type)
426{
427 garray_T *instr = &cctx->ctx_instr;
428 isn_T *isn;
429
Bram Moolenaar080457c2020-03-03 21:53:32 +0100430 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431 if (ga_grow(instr, 1) == FAIL)
432 return NULL;
433 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
434 isn->isn_type = isn_type;
435 isn->isn_lnum = cctx->ctx_lnum + 1;
436 ++instr->ga_len;
437
438 return isn;
439}
440
441/*
442 * Generate an instruction without arguments.
443 * "drop" will be removed from the stack.
444 * Returns a pointer to the new instruction, NULL if failed.
445 */
446 static isn_T *
447generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
448{
449 garray_T *stack = &cctx->ctx_type_stack;
450
Bram Moolenaar080457c2020-03-03 21:53:32 +0100451 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452 stack->ga_len -= drop;
453 return generate_instr(cctx, isn_type);
454}
455
456/*
457 * Generate instruction "isn_type" and put "type" on the type stack.
458 */
459 static isn_T *
460generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
461{
462 isn_T *isn;
463 garray_T *stack = &cctx->ctx_type_stack;
464
465 if ((isn = generate_instr(cctx, isn_type)) == NULL)
466 return NULL;
467
468 if (ga_grow(stack, 1) == FAIL)
469 return NULL;
470 ((type_T **)stack->ga_data)[stack->ga_len] = type;
471 ++stack->ga_len;
472
473 return isn;
474}
475
476/*
477 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
478 */
479 static int
480may_generate_2STRING(int offset, cctx_T *cctx)
481{
482 isn_T *isn;
483 garray_T *stack = &cctx->ctx_type_stack;
484 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
485
486 if ((*type)->tt_type == VAR_STRING)
487 return OK;
488 *type = &t_string;
489
490 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
491 return FAIL;
492 isn->isn_arg.number = offset;
493
494 return OK;
495}
496
497 static int
498check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
499{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200500 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200502 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 {
504 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100505 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 else
507 semsg(_("E1036: %c requires number or float arguments"), *op);
508 return FAIL;
509 }
510 return OK;
511}
512
513/*
514 * Generate an instruction with two arguments. The instruction depends on the
515 * type of the arguments.
516 */
517 static int
518generate_two_op(cctx_T *cctx, char_u *op)
519{
520 garray_T *stack = &cctx->ctx_type_stack;
521 type_T *type1;
522 type_T *type2;
523 vartype_T vartype;
524 isn_T *isn;
525
Bram Moolenaar080457c2020-03-03 21:53:32 +0100526 RETURN_OK_IF_SKIP(cctx);
527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 // Get the known type of the two items on the stack. If they are matching
529 // use a type-specific instruction. Otherwise fall back to runtime type
530 // checking.
531 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
532 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200533 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534 if (type1->tt_type == type2->tt_type
535 && (type1->tt_type == VAR_NUMBER
536 || type1->tt_type == VAR_LIST
537#ifdef FEAT_FLOAT
538 || type1->tt_type == VAR_FLOAT
539#endif
540 || type1->tt_type == VAR_BLOB))
541 vartype = type1->tt_type;
542
543 switch (*op)
544 {
545 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200546 && type1->tt_type != VAR_ANY
547 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 && check_number_or_float(
549 type1->tt_type, type2->tt_type, op) == FAIL)
550 return FAIL;
551 isn = generate_instr_drop(cctx,
552 vartype == VAR_NUMBER ? ISN_OPNR
553 : vartype == VAR_LIST ? ISN_ADDLIST
554 : vartype == VAR_BLOB ? ISN_ADDBLOB
555#ifdef FEAT_FLOAT
556 : vartype == VAR_FLOAT ? ISN_OPFLOAT
557#endif
558 : ISN_OPANY, 1);
559 if (isn != NULL)
560 isn->isn_arg.op.op_type = EXPR_ADD;
561 break;
562
563 case '-':
564 case '*':
565 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
566 op) == FAIL)
567 return FAIL;
568 if (vartype == VAR_NUMBER)
569 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
570#ifdef FEAT_FLOAT
571 else if (vartype == VAR_FLOAT)
572 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
573#endif
574 else
575 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
576 if (isn != NULL)
577 isn->isn_arg.op.op_type = *op == '*'
578 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
579 break;
580
Bram Moolenaar4c683752020-04-05 21:38:23 +0200581 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200583 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 && type2->tt_type != VAR_NUMBER))
585 {
586 emsg(_("E1035: % requires number arguments"));
587 return FAIL;
588 }
589 isn = generate_instr_drop(cctx,
590 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
591 if (isn != NULL)
592 isn->isn_arg.op.op_type = EXPR_REM;
593 break;
594 }
595
596 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200597 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 {
599 type_T *type = &t_any;
600
601#ifdef FEAT_FLOAT
602 // float+number and number+float results in float
603 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
604 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
605 type = &t_float;
606#endif
607 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
608 }
609
610 return OK;
611}
612
613/*
614 * Generate an ISN_COMPARE* instruction with a boolean result.
615 */
616 static int
617generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
618{
619 isntype_T isntype = ISN_DROP;
620 isn_T *isn;
621 garray_T *stack = &cctx->ctx_type_stack;
622 vartype_T type1;
623 vartype_T type2;
624
Bram Moolenaar080457c2020-03-03 21:53:32 +0100625 RETURN_OK_IF_SKIP(cctx);
626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627 // Get the known type of the two items on the stack. If they are matching
628 // use a type-specific instruction. Otherwise fall back to runtime type
629 // checking.
630 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
631 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200632 if (type1 == VAR_UNKNOWN)
633 type1 = VAR_ANY;
634 if (type2 == VAR_UNKNOWN)
635 type2 = VAR_ANY;
636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 if (type1 == type2)
638 {
639 switch (type1)
640 {
641 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
642 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
643 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
644 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
645 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
646 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
647 case VAR_LIST: isntype = ISN_COMPARELIST; break;
648 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
649 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650 default: isntype = ISN_COMPAREANY; break;
651 }
652 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200653 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
655 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
656 isntype = ISN_COMPAREANY;
657
658 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
659 && (isntype == ISN_COMPAREBOOL
660 || isntype == ISN_COMPARESPECIAL
661 || isntype == ISN_COMPARENR
662 || isntype == ISN_COMPAREFLOAT))
663 {
664 semsg(_("E1037: Cannot use \"%s\" with %s"),
665 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
666 return FAIL;
667 }
668 if (isntype == ISN_DROP
669 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
670 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
671 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
672 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
673 && exptype != EXPR_IS && exptype != EXPR_ISNOT
674 && (type1 == VAR_BLOB || type2 == VAR_BLOB
675 || type1 == VAR_LIST || type2 == VAR_LIST))))
676 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100677 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678 vartype_name(type1), vartype_name(type2));
679 return FAIL;
680 }
681
682 if ((isn = generate_instr(cctx, isntype)) == NULL)
683 return FAIL;
684 isn->isn_arg.op.op_type = exptype;
685 isn->isn_arg.op.op_ic = ic;
686
687 // takes two arguments, puts one bool back
688 if (stack->ga_len >= 2)
689 {
690 --stack->ga_len;
691 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
692 }
693
694 return OK;
695}
696
697/*
698 * Generate an ISN_2BOOL instruction.
699 */
700 static int
701generate_2BOOL(cctx_T *cctx, int invert)
702{
703 isn_T *isn;
704 garray_T *stack = &cctx->ctx_type_stack;
705
Bram Moolenaar080457c2020-03-03 21:53:32 +0100706 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
708 return FAIL;
709 isn->isn_arg.number = invert;
710
711 // type becomes bool
712 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
713
714 return OK;
715}
716
717 static int
718generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
719{
720 isn_T *isn;
721 garray_T *stack = &cctx->ctx_type_stack;
722
Bram Moolenaar080457c2020-03-03 21:53:32 +0100723 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
725 return FAIL;
726 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
727 isn->isn_arg.type.ct_off = offset;
728
729 // type becomes vartype
730 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
731
732 return OK;
733}
734
735/*
736 * Generate an ISN_PUSHNR instruction.
737 */
738 static int
739generate_PUSHNR(cctx_T *cctx, varnumber_T number)
740{
741 isn_T *isn;
742
Bram Moolenaar080457c2020-03-03 21:53:32 +0100743 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
745 return FAIL;
746 isn->isn_arg.number = number;
747
748 return OK;
749}
750
751/*
752 * Generate an ISN_PUSHBOOL instruction.
753 */
754 static int
755generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
756{
757 isn_T *isn;
758
Bram Moolenaar080457c2020-03-03 21:53:32 +0100759 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
761 return FAIL;
762 isn->isn_arg.number = number;
763
764 return OK;
765}
766
767/*
768 * Generate an ISN_PUSHSPEC instruction.
769 */
770 static int
771generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
772{
773 isn_T *isn;
774
Bram Moolenaar080457c2020-03-03 21:53:32 +0100775 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
777 return FAIL;
778 isn->isn_arg.number = number;
779
780 return OK;
781}
782
783#ifdef FEAT_FLOAT
784/*
785 * Generate an ISN_PUSHF instruction.
786 */
787 static int
788generate_PUSHF(cctx_T *cctx, float_T fnumber)
789{
790 isn_T *isn;
791
Bram Moolenaar080457c2020-03-03 21:53:32 +0100792 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
794 return FAIL;
795 isn->isn_arg.fnumber = fnumber;
796
797 return OK;
798}
799#endif
800
801/*
802 * Generate an ISN_PUSHS instruction.
803 * Consumes "str".
804 */
805 static int
806generate_PUSHS(cctx_T *cctx, char_u *str)
807{
808 isn_T *isn;
809
Bram Moolenaar080457c2020-03-03 21:53:32 +0100810 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
812 return FAIL;
813 isn->isn_arg.string = str;
814
815 return OK;
816}
817
818/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100819 * Generate an ISN_PUSHCHANNEL instruction.
820 * Consumes "channel".
821 */
822 static int
823generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
824{
825 isn_T *isn;
826
Bram Moolenaar080457c2020-03-03 21:53:32 +0100827 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100828 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
829 return FAIL;
830 isn->isn_arg.channel = channel;
831
832 return OK;
833}
834
835/*
836 * Generate an ISN_PUSHJOB instruction.
837 * Consumes "job".
838 */
839 static int
840generate_PUSHJOB(cctx_T *cctx, job_T *job)
841{
842 isn_T *isn;
843
Bram Moolenaar080457c2020-03-03 21:53:32 +0100844 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100845 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100846 return FAIL;
847 isn->isn_arg.job = job;
848
849 return OK;
850}
851
852/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 * Generate an ISN_PUSHBLOB instruction.
854 * Consumes "blob".
855 */
856 static int
857generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
858{
859 isn_T *isn;
860
Bram Moolenaar080457c2020-03-03 21:53:32 +0100861 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
863 return FAIL;
864 isn->isn_arg.blob = blob;
865
866 return OK;
867}
868
869/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100870 * Generate an ISN_PUSHFUNC instruction with name "name".
871 * Consumes "name".
872 */
873 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200874generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100875{
876 isn_T *isn;
877
Bram Moolenaar080457c2020-03-03 21:53:32 +0100878 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200879 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100880 return FAIL;
881 isn->isn_arg.string = name;
882
883 return OK;
884}
885
886/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 * Generate an ISN_STORE instruction.
888 */
889 static int
890generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
891{
892 isn_T *isn;
893
Bram Moolenaar080457c2020-03-03 21:53:32 +0100894 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
896 return FAIL;
897 if (name != NULL)
898 isn->isn_arg.string = vim_strsave(name);
899 else
900 isn->isn_arg.number = idx;
901
902 return OK;
903}
904
905/*
906 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
907 */
908 static int
909generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
910{
911 isn_T *isn;
912
Bram Moolenaar080457c2020-03-03 21:53:32 +0100913 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
915 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100916 isn->isn_arg.storenr.stnr_idx = idx;
917 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100918
919 return OK;
920}
921
922/*
923 * Generate an ISN_STOREOPT instruction
924 */
925 static int
926generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
927{
928 isn_T *isn;
929
Bram Moolenaar080457c2020-03-03 21:53:32 +0100930 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
932 return FAIL;
933 isn->isn_arg.storeopt.so_name = vim_strsave(name);
934 isn->isn_arg.storeopt.so_flags = opt_flags;
935
936 return OK;
937}
938
939/*
940 * Generate an ISN_LOAD or similar instruction.
941 */
942 static int
943generate_LOAD(
944 cctx_T *cctx,
945 isntype_T isn_type,
946 int idx,
947 char_u *name,
948 type_T *type)
949{
950 isn_T *isn;
951
Bram Moolenaar080457c2020-03-03 21:53:32 +0100952 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
954 return FAIL;
955 if (name != NULL)
956 isn->isn_arg.string = vim_strsave(name);
957 else
958 isn->isn_arg.number = idx;
959
960 return OK;
961}
962
963/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200964 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100965 */
966 static int
967generate_LOADV(
968 cctx_T *cctx,
969 char_u *name,
970 int error)
971{
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200972 int di_flags;
973 int vidx = find_vim_var(name, &di_flags);
974 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100975
Bram Moolenaar080457c2020-03-03 21:53:32 +0100976 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100977 if (vidx < 0)
978 {
979 if (error)
980 semsg(_(e_var_notfound), name);
981 return FAIL;
982 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200983 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100984
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200985 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100986}
987
988/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989 * Generate an ISN_LOADS instruction.
990 */
991 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100992generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100994 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100996 int sid,
997 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998{
999 isn_T *isn;
1000
Bram Moolenaar080457c2020-03-03 21:53:32 +01001001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001002 if (isn_type == ISN_LOADS)
1003 isn = generate_instr_type(cctx, isn_type, type);
1004 else
1005 isn = generate_instr_drop(cctx, isn_type, 1);
1006 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001008 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1009 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010
1011 return OK;
1012}
1013
1014/*
1015 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1016 */
1017 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001018generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 cctx_T *cctx,
1020 isntype_T isn_type,
1021 int sid,
1022 int idx,
1023 type_T *type)
1024{
1025 isn_T *isn;
1026
Bram Moolenaar080457c2020-03-03 21:53:32 +01001027 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 if (isn_type == ISN_LOADSCRIPT)
1029 isn = generate_instr_type(cctx, isn_type, type);
1030 else
1031 isn = generate_instr_drop(cctx, isn_type, 1);
1032 if (isn == NULL)
1033 return FAIL;
1034 isn->isn_arg.script.script_sid = sid;
1035 isn->isn_arg.script.script_idx = idx;
1036 return OK;
1037}
1038
1039/*
1040 * Generate an ISN_NEWLIST instruction.
1041 */
1042 static int
1043generate_NEWLIST(cctx_T *cctx, int count)
1044{
1045 isn_T *isn;
1046 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047 type_T *type;
1048 type_T *member;
1049
Bram Moolenaar080457c2020-03-03 21:53:32 +01001050 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1052 return FAIL;
1053 isn->isn_arg.number = count;
1054
1055 // drop the value types
1056 stack->ga_len -= count;
1057
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001058 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001059 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060 if (count > 0)
1061 member = ((type_T **)stack->ga_data)[stack->ga_len];
1062 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001063 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001064 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065
1066 // add the list type to the type stack
1067 if (ga_grow(stack, 1) == FAIL)
1068 return FAIL;
1069 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1070 ++stack->ga_len;
1071
1072 return OK;
1073}
1074
1075/*
1076 * Generate an ISN_NEWDICT instruction.
1077 */
1078 static int
1079generate_NEWDICT(cctx_T *cctx, int count)
1080{
1081 isn_T *isn;
1082 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 type_T *type;
1084 type_T *member;
1085
Bram Moolenaar080457c2020-03-03 21:53:32 +01001086 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1088 return FAIL;
1089 isn->isn_arg.number = count;
1090
1091 // drop the key and value types
1092 stack->ga_len -= 2 * count;
1093
Bram Moolenaar436472f2020-02-20 22:54:43 +01001094 // Use the first value type for the list member type. Use "void" for an
1095 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 if (count > 0)
1097 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1098 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001099 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001100 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101
1102 // add the dict type to the type stack
1103 if (ga_grow(stack, 1) == FAIL)
1104 return FAIL;
1105 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1106 ++stack->ga_len;
1107
1108 return OK;
1109}
1110
1111/*
1112 * Generate an ISN_FUNCREF instruction.
1113 */
1114 static int
1115generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1116{
1117 isn_T *isn;
1118 garray_T *stack = &cctx->ctx_type_stack;
1119
Bram Moolenaar080457c2020-03-03 21:53:32 +01001120 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1122 return FAIL;
1123 isn->isn_arg.number = dfunc_idx;
1124
1125 if (ga_grow(stack, 1) == FAIL)
1126 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001127 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 // TODO: argument and return types
1129 ++stack->ga_len;
1130
1131 return OK;
1132}
1133
1134/*
1135 * Generate an ISN_JUMP instruction.
1136 */
1137 static int
1138generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1139{
1140 isn_T *isn;
1141 garray_T *stack = &cctx->ctx_type_stack;
1142
Bram Moolenaar080457c2020-03-03 21:53:32 +01001143 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1145 return FAIL;
1146 isn->isn_arg.jump.jump_when = when;
1147 isn->isn_arg.jump.jump_where = where;
1148
1149 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1150 --stack->ga_len;
1151
1152 return OK;
1153}
1154
1155 static int
1156generate_FOR(cctx_T *cctx, int loop_idx)
1157{
1158 isn_T *isn;
1159 garray_T *stack = &cctx->ctx_type_stack;
1160
Bram Moolenaar080457c2020-03-03 21:53:32 +01001161 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1163 return FAIL;
1164 isn->isn_arg.forloop.for_idx = loop_idx;
1165
1166 if (ga_grow(stack, 1) == FAIL)
1167 return FAIL;
1168 // type doesn't matter, will be stored next
1169 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1170 ++stack->ga_len;
1171
1172 return OK;
1173}
1174
1175/*
1176 * Generate an ISN_BCALL instruction.
1177 * Return FAIL if the number of arguments is wrong.
1178 */
1179 static int
1180generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1181{
1182 isn_T *isn;
1183 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001184 type_T *argtypes[MAX_FUNC_ARGS];
1185 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186
Bram Moolenaar080457c2020-03-03 21:53:32 +01001187 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 if (check_internal_func(func_idx, argcount) == FAIL)
1189 return FAIL;
1190
1191 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1192 return FAIL;
1193 isn->isn_arg.bfunc.cbf_idx = func_idx;
1194 isn->isn_arg.bfunc.cbf_argcount = argcount;
1195
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001196 for (i = 0; i < argcount; ++i)
1197 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 stack->ga_len -= argcount; // drop the arguments
1200 if (ga_grow(stack, 1) == FAIL)
1201 return FAIL;
1202 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001203 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 ++stack->ga_len; // add return value
1205
1206 return OK;
1207}
1208
1209/*
1210 * Generate an ISN_DCALL or ISN_UCALL instruction.
1211 * Return FAIL if the number of arguments is wrong.
1212 */
1213 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001214generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215{
1216 isn_T *isn;
1217 garray_T *stack = &cctx->ctx_type_stack;
1218 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001219 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220
Bram Moolenaar080457c2020-03-03 21:53:32 +01001221 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 if (argcount > regular_args && !has_varargs(ufunc))
1223 {
1224 semsg(_(e_toomanyarg), ufunc->uf_name);
1225 return FAIL;
1226 }
1227 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1228 {
1229 semsg(_(e_toofewarg), ufunc->uf_name);
1230 return FAIL;
1231 }
1232
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001233 if (ufunc->uf_dfunc_idx >= 0)
1234 {
1235 int i;
1236
1237 for (i = 0; i < argcount; ++i)
1238 {
1239 type_T *expected;
1240 type_T *actual;
1241
1242 if (i < regular_args)
1243 {
1244 if (ufunc->uf_arg_types == NULL)
1245 continue;
1246 expected = ufunc->uf_arg_types[i];
1247 }
1248 else
1249 expected = ufunc->uf_va_type->tt_member;
1250 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1251 if (check_type(expected, actual, FALSE) == FAIL)
1252 {
1253 arg_type_mismatch(expected, actual, i + 1);
1254 return FAIL;
1255 }
1256 }
1257 }
1258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 if ((isn = generate_instr(cctx,
1260 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1261 return FAIL;
1262 if (ufunc->uf_dfunc_idx >= 0)
1263 {
1264 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1265 isn->isn_arg.dfunc.cdf_argcount = argcount;
1266 }
1267 else
1268 {
1269 // A user function may be deleted and redefined later, can't use the
1270 // ufunc pointer, need to look it up again at runtime.
1271 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1272 isn->isn_arg.ufunc.cuf_argcount = argcount;
1273 }
1274
1275 stack->ga_len -= argcount; // drop the arguments
1276 if (ga_grow(stack, 1) == FAIL)
1277 return FAIL;
1278 // add return value
1279 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1280 ++stack->ga_len;
1281
1282 return OK;
1283}
1284
1285/*
1286 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1287 */
1288 static int
1289generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1290{
1291 isn_T *isn;
1292 garray_T *stack = &cctx->ctx_type_stack;
1293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1296 return FAIL;
1297 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1298 isn->isn_arg.ufunc.cuf_argcount = argcount;
1299
1300 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001301 if (ga_grow(stack, 1) == FAIL)
1302 return FAIL;
1303 // add return value
1304 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1305 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306
1307 return OK;
1308}
1309
1310/*
1311 * Generate an ISN_PCALL instruction.
1312 */
1313 static int
1314generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1315{
1316 isn_T *isn;
1317 garray_T *stack = &cctx->ctx_type_stack;
1318
Bram Moolenaar080457c2020-03-03 21:53:32 +01001319 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1322 return FAIL;
1323 isn->isn_arg.pfunc.cpf_top = at_top;
1324 isn->isn_arg.pfunc.cpf_argcount = argcount;
1325
1326 stack->ga_len -= argcount; // drop the arguments
1327
1328 // drop the funcref/partial, get back the return value
1329 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1330
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001331 // If partial is above the arguments it must be cleared and replaced with
1332 // the return value.
1333 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1334 return FAIL;
1335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 return OK;
1337}
1338
1339/*
1340 * Generate an ISN_MEMBER instruction.
1341 */
1342 static int
1343generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1344{
1345 isn_T *isn;
1346 garray_T *stack = &cctx->ctx_type_stack;
1347 type_T *type;
1348
Bram Moolenaar080457c2020-03-03 21:53:32 +01001349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1351 return FAIL;
1352 isn->isn_arg.string = vim_strnsave(name, (int)len);
1353
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001354 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001356 if (type->tt_type != VAR_DICT && type != &t_any)
1357 {
1358 emsg(_(e_dictreq));
1359 return FAIL;
1360 }
1361 // change dict type to dict member type
1362 if (type->tt_type == VAR_DICT)
1363 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364
1365 return OK;
1366}
1367
1368/*
1369 * Generate an ISN_ECHO instruction.
1370 */
1371 static int
1372generate_ECHO(cctx_T *cctx, int with_white, int count)
1373{
1374 isn_T *isn;
1375
Bram Moolenaar080457c2020-03-03 21:53:32 +01001376 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1378 return FAIL;
1379 isn->isn_arg.echo.echo_with_white = with_white;
1380 isn->isn_arg.echo.echo_count = count;
1381
1382 return OK;
1383}
1384
Bram Moolenaarad39c092020-02-26 18:23:43 +01001385/*
1386 * Generate an ISN_EXECUTE instruction.
1387 */
1388 static int
1389generate_EXECUTE(cctx_T *cctx, int count)
1390{
1391 isn_T *isn;
1392
1393 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1394 return FAIL;
1395 isn->isn_arg.number = count;
1396
1397 return OK;
1398}
1399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 static int
1401generate_EXEC(cctx_T *cctx, char_u *line)
1402{
1403 isn_T *isn;
1404
Bram Moolenaar080457c2020-03-03 21:53:32 +01001405 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1407 return FAIL;
1408 isn->isn_arg.string = vim_strsave(line);
1409 return OK;
1410}
1411
1412static char e_white_both[] =
1413 N_("E1004: white space required before and after '%s'");
Bram Moolenaard77a8522020-04-03 21:59:57 +02001414static char e_white_after[] = N_("E1069: white space required after '%s'");
1415static char e_no_white_before[] = N_("E1068: No white space allowed before '%s'");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416
1417/*
1418 * Reserve space for a local variable.
1419 * Return the index or -1 if it failed.
1420 */
1421 static int
1422reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1423{
1424 int idx;
1425 lvar_T *lvar;
1426
1427 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1428 {
1429 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1430 return -1;
1431 }
1432
1433 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1434 return -1;
1435 idx = cctx->ctx_locals.ga_len;
1436 if (cctx->ctx_max_local < idx + 1)
1437 cctx->ctx_max_local = idx + 1;
1438 ++cctx->ctx_locals.ga_len;
1439
1440 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1441 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1442 lvar->lv_const = isConst;
1443 lvar->lv_type = type;
1444
1445 return idx;
1446}
1447
1448/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001449 * Remove local variables above "new_top".
1450 */
1451 static void
1452unwind_locals(cctx_T *cctx, int new_top)
1453{
1454 if (cctx->ctx_locals.ga_len > new_top)
1455 {
1456 int idx;
1457 lvar_T *lvar;
1458
1459 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1460 {
1461 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1462 vim_free(lvar->lv_name);
1463 }
1464 }
1465 cctx->ctx_locals.ga_len = new_top;
1466}
1467
1468/*
1469 * Free all local variables.
1470 */
1471 static void
1472free_local(cctx_T *cctx)
1473{
1474 unwind_locals(cctx, 0);
1475 ga_clear(&cctx->ctx_locals);
1476}
1477
1478/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 * Skip over a type definition and return a pointer to just after it.
1480 */
1481 char_u *
1482skip_type(char_u *start)
1483{
1484 char_u *p = start;
1485
1486 while (ASCII_ISALNUM(*p) || *p == '_')
1487 ++p;
1488
1489 // Skip over "<type>"; this is permissive about white space.
1490 if (*skipwhite(p) == '<')
1491 {
1492 p = skipwhite(p);
1493 p = skip_type(skipwhite(p + 1));
1494 p = skipwhite(p);
1495 if (*p == '>')
1496 ++p;
1497 }
1498 return p;
1499}
1500
1501/*
1502 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001503 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 * Returns NULL in case of failure.
1505 */
1506 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001507parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508{
1509 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001510 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511
1512 if (**arg != '<')
1513 {
1514 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001515 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516 else
1517 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001518 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 }
1520 *arg = skipwhite(*arg + 1);
1521
Bram Moolenaard77a8522020-04-03 21:59:57 +02001522 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523
1524 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001525 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 {
1527 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001528 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529 }
1530 ++*arg;
1531
1532 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001533 return get_list_type(member_type, type_gap);
1534 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535}
1536
1537/*
1538 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001539 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 */
1541 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001542parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543{
1544 char_u *p = *arg;
1545 size_t len;
1546
1547 // skip over the first word
1548 while (ASCII_ISALNUM(*p) || *p == '_')
1549 ++p;
1550 len = p - *arg;
1551
1552 switch (**arg)
1553 {
1554 case 'a':
1555 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1556 {
1557 *arg += len;
1558 return &t_any;
1559 }
1560 break;
1561 case 'b':
1562 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1563 {
1564 *arg += len;
1565 return &t_bool;
1566 }
1567 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1568 {
1569 *arg += len;
1570 return &t_blob;
1571 }
1572 break;
1573 case 'c':
1574 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1575 {
1576 *arg += len;
1577 return &t_channel;
1578 }
1579 break;
1580 case 'd':
1581 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1582 {
1583 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001584 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 }
1586 break;
1587 case 'f':
1588 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1589 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001590#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 *arg += len;
1592 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001593#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001594 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001595 return &t_any;
1596#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 }
1598 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1599 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001600 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001601 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001602 int argcount = -1;
1603 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001604 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001605 type_T *arg_type[MAX_FUNC_ARGS + 1];
1606
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001607 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001609 if (**arg == '(')
1610 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001611 // "func" may or may not return a value, "func()" does
1612 // not return a value.
1613 ret_type = &t_void;
1614
Bram Moolenaard77a8522020-04-03 21:59:57 +02001615 p = ++*arg;
1616 argcount = 0;
1617 while (*p != NUL && *p != ')')
1618 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001619 if (*p == '?')
1620 {
1621 if (first_optional == -1)
1622 first_optional = argcount;
1623 ++p;
1624 }
1625 else if (first_optional != -1)
1626 {
1627 emsg(_("E1007: mandatory argument after optional argument"));
1628 return &t_any;
1629 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001630 else if (STRNCMP(p, "...", 3) == 0)
1631 {
1632 flags |= TTFLAG_VARARGS;
1633 p += 3;
1634 }
1635
1636 arg_type[argcount++] = parse_type(&p, type_gap);
1637
1638 // Nothing comes after "...{type}".
1639 if (flags & TTFLAG_VARARGS)
1640 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001641
Bram Moolenaard77a8522020-04-03 21:59:57 +02001642 if (*p != ',' && *skipwhite(p) == ',')
1643 {
1644 semsg(_(e_no_white_before), ",");
1645 return &t_any;
1646 }
1647 if (*p == ',')
1648 {
1649 ++p;
1650 if (!VIM_ISWHITE(*p))
1651 semsg(_(e_white_after), ",");
1652 }
1653 p = skipwhite(p);
1654 if (argcount == MAX_FUNC_ARGS)
1655 {
1656 emsg(_("E740: Too many argument types"));
1657 return &t_any;
1658 }
1659 }
1660
1661 p = skipwhite(p);
1662 if (*p != ')')
1663 {
1664 emsg(_(e_missing_close));
1665 return &t_any;
1666 }
1667 *arg = p + 1;
1668 }
1669 if (**arg == ':')
1670 {
1671 // parse return type
1672 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001673 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001674 semsg(_(e_white_after), ":");
1675 *arg = skipwhite(*arg);
1676 ret_type = parse_type(arg, type_gap);
1677 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001678 if (flags == 0 && first_optional == -1)
1679 type = get_func_type(ret_type, argcount, type_gap);
1680 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001681 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001682 type = alloc_func_type(ret_type, argcount, type_gap);
1683 type->tt_flags = flags;
1684 if (argcount > 0)
1685 {
1686 type->tt_argcount = argcount;
1687 type->tt_min_argcount = first_optional == -1
1688 ? argcount : first_optional;
1689 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001690 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001691 return &t_any;
1692 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001693 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001694 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001695 }
1696 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 }
1698 break;
1699 case 'j':
1700 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1701 {
1702 *arg += len;
1703 return &t_job;
1704 }
1705 break;
1706 case 'l':
1707 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1708 {
1709 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001710 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 }
1712 break;
1713 case 'n':
1714 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1715 {
1716 *arg += len;
1717 return &t_number;
1718 }
1719 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 case 's':
1721 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1722 {
1723 *arg += len;
1724 return &t_string;
1725 }
1726 break;
1727 case 'v':
1728 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1729 {
1730 *arg += len;
1731 return &t_void;
1732 }
1733 break;
1734 }
1735
1736 semsg(_("E1010: Type not recognized: %s"), *arg);
1737 return &t_any;
1738}
1739
1740/*
1741 * Check if "type1" and "type2" are exactly the same.
1742 */
1743 static int
1744equal_type(type_T *type1, type_T *type2)
1745{
1746 if (type1->tt_type != type2->tt_type)
1747 return FALSE;
1748 switch (type1->tt_type)
1749 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001751 case VAR_ANY:
1752 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 case VAR_SPECIAL:
1754 case VAR_BOOL:
1755 case VAR_NUMBER:
1756 case VAR_FLOAT:
1757 case VAR_STRING:
1758 case VAR_BLOB:
1759 case VAR_JOB:
1760 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001761 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 case VAR_LIST:
1763 case VAR_DICT:
1764 return equal_type(type1->tt_member, type2->tt_member);
1765 case VAR_FUNC:
1766 case VAR_PARTIAL:
1767 // TODO; check argument types.
1768 return equal_type(type1->tt_member, type2->tt_member)
1769 && type1->tt_argcount == type2->tt_argcount;
1770 }
1771 return TRUE;
1772}
1773
1774/*
1775 * Find the common type of "type1" and "type2" and put it in "dest".
1776 * "type2" and "dest" may be the same.
1777 */
1778 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02001779common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780{
1781 if (equal_type(type1, type2))
1782 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001783 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 return;
1785 }
1786
1787 if (type1->tt_type == type2->tt_type)
1788 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1790 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001791 type_T *common;
1792
Bram Moolenaard77a8522020-04-03 21:59:57 +02001793 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001794 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001795 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001796 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001797 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 return;
1799 }
1800 // TODO: VAR_FUNC and VAR_PARTIAL
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001801 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 }
1803
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001804 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805}
1806
1807 char *
1808vartype_name(vartype_T type)
1809{
1810 switch (type)
1811 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001812 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02001813 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 case VAR_SPECIAL: return "special";
1816 case VAR_BOOL: return "bool";
1817 case VAR_NUMBER: return "number";
1818 case VAR_FLOAT: return "float";
1819 case VAR_STRING: return "string";
1820 case VAR_BLOB: return "blob";
1821 case VAR_JOB: return "job";
1822 case VAR_CHANNEL: return "channel";
1823 case VAR_LIST: return "list";
1824 case VAR_DICT: return "dict";
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001825 case VAR_FUNC: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 case VAR_PARTIAL: return "partial";
1827 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02001828 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829}
1830
1831/*
1832 * Return the name of a type.
1833 * The result may be in allocated memory, in which case "tofree" is set.
1834 */
1835 char *
1836type_name(type_T *type, char **tofree)
1837{
1838 char *name = vartype_name(type->tt_type);
1839
1840 *tofree = NULL;
1841 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1842 {
1843 char *member_free;
1844 char *member_name = type_name(type->tt_member, &member_free);
1845 size_t len;
1846
1847 len = STRLEN(name) + STRLEN(member_name) + 3;
1848 *tofree = alloc(len);
1849 if (*tofree != NULL)
1850 {
1851 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1852 vim_free(member_free);
1853 return *tofree;
1854 }
1855 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001856 if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1857 {
1858 garray_T ga;
1859 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001860 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001861
1862 ga_init2(&ga, 1, 100);
1863 if (ga_grow(&ga, 20) == FAIL)
1864 return "[unknown]";
1865 *tofree = ga.ga_data;
1866 STRCPY(ga.ga_data, "func(");
1867 ga.ga_len += 5;
1868
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001869 for (i = 0; i < type->tt_argcount + varargs; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001870 {
1871 char *arg_free;
1872 char *arg_type = type_name(type->tt_args[i], &arg_free);
1873 int len;
1874
1875 if (i > 0)
1876 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001877 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001878 ga.ga_len += 2;
1879 }
1880 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001881 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001882 {
1883 vim_free(arg_free);
1884 return "[unknown]";
1885 }
1886 *tofree = ga.ga_data;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001887 if (i == type->tt_argcount)
1888 {
1889 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
1890 ga.ga_len += 3;
1891 }
1892 else if (i >= type->tt_min_argcount)
1893 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001894 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001895 ga.ga_len += len;
1896 vim_free(arg_free);
1897 }
1898
1899 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001900 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001901 else
1902 {
1903 char *ret_free;
1904 char *ret_name = type_name(type->tt_member, &ret_free);
1905 int len;
1906
1907 len = (int)STRLEN(ret_name) + 4;
1908 if (ga_grow(&ga, len) == FAIL)
1909 {
1910 vim_free(ret_free);
1911 return "[unknown]";
1912 }
1913 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001914 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
1915 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001916 vim_free(ret_free);
1917 }
1918 return ga.ga_data;
1919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920
1921 return name;
1922}
1923
1924/*
1925 * Find "name" in script-local items of script "sid".
1926 * Returns the index in "sn_var_vals" if found.
1927 * If found but not in "sn_var_vals" returns -1.
1928 * If not found returns -2.
1929 */
1930 int
1931get_script_item_idx(int sid, char_u *name, int check_writable)
1932{
1933 hashtab_T *ht;
1934 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001935 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936 int idx;
1937
1938 // First look the name up in the hashtable.
1939 if (sid <= 0 || sid > script_items.ga_len)
1940 return -1;
1941 ht = &SCRIPT_VARS(sid);
1942 di = find_var_in_ht(ht, 0, name, TRUE);
1943 if (di == NULL)
1944 return -2;
1945
1946 // Now find the svar_T index in sn_var_vals.
1947 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1948 {
1949 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1950
1951 if (sv->sv_tv == &di->di_tv)
1952 {
1953 if (check_writable && sv->sv_const)
1954 semsg(_(e_readonlyvar), name);
1955 return idx;
1956 }
1957 }
1958 return -1;
1959}
1960
1961/*
1962 * Find "name" in imported items of the current script/
1963 */
1964 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001965find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001967 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 int idx;
1969
1970 if (cctx != NULL)
1971 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1972 {
1973 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1974 + idx;
1975
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001976 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1977 : STRLEN(import->imp_name) == len
1978 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 return import;
1980 }
1981
1982 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1983 {
1984 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1985
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001986 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1987 : STRLEN(import->imp_name) == len
1988 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 return import;
1990 }
1991 return NULL;
1992}
1993
1994/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001995 * Free all imported variables.
1996 */
1997 static void
1998free_imported(cctx_T *cctx)
1999{
2000 int idx;
2001
2002 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2003 {
2004 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2005
2006 vim_free(import->imp_name);
2007 }
2008 ga_clear(&cctx->ctx_imports);
2009}
2010
2011/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002012 * Generate an instruction to load script-local variable "name", without the
2013 * leading "s:".
2014 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 */
2016 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002017compile_load_scriptvar(
2018 cctx_T *cctx,
2019 char_u *name, // variable NUL terminated
2020 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002021 char_u **end, // end of variable
2022 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002024 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2026 imported_T *import;
2027
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002028 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002030 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002031 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2032 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 }
2034 if (idx >= 0)
2035 {
2036 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2037
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002038 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 current_sctx.sc_sid, idx, sv->sv_type);
2040 return OK;
2041 }
2042
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002043 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 if (import != NULL)
2045 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002046 if (import->imp_all)
2047 {
2048 char_u *p = skipwhite(*end);
2049 int name_len;
2050 ufunc_T *ufunc;
2051 type_T *type;
2052
2053 // Used "import * as Name", need to lookup the member.
2054 if (*p != '.')
2055 {
2056 semsg(_("E1060: expected dot after name: %s"), start);
2057 return FAIL;
2058 }
2059 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002060 if (VIM_ISWHITE(*p))
2061 {
2062 emsg(_("E1074: no white space allowed after dot"));
2063 return FAIL;
2064 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002065
2066 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2067 // TODO: what if it is a function?
2068 if (idx < 0)
2069 return FAIL;
2070 *end = p;
2071
2072 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2073 import->imp_sid,
2074 idx,
2075 type);
2076 }
2077 else
2078 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002079 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002080 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2081 import->imp_sid,
2082 import->imp_var_vals_idx,
2083 import->imp_type);
2084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 return OK;
2086 }
2087
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002088 if (error)
2089 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 return FAIL;
2091}
2092
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002093 static int
2094generate_funcref(cctx_T *cctx, char_u *name)
2095{
2096 ufunc_T *ufunc = find_func(name, cctx);
2097
2098 if (ufunc == NULL)
2099 return FAIL;
2100
2101 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2102}
2103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104/*
2105 * Compile a variable name into a load instruction.
2106 * "end" points to just after the name.
2107 * When "error" is FALSE do not give an error when not found.
2108 */
2109 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002110compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111{
2112 type_T *type;
2113 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002114 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002116 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117
2118 if (*(*arg + 1) == ':')
2119 {
2120 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002121 if (end <= *arg + 2)
2122 name = vim_strsave((char_u *)"[empty]");
2123 else
2124 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 if (name == NULL)
2126 return FAIL;
2127
2128 if (**arg == 'v')
2129 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002130 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 }
2132 else if (**arg == 'g')
2133 {
2134 // Global variables can be defined later, thus we don't check if it
2135 // exists, give error at runtime.
2136 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2137 }
2138 else if (**arg == 's')
2139 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002140 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002142 else if (**arg == 'b')
2143 {
2144 semsg("Namespace b: not supported yet: %s", *arg);
2145 goto theend;
2146 }
2147 else if (**arg == 'w')
2148 {
2149 semsg("Namespace w: not supported yet: %s", *arg);
2150 goto theend;
2151 }
2152 else if (**arg == 't')
2153 {
2154 semsg("Namespace t: not supported yet: %s", *arg);
2155 goto theend;
2156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 else
2158 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002159 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 goto theend;
2161 }
2162 }
2163 else
2164 {
2165 size_t len = end - *arg;
2166 int idx;
2167 int gen_load = FALSE;
2168
2169 name = vim_strnsave(*arg, end - *arg);
2170 if (name == NULL)
2171 return FAIL;
2172
2173 idx = lookup_arg(*arg, len, cctx);
2174 if (idx >= 0)
2175 {
2176 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2177 type = cctx->ctx_ufunc->uf_arg_types[idx];
2178 else
2179 type = &t_any;
2180
2181 // Arguments are located above the frame pointer.
2182 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2183 if (cctx->ctx_ufunc->uf_va_name != NULL)
2184 --idx;
2185 gen_load = TRUE;
2186 }
2187 else if (lookup_vararg(*arg, len, cctx))
2188 {
2189 // varargs is always the last argument
2190 idx = -STACK_FRAME_SIZE - 1;
2191 type = cctx->ctx_ufunc->uf_va_type;
2192 gen_load = TRUE;
2193 }
2194 else
2195 {
2196 idx = lookup_local(*arg, len, cctx);
2197 if (idx >= 0)
2198 {
2199 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
2200 gen_load = TRUE;
2201 }
2202 else
2203 {
2204 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2205 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2206 res = generate_PUSHBOOL(cctx, **arg == 't'
2207 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002208 else
2209 {
2210 // "var" can be script-local even without using "s:" if it
2211 // already exists.
2212 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2213 == SCRIPT_VERSION_VIM9
2214 || lookup_script(*arg, len) == OK)
2215 res = compile_load_scriptvar(cctx, name, *arg, &end,
2216 FALSE);
2217
2218 // When the name starts with an uppercase letter or "x:" it
2219 // can be a user defined function.
2220 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2221 res = generate_funcref(cctx, name);
2222 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223 }
2224 }
2225 if (gen_load)
2226 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
2227 }
2228
2229 *arg = end;
2230
2231theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002232 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 semsg(_(e_var_notfound), name);
2234 vim_free(name);
2235 return res;
2236}
2237
2238/*
2239 * Compile the argument expressions.
2240 * "arg" points to just after the "(" and is advanced to after the ")"
2241 */
2242 static int
2243compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2244{
2245 char_u *p = *arg;
2246
2247 while (*p != NUL && *p != ')')
2248 {
2249 if (compile_expr1(&p, cctx) == FAIL)
2250 return FAIL;
2251 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002252
2253 if (*p != ',' && *skipwhite(p) == ',')
2254 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002255 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002256 p = skipwhite(p);
2257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002259 {
2260 ++p;
2261 if (!VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002262 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002263 }
2264 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002266 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 if (*p != ')')
2268 {
2269 emsg(_(e_missing_close));
2270 return FAIL;
2271 }
2272 *arg = p + 1;
2273 return OK;
2274}
2275
2276/*
2277 * Compile a function call: name(arg1, arg2)
2278 * "arg" points to "name", "arg + varlen" to the "(".
2279 * "argcount_init" is 1 for "value->method()"
2280 * Instructions:
2281 * EVAL arg1
2282 * EVAL arg2
2283 * BCALL / DCALL / UCALL
2284 */
2285 static int
2286compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2287{
2288 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002289 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 int argcount = argcount_init;
2291 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002292 char_u fname_buf[FLEN_FIXED + 1];
2293 char_u *tofree = NULL;
2294 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002296 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297
2298 if (varlen >= sizeof(namebuf))
2299 {
2300 semsg(_("E1011: name too long: %s"), name);
2301 return FAIL;
2302 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002303 vim_strncpy(namebuf, *arg, varlen);
2304 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305
2306 *arg = skipwhite(*arg + varlen + 1);
2307 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002308 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002310 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 {
2312 int idx;
2313
2314 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002315 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002317 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002318 else
2319 semsg(_(e_unknownfunc), namebuf);
2320 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 }
2322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002324 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002326 {
2327 res = generate_CALL(cctx, ufunc, argcount);
2328 goto theend;
2329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330
2331 // If the name is a variable, load it and use PCALL.
2332 p = namebuf;
2333 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002334 {
2335 res = generate_PCALL(cctx, argcount, FALSE);
2336 goto theend;
2337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338
2339 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002340 res = generate_UCALL(cctx, name, argcount);
2341
2342theend:
2343 vim_free(tofree);
2344 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345}
2346
2347// like NAMESPACE_CHAR but with 'a' and 'l'.
2348#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2349
2350/*
2351 * Find the end of a variable or function name. Unlike find_name_end() this
2352 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002353 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 * Return a pointer to just after the name. Equal to "arg" if there is no
2355 * valid name.
2356 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002357 static char_u *
2358to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359{
2360 char_u *p;
2361
2362 // Quick check for valid starting character.
2363 if (!eval_isnamec1(*arg))
2364 return arg;
2365
2366 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2367 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2368 // and can be used in slice "[n:]".
2369 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002370 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2372 break;
2373 return p;
2374}
2375
2376/*
2377 * Like to_name_end() but also skip over a list or dict constant.
2378 */
2379 char_u *
2380to_name_const_end(char_u *arg)
2381{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002382 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 typval_T rettv;
2384
2385 if (p == arg && *arg == '[')
2386 {
2387
2388 // Can be "[1, 2, 3]->Func()".
2389 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2390 p = arg;
2391 }
2392 else if (p == arg && *arg == '#' && arg[1] == '{')
2393 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002394 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 ++p;
2396 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2397 p = arg;
2398 }
2399 else if (p == arg && *arg == '{')
2400 {
2401 int ret = get_lambda_tv(&p, &rettv, FALSE);
2402
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002403 // Can be "{x -> ret}()".
2404 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 if (ret == NOTDONE)
2406 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2407 if (ret != OK)
2408 p = arg;
2409 }
2410
2411 return p;
2412}
2413
2414 static void
2415type_mismatch(type_T *expected, type_T *actual)
2416{
2417 char *tofree1, *tofree2;
2418
2419 semsg(_("E1013: type mismatch, expected %s but got %s"),
2420 type_name(expected, &tofree1), type_name(actual, &tofree2));
2421 vim_free(tofree1);
2422 vim_free(tofree2);
2423}
2424
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002425 static void
2426arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
2427{
2428 char *tofree1, *tofree2;
2429
2430 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
2431 argidx,
2432 type_name(expected, &tofree1), type_name(actual, &tofree2));
2433 vim_free(tofree1);
2434 vim_free(tofree2);
2435}
2436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437/*
2438 * Check if the expected and actual types match.
2439 */
2440 static int
2441check_type(type_T *expected, type_T *actual, int give_msg)
2442{
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002443 int ret = OK;
2444
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002445 // When expected is "unknown" we accept any actual type.
2446 // When expected is "any" we accept any actual type except "void".
2447 if (expected->tt_type != VAR_UNKNOWN
2448 && (expected->tt_type != VAR_ANY || actual->tt_type == VAR_VOID))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 {
2450 if (expected->tt_type != actual->tt_type)
2451 {
2452 if (give_msg)
2453 type_mismatch(expected, actual);
2454 return FAIL;
2455 }
2456 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2457 {
Bram Moolenaar4c683752020-04-05 21:38:23 +02002458 // "unknown" is used for an empty list or dict
2459 if (actual->tt_member != &t_unknown)
Bram Moolenaar436472f2020-02-20 22:54:43 +01002460 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002462 else if (expected->tt_type == VAR_FUNC)
2463 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002464 if (expected->tt_member != &t_unknown)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002465 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
2466 if (ret == OK && expected->tt_argcount != -1
2467 && (actual->tt_argcount < expected->tt_min_argcount
2468 || actual->tt_argcount > expected->tt_argcount))
2469 ret = FAIL;
2470 }
2471 if (ret == FAIL && give_msg)
2472 type_mismatch(expected, actual);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 }
Bram Moolenaar89228602020-04-05 22:14:54 +02002474 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475}
2476
2477/*
2478 * Check that
2479 * - "actual" is "expected" type or
2480 * - "actual" is a type that can be "expected" type: add a runtime check; or
2481 * - return FAIL.
2482 */
2483 static int
2484need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2485{
Bram Moolenaar89228602020-04-05 22:14:54 +02002486 if (check_type(expected, actual, FALSE) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 return OK;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002488 if (actual->tt_type != VAR_ANY && actual->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 {
2490 type_mismatch(expected, actual);
2491 return FAIL;
2492 }
2493 generate_TYPECHECK(cctx, expected, offset);
2494 return OK;
2495}
2496
2497/*
2498 * parse a list: [expr, expr]
2499 * "*arg" points to the '['.
2500 */
2501 static int
2502compile_list(char_u **arg, cctx_T *cctx)
2503{
2504 char_u *p = skipwhite(*arg + 1);
2505 int count = 0;
2506
2507 while (*p != ']')
2508 {
2509 if (*p == NUL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002510 {
2511 semsg(_(e_list_end), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 return FAIL;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 if (compile_expr1(&p, cctx) == FAIL)
2515 break;
2516 ++count;
2517 if (*p == ',')
2518 ++p;
2519 p = skipwhite(p);
2520 }
2521 *arg = p + 1;
2522
2523 generate_NEWLIST(cctx, count);
2524 return OK;
2525}
2526
2527/*
2528 * parse a lambda: {arg, arg -> expr}
2529 * "*arg" points to the '{'.
2530 */
2531 static int
2532compile_lambda(char_u **arg, cctx_T *cctx)
2533{
2534 garray_T *instr = &cctx->ctx_instr;
2535 typval_T rettv;
2536 ufunc_T *ufunc;
2537
2538 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002539 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002543 ++ufunc->uf_refcount;
2544 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002545 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546
2547 // The function will have one line: "return {expr}".
2548 // Compile it into instructions.
2549 compile_def_function(ufunc, TRUE);
2550
2551 if (ufunc->uf_dfunc_idx >= 0)
2552 {
2553 if (ga_grow(instr, 1) == FAIL)
2554 return FAIL;
2555 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2556 return OK;
2557 }
2558 return FAIL;
2559}
2560
2561/*
2562 * Compile a lamda call: expr->{lambda}(args)
2563 * "arg" points to the "{".
2564 */
2565 static int
2566compile_lambda_call(char_u **arg, cctx_T *cctx)
2567{
2568 ufunc_T *ufunc;
2569 typval_T rettv;
2570 int argcount = 1;
2571 int ret = FAIL;
2572
2573 // Get the funcref in "rettv".
2574 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2575 return FAIL;
2576
2577 if (**arg != '(')
2578 {
2579 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002580 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 else
2582 semsg(_(e_missing_paren), "lambda");
2583 clear_tv(&rettv);
2584 return FAIL;
2585 }
2586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 ufunc = rettv.vval.v_partial->pt_func;
2588 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002589 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002590 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002591
2592 // The function will have one line: "return {expr}".
2593 // Compile it into instructions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 compile_def_function(ufunc, TRUE);
2595
2596 // compile the arguments
2597 *arg = skipwhite(*arg + 1);
2598 if (compile_arguments(arg, cctx, &argcount) == OK)
2599 // call the compiled function
2600 ret = generate_CALL(cctx, ufunc, argcount);
2601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 return ret;
2603}
2604
2605/*
2606 * parse a dict: {'key': val} or #{key: val}
2607 * "*arg" points to the '{'.
2608 */
2609 static int
2610compile_dict(char_u **arg, cctx_T *cctx, int literal)
2611{
2612 garray_T *instr = &cctx->ctx_instr;
2613 int count = 0;
2614 dict_T *d = dict_alloc();
2615 dictitem_T *item;
2616
2617 if (d == NULL)
2618 return FAIL;
2619 *arg = skipwhite(*arg + 1);
2620 while (**arg != '}' && **arg != NUL)
2621 {
2622 char_u *key = NULL;
2623
2624 if (literal)
2625 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002626 char_u *p = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627
2628 if (p == *arg)
2629 {
2630 semsg(_("E1014: Invalid key: %s"), *arg);
2631 return FAIL;
2632 }
2633 key = vim_strnsave(*arg, p - *arg);
2634 if (generate_PUSHS(cctx, key) == FAIL)
2635 return FAIL;
2636 *arg = p;
2637 }
2638 else
2639 {
2640 isn_T *isn;
2641
2642 if (compile_expr1(arg, cctx) == FAIL)
2643 return FAIL;
2644 // TODO: check type is string
2645 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2646 if (isn->isn_type == ISN_PUSHS)
2647 key = isn->isn_arg.string;
2648 }
2649
2650 // Check for duplicate keys, if using string keys.
2651 if (key != NULL)
2652 {
2653 item = dict_find(d, key, -1);
2654 if (item != NULL)
2655 {
2656 semsg(_(e_duplicate_key), key);
2657 goto failret;
2658 }
2659 item = dictitem_alloc(key);
2660 if (item != NULL)
2661 {
2662 item->di_tv.v_type = VAR_UNKNOWN;
2663 item->di_tv.v_lock = 0;
2664 if (dict_add(d, item) == FAIL)
2665 dictitem_free(item);
2666 }
2667 }
2668
2669 *arg = skipwhite(*arg);
2670 if (**arg != ':')
2671 {
2672 semsg(_(e_missing_dict_colon), *arg);
2673 return FAIL;
2674 }
2675
2676 *arg = skipwhite(*arg + 1);
2677 if (compile_expr1(arg, cctx) == FAIL)
2678 return FAIL;
2679 ++count;
2680
2681 if (**arg == '}')
2682 break;
2683 if (**arg != ',')
2684 {
2685 semsg(_(e_missing_dict_comma), *arg);
2686 goto failret;
2687 }
2688 *arg = skipwhite(*arg + 1);
2689 }
2690
2691 if (**arg != '}')
2692 {
2693 semsg(_(e_missing_dict_end), *arg);
2694 goto failret;
2695 }
2696 *arg = *arg + 1;
2697
2698 dict_unref(d);
2699 return generate_NEWDICT(cctx, count);
2700
2701failret:
2702 dict_unref(d);
2703 return FAIL;
2704}
2705
2706/*
2707 * Compile "&option".
2708 */
2709 static int
2710compile_get_option(char_u **arg, cctx_T *cctx)
2711{
2712 typval_T rettv;
2713 char_u *start = *arg;
2714 int ret;
2715
2716 // parse the option and get the current value to get the type.
2717 rettv.v_type = VAR_UNKNOWN;
2718 ret = get_option_tv(arg, &rettv, TRUE);
2719 if (ret == OK)
2720 {
2721 // include the '&' in the name, get_option_tv() expects it.
2722 char_u *name = vim_strnsave(start, *arg - start);
2723 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2724
2725 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2726 vim_free(name);
2727 }
2728 clear_tv(&rettv);
2729
2730 return ret;
2731}
2732
2733/*
2734 * Compile "$VAR".
2735 */
2736 static int
2737compile_get_env(char_u **arg, cctx_T *cctx)
2738{
2739 char_u *start = *arg;
2740 int len;
2741 int ret;
2742 char_u *name;
2743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744 ++*arg;
2745 len = get_env_len(arg);
2746 if (len == 0)
2747 {
2748 semsg(_(e_syntax_at), start - 1);
2749 return FAIL;
2750 }
2751
2752 // include the '$' in the name, get_env_tv() expects it.
2753 name = vim_strnsave(start, len + 1);
2754 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2755 vim_free(name);
2756 return ret;
2757}
2758
2759/*
2760 * Compile "@r".
2761 */
2762 static int
2763compile_get_register(char_u **arg, cctx_T *cctx)
2764{
2765 int ret;
2766
2767 ++*arg;
2768 if (**arg == NUL)
2769 {
2770 semsg(_(e_syntax_at), *arg - 1);
2771 return FAIL;
2772 }
2773 if (!valid_yank_reg(**arg, TRUE))
2774 {
2775 emsg_invreg(**arg);
2776 return FAIL;
2777 }
2778 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2779 ++*arg;
2780 return ret;
2781}
2782
2783/*
2784 * Apply leading '!', '-' and '+' to constant "rettv".
2785 */
2786 static int
2787apply_leader(typval_T *rettv, char_u *start, char_u *end)
2788{
2789 char_u *p = end;
2790
2791 // this works from end to start
2792 while (p > start)
2793 {
2794 --p;
2795 if (*p == '-' || *p == '+')
2796 {
2797 // only '-' has an effect, for '+' we only check the type
2798#ifdef FEAT_FLOAT
2799 if (rettv->v_type == VAR_FLOAT)
2800 {
2801 if (*p == '-')
2802 rettv->vval.v_float = -rettv->vval.v_float;
2803 }
2804 else
2805#endif
2806 {
2807 varnumber_T val;
2808 int error = FALSE;
2809
2810 // tv_get_number_chk() accepts a string, but we don't want that
2811 // here
2812 if (check_not_string(rettv) == FAIL)
2813 return FAIL;
2814 val = tv_get_number_chk(rettv, &error);
2815 clear_tv(rettv);
2816 if (error)
2817 return FAIL;
2818 if (*p == '-')
2819 val = -val;
2820 rettv->v_type = VAR_NUMBER;
2821 rettv->vval.v_number = val;
2822 }
2823 }
2824 else
2825 {
2826 int v = tv2bool(rettv);
2827
2828 // '!' is permissive in the type.
2829 clear_tv(rettv);
2830 rettv->v_type = VAR_BOOL;
2831 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2832 }
2833 }
2834 return OK;
2835}
2836
2837/*
2838 * Recognize v: variables that are constants and set "rettv".
2839 */
2840 static void
2841get_vim_constant(char_u **arg, typval_T *rettv)
2842{
2843 if (STRNCMP(*arg, "v:true", 6) == 0)
2844 {
2845 rettv->v_type = VAR_BOOL;
2846 rettv->vval.v_number = VVAL_TRUE;
2847 *arg += 6;
2848 }
2849 else if (STRNCMP(*arg, "v:false", 7) == 0)
2850 {
2851 rettv->v_type = VAR_BOOL;
2852 rettv->vval.v_number = VVAL_FALSE;
2853 *arg += 7;
2854 }
2855 else if (STRNCMP(*arg, "v:null", 6) == 0)
2856 {
2857 rettv->v_type = VAR_SPECIAL;
2858 rettv->vval.v_number = VVAL_NULL;
2859 *arg += 6;
2860 }
2861 else if (STRNCMP(*arg, "v:none", 6) == 0)
2862 {
2863 rettv->v_type = VAR_SPECIAL;
2864 rettv->vval.v_number = VVAL_NONE;
2865 *arg += 6;
2866 }
2867}
2868
2869/*
2870 * Compile code to apply '-', '+' and '!'.
2871 */
2872 static int
2873compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2874{
2875 char_u *p = end;
2876
2877 // this works from end to start
2878 while (p > start)
2879 {
2880 --p;
2881 if (*p == '-' || *p == '+')
2882 {
2883 int negate = *p == '-';
2884 isn_T *isn;
2885
2886 // TODO: check type
2887 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2888 {
2889 --p;
2890 if (*p == '-')
2891 negate = !negate;
2892 }
2893 // only '-' has an effect, for '+' we only check the type
2894 if (negate)
2895 isn = generate_instr(cctx, ISN_NEGATENR);
2896 else
2897 isn = generate_instr(cctx, ISN_CHECKNR);
2898 if (isn == NULL)
2899 return FAIL;
2900 }
2901 else
2902 {
2903 int invert = TRUE;
2904
2905 while (p > start && p[-1] == '!')
2906 {
2907 --p;
2908 invert = !invert;
2909 }
2910 if (generate_2BOOL(cctx, invert) == FAIL)
2911 return FAIL;
2912 }
2913 }
2914 return OK;
2915}
2916
2917/*
2918 * Compile whatever comes after "name" or "name()".
2919 */
2920 static int
2921compile_subscript(
2922 char_u **arg,
2923 cctx_T *cctx,
2924 char_u **start_leader,
2925 char_u *end_leader)
2926{
2927 for (;;)
2928 {
2929 if (**arg == '(')
2930 {
2931 int argcount = 0;
2932
2933 // funcref(arg)
2934 *arg = skipwhite(*arg + 1);
2935 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2936 return FAIL;
2937 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2938 return FAIL;
2939 }
2940 else if (**arg == '-' && (*arg)[1] == '>')
2941 {
2942 char_u *p;
2943
2944 // something->method()
2945 // Apply the '!', '-' and '+' first:
2946 // -1.0->func() works like (-1.0)->func()
2947 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2948 return FAIL;
2949 *start_leader = end_leader; // don't apply again later
2950
2951 *arg = skipwhite(*arg + 2);
2952 if (**arg == '{')
2953 {
2954 // lambda call: list->{lambda}
2955 if (compile_lambda_call(arg, cctx) == FAIL)
2956 return FAIL;
2957 }
2958 else
2959 {
2960 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002961 p = *arg;
2962 if (ASCII_ISALPHA(*p) && p[1] == ':')
2963 p += 2;
2964 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 ;
2966 if (*p != '(')
2967 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002968 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 return FAIL;
2970 }
2971 // TODO: base value may not be the first argument
2972 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2973 return FAIL;
2974 }
2975 }
2976 else if (**arg == '[')
2977 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002978 garray_T *stack;
2979 type_T **typep;
2980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 // list index: list[123]
2982 // TODO: more arguments
2983 // TODO: dict member dict['name']
2984 *arg = skipwhite(*arg + 1);
2985 if (compile_expr1(arg, cctx) == FAIL)
2986 return FAIL;
2987
2988 if (**arg != ']')
2989 {
2990 emsg(_(e_missbrac));
2991 return FAIL;
2992 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002993 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994
2995 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2996 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01002997 stack = &cctx->ctx_type_stack;
2998 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
2999 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3000 {
3001 emsg(_(e_listreq));
3002 return FAIL;
3003 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003004 if ((*typep)->tt_type == VAR_LIST)
3005 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 }
3007 else if (**arg == '.' && (*arg)[1] != '.')
3008 {
3009 char_u *p;
3010
3011 ++*arg;
3012 p = *arg;
3013 // dictionary member: dict.name
3014 if (eval_isnamec1(*p))
3015 while (eval_isnamec(*p))
3016 MB_PTR_ADV(p);
3017 if (p == *arg)
3018 {
3019 semsg(_(e_syntax_at), *arg);
3020 return FAIL;
3021 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3023 return FAIL;
3024 *arg = p;
3025 }
3026 else
3027 break;
3028 }
3029
3030 // TODO - see handle_subscript():
3031 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3032 // Don't do this when "Func" is already a partial that was bound
3033 // explicitly (pt_auto is FALSE).
3034
3035 return OK;
3036}
3037
3038/*
3039 * Compile an expression at "*p" and add instructions to "instr".
3040 * "p" is advanced until after the expression, skipping white space.
3041 *
3042 * This is the equivalent of eval1(), eval2(), etc.
3043 */
3044
3045/*
3046 * number number constant
3047 * 0zFFFFFFFF Blob constant
3048 * "string" string constant
3049 * 'string' literal string constant
3050 * &option-name option value
3051 * @r register contents
3052 * identifier variable value
3053 * function() function call
3054 * $VAR environment variable
3055 * (expression) nested expression
3056 * [expr, expr] List
3057 * {key: val, key: val} Dictionary
3058 * #{key: val, key: val} Dictionary with literal keys
3059 *
3060 * Also handle:
3061 * ! in front logical NOT
3062 * - in front unary minus
3063 * + in front unary plus (ignored)
3064 * trailing (arg) funcref/partial call
3065 * trailing [] subscript in String or List
3066 * trailing .name entry in Dictionary
3067 * trailing ->name() method call
3068 */
3069 static int
3070compile_expr7(char_u **arg, cctx_T *cctx)
3071{
3072 typval_T rettv;
3073 char_u *start_leader, *end_leader;
3074 int ret = OK;
3075
3076 /*
3077 * Skip '!', '-' and '+' characters. They are handled later.
3078 */
3079 start_leader = *arg;
3080 while (**arg == '!' || **arg == '-' || **arg == '+')
3081 *arg = skipwhite(*arg + 1);
3082 end_leader = *arg;
3083
3084 rettv.v_type = VAR_UNKNOWN;
3085 switch (**arg)
3086 {
3087 /*
3088 * Number constant.
3089 */
3090 case '0': // also for blob starting with 0z
3091 case '1':
3092 case '2':
3093 case '3':
3094 case '4':
3095 case '5':
3096 case '6':
3097 case '7':
3098 case '8':
3099 case '9':
3100 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3101 return FAIL;
3102 break;
3103
3104 /*
3105 * String constant: "string".
3106 */
3107 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3108 return FAIL;
3109 break;
3110
3111 /*
3112 * Literal string constant: 'str''ing'.
3113 */
3114 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3115 return FAIL;
3116 break;
3117
3118 /*
3119 * Constant Vim variable.
3120 */
3121 case 'v': get_vim_constant(arg, &rettv);
3122 ret = NOTDONE;
3123 break;
3124
3125 /*
3126 * List: [expr, expr]
3127 */
3128 case '[': ret = compile_list(arg, cctx);
3129 break;
3130
3131 /*
3132 * Dictionary: #{key: val, key: val}
3133 */
3134 case '#': if ((*arg)[1] == '{')
3135 {
3136 ++*arg;
3137 ret = compile_dict(arg, cctx, TRUE);
3138 }
3139 else
3140 ret = NOTDONE;
3141 break;
3142
3143 /*
3144 * Lambda: {arg, arg -> expr}
3145 * Dictionary: {'key': val, 'key': val}
3146 */
3147 case '{': {
3148 char_u *start = skipwhite(*arg + 1);
3149
3150 // Find out what comes after the arguments.
3151 ret = get_function_args(&start, '-', NULL,
3152 NULL, NULL, NULL, TRUE);
3153 if (ret != FAIL && *start == '>')
3154 ret = compile_lambda(arg, cctx);
3155 else
3156 ret = compile_dict(arg, cctx, FALSE);
3157 }
3158 break;
3159
3160 /*
3161 * Option value: &name
3162 */
3163 case '&': ret = compile_get_option(arg, cctx);
3164 break;
3165
3166 /*
3167 * Environment variable: $VAR.
3168 */
3169 case '$': ret = compile_get_env(arg, cctx);
3170 break;
3171
3172 /*
3173 * Register contents: @r.
3174 */
3175 case '@': ret = compile_get_register(arg, cctx);
3176 break;
3177 /*
3178 * nested expression: (expression).
3179 */
3180 case '(': *arg = skipwhite(*arg + 1);
3181 ret = compile_expr1(arg, cctx); // recursive!
3182 *arg = skipwhite(*arg);
3183 if (**arg == ')')
3184 ++*arg;
3185 else if (ret == OK)
3186 {
3187 emsg(_(e_missing_close));
3188 ret = FAIL;
3189 }
3190 break;
3191
3192 default: ret = NOTDONE;
3193 break;
3194 }
3195 if (ret == FAIL)
3196 return FAIL;
3197
3198 if (rettv.v_type != VAR_UNKNOWN)
3199 {
3200 // apply the '!', '-' and '+' before the constant
3201 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3202 {
3203 clear_tv(&rettv);
3204 return FAIL;
3205 }
3206 start_leader = end_leader; // don't apply again below
3207
3208 // push constant
3209 switch (rettv.v_type)
3210 {
3211 case VAR_BOOL:
3212 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3213 break;
3214 case VAR_SPECIAL:
3215 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3216 break;
3217 case VAR_NUMBER:
3218 generate_PUSHNR(cctx, rettv.vval.v_number);
3219 break;
3220#ifdef FEAT_FLOAT
3221 case VAR_FLOAT:
3222 generate_PUSHF(cctx, rettv.vval.v_float);
3223 break;
3224#endif
3225 case VAR_BLOB:
3226 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3227 rettv.vval.v_blob = NULL;
3228 break;
3229 case VAR_STRING:
3230 generate_PUSHS(cctx, rettv.vval.v_string);
3231 rettv.vval.v_string = NULL;
3232 break;
3233 default:
3234 iemsg("constant type missing");
3235 return FAIL;
3236 }
3237 }
3238 else if (ret == NOTDONE)
3239 {
3240 char_u *p;
3241 int r;
3242
3243 if (!eval_isnamec1(**arg))
3244 {
3245 semsg(_("E1015: Name expected: %s"), *arg);
3246 return FAIL;
3247 }
3248
3249 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003250 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 if (*p == '(')
3252 r = compile_call(arg, p - *arg, cctx, 0);
3253 else
3254 r = compile_load(arg, p, cctx, TRUE);
3255 if (r == FAIL)
3256 return FAIL;
3257 }
3258
3259 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3260 return FAIL;
3261
3262 // Now deal with prefixed '-', '+' and '!', if not done already.
3263 return compile_leader(cctx, start_leader, end_leader);
3264}
3265
3266/*
3267 * * number multiplication
3268 * / number division
3269 * % number modulo
3270 */
3271 static int
3272compile_expr6(char_u **arg, cctx_T *cctx)
3273{
3274 char_u *op;
3275
3276 // get the first variable
3277 if (compile_expr7(arg, cctx) == FAIL)
3278 return FAIL;
3279
3280 /*
3281 * Repeat computing, until no "*", "/" or "%" is following.
3282 */
3283 for (;;)
3284 {
3285 op = skipwhite(*arg);
3286 if (*op != '*' && *op != '/' && *op != '%')
3287 break;
3288 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
3289 {
3290 char_u buf[3];
3291
3292 vim_strncpy(buf, op, 1);
3293 semsg(_(e_white_both), buf);
3294 }
3295 *arg = skipwhite(op + 1);
3296
3297 // get the second variable
3298 if (compile_expr7(arg, cctx) == FAIL)
3299 return FAIL;
3300
3301 generate_two_op(cctx, op);
3302 }
3303
3304 return OK;
3305}
3306
3307/*
3308 * + number addition
3309 * - number subtraction
3310 * .. string concatenation
3311 */
3312 static int
3313compile_expr5(char_u **arg, cctx_T *cctx)
3314{
3315 char_u *op;
3316 int oplen;
3317
3318 // get the first variable
3319 if (compile_expr6(arg, cctx) == FAIL)
3320 return FAIL;
3321
3322 /*
3323 * Repeat computing, until no "+", "-" or ".." is following.
3324 */
3325 for (;;)
3326 {
3327 op = skipwhite(*arg);
3328 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3329 break;
3330 oplen = (*op == '.' ? 2 : 1);
3331
3332 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
3333 {
3334 char_u buf[3];
3335
3336 vim_strncpy(buf, op, oplen);
3337 semsg(_(e_white_both), buf);
3338 }
3339
3340 *arg = skipwhite(op + oplen);
3341
3342 // get the second variable
3343 if (compile_expr6(arg, cctx) == FAIL)
3344 return FAIL;
3345
3346 if (*op == '.')
3347 {
3348 if (may_generate_2STRING(-2, cctx) == FAIL
3349 || may_generate_2STRING(-1, cctx) == FAIL)
3350 return FAIL;
3351 generate_instr_drop(cctx, ISN_CONCAT, 1);
3352 }
3353 else
3354 generate_two_op(cctx, op);
3355 }
3356
3357 return OK;
3358}
3359
Bram Moolenaar080457c2020-03-03 21:53:32 +01003360 static exptype_T
3361get_compare_type(char_u *p, int *len, int *type_is)
3362{
3363 exptype_T type = EXPR_UNKNOWN;
3364 int i;
3365
3366 switch (p[0])
3367 {
3368 case '=': if (p[1] == '=')
3369 type = EXPR_EQUAL;
3370 else if (p[1] == '~')
3371 type = EXPR_MATCH;
3372 break;
3373 case '!': if (p[1] == '=')
3374 type = EXPR_NEQUAL;
3375 else if (p[1] == '~')
3376 type = EXPR_NOMATCH;
3377 break;
3378 case '>': if (p[1] != '=')
3379 {
3380 type = EXPR_GREATER;
3381 *len = 1;
3382 }
3383 else
3384 type = EXPR_GEQUAL;
3385 break;
3386 case '<': if (p[1] != '=')
3387 {
3388 type = EXPR_SMALLER;
3389 *len = 1;
3390 }
3391 else
3392 type = EXPR_SEQUAL;
3393 break;
3394 case 'i': if (p[1] == 's')
3395 {
3396 // "is" and "isnot"; but not a prefix of a name
3397 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3398 *len = 5;
3399 i = p[*len];
3400 if (!isalnum(i) && i != '_')
3401 {
3402 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3403 *type_is = TRUE;
3404 }
3405 }
3406 break;
3407 }
3408 return type;
3409}
3410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411/*
3412 * expr5a == expr5b
3413 * expr5a =~ expr5b
3414 * expr5a != expr5b
3415 * expr5a !~ expr5b
3416 * expr5a > expr5b
3417 * expr5a >= expr5b
3418 * expr5a < expr5b
3419 * expr5a <= expr5b
3420 * expr5a is expr5b
3421 * expr5a isnot expr5b
3422 *
3423 * Produces instructions:
3424 * EVAL expr5a Push result of "expr5a"
3425 * EVAL expr5b Push result of "expr5b"
3426 * COMPARE one of the compare instructions
3427 */
3428 static int
3429compile_expr4(char_u **arg, cctx_T *cctx)
3430{
3431 exptype_T type = EXPR_UNKNOWN;
3432 char_u *p;
3433 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 int type_is = FALSE;
3435
3436 // get the first variable
3437 if (compile_expr5(arg, cctx) == FAIL)
3438 return FAIL;
3439
3440 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003441 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442
3443 /*
3444 * If there is a comparative operator, use it.
3445 */
3446 if (type != EXPR_UNKNOWN)
3447 {
3448 int ic = FALSE; // Default: do not ignore case
3449
3450 if (type_is && (p[len] == '?' || p[len] == '#'))
3451 {
3452 semsg(_(e_invexpr2), *arg);
3453 return FAIL;
3454 }
3455 // extra question mark appended: ignore case
3456 if (p[len] == '?')
3457 {
3458 ic = TRUE;
3459 ++len;
3460 }
3461 // extra '#' appended: match case (ignored)
3462 else if (p[len] == '#')
3463 ++len;
3464 // nothing appended: match case
3465
3466 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
3467 {
3468 char_u buf[7];
3469
3470 vim_strncpy(buf, p, len);
3471 semsg(_(e_white_both), buf);
3472 }
3473
3474 // get the second variable
3475 *arg = skipwhite(p + len);
3476 if (compile_expr5(arg, cctx) == FAIL)
3477 return FAIL;
3478
3479 generate_COMPARE(cctx, type, ic);
3480 }
3481
3482 return OK;
3483}
3484
3485/*
3486 * Compile || or &&.
3487 */
3488 static int
3489compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3490{
3491 char_u *p = skipwhite(*arg);
3492 int opchar = *op;
3493
3494 if (p[0] == opchar && p[1] == opchar)
3495 {
3496 garray_T *instr = &cctx->ctx_instr;
3497 garray_T end_ga;
3498
3499 /*
3500 * Repeat until there is no following "||" or "&&"
3501 */
3502 ga_init2(&end_ga, sizeof(int), 10);
3503 while (p[0] == opchar && p[1] == opchar)
3504 {
3505 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3506 semsg(_(e_white_both), op);
3507
3508 if (ga_grow(&end_ga, 1) == FAIL)
3509 {
3510 ga_clear(&end_ga);
3511 return FAIL;
3512 }
3513 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3514 ++end_ga.ga_len;
3515 generate_JUMP(cctx, opchar == '|'
3516 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3517
3518 // eval the next expression
3519 *arg = skipwhite(p + 2);
3520 if ((opchar == '|' ? compile_expr3(arg, cctx)
3521 : compile_expr4(arg, cctx)) == FAIL)
3522 {
3523 ga_clear(&end_ga);
3524 return FAIL;
3525 }
3526 p = skipwhite(*arg);
3527 }
3528
3529 // Fill in the end label in all jumps.
3530 while (end_ga.ga_len > 0)
3531 {
3532 isn_T *isn;
3533
3534 --end_ga.ga_len;
3535 isn = ((isn_T *)instr->ga_data)
3536 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3537 isn->isn_arg.jump.jump_where = instr->ga_len;
3538 }
3539 ga_clear(&end_ga);
3540 }
3541
3542 return OK;
3543}
3544
3545/*
3546 * expr4a && expr4a && expr4a logical AND
3547 *
3548 * Produces instructions:
3549 * EVAL expr4a Push result of "expr4a"
3550 * JUMP_AND_KEEP_IF_FALSE end
3551 * EVAL expr4b Push result of "expr4b"
3552 * JUMP_AND_KEEP_IF_FALSE end
3553 * EVAL expr4c Push result of "expr4c"
3554 * end:
3555 */
3556 static int
3557compile_expr3(char_u **arg, cctx_T *cctx)
3558{
3559 // get the first variable
3560 if (compile_expr4(arg, cctx) == FAIL)
3561 return FAIL;
3562
3563 // || and && work almost the same
3564 return compile_and_or(arg, cctx, "&&");
3565}
3566
3567/*
3568 * expr3a || expr3b || expr3c logical OR
3569 *
3570 * Produces instructions:
3571 * EVAL expr3a Push result of "expr3a"
3572 * JUMP_AND_KEEP_IF_TRUE end
3573 * EVAL expr3b Push result of "expr3b"
3574 * JUMP_AND_KEEP_IF_TRUE end
3575 * EVAL expr3c Push result of "expr3c"
3576 * end:
3577 */
3578 static int
3579compile_expr2(char_u **arg, cctx_T *cctx)
3580{
3581 // eval the first expression
3582 if (compile_expr3(arg, cctx) == FAIL)
3583 return FAIL;
3584
3585 // || and && work almost the same
3586 return compile_and_or(arg, cctx, "||");
3587}
3588
3589/*
3590 * Toplevel expression: expr2 ? expr1a : expr1b
3591 *
3592 * Produces instructions:
3593 * EVAL expr2 Push result of "expr"
3594 * JUMP_IF_FALSE alt jump if false
3595 * EVAL expr1a
3596 * JUMP_ALWAYS end
3597 * alt: EVAL expr1b
3598 * end:
3599 */
3600 static int
3601compile_expr1(char_u **arg, cctx_T *cctx)
3602{
3603 char_u *p;
3604
3605 // evaluate the first expression
3606 if (compile_expr2(arg, cctx) == FAIL)
3607 return FAIL;
3608
3609 p = skipwhite(*arg);
3610 if (*p == '?')
3611 {
3612 garray_T *instr = &cctx->ctx_instr;
3613 garray_T *stack = &cctx->ctx_type_stack;
3614 int alt_idx = instr->ga_len;
3615 int end_idx;
3616 isn_T *isn;
3617 type_T *type1;
3618 type_T *type2;
3619
3620 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3621 semsg(_(e_white_both), "?");
3622
3623 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3624
3625 // evaluate the second expression; any type is accepted
3626 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003627 if (compile_expr1(arg, cctx) == FAIL)
3628 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629
3630 // remember the type and drop it
3631 --stack->ga_len;
3632 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3633
3634 end_idx = instr->ga_len;
3635 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3636
3637 // jump here from JUMP_IF_FALSE
3638 isn = ((isn_T *)instr->ga_data) + alt_idx;
3639 isn->isn_arg.jump.jump_where = instr->ga_len;
3640
3641 // Check for the ":".
3642 p = skipwhite(*arg);
3643 if (*p != ':')
3644 {
3645 emsg(_(e_missing_colon));
3646 return FAIL;
3647 }
3648 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3649 semsg(_(e_white_both), ":");
3650
3651 // evaluate the third expression
3652 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003653 if (compile_expr1(arg, cctx) == FAIL)
3654 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655
3656 // If the types differ, the result has a more generic type.
3657 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003658 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659
3660 // jump here from JUMP_ALWAYS
3661 isn = ((isn_T *)instr->ga_data) + end_idx;
3662 isn->isn_arg.jump.jump_where = instr->ga_len;
3663 }
3664 return OK;
3665}
3666
3667/*
3668 * compile "return [expr]"
3669 */
3670 static char_u *
3671compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3672{
3673 char_u *p = arg;
3674 garray_T *stack = &cctx->ctx_type_stack;
3675 type_T *stack_type;
3676
3677 if (*p != NUL && *p != '|' && *p != '\n')
3678 {
3679 // compile return argument into instructions
3680 if (compile_expr1(&p, cctx) == FAIL)
3681 return NULL;
3682
3683 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3684 if (set_return_type)
3685 cctx->ctx_ufunc->uf_ret_type = stack_type;
3686 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3687 == FAIL)
3688 return NULL;
3689 }
3690 else
3691 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003692 // "set_return_type" cannot be TRUE, only used for a lambda which
3693 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02003694 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
3695 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 {
3697 emsg(_("E1003: Missing return value"));
3698 return NULL;
3699 }
3700
3701 // No argument, return zero.
3702 generate_PUSHNR(cctx, 0);
3703 }
3704
3705 if (generate_instr(cctx, ISN_RETURN) == NULL)
3706 return NULL;
3707
3708 // "return val | endif" is possible
3709 return skipwhite(p);
3710}
3711
3712/*
3713 * Return the length of an assignment operator, or zero if there isn't one.
3714 */
3715 int
3716assignment_len(char_u *p, int *heredoc)
3717{
3718 if (*p == '=')
3719 {
3720 if (p[1] == '<' && p[2] == '<')
3721 {
3722 *heredoc = TRUE;
3723 return 3;
3724 }
3725 return 1;
3726 }
3727 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3728 return 2;
3729 if (STRNCMP(p, "..=", 3) == 0)
3730 return 3;
3731 return 0;
3732}
3733
3734// words that cannot be used as a variable
3735static char *reserved[] = {
3736 "true",
3737 "false",
3738 NULL
3739};
3740
3741/*
3742 * Get a line for "=<<".
3743 * Return a pointer to the line in allocated memory.
3744 * Return NULL for end-of-file or some error.
3745 */
3746 static char_u *
3747heredoc_getline(
3748 int c UNUSED,
3749 void *cookie,
3750 int indent UNUSED,
3751 int do_concat UNUSED)
3752{
3753 cctx_T *cctx = (cctx_T *)cookie;
3754
3755 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003756 {
3757 iemsg("Heredoc got to end");
3758 return NULL;
3759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 ++cctx->ctx_lnum;
3761 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3762 [cctx->ctx_lnum]);
3763}
3764
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003765typedef enum {
3766 dest_local,
3767 dest_option,
3768 dest_env,
3769 dest_global,
3770 dest_vimvar,
3771 dest_script,
3772 dest_reg,
3773} assign_dest_T;
3774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775/*
3776 * compile "let var [= expr]", "const var = expr" and "var = expr"
3777 * "arg" points to "var".
3778 */
3779 static char_u *
3780compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3781{
3782 char_u *p;
3783 char_u *ret = NULL;
3784 int var_count = 0;
3785 int semicolon = 0;
3786 size_t varlen;
3787 garray_T *instr = &cctx->ctx_instr;
3788 int idx = -1;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003789 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003792 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003794 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 int oplen = 0;
3796 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003797 type_T *type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 lvar_T *lvar;
3799 char_u *name;
3800 char_u *sp;
3801 int has_type = FALSE;
3802 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3803 int instr_count = -1;
3804
3805 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3806 if (p == NULL)
3807 return NULL;
3808 if (var_count > 0)
3809 {
3810 // TODO: let [var, var] = list
3811 emsg("Cannot handle a list yet");
3812 return NULL;
3813 }
3814
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003815 // "a: type" is declaring variable "a" with a type, not "a:".
3816 if (is_decl && p == arg + 2 && p[-1] == ':')
3817 --p;
3818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 varlen = p - arg;
3820 name = vim_strnsave(arg, (int)varlen);
3821 if (name == NULL)
3822 return NULL;
3823
Bram Moolenaar080457c2020-03-03 21:53:32 +01003824 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003826 if (*arg == '&')
3827 {
3828 int cc;
3829 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830
Bram Moolenaar080457c2020-03-03 21:53:32 +01003831 dest = dest_option;
3832 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003834 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003835 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 if (is_decl)
3838 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003839 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 goto theend;
3841 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003842 p = arg;
3843 p = find_option_end(&p, &opt_flags);
3844 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003846 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01003847 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003848 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003849 }
3850 cc = *p;
3851 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003852 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003853 *p = cc;
3854 if (opt_type == -3)
3855 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003856 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003857 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003858 }
3859 if (opt_type == -2 || opt_type == 0)
3860 type = &t_string;
3861 else
3862 type = &t_number; // both number and boolean option
3863 }
3864 else if (*arg == '$')
3865 {
3866 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003867 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003868 if (is_decl)
3869 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003870 semsg(_("E1065: Cannot declare an environment variable: %s"),
3871 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003872 goto theend;
3873 }
3874 }
3875 else if (*arg == '@')
3876 {
3877 if (!valid_yank_reg(arg[1], TRUE))
3878 {
3879 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003880 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003881 }
3882 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003883 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003884 if (is_decl)
3885 {
3886 semsg(_("E1066: Cannot declare a register: %s"), name);
3887 goto theend;
3888 }
3889 }
3890 else if (STRNCMP(arg, "g:", 2) == 0)
3891 {
3892 dest = dest_global;
3893 if (is_decl)
3894 {
3895 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3896 goto theend;
3897 }
3898 }
3899 else if (STRNCMP(arg, "v:", 2) == 0)
3900 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02003901 typval_T *vtv;
3902 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003903
Bram Moolenaar5da356e2020-04-09 19:34:43 +02003904 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003905 if (vimvaridx < 0)
3906 {
3907 semsg(_(e_var_notfound), arg);
3908 goto theend;
3909 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02003910 // We use the current value of "sandbox" here, is that OK?
3911 if (var_check_ro(di_flags, name, FALSE))
3912 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003913 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003914 vtv = get_vim_var_tv(vimvaridx);
3915 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003916 if (is_decl)
3917 {
3918 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3919 goto theend;
3920 }
3921 }
3922 else
3923 {
3924 for (idx = 0; reserved[idx] != NULL; ++idx)
3925 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003927 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 goto theend;
3929 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003930
3931 idx = lookup_local(arg, varlen, cctx);
3932 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003934 if (is_decl)
3935 {
3936 semsg(_("E1017: Variable already declared: %s"), name);
3937 goto theend;
3938 }
3939 else
3940 {
3941 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3942 if (lvar->lv_const)
3943 {
3944 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3945 goto theend;
3946 }
3947 }
3948 }
3949 else if (STRNCMP(arg, "s:", 2) == 0
3950 || lookup_script(arg, varlen) == OK
3951 || find_imported(arg, varlen, cctx) != NULL)
3952 {
3953 dest = dest_script;
3954 if (is_decl)
3955 {
3956 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003957 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003958 goto theend;
3959 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 }
3961 }
3962 }
3963
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003964 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 {
3966 if (is_decl && *p == ':')
3967 {
3968 // parse optional type: "let var: type = expr"
3969 p = skipwhite(p + 1);
3970 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 has_type = TRUE;
3972 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02003973 else if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 {
3975 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3976 type = lvar->lv_type;
3977 }
3978 }
3979
3980 sp = p;
3981 p = skipwhite(p);
3982 op = p;
3983 oplen = assignment_len(p, &heredoc);
3984 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3985 {
3986 char_u buf[4];
3987
3988 vim_strncpy(buf, op, oplen);
3989 semsg(_(e_white_both), buf);
3990 }
3991
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003992 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02003993 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01003995 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 goto theend;
3997 }
3998
Bram Moolenaar080457c2020-03-03 21:53:32 +01003999 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 {
4001 if (oplen > 1 && !heredoc)
4002 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004003 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4005 name);
4006 goto theend;
4007 }
4008
4009 // new local variable
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004010 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4011 && var_check_func_name(name, TRUE))
4012 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4014 if (idx < 0)
4015 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004016 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 }
4018
4019 if (heredoc)
4020 {
4021 list_T *l;
4022 listitem_T *li;
4023
4024 // [let] varname =<< [trim] {end}
4025 eap->getline = heredoc_getline;
4026 eap->cookie = cctx;
4027 l = heredoc_get(eap, op + 3);
4028
4029 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004030 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 {
4032 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4033 li->li_tv.vval.v_string = NULL;
4034 }
4035 generate_NEWLIST(cctx, l->lv_len);
4036 type = &t_list_string;
4037 list_free(l);
4038 p += STRLEN(p);
4039 }
4040 else if (oplen > 0)
4041 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004042 int r;
4043 type_T *stacktype;
4044 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 // for "+=", "*=", "..=" etc. first load the current value
4047 if (*op != '=')
4048 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004049 switch (dest)
4050 {
4051 case dest_option:
4052 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004053 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004054 break;
4055 case dest_global:
4056 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4057 break;
4058 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004059 compile_load_scriptvar(cctx,
4060 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004061 break;
4062 case dest_env:
4063 // Include $ in the name here
4064 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4065 break;
4066 case dest_reg:
4067 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4068 break;
4069 case dest_vimvar:
4070 generate_LOADV(cctx, name + 2, TRUE);
4071 break;
4072 case dest_local:
4073 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
4074 break;
4075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 }
4077
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004078 // Compile the expression. Temporarily hide the new local variable
4079 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004080 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004081 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 instr_count = instr->ga_len;
4083 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004084 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004085 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004086 ++cctx->ctx_locals.ga_len;
4087 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 goto theend;
4089
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004090 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004092 stack = &cctx->ctx_type_stack;
4093 stacktype = stack->ga_len == 0 ? &t_void
4094 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
4095 if (idx >= 0 && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004097 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
4098 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004100 if (stacktype->tt_type == VAR_VOID)
4101 {
4102 emsg(_("E1031: Cannot use void value"));
4103 goto theend;
4104 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004105 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004106 {
4107 // An empty list or dict has a &t_void member, for a
4108 // variable that implies &t_any.
4109 if (stacktype == &t_list_empty)
4110 lvar->lv_type = &t_list_any;
4111 else if (stacktype == &t_dict_empty)
4112 lvar->lv_type = &t_dict_any;
4113 else
4114 lvar->lv_type = stacktype;
4115 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004116 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004117 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4118 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004120 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004121 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 }
4123 }
4124 else if (cmdidx == CMD_const)
4125 {
4126 emsg(_("E1021: const requires a value"));
4127 goto theend;
4128 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004129 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 {
4131 emsg(_("E1022: type or initialization required"));
4132 goto theend;
4133 }
4134 else
4135 {
4136 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 if (ga_grow(instr, 1) == FAIL)
4138 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004139 switch (type->tt_type)
4140 {
4141 case VAR_BOOL:
4142 generate_PUSHBOOL(cctx, VVAL_FALSE);
4143 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004144 case VAR_FLOAT:
4145#ifdef FEAT_FLOAT
4146 generate_PUSHF(cctx, 0.0);
4147#endif
4148 break;
4149 case VAR_STRING:
4150 generate_PUSHS(cctx, NULL);
4151 break;
4152 case VAR_BLOB:
4153 generate_PUSHBLOB(cctx, NULL);
4154 break;
4155 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004156 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004157 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004158 case VAR_LIST:
4159 generate_NEWLIST(cctx, 0);
4160 break;
4161 case VAR_DICT:
4162 generate_NEWDICT(cctx, 0);
4163 break;
4164 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004165 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004166 break;
4167 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004168 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004169 break;
4170 case VAR_NUMBER:
4171 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004172 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02004173 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004174 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004175 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004176 generate_PUSHNR(cctx, 0);
4177 break;
4178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 }
4180
4181 if (oplen > 0 && *op != '=')
4182 {
4183 type_T *expected = &t_number;
4184 garray_T *stack = &cctx->ctx_type_stack;
4185 type_T *stacktype;
4186
4187 // TODO: if type is known use float or any operation
4188
4189 if (*op == '.')
4190 expected = &t_string;
4191 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4192 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4193 goto theend;
4194
4195 if (*op == '.')
4196 generate_instr_drop(cctx, ISN_CONCAT, 1);
4197 else
4198 {
4199 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4200
4201 if (isn == NULL)
4202 goto theend;
4203 switch (*op)
4204 {
4205 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4206 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4207 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4208 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4209 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4210 }
4211 }
4212 }
4213
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004214 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004216 case dest_option:
4217 generate_STOREOPT(cctx, name + 1, opt_flags);
4218 break;
4219 case dest_global:
4220 // include g: with the name, easier to execute that way
4221 generate_STORE(cctx, ISN_STOREG, 0, name);
4222 break;
4223 case dest_env:
4224 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4225 break;
4226 case dest_reg:
4227 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4228 break;
4229 case dest_vimvar:
4230 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4231 break;
4232 case dest_script:
4233 {
4234 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4235 imported_T *import = NULL;
4236 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004237
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004238 if (name[1] != ':')
4239 {
4240 import = find_imported(name, 0, cctx);
4241 if (import != NULL)
4242 sid = import->imp_sid;
4243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004245 idx = get_script_item_idx(sid, rawname, TRUE);
4246 // TODO: specific type
4247 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004248 {
4249 char_u *name_s = name;
4250
4251 // Include s: in the name for store_var()
4252 if (name[1] != ':')
4253 {
4254 int len = (int)STRLEN(name) + 3;
4255
4256 name_s = alloc(len);
4257 if (name_s == NULL)
4258 name_s = name;
4259 else
4260 vim_snprintf((char *)name_s, len, "s:%s", name);
4261 }
4262 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4263 if (name_s != name)
4264 vim_free(name_s);
4265 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004266 else
4267 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4268 sid, idx, &t_any);
4269 }
4270 break;
4271 case dest_local:
4272 {
4273 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004275 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4276 // into ISN_STORENR
4277 if (instr->ga_len == instr_count + 1
4278 && isn->isn_type == ISN_PUSHNR)
4279 {
4280 varnumber_T val = isn->isn_arg.number;
4281 garray_T *stack = &cctx->ctx_type_stack;
4282
4283 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004284 isn->isn_arg.storenr.stnr_idx = idx;
4285 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004286 if (stack->ga_len > 0)
4287 --stack->ga_len;
4288 }
4289 else
4290 generate_STORE(cctx, ISN_STORE, idx, NULL);
4291 }
4292 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 }
4294 ret = p;
4295
4296theend:
4297 vim_free(name);
4298 return ret;
4299}
4300
4301/*
4302 * Compile an :import command.
4303 */
4304 static char_u *
4305compile_import(char_u *arg, cctx_T *cctx)
4306{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004307 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308}
4309
4310/*
4311 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4312 */
4313 static int
4314compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4315{
4316 garray_T *instr = &cctx->ctx_instr;
4317 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4318
4319 if (endlabel == NULL)
4320 return FAIL;
4321 endlabel->el_next = *el;
4322 *el = endlabel;
4323 endlabel->el_end_label = instr->ga_len;
4324
4325 generate_JUMP(cctx, when, 0);
4326 return OK;
4327}
4328
4329 static void
4330compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4331{
4332 garray_T *instr = &cctx->ctx_instr;
4333
4334 while (*el != NULL)
4335 {
4336 endlabel_T *cur = (*el);
4337 isn_T *isn;
4338
4339 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4340 isn->isn_arg.jump.jump_where = instr->ga_len;
4341 *el = cur->el_next;
4342 vim_free(cur);
4343 }
4344}
4345
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004346 static void
4347compile_free_jump_to_end(endlabel_T **el)
4348{
4349 while (*el != NULL)
4350 {
4351 endlabel_T *cur = (*el);
4352
4353 *el = cur->el_next;
4354 vim_free(cur);
4355 }
4356}
4357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358/*
4359 * Create a new scope and set up the generic items.
4360 */
4361 static scope_T *
4362new_scope(cctx_T *cctx, scopetype_T type)
4363{
4364 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4365
4366 if (scope == NULL)
4367 return NULL;
4368 scope->se_outer = cctx->ctx_scope;
4369 cctx->ctx_scope = scope;
4370 scope->se_type = type;
4371 scope->se_local_count = cctx->ctx_locals.ga_len;
4372 return scope;
4373}
4374
4375/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004376 * Free the current scope and go back to the outer scope.
4377 */
4378 static void
4379drop_scope(cctx_T *cctx)
4380{
4381 scope_T *scope = cctx->ctx_scope;
4382
4383 if (scope == NULL)
4384 {
4385 iemsg("calling drop_scope() without a scope");
4386 return;
4387 }
4388 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004389 switch (scope->se_type)
4390 {
4391 case IF_SCOPE:
4392 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4393 case FOR_SCOPE:
4394 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4395 case WHILE_SCOPE:
4396 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4397 case TRY_SCOPE:
4398 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4399 case NO_SCOPE:
4400 case BLOCK_SCOPE:
4401 break;
4402 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004403 vim_free(scope);
4404}
4405
4406/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004407 * Evaluate an expression that is a constant:
4408 * has(arg)
4409 *
4410 * Also handle:
4411 * ! in front logical NOT
4412 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004413 * Return FAIL if the expression is not a constant.
4414 */
4415 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004416evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004417{
4418 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004419 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004420 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004421
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004422 /*
4423 * Skip '!' characters. They are handled later.
4424 */
4425 start_leader = *arg;
4426 while (**arg == '!')
4427 *arg = skipwhite(*arg + 1);
4428 end_leader = *arg;
4429
4430 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004431 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004432 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004433 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4434 {
4435 tv->v_type = VAR_SPECIAL;
4436 tv->vval.v_number = VVAL_TRUE;
4437 *arg += 4;
4438 return OK;
4439 }
4440 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4441 {
4442 tv->v_type = VAR_SPECIAL;
4443 tv->vval.v_number = VVAL_FALSE;
4444 *arg += 5;
4445 return OK;
4446 }
4447
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004448 if (STRNCMP("has(", *arg, 4) == 0)
4449 {
4450 has_call = TRUE;
4451 *arg = skipwhite(*arg + 4);
4452 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004453
4454 if (**arg == '"')
4455 {
4456 if (get_string_tv(arg, tv, TRUE) == FAIL)
4457 return FAIL;
4458 }
4459 else if (**arg == '\'')
4460 {
4461 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4462 return FAIL;
4463 }
4464 else
4465 return FAIL;
4466
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004467 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004468 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004469 *arg = skipwhite(*arg);
4470 if (**arg != ')')
4471 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004472 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004473
4474 argvars[0] = *tv;
4475 argvars[1].v_type = VAR_UNKNOWN;
4476 tv->v_type = VAR_NUMBER;
4477 tv->vval.v_number = 0;
4478 f_has(argvars, tv);
4479 clear_tv(&argvars[0]);
4480
4481 while (start_leader < end_leader)
4482 {
4483 if (*start_leader == '!')
4484 tv->vval.v_number = !tv->vval.v_number;
4485 ++start_leader;
4486 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004487 }
4488
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004489 return OK;
4490}
4491
Bram Moolenaar080457c2020-03-03 21:53:32 +01004492 static int
4493evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4494{
4495 exptype_T type = EXPR_UNKNOWN;
4496 char_u *p;
4497 int len = 2;
4498 int type_is = FALSE;
4499
4500 // get the first variable
4501 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4502 return FAIL;
4503
4504 p = skipwhite(*arg);
4505 type = get_compare_type(p, &len, &type_is);
4506
4507 /*
4508 * If there is a comparative operator, use it.
4509 */
4510 if (type != EXPR_UNKNOWN)
4511 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004512 typval_T tv2;
4513 char_u *s1, *s2;
4514 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4515 int n;
4516
4517 // TODO: Only string == string is supported now
4518 if (tv->v_type != VAR_STRING)
4519 return FAIL;
4520 if (type != EXPR_EQUAL)
4521 return FAIL;
4522
4523 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004524 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004525 *arg = skipwhite(p + len);
4526 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4527 || tv2.v_type != VAR_STRING)
4528 {
4529 clear_tv(&tv2);
4530 return FAIL;
4531 }
4532 s1 = tv_get_string_buf(tv, buf1);
4533 s2 = tv_get_string_buf(&tv2, buf2);
4534 n = STRCMP(s1, s2);
4535 clear_tv(tv);
4536 clear_tv(&tv2);
4537 tv->v_type = VAR_BOOL;
4538 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004539 }
4540
4541 return OK;
4542}
4543
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004544static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4545
4546/*
4547 * Compile constant || or &&.
4548 */
4549 static int
4550evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4551{
4552 char_u *p = skipwhite(*arg);
4553 int opchar = *op;
4554
4555 if (p[0] == opchar && p[1] == opchar)
4556 {
4557 int val = tv2bool(tv);
4558
4559 /*
4560 * Repeat until there is no following "||" or "&&"
4561 */
4562 while (p[0] == opchar && p[1] == opchar)
4563 {
4564 typval_T tv2;
4565
4566 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4567 return FAIL;
4568
4569 // eval the next expression
4570 *arg = skipwhite(p + 2);
4571 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004572 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004573 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004574 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004575 {
4576 clear_tv(&tv2);
4577 return FAIL;
4578 }
4579 if ((opchar == '&') == val)
4580 {
4581 // false || tv2 or true && tv2: use tv2
4582 clear_tv(tv);
4583 *tv = tv2;
4584 val = tv2bool(tv);
4585 }
4586 else
4587 clear_tv(&tv2);
4588 p = skipwhite(*arg);
4589 }
4590 }
4591
4592 return OK;
4593}
4594
4595/*
4596 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4597 * Return FAIL if the expression is not a constant.
4598 */
4599 static int
4600evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4601{
4602 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004603 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004604 return FAIL;
4605
4606 // || and && work almost the same
4607 return evaluate_const_and_or(arg, cctx, "&&", tv);
4608}
4609
4610/*
4611 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4612 * Return FAIL if the expression is not a constant.
4613 */
4614 static int
4615evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4616{
4617 // evaluate the first expression
4618 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4619 return FAIL;
4620
4621 // || and && work almost the same
4622 return evaluate_const_and_or(arg, cctx, "||", tv);
4623}
4624
4625/*
4626 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4627 * E.g. for "has('feature')".
4628 * This does not produce error messages. "tv" should be cleared afterwards.
4629 * Return FAIL if the expression is not a constant.
4630 */
4631 static int
4632evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4633{
4634 char_u *p;
4635
4636 // evaluate the first expression
4637 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4638 return FAIL;
4639
4640 p = skipwhite(*arg);
4641 if (*p == '?')
4642 {
4643 int val = tv2bool(tv);
4644 typval_T tv2;
4645
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004646 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004647 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4648 return FAIL;
4649
4650 // evaluate the second expression; any type is accepted
4651 clear_tv(tv);
4652 *arg = skipwhite(p + 1);
4653 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4654 return FAIL;
4655
4656 // Check for the ":".
4657 p = skipwhite(*arg);
4658 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4659 return FAIL;
4660
4661 // evaluate the third expression
4662 *arg = skipwhite(p + 1);
4663 tv2.v_type = VAR_UNKNOWN;
4664 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4665 {
4666 clear_tv(&tv2);
4667 return FAIL;
4668 }
4669 if (val)
4670 {
4671 // use the expr after "?"
4672 clear_tv(&tv2);
4673 }
4674 else
4675 {
4676 // use the expr after ":"
4677 clear_tv(tv);
4678 *tv = tv2;
4679 }
4680 }
4681 return OK;
4682}
4683
4684/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 * compile "if expr"
4686 *
4687 * "if expr" Produces instructions:
4688 * EVAL expr Push result of "expr"
4689 * JUMP_IF_FALSE end
4690 * ... body ...
4691 * end:
4692 *
4693 * "if expr | else" Produces instructions:
4694 * EVAL expr Push result of "expr"
4695 * JUMP_IF_FALSE else
4696 * ... body ...
4697 * JUMP_ALWAYS end
4698 * else:
4699 * ... body ...
4700 * end:
4701 *
4702 * "if expr1 | elseif expr2 | else" Produces instructions:
4703 * EVAL expr Push result of "expr"
4704 * JUMP_IF_FALSE elseif
4705 * ... body ...
4706 * JUMP_ALWAYS end
4707 * elseif:
4708 * EVAL expr Push result of "expr"
4709 * JUMP_IF_FALSE else
4710 * ... body ...
4711 * JUMP_ALWAYS end
4712 * else:
4713 * ... body ...
4714 * end:
4715 */
4716 static char_u *
4717compile_if(char_u *arg, cctx_T *cctx)
4718{
4719 char_u *p = arg;
4720 garray_T *instr = &cctx->ctx_instr;
4721 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004722 typval_T tv;
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;
4736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737
4738 scope = new_scope(cctx, IF_SCOPE);
4739 if (scope == NULL)
4740 return NULL;
4741
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004742 if (cctx->ctx_skip == MAYBE)
4743 {
4744 // "where" is set when ":elseif", "else" or ":endif" is found
4745 scope->se_u.se_if.is_if_label = instr->ga_len;
4746 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4747 }
4748 else
4749 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750
4751 return p;
4752}
4753
4754 static char_u *
4755compile_elseif(char_u *arg, cctx_T *cctx)
4756{
4757 char_u *p = arg;
4758 garray_T *instr = &cctx->ctx_instr;
4759 isn_T *isn;
4760 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004761 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762
4763 if (scope == NULL || scope->se_type != IF_SCOPE)
4764 {
4765 emsg(_(e_elseif_without_if));
4766 return NULL;
4767 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004768 unwind_locals(cctx, scope->se_local_count);
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 (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004774 return NULL;
4775 // previous "if" or "elseif" jumps here
4776 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4777 isn->isn_arg.jump.jump_where = instr->ga_len;
4778 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004780 // compile "expr"; if we know it evaluates to FALSE skip the block
4781 tv.v_type = VAR_UNKNOWN;
4782 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4783 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4784 else
4785 cctx->ctx_skip = MAYBE;
4786 clear_tv(&tv);
4787 if (cctx->ctx_skip == MAYBE)
4788 {
4789 p = arg;
4790 if (compile_expr1(&p, cctx) == FAIL)
4791 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004793 // "where" is set when ":elseif", "else" or ":endif" is found
4794 scope->se_u.se_if.is_if_label = instr->ga_len;
4795 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4796 }
4797 else
4798 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799
4800 return p;
4801}
4802
4803 static char_u *
4804compile_else(char_u *arg, cctx_T *cctx)
4805{
4806 char_u *p = arg;
4807 garray_T *instr = &cctx->ctx_instr;
4808 isn_T *isn;
4809 scope_T *scope = cctx->ctx_scope;
4810
4811 if (scope == NULL || scope->se_type != IF_SCOPE)
4812 {
4813 emsg(_(e_else_without_if));
4814 return NULL;
4815 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004816 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004818 // jump from previous block to the end, unless the else block is empty
4819 if (cctx->ctx_skip == MAYBE)
4820 {
4821 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004823 return NULL;
4824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825
Bram Moolenaar158906c2020-02-06 20:39:45 +01004826 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004827 {
4828 if (scope->se_u.se_if.is_if_label >= 0)
4829 {
4830 // previous "if" or "elseif" jumps here
4831 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4832 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004833 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004834 }
4835 }
4836
4837 if (cctx->ctx_skip != MAYBE)
4838 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839
4840 return p;
4841}
4842
4843 static char_u *
4844compile_endif(char_u *arg, cctx_T *cctx)
4845{
4846 scope_T *scope = cctx->ctx_scope;
4847 ifscope_T *ifscope;
4848 garray_T *instr = &cctx->ctx_instr;
4849 isn_T *isn;
4850
4851 if (scope == NULL || scope->se_type != IF_SCOPE)
4852 {
4853 emsg(_(e_endif_without_if));
4854 return NULL;
4855 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004856 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004857 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004859 if (scope->se_u.se_if.is_if_label >= 0)
4860 {
4861 // previous "if" or "elseif" jumps here
4862 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4863 isn->isn_arg.jump.jump_where = instr->ga_len;
4864 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 // Fill in the "end" label in jumps at the end of the blocks.
4866 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004867 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004869 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 return arg;
4871}
4872
4873/*
4874 * compile "for var in expr"
4875 *
4876 * Produces instructions:
4877 * PUSHNR -1
4878 * STORE loop-idx Set index to -1
4879 * EVAL expr Push result of "expr"
4880 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4881 * - if beyond end, jump to "end"
4882 * - otherwise get item from list and push it
4883 * STORE var Store item in "var"
4884 * ... body ...
4885 * JUMP top Jump back to repeat
4886 * end: DROP Drop the result of "expr"
4887 *
4888 */
4889 static char_u *
4890compile_for(char_u *arg, cctx_T *cctx)
4891{
4892 char_u *p;
4893 size_t varlen;
4894 garray_T *instr = &cctx->ctx_instr;
4895 garray_T *stack = &cctx->ctx_type_stack;
4896 scope_T *scope;
4897 int loop_idx; // index of loop iteration variable
4898 int var_idx; // index of "var"
4899 type_T *vartype;
4900
4901 // TODO: list of variables: "for [key, value] in dict"
4902 // parse "var"
4903 for (p = arg; eval_isnamec1(*p); ++p)
4904 ;
4905 varlen = p - arg;
4906 var_idx = lookup_local(arg, varlen, cctx);
4907 if (var_idx >= 0)
4908 {
4909 semsg(_("E1023: variable already defined: %s"), arg);
4910 return NULL;
4911 }
4912
4913 // consume "in"
4914 p = skipwhite(p);
4915 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4916 {
4917 emsg(_(e_missing_in));
4918 return NULL;
4919 }
4920 p = skipwhite(p + 2);
4921
4922
4923 scope = new_scope(cctx, FOR_SCOPE);
4924 if (scope == NULL)
4925 return NULL;
4926
4927 // Reserve a variable to store the loop iteration counter.
4928 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4929 if (loop_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004930 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004931 // only happens when out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004932 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935
4936 // Reserve a variable to store "var"
4937 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4938 if (var_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004939 {
4940 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943
4944 generate_STORENR(cctx, loop_idx, -1);
4945
4946 // compile "expr", it remains on the stack until "endfor"
4947 arg = p;
4948 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004949 {
4950 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953
4954 // now we know the type of "var"
4955 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4956 if (vartype->tt_type != VAR_LIST)
4957 {
4958 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004959 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 return NULL;
4961 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02004962 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 {
4964 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4965
4966 lvar->lv_type = vartype->tt_member;
4967 }
4968
4969 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004970 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971
4972 generate_FOR(cctx, loop_idx);
4973 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4974
4975 return arg;
4976}
4977
4978/*
4979 * compile "endfor"
4980 */
4981 static char_u *
4982compile_endfor(char_u *arg, cctx_T *cctx)
4983{
4984 garray_T *instr = &cctx->ctx_instr;
4985 scope_T *scope = cctx->ctx_scope;
4986 forscope_T *forscope;
4987 isn_T *isn;
4988
4989 if (scope == NULL || scope->se_type != FOR_SCOPE)
4990 {
4991 emsg(_(e_for));
4992 return NULL;
4993 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004994 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004996 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997
4998 // At end of ":for" scope jump back to the FOR instruction.
4999 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5000
5001 // Fill in the "end" label in the FOR statement so it can jump here
5002 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5003 isn->isn_arg.forloop.for_end = instr->ga_len;
5004
5005 // Fill in the "end" label any BREAK statements
5006 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5007
5008 // Below the ":for" scope drop the "expr" list from the stack.
5009 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5010 return NULL;
5011
5012 vim_free(scope);
5013
5014 return arg;
5015}
5016
5017/*
5018 * compile "while expr"
5019 *
5020 * Produces instructions:
5021 * top: EVAL expr Push result of "expr"
5022 * JUMP_IF_FALSE end jump if false
5023 * ... body ...
5024 * JUMP top Jump back to repeat
5025 * end:
5026 *
5027 */
5028 static char_u *
5029compile_while(char_u *arg, cctx_T *cctx)
5030{
5031 char_u *p = arg;
5032 garray_T *instr = &cctx->ctx_instr;
5033 scope_T *scope;
5034
5035 scope = new_scope(cctx, WHILE_SCOPE);
5036 if (scope == NULL)
5037 return NULL;
5038
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005039 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040
5041 // compile "expr"
5042 if (compile_expr1(&p, cctx) == FAIL)
5043 return NULL;
5044
5045 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005046 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047 JUMP_IF_FALSE, cctx) == FAIL)
5048 return FAIL;
5049
5050 return p;
5051}
5052
5053/*
5054 * compile "endwhile"
5055 */
5056 static char_u *
5057compile_endwhile(char_u *arg, cctx_T *cctx)
5058{
5059 scope_T *scope = cctx->ctx_scope;
5060
5061 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5062 {
5063 emsg(_(e_while));
5064 return NULL;
5065 }
5066 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005067 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068
5069 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005070 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071
5072 // Fill in the "end" label in the WHILE statement so it can jump here.
5073 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005074 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075
5076 vim_free(scope);
5077
5078 return arg;
5079}
5080
5081/*
5082 * compile "continue"
5083 */
5084 static char_u *
5085compile_continue(char_u *arg, cctx_T *cctx)
5086{
5087 scope_T *scope = cctx->ctx_scope;
5088
5089 for (;;)
5090 {
5091 if (scope == NULL)
5092 {
5093 emsg(_(e_continue));
5094 return NULL;
5095 }
5096 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5097 break;
5098 scope = scope->se_outer;
5099 }
5100
5101 // Jump back to the FOR or WHILE instruction.
5102 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005103 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5104 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 return arg;
5106}
5107
5108/*
5109 * compile "break"
5110 */
5111 static char_u *
5112compile_break(char_u *arg, cctx_T *cctx)
5113{
5114 scope_T *scope = cctx->ctx_scope;
5115 endlabel_T **el;
5116
5117 for (;;)
5118 {
5119 if (scope == NULL)
5120 {
5121 emsg(_(e_break));
5122 return NULL;
5123 }
5124 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5125 break;
5126 scope = scope->se_outer;
5127 }
5128
5129 // Jump to the end of the FOR or WHILE loop.
5130 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005131 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005133 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5135 return FAIL;
5136
5137 return arg;
5138}
5139
5140/*
5141 * compile "{" start of block
5142 */
5143 static char_u *
5144compile_block(char_u *arg, cctx_T *cctx)
5145{
5146 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5147 return NULL;
5148 return skipwhite(arg + 1);
5149}
5150
5151/*
5152 * compile end of block: drop one scope
5153 */
5154 static void
5155compile_endblock(cctx_T *cctx)
5156{
5157 scope_T *scope = cctx->ctx_scope;
5158
5159 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005160 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161 vim_free(scope);
5162}
5163
5164/*
5165 * compile "try"
5166 * Creates a new scope for the try-endtry, pointing to the first catch and
5167 * finally.
5168 * Creates another scope for the "try" block itself.
5169 * TRY instruction sets up exception handling at runtime.
5170 *
5171 * "try"
5172 * TRY -> catch1, -> finally push trystack entry
5173 * ... try block
5174 * "throw {exception}"
5175 * EVAL {exception}
5176 * THROW create exception
5177 * ... try block
5178 * " catch {expr}"
5179 * JUMP -> finally
5180 * catch1: PUSH exeception
5181 * EVAL {expr}
5182 * MATCH
5183 * JUMP nomatch -> catch2
5184 * CATCH remove exception
5185 * ... catch block
5186 * " catch"
5187 * JUMP -> finally
5188 * catch2: CATCH remove exception
5189 * ... catch block
5190 * " finally"
5191 * finally:
5192 * ... finally block
5193 * " endtry"
5194 * ENDTRY pop trystack entry, may rethrow
5195 */
5196 static char_u *
5197compile_try(char_u *arg, cctx_T *cctx)
5198{
5199 garray_T *instr = &cctx->ctx_instr;
5200 scope_T *try_scope;
5201 scope_T *scope;
5202
5203 // scope that holds the jumps that go to catch/finally/endtry
5204 try_scope = new_scope(cctx, TRY_SCOPE);
5205 if (try_scope == NULL)
5206 return NULL;
5207
5208 // "catch" is set when the first ":catch" is found.
5209 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005210 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 if (generate_instr(cctx, ISN_TRY) == NULL)
5212 return NULL;
5213
5214 // scope for the try block itself
5215 scope = new_scope(cctx, BLOCK_SCOPE);
5216 if (scope == NULL)
5217 return NULL;
5218
5219 return arg;
5220}
5221
5222/*
5223 * compile "catch {expr}"
5224 */
5225 static char_u *
5226compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5227{
5228 scope_T *scope = cctx->ctx_scope;
5229 garray_T *instr = &cctx->ctx_instr;
5230 char_u *p;
5231 isn_T *isn;
5232
5233 // end block scope from :try or :catch
5234 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5235 compile_endblock(cctx);
5236 scope = cctx->ctx_scope;
5237
5238 // Error if not in a :try scope
5239 if (scope == NULL || scope->se_type != TRY_SCOPE)
5240 {
5241 emsg(_(e_catch));
5242 return NULL;
5243 }
5244
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005245 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246 {
5247 emsg(_("E1033: catch unreachable after catch-all"));
5248 return NULL;
5249 }
5250
5251 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005252 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253 JUMP_ALWAYS, cctx) == FAIL)
5254 return NULL;
5255
5256 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005257 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258 if (isn->isn_arg.try.try_catch == 0)
5259 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005260 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261 {
5262 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005263 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005264 isn->isn_arg.jump.jump_where = instr->ga_len;
5265 }
5266
5267 p = skipwhite(arg);
5268 if (ends_excmd(*p))
5269 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005270 scope->se_u.se_try.ts_caught_all = TRUE;
5271 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005272 }
5273 else
5274 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005275 char_u *end;
5276 char_u *pat;
5277 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005278 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005279 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005281 // Push v:exception, push {expr} and MATCH
5282 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5283
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005284 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005285 if (*end != *p)
5286 {
5287 semsg(_("E1067: Separator mismatch: %s"), p);
5288 vim_free(tofree);
5289 return FAIL;
5290 }
5291 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005292 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005293 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005294 len = (int)(end - tofree);
5295 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005296 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005297 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005298 if (pat == NULL)
5299 return FAIL;
5300 if (generate_PUSHS(cctx, pat) == FAIL)
5301 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5304 return NULL;
5305
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005306 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005307 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5308 return NULL;
5309 }
5310
5311 if (generate_instr(cctx, ISN_CATCH) == NULL)
5312 return NULL;
5313
5314 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5315 return NULL;
5316 return p;
5317}
5318
5319 static char_u *
5320compile_finally(char_u *arg, cctx_T *cctx)
5321{
5322 scope_T *scope = cctx->ctx_scope;
5323 garray_T *instr = &cctx->ctx_instr;
5324 isn_T *isn;
5325
5326 // end block scope from :try or :catch
5327 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5328 compile_endblock(cctx);
5329 scope = cctx->ctx_scope;
5330
5331 // Error if not in a :try scope
5332 if (scope == NULL || scope->se_type != TRY_SCOPE)
5333 {
5334 emsg(_(e_finally));
5335 return NULL;
5336 }
5337
5338 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005339 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005340 if (isn->isn_arg.try.try_finally != 0)
5341 {
5342 emsg(_(e_finally_dup));
5343 return NULL;
5344 }
5345
5346 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005347 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005348
Bram Moolenaar585fea72020-04-02 22:33:21 +02005349 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005350 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351 {
5352 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005353 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 isn->isn_arg.jump.jump_where = instr->ga_len;
5355 }
5356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357 // TODO: set index in ts_finally_label jumps
5358
5359 return arg;
5360}
5361
5362 static char_u *
5363compile_endtry(char_u *arg, cctx_T *cctx)
5364{
5365 scope_T *scope = cctx->ctx_scope;
5366 garray_T *instr = &cctx->ctx_instr;
5367 isn_T *isn;
5368
5369 // end block scope from :catch or :finally
5370 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5371 compile_endblock(cctx);
5372 scope = cctx->ctx_scope;
5373
5374 // Error if not in a :try scope
5375 if (scope == NULL || scope->se_type != TRY_SCOPE)
5376 {
5377 if (scope == NULL)
5378 emsg(_(e_no_endtry));
5379 else if (scope->se_type == WHILE_SCOPE)
5380 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005381 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005382 emsg(_(e_endfor));
5383 else
5384 emsg(_(e_endif));
5385 return NULL;
5386 }
5387
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005388 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5390 {
5391 emsg(_("E1032: missing :catch or :finally"));
5392 return NULL;
5393 }
5394
5395 // Fill in the "end" label in jumps at the end of the blocks, if not done
5396 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005397 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398
5399 // End :catch or :finally scope: set value in ISN_TRY instruction
5400 if (isn->isn_arg.try.try_finally == 0)
5401 isn->isn_arg.try.try_finally = instr->ga_len;
5402 compile_endblock(cctx);
5403
5404 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5405 return NULL;
5406 return arg;
5407}
5408
5409/*
5410 * compile "throw {expr}"
5411 */
5412 static char_u *
5413compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5414{
5415 char_u *p = skipwhite(arg);
5416
5417 if (ends_excmd(*p))
5418 {
5419 emsg(_(e_argreq));
5420 return NULL;
5421 }
5422 if (compile_expr1(&p, cctx) == FAIL)
5423 return NULL;
5424 if (may_generate_2STRING(-1, cctx) == FAIL)
5425 return NULL;
5426 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5427 return NULL;
5428
5429 return p;
5430}
5431
5432/*
5433 * compile "echo expr"
5434 */
5435 static char_u *
5436compile_echo(char_u *arg, int with_white, cctx_T *cctx)
5437{
5438 char_u *p = arg;
5439 int count = 0;
5440
Bram Moolenaarad39c092020-02-26 18:23:43 +01005441 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 {
5443 if (compile_expr1(&p, cctx) == FAIL)
5444 return NULL;
5445 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005446 p = skipwhite(p);
5447 if (ends_excmd(*p))
5448 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449 }
5450
5451 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01005452 return p;
5453}
5454
5455/*
5456 * compile "execute expr"
5457 */
5458 static char_u *
5459compile_execute(char_u *arg, cctx_T *cctx)
5460{
5461 char_u *p = arg;
5462 int count = 0;
5463
5464 for (;;)
5465 {
5466 if (compile_expr1(&p, cctx) == FAIL)
5467 return NULL;
5468 ++count;
5469 p = skipwhite(p);
5470 if (ends_excmd(*p))
5471 break;
5472 }
5473
5474 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005475
5476 return p;
5477}
5478
5479/*
5480 * After ex_function() has collected all the function lines: parse and compile
5481 * the lines into instructions.
5482 * Adds the function to "def_functions".
5483 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5484 * return statement (used for lambda).
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005485 * This can be used recursively through compile_lambda(), which may reallocate
5486 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005487 */
5488 void
5489compile_def_function(ufunc_T *ufunc, int set_return_type)
5490{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491 char_u *line = NULL;
5492 char_u *p;
5493 exarg_T ea;
5494 char *errormsg = NULL; // error message
5495 int had_return = FALSE;
5496 cctx_T cctx;
5497 garray_T *instr;
5498 int called_emsg_before = called_emsg;
5499 int ret = FAIL;
5500 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005501 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005503 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005504 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005505
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005506 if (ufunc->uf_dfunc_idx >= 0)
5507 {
5508 // Redefining a function that was compiled before.
5509 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5510
5511 // Free old instructions.
5512 delete_def_function_contents(dfunc);
5513 }
5514 else
5515 {
5516 // Add the function to "def_functions".
5517 if (ga_grow(&def_functions, 1) == FAIL)
5518 return;
5519 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
5520 vim_memset(dfunc, 0, sizeof(dfunc_T));
5521 dfunc->df_idx = def_functions.ga_len;
5522 ufunc->uf_dfunc_idx = dfunc->df_idx;
5523 dfunc->df_ufunc = ufunc;
5524 ++def_functions.ga_len;
5525 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005526 }
5527
5528 vim_memset(&cctx, 0, sizeof(cctx));
5529 cctx.ctx_ufunc = ufunc;
5530 cctx.ctx_lnum = -1;
5531 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5532 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5533 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5534 cctx.ctx_type_list = &ufunc->uf_type_list;
5535 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5536 instr = &cctx.ctx_instr;
5537
5538 // Most modern script version.
5539 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5540
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005541 if (ufunc->uf_def_args.ga_len > 0)
5542 {
5543 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005544 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005545 int i;
5546 char_u *arg;
5547 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5548
5549 // Produce instructions for the default values of optional arguments.
5550 // Store the instruction index in uf_def_arg_idx[] so that we know
5551 // where to start when the function is called, depending on the number
5552 // of arguments.
5553 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5554 if (ufunc->uf_def_arg_idx == NULL)
5555 goto erret;
5556 for (i = 0; i < count; ++i)
5557 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005558 garray_T *stack = &cctx.ctx_type_stack;
5559 type_T *val_type;
5560 int arg_idx = first_def_arg + i;
5561
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005562 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5563 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005564 if (compile_expr1(&arg, &cctx) == FAIL)
5565 goto erret;
5566
5567 // If no type specified use the type of the default value.
5568 // Otherwise check that the default value type matches the
5569 // specified type.
5570 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5571 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
5572 ufunc->uf_arg_types[arg_idx] = val_type;
5573 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
5574 == FAIL)
5575 {
5576 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
5577 arg_idx + 1);
5578 goto erret;
5579 }
5580
5581 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005582 goto erret;
5583 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005584 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5585 }
5586
5587 /*
5588 * Loop over all the lines of the function and generate instructions.
5589 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590 for (;;)
5591 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005592 int is_ex_command;
5593
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005594 // Bail out on the first error to avoid a flood of errors and report
5595 // the right line number when inside try/catch.
5596 if (emsg_before != called_emsg)
5597 goto erret;
5598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005599 if (line != NULL && *line == '|')
5600 // the line continues after a '|'
5601 ++line;
5602 else if (line != NULL && *line != NUL)
5603 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005604 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 goto erret;
5606 }
5607 else
5608 {
5609 do
5610 {
5611 ++cctx.ctx_lnum;
5612 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5613 break;
5614 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
5615 } while (line == NULL);
5616 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5617 break;
5618 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
5619 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005620 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005621
5622 had_return = FALSE;
5623 vim_memset(&ea, 0, sizeof(ea));
5624 ea.cmdlinep = &line;
5625 ea.cmd = skipwhite(line);
5626
5627 // "}" ends a block scope
5628 if (*ea.cmd == '}')
5629 {
5630 scopetype_T stype = cctx.ctx_scope == NULL
5631 ? NO_SCOPE : cctx.ctx_scope->se_type;
5632
5633 if (stype == BLOCK_SCOPE)
5634 {
5635 compile_endblock(&cctx);
5636 line = ea.cmd;
5637 }
5638 else
5639 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005640 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641 goto erret;
5642 }
5643 if (line != NULL)
5644 line = skipwhite(ea.cmd + 1);
5645 continue;
5646 }
5647
5648 // "{" starts a block scope
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01005649 // "{'a': 1}->func() is something else
5650 if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005651 {
5652 line = compile_block(ea.cmd, &cctx);
5653 continue;
5654 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005655 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005656
5657 /*
5658 * COMMAND MODIFIERS
5659 */
5660 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5661 {
5662 if (errormsg != NULL)
5663 goto erret;
5664 // empty line or comment
5665 line = (char_u *)"";
5666 continue;
5667 }
5668
5669 // Skip ":call" to get to the function name.
5670 if (checkforcmd(&ea.cmd, "call", 3))
5671 ea.cmd = skipwhite(ea.cmd);
5672
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005673 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005675 // Assuming the command starts with a variable or function name,
5676 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5677 // val".
5678 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5679 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005680 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005681 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005682 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005683 int oplen;
5684 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005685
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005686 oplen = assignment_len(skipwhite(p), &heredoc);
5687 if (oplen > 0)
5688 {
5689 // Recognize an assignment if we recognize the variable
5690 // name:
5691 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005692 // "local = expr" where "local" is a local var.
5693 // "script = expr" where "script" is a script-local var.
5694 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005695 // "&opt = expr"
5696 // "$ENV = expr"
5697 // "@r = expr"
5698 if (*ea.cmd == '&'
5699 || *ea.cmd == '$'
5700 || *ea.cmd == '@'
5701 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5702 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5703 || lookup_script(ea.cmd, p - ea.cmd) == OK
5704 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5705 {
5706 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5707 if (line == NULL)
5708 goto erret;
5709 continue;
5710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711 }
5712 }
5713 }
5714
5715 /*
5716 * COMMAND after range
5717 */
5718 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005719 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5720 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005721
5722 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5723 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005724 if (cctx.ctx_skip == TRUE)
5725 {
5726 line += STRLEN(line);
5727 continue;
5728 }
5729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005730 // Expression or function call.
5731 if (ea.cmdidx == CMD_eval)
5732 {
5733 p = ea.cmd;
5734 if (compile_expr1(&p, &cctx) == FAIL)
5735 goto erret;
5736
5737 // drop the return value
5738 generate_instr_drop(&cctx, ISN_DROP, 1);
5739 line = p;
5740 continue;
5741 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02005742 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005743 iemsg("Command from find_ex_command() not handled");
5744 goto erret;
5745 }
5746
5747 p = skipwhite(p);
5748
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005749 if (cctx.ctx_skip == TRUE
5750 && ea.cmdidx != CMD_elseif
5751 && ea.cmdidx != CMD_else
5752 && ea.cmdidx != CMD_endif)
5753 {
5754 line += STRLEN(line);
5755 continue;
5756 }
5757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005758 switch (ea.cmdidx)
5759 {
5760 case CMD_def:
5761 case CMD_function:
5762 // TODO: Nested function
5763 emsg("Nested function not implemented yet");
5764 goto erret;
5765
5766 case CMD_return:
5767 line = compile_return(p, set_return_type, &cctx);
5768 had_return = TRUE;
5769 break;
5770
5771 case CMD_let:
5772 case CMD_const:
5773 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5774 break;
5775
5776 case CMD_import:
5777 line = compile_import(p, &cctx);
5778 break;
5779
5780 case CMD_if:
5781 line = compile_if(p, &cctx);
5782 break;
5783 case CMD_elseif:
5784 line = compile_elseif(p, &cctx);
5785 break;
5786 case CMD_else:
5787 line = compile_else(p, &cctx);
5788 break;
5789 case CMD_endif:
5790 line = compile_endif(p, &cctx);
5791 break;
5792
5793 case CMD_while:
5794 line = compile_while(p, &cctx);
5795 break;
5796 case CMD_endwhile:
5797 line = compile_endwhile(p, &cctx);
5798 break;
5799
5800 case CMD_for:
5801 line = compile_for(p, &cctx);
5802 break;
5803 case CMD_endfor:
5804 line = compile_endfor(p, &cctx);
5805 break;
5806 case CMD_continue:
5807 line = compile_continue(p, &cctx);
5808 break;
5809 case CMD_break:
5810 line = compile_break(p, &cctx);
5811 break;
5812
5813 case CMD_try:
5814 line = compile_try(p, &cctx);
5815 break;
5816 case CMD_catch:
5817 line = compile_catch(p, &cctx);
5818 break;
5819 case CMD_finally:
5820 line = compile_finally(p, &cctx);
5821 break;
5822 case CMD_endtry:
5823 line = compile_endtry(p, &cctx);
5824 break;
5825 case CMD_throw:
5826 line = compile_throw(p, &cctx);
5827 break;
5828
5829 case CMD_echo:
5830 line = compile_echo(p, TRUE, &cctx);
5831 break;
5832 case CMD_echon:
5833 line = compile_echo(p, FALSE, &cctx);
5834 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005835 case CMD_execute:
5836 line = compile_execute(p, &cctx);
5837 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005838
5839 default:
5840 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005841 // TODO:
5842 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005843 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844 generate_EXEC(&cctx, line);
5845 line = (char_u *)"";
5846 break;
5847 }
5848 if (line == NULL)
5849 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02005850 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005851
5852 if (cctx.ctx_type_stack.ga_len < 0)
5853 {
5854 iemsg("Type stack underflow");
5855 goto erret;
5856 }
5857 }
5858
5859 if (cctx.ctx_scope != NULL)
5860 {
5861 if (cctx.ctx_scope->se_type == IF_SCOPE)
5862 emsg(_(e_endif));
5863 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5864 emsg(_(e_endwhile));
5865 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5866 emsg(_(e_endfor));
5867 else
5868 emsg(_("E1026: Missing }"));
5869 goto erret;
5870 }
5871
5872 if (!had_return)
5873 {
5874 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5875 {
5876 emsg(_("E1027: Missing return statement"));
5877 goto erret;
5878 }
5879
5880 // Return zero if there is no return at the end.
5881 generate_PUSHNR(&cctx, 0);
5882 generate_instr(&cctx, ISN_RETURN);
5883 }
5884
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005885 {
5886 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5887 + ufunc->uf_dfunc_idx;
5888 dfunc->df_deleted = FALSE;
5889 dfunc->df_instr = instr->ga_data;
5890 dfunc->df_instr_count = instr->ga_len;
5891 dfunc->df_varcount = cctx.ctx_max_local;
5892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005893
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005894 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005895 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005896 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005897
5898 // Create a type for the function, with the return type and any
5899 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005900 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5901 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005902 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005903 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005904 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
5905 argcount, &ufunc->uf_type_list);
5906 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005907 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005908 argcount + varargs,
5909 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005910 {
5911 ret = FAIL;
5912 goto erret;
5913 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005914 ufunc->uf_func_type->tt_argcount = argcount + varargs;
5915 ufunc->uf_func_type->tt_min_argcount =
5916 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005917 if (ufunc->uf_arg_types == NULL)
5918 {
5919 int i;
5920
5921 // lambda does not have argument types.
5922 for (i = 0; i < argcount; ++i)
5923 ufunc->uf_func_type->tt_args[i] = &t_any;
5924 }
5925 else
5926 mch_memmove(ufunc->uf_func_type->tt_args,
5927 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005928 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005929 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005930 ufunc->uf_func_type->tt_args[argcount] =
5931 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005932 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
5933 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005934 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005935 else
5936 // No arguments, can use a predefined type.
5937 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5938 argcount, &ufunc->uf_type_list);
5939
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005940 }
5941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942 ret = OK;
5943
5944erret:
5945 if (ret == FAIL)
5946 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005947 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005948 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5949 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005950
5951 for (idx = 0; idx < instr->ga_len; ++idx)
5952 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005955 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005956 if (!dfunc->df_deleted)
5957 --def_functions.ga_len;
5958
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005959 while (cctx.ctx_scope != NULL)
5960 drop_scope(&cctx);
5961
Bram Moolenaar20431c92020-03-20 18:39:46 +01005962 // Don't execute this function body.
5963 ga_clear_strings(&ufunc->uf_lines);
5964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005965 if (errormsg != NULL)
5966 emsg(errormsg);
5967 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005968 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969 }
5970
5971 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005972 free_imported(&cctx);
5973 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975}
5976
5977/*
5978 * Delete an instruction, free what it contains.
5979 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01005980 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005981delete_instr(isn_T *isn)
5982{
5983 switch (isn->isn_type)
5984 {
5985 case ISN_EXEC:
5986 case ISN_LOADENV:
5987 case ISN_LOADG:
5988 case ISN_LOADOPT:
5989 case ISN_MEMBER:
5990 case ISN_PUSHEXC:
5991 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005992 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005994 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005995 vim_free(isn->isn_arg.string);
5996 break;
5997
5998 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005999 case ISN_STORES:
6000 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001 break;
6002
6003 case ISN_STOREOPT:
6004 vim_free(isn->isn_arg.storeopt.so_name);
6005 break;
6006
6007 case ISN_PUSHBLOB: // push blob isn_arg.blob
6008 blob_unref(isn->isn_arg.blob);
6009 break;
6010
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006011 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006012#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006013 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006014#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006015 break;
6016
6017 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006018#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006019 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006020#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006021 break;
6022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 case ISN_UCALL:
6024 vim_free(isn->isn_arg.ufunc.cuf_name);
6025 break;
6026
6027 case ISN_2BOOL:
6028 case ISN_2STRING:
6029 case ISN_ADDBLOB:
6030 case ISN_ADDLIST:
6031 case ISN_BCALL:
6032 case ISN_CATCH:
6033 case ISN_CHECKNR:
6034 case ISN_CHECKTYPE:
6035 case ISN_COMPAREANY:
6036 case ISN_COMPAREBLOB:
6037 case ISN_COMPAREBOOL:
6038 case ISN_COMPAREDICT:
6039 case ISN_COMPAREFLOAT:
6040 case ISN_COMPAREFUNC:
6041 case ISN_COMPARELIST:
6042 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006043 case ISN_COMPARESPECIAL:
6044 case ISN_COMPARESTRING:
6045 case ISN_CONCAT:
6046 case ISN_DCALL:
6047 case ISN_DROP:
6048 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006049 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050 case ISN_ENDTRY:
6051 case ISN_FOR:
6052 case ISN_FUNCREF:
6053 case ISN_INDEX:
6054 case ISN_JUMP:
6055 case ISN_LOAD:
6056 case ISN_LOADSCRIPT:
6057 case ISN_LOADREG:
6058 case ISN_LOADV:
6059 case ISN_NEGATENR:
6060 case ISN_NEWDICT:
6061 case ISN_NEWLIST:
6062 case ISN_OPNR:
6063 case ISN_OPFLOAT:
6064 case ISN_OPANY:
6065 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006066 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006067 case ISN_PUSHF:
6068 case ISN_PUSHNR:
6069 case ISN_PUSHBOOL:
6070 case ISN_PUSHSPEC:
6071 case ISN_RETURN:
6072 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006073 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006075 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076 case ISN_STORESCRIPT:
6077 case ISN_THROW:
6078 case ISN_TRY:
6079 // nothing allocated
6080 break;
6081 }
6082}
6083
6084/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006085 * Free all instructions for "dfunc".
6086 */
6087 static void
6088delete_def_function_contents(dfunc_T *dfunc)
6089{
6090 int idx;
6091
6092 ga_clear(&dfunc->df_def_args_isn);
6093
6094 if (dfunc->df_instr != NULL)
6095 {
6096 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6097 delete_instr(dfunc->df_instr + idx);
6098 VIM_CLEAR(dfunc->df_instr);
6099 }
6100
6101 dfunc->df_deleted = TRUE;
6102}
6103
6104/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006105 * When a user function is deleted, delete any associated def function.
6106 */
6107 void
6108delete_def_function(ufunc_T *ufunc)
6109{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006110 if (ufunc->uf_dfunc_idx >= 0)
6111 {
6112 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6113 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006114
Bram Moolenaar20431c92020-03-20 18:39:46 +01006115 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116 }
6117}
6118
6119#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006120/*
6121 * Free all functions defined with ":def".
6122 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006123 void
6124free_def_functions(void)
6125{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006126 int idx;
6127
6128 for (idx = 0; idx < def_functions.ga_len; ++idx)
6129 {
6130 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6131
6132 delete_def_function_contents(dfunc);
6133 }
6134
6135 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006136}
6137#endif
6138
6139
6140#endif // FEAT_EVAL