blob: c99d39602ed8bd091e509c55f980df41aeb8f8cf [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
Bram Moolenaar7a092242020-04-16 22:10:49 +0200111 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112 garray_T ctx_instr; // generated instructions
113
114 garray_T ctx_locals; // currently visible local variables
115 int ctx_max_local; // maximum number of locals at one time
116
117 garray_T ctx_imports; // imported items
118
Bram Moolenaara259d8d2020-01-31 20:10:50 +0100119 int ctx_skip; // when TRUE skip commands, when FALSE skip
120 // commands after "else"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 scope_T *ctx_scope; // current scope, NULL at toplevel
122
123 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200124 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125};
126
127static char e_var_notfound[] = N_("E1001: variable not found: %s");
128static char e_syntax_at[] = N_("E1002: Syntax error at %s");
129
130static int compile_expr1(char_u **arg, cctx_T *cctx);
131static int compile_expr2(char_u **arg, cctx_T *cctx);
132static int compile_expr3(char_u **arg, cctx_T *cctx);
Bram Moolenaar20431c92020-03-20 18:39:46 +0100133static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200134static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
135static int check_type(type_T *expected, type_T *actual, int give_msg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100136
137/*
138 * Lookup variable "name" in the local scope and return the index.
139 */
140 static int
141lookup_local(char_u *name, size_t len, cctx_T *cctx)
142{
143 int idx;
144
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100145 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146 return -1;
147 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
148 {
149 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
150
151 if (STRNCMP(name, lvar->lv_name, len) == 0
152 && STRLEN(lvar->lv_name) == len)
153 return idx;
154 }
155 return -1;
156}
157
158/*
159 * Lookup an argument in the current function.
160 * Returns the argument index or -1 if not found.
161 */
162 static int
163lookup_arg(char_u *name, size_t len, cctx_T *cctx)
164{
165 int idx;
166
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100167 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 return -1;
169 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
170 {
171 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
172
173 if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
174 return idx;
175 }
176 return -1;
177}
178
179/*
180 * Lookup a vararg argument in the current function.
181 * Returns TRUE if there is a match.
182 */
183 static int
184lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
185{
186 char_u *va_name = cctx->ctx_ufunc->uf_va_name;
187
188 return len > 0 && va_name != NULL
189 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
190}
191
192/*
193 * Lookup a variable in the current script.
194 * Returns OK or FAIL.
195 */
196 static int
197lookup_script(char_u *name, size_t len)
198{
199 int cc;
200 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
201 dictitem_T *di;
202
203 cc = name[len];
204 name[len] = NUL;
205 di = find_var_in_ht(ht, 0, name, TRUE);
206 name[len] = cc;
207 return di == NULL ? FAIL: OK;
208}
209
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100210/*
211 * Check if "p[len]" is already defined, either in script "import_sid" or in
212 * compilation context "cctx".
213 * Return FAIL and give an error if it defined.
214 */
215 int
216check_defined(char_u *p, int len, cctx_T *cctx)
217{
218 if (lookup_script(p, len) == OK
219 || (cctx != NULL
220 && (lookup_local(p, len, cctx) >= 0
221 || find_imported(p, len, cctx) != NULL)))
222 {
223 semsg("E1073: imported name already defined: %s", p);
224 return FAIL;
225 }
226 return OK;
227}
228
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200229/*
230 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
231 * be freed later.
232 */
233 static type_T *
234alloc_type(garray_T *type_gap)
235{
236 type_T *type;
237
238 if (ga_grow(type_gap, 1) == FAIL)
239 return NULL;
240 type = ALLOC_CLEAR_ONE(type_T);
241 if (type != NULL)
242 {
243 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
244 ++type_gap->ga_len;
245 }
246 return type;
247}
248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200250get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251{
252 type_T *type;
253
254 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200255 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200257 if (member_type->tt_type == VAR_VOID
258 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100259 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100260 if (member_type->tt_type == VAR_BOOL)
261 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 if (member_type->tt_type == VAR_NUMBER)
263 return &t_list_number;
264 if (member_type->tt_type == VAR_STRING)
265 return &t_list_string;
266
267 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200268 type = alloc_type(type_gap);
269 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100270 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 type->tt_type = VAR_LIST;
272 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200273 type->tt_argcount = 0;
274 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275 return type;
276}
277
278 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200279get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280{
281 type_T *type;
282
283 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200284 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200286 if (member_type->tt_type == VAR_VOID
287 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100288 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100289 if (member_type->tt_type == VAR_BOOL)
290 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100291 if (member_type->tt_type == VAR_NUMBER)
292 return &t_dict_number;
293 if (member_type->tt_type == VAR_STRING)
294 return &t_dict_string;
295
296 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200297 type = alloc_type(type_gap);
298 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100299 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100300 type->tt_type = VAR_DICT;
301 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200302 type->tt_argcount = 0;
303 type->tt_args = NULL;
304 return type;
305}
306
307/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200308 * Allocate a new type for a function.
309 */
310 static type_T *
311alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
312{
313 type_T *type = alloc_type(type_gap);
314
315 if (type == NULL)
316 return &t_any;
317 type->tt_type = VAR_FUNC;
318 type->tt_member = ret_type;
319 type->tt_argcount = argcount;
320 type->tt_args = NULL;
321 return type;
322}
323
324/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200325 * Get a function type, based on the return type "ret_type".
326 * If "argcount" is -1 or 0 a predefined type can be used.
327 * If "argcount" > 0 always create a new type, so that arguments can be added.
328 */
329 static type_T *
330get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
331{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200332 // recognize commonly used types
333 if (argcount <= 0)
334 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200335 if (ret_type == &t_unknown)
336 {
337 // (argcount == 0) is not possible
338 return &t_func_unknown;
339 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200340 if (ret_type == &t_void)
341 {
342 if (argcount == 0)
343 return &t_func_0_void;
344 else
345 return &t_func_void;
346 }
347 if (ret_type == &t_any)
348 {
349 if (argcount == 0)
350 return &t_func_0_any;
351 else
352 return &t_func_any;
353 }
354 if (ret_type == &t_number)
355 {
356 if (argcount == 0)
357 return &t_func_0_number;
358 else
359 return &t_func_number;
360 }
361 if (ret_type == &t_string)
362 {
363 if (argcount == 0)
364 return &t_func_0_string;
365 else
366 return &t_func_string;
367 }
368 }
369
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200370 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371}
372
Bram Moolenaara8c17702020-04-01 21:17:24 +0200373/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200374 * For a function type, reserve space for "argcount" argument types (including
375 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200376 */
377 static int
378func_type_add_arg_types(
379 type_T *functype,
380 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200381 garray_T *type_gap)
382{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200383 // To make it easy to free the space needed for the argument types, add the
384 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200385 if (ga_grow(type_gap, 1) == FAIL)
386 return FAIL;
387 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
388 if (functype->tt_args == NULL)
389 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200390 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
391 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200392 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200393 return OK;
394}
395
396/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200397 * Return the type_T for a typval. Only for primitive types.
398 */
399 static type_T *
400typval2type(typval_T *tv)
401{
402 if (tv->v_type == VAR_NUMBER)
403 return &t_number;
404 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200405 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200406 if (tv->v_type == VAR_STRING)
407 return &t_string;
408 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
409 return &t_list_string;
410 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
411 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200412 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200413}
414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100415/////////////////////////////////////////////////////////////////////
416// Following generate_ functions expect the caller to call ga_grow().
417
Bram Moolenaar080457c2020-03-03 21:53:32 +0100418#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
419#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100421/*
422 * Generate an instruction without arguments.
423 * Returns a pointer to the new instruction, NULL if failed.
424 */
425 static isn_T *
426generate_instr(cctx_T *cctx, isntype_T isn_type)
427{
428 garray_T *instr = &cctx->ctx_instr;
429 isn_T *isn;
430
Bram Moolenaar080457c2020-03-03 21:53:32 +0100431 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 if (ga_grow(instr, 1) == FAIL)
433 return NULL;
434 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
435 isn->isn_type = isn_type;
436 isn->isn_lnum = cctx->ctx_lnum + 1;
437 ++instr->ga_len;
438
439 return isn;
440}
441
442/*
443 * Generate an instruction without arguments.
444 * "drop" will be removed from the stack.
445 * Returns a pointer to the new instruction, NULL if failed.
446 */
447 static isn_T *
448generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
449{
450 garray_T *stack = &cctx->ctx_type_stack;
451
Bram Moolenaar080457c2020-03-03 21:53:32 +0100452 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 stack->ga_len -= drop;
454 return generate_instr(cctx, isn_type);
455}
456
457/*
458 * Generate instruction "isn_type" and put "type" on the type stack.
459 */
460 static isn_T *
461generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
462{
463 isn_T *isn;
464 garray_T *stack = &cctx->ctx_type_stack;
465
466 if ((isn = generate_instr(cctx, isn_type)) == NULL)
467 return NULL;
468
469 if (ga_grow(stack, 1) == FAIL)
470 return NULL;
471 ((type_T **)stack->ga_data)[stack->ga_len] = type;
472 ++stack->ga_len;
473
474 return isn;
475}
476
477/*
478 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
479 */
480 static int
481may_generate_2STRING(int offset, cctx_T *cctx)
482{
483 isn_T *isn;
484 garray_T *stack = &cctx->ctx_type_stack;
485 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
486
487 if ((*type)->tt_type == VAR_STRING)
488 return OK;
489 *type = &t_string;
490
491 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
492 return FAIL;
493 isn->isn_arg.number = offset;
494
495 return OK;
496}
497
498 static int
499check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
500{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200501 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200503 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 {
505 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100506 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507 else
508 semsg(_("E1036: %c requires number or float arguments"), *op);
509 return FAIL;
510 }
511 return OK;
512}
513
514/*
515 * Generate an instruction with two arguments. The instruction depends on the
516 * type of the arguments.
517 */
518 static int
519generate_two_op(cctx_T *cctx, char_u *op)
520{
521 garray_T *stack = &cctx->ctx_type_stack;
522 type_T *type1;
523 type_T *type2;
524 vartype_T vartype;
525 isn_T *isn;
526
Bram Moolenaar080457c2020-03-03 21:53:32 +0100527 RETURN_OK_IF_SKIP(cctx);
528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 // Get the known type of the two items on the stack. If they are matching
530 // use a type-specific instruction. Otherwise fall back to runtime type
531 // checking.
532 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
533 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200534 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 if (type1->tt_type == type2->tt_type
536 && (type1->tt_type == VAR_NUMBER
537 || type1->tt_type == VAR_LIST
538#ifdef FEAT_FLOAT
539 || type1->tt_type == VAR_FLOAT
540#endif
541 || type1->tt_type == VAR_BLOB))
542 vartype = type1->tt_type;
543
544 switch (*op)
545 {
546 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200547 && type1->tt_type != VAR_ANY
548 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 && check_number_or_float(
550 type1->tt_type, type2->tt_type, op) == FAIL)
551 return FAIL;
552 isn = generate_instr_drop(cctx,
553 vartype == VAR_NUMBER ? ISN_OPNR
554 : vartype == VAR_LIST ? ISN_ADDLIST
555 : vartype == VAR_BLOB ? ISN_ADDBLOB
556#ifdef FEAT_FLOAT
557 : vartype == VAR_FLOAT ? ISN_OPFLOAT
558#endif
559 : ISN_OPANY, 1);
560 if (isn != NULL)
561 isn->isn_arg.op.op_type = EXPR_ADD;
562 break;
563
564 case '-':
565 case '*':
566 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
567 op) == FAIL)
568 return FAIL;
569 if (vartype == VAR_NUMBER)
570 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
571#ifdef FEAT_FLOAT
572 else if (vartype == VAR_FLOAT)
573 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
574#endif
575 else
576 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
577 if (isn != NULL)
578 isn->isn_arg.op.op_type = *op == '*'
579 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
580 break;
581
Bram Moolenaar4c683752020-04-05 21:38:23 +0200582 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200584 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 && type2->tt_type != VAR_NUMBER))
586 {
587 emsg(_("E1035: % requires number arguments"));
588 return FAIL;
589 }
590 isn = generate_instr_drop(cctx,
591 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
592 if (isn != NULL)
593 isn->isn_arg.op.op_type = EXPR_REM;
594 break;
595 }
596
597 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200598 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 {
600 type_T *type = &t_any;
601
602#ifdef FEAT_FLOAT
603 // float+number and number+float results in float
604 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
605 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
606 type = &t_float;
607#endif
608 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
609 }
610
611 return OK;
612}
613
614/*
615 * Generate an ISN_COMPARE* instruction with a boolean result.
616 */
617 static int
618generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
619{
620 isntype_T isntype = ISN_DROP;
621 isn_T *isn;
622 garray_T *stack = &cctx->ctx_type_stack;
623 vartype_T type1;
624 vartype_T type2;
625
Bram Moolenaar080457c2020-03-03 21:53:32 +0100626 RETURN_OK_IF_SKIP(cctx);
627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100628 // Get the known type of the two items on the stack. If they are matching
629 // use a type-specific instruction. Otherwise fall back to runtime type
630 // checking.
631 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
632 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200633 if (type1 == VAR_UNKNOWN)
634 type1 = VAR_ANY;
635 if (type2 == VAR_UNKNOWN)
636 type2 = VAR_ANY;
637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 if (type1 == type2)
639 {
640 switch (type1)
641 {
642 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
643 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
644 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
645 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
646 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
647 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
648 case VAR_LIST: isntype = ISN_COMPARELIST; break;
649 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
650 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651 default: isntype = ISN_COMPAREANY; break;
652 }
653 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200654 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
656 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
657 isntype = ISN_COMPAREANY;
658
659 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
660 && (isntype == ISN_COMPAREBOOL
661 || isntype == ISN_COMPARESPECIAL
662 || isntype == ISN_COMPARENR
663 || isntype == ISN_COMPAREFLOAT))
664 {
665 semsg(_("E1037: Cannot use \"%s\" with %s"),
666 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
667 return FAIL;
668 }
669 if (isntype == ISN_DROP
670 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
671 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
672 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
673 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
674 && exptype != EXPR_IS && exptype != EXPR_ISNOT
675 && (type1 == VAR_BLOB || type2 == VAR_BLOB
676 || type1 == VAR_LIST || type2 == VAR_LIST))))
677 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100678 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 vartype_name(type1), vartype_name(type2));
680 return FAIL;
681 }
682
683 if ((isn = generate_instr(cctx, isntype)) == NULL)
684 return FAIL;
685 isn->isn_arg.op.op_type = exptype;
686 isn->isn_arg.op.op_ic = ic;
687
688 // takes two arguments, puts one bool back
689 if (stack->ga_len >= 2)
690 {
691 --stack->ga_len;
692 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
693 }
694
695 return OK;
696}
697
698/*
699 * Generate an ISN_2BOOL instruction.
700 */
701 static int
702generate_2BOOL(cctx_T *cctx, int invert)
703{
704 isn_T *isn;
705 garray_T *stack = &cctx->ctx_type_stack;
706
Bram Moolenaar080457c2020-03-03 21:53:32 +0100707 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
709 return FAIL;
710 isn->isn_arg.number = invert;
711
712 // type becomes bool
713 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
714
715 return OK;
716}
717
718 static int
719generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
720{
721 isn_T *isn;
722 garray_T *stack = &cctx->ctx_type_stack;
723
Bram Moolenaar080457c2020-03-03 21:53:32 +0100724 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
726 return FAIL;
727 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
728 isn->isn_arg.type.ct_off = offset;
729
730 // type becomes vartype
731 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
732
733 return OK;
734}
735
736/*
737 * Generate an ISN_PUSHNR instruction.
738 */
739 static int
740generate_PUSHNR(cctx_T *cctx, varnumber_T number)
741{
742 isn_T *isn;
743
Bram Moolenaar080457c2020-03-03 21:53:32 +0100744 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
746 return FAIL;
747 isn->isn_arg.number = number;
748
749 return OK;
750}
751
752/*
753 * Generate an ISN_PUSHBOOL instruction.
754 */
755 static int
756generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
757{
758 isn_T *isn;
759
Bram Moolenaar080457c2020-03-03 21:53:32 +0100760 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
762 return FAIL;
763 isn->isn_arg.number = number;
764
765 return OK;
766}
767
768/*
769 * Generate an ISN_PUSHSPEC instruction.
770 */
771 static int
772generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
773{
774 isn_T *isn;
775
Bram Moolenaar080457c2020-03-03 21:53:32 +0100776 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
778 return FAIL;
779 isn->isn_arg.number = number;
780
781 return OK;
782}
783
784#ifdef FEAT_FLOAT
785/*
786 * Generate an ISN_PUSHF instruction.
787 */
788 static int
789generate_PUSHF(cctx_T *cctx, float_T fnumber)
790{
791 isn_T *isn;
792
Bram Moolenaar080457c2020-03-03 21:53:32 +0100793 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
795 return FAIL;
796 isn->isn_arg.fnumber = fnumber;
797
798 return OK;
799}
800#endif
801
802/*
803 * Generate an ISN_PUSHS instruction.
804 * Consumes "str".
805 */
806 static int
807generate_PUSHS(cctx_T *cctx, char_u *str)
808{
809 isn_T *isn;
810
Bram Moolenaar080457c2020-03-03 21:53:32 +0100811 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
813 return FAIL;
814 isn->isn_arg.string = str;
815
816 return OK;
817}
818
819/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100820 * Generate an ISN_PUSHCHANNEL instruction.
821 * Consumes "channel".
822 */
823 static int
824generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
825{
826 isn_T *isn;
827
Bram Moolenaar080457c2020-03-03 21:53:32 +0100828 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100829 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
830 return FAIL;
831 isn->isn_arg.channel = channel;
832
833 return OK;
834}
835
836/*
837 * Generate an ISN_PUSHJOB instruction.
838 * Consumes "job".
839 */
840 static int
841generate_PUSHJOB(cctx_T *cctx, job_T *job)
842{
843 isn_T *isn;
844
Bram Moolenaar080457c2020-03-03 21:53:32 +0100845 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100846 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100847 return FAIL;
848 isn->isn_arg.job = job;
849
850 return OK;
851}
852
853/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854 * Generate an ISN_PUSHBLOB instruction.
855 * Consumes "blob".
856 */
857 static int
858generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
859{
860 isn_T *isn;
861
Bram Moolenaar080457c2020-03-03 21:53:32 +0100862 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100863 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
864 return FAIL;
865 isn->isn_arg.blob = blob;
866
867 return OK;
868}
869
870/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100871 * Generate an ISN_PUSHFUNC instruction with name "name".
872 * Consumes "name".
873 */
874 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200875generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100876{
877 isn_T *isn;
878
Bram Moolenaar080457c2020-03-03 21:53:32 +0100879 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200880 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100881 return FAIL;
882 isn->isn_arg.string = name;
883
884 return OK;
885}
886
887/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 * Generate an ISN_STORE instruction.
889 */
890 static int
891generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
892{
893 isn_T *isn;
894
Bram Moolenaar080457c2020-03-03 21:53:32 +0100895 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
897 return FAIL;
898 if (name != NULL)
899 isn->isn_arg.string = vim_strsave(name);
900 else
901 isn->isn_arg.number = idx;
902
903 return OK;
904}
905
906/*
907 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
908 */
909 static int
910generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
911{
912 isn_T *isn;
913
Bram Moolenaar080457c2020-03-03 21:53:32 +0100914 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
916 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100917 isn->isn_arg.storenr.stnr_idx = idx;
918 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919
920 return OK;
921}
922
923/*
924 * Generate an ISN_STOREOPT instruction
925 */
926 static int
927generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
928{
929 isn_T *isn;
930
Bram Moolenaar080457c2020-03-03 21:53:32 +0100931 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
933 return FAIL;
934 isn->isn_arg.storeopt.so_name = vim_strsave(name);
935 isn->isn_arg.storeopt.so_flags = opt_flags;
936
937 return OK;
938}
939
940/*
941 * Generate an ISN_LOAD or similar instruction.
942 */
943 static int
944generate_LOAD(
945 cctx_T *cctx,
946 isntype_T isn_type,
947 int idx,
948 char_u *name,
949 type_T *type)
950{
951 isn_T *isn;
952
Bram Moolenaar080457c2020-03-03 21:53:32 +0100953 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
955 return FAIL;
956 if (name != NULL)
957 isn->isn_arg.string = vim_strsave(name);
958 else
959 isn->isn_arg.number = idx;
960
961 return OK;
962}
963
964/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200965 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100966 */
967 static int
968generate_LOADV(
969 cctx_T *cctx,
970 char_u *name,
971 int error)
972{
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200973 int di_flags;
974 int vidx = find_vim_var(name, &di_flags);
975 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100976
Bram Moolenaar080457c2020-03-03 21:53:32 +0100977 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100978 if (vidx < 0)
979 {
980 if (error)
981 semsg(_(e_var_notfound), name);
982 return FAIL;
983 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200984 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100985
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200986 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100987}
988
989/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 * Generate an ISN_LOADS instruction.
991 */
992 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100993generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100995 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100997 int sid,
998 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999{
1000 isn_T *isn;
1001
Bram Moolenaar080457c2020-03-03 21:53:32 +01001002 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001003 if (isn_type == ISN_LOADS)
1004 isn = generate_instr_type(cctx, isn_type, type);
1005 else
1006 isn = generate_instr_drop(cctx, isn_type, 1);
1007 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001009 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1010 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011
1012 return OK;
1013}
1014
1015/*
1016 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1017 */
1018 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001019generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 cctx_T *cctx,
1021 isntype_T isn_type,
1022 int sid,
1023 int idx,
1024 type_T *type)
1025{
1026 isn_T *isn;
1027
Bram Moolenaar080457c2020-03-03 21:53:32 +01001028 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 if (isn_type == ISN_LOADSCRIPT)
1030 isn = generate_instr_type(cctx, isn_type, type);
1031 else
1032 isn = generate_instr_drop(cctx, isn_type, 1);
1033 if (isn == NULL)
1034 return FAIL;
1035 isn->isn_arg.script.script_sid = sid;
1036 isn->isn_arg.script.script_idx = idx;
1037 return OK;
1038}
1039
1040/*
1041 * Generate an ISN_NEWLIST instruction.
1042 */
1043 static int
1044generate_NEWLIST(cctx_T *cctx, int count)
1045{
1046 isn_T *isn;
1047 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048 type_T *type;
1049 type_T *member;
1050
Bram Moolenaar080457c2020-03-03 21:53:32 +01001051 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1053 return FAIL;
1054 isn->isn_arg.number = count;
1055
1056 // drop the value types
1057 stack->ga_len -= count;
1058
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001059 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001060 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061 if (count > 0)
1062 member = ((type_T **)stack->ga_data)[stack->ga_len];
1063 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001064 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001065 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066
1067 // add the list type to the type stack
1068 if (ga_grow(stack, 1) == FAIL)
1069 return FAIL;
1070 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1071 ++stack->ga_len;
1072
1073 return OK;
1074}
1075
1076/*
1077 * Generate an ISN_NEWDICT instruction.
1078 */
1079 static int
1080generate_NEWDICT(cctx_T *cctx, int count)
1081{
1082 isn_T *isn;
1083 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 type_T *type;
1085 type_T *member;
1086
Bram Moolenaar080457c2020-03-03 21:53:32 +01001087 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1089 return FAIL;
1090 isn->isn_arg.number = count;
1091
1092 // drop the key and value types
1093 stack->ga_len -= 2 * count;
1094
Bram Moolenaar436472f2020-02-20 22:54:43 +01001095 // Use the first value type for the list member type. Use "void" for an
1096 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 if (count > 0)
1098 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1099 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001100 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001101 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102
1103 // add the dict type to the type stack
1104 if (ga_grow(stack, 1) == FAIL)
1105 return FAIL;
1106 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1107 ++stack->ga_len;
1108
1109 return OK;
1110}
1111
1112/*
1113 * Generate an ISN_FUNCREF instruction.
1114 */
1115 static int
1116generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1117{
1118 isn_T *isn;
1119 garray_T *stack = &cctx->ctx_type_stack;
1120
Bram Moolenaar080457c2020-03-03 21:53:32 +01001121 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1123 return FAIL;
1124 isn->isn_arg.number = dfunc_idx;
1125
1126 if (ga_grow(stack, 1) == FAIL)
1127 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001128 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 // TODO: argument and return types
1130 ++stack->ga_len;
1131
1132 return OK;
1133}
1134
1135/*
1136 * Generate an ISN_JUMP instruction.
1137 */
1138 static int
1139generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1140{
1141 isn_T *isn;
1142 garray_T *stack = &cctx->ctx_type_stack;
1143
Bram Moolenaar080457c2020-03-03 21:53:32 +01001144 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1146 return FAIL;
1147 isn->isn_arg.jump.jump_when = when;
1148 isn->isn_arg.jump.jump_where = where;
1149
1150 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1151 --stack->ga_len;
1152
1153 return OK;
1154}
1155
1156 static int
1157generate_FOR(cctx_T *cctx, int loop_idx)
1158{
1159 isn_T *isn;
1160 garray_T *stack = &cctx->ctx_type_stack;
1161
Bram Moolenaar080457c2020-03-03 21:53:32 +01001162 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1164 return FAIL;
1165 isn->isn_arg.forloop.for_idx = loop_idx;
1166
1167 if (ga_grow(stack, 1) == FAIL)
1168 return FAIL;
1169 // type doesn't matter, will be stored next
1170 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1171 ++stack->ga_len;
1172
1173 return OK;
1174}
1175
1176/*
1177 * Generate an ISN_BCALL instruction.
1178 * Return FAIL if the number of arguments is wrong.
1179 */
1180 static int
1181generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1182{
1183 isn_T *isn;
1184 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001185 type_T *argtypes[MAX_FUNC_ARGS];
1186 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187
Bram Moolenaar080457c2020-03-03 21:53:32 +01001188 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 if (check_internal_func(func_idx, argcount) == FAIL)
1190 return FAIL;
1191
1192 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1193 return FAIL;
1194 isn->isn_arg.bfunc.cbf_idx = func_idx;
1195 isn->isn_arg.bfunc.cbf_argcount = argcount;
1196
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001197 for (i = 0; i < argcount; ++i)
1198 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 stack->ga_len -= argcount; // drop the arguments
1201 if (ga_grow(stack, 1) == FAIL)
1202 return FAIL;
1203 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001204 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205 ++stack->ga_len; // add return value
1206
1207 return OK;
1208}
1209
1210/*
1211 * Generate an ISN_DCALL or ISN_UCALL instruction.
1212 * Return FAIL if the number of arguments is wrong.
1213 */
1214 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001215generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216{
1217 isn_T *isn;
1218 garray_T *stack = &cctx->ctx_type_stack;
1219 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001220 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221
Bram Moolenaar080457c2020-03-03 21:53:32 +01001222 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 if (argcount > regular_args && !has_varargs(ufunc))
1224 {
1225 semsg(_(e_toomanyarg), ufunc->uf_name);
1226 return FAIL;
1227 }
1228 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1229 {
1230 semsg(_(e_toofewarg), ufunc->uf_name);
1231 return FAIL;
1232 }
1233
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001234 if (ufunc->uf_dfunc_idx >= 0)
1235 {
1236 int i;
1237
1238 for (i = 0; i < argcount; ++i)
1239 {
1240 type_T *expected;
1241 type_T *actual;
1242
1243 if (i < regular_args)
1244 {
1245 if (ufunc->uf_arg_types == NULL)
1246 continue;
1247 expected = ufunc->uf_arg_types[i];
1248 }
1249 else
1250 expected = ufunc->uf_va_type->tt_member;
1251 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1252 if (check_type(expected, actual, FALSE) == FAIL)
1253 {
1254 arg_type_mismatch(expected, actual, i + 1);
1255 return FAIL;
1256 }
1257 }
1258 }
1259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 if ((isn = generate_instr(cctx,
1261 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1262 return FAIL;
1263 if (ufunc->uf_dfunc_idx >= 0)
1264 {
1265 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1266 isn->isn_arg.dfunc.cdf_argcount = argcount;
1267 }
1268 else
1269 {
1270 // A user function may be deleted and redefined later, can't use the
1271 // ufunc pointer, need to look it up again at runtime.
1272 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1273 isn->isn_arg.ufunc.cuf_argcount = argcount;
1274 }
1275
1276 stack->ga_len -= argcount; // drop the arguments
1277 if (ga_grow(stack, 1) == FAIL)
1278 return FAIL;
1279 // add return value
1280 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1281 ++stack->ga_len;
1282
1283 return OK;
1284}
1285
1286/*
1287 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1288 */
1289 static int
1290generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1291{
1292 isn_T *isn;
1293 garray_T *stack = &cctx->ctx_type_stack;
1294
Bram Moolenaar080457c2020-03-03 21:53:32 +01001295 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1297 return FAIL;
1298 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1299 isn->isn_arg.ufunc.cuf_argcount = argcount;
1300
1301 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001302 if (ga_grow(stack, 1) == FAIL)
1303 return FAIL;
1304 // add return value
1305 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1306 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307
1308 return OK;
1309}
1310
1311/*
1312 * Generate an ISN_PCALL instruction.
1313 */
1314 static int
1315generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1316{
1317 isn_T *isn;
1318 garray_T *stack = &cctx->ctx_type_stack;
1319
Bram Moolenaar080457c2020-03-03 21:53:32 +01001320 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1323 return FAIL;
1324 isn->isn_arg.pfunc.cpf_top = at_top;
1325 isn->isn_arg.pfunc.cpf_argcount = argcount;
1326
1327 stack->ga_len -= argcount; // drop the arguments
1328
1329 // drop the funcref/partial, get back the return value
1330 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1331
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001332 // If partial is above the arguments it must be cleared and replaced with
1333 // the return value.
1334 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1335 return FAIL;
1336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 return OK;
1338}
1339
1340/*
1341 * Generate an ISN_MEMBER instruction.
1342 */
1343 static int
1344generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1345{
1346 isn_T *isn;
1347 garray_T *stack = &cctx->ctx_type_stack;
1348 type_T *type;
1349
Bram Moolenaar080457c2020-03-03 21:53:32 +01001350 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1352 return FAIL;
1353 isn->isn_arg.string = vim_strnsave(name, (int)len);
1354
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001355 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001357 if (type->tt_type != VAR_DICT && type != &t_any)
1358 {
1359 emsg(_(e_dictreq));
1360 return FAIL;
1361 }
1362 // change dict type to dict member type
1363 if (type->tt_type == VAR_DICT)
1364 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365
1366 return OK;
1367}
1368
1369/*
1370 * Generate an ISN_ECHO instruction.
1371 */
1372 static int
1373generate_ECHO(cctx_T *cctx, int with_white, int count)
1374{
1375 isn_T *isn;
1376
Bram Moolenaar080457c2020-03-03 21:53:32 +01001377 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1379 return FAIL;
1380 isn->isn_arg.echo.echo_with_white = with_white;
1381 isn->isn_arg.echo.echo_count = count;
1382
1383 return OK;
1384}
1385
Bram Moolenaarad39c092020-02-26 18:23:43 +01001386/*
1387 * Generate an ISN_EXECUTE instruction.
1388 */
1389 static int
1390generate_EXECUTE(cctx_T *cctx, int count)
1391{
1392 isn_T *isn;
1393
1394 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1395 return FAIL;
1396 isn->isn_arg.number = count;
1397
1398 return OK;
1399}
1400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 static int
1402generate_EXEC(cctx_T *cctx, char_u *line)
1403{
1404 isn_T *isn;
1405
Bram Moolenaar080457c2020-03-03 21:53:32 +01001406 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1408 return FAIL;
1409 isn->isn_arg.string = vim_strsave(line);
1410 return OK;
1411}
1412
1413static char e_white_both[] =
1414 N_("E1004: white space required before and after '%s'");
Bram Moolenaard77a8522020-04-03 21:59:57 +02001415static char e_white_after[] = N_("E1069: white space required after '%s'");
1416static char e_no_white_before[] = N_("E1068: No white space allowed before '%s'");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417
1418/*
1419 * Reserve space for a local variable.
1420 * Return the index or -1 if it failed.
1421 */
1422 static int
1423reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1424{
1425 int idx;
1426 lvar_T *lvar;
1427
1428 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1429 {
1430 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1431 return -1;
1432 }
1433
1434 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1435 return -1;
1436 idx = cctx->ctx_locals.ga_len;
1437 if (cctx->ctx_max_local < idx + 1)
1438 cctx->ctx_max_local = idx + 1;
1439 ++cctx->ctx_locals.ga_len;
1440
1441 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1442 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1443 lvar->lv_const = isConst;
1444 lvar->lv_type = type;
1445
1446 return idx;
1447}
1448
1449/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001450 * Remove local variables above "new_top".
1451 */
1452 static void
1453unwind_locals(cctx_T *cctx, int new_top)
1454{
1455 if (cctx->ctx_locals.ga_len > new_top)
1456 {
1457 int idx;
1458 lvar_T *lvar;
1459
1460 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1461 {
1462 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1463 vim_free(lvar->lv_name);
1464 }
1465 }
1466 cctx->ctx_locals.ga_len = new_top;
1467}
1468
1469/*
1470 * Free all local variables.
1471 */
1472 static void
1473free_local(cctx_T *cctx)
1474{
1475 unwind_locals(cctx, 0);
1476 ga_clear(&cctx->ctx_locals);
1477}
1478
1479/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 * Skip over a type definition and return a pointer to just after it.
1481 */
1482 char_u *
1483skip_type(char_u *start)
1484{
1485 char_u *p = start;
1486
1487 while (ASCII_ISALNUM(*p) || *p == '_')
1488 ++p;
1489
1490 // Skip over "<type>"; this is permissive about white space.
1491 if (*skipwhite(p) == '<')
1492 {
1493 p = skipwhite(p);
1494 p = skip_type(skipwhite(p + 1));
1495 p = skipwhite(p);
1496 if (*p == '>')
1497 ++p;
1498 }
1499 return p;
1500}
1501
1502/*
1503 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001504 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 * Returns NULL in case of failure.
1506 */
1507 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001508parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509{
1510 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001511 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512
1513 if (**arg != '<')
1514 {
1515 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001516 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517 else
1518 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001519 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 }
1521 *arg = skipwhite(*arg + 1);
1522
Bram Moolenaard77a8522020-04-03 21:59:57 +02001523 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001524
1525 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001526 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 {
1528 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001529 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 }
1531 ++*arg;
1532
1533 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001534 return get_list_type(member_type, type_gap);
1535 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536}
1537
1538/*
1539 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001540 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 */
1542 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001543parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544{
1545 char_u *p = *arg;
1546 size_t len;
1547
1548 // skip over the first word
1549 while (ASCII_ISALNUM(*p) || *p == '_')
1550 ++p;
1551 len = p - *arg;
1552
1553 switch (**arg)
1554 {
1555 case 'a':
1556 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1557 {
1558 *arg += len;
1559 return &t_any;
1560 }
1561 break;
1562 case 'b':
1563 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1564 {
1565 *arg += len;
1566 return &t_bool;
1567 }
1568 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1569 {
1570 *arg += len;
1571 return &t_blob;
1572 }
1573 break;
1574 case 'c':
1575 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1576 {
1577 *arg += len;
1578 return &t_channel;
1579 }
1580 break;
1581 case 'd':
1582 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1583 {
1584 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001585 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 }
1587 break;
1588 case 'f':
1589 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1590 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001591#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 *arg += len;
1593 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001594#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001595 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001596 return &t_any;
1597#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 }
1599 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1600 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001601 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001602 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001603 int argcount = -1;
1604 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001605 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001606 type_T *arg_type[MAX_FUNC_ARGS + 1];
1607
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001608 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001610 if (**arg == '(')
1611 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001612 // "func" may or may not return a value, "func()" does
1613 // not return a value.
1614 ret_type = &t_void;
1615
Bram Moolenaard77a8522020-04-03 21:59:57 +02001616 p = ++*arg;
1617 argcount = 0;
1618 while (*p != NUL && *p != ')')
1619 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001620 if (*p == '?')
1621 {
1622 if (first_optional == -1)
1623 first_optional = argcount;
1624 ++p;
1625 }
1626 else if (first_optional != -1)
1627 {
1628 emsg(_("E1007: mandatory argument after optional argument"));
1629 return &t_any;
1630 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001631 else if (STRNCMP(p, "...", 3) == 0)
1632 {
1633 flags |= TTFLAG_VARARGS;
1634 p += 3;
1635 }
1636
1637 arg_type[argcount++] = parse_type(&p, type_gap);
1638
1639 // Nothing comes after "...{type}".
1640 if (flags & TTFLAG_VARARGS)
1641 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001642
Bram Moolenaard77a8522020-04-03 21:59:57 +02001643 if (*p != ',' && *skipwhite(p) == ',')
1644 {
1645 semsg(_(e_no_white_before), ",");
1646 return &t_any;
1647 }
1648 if (*p == ',')
1649 {
1650 ++p;
1651 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001652 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001653 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001654 return &t_any;
1655 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001656 }
1657 p = skipwhite(p);
1658 if (argcount == MAX_FUNC_ARGS)
1659 {
1660 emsg(_("E740: Too many argument types"));
1661 return &t_any;
1662 }
1663 }
1664
1665 p = skipwhite(p);
1666 if (*p != ')')
1667 {
1668 emsg(_(e_missing_close));
1669 return &t_any;
1670 }
1671 *arg = p + 1;
1672 }
1673 if (**arg == ':')
1674 {
1675 // parse return type
1676 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001677 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001678 semsg(_(e_white_after), ":");
1679 *arg = skipwhite(*arg);
1680 ret_type = parse_type(arg, type_gap);
1681 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001682 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001683 type = get_func_type(ret_type, argcount, type_gap);
1684 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001685 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001686 type = alloc_func_type(ret_type, argcount, type_gap);
1687 type->tt_flags = flags;
1688 if (argcount > 0)
1689 {
1690 type->tt_argcount = argcount;
1691 type->tt_min_argcount = first_optional == -1
1692 ? argcount : first_optional;
1693 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001694 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001695 return &t_any;
1696 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001697 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001698 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001699 }
1700 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 }
1702 break;
1703 case 'j':
1704 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1705 {
1706 *arg += len;
1707 return &t_job;
1708 }
1709 break;
1710 case 'l':
1711 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1712 {
1713 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001714 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715 }
1716 break;
1717 case 'n':
1718 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1719 {
1720 *arg += len;
1721 return &t_number;
1722 }
1723 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 case 's':
1725 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1726 {
1727 *arg += len;
1728 return &t_string;
1729 }
1730 break;
1731 case 'v':
1732 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1733 {
1734 *arg += len;
1735 return &t_void;
1736 }
1737 break;
1738 }
1739
1740 semsg(_("E1010: Type not recognized: %s"), *arg);
1741 return &t_any;
1742}
1743
1744/*
1745 * Check if "type1" and "type2" are exactly the same.
1746 */
1747 static int
1748equal_type(type_T *type1, type_T *type2)
1749{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001750 int i;
1751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 if (type1->tt_type != type2->tt_type)
1753 return FALSE;
1754 switch (type1->tt_type)
1755 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001757 case VAR_ANY:
1758 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759 case VAR_SPECIAL:
1760 case VAR_BOOL:
1761 case VAR_NUMBER:
1762 case VAR_FLOAT:
1763 case VAR_STRING:
1764 case VAR_BLOB:
1765 case VAR_JOB:
1766 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001767 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 case VAR_LIST:
1769 case VAR_DICT:
1770 return equal_type(type1->tt_member, type2->tt_member);
1771 case VAR_FUNC:
1772 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001773 if (!equal_type(type1->tt_member, type2->tt_member)
1774 || type1->tt_argcount != type2->tt_argcount)
1775 return FALSE;
1776 if (type1->tt_argcount < 0
1777 || type1->tt_args == NULL || type2->tt_args == NULL)
1778 return TRUE;
1779 for (i = 0; i < type1->tt_argcount; ++i)
1780 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
1781 return FALSE;
1782 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 }
1784 return TRUE;
1785}
1786
1787/*
1788 * Find the common type of "type1" and "type2" and put it in "dest".
1789 * "type2" and "dest" may be the same.
1790 */
1791 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02001792common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793{
1794 if (equal_type(type1, type2))
1795 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001796 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 return;
1798 }
1799
1800 if (type1->tt_type == type2->tt_type)
1801 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1803 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001804 type_T *common;
1805
Bram Moolenaard77a8522020-04-03 21:59:57 +02001806 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001807 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001808 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001809 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001810 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 return;
1812 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001813 if (type1->tt_type == VAR_FUNC)
1814 {
1815 type_T *common;
1816
1817 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
1818 if (type1->tt_argcount == type2->tt_argcount
1819 && type1->tt_argcount >= 0)
1820 {
1821 int argcount = type1->tt_argcount;
1822 int i;
1823
1824 *dest = alloc_func_type(common, argcount, type_gap);
1825 if (type1->tt_args != NULL && type2->tt_args != NULL)
1826 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02001827 if (func_type_add_arg_types(*dest, argcount,
1828 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001829 for (i = 0; i < argcount; ++i)
1830 common_type(type1->tt_args[i], type2->tt_args[i],
1831 &(*dest)->tt_args[i], type_gap);
1832 }
1833 }
1834 else
1835 *dest = alloc_func_type(common, -1, type_gap);
1836 return;
1837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 }
1839
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001840 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841}
1842
1843 char *
1844vartype_name(vartype_T type)
1845{
1846 switch (type)
1847 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001848 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02001849 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 case VAR_SPECIAL: return "special";
1852 case VAR_BOOL: return "bool";
1853 case VAR_NUMBER: return "number";
1854 case VAR_FLOAT: return "float";
1855 case VAR_STRING: return "string";
1856 case VAR_BLOB: return "blob";
1857 case VAR_JOB: return "job";
1858 case VAR_CHANNEL: return "channel";
1859 case VAR_LIST: return "list";
1860 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001861
1862 case VAR_FUNC:
1863 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02001865 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866}
1867
1868/*
1869 * Return the name of a type.
1870 * The result may be in allocated memory, in which case "tofree" is set.
1871 */
1872 char *
1873type_name(type_T *type, char **tofree)
1874{
1875 char *name = vartype_name(type->tt_type);
1876
1877 *tofree = NULL;
1878 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1879 {
1880 char *member_free;
1881 char *member_name = type_name(type->tt_member, &member_free);
1882 size_t len;
1883
1884 len = STRLEN(name) + STRLEN(member_name) + 3;
1885 *tofree = alloc(len);
1886 if (*tofree != NULL)
1887 {
1888 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1889 vim_free(member_free);
1890 return *tofree;
1891 }
1892 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001893 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001894 {
1895 garray_T ga;
1896 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001897 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001898
1899 ga_init2(&ga, 1, 100);
1900 if (ga_grow(&ga, 20) == FAIL)
1901 return "[unknown]";
1902 *tofree = ga.ga_data;
1903 STRCPY(ga.ga_data, "func(");
1904 ga.ga_len += 5;
1905
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001906 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001907 {
1908 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001909 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001910 int len;
1911
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001912 if (type->tt_args == NULL)
1913 arg_type = "[unknown]";
1914 else
1915 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001916 if (i > 0)
1917 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001918 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001919 ga.ga_len += 2;
1920 }
1921 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001922 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001923 {
1924 vim_free(arg_free);
1925 return "[unknown]";
1926 }
1927 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001928 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001929 {
1930 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
1931 ga.ga_len += 3;
1932 }
1933 else if (i >= type->tt_min_argcount)
1934 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001935 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001936 ga.ga_len += len;
1937 vim_free(arg_free);
1938 }
1939
1940 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001941 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001942 else
1943 {
1944 char *ret_free;
1945 char *ret_name = type_name(type->tt_member, &ret_free);
1946 int len;
1947
1948 len = (int)STRLEN(ret_name) + 4;
1949 if (ga_grow(&ga, len) == FAIL)
1950 {
1951 vim_free(ret_free);
1952 return "[unknown]";
1953 }
1954 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001955 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
1956 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001957 vim_free(ret_free);
1958 }
1959 return ga.ga_data;
1960 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961
1962 return name;
1963}
1964
1965/*
1966 * Find "name" in script-local items of script "sid".
1967 * Returns the index in "sn_var_vals" if found.
1968 * If found but not in "sn_var_vals" returns -1.
1969 * If not found returns -2.
1970 */
1971 int
1972get_script_item_idx(int sid, char_u *name, int check_writable)
1973{
1974 hashtab_T *ht;
1975 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001976 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 int idx;
1978
1979 // First look the name up in the hashtable.
1980 if (sid <= 0 || sid > script_items.ga_len)
1981 return -1;
1982 ht = &SCRIPT_VARS(sid);
1983 di = find_var_in_ht(ht, 0, name, TRUE);
1984 if (di == NULL)
1985 return -2;
1986
1987 // Now find the svar_T index in sn_var_vals.
1988 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1989 {
1990 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1991
1992 if (sv->sv_tv == &di->di_tv)
1993 {
1994 if (check_writable && sv->sv_const)
1995 semsg(_(e_readonlyvar), name);
1996 return idx;
1997 }
1998 }
1999 return -1;
2000}
2001
2002/*
2003 * Find "name" in imported items of the current script/
2004 */
2005 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002006find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002008 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 int idx;
2010
2011 if (cctx != NULL)
2012 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2013 {
2014 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2015 + idx;
2016
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002017 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2018 : STRLEN(import->imp_name) == len
2019 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 return import;
2021 }
2022
2023 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2024 {
2025 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2026
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002027 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2028 : STRLEN(import->imp_name) == len
2029 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 return import;
2031 }
2032 return NULL;
2033}
2034
2035/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002036 * Free all imported variables.
2037 */
2038 static void
2039free_imported(cctx_T *cctx)
2040{
2041 int idx;
2042
2043 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2044 {
2045 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2046
2047 vim_free(import->imp_name);
2048 }
2049 ga_clear(&cctx->ctx_imports);
2050}
2051
2052/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002053 * Get the next line of the function from "cctx".
2054 * Returns NULL when at the end.
2055 */
2056 static char_u *
2057next_line_from_context(cctx_T *cctx)
2058{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002059 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002060
2061 do
2062 {
2063 ++cctx->ctx_lnum;
2064 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002065 {
2066 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002067 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002068 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002069 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002070 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002071 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2072 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002073 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002074 return line;
2075}
2076
2077/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002078 * Return TRUE if "p" points at a "#" but not at "#{".
2079 */
2080 static int
2081comment_start(char_u *p)
2082{
2083 return p[0] == '#' && p[1] != '{';
2084}
2085
2086/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002087 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002088 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002089 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2090 */
2091 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002092may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002093{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002094 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002095 {
2096 char_u *next = next_line_from_context(cctx);
2097
2098 if (next == NULL)
2099 return FAIL;
2100 *arg = skipwhite(next);
2101 }
2102 return OK;
2103}
2104
2105/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002106 * Generate an instruction to load script-local variable "name", without the
2107 * leading "s:".
2108 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 */
2110 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002111compile_load_scriptvar(
2112 cctx_T *cctx,
2113 char_u *name, // variable NUL terminated
2114 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002115 char_u **end, // end of variable
2116 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002118 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2120 imported_T *import;
2121
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002122 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002124 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002125 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2126 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 }
2128 if (idx >= 0)
2129 {
2130 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2131
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002132 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 current_sctx.sc_sid, idx, sv->sv_type);
2134 return OK;
2135 }
2136
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002137 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 if (import != NULL)
2139 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002140 if (import->imp_all)
2141 {
2142 char_u *p = skipwhite(*end);
2143 int name_len;
2144 ufunc_T *ufunc;
2145 type_T *type;
2146
2147 // Used "import * as Name", need to lookup the member.
2148 if (*p != '.')
2149 {
2150 semsg(_("E1060: expected dot after name: %s"), start);
2151 return FAIL;
2152 }
2153 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002154 if (VIM_ISWHITE(*p))
2155 {
2156 emsg(_("E1074: no white space allowed after dot"));
2157 return FAIL;
2158 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002159
2160 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2161 // TODO: what if it is a function?
2162 if (idx < 0)
2163 return FAIL;
2164 *end = p;
2165
2166 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2167 import->imp_sid,
2168 idx,
2169 type);
2170 }
2171 else
2172 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002173 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002174 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2175 import->imp_sid,
2176 import->imp_var_vals_idx,
2177 import->imp_type);
2178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 return OK;
2180 }
2181
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002182 if (error)
2183 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 return FAIL;
2185}
2186
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002187 static int
2188generate_funcref(cctx_T *cctx, char_u *name)
2189{
2190 ufunc_T *ufunc = find_func(name, cctx);
2191
2192 if (ufunc == NULL)
2193 return FAIL;
2194
2195 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2196}
2197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198/*
2199 * Compile a variable name into a load instruction.
2200 * "end" points to just after the name.
2201 * When "error" is FALSE do not give an error when not found.
2202 */
2203 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002204compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205{
2206 type_T *type;
2207 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002208 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002210 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211
2212 if (*(*arg + 1) == ':')
2213 {
2214 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002215 if (end <= *arg + 2)
2216 name = vim_strsave((char_u *)"[empty]");
2217 else
2218 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 if (name == NULL)
2220 return FAIL;
2221
2222 if (**arg == 'v')
2223 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002224 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 }
2226 else if (**arg == 'g')
2227 {
2228 // Global variables can be defined later, thus we don't check if it
2229 // exists, give error at runtime.
2230 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2231 }
2232 else if (**arg == 's')
2233 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002234 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002236 else if (**arg == 'b')
2237 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002238 // Buffer-local variables can be defined later, thus we don't check
2239 // if it exists, give error at runtime.
2240 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002241 }
2242 else if (**arg == 'w')
2243 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002244 // Window-local variables can be defined later, thus we don't check
2245 // if it exists, give error at runtime.
2246 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002247 }
2248 else if (**arg == 't')
2249 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002250 // Tabpage-local variables can be defined later, thus we don't
2251 // check if it exists, give error at runtime.
2252 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 else
2255 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002256 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 goto theend;
2258 }
2259 }
2260 else
2261 {
2262 size_t len = end - *arg;
2263 int idx;
2264 int gen_load = FALSE;
2265
2266 name = vim_strnsave(*arg, end - *arg);
2267 if (name == NULL)
2268 return FAIL;
2269
2270 idx = lookup_arg(*arg, len, cctx);
2271 if (idx >= 0)
2272 {
2273 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2274 type = cctx->ctx_ufunc->uf_arg_types[idx];
2275 else
2276 type = &t_any;
2277
2278 // Arguments are located above the frame pointer.
2279 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2280 if (cctx->ctx_ufunc->uf_va_name != NULL)
2281 --idx;
2282 gen_load = TRUE;
2283 }
2284 else if (lookup_vararg(*arg, len, cctx))
2285 {
2286 // varargs is always the last argument
2287 idx = -STACK_FRAME_SIZE - 1;
2288 type = cctx->ctx_ufunc->uf_va_type;
2289 gen_load = TRUE;
2290 }
2291 else
2292 {
2293 idx = lookup_local(*arg, len, cctx);
2294 if (idx >= 0)
2295 {
2296 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
2297 gen_load = TRUE;
2298 }
2299 else
2300 {
2301 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2302 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2303 res = generate_PUSHBOOL(cctx, **arg == 't'
2304 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002305 else
2306 {
2307 // "var" can be script-local even without using "s:" if it
2308 // already exists.
2309 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2310 == SCRIPT_VERSION_VIM9
2311 || lookup_script(*arg, len) == OK)
2312 res = compile_load_scriptvar(cctx, name, *arg, &end,
2313 FALSE);
2314
2315 // When the name starts with an uppercase letter or "x:" it
2316 // can be a user defined function.
2317 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2318 res = generate_funcref(cctx, name);
2319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 }
2321 }
2322 if (gen_load)
2323 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
2324 }
2325
2326 *arg = end;
2327
2328theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002329 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 semsg(_(e_var_notfound), name);
2331 vim_free(name);
2332 return res;
2333}
2334
2335/*
2336 * Compile the argument expressions.
2337 * "arg" points to just after the "(" and is advanced to after the ")"
2338 */
2339 static int
2340compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2341{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002342 char_u *p = *arg;
2343 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344
Bram Moolenaare6085c52020-04-12 20:19:16 +02002345 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002347 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002348 {
2349 p = next_line_from_context(cctx);
2350 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002351 goto failret;
2352 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002353 p = skipwhite(p);
2354 }
2355 if (*p == ')')
2356 {
2357 *arg = p + 1;
2358 return OK;
2359 }
2360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 if (compile_expr1(&p, cctx) == FAIL)
2362 return FAIL;
2363 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002364
2365 if (*p != ',' && *skipwhite(p) == ',')
2366 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002367 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002368 p = skipwhite(p);
2369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002371 {
2372 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002373 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002374 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002375 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002376 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002377 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002379failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002380 emsg(_(e_missing_close));
2381 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382}
2383
2384/*
2385 * Compile a function call: name(arg1, arg2)
2386 * "arg" points to "name", "arg + varlen" to the "(".
2387 * "argcount_init" is 1 for "value->method()"
2388 * Instructions:
2389 * EVAL arg1
2390 * EVAL arg2
2391 * BCALL / DCALL / UCALL
2392 */
2393 static int
2394compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2395{
2396 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002397 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 int argcount = argcount_init;
2399 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002400 char_u fname_buf[FLEN_FIXED + 1];
2401 char_u *tofree = NULL;
2402 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002404 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405
2406 if (varlen >= sizeof(namebuf))
2407 {
2408 semsg(_("E1011: name too long: %s"), name);
2409 return FAIL;
2410 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002411 vim_strncpy(namebuf, *arg, varlen);
2412 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413
2414 *arg = skipwhite(*arg + varlen + 1);
2415 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002416 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002418 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 {
2420 int idx;
2421
2422 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002423 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002425 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002426 else
2427 semsg(_(e_unknownfunc), namebuf);
2428 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 }
2430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002432 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002434 {
2435 res = generate_CALL(cctx, ufunc, argcount);
2436 goto theend;
2437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438
2439 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002440 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002442 if (STRNCMP(namebuf, "g:", 2) != 0
2443 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002444 {
2445 res = generate_PCALL(cctx, argcount, FALSE);
2446 goto theend;
2447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448
2449 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002450 res = generate_UCALL(cctx, name, argcount);
2451
2452theend:
2453 vim_free(tofree);
2454 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455}
2456
2457// like NAMESPACE_CHAR but with 'a' and 'l'.
2458#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2459
2460/*
2461 * Find the end of a variable or function name. Unlike find_name_end() this
2462 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002463 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 * Return a pointer to just after the name. Equal to "arg" if there is no
2465 * valid name.
2466 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002467 static char_u *
2468to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469{
2470 char_u *p;
2471
2472 // Quick check for valid starting character.
2473 if (!eval_isnamec1(*arg))
2474 return arg;
2475
2476 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2477 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2478 // and can be used in slice "[n:]".
2479 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002480 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2482 break;
2483 return p;
2484}
2485
2486/*
2487 * Like to_name_end() but also skip over a list or dict constant.
2488 */
2489 char_u *
2490to_name_const_end(char_u *arg)
2491{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002492 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 typval_T rettv;
2494
2495 if (p == arg && *arg == '[')
2496 {
2497
2498 // Can be "[1, 2, 3]->Func()".
2499 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2500 p = arg;
2501 }
2502 else if (p == arg && *arg == '#' && arg[1] == '{')
2503 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002504 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 ++p;
2506 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2507 p = arg;
2508 }
2509 else if (p == arg && *arg == '{')
2510 {
2511 int ret = get_lambda_tv(&p, &rettv, FALSE);
2512
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002513 // Can be "{x -> ret}()".
2514 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 if (ret == NOTDONE)
2516 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2517 if (ret != OK)
2518 p = arg;
2519 }
2520
2521 return p;
2522}
2523
2524 static void
2525type_mismatch(type_T *expected, type_T *actual)
2526{
2527 char *tofree1, *tofree2;
2528
2529 semsg(_("E1013: type mismatch, expected %s but got %s"),
2530 type_name(expected, &tofree1), type_name(actual, &tofree2));
2531 vim_free(tofree1);
2532 vim_free(tofree2);
2533}
2534
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002535 static void
2536arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
2537{
2538 char *tofree1, *tofree2;
2539
2540 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
2541 argidx,
2542 type_name(expected, &tofree1), type_name(actual, &tofree2));
2543 vim_free(tofree1);
2544 vim_free(tofree2);
2545}
2546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547/*
2548 * Check if the expected and actual types match.
2549 */
2550 static int
2551check_type(type_T *expected, type_T *actual, int give_msg)
2552{
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002553 int ret = OK;
2554
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002555 // When expected is "unknown" we accept any actual type.
2556 // When expected is "any" we accept any actual type except "void".
2557 if (expected->tt_type != VAR_UNKNOWN
2558 && (expected->tt_type != VAR_ANY || actual->tt_type == VAR_VOID))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 {
2560 if (expected->tt_type != actual->tt_type)
2561 {
2562 if (give_msg)
2563 type_mismatch(expected, actual);
2564 return FAIL;
2565 }
2566 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2567 {
Bram Moolenaar4c683752020-04-05 21:38:23 +02002568 // "unknown" is used for an empty list or dict
2569 if (actual->tt_member != &t_unknown)
Bram Moolenaar436472f2020-02-20 22:54:43 +01002570 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002572 else if (expected->tt_type == VAR_FUNC)
2573 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002574 if (expected->tt_member != &t_unknown)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002575 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
2576 if (ret == OK && expected->tt_argcount != -1
2577 && (actual->tt_argcount < expected->tt_min_argcount
2578 || actual->tt_argcount > expected->tt_argcount))
2579 ret = FAIL;
2580 }
2581 if (ret == FAIL && give_msg)
2582 type_mismatch(expected, actual);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 }
Bram Moolenaar89228602020-04-05 22:14:54 +02002584 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585}
2586
2587/*
2588 * Check that
2589 * - "actual" is "expected" type or
2590 * - "actual" is a type that can be "expected" type: add a runtime check; or
2591 * - return FAIL.
2592 */
2593 static int
2594need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2595{
Bram Moolenaar89228602020-04-05 22:14:54 +02002596 if (check_type(expected, actual, FALSE) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 return OK;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002598 if (actual->tt_type != VAR_ANY && actual->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 {
2600 type_mismatch(expected, actual);
2601 return FAIL;
2602 }
2603 generate_TYPECHECK(cctx, expected, offset);
2604 return OK;
2605}
2606
2607/*
2608 * parse a list: [expr, expr]
2609 * "*arg" points to the '['.
2610 */
2611 static int
2612compile_list(char_u **arg, cctx_T *cctx)
2613{
2614 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002615 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 int count = 0;
2617
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002618 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002620 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002621 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002622 p = next_line_from_context(cctx);
2623 if (p == NULL)
2624 {
2625 semsg(_(e_list_end), *arg);
2626 return FAIL;
2627 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002628 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002629 p = skipwhite(p);
2630 }
2631 if (*p == ']')
2632 {
2633 ++p;
2634 // Allow for following comment, after at least one space.
2635 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2636 p += STRLEN(p);
2637 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 if (compile_expr1(&p, cctx) == FAIL)
2640 break;
2641 ++count;
2642 if (*p == ',')
2643 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002644 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 p = skipwhite(p);
2646 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002647 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648
2649 generate_NEWLIST(cctx, count);
2650 return OK;
2651}
2652
2653/*
2654 * parse a lambda: {arg, arg -> expr}
2655 * "*arg" points to the '{'.
2656 */
2657 static int
2658compile_lambda(char_u **arg, cctx_T *cctx)
2659{
2660 garray_T *instr = &cctx->ctx_instr;
2661 typval_T rettv;
2662 ufunc_T *ufunc;
2663
2664 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002665 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002669 ++ufunc->uf_refcount;
2670 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002671 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672
2673 // The function will have one line: "return {expr}".
2674 // Compile it into instructions.
2675 compile_def_function(ufunc, TRUE);
2676
2677 if (ufunc->uf_dfunc_idx >= 0)
2678 {
2679 if (ga_grow(instr, 1) == FAIL)
2680 return FAIL;
2681 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2682 return OK;
2683 }
2684 return FAIL;
2685}
2686
2687/*
2688 * Compile a lamda call: expr->{lambda}(args)
2689 * "arg" points to the "{".
2690 */
2691 static int
2692compile_lambda_call(char_u **arg, cctx_T *cctx)
2693{
2694 ufunc_T *ufunc;
2695 typval_T rettv;
2696 int argcount = 1;
2697 int ret = FAIL;
2698
2699 // Get the funcref in "rettv".
2700 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2701 return FAIL;
2702
2703 if (**arg != '(')
2704 {
2705 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002706 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 else
2708 semsg(_(e_missing_paren), "lambda");
2709 clear_tv(&rettv);
2710 return FAIL;
2711 }
2712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 ufunc = rettv.vval.v_partial->pt_func;
2714 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002715 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002716 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002717
2718 // The function will have one line: "return {expr}".
2719 // Compile it into instructions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 compile_def_function(ufunc, TRUE);
2721
2722 // compile the arguments
2723 *arg = skipwhite(*arg + 1);
2724 if (compile_arguments(arg, cctx, &argcount) == OK)
2725 // call the compiled function
2726 ret = generate_CALL(cctx, ufunc, argcount);
2727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 return ret;
2729}
2730
2731/*
2732 * parse a dict: {'key': val} or #{key: val}
2733 * "*arg" points to the '{'.
2734 */
2735 static int
2736compile_dict(char_u **arg, cctx_T *cctx, int literal)
2737{
2738 garray_T *instr = &cctx->ctx_instr;
2739 int count = 0;
2740 dict_T *d = dict_alloc();
2741 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002742 char_u *whitep = *arg;
2743 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744
2745 if (d == NULL)
2746 return FAIL;
2747 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002748 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 {
2750 char_u *key = NULL;
2751
Bram Moolenaar2c330432020-04-13 14:41:35 +02002752 while (**arg == NUL || (literal && **arg == '"')
2753 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002754 {
2755 *arg = next_line_from_context(cctx);
2756 if (*arg == NULL)
2757 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002758 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002759 *arg = skipwhite(*arg);
2760 }
2761
2762 if (**arg == '}')
2763 break;
2764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 if (literal)
2766 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002767 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768
Bram Moolenaar2c330432020-04-13 14:41:35 +02002769 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 {
2771 semsg(_("E1014: Invalid key: %s"), *arg);
2772 return FAIL;
2773 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002774 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 if (generate_PUSHS(cctx, key) == FAIL)
2776 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002777 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 }
2779 else
2780 {
2781 isn_T *isn;
2782
2783 if (compile_expr1(arg, cctx) == FAIL)
2784 return FAIL;
2785 // TODO: check type is string
2786 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2787 if (isn->isn_type == ISN_PUSHS)
2788 key = isn->isn_arg.string;
2789 }
2790
2791 // Check for duplicate keys, if using string keys.
2792 if (key != NULL)
2793 {
2794 item = dict_find(d, key, -1);
2795 if (item != NULL)
2796 {
2797 semsg(_(e_duplicate_key), key);
2798 goto failret;
2799 }
2800 item = dictitem_alloc(key);
2801 if (item != NULL)
2802 {
2803 item->di_tv.v_type = VAR_UNKNOWN;
2804 item->di_tv.v_lock = 0;
2805 if (dict_add(d, item) == FAIL)
2806 dictitem_free(item);
2807 }
2808 }
2809
2810 *arg = skipwhite(*arg);
2811 if (**arg != ':')
2812 {
2813 semsg(_(e_missing_dict_colon), *arg);
2814 return FAIL;
2815 }
2816
Bram Moolenaar2c330432020-04-13 14:41:35 +02002817 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002819 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002820 {
2821 *arg = next_line_from_context(cctx);
2822 if (*arg == NULL)
2823 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002824 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002825 *arg = skipwhite(*arg);
2826 }
2827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 if (compile_expr1(arg, cctx) == FAIL)
2829 return FAIL;
2830 ++count;
2831
Bram Moolenaar2c330432020-04-13 14:41:35 +02002832 whitep = *arg;
2833 p = skipwhite(*arg);
2834 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002835 {
2836 *arg = next_line_from_context(cctx);
2837 if (*arg == NULL)
2838 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002839 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002840 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002841 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 if (**arg == '}')
2844 break;
2845 if (**arg != ',')
2846 {
2847 semsg(_(e_missing_dict_comma), *arg);
2848 goto failret;
2849 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002850 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 *arg = skipwhite(*arg + 1);
2852 }
2853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 *arg = *arg + 1;
2855
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002856 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002857 p = skipwhite(*arg);
2858 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002859 *arg += STRLEN(*arg);
2860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 dict_unref(d);
2862 return generate_NEWDICT(cctx, count);
2863
2864failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002865 if (*arg == NULL)
2866 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 dict_unref(d);
2868 return FAIL;
2869}
2870
2871/*
2872 * Compile "&option".
2873 */
2874 static int
2875compile_get_option(char_u **arg, cctx_T *cctx)
2876{
2877 typval_T rettv;
2878 char_u *start = *arg;
2879 int ret;
2880
2881 // parse the option and get the current value to get the type.
2882 rettv.v_type = VAR_UNKNOWN;
2883 ret = get_option_tv(arg, &rettv, TRUE);
2884 if (ret == OK)
2885 {
2886 // include the '&' in the name, get_option_tv() expects it.
2887 char_u *name = vim_strnsave(start, *arg - start);
2888 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2889
2890 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2891 vim_free(name);
2892 }
2893 clear_tv(&rettv);
2894
2895 return ret;
2896}
2897
2898/*
2899 * Compile "$VAR".
2900 */
2901 static int
2902compile_get_env(char_u **arg, cctx_T *cctx)
2903{
2904 char_u *start = *arg;
2905 int len;
2906 int ret;
2907 char_u *name;
2908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 ++*arg;
2910 len = get_env_len(arg);
2911 if (len == 0)
2912 {
2913 semsg(_(e_syntax_at), start - 1);
2914 return FAIL;
2915 }
2916
2917 // include the '$' in the name, get_env_tv() expects it.
2918 name = vim_strnsave(start, len + 1);
2919 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2920 vim_free(name);
2921 return ret;
2922}
2923
2924/*
2925 * Compile "@r".
2926 */
2927 static int
2928compile_get_register(char_u **arg, cctx_T *cctx)
2929{
2930 int ret;
2931
2932 ++*arg;
2933 if (**arg == NUL)
2934 {
2935 semsg(_(e_syntax_at), *arg - 1);
2936 return FAIL;
2937 }
2938 if (!valid_yank_reg(**arg, TRUE))
2939 {
2940 emsg_invreg(**arg);
2941 return FAIL;
2942 }
2943 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2944 ++*arg;
2945 return ret;
2946}
2947
2948/*
2949 * Apply leading '!', '-' and '+' to constant "rettv".
2950 */
2951 static int
2952apply_leader(typval_T *rettv, char_u *start, char_u *end)
2953{
2954 char_u *p = end;
2955
2956 // this works from end to start
2957 while (p > start)
2958 {
2959 --p;
2960 if (*p == '-' || *p == '+')
2961 {
2962 // only '-' has an effect, for '+' we only check the type
2963#ifdef FEAT_FLOAT
2964 if (rettv->v_type == VAR_FLOAT)
2965 {
2966 if (*p == '-')
2967 rettv->vval.v_float = -rettv->vval.v_float;
2968 }
2969 else
2970#endif
2971 {
2972 varnumber_T val;
2973 int error = FALSE;
2974
2975 // tv_get_number_chk() accepts a string, but we don't want that
2976 // here
2977 if (check_not_string(rettv) == FAIL)
2978 return FAIL;
2979 val = tv_get_number_chk(rettv, &error);
2980 clear_tv(rettv);
2981 if (error)
2982 return FAIL;
2983 if (*p == '-')
2984 val = -val;
2985 rettv->v_type = VAR_NUMBER;
2986 rettv->vval.v_number = val;
2987 }
2988 }
2989 else
2990 {
2991 int v = tv2bool(rettv);
2992
2993 // '!' is permissive in the type.
2994 clear_tv(rettv);
2995 rettv->v_type = VAR_BOOL;
2996 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2997 }
2998 }
2999 return OK;
3000}
3001
3002/*
3003 * Recognize v: variables that are constants and set "rettv".
3004 */
3005 static void
3006get_vim_constant(char_u **arg, typval_T *rettv)
3007{
3008 if (STRNCMP(*arg, "v:true", 6) == 0)
3009 {
3010 rettv->v_type = VAR_BOOL;
3011 rettv->vval.v_number = VVAL_TRUE;
3012 *arg += 6;
3013 }
3014 else if (STRNCMP(*arg, "v:false", 7) == 0)
3015 {
3016 rettv->v_type = VAR_BOOL;
3017 rettv->vval.v_number = VVAL_FALSE;
3018 *arg += 7;
3019 }
3020 else if (STRNCMP(*arg, "v:null", 6) == 0)
3021 {
3022 rettv->v_type = VAR_SPECIAL;
3023 rettv->vval.v_number = VVAL_NULL;
3024 *arg += 6;
3025 }
3026 else if (STRNCMP(*arg, "v:none", 6) == 0)
3027 {
3028 rettv->v_type = VAR_SPECIAL;
3029 rettv->vval.v_number = VVAL_NONE;
3030 *arg += 6;
3031 }
3032}
3033
3034/*
3035 * Compile code to apply '-', '+' and '!'.
3036 */
3037 static int
3038compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3039{
3040 char_u *p = end;
3041
3042 // this works from end to start
3043 while (p > start)
3044 {
3045 --p;
3046 if (*p == '-' || *p == '+')
3047 {
3048 int negate = *p == '-';
3049 isn_T *isn;
3050
3051 // TODO: check type
3052 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3053 {
3054 --p;
3055 if (*p == '-')
3056 negate = !negate;
3057 }
3058 // only '-' has an effect, for '+' we only check the type
3059 if (negate)
3060 isn = generate_instr(cctx, ISN_NEGATENR);
3061 else
3062 isn = generate_instr(cctx, ISN_CHECKNR);
3063 if (isn == NULL)
3064 return FAIL;
3065 }
3066 else
3067 {
3068 int invert = TRUE;
3069
3070 while (p > start && p[-1] == '!')
3071 {
3072 --p;
3073 invert = !invert;
3074 }
3075 if (generate_2BOOL(cctx, invert) == FAIL)
3076 return FAIL;
3077 }
3078 }
3079 return OK;
3080}
3081
3082/*
3083 * Compile whatever comes after "name" or "name()".
3084 */
3085 static int
3086compile_subscript(
3087 char_u **arg,
3088 cctx_T *cctx,
3089 char_u **start_leader,
3090 char_u *end_leader)
3091{
3092 for (;;)
3093 {
3094 if (**arg == '(')
3095 {
3096 int argcount = 0;
3097
3098 // funcref(arg)
3099 *arg = skipwhite(*arg + 1);
3100 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3101 return FAIL;
3102 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
3103 return FAIL;
3104 }
3105 else if (**arg == '-' && (*arg)[1] == '>')
3106 {
3107 char_u *p;
3108
3109 // something->method()
3110 // Apply the '!', '-' and '+' first:
3111 // -1.0->func() works like (-1.0)->func()
3112 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3113 return FAIL;
3114 *start_leader = end_leader; // don't apply again later
3115
3116 *arg = skipwhite(*arg + 2);
3117 if (**arg == '{')
3118 {
3119 // lambda call: list->{lambda}
3120 if (compile_lambda_call(arg, cctx) == FAIL)
3121 return FAIL;
3122 }
3123 else
3124 {
3125 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003126 p = *arg;
3127 if (ASCII_ISALPHA(*p) && p[1] == ':')
3128 p += 2;
3129 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 ;
3131 if (*p != '(')
3132 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003133 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 return FAIL;
3135 }
3136 // TODO: base value may not be the first argument
3137 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
3138 return FAIL;
3139 }
3140 }
3141 else if (**arg == '[')
3142 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003143 garray_T *stack;
3144 type_T **typep;
3145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 // list index: list[123]
3147 // TODO: more arguments
3148 // TODO: dict member dict['name']
3149 *arg = skipwhite(*arg + 1);
3150 if (compile_expr1(arg, cctx) == FAIL)
3151 return FAIL;
3152
3153 if (**arg != ']')
3154 {
3155 emsg(_(e_missbrac));
3156 return FAIL;
3157 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003158 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159
3160 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3161 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003162 stack = &cctx->ctx_type_stack;
3163 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3164 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3165 {
3166 emsg(_(e_listreq));
3167 return FAIL;
3168 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003169 if ((*typep)->tt_type == VAR_LIST)
3170 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 }
3172 else if (**arg == '.' && (*arg)[1] != '.')
3173 {
3174 char_u *p;
3175
3176 ++*arg;
3177 p = *arg;
3178 // dictionary member: dict.name
3179 if (eval_isnamec1(*p))
3180 while (eval_isnamec(*p))
3181 MB_PTR_ADV(p);
3182 if (p == *arg)
3183 {
3184 semsg(_(e_syntax_at), *arg);
3185 return FAIL;
3186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3188 return FAIL;
3189 *arg = p;
3190 }
3191 else
3192 break;
3193 }
3194
3195 // TODO - see handle_subscript():
3196 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3197 // Don't do this when "Func" is already a partial that was bound
3198 // explicitly (pt_auto is FALSE).
3199
3200 return OK;
3201}
3202
3203/*
3204 * Compile an expression at "*p" and add instructions to "instr".
3205 * "p" is advanced until after the expression, skipping white space.
3206 *
3207 * This is the equivalent of eval1(), eval2(), etc.
3208 */
3209
3210/*
3211 * number number constant
3212 * 0zFFFFFFFF Blob constant
3213 * "string" string constant
3214 * 'string' literal string constant
3215 * &option-name option value
3216 * @r register contents
3217 * identifier variable value
3218 * function() function call
3219 * $VAR environment variable
3220 * (expression) nested expression
3221 * [expr, expr] List
3222 * {key: val, key: val} Dictionary
3223 * #{key: val, key: val} Dictionary with literal keys
3224 *
3225 * Also handle:
3226 * ! in front logical NOT
3227 * - in front unary minus
3228 * + in front unary plus (ignored)
3229 * trailing (arg) funcref/partial call
3230 * trailing [] subscript in String or List
3231 * trailing .name entry in Dictionary
3232 * trailing ->name() method call
3233 */
3234 static int
3235compile_expr7(char_u **arg, cctx_T *cctx)
3236{
3237 typval_T rettv;
3238 char_u *start_leader, *end_leader;
3239 int ret = OK;
3240
3241 /*
3242 * Skip '!', '-' and '+' characters. They are handled later.
3243 */
3244 start_leader = *arg;
3245 while (**arg == '!' || **arg == '-' || **arg == '+')
3246 *arg = skipwhite(*arg + 1);
3247 end_leader = *arg;
3248
3249 rettv.v_type = VAR_UNKNOWN;
3250 switch (**arg)
3251 {
3252 /*
3253 * Number constant.
3254 */
3255 case '0': // also for blob starting with 0z
3256 case '1':
3257 case '2':
3258 case '3':
3259 case '4':
3260 case '5':
3261 case '6':
3262 case '7':
3263 case '8':
3264 case '9':
3265 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3266 return FAIL;
3267 break;
3268
3269 /*
3270 * String constant: "string".
3271 */
3272 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3273 return FAIL;
3274 break;
3275
3276 /*
3277 * Literal string constant: 'str''ing'.
3278 */
3279 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3280 return FAIL;
3281 break;
3282
3283 /*
3284 * Constant Vim variable.
3285 */
3286 case 'v': get_vim_constant(arg, &rettv);
3287 ret = NOTDONE;
3288 break;
3289
3290 /*
3291 * List: [expr, expr]
3292 */
3293 case '[': ret = compile_list(arg, cctx);
3294 break;
3295
3296 /*
3297 * Dictionary: #{key: val, key: val}
3298 */
3299 case '#': if ((*arg)[1] == '{')
3300 {
3301 ++*arg;
3302 ret = compile_dict(arg, cctx, TRUE);
3303 }
3304 else
3305 ret = NOTDONE;
3306 break;
3307
3308 /*
3309 * Lambda: {arg, arg -> expr}
3310 * Dictionary: {'key': val, 'key': val}
3311 */
3312 case '{': {
3313 char_u *start = skipwhite(*arg + 1);
3314
3315 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003316 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003318 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 if (ret != FAIL && *start == '>')
3320 ret = compile_lambda(arg, cctx);
3321 else
3322 ret = compile_dict(arg, cctx, FALSE);
3323 }
3324 break;
3325
3326 /*
3327 * Option value: &name
3328 */
3329 case '&': ret = compile_get_option(arg, cctx);
3330 break;
3331
3332 /*
3333 * Environment variable: $VAR.
3334 */
3335 case '$': ret = compile_get_env(arg, cctx);
3336 break;
3337
3338 /*
3339 * Register contents: @r.
3340 */
3341 case '@': ret = compile_get_register(arg, cctx);
3342 break;
3343 /*
3344 * nested expression: (expression).
3345 */
3346 case '(': *arg = skipwhite(*arg + 1);
3347 ret = compile_expr1(arg, cctx); // recursive!
3348 *arg = skipwhite(*arg);
3349 if (**arg == ')')
3350 ++*arg;
3351 else if (ret == OK)
3352 {
3353 emsg(_(e_missing_close));
3354 ret = FAIL;
3355 }
3356 break;
3357
3358 default: ret = NOTDONE;
3359 break;
3360 }
3361 if (ret == FAIL)
3362 return FAIL;
3363
3364 if (rettv.v_type != VAR_UNKNOWN)
3365 {
3366 // apply the '!', '-' and '+' before the constant
3367 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3368 {
3369 clear_tv(&rettv);
3370 return FAIL;
3371 }
3372 start_leader = end_leader; // don't apply again below
3373
3374 // push constant
3375 switch (rettv.v_type)
3376 {
3377 case VAR_BOOL:
3378 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3379 break;
3380 case VAR_SPECIAL:
3381 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3382 break;
3383 case VAR_NUMBER:
3384 generate_PUSHNR(cctx, rettv.vval.v_number);
3385 break;
3386#ifdef FEAT_FLOAT
3387 case VAR_FLOAT:
3388 generate_PUSHF(cctx, rettv.vval.v_float);
3389 break;
3390#endif
3391 case VAR_BLOB:
3392 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3393 rettv.vval.v_blob = NULL;
3394 break;
3395 case VAR_STRING:
3396 generate_PUSHS(cctx, rettv.vval.v_string);
3397 rettv.vval.v_string = NULL;
3398 break;
3399 default:
3400 iemsg("constant type missing");
3401 return FAIL;
3402 }
3403 }
3404 else if (ret == NOTDONE)
3405 {
3406 char_u *p;
3407 int r;
3408
3409 if (!eval_isnamec1(**arg))
3410 {
3411 semsg(_("E1015: Name expected: %s"), *arg);
3412 return FAIL;
3413 }
3414
3415 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003416 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 if (*p == '(')
3418 r = compile_call(arg, p - *arg, cctx, 0);
3419 else
3420 r = compile_load(arg, p, cctx, TRUE);
3421 if (r == FAIL)
3422 return FAIL;
3423 }
3424
3425 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3426 return FAIL;
3427
3428 // Now deal with prefixed '-', '+' and '!', if not done already.
3429 return compile_leader(cctx, start_leader, end_leader);
3430}
3431
3432/*
3433 * * number multiplication
3434 * / number division
3435 * % number modulo
3436 */
3437 static int
3438compile_expr6(char_u **arg, cctx_T *cctx)
3439{
3440 char_u *op;
3441
3442 // get the first variable
3443 if (compile_expr7(arg, cctx) == FAIL)
3444 return FAIL;
3445
3446 /*
3447 * Repeat computing, until no "*", "/" or "%" is following.
3448 */
3449 for (;;)
3450 {
3451 op = skipwhite(*arg);
3452 if (*op != '*' && *op != '/' && *op != '%')
3453 break;
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003454 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 {
3456 char_u buf[3];
3457
3458 vim_strncpy(buf, op, 1);
3459 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003460 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 }
3462 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003463 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003464 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465
3466 // get the second variable
3467 if (compile_expr7(arg, cctx) == FAIL)
3468 return FAIL;
3469
3470 generate_two_op(cctx, op);
3471 }
3472
3473 return OK;
3474}
3475
3476/*
3477 * + number addition
3478 * - number subtraction
3479 * .. string concatenation
3480 */
3481 static int
3482compile_expr5(char_u **arg, cctx_T *cctx)
3483{
3484 char_u *op;
3485 int oplen;
3486
3487 // get the first variable
3488 if (compile_expr6(arg, cctx) == FAIL)
3489 return FAIL;
3490
3491 /*
3492 * Repeat computing, until no "+", "-" or ".." is following.
3493 */
3494 for (;;)
3495 {
3496 op = skipwhite(*arg);
3497 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3498 break;
3499 oplen = (*op == '.' ? 2 : 1);
3500
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003501 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 {
3503 char_u buf[3];
3504
3505 vim_strncpy(buf, op, oplen);
3506 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003507 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 }
3509
3510 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003511 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003512 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513
3514 // get the second variable
3515 if (compile_expr6(arg, cctx) == FAIL)
3516 return FAIL;
3517
3518 if (*op == '.')
3519 {
3520 if (may_generate_2STRING(-2, cctx) == FAIL
3521 || may_generate_2STRING(-1, cctx) == FAIL)
3522 return FAIL;
3523 generate_instr_drop(cctx, ISN_CONCAT, 1);
3524 }
3525 else
3526 generate_two_op(cctx, op);
3527 }
3528
3529 return OK;
3530}
3531
Bram Moolenaar080457c2020-03-03 21:53:32 +01003532 static exptype_T
3533get_compare_type(char_u *p, int *len, int *type_is)
3534{
3535 exptype_T type = EXPR_UNKNOWN;
3536 int i;
3537
3538 switch (p[0])
3539 {
3540 case '=': if (p[1] == '=')
3541 type = EXPR_EQUAL;
3542 else if (p[1] == '~')
3543 type = EXPR_MATCH;
3544 break;
3545 case '!': if (p[1] == '=')
3546 type = EXPR_NEQUAL;
3547 else if (p[1] == '~')
3548 type = EXPR_NOMATCH;
3549 break;
3550 case '>': if (p[1] != '=')
3551 {
3552 type = EXPR_GREATER;
3553 *len = 1;
3554 }
3555 else
3556 type = EXPR_GEQUAL;
3557 break;
3558 case '<': if (p[1] != '=')
3559 {
3560 type = EXPR_SMALLER;
3561 *len = 1;
3562 }
3563 else
3564 type = EXPR_SEQUAL;
3565 break;
3566 case 'i': if (p[1] == 's')
3567 {
3568 // "is" and "isnot"; but not a prefix of a name
3569 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3570 *len = 5;
3571 i = p[*len];
3572 if (!isalnum(i) && i != '_')
3573 {
3574 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3575 *type_is = TRUE;
3576 }
3577 }
3578 break;
3579 }
3580 return type;
3581}
3582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583/*
3584 * expr5a == expr5b
3585 * expr5a =~ expr5b
3586 * expr5a != expr5b
3587 * expr5a !~ expr5b
3588 * expr5a > expr5b
3589 * expr5a >= expr5b
3590 * expr5a < expr5b
3591 * expr5a <= expr5b
3592 * expr5a is expr5b
3593 * expr5a isnot expr5b
3594 *
3595 * Produces instructions:
3596 * EVAL expr5a Push result of "expr5a"
3597 * EVAL expr5b Push result of "expr5b"
3598 * COMPARE one of the compare instructions
3599 */
3600 static int
3601compile_expr4(char_u **arg, cctx_T *cctx)
3602{
3603 exptype_T type = EXPR_UNKNOWN;
3604 char_u *p;
3605 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 int type_is = FALSE;
3607
3608 // get the first variable
3609 if (compile_expr5(arg, cctx) == FAIL)
3610 return FAIL;
3611
3612 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003613 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614
3615 /*
3616 * If there is a comparative operator, use it.
3617 */
3618 if (type != EXPR_UNKNOWN)
3619 {
3620 int ic = FALSE; // Default: do not ignore case
3621
3622 if (type_is && (p[len] == '?' || p[len] == '#'))
3623 {
3624 semsg(_(e_invexpr2), *arg);
3625 return FAIL;
3626 }
3627 // extra question mark appended: ignore case
3628 if (p[len] == '?')
3629 {
3630 ic = TRUE;
3631 ++len;
3632 }
3633 // extra '#' appended: match case (ignored)
3634 else if (p[len] == '#')
3635 ++len;
3636 // nothing appended: match case
3637
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003638 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 {
3640 char_u buf[7];
3641
3642 vim_strncpy(buf, p, len);
3643 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003644 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 }
3646
3647 // get the second variable
3648 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003649 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003650 return FAIL;
3651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 if (compile_expr5(arg, cctx) == FAIL)
3653 return FAIL;
3654
3655 generate_COMPARE(cctx, type, ic);
3656 }
3657
3658 return OK;
3659}
3660
3661/*
3662 * Compile || or &&.
3663 */
3664 static int
3665compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3666{
3667 char_u *p = skipwhite(*arg);
3668 int opchar = *op;
3669
3670 if (p[0] == opchar && p[1] == opchar)
3671 {
3672 garray_T *instr = &cctx->ctx_instr;
3673 garray_T end_ga;
3674
3675 /*
3676 * Repeat until there is no following "||" or "&&"
3677 */
3678 ga_init2(&end_ga, sizeof(int), 10);
3679 while (p[0] == opchar && p[1] == opchar)
3680 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003681 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3682 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003684 return FAIL;
3685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686
3687 if (ga_grow(&end_ga, 1) == FAIL)
3688 {
3689 ga_clear(&end_ga);
3690 return FAIL;
3691 }
3692 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3693 ++end_ga.ga_len;
3694 generate_JUMP(cctx, opchar == '|'
3695 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3696
3697 // eval the next expression
3698 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003699 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003700 return FAIL;
3701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 if ((opchar == '|' ? compile_expr3(arg, cctx)
3703 : compile_expr4(arg, cctx)) == FAIL)
3704 {
3705 ga_clear(&end_ga);
3706 return FAIL;
3707 }
3708 p = skipwhite(*arg);
3709 }
3710
3711 // Fill in the end label in all jumps.
3712 while (end_ga.ga_len > 0)
3713 {
3714 isn_T *isn;
3715
3716 --end_ga.ga_len;
3717 isn = ((isn_T *)instr->ga_data)
3718 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3719 isn->isn_arg.jump.jump_where = instr->ga_len;
3720 }
3721 ga_clear(&end_ga);
3722 }
3723
3724 return OK;
3725}
3726
3727/*
3728 * expr4a && expr4a && expr4a logical AND
3729 *
3730 * Produces instructions:
3731 * EVAL expr4a Push result of "expr4a"
3732 * JUMP_AND_KEEP_IF_FALSE end
3733 * EVAL expr4b Push result of "expr4b"
3734 * JUMP_AND_KEEP_IF_FALSE end
3735 * EVAL expr4c Push result of "expr4c"
3736 * end:
3737 */
3738 static int
3739compile_expr3(char_u **arg, cctx_T *cctx)
3740{
3741 // get the first variable
3742 if (compile_expr4(arg, cctx) == FAIL)
3743 return FAIL;
3744
3745 // || and && work almost the same
3746 return compile_and_or(arg, cctx, "&&");
3747}
3748
3749/*
3750 * expr3a || expr3b || expr3c logical OR
3751 *
3752 * Produces instructions:
3753 * EVAL expr3a Push result of "expr3a"
3754 * JUMP_AND_KEEP_IF_TRUE end
3755 * EVAL expr3b Push result of "expr3b"
3756 * JUMP_AND_KEEP_IF_TRUE end
3757 * EVAL expr3c Push result of "expr3c"
3758 * end:
3759 */
3760 static int
3761compile_expr2(char_u **arg, cctx_T *cctx)
3762{
3763 // eval the first expression
3764 if (compile_expr3(arg, cctx) == FAIL)
3765 return FAIL;
3766
3767 // || and && work almost the same
3768 return compile_and_or(arg, cctx, "||");
3769}
3770
3771/*
3772 * Toplevel expression: expr2 ? expr1a : expr1b
3773 *
3774 * Produces instructions:
3775 * EVAL expr2 Push result of "expr"
3776 * JUMP_IF_FALSE alt jump if false
3777 * EVAL expr1a
3778 * JUMP_ALWAYS end
3779 * alt: EVAL expr1b
3780 * end:
3781 */
3782 static int
3783compile_expr1(char_u **arg, cctx_T *cctx)
3784{
3785 char_u *p;
3786
3787 // evaluate the first expression
3788 if (compile_expr2(arg, cctx) == FAIL)
3789 return FAIL;
3790
3791 p = skipwhite(*arg);
3792 if (*p == '?')
3793 {
3794 garray_T *instr = &cctx->ctx_instr;
3795 garray_T *stack = &cctx->ctx_type_stack;
3796 int alt_idx = instr->ga_len;
3797 int end_idx;
3798 isn_T *isn;
3799 type_T *type1;
3800 type_T *type2;
3801
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003802 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3803 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003805 return FAIL;
3806 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
3808 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3809
3810 // evaluate the second expression; any type is accepted
3811 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003812 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003813 return FAIL;
3814
Bram Moolenaara6d53682020-01-28 23:04:06 +01003815 if (compile_expr1(arg, cctx) == FAIL)
3816 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817
3818 // remember the type and drop it
3819 --stack->ga_len;
3820 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3821
3822 end_idx = instr->ga_len;
3823 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3824
3825 // jump here from JUMP_IF_FALSE
3826 isn = ((isn_T *)instr->ga_data) + alt_idx;
3827 isn->isn_arg.jump.jump_where = instr->ga_len;
3828
3829 // Check for the ":".
3830 p = skipwhite(*arg);
3831 if (*p != ':')
3832 {
3833 emsg(_(e_missing_colon));
3834 return FAIL;
3835 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003836 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3837 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003839 return FAIL;
3840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841
3842 // evaluate the third expression
3843 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003844 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003845 return FAIL;
3846
Bram Moolenaara6d53682020-01-28 23:04:06 +01003847 if (compile_expr1(arg, cctx) == FAIL)
3848 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849
3850 // If the types differ, the result has a more generic type.
3851 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003852 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853
3854 // jump here from JUMP_ALWAYS
3855 isn = ((isn_T *)instr->ga_data) + end_idx;
3856 isn->isn_arg.jump.jump_where = instr->ga_len;
3857 }
3858 return OK;
3859}
3860
3861/*
3862 * compile "return [expr]"
3863 */
3864 static char_u *
3865compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3866{
3867 char_u *p = arg;
3868 garray_T *stack = &cctx->ctx_type_stack;
3869 type_T *stack_type;
3870
3871 if (*p != NUL && *p != '|' && *p != '\n')
3872 {
3873 // compile return argument into instructions
3874 if (compile_expr1(&p, cctx) == FAIL)
3875 return NULL;
3876
3877 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3878 if (set_return_type)
3879 cctx->ctx_ufunc->uf_ret_type = stack_type;
3880 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3881 == FAIL)
3882 return NULL;
3883 }
3884 else
3885 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003886 // "set_return_type" cannot be TRUE, only used for a lambda which
3887 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02003888 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
3889 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 {
3891 emsg(_("E1003: Missing return value"));
3892 return NULL;
3893 }
3894
3895 // No argument, return zero.
3896 generate_PUSHNR(cctx, 0);
3897 }
3898
3899 if (generate_instr(cctx, ISN_RETURN) == NULL)
3900 return NULL;
3901
3902 // "return val | endif" is possible
3903 return skipwhite(p);
3904}
3905
3906/*
3907 * Return the length of an assignment operator, or zero if there isn't one.
3908 */
3909 int
3910assignment_len(char_u *p, int *heredoc)
3911{
3912 if (*p == '=')
3913 {
3914 if (p[1] == '<' && p[2] == '<')
3915 {
3916 *heredoc = TRUE;
3917 return 3;
3918 }
3919 return 1;
3920 }
3921 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3922 return 2;
3923 if (STRNCMP(p, "..=", 3) == 0)
3924 return 3;
3925 return 0;
3926}
3927
3928// words that cannot be used as a variable
3929static char *reserved[] = {
3930 "true",
3931 "false",
3932 NULL
3933};
3934
3935/*
3936 * Get a line for "=<<".
3937 * Return a pointer to the line in allocated memory.
3938 * Return NULL for end-of-file or some error.
3939 */
3940 static char_u *
3941heredoc_getline(
3942 int c UNUSED,
3943 void *cookie,
3944 int indent UNUSED,
3945 int do_concat UNUSED)
3946{
3947 cctx_T *cctx = (cctx_T *)cookie;
3948
3949 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003950 {
3951 iemsg("Heredoc got to end");
3952 return NULL;
3953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 ++cctx->ctx_lnum;
3955 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3956 [cctx->ctx_lnum]);
3957}
3958
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003959typedef enum {
3960 dest_local,
3961 dest_option,
3962 dest_env,
3963 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02003964 dest_buffer,
3965 dest_window,
3966 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003967 dest_vimvar,
3968 dest_script,
3969 dest_reg,
3970} assign_dest_T;
3971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972/*
3973 * compile "let var [= expr]", "const var = expr" and "var = expr"
3974 * "arg" points to "var".
3975 */
3976 static char_u *
3977compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3978{
3979 char_u *p;
3980 char_u *ret = NULL;
3981 int var_count = 0;
3982 int semicolon = 0;
3983 size_t varlen;
3984 garray_T *instr = &cctx->ctx_instr;
3985 int idx = -1;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003986 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003989 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003991 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 int oplen = 0;
3993 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003994 type_T *type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 lvar_T *lvar;
3996 char_u *name;
3997 char_u *sp;
3998 int has_type = FALSE;
3999 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4000 int instr_count = -1;
4001
4002 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4003 if (p == NULL)
4004 return NULL;
4005 if (var_count > 0)
4006 {
4007 // TODO: let [var, var] = list
4008 emsg("Cannot handle a list yet");
4009 return NULL;
4010 }
4011
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004012 // "a: type" is declaring variable "a" with a type, not "a:".
4013 if (is_decl && p == arg + 2 && p[-1] == ':')
4014 --p;
4015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 varlen = p - arg;
4017 name = vim_strnsave(arg, (int)varlen);
4018 if (name == NULL)
4019 return NULL;
4020
Bram Moolenaar080457c2020-03-03 21:53:32 +01004021 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004023 if (*arg == '&')
4024 {
4025 int cc;
4026 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027
Bram Moolenaar080457c2020-03-03 21:53:32 +01004028 dest = dest_option;
4029 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004031 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004032 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 if (is_decl)
4035 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004036 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 goto theend;
4038 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004039 p = arg;
4040 p = find_option_end(&p, &opt_flags);
4041 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004043 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004044 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004045 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004046 }
4047 cc = *p;
4048 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004049 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004050 *p = cc;
4051 if (opt_type == -3)
4052 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004053 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004054 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004055 }
4056 if (opt_type == -2 || opt_type == 0)
4057 type = &t_string;
4058 else
4059 type = &t_number; // both number and boolean option
4060 }
4061 else if (*arg == '$')
4062 {
4063 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004064 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004065 if (is_decl)
4066 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004067 semsg(_("E1065: Cannot declare an environment variable: %s"),
4068 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004069 goto theend;
4070 }
4071 }
4072 else if (*arg == '@')
4073 {
4074 if (!valid_yank_reg(arg[1], TRUE))
4075 {
4076 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004077 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004078 }
4079 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004080 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004081 if (is_decl)
4082 {
4083 semsg(_("E1066: Cannot declare a register: %s"), name);
4084 goto theend;
4085 }
4086 }
4087 else if (STRNCMP(arg, "g:", 2) == 0)
4088 {
4089 dest = dest_global;
4090 if (is_decl)
4091 {
4092 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4093 goto theend;
4094 }
4095 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004096 else if (STRNCMP(arg, "b:", 2) == 0)
4097 {
4098 dest = dest_buffer;
4099 if (is_decl)
4100 {
4101 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4102 goto theend;
4103 }
4104 }
4105 else if (STRNCMP(arg, "w:", 2) == 0)
4106 {
4107 dest = dest_window;
4108 if (is_decl)
4109 {
4110 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4111 goto theend;
4112 }
4113 }
4114 else if (STRNCMP(arg, "t:", 2) == 0)
4115 {
4116 dest = dest_tab;
4117 if (is_decl)
4118 {
4119 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4120 goto theend;
4121 }
4122 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004123 else if (STRNCMP(arg, "v:", 2) == 0)
4124 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004125 typval_T *vtv;
4126 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004127
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004128 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004129 if (vimvaridx < 0)
4130 {
4131 semsg(_(e_var_notfound), arg);
4132 goto theend;
4133 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004134 // We use the current value of "sandbox" here, is that OK?
4135 if (var_check_ro(di_flags, name, FALSE))
4136 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004137 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004138 vtv = get_vim_var_tv(vimvaridx);
4139 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004140 if (is_decl)
4141 {
4142 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4143 goto theend;
4144 }
4145 }
4146 else
4147 {
4148 for (idx = 0; reserved[idx] != NULL; ++idx)
4149 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004151 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 goto theend;
4153 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004154
4155 idx = lookup_local(arg, varlen, cctx);
4156 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004158 if (is_decl)
4159 {
4160 semsg(_("E1017: Variable already declared: %s"), name);
4161 goto theend;
4162 }
4163 else
4164 {
4165 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
4166 if (lvar->lv_const)
4167 {
4168 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4169 goto theend;
4170 }
4171 }
4172 }
4173 else if (STRNCMP(arg, "s:", 2) == 0
4174 || lookup_script(arg, varlen) == OK
4175 || find_imported(arg, varlen, cctx) != NULL)
4176 {
4177 dest = dest_script;
4178 if (is_decl)
4179 {
4180 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004181 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004182 goto theend;
4183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 }
4185 }
4186 }
4187
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004188 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 {
4190 if (is_decl && *p == ':')
4191 {
4192 // parse optional type: "let var: type = expr"
4193 p = skipwhite(p + 1);
4194 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 has_type = TRUE;
4196 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02004197 else if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 {
4199 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
4200 type = lvar->lv_type;
4201 }
4202 }
4203
4204 sp = p;
4205 p = skipwhite(p);
4206 op = p;
4207 oplen = assignment_len(p, &heredoc);
4208 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4209 {
4210 char_u buf[4];
4211
4212 vim_strncpy(buf, op, oplen);
4213 semsg(_(e_white_both), buf);
4214 }
4215
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004216 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004217 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004219 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 goto theend;
4221 }
4222
Bram Moolenaar080457c2020-03-03 21:53:32 +01004223 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 {
4225 if (oplen > 1 && !heredoc)
4226 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004227 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4229 name);
4230 goto theend;
4231 }
4232
4233 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004234 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004235 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4237 if (idx < 0)
4238 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004239 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 }
4241
4242 if (heredoc)
4243 {
4244 list_T *l;
4245 listitem_T *li;
4246
4247 // [let] varname =<< [trim] {end}
4248 eap->getline = heredoc_getline;
4249 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004250 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251
4252 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004253 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 {
4255 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4256 li->li_tv.vval.v_string = NULL;
4257 }
4258 generate_NEWLIST(cctx, l->lv_len);
4259 type = &t_list_string;
4260 list_free(l);
4261 p += STRLEN(p);
4262 }
4263 else if (oplen > 0)
4264 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004265 int r;
4266 type_T *stacktype;
4267 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 // for "+=", "*=", "..=" etc. first load the current value
4270 if (*op != '=')
4271 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004272 switch (dest)
4273 {
4274 case dest_option:
4275 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004276 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004277 break;
4278 case dest_global:
4279 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4280 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004281 case dest_buffer:
4282 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4283 break;
4284 case dest_window:
4285 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4286 break;
4287 case dest_tab:
4288 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4289 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004290 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004291 compile_load_scriptvar(cctx,
4292 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004293 break;
4294 case dest_env:
4295 // Include $ in the name here
4296 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4297 break;
4298 case dest_reg:
4299 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4300 break;
4301 case dest_vimvar:
4302 generate_LOADV(cctx, name + 2, TRUE);
4303 break;
4304 case dest_local:
4305 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
4306 break;
4307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 }
4309
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004310 // Compile the expression. Temporarily hide the new local variable
4311 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004312 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004313 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 instr_count = instr->ga_len;
4315 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004316 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004317 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004318 ++cctx->ctx_locals.ga_len;
4319 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 goto theend;
4321
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004322 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004324 stack = &cctx->ctx_type_stack;
4325 stacktype = stack->ga_len == 0 ? &t_void
4326 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
4327 if (idx >= 0 && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004329 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
4330 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004332 if (stacktype->tt_type == VAR_VOID)
4333 {
4334 emsg(_("E1031: Cannot use void value"));
4335 goto theend;
4336 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004337 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004338 {
4339 // An empty list or dict has a &t_void member, for a
4340 // variable that implies &t_any.
4341 if (stacktype == &t_list_empty)
4342 lvar->lv_type = &t_list_any;
4343 else if (stacktype == &t_dict_empty)
4344 lvar->lv_type = &t_dict_any;
4345 else
4346 lvar->lv_type = stacktype;
4347 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004348 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004349 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4350 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004352 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004353 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 }
4355 }
4356 else if (cmdidx == CMD_const)
4357 {
4358 emsg(_("E1021: const requires a value"));
4359 goto theend;
4360 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004361 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 {
4363 emsg(_("E1022: type or initialization required"));
4364 goto theend;
4365 }
4366 else
4367 {
4368 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 if (ga_grow(instr, 1) == FAIL)
4370 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004371 switch (type->tt_type)
4372 {
4373 case VAR_BOOL:
4374 generate_PUSHBOOL(cctx, VVAL_FALSE);
4375 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004376 case VAR_FLOAT:
4377#ifdef FEAT_FLOAT
4378 generate_PUSHF(cctx, 0.0);
4379#endif
4380 break;
4381 case VAR_STRING:
4382 generate_PUSHS(cctx, NULL);
4383 break;
4384 case VAR_BLOB:
4385 generate_PUSHBLOB(cctx, NULL);
4386 break;
4387 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004388 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004389 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004390 case VAR_LIST:
4391 generate_NEWLIST(cctx, 0);
4392 break;
4393 case VAR_DICT:
4394 generate_NEWDICT(cctx, 0);
4395 break;
4396 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004397 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004398 break;
4399 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004400 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004401 break;
4402 case VAR_NUMBER:
4403 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004404 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02004405 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004406 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004407 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004408 generate_PUSHNR(cctx, 0);
4409 break;
4410 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 }
4412
4413 if (oplen > 0 && *op != '=')
4414 {
4415 type_T *expected = &t_number;
4416 garray_T *stack = &cctx->ctx_type_stack;
4417 type_T *stacktype;
4418
4419 // TODO: if type is known use float or any operation
4420
4421 if (*op == '.')
4422 expected = &t_string;
4423 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4424 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4425 goto theend;
4426
4427 if (*op == '.')
4428 generate_instr_drop(cctx, ISN_CONCAT, 1);
4429 else
4430 {
4431 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4432
4433 if (isn == NULL)
4434 goto theend;
4435 switch (*op)
4436 {
4437 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4438 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4439 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4440 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4441 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4442 }
4443 }
4444 }
4445
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004446 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004448 case dest_option:
4449 generate_STOREOPT(cctx, name + 1, opt_flags);
4450 break;
4451 case dest_global:
4452 // include g: with the name, easier to execute that way
4453 generate_STORE(cctx, ISN_STOREG, 0, name);
4454 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004455 case dest_buffer:
4456 // include b: with the name, easier to execute that way
4457 generate_STORE(cctx, ISN_STOREB, 0, name);
4458 break;
4459 case dest_window:
4460 // include w: with the name, easier to execute that way
4461 generate_STORE(cctx, ISN_STOREW, 0, name);
4462 break;
4463 case dest_tab:
4464 // include t: with the name, easier to execute that way
4465 generate_STORE(cctx, ISN_STORET, 0, name);
4466 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004467 case dest_env:
4468 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4469 break;
4470 case dest_reg:
4471 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4472 break;
4473 case dest_vimvar:
4474 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4475 break;
4476 case dest_script:
4477 {
4478 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4479 imported_T *import = NULL;
4480 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004481
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004482 if (name[1] != ':')
4483 {
4484 import = find_imported(name, 0, cctx);
4485 if (import != NULL)
4486 sid = import->imp_sid;
4487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004489 idx = get_script_item_idx(sid, rawname, TRUE);
4490 // TODO: specific type
4491 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004492 {
4493 char_u *name_s = name;
4494
4495 // Include s: in the name for store_var()
4496 if (name[1] != ':')
4497 {
4498 int len = (int)STRLEN(name) + 3;
4499
4500 name_s = alloc(len);
4501 if (name_s == NULL)
4502 name_s = name;
4503 else
4504 vim_snprintf((char *)name_s, len, "s:%s", name);
4505 }
4506 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4507 if (name_s != name)
4508 vim_free(name_s);
4509 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004510 else
4511 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4512 sid, idx, &t_any);
4513 }
4514 break;
4515 case dest_local:
4516 {
4517 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004519 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4520 // into ISN_STORENR
4521 if (instr->ga_len == instr_count + 1
4522 && isn->isn_type == ISN_PUSHNR)
4523 {
4524 varnumber_T val = isn->isn_arg.number;
4525 garray_T *stack = &cctx->ctx_type_stack;
4526
4527 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004528 isn->isn_arg.storenr.stnr_idx = idx;
4529 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004530 if (stack->ga_len > 0)
4531 --stack->ga_len;
4532 }
4533 else
4534 generate_STORE(cctx, ISN_STORE, idx, NULL);
4535 }
4536 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 }
4538 ret = p;
4539
4540theend:
4541 vim_free(name);
4542 return ret;
4543}
4544
4545/*
4546 * Compile an :import command.
4547 */
4548 static char_u *
4549compile_import(char_u *arg, cctx_T *cctx)
4550{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004551 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552}
4553
4554/*
4555 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4556 */
4557 static int
4558compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4559{
4560 garray_T *instr = &cctx->ctx_instr;
4561 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4562
4563 if (endlabel == NULL)
4564 return FAIL;
4565 endlabel->el_next = *el;
4566 *el = endlabel;
4567 endlabel->el_end_label = instr->ga_len;
4568
4569 generate_JUMP(cctx, when, 0);
4570 return OK;
4571}
4572
4573 static void
4574compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4575{
4576 garray_T *instr = &cctx->ctx_instr;
4577
4578 while (*el != NULL)
4579 {
4580 endlabel_T *cur = (*el);
4581 isn_T *isn;
4582
4583 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4584 isn->isn_arg.jump.jump_where = instr->ga_len;
4585 *el = cur->el_next;
4586 vim_free(cur);
4587 }
4588}
4589
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004590 static void
4591compile_free_jump_to_end(endlabel_T **el)
4592{
4593 while (*el != NULL)
4594 {
4595 endlabel_T *cur = (*el);
4596
4597 *el = cur->el_next;
4598 vim_free(cur);
4599 }
4600}
4601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602/*
4603 * Create a new scope and set up the generic items.
4604 */
4605 static scope_T *
4606new_scope(cctx_T *cctx, scopetype_T type)
4607{
4608 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4609
4610 if (scope == NULL)
4611 return NULL;
4612 scope->se_outer = cctx->ctx_scope;
4613 cctx->ctx_scope = scope;
4614 scope->se_type = type;
4615 scope->se_local_count = cctx->ctx_locals.ga_len;
4616 return scope;
4617}
4618
4619/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004620 * Free the current scope and go back to the outer scope.
4621 */
4622 static void
4623drop_scope(cctx_T *cctx)
4624{
4625 scope_T *scope = cctx->ctx_scope;
4626
4627 if (scope == NULL)
4628 {
4629 iemsg("calling drop_scope() without a scope");
4630 return;
4631 }
4632 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004633 switch (scope->se_type)
4634 {
4635 case IF_SCOPE:
4636 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4637 case FOR_SCOPE:
4638 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4639 case WHILE_SCOPE:
4640 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4641 case TRY_SCOPE:
4642 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4643 case NO_SCOPE:
4644 case BLOCK_SCOPE:
4645 break;
4646 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004647 vim_free(scope);
4648}
4649
4650/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004651 * Evaluate an expression that is a constant:
4652 * has(arg)
4653 *
4654 * Also handle:
4655 * ! in front logical NOT
4656 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004657 * Return FAIL if the expression is not a constant.
4658 */
4659 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004660evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004661{
4662 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004663 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004664 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004665
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004666 /*
4667 * Skip '!' characters. They are handled later.
4668 */
4669 start_leader = *arg;
4670 while (**arg == '!')
4671 *arg = skipwhite(*arg + 1);
4672 end_leader = *arg;
4673
4674 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004675 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004676 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004677 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4678 {
4679 tv->v_type = VAR_SPECIAL;
4680 tv->vval.v_number = VVAL_TRUE;
4681 *arg += 4;
4682 return OK;
4683 }
4684 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4685 {
4686 tv->v_type = VAR_SPECIAL;
4687 tv->vval.v_number = VVAL_FALSE;
4688 *arg += 5;
4689 return OK;
4690 }
4691
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004692 if (STRNCMP("has(", *arg, 4) == 0)
4693 {
4694 has_call = TRUE;
4695 *arg = skipwhite(*arg + 4);
4696 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004697
4698 if (**arg == '"')
4699 {
4700 if (get_string_tv(arg, tv, TRUE) == FAIL)
4701 return FAIL;
4702 }
4703 else if (**arg == '\'')
4704 {
4705 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4706 return FAIL;
4707 }
4708 else
4709 return FAIL;
4710
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004711 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004712 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004713 *arg = skipwhite(*arg);
4714 if (**arg != ')')
4715 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004716 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004717
4718 argvars[0] = *tv;
4719 argvars[1].v_type = VAR_UNKNOWN;
4720 tv->v_type = VAR_NUMBER;
4721 tv->vval.v_number = 0;
4722 f_has(argvars, tv);
4723 clear_tv(&argvars[0]);
4724
4725 while (start_leader < end_leader)
4726 {
4727 if (*start_leader == '!')
4728 tv->vval.v_number = !tv->vval.v_number;
4729 ++start_leader;
4730 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004731 }
4732
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004733 return OK;
4734}
4735
Bram Moolenaar080457c2020-03-03 21:53:32 +01004736 static int
4737evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4738{
4739 exptype_T type = EXPR_UNKNOWN;
4740 char_u *p;
4741 int len = 2;
4742 int type_is = FALSE;
4743
4744 // get the first variable
4745 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4746 return FAIL;
4747
4748 p = skipwhite(*arg);
4749 type = get_compare_type(p, &len, &type_is);
4750
4751 /*
4752 * If there is a comparative operator, use it.
4753 */
4754 if (type != EXPR_UNKNOWN)
4755 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004756 typval_T tv2;
4757 char_u *s1, *s2;
4758 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4759 int n;
4760
4761 // TODO: Only string == string is supported now
4762 if (tv->v_type != VAR_STRING)
4763 return FAIL;
4764 if (type != EXPR_EQUAL)
4765 return FAIL;
4766
4767 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004768 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004769 *arg = skipwhite(p + len);
4770 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4771 || tv2.v_type != VAR_STRING)
4772 {
4773 clear_tv(&tv2);
4774 return FAIL;
4775 }
4776 s1 = tv_get_string_buf(tv, buf1);
4777 s2 = tv_get_string_buf(&tv2, buf2);
4778 n = STRCMP(s1, s2);
4779 clear_tv(tv);
4780 clear_tv(&tv2);
4781 tv->v_type = VAR_BOOL;
4782 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004783 }
4784
4785 return OK;
4786}
4787
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004788static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4789
4790/*
4791 * Compile constant || or &&.
4792 */
4793 static int
4794evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4795{
4796 char_u *p = skipwhite(*arg);
4797 int opchar = *op;
4798
4799 if (p[0] == opchar && p[1] == opchar)
4800 {
4801 int val = tv2bool(tv);
4802
4803 /*
4804 * Repeat until there is no following "||" or "&&"
4805 */
4806 while (p[0] == opchar && p[1] == opchar)
4807 {
4808 typval_T tv2;
4809
4810 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4811 return FAIL;
4812
4813 // eval the next expression
4814 *arg = skipwhite(p + 2);
4815 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004816 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004817 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004818 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004819 {
4820 clear_tv(&tv2);
4821 return FAIL;
4822 }
4823 if ((opchar == '&') == val)
4824 {
4825 // false || tv2 or true && tv2: use tv2
4826 clear_tv(tv);
4827 *tv = tv2;
4828 val = tv2bool(tv);
4829 }
4830 else
4831 clear_tv(&tv2);
4832 p = skipwhite(*arg);
4833 }
4834 }
4835
4836 return OK;
4837}
4838
4839/*
4840 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4841 * Return FAIL if the expression is not a constant.
4842 */
4843 static int
4844evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4845{
4846 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004847 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004848 return FAIL;
4849
4850 // || and && work almost the same
4851 return evaluate_const_and_or(arg, cctx, "&&", tv);
4852}
4853
4854/*
4855 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4856 * Return FAIL if the expression is not a constant.
4857 */
4858 static int
4859evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4860{
4861 // evaluate the first expression
4862 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4863 return FAIL;
4864
4865 // || and && work almost the same
4866 return evaluate_const_and_or(arg, cctx, "||", tv);
4867}
4868
4869/*
4870 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4871 * E.g. for "has('feature')".
4872 * This does not produce error messages. "tv" should be cleared afterwards.
4873 * Return FAIL if the expression is not a constant.
4874 */
4875 static int
4876evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4877{
4878 char_u *p;
4879
4880 // evaluate the first expression
4881 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4882 return FAIL;
4883
4884 p = skipwhite(*arg);
4885 if (*p == '?')
4886 {
4887 int val = tv2bool(tv);
4888 typval_T tv2;
4889
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004890 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004891 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4892 return FAIL;
4893
4894 // evaluate the second expression; any type is accepted
4895 clear_tv(tv);
4896 *arg = skipwhite(p + 1);
4897 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4898 return FAIL;
4899
4900 // Check for the ":".
4901 p = skipwhite(*arg);
4902 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4903 return FAIL;
4904
4905 // evaluate the third expression
4906 *arg = skipwhite(p + 1);
4907 tv2.v_type = VAR_UNKNOWN;
4908 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4909 {
4910 clear_tv(&tv2);
4911 return FAIL;
4912 }
4913 if (val)
4914 {
4915 // use the expr after "?"
4916 clear_tv(&tv2);
4917 }
4918 else
4919 {
4920 // use the expr after ":"
4921 clear_tv(tv);
4922 *tv = tv2;
4923 }
4924 }
4925 return OK;
4926}
4927
4928/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929 * compile "if expr"
4930 *
4931 * "if expr" Produces instructions:
4932 * EVAL expr Push result of "expr"
4933 * JUMP_IF_FALSE end
4934 * ... body ...
4935 * end:
4936 *
4937 * "if expr | else" Produces instructions:
4938 * EVAL expr Push result of "expr"
4939 * JUMP_IF_FALSE else
4940 * ... body ...
4941 * JUMP_ALWAYS end
4942 * else:
4943 * ... body ...
4944 * end:
4945 *
4946 * "if expr1 | elseif expr2 | else" Produces instructions:
4947 * EVAL expr Push result of "expr"
4948 * JUMP_IF_FALSE elseif
4949 * ... body ...
4950 * JUMP_ALWAYS end
4951 * elseif:
4952 * EVAL expr Push result of "expr"
4953 * JUMP_IF_FALSE else
4954 * ... body ...
4955 * JUMP_ALWAYS end
4956 * else:
4957 * ... body ...
4958 * end:
4959 */
4960 static char_u *
4961compile_if(char_u *arg, cctx_T *cctx)
4962{
4963 char_u *p = arg;
4964 garray_T *instr = &cctx->ctx_instr;
4965 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004966 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004968 // compile "expr"; if we know it evaluates to FALSE skip the block
4969 tv.v_type = VAR_UNKNOWN;
4970 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4971 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4972 else
4973 cctx->ctx_skip = MAYBE;
4974 clear_tv(&tv);
4975 if (cctx->ctx_skip == MAYBE)
4976 {
4977 p = arg;
4978 if (compile_expr1(&p, cctx) == FAIL)
4979 return NULL;
4980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981
4982 scope = new_scope(cctx, IF_SCOPE);
4983 if (scope == NULL)
4984 return NULL;
4985
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004986 if (cctx->ctx_skip == MAYBE)
4987 {
4988 // "where" is set when ":elseif", "else" or ":endif" is found
4989 scope->se_u.se_if.is_if_label = instr->ga_len;
4990 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4991 }
4992 else
4993 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994
4995 return p;
4996}
4997
4998 static char_u *
4999compile_elseif(char_u *arg, cctx_T *cctx)
5000{
5001 char_u *p = arg;
5002 garray_T *instr = &cctx->ctx_instr;
5003 isn_T *isn;
5004 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005005 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006
5007 if (scope == NULL || scope->se_type != IF_SCOPE)
5008 {
5009 emsg(_(e_elseif_without_if));
5010 return NULL;
5011 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005012 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013
Bram Moolenaar158906c2020-02-06 20:39:45 +01005014 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005015 {
5016 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005018 return NULL;
5019 // previous "if" or "elseif" jumps here
5020 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5021 isn->isn_arg.jump.jump_where = instr->ga_len;
5022 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005024 // compile "expr"; if we know it evaluates to FALSE skip the block
5025 tv.v_type = VAR_UNKNOWN;
5026 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5027 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5028 else
5029 cctx->ctx_skip = MAYBE;
5030 clear_tv(&tv);
5031 if (cctx->ctx_skip == MAYBE)
5032 {
5033 p = arg;
5034 if (compile_expr1(&p, cctx) == FAIL)
5035 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005037 // "where" is set when ":elseif", "else" or ":endif" is found
5038 scope->se_u.se_if.is_if_label = instr->ga_len;
5039 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5040 }
5041 else
5042 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043
5044 return p;
5045}
5046
5047 static char_u *
5048compile_else(char_u *arg, cctx_T *cctx)
5049{
5050 char_u *p = arg;
5051 garray_T *instr = &cctx->ctx_instr;
5052 isn_T *isn;
5053 scope_T *scope = cctx->ctx_scope;
5054
5055 if (scope == NULL || scope->se_type != IF_SCOPE)
5056 {
5057 emsg(_(e_else_without_if));
5058 return NULL;
5059 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005060 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005062 // jump from previous block to the end, unless the else block is empty
5063 if (cctx->ctx_skip == MAYBE)
5064 {
5065 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005067 return NULL;
5068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069
Bram Moolenaar158906c2020-02-06 20:39:45 +01005070 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005071 {
5072 if (scope->se_u.se_if.is_if_label >= 0)
5073 {
5074 // previous "if" or "elseif" jumps here
5075 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5076 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005077 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005078 }
5079 }
5080
5081 if (cctx->ctx_skip != MAYBE)
5082 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083
5084 return p;
5085}
5086
5087 static char_u *
5088compile_endif(char_u *arg, cctx_T *cctx)
5089{
5090 scope_T *scope = cctx->ctx_scope;
5091 ifscope_T *ifscope;
5092 garray_T *instr = &cctx->ctx_instr;
5093 isn_T *isn;
5094
5095 if (scope == NULL || scope->se_type != IF_SCOPE)
5096 {
5097 emsg(_(e_endif_without_if));
5098 return NULL;
5099 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005100 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005101 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005103 if (scope->se_u.se_if.is_if_label >= 0)
5104 {
5105 // previous "if" or "elseif" jumps here
5106 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5107 isn->isn_arg.jump.jump_where = instr->ga_len;
5108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 // Fill in the "end" label in jumps at the end of the blocks.
5110 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005111 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005113 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 return arg;
5115}
5116
5117/*
5118 * compile "for var in expr"
5119 *
5120 * Produces instructions:
5121 * PUSHNR -1
5122 * STORE loop-idx Set index to -1
5123 * EVAL expr Push result of "expr"
5124 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5125 * - if beyond end, jump to "end"
5126 * - otherwise get item from list and push it
5127 * STORE var Store item in "var"
5128 * ... body ...
5129 * JUMP top Jump back to repeat
5130 * end: DROP Drop the result of "expr"
5131 *
5132 */
5133 static char_u *
5134compile_for(char_u *arg, cctx_T *cctx)
5135{
5136 char_u *p;
5137 size_t varlen;
5138 garray_T *instr = &cctx->ctx_instr;
5139 garray_T *stack = &cctx->ctx_type_stack;
5140 scope_T *scope;
5141 int loop_idx; // index of loop iteration variable
5142 int var_idx; // index of "var"
5143 type_T *vartype;
5144
5145 // TODO: list of variables: "for [key, value] in dict"
5146 // parse "var"
5147 for (p = arg; eval_isnamec1(*p); ++p)
5148 ;
5149 varlen = p - arg;
5150 var_idx = lookup_local(arg, varlen, cctx);
5151 if (var_idx >= 0)
5152 {
5153 semsg(_("E1023: variable already defined: %s"), arg);
5154 return NULL;
5155 }
5156
5157 // consume "in"
5158 p = skipwhite(p);
5159 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5160 {
5161 emsg(_(e_missing_in));
5162 return NULL;
5163 }
5164 p = skipwhite(p + 2);
5165
5166
5167 scope = new_scope(cctx, FOR_SCOPE);
5168 if (scope == NULL)
5169 return NULL;
5170
5171 // Reserve a variable to store the loop iteration counter.
5172 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5173 if (loop_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005174 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005175 // only happens when out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005176 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179
5180 // Reserve a variable to store "var"
5181 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5182 if (var_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005183 {
5184 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005185 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005187
5188 generate_STORENR(cctx, loop_idx, -1);
5189
5190 // compile "expr", it remains on the stack until "endfor"
5191 arg = p;
5192 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005193 {
5194 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197
5198 // now we know the type of "var"
5199 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5200 if (vartype->tt_type != VAR_LIST)
5201 {
5202 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005203 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 return NULL;
5205 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005206 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 {
5208 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
5209
5210 lvar->lv_type = vartype->tt_member;
5211 }
5212
5213 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005214 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215
5216 generate_FOR(cctx, loop_idx);
5217 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
5218
5219 return arg;
5220}
5221
5222/*
5223 * compile "endfor"
5224 */
5225 static char_u *
5226compile_endfor(char_u *arg, cctx_T *cctx)
5227{
5228 garray_T *instr = &cctx->ctx_instr;
5229 scope_T *scope = cctx->ctx_scope;
5230 forscope_T *forscope;
5231 isn_T *isn;
5232
5233 if (scope == NULL || scope->se_type != FOR_SCOPE)
5234 {
5235 emsg(_(e_for));
5236 return NULL;
5237 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005238 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005240 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241
5242 // At end of ":for" scope jump back to the FOR instruction.
5243 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5244
5245 // Fill in the "end" label in the FOR statement so it can jump here
5246 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5247 isn->isn_arg.forloop.for_end = instr->ga_len;
5248
5249 // Fill in the "end" label any BREAK statements
5250 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5251
5252 // Below the ":for" scope drop the "expr" list from the stack.
5253 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5254 return NULL;
5255
5256 vim_free(scope);
5257
5258 return arg;
5259}
5260
5261/*
5262 * compile "while expr"
5263 *
5264 * Produces instructions:
5265 * top: EVAL expr Push result of "expr"
5266 * JUMP_IF_FALSE end jump if false
5267 * ... body ...
5268 * JUMP top Jump back to repeat
5269 * end:
5270 *
5271 */
5272 static char_u *
5273compile_while(char_u *arg, cctx_T *cctx)
5274{
5275 char_u *p = arg;
5276 garray_T *instr = &cctx->ctx_instr;
5277 scope_T *scope;
5278
5279 scope = new_scope(cctx, WHILE_SCOPE);
5280 if (scope == NULL)
5281 return NULL;
5282
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005283 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284
5285 // compile "expr"
5286 if (compile_expr1(&p, cctx) == FAIL)
5287 return NULL;
5288
5289 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005290 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291 JUMP_IF_FALSE, cctx) == FAIL)
5292 return FAIL;
5293
5294 return p;
5295}
5296
5297/*
5298 * compile "endwhile"
5299 */
5300 static char_u *
5301compile_endwhile(char_u *arg, cctx_T *cctx)
5302{
5303 scope_T *scope = cctx->ctx_scope;
5304
5305 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5306 {
5307 emsg(_(e_while));
5308 return NULL;
5309 }
5310 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005311 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312
5313 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005314 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315
5316 // Fill in the "end" label in the WHILE statement so it can jump here.
5317 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005318 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005319
5320 vim_free(scope);
5321
5322 return arg;
5323}
5324
5325/*
5326 * compile "continue"
5327 */
5328 static char_u *
5329compile_continue(char_u *arg, cctx_T *cctx)
5330{
5331 scope_T *scope = cctx->ctx_scope;
5332
5333 for (;;)
5334 {
5335 if (scope == NULL)
5336 {
5337 emsg(_(e_continue));
5338 return NULL;
5339 }
5340 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5341 break;
5342 scope = scope->se_outer;
5343 }
5344
5345 // Jump back to the FOR or WHILE instruction.
5346 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005347 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5348 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005349 return arg;
5350}
5351
5352/*
5353 * compile "break"
5354 */
5355 static char_u *
5356compile_break(char_u *arg, cctx_T *cctx)
5357{
5358 scope_T *scope = cctx->ctx_scope;
5359 endlabel_T **el;
5360
5361 for (;;)
5362 {
5363 if (scope == NULL)
5364 {
5365 emsg(_(e_break));
5366 return NULL;
5367 }
5368 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5369 break;
5370 scope = scope->se_outer;
5371 }
5372
5373 // Jump to the end of the FOR or WHILE loop.
5374 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005375 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005376 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005377 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5379 return FAIL;
5380
5381 return arg;
5382}
5383
5384/*
5385 * compile "{" start of block
5386 */
5387 static char_u *
5388compile_block(char_u *arg, cctx_T *cctx)
5389{
5390 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5391 return NULL;
5392 return skipwhite(arg + 1);
5393}
5394
5395/*
5396 * compile end of block: drop one scope
5397 */
5398 static void
5399compile_endblock(cctx_T *cctx)
5400{
5401 scope_T *scope = cctx->ctx_scope;
5402
5403 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005404 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405 vim_free(scope);
5406}
5407
5408/*
5409 * compile "try"
5410 * Creates a new scope for the try-endtry, pointing to the first catch and
5411 * finally.
5412 * Creates another scope for the "try" block itself.
5413 * TRY instruction sets up exception handling at runtime.
5414 *
5415 * "try"
5416 * TRY -> catch1, -> finally push trystack entry
5417 * ... try block
5418 * "throw {exception}"
5419 * EVAL {exception}
5420 * THROW create exception
5421 * ... try block
5422 * " catch {expr}"
5423 * JUMP -> finally
5424 * catch1: PUSH exeception
5425 * EVAL {expr}
5426 * MATCH
5427 * JUMP nomatch -> catch2
5428 * CATCH remove exception
5429 * ... catch block
5430 * " catch"
5431 * JUMP -> finally
5432 * catch2: CATCH remove exception
5433 * ... catch block
5434 * " finally"
5435 * finally:
5436 * ... finally block
5437 * " endtry"
5438 * ENDTRY pop trystack entry, may rethrow
5439 */
5440 static char_u *
5441compile_try(char_u *arg, cctx_T *cctx)
5442{
5443 garray_T *instr = &cctx->ctx_instr;
5444 scope_T *try_scope;
5445 scope_T *scope;
5446
5447 // scope that holds the jumps that go to catch/finally/endtry
5448 try_scope = new_scope(cctx, TRY_SCOPE);
5449 if (try_scope == NULL)
5450 return NULL;
5451
5452 // "catch" is set when the first ":catch" is found.
5453 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005454 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455 if (generate_instr(cctx, ISN_TRY) == NULL)
5456 return NULL;
5457
5458 // scope for the try block itself
5459 scope = new_scope(cctx, BLOCK_SCOPE);
5460 if (scope == NULL)
5461 return NULL;
5462
5463 return arg;
5464}
5465
5466/*
5467 * compile "catch {expr}"
5468 */
5469 static char_u *
5470compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5471{
5472 scope_T *scope = cctx->ctx_scope;
5473 garray_T *instr = &cctx->ctx_instr;
5474 char_u *p;
5475 isn_T *isn;
5476
5477 // end block scope from :try or :catch
5478 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5479 compile_endblock(cctx);
5480 scope = cctx->ctx_scope;
5481
5482 // Error if not in a :try scope
5483 if (scope == NULL || scope->se_type != TRY_SCOPE)
5484 {
5485 emsg(_(e_catch));
5486 return NULL;
5487 }
5488
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005489 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490 {
5491 emsg(_("E1033: catch unreachable after catch-all"));
5492 return NULL;
5493 }
5494
5495 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005496 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 JUMP_ALWAYS, cctx) == FAIL)
5498 return NULL;
5499
5500 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005501 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502 if (isn->isn_arg.try.try_catch == 0)
5503 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005504 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005505 {
5506 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005507 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005508 isn->isn_arg.jump.jump_where = instr->ga_len;
5509 }
5510
5511 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005512 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005514 scope->se_u.se_try.ts_caught_all = TRUE;
5515 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516 }
5517 else
5518 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005519 char_u *end;
5520 char_u *pat;
5521 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005522 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005523 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525 // Push v:exception, push {expr} and MATCH
5526 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5527
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005528 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005529 if (*end != *p)
5530 {
5531 semsg(_("E1067: Separator mismatch: %s"), p);
5532 vim_free(tofree);
5533 return FAIL;
5534 }
5535 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005536 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005537 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005538 len = (int)(end - tofree);
5539 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005540 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005541 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005542 if (pat == NULL)
5543 return FAIL;
5544 if (generate_PUSHS(cctx, pat) == FAIL)
5545 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005547 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5548 return NULL;
5549
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005550 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005551 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5552 return NULL;
5553 }
5554
5555 if (generate_instr(cctx, ISN_CATCH) == NULL)
5556 return NULL;
5557
5558 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5559 return NULL;
5560 return p;
5561}
5562
5563 static char_u *
5564compile_finally(char_u *arg, cctx_T *cctx)
5565{
5566 scope_T *scope = cctx->ctx_scope;
5567 garray_T *instr = &cctx->ctx_instr;
5568 isn_T *isn;
5569
5570 // end block scope from :try or :catch
5571 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5572 compile_endblock(cctx);
5573 scope = cctx->ctx_scope;
5574
5575 // Error if not in a :try scope
5576 if (scope == NULL || scope->se_type != TRY_SCOPE)
5577 {
5578 emsg(_(e_finally));
5579 return NULL;
5580 }
5581
5582 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005583 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 if (isn->isn_arg.try.try_finally != 0)
5585 {
5586 emsg(_(e_finally_dup));
5587 return NULL;
5588 }
5589
5590 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005591 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592
Bram Moolenaar585fea72020-04-02 22:33:21 +02005593 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005594 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005595 {
5596 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005597 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005598 isn->isn_arg.jump.jump_where = instr->ga_len;
5599 }
5600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005601 // TODO: set index in ts_finally_label jumps
5602
5603 return arg;
5604}
5605
5606 static char_u *
5607compile_endtry(char_u *arg, cctx_T *cctx)
5608{
5609 scope_T *scope = cctx->ctx_scope;
5610 garray_T *instr = &cctx->ctx_instr;
5611 isn_T *isn;
5612
5613 // end block scope from :catch or :finally
5614 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5615 compile_endblock(cctx);
5616 scope = cctx->ctx_scope;
5617
5618 // Error if not in a :try scope
5619 if (scope == NULL || scope->se_type != TRY_SCOPE)
5620 {
5621 if (scope == NULL)
5622 emsg(_(e_no_endtry));
5623 else if (scope->se_type == WHILE_SCOPE)
5624 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005625 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005626 emsg(_(e_endfor));
5627 else
5628 emsg(_(e_endif));
5629 return NULL;
5630 }
5631
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005632 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5634 {
5635 emsg(_("E1032: missing :catch or :finally"));
5636 return NULL;
5637 }
5638
5639 // Fill in the "end" label in jumps at the end of the blocks, if not done
5640 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005641 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642
5643 // End :catch or :finally scope: set value in ISN_TRY instruction
5644 if (isn->isn_arg.try.try_finally == 0)
5645 isn->isn_arg.try.try_finally = instr->ga_len;
5646 compile_endblock(cctx);
5647
5648 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5649 return NULL;
5650 return arg;
5651}
5652
5653/*
5654 * compile "throw {expr}"
5655 */
5656 static char_u *
5657compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5658{
5659 char_u *p = skipwhite(arg);
5660
5661 if (ends_excmd(*p))
5662 {
5663 emsg(_(e_argreq));
5664 return NULL;
5665 }
5666 if (compile_expr1(&p, cctx) == FAIL)
5667 return NULL;
5668 if (may_generate_2STRING(-1, cctx) == FAIL)
5669 return NULL;
5670 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5671 return NULL;
5672
5673 return p;
5674}
5675
5676/*
5677 * compile "echo expr"
5678 */
5679 static char_u *
5680compile_echo(char_u *arg, int with_white, cctx_T *cctx)
5681{
5682 char_u *p = arg;
5683 int count = 0;
5684
Bram Moolenaarad39c092020-02-26 18:23:43 +01005685 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005686 {
5687 if (compile_expr1(&p, cctx) == FAIL)
5688 return NULL;
5689 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005690 p = skipwhite(p);
5691 if (ends_excmd(*p))
5692 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005693 }
5694
5695 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01005696 return p;
5697}
5698
5699/*
5700 * compile "execute expr"
5701 */
5702 static char_u *
5703compile_execute(char_u *arg, cctx_T *cctx)
5704{
5705 char_u *p = arg;
5706 int count = 0;
5707
5708 for (;;)
5709 {
5710 if (compile_expr1(&p, cctx) == FAIL)
5711 return NULL;
5712 ++count;
5713 p = skipwhite(p);
5714 if (ends_excmd(*p))
5715 break;
5716 }
5717
5718 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005719
5720 return p;
5721}
5722
5723/*
5724 * After ex_function() has collected all the function lines: parse and compile
5725 * the lines into instructions.
5726 * Adds the function to "def_functions".
5727 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5728 * return statement (used for lambda).
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005729 * This can be used recursively through compile_lambda(), which may reallocate
5730 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005731 */
5732 void
5733compile_def_function(ufunc_T *ufunc, int set_return_type)
5734{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005735 char_u *line = NULL;
5736 char_u *p;
5737 exarg_T ea;
5738 char *errormsg = NULL; // error message
5739 int had_return = FALSE;
5740 cctx_T cctx;
5741 garray_T *instr;
5742 int called_emsg_before = called_emsg;
5743 int ret = FAIL;
5744 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005745 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005747 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005748 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005749
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005750 if (ufunc->uf_dfunc_idx >= 0)
5751 {
5752 // Redefining a function that was compiled before.
5753 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5754
5755 // Free old instructions.
5756 delete_def_function_contents(dfunc);
5757 }
5758 else
5759 {
5760 // Add the function to "def_functions".
5761 if (ga_grow(&def_functions, 1) == FAIL)
5762 return;
5763 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02005764 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005765 dfunc->df_idx = def_functions.ga_len;
5766 ufunc->uf_dfunc_idx = dfunc->df_idx;
5767 dfunc->df_ufunc = ufunc;
5768 ++def_functions.ga_len;
5769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005770 }
5771
Bram Moolenaara80faa82020-04-12 19:37:17 +02005772 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005773 cctx.ctx_ufunc = ufunc;
5774 cctx.ctx_lnum = -1;
5775 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5776 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5777 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5778 cctx.ctx_type_list = &ufunc->uf_type_list;
5779 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5780 instr = &cctx.ctx_instr;
5781
5782 // Most modern script version.
5783 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5784
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005785 if (ufunc->uf_def_args.ga_len > 0)
5786 {
5787 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005788 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005789 int i;
5790 char_u *arg;
5791 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5792
5793 // Produce instructions for the default values of optional arguments.
5794 // Store the instruction index in uf_def_arg_idx[] so that we know
5795 // where to start when the function is called, depending on the number
5796 // of arguments.
5797 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5798 if (ufunc->uf_def_arg_idx == NULL)
5799 goto erret;
5800 for (i = 0; i < count; ++i)
5801 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005802 garray_T *stack = &cctx.ctx_type_stack;
5803 type_T *val_type;
5804 int arg_idx = first_def_arg + i;
5805
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005806 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5807 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005808 if (compile_expr1(&arg, &cctx) == FAIL)
5809 goto erret;
5810
5811 // If no type specified use the type of the default value.
5812 // Otherwise check that the default value type matches the
5813 // specified type.
5814 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5815 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
5816 ufunc->uf_arg_types[arg_idx] = val_type;
5817 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
5818 == FAIL)
5819 {
5820 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
5821 arg_idx + 1);
5822 goto erret;
5823 }
5824
5825 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005826 goto erret;
5827 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005828 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5829 }
5830
5831 /*
5832 * Loop over all the lines of the function and generate instructions.
5833 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005834 for (;;)
5835 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02005836 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005837
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005838 // Bail out on the first error to avoid a flood of errors and report
5839 // the right line number when inside try/catch.
5840 if (emsg_before != called_emsg)
5841 goto erret;
5842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005843 if (line != NULL && *line == '|')
5844 // the line continues after a '|'
5845 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005846 else if (line != NULL && *line != NUL
5847 && !(*line == '#' && (line == cctx.ctx_line_start
5848 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005849 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005850 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005851 goto erret;
5852 }
5853 else
5854 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02005855 line = next_line_from_context(&cctx);
5856 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02005857 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005858 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005859 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005860 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861
5862 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02005863 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005864 ea.cmdlinep = &line;
5865 ea.cmd = skipwhite(line);
5866
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02005867 // Some things can be recognized by the first character.
5868 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02005870 case '#':
5871 // "#" starts a comment, but not "#{".
5872 if (ea.cmd[1] != '{')
5873 {
5874 line = (char_u *)"";
5875 continue;
5876 }
5877 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005878
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02005879 case '}':
5880 {
5881 // "}" ends a block scope
5882 scopetype_T stype = cctx.ctx_scope == NULL
5883 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005884
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02005885 if (stype == BLOCK_SCOPE)
5886 {
5887 compile_endblock(&cctx);
5888 line = ea.cmd;
5889 }
5890 else
5891 {
5892 emsg(_("E1025: using } outside of a block scope"));
5893 goto erret;
5894 }
5895 if (line != NULL)
5896 line = skipwhite(ea.cmd + 1);
5897 continue;
5898 }
5899
5900 case '{':
5901 // "{" starts a block scope
5902 // "{'a': 1}->func() is something else
5903 if (ends_excmd(*skipwhite(ea.cmd + 1)))
5904 {
5905 line = compile_block(ea.cmd, &cctx);
5906 continue;
5907 }
5908 break;
5909
5910 case ':':
5911 is_ex_command = TRUE;
5912 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913 }
5914
5915 /*
5916 * COMMAND MODIFIERS
5917 */
5918 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5919 {
5920 if (errormsg != NULL)
5921 goto erret;
5922 // empty line or comment
5923 line = (char_u *)"";
5924 continue;
5925 }
5926
5927 // Skip ":call" to get to the function name.
5928 if (checkforcmd(&ea.cmd, "call", 3))
5929 ea.cmd = skipwhite(ea.cmd);
5930
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005931 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005932 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005933 // Assuming the command starts with a variable or function name,
5934 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5935 // val".
5936 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5937 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005938 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005939 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005941 int oplen;
5942 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005943
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005944 oplen = assignment_len(skipwhite(p), &heredoc);
5945 if (oplen > 0)
5946 {
5947 // Recognize an assignment if we recognize the variable
5948 // name:
5949 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005950 // "local = expr" where "local" is a local var.
5951 // "script = expr" where "script" is a script-local var.
5952 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005953 // "&opt = expr"
5954 // "$ENV = expr"
5955 // "@r = expr"
5956 if (*ea.cmd == '&'
5957 || *ea.cmd == '$'
5958 || *ea.cmd == '@'
5959 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5960 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5961 || lookup_script(ea.cmd, p - ea.cmd) == OK
5962 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5963 {
5964 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5965 if (line == NULL)
5966 goto erret;
5967 continue;
5968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969 }
5970 }
5971 }
5972
5973 /*
5974 * COMMAND after range
5975 */
5976 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005977 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5978 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979
5980 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5981 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005982 if (cctx.ctx_skip == TRUE)
5983 {
5984 line += STRLEN(line);
5985 continue;
5986 }
5987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988 // Expression or function call.
5989 if (ea.cmdidx == CMD_eval)
5990 {
5991 p = ea.cmd;
5992 if (compile_expr1(&p, &cctx) == FAIL)
5993 goto erret;
5994
5995 // drop the return value
5996 generate_instr_drop(&cctx, ISN_DROP, 1);
5997 line = p;
5998 continue;
5999 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006000 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001 iemsg("Command from find_ex_command() not handled");
6002 goto erret;
6003 }
6004
6005 p = skipwhite(p);
6006
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006007 if (cctx.ctx_skip == TRUE
6008 && ea.cmdidx != CMD_elseif
6009 && ea.cmdidx != CMD_else
6010 && ea.cmdidx != CMD_endif)
6011 {
6012 line += STRLEN(line);
6013 continue;
6014 }
6015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016 switch (ea.cmdidx)
6017 {
6018 case CMD_def:
6019 case CMD_function:
6020 // TODO: Nested function
6021 emsg("Nested function not implemented yet");
6022 goto erret;
6023
6024 case CMD_return:
6025 line = compile_return(p, set_return_type, &cctx);
6026 had_return = TRUE;
6027 break;
6028
6029 case CMD_let:
6030 case CMD_const:
6031 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6032 break;
6033
6034 case CMD_import:
6035 line = compile_import(p, &cctx);
6036 break;
6037
6038 case CMD_if:
6039 line = compile_if(p, &cctx);
6040 break;
6041 case CMD_elseif:
6042 line = compile_elseif(p, &cctx);
6043 break;
6044 case CMD_else:
6045 line = compile_else(p, &cctx);
6046 break;
6047 case CMD_endif:
6048 line = compile_endif(p, &cctx);
6049 break;
6050
6051 case CMD_while:
6052 line = compile_while(p, &cctx);
6053 break;
6054 case CMD_endwhile:
6055 line = compile_endwhile(p, &cctx);
6056 break;
6057
6058 case CMD_for:
6059 line = compile_for(p, &cctx);
6060 break;
6061 case CMD_endfor:
6062 line = compile_endfor(p, &cctx);
6063 break;
6064 case CMD_continue:
6065 line = compile_continue(p, &cctx);
6066 break;
6067 case CMD_break:
6068 line = compile_break(p, &cctx);
6069 break;
6070
6071 case CMD_try:
6072 line = compile_try(p, &cctx);
6073 break;
6074 case CMD_catch:
6075 line = compile_catch(p, &cctx);
6076 break;
6077 case CMD_finally:
6078 line = compile_finally(p, &cctx);
6079 break;
6080 case CMD_endtry:
6081 line = compile_endtry(p, &cctx);
6082 break;
6083 case CMD_throw:
6084 line = compile_throw(p, &cctx);
6085 break;
6086
6087 case CMD_echo:
6088 line = compile_echo(p, TRUE, &cctx);
6089 break;
6090 case CMD_echon:
6091 line = compile_echo(p, FALSE, &cctx);
6092 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006093 case CMD_execute:
6094 line = compile_execute(p, &cctx);
6095 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096
6097 default:
6098 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01006099 // TODO:
6100 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01006101 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102 generate_EXEC(&cctx, line);
6103 line = (char_u *)"";
6104 break;
6105 }
6106 if (line == NULL)
6107 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006108 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109
6110 if (cctx.ctx_type_stack.ga_len < 0)
6111 {
6112 iemsg("Type stack underflow");
6113 goto erret;
6114 }
6115 }
6116
6117 if (cctx.ctx_scope != NULL)
6118 {
6119 if (cctx.ctx_scope->se_type == IF_SCOPE)
6120 emsg(_(e_endif));
6121 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6122 emsg(_(e_endwhile));
6123 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6124 emsg(_(e_endfor));
6125 else
6126 emsg(_("E1026: Missing }"));
6127 goto erret;
6128 }
6129
6130 if (!had_return)
6131 {
6132 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6133 {
6134 emsg(_("E1027: Missing return statement"));
6135 goto erret;
6136 }
6137
6138 // Return zero if there is no return at the end.
6139 generate_PUSHNR(&cctx, 0);
6140 generate_instr(&cctx, ISN_RETURN);
6141 }
6142
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006143 {
6144 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6145 + ufunc->uf_dfunc_idx;
6146 dfunc->df_deleted = FALSE;
6147 dfunc->df_instr = instr->ga_data;
6148 dfunc->df_instr_count = instr->ga_len;
6149 dfunc->df_varcount = cctx.ctx_max_local;
6150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006152 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006153 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006154 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006155
6156 // Create a type for the function, with the return type and any
6157 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006158 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6159 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006160 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006161 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006162 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6163 argcount, &ufunc->uf_type_list);
6164 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006165 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006166 argcount + varargs,
6167 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006168 {
6169 ret = FAIL;
6170 goto erret;
6171 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006172 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6173 ufunc->uf_func_type->tt_min_argcount =
6174 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006175 if (ufunc->uf_arg_types == NULL)
6176 {
6177 int i;
6178
6179 // lambda does not have argument types.
6180 for (i = 0; i < argcount; ++i)
6181 ufunc->uf_func_type->tt_args[i] = &t_any;
6182 }
6183 else
6184 mch_memmove(ufunc->uf_func_type->tt_args,
6185 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006186 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006187 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006188 ufunc->uf_func_type->tt_args[argcount] =
6189 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006190 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6191 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006192 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006193 else
6194 // No arguments, can use a predefined type.
6195 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6196 argcount, &ufunc->uf_type_list);
6197
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006198 }
6199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200 ret = OK;
6201
6202erret:
6203 if (ret == FAIL)
6204 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006205 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006206 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6207 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006208
6209 for (idx = 0; idx < instr->ga_len; ++idx)
6210 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006213 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006214 if (!dfunc->df_deleted)
6215 --def_functions.ga_len;
6216
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006217 while (cctx.ctx_scope != NULL)
6218 drop_scope(&cctx);
6219
Bram Moolenaar20431c92020-03-20 18:39:46 +01006220 // Don't execute this function body.
6221 ga_clear_strings(&ufunc->uf_lines);
6222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006223 if (errormsg != NULL)
6224 emsg(errormsg);
6225 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006226 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006227 }
6228
6229 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006230 free_imported(&cctx);
6231 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233}
6234
6235/*
6236 * Delete an instruction, free what it contains.
6237 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006238 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006239delete_instr(isn_T *isn)
6240{
6241 switch (isn->isn_type)
6242 {
6243 case ISN_EXEC:
6244 case ISN_LOADENV:
6245 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006246 case ISN_LOADB:
6247 case ISN_LOADW:
6248 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006249 case ISN_LOADOPT:
6250 case ISN_MEMBER:
6251 case ISN_PUSHEXC:
6252 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006253 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006255 case ISN_STOREB:
6256 case ISN_STOREW:
6257 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006258 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 vim_free(isn->isn_arg.string);
6260 break;
6261
6262 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006263 case ISN_STORES:
6264 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265 break;
6266
6267 case ISN_STOREOPT:
6268 vim_free(isn->isn_arg.storeopt.so_name);
6269 break;
6270
6271 case ISN_PUSHBLOB: // push blob isn_arg.blob
6272 blob_unref(isn->isn_arg.blob);
6273 break;
6274
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006275 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006276#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006277 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006278#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006279 break;
6280
6281 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006282#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006283 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006284#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006285 break;
6286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287 case ISN_UCALL:
6288 vim_free(isn->isn_arg.ufunc.cuf_name);
6289 break;
6290
6291 case ISN_2BOOL:
6292 case ISN_2STRING:
6293 case ISN_ADDBLOB:
6294 case ISN_ADDLIST:
6295 case ISN_BCALL:
6296 case ISN_CATCH:
6297 case ISN_CHECKNR:
6298 case ISN_CHECKTYPE:
6299 case ISN_COMPAREANY:
6300 case ISN_COMPAREBLOB:
6301 case ISN_COMPAREBOOL:
6302 case ISN_COMPAREDICT:
6303 case ISN_COMPAREFLOAT:
6304 case ISN_COMPAREFUNC:
6305 case ISN_COMPARELIST:
6306 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 case ISN_COMPARESPECIAL:
6308 case ISN_COMPARESTRING:
6309 case ISN_CONCAT:
6310 case ISN_DCALL:
6311 case ISN_DROP:
6312 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006313 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006314 case ISN_ENDTRY:
6315 case ISN_FOR:
6316 case ISN_FUNCREF:
6317 case ISN_INDEX:
6318 case ISN_JUMP:
6319 case ISN_LOAD:
6320 case ISN_LOADSCRIPT:
6321 case ISN_LOADREG:
6322 case ISN_LOADV:
6323 case ISN_NEGATENR:
6324 case ISN_NEWDICT:
6325 case ISN_NEWLIST:
6326 case ISN_OPNR:
6327 case ISN_OPFLOAT:
6328 case ISN_OPANY:
6329 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006330 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006331 case ISN_PUSHF:
6332 case ISN_PUSHNR:
6333 case ISN_PUSHBOOL:
6334 case ISN_PUSHSPEC:
6335 case ISN_RETURN:
6336 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006337 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006338 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006339 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006340 case ISN_STORESCRIPT:
6341 case ISN_THROW:
6342 case ISN_TRY:
6343 // nothing allocated
6344 break;
6345 }
6346}
6347
6348/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006349 * Free all instructions for "dfunc".
6350 */
6351 static void
6352delete_def_function_contents(dfunc_T *dfunc)
6353{
6354 int idx;
6355
6356 ga_clear(&dfunc->df_def_args_isn);
6357
6358 if (dfunc->df_instr != NULL)
6359 {
6360 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6361 delete_instr(dfunc->df_instr + idx);
6362 VIM_CLEAR(dfunc->df_instr);
6363 }
6364
6365 dfunc->df_deleted = TRUE;
6366}
6367
6368/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006369 * When a user function is deleted, delete any associated def function.
6370 */
6371 void
6372delete_def_function(ufunc_T *ufunc)
6373{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006374 if (ufunc->uf_dfunc_idx >= 0)
6375 {
6376 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6377 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006378
Bram Moolenaar20431c92020-03-20 18:39:46 +01006379 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380 }
6381}
6382
6383#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006384/*
6385 * Free all functions defined with ":def".
6386 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387 void
6388free_def_functions(void)
6389{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006390 int idx;
6391
6392 for (idx = 0; idx < def_functions.ga_len; ++idx)
6393 {
6394 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6395
6396 delete_def_function_contents(dfunc);
6397 }
6398
6399 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400}
6401#endif
6402
6403
6404#endif // FEAT_EVAL