blob: 666db1599aa5bc41da50ae1428732cb799eaa555 [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 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02001826 if (func_type_add_arg_types(*dest, argcount,
1827 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001828 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 Moolenaare6085c52020-04-12 20:19:16 +02002052 * Get the next line of the function from "cctx".
2053 * Returns NULL when at the end.
2054 */
2055 static char_u *
2056next_line_from_context(cctx_T *cctx)
2057{
2058 char_u *line = NULL;
2059
2060 do
2061 {
2062 ++cctx->ctx_lnum;
2063 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
2064 break;
2065 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
2066 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2067 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002068 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002069 return line;
2070}
2071
2072/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002073 * Return TRUE if "p" points at a "#" but not at "#{".
2074 */
2075 static int
2076comment_start(char_u *p)
2077{
2078 return p[0] == '#' && p[1] != '{';
2079}
2080
2081/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002082 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002083 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002084 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2085 */
2086 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002087may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002088{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002089 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002090 {
2091 char_u *next = next_line_from_context(cctx);
2092
2093 if (next == NULL)
2094 return FAIL;
2095 *arg = skipwhite(next);
2096 }
2097 return OK;
2098}
2099
2100/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002101 * Generate an instruction to load script-local variable "name", without the
2102 * leading "s:".
2103 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 */
2105 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002106compile_load_scriptvar(
2107 cctx_T *cctx,
2108 char_u *name, // variable NUL terminated
2109 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002110 char_u **end, // end of variable
2111 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002113 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2115 imported_T *import;
2116
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002117 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002119 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002120 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2121 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 }
2123 if (idx >= 0)
2124 {
2125 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2126
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002127 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 current_sctx.sc_sid, idx, sv->sv_type);
2129 return OK;
2130 }
2131
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002132 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 if (import != NULL)
2134 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002135 if (import->imp_all)
2136 {
2137 char_u *p = skipwhite(*end);
2138 int name_len;
2139 ufunc_T *ufunc;
2140 type_T *type;
2141
2142 // Used "import * as Name", need to lookup the member.
2143 if (*p != '.')
2144 {
2145 semsg(_("E1060: expected dot after name: %s"), start);
2146 return FAIL;
2147 }
2148 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002149 if (VIM_ISWHITE(*p))
2150 {
2151 emsg(_("E1074: no white space allowed after dot"));
2152 return FAIL;
2153 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002154
2155 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2156 // TODO: what if it is a function?
2157 if (idx < 0)
2158 return FAIL;
2159 *end = p;
2160
2161 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2162 import->imp_sid,
2163 idx,
2164 type);
2165 }
2166 else
2167 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002168 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002169 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2170 import->imp_sid,
2171 import->imp_var_vals_idx,
2172 import->imp_type);
2173 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 return OK;
2175 }
2176
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002177 if (error)
2178 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 return FAIL;
2180}
2181
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002182 static int
2183generate_funcref(cctx_T *cctx, char_u *name)
2184{
2185 ufunc_T *ufunc = find_func(name, cctx);
2186
2187 if (ufunc == NULL)
2188 return FAIL;
2189
2190 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2191}
2192
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193/*
2194 * Compile a variable name into a load instruction.
2195 * "end" points to just after the name.
2196 * When "error" is FALSE do not give an error when not found.
2197 */
2198 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002199compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200{
2201 type_T *type;
2202 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002203 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002205 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206
2207 if (*(*arg + 1) == ':')
2208 {
2209 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002210 if (end <= *arg + 2)
2211 name = vim_strsave((char_u *)"[empty]");
2212 else
2213 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 if (name == NULL)
2215 return FAIL;
2216
2217 if (**arg == 'v')
2218 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002219 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 }
2221 else if (**arg == 'g')
2222 {
2223 // Global variables can be defined later, thus we don't check if it
2224 // exists, give error at runtime.
2225 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2226 }
2227 else if (**arg == 's')
2228 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002229 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002231 else if (**arg == 'b')
2232 {
2233 semsg("Namespace b: not supported yet: %s", *arg);
2234 goto theend;
2235 }
2236 else if (**arg == 'w')
2237 {
2238 semsg("Namespace w: not supported yet: %s", *arg);
2239 goto theend;
2240 }
2241 else if (**arg == 't')
2242 {
2243 semsg("Namespace t: not supported yet: %s", *arg);
2244 goto theend;
2245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 else
2247 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002248 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 goto theend;
2250 }
2251 }
2252 else
2253 {
2254 size_t len = end - *arg;
2255 int idx;
2256 int gen_load = FALSE;
2257
2258 name = vim_strnsave(*arg, end - *arg);
2259 if (name == NULL)
2260 return FAIL;
2261
2262 idx = lookup_arg(*arg, len, cctx);
2263 if (idx >= 0)
2264 {
2265 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2266 type = cctx->ctx_ufunc->uf_arg_types[idx];
2267 else
2268 type = &t_any;
2269
2270 // Arguments are located above the frame pointer.
2271 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2272 if (cctx->ctx_ufunc->uf_va_name != NULL)
2273 --idx;
2274 gen_load = TRUE;
2275 }
2276 else if (lookup_vararg(*arg, len, cctx))
2277 {
2278 // varargs is always the last argument
2279 idx = -STACK_FRAME_SIZE - 1;
2280 type = cctx->ctx_ufunc->uf_va_type;
2281 gen_load = TRUE;
2282 }
2283 else
2284 {
2285 idx = lookup_local(*arg, len, cctx);
2286 if (idx >= 0)
2287 {
2288 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
2289 gen_load = TRUE;
2290 }
2291 else
2292 {
2293 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2294 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2295 res = generate_PUSHBOOL(cctx, **arg == 't'
2296 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002297 else
2298 {
2299 // "var" can be script-local even without using "s:" if it
2300 // already exists.
2301 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2302 == SCRIPT_VERSION_VIM9
2303 || lookup_script(*arg, len) == OK)
2304 res = compile_load_scriptvar(cctx, name, *arg, &end,
2305 FALSE);
2306
2307 // When the name starts with an uppercase letter or "x:" it
2308 // can be a user defined function.
2309 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2310 res = generate_funcref(cctx, name);
2311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 }
2313 }
2314 if (gen_load)
2315 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
2316 }
2317
2318 *arg = end;
2319
2320theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002321 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 semsg(_(e_var_notfound), name);
2323 vim_free(name);
2324 return res;
2325}
2326
2327/*
2328 * Compile the argument expressions.
2329 * "arg" points to just after the "(" and is advanced to after the ")"
2330 */
2331 static int
2332compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2333{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002334 char_u *p = *arg;
2335 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336
Bram Moolenaare6085c52020-04-12 20:19:16 +02002337 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002339 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002340 {
2341 p = next_line_from_context(cctx);
2342 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002343 goto failret;
2344 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002345 p = skipwhite(p);
2346 }
2347 if (*p == ')')
2348 {
2349 *arg = p + 1;
2350 return OK;
2351 }
2352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 if (compile_expr1(&p, cctx) == FAIL)
2354 return FAIL;
2355 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002356
2357 if (*p != ',' && *skipwhite(p) == ',')
2358 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002359 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002360 p = skipwhite(p);
2361 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002363 {
2364 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002365 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002366 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002367 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002368 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002369 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002371failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002372 emsg(_(e_missing_close));
2373 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374}
2375
2376/*
2377 * Compile a function call: name(arg1, arg2)
2378 * "arg" points to "name", "arg + varlen" to the "(".
2379 * "argcount_init" is 1 for "value->method()"
2380 * Instructions:
2381 * EVAL arg1
2382 * EVAL arg2
2383 * BCALL / DCALL / UCALL
2384 */
2385 static int
2386compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2387{
2388 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002389 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 int argcount = argcount_init;
2391 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002392 char_u fname_buf[FLEN_FIXED + 1];
2393 char_u *tofree = NULL;
2394 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002396 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397
2398 if (varlen >= sizeof(namebuf))
2399 {
2400 semsg(_("E1011: name too long: %s"), name);
2401 return FAIL;
2402 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002403 vim_strncpy(namebuf, *arg, varlen);
2404 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405
2406 *arg = skipwhite(*arg + varlen + 1);
2407 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002408 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002410 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 {
2412 int idx;
2413
2414 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002415 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002417 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002418 else
2419 semsg(_(e_unknownfunc), namebuf);
2420 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 }
2422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002424 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002426 {
2427 res = generate_CALL(cctx, ufunc, argcount);
2428 goto theend;
2429 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430
2431 // If the name is a variable, load it and use PCALL.
2432 p = namebuf;
2433 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002434 {
2435 res = generate_PCALL(cctx, argcount, FALSE);
2436 goto theend;
2437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438
2439 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002440 res = generate_UCALL(cctx, name, argcount);
2441
2442theend:
2443 vim_free(tofree);
2444 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445}
2446
2447// like NAMESPACE_CHAR but with 'a' and 'l'.
2448#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2449
2450/*
2451 * Find the end of a variable or function name. Unlike find_name_end() this
2452 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002453 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 * Return a pointer to just after the name. Equal to "arg" if there is no
2455 * valid name.
2456 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002457 static char_u *
2458to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459{
2460 char_u *p;
2461
2462 // Quick check for valid starting character.
2463 if (!eval_isnamec1(*arg))
2464 return arg;
2465
2466 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2467 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2468 // and can be used in slice "[n:]".
2469 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002470 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2472 break;
2473 return p;
2474}
2475
2476/*
2477 * Like to_name_end() but also skip over a list or dict constant.
2478 */
2479 char_u *
2480to_name_const_end(char_u *arg)
2481{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002482 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 typval_T rettv;
2484
2485 if (p == arg && *arg == '[')
2486 {
2487
2488 // Can be "[1, 2, 3]->Func()".
2489 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2490 p = arg;
2491 }
2492 else if (p == arg && *arg == '#' && arg[1] == '{')
2493 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002494 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 ++p;
2496 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2497 p = arg;
2498 }
2499 else if (p == arg && *arg == '{')
2500 {
2501 int ret = get_lambda_tv(&p, &rettv, FALSE);
2502
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002503 // Can be "{x -> ret}()".
2504 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 if (ret == NOTDONE)
2506 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2507 if (ret != OK)
2508 p = arg;
2509 }
2510
2511 return p;
2512}
2513
2514 static void
2515type_mismatch(type_T *expected, type_T *actual)
2516{
2517 char *tofree1, *tofree2;
2518
2519 semsg(_("E1013: type mismatch, expected %s but got %s"),
2520 type_name(expected, &tofree1), type_name(actual, &tofree2));
2521 vim_free(tofree1);
2522 vim_free(tofree2);
2523}
2524
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002525 static void
2526arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
2527{
2528 char *tofree1, *tofree2;
2529
2530 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
2531 argidx,
2532 type_name(expected, &tofree1), type_name(actual, &tofree2));
2533 vim_free(tofree1);
2534 vim_free(tofree2);
2535}
2536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537/*
2538 * Check if the expected and actual types match.
2539 */
2540 static int
2541check_type(type_T *expected, type_T *actual, int give_msg)
2542{
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002543 int ret = OK;
2544
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002545 // When expected is "unknown" we accept any actual type.
2546 // When expected is "any" we accept any actual type except "void".
2547 if (expected->tt_type != VAR_UNKNOWN
2548 && (expected->tt_type != VAR_ANY || actual->tt_type == VAR_VOID))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 {
2550 if (expected->tt_type != actual->tt_type)
2551 {
2552 if (give_msg)
2553 type_mismatch(expected, actual);
2554 return FAIL;
2555 }
2556 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2557 {
Bram Moolenaar4c683752020-04-05 21:38:23 +02002558 // "unknown" is used for an empty list or dict
2559 if (actual->tt_member != &t_unknown)
Bram Moolenaar436472f2020-02-20 22:54:43 +01002560 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002562 else if (expected->tt_type == VAR_FUNC)
2563 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002564 if (expected->tt_member != &t_unknown)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002565 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
2566 if (ret == OK && expected->tt_argcount != -1
2567 && (actual->tt_argcount < expected->tt_min_argcount
2568 || actual->tt_argcount > expected->tt_argcount))
2569 ret = FAIL;
2570 }
2571 if (ret == FAIL && give_msg)
2572 type_mismatch(expected, actual);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 }
Bram Moolenaar89228602020-04-05 22:14:54 +02002574 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575}
2576
2577/*
2578 * Check that
2579 * - "actual" is "expected" type or
2580 * - "actual" is a type that can be "expected" type: add a runtime check; or
2581 * - return FAIL.
2582 */
2583 static int
2584need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2585{
Bram Moolenaar89228602020-04-05 22:14:54 +02002586 if (check_type(expected, actual, FALSE) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 return OK;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002588 if (actual->tt_type != VAR_ANY && actual->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 {
2590 type_mismatch(expected, actual);
2591 return FAIL;
2592 }
2593 generate_TYPECHECK(cctx, expected, offset);
2594 return OK;
2595}
2596
2597/*
2598 * parse a list: [expr, expr]
2599 * "*arg" points to the '['.
2600 */
2601 static int
2602compile_list(char_u **arg, cctx_T *cctx)
2603{
2604 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002605 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 int count = 0;
2607
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002608 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002610 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002611 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002612 p = next_line_from_context(cctx);
2613 if (p == NULL)
2614 {
2615 semsg(_(e_list_end), *arg);
2616 return FAIL;
2617 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002618 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002619 p = skipwhite(p);
2620 }
2621 if (*p == ']')
2622 {
2623 ++p;
2624 // Allow for following comment, after at least one space.
2625 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2626 p += STRLEN(p);
2627 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002628 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 if (compile_expr1(&p, cctx) == FAIL)
2630 break;
2631 ++count;
2632 if (*p == ',')
2633 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002634 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 p = skipwhite(p);
2636 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002637 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638
2639 generate_NEWLIST(cctx, count);
2640 return OK;
2641}
2642
2643/*
2644 * parse a lambda: {arg, arg -> expr}
2645 * "*arg" points to the '{'.
2646 */
2647 static int
2648compile_lambda(char_u **arg, cctx_T *cctx)
2649{
2650 garray_T *instr = &cctx->ctx_instr;
2651 typval_T rettv;
2652 ufunc_T *ufunc;
2653
2654 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002655 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002659 ++ufunc->uf_refcount;
2660 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002661 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662
2663 // The function will have one line: "return {expr}".
2664 // Compile it into instructions.
2665 compile_def_function(ufunc, TRUE);
2666
2667 if (ufunc->uf_dfunc_idx >= 0)
2668 {
2669 if (ga_grow(instr, 1) == FAIL)
2670 return FAIL;
2671 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2672 return OK;
2673 }
2674 return FAIL;
2675}
2676
2677/*
2678 * Compile a lamda call: expr->{lambda}(args)
2679 * "arg" points to the "{".
2680 */
2681 static int
2682compile_lambda_call(char_u **arg, cctx_T *cctx)
2683{
2684 ufunc_T *ufunc;
2685 typval_T rettv;
2686 int argcount = 1;
2687 int ret = FAIL;
2688
2689 // Get the funcref in "rettv".
2690 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2691 return FAIL;
2692
2693 if (**arg != '(')
2694 {
2695 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002696 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 else
2698 semsg(_(e_missing_paren), "lambda");
2699 clear_tv(&rettv);
2700 return FAIL;
2701 }
2702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 ufunc = rettv.vval.v_partial->pt_func;
2704 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002705 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002706 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002707
2708 // The function will have one line: "return {expr}".
2709 // Compile it into instructions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 compile_def_function(ufunc, TRUE);
2711
2712 // compile the arguments
2713 *arg = skipwhite(*arg + 1);
2714 if (compile_arguments(arg, cctx, &argcount) == OK)
2715 // call the compiled function
2716 ret = generate_CALL(cctx, ufunc, argcount);
2717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 return ret;
2719}
2720
2721/*
2722 * parse a dict: {'key': val} or #{key: val}
2723 * "*arg" points to the '{'.
2724 */
2725 static int
2726compile_dict(char_u **arg, cctx_T *cctx, int literal)
2727{
2728 garray_T *instr = &cctx->ctx_instr;
2729 int count = 0;
2730 dict_T *d = dict_alloc();
2731 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002732 char_u *whitep = *arg;
2733 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734
2735 if (d == NULL)
2736 return FAIL;
2737 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002738 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 {
2740 char_u *key = NULL;
2741
Bram Moolenaar2c330432020-04-13 14:41:35 +02002742 while (**arg == NUL || (literal && **arg == '"')
2743 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002744 {
2745 *arg = next_line_from_context(cctx);
2746 if (*arg == NULL)
2747 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002748 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002749 *arg = skipwhite(*arg);
2750 }
2751
2752 if (**arg == '}')
2753 break;
2754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 if (literal)
2756 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002757 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758
Bram Moolenaar2c330432020-04-13 14:41:35 +02002759 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 {
2761 semsg(_("E1014: Invalid key: %s"), *arg);
2762 return FAIL;
2763 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002764 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 if (generate_PUSHS(cctx, key) == FAIL)
2766 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002767 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 }
2769 else
2770 {
2771 isn_T *isn;
2772
2773 if (compile_expr1(arg, cctx) == FAIL)
2774 return FAIL;
2775 // TODO: check type is string
2776 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2777 if (isn->isn_type == ISN_PUSHS)
2778 key = isn->isn_arg.string;
2779 }
2780
2781 // Check for duplicate keys, if using string keys.
2782 if (key != NULL)
2783 {
2784 item = dict_find(d, key, -1);
2785 if (item != NULL)
2786 {
2787 semsg(_(e_duplicate_key), key);
2788 goto failret;
2789 }
2790 item = dictitem_alloc(key);
2791 if (item != NULL)
2792 {
2793 item->di_tv.v_type = VAR_UNKNOWN;
2794 item->di_tv.v_lock = 0;
2795 if (dict_add(d, item) == FAIL)
2796 dictitem_free(item);
2797 }
2798 }
2799
2800 *arg = skipwhite(*arg);
2801 if (**arg != ':')
2802 {
2803 semsg(_(e_missing_dict_colon), *arg);
2804 return FAIL;
2805 }
2806
Bram Moolenaar2c330432020-04-13 14:41:35 +02002807 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002809 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002810 {
2811 *arg = next_line_from_context(cctx);
2812 if (*arg == NULL)
2813 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002814 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002815 *arg = skipwhite(*arg);
2816 }
2817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 if (compile_expr1(arg, cctx) == FAIL)
2819 return FAIL;
2820 ++count;
2821
Bram Moolenaar2c330432020-04-13 14:41:35 +02002822 whitep = *arg;
2823 p = skipwhite(*arg);
2824 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002825 {
2826 *arg = next_line_from_context(cctx);
2827 if (*arg == NULL)
2828 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002829 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002830 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002831 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 if (**arg == '}')
2834 break;
2835 if (**arg != ',')
2836 {
2837 semsg(_(e_missing_dict_comma), *arg);
2838 goto failret;
2839 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002840 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 *arg = skipwhite(*arg + 1);
2842 }
2843
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 *arg = *arg + 1;
2845
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002846 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002847 p = skipwhite(*arg);
2848 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002849 *arg += STRLEN(*arg);
2850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 dict_unref(d);
2852 return generate_NEWDICT(cctx, count);
2853
2854failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002855 if (*arg == NULL)
2856 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 dict_unref(d);
2858 return FAIL;
2859}
2860
2861/*
2862 * Compile "&option".
2863 */
2864 static int
2865compile_get_option(char_u **arg, cctx_T *cctx)
2866{
2867 typval_T rettv;
2868 char_u *start = *arg;
2869 int ret;
2870
2871 // parse the option and get the current value to get the type.
2872 rettv.v_type = VAR_UNKNOWN;
2873 ret = get_option_tv(arg, &rettv, TRUE);
2874 if (ret == OK)
2875 {
2876 // include the '&' in the name, get_option_tv() expects it.
2877 char_u *name = vim_strnsave(start, *arg - start);
2878 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2879
2880 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2881 vim_free(name);
2882 }
2883 clear_tv(&rettv);
2884
2885 return ret;
2886}
2887
2888/*
2889 * Compile "$VAR".
2890 */
2891 static int
2892compile_get_env(char_u **arg, cctx_T *cctx)
2893{
2894 char_u *start = *arg;
2895 int len;
2896 int ret;
2897 char_u *name;
2898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 ++*arg;
2900 len = get_env_len(arg);
2901 if (len == 0)
2902 {
2903 semsg(_(e_syntax_at), start - 1);
2904 return FAIL;
2905 }
2906
2907 // include the '$' in the name, get_env_tv() expects it.
2908 name = vim_strnsave(start, len + 1);
2909 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2910 vim_free(name);
2911 return ret;
2912}
2913
2914/*
2915 * Compile "@r".
2916 */
2917 static int
2918compile_get_register(char_u **arg, cctx_T *cctx)
2919{
2920 int ret;
2921
2922 ++*arg;
2923 if (**arg == NUL)
2924 {
2925 semsg(_(e_syntax_at), *arg - 1);
2926 return FAIL;
2927 }
2928 if (!valid_yank_reg(**arg, TRUE))
2929 {
2930 emsg_invreg(**arg);
2931 return FAIL;
2932 }
2933 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2934 ++*arg;
2935 return ret;
2936}
2937
2938/*
2939 * Apply leading '!', '-' and '+' to constant "rettv".
2940 */
2941 static int
2942apply_leader(typval_T *rettv, char_u *start, char_u *end)
2943{
2944 char_u *p = end;
2945
2946 // this works from end to start
2947 while (p > start)
2948 {
2949 --p;
2950 if (*p == '-' || *p == '+')
2951 {
2952 // only '-' has an effect, for '+' we only check the type
2953#ifdef FEAT_FLOAT
2954 if (rettv->v_type == VAR_FLOAT)
2955 {
2956 if (*p == '-')
2957 rettv->vval.v_float = -rettv->vval.v_float;
2958 }
2959 else
2960#endif
2961 {
2962 varnumber_T val;
2963 int error = FALSE;
2964
2965 // tv_get_number_chk() accepts a string, but we don't want that
2966 // here
2967 if (check_not_string(rettv) == FAIL)
2968 return FAIL;
2969 val = tv_get_number_chk(rettv, &error);
2970 clear_tv(rettv);
2971 if (error)
2972 return FAIL;
2973 if (*p == '-')
2974 val = -val;
2975 rettv->v_type = VAR_NUMBER;
2976 rettv->vval.v_number = val;
2977 }
2978 }
2979 else
2980 {
2981 int v = tv2bool(rettv);
2982
2983 // '!' is permissive in the type.
2984 clear_tv(rettv);
2985 rettv->v_type = VAR_BOOL;
2986 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2987 }
2988 }
2989 return OK;
2990}
2991
2992/*
2993 * Recognize v: variables that are constants and set "rettv".
2994 */
2995 static void
2996get_vim_constant(char_u **arg, typval_T *rettv)
2997{
2998 if (STRNCMP(*arg, "v:true", 6) == 0)
2999 {
3000 rettv->v_type = VAR_BOOL;
3001 rettv->vval.v_number = VVAL_TRUE;
3002 *arg += 6;
3003 }
3004 else if (STRNCMP(*arg, "v:false", 7) == 0)
3005 {
3006 rettv->v_type = VAR_BOOL;
3007 rettv->vval.v_number = VVAL_FALSE;
3008 *arg += 7;
3009 }
3010 else if (STRNCMP(*arg, "v:null", 6) == 0)
3011 {
3012 rettv->v_type = VAR_SPECIAL;
3013 rettv->vval.v_number = VVAL_NULL;
3014 *arg += 6;
3015 }
3016 else if (STRNCMP(*arg, "v:none", 6) == 0)
3017 {
3018 rettv->v_type = VAR_SPECIAL;
3019 rettv->vval.v_number = VVAL_NONE;
3020 *arg += 6;
3021 }
3022}
3023
3024/*
3025 * Compile code to apply '-', '+' and '!'.
3026 */
3027 static int
3028compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3029{
3030 char_u *p = end;
3031
3032 // this works from end to start
3033 while (p > start)
3034 {
3035 --p;
3036 if (*p == '-' || *p == '+')
3037 {
3038 int negate = *p == '-';
3039 isn_T *isn;
3040
3041 // TODO: check type
3042 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3043 {
3044 --p;
3045 if (*p == '-')
3046 negate = !negate;
3047 }
3048 // only '-' has an effect, for '+' we only check the type
3049 if (negate)
3050 isn = generate_instr(cctx, ISN_NEGATENR);
3051 else
3052 isn = generate_instr(cctx, ISN_CHECKNR);
3053 if (isn == NULL)
3054 return FAIL;
3055 }
3056 else
3057 {
3058 int invert = TRUE;
3059
3060 while (p > start && p[-1] == '!')
3061 {
3062 --p;
3063 invert = !invert;
3064 }
3065 if (generate_2BOOL(cctx, invert) == FAIL)
3066 return FAIL;
3067 }
3068 }
3069 return OK;
3070}
3071
3072/*
3073 * Compile whatever comes after "name" or "name()".
3074 */
3075 static int
3076compile_subscript(
3077 char_u **arg,
3078 cctx_T *cctx,
3079 char_u **start_leader,
3080 char_u *end_leader)
3081{
3082 for (;;)
3083 {
3084 if (**arg == '(')
3085 {
3086 int argcount = 0;
3087
3088 // funcref(arg)
3089 *arg = skipwhite(*arg + 1);
3090 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3091 return FAIL;
3092 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
3093 return FAIL;
3094 }
3095 else if (**arg == '-' && (*arg)[1] == '>')
3096 {
3097 char_u *p;
3098
3099 // something->method()
3100 // Apply the '!', '-' and '+' first:
3101 // -1.0->func() works like (-1.0)->func()
3102 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3103 return FAIL;
3104 *start_leader = end_leader; // don't apply again later
3105
3106 *arg = skipwhite(*arg + 2);
3107 if (**arg == '{')
3108 {
3109 // lambda call: list->{lambda}
3110 if (compile_lambda_call(arg, cctx) == FAIL)
3111 return FAIL;
3112 }
3113 else
3114 {
3115 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003116 p = *arg;
3117 if (ASCII_ISALPHA(*p) && p[1] == ':')
3118 p += 2;
3119 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 ;
3121 if (*p != '(')
3122 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003123 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 return FAIL;
3125 }
3126 // TODO: base value may not be the first argument
3127 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
3128 return FAIL;
3129 }
3130 }
3131 else if (**arg == '[')
3132 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003133 garray_T *stack;
3134 type_T **typep;
3135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 // list index: list[123]
3137 // TODO: more arguments
3138 // TODO: dict member dict['name']
3139 *arg = skipwhite(*arg + 1);
3140 if (compile_expr1(arg, cctx) == FAIL)
3141 return FAIL;
3142
3143 if (**arg != ']')
3144 {
3145 emsg(_(e_missbrac));
3146 return FAIL;
3147 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003148 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149
3150 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3151 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003152 stack = &cctx->ctx_type_stack;
3153 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3154 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3155 {
3156 emsg(_(e_listreq));
3157 return FAIL;
3158 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003159 if ((*typep)->tt_type == VAR_LIST)
3160 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 }
3162 else if (**arg == '.' && (*arg)[1] != '.')
3163 {
3164 char_u *p;
3165
3166 ++*arg;
3167 p = *arg;
3168 // dictionary member: dict.name
3169 if (eval_isnamec1(*p))
3170 while (eval_isnamec(*p))
3171 MB_PTR_ADV(p);
3172 if (p == *arg)
3173 {
3174 semsg(_(e_syntax_at), *arg);
3175 return FAIL;
3176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3178 return FAIL;
3179 *arg = p;
3180 }
3181 else
3182 break;
3183 }
3184
3185 // TODO - see handle_subscript():
3186 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3187 // Don't do this when "Func" is already a partial that was bound
3188 // explicitly (pt_auto is FALSE).
3189
3190 return OK;
3191}
3192
3193/*
3194 * Compile an expression at "*p" and add instructions to "instr".
3195 * "p" is advanced until after the expression, skipping white space.
3196 *
3197 * This is the equivalent of eval1(), eval2(), etc.
3198 */
3199
3200/*
3201 * number number constant
3202 * 0zFFFFFFFF Blob constant
3203 * "string" string constant
3204 * 'string' literal string constant
3205 * &option-name option value
3206 * @r register contents
3207 * identifier variable value
3208 * function() function call
3209 * $VAR environment variable
3210 * (expression) nested expression
3211 * [expr, expr] List
3212 * {key: val, key: val} Dictionary
3213 * #{key: val, key: val} Dictionary with literal keys
3214 *
3215 * Also handle:
3216 * ! in front logical NOT
3217 * - in front unary minus
3218 * + in front unary plus (ignored)
3219 * trailing (arg) funcref/partial call
3220 * trailing [] subscript in String or List
3221 * trailing .name entry in Dictionary
3222 * trailing ->name() method call
3223 */
3224 static int
3225compile_expr7(char_u **arg, cctx_T *cctx)
3226{
3227 typval_T rettv;
3228 char_u *start_leader, *end_leader;
3229 int ret = OK;
3230
3231 /*
3232 * Skip '!', '-' and '+' characters. They are handled later.
3233 */
3234 start_leader = *arg;
3235 while (**arg == '!' || **arg == '-' || **arg == '+')
3236 *arg = skipwhite(*arg + 1);
3237 end_leader = *arg;
3238
3239 rettv.v_type = VAR_UNKNOWN;
3240 switch (**arg)
3241 {
3242 /*
3243 * Number constant.
3244 */
3245 case '0': // also for blob starting with 0z
3246 case '1':
3247 case '2':
3248 case '3':
3249 case '4':
3250 case '5':
3251 case '6':
3252 case '7':
3253 case '8':
3254 case '9':
3255 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3256 return FAIL;
3257 break;
3258
3259 /*
3260 * String constant: "string".
3261 */
3262 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3263 return FAIL;
3264 break;
3265
3266 /*
3267 * Literal string constant: 'str''ing'.
3268 */
3269 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3270 return FAIL;
3271 break;
3272
3273 /*
3274 * Constant Vim variable.
3275 */
3276 case 'v': get_vim_constant(arg, &rettv);
3277 ret = NOTDONE;
3278 break;
3279
3280 /*
3281 * List: [expr, expr]
3282 */
3283 case '[': ret = compile_list(arg, cctx);
3284 break;
3285
3286 /*
3287 * Dictionary: #{key: val, key: val}
3288 */
3289 case '#': if ((*arg)[1] == '{')
3290 {
3291 ++*arg;
3292 ret = compile_dict(arg, cctx, TRUE);
3293 }
3294 else
3295 ret = NOTDONE;
3296 break;
3297
3298 /*
3299 * Lambda: {arg, arg -> expr}
3300 * Dictionary: {'key': val, 'key': val}
3301 */
3302 case '{': {
3303 char_u *start = skipwhite(*arg + 1);
3304
3305 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003306 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003308 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 if (ret != FAIL && *start == '>')
3310 ret = compile_lambda(arg, cctx);
3311 else
3312 ret = compile_dict(arg, cctx, FALSE);
3313 }
3314 break;
3315
3316 /*
3317 * Option value: &name
3318 */
3319 case '&': ret = compile_get_option(arg, cctx);
3320 break;
3321
3322 /*
3323 * Environment variable: $VAR.
3324 */
3325 case '$': ret = compile_get_env(arg, cctx);
3326 break;
3327
3328 /*
3329 * Register contents: @r.
3330 */
3331 case '@': ret = compile_get_register(arg, cctx);
3332 break;
3333 /*
3334 * nested expression: (expression).
3335 */
3336 case '(': *arg = skipwhite(*arg + 1);
3337 ret = compile_expr1(arg, cctx); // recursive!
3338 *arg = skipwhite(*arg);
3339 if (**arg == ')')
3340 ++*arg;
3341 else if (ret == OK)
3342 {
3343 emsg(_(e_missing_close));
3344 ret = FAIL;
3345 }
3346 break;
3347
3348 default: ret = NOTDONE;
3349 break;
3350 }
3351 if (ret == FAIL)
3352 return FAIL;
3353
3354 if (rettv.v_type != VAR_UNKNOWN)
3355 {
3356 // apply the '!', '-' and '+' before the constant
3357 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3358 {
3359 clear_tv(&rettv);
3360 return FAIL;
3361 }
3362 start_leader = end_leader; // don't apply again below
3363
3364 // push constant
3365 switch (rettv.v_type)
3366 {
3367 case VAR_BOOL:
3368 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3369 break;
3370 case VAR_SPECIAL:
3371 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3372 break;
3373 case VAR_NUMBER:
3374 generate_PUSHNR(cctx, rettv.vval.v_number);
3375 break;
3376#ifdef FEAT_FLOAT
3377 case VAR_FLOAT:
3378 generate_PUSHF(cctx, rettv.vval.v_float);
3379 break;
3380#endif
3381 case VAR_BLOB:
3382 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3383 rettv.vval.v_blob = NULL;
3384 break;
3385 case VAR_STRING:
3386 generate_PUSHS(cctx, rettv.vval.v_string);
3387 rettv.vval.v_string = NULL;
3388 break;
3389 default:
3390 iemsg("constant type missing");
3391 return FAIL;
3392 }
3393 }
3394 else if (ret == NOTDONE)
3395 {
3396 char_u *p;
3397 int r;
3398
3399 if (!eval_isnamec1(**arg))
3400 {
3401 semsg(_("E1015: Name expected: %s"), *arg);
3402 return FAIL;
3403 }
3404
3405 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003406 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 if (*p == '(')
3408 r = compile_call(arg, p - *arg, cctx, 0);
3409 else
3410 r = compile_load(arg, p, cctx, TRUE);
3411 if (r == FAIL)
3412 return FAIL;
3413 }
3414
3415 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3416 return FAIL;
3417
3418 // Now deal with prefixed '-', '+' and '!', if not done already.
3419 return compile_leader(cctx, start_leader, end_leader);
3420}
3421
3422/*
3423 * * number multiplication
3424 * / number division
3425 * % number modulo
3426 */
3427 static int
3428compile_expr6(char_u **arg, cctx_T *cctx)
3429{
3430 char_u *op;
3431
3432 // get the first variable
3433 if (compile_expr7(arg, cctx) == FAIL)
3434 return FAIL;
3435
3436 /*
3437 * Repeat computing, until no "*", "/" or "%" is following.
3438 */
3439 for (;;)
3440 {
3441 op = skipwhite(*arg);
3442 if (*op != '*' && *op != '/' && *op != '%')
3443 break;
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003444 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 {
3446 char_u buf[3];
3447
3448 vim_strncpy(buf, op, 1);
3449 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003450 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 }
3452 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003453 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003454 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455
3456 // get the second variable
3457 if (compile_expr7(arg, cctx) == FAIL)
3458 return FAIL;
3459
3460 generate_two_op(cctx, op);
3461 }
3462
3463 return OK;
3464}
3465
3466/*
3467 * + number addition
3468 * - number subtraction
3469 * .. string concatenation
3470 */
3471 static int
3472compile_expr5(char_u **arg, cctx_T *cctx)
3473{
3474 char_u *op;
3475 int oplen;
3476
3477 // get the first variable
3478 if (compile_expr6(arg, cctx) == FAIL)
3479 return FAIL;
3480
3481 /*
3482 * Repeat computing, until no "+", "-" or ".." is following.
3483 */
3484 for (;;)
3485 {
3486 op = skipwhite(*arg);
3487 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3488 break;
3489 oplen = (*op == '.' ? 2 : 1);
3490
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003491 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 {
3493 char_u buf[3];
3494
3495 vim_strncpy(buf, op, oplen);
3496 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003497 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 }
3499
3500 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003501 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003502 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503
3504 // get the second variable
3505 if (compile_expr6(arg, cctx) == FAIL)
3506 return FAIL;
3507
3508 if (*op == '.')
3509 {
3510 if (may_generate_2STRING(-2, cctx) == FAIL
3511 || may_generate_2STRING(-1, cctx) == FAIL)
3512 return FAIL;
3513 generate_instr_drop(cctx, ISN_CONCAT, 1);
3514 }
3515 else
3516 generate_two_op(cctx, op);
3517 }
3518
3519 return OK;
3520}
3521
Bram Moolenaar080457c2020-03-03 21:53:32 +01003522 static exptype_T
3523get_compare_type(char_u *p, int *len, int *type_is)
3524{
3525 exptype_T type = EXPR_UNKNOWN;
3526 int i;
3527
3528 switch (p[0])
3529 {
3530 case '=': if (p[1] == '=')
3531 type = EXPR_EQUAL;
3532 else if (p[1] == '~')
3533 type = EXPR_MATCH;
3534 break;
3535 case '!': if (p[1] == '=')
3536 type = EXPR_NEQUAL;
3537 else if (p[1] == '~')
3538 type = EXPR_NOMATCH;
3539 break;
3540 case '>': if (p[1] != '=')
3541 {
3542 type = EXPR_GREATER;
3543 *len = 1;
3544 }
3545 else
3546 type = EXPR_GEQUAL;
3547 break;
3548 case '<': if (p[1] != '=')
3549 {
3550 type = EXPR_SMALLER;
3551 *len = 1;
3552 }
3553 else
3554 type = EXPR_SEQUAL;
3555 break;
3556 case 'i': if (p[1] == 's')
3557 {
3558 // "is" and "isnot"; but not a prefix of a name
3559 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3560 *len = 5;
3561 i = p[*len];
3562 if (!isalnum(i) && i != '_')
3563 {
3564 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3565 *type_is = TRUE;
3566 }
3567 }
3568 break;
3569 }
3570 return type;
3571}
3572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573/*
3574 * expr5a == expr5b
3575 * expr5a =~ expr5b
3576 * expr5a != expr5b
3577 * expr5a !~ expr5b
3578 * expr5a > expr5b
3579 * expr5a >= expr5b
3580 * expr5a < expr5b
3581 * expr5a <= expr5b
3582 * expr5a is expr5b
3583 * expr5a isnot expr5b
3584 *
3585 * Produces instructions:
3586 * EVAL expr5a Push result of "expr5a"
3587 * EVAL expr5b Push result of "expr5b"
3588 * COMPARE one of the compare instructions
3589 */
3590 static int
3591compile_expr4(char_u **arg, cctx_T *cctx)
3592{
3593 exptype_T type = EXPR_UNKNOWN;
3594 char_u *p;
3595 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 int type_is = FALSE;
3597
3598 // get the first variable
3599 if (compile_expr5(arg, cctx) == FAIL)
3600 return FAIL;
3601
3602 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003603 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604
3605 /*
3606 * If there is a comparative operator, use it.
3607 */
3608 if (type != EXPR_UNKNOWN)
3609 {
3610 int ic = FALSE; // Default: do not ignore case
3611
3612 if (type_is && (p[len] == '?' || p[len] == '#'))
3613 {
3614 semsg(_(e_invexpr2), *arg);
3615 return FAIL;
3616 }
3617 // extra question mark appended: ignore case
3618 if (p[len] == '?')
3619 {
3620 ic = TRUE;
3621 ++len;
3622 }
3623 // extra '#' appended: match case (ignored)
3624 else if (p[len] == '#')
3625 ++len;
3626 // nothing appended: match case
3627
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003628 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 {
3630 char_u buf[7];
3631
3632 vim_strncpy(buf, p, len);
3633 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003634 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 }
3636
3637 // get the second variable
3638 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003639 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003640 return FAIL;
3641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 if (compile_expr5(arg, cctx) == FAIL)
3643 return FAIL;
3644
3645 generate_COMPARE(cctx, type, ic);
3646 }
3647
3648 return OK;
3649}
3650
3651/*
3652 * Compile || or &&.
3653 */
3654 static int
3655compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3656{
3657 char_u *p = skipwhite(*arg);
3658 int opchar = *op;
3659
3660 if (p[0] == opchar && p[1] == opchar)
3661 {
3662 garray_T *instr = &cctx->ctx_instr;
3663 garray_T end_ga;
3664
3665 /*
3666 * Repeat until there is no following "||" or "&&"
3667 */
3668 ga_init2(&end_ga, sizeof(int), 10);
3669 while (p[0] == opchar && p[1] == opchar)
3670 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003671 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3672 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003674 return FAIL;
3675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676
3677 if (ga_grow(&end_ga, 1) == FAIL)
3678 {
3679 ga_clear(&end_ga);
3680 return FAIL;
3681 }
3682 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3683 ++end_ga.ga_len;
3684 generate_JUMP(cctx, opchar == '|'
3685 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3686
3687 // eval the next expression
3688 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003689 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003690 return FAIL;
3691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 if ((opchar == '|' ? compile_expr3(arg, cctx)
3693 : compile_expr4(arg, cctx)) == FAIL)
3694 {
3695 ga_clear(&end_ga);
3696 return FAIL;
3697 }
3698 p = skipwhite(*arg);
3699 }
3700
3701 // Fill in the end label in all jumps.
3702 while (end_ga.ga_len > 0)
3703 {
3704 isn_T *isn;
3705
3706 --end_ga.ga_len;
3707 isn = ((isn_T *)instr->ga_data)
3708 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3709 isn->isn_arg.jump.jump_where = instr->ga_len;
3710 }
3711 ga_clear(&end_ga);
3712 }
3713
3714 return OK;
3715}
3716
3717/*
3718 * expr4a && expr4a && expr4a logical AND
3719 *
3720 * Produces instructions:
3721 * EVAL expr4a Push result of "expr4a"
3722 * JUMP_AND_KEEP_IF_FALSE end
3723 * EVAL expr4b Push result of "expr4b"
3724 * JUMP_AND_KEEP_IF_FALSE end
3725 * EVAL expr4c Push result of "expr4c"
3726 * end:
3727 */
3728 static int
3729compile_expr3(char_u **arg, cctx_T *cctx)
3730{
3731 // get the first variable
3732 if (compile_expr4(arg, cctx) == FAIL)
3733 return FAIL;
3734
3735 // || and && work almost the same
3736 return compile_and_or(arg, cctx, "&&");
3737}
3738
3739/*
3740 * expr3a || expr3b || expr3c logical OR
3741 *
3742 * Produces instructions:
3743 * EVAL expr3a Push result of "expr3a"
3744 * JUMP_AND_KEEP_IF_TRUE end
3745 * EVAL expr3b Push result of "expr3b"
3746 * JUMP_AND_KEEP_IF_TRUE end
3747 * EVAL expr3c Push result of "expr3c"
3748 * end:
3749 */
3750 static int
3751compile_expr2(char_u **arg, cctx_T *cctx)
3752{
3753 // eval the first expression
3754 if (compile_expr3(arg, cctx) == FAIL)
3755 return FAIL;
3756
3757 // || and && work almost the same
3758 return compile_and_or(arg, cctx, "||");
3759}
3760
3761/*
3762 * Toplevel expression: expr2 ? expr1a : expr1b
3763 *
3764 * Produces instructions:
3765 * EVAL expr2 Push result of "expr"
3766 * JUMP_IF_FALSE alt jump if false
3767 * EVAL expr1a
3768 * JUMP_ALWAYS end
3769 * alt: EVAL expr1b
3770 * end:
3771 */
3772 static int
3773compile_expr1(char_u **arg, cctx_T *cctx)
3774{
3775 char_u *p;
3776
3777 // evaluate the first expression
3778 if (compile_expr2(arg, cctx) == FAIL)
3779 return FAIL;
3780
3781 p = skipwhite(*arg);
3782 if (*p == '?')
3783 {
3784 garray_T *instr = &cctx->ctx_instr;
3785 garray_T *stack = &cctx->ctx_type_stack;
3786 int alt_idx = instr->ga_len;
3787 int end_idx;
3788 isn_T *isn;
3789 type_T *type1;
3790 type_T *type2;
3791
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003792 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3793 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003795 return FAIL;
3796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797
3798 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3799
3800 // evaluate the second expression; any type is accepted
3801 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003802 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003803 return FAIL;
3804
Bram Moolenaara6d53682020-01-28 23:04:06 +01003805 if (compile_expr1(arg, cctx) == FAIL)
3806 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
3808 // remember the type and drop it
3809 --stack->ga_len;
3810 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3811
3812 end_idx = instr->ga_len;
3813 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3814
3815 // jump here from JUMP_IF_FALSE
3816 isn = ((isn_T *)instr->ga_data) + alt_idx;
3817 isn->isn_arg.jump.jump_where = instr->ga_len;
3818
3819 // Check for the ":".
3820 p = skipwhite(*arg);
3821 if (*p != ':')
3822 {
3823 emsg(_(e_missing_colon));
3824 return FAIL;
3825 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003826 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3827 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003829 return FAIL;
3830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831
3832 // evaluate the third expression
3833 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003834 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003835 return FAIL;
3836
Bram Moolenaara6d53682020-01-28 23:04:06 +01003837 if (compile_expr1(arg, cctx) == FAIL)
3838 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839
3840 // If the types differ, the result has a more generic type.
3841 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003842 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843
3844 // jump here from JUMP_ALWAYS
3845 isn = ((isn_T *)instr->ga_data) + end_idx;
3846 isn->isn_arg.jump.jump_where = instr->ga_len;
3847 }
3848 return OK;
3849}
3850
3851/*
3852 * compile "return [expr]"
3853 */
3854 static char_u *
3855compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3856{
3857 char_u *p = arg;
3858 garray_T *stack = &cctx->ctx_type_stack;
3859 type_T *stack_type;
3860
3861 if (*p != NUL && *p != '|' && *p != '\n')
3862 {
3863 // compile return argument into instructions
3864 if (compile_expr1(&p, cctx) == FAIL)
3865 return NULL;
3866
3867 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3868 if (set_return_type)
3869 cctx->ctx_ufunc->uf_ret_type = stack_type;
3870 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3871 == FAIL)
3872 return NULL;
3873 }
3874 else
3875 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003876 // "set_return_type" cannot be TRUE, only used for a lambda which
3877 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02003878 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
3879 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 {
3881 emsg(_("E1003: Missing return value"));
3882 return NULL;
3883 }
3884
3885 // No argument, return zero.
3886 generate_PUSHNR(cctx, 0);
3887 }
3888
3889 if (generate_instr(cctx, ISN_RETURN) == NULL)
3890 return NULL;
3891
3892 // "return val | endif" is possible
3893 return skipwhite(p);
3894}
3895
3896/*
3897 * Return the length of an assignment operator, or zero if there isn't one.
3898 */
3899 int
3900assignment_len(char_u *p, int *heredoc)
3901{
3902 if (*p == '=')
3903 {
3904 if (p[1] == '<' && p[2] == '<')
3905 {
3906 *heredoc = TRUE;
3907 return 3;
3908 }
3909 return 1;
3910 }
3911 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3912 return 2;
3913 if (STRNCMP(p, "..=", 3) == 0)
3914 return 3;
3915 return 0;
3916}
3917
3918// words that cannot be used as a variable
3919static char *reserved[] = {
3920 "true",
3921 "false",
3922 NULL
3923};
3924
3925/*
3926 * Get a line for "=<<".
3927 * Return a pointer to the line in allocated memory.
3928 * Return NULL for end-of-file or some error.
3929 */
3930 static char_u *
3931heredoc_getline(
3932 int c UNUSED,
3933 void *cookie,
3934 int indent UNUSED,
3935 int do_concat UNUSED)
3936{
3937 cctx_T *cctx = (cctx_T *)cookie;
3938
3939 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003940 {
3941 iemsg("Heredoc got to end");
3942 return NULL;
3943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 ++cctx->ctx_lnum;
3945 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3946 [cctx->ctx_lnum]);
3947}
3948
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003949typedef enum {
3950 dest_local,
3951 dest_option,
3952 dest_env,
3953 dest_global,
3954 dest_vimvar,
3955 dest_script,
3956 dest_reg,
3957} assign_dest_T;
3958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959/*
3960 * compile "let var [= expr]", "const var = expr" and "var = expr"
3961 * "arg" points to "var".
3962 */
3963 static char_u *
3964compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3965{
3966 char_u *p;
3967 char_u *ret = NULL;
3968 int var_count = 0;
3969 int semicolon = 0;
3970 size_t varlen;
3971 garray_T *instr = &cctx->ctx_instr;
3972 int idx = -1;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003973 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003976 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003978 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 int oplen = 0;
3980 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003981 type_T *type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 lvar_T *lvar;
3983 char_u *name;
3984 char_u *sp;
3985 int has_type = FALSE;
3986 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3987 int instr_count = -1;
3988
3989 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3990 if (p == NULL)
3991 return NULL;
3992 if (var_count > 0)
3993 {
3994 // TODO: let [var, var] = list
3995 emsg("Cannot handle a list yet");
3996 return NULL;
3997 }
3998
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003999 // "a: type" is declaring variable "a" with a type, not "a:".
4000 if (is_decl && p == arg + 2 && p[-1] == ':')
4001 --p;
4002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 varlen = p - arg;
4004 name = vim_strnsave(arg, (int)varlen);
4005 if (name == NULL)
4006 return NULL;
4007
Bram Moolenaar080457c2020-03-03 21:53:32 +01004008 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004010 if (*arg == '&')
4011 {
4012 int cc;
4013 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014
Bram Moolenaar080457c2020-03-03 21:53:32 +01004015 dest = dest_option;
4016 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004018 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004019 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 if (is_decl)
4022 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004023 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 goto theend;
4025 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004026 p = arg;
4027 p = find_option_end(&p, &opt_flags);
4028 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004030 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004031 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004032 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004033 }
4034 cc = *p;
4035 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004036 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004037 *p = cc;
4038 if (opt_type == -3)
4039 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004040 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004041 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004042 }
4043 if (opt_type == -2 || opt_type == 0)
4044 type = &t_string;
4045 else
4046 type = &t_number; // both number and boolean option
4047 }
4048 else if (*arg == '$')
4049 {
4050 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004051 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004052 if (is_decl)
4053 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004054 semsg(_("E1065: Cannot declare an environment variable: %s"),
4055 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004056 goto theend;
4057 }
4058 }
4059 else if (*arg == '@')
4060 {
4061 if (!valid_yank_reg(arg[1], TRUE))
4062 {
4063 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004064 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004065 }
4066 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004067 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004068 if (is_decl)
4069 {
4070 semsg(_("E1066: Cannot declare a register: %s"), name);
4071 goto theend;
4072 }
4073 }
4074 else if (STRNCMP(arg, "g:", 2) == 0)
4075 {
4076 dest = dest_global;
4077 if (is_decl)
4078 {
4079 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4080 goto theend;
4081 }
4082 }
4083 else if (STRNCMP(arg, "v:", 2) == 0)
4084 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004085 typval_T *vtv;
4086 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004087
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004088 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004089 if (vimvaridx < 0)
4090 {
4091 semsg(_(e_var_notfound), arg);
4092 goto theend;
4093 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004094 // We use the current value of "sandbox" here, is that OK?
4095 if (var_check_ro(di_flags, name, FALSE))
4096 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004097 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004098 vtv = get_vim_var_tv(vimvaridx);
4099 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004100 if (is_decl)
4101 {
4102 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4103 goto theend;
4104 }
4105 }
4106 else
4107 {
4108 for (idx = 0; reserved[idx] != NULL; ++idx)
4109 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004111 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 goto theend;
4113 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004114
4115 idx = lookup_local(arg, varlen, cctx);
4116 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004118 if (is_decl)
4119 {
4120 semsg(_("E1017: Variable already declared: %s"), name);
4121 goto theend;
4122 }
4123 else
4124 {
4125 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
4126 if (lvar->lv_const)
4127 {
4128 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4129 goto theend;
4130 }
4131 }
4132 }
4133 else if (STRNCMP(arg, "s:", 2) == 0
4134 || lookup_script(arg, varlen) == OK
4135 || find_imported(arg, varlen, cctx) != NULL)
4136 {
4137 dest = dest_script;
4138 if (is_decl)
4139 {
4140 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004141 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004142 goto theend;
4143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 }
4145 }
4146 }
4147
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004148 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 {
4150 if (is_decl && *p == ':')
4151 {
4152 // parse optional type: "let var: type = expr"
4153 p = skipwhite(p + 1);
4154 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 has_type = TRUE;
4156 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02004157 else if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 {
4159 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
4160 type = lvar->lv_type;
4161 }
4162 }
4163
4164 sp = p;
4165 p = skipwhite(p);
4166 op = p;
4167 oplen = assignment_len(p, &heredoc);
4168 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4169 {
4170 char_u buf[4];
4171
4172 vim_strncpy(buf, op, oplen);
4173 semsg(_(e_white_both), buf);
4174 }
4175
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004176 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004177 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004179 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 goto theend;
4181 }
4182
Bram Moolenaar080457c2020-03-03 21:53:32 +01004183 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 {
4185 if (oplen > 1 && !heredoc)
4186 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004187 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4189 name);
4190 goto theend;
4191 }
4192
4193 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004194 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004195 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4197 if (idx < 0)
4198 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004199 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 }
4201
4202 if (heredoc)
4203 {
4204 list_T *l;
4205 listitem_T *li;
4206
4207 // [let] varname =<< [trim] {end}
4208 eap->getline = heredoc_getline;
4209 eap->cookie = cctx;
4210 l = heredoc_get(eap, op + 3);
4211
4212 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004213 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 {
4215 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4216 li->li_tv.vval.v_string = NULL;
4217 }
4218 generate_NEWLIST(cctx, l->lv_len);
4219 type = &t_list_string;
4220 list_free(l);
4221 p += STRLEN(p);
4222 }
4223 else if (oplen > 0)
4224 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004225 int r;
4226 type_T *stacktype;
4227 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 // for "+=", "*=", "..=" etc. first load the current value
4230 if (*op != '=')
4231 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004232 switch (dest)
4233 {
4234 case dest_option:
4235 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004236 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004237 break;
4238 case dest_global:
4239 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4240 break;
4241 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004242 compile_load_scriptvar(cctx,
4243 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004244 break;
4245 case dest_env:
4246 // Include $ in the name here
4247 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4248 break;
4249 case dest_reg:
4250 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4251 break;
4252 case dest_vimvar:
4253 generate_LOADV(cctx, name + 2, TRUE);
4254 break;
4255 case dest_local:
4256 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
4257 break;
4258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 }
4260
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004261 // Compile the expression. Temporarily hide the new local variable
4262 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004263 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004264 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 instr_count = instr->ga_len;
4266 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004267 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004268 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004269 ++cctx->ctx_locals.ga_len;
4270 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 goto theend;
4272
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004273 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004275 stack = &cctx->ctx_type_stack;
4276 stacktype = stack->ga_len == 0 ? &t_void
4277 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
4278 if (idx >= 0 && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004280 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
4281 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004283 if (stacktype->tt_type == VAR_VOID)
4284 {
4285 emsg(_("E1031: Cannot use void value"));
4286 goto theend;
4287 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004288 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004289 {
4290 // An empty list or dict has a &t_void member, for a
4291 // variable that implies &t_any.
4292 if (stacktype == &t_list_empty)
4293 lvar->lv_type = &t_list_any;
4294 else if (stacktype == &t_dict_empty)
4295 lvar->lv_type = &t_dict_any;
4296 else
4297 lvar->lv_type = stacktype;
4298 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004299 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004300 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4301 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004303 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004304 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 }
4306 }
4307 else if (cmdidx == CMD_const)
4308 {
4309 emsg(_("E1021: const requires a value"));
4310 goto theend;
4311 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004312 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 {
4314 emsg(_("E1022: type or initialization required"));
4315 goto theend;
4316 }
4317 else
4318 {
4319 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 if (ga_grow(instr, 1) == FAIL)
4321 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004322 switch (type->tt_type)
4323 {
4324 case VAR_BOOL:
4325 generate_PUSHBOOL(cctx, VVAL_FALSE);
4326 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004327 case VAR_FLOAT:
4328#ifdef FEAT_FLOAT
4329 generate_PUSHF(cctx, 0.0);
4330#endif
4331 break;
4332 case VAR_STRING:
4333 generate_PUSHS(cctx, NULL);
4334 break;
4335 case VAR_BLOB:
4336 generate_PUSHBLOB(cctx, NULL);
4337 break;
4338 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004339 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004340 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004341 case VAR_LIST:
4342 generate_NEWLIST(cctx, 0);
4343 break;
4344 case VAR_DICT:
4345 generate_NEWDICT(cctx, 0);
4346 break;
4347 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004348 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004349 break;
4350 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004351 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004352 break;
4353 case VAR_NUMBER:
4354 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004355 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02004356 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004357 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004358 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004359 generate_PUSHNR(cctx, 0);
4360 break;
4361 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 }
4363
4364 if (oplen > 0 && *op != '=')
4365 {
4366 type_T *expected = &t_number;
4367 garray_T *stack = &cctx->ctx_type_stack;
4368 type_T *stacktype;
4369
4370 // TODO: if type is known use float or any operation
4371
4372 if (*op == '.')
4373 expected = &t_string;
4374 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4375 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4376 goto theend;
4377
4378 if (*op == '.')
4379 generate_instr_drop(cctx, ISN_CONCAT, 1);
4380 else
4381 {
4382 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4383
4384 if (isn == NULL)
4385 goto theend;
4386 switch (*op)
4387 {
4388 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4389 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4390 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4391 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4392 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4393 }
4394 }
4395 }
4396
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004397 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004399 case dest_option:
4400 generate_STOREOPT(cctx, name + 1, opt_flags);
4401 break;
4402 case dest_global:
4403 // include g: with the name, easier to execute that way
4404 generate_STORE(cctx, ISN_STOREG, 0, name);
4405 break;
4406 case dest_env:
4407 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4408 break;
4409 case dest_reg:
4410 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4411 break;
4412 case dest_vimvar:
4413 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4414 break;
4415 case dest_script:
4416 {
4417 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4418 imported_T *import = NULL;
4419 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004420
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004421 if (name[1] != ':')
4422 {
4423 import = find_imported(name, 0, cctx);
4424 if (import != NULL)
4425 sid = import->imp_sid;
4426 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004428 idx = get_script_item_idx(sid, rawname, TRUE);
4429 // TODO: specific type
4430 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004431 {
4432 char_u *name_s = name;
4433
4434 // Include s: in the name for store_var()
4435 if (name[1] != ':')
4436 {
4437 int len = (int)STRLEN(name) + 3;
4438
4439 name_s = alloc(len);
4440 if (name_s == NULL)
4441 name_s = name;
4442 else
4443 vim_snprintf((char *)name_s, len, "s:%s", name);
4444 }
4445 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4446 if (name_s != name)
4447 vim_free(name_s);
4448 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004449 else
4450 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4451 sid, idx, &t_any);
4452 }
4453 break;
4454 case dest_local:
4455 {
4456 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004458 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4459 // into ISN_STORENR
4460 if (instr->ga_len == instr_count + 1
4461 && isn->isn_type == ISN_PUSHNR)
4462 {
4463 varnumber_T val = isn->isn_arg.number;
4464 garray_T *stack = &cctx->ctx_type_stack;
4465
4466 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004467 isn->isn_arg.storenr.stnr_idx = idx;
4468 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004469 if (stack->ga_len > 0)
4470 --stack->ga_len;
4471 }
4472 else
4473 generate_STORE(cctx, ISN_STORE, idx, NULL);
4474 }
4475 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 }
4477 ret = p;
4478
4479theend:
4480 vim_free(name);
4481 return ret;
4482}
4483
4484/*
4485 * Compile an :import command.
4486 */
4487 static char_u *
4488compile_import(char_u *arg, cctx_T *cctx)
4489{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004490 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491}
4492
4493/*
4494 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4495 */
4496 static int
4497compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4498{
4499 garray_T *instr = &cctx->ctx_instr;
4500 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4501
4502 if (endlabel == NULL)
4503 return FAIL;
4504 endlabel->el_next = *el;
4505 *el = endlabel;
4506 endlabel->el_end_label = instr->ga_len;
4507
4508 generate_JUMP(cctx, when, 0);
4509 return OK;
4510}
4511
4512 static void
4513compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4514{
4515 garray_T *instr = &cctx->ctx_instr;
4516
4517 while (*el != NULL)
4518 {
4519 endlabel_T *cur = (*el);
4520 isn_T *isn;
4521
4522 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4523 isn->isn_arg.jump.jump_where = instr->ga_len;
4524 *el = cur->el_next;
4525 vim_free(cur);
4526 }
4527}
4528
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004529 static void
4530compile_free_jump_to_end(endlabel_T **el)
4531{
4532 while (*el != NULL)
4533 {
4534 endlabel_T *cur = (*el);
4535
4536 *el = cur->el_next;
4537 vim_free(cur);
4538 }
4539}
4540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541/*
4542 * Create a new scope and set up the generic items.
4543 */
4544 static scope_T *
4545new_scope(cctx_T *cctx, scopetype_T type)
4546{
4547 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4548
4549 if (scope == NULL)
4550 return NULL;
4551 scope->se_outer = cctx->ctx_scope;
4552 cctx->ctx_scope = scope;
4553 scope->se_type = type;
4554 scope->se_local_count = cctx->ctx_locals.ga_len;
4555 return scope;
4556}
4557
4558/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004559 * Free the current scope and go back to the outer scope.
4560 */
4561 static void
4562drop_scope(cctx_T *cctx)
4563{
4564 scope_T *scope = cctx->ctx_scope;
4565
4566 if (scope == NULL)
4567 {
4568 iemsg("calling drop_scope() without a scope");
4569 return;
4570 }
4571 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004572 switch (scope->se_type)
4573 {
4574 case IF_SCOPE:
4575 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4576 case FOR_SCOPE:
4577 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4578 case WHILE_SCOPE:
4579 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4580 case TRY_SCOPE:
4581 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4582 case NO_SCOPE:
4583 case BLOCK_SCOPE:
4584 break;
4585 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004586 vim_free(scope);
4587}
4588
4589/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004590 * Evaluate an expression that is a constant:
4591 * has(arg)
4592 *
4593 * Also handle:
4594 * ! in front logical NOT
4595 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004596 * Return FAIL if the expression is not a constant.
4597 */
4598 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004599evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004600{
4601 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004602 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004603 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004604
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004605 /*
4606 * Skip '!' characters. They are handled later.
4607 */
4608 start_leader = *arg;
4609 while (**arg == '!')
4610 *arg = skipwhite(*arg + 1);
4611 end_leader = *arg;
4612
4613 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004614 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004615 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004616 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4617 {
4618 tv->v_type = VAR_SPECIAL;
4619 tv->vval.v_number = VVAL_TRUE;
4620 *arg += 4;
4621 return OK;
4622 }
4623 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4624 {
4625 tv->v_type = VAR_SPECIAL;
4626 tv->vval.v_number = VVAL_FALSE;
4627 *arg += 5;
4628 return OK;
4629 }
4630
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004631 if (STRNCMP("has(", *arg, 4) == 0)
4632 {
4633 has_call = TRUE;
4634 *arg = skipwhite(*arg + 4);
4635 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004636
4637 if (**arg == '"')
4638 {
4639 if (get_string_tv(arg, tv, TRUE) == FAIL)
4640 return FAIL;
4641 }
4642 else if (**arg == '\'')
4643 {
4644 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4645 return FAIL;
4646 }
4647 else
4648 return FAIL;
4649
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004650 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004651 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004652 *arg = skipwhite(*arg);
4653 if (**arg != ')')
4654 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004655 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004656
4657 argvars[0] = *tv;
4658 argvars[1].v_type = VAR_UNKNOWN;
4659 tv->v_type = VAR_NUMBER;
4660 tv->vval.v_number = 0;
4661 f_has(argvars, tv);
4662 clear_tv(&argvars[0]);
4663
4664 while (start_leader < end_leader)
4665 {
4666 if (*start_leader == '!')
4667 tv->vval.v_number = !tv->vval.v_number;
4668 ++start_leader;
4669 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004670 }
4671
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004672 return OK;
4673}
4674
Bram Moolenaar080457c2020-03-03 21:53:32 +01004675 static int
4676evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4677{
4678 exptype_T type = EXPR_UNKNOWN;
4679 char_u *p;
4680 int len = 2;
4681 int type_is = FALSE;
4682
4683 // get the first variable
4684 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4685 return FAIL;
4686
4687 p = skipwhite(*arg);
4688 type = get_compare_type(p, &len, &type_is);
4689
4690 /*
4691 * If there is a comparative operator, use it.
4692 */
4693 if (type != EXPR_UNKNOWN)
4694 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004695 typval_T tv2;
4696 char_u *s1, *s2;
4697 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4698 int n;
4699
4700 // TODO: Only string == string is supported now
4701 if (tv->v_type != VAR_STRING)
4702 return FAIL;
4703 if (type != EXPR_EQUAL)
4704 return FAIL;
4705
4706 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004707 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004708 *arg = skipwhite(p + len);
4709 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4710 || tv2.v_type != VAR_STRING)
4711 {
4712 clear_tv(&tv2);
4713 return FAIL;
4714 }
4715 s1 = tv_get_string_buf(tv, buf1);
4716 s2 = tv_get_string_buf(&tv2, buf2);
4717 n = STRCMP(s1, s2);
4718 clear_tv(tv);
4719 clear_tv(&tv2);
4720 tv->v_type = VAR_BOOL;
4721 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004722 }
4723
4724 return OK;
4725}
4726
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004727static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4728
4729/*
4730 * Compile constant || or &&.
4731 */
4732 static int
4733evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4734{
4735 char_u *p = skipwhite(*arg);
4736 int opchar = *op;
4737
4738 if (p[0] == opchar && p[1] == opchar)
4739 {
4740 int val = tv2bool(tv);
4741
4742 /*
4743 * Repeat until there is no following "||" or "&&"
4744 */
4745 while (p[0] == opchar && p[1] == opchar)
4746 {
4747 typval_T tv2;
4748
4749 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4750 return FAIL;
4751
4752 // eval the next expression
4753 *arg = skipwhite(p + 2);
4754 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004755 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004756 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004757 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004758 {
4759 clear_tv(&tv2);
4760 return FAIL;
4761 }
4762 if ((opchar == '&') == val)
4763 {
4764 // false || tv2 or true && tv2: use tv2
4765 clear_tv(tv);
4766 *tv = tv2;
4767 val = tv2bool(tv);
4768 }
4769 else
4770 clear_tv(&tv2);
4771 p = skipwhite(*arg);
4772 }
4773 }
4774
4775 return OK;
4776}
4777
4778/*
4779 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4780 * Return FAIL if the expression is not a constant.
4781 */
4782 static int
4783evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4784{
4785 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004786 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004787 return FAIL;
4788
4789 // || and && work almost the same
4790 return evaluate_const_and_or(arg, cctx, "&&", tv);
4791}
4792
4793/*
4794 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4795 * Return FAIL if the expression is not a constant.
4796 */
4797 static int
4798evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4799{
4800 // evaluate the first expression
4801 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4802 return FAIL;
4803
4804 // || and && work almost the same
4805 return evaluate_const_and_or(arg, cctx, "||", tv);
4806}
4807
4808/*
4809 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4810 * E.g. for "has('feature')".
4811 * This does not produce error messages. "tv" should be cleared afterwards.
4812 * Return FAIL if the expression is not a constant.
4813 */
4814 static int
4815evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4816{
4817 char_u *p;
4818
4819 // evaluate the first expression
4820 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4821 return FAIL;
4822
4823 p = skipwhite(*arg);
4824 if (*p == '?')
4825 {
4826 int val = tv2bool(tv);
4827 typval_T tv2;
4828
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004829 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004830 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4831 return FAIL;
4832
4833 // evaluate the second expression; any type is accepted
4834 clear_tv(tv);
4835 *arg = skipwhite(p + 1);
4836 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4837 return FAIL;
4838
4839 // Check for the ":".
4840 p = skipwhite(*arg);
4841 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4842 return FAIL;
4843
4844 // evaluate the third expression
4845 *arg = skipwhite(p + 1);
4846 tv2.v_type = VAR_UNKNOWN;
4847 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4848 {
4849 clear_tv(&tv2);
4850 return FAIL;
4851 }
4852 if (val)
4853 {
4854 // use the expr after "?"
4855 clear_tv(&tv2);
4856 }
4857 else
4858 {
4859 // use the expr after ":"
4860 clear_tv(tv);
4861 *tv = tv2;
4862 }
4863 }
4864 return OK;
4865}
4866
4867/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 * compile "if expr"
4869 *
4870 * "if expr" Produces instructions:
4871 * EVAL expr Push result of "expr"
4872 * JUMP_IF_FALSE end
4873 * ... body ...
4874 * end:
4875 *
4876 * "if expr | else" Produces instructions:
4877 * EVAL expr Push result of "expr"
4878 * JUMP_IF_FALSE else
4879 * ... body ...
4880 * JUMP_ALWAYS end
4881 * else:
4882 * ... body ...
4883 * end:
4884 *
4885 * "if expr1 | elseif expr2 | else" Produces instructions:
4886 * EVAL expr Push result of "expr"
4887 * JUMP_IF_FALSE elseif
4888 * ... body ...
4889 * JUMP_ALWAYS end
4890 * elseif:
4891 * EVAL expr Push result of "expr"
4892 * JUMP_IF_FALSE else
4893 * ... body ...
4894 * JUMP_ALWAYS end
4895 * else:
4896 * ... body ...
4897 * end:
4898 */
4899 static char_u *
4900compile_if(char_u *arg, cctx_T *cctx)
4901{
4902 char_u *p = arg;
4903 garray_T *instr = &cctx->ctx_instr;
4904 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004905 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004907 // compile "expr"; if we know it evaluates to FALSE skip the block
4908 tv.v_type = VAR_UNKNOWN;
4909 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4910 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4911 else
4912 cctx->ctx_skip = MAYBE;
4913 clear_tv(&tv);
4914 if (cctx->ctx_skip == MAYBE)
4915 {
4916 p = arg;
4917 if (compile_expr1(&p, cctx) == FAIL)
4918 return NULL;
4919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920
4921 scope = new_scope(cctx, IF_SCOPE);
4922 if (scope == NULL)
4923 return NULL;
4924
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004925 if (cctx->ctx_skip == MAYBE)
4926 {
4927 // "where" is set when ":elseif", "else" or ":endif" is found
4928 scope->se_u.se_if.is_if_label = instr->ga_len;
4929 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4930 }
4931 else
4932 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933
4934 return p;
4935}
4936
4937 static char_u *
4938compile_elseif(char_u *arg, cctx_T *cctx)
4939{
4940 char_u *p = arg;
4941 garray_T *instr = &cctx->ctx_instr;
4942 isn_T *isn;
4943 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004944 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945
4946 if (scope == NULL || scope->se_type != IF_SCOPE)
4947 {
4948 emsg(_(e_elseif_without_if));
4949 return NULL;
4950 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004951 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952
Bram Moolenaar158906c2020-02-06 20:39:45 +01004953 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004954 {
4955 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004957 return NULL;
4958 // previous "if" or "elseif" jumps here
4959 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4960 isn->isn_arg.jump.jump_where = instr->ga_len;
4961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004963 // compile "expr"; if we know it evaluates to FALSE skip the block
4964 tv.v_type = VAR_UNKNOWN;
4965 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4966 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4967 else
4968 cctx->ctx_skip = MAYBE;
4969 clear_tv(&tv);
4970 if (cctx->ctx_skip == MAYBE)
4971 {
4972 p = arg;
4973 if (compile_expr1(&p, cctx) == FAIL)
4974 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004976 // "where" is set when ":elseif", "else" or ":endif" is found
4977 scope->se_u.se_if.is_if_label = instr->ga_len;
4978 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4979 }
4980 else
4981 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982
4983 return p;
4984}
4985
4986 static char_u *
4987compile_else(char_u *arg, cctx_T *cctx)
4988{
4989 char_u *p = arg;
4990 garray_T *instr = &cctx->ctx_instr;
4991 isn_T *isn;
4992 scope_T *scope = cctx->ctx_scope;
4993
4994 if (scope == NULL || scope->se_type != IF_SCOPE)
4995 {
4996 emsg(_(e_else_without_if));
4997 return NULL;
4998 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004999 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005001 // jump from previous block to the end, unless the else block is empty
5002 if (cctx->ctx_skip == MAYBE)
5003 {
5004 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005006 return NULL;
5007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008
Bram Moolenaar158906c2020-02-06 20:39:45 +01005009 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005010 {
5011 if (scope->se_u.se_if.is_if_label >= 0)
5012 {
5013 // previous "if" or "elseif" jumps here
5014 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5015 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005016 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005017 }
5018 }
5019
5020 if (cctx->ctx_skip != MAYBE)
5021 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022
5023 return p;
5024}
5025
5026 static char_u *
5027compile_endif(char_u *arg, cctx_T *cctx)
5028{
5029 scope_T *scope = cctx->ctx_scope;
5030 ifscope_T *ifscope;
5031 garray_T *instr = &cctx->ctx_instr;
5032 isn_T *isn;
5033
5034 if (scope == NULL || scope->se_type != IF_SCOPE)
5035 {
5036 emsg(_(e_endif_without_if));
5037 return NULL;
5038 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005039 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005040 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005042 if (scope->se_u.se_if.is_if_label >= 0)
5043 {
5044 // previous "if" or "elseif" jumps here
5045 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5046 isn->isn_arg.jump.jump_where = instr->ga_len;
5047 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048 // Fill in the "end" label in jumps at the end of the blocks.
5049 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005050 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005052 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005053 return arg;
5054}
5055
5056/*
5057 * compile "for var in expr"
5058 *
5059 * Produces instructions:
5060 * PUSHNR -1
5061 * STORE loop-idx Set index to -1
5062 * EVAL expr Push result of "expr"
5063 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5064 * - if beyond end, jump to "end"
5065 * - otherwise get item from list and push it
5066 * STORE var Store item in "var"
5067 * ... body ...
5068 * JUMP top Jump back to repeat
5069 * end: DROP Drop the result of "expr"
5070 *
5071 */
5072 static char_u *
5073compile_for(char_u *arg, cctx_T *cctx)
5074{
5075 char_u *p;
5076 size_t varlen;
5077 garray_T *instr = &cctx->ctx_instr;
5078 garray_T *stack = &cctx->ctx_type_stack;
5079 scope_T *scope;
5080 int loop_idx; // index of loop iteration variable
5081 int var_idx; // index of "var"
5082 type_T *vartype;
5083
5084 // TODO: list of variables: "for [key, value] in dict"
5085 // parse "var"
5086 for (p = arg; eval_isnamec1(*p); ++p)
5087 ;
5088 varlen = p - arg;
5089 var_idx = lookup_local(arg, varlen, cctx);
5090 if (var_idx >= 0)
5091 {
5092 semsg(_("E1023: variable already defined: %s"), arg);
5093 return NULL;
5094 }
5095
5096 // consume "in"
5097 p = skipwhite(p);
5098 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5099 {
5100 emsg(_(e_missing_in));
5101 return NULL;
5102 }
5103 p = skipwhite(p + 2);
5104
5105
5106 scope = new_scope(cctx, FOR_SCOPE);
5107 if (scope == NULL)
5108 return NULL;
5109
5110 // Reserve a variable to store the loop iteration counter.
5111 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5112 if (loop_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005113 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005114 // only happens when out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005115 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118
5119 // Reserve a variable to store "var"
5120 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5121 if (var_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005122 {
5123 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126
5127 generate_STORENR(cctx, loop_idx, -1);
5128
5129 // compile "expr", it remains on the stack until "endfor"
5130 arg = p;
5131 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005132 {
5133 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005136
5137 // now we know the type of "var"
5138 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5139 if (vartype->tt_type != VAR_LIST)
5140 {
5141 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005142 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005143 return NULL;
5144 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005145 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 {
5147 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
5148
5149 lvar->lv_type = vartype->tt_member;
5150 }
5151
5152 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005153 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005154
5155 generate_FOR(cctx, loop_idx);
5156 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
5157
5158 return arg;
5159}
5160
5161/*
5162 * compile "endfor"
5163 */
5164 static char_u *
5165compile_endfor(char_u *arg, cctx_T *cctx)
5166{
5167 garray_T *instr = &cctx->ctx_instr;
5168 scope_T *scope = cctx->ctx_scope;
5169 forscope_T *forscope;
5170 isn_T *isn;
5171
5172 if (scope == NULL || scope->se_type != FOR_SCOPE)
5173 {
5174 emsg(_(e_for));
5175 return NULL;
5176 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005177 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005179 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180
5181 // At end of ":for" scope jump back to the FOR instruction.
5182 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5183
5184 // Fill in the "end" label in the FOR statement so it can jump here
5185 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5186 isn->isn_arg.forloop.for_end = instr->ga_len;
5187
5188 // Fill in the "end" label any BREAK statements
5189 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5190
5191 // Below the ":for" scope drop the "expr" list from the stack.
5192 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5193 return NULL;
5194
5195 vim_free(scope);
5196
5197 return arg;
5198}
5199
5200/*
5201 * compile "while expr"
5202 *
5203 * Produces instructions:
5204 * top: EVAL expr Push result of "expr"
5205 * JUMP_IF_FALSE end jump if false
5206 * ... body ...
5207 * JUMP top Jump back to repeat
5208 * end:
5209 *
5210 */
5211 static char_u *
5212compile_while(char_u *arg, cctx_T *cctx)
5213{
5214 char_u *p = arg;
5215 garray_T *instr = &cctx->ctx_instr;
5216 scope_T *scope;
5217
5218 scope = new_scope(cctx, WHILE_SCOPE);
5219 if (scope == NULL)
5220 return NULL;
5221
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005222 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223
5224 // compile "expr"
5225 if (compile_expr1(&p, cctx) == FAIL)
5226 return NULL;
5227
5228 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005229 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230 JUMP_IF_FALSE, cctx) == FAIL)
5231 return FAIL;
5232
5233 return p;
5234}
5235
5236/*
5237 * compile "endwhile"
5238 */
5239 static char_u *
5240compile_endwhile(char_u *arg, cctx_T *cctx)
5241{
5242 scope_T *scope = cctx->ctx_scope;
5243
5244 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5245 {
5246 emsg(_(e_while));
5247 return NULL;
5248 }
5249 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005250 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251
5252 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005253 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254
5255 // Fill in the "end" label in the WHILE statement so it can jump here.
5256 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005257 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258
5259 vim_free(scope);
5260
5261 return arg;
5262}
5263
5264/*
5265 * compile "continue"
5266 */
5267 static char_u *
5268compile_continue(char_u *arg, cctx_T *cctx)
5269{
5270 scope_T *scope = cctx->ctx_scope;
5271
5272 for (;;)
5273 {
5274 if (scope == NULL)
5275 {
5276 emsg(_(e_continue));
5277 return NULL;
5278 }
5279 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5280 break;
5281 scope = scope->se_outer;
5282 }
5283
5284 // Jump back to the FOR or WHILE instruction.
5285 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005286 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5287 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288 return arg;
5289}
5290
5291/*
5292 * compile "break"
5293 */
5294 static char_u *
5295compile_break(char_u *arg, cctx_T *cctx)
5296{
5297 scope_T *scope = cctx->ctx_scope;
5298 endlabel_T **el;
5299
5300 for (;;)
5301 {
5302 if (scope == NULL)
5303 {
5304 emsg(_(e_break));
5305 return NULL;
5306 }
5307 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5308 break;
5309 scope = scope->se_outer;
5310 }
5311
5312 // Jump to the end of the FOR or WHILE loop.
5313 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005314 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005316 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5318 return FAIL;
5319
5320 return arg;
5321}
5322
5323/*
5324 * compile "{" start of block
5325 */
5326 static char_u *
5327compile_block(char_u *arg, cctx_T *cctx)
5328{
5329 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5330 return NULL;
5331 return skipwhite(arg + 1);
5332}
5333
5334/*
5335 * compile end of block: drop one scope
5336 */
5337 static void
5338compile_endblock(cctx_T *cctx)
5339{
5340 scope_T *scope = cctx->ctx_scope;
5341
5342 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005343 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005344 vim_free(scope);
5345}
5346
5347/*
5348 * compile "try"
5349 * Creates a new scope for the try-endtry, pointing to the first catch and
5350 * finally.
5351 * Creates another scope for the "try" block itself.
5352 * TRY instruction sets up exception handling at runtime.
5353 *
5354 * "try"
5355 * TRY -> catch1, -> finally push trystack entry
5356 * ... try block
5357 * "throw {exception}"
5358 * EVAL {exception}
5359 * THROW create exception
5360 * ... try block
5361 * " catch {expr}"
5362 * JUMP -> finally
5363 * catch1: PUSH exeception
5364 * EVAL {expr}
5365 * MATCH
5366 * JUMP nomatch -> catch2
5367 * CATCH remove exception
5368 * ... catch block
5369 * " catch"
5370 * JUMP -> finally
5371 * catch2: CATCH remove exception
5372 * ... catch block
5373 * " finally"
5374 * finally:
5375 * ... finally block
5376 * " endtry"
5377 * ENDTRY pop trystack entry, may rethrow
5378 */
5379 static char_u *
5380compile_try(char_u *arg, cctx_T *cctx)
5381{
5382 garray_T *instr = &cctx->ctx_instr;
5383 scope_T *try_scope;
5384 scope_T *scope;
5385
5386 // scope that holds the jumps that go to catch/finally/endtry
5387 try_scope = new_scope(cctx, TRY_SCOPE);
5388 if (try_scope == NULL)
5389 return NULL;
5390
5391 // "catch" is set when the first ":catch" is found.
5392 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005393 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394 if (generate_instr(cctx, ISN_TRY) == NULL)
5395 return NULL;
5396
5397 // scope for the try block itself
5398 scope = new_scope(cctx, BLOCK_SCOPE);
5399 if (scope == NULL)
5400 return NULL;
5401
5402 return arg;
5403}
5404
5405/*
5406 * compile "catch {expr}"
5407 */
5408 static char_u *
5409compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5410{
5411 scope_T *scope = cctx->ctx_scope;
5412 garray_T *instr = &cctx->ctx_instr;
5413 char_u *p;
5414 isn_T *isn;
5415
5416 // end block scope from :try or :catch
5417 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5418 compile_endblock(cctx);
5419 scope = cctx->ctx_scope;
5420
5421 // Error if not in a :try scope
5422 if (scope == NULL || scope->se_type != TRY_SCOPE)
5423 {
5424 emsg(_(e_catch));
5425 return NULL;
5426 }
5427
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005428 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429 {
5430 emsg(_("E1033: catch unreachable after catch-all"));
5431 return NULL;
5432 }
5433
5434 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005435 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005436 JUMP_ALWAYS, cctx) == FAIL)
5437 return NULL;
5438
5439 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005440 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005441 if (isn->isn_arg.try.try_catch == 0)
5442 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005443 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005444 {
5445 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005446 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447 isn->isn_arg.jump.jump_where = instr->ga_len;
5448 }
5449
5450 p = skipwhite(arg);
5451 if (ends_excmd(*p))
5452 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005453 scope->se_u.se_try.ts_caught_all = TRUE;
5454 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455 }
5456 else
5457 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005458 char_u *end;
5459 char_u *pat;
5460 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005461 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005462 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005464 // Push v:exception, push {expr} and MATCH
5465 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5466
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005467 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005468 if (*end != *p)
5469 {
5470 semsg(_("E1067: Separator mismatch: %s"), p);
5471 vim_free(tofree);
5472 return FAIL;
5473 }
5474 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005475 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005476 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005477 len = (int)(end - tofree);
5478 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005479 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005480 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005481 if (pat == NULL)
5482 return FAIL;
5483 if (generate_PUSHS(cctx, pat) == FAIL)
5484 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005486 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5487 return NULL;
5488
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005489 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5491 return NULL;
5492 }
5493
5494 if (generate_instr(cctx, ISN_CATCH) == NULL)
5495 return NULL;
5496
5497 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5498 return NULL;
5499 return p;
5500}
5501
5502 static char_u *
5503compile_finally(char_u *arg, cctx_T *cctx)
5504{
5505 scope_T *scope = cctx->ctx_scope;
5506 garray_T *instr = &cctx->ctx_instr;
5507 isn_T *isn;
5508
5509 // end block scope from :try or :catch
5510 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5511 compile_endblock(cctx);
5512 scope = cctx->ctx_scope;
5513
5514 // Error if not in a :try scope
5515 if (scope == NULL || scope->se_type != TRY_SCOPE)
5516 {
5517 emsg(_(e_finally));
5518 return NULL;
5519 }
5520
5521 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005522 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005523 if (isn->isn_arg.try.try_finally != 0)
5524 {
5525 emsg(_(e_finally_dup));
5526 return NULL;
5527 }
5528
5529 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005530 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531
Bram Moolenaar585fea72020-04-02 22:33:21 +02005532 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005533 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005534 {
5535 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005536 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005537 isn->isn_arg.jump.jump_where = instr->ga_len;
5538 }
5539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005540 // TODO: set index in ts_finally_label jumps
5541
5542 return arg;
5543}
5544
5545 static char_u *
5546compile_endtry(char_u *arg, cctx_T *cctx)
5547{
5548 scope_T *scope = cctx->ctx_scope;
5549 garray_T *instr = &cctx->ctx_instr;
5550 isn_T *isn;
5551
5552 // end block scope from :catch or :finally
5553 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5554 compile_endblock(cctx);
5555 scope = cctx->ctx_scope;
5556
5557 // Error if not in a :try scope
5558 if (scope == NULL || scope->se_type != TRY_SCOPE)
5559 {
5560 if (scope == NULL)
5561 emsg(_(e_no_endtry));
5562 else if (scope->se_type == WHILE_SCOPE)
5563 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005564 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565 emsg(_(e_endfor));
5566 else
5567 emsg(_(e_endif));
5568 return NULL;
5569 }
5570
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005571 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005572 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5573 {
5574 emsg(_("E1032: missing :catch or :finally"));
5575 return NULL;
5576 }
5577
5578 // Fill in the "end" label in jumps at the end of the blocks, if not done
5579 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005580 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581
5582 // End :catch or :finally scope: set value in ISN_TRY instruction
5583 if (isn->isn_arg.try.try_finally == 0)
5584 isn->isn_arg.try.try_finally = instr->ga_len;
5585 compile_endblock(cctx);
5586
5587 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5588 return NULL;
5589 return arg;
5590}
5591
5592/*
5593 * compile "throw {expr}"
5594 */
5595 static char_u *
5596compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5597{
5598 char_u *p = skipwhite(arg);
5599
5600 if (ends_excmd(*p))
5601 {
5602 emsg(_(e_argreq));
5603 return NULL;
5604 }
5605 if (compile_expr1(&p, cctx) == FAIL)
5606 return NULL;
5607 if (may_generate_2STRING(-1, cctx) == FAIL)
5608 return NULL;
5609 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5610 return NULL;
5611
5612 return p;
5613}
5614
5615/*
5616 * compile "echo expr"
5617 */
5618 static char_u *
5619compile_echo(char_u *arg, int with_white, cctx_T *cctx)
5620{
5621 char_u *p = arg;
5622 int count = 0;
5623
Bram Moolenaarad39c092020-02-26 18:23:43 +01005624 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625 {
5626 if (compile_expr1(&p, cctx) == FAIL)
5627 return NULL;
5628 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005629 p = skipwhite(p);
5630 if (ends_excmd(*p))
5631 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005632 }
5633
5634 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01005635 return p;
5636}
5637
5638/*
5639 * compile "execute expr"
5640 */
5641 static char_u *
5642compile_execute(char_u *arg, cctx_T *cctx)
5643{
5644 char_u *p = arg;
5645 int count = 0;
5646
5647 for (;;)
5648 {
5649 if (compile_expr1(&p, cctx) == FAIL)
5650 return NULL;
5651 ++count;
5652 p = skipwhite(p);
5653 if (ends_excmd(*p))
5654 break;
5655 }
5656
5657 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658
5659 return p;
5660}
5661
5662/*
5663 * After ex_function() has collected all the function lines: parse and compile
5664 * the lines into instructions.
5665 * Adds the function to "def_functions".
5666 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5667 * return statement (used for lambda).
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005668 * This can be used recursively through compile_lambda(), which may reallocate
5669 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005670 */
5671 void
5672compile_def_function(ufunc_T *ufunc, int set_return_type)
5673{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674 char_u *line = NULL;
5675 char_u *p;
5676 exarg_T ea;
5677 char *errormsg = NULL; // error message
5678 int had_return = FALSE;
5679 cctx_T cctx;
5680 garray_T *instr;
5681 int called_emsg_before = called_emsg;
5682 int ret = FAIL;
5683 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005684 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005686 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005687 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005688
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005689 if (ufunc->uf_dfunc_idx >= 0)
5690 {
5691 // Redefining a function that was compiled before.
5692 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5693
5694 // Free old instructions.
5695 delete_def_function_contents(dfunc);
5696 }
5697 else
5698 {
5699 // Add the function to "def_functions".
5700 if (ga_grow(&def_functions, 1) == FAIL)
5701 return;
5702 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02005703 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005704 dfunc->df_idx = def_functions.ga_len;
5705 ufunc->uf_dfunc_idx = dfunc->df_idx;
5706 dfunc->df_ufunc = ufunc;
5707 ++def_functions.ga_len;
5708 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005709 }
5710
Bram Moolenaara80faa82020-04-12 19:37:17 +02005711 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005712 cctx.ctx_ufunc = ufunc;
5713 cctx.ctx_lnum = -1;
5714 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5715 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5716 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5717 cctx.ctx_type_list = &ufunc->uf_type_list;
5718 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5719 instr = &cctx.ctx_instr;
5720
5721 // Most modern script version.
5722 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5723
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005724 if (ufunc->uf_def_args.ga_len > 0)
5725 {
5726 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005727 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005728 int i;
5729 char_u *arg;
5730 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5731
5732 // Produce instructions for the default values of optional arguments.
5733 // Store the instruction index in uf_def_arg_idx[] so that we know
5734 // where to start when the function is called, depending on the number
5735 // of arguments.
5736 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5737 if (ufunc->uf_def_arg_idx == NULL)
5738 goto erret;
5739 for (i = 0; i < count; ++i)
5740 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005741 garray_T *stack = &cctx.ctx_type_stack;
5742 type_T *val_type;
5743 int arg_idx = first_def_arg + i;
5744
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005745 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5746 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005747 if (compile_expr1(&arg, &cctx) == FAIL)
5748 goto erret;
5749
5750 // If no type specified use the type of the default value.
5751 // Otherwise check that the default value type matches the
5752 // specified type.
5753 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5754 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
5755 ufunc->uf_arg_types[arg_idx] = val_type;
5756 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
5757 == FAIL)
5758 {
5759 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
5760 arg_idx + 1);
5761 goto erret;
5762 }
5763
5764 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005765 goto erret;
5766 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005767 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5768 }
5769
5770 /*
5771 * Loop over all the lines of the function and generate instructions.
5772 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005773 for (;;)
5774 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005775 int is_ex_command;
5776
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005777 // Bail out on the first error to avoid a flood of errors and report
5778 // the right line number when inside try/catch.
5779 if (emsg_before != called_emsg)
5780 goto erret;
5781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005782 if (line != NULL && *line == '|')
5783 // the line continues after a '|'
5784 ++line;
5785 else if (line != NULL && *line != NUL)
5786 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005787 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005788 goto erret;
5789 }
5790 else
5791 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02005792 line = next_line_from_context(&cctx);
5793 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005794 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005795 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005796 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005797
5798 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02005799 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005800 ea.cmdlinep = &line;
5801 ea.cmd = skipwhite(line);
5802
5803 // "}" ends a block scope
5804 if (*ea.cmd == '}')
5805 {
5806 scopetype_T stype = cctx.ctx_scope == NULL
5807 ? NO_SCOPE : cctx.ctx_scope->se_type;
5808
5809 if (stype == BLOCK_SCOPE)
5810 {
5811 compile_endblock(&cctx);
5812 line = ea.cmd;
5813 }
5814 else
5815 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005816 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005817 goto erret;
5818 }
5819 if (line != NULL)
5820 line = skipwhite(ea.cmd + 1);
5821 continue;
5822 }
5823
5824 // "{" starts a block scope
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01005825 // "{'a': 1}->func() is something else
5826 if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005827 {
5828 line = compile_block(ea.cmd, &cctx);
5829 continue;
5830 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005831 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005832
5833 /*
5834 * COMMAND MODIFIERS
5835 */
5836 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5837 {
5838 if (errormsg != NULL)
5839 goto erret;
5840 // empty line or comment
5841 line = (char_u *)"";
5842 continue;
5843 }
5844
5845 // Skip ":call" to get to the function name.
5846 if (checkforcmd(&ea.cmd, "call", 3))
5847 ea.cmd = skipwhite(ea.cmd);
5848
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005849 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005851 // Assuming the command starts with a variable or function name,
5852 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5853 // val".
5854 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5855 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005856 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005857 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005858 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005859 int oplen;
5860 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005862 oplen = assignment_len(skipwhite(p), &heredoc);
5863 if (oplen > 0)
5864 {
5865 // Recognize an assignment if we recognize the variable
5866 // name:
5867 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005868 // "local = expr" where "local" is a local var.
5869 // "script = expr" where "script" is a script-local var.
5870 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005871 // "&opt = expr"
5872 // "$ENV = expr"
5873 // "@r = expr"
5874 if (*ea.cmd == '&'
5875 || *ea.cmd == '$'
5876 || *ea.cmd == '@'
5877 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5878 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5879 || lookup_script(ea.cmd, p - ea.cmd) == OK
5880 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5881 {
5882 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5883 if (line == NULL)
5884 goto erret;
5885 continue;
5886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005887 }
5888 }
5889 }
5890
5891 /*
5892 * COMMAND after range
5893 */
5894 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005895 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5896 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897
5898 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5899 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005900 if (cctx.ctx_skip == TRUE)
5901 {
5902 line += STRLEN(line);
5903 continue;
5904 }
5905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005906 // Expression or function call.
5907 if (ea.cmdidx == CMD_eval)
5908 {
5909 p = ea.cmd;
5910 if (compile_expr1(&p, &cctx) == FAIL)
5911 goto erret;
5912
5913 // drop the return value
5914 generate_instr_drop(&cctx, ISN_DROP, 1);
5915 line = p;
5916 continue;
5917 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02005918 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005919 iemsg("Command from find_ex_command() not handled");
5920 goto erret;
5921 }
5922
5923 p = skipwhite(p);
5924
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005925 if (cctx.ctx_skip == TRUE
5926 && ea.cmdidx != CMD_elseif
5927 && ea.cmdidx != CMD_else
5928 && ea.cmdidx != CMD_endif)
5929 {
5930 line += STRLEN(line);
5931 continue;
5932 }
5933
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005934 switch (ea.cmdidx)
5935 {
5936 case CMD_def:
5937 case CMD_function:
5938 // TODO: Nested function
5939 emsg("Nested function not implemented yet");
5940 goto erret;
5941
5942 case CMD_return:
5943 line = compile_return(p, set_return_type, &cctx);
5944 had_return = TRUE;
5945 break;
5946
5947 case CMD_let:
5948 case CMD_const:
5949 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5950 break;
5951
5952 case CMD_import:
5953 line = compile_import(p, &cctx);
5954 break;
5955
5956 case CMD_if:
5957 line = compile_if(p, &cctx);
5958 break;
5959 case CMD_elseif:
5960 line = compile_elseif(p, &cctx);
5961 break;
5962 case CMD_else:
5963 line = compile_else(p, &cctx);
5964 break;
5965 case CMD_endif:
5966 line = compile_endif(p, &cctx);
5967 break;
5968
5969 case CMD_while:
5970 line = compile_while(p, &cctx);
5971 break;
5972 case CMD_endwhile:
5973 line = compile_endwhile(p, &cctx);
5974 break;
5975
5976 case CMD_for:
5977 line = compile_for(p, &cctx);
5978 break;
5979 case CMD_endfor:
5980 line = compile_endfor(p, &cctx);
5981 break;
5982 case CMD_continue:
5983 line = compile_continue(p, &cctx);
5984 break;
5985 case CMD_break:
5986 line = compile_break(p, &cctx);
5987 break;
5988
5989 case CMD_try:
5990 line = compile_try(p, &cctx);
5991 break;
5992 case CMD_catch:
5993 line = compile_catch(p, &cctx);
5994 break;
5995 case CMD_finally:
5996 line = compile_finally(p, &cctx);
5997 break;
5998 case CMD_endtry:
5999 line = compile_endtry(p, &cctx);
6000 break;
6001 case CMD_throw:
6002 line = compile_throw(p, &cctx);
6003 break;
6004
6005 case CMD_echo:
6006 line = compile_echo(p, TRUE, &cctx);
6007 break;
6008 case CMD_echon:
6009 line = compile_echo(p, FALSE, &cctx);
6010 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006011 case CMD_execute:
6012 line = compile_execute(p, &cctx);
6013 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006014
6015 default:
6016 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01006017 // TODO:
6018 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01006019 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006020 generate_EXEC(&cctx, line);
6021 line = (char_u *)"";
6022 break;
6023 }
6024 if (line == NULL)
6025 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006026 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006027
6028 if (cctx.ctx_type_stack.ga_len < 0)
6029 {
6030 iemsg("Type stack underflow");
6031 goto erret;
6032 }
6033 }
6034
6035 if (cctx.ctx_scope != NULL)
6036 {
6037 if (cctx.ctx_scope->se_type == IF_SCOPE)
6038 emsg(_(e_endif));
6039 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6040 emsg(_(e_endwhile));
6041 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6042 emsg(_(e_endfor));
6043 else
6044 emsg(_("E1026: Missing }"));
6045 goto erret;
6046 }
6047
6048 if (!had_return)
6049 {
6050 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6051 {
6052 emsg(_("E1027: Missing return statement"));
6053 goto erret;
6054 }
6055
6056 // Return zero if there is no return at the end.
6057 generate_PUSHNR(&cctx, 0);
6058 generate_instr(&cctx, ISN_RETURN);
6059 }
6060
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006061 {
6062 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6063 + ufunc->uf_dfunc_idx;
6064 dfunc->df_deleted = FALSE;
6065 dfunc->df_instr = instr->ga_data;
6066 dfunc->df_instr_count = instr->ga_len;
6067 dfunc->df_varcount = cctx.ctx_max_local;
6068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006069
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006070 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006071 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006072 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006073
6074 // Create a type for the function, with the return type and any
6075 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006076 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6077 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006078 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006079 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006080 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6081 argcount, &ufunc->uf_type_list);
6082 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006083 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006084 argcount + varargs,
6085 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006086 {
6087 ret = FAIL;
6088 goto erret;
6089 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006090 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6091 ufunc->uf_func_type->tt_min_argcount =
6092 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006093 if (ufunc->uf_arg_types == NULL)
6094 {
6095 int i;
6096
6097 // lambda does not have argument types.
6098 for (i = 0; i < argcount; ++i)
6099 ufunc->uf_func_type->tt_args[i] = &t_any;
6100 }
6101 else
6102 mch_memmove(ufunc->uf_func_type->tt_args,
6103 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006104 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006105 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006106 ufunc->uf_func_type->tt_args[argcount] =
6107 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006108 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6109 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006110 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006111 else
6112 // No arguments, can use a predefined type.
6113 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6114 argcount, &ufunc->uf_type_list);
6115
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006116 }
6117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118 ret = OK;
6119
6120erret:
6121 if (ret == FAIL)
6122 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006123 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006124 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6125 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006126
6127 for (idx = 0; idx < instr->ga_len; ++idx)
6128 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006131 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006132 if (!dfunc->df_deleted)
6133 --def_functions.ga_len;
6134
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006135 while (cctx.ctx_scope != NULL)
6136 drop_scope(&cctx);
6137
Bram Moolenaar20431c92020-03-20 18:39:46 +01006138 // Don't execute this function body.
6139 ga_clear_strings(&ufunc->uf_lines);
6140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006141 if (errormsg != NULL)
6142 emsg(errormsg);
6143 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006144 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006145 }
6146
6147 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006148 free_imported(&cctx);
6149 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006150 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151}
6152
6153/*
6154 * Delete an instruction, free what it contains.
6155 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006156 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006157delete_instr(isn_T *isn)
6158{
6159 switch (isn->isn_type)
6160 {
6161 case ISN_EXEC:
6162 case ISN_LOADENV:
6163 case ISN_LOADG:
6164 case ISN_LOADOPT:
6165 case ISN_MEMBER:
6166 case ISN_PUSHEXC:
6167 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006168 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006169 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006170 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006171 vim_free(isn->isn_arg.string);
6172 break;
6173
6174 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006175 case ISN_STORES:
6176 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177 break;
6178
6179 case ISN_STOREOPT:
6180 vim_free(isn->isn_arg.storeopt.so_name);
6181 break;
6182
6183 case ISN_PUSHBLOB: // push blob isn_arg.blob
6184 blob_unref(isn->isn_arg.blob);
6185 break;
6186
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006187 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006188#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006189 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006190#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006191 break;
6192
6193 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006194#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006195 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006196#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006197 break;
6198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006199 case ISN_UCALL:
6200 vim_free(isn->isn_arg.ufunc.cuf_name);
6201 break;
6202
6203 case ISN_2BOOL:
6204 case ISN_2STRING:
6205 case ISN_ADDBLOB:
6206 case ISN_ADDLIST:
6207 case ISN_BCALL:
6208 case ISN_CATCH:
6209 case ISN_CHECKNR:
6210 case ISN_CHECKTYPE:
6211 case ISN_COMPAREANY:
6212 case ISN_COMPAREBLOB:
6213 case ISN_COMPAREBOOL:
6214 case ISN_COMPAREDICT:
6215 case ISN_COMPAREFLOAT:
6216 case ISN_COMPAREFUNC:
6217 case ISN_COMPARELIST:
6218 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219 case ISN_COMPARESPECIAL:
6220 case ISN_COMPARESTRING:
6221 case ISN_CONCAT:
6222 case ISN_DCALL:
6223 case ISN_DROP:
6224 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006225 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226 case ISN_ENDTRY:
6227 case ISN_FOR:
6228 case ISN_FUNCREF:
6229 case ISN_INDEX:
6230 case ISN_JUMP:
6231 case ISN_LOAD:
6232 case ISN_LOADSCRIPT:
6233 case ISN_LOADREG:
6234 case ISN_LOADV:
6235 case ISN_NEGATENR:
6236 case ISN_NEWDICT:
6237 case ISN_NEWLIST:
6238 case ISN_OPNR:
6239 case ISN_OPFLOAT:
6240 case ISN_OPANY:
6241 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006242 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243 case ISN_PUSHF:
6244 case ISN_PUSHNR:
6245 case ISN_PUSHBOOL:
6246 case ISN_PUSHSPEC:
6247 case ISN_RETURN:
6248 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006249 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006250 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006251 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006252 case ISN_STORESCRIPT:
6253 case ISN_THROW:
6254 case ISN_TRY:
6255 // nothing allocated
6256 break;
6257 }
6258}
6259
6260/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006261 * Free all instructions for "dfunc".
6262 */
6263 static void
6264delete_def_function_contents(dfunc_T *dfunc)
6265{
6266 int idx;
6267
6268 ga_clear(&dfunc->df_def_args_isn);
6269
6270 if (dfunc->df_instr != NULL)
6271 {
6272 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6273 delete_instr(dfunc->df_instr + idx);
6274 VIM_CLEAR(dfunc->df_instr);
6275 }
6276
6277 dfunc->df_deleted = TRUE;
6278}
6279
6280/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281 * When a user function is deleted, delete any associated def function.
6282 */
6283 void
6284delete_def_function(ufunc_T *ufunc)
6285{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 if (ufunc->uf_dfunc_idx >= 0)
6287 {
6288 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6289 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290
Bram Moolenaar20431c92020-03-20 18:39:46 +01006291 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 }
6293}
6294
6295#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006296/*
6297 * Free all functions defined with ":def".
6298 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299 void
6300free_def_functions(void)
6301{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006302 int idx;
6303
6304 for (idx = 0; idx < def_functions.ga_len; ++idx)
6305 {
6306 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6307
6308 delete_def_function_contents(dfunc);
6309 }
6310
6311 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312}
6313#endif
6314
6315
6316#endif // FEAT_EVAL