blob: 772e29f353d1fb8c64f2cfa068358c7e4a6be788 [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))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001651 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001652 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001653 return &t_any;
1654 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001655 }
1656 p = skipwhite(p);
1657 if (argcount == MAX_FUNC_ARGS)
1658 {
1659 emsg(_("E740: Too many argument types"));
1660 return &t_any;
1661 }
1662 }
1663
1664 p = skipwhite(p);
1665 if (*p != ')')
1666 {
1667 emsg(_(e_missing_close));
1668 return &t_any;
1669 }
1670 *arg = p + 1;
1671 }
1672 if (**arg == ':')
1673 {
1674 // parse return type
1675 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001676 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001677 semsg(_(e_white_after), ":");
1678 *arg = skipwhite(*arg);
1679 ret_type = parse_type(arg, type_gap);
1680 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001681 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001682 type = get_func_type(ret_type, argcount, type_gap);
1683 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001684 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001685 type = alloc_func_type(ret_type, argcount, type_gap);
1686 type->tt_flags = flags;
1687 if (argcount > 0)
1688 {
1689 type->tt_argcount = argcount;
1690 type->tt_min_argcount = first_optional == -1
1691 ? argcount : first_optional;
1692 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001693 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001694 return &t_any;
1695 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001696 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001697 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001698 }
1699 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 }
1701 break;
1702 case 'j':
1703 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1704 {
1705 *arg += len;
1706 return &t_job;
1707 }
1708 break;
1709 case 'l':
1710 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1711 {
1712 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001713 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 }
1715 break;
1716 case 'n':
1717 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1718 {
1719 *arg += len;
1720 return &t_number;
1721 }
1722 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 case 's':
1724 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1725 {
1726 *arg += len;
1727 return &t_string;
1728 }
1729 break;
1730 case 'v':
1731 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1732 {
1733 *arg += len;
1734 return &t_void;
1735 }
1736 break;
1737 }
1738
1739 semsg(_("E1010: Type not recognized: %s"), *arg);
1740 return &t_any;
1741}
1742
1743/*
1744 * Check if "type1" and "type2" are exactly the same.
1745 */
1746 static int
1747equal_type(type_T *type1, type_T *type2)
1748{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001749 int i;
1750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 if (type1->tt_type != type2->tt_type)
1752 return FALSE;
1753 switch (type1->tt_type)
1754 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001756 case VAR_ANY:
1757 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 case VAR_SPECIAL:
1759 case VAR_BOOL:
1760 case VAR_NUMBER:
1761 case VAR_FLOAT:
1762 case VAR_STRING:
1763 case VAR_BLOB:
1764 case VAR_JOB:
1765 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001766 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 case VAR_LIST:
1768 case VAR_DICT:
1769 return equal_type(type1->tt_member, type2->tt_member);
1770 case VAR_FUNC:
1771 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001772 if (!equal_type(type1->tt_member, type2->tt_member)
1773 || type1->tt_argcount != type2->tt_argcount)
1774 return FALSE;
1775 if (type1->tt_argcount < 0
1776 || type1->tt_args == NULL || type2->tt_args == NULL)
1777 return TRUE;
1778 for (i = 0; i < type1->tt_argcount; ++i)
1779 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
1780 return FALSE;
1781 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 }
1783 return TRUE;
1784}
1785
1786/*
1787 * Find the common type of "type1" and "type2" and put it in "dest".
1788 * "type2" and "dest" may be the same.
1789 */
1790 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02001791common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792{
1793 if (equal_type(type1, type2))
1794 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001795 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 return;
1797 }
1798
1799 if (type1->tt_type == type2->tt_type)
1800 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1802 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001803 type_T *common;
1804
Bram Moolenaard77a8522020-04-03 21:59:57 +02001805 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001806 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001807 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001808 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001809 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 return;
1811 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001812 if (type1->tt_type == VAR_FUNC)
1813 {
1814 type_T *common;
1815
1816 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
1817 if (type1->tt_argcount == type2->tt_argcount
1818 && type1->tt_argcount >= 0)
1819 {
1820 int argcount = type1->tt_argcount;
1821 int i;
1822
1823 *dest = alloc_func_type(common, argcount, type_gap);
1824 if (type1->tt_args != NULL && type2->tt_args != NULL)
1825 {
1826 (*dest)->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
1827 if ((*dest)->tt_args != NULL)
1828 for (i = 0; i < argcount; ++i)
1829 common_type(type1->tt_args[i], type2->tt_args[i],
1830 &(*dest)->tt_args[i], type_gap);
1831 }
1832 }
1833 else
1834 *dest = alloc_func_type(common, -1, type_gap);
1835 return;
1836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 }
1838
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001839 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840}
1841
1842 char *
1843vartype_name(vartype_T type)
1844{
1845 switch (type)
1846 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001847 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02001848 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 case VAR_SPECIAL: return "special";
1851 case VAR_BOOL: return "bool";
1852 case VAR_NUMBER: return "number";
1853 case VAR_FLOAT: return "float";
1854 case VAR_STRING: return "string";
1855 case VAR_BLOB: return "blob";
1856 case VAR_JOB: return "job";
1857 case VAR_CHANNEL: return "channel";
1858 case VAR_LIST: return "list";
1859 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001860
1861 case VAR_FUNC:
1862 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02001864 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865}
1866
1867/*
1868 * Return the name of a type.
1869 * The result may be in allocated memory, in which case "tofree" is set.
1870 */
1871 char *
1872type_name(type_T *type, char **tofree)
1873{
1874 char *name = vartype_name(type->tt_type);
1875
1876 *tofree = NULL;
1877 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1878 {
1879 char *member_free;
1880 char *member_name = type_name(type->tt_member, &member_free);
1881 size_t len;
1882
1883 len = STRLEN(name) + STRLEN(member_name) + 3;
1884 *tofree = alloc(len);
1885 if (*tofree != NULL)
1886 {
1887 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1888 vim_free(member_free);
1889 return *tofree;
1890 }
1891 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001892 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001893 {
1894 garray_T ga;
1895 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001896 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001897
1898 ga_init2(&ga, 1, 100);
1899 if (ga_grow(&ga, 20) == FAIL)
1900 return "[unknown]";
1901 *tofree = ga.ga_data;
1902 STRCPY(ga.ga_data, "func(");
1903 ga.ga_len += 5;
1904
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001905 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001906 {
1907 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001908 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001909 int len;
1910
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001911 if (type->tt_args == NULL)
1912 arg_type = "[unknown]";
1913 else
1914 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001915 if (i > 0)
1916 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001917 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001918 ga.ga_len += 2;
1919 }
1920 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001921 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001922 {
1923 vim_free(arg_free);
1924 return "[unknown]";
1925 }
1926 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001927 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001928 {
1929 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
1930 ga.ga_len += 3;
1931 }
1932 else if (i >= type->tt_min_argcount)
1933 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001934 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001935 ga.ga_len += len;
1936 vim_free(arg_free);
1937 }
1938
1939 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001940 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001941 else
1942 {
1943 char *ret_free;
1944 char *ret_name = type_name(type->tt_member, &ret_free);
1945 int len;
1946
1947 len = (int)STRLEN(ret_name) + 4;
1948 if (ga_grow(&ga, len) == FAIL)
1949 {
1950 vim_free(ret_free);
1951 return "[unknown]";
1952 }
1953 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001954 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
1955 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001956 vim_free(ret_free);
1957 }
1958 return ga.ga_data;
1959 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960
1961 return name;
1962}
1963
1964/*
1965 * Find "name" in script-local items of script "sid".
1966 * Returns the index in "sn_var_vals" if found.
1967 * If found but not in "sn_var_vals" returns -1.
1968 * If not found returns -2.
1969 */
1970 int
1971get_script_item_idx(int sid, char_u *name, int check_writable)
1972{
1973 hashtab_T *ht;
1974 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001975 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 int idx;
1977
1978 // First look the name up in the hashtable.
1979 if (sid <= 0 || sid > script_items.ga_len)
1980 return -1;
1981 ht = &SCRIPT_VARS(sid);
1982 di = find_var_in_ht(ht, 0, name, TRUE);
1983 if (di == NULL)
1984 return -2;
1985
1986 // Now find the svar_T index in sn_var_vals.
1987 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1988 {
1989 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1990
1991 if (sv->sv_tv == &di->di_tv)
1992 {
1993 if (check_writable && sv->sv_const)
1994 semsg(_(e_readonlyvar), name);
1995 return idx;
1996 }
1997 }
1998 return -1;
1999}
2000
2001/*
2002 * Find "name" in imported items of the current script/
2003 */
2004 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002005find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002007 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 int idx;
2009
2010 if (cctx != NULL)
2011 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2012 {
2013 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2014 + idx;
2015
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002016 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2017 : STRLEN(import->imp_name) == len
2018 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 return import;
2020 }
2021
2022 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2023 {
2024 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2025
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002026 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2027 : STRLEN(import->imp_name) == len
2028 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 return import;
2030 }
2031 return NULL;
2032}
2033
2034/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002035 * Free all imported variables.
2036 */
2037 static void
2038free_imported(cctx_T *cctx)
2039{
2040 int idx;
2041
2042 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2043 {
2044 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2045
2046 vim_free(import->imp_name);
2047 }
2048 ga_clear(&cctx->ctx_imports);
2049}
2050
2051/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002052 * Generate an instruction to load script-local variable "name", without the
2053 * leading "s:".
2054 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055 */
2056 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002057compile_load_scriptvar(
2058 cctx_T *cctx,
2059 char_u *name, // variable NUL terminated
2060 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002061 char_u **end, // end of variable
2062 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002064 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2066 imported_T *import;
2067
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002068 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002070 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002071 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2072 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 }
2074 if (idx >= 0)
2075 {
2076 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2077
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002078 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 current_sctx.sc_sid, idx, sv->sv_type);
2080 return OK;
2081 }
2082
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002083 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084 if (import != NULL)
2085 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002086 if (import->imp_all)
2087 {
2088 char_u *p = skipwhite(*end);
2089 int name_len;
2090 ufunc_T *ufunc;
2091 type_T *type;
2092
2093 // Used "import * as Name", need to lookup the member.
2094 if (*p != '.')
2095 {
2096 semsg(_("E1060: expected dot after name: %s"), start);
2097 return FAIL;
2098 }
2099 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002100 if (VIM_ISWHITE(*p))
2101 {
2102 emsg(_("E1074: no white space allowed after dot"));
2103 return FAIL;
2104 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002105
2106 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2107 // TODO: what if it is a function?
2108 if (idx < 0)
2109 return FAIL;
2110 *end = p;
2111
2112 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2113 import->imp_sid,
2114 idx,
2115 type);
2116 }
2117 else
2118 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002119 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002120 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2121 import->imp_sid,
2122 import->imp_var_vals_idx,
2123 import->imp_type);
2124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 return OK;
2126 }
2127
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002128 if (error)
2129 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130 return FAIL;
2131}
2132
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002133 static int
2134generate_funcref(cctx_T *cctx, char_u *name)
2135{
2136 ufunc_T *ufunc = find_func(name, cctx);
2137
2138 if (ufunc == NULL)
2139 return FAIL;
2140
2141 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2142}
2143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144/*
2145 * Compile a variable name into a load instruction.
2146 * "end" points to just after the name.
2147 * When "error" is FALSE do not give an error when not found.
2148 */
2149 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002150compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151{
2152 type_T *type;
2153 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002154 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002156 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157
2158 if (*(*arg + 1) == ':')
2159 {
2160 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002161 if (end <= *arg + 2)
2162 name = vim_strsave((char_u *)"[empty]");
2163 else
2164 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165 if (name == NULL)
2166 return FAIL;
2167
2168 if (**arg == 'v')
2169 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002170 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 }
2172 else if (**arg == 'g')
2173 {
2174 // Global variables can be defined later, thus we don't check if it
2175 // exists, give error at runtime.
2176 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2177 }
2178 else if (**arg == 's')
2179 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002180 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002182 else if (**arg == 'b')
2183 {
2184 semsg("Namespace b: not supported yet: %s", *arg);
2185 goto theend;
2186 }
2187 else if (**arg == 'w')
2188 {
2189 semsg("Namespace w: not supported yet: %s", *arg);
2190 goto theend;
2191 }
2192 else if (**arg == 't')
2193 {
2194 semsg("Namespace t: not supported yet: %s", *arg);
2195 goto theend;
2196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 else
2198 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002199 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 goto theend;
2201 }
2202 }
2203 else
2204 {
2205 size_t len = end - *arg;
2206 int idx;
2207 int gen_load = FALSE;
2208
2209 name = vim_strnsave(*arg, end - *arg);
2210 if (name == NULL)
2211 return FAIL;
2212
2213 idx = lookup_arg(*arg, len, cctx);
2214 if (idx >= 0)
2215 {
2216 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2217 type = cctx->ctx_ufunc->uf_arg_types[idx];
2218 else
2219 type = &t_any;
2220
2221 // Arguments are located above the frame pointer.
2222 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2223 if (cctx->ctx_ufunc->uf_va_name != NULL)
2224 --idx;
2225 gen_load = TRUE;
2226 }
2227 else if (lookup_vararg(*arg, len, cctx))
2228 {
2229 // varargs is always the last argument
2230 idx = -STACK_FRAME_SIZE - 1;
2231 type = cctx->ctx_ufunc->uf_va_type;
2232 gen_load = TRUE;
2233 }
2234 else
2235 {
2236 idx = lookup_local(*arg, len, cctx);
2237 if (idx >= 0)
2238 {
2239 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
2240 gen_load = TRUE;
2241 }
2242 else
2243 {
2244 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2245 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2246 res = generate_PUSHBOOL(cctx, **arg == 't'
2247 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002248 else
2249 {
2250 // "var" can be script-local even without using "s:" if it
2251 // already exists.
2252 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2253 == SCRIPT_VERSION_VIM9
2254 || lookup_script(*arg, len) == OK)
2255 res = compile_load_scriptvar(cctx, name, *arg, &end,
2256 FALSE);
2257
2258 // When the name starts with an uppercase letter or "x:" it
2259 // can be a user defined function.
2260 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2261 res = generate_funcref(cctx, name);
2262 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 }
2264 }
2265 if (gen_load)
2266 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
2267 }
2268
2269 *arg = end;
2270
2271theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002272 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 semsg(_(e_var_notfound), name);
2274 vim_free(name);
2275 return res;
2276}
2277
2278/*
2279 * Compile the argument expressions.
2280 * "arg" points to just after the "(" and is advanced to after the ")"
2281 */
2282 static int
2283compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2284{
2285 char_u *p = *arg;
2286
2287 while (*p != NUL && *p != ')')
2288 {
2289 if (compile_expr1(&p, cctx) == FAIL)
2290 return FAIL;
2291 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002292
2293 if (*p != ',' && *skipwhite(p) == ',')
2294 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002295 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002296 p = skipwhite(p);
2297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002299 {
2300 ++p;
2301 if (!VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002302 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002303 }
2304 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002306 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 if (*p != ')')
2308 {
2309 emsg(_(e_missing_close));
2310 return FAIL;
2311 }
2312 *arg = p + 1;
2313 return OK;
2314}
2315
2316/*
2317 * Compile a function call: name(arg1, arg2)
2318 * "arg" points to "name", "arg + varlen" to the "(".
2319 * "argcount_init" is 1 for "value->method()"
2320 * Instructions:
2321 * EVAL arg1
2322 * EVAL arg2
2323 * BCALL / DCALL / UCALL
2324 */
2325 static int
2326compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2327{
2328 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002329 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 int argcount = argcount_init;
2331 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002332 char_u fname_buf[FLEN_FIXED + 1];
2333 char_u *tofree = NULL;
2334 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002336 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337
2338 if (varlen >= sizeof(namebuf))
2339 {
2340 semsg(_("E1011: name too long: %s"), name);
2341 return FAIL;
2342 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002343 vim_strncpy(namebuf, *arg, varlen);
2344 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345
2346 *arg = skipwhite(*arg + varlen + 1);
2347 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002348 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002350 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 {
2352 int idx;
2353
2354 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002355 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002357 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002358 else
2359 semsg(_(e_unknownfunc), namebuf);
2360 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 }
2362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002364 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002366 {
2367 res = generate_CALL(cctx, ufunc, argcount);
2368 goto theend;
2369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370
2371 // If the name is a variable, load it and use PCALL.
2372 p = namebuf;
2373 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002374 {
2375 res = generate_PCALL(cctx, argcount, FALSE);
2376 goto theend;
2377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378
2379 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002380 res = generate_UCALL(cctx, name, argcount);
2381
2382theend:
2383 vim_free(tofree);
2384 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385}
2386
2387// like NAMESPACE_CHAR but with 'a' and 'l'.
2388#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2389
2390/*
2391 * Find the end of a variable or function name. Unlike find_name_end() this
2392 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002393 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 * Return a pointer to just after the name. Equal to "arg" if there is no
2395 * valid name.
2396 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002397 static char_u *
2398to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399{
2400 char_u *p;
2401
2402 // Quick check for valid starting character.
2403 if (!eval_isnamec1(*arg))
2404 return arg;
2405
2406 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2407 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2408 // and can be used in slice "[n:]".
2409 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002410 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2412 break;
2413 return p;
2414}
2415
2416/*
2417 * Like to_name_end() but also skip over a list or dict constant.
2418 */
2419 char_u *
2420to_name_const_end(char_u *arg)
2421{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002422 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 typval_T rettv;
2424
2425 if (p == arg && *arg == '[')
2426 {
2427
2428 // Can be "[1, 2, 3]->Func()".
2429 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2430 p = arg;
2431 }
2432 else if (p == arg && *arg == '#' && arg[1] == '{')
2433 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002434 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 ++p;
2436 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2437 p = arg;
2438 }
2439 else if (p == arg && *arg == '{')
2440 {
2441 int ret = get_lambda_tv(&p, &rettv, FALSE);
2442
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002443 // Can be "{x -> ret}()".
2444 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 if (ret == NOTDONE)
2446 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2447 if (ret != OK)
2448 p = arg;
2449 }
2450
2451 return p;
2452}
2453
2454 static void
2455type_mismatch(type_T *expected, type_T *actual)
2456{
2457 char *tofree1, *tofree2;
2458
2459 semsg(_("E1013: type mismatch, expected %s but got %s"),
2460 type_name(expected, &tofree1), type_name(actual, &tofree2));
2461 vim_free(tofree1);
2462 vim_free(tofree2);
2463}
2464
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002465 static void
2466arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
2467{
2468 char *tofree1, *tofree2;
2469
2470 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
2471 argidx,
2472 type_name(expected, &tofree1), type_name(actual, &tofree2));
2473 vim_free(tofree1);
2474 vim_free(tofree2);
2475}
2476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477/*
2478 * Check if the expected and actual types match.
2479 */
2480 static int
2481check_type(type_T *expected, type_T *actual, int give_msg)
2482{
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002483 int ret = OK;
2484
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002485 // When expected is "unknown" we accept any actual type.
2486 // When expected is "any" we accept any actual type except "void".
2487 if (expected->tt_type != VAR_UNKNOWN
2488 && (expected->tt_type != VAR_ANY || actual->tt_type == VAR_VOID))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 {
2490 if (expected->tt_type != actual->tt_type)
2491 {
2492 if (give_msg)
2493 type_mismatch(expected, actual);
2494 return FAIL;
2495 }
2496 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2497 {
Bram Moolenaar4c683752020-04-05 21:38:23 +02002498 // "unknown" is used for an empty list or dict
2499 if (actual->tt_member != &t_unknown)
Bram Moolenaar436472f2020-02-20 22:54:43 +01002500 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002502 else if (expected->tt_type == VAR_FUNC)
2503 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002504 if (expected->tt_member != &t_unknown)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002505 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
2506 if (ret == OK && expected->tt_argcount != -1
2507 && (actual->tt_argcount < expected->tt_min_argcount
2508 || actual->tt_argcount > expected->tt_argcount))
2509 ret = FAIL;
2510 }
2511 if (ret == FAIL && give_msg)
2512 type_mismatch(expected, actual);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 }
Bram Moolenaar89228602020-04-05 22:14:54 +02002514 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515}
2516
2517/*
2518 * Check that
2519 * - "actual" is "expected" type or
2520 * - "actual" is a type that can be "expected" type: add a runtime check; or
2521 * - return FAIL.
2522 */
2523 static int
2524need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2525{
Bram Moolenaar89228602020-04-05 22:14:54 +02002526 if (check_type(expected, actual, FALSE) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 return OK;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002528 if (actual->tt_type != VAR_ANY && actual->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 {
2530 type_mismatch(expected, actual);
2531 return FAIL;
2532 }
2533 generate_TYPECHECK(cctx, expected, offset);
2534 return OK;
2535}
2536
2537/*
2538 * parse a list: [expr, expr]
2539 * "*arg" points to the '['.
2540 */
2541 static int
2542compile_list(char_u **arg, cctx_T *cctx)
2543{
2544 char_u *p = skipwhite(*arg + 1);
2545 int count = 0;
2546
2547 while (*p != ']')
2548 {
2549 if (*p == NUL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002550 {
2551 semsg(_(e_list_end), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 return FAIL;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002553 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 if (compile_expr1(&p, cctx) == FAIL)
2555 break;
2556 ++count;
2557 if (*p == ',')
2558 ++p;
2559 p = skipwhite(p);
2560 }
2561 *arg = p + 1;
2562
2563 generate_NEWLIST(cctx, count);
2564 return OK;
2565}
2566
2567/*
2568 * parse a lambda: {arg, arg -> expr}
2569 * "*arg" points to the '{'.
2570 */
2571 static int
2572compile_lambda(char_u **arg, cctx_T *cctx)
2573{
2574 garray_T *instr = &cctx->ctx_instr;
2575 typval_T rettv;
2576 ufunc_T *ufunc;
2577
2578 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002579 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002581
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002583 ++ufunc->uf_refcount;
2584 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002585 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586
2587 // The function will have one line: "return {expr}".
2588 // Compile it into instructions.
2589 compile_def_function(ufunc, TRUE);
2590
2591 if (ufunc->uf_dfunc_idx >= 0)
2592 {
2593 if (ga_grow(instr, 1) == FAIL)
2594 return FAIL;
2595 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2596 return OK;
2597 }
2598 return FAIL;
2599}
2600
2601/*
2602 * Compile a lamda call: expr->{lambda}(args)
2603 * "arg" points to the "{".
2604 */
2605 static int
2606compile_lambda_call(char_u **arg, cctx_T *cctx)
2607{
2608 ufunc_T *ufunc;
2609 typval_T rettv;
2610 int argcount = 1;
2611 int ret = FAIL;
2612
2613 // Get the funcref in "rettv".
2614 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2615 return FAIL;
2616
2617 if (**arg != '(')
2618 {
2619 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002620 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 else
2622 semsg(_(e_missing_paren), "lambda");
2623 clear_tv(&rettv);
2624 return FAIL;
2625 }
2626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 ufunc = rettv.vval.v_partial->pt_func;
2628 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002629 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002630 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002631
2632 // The function will have one line: "return {expr}".
2633 // Compile it into instructions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 compile_def_function(ufunc, TRUE);
2635
2636 // compile the arguments
2637 *arg = skipwhite(*arg + 1);
2638 if (compile_arguments(arg, cctx, &argcount) == OK)
2639 // call the compiled function
2640 ret = generate_CALL(cctx, ufunc, argcount);
2641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 return ret;
2643}
2644
2645/*
2646 * parse a dict: {'key': val} or #{key: val}
2647 * "*arg" points to the '{'.
2648 */
2649 static int
2650compile_dict(char_u **arg, cctx_T *cctx, int literal)
2651{
2652 garray_T *instr = &cctx->ctx_instr;
2653 int count = 0;
2654 dict_T *d = dict_alloc();
2655 dictitem_T *item;
2656
2657 if (d == NULL)
2658 return FAIL;
2659 *arg = skipwhite(*arg + 1);
2660 while (**arg != '}' && **arg != NUL)
2661 {
2662 char_u *key = NULL;
2663
2664 if (literal)
2665 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002666 char_u *p = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667
2668 if (p == *arg)
2669 {
2670 semsg(_("E1014: Invalid key: %s"), *arg);
2671 return FAIL;
2672 }
2673 key = vim_strnsave(*arg, p - *arg);
2674 if (generate_PUSHS(cctx, key) == FAIL)
2675 return FAIL;
2676 *arg = p;
2677 }
2678 else
2679 {
2680 isn_T *isn;
2681
2682 if (compile_expr1(arg, cctx) == FAIL)
2683 return FAIL;
2684 // TODO: check type is string
2685 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2686 if (isn->isn_type == ISN_PUSHS)
2687 key = isn->isn_arg.string;
2688 }
2689
2690 // Check for duplicate keys, if using string keys.
2691 if (key != NULL)
2692 {
2693 item = dict_find(d, key, -1);
2694 if (item != NULL)
2695 {
2696 semsg(_(e_duplicate_key), key);
2697 goto failret;
2698 }
2699 item = dictitem_alloc(key);
2700 if (item != NULL)
2701 {
2702 item->di_tv.v_type = VAR_UNKNOWN;
2703 item->di_tv.v_lock = 0;
2704 if (dict_add(d, item) == FAIL)
2705 dictitem_free(item);
2706 }
2707 }
2708
2709 *arg = skipwhite(*arg);
2710 if (**arg != ':')
2711 {
2712 semsg(_(e_missing_dict_colon), *arg);
2713 return FAIL;
2714 }
2715
2716 *arg = skipwhite(*arg + 1);
2717 if (compile_expr1(arg, cctx) == FAIL)
2718 return FAIL;
2719 ++count;
2720
2721 if (**arg == '}')
2722 break;
2723 if (**arg != ',')
2724 {
2725 semsg(_(e_missing_dict_comma), *arg);
2726 goto failret;
2727 }
2728 *arg = skipwhite(*arg + 1);
2729 }
2730
2731 if (**arg != '}')
2732 {
2733 semsg(_(e_missing_dict_end), *arg);
2734 goto failret;
2735 }
2736 *arg = *arg + 1;
2737
2738 dict_unref(d);
2739 return generate_NEWDICT(cctx, count);
2740
2741failret:
2742 dict_unref(d);
2743 return FAIL;
2744}
2745
2746/*
2747 * Compile "&option".
2748 */
2749 static int
2750compile_get_option(char_u **arg, cctx_T *cctx)
2751{
2752 typval_T rettv;
2753 char_u *start = *arg;
2754 int ret;
2755
2756 // parse the option and get the current value to get the type.
2757 rettv.v_type = VAR_UNKNOWN;
2758 ret = get_option_tv(arg, &rettv, TRUE);
2759 if (ret == OK)
2760 {
2761 // include the '&' in the name, get_option_tv() expects it.
2762 char_u *name = vim_strnsave(start, *arg - start);
2763 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2764
2765 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2766 vim_free(name);
2767 }
2768 clear_tv(&rettv);
2769
2770 return ret;
2771}
2772
2773/*
2774 * Compile "$VAR".
2775 */
2776 static int
2777compile_get_env(char_u **arg, cctx_T *cctx)
2778{
2779 char_u *start = *arg;
2780 int len;
2781 int ret;
2782 char_u *name;
2783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 ++*arg;
2785 len = get_env_len(arg);
2786 if (len == 0)
2787 {
2788 semsg(_(e_syntax_at), start - 1);
2789 return FAIL;
2790 }
2791
2792 // include the '$' in the name, get_env_tv() expects it.
2793 name = vim_strnsave(start, len + 1);
2794 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2795 vim_free(name);
2796 return ret;
2797}
2798
2799/*
2800 * Compile "@r".
2801 */
2802 static int
2803compile_get_register(char_u **arg, cctx_T *cctx)
2804{
2805 int ret;
2806
2807 ++*arg;
2808 if (**arg == NUL)
2809 {
2810 semsg(_(e_syntax_at), *arg - 1);
2811 return FAIL;
2812 }
2813 if (!valid_yank_reg(**arg, TRUE))
2814 {
2815 emsg_invreg(**arg);
2816 return FAIL;
2817 }
2818 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2819 ++*arg;
2820 return ret;
2821}
2822
2823/*
2824 * Apply leading '!', '-' and '+' to constant "rettv".
2825 */
2826 static int
2827apply_leader(typval_T *rettv, char_u *start, char_u *end)
2828{
2829 char_u *p = end;
2830
2831 // this works from end to start
2832 while (p > start)
2833 {
2834 --p;
2835 if (*p == '-' || *p == '+')
2836 {
2837 // only '-' has an effect, for '+' we only check the type
2838#ifdef FEAT_FLOAT
2839 if (rettv->v_type == VAR_FLOAT)
2840 {
2841 if (*p == '-')
2842 rettv->vval.v_float = -rettv->vval.v_float;
2843 }
2844 else
2845#endif
2846 {
2847 varnumber_T val;
2848 int error = FALSE;
2849
2850 // tv_get_number_chk() accepts a string, but we don't want that
2851 // here
2852 if (check_not_string(rettv) == FAIL)
2853 return FAIL;
2854 val = tv_get_number_chk(rettv, &error);
2855 clear_tv(rettv);
2856 if (error)
2857 return FAIL;
2858 if (*p == '-')
2859 val = -val;
2860 rettv->v_type = VAR_NUMBER;
2861 rettv->vval.v_number = val;
2862 }
2863 }
2864 else
2865 {
2866 int v = tv2bool(rettv);
2867
2868 // '!' is permissive in the type.
2869 clear_tv(rettv);
2870 rettv->v_type = VAR_BOOL;
2871 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2872 }
2873 }
2874 return OK;
2875}
2876
2877/*
2878 * Recognize v: variables that are constants and set "rettv".
2879 */
2880 static void
2881get_vim_constant(char_u **arg, typval_T *rettv)
2882{
2883 if (STRNCMP(*arg, "v:true", 6) == 0)
2884 {
2885 rettv->v_type = VAR_BOOL;
2886 rettv->vval.v_number = VVAL_TRUE;
2887 *arg += 6;
2888 }
2889 else if (STRNCMP(*arg, "v:false", 7) == 0)
2890 {
2891 rettv->v_type = VAR_BOOL;
2892 rettv->vval.v_number = VVAL_FALSE;
2893 *arg += 7;
2894 }
2895 else if (STRNCMP(*arg, "v:null", 6) == 0)
2896 {
2897 rettv->v_type = VAR_SPECIAL;
2898 rettv->vval.v_number = VVAL_NULL;
2899 *arg += 6;
2900 }
2901 else if (STRNCMP(*arg, "v:none", 6) == 0)
2902 {
2903 rettv->v_type = VAR_SPECIAL;
2904 rettv->vval.v_number = VVAL_NONE;
2905 *arg += 6;
2906 }
2907}
2908
2909/*
2910 * Compile code to apply '-', '+' and '!'.
2911 */
2912 static int
2913compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2914{
2915 char_u *p = end;
2916
2917 // this works from end to start
2918 while (p > start)
2919 {
2920 --p;
2921 if (*p == '-' || *p == '+')
2922 {
2923 int negate = *p == '-';
2924 isn_T *isn;
2925
2926 // TODO: check type
2927 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2928 {
2929 --p;
2930 if (*p == '-')
2931 negate = !negate;
2932 }
2933 // only '-' has an effect, for '+' we only check the type
2934 if (negate)
2935 isn = generate_instr(cctx, ISN_NEGATENR);
2936 else
2937 isn = generate_instr(cctx, ISN_CHECKNR);
2938 if (isn == NULL)
2939 return FAIL;
2940 }
2941 else
2942 {
2943 int invert = TRUE;
2944
2945 while (p > start && p[-1] == '!')
2946 {
2947 --p;
2948 invert = !invert;
2949 }
2950 if (generate_2BOOL(cctx, invert) == FAIL)
2951 return FAIL;
2952 }
2953 }
2954 return OK;
2955}
2956
2957/*
2958 * Compile whatever comes after "name" or "name()".
2959 */
2960 static int
2961compile_subscript(
2962 char_u **arg,
2963 cctx_T *cctx,
2964 char_u **start_leader,
2965 char_u *end_leader)
2966{
2967 for (;;)
2968 {
2969 if (**arg == '(')
2970 {
2971 int argcount = 0;
2972
2973 // funcref(arg)
2974 *arg = skipwhite(*arg + 1);
2975 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2976 return FAIL;
2977 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2978 return FAIL;
2979 }
2980 else if (**arg == '-' && (*arg)[1] == '>')
2981 {
2982 char_u *p;
2983
2984 // something->method()
2985 // Apply the '!', '-' and '+' first:
2986 // -1.0->func() works like (-1.0)->func()
2987 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2988 return FAIL;
2989 *start_leader = end_leader; // don't apply again later
2990
2991 *arg = skipwhite(*arg + 2);
2992 if (**arg == '{')
2993 {
2994 // lambda call: list->{lambda}
2995 if (compile_lambda_call(arg, cctx) == FAIL)
2996 return FAIL;
2997 }
2998 else
2999 {
3000 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003001 p = *arg;
3002 if (ASCII_ISALPHA(*p) && p[1] == ':')
3003 p += 2;
3004 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 ;
3006 if (*p != '(')
3007 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003008 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 return FAIL;
3010 }
3011 // TODO: base value may not be the first argument
3012 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
3013 return FAIL;
3014 }
3015 }
3016 else if (**arg == '[')
3017 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003018 garray_T *stack;
3019 type_T **typep;
3020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 // list index: list[123]
3022 // TODO: more arguments
3023 // TODO: dict member dict['name']
3024 *arg = skipwhite(*arg + 1);
3025 if (compile_expr1(arg, cctx) == FAIL)
3026 return FAIL;
3027
3028 if (**arg != ']')
3029 {
3030 emsg(_(e_missbrac));
3031 return FAIL;
3032 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003033 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034
3035 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3036 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003037 stack = &cctx->ctx_type_stack;
3038 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3039 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3040 {
3041 emsg(_(e_listreq));
3042 return FAIL;
3043 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003044 if ((*typep)->tt_type == VAR_LIST)
3045 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 }
3047 else if (**arg == '.' && (*arg)[1] != '.')
3048 {
3049 char_u *p;
3050
3051 ++*arg;
3052 p = *arg;
3053 // dictionary member: dict.name
3054 if (eval_isnamec1(*p))
3055 while (eval_isnamec(*p))
3056 MB_PTR_ADV(p);
3057 if (p == *arg)
3058 {
3059 semsg(_(e_syntax_at), *arg);
3060 return FAIL;
3061 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3063 return FAIL;
3064 *arg = p;
3065 }
3066 else
3067 break;
3068 }
3069
3070 // TODO - see handle_subscript():
3071 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3072 // Don't do this when "Func" is already a partial that was bound
3073 // explicitly (pt_auto is FALSE).
3074
3075 return OK;
3076}
3077
3078/*
3079 * Compile an expression at "*p" and add instructions to "instr".
3080 * "p" is advanced until after the expression, skipping white space.
3081 *
3082 * This is the equivalent of eval1(), eval2(), etc.
3083 */
3084
3085/*
3086 * number number constant
3087 * 0zFFFFFFFF Blob constant
3088 * "string" string constant
3089 * 'string' literal string constant
3090 * &option-name option value
3091 * @r register contents
3092 * identifier variable value
3093 * function() function call
3094 * $VAR environment variable
3095 * (expression) nested expression
3096 * [expr, expr] List
3097 * {key: val, key: val} Dictionary
3098 * #{key: val, key: val} Dictionary with literal keys
3099 *
3100 * Also handle:
3101 * ! in front logical NOT
3102 * - in front unary minus
3103 * + in front unary plus (ignored)
3104 * trailing (arg) funcref/partial call
3105 * trailing [] subscript in String or List
3106 * trailing .name entry in Dictionary
3107 * trailing ->name() method call
3108 */
3109 static int
3110compile_expr7(char_u **arg, cctx_T *cctx)
3111{
3112 typval_T rettv;
3113 char_u *start_leader, *end_leader;
3114 int ret = OK;
3115
3116 /*
3117 * Skip '!', '-' and '+' characters. They are handled later.
3118 */
3119 start_leader = *arg;
3120 while (**arg == '!' || **arg == '-' || **arg == '+')
3121 *arg = skipwhite(*arg + 1);
3122 end_leader = *arg;
3123
3124 rettv.v_type = VAR_UNKNOWN;
3125 switch (**arg)
3126 {
3127 /*
3128 * Number constant.
3129 */
3130 case '0': // also for blob starting with 0z
3131 case '1':
3132 case '2':
3133 case '3':
3134 case '4':
3135 case '5':
3136 case '6':
3137 case '7':
3138 case '8':
3139 case '9':
3140 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3141 return FAIL;
3142 break;
3143
3144 /*
3145 * String constant: "string".
3146 */
3147 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3148 return FAIL;
3149 break;
3150
3151 /*
3152 * Literal string constant: 'str''ing'.
3153 */
3154 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3155 return FAIL;
3156 break;
3157
3158 /*
3159 * Constant Vim variable.
3160 */
3161 case 'v': get_vim_constant(arg, &rettv);
3162 ret = NOTDONE;
3163 break;
3164
3165 /*
3166 * List: [expr, expr]
3167 */
3168 case '[': ret = compile_list(arg, cctx);
3169 break;
3170
3171 /*
3172 * Dictionary: #{key: val, key: val}
3173 */
3174 case '#': if ((*arg)[1] == '{')
3175 {
3176 ++*arg;
3177 ret = compile_dict(arg, cctx, TRUE);
3178 }
3179 else
3180 ret = NOTDONE;
3181 break;
3182
3183 /*
3184 * Lambda: {arg, arg -> expr}
3185 * Dictionary: {'key': val, 'key': val}
3186 */
3187 case '{': {
3188 char_u *start = skipwhite(*arg + 1);
3189
3190 // Find out what comes after the arguments.
3191 ret = get_function_args(&start, '-', NULL,
3192 NULL, NULL, NULL, TRUE);
3193 if (ret != FAIL && *start == '>')
3194 ret = compile_lambda(arg, cctx);
3195 else
3196 ret = compile_dict(arg, cctx, FALSE);
3197 }
3198 break;
3199
3200 /*
3201 * Option value: &name
3202 */
3203 case '&': ret = compile_get_option(arg, cctx);
3204 break;
3205
3206 /*
3207 * Environment variable: $VAR.
3208 */
3209 case '$': ret = compile_get_env(arg, cctx);
3210 break;
3211
3212 /*
3213 * Register contents: @r.
3214 */
3215 case '@': ret = compile_get_register(arg, cctx);
3216 break;
3217 /*
3218 * nested expression: (expression).
3219 */
3220 case '(': *arg = skipwhite(*arg + 1);
3221 ret = compile_expr1(arg, cctx); // recursive!
3222 *arg = skipwhite(*arg);
3223 if (**arg == ')')
3224 ++*arg;
3225 else if (ret == OK)
3226 {
3227 emsg(_(e_missing_close));
3228 ret = FAIL;
3229 }
3230 break;
3231
3232 default: ret = NOTDONE;
3233 break;
3234 }
3235 if (ret == FAIL)
3236 return FAIL;
3237
3238 if (rettv.v_type != VAR_UNKNOWN)
3239 {
3240 // apply the '!', '-' and '+' before the constant
3241 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3242 {
3243 clear_tv(&rettv);
3244 return FAIL;
3245 }
3246 start_leader = end_leader; // don't apply again below
3247
3248 // push constant
3249 switch (rettv.v_type)
3250 {
3251 case VAR_BOOL:
3252 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3253 break;
3254 case VAR_SPECIAL:
3255 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3256 break;
3257 case VAR_NUMBER:
3258 generate_PUSHNR(cctx, rettv.vval.v_number);
3259 break;
3260#ifdef FEAT_FLOAT
3261 case VAR_FLOAT:
3262 generate_PUSHF(cctx, rettv.vval.v_float);
3263 break;
3264#endif
3265 case VAR_BLOB:
3266 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3267 rettv.vval.v_blob = NULL;
3268 break;
3269 case VAR_STRING:
3270 generate_PUSHS(cctx, rettv.vval.v_string);
3271 rettv.vval.v_string = NULL;
3272 break;
3273 default:
3274 iemsg("constant type missing");
3275 return FAIL;
3276 }
3277 }
3278 else if (ret == NOTDONE)
3279 {
3280 char_u *p;
3281 int r;
3282
3283 if (!eval_isnamec1(**arg))
3284 {
3285 semsg(_("E1015: Name expected: %s"), *arg);
3286 return FAIL;
3287 }
3288
3289 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003290 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 if (*p == '(')
3292 r = compile_call(arg, p - *arg, cctx, 0);
3293 else
3294 r = compile_load(arg, p, cctx, TRUE);
3295 if (r == FAIL)
3296 return FAIL;
3297 }
3298
3299 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3300 return FAIL;
3301
3302 // Now deal with prefixed '-', '+' and '!', if not done already.
3303 return compile_leader(cctx, start_leader, end_leader);
3304}
3305
3306/*
3307 * * number multiplication
3308 * / number division
3309 * % number modulo
3310 */
3311 static int
3312compile_expr6(char_u **arg, cctx_T *cctx)
3313{
3314 char_u *op;
3315
3316 // get the first variable
3317 if (compile_expr7(arg, cctx) == FAIL)
3318 return FAIL;
3319
3320 /*
3321 * Repeat computing, until no "*", "/" or "%" is following.
3322 */
3323 for (;;)
3324 {
3325 op = skipwhite(*arg);
3326 if (*op != '*' && *op != '/' && *op != '%')
3327 break;
3328 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
3329 {
3330 char_u buf[3];
3331
3332 vim_strncpy(buf, op, 1);
3333 semsg(_(e_white_both), buf);
3334 }
3335 *arg = skipwhite(op + 1);
3336
3337 // get the second variable
3338 if (compile_expr7(arg, cctx) == FAIL)
3339 return FAIL;
3340
3341 generate_two_op(cctx, op);
3342 }
3343
3344 return OK;
3345}
3346
3347/*
3348 * + number addition
3349 * - number subtraction
3350 * .. string concatenation
3351 */
3352 static int
3353compile_expr5(char_u **arg, cctx_T *cctx)
3354{
3355 char_u *op;
3356 int oplen;
3357
3358 // get the first variable
3359 if (compile_expr6(arg, cctx) == FAIL)
3360 return FAIL;
3361
3362 /*
3363 * Repeat computing, until no "+", "-" or ".." is following.
3364 */
3365 for (;;)
3366 {
3367 op = skipwhite(*arg);
3368 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3369 break;
3370 oplen = (*op == '.' ? 2 : 1);
3371
3372 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
3373 {
3374 char_u buf[3];
3375
3376 vim_strncpy(buf, op, oplen);
3377 semsg(_(e_white_both), buf);
3378 }
3379
3380 *arg = skipwhite(op + oplen);
3381
3382 // get the second variable
3383 if (compile_expr6(arg, cctx) == FAIL)
3384 return FAIL;
3385
3386 if (*op == '.')
3387 {
3388 if (may_generate_2STRING(-2, cctx) == FAIL
3389 || may_generate_2STRING(-1, cctx) == FAIL)
3390 return FAIL;
3391 generate_instr_drop(cctx, ISN_CONCAT, 1);
3392 }
3393 else
3394 generate_two_op(cctx, op);
3395 }
3396
3397 return OK;
3398}
3399
Bram Moolenaar080457c2020-03-03 21:53:32 +01003400 static exptype_T
3401get_compare_type(char_u *p, int *len, int *type_is)
3402{
3403 exptype_T type = EXPR_UNKNOWN;
3404 int i;
3405
3406 switch (p[0])
3407 {
3408 case '=': if (p[1] == '=')
3409 type = EXPR_EQUAL;
3410 else if (p[1] == '~')
3411 type = EXPR_MATCH;
3412 break;
3413 case '!': if (p[1] == '=')
3414 type = EXPR_NEQUAL;
3415 else if (p[1] == '~')
3416 type = EXPR_NOMATCH;
3417 break;
3418 case '>': if (p[1] != '=')
3419 {
3420 type = EXPR_GREATER;
3421 *len = 1;
3422 }
3423 else
3424 type = EXPR_GEQUAL;
3425 break;
3426 case '<': if (p[1] != '=')
3427 {
3428 type = EXPR_SMALLER;
3429 *len = 1;
3430 }
3431 else
3432 type = EXPR_SEQUAL;
3433 break;
3434 case 'i': if (p[1] == 's')
3435 {
3436 // "is" and "isnot"; but not a prefix of a name
3437 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3438 *len = 5;
3439 i = p[*len];
3440 if (!isalnum(i) && i != '_')
3441 {
3442 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3443 *type_is = TRUE;
3444 }
3445 }
3446 break;
3447 }
3448 return type;
3449}
3450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451/*
3452 * expr5a == expr5b
3453 * expr5a =~ expr5b
3454 * expr5a != expr5b
3455 * expr5a !~ expr5b
3456 * expr5a > expr5b
3457 * expr5a >= expr5b
3458 * expr5a < expr5b
3459 * expr5a <= expr5b
3460 * expr5a is expr5b
3461 * expr5a isnot expr5b
3462 *
3463 * Produces instructions:
3464 * EVAL expr5a Push result of "expr5a"
3465 * EVAL expr5b Push result of "expr5b"
3466 * COMPARE one of the compare instructions
3467 */
3468 static int
3469compile_expr4(char_u **arg, cctx_T *cctx)
3470{
3471 exptype_T type = EXPR_UNKNOWN;
3472 char_u *p;
3473 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 int type_is = FALSE;
3475
3476 // get the first variable
3477 if (compile_expr5(arg, cctx) == FAIL)
3478 return FAIL;
3479
3480 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003481 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482
3483 /*
3484 * If there is a comparative operator, use it.
3485 */
3486 if (type != EXPR_UNKNOWN)
3487 {
3488 int ic = FALSE; // Default: do not ignore case
3489
3490 if (type_is && (p[len] == '?' || p[len] == '#'))
3491 {
3492 semsg(_(e_invexpr2), *arg);
3493 return FAIL;
3494 }
3495 // extra question mark appended: ignore case
3496 if (p[len] == '?')
3497 {
3498 ic = TRUE;
3499 ++len;
3500 }
3501 // extra '#' appended: match case (ignored)
3502 else if (p[len] == '#')
3503 ++len;
3504 // nothing appended: match case
3505
3506 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
3507 {
3508 char_u buf[7];
3509
3510 vim_strncpy(buf, p, len);
3511 semsg(_(e_white_both), buf);
3512 }
3513
3514 // get the second variable
3515 *arg = skipwhite(p + len);
3516 if (compile_expr5(arg, cctx) == FAIL)
3517 return FAIL;
3518
3519 generate_COMPARE(cctx, type, ic);
3520 }
3521
3522 return OK;
3523}
3524
3525/*
3526 * Compile || or &&.
3527 */
3528 static int
3529compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3530{
3531 char_u *p = skipwhite(*arg);
3532 int opchar = *op;
3533
3534 if (p[0] == opchar && p[1] == opchar)
3535 {
3536 garray_T *instr = &cctx->ctx_instr;
3537 garray_T end_ga;
3538
3539 /*
3540 * Repeat until there is no following "||" or "&&"
3541 */
3542 ga_init2(&end_ga, sizeof(int), 10);
3543 while (p[0] == opchar && p[1] == opchar)
3544 {
3545 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3546 semsg(_(e_white_both), op);
3547
3548 if (ga_grow(&end_ga, 1) == FAIL)
3549 {
3550 ga_clear(&end_ga);
3551 return FAIL;
3552 }
3553 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3554 ++end_ga.ga_len;
3555 generate_JUMP(cctx, opchar == '|'
3556 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3557
3558 // eval the next expression
3559 *arg = skipwhite(p + 2);
3560 if ((opchar == '|' ? compile_expr3(arg, cctx)
3561 : compile_expr4(arg, cctx)) == FAIL)
3562 {
3563 ga_clear(&end_ga);
3564 return FAIL;
3565 }
3566 p = skipwhite(*arg);
3567 }
3568
3569 // Fill in the end label in all jumps.
3570 while (end_ga.ga_len > 0)
3571 {
3572 isn_T *isn;
3573
3574 --end_ga.ga_len;
3575 isn = ((isn_T *)instr->ga_data)
3576 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3577 isn->isn_arg.jump.jump_where = instr->ga_len;
3578 }
3579 ga_clear(&end_ga);
3580 }
3581
3582 return OK;
3583}
3584
3585/*
3586 * expr4a && expr4a && expr4a logical AND
3587 *
3588 * Produces instructions:
3589 * EVAL expr4a Push result of "expr4a"
3590 * JUMP_AND_KEEP_IF_FALSE end
3591 * EVAL expr4b Push result of "expr4b"
3592 * JUMP_AND_KEEP_IF_FALSE end
3593 * EVAL expr4c Push result of "expr4c"
3594 * end:
3595 */
3596 static int
3597compile_expr3(char_u **arg, cctx_T *cctx)
3598{
3599 // get the first variable
3600 if (compile_expr4(arg, cctx) == FAIL)
3601 return FAIL;
3602
3603 // || and && work almost the same
3604 return compile_and_or(arg, cctx, "&&");
3605}
3606
3607/*
3608 * expr3a || expr3b || expr3c logical OR
3609 *
3610 * Produces instructions:
3611 * EVAL expr3a Push result of "expr3a"
3612 * JUMP_AND_KEEP_IF_TRUE end
3613 * EVAL expr3b Push result of "expr3b"
3614 * JUMP_AND_KEEP_IF_TRUE end
3615 * EVAL expr3c Push result of "expr3c"
3616 * end:
3617 */
3618 static int
3619compile_expr2(char_u **arg, cctx_T *cctx)
3620{
3621 // eval the first expression
3622 if (compile_expr3(arg, cctx) == FAIL)
3623 return FAIL;
3624
3625 // || and && work almost the same
3626 return compile_and_or(arg, cctx, "||");
3627}
3628
3629/*
3630 * Toplevel expression: expr2 ? expr1a : expr1b
3631 *
3632 * Produces instructions:
3633 * EVAL expr2 Push result of "expr"
3634 * JUMP_IF_FALSE alt jump if false
3635 * EVAL expr1a
3636 * JUMP_ALWAYS end
3637 * alt: EVAL expr1b
3638 * end:
3639 */
3640 static int
3641compile_expr1(char_u **arg, cctx_T *cctx)
3642{
3643 char_u *p;
3644
3645 // evaluate the first expression
3646 if (compile_expr2(arg, cctx) == FAIL)
3647 return FAIL;
3648
3649 p = skipwhite(*arg);
3650 if (*p == '?')
3651 {
3652 garray_T *instr = &cctx->ctx_instr;
3653 garray_T *stack = &cctx->ctx_type_stack;
3654 int alt_idx = instr->ga_len;
3655 int end_idx;
3656 isn_T *isn;
3657 type_T *type1;
3658 type_T *type2;
3659
3660 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3661 semsg(_(e_white_both), "?");
3662
3663 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3664
3665 // evaluate the second expression; any type is accepted
3666 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003667 if (compile_expr1(arg, cctx) == FAIL)
3668 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669
3670 // remember the type and drop it
3671 --stack->ga_len;
3672 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3673
3674 end_idx = instr->ga_len;
3675 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3676
3677 // jump here from JUMP_IF_FALSE
3678 isn = ((isn_T *)instr->ga_data) + alt_idx;
3679 isn->isn_arg.jump.jump_where = instr->ga_len;
3680
3681 // Check for the ":".
3682 p = skipwhite(*arg);
3683 if (*p != ':')
3684 {
3685 emsg(_(e_missing_colon));
3686 return FAIL;
3687 }
3688 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3689 semsg(_(e_white_both), ":");
3690
3691 // evaluate the third expression
3692 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003693 if (compile_expr1(arg, cctx) == FAIL)
3694 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695
3696 // If the types differ, the result has a more generic type.
3697 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003698 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699
3700 // jump here from JUMP_ALWAYS
3701 isn = ((isn_T *)instr->ga_data) + end_idx;
3702 isn->isn_arg.jump.jump_where = instr->ga_len;
3703 }
3704 return OK;
3705}
3706
3707/*
3708 * compile "return [expr]"
3709 */
3710 static char_u *
3711compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3712{
3713 char_u *p = arg;
3714 garray_T *stack = &cctx->ctx_type_stack;
3715 type_T *stack_type;
3716
3717 if (*p != NUL && *p != '|' && *p != '\n')
3718 {
3719 // compile return argument into instructions
3720 if (compile_expr1(&p, cctx) == FAIL)
3721 return NULL;
3722
3723 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3724 if (set_return_type)
3725 cctx->ctx_ufunc->uf_ret_type = stack_type;
3726 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3727 == FAIL)
3728 return NULL;
3729 }
3730 else
3731 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003732 // "set_return_type" cannot be TRUE, only used for a lambda which
3733 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02003734 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
3735 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 {
3737 emsg(_("E1003: Missing return value"));
3738 return NULL;
3739 }
3740
3741 // No argument, return zero.
3742 generate_PUSHNR(cctx, 0);
3743 }
3744
3745 if (generate_instr(cctx, ISN_RETURN) == NULL)
3746 return NULL;
3747
3748 // "return val | endif" is possible
3749 return skipwhite(p);
3750}
3751
3752/*
3753 * Return the length of an assignment operator, or zero if there isn't one.
3754 */
3755 int
3756assignment_len(char_u *p, int *heredoc)
3757{
3758 if (*p == '=')
3759 {
3760 if (p[1] == '<' && p[2] == '<')
3761 {
3762 *heredoc = TRUE;
3763 return 3;
3764 }
3765 return 1;
3766 }
3767 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3768 return 2;
3769 if (STRNCMP(p, "..=", 3) == 0)
3770 return 3;
3771 return 0;
3772}
3773
3774// words that cannot be used as a variable
3775static char *reserved[] = {
3776 "true",
3777 "false",
3778 NULL
3779};
3780
3781/*
3782 * Get a line for "=<<".
3783 * Return a pointer to the line in allocated memory.
3784 * Return NULL for end-of-file or some error.
3785 */
3786 static char_u *
3787heredoc_getline(
3788 int c UNUSED,
3789 void *cookie,
3790 int indent UNUSED,
3791 int do_concat UNUSED)
3792{
3793 cctx_T *cctx = (cctx_T *)cookie;
3794
3795 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003796 {
3797 iemsg("Heredoc got to end");
3798 return NULL;
3799 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 ++cctx->ctx_lnum;
3801 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3802 [cctx->ctx_lnum]);
3803}
3804
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003805typedef enum {
3806 dest_local,
3807 dest_option,
3808 dest_env,
3809 dest_global,
3810 dest_vimvar,
3811 dest_script,
3812 dest_reg,
3813} assign_dest_T;
3814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815/*
3816 * compile "let var [= expr]", "const var = expr" and "var = expr"
3817 * "arg" points to "var".
3818 */
3819 static char_u *
3820compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3821{
3822 char_u *p;
3823 char_u *ret = NULL;
3824 int var_count = 0;
3825 int semicolon = 0;
3826 size_t varlen;
3827 garray_T *instr = &cctx->ctx_instr;
3828 int idx = -1;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003829 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003832 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003834 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 int oplen = 0;
3836 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003837 type_T *type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 lvar_T *lvar;
3839 char_u *name;
3840 char_u *sp;
3841 int has_type = FALSE;
3842 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3843 int instr_count = -1;
3844
3845 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3846 if (p == NULL)
3847 return NULL;
3848 if (var_count > 0)
3849 {
3850 // TODO: let [var, var] = list
3851 emsg("Cannot handle a list yet");
3852 return NULL;
3853 }
3854
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003855 // "a: type" is declaring variable "a" with a type, not "a:".
3856 if (is_decl && p == arg + 2 && p[-1] == ':')
3857 --p;
3858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 varlen = p - arg;
3860 name = vim_strnsave(arg, (int)varlen);
3861 if (name == NULL)
3862 return NULL;
3863
Bram Moolenaar080457c2020-03-03 21:53:32 +01003864 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003866 if (*arg == '&')
3867 {
3868 int cc;
3869 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870
Bram Moolenaar080457c2020-03-03 21:53:32 +01003871 dest = dest_option;
3872 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003874 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003875 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 if (is_decl)
3878 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003879 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 goto theend;
3881 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003882 p = arg;
3883 p = find_option_end(&p, &opt_flags);
3884 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003886 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01003887 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003888 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003889 }
3890 cc = *p;
3891 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003892 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003893 *p = cc;
3894 if (opt_type == -3)
3895 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003896 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003897 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003898 }
3899 if (opt_type == -2 || opt_type == 0)
3900 type = &t_string;
3901 else
3902 type = &t_number; // both number and boolean option
3903 }
3904 else if (*arg == '$')
3905 {
3906 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003907 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003908 if (is_decl)
3909 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003910 semsg(_("E1065: Cannot declare an environment variable: %s"),
3911 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003912 goto theend;
3913 }
3914 }
3915 else if (*arg == '@')
3916 {
3917 if (!valid_yank_reg(arg[1], TRUE))
3918 {
3919 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003920 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003921 }
3922 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003923 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003924 if (is_decl)
3925 {
3926 semsg(_("E1066: Cannot declare a register: %s"), name);
3927 goto theend;
3928 }
3929 }
3930 else if (STRNCMP(arg, "g:", 2) == 0)
3931 {
3932 dest = dest_global;
3933 if (is_decl)
3934 {
3935 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3936 goto theend;
3937 }
3938 }
3939 else if (STRNCMP(arg, "v:", 2) == 0)
3940 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02003941 typval_T *vtv;
3942 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003943
Bram Moolenaar5da356e2020-04-09 19:34:43 +02003944 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003945 if (vimvaridx < 0)
3946 {
3947 semsg(_(e_var_notfound), arg);
3948 goto theend;
3949 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02003950 // We use the current value of "sandbox" here, is that OK?
3951 if (var_check_ro(di_flags, name, FALSE))
3952 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003953 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003954 vtv = get_vim_var_tv(vimvaridx);
3955 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003956 if (is_decl)
3957 {
3958 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3959 goto theend;
3960 }
3961 }
3962 else
3963 {
3964 for (idx = 0; reserved[idx] != NULL; ++idx)
3965 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003967 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 goto theend;
3969 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003970
3971 idx = lookup_local(arg, varlen, cctx);
3972 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003974 if (is_decl)
3975 {
3976 semsg(_("E1017: Variable already declared: %s"), name);
3977 goto theend;
3978 }
3979 else
3980 {
3981 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3982 if (lvar->lv_const)
3983 {
3984 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3985 goto theend;
3986 }
3987 }
3988 }
3989 else if (STRNCMP(arg, "s:", 2) == 0
3990 || lookup_script(arg, varlen) == OK
3991 || find_imported(arg, varlen, cctx) != NULL)
3992 {
3993 dest = dest_script;
3994 if (is_decl)
3995 {
3996 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003997 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003998 goto theend;
3999 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 }
4001 }
4002 }
4003
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004004 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 {
4006 if (is_decl && *p == ':')
4007 {
4008 // parse optional type: "let var: type = expr"
4009 p = skipwhite(p + 1);
4010 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 has_type = TRUE;
4012 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02004013 else if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 {
4015 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
4016 type = lvar->lv_type;
4017 }
4018 }
4019
4020 sp = p;
4021 p = skipwhite(p);
4022 op = p;
4023 oplen = assignment_len(p, &heredoc);
4024 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4025 {
4026 char_u buf[4];
4027
4028 vim_strncpy(buf, op, oplen);
4029 semsg(_(e_white_both), buf);
4030 }
4031
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004032 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004033 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004035 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 goto theend;
4037 }
4038
Bram Moolenaar080457c2020-03-03 21:53:32 +01004039 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 {
4041 if (oplen > 1 && !heredoc)
4042 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004043 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4045 name);
4046 goto theend;
4047 }
4048
4049 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004050 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004051 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4053 if (idx < 0)
4054 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004055 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 }
4057
4058 if (heredoc)
4059 {
4060 list_T *l;
4061 listitem_T *li;
4062
4063 // [let] varname =<< [trim] {end}
4064 eap->getline = heredoc_getline;
4065 eap->cookie = cctx;
4066 l = heredoc_get(eap, op + 3);
4067
4068 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004069 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 {
4071 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4072 li->li_tv.vval.v_string = NULL;
4073 }
4074 generate_NEWLIST(cctx, l->lv_len);
4075 type = &t_list_string;
4076 list_free(l);
4077 p += STRLEN(p);
4078 }
4079 else if (oplen > 0)
4080 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004081 int r;
4082 type_T *stacktype;
4083 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 // for "+=", "*=", "..=" etc. first load the current value
4086 if (*op != '=')
4087 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004088 switch (dest)
4089 {
4090 case dest_option:
4091 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004092 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004093 break;
4094 case dest_global:
4095 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4096 break;
4097 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004098 compile_load_scriptvar(cctx,
4099 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004100 break;
4101 case dest_env:
4102 // Include $ in the name here
4103 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4104 break;
4105 case dest_reg:
4106 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4107 break;
4108 case dest_vimvar:
4109 generate_LOADV(cctx, name + 2, TRUE);
4110 break;
4111 case dest_local:
4112 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
4113 break;
4114 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 }
4116
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004117 // Compile the expression. Temporarily hide the new local variable
4118 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004119 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004120 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 instr_count = instr->ga_len;
4122 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004123 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004124 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004125 ++cctx->ctx_locals.ga_len;
4126 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 goto theend;
4128
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004129 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004131 stack = &cctx->ctx_type_stack;
4132 stacktype = stack->ga_len == 0 ? &t_void
4133 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
4134 if (idx >= 0 && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004136 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
4137 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004139 if (stacktype->tt_type == VAR_VOID)
4140 {
4141 emsg(_("E1031: Cannot use void value"));
4142 goto theend;
4143 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004144 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004145 {
4146 // An empty list or dict has a &t_void member, for a
4147 // variable that implies &t_any.
4148 if (stacktype == &t_list_empty)
4149 lvar->lv_type = &t_list_any;
4150 else if (stacktype == &t_dict_empty)
4151 lvar->lv_type = &t_dict_any;
4152 else
4153 lvar->lv_type = stacktype;
4154 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004155 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004156 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4157 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004159 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004160 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 }
4162 }
4163 else if (cmdidx == CMD_const)
4164 {
4165 emsg(_("E1021: const requires a value"));
4166 goto theend;
4167 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004168 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 {
4170 emsg(_("E1022: type or initialization required"));
4171 goto theend;
4172 }
4173 else
4174 {
4175 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 if (ga_grow(instr, 1) == FAIL)
4177 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004178 switch (type->tt_type)
4179 {
4180 case VAR_BOOL:
4181 generate_PUSHBOOL(cctx, VVAL_FALSE);
4182 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004183 case VAR_FLOAT:
4184#ifdef FEAT_FLOAT
4185 generate_PUSHF(cctx, 0.0);
4186#endif
4187 break;
4188 case VAR_STRING:
4189 generate_PUSHS(cctx, NULL);
4190 break;
4191 case VAR_BLOB:
4192 generate_PUSHBLOB(cctx, NULL);
4193 break;
4194 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004195 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004196 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004197 case VAR_LIST:
4198 generate_NEWLIST(cctx, 0);
4199 break;
4200 case VAR_DICT:
4201 generate_NEWDICT(cctx, 0);
4202 break;
4203 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004204 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004205 break;
4206 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004207 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004208 break;
4209 case VAR_NUMBER:
4210 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004211 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02004212 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004213 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004214 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004215 generate_PUSHNR(cctx, 0);
4216 break;
4217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 }
4219
4220 if (oplen > 0 && *op != '=')
4221 {
4222 type_T *expected = &t_number;
4223 garray_T *stack = &cctx->ctx_type_stack;
4224 type_T *stacktype;
4225
4226 // TODO: if type is known use float or any operation
4227
4228 if (*op == '.')
4229 expected = &t_string;
4230 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4231 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4232 goto theend;
4233
4234 if (*op == '.')
4235 generate_instr_drop(cctx, ISN_CONCAT, 1);
4236 else
4237 {
4238 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4239
4240 if (isn == NULL)
4241 goto theend;
4242 switch (*op)
4243 {
4244 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4245 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4246 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4247 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4248 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4249 }
4250 }
4251 }
4252
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004253 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004255 case dest_option:
4256 generate_STOREOPT(cctx, name + 1, opt_flags);
4257 break;
4258 case dest_global:
4259 // include g: with the name, easier to execute that way
4260 generate_STORE(cctx, ISN_STOREG, 0, name);
4261 break;
4262 case dest_env:
4263 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4264 break;
4265 case dest_reg:
4266 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4267 break;
4268 case dest_vimvar:
4269 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4270 break;
4271 case dest_script:
4272 {
4273 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4274 imported_T *import = NULL;
4275 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004276
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004277 if (name[1] != ':')
4278 {
4279 import = find_imported(name, 0, cctx);
4280 if (import != NULL)
4281 sid = import->imp_sid;
4282 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004284 idx = get_script_item_idx(sid, rawname, TRUE);
4285 // TODO: specific type
4286 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004287 {
4288 char_u *name_s = name;
4289
4290 // Include s: in the name for store_var()
4291 if (name[1] != ':')
4292 {
4293 int len = (int)STRLEN(name) + 3;
4294
4295 name_s = alloc(len);
4296 if (name_s == NULL)
4297 name_s = name;
4298 else
4299 vim_snprintf((char *)name_s, len, "s:%s", name);
4300 }
4301 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4302 if (name_s != name)
4303 vim_free(name_s);
4304 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004305 else
4306 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4307 sid, idx, &t_any);
4308 }
4309 break;
4310 case dest_local:
4311 {
4312 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004314 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4315 // into ISN_STORENR
4316 if (instr->ga_len == instr_count + 1
4317 && isn->isn_type == ISN_PUSHNR)
4318 {
4319 varnumber_T val = isn->isn_arg.number;
4320 garray_T *stack = &cctx->ctx_type_stack;
4321
4322 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004323 isn->isn_arg.storenr.stnr_idx = idx;
4324 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004325 if (stack->ga_len > 0)
4326 --stack->ga_len;
4327 }
4328 else
4329 generate_STORE(cctx, ISN_STORE, idx, NULL);
4330 }
4331 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 }
4333 ret = p;
4334
4335theend:
4336 vim_free(name);
4337 return ret;
4338}
4339
4340/*
4341 * Compile an :import command.
4342 */
4343 static char_u *
4344compile_import(char_u *arg, cctx_T *cctx)
4345{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004346 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347}
4348
4349/*
4350 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4351 */
4352 static int
4353compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4354{
4355 garray_T *instr = &cctx->ctx_instr;
4356 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4357
4358 if (endlabel == NULL)
4359 return FAIL;
4360 endlabel->el_next = *el;
4361 *el = endlabel;
4362 endlabel->el_end_label = instr->ga_len;
4363
4364 generate_JUMP(cctx, when, 0);
4365 return OK;
4366}
4367
4368 static void
4369compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4370{
4371 garray_T *instr = &cctx->ctx_instr;
4372
4373 while (*el != NULL)
4374 {
4375 endlabel_T *cur = (*el);
4376 isn_T *isn;
4377
4378 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4379 isn->isn_arg.jump.jump_where = instr->ga_len;
4380 *el = cur->el_next;
4381 vim_free(cur);
4382 }
4383}
4384
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004385 static void
4386compile_free_jump_to_end(endlabel_T **el)
4387{
4388 while (*el != NULL)
4389 {
4390 endlabel_T *cur = (*el);
4391
4392 *el = cur->el_next;
4393 vim_free(cur);
4394 }
4395}
4396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397/*
4398 * Create a new scope and set up the generic items.
4399 */
4400 static scope_T *
4401new_scope(cctx_T *cctx, scopetype_T type)
4402{
4403 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4404
4405 if (scope == NULL)
4406 return NULL;
4407 scope->se_outer = cctx->ctx_scope;
4408 cctx->ctx_scope = scope;
4409 scope->se_type = type;
4410 scope->se_local_count = cctx->ctx_locals.ga_len;
4411 return scope;
4412}
4413
4414/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004415 * Free the current scope and go back to the outer scope.
4416 */
4417 static void
4418drop_scope(cctx_T *cctx)
4419{
4420 scope_T *scope = cctx->ctx_scope;
4421
4422 if (scope == NULL)
4423 {
4424 iemsg("calling drop_scope() without a scope");
4425 return;
4426 }
4427 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004428 switch (scope->se_type)
4429 {
4430 case IF_SCOPE:
4431 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4432 case FOR_SCOPE:
4433 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4434 case WHILE_SCOPE:
4435 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4436 case TRY_SCOPE:
4437 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4438 case NO_SCOPE:
4439 case BLOCK_SCOPE:
4440 break;
4441 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004442 vim_free(scope);
4443}
4444
4445/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004446 * Evaluate an expression that is a constant:
4447 * has(arg)
4448 *
4449 * Also handle:
4450 * ! in front logical NOT
4451 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004452 * Return FAIL if the expression is not a constant.
4453 */
4454 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004455evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004456{
4457 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004458 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004459 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004460
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004461 /*
4462 * Skip '!' characters. They are handled later.
4463 */
4464 start_leader = *arg;
4465 while (**arg == '!')
4466 *arg = skipwhite(*arg + 1);
4467 end_leader = *arg;
4468
4469 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004470 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004471 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004472 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4473 {
4474 tv->v_type = VAR_SPECIAL;
4475 tv->vval.v_number = VVAL_TRUE;
4476 *arg += 4;
4477 return OK;
4478 }
4479 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4480 {
4481 tv->v_type = VAR_SPECIAL;
4482 tv->vval.v_number = VVAL_FALSE;
4483 *arg += 5;
4484 return OK;
4485 }
4486
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004487 if (STRNCMP("has(", *arg, 4) == 0)
4488 {
4489 has_call = TRUE;
4490 *arg = skipwhite(*arg + 4);
4491 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004492
4493 if (**arg == '"')
4494 {
4495 if (get_string_tv(arg, tv, TRUE) == FAIL)
4496 return FAIL;
4497 }
4498 else if (**arg == '\'')
4499 {
4500 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4501 return FAIL;
4502 }
4503 else
4504 return FAIL;
4505
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004506 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004507 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004508 *arg = skipwhite(*arg);
4509 if (**arg != ')')
4510 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004511 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004512
4513 argvars[0] = *tv;
4514 argvars[1].v_type = VAR_UNKNOWN;
4515 tv->v_type = VAR_NUMBER;
4516 tv->vval.v_number = 0;
4517 f_has(argvars, tv);
4518 clear_tv(&argvars[0]);
4519
4520 while (start_leader < end_leader)
4521 {
4522 if (*start_leader == '!')
4523 tv->vval.v_number = !tv->vval.v_number;
4524 ++start_leader;
4525 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004526 }
4527
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004528 return OK;
4529}
4530
Bram Moolenaar080457c2020-03-03 21:53:32 +01004531 static int
4532evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4533{
4534 exptype_T type = EXPR_UNKNOWN;
4535 char_u *p;
4536 int len = 2;
4537 int type_is = FALSE;
4538
4539 // get the first variable
4540 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4541 return FAIL;
4542
4543 p = skipwhite(*arg);
4544 type = get_compare_type(p, &len, &type_is);
4545
4546 /*
4547 * If there is a comparative operator, use it.
4548 */
4549 if (type != EXPR_UNKNOWN)
4550 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004551 typval_T tv2;
4552 char_u *s1, *s2;
4553 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4554 int n;
4555
4556 // TODO: Only string == string is supported now
4557 if (tv->v_type != VAR_STRING)
4558 return FAIL;
4559 if (type != EXPR_EQUAL)
4560 return FAIL;
4561
4562 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004563 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004564 *arg = skipwhite(p + len);
4565 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4566 || tv2.v_type != VAR_STRING)
4567 {
4568 clear_tv(&tv2);
4569 return FAIL;
4570 }
4571 s1 = tv_get_string_buf(tv, buf1);
4572 s2 = tv_get_string_buf(&tv2, buf2);
4573 n = STRCMP(s1, s2);
4574 clear_tv(tv);
4575 clear_tv(&tv2);
4576 tv->v_type = VAR_BOOL;
4577 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004578 }
4579
4580 return OK;
4581}
4582
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004583static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4584
4585/*
4586 * Compile constant || or &&.
4587 */
4588 static int
4589evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4590{
4591 char_u *p = skipwhite(*arg);
4592 int opchar = *op;
4593
4594 if (p[0] == opchar && p[1] == opchar)
4595 {
4596 int val = tv2bool(tv);
4597
4598 /*
4599 * Repeat until there is no following "||" or "&&"
4600 */
4601 while (p[0] == opchar && p[1] == opchar)
4602 {
4603 typval_T tv2;
4604
4605 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4606 return FAIL;
4607
4608 // eval the next expression
4609 *arg = skipwhite(p + 2);
4610 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004611 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004612 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004613 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004614 {
4615 clear_tv(&tv2);
4616 return FAIL;
4617 }
4618 if ((opchar == '&') == val)
4619 {
4620 // false || tv2 or true && tv2: use tv2
4621 clear_tv(tv);
4622 *tv = tv2;
4623 val = tv2bool(tv);
4624 }
4625 else
4626 clear_tv(&tv2);
4627 p = skipwhite(*arg);
4628 }
4629 }
4630
4631 return OK;
4632}
4633
4634/*
4635 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4636 * Return FAIL if the expression is not a constant.
4637 */
4638 static int
4639evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4640{
4641 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004642 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004643 return FAIL;
4644
4645 // || and && work almost the same
4646 return evaluate_const_and_or(arg, cctx, "&&", tv);
4647}
4648
4649/*
4650 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4651 * Return FAIL if the expression is not a constant.
4652 */
4653 static int
4654evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4655{
4656 // evaluate the first expression
4657 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4658 return FAIL;
4659
4660 // || and && work almost the same
4661 return evaluate_const_and_or(arg, cctx, "||", tv);
4662}
4663
4664/*
4665 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4666 * E.g. for "has('feature')".
4667 * This does not produce error messages. "tv" should be cleared afterwards.
4668 * Return FAIL if the expression is not a constant.
4669 */
4670 static int
4671evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4672{
4673 char_u *p;
4674
4675 // evaluate the first expression
4676 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4677 return FAIL;
4678
4679 p = skipwhite(*arg);
4680 if (*p == '?')
4681 {
4682 int val = tv2bool(tv);
4683 typval_T tv2;
4684
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004685 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004686 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4687 return FAIL;
4688
4689 // evaluate the second expression; any type is accepted
4690 clear_tv(tv);
4691 *arg = skipwhite(p + 1);
4692 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4693 return FAIL;
4694
4695 // Check for the ":".
4696 p = skipwhite(*arg);
4697 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4698 return FAIL;
4699
4700 // evaluate the third expression
4701 *arg = skipwhite(p + 1);
4702 tv2.v_type = VAR_UNKNOWN;
4703 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4704 {
4705 clear_tv(&tv2);
4706 return FAIL;
4707 }
4708 if (val)
4709 {
4710 // use the expr after "?"
4711 clear_tv(&tv2);
4712 }
4713 else
4714 {
4715 // use the expr after ":"
4716 clear_tv(tv);
4717 *tv = tv2;
4718 }
4719 }
4720 return OK;
4721}
4722
4723/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 * compile "if expr"
4725 *
4726 * "if expr" Produces instructions:
4727 * EVAL expr Push result of "expr"
4728 * JUMP_IF_FALSE end
4729 * ... body ...
4730 * end:
4731 *
4732 * "if expr | else" Produces instructions:
4733 * EVAL expr Push result of "expr"
4734 * JUMP_IF_FALSE else
4735 * ... body ...
4736 * JUMP_ALWAYS end
4737 * else:
4738 * ... body ...
4739 * end:
4740 *
4741 * "if expr1 | elseif expr2 | else" Produces instructions:
4742 * EVAL expr Push result of "expr"
4743 * JUMP_IF_FALSE elseif
4744 * ... body ...
4745 * JUMP_ALWAYS end
4746 * elseif:
4747 * EVAL expr Push result of "expr"
4748 * JUMP_IF_FALSE else
4749 * ... body ...
4750 * JUMP_ALWAYS end
4751 * else:
4752 * ... body ...
4753 * end:
4754 */
4755 static char_u *
4756compile_if(char_u *arg, cctx_T *cctx)
4757{
4758 char_u *p = arg;
4759 garray_T *instr = &cctx->ctx_instr;
4760 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004761 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004763 // compile "expr"; if we know it evaluates to FALSE skip the block
4764 tv.v_type = VAR_UNKNOWN;
4765 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4766 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4767 else
4768 cctx->ctx_skip = MAYBE;
4769 clear_tv(&tv);
4770 if (cctx->ctx_skip == MAYBE)
4771 {
4772 p = arg;
4773 if (compile_expr1(&p, cctx) == FAIL)
4774 return NULL;
4775 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776
4777 scope = new_scope(cctx, IF_SCOPE);
4778 if (scope == NULL)
4779 return NULL;
4780
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004781 if (cctx->ctx_skip == MAYBE)
4782 {
4783 // "where" is set when ":elseif", "else" or ":endif" is found
4784 scope->se_u.se_if.is_if_label = instr->ga_len;
4785 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4786 }
4787 else
4788 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789
4790 return p;
4791}
4792
4793 static char_u *
4794compile_elseif(char_u *arg, cctx_T *cctx)
4795{
4796 char_u *p = arg;
4797 garray_T *instr = &cctx->ctx_instr;
4798 isn_T *isn;
4799 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004800 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801
4802 if (scope == NULL || scope->se_type != IF_SCOPE)
4803 {
4804 emsg(_(e_elseif_without_if));
4805 return NULL;
4806 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004807 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808
Bram Moolenaar158906c2020-02-06 20:39:45 +01004809 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004810 {
4811 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004813 return NULL;
4814 // previous "if" or "elseif" jumps here
4815 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4816 isn->isn_arg.jump.jump_where = instr->ga_len;
4817 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004818
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004819 // compile "expr"; if we know it evaluates to FALSE skip the block
4820 tv.v_type = VAR_UNKNOWN;
4821 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4822 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4823 else
4824 cctx->ctx_skip = MAYBE;
4825 clear_tv(&tv);
4826 if (cctx->ctx_skip == MAYBE)
4827 {
4828 p = arg;
4829 if (compile_expr1(&p, cctx) == FAIL)
4830 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004832 // "where" is set when ":elseif", "else" or ":endif" is found
4833 scope->se_u.se_if.is_if_label = instr->ga_len;
4834 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4835 }
4836 else
4837 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838
4839 return p;
4840}
4841
4842 static char_u *
4843compile_else(char_u *arg, cctx_T *cctx)
4844{
4845 char_u *p = arg;
4846 garray_T *instr = &cctx->ctx_instr;
4847 isn_T *isn;
4848 scope_T *scope = cctx->ctx_scope;
4849
4850 if (scope == NULL || scope->se_type != IF_SCOPE)
4851 {
4852 emsg(_(e_else_without_if));
4853 return NULL;
4854 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004855 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004857 // jump from previous block to the end, unless the else block is empty
4858 if (cctx->ctx_skip == MAYBE)
4859 {
4860 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004862 return NULL;
4863 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864
Bram Moolenaar158906c2020-02-06 20:39:45 +01004865 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004866 {
4867 if (scope->se_u.se_if.is_if_label >= 0)
4868 {
4869 // previous "if" or "elseif" jumps here
4870 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4871 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004872 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004873 }
4874 }
4875
4876 if (cctx->ctx_skip != MAYBE)
4877 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878
4879 return p;
4880}
4881
4882 static char_u *
4883compile_endif(char_u *arg, cctx_T *cctx)
4884{
4885 scope_T *scope = cctx->ctx_scope;
4886 ifscope_T *ifscope;
4887 garray_T *instr = &cctx->ctx_instr;
4888 isn_T *isn;
4889
4890 if (scope == NULL || scope->se_type != IF_SCOPE)
4891 {
4892 emsg(_(e_endif_without_if));
4893 return NULL;
4894 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004895 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004896 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004898 if (scope->se_u.se_if.is_if_label >= 0)
4899 {
4900 // previous "if" or "elseif" jumps here
4901 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4902 isn->isn_arg.jump.jump_where = instr->ga_len;
4903 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 // Fill in the "end" label in jumps at the end of the blocks.
4905 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004906 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004908 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 return arg;
4910}
4911
4912/*
4913 * compile "for var in expr"
4914 *
4915 * Produces instructions:
4916 * PUSHNR -1
4917 * STORE loop-idx Set index to -1
4918 * EVAL expr Push result of "expr"
4919 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4920 * - if beyond end, jump to "end"
4921 * - otherwise get item from list and push it
4922 * STORE var Store item in "var"
4923 * ... body ...
4924 * JUMP top Jump back to repeat
4925 * end: DROP Drop the result of "expr"
4926 *
4927 */
4928 static char_u *
4929compile_for(char_u *arg, cctx_T *cctx)
4930{
4931 char_u *p;
4932 size_t varlen;
4933 garray_T *instr = &cctx->ctx_instr;
4934 garray_T *stack = &cctx->ctx_type_stack;
4935 scope_T *scope;
4936 int loop_idx; // index of loop iteration variable
4937 int var_idx; // index of "var"
4938 type_T *vartype;
4939
4940 // TODO: list of variables: "for [key, value] in dict"
4941 // parse "var"
4942 for (p = arg; eval_isnamec1(*p); ++p)
4943 ;
4944 varlen = p - arg;
4945 var_idx = lookup_local(arg, varlen, cctx);
4946 if (var_idx >= 0)
4947 {
4948 semsg(_("E1023: variable already defined: %s"), arg);
4949 return NULL;
4950 }
4951
4952 // consume "in"
4953 p = skipwhite(p);
4954 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4955 {
4956 emsg(_(e_missing_in));
4957 return NULL;
4958 }
4959 p = skipwhite(p + 2);
4960
4961
4962 scope = new_scope(cctx, FOR_SCOPE);
4963 if (scope == NULL)
4964 return NULL;
4965
4966 // Reserve a variable to store the loop iteration counter.
4967 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4968 if (loop_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004969 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004970 // only happens when out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004971 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004973 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974
4975 // Reserve a variable to store "var"
4976 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4977 if (var_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004978 {
4979 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004981 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982
4983 generate_STORENR(cctx, loop_idx, -1);
4984
4985 // compile "expr", it remains on the stack until "endfor"
4986 arg = p;
4987 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004988 {
4989 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992
4993 // now we know the type of "var"
4994 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4995 if (vartype->tt_type != VAR_LIST)
4996 {
4997 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004998 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 return NULL;
5000 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005001 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 {
5003 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
5004
5005 lvar->lv_type = vartype->tt_member;
5006 }
5007
5008 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005009 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010
5011 generate_FOR(cctx, loop_idx);
5012 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
5013
5014 return arg;
5015}
5016
5017/*
5018 * compile "endfor"
5019 */
5020 static char_u *
5021compile_endfor(char_u *arg, cctx_T *cctx)
5022{
5023 garray_T *instr = &cctx->ctx_instr;
5024 scope_T *scope = cctx->ctx_scope;
5025 forscope_T *forscope;
5026 isn_T *isn;
5027
5028 if (scope == NULL || scope->se_type != FOR_SCOPE)
5029 {
5030 emsg(_(e_for));
5031 return NULL;
5032 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005033 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005035 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036
5037 // At end of ":for" scope jump back to the FOR instruction.
5038 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5039
5040 // Fill in the "end" label in the FOR statement so it can jump here
5041 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5042 isn->isn_arg.forloop.for_end = instr->ga_len;
5043
5044 // Fill in the "end" label any BREAK statements
5045 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5046
5047 // Below the ":for" scope drop the "expr" list from the stack.
5048 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5049 return NULL;
5050
5051 vim_free(scope);
5052
5053 return arg;
5054}
5055
5056/*
5057 * compile "while expr"
5058 *
5059 * Produces instructions:
5060 * top: EVAL expr Push result of "expr"
5061 * JUMP_IF_FALSE end jump if false
5062 * ... body ...
5063 * JUMP top Jump back to repeat
5064 * end:
5065 *
5066 */
5067 static char_u *
5068compile_while(char_u *arg, cctx_T *cctx)
5069{
5070 char_u *p = arg;
5071 garray_T *instr = &cctx->ctx_instr;
5072 scope_T *scope;
5073
5074 scope = new_scope(cctx, WHILE_SCOPE);
5075 if (scope == NULL)
5076 return NULL;
5077
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005078 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079
5080 // compile "expr"
5081 if (compile_expr1(&p, cctx) == FAIL)
5082 return NULL;
5083
5084 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005085 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 JUMP_IF_FALSE, cctx) == FAIL)
5087 return FAIL;
5088
5089 return p;
5090}
5091
5092/*
5093 * compile "endwhile"
5094 */
5095 static char_u *
5096compile_endwhile(char_u *arg, cctx_T *cctx)
5097{
5098 scope_T *scope = cctx->ctx_scope;
5099
5100 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5101 {
5102 emsg(_(e_while));
5103 return NULL;
5104 }
5105 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005106 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107
5108 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005109 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110
5111 // Fill in the "end" label in the WHILE statement so it can jump here.
5112 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005113 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114
5115 vim_free(scope);
5116
5117 return arg;
5118}
5119
5120/*
5121 * compile "continue"
5122 */
5123 static char_u *
5124compile_continue(char_u *arg, cctx_T *cctx)
5125{
5126 scope_T *scope = cctx->ctx_scope;
5127
5128 for (;;)
5129 {
5130 if (scope == NULL)
5131 {
5132 emsg(_(e_continue));
5133 return NULL;
5134 }
5135 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5136 break;
5137 scope = scope->se_outer;
5138 }
5139
5140 // Jump back to the FOR or WHILE instruction.
5141 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005142 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5143 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 return arg;
5145}
5146
5147/*
5148 * compile "break"
5149 */
5150 static char_u *
5151compile_break(char_u *arg, cctx_T *cctx)
5152{
5153 scope_T *scope = cctx->ctx_scope;
5154 endlabel_T **el;
5155
5156 for (;;)
5157 {
5158 if (scope == NULL)
5159 {
5160 emsg(_(e_break));
5161 return NULL;
5162 }
5163 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5164 break;
5165 scope = scope->se_outer;
5166 }
5167
5168 // Jump to the end of the FOR or WHILE loop.
5169 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005170 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005172 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5174 return FAIL;
5175
5176 return arg;
5177}
5178
5179/*
5180 * compile "{" start of block
5181 */
5182 static char_u *
5183compile_block(char_u *arg, cctx_T *cctx)
5184{
5185 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5186 return NULL;
5187 return skipwhite(arg + 1);
5188}
5189
5190/*
5191 * compile end of block: drop one scope
5192 */
5193 static void
5194compile_endblock(cctx_T *cctx)
5195{
5196 scope_T *scope = cctx->ctx_scope;
5197
5198 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005199 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 vim_free(scope);
5201}
5202
5203/*
5204 * compile "try"
5205 * Creates a new scope for the try-endtry, pointing to the first catch and
5206 * finally.
5207 * Creates another scope for the "try" block itself.
5208 * TRY instruction sets up exception handling at runtime.
5209 *
5210 * "try"
5211 * TRY -> catch1, -> finally push trystack entry
5212 * ... try block
5213 * "throw {exception}"
5214 * EVAL {exception}
5215 * THROW create exception
5216 * ... try block
5217 * " catch {expr}"
5218 * JUMP -> finally
5219 * catch1: PUSH exeception
5220 * EVAL {expr}
5221 * MATCH
5222 * JUMP nomatch -> catch2
5223 * CATCH remove exception
5224 * ... catch block
5225 * " catch"
5226 * JUMP -> finally
5227 * catch2: CATCH remove exception
5228 * ... catch block
5229 * " finally"
5230 * finally:
5231 * ... finally block
5232 * " endtry"
5233 * ENDTRY pop trystack entry, may rethrow
5234 */
5235 static char_u *
5236compile_try(char_u *arg, cctx_T *cctx)
5237{
5238 garray_T *instr = &cctx->ctx_instr;
5239 scope_T *try_scope;
5240 scope_T *scope;
5241
5242 // scope that holds the jumps that go to catch/finally/endtry
5243 try_scope = new_scope(cctx, TRY_SCOPE);
5244 if (try_scope == NULL)
5245 return NULL;
5246
5247 // "catch" is set when the first ":catch" is found.
5248 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005249 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005250 if (generate_instr(cctx, ISN_TRY) == NULL)
5251 return NULL;
5252
5253 // scope for the try block itself
5254 scope = new_scope(cctx, BLOCK_SCOPE);
5255 if (scope == NULL)
5256 return NULL;
5257
5258 return arg;
5259}
5260
5261/*
5262 * compile "catch {expr}"
5263 */
5264 static char_u *
5265compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5266{
5267 scope_T *scope = cctx->ctx_scope;
5268 garray_T *instr = &cctx->ctx_instr;
5269 char_u *p;
5270 isn_T *isn;
5271
5272 // end block scope from :try or :catch
5273 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5274 compile_endblock(cctx);
5275 scope = cctx->ctx_scope;
5276
5277 // Error if not in a :try scope
5278 if (scope == NULL || scope->se_type != TRY_SCOPE)
5279 {
5280 emsg(_(e_catch));
5281 return NULL;
5282 }
5283
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005284 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005285 {
5286 emsg(_("E1033: catch unreachable after catch-all"));
5287 return NULL;
5288 }
5289
5290 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005291 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292 JUMP_ALWAYS, cctx) == FAIL)
5293 return NULL;
5294
5295 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005296 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297 if (isn->isn_arg.try.try_catch == 0)
5298 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005299 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005300 {
5301 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005302 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303 isn->isn_arg.jump.jump_where = instr->ga_len;
5304 }
5305
5306 p = skipwhite(arg);
5307 if (ends_excmd(*p))
5308 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005309 scope->se_u.se_try.ts_caught_all = TRUE;
5310 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005311 }
5312 else
5313 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005314 char_u *end;
5315 char_u *pat;
5316 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005317 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005318 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320 // Push v:exception, push {expr} and MATCH
5321 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5322
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005323 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005324 if (*end != *p)
5325 {
5326 semsg(_("E1067: Separator mismatch: %s"), p);
5327 vim_free(tofree);
5328 return FAIL;
5329 }
5330 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005331 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005332 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005333 len = (int)(end - tofree);
5334 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005335 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005336 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005337 if (pat == NULL)
5338 return FAIL;
5339 if (generate_PUSHS(cctx, pat) == FAIL)
5340 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5343 return NULL;
5344
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005345 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5347 return NULL;
5348 }
5349
5350 if (generate_instr(cctx, ISN_CATCH) == NULL)
5351 return NULL;
5352
5353 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5354 return NULL;
5355 return p;
5356}
5357
5358 static char_u *
5359compile_finally(char_u *arg, cctx_T *cctx)
5360{
5361 scope_T *scope = cctx->ctx_scope;
5362 garray_T *instr = &cctx->ctx_instr;
5363 isn_T *isn;
5364
5365 // end block scope from :try or :catch
5366 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5367 compile_endblock(cctx);
5368 scope = cctx->ctx_scope;
5369
5370 // Error if not in a :try scope
5371 if (scope == NULL || scope->se_type != TRY_SCOPE)
5372 {
5373 emsg(_(e_finally));
5374 return NULL;
5375 }
5376
5377 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005378 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005379 if (isn->isn_arg.try.try_finally != 0)
5380 {
5381 emsg(_(e_finally_dup));
5382 return NULL;
5383 }
5384
5385 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005386 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387
Bram Moolenaar585fea72020-04-02 22:33:21 +02005388 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005389 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390 {
5391 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005392 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393 isn->isn_arg.jump.jump_where = instr->ga_len;
5394 }
5395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 // TODO: set index in ts_finally_label jumps
5397
5398 return arg;
5399}
5400
5401 static char_u *
5402compile_endtry(char_u *arg, cctx_T *cctx)
5403{
5404 scope_T *scope = cctx->ctx_scope;
5405 garray_T *instr = &cctx->ctx_instr;
5406 isn_T *isn;
5407
5408 // end block scope from :catch or :finally
5409 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5410 compile_endblock(cctx);
5411 scope = cctx->ctx_scope;
5412
5413 // Error if not in a :try scope
5414 if (scope == NULL || scope->se_type != TRY_SCOPE)
5415 {
5416 if (scope == NULL)
5417 emsg(_(e_no_endtry));
5418 else if (scope->se_type == WHILE_SCOPE)
5419 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005420 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421 emsg(_(e_endfor));
5422 else
5423 emsg(_(e_endif));
5424 return NULL;
5425 }
5426
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005427 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5429 {
5430 emsg(_("E1032: missing :catch or :finally"));
5431 return NULL;
5432 }
5433
5434 // Fill in the "end" label in jumps at the end of the blocks, if not done
5435 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005436 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005437
5438 // End :catch or :finally scope: set value in ISN_TRY instruction
5439 if (isn->isn_arg.try.try_finally == 0)
5440 isn->isn_arg.try.try_finally = instr->ga_len;
5441 compile_endblock(cctx);
5442
5443 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5444 return NULL;
5445 return arg;
5446}
5447
5448/*
5449 * compile "throw {expr}"
5450 */
5451 static char_u *
5452compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5453{
5454 char_u *p = skipwhite(arg);
5455
5456 if (ends_excmd(*p))
5457 {
5458 emsg(_(e_argreq));
5459 return NULL;
5460 }
5461 if (compile_expr1(&p, cctx) == FAIL)
5462 return NULL;
5463 if (may_generate_2STRING(-1, cctx) == FAIL)
5464 return NULL;
5465 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5466 return NULL;
5467
5468 return p;
5469}
5470
5471/*
5472 * compile "echo expr"
5473 */
5474 static char_u *
5475compile_echo(char_u *arg, int with_white, cctx_T *cctx)
5476{
5477 char_u *p = arg;
5478 int count = 0;
5479
Bram Moolenaarad39c092020-02-26 18:23:43 +01005480 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005481 {
5482 if (compile_expr1(&p, cctx) == FAIL)
5483 return NULL;
5484 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005485 p = skipwhite(p);
5486 if (ends_excmd(*p))
5487 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005488 }
5489
5490 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01005491 return p;
5492}
5493
5494/*
5495 * compile "execute expr"
5496 */
5497 static char_u *
5498compile_execute(char_u *arg, cctx_T *cctx)
5499{
5500 char_u *p = arg;
5501 int count = 0;
5502
5503 for (;;)
5504 {
5505 if (compile_expr1(&p, cctx) == FAIL)
5506 return NULL;
5507 ++count;
5508 p = skipwhite(p);
5509 if (ends_excmd(*p))
5510 break;
5511 }
5512
5513 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005514
5515 return p;
5516}
5517
5518/*
5519 * After ex_function() has collected all the function lines: parse and compile
5520 * the lines into instructions.
5521 * Adds the function to "def_functions".
5522 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5523 * return statement (used for lambda).
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005524 * This can be used recursively through compile_lambda(), which may reallocate
5525 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005526 */
5527 void
5528compile_def_function(ufunc_T *ufunc, int set_return_type)
5529{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005530 char_u *line = NULL;
5531 char_u *p;
5532 exarg_T ea;
5533 char *errormsg = NULL; // error message
5534 int had_return = FALSE;
5535 cctx_T cctx;
5536 garray_T *instr;
5537 int called_emsg_before = called_emsg;
5538 int ret = FAIL;
5539 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005540 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005543 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005544
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005545 if (ufunc->uf_dfunc_idx >= 0)
5546 {
5547 // Redefining a function that was compiled before.
5548 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5549
5550 // Free old instructions.
5551 delete_def_function_contents(dfunc);
5552 }
5553 else
5554 {
5555 // Add the function to "def_functions".
5556 if (ga_grow(&def_functions, 1) == FAIL)
5557 return;
5558 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
5559 vim_memset(dfunc, 0, sizeof(dfunc_T));
5560 dfunc->df_idx = def_functions.ga_len;
5561 ufunc->uf_dfunc_idx = dfunc->df_idx;
5562 dfunc->df_ufunc = ufunc;
5563 ++def_functions.ga_len;
5564 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565 }
5566
5567 vim_memset(&cctx, 0, sizeof(cctx));
5568 cctx.ctx_ufunc = ufunc;
5569 cctx.ctx_lnum = -1;
5570 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5571 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5572 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5573 cctx.ctx_type_list = &ufunc->uf_type_list;
5574 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5575 instr = &cctx.ctx_instr;
5576
5577 // Most modern script version.
5578 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5579
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005580 if (ufunc->uf_def_args.ga_len > 0)
5581 {
5582 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005583 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005584 int i;
5585 char_u *arg;
5586 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5587
5588 // Produce instructions for the default values of optional arguments.
5589 // Store the instruction index in uf_def_arg_idx[] so that we know
5590 // where to start when the function is called, depending on the number
5591 // of arguments.
5592 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5593 if (ufunc->uf_def_arg_idx == NULL)
5594 goto erret;
5595 for (i = 0; i < count; ++i)
5596 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005597 garray_T *stack = &cctx.ctx_type_stack;
5598 type_T *val_type;
5599 int arg_idx = first_def_arg + i;
5600
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005601 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5602 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005603 if (compile_expr1(&arg, &cctx) == FAIL)
5604 goto erret;
5605
5606 // If no type specified use the type of the default value.
5607 // Otherwise check that the default value type matches the
5608 // specified type.
5609 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5610 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
5611 ufunc->uf_arg_types[arg_idx] = val_type;
5612 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
5613 == FAIL)
5614 {
5615 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
5616 arg_idx + 1);
5617 goto erret;
5618 }
5619
5620 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005621 goto erret;
5622 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005623 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5624 }
5625
5626 /*
5627 * Loop over all the lines of the function and generate instructions.
5628 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005629 for (;;)
5630 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005631 int is_ex_command;
5632
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005633 // Bail out on the first error to avoid a flood of errors and report
5634 // the right line number when inside try/catch.
5635 if (emsg_before != called_emsg)
5636 goto erret;
5637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005638 if (line != NULL && *line == '|')
5639 // the line continues after a '|'
5640 ++line;
5641 else if (line != NULL && *line != NUL)
5642 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005643 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644 goto erret;
5645 }
5646 else
5647 {
5648 do
5649 {
5650 ++cctx.ctx_lnum;
5651 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5652 break;
5653 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
5654 } while (line == NULL);
5655 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5656 break;
5657 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
5658 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005659 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005660
5661 had_return = FALSE;
5662 vim_memset(&ea, 0, sizeof(ea));
5663 ea.cmdlinep = &line;
5664 ea.cmd = skipwhite(line);
5665
5666 // "}" ends a block scope
5667 if (*ea.cmd == '}')
5668 {
5669 scopetype_T stype = cctx.ctx_scope == NULL
5670 ? NO_SCOPE : cctx.ctx_scope->se_type;
5671
5672 if (stype == BLOCK_SCOPE)
5673 {
5674 compile_endblock(&cctx);
5675 line = ea.cmd;
5676 }
5677 else
5678 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005679 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005680 goto erret;
5681 }
5682 if (line != NULL)
5683 line = skipwhite(ea.cmd + 1);
5684 continue;
5685 }
5686
5687 // "{" starts a block scope
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01005688 // "{'a': 1}->func() is something else
5689 if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005690 {
5691 line = compile_block(ea.cmd, &cctx);
5692 continue;
5693 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005694 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005695
5696 /*
5697 * COMMAND MODIFIERS
5698 */
5699 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5700 {
5701 if (errormsg != NULL)
5702 goto erret;
5703 // empty line or comment
5704 line = (char_u *)"";
5705 continue;
5706 }
5707
5708 // Skip ":call" to get to the function name.
5709 if (checkforcmd(&ea.cmd, "call", 3))
5710 ea.cmd = skipwhite(ea.cmd);
5711
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005712 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005713 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005714 // Assuming the command starts with a variable or function name,
5715 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5716 // val".
5717 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5718 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005719 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005720 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005721 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005722 int oplen;
5723 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005724
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005725 oplen = assignment_len(skipwhite(p), &heredoc);
5726 if (oplen > 0)
5727 {
5728 // Recognize an assignment if we recognize the variable
5729 // name:
5730 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005731 // "local = expr" where "local" is a local var.
5732 // "script = expr" where "script" is a script-local var.
5733 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005734 // "&opt = expr"
5735 // "$ENV = expr"
5736 // "@r = expr"
5737 if (*ea.cmd == '&'
5738 || *ea.cmd == '$'
5739 || *ea.cmd == '@'
5740 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5741 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5742 || lookup_script(ea.cmd, p - ea.cmd) == OK
5743 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5744 {
5745 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5746 if (line == NULL)
5747 goto erret;
5748 continue;
5749 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005750 }
5751 }
5752 }
5753
5754 /*
5755 * COMMAND after range
5756 */
5757 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005758 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5759 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005760
5761 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5762 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005763 if (cctx.ctx_skip == TRUE)
5764 {
5765 line += STRLEN(line);
5766 continue;
5767 }
5768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005769 // Expression or function call.
5770 if (ea.cmdidx == CMD_eval)
5771 {
5772 p = ea.cmd;
5773 if (compile_expr1(&p, &cctx) == FAIL)
5774 goto erret;
5775
5776 // drop the return value
5777 generate_instr_drop(&cctx, ISN_DROP, 1);
5778 line = p;
5779 continue;
5780 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02005781 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005782 iemsg("Command from find_ex_command() not handled");
5783 goto erret;
5784 }
5785
5786 p = skipwhite(p);
5787
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005788 if (cctx.ctx_skip == TRUE
5789 && ea.cmdidx != CMD_elseif
5790 && ea.cmdidx != CMD_else
5791 && ea.cmdidx != CMD_endif)
5792 {
5793 line += STRLEN(line);
5794 continue;
5795 }
5796
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005797 switch (ea.cmdidx)
5798 {
5799 case CMD_def:
5800 case CMD_function:
5801 // TODO: Nested function
5802 emsg("Nested function not implemented yet");
5803 goto erret;
5804
5805 case CMD_return:
5806 line = compile_return(p, set_return_type, &cctx);
5807 had_return = TRUE;
5808 break;
5809
5810 case CMD_let:
5811 case CMD_const:
5812 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5813 break;
5814
5815 case CMD_import:
5816 line = compile_import(p, &cctx);
5817 break;
5818
5819 case CMD_if:
5820 line = compile_if(p, &cctx);
5821 break;
5822 case CMD_elseif:
5823 line = compile_elseif(p, &cctx);
5824 break;
5825 case CMD_else:
5826 line = compile_else(p, &cctx);
5827 break;
5828 case CMD_endif:
5829 line = compile_endif(p, &cctx);
5830 break;
5831
5832 case CMD_while:
5833 line = compile_while(p, &cctx);
5834 break;
5835 case CMD_endwhile:
5836 line = compile_endwhile(p, &cctx);
5837 break;
5838
5839 case CMD_for:
5840 line = compile_for(p, &cctx);
5841 break;
5842 case CMD_endfor:
5843 line = compile_endfor(p, &cctx);
5844 break;
5845 case CMD_continue:
5846 line = compile_continue(p, &cctx);
5847 break;
5848 case CMD_break:
5849 line = compile_break(p, &cctx);
5850 break;
5851
5852 case CMD_try:
5853 line = compile_try(p, &cctx);
5854 break;
5855 case CMD_catch:
5856 line = compile_catch(p, &cctx);
5857 break;
5858 case CMD_finally:
5859 line = compile_finally(p, &cctx);
5860 break;
5861 case CMD_endtry:
5862 line = compile_endtry(p, &cctx);
5863 break;
5864 case CMD_throw:
5865 line = compile_throw(p, &cctx);
5866 break;
5867
5868 case CMD_echo:
5869 line = compile_echo(p, TRUE, &cctx);
5870 break;
5871 case CMD_echon:
5872 line = compile_echo(p, FALSE, &cctx);
5873 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005874 case CMD_execute:
5875 line = compile_execute(p, &cctx);
5876 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005877
5878 default:
5879 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005880 // TODO:
5881 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005882 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883 generate_EXEC(&cctx, line);
5884 line = (char_u *)"";
5885 break;
5886 }
5887 if (line == NULL)
5888 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02005889 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890
5891 if (cctx.ctx_type_stack.ga_len < 0)
5892 {
5893 iemsg("Type stack underflow");
5894 goto erret;
5895 }
5896 }
5897
5898 if (cctx.ctx_scope != NULL)
5899 {
5900 if (cctx.ctx_scope->se_type == IF_SCOPE)
5901 emsg(_(e_endif));
5902 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5903 emsg(_(e_endwhile));
5904 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5905 emsg(_(e_endfor));
5906 else
5907 emsg(_("E1026: Missing }"));
5908 goto erret;
5909 }
5910
5911 if (!had_return)
5912 {
5913 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5914 {
5915 emsg(_("E1027: Missing return statement"));
5916 goto erret;
5917 }
5918
5919 // Return zero if there is no return at the end.
5920 generate_PUSHNR(&cctx, 0);
5921 generate_instr(&cctx, ISN_RETURN);
5922 }
5923
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005924 {
5925 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5926 + ufunc->uf_dfunc_idx;
5927 dfunc->df_deleted = FALSE;
5928 dfunc->df_instr = instr->ga_data;
5929 dfunc->df_instr_count = instr->ga_len;
5930 dfunc->df_varcount = cctx.ctx_max_local;
5931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005932
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005933 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005934 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005935 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005936
5937 // Create a type for the function, with the return type and any
5938 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005939 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5940 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005941 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005942 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005943 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
5944 argcount, &ufunc->uf_type_list);
5945 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005946 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005947 argcount + varargs,
5948 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005949 {
5950 ret = FAIL;
5951 goto erret;
5952 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005953 ufunc->uf_func_type->tt_argcount = argcount + varargs;
5954 ufunc->uf_func_type->tt_min_argcount =
5955 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005956 if (ufunc->uf_arg_types == NULL)
5957 {
5958 int i;
5959
5960 // lambda does not have argument types.
5961 for (i = 0; i < argcount; ++i)
5962 ufunc->uf_func_type->tt_args[i] = &t_any;
5963 }
5964 else
5965 mch_memmove(ufunc->uf_func_type->tt_args,
5966 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005967 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005968 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005969 ufunc->uf_func_type->tt_args[argcount] =
5970 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005971 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
5972 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005973 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005974 else
5975 // No arguments, can use a predefined type.
5976 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5977 argcount, &ufunc->uf_type_list);
5978
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005979 }
5980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005981 ret = OK;
5982
5983erret:
5984 if (ret == FAIL)
5985 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005986 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005987 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5988 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005989
5990 for (idx = 0; idx < instr->ga_len; ++idx)
5991 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005992 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005995 if (!dfunc->df_deleted)
5996 --def_functions.ga_len;
5997
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005998 while (cctx.ctx_scope != NULL)
5999 drop_scope(&cctx);
6000
Bram Moolenaar20431c92020-03-20 18:39:46 +01006001 // Don't execute this function body.
6002 ga_clear_strings(&ufunc->uf_lines);
6003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006004 if (errormsg != NULL)
6005 emsg(errormsg);
6006 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006007 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008 }
6009
6010 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006011 free_imported(&cctx);
6012 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006013 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006014}
6015
6016/*
6017 * Delete an instruction, free what it contains.
6018 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006019 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006020delete_instr(isn_T *isn)
6021{
6022 switch (isn->isn_type)
6023 {
6024 case ISN_EXEC:
6025 case ISN_LOADENV:
6026 case ISN_LOADG:
6027 case ISN_LOADOPT:
6028 case ISN_MEMBER:
6029 case ISN_PUSHEXC:
6030 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006031 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006032 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006033 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034 vim_free(isn->isn_arg.string);
6035 break;
6036
6037 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006038 case ISN_STORES:
6039 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006040 break;
6041
6042 case ISN_STOREOPT:
6043 vim_free(isn->isn_arg.storeopt.so_name);
6044 break;
6045
6046 case ISN_PUSHBLOB: // push blob isn_arg.blob
6047 blob_unref(isn->isn_arg.blob);
6048 break;
6049
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006050 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006051#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006052 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006053#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006054 break;
6055
6056 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006057#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006058 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006059#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006060 break;
6061
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062 case ISN_UCALL:
6063 vim_free(isn->isn_arg.ufunc.cuf_name);
6064 break;
6065
6066 case ISN_2BOOL:
6067 case ISN_2STRING:
6068 case ISN_ADDBLOB:
6069 case ISN_ADDLIST:
6070 case ISN_BCALL:
6071 case ISN_CATCH:
6072 case ISN_CHECKNR:
6073 case ISN_CHECKTYPE:
6074 case ISN_COMPAREANY:
6075 case ISN_COMPAREBLOB:
6076 case ISN_COMPAREBOOL:
6077 case ISN_COMPAREDICT:
6078 case ISN_COMPAREFLOAT:
6079 case ISN_COMPAREFUNC:
6080 case ISN_COMPARELIST:
6081 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082 case ISN_COMPARESPECIAL:
6083 case ISN_COMPARESTRING:
6084 case ISN_CONCAT:
6085 case ISN_DCALL:
6086 case ISN_DROP:
6087 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006088 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006089 case ISN_ENDTRY:
6090 case ISN_FOR:
6091 case ISN_FUNCREF:
6092 case ISN_INDEX:
6093 case ISN_JUMP:
6094 case ISN_LOAD:
6095 case ISN_LOADSCRIPT:
6096 case ISN_LOADREG:
6097 case ISN_LOADV:
6098 case ISN_NEGATENR:
6099 case ISN_NEWDICT:
6100 case ISN_NEWLIST:
6101 case ISN_OPNR:
6102 case ISN_OPFLOAT:
6103 case ISN_OPANY:
6104 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006105 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106 case ISN_PUSHF:
6107 case ISN_PUSHNR:
6108 case ISN_PUSHBOOL:
6109 case ISN_PUSHSPEC:
6110 case ISN_RETURN:
6111 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006112 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006114 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006115 case ISN_STORESCRIPT:
6116 case ISN_THROW:
6117 case ISN_TRY:
6118 // nothing allocated
6119 break;
6120 }
6121}
6122
6123/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006124 * Free all instructions for "dfunc".
6125 */
6126 static void
6127delete_def_function_contents(dfunc_T *dfunc)
6128{
6129 int idx;
6130
6131 ga_clear(&dfunc->df_def_args_isn);
6132
6133 if (dfunc->df_instr != NULL)
6134 {
6135 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6136 delete_instr(dfunc->df_instr + idx);
6137 VIM_CLEAR(dfunc->df_instr);
6138 }
6139
6140 dfunc->df_deleted = TRUE;
6141}
6142
6143/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144 * When a user function is deleted, delete any associated def function.
6145 */
6146 void
6147delete_def_function(ufunc_T *ufunc)
6148{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006149 if (ufunc->uf_dfunc_idx >= 0)
6150 {
6151 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6152 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006153
Bram Moolenaar20431c92020-03-20 18:39:46 +01006154 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006155 }
6156}
6157
6158#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006159/*
6160 * Free all functions defined with ":def".
6161 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006162 void
6163free_def_functions(void)
6164{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006165 int idx;
6166
6167 for (idx = 0; idx < def_functions.ga_len; ++idx)
6168 {
6169 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6170
6171 delete_def_function_contents(dfunc);
6172 }
6173
6174 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006175}
6176#endif
6177
6178
6179#endif // FEAT_EVAL