blob: 2ce85dd3141701fd6a5bf61d4d552dd52008a052 [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;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200100 int lv_idx; // index of the variable on the stack
101 int lv_from_outer; // when TRUE using ctx_outer scope
102 int lv_const; // when TRUE cannot be assigned to
103 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104} lvar_T;
105
106/*
107 * Context for compiling lines of Vim script.
108 * Stores info about the local variables and condition stack.
109 */
110struct cctx_S {
111 ufunc_T *ctx_ufunc; // current function
112 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200113 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114 garray_T ctx_instr; // generated instructions
115
116 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200117 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100118
119 garray_T ctx_imports; // imported items
120
Bram Moolenaara259d8d2020-01-31 20:10:50 +0100121 int ctx_skip; // when TRUE skip commands, when FALSE skip
122 // commands after "else"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100123 scope_T *ctx_scope; // current scope, NULL at toplevel
124
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200125 cctx_T *ctx_outer; // outer scope for lambda or nested
126 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200127 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200130 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100131};
132
133static char e_var_notfound[] = N_("E1001: variable not found: %s");
134static char e_syntax_at[] = N_("E1002: Syntax error at %s");
135
136static int compile_expr1(char_u **arg, cctx_T *cctx);
137static int compile_expr2(char_u **arg, cctx_T *cctx);
138static int compile_expr3(char_u **arg, cctx_T *cctx);
Bram Moolenaar20431c92020-03-20 18:39:46 +0100139static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200140static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
141static int check_type(type_T *expected, type_T *actual, int give_msg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142
143/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200144 * Lookup variable "name" in the local scope and return it.
145 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200147 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148lookup_local(char_u *name, size_t len, cctx_T *cctx)
149{
150 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200151 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100153 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200154 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200155
156 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
158 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200159 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160 if (STRNCMP(name, lvar->lv_name, len) == 0
161 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162 {
163 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200164 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200165 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in outer function scope.
169 if (cctx->ctx_outer != NULL)
170 {
171 lvar = lookup_local(name, len, cctx->ctx_outer);
172 if (lvar != NULL)
173 {
174 // TODO: are there situations we should not mark the outer scope as
175 // used?
176 cctx->ctx_outer_used = TRUE;
177 lvar->lv_from_outer = TRUE;
178 return lvar;
179 }
180 }
181
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200182 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183}
184
185/*
186 * Lookup an argument in the current function.
187 * Returns the argument index or -1 if not found.
188 */
189 static int
190lookup_arg(char_u *name, size_t len, cctx_T *cctx)
191{
192 int idx;
193
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100194 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195 return -1;
196 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
197 {
198 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
199
200 if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
201 return idx;
202 }
203 return -1;
204}
205
206/*
207 * Lookup a vararg argument in the current function.
208 * Returns TRUE if there is a match.
209 */
210 static int
211lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
212{
213 char_u *va_name = cctx->ctx_ufunc->uf_va_name;
214
215 return len > 0 && va_name != NULL
216 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
217}
218
219/*
220 * Lookup a variable in the current script.
221 * Returns OK or FAIL.
222 */
223 static int
224lookup_script(char_u *name, size_t len)
225{
226 int cc;
227 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
228 dictitem_T *di;
229
230 cc = name[len];
231 name[len] = NUL;
232 di = find_var_in_ht(ht, 0, name, TRUE);
233 name[len] = cc;
234 return di == NULL ? FAIL: OK;
235}
236
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100237/*
238 * Check if "p[len]" is already defined, either in script "import_sid" or in
239 * compilation context "cctx".
240 * Return FAIL and give an error if it defined.
241 */
242 int
243check_defined(char_u *p, int len, cctx_T *cctx)
244{
245 if (lookup_script(p, len) == OK
246 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200247 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100248 || find_imported(p, len, cctx) != NULL)))
249 {
250 semsg("E1073: imported name already defined: %s", p);
251 return FAIL;
252 }
253 return OK;
254}
255
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200256/*
257 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
258 * be freed later.
259 */
260 static type_T *
261alloc_type(garray_T *type_gap)
262{
263 type_T *type;
264
265 if (ga_grow(type_gap, 1) == FAIL)
266 return NULL;
267 type = ALLOC_CLEAR_ONE(type_T);
268 if (type != NULL)
269 {
270 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
271 ++type_gap->ga_len;
272 }
273 return type;
274}
275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200277get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278{
279 type_T *type;
280
281 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200282 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100283 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200284 if (member_type->tt_type == VAR_VOID
285 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100286 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100287 if (member_type->tt_type == VAR_BOOL)
288 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 if (member_type->tt_type == VAR_NUMBER)
290 return &t_list_number;
291 if (member_type->tt_type == VAR_STRING)
292 return &t_list_string;
293
294 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200295 type = alloc_type(type_gap);
296 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100297 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 type->tt_type = VAR_LIST;
299 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200300 type->tt_argcount = 0;
301 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100302 return type;
303}
304
305 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200306get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307{
308 type_T *type;
309
310 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200311 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200313 if (member_type->tt_type == VAR_VOID
314 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100315 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100316 if (member_type->tt_type == VAR_BOOL)
317 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318 if (member_type->tt_type == VAR_NUMBER)
319 return &t_dict_number;
320 if (member_type->tt_type == VAR_STRING)
321 return &t_dict_string;
322
323 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200324 type = alloc_type(type_gap);
325 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100326 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100327 type->tt_type = VAR_DICT;
328 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200329 type->tt_argcount = 0;
330 type->tt_args = NULL;
331 return type;
332}
333
334/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200335 * Allocate a new type for a function.
336 */
337 static type_T *
338alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
339{
340 type_T *type = alloc_type(type_gap);
341
342 if (type == NULL)
343 return &t_any;
344 type->tt_type = VAR_FUNC;
345 type->tt_member = ret_type;
346 type->tt_argcount = argcount;
347 type->tt_args = NULL;
348 return type;
349}
350
351/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200352 * Get a function type, based on the return type "ret_type".
353 * If "argcount" is -1 or 0 a predefined type can be used.
354 * If "argcount" > 0 always create a new type, so that arguments can be added.
355 */
356 static type_T *
357get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
358{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200359 // recognize commonly used types
360 if (argcount <= 0)
361 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200362 if (ret_type == &t_unknown)
363 {
364 // (argcount == 0) is not possible
365 return &t_func_unknown;
366 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200367 if (ret_type == &t_void)
368 {
369 if (argcount == 0)
370 return &t_func_0_void;
371 else
372 return &t_func_void;
373 }
374 if (ret_type == &t_any)
375 {
376 if (argcount == 0)
377 return &t_func_0_any;
378 else
379 return &t_func_any;
380 }
381 if (ret_type == &t_number)
382 {
383 if (argcount == 0)
384 return &t_func_0_number;
385 else
386 return &t_func_number;
387 }
388 if (ret_type == &t_string)
389 {
390 if (argcount == 0)
391 return &t_func_0_string;
392 else
393 return &t_func_string;
394 }
395 }
396
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200397 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398}
399
Bram Moolenaara8c17702020-04-01 21:17:24 +0200400/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200401 * For a function type, reserve space for "argcount" argument types (including
402 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200403 */
404 static int
405func_type_add_arg_types(
406 type_T *functype,
407 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200408 garray_T *type_gap)
409{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200410 // To make it easy to free the space needed for the argument types, add the
411 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200412 if (ga_grow(type_gap, 1) == FAIL)
413 return FAIL;
414 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
415 if (functype->tt_args == NULL)
416 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200417 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
418 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200419 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200420 return OK;
421}
422
423/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200424 * Return the type_T for a typval. Only for primitive types.
425 */
426 static type_T *
427typval2type(typval_T *tv)
428{
429 if (tv->v_type == VAR_NUMBER)
430 return &t_number;
431 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200432 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200433 if (tv->v_type == VAR_STRING)
434 return &t_string;
435 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
436 return &t_list_string;
437 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
438 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200439 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200440}
441
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200442 static void
443type_mismatch(type_T *expected, type_T *actual)
444{
445 char *tofree1, *tofree2;
446
447 semsg(_("E1013: type mismatch, expected %s but got %s"),
448 type_name(expected, &tofree1), type_name(actual, &tofree2));
449 vim_free(tofree1);
450 vim_free(tofree2);
451}
452
453 static void
454arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
455{
456 char *tofree1, *tofree2;
457
458 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
459 argidx,
460 type_name(expected, &tofree1), type_name(actual, &tofree2));
461 vim_free(tofree1);
462 vim_free(tofree2);
463}
464
465/*
466 * Check if the expected and actual types match.
467 * Does not allow for assigning "any" to a specific type.
468 */
469 static int
470check_type(type_T *expected, type_T *actual, int give_msg)
471{
472 int ret = OK;
473
474 // When expected is "unknown" we accept any actual type.
475 // When expected is "any" we accept any actual type except "void".
476 if (expected->tt_type != VAR_UNKNOWN
477 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
478
479 {
480 if (expected->tt_type != actual->tt_type)
481 {
482 if (give_msg)
483 type_mismatch(expected, actual);
484 return FAIL;
485 }
486 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
487 {
488 // "unknown" is used for an empty list or dict
489 if (actual->tt_member != &t_unknown)
490 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
491 }
492 else if (expected->tt_type == VAR_FUNC)
493 {
494 if (expected->tt_member != &t_unknown)
495 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
496 if (ret == OK && expected->tt_argcount != -1
497 && (actual->tt_argcount < expected->tt_min_argcount
498 || actual->tt_argcount > expected->tt_argcount))
499 ret = FAIL;
500 }
501 if (ret == FAIL && give_msg)
502 type_mismatch(expected, actual);
503 }
504 return ret;
505}
506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507/////////////////////////////////////////////////////////////////////
508// Following generate_ functions expect the caller to call ga_grow().
509
Bram Moolenaar080457c2020-03-03 21:53:32 +0100510#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
511#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513/*
514 * Generate an instruction without arguments.
515 * Returns a pointer to the new instruction, NULL if failed.
516 */
517 static isn_T *
518generate_instr(cctx_T *cctx, isntype_T isn_type)
519{
520 garray_T *instr = &cctx->ctx_instr;
521 isn_T *isn;
522
Bram Moolenaar080457c2020-03-03 21:53:32 +0100523 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 if (ga_grow(instr, 1) == FAIL)
525 return NULL;
526 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
527 isn->isn_type = isn_type;
528 isn->isn_lnum = cctx->ctx_lnum + 1;
529 ++instr->ga_len;
530
531 return isn;
532}
533
534/*
535 * Generate an instruction without arguments.
536 * "drop" will be removed from the stack.
537 * Returns a pointer to the new instruction, NULL if failed.
538 */
539 static isn_T *
540generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
541{
542 garray_T *stack = &cctx->ctx_type_stack;
543
Bram Moolenaar080457c2020-03-03 21:53:32 +0100544 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545 stack->ga_len -= drop;
546 return generate_instr(cctx, isn_type);
547}
548
549/*
550 * Generate instruction "isn_type" and put "type" on the type stack.
551 */
552 static isn_T *
553generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
554{
555 isn_T *isn;
556 garray_T *stack = &cctx->ctx_type_stack;
557
558 if ((isn = generate_instr(cctx, isn_type)) == NULL)
559 return NULL;
560
561 if (ga_grow(stack, 1) == FAIL)
562 return NULL;
563 ((type_T **)stack->ga_data)[stack->ga_len] = type;
564 ++stack->ga_len;
565
566 return isn;
567}
568
569/*
570 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
571 */
572 static int
573may_generate_2STRING(int offset, cctx_T *cctx)
574{
575 isn_T *isn;
576 garray_T *stack = &cctx->ctx_type_stack;
577 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
578
579 if ((*type)->tt_type == VAR_STRING)
580 return OK;
581 *type = &t_string;
582
583 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
584 return FAIL;
585 isn->isn_arg.number = offset;
586
587 return OK;
588}
589
590 static int
591check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
592{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200593 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200595 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 {
597 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100598 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 else
600 semsg(_("E1036: %c requires number or float arguments"), *op);
601 return FAIL;
602 }
603 return OK;
604}
605
606/*
607 * Generate an instruction with two arguments. The instruction depends on the
608 * type of the arguments.
609 */
610 static int
611generate_two_op(cctx_T *cctx, char_u *op)
612{
613 garray_T *stack = &cctx->ctx_type_stack;
614 type_T *type1;
615 type_T *type2;
616 vartype_T vartype;
617 isn_T *isn;
618
Bram Moolenaar080457c2020-03-03 21:53:32 +0100619 RETURN_OK_IF_SKIP(cctx);
620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 // Get the known type of the two items on the stack. If they are matching
622 // use a type-specific instruction. Otherwise fall back to runtime type
623 // checking.
624 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
625 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200626 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627 if (type1->tt_type == type2->tt_type
628 && (type1->tt_type == VAR_NUMBER
629 || type1->tt_type == VAR_LIST
630#ifdef FEAT_FLOAT
631 || type1->tt_type == VAR_FLOAT
632#endif
633 || type1->tt_type == VAR_BLOB))
634 vartype = type1->tt_type;
635
636 switch (*op)
637 {
638 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200639 && type1->tt_type != VAR_ANY
640 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 && check_number_or_float(
642 type1->tt_type, type2->tt_type, op) == FAIL)
643 return FAIL;
644 isn = generate_instr_drop(cctx,
645 vartype == VAR_NUMBER ? ISN_OPNR
646 : vartype == VAR_LIST ? ISN_ADDLIST
647 : vartype == VAR_BLOB ? ISN_ADDBLOB
648#ifdef FEAT_FLOAT
649 : vartype == VAR_FLOAT ? ISN_OPFLOAT
650#endif
651 : ISN_OPANY, 1);
652 if (isn != NULL)
653 isn->isn_arg.op.op_type = EXPR_ADD;
654 break;
655
656 case '-':
657 case '*':
658 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
659 op) == FAIL)
660 return FAIL;
661 if (vartype == VAR_NUMBER)
662 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
663#ifdef FEAT_FLOAT
664 else if (vartype == VAR_FLOAT)
665 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
666#endif
667 else
668 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
669 if (isn != NULL)
670 isn->isn_arg.op.op_type = *op == '*'
671 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
672 break;
673
Bram Moolenaar4c683752020-04-05 21:38:23 +0200674 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200676 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 && type2->tt_type != VAR_NUMBER))
678 {
679 emsg(_("E1035: % requires number arguments"));
680 return FAIL;
681 }
682 isn = generate_instr_drop(cctx,
683 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
684 if (isn != NULL)
685 isn->isn_arg.op.op_type = EXPR_REM;
686 break;
687 }
688
689 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200690 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 {
692 type_T *type = &t_any;
693
694#ifdef FEAT_FLOAT
695 // float+number and number+float results in float
696 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
697 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
698 type = &t_float;
699#endif
700 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
701 }
702
703 return OK;
704}
705
706/*
707 * Generate an ISN_COMPARE* instruction with a boolean result.
708 */
709 static int
710generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
711{
712 isntype_T isntype = ISN_DROP;
713 isn_T *isn;
714 garray_T *stack = &cctx->ctx_type_stack;
715 vartype_T type1;
716 vartype_T type2;
717
Bram Moolenaar080457c2020-03-03 21:53:32 +0100718 RETURN_OK_IF_SKIP(cctx);
719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 // Get the known type of the two items on the stack. If they are matching
721 // use a type-specific instruction. Otherwise fall back to runtime type
722 // checking.
723 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
724 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200725 if (type1 == VAR_UNKNOWN)
726 type1 = VAR_ANY;
727 if (type2 == VAR_UNKNOWN)
728 type2 = VAR_ANY;
729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730 if (type1 == type2)
731 {
732 switch (type1)
733 {
734 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
735 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
736 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
737 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
738 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
739 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
740 case VAR_LIST: isntype = ISN_COMPARELIST; break;
741 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
742 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 default: isntype = ISN_COMPAREANY; break;
744 }
745 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200746 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
748 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
749 isntype = ISN_COMPAREANY;
750
751 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
752 && (isntype == ISN_COMPAREBOOL
753 || isntype == ISN_COMPARESPECIAL
754 || isntype == ISN_COMPARENR
755 || isntype == ISN_COMPAREFLOAT))
756 {
757 semsg(_("E1037: Cannot use \"%s\" with %s"),
758 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
759 return FAIL;
760 }
761 if (isntype == ISN_DROP
762 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
763 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
764 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
765 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
766 && exptype != EXPR_IS && exptype != EXPR_ISNOT
767 && (type1 == VAR_BLOB || type2 == VAR_BLOB
768 || type1 == VAR_LIST || type2 == VAR_LIST))))
769 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100770 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771 vartype_name(type1), vartype_name(type2));
772 return FAIL;
773 }
774
775 if ((isn = generate_instr(cctx, isntype)) == NULL)
776 return FAIL;
777 isn->isn_arg.op.op_type = exptype;
778 isn->isn_arg.op.op_ic = ic;
779
780 // takes two arguments, puts one bool back
781 if (stack->ga_len >= 2)
782 {
783 --stack->ga_len;
784 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
785 }
786
787 return OK;
788}
789
790/*
791 * Generate an ISN_2BOOL instruction.
792 */
793 static int
794generate_2BOOL(cctx_T *cctx, int invert)
795{
796 isn_T *isn;
797 garray_T *stack = &cctx->ctx_type_stack;
798
Bram Moolenaar080457c2020-03-03 21:53:32 +0100799 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
801 return FAIL;
802 isn->isn_arg.number = invert;
803
804 // type becomes bool
805 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
806
807 return OK;
808}
809
810 static int
811generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
812{
813 isn_T *isn;
814 garray_T *stack = &cctx->ctx_type_stack;
815
Bram Moolenaar080457c2020-03-03 21:53:32 +0100816 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
818 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200819 // TODO: whole type, e.g. for a function also arg and return types
820 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 isn->isn_arg.type.ct_off = offset;
822
823 // type becomes vartype
824 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
825
826 return OK;
827}
828
829/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200830 * Check that
831 * - "actual" is "expected" type or
832 * - "actual" is a type that can be "expected" type: add a runtime check; or
833 * - return FAIL.
834 */
835 static int
836need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
837{
838 if (check_type(expected, actual, FALSE) == OK)
839 return OK;
840 if (actual->tt_type != VAR_ANY
841 && actual->tt_type != VAR_UNKNOWN
842 && !(actual->tt_type == VAR_FUNC
843 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
844 {
845 type_mismatch(expected, actual);
846 return FAIL;
847 }
848 generate_TYPECHECK(cctx, expected, offset);
849 return OK;
850}
851
852/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 * Generate an ISN_PUSHNR instruction.
854 */
855 static int
856generate_PUSHNR(cctx_T *cctx, varnumber_T number)
857{
858 isn_T *isn;
859
Bram Moolenaar080457c2020-03-03 21:53:32 +0100860 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
862 return FAIL;
863 isn->isn_arg.number = number;
864
865 return OK;
866}
867
868/*
869 * Generate an ISN_PUSHBOOL instruction.
870 */
871 static int
872generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
873{
874 isn_T *isn;
875
Bram Moolenaar080457c2020-03-03 21:53:32 +0100876 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
878 return FAIL;
879 isn->isn_arg.number = number;
880
881 return OK;
882}
883
884/*
885 * Generate an ISN_PUSHSPEC instruction.
886 */
887 static int
888generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
889{
890 isn_T *isn;
891
Bram Moolenaar080457c2020-03-03 21:53:32 +0100892 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
894 return FAIL;
895 isn->isn_arg.number = number;
896
897 return OK;
898}
899
900#ifdef FEAT_FLOAT
901/*
902 * Generate an ISN_PUSHF instruction.
903 */
904 static int
905generate_PUSHF(cctx_T *cctx, float_T fnumber)
906{
907 isn_T *isn;
908
Bram Moolenaar080457c2020-03-03 21:53:32 +0100909 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100910 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
911 return FAIL;
912 isn->isn_arg.fnumber = fnumber;
913
914 return OK;
915}
916#endif
917
918/*
919 * Generate an ISN_PUSHS instruction.
920 * Consumes "str".
921 */
922 static int
923generate_PUSHS(cctx_T *cctx, char_u *str)
924{
925 isn_T *isn;
926
Bram Moolenaar080457c2020-03-03 21:53:32 +0100927 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
929 return FAIL;
930 isn->isn_arg.string = str;
931
932 return OK;
933}
934
935/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100936 * Generate an ISN_PUSHCHANNEL instruction.
937 * Consumes "channel".
938 */
939 static int
940generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
941{
942 isn_T *isn;
943
Bram Moolenaar080457c2020-03-03 21:53:32 +0100944 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100945 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
946 return FAIL;
947 isn->isn_arg.channel = channel;
948
949 return OK;
950}
951
952/*
953 * Generate an ISN_PUSHJOB instruction.
954 * Consumes "job".
955 */
956 static int
957generate_PUSHJOB(cctx_T *cctx, job_T *job)
958{
959 isn_T *isn;
960
Bram Moolenaar080457c2020-03-03 21:53:32 +0100961 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100962 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100963 return FAIL;
964 isn->isn_arg.job = job;
965
966 return OK;
967}
968
969/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 * Generate an ISN_PUSHBLOB instruction.
971 * Consumes "blob".
972 */
973 static int
974generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
975{
976 isn_T *isn;
977
Bram Moolenaar080457c2020-03-03 21:53:32 +0100978 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
980 return FAIL;
981 isn->isn_arg.blob = blob;
982
983 return OK;
984}
985
986/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100987 * Generate an ISN_PUSHFUNC instruction with name "name".
988 * Consumes "name".
989 */
990 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200991generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100992{
993 isn_T *isn;
994
Bram Moolenaar080457c2020-03-03 21:53:32 +0100995 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200996 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100997 return FAIL;
998 isn->isn_arg.string = name;
999
1000 return OK;
1001}
1002
1003/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004 * Generate an ISN_STORE instruction.
1005 */
1006 static int
1007generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1008{
1009 isn_T *isn;
1010
Bram Moolenaar080457c2020-03-03 21:53:32 +01001011 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1013 return FAIL;
1014 if (name != NULL)
1015 isn->isn_arg.string = vim_strsave(name);
1016 else
1017 isn->isn_arg.number = idx;
1018
1019 return OK;
1020}
1021
1022/*
1023 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1024 */
1025 static int
1026generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1027{
1028 isn_T *isn;
1029
Bram Moolenaar080457c2020-03-03 21:53:32 +01001030 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1032 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001033 isn->isn_arg.storenr.stnr_idx = idx;
1034 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001035
1036 return OK;
1037}
1038
1039/*
1040 * Generate an ISN_STOREOPT instruction
1041 */
1042 static int
1043generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1044{
1045 isn_T *isn;
1046
Bram Moolenaar080457c2020-03-03 21:53:32 +01001047 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1049 return FAIL;
1050 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1051 isn->isn_arg.storeopt.so_flags = opt_flags;
1052
1053 return OK;
1054}
1055
1056/*
1057 * Generate an ISN_LOAD or similar instruction.
1058 */
1059 static int
1060generate_LOAD(
1061 cctx_T *cctx,
1062 isntype_T isn_type,
1063 int idx,
1064 char_u *name,
1065 type_T *type)
1066{
1067 isn_T *isn;
1068
Bram Moolenaar080457c2020-03-03 21:53:32 +01001069 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1071 return FAIL;
1072 if (name != NULL)
1073 isn->isn_arg.string = vim_strsave(name);
1074 else
1075 isn->isn_arg.number = idx;
1076
1077 return OK;
1078}
1079
1080/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001081 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001082 */
1083 static int
1084generate_LOADV(
1085 cctx_T *cctx,
1086 char_u *name,
1087 int error)
1088{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001089 int di_flags;
1090 int vidx = find_vim_var(name, &di_flags);
1091 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001092
Bram Moolenaar080457c2020-03-03 21:53:32 +01001093 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001094 if (vidx < 0)
1095 {
1096 if (error)
1097 semsg(_(e_var_notfound), name);
1098 return FAIL;
1099 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001100 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001101
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001102 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001103}
1104
1105/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001106 * Generate an ISN_UNLET instruction.
1107 */
1108 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001109generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001110{
1111 isn_T *isn;
1112
1113 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001114 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001115 return FAIL;
1116 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1117 isn->isn_arg.unlet.ul_forceit = forceit;
1118
1119 return OK;
1120}
1121
1122/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 * Generate an ISN_LOADS instruction.
1124 */
1125 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001126generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001128 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001130 int sid,
1131 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132{
1133 isn_T *isn;
1134
Bram Moolenaar080457c2020-03-03 21:53:32 +01001135 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001136 if (isn_type == ISN_LOADS)
1137 isn = generate_instr_type(cctx, isn_type, type);
1138 else
1139 isn = generate_instr_drop(cctx, isn_type, 1);
1140 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001142 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1143 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144
1145 return OK;
1146}
1147
1148/*
1149 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1150 */
1151 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001152generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153 cctx_T *cctx,
1154 isntype_T isn_type,
1155 int sid,
1156 int idx,
1157 type_T *type)
1158{
1159 isn_T *isn;
1160
Bram Moolenaar080457c2020-03-03 21:53:32 +01001161 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 if (isn_type == ISN_LOADSCRIPT)
1163 isn = generate_instr_type(cctx, isn_type, type);
1164 else
1165 isn = generate_instr_drop(cctx, isn_type, 1);
1166 if (isn == NULL)
1167 return FAIL;
1168 isn->isn_arg.script.script_sid = sid;
1169 isn->isn_arg.script.script_idx = idx;
1170 return OK;
1171}
1172
1173/*
1174 * Generate an ISN_NEWLIST instruction.
1175 */
1176 static int
1177generate_NEWLIST(cctx_T *cctx, int count)
1178{
1179 isn_T *isn;
1180 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 type_T *type;
1182 type_T *member;
1183
Bram Moolenaar080457c2020-03-03 21:53:32 +01001184 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1186 return FAIL;
1187 isn->isn_arg.number = count;
1188
1189 // drop the value types
1190 stack->ga_len -= count;
1191
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001192 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001193 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 if (count > 0)
1195 member = ((type_T **)stack->ga_data)[stack->ga_len];
1196 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001197 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001198 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199
1200 // add the list type to the type stack
1201 if (ga_grow(stack, 1) == FAIL)
1202 return FAIL;
1203 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1204 ++stack->ga_len;
1205
1206 return OK;
1207}
1208
1209/*
1210 * Generate an ISN_NEWDICT instruction.
1211 */
1212 static int
1213generate_NEWDICT(cctx_T *cctx, int count)
1214{
1215 isn_T *isn;
1216 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 type_T *type;
1218 type_T *member;
1219
Bram Moolenaar080457c2020-03-03 21:53:32 +01001220 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1222 return FAIL;
1223 isn->isn_arg.number = count;
1224
1225 // drop the key and value types
1226 stack->ga_len -= 2 * count;
1227
Bram Moolenaar436472f2020-02-20 22:54:43 +01001228 // Use the first value type for the list member type. Use "void" for an
1229 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 if (count > 0)
1231 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1232 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001233 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001234 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235
1236 // add the dict type to the type stack
1237 if (ga_grow(stack, 1) == FAIL)
1238 return FAIL;
1239 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1240 ++stack->ga_len;
1241
1242 return OK;
1243}
1244
1245/*
1246 * Generate an ISN_FUNCREF instruction.
1247 */
1248 static int
1249generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1250{
1251 isn_T *isn;
1252 garray_T *stack = &cctx->ctx_type_stack;
1253
Bram Moolenaar080457c2020-03-03 21:53:32 +01001254 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1256 return FAIL;
1257 isn->isn_arg.number = dfunc_idx;
1258
1259 if (ga_grow(stack, 1) == FAIL)
1260 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001261 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 // TODO: argument and return types
1263 ++stack->ga_len;
1264
1265 return OK;
1266}
1267
1268/*
1269 * Generate an ISN_JUMP instruction.
1270 */
1271 static int
1272generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1273{
1274 isn_T *isn;
1275 garray_T *stack = &cctx->ctx_type_stack;
1276
Bram Moolenaar080457c2020-03-03 21:53:32 +01001277 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1279 return FAIL;
1280 isn->isn_arg.jump.jump_when = when;
1281 isn->isn_arg.jump.jump_where = where;
1282
1283 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1284 --stack->ga_len;
1285
1286 return OK;
1287}
1288
1289 static int
1290generate_FOR(cctx_T *cctx, int loop_idx)
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_FOR)) == NULL)
1297 return FAIL;
1298 isn->isn_arg.forloop.for_idx = loop_idx;
1299
1300 if (ga_grow(stack, 1) == FAIL)
1301 return FAIL;
1302 // type doesn't matter, will be stored next
1303 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1304 ++stack->ga_len;
1305
1306 return OK;
1307}
1308
1309/*
1310 * Generate an ISN_BCALL instruction.
1311 * Return FAIL if the number of arguments is wrong.
1312 */
1313 static int
1314generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1315{
1316 isn_T *isn;
1317 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001318 type_T *argtypes[MAX_FUNC_ARGS];
1319 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320
Bram Moolenaar080457c2020-03-03 21:53:32 +01001321 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 if (check_internal_func(func_idx, argcount) == FAIL)
1323 return FAIL;
1324
1325 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1326 return FAIL;
1327 isn->isn_arg.bfunc.cbf_idx = func_idx;
1328 isn->isn_arg.bfunc.cbf_argcount = argcount;
1329
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001330 for (i = 0; i < argcount; ++i)
1331 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 stack->ga_len -= argcount; // drop the arguments
1334 if (ga_grow(stack, 1) == FAIL)
1335 return FAIL;
1336 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001337 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 ++stack->ga_len; // add return value
1339
1340 return OK;
1341}
1342
1343/*
1344 * Generate an ISN_DCALL or ISN_UCALL instruction.
1345 * Return FAIL if the number of arguments is wrong.
1346 */
1347 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001348generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349{
1350 isn_T *isn;
1351 garray_T *stack = &cctx->ctx_type_stack;
1352 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001353 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354
Bram Moolenaar080457c2020-03-03 21:53:32 +01001355 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 if (argcount > regular_args && !has_varargs(ufunc))
1357 {
1358 semsg(_(e_toomanyarg), ufunc->uf_name);
1359 return FAIL;
1360 }
1361 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1362 {
1363 semsg(_(e_toofewarg), ufunc->uf_name);
1364 return FAIL;
1365 }
1366
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001367 if (ufunc->uf_dfunc_idx >= 0)
1368 {
1369 int i;
1370
1371 for (i = 0; i < argcount; ++i)
1372 {
1373 type_T *expected;
1374 type_T *actual;
1375
1376 if (i < regular_args)
1377 {
1378 if (ufunc->uf_arg_types == NULL)
1379 continue;
1380 expected = ufunc->uf_arg_types[i];
1381 }
1382 else
1383 expected = ufunc->uf_va_type->tt_member;
1384 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001385 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001386 {
1387 arg_type_mismatch(expected, actual, i + 1);
1388 return FAIL;
1389 }
1390 }
1391 }
1392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 if ((isn = generate_instr(cctx,
1394 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1395 return FAIL;
1396 if (ufunc->uf_dfunc_idx >= 0)
1397 {
1398 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1399 isn->isn_arg.dfunc.cdf_argcount = argcount;
1400 }
1401 else
1402 {
1403 // A user function may be deleted and redefined later, can't use the
1404 // ufunc pointer, need to look it up again at runtime.
1405 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1406 isn->isn_arg.ufunc.cuf_argcount = argcount;
1407 }
1408
1409 stack->ga_len -= argcount; // drop the arguments
1410 if (ga_grow(stack, 1) == FAIL)
1411 return FAIL;
1412 // add return value
1413 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1414 ++stack->ga_len;
1415
1416 return OK;
1417}
1418
1419/*
1420 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1421 */
1422 static int
1423generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1424{
1425 isn_T *isn;
1426 garray_T *stack = &cctx->ctx_type_stack;
1427
Bram Moolenaar080457c2020-03-03 21:53:32 +01001428 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1430 return FAIL;
1431 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1432 isn->isn_arg.ufunc.cuf_argcount = argcount;
1433
1434 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001435 if (ga_grow(stack, 1) == FAIL)
1436 return FAIL;
1437 // add return value
1438 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1439 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440
1441 return OK;
1442}
1443
1444/*
1445 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001446 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 */
1448 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001449generate_PCALL(
1450 cctx_T *cctx,
1451 int argcount,
1452 char_u *name,
1453 type_T *type,
1454 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455{
1456 isn_T *isn;
1457 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001458 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459
Bram Moolenaar080457c2020-03-03 21:53:32 +01001460 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001461
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001462 if (type->tt_type == VAR_ANY)
1463 ret_type = &t_any;
1464 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1465 ret_type = type->tt_member;
1466 else
1467 {
1468 semsg(_("E1085: Not a callable type: %s"), name);
1469 return FAIL;
1470 }
1471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1473 return FAIL;
1474 isn->isn_arg.pfunc.cpf_top = at_top;
1475 isn->isn_arg.pfunc.cpf_argcount = argcount;
1476
1477 stack->ga_len -= argcount; // drop the arguments
1478
1479 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001480 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001482 // If partial is above the arguments it must be cleared and replaced with
1483 // the return value.
1484 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1485 return FAIL;
1486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 return OK;
1488}
1489
1490/*
1491 * Generate an ISN_MEMBER instruction.
1492 */
1493 static int
1494generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1495{
1496 isn_T *isn;
1497 garray_T *stack = &cctx->ctx_type_stack;
1498 type_T *type;
1499
Bram Moolenaar080457c2020-03-03 21:53:32 +01001500 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1502 return FAIL;
1503 isn->isn_arg.string = vim_strnsave(name, (int)len);
1504
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001505 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001507 if (type->tt_type != VAR_DICT && type != &t_any)
1508 {
1509 emsg(_(e_dictreq));
1510 return FAIL;
1511 }
1512 // change dict type to dict member type
1513 if (type->tt_type == VAR_DICT)
1514 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515
1516 return OK;
1517}
1518
1519/*
1520 * Generate an ISN_ECHO instruction.
1521 */
1522 static int
1523generate_ECHO(cctx_T *cctx, int with_white, int count)
1524{
1525 isn_T *isn;
1526
Bram Moolenaar080457c2020-03-03 21:53:32 +01001527 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1529 return FAIL;
1530 isn->isn_arg.echo.echo_with_white = with_white;
1531 isn->isn_arg.echo.echo_count = count;
1532
1533 return OK;
1534}
1535
Bram Moolenaarad39c092020-02-26 18:23:43 +01001536/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001537 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001538 */
1539 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001540generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001541{
1542 isn_T *isn;
1543
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001544 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001545 return FAIL;
1546 isn->isn_arg.number = count;
1547
1548 return OK;
1549}
1550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 static int
1552generate_EXEC(cctx_T *cctx, char_u *line)
1553{
1554 isn_T *isn;
1555
Bram Moolenaar080457c2020-03-03 21:53:32 +01001556 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1558 return FAIL;
1559 isn->isn_arg.string = vim_strsave(line);
1560 return OK;
1561}
1562
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001563 static int
1564generate_EXECCONCAT(cctx_T *cctx, int count)
1565{
1566 isn_T *isn;
1567
1568 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1569 return FAIL;
1570 isn->isn_arg.number = count;
1571 return OK;
1572}
1573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574/*
1575 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001576 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001578 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1580{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 lvar_T *lvar;
1582
1583 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1584 {
1585 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001586 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 }
1588
1589 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001590 return NULL;
1591 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001593 // Every local variable uses the next entry on the stack. We could re-use
1594 // the last ones when leaving a scope, but then variables used in a closure
1595 // might get overwritten. To keep things simple do not re-use stack
1596 // entries. This is less efficient, but memory is cheap these days.
1597 lvar->lv_idx = cctx->ctx_locals_count++;
1598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1600 lvar->lv_const = isConst;
1601 lvar->lv_type = type;
1602
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001603 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604}
1605
1606/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001607 * Remove local variables above "new_top".
1608 */
1609 static void
1610unwind_locals(cctx_T *cctx, int new_top)
1611{
1612 if (cctx->ctx_locals.ga_len > new_top)
1613 {
1614 int idx;
1615 lvar_T *lvar;
1616
1617 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1618 {
1619 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1620 vim_free(lvar->lv_name);
1621 }
1622 }
1623 cctx->ctx_locals.ga_len = new_top;
1624}
1625
1626/*
1627 * Free all local variables.
1628 */
1629 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001630free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001631{
1632 unwind_locals(cctx, 0);
1633 ga_clear(&cctx->ctx_locals);
1634}
1635
1636/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 * Skip over a type definition and return a pointer to just after it.
1638 */
1639 char_u *
1640skip_type(char_u *start)
1641{
1642 char_u *p = start;
1643
1644 while (ASCII_ISALNUM(*p) || *p == '_')
1645 ++p;
1646
1647 // Skip over "<type>"; this is permissive about white space.
1648 if (*skipwhite(p) == '<')
1649 {
1650 p = skipwhite(p);
1651 p = skip_type(skipwhite(p + 1));
1652 p = skipwhite(p);
1653 if (*p == '>')
1654 ++p;
1655 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001656 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1657 {
1658 // handle func(args): type
1659 ++p;
1660 while (*p != ')' && *p != NUL)
1661 {
1662 p = skip_type(p);
1663 if (*p == ',')
1664 p = skipwhite(p + 1);
1665 }
1666 if (*p == ')' && p[1] == ':')
1667 p = skip_type(skipwhite(p + 2));
1668 }
1669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 return p;
1671}
1672
1673/*
1674 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001675 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 * Returns NULL in case of failure.
1677 */
1678 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001679parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680{
1681 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001682 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683
1684 if (**arg != '<')
1685 {
1686 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001687 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 else
1689 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001690 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 }
1692 *arg = skipwhite(*arg + 1);
1693
Bram Moolenaard77a8522020-04-03 21:59:57 +02001694 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695
1696 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001697 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 {
1699 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001700 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 }
1702 ++*arg;
1703
1704 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001705 return get_list_type(member_type, type_gap);
1706 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707}
1708
1709/*
1710 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001711 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 */
1713 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001714parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715{
1716 char_u *p = *arg;
1717 size_t len;
1718
1719 // skip over the first word
1720 while (ASCII_ISALNUM(*p) || *p == '_')
1721 ++p;
1722 len = p - *arg;
1723
1724 switch (**arg)
1725 {
1726 case 'a':
1727 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1728 {
1729 *arg += len;
1730 return &t_any;
1731 }
1732 break;
1733 case 'b':
1734 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1735 {
1736 *arg += len;
1737 return &t_bool;
1738 }
1739 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1740 {
1741 *arg += len;
1742 return &t_blob;
1743 }
1744 break;
1745 case 'c':
1746 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1747 {
1748 *arg += len;
1749 return &t_channel;
1750 }
1751 break;
1752 case 'd':
1753 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1754 {
1755 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001756 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 }
1758 break;
1759 case 'f':
1760 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1761 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001762#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763 *arg += len;
1764 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001765#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001766 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001767 return &t_any;
1768#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 }
1770 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1771 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001772 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001773 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001774 int argcount = -1;
1775 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001776 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001777 type_T *arg_type[MAX_FUNC_ARGS + 1];
1778
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001779 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001781 if (**arg == '(')
1782 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001783 // "func" may or may not return a value, "func()" does
1784 // not return a value.
1785 ret_type = &t_void;
1786
Bram Moolenaard77a8522020-04-03 21:59:57 +02001787 p = ++*arg;
1788 argcount = 0;
1789 while (*p != NUL && *p != ')')
1790 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001791 if (*p == '?')
1792 {
1793 if (first_optional == -1)
1794 first_optional = argcount;
1795 ++p;
1796 }
1797 else if (first_optional != -1)
1798 {
1799 emsg(_("E1007: mandatory argument after optional argument"));
1800 return &t_any;
1801 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001802 else if (STRNCMP(p, "...", 3) == 0)
1803 {
1804 flags |= TTFLAG_VARARGS;
1805 p += 3;
1806 }
1807
1808 arg_type[argcount++] = parse_type(&p, type_gap);
1809
1810 // Nothing comes after "...{type}".
1811 if (flags & TTFLAG_VARARGS)
1812 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001813
Bram Moolenaard77a8522020-04-03 21:59:57 +02001814 if (*p != ',' && *skipwhite(p) == ',')
1815 {
1816 semsg(_(e_no_white_before), ",");
1817 return &t_any;
1818 }
1819 if (*p == ',')
1820 {
1821 ++p;
1822 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001823 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001824 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001825 return &t_any;
1826 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001827 }
1828 p = skipwhite(p);
1829 if (argcount == MAX_FUNC_ARGS)
1830 {
1831 emsg(_("E740: Too many argument types"));
1832 return &t_any;
1833 }
1834 }
1835
1836 p = skipwhite(p);
1837 if (*p != ')')
1838 {
1839 emsg(_(e_missing_close));
1840 return &t_any;
1841 }
1842 *arg = p + 1;
1843 }
1844 if (**arg == ':')
1845 {
1846 // parse return type
1847 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001848 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001849 semsg(_(e_white_after), ":");
1850 *arg = skipwhite(*arg);
1851 ret_type = parse_type(arg, type_gap);
1852 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001853 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001854 type = get_func_type(ret_type, argcount, type_gap);
1855 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001856 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001857 type = alloc_func_type(ret_type, argcount, type_gap);
1858 type->tt_flags = flags;
1859 if (argcount > 0)
1860 {
1861 type->tt_argcount = argcount;
1862 type->tt_min_argcount = first_optional == -1
1863 ? argcount : first_optional;
1864 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001865 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001866 return &t_any;
1867 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001868 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001869 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001870 }
1871 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 }
1873 break;
1874 case 'j':
1875 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1876 {
1877 *arg += len;
1878 return &t_job;
1879 }
1880 break;
1881 case 'l':
1882 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1883 {
1884 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001885 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886 }
1887 break;
1888 case 'n':
1889 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1890 {
1891 *arg += len;
1892 return &t_number;
1893 }
1894 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 case 's':
1896 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1897 {
1898 *arg += len;
1899 return &t_string;
1900 }
1901 break;
1902 case 'v':
1903 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1904 {
1905 *arg += len;
1906 return &t_void;
1907 }
1908 break;
1909 }
1910
1911 semsg(_("E1010: Type not recognized: %s"), *arg);
1912 return &t_any;
1913}
1914
1915/*
1916 * Check if "type1" and "type2" are exactly the same.
1917 */
1918 static int
1919equal_type(type_T *type1, type_T *type2)
1920{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001921 int i;
1922
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923 if (type1->tt_type != type2->tt_type)
1924 return FALSE;
1925 switch (type1->tt_type)
1926 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001928 case VAR_ANY:
1929 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 case VAR_SPECIAL:
1931 case VAR_BOOL:
1932 case VAR_NUMBER:
1933 case VAR_FLOAT:
1934 case VAR_STRING:
1935 case VAR_BLOB:
1936 case VAR_JOB:
1937 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001938 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 case VAR_LIST:
1940 case VAR_DICT:
1941 return equal_type(type1->tt_member, type2->tt_member);
1942 case VAR_FUNC:
1943 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001944 if (!equal_type(type1->tt_member, type2->tt_member)
1945 || type1->tt_argcount != type2->tt_argcount)
1946 return FALSE;
1947 if (type1->tt_argcount < 0
1948 || type1->tt_args == NULL || type2->tt_args == NULL)
1949 return TRUE;
1950 for (i = 0; i < type1->tt_argcount; ++i)
1951 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
1952 return FALSE;
1953 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954 }
1955 return TRUE;
1956}
1957
1958/*
1959 * Find the common type of "type1" and "type2" and put it in "dest".
1960 * "type2" and "dest" may be the same.
1961 */
1962 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02001963common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964{
1965 if (equal_type(type1, type2))
1966 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001967 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 return;
1969 }
1970
1971 if (type1->tt_type == type2->tt_type)
1972 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1974 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001975 type_T *common;
1976
Bram Moolenaard77a8522020-04-03 21:59:57 +02001977 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001978 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001979 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001980 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001981 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 return;
1983 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001984 if (type1->tt_type == VAR_FUNC)
1985 {
1986 type_T *common;
1987
1988 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
1989 if (type1->tt_argcount == type2->tt_argcount
1990 && type1->tt_argcount >= 0)
1991 {
1992 int argcount = type1->tt_argcount;
1993 int i;
1994
1995 *dest = alloc_func_type(common, argcount, type_gap);
1996 if (type1->tt_args != NULL && type2->tt_args != NULL)
1997 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02001998 if (func_type_add_arg_types(*dest, argcount,
1999 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002000 for (i = 0; i < argcount; ++i)
2001 common_type(type1->tt_args[i], type2->tt_args[i],
2002 &(*dest)->tt_args[i], type_gap);
2003 }
2004 }
2005 else
2006 *dest = alloc_func_type(common, -1, type_gap);
2007 return;
2008 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 }
2010
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002011 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012}
2013
2014 char *
2015vartype_name(vartype_T type)
2016{
2017 switch (type)
2018 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002019 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002020 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 case VAR_SPECIAL: return "special";
2023 case VAR_BOOL: return "bool";
2024 case VAR_NUMBER: return "number";
2025 case VAR_FLOAT: return "float";
2026 case VAR_STRING: return "string";
2027 case VAR_BLOB: return "blob";
2028 case VAR_JOB: return "job";
2029 case VAR_CHANNEL: return "channel";
2030 case VAR_LIST: return "list";
2031 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002032
2033 case VAR_FUNC:
2034 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002036 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037}
2038
2039/*
2040 * Return the name of a type.
2041 * The result may be in allocated memory, in which case "tofree" is set.
2042 */
2043 char *
2044type_name(type_T *type, char **tofree)
2045{
2046 char *name = vartype_name(type->tt_type);
2047
2048 *tofree = NULL;
2049 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2050 {
2051 char *member_free;
2052 char *member_name = type_name(type->tt_member, &member_free);
2053 size_t len;
2054
2055 len = STRLEN(name) + STRLEN(member_name) + 3;
2056 *tofree = alloc(len);
2057 if (*tofree != NULL)
2058 {
2059 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2060 vim_free(member_free);
2061 return *tofree;
2062 }
2063 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002064 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002065 {
2066 garray_T ga;
2067 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002068 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002069
2070 ga_init2(&ga, 1, 100);
2071 if (ga_grow(&ga, 20) == FAIL)
2072 return "[unknown]";
2073 *tofree = ga.ga_data;
2074 STRCPY(ga.ga_data, "func(");
2075 ga.ga_len += 5;
2076
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002077 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002078 {
2079 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002080 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002081 int len;
2082
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002083 if (type->tt_args == NULL)
2084 arg_type = "[unknown]";
2085 else
2086 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002087 if (i > 0)
2088 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002089 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002090 ga.ga_len += 2;
2091 }
2092 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002093 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002094 {
2095 vim_free(arg_free);
2096 return "[unknown]";
2097 }
2098 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002099 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002100 {
2101 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2102 ga.ga_len += 3;
2103 }
2104 else if (i >= type->tt_min_argcount)
2105 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002106 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002107 ga.ga_len += len;
2108 vim_free(arg_free);
2109 }
2110
2111 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002112 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002113 else
2114 {
2115 char *ret_free;
2116 char *ret_name = type_name(type->tt_member, &ret_free);
2117 int len;
2118
2119 len = (int)STRLEN(ret_name) + 4;
2120 if (ga_grow(&ga, len) == FAIL)
2121 {
2122 vim_free(ret_free);
2123 return "[unknown]";
2124 }
2125 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002126 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2127 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002128 vim_free(ret_free);
2129 }
2130 return ga.ga_data;
2131 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132
2133 return name;
2134}
2135
2136/*
2137 * Find "name" in script-local items of script "sid".
2138 * Returns the index in "sn_var_vals" if found.
2139 * If found but not in "sn_var_vals" returns -1.
2140 * If not found returns -2.
2141 */
2142 int
2143get_script_item_idx(int sid, char_u *name, int check_writable)
2144{
2145 hashtab_T *ht;
2146 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002147 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 int idx;
2149
2150 // First look the name up in the hashtable.
2151 if (sid <= 0 || sid > script_items.ga_len)
2152 return -1;
2153 ht = &SCRIPT_VARS(sid);
2154 di = find_var_in_ht(ht, 0, name, TRUE);
2155 if (di == NULL)
2156 return -2;
2157
2158 // Now find the svar_T index in sn_var_vals.
2159 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2160 {
2161 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2162
2163 if (sv->sv_tv == &di->di_tv)
2164 {
2165 if (check_writable && sv->sv_const)
2166 semsg(_(e_readonlyvar), name);
2167 return idx;
2168 }
2169 }
2170 return -1;
2171}
2172
2173/*
2174 * Find "name" in imported items of the current script/
2175 */
2176 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002177find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002178{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002179 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 int idx;
2181
2182 if (cctx != NULL)
2183 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2184 {
2185 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2186 + idx;
2187
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002188 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2189 : STRLEN(import->imp_name) == len
2190 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 return import;
2192 }
2193
2194 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2195 {
2196 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2197
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002198 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2199 : STRLEN(import->imp_name) == len
2200 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 return import;
2202 }
2203 return NULL;
2204}
2205
2206/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002207 * Free all imported variables.
2208 */
2209 static void
2210free_imported(cctx_T *cctx)
2211{
2212 int idx;
2213
2214 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2215 {
2216 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2217
2218 vim_free(import->imp_name);
2219 }
2220 ga_clear(&cctx->ctx_imports);
2221}
2222
2223/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002224 * Get the next line of the function from "cctx".
2225 * Returns NULL when at the end.
2226 */
2227 static char_u *
2228next_line_from_context(cctx_T *cctx)
2229{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002230 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002231
2232 do
2233 {
2234 ++cctx->ctx_lnum;
2235 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002236 {
2237 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002238 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002239 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002240 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002241 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002242 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2243 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002244 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002245 return line;
2246}
2247
2248/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002249 * Return TRUE if "p" points at a "#" but not at "#{".
2250 */
2251 static int
2252comment_start(char_u *p)
2253{
2254 return p[0] == '#' && p[1] != '{';
2255}
2256
2257/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002258 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002259 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002260 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2261 */
2262 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002263may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002264{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002265 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002266 {
2267 char_u *next = next_line_from_context(cctx);
2268
2269 if (next == NULL)
2270 return FAIL;
2271 *arg = skipwhite(next);
2272 }
2273 return OK;
2274}
2275
2276/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002277 * Generate an instruction to load script-local variable "name", without the
2278 * leading "s:".
2279 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 */
2281 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002282compile_load_scriptvar(
2283 cctx_T *cctx,
2284 char_u *name, // variable NUL terminated
2285 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002286 char_u **end, // end of variable
2287 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002289 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2291 imported_T *import;
2292
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002293 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002295 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002296 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2297 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 }
2299 if (idx >= 0)
2300 {
2301 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2302
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002303 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 current_sctx.sc_sid, idx, sv->sv_type);
2305 return OK;
2306 }
2307
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002308 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 if (import != NULL)
2310 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002311 if (import->imp_all)
2312 {
2313 char_u *p = skipwhite(*end);
2314 int name_len;
2315 ufunc_T *ufunc;
2316 type_T *type;
2317
2318 // Used "import * as Name", need to lookup the member.
2319 if (*p != '.')
2320 {
2321 semsg(_("E1060: expected dot after name: %s"), start);
2322 return FAIL;
2323 }
2324 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002325 if (VIM_ISWHITE(*p))
2326 {
2327 emsg(_("E1074: no white space allowed after dot"));
2328 return FAIL;
2329 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002330
2331 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2332 // TODO: what if it is a function?
2333 if (idx < 0)
2334 return FAIL;
2335 *end = p;
2336
2337 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2338 import->imp_sid,
2339 idx,
2340 type);
2341 }
2342 else
2343 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002344 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002345 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2346 import->imp_sid,
2347 import->imp_var_vals_idx,
2348 import->imp_type);
2349 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 return OK;
2351 }
2352
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002353 if (error)
2354 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355 return FAIL;
2356}
2357
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002358 static int
2359generate_funcref(cctx_T *cctx, char_u *name)
2360{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002361 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002362
2363 if (ufunc == NULL)
2364 return FAIL;
2365
2366 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2367}
2368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369/*
2370 * Compile a variable name into a load instruction.
2371 * "end" points to just after the name.
2372 * When "error" is FALSE do not give an error when not found.
2373 */
2374 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002375compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376{
2377 type_T *type;
2378 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002379 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002381 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382
2383 if (*(*arg + 1) == ':')
2384 {
2385 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002386 if (end <= *arg + 2)
2387 name = vim_strsave((char_u *)"[empty]");
2388 else
2389 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 if (name == NULL)
2391 return FAIL;
2392
2393 if (**arg == 'v')
2394 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002395 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 }
2397 else if (**arg == 'g')
2398 {
2399 // Global variables can be defined later, thus we don't check if it
2400 // exists, give error at runtime.
2401 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2402 }
2403 else if (**arg == 's')
2404 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002405 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002407 else if (**arg == 'b')
2408 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002409 // Buffer-local variables can be defined later, thus we don't check
2410 // if it exists, give error at runtime.
2411 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002412 }
2413 else if (**arg == 'w')
2414 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002415 // Window-local variables can be defined later, thus we don't check
2416 // if it exists, give error at runtime.
2417 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002418 }
2419 else if (**arg == 't')
2420 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002421 // Tabpage-local variables can be defined later, thus we don't
2422 // check if it exists, give error at runtime.
2423 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002424 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 else
2426 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002427 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 goto theend;
2429 }
2430 }
2431 else
2432 {
2433 size_t len = end - *arg;
2434 int idx;
2435 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002436 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437
2438 name = vim_strnsave(*arg, end - *arg);
2439 if (name == NULL)
2440 return FAIL;
2441
2442 idx = lookup_arg(*arg, len, cctx);
2443 if (idx >= 0)
2444 {
2445 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2446 type = cctx->ctx_ufunc->uf_arg_types[idx];
2447 else
2448 type = &t_any;
2449
2450 // Arguments are located above the frame pointer.
2451 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2452 if (cctx->ctx_ufunc->uf_va_name != NULL)
2453 --idx;
2454 gen_load = TRUE;
2455 }
2456 else if (lookup_vararg(*arg, len, cctx))
2457 {
2458 // varargs is always the last argument
2459 idx = -STACK_FRAME_SIZE - 1;
2460 type = cctx->ctx_ufunc->uf_va_type;
2461 gen_load = TRUE;
2462 }
2463 else
2464 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002465 lvar_T *lvar = lookup_local(*arg, len, cctx);
2466
2467 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002469 type = lvar->lv_type;
2470 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002471 if (lvar->lv_from_outer)
2472 gen_load_outer = TRUE;
2473 else
2474 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 }
2476 else
2477 {
2478 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2479 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2480 res = generate_PUSHBOOL(cctx, **arg == 't'
2481 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002482 else
2483 {
2484 // "var" can be script-local even without using "s:" if it
2485 // already exists.
2486 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2487 == SCRIPT_VERSION_VIM9
2488 || lookup_script(*arg, len) == OK)
2489 res = compile_load_scriptvar(cctx, name, *arg, &end,
2490 FALSE);
2491
2492 // When the name starts with an uppercase letter or "x:" it
2493 // can be a user defined function.
2494 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2495 res = generate_funcref(cctx, name);
2496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 }
2498 }
2499 if (gen_load)
2500 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002501 if (gen_load_outer)
2502 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 }
2504
2505 *arg = end;
2506
2507theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002508 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 semsg(_(e_var_notfound), name);
2510 vim_free(name);
2511 return res;
2512}
2513
2514/*
2515 * Compile the argument expressions.
2516 * "arg" points to just after the "(" and is advanced to after the ")"
2517 */
2518 static int
2519compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2520{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002521 char_u *p = *arg;
2522 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523
Bram Moolenaare6085c52020-04-12 20:19:16 +02002524 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002526 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002527 {
2528 p = next_line_from_context(cctx);
2529 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002530 goto failret;
2531 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002532 p = skipwhite(p);
2533 }
2534 if (*p == ')')
2535 {
2536 *arg = p + 1;
2537 return OK;
2538 }
2539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 if (compile_expr1(&p, cctx) == FAIL)
2541 return FAIL;
2542 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002543
2544 if (*p != ',' && *skipwhite(p) == ',')
2545 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002546 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002547 p = skipwhite(p);
2548 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002550 {
2551 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002552 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002553 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002554 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002555 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002556 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002558failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002559 emsg(_(e_missing_close));
2560 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561}
2562
2563/*
2564 * Compile a function call: name(arg1, arg2)
2565 * "arg" points to "name", "arg + varlen" to the "(".
2566 * "argcount_init" is 1 for "value->method()"
2567 * Instructions:
2568 * EVAL arg1
2569 * EVAL arg2
2570 * BCALL / DCALL / UCALL
2571 */
2572 static int
2573compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2574{
2575 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002576 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 int argcount = argcount_init;
2578 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002579 char_u fname_buf[FLEN_FIXED + 1];
2580 char_u *tofree = NULL;
2581 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002583 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584
2585 if (varlen >= sizeof(namebuf))
2586 {
2587 semsg(_("E1011: name too long: %s"), name);
2588 return FAIL;
2589 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002590 vim_strncpy(namebuf, *arg, varlen);
2591 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592
2593 *arg = skipwhite(*arg + varlen + 1);
2594 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002595 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002597 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 {
2599 int idx;
2600
2601 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002602 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002604 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002605 else
2606 semsg(_(e_unknownfunc), namebuf);
2607 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 }
2609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002611 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002613 {
2614 res = generate_CALL(cctx, ufunc, argcount);
2615 goto theend;
2616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617
2618 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002619 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002621 if (STRNCMP(namebuf, "g:", 2) != 0
2622 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002623 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002624 garray_T *stack = &cctx->ctx_type_stack;
2625 type_T *type;
2626
2627 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2628 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002629 goto theend;
2630 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002632 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002633 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002634 if (STRNCMP(namebuf, "g:", 2) == 0)
2635 res = generate_UCALL(cctx, name, argcount);
2636 else
2637 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002638
2639theend:
2640 vim_free(tofree);
2641 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642}
2643
2644// like NAMESPACE_CHAR but with 'a' and 'l'.
2645#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2646
2647/*
2648 * Find the end of a variable or function name. Unlike find_name_end() this
2649 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002650 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 * Return a pointer to just after the name. Equal to "arg" if there is no
2652 * valid name.
2653 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002654 static char_u *
2655to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656{
2657 char_u *p;
2658
2659 // Quick check for valid starting character.
2660 if (!eval_isnamec1(*arg))
2661 return arg;
2662
2663 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2664 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2665 // and can be used in slice "[n:]".
2666 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002667 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2669 break;
2670 return p;
2671}
2672
2673/*
2674 * Like to_name_end() but also skip over a list or dict constant.
2675 */
2676 char_u *
2677to_name_const_end(char_u *arg)
2678{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002679 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 typval_T rettv;
2681
2682 if (p == arg && *arg == '[')
2683 {
2684
2685 // Can be "[1, 2, 3]->Func()".
2686 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2687 p = arg;
2688 }
2689 else if (p == arg && *arg == '#' && arg[1] == '{')
2690 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002691 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 ++p;
2693 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2694 p = arg;
2695 }
2696 else if (p == arg && *arg == '{')
2697 {
2698 int ret = get_lambda_tv(&p, &rettv, FALSE);
2699
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002700 // Can be "{x -> ret}()".
2701 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 if (ret == NOTDONE)
2703 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2704 if (ret != OK)
2705 p = arg;
2706 }
2707
2708 return p;
2709}
2710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711/*
2712 * parse a list: [expr, expr]
2713 * "*arg" points to the '['.
2714 */
2715 static int
2716compile_list(char_u **arg, cctx_T *cctx)
2717{
2718 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002719 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 int count = 0;
2721
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002722 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002724 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002725 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002726 p = next_line_from_context(cctx);
2727 if (p == NULL)
2728 {
2729 semsg(_(e_list_end), *arg);
2730 return FAIL;
2731 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002732 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002733 p = skipwhite(p);
2734 }
2735 if (*p == ']')
2736 {
2737 ++p;
2738 // Allow for following comment, after at least one space.
2739 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2740 p += STRLEN(p);
2741 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 if (compile_expr1(&p, cctx) == FAIL)
2744 break;
2745 ++count;
2746 if (*p == ',')
2747 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002748 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 p = skipwhite(p);
2750 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002751 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752
2753 generate_NEWLIST(cctx, count);
2754 return OK;
2755}
2756
2757/*
2758 * parse a lambda: {arg, arg -> expr}
2759 * "*arg" points to the '{'.
2760 */
2761 static int
2762compile_lambda(char_u **arg, cctx_T *cctx)
2763{
2764 garray_T *instr = &cctx->ctx_instr;
2765 typval_T rettv;
2766 ufunc_T *ufunc;
2767
2768 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002769 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002773 ++ufunc->uf_refcount;
2774 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002775 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776
2777 // The function will have one line: "return {expr}".
2778 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002779 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780
2781 if (ufunc->uf_dfunc_idx >= 0)
2782 {
2783 if (ga_grow(instr, 1) == FAIL)
2784 return FAIL;
2785 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2786 return OK;
2787 }
2788 return FAIL;
2789}
2790
2791/*
2792 * Compile a lamda call: expr->{lambda}(args)
2793 * "arg" points to the "{".
2794 */
2795 static int
2796compile_lambda_call(char_u **arg, cctx_T *cctx)
2797{
2798 ufunc_T *ufunc;
2799 typval_T rettv;
2800 int argcount = 1;
2801 int ret = FAIL;
2802
2803 // Get the funcref in "rettv".
2804 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2805 return FAIL;
2806
2807 if (**arg != '(')
2808 {
2809 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002810 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 else
2812 semsg(_(e_missing_paren), "lambda");
2813 clear_tv(&rettv);
2814 return FAIL;
2815 }
2816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 ufunc = rettv.vval.v_partial->pt_func;
2818 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002819 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002820 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002821
2822 // The function will have one line: "return {expr}".
2823 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002824 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825
2826 // compile the arguments
2827 *arg = skipwhite(*arg + 1);
2828 if (compile_arguments(arg, cctx, &argcount) == OK)
2829 // call the compiled function
2830 ret = generate_CALL(cctx, ufunc, argcount);
2831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 return ret;
2833}
2834
2835/*
2836 * parse a dict: {'key': val} or #{key: val}
2837 * "*arg" points to the '{'.
2838 */
2839 static int
2840compile_dict(char_u **arg, cctx_T *cctx, int literal)
2841{
2842 garray_T *instr = &cctx->ctx_instr;
2843 int count = 0;
2844 dict_T *d = dict_alloc();
2845 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002846 char_u *whitep = *arg;
2847 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848
2849 if (d == NULL)
2850 return FAIL;
2851 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002852 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 {
2854 char_u *key = NULL;
2855
Bram Moolenaar2c330432020-04-13 14:41:35 +02002856 while (**arg == NUL || (literal && **arg == '"')
2857 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002858 {
2859 *arg = next_line_from_context(cctx);
2860 if (*arg == NULL)
2861 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002862 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002863 *arg = skipwhite(*arg);
2864 }
2865
2866 if (**arg == '}')
2867 break;
2868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 if (literal)
2870 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002871 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872
Bram Moolenaar2c330432020-04-13 14:41:35 +02002873 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 {
2875 semsg(_("E1014: Invalid key: %s"), *arg);
2876 return FAIL;
2877 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002878 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 if (generate_PUSHS(cctx, key) == FAIL)
2880 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002881 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 }
2883 else
2884 {
2885 isn_T *isn;
2886
2887 if (compile_expr1(arg, cctx) == FAIL)
2888 return FAIL;
2889 // TODO: check type is string
2890 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2891 if (isn->isn_type == ISN_PUSHS)
2892 key = isn->isn_arg.string;
2893 }
2894
2895 // Check for duplicate keys, if using string keys.
2896 if (key != NULL)
2897 {
2898 item = dict_find(d, key, -1);
2899 if (item != NULL)
2900 {
2901 semsg(_(e_duplicate_key), key);
2902 goto failret;
2903 }
2904 item = dictitem_alloc(key);
2905 if (item != NULL)
2906 {
2907 item->di_tv.v_type = VAR_UNKNOWN;
2908 item->di_tv.v_lock = 0;
2909 if (dict_add(d, item) == FAIL)
2910 dictitem_free(item);
2911 }
2912 }
2913
2914 *arg = skipwhite(*arg);
2915 if (**arg != ':')
2916 {
2917 semsg(_(e_missing_dict_colon), *arg);
2918 return FAIL;
2919 }
2920
Bram Moolenaar2c330432020-04-13 14:41:35 +02002921 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002923 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002924 {
2925 *arg = next_line_from_context(cctx);
2926 if (*arg == NULL)
2927 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002928 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002929 *arg = skipwhite(*arg);
2930 }
2931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 if (compile_expr1(arg, cctx) == FAIL)
2933 return FAIL;
2934 ++count;
2935
Bram Moolenaar2c330432020-04-13 14:41:35 +02002936 whitep = *arg;
2937 p = skipwhite(*arg);
2938 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002939 {
2940 *arg = next_line_from_context(cctx);
2941 if (*arg == NULL)
2942 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002943 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002944 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002945 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002946 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 if (**arg == '}')
2948 break;
2949 if (**arg != ',')
2950 {
2951 semsg(_(e_missing_dict_comma), *arg);
2952 goto failret;
2953 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002954 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 *arg = skipwhite(*arg + 1);
2956 }
2957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 *arg = *arg + 1;
2959
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002960 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002961 p = skipwhite(*arg);
2962 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002963 *arg += STRLEN(*arg);
2964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 dict_unref(d);
2966 return generate_NEWDICT(cctx, count);
2967
2968failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002969 if (*arg == NULL)
2970 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 dict_unref(d);
2972 return FAIL;
2973}
2974
2975/*
2976 * Compile "&option".
2977 */
2978 static int
2979compile_get_option(char_u **arg, cctx_T *cctx)
2980{
2981 typval_T rettv;
2982 char_u *start = *arg;
2983 int ret;
2984
2985 // parse the option and get the current value to get the type.
2986 rettv.v_type = VAR_UNKNOWN;
2987 ret = get_option_tv(arg, &rettv, TRUE);
2988 if (ret == OK)
2989 {
2990 // include the '&' in the name, get_option_tv() expects it.
2991 char_u *name = vim_strnsave(start, *arg - start);
2992 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2993
2994 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2995 vim_free(name);
2996 }
2997 clear_tv(&rettv);
2998
2999 return ret;
3000}
3001
3002/*
3003 * Compile "$VAR".
3004 */
3005 static int
3006compile_get_env(char_u **arg, cctx_T *cctx)
3007{
3008 char_u *start = *arg;
3009 int len;
3010 int ret;
3011 char_u *name;
3012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 ++*arg;
3014 len = get_env_len(arg);
3015 if (len == 0)
3016 {
3017 semsg(_(e_syntax_at), start - 1);
3018 return FAIL;
3019 }
3020
3021 // include the '$' in the name, get_env_tv() expects it.
3022 name = vim_strnsave(start, len + 1);
3023 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3024 vim_free(name);
3025 return ret;
3026}
3027
3028/*
3029 * Compile "@r".
3030 */
3031 static int
3032compile_get_register(char_u **arg, cctx_T *cctx)
3033{
3034 int ret;
3035
3036 ++*arg;
3037 if (**arg == NUL)
3038 {
3039 semsg(_(e_syntax_at), *arg - 1);
3040 return FAIL;
3041 }
3042 if (!valid_yank_reg(**arg, TRUE))
3043 {
3044 emsg_invreg(**arg);
3045 return FAIL;
3046 }
3047 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3048 ++*arg;
3049 return ret;
3050}
3051
3052/*
3053 * Apply leading '!', '-' and '+' to constant "rettv".
3054 */
3055 static int
3056apply_leader(typval_T *rettv, char_u *start, char_u *end)
3057{
3058 char_u *p = end;
3059
3060 // this works from end to start
3061 while (p > start)
3062 {
3063 --p;
3064 if (*p == '-' || *p == '+')
3065 {
3066 // only '-' has an effect, for '+' we only check the type
3067#ifdef FEAT_FLOAT
3068 if (rettv->v_type == VAR_FLOAT)
3069 {
3070 if (*p == '-')
3071 rettv->vval.v_float = -rettv->vval.v_float;
3072 }
3073 else
3074#endif
3075 {
3076 varnumber_T val;
3077 int error = FALSE;
3078
3079 // tv_get_number_chk() accepts a string, but we don't want that
3080 // here
3081 if (check_not_string(rettv) == FAIL)
3082 return FAIL;
3083 val = tv_get_number_chk(rettv, &error);
3084 clear_tv(rettv);
3085 if (error)
3086 return FAIL;
3087 if (*p == '-')
3088 val = -val;
3089 rettv->v_type = VAR_NUMBER;
3090 rettv->vval.v_number = val;
3091 }
3092 }
3093 else
3094 {
3095 int v = tv2bool(rettv);
3096
3097 // '!' is permissive in the type.
3098 clear_tv(rettv);
3099 rettv->v_type = VAR_BOOL;
3100 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3101 }
3102 }
3103 return OK;
3104}
3105
3106/*
3107 * Recognize v: variables that are constants and set "rettv".
3108 */
3109 static void
3110get_vim_constant(char_u **arg, typval_T *rettv)
3111{
3112 if (STRNCMP(*arg, "v:true", 6) == 0)
3113 {
3114 rettv->v_type = VAR_BOOL;
3115 rettv->vval.v_number = VVAL_TRUE;
3116 *arg += 6;
3117 }
3118 else if (STRNCMP(*arg, "v:false", 7) == 0)
3119 {
3120 rettv->v_type = VAR_BOOL;
3121 rettv->vval.v_number = VVAL_FALSE;
3122 *arg += 7;
3123 }
3124 else if (STRNCMP(*arg, "v:null", 6) == 0)
3125 {
3126 rettv->v_type = VAR_SPECIAL;
3127 rettv->vval.v_number = VVAL_NULL;
3128 *arg += 6;
3129 }
3130 else if (STRNCMP(*arg, "v:none", 6) == 0)
3131 {
3132 rettv->v_type = VAR_SPECIAL;
3133 rettv->vval.v_number = VVAL_NONE;
3134 *arg += 6;
3135 }
3136}
3137
3138/*
3139 * Compile code to apply '-', '+' and '!'.
3140 */
3141 static int
3142compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3143{
3144 char_u *p = end;
3145
3146 // this works from end to start
3147 while (p > start)
3148 {
3149 --p;
3150 if (*p == '-' || *p == '+')
3151 {
3152 int negate = *p == '-';
3153 isn_T *isn;
3154
3155 // TODO: check type
3156 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3157 {
3158 --p;
3159 if (*p == '-')
3160 negate = !negate;
3161 }
3162 // only '-' has an effect, for '+' we only check the type
3163 if (negate)
3164 isn = generate_instr(cctx, ISN_NEGATENR);
3165 else
3166 isn = generate_instr(cctx, ISN_CHECKNR);
3167 if (isn == NULL)
3168 return FAIL;
3169 }
3170 else
3171 {
3172 int invert = TRUE;
3173
3174 while (p > start && p[-1] == '!')
3175 {
3176 --p;
3177 invert = !invert;
3178 }
3179 if (generate_2BOOL(cctx, invert) == FAIL)
3180 return FAIL;
3181 }
3182 }
3183 return OK;
3184}
3185
3186/*
3187 * Compile whatever comes after "name" or "name()".
3188 */
3189 static int
3190compile_subscript(
3191 char_u **arg,
3192 cctx_T *cctx,
3193 char_u **start_leader,
3194 char_u *end_leader)
3195{
3196 for (;;)
3197 {
3198 if (**arg == '(')
3199 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003200 garray_T *stack = &cctx->ctx_type_stack;
3201 type_T *type;
3202 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203
3204 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003205 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 *arg = skipwhite(*arg + 1);
3208 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3209 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003210 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 return FAIL;
3212 }
3213 else if (**arg == '-' && (*arg)[1] == '>')
3214 {
3215 char_u *p;
3216
3217 // something->method()
3218 // Apply the '!', '-' and '+' first:
3219 // -1.0->func() works like (-1.0)->func()
3220 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3221 return FAIL;
3222 *start_leader = end_leader; // don't apply again later
3223
3224 *arg = skipwhite(*arg + 2);
3225 if (**arg == '{')
3226 {
3227 // lambda call: list->{lambda}
3228 if (compile_lambda_call(arg, cctx) == FAIL)
3229 return FAIL;
3230 }
3231 else
3232 {
3233 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003234 p = *arg;
3235 if (ASCII_ISALPHA(*p) && p[1] == ':')
3236 p += 2;
3237 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 ;
3239 if (*p != '(')
3240 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003241 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 return FAIL;
3243 }
3244 // TODO: base value may not be the first argument
3245 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
3246 return FAIL;
3247 }
3248 }
3249 else if (**arg == '[')
3250 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003251 garray_T *stack;
3252 type_T **typep;
3253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 // list index: list[123]
3255 // TODO: more arguments
3256 // TODO: dict member dict['name']
3257 *arg = skipwhite(*arg + 1);
3258 if (compile_expr1(arg, cctx) == FAIL)
3259 return FAIL;
3260
3261 if (**arg != ']')
3262 {
3263 emsg(_(e_missbrac));
3264 return FAIL;
3265 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003266 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267
3268 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3269 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003270 stack = &cctx->ctx_type_stack;
3271 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3272 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3273 {
3274 emsg(_(e_listreq));
3275 return FAIL;
3276 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003277 if ((*typep)->tt_type == VAR_LIST)
3278 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 }
3280 else if (**arg == '.' && (*arg)[1] != '.')
3281 {
3282 char_u *p;
3283
3284 ++*arg;
3285 p = *arg;
3286 // dictionary member: dict.name
3287 if (eval_isnamec1(*p))
3288 while (eval_isnamec(*p))
3289 MB_PTR_ADV(p);
3290 if (p == *arg)
3291 {
3292 semsg(_(e_syntax_at), *arg);
3293 return FAIL;
3294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3296 return FAIL;
3297 *arg = p;
3298 }
3299 else
3300 break;
3301 }
3302
3303 // TODO - see handle_subscript():
3304 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3305 // Don't do this when "Func" is already a partial that was bound
3306 // explicitly (pt_auto is FALSE).
3307
3308 return OK;
3309}
3310
3311/*
3312 * Compile an expression at "*p" and add instructions to "instr".
3313 * "p" is advanced until after the expression, skipping white space.
3314 *
3315 * This is the equivalent of eval1(), eval2(), etc.
3316 */
3317
3318/*
3319 * number number constant
3320 * 0zFFFFFFFF Blob constant
3321 * "string" string constant
3322 * 'string' literal string constant
3323 * &option-name option value
3324 * @r register contents
3325 * identifier variable value
3326 * function() function call
3327 * $VAR environment variable
3328 * (expression) nested expression
3329 * [expr, expr] List
3330 * {key: val, key: val} Dictionary
3331 * #{key: val, key: val} Dictionary with literal keys
3332 *
3333 * Also handle:
3334 * ! in front logical NOT
3335 * - in front unary minus
3336 * + in front unary plus (ignored)
3337 * trailing (arg) funcref/partial call
3338 * trailing [] subscript in String or List
3339 * trailing .name entry in Dictionary
3340 * trailing ->name() method call
3341 */
3342 static int
3343compile_expr7(char_u **arg, cctx_T *cctx)
3344{
3345 typval_T rettv;
3346 char_u *start_leader, *end_leader;
3347 int ret = OK;
3348
3349 /*
3350 * Skip '!', '-' and '+' characters. They are handled later.
3351 */
3352 start_leader = *arg;
3353 while (**arg == '!' || **arg == '-' || **arg == '+')
3354 *arg = skipwhite(*arg + 1);
3355 end_leader = *arg;
3356
3357 rettv.v_type = VAR_UNKNOWN;
3358 switch (**arg)
3359 {
3360 /*
3361 * Number constant.
3362 */
3363 case '0': // also for blob starting with 0z
3364 case '1':
3365 case '2':
3366 case '3':
3367 case '4':
3368 case '5':
3369 case '6':
3370 case '7':
3371 case '8':
3372 case '9':
3373 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3374 return FAIL;
3375 break;
3376
3377 /*
3378 * String constant: "string".
3379 */
3380 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3381 return FAIL;
3382 break;
3383
3384 /*
3385 * Literal string constant: 'str''ing'.
3386 */
3387 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3388 return FAIL;
3389 break;
3390
3391 /*
3392 * Constant Vim variable.
3393 */
3394 case 'v': get_vim_constant(arg, &rettv);
3395 ret = NOTDONE;
3396 break;
3397
3398 /*
3399 * List: [expr, expr]
3400 */
3401 case '[': ret = compile_list(arg, cctx);
3402 break;
3403
3404 /*
3405 * Dictionary: #{key: val, key: val}
3406 */
3407 case '#': if ((*arg)[1] == '{')
3408 {
3409 ++*arg;
3410 ret = compile_dict(arg, cctx, TRUE);
3411 }
3412 else
3413 ret = NOTDONE;
3414 break;
3415
3416 /*
3417 * Lambda: {arg, arg -> expr}
3418 * Dictionary: {'key': val, 'key': val}
3419 */
3420 case '{': {
3421 char_u *start = skipwhite(*arg + 1);
3422
3423 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003424 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003426 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 if (ret != FAIL && *start == '>')
3428 ret = compile_lambda(arg, cctx);
3429 else
3430 ret = compile_dict(arg, cctx, FALSE);
3431 }
3432 break;
3433
3434 /*
3435 * Option value: &name
3436 */
3437 case '&': ret = compile_get_option(arg, cctx);
3438 break;
3439
3440 /*
3441 * Environment variable: $VAR.
3442 */
3443 case '$': ret = compile_get_env(arg, cctx);
3444 break;
3445
3446 /*
3447 * Register contents: @r.
3448 */
3449 case '@': ret = compile_get_register(arg, cctx);
3450 break;
3451 /*
3452 * nested expression: (expression).
3453 */
3454 case '(': *arg = skipwhite(*arg + 1);
3455 ret = compile_expr1(arg, cctx); // recursive!
3456 *arg = skipwhite(*arg);
3457 if (**arg == ')')
3458 ++*arg;
3459 else if (ret == OK)
3460 {
3461 emsg(_(e_missing_close));
3462 ret = FAIL;
3463 }
3464 break;
3465
3466 default: ret = NOTDONE;
3467 break;
3468 }
3469 if (ret == FAIL)
3470 return FAIL;
3471
3472 if (rettv.v_type != VAR_UNKNOWN)
3473 {
3474 // apply the '!', '-' and '+' before the constant
3475 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3476 {
3477 clear_tv(&rettv);
3478 return FAIL;
3479 }
3480 start_leader = end_leader; // don't apply again below
3481
3482 // push constant
3483 switch (rettv.v_type)
3484 {
3485 case VAR_BOOL:
3486 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3487 break;
3488 case VAR_SPECIAL:
3489 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3490 break;
3491 case VAR_NUMBER:
3492 generate_PUSHNR(cctx, rettv.vval.v_number);
3493 break;
3494#ifdef FEAT_FLOAT
3495 case VAR_FLOAT:
3496 generate_PUSHF(cctx, rettv.vval.v_float);
3497 break;
3498#endif
3499 case VAR_BLOB:
3500 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3501 rettv.vval.v_blob = NULL;
3502 break;
3503 case VAR_STRING:
3504 generate_PUSHS(cctx, rettv.vval.v_string);
3505 rettv.vval.v_string = NULL;
3506 break;
3507 default:
3508 iemsg("constant type missing");
3509 return FAIL;
3510 }
3511 }
3512 else if (ret == NOTDONE)
3513 {
3514 char_u *p;
3515 int r;
3516
3517 if (!eval_isnamec1(**arg))
3518 {
3519 semsg(_("E1015: Name expected: %s"), *arg);
3520 return FAIL;
3521 }
3522
3523 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003524 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 if (*p == '(')
3526 r = compile_call(arg, p - *arg, cctx, 0);
3527 else
3528 r = compile_load(arg, p, cctx, TRUE);
3529 if (r == FAIL)
3530 return FAIL;
3531 }
3532
3533 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3534 return FAIL;
3535
3536 // Now deal with prefixed '-', '+' and '!', if not done already.
3537 return compile_leader(cctx, start_leader, end_leader);
3538}
3539
3540/*
3541 * * number multiplication
3542 * / number division
3543 * % number modulo
3544 */
3545 static int
3546compile_expr6(char_u **arg, cctx_T *cctx)
3547{
3548 char_u *op;
3549
3550 // get the first variable
3551 if (compile_expr7(arg, cctx) == FAIL)
3552 return FAIL;
3553
3554 /*
3555 * Repeat computing, until no "*", "/" or "%" is following.
3556 */
3557 for (;;)
3558 {
3559 op = skipwhite(*arg);
3560 if (*op != '*' && *op != '/' && *op != '%')
3561 break;
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003562 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 {
3564 char_u buf[3];
3565
3566 vim_strncpy(buf, op, 1);
3567 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003568 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 }
3570 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003571 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003572 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573
3574 // get the second variable
3575 if (compile_expr7(arg, cctx) == FAIL)
3576 return FAIL;
3577
3578 generate_two_op(cctx, op);
3579 }
3580
3581 return OK;
3582}
3583
3584/*
3585 * + number addition
3586 * - number subtraction
3587 * .. string concatenation
3588 */
3589 static int
3590compile_expr5(char_u **arg, cctx_T *cctx)
3591{
3592 char_u *op;
3593 int oplen;
3594
3595 // get the first variable
3596 if (compile_expr6(arg, cctx) == FAIL)
3597 return FAIL;
3598
3599 /*
3600 * Repeat computing, until no "+", "-" or ".." is following.
3601 */
3602 for (;;)
3603 {
3604 op = skipwhite(*arg);
3605 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3606 break;
3607 oplen = (*op == '.' ? 2 : 1);
3608
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003609 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 {
3611 char_u buf[3];
3612
3613 vim_strncpy(buf, op, oplen);
3614 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003615 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 }
3617
3618 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003619 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003620 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621
3622 // get the second variable
3623 if (compile_expr6(arg, cctx) == FAIL)
3624 return FAIL;
3625
3626 if (*op == '.')
3627 {
3628 if (may_generate_2STRING(-2, cctx) == FAIL
3629 || may_generate_2STRING(-1, cctx) == FAIL)
3630 return FAIL;
3631 generate_instr_drop(cctx, ISN_CONCAT, 1);
3632 }
3633 else
3634 generate_two_op(cctx, op);
3635 }
3636
3637 return OK;
3638}
3639
Bram Moolenaar080457c2020-03-03 21:53:32 +01003640 static exptype_T
3641get_compare_type(char_u *p, int *len, int *type_is)
3642{
3643 exptype_T type = EXPR_UNKNOWN;
3644 int i;
3645
3646 switch (p[0])
3647 {
3648 case '=': if (p[1] == '=')
3649 type = EXPR_EQUAL;
3650 else if (p[1] == '~')
3651 type = EXPR_MATCH;
3652 break;
3653 case '!': if (p[1] == '=')
3654 type = EXPR_NEQUAL;
3655 else if (p[1] == '~')
3656 type = EXPR_NOMATCH;
3657 break;
3658 case '>': if (p[1] != '=')
3659 {
3660 type = EXPR_GREATER;
3661 *len = 1;
3662 }
3663 else
3664 type = EXPR_GEQUAL;
3665 break;
3666 case '<': if (p[1] != '=')
3667 {
3668 type = EXPR_SMALLER;
3669 *len = 1;
3670 }
3671 else
3672 type = EXPR_SEQUAL;
3673 break;
3674 case 'i': if (p[1] == 's')
3675 {
3676 // "is" and "isnot"; but not a prefix of a name
3677 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3678 *len = 5;
3679 i = p[*len];
3680 if (!isalnum(i) && i != '_')
3681 {
3682 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3683 *type_is = TRUE;
3684 }
3685 }
3686 break;
3687 }
3688 return type;
3689}
3690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691/*
3692 * expr5a == expr5b
3693 * expr5a =~ expr5b
3694 * expr5a != expr5b
3695 * expr5a !~ expr5b
3696 * expr5a > expr5b
3697 * expr5a >= expr5b
3698 * expr5a < expr5b
3699 * expr5a <= expr5b
3700 * expr5a is expr5b
3701 * expr5a isnot expr5b
3702 *
3703 * Produces instructions:
3704 * EVAL expr5a Push result of "expr5a"
3705 * EVAL expr5b Push result of "expr5b"
3706 * COMPARE one of the compare instructions
3707 */
3708 static int
3709compile_expr4(char_u **arg, cctx_T *cctx)
3710{
3711 exptype_T type = EXPR_UNKNOWN;
3712 char_u *p;
3713 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 int type_is = FALSE;
3715
3716 // get the first variable
3717 if (compile_expr5(arg, cctx) == FAIL)
3718 return FAIL;
3719
3720 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003721 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722
3723 /*
3724 * If there is a comparative operator, use it.
3725 */
3726 if (type != EXPR_UNKNOWN)
3727 {
3728 int ic = FALSE; // Default: do not ignore case
3729
3730 if (type_is && (p[len] == '?' || p[len] == '#'))
3731 {
3732 semsg(_(e_invexpr2), *arg);
3733 return FAIL;
3734 }
3735 // extra question mark appended: ignore case
3736 if (p[len] == '?')
3737 {
3738 ic = TRUE;
3739 ++len;
3740 }
3741 // extra '#' appended: match case (ignored)
3742 else if (p[len] == '#')
3743 ++len;
3744 // nothing appended: match case
3745
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003746 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 {
3748 char_u buf[7];
3749
3750 vim_strncpy(buf, p, len);
3751 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003752 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 }
3754
3755 // get the second variable
3756 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003757 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003758 return FAIL;
3759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 if (compile_expr5(arg, cctx) == FAIL)
3761 return FAIL;
3762
3763 generate_COMPARE(cctx, type, ic);
3764 }
3765
3766 return OK;
3767}
3768
3769/*
3770 * Compile || or &&.
3771 */
3772 static int
3773compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3774{
3775 char_u *p = skipwhite(*arg);
3776 int opchar = *op;
3777
3778 if (p[0] == opchar && p[1] == opchar)
3779 {
3780 garray_T *instr = &cctx->ctx_instr;
3781 garray_T end_ga;
3782
3783 /*
3784 * Repeat until there is no following "||" or "&&"
3785 */
3786 ga_init2(&end_ga, sizeof(int), 10);
3787 while (p[0] == opchar && p[1] == opchar)
3788 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003789 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3790 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003792 return FAIL;
3793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794
3795 if (ga_grow(&end_ga, 1) == FAIL)
3796 {
3797 ga_clear(&end_ga);
3798 return FAIL;
3799 }
3800 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3801 ++end_ga.ga_len;
3802 generate_JUMP(cctx, opchar == '|'
3803 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3804
3805 // eval the next expression
3806 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003807 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003808 return FAIL;
3809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 if ((opchar == '|' ? compile_expr3(arg, cctx)
3811 : compile_expr4(arg, cctx)) == FAIL)
3812 {
3813 ga_clear(&end_ga);
3814 return FAIL;
3815 }
3816 p = skipwhite(*arg);
3817 }
3818
3819 // Fill in the end label in all jumps.
3820 while (end_ga.ga_len > 0)
3821 {
3822 isn_T *isn;
3823
3824 --end_ga.ga_len;
3825 isn = ((isn_T *)instr->ga_data)
3826 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3827 isn->isn_arg.jump.jump_where = instr->ga_len;
3828 }
3829 ga_clear(&end_ga);
3830 }
3831
3832 return OK;
3833}
3834
3835/*
3836 * expr4a && expr4a && expr4a logical AND
3837 *
3838 * Produces instructions:
3839 * EVAL expr4a Push result of "expr4a"
3840 * JUMP_AND_KEEP_IF_FALSE end
3841 * EVAL expr4b Push result of "expr4b"
3842 * JUMP_AND_KEEP_IF_FALSE end
3843 * EVAL expr4c Push result of "expr4c"
3844 * end:
3845 */
3846 static int
3847compile_expr3(char_u **arg, cctx_T *cctx)
3848{
3849 // get the first variable
3850 if (compile_expr4(arg, cctx) == FAIL)
3851 return FAIL;
3852
3853 // || and && work almost the same
3854 return compile_and_or(arg, cctx, "&&");
3855}
3856
3857/*
3858 * expr3a || expr3b || expr3c logical OR
3859 *
3860 * Produces instructions:
3861 * EVAL expr3a Push result of "expr3a"
3862 * JUMP_AND_KEEP_IF_TRUE end
3863 * EVAL expr3b Push result of "expr3b"
3864 * JUMP_AND_KEEP_IF_TRUE end
3865 * EVAL expr3c Push result of "expr3c"
3866 * end:
3867 */
3868 static int
3869compile_expr2(char_u **arg, cctx_T *cctx)
3870{
3871 // eval the first expression
3872 if (compile_expr3(arg, cctx) == FAIL)
3873 return FAIL;
3874
3875 // || and && work almost the same
3876 return compile_and_or(arg, cctx, "||");
3877}
3878
3879/*
3880 * Toplevel expression: expr2 ? expr1a : expr1b
3881 *
3882 * Produces instructions:
3883 * EVAL expr2 Push result of "expr"
3884 * JUMP_IF_FALSE alt jump if false
3885 * EVAL expr1a
3886 * JUMP_ALWAYS end
3887 * alt: EVAL expr1b
3888 * end:
3889 */
3890 static int
3891compile_expr1(char_u **arg, cctx_T *cctx)
3892{
3893 char_u *p;
3894
3895 // evaluate the first expression
3896 if (compile_expr2(arg, cctx) == FAIL)
3897 return FAIL;
3898
3899 p = skipwhite(*arg);
3900 if (*p == '?')
3901 {
3902 garray_T *instr = &cctx->ctx_instr;
3903 garray_T *stack = &cctx->ctx_type_stack;
3904 int alt_idx = instr->ga_len;
3905 int end_idx;
3906 isn_T *isn;
3907 type_T *type1;
3908 type_T *type2;
3909
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003910 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3911 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003913 return FAIL;
3914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915
3916 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3917
3918 // evaluate the second expression; any type is accepted
3919 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003920 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003921 return FAIL;
3922
Bram Moolenaara6d53682020-01-28 23:04:06 +01003923 if (compile_expr1(arg, cctx) == FAIL)
3924 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925
3926 // remember the type and drop it
3927 --stack->ga_len;
3928 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3929
3930 end_idx = instr->ga_len;
3931 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3932
3933 // jump here from JUMP_IF_FALSE
3934 isn = ((isn_T *)instr->ga_data) + alt_idx;
3935 isn->isn_arg.jump.jump_where = instr->ga_len;
3936
3937 // Check for the ":".
3938 p = skipwhite(*arg);
3939 if (*p != ':')
3940 {
3941 emsg(_(e_missing_colon));
3942 return FAIL;
3943 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003944 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3945 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003947 return FAIL;
3948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949
3950 // evaluate the third expression
3951 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003952 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003953 return FAIL;
3954
Bram Moolenaara6d53682020-01-28 23:04:06 +01003955 if (compile_expr1(arg, cctx) == FAIL)
3956 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957
3958 // If the types differ, the result has a more generic type.
3959 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003960 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961
3962 // jump here from JUMP_ALWAYS
3963 isn = ((isn_T *)instr->ga_data) + end_idx;
3964 isn->isn_arg.jump.jump_where = instr->ga_len;
3965 }
3966 return OK;
3967}
3968
3969/*
3970 * compile "return [expr]"
3971 */
3972 static char_u *
3973compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3974{
3975 char_u *p = arg;
3976 garray_T *stack = &cctx->ctx_type_stack;
3977 type_T *stack_type;
3978
3979 if (*p != NUL && *p != '|' && *p != '\n')
3980 {
3981 // compile return argument into instructions
3982 if (compile_expr1(&p, cctx) == FAIL)
3983 return NULL;
3984
3985 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3986 if (set_return_type)
3987 cctx->ctx_ufunc->uf_ret_type = stack_type;
3988 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3989 == FAIL)
3990 return NULL;
3991 }
3992 else
3993 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003994 // "set_return_type" cannot be TRUE, only used for a lambda which
3995 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02003996 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
3997 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 {
3999 emsg(_("E1003: Missing return value"));
4000 return NULL;
4001 }
4002
4003 // No argument, return zero.
4004 generate_PUSHNR(cctx, 0);
4005 }
4006
4007 if (generate_instr(cctx, ISN_RETURN) == NULL)
4008 return NULL;
4009
4010 // "return val | endif" is possible
4011 return skipwhite(p);
4012}
4013
4014/*
4015 * Return the length of an assignment operator, or zero if there isn't one.
4016 */
4017 int
4018assignment_len(char_u *p, int *heredoc)
4019{
4020 if (*p == '=')
4021 {
4022 if (p[1] == '<' && p[2] == '<')
4023 {
4024 *heredoc = TRUE;
4025 return 3;
4026 }
4027 return 1;
4028 }
4029 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4030 return 2;
4031 if (STRNCMP(p, "..=", 3) == 0)
4032 return 3;
4033 return 0;
4034}
4035
4036// words that cannot be used as a variable
4037static char *reserved[] = {
4038 "true",
4039 "false",
4040 NULL
4041};
4042
4043/*
4044 * Get a line for "=<<".
4045 * Return a pointer to the line in allocated memory.
4046 * Return NULL for end-of-file or some error.
4047 */
4048 static char_u *
4049heredoc_getline(
4050 int c UNUSED,
4051 void *cookie,
4052 int indent UNUSED,
4053 int do_concat UNUSED)
4054{
4055 cctx_T *cctx = (cctx_T *)cookie;
4056
4057 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004058 {
4059 iemsg("Heredoc got to end");
4060 return NULL;
4061 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 ++cctx->ctx_lnum;
4063 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4064 [cctx->ctx_lnum]);
4065}
4066
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004067typedef enum {
4068 dest_local,
4069 dest_option,
4070 dest_env,
4071 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004072 dest_buffer,
4073 dest_window,
4074 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004075 dest_vimvar,
4076 dest_script,
4077 dest_reg,
4078} assign_dest_T;
4079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080/*
4081 * compile "let var [= expr]", "const var = expr" and "var = expr"
4082 * "arg" points to "var".
4083 */
4084 static char_u *
4085compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4086{
4087 char_u *p;
4088 char_u *ret = NULL;
4089 int var_count = 0;
4090 int semicolon = 0;
4091 size_t varlen;
4092 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004093 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004096 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004098 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 int oplen = 0;
4100 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004101 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004102 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 char_u *name;
4104 char_u *sp;
4105 int has_type = FALSE;
4106 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4107 int instr_count = -1;
4108
4109 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4110 if (p == NULL)
4111 return NULL;
4112 if (var_count > 0)
4113 {
4114 // TODO: let [var, var] = list
4115 emsg("Cannot handle a list yet");
4116 return NULL;
4117 }
4118
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004119 // "a: type" is declaring variable "a" with a type, not "a:".
4120 if (is_decl && p == arg + 2 && p[-1] == ':')
4121 --p;
4122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 varlen = p - arg;
4124 name = vim_strnsave(arg, (int)varlen);
4125 if (name == NULL)
4126 return NULL;
4127
Bram Moolenaar080457c2020-03-03 21:53:32 +01004128 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004130 if (*arg == '&')
4131 {
4132 int cc;
4133 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134
Bram Moolenaar080457c2020-03-03 21:53:32 +01004135 dest = dest_option;
4136 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004138 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004139 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 if (is_decl)
4142 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004143 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 goto theend;
4145 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004146 p = arg;
4147 p = find_option_end(&p, &opt_flags);
4148 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004150 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004151 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004152 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004153 }
4154 cc = *p;
4155 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004156 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004157 *p = cc;
4158 if (opt_type == -3)
4159 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004160 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004161 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004162 }
4163 if (opt_type == -2 || opt_type == 0)
4164 type = &t_string;
4165 else
4166 type = &t_number; // both number and boolean option
4167 }
4168 else if (*arg == '$')
4169 {
4170 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004171 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004172 if (is_decl)
4173 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004174 semsg(_("E1065: Cannot declare an environment variable: %s"),
4175 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004176 goto theend;
4177 }
4178 }
4179 else if (*arg == '@')
4180 {
4181 if (!valid_yank_reg(arg[1], TRUE))
4182 {
4183 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004184 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004185 }
4186 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004187 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004188 if (is_decl)
4189 {
4190 semsg(_("E1066: Cannot declare a register: %s"), name);
4191 goto theend;
4192 }
4193 }
4194 else if (STRNCMP(arg, "g:", 2) == 0)
4195 {
4196 dest = dest_global;
4197 if (is_decl)
4198 {
4199 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4200 goto theend;
4201 }
4202 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004203 else if (STRNCMP(arg, "b:", 2) == 0)
4204 {
4205 dest = dest_buffer;
4206 if (is_decl)
4207 {
4208 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4209 goto theend;
4210 }
4211 }
4212 else if (STRNCMP(arg, "w:", 2) == 0)
4213 {
4214 dest = dest_window;
4215 if (is_decl)
4216 {
4217 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4218 goto theend;
4219 }
4220 }
4221 else if (STRNCMP(arg, "t:", 2) == 0)
4222 {
4223 dest = dest_tab;
4224 if (is_decl)
4225 {
4226 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4227 goto theend;
4228 }
4229 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004230 else if (STRNCMP(arg, "v:", 2) == 0)
4231 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004232 typval_T *vtv;
4233 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004234
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004235 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004236 if (vimvaridx < 0)
4237 {
4238 semsg(_(e_var_notfound), arg);
4239 goto theend;
4240 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004241 // We use the current value of "sandbox" here, is that OK?
4242 if (var_check_ro(di_flags, name, FALSE))
4243 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004244 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004245 vtv = get_vim_var_tv(vimvaridx);
4246 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004247 if (is_decl)
4248 {
4249 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4250 goto theend;
4251 }
4252 }
4253 else
4254 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004255 int idx;
4256
Bram Moolenaar080457c2020-03-03 21:53:32 +01004257 for (idx = 0; reserved[idx] != NULL; ++idx)
4258 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004260 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 goto theend;
4262 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004263
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004264 lvar = lookup_local(arg, varlen, cctx);
4265 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004267 if (is_decl)
4268 {
4269 semsg(_("E1017: Variable already declared: %s"), name);
4270 goto theend;
4271 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004272 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004273 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004274 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4275 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004276 }
4277 }
4278 else if (STRNCMP(arg, "s:", 2) == 0
4279 || lookup_script(arg, varlen) == OK
4280 || find_imported(arg, varlen, cctx) != NULL)
4281 {
4282 dest = dest_script;
4283 if (is_decl)
4284 {
4285 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004286 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004287 goto theend;
4288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004290 else if (name[1] == ':' && name[2] != NUL)
4291 {
4292 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
4293 goto theend;
4294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 }
4296 }
4297
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004298 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 {
4300 if (is_decl && *p == ':')
4301 {
4302 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004303 if (!VIM_ISWHITE(p[1]))
4304 {
4305 semsg(_(e_white_after), ":");
4306 goto theend;
4307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 p = skipwhite(p + 1);
4309 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 has_type = TRUE;
4311 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004312 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 }
4315
4316 sp = p;
4317 p = skipwhite(p);
4318 op = p;
4319 oplen = assignment_len(p, &heredoc);
4320 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4321 {
4322 char_u buf[4];
4323
4324 vim_strncpy(buf, op, oplen);
4325 semsg(_(e_white_both), buf);
4326 }
4327
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004328 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004329 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004331 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 goto theend;
4333 }
4334
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004335 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 {
4337 if (oplen > 1 && !heredoc)
4338 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004339 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4341 name);
4342 goto theend;
4343 }
4344
4345 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004346 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004347 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004348 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4349 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004351 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352 }
4353
4354 if (heredoc)
4355 {
4356 list_T *l;
4357 listitem_T *li;
4358
4359 // [let] varname =<< [trim] {end}
4360 eap->getline = heredoc_getline;
4361 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004362 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363
4364 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004365 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 {
4367 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4368 li->li_tv.vval.v_string = NULL;
4369 }
4370 generate_NEWLIST(cctx, l->lv_len);
4371 type = &t_list_string;
4372 list_free(l);
4373 p += STRLEN(p);
4374 }
4375 else if (oplen > 0)
4376 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004377 int r;
4378 type_T *stacktype;
4379 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 // for "+=", "*=", "..=" etc. first load the current value
4382 if (*op != '=')
4383 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004384 switch (dest)
4385 {
4386 case dest_option:
4387 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004388 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004389 break;
4390 case dest_global:
4391 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4392 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004393 case dest_buffer:
4394 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4395 break;
4396 case dest_window:
4397 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4398 break;
4399 case dest_tab:
4400 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4401 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004402 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004403 compile_load_scriptvar(cctx,
4404 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004405 break;
4406 case dest_env:
4407 // Include $ in the name here
4408 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4409 break;
4410 case dest_reg:
4411 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4412 break;
4413 case dest_vimvar:
4414 generate_LOADV(cctx, name + 2, TRUE);
4415 break;
4416 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004417 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004418 break;
4419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 }
4421
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004422 // Compile the expression. Temporarily hide the new local variable
4423 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004424 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004425 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 instr_count = instr->ga_len;
4427 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004428 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004429 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004430 ++cctx->ctx_locals.ga_len;
4431 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 goto theend;
4433
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004434 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004436 stack = &cctx->ctx_type_stack;
4437 stacktype = stack->ga_len == 0 ? &t_void
4438 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004439 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004441 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004443 if (stacktype->tt_type == VAR_VOID)
4444 {
4445 emsg(_("E1031: Cannot use void value"));
4446 goto theend;
4447 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004448 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004449 {
4450 // An empty list or dict has a &t_void member, for a
4451 // variable that implies &t_any.
4452 if (stacktype == &t_list_empty)
4453 lvar->lv_type = &t_list_any;
4454 else if (stacktype == &t_dict_empty)
4455 lvar->lv_type = &t_dict_any;
4456 else
4457 lvar->lv_type = stacktype;
4458 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004459 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004460 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4461 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004463 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004464 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 }
4466 }
4467 else if (cmdidx == CMD_const)
4468 {
4469 emsg(_("E1021: const requires a value"));
4470 goto theend;
4471 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004472 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 {
4474 emsg(_("E1022: type or initialization required"));
4475 goto theend;
4476 }
4477 else
4478 {
4479 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 if (ga_grow(instr, 1) == FAIL)
4481 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004482 switch (type->tt_type)
4483 {
4484 case VAR_BOOL:
4485 generate_PUSHBOOL(cctx, VVAL_FALSE);
4486 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004487 case VAR_FLOAT:
4488#ifdef FEAT_FLOAT
4489 generate_PUSHF(cctx, 0.0);
4490#endif
4491 break;
4492 case VAR_STRING:
4493 generate_PUSHS(cctx, NULL);
4494 break;
4495 case VAR_BLOB:
4496 generate_PUSHBLOB(cctx, NULL);
4497 break;
4498 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004499 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004500 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004501 case VAR_LIST:
4502 generate_NEWLIST(cctx, 0);
4503 break;
4504 case VAR_DICT:
4505 generate_NEWDICT(cctx, 0);
4506 break;
4507 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004508 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004509 break;
4510 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004511 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004512 break;
4513 case VAR_NUMBER:
4514 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004515 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02004516 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004517 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004518 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004519 generate_PUSHNR(cctx, 0);
4520 break;
4521 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 }
4523
4524 if (oplen > 0 && *op != '=')
4525 {
4526 type_T *expected = &t_number;
4527 garray_T *stack = &cctx->ctx_type_stack;
4528 type_T *stacktype;
4529
4530 // TODO: if type is known use float or any operation
4531
4532 if (*op == '.')
4533 expected = &t_string;
4534 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4535 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4536 goto theend;
4537
4538 if (*op == '.')
4539 generate_instr_drop(cctx, ISN_CONCAT, 1);
4540 else
4541 {
4542 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4543
4544 if (isn == NULL)
4545 goto theend;
4546 switch (*op)
4547 {
4548 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4549 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4550 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4551 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4552 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4553 }
4554 }
4555 }
4556
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004557 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004559 case dest_option:
4560 generate_STOREOPT(cctx, name + 1, opt_flags);
4561 break;
4562 case dest_global:
4563 // include g: with the name, easier to execute that way
4564 generate_STORE(cctx, ISN_STOREG, 0, name);
4565 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004566 case dest_buffer:
4567 // include b: with the name, easier to execute that way
4568 generate_STORE(cctx, ISN_STOREB, 0, name);
4569 break;
4570 case dest_window:
4571 // include w: with the name, easier to execute that way
4572 generate_STORE(cctx, ISN_STOREW, 0, name);
4573 break;
4574 case dest_tab:
4575 // include t: with the name, easier to execute that way
4576 generate_STORE(cctx, ISN_STORET, 0, name);
4577 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004578 case dest_env:
4579 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4580 break;
4581 case dest_reg:
4582 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4583 break;
4584 case dest_vimvar:
4585 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4586 break;
4587 case dest_script:
4588 {
4589 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4590 imported_T *import = NULL;
4591 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004592 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004593
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004594 if (name[1] != ':')
4595 {
4596 import = find_imported(name, 0, cctx);
4597 if (import != NULL)
4598 sid = import->imp_sid;
4599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004601 idx = get_script_item_idx(sid, rawname, TRUE);
4602 // TODO: specific type
4603 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004604 {
4605 char_u *name_s = name;
4606
4607 // Include s: in the name for store_var()
4608 if (name[1] != ':')
4609 {
4610 int len = (int)STRLEN(name) + 3;
4611
4612 name_s = alloc(len);
4613 if (name_s == NULL)
4614 name_s = name;
4615 else
4616 vim_snprintf((char *)name_s, len, "s:%s", name);
4617 }
4618 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4619 if (name_s != name)
4620 vim_free(name_s);
4621 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004622 else
4623 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4624 sid, idx, &t_any);
4625 }
4626 break;
4627 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004628 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004629 {
4630 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004632 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4633 // into ISN_STORENR
4634 if (instr->ga_len == instr_count + 1
4635 && isn->isn_type == ISN_PUSHNR)
4636 {
4637 varnumber_T val = isn->isn_arg.number;
4638 garray_T *stack = &cctx->ctx_type_stack;
4639
4640 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004641 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004642 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004643 if (stack->ga_len > 0)
4644 --stack->ga_len;
4645 }
4646 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004647 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004648 }
4649 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 }
4651 ret = p;
4652
4653theend:
4654 vim_free(name);
4655 return ret;
4656}
4657
4658/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004659 * Check if "name" can be "unlet".
4660 */
4661 int
4662check_vim9_unlet(char_u *name)
4663{
4664 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
4665 {
4666 semsg(_("E1081: Cannot unlet %s"), name);
4667 return FAIL;
4668 }
4669 return OK;
4670}
4671
4672/*
4673 * Callback passed to ex_unletlock().
4674 */
4675 static int
4676compile_unlet(
4677 lval_T *lvp,
4678 char_u *name_end,
4679 exarg_T *eap,
4680 int deep UNUSED,
4681 void *coookie)
4682{
4683 cctx_T *cctx = coookie;
4684
4685 if (lvp->ll_tv == NULL)
4686 {
4687 char_u *p = lvp->ll_name;
4688 int cc = *name_end;
4689 int ret = OK;
4690
4691 // Normal name. Only supports g:, w:, t: and b: namespaces.
4692 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004693 if (*p == '$')
4694 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
4695 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004696 ret = FAIL;
4697 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004698 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004699
4700 *name_end = cc;
4701 return ret;
4702 }
4703
4704 // TODO: unlet {list}[idx]
4705 // TODO: unlet {dict}[key]
4706 emsg("Sorry, :unlet not fully implemented yet");
4707 return FAIL;
4708}
4709
4710/*
4711 * compile "unlet var", "lock var" and "unlock var"
4712 * "arg" points to "var".
4713 */
4714 static char_u *
4715compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
4716{
4717 char_u *p = arg;
4718
4719 if (eap->cmdidx != CMD_unlet)
4720 {
4721 emsg("Sorry, :lock and unlock not implemented yet");
4722 return NULL;
4723 }
4724
4725 if (*p == '!')
4726 {
4727 p = skipwhite(p + 1);
4728 eap->forceit = TRUE;
4729 }
4730
4731 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
4732 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4733}
4734
4735/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 * Compile an :import command.
4737 */
4738 static char_u *
4739compile_import(char_u *arg, cctx_T *cctx)
4740{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004741 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742}
4743
4744/*
4745 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4746 */
4747 static int
4748compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4749{
4750 garray_T *instr = &cctx->ctx_instr;
4751 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4752
4753 if (endlabel == NULL)
4754 return FAIL;
4755 endlabel->el_next = *el;
4756 *el = endlabel;
4757 endlabel->el_end_label = instr->ga_len;
4758
4759 generate_JUMP(cctx, when, 0);
4760 return OK;
4761}
4762
4763 static void
4764compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4765{
4766 garray_T *instr = &cctx->ctx_instr;
4767
4768 while (*el != NULL)
4769 {
4770 endlabel_T *cur = (*el);
4771 isn_T *isn;
4772
4773 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4774 isn->isn_arg.jump.jump_where = instr->ga_len;
4775 *el = cur->el_next;
4776 vim_free(cur);
4777 }
4778}
4779
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004780 static void
4781compile_free_jump_to_end(endlabel_T **el)
4782{
4783 while (*el != NULL)
4784 {
4785 endlabel_T *cur = (*el);
4786
4787 *el = cur->el_next;
4788 vim_free(cur);
4789 }
4790}
4791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792/*
4793 * Create a new scope and set up the generic items.
4794 */
4795 static scope_T *
4796new_scope(cctx_T *cctx, scopetype_T type)
4797{
4798 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4799
4800 if (scope == NULL)
4801 return NULL;
4802 scope->se_outer = cctx->ctx_scope;
4803 cctx->ctx_scope = scope;
4804 scope->se_type = type;
4805 scope->se_local_count = cctx->ctx_locals.ga_len;
4806 return scope;
4807}
4808
4809/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004810 * Free the current scope and go back to the outer scope.
4811 */
4812 static void
4813drop_scope(cctx_T *cctx)
4814{
4815 scope_T *scope = cctx->ctx_scope;
4816
4817 if (scope == NULL)
4818 {
4819 iemsg("calling drop_scope() without a scope");
4820 return;
4821 }
4822 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004823 switch (scope->se_type)
4824 {
4825 case IF_SCOPE:
4826 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4827 case FOR_SCOPE:
4828 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4829 case WHILE_SCOPE:
4830 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4831 case TRY_SCOPE:
4832 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4833 case NO_SCOPE:
4834 case BLOCK_SCOPE:
4835 break;
4836 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004837 vim_free(scope);
4838}
4839
4840/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004841 * Evaluate an expression that is a constant:
4842 * has(arg)
4843 *
4844 * Also handle:
4845 * ! in front logical NOT
4846 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004847 * Return FAIL if the expression is not a constant.
4848 */
4849 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004850evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004851{
4852 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004853 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004854 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004855
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004856 /*
4857 * Skip '!' characters. They are handled later.
4858 */
4859 start_leader = *arg;
4860 while (**arg == '!')
4861 *arg = skipwhite(*arg + 1);
4862 end_leader = *arg;
4863
4864 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004865 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004866 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004867 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4868 {
4869 tv->v_type = VAR_SPECIAL;
4870 tv->vval.v_number = VVAL_TRUE;
4871 *arg += 4;
4872 return OK;
4873 }
4874 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4875 {
4876 tv->v_type = VAR_SPECIAL;
4877 tv->vval.v_number = VVAL_FALSE;
4878 *arg += 5;
4879 return OK;
4880 }
4881
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004882 if (STRNCMP("has(", *arg, 4) == 0)
4883 {
4884 has_call = TRUE;
4885 *arg = skipwhite(*arg + 4);
4886 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004887
4888 if (**arg == '"')
4889 {
4890 if (get_string_tv(arg, tv, TRUE) == FAIL)
4891 return FAIL;
4892 }
4893 else if (**arg == '\'')
4894 {
4895 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4896 return FAIL;
4897 }
4898 else
4899 return FAIL;
4900
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004901 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004902 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004903 *arg = skipwhite(*arg);
4904 if (**arg != ')')
4905 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004906 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004907
4908 argvars[0] = *tv;
4909 argvars[1].v_type = VAR_UNKNOWN;
4910 tv->v_type = VAR_NUMBER;
4911 tv->vval.v_number = 0;
4912 f_has(argvars, tv);
4913 clear_tv(&argvars[0]);
4914
4915 while (start_leader < end_leader)
4916 {
4917 if (*start_leader == '!')
4918 tv->vval.v_number = !tv->vval.v_number;
4919 ++start_leader;
4920 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004921 }
4922
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004923 return OK;
4924}
4925
Bram Moolenaar080457c2020-03-03 21:53:32 +01004926 static int
4927evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4928{
4929 exptype_T type = EXPR_UNKNOWN;
4930 char_u *p;
4931 int len = 2;
4932 int type_is = FALSE;
4933
4934 // get the first variable
4935 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4936 return FAIL;
4937
4938 p = skipwhite(*arg);
4939 type = get_compare_type(p, &len, &type_is);
4940
4941 /*
4942 * If there is a comparative operator, use it.
4943 */
4944 if (type != EXPR_UNKNOWN)
4945 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004946 typval_T tv2;
4947 char_u *s1, *s2;
4948 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4949 int n;
4950
4951 // TODO: Only string == string is supported now
4952 if (tv->v_type != VAR_STRING)
4953 return FAIL;
4954 if (type != EXPR_EQUAL)
4955 return FAIL;
4956
4957 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004958 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004959 *arg = skipwhite(p + len);
4960 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4961 || tv2.v_type != VAR_STRING)
4962 {
4963 clear_tv(&tv2);
4964 return FAIL;
4965 }
4966 s1 = tv_get_string_buf(tv, buf1);
4967 s2 = tv_get_string_buf(&tv2, buf2);
4968 n = STRCMP(s1, s2);
4969 clear_tv(tv);
4970 clear_tv(&tv2);
4971 tv->v_type = VAR_BOOL;
4972 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004973 }
4974
4975 return OK;
4976}
4977
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004978static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4979
4980/*
4981 * Compile constant || or &&.
4982 */
4983 static int
4984evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4985{
4986 char_u *p = skipwhite(*arg);
4987 int opchar = *op;
4988
4989 if (p[0] == opchar && p[1] == opchar)
4990 {
4991 int val = tv2bool(tv);
4992
4993 /*
4994 * Repeat until there is no following "||" or "&&"
4995 */
4996 while (p[0] == opchar && p[1] == opchar)
4997 {
4998 typval_T tv2;
4999
5000 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
5001 return FAIL;
5002
5003 // eval the next expression
5004 *arg = skipwhite(p + 2);
5005 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01005006 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005007 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01005008 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005009 {
5010 clear_tv(&tv2);
5011 return FAIL;
5012 }
5013 if ((opchar == '&') == val)
5014 {
5015 // false || tv2 or true && tv2: use tv2
5016 clear_tv(tv);
5017 *tv = tv2;
5018 val = tv2bool(tv);
5019 }
5020 else
5021 clear_tv(&tv2);
5022 p = skipwhite(*arg);
5023 }
5024 }
5025
5026 return OK;
5027}
5028
5029/*
5030 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
5031 * Return FAIL if the expression is not a constant.
5032 */
5033 static int
5034evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
5035{
5036 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01005037 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005038 return FAIL;
5039
5040 // || and && work almost the same
5041 return evaluate_const_and_or(arg, cctx, "&&", tv);
5042}
5043
5044/*
5045 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
5046 * Return FAIL if the expression is not a constant.
5047 */
5048 static int
5049evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
5050{
5051 // evaluate the first expression
5052 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
5053 return FAIL;
5054
5055 // || and && work almost the same
5056 return evaluate_const_and_or(arg, cctx, "||", tv);
5057}
5058
5059/*
5060 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
5061 * E.g. for "has('feature')".
5062 * This does not produce error messages. "tv" should be cleared afterwards.
5063 * Return FAIL if the expression is not a constant.
5064 */
5065 static int
5066evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
5067{
5068 char_u *p;
5069
5070 // evaluate the first expression
5071 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
5072 return FAIL;
5073
5074 p = skipwhite(*arg);
5075 if (*p == '?')
5076 {
5077 int val = tv2bool(tv);
5078 typval_T tv2;
5079
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005080 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005081 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
5082 return FAIL;
5083
5084 // evaluate the second expression; any type is accepted
5085 clear_tv(tv);
5086 *arg = skipwhite(p + 1);
5087 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
5088 return FAIL;
5089
5090 // Check for the ":".
5091 p = skipwhite(*arg);
5092 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
5093 return FAIL;
5094
5095 // evaluate the third expression
5096 *arg = skipwhite(p + 1);
5097 tv2.v_type = VAR_UNKNOWN;
5098 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
5099 {
5100 clear_tv(&tv2);
5101 return FAIL;
5102 }
5103 if (val)
5104 {
5105 // use the expr after "?"
5106 clear_tv(&tv2);
5107 }
5108 else
5109 {
5110 // use the expr after ":"
5111 clear_tv(tv);
5112 *tv = tv2;
5113 }
5114 }
5115 return OK;
5116}
5117
5118/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 * compile "if expr"
5120 *
5121 * "if expr" Produces instructions:
5122 * EVAL expr Push result of "expr"
5123 * JUMP_IF_FALSE end
5124 * ... body ...
5125 * end:
5126 *
5127 * "if expr | else" Produces instructions:
5128 * EVAL expr Push result of "expr"
5129 * JUMP_IF_FALSE else
5130 * ... body ...
5131 * JUMP_ALWAYS end
5132 * else:
5133 * ... body ...
5134 * end:
5135 *
5136 * "if expr1 | elseif expr2 | else" Produces instructions:
5137 * EVAL expr Push result of "expr"
5138 * JUMP_IF_FALSE elseif
5139 * ... body ...
5140 * JUMP_ALWAYS end
5141 * elseif:
5142 * EVAL expr Push result of "expr"
5143 * JUMP_IF_FALSE else
5144 * ... body ...
5145 * JUMP_ALWAYS end
5146 * else:
5147 * ... body ...
5148 * end:
5149 */
5150 static char_u *
5151compile_if(char_u *arg, cctx_T *cctx)
5152{
5153 char_u *p = arg;
5154 garray_T *instr = &cctx->ctx_instr;
5155 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005156 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005157
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005158 // compile "expr"; if we know it evaluates to FALSE skip the block
5159 tv.v_type = VAR_UNKNOWN;
5160 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5161 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5162 else
5163 cctx->ctx_skip = MAYBE;
5164 clear_tv(&tv);
5165 if (cctx->ctx_skip == MAYBE)
5166 {
5167 p = arg;
5168 if (compile_expr1(&p, cctx) == FAIL)
5169 return NULL;
5170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171
5172 scope = new_scope(cctx, IF_SCOPE);
5173 if (scope == NULL)
5174 return NULL;
5175
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005176 if (cctx->ctx_skip == MAYBE)
5177 {
5178 // "where" is set when ":elseif", "else" or ":endif" is found
5179 scope->se_u.se_if.is_if_label = instr->ga_len;
5180 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5181 }
5182 else
5183 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184
5185 return p;
5186}
5187
5188 static char_u *
5189compile_elseif(char_u *arg, cctx_T *cctx)
5190{
5191 char_u *p = arg;
5192 garray_T *instr = &cctx->ctx_instr;
5193 isn_T *isn;
5194 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005195 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196
5197 if (scope == NULL || scope->se_type != IF_SCOPE)
5198 {
5199 emsg(_(e_elseif_without_if));
5200 return NULL;
5201 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005202 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203
Bram Moolenaar158906c2020-02-06 20:39:45 +01005204 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005205 {
5206 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005208 return NULL;
5209 // previous "if" or "elseif" jumps here
5210 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5211 isn->isn_arg.jump.jump_where = instr->ga_len;
5212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005214 // compile "expr"; if we know it evaluates to FALSE skip the block
5215 tv.v_type = VAR_UNKNOWN;
5216 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5217 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5218 else
5219 cctx->ctx_skip = MAYBE;
5220 clear_tv(&tv);
5221 if (cctx->ctx_skip == MAYBE)
5222 {
5223 p = arg;
5224 if (compile_expr1(&p, cctx) == FAIL)
5225 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005226
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005227 // "where" is set when ":elseif", "else" or ":endif" is found
5228 scope->se_u.se_if.is_if_label = instr->ga_len;
5229 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5230 }
5231 else
5232 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233
5234 return p;
5235}
5236
5237 static char_u *
5238compile_else(char_u *arg, cctx_T *cctx)
5239{
5240 char_u *p = arg;
5241 garray_T *instr = &cctx->ctx_instr;
5242 isn_T *isn;
5243 scope_T *scope = cctx->ctx_scope;
5244
5245 if (scope == NULL || scope->se_type != IF_SCOPE)
5246 {
5247 emsg(_(e_else_without_if));
5248 return NULL;
5249 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005250 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005252 // jump from previous block to the end, unless the else block is empty
5253 if (cctx->ctx_skip == MAYBE)
5254 {
5255 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005257 return NULL;
5258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259
Bram Moolenaar158906c2020-02-06 20:39:45 +01005260 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005261 {
5262 if (scope->se_u.se_if.is_if_label >= 0)
5263 {
5264 // previous "if" or "elseif" jumps here
5265 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5266 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005267 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005268 }
5269 }
5270
5271 if (cctx->ctx_skip != MAYBE)
5272 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005273
5274 return p;
5275}
5276
5277 static char_u *
5278compile_endif(char_u *arg, cctx_T *cctx)
5279{
5280 scope_T *scope = cctx->ctx_scope;
5281 ifscope_T *ifscope;
5282 garray_T *instr = &cctx->ctx_instr;
5283 isn_T *isn;
5284
5285 if (scope == NULL || scope->se_type != IF_SCOPE)
5286 {
5287 emsg(_(e_endif_without_if));
5288 return NULL;
5289 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005290 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005291 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005293 if (scope->se_u.se_if.is_if_label >= 0)
5294 {
5295 // previous "if" or "elseif" jumps here
5296 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5297 isn->isn_arg.jump.jump_where = instr->ga_len;
5298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005299 // Fill in the "end" label in jumps at the end of the blocks.
5300 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005301 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005303 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304 return arg;
5305}
5306
5307/*
5308 * compile "for var in expr"
5309 *
5310 * Produces instructions:
5311 * PUSHNR -1
5312 * STORE loop-idx Set index to -1
5313 * EVAL expr Push result of "expr"
5314 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5315 * - if beyond end, jump to "end"
5316 * - otherwise get item from list and push it
5317 * STORE var Store item in "var"
5318 * ... body ...
5319 * JUMP top Jump back to repeat
5320 * end: DROP Drop the result of "expr"
5321 *
5322 */
5323 static char_u *
5324compile_for(char_u *arg, cctx_T *cctx)
5325{
5326 char_u *p;
5327 size_t varlen;
5328 garray_T *instr = &cctx->ctx_instr;
5329 garray_T *stack = &cctx->ctx_type_stack;
5330 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005331 lvar_T *loop_lvar; // loop iteration variable
5332 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333 type_T *vartype;
5334
5335 // TODO: list of variables: "for [key, value] in dict"
5336 // parse "var"
5337 for (p = arg; eval_isnamec1(*p); ++p)
5338 ;
5339 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005340 var_lvar = lookup_local(arg, varlen, cctx);
5341 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 {
5343 semsg(_("E1023: variable already defined: %s"), arg);
5344 return NULL;
5345 }
5346
5347 // consume "in"
5348 p = skipwhite(p);
5349 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5350 {
5351 emsg(_(e_missing_in));
5352 return NULL;
5353 }
5354 p = skipwhite(p + 2);
5355
5356
5357 scope = new_scope(cctx, FOR_SCOPE);
5358 if (scope == NULL)
5359 return NULL;
5360
5361 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005362 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5363 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005364 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005365 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005366 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369
5370 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005371 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5372 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005373 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005374 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005375 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005376 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005379 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005380
5381 // compile "expr", it remains on the stack until "endfor"
5382 arg = p;
5383 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005384 {
5385 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005387 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005388
5389 // now we know the type of "var"
5390 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5391 if (vartype->tt_type != VAR_LIST)
5392 {
5393 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005394 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395 return NULL;
5396 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005397 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005398 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005399
5400 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005401 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005403 generate_FOR(cctx, loop_lvar->lv_idx);
5404 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405
5406 return arg;
5407}
5408
5409/*
5410 * compile "endfor"
5411 */
5412 static char_u *
5413compile_endfor(char_u *arg, cctx_T *cctx)
5414{
5415 garray_T *instr = &cctx->ctx_instr;
5416 scope_T *scope = cctx->ctx_scope;
5417 forscope_T *forscope;
5418 isn_T *isn;
5419
5420 if (scope == NULL || scope->se_type != FOR_SCOPE)
5421 {
5422 emsg(_(e_for));
5423 return NULL;
5424 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005425 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005427 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428
5429 // At end of ":for" scope jump back to the FOR instruction.
5430 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5431
5432 // Fill in the "end" label in the FOR statement so it can jump here
5433 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5434 isn->isn_arg.forloop.for_end = instr->ga_len;
5435
5436 // Fill in the "end" label any BREAK statements
5437 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5438
5439 // Below the ":for" scope drop the "expr" list from the stack.
5440 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5441 return NULL;
5442
5443 vim_free(scope);
5444
5445 return arg;
5446}
5447
5448/*
5449 * compile "while expr"
5450 *
5451 * Produces instructions:
5452 * top: EVAL expr Push result of "expr"
5453 * JUMP_IF_FALSE end jump if false
5454 * ... body ...
5455 * JUMP top Jump back to repeat
5456 * end:
5457 *
5458 */
5459 static char_u *
5460compile_while(char_u *arg, cctx_T *cctx)
5461{
5462 char_u *p = arg;
5463 garray_T *instr = &cctx->ctx_instr;
5464 scope_T *scope;
5465
5466 scope = new_scope(cctx, WHILE_SCOPE);
5467 if (scope == NULL)
5468 return NULL;
5469
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005470 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005471
5472 // compile "expr"
5473 if (compile_expr1(&p, cctx) == FAIL)
5474 return NULL;
5475
5476 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005477 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005478 JUMP_IF_FALSE, cctx) == FAIL)
5479 return FAIL;
5480
5481 return p;
5482}
5483
5484/*
5485 * compile "endwhile"
5486 */
5487 static char_u *
5488compile_endwhile(char_u *arg, cctx_T *cctx)
5489{
5490 scope_T *scope = cctx->ctx_scope;
5491
5492 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5493 {
5494 emsg(_(e_while));
5495 return NULL;
5496 }
5497 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005498 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005499
5500 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005501 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502
5503 // Fill in the "end" label in the WHILE statement so it can jump here.
5504 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005505 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005506
5507 vim_free(scope);
5508
5509 return arg;
5510}
5511
5512/*
5513 * compile "continue"
5514 */
5515 static char_u *
5516compile_continue(char_u *arg, cctx_T *cctx)
5517{
5518 scope_T *scope = cctx->ctx_scope;
5519
5520 for (;;)
5521 {
5522 if (scope == NULL)
5523 {
5524 emsg(_(e_continue));
5525 return NULL;
5526 }
5527 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5528 break;
5529 scope = scope->se_outer;
5530 }
5531
5532 // Jump back to the FOR or WHILE instruction.
5533 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005534 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5535 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005536 return arg;
5537}
5538
5539/*
5540 * compile "break"
5541 */
5542 static char_u *
5543compile_break(char_u *arg, cctx_T *cctx)
5544{
5545 scope_T *scope = cctx->ctx_scope;
5546 endlabel_T **el;
5547
5548 for (;;)
5549 {
5550 if (scope == NULL)
5551 {
5552 emsg(_(e_break));
5553 return NULL;
5554 }
5555 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5556 break;
5557 scope = scope->se_outer;
5558 }
5559
5560 // Jump to the end of the FOR or WHILE loop.
5561 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005562 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005563 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005564 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5566 return FAIL;
5567
5568 return arg;
5569}
5570
5571/*
5572 * compile "{" start of block
5573 */
5574 static char_u *
5575compile_block(char_u *arg, cctx_T *cctx)
5576{
5577 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5578 return NULL;
5579 return skipwhite(arg + 1);
5580}
5581
5582/*
5583 * compile end of block: drop one scope
5584 */
5585 static void
5586compile_endblock(cctx_T *cctx)
5587{
5588 scope_T *scope = cctx->ctx_scope;
5589
5590 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005591 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592 vim_free(scope);
5593}
5594
5595/*
5596 * compile "try"
5597 * Creates a new scope for the try-endtry, pointing to the first catch and
5598 * finally.
5599 * Creates another scope for the "try" block itself.
5600 * TRY instruction sets up exception handling at runtime.
5601 *
5602 * "try"
5603 * TRY -> catch1, -> finally push trystack entry
5604 * ... try block
5605 * "throw {exception}"
5606 * EVAL {exception}
5607 * THROW create exception
5608 * ... try block
5609 * " catch {expr}"
5610 * JUMP -> finally
5611 * catch1: PUSH exeception
5612 * EVAL {expr}
5613 * MATCH
5614 * JUMP nomatch -> catch2
5615 * CATCH remove exception
5616 * ... catch block
5617 * " catch"
5618 * JUMP -> finally
5619 * catch2: CATCH remove exception
5620 * ... catch block
5621 * " finally"
5622 * finally:
5623 * ... finally block
5624 * " endtry"
5625 * ENDTRY pop trystack entry, may rethrow
5626 */
5627 static char_u *
5628compile_try(char_u *arg, cctx_T *cctx)
5629{
5630 garray_T *instr = &cctx->ctx_instr;
5631 scope_T *try_scope;
5632 scope_T *scope;
5633
5634 // scope that holds the jumps that go to catch/finally/endtry
5635 try_scope = new_scope(cctx, TRY_SCOPE);
5636 if (try_scope == NULL)
5637 return NULL;
5638
5639 // "catch" is set when the first ":catch" is found.
5640 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005641 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642 if (generate_instr(cctx, ISN_TRY) == NULL)
5643 return NULL;
5644
5645 // scope for the try block itself
5646 scope = new_scope(cctx, BLOCK_SCOPE);
5647 if (scope == NULL)
5648 return NULL;
5649
5650 return arg;
5651}
5652
5653/*
5654 * compile "catch {expr}"
5655 */
5656 static char_u *
5657compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5658{
5659 scope_T *scope = cctx->ctx_scope;
5660 garray_T *instr = &cctx->ctx_instr;
5661 char_u *p;
5662 isn_T *isn;
5663
5664 // end block scope from :try or :catch
5665 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5666 compile_endblock(cctx);
5667 scope = cctx->ctx_scope;
5668
5669 // Error if not in a :try scope
5670 if (scope == NULL || scope->se_type != TRY_SCOPE)
5671 {
5672 emsg(_(e_catch));
5673 return NULL;
5674 }
5675
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005676 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005677 {
5678 emsg(_("E1033: catch unreachable after catch-all"));
5679 return NULL;
5680 }
5681
5682 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005683 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005684 JUMP_ALWAYS, cctx) == FAIL)
5685 return NULL;
5686
5687 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005688 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005689 if (isn->isn_arg.try.try_catch == 0)
5690 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005691 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005692 {
5693 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005694 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005695 isn->isn_arg.jump.jump_where = instr->ga_len;
5696 }
5697
5698 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005699 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005700 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005701 scope->se_u.se_try.ts_caught_all = TRUE;
5702 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005703 }
5704 else
5705 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005706 char_u *end;
5707 char_u *pat;
5708 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005709 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005710 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005712 // Push v:exception, push {expr} and MATCH
5713 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5714
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005715 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005716 if (*end != *p)
5717 {
5718 semsg(_("E1067: Separator mismatch: %s"), p);
5719 vim_free(tofree);
5720 return FAIL;
5721 }
5722 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005723 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005724 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005725 len = (int)(end - tofree);
5726 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005727 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005728 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005729 if (pat == NULL)
5730 return FAIL;
5731 if (generate_PUSHS(cctx, pat) == FAIL)
5732 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005734 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5735 return NULL;
5736
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005737 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005738 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5739 return NULL;
5740 }
5741
5742 if (generate_instr(cctx, ISN_CATCH) == NULL)
5743 return NULL;
5744
5745 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5746 return NULL;
5747 return p;
5748}
5749
5750 static char_u *
5751compile_finally(char_u *arg, cctx_T *cctx)
5752{
5753 scope_T *scope = cctx->ctx_scope;
5754 garray_T *instr = &cctx->ctx_instr;
5755 isn_T *isn;
5756
5757 // end block scope from :try or :catch
5758 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5759 compile_endblock(cctx);
5760 scope = cctx->ctx_scope;
5761
5762 // Error if not in a :try scope
5763 if (scope == NULL || scope->se_type != TRY_SCOPE)
5764 {
5765 emsg(_(e_finally));
5766 return NULL;
5767 }
5768
5769 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005770 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771 if (isn->isn_arg.try.try_finally != 0)
5772 {
5773 emsg(_(e_finally_dup));
5774 return NULL;
5775 }
5776
5777 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005778 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005779
Bram Moolenaar585fea72020-04-02 22:33:21 +02005780 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005781 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005782 {
5783 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005784 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005785 isn->isn_arg.jump.jump_where = instr->ga_len;
5786 }
5787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005788 // TODO: set index in ts_finally_label jumps
5789
5790 return arg;
5791}
5792
5793 static char_u *
5794compile_endtry(char_u *arg, cctx_T *cctx)
5795{
5796 scope_T *scope = cctx->ctx_scope;
5797 garray_T *instr = &cctx->ctx_instr;
5798 isn_T *isn;
5799
5800 // end block scope from :catch or :finally
5801 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5802 compile_endblock(cctx);
5803 scope = cctx->ctx_scope;
5804
5805 // Error if not in a :try scope
5806 if (scope == NULL || scope->se_type != TRY_SCOPE)
5807 {
5808 if (scope == NULL)
5809 emsg(_(e_no_endtry));
5810 else if (scope->se_type == WHILE_SCOPE)
5811 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005812 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005813 emsg(_(e_endfor));
5814 else
5815 emsg(_(e_endif));
5816 return NULL;
5817 }
5818
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005819 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5821 {
5822 emsg(_("E1032: missing :catch or :finally"));
5823 return NULL;
5824 }
5825
5826 // Fill in the "end" label in jumps at the end of the blocks, if not done
5827 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005828 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005829
5830 // End :catch or :finally scope: set value in ISN_TRY instruction
5831 if (isn->isn_arg.try.try_finally == 0)
5832 isn->isn_arg.try.try_finally = instr->ga_len;
5833 compile_endblock(cctx);
5834
5835 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5836 return NULL;
5837 return arg;
5838}
5839
5840/*
5841 * compile "throw {expr}"
5842 */
5843 static char_u *
5844compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5845{
5846 char_u *p = skipwhite(arg);
5847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848 if (compile_expr1(&p, cctx) == FAIL)
5849 return NULL;
5850 if (may_generate_2STRING(-1, cctx) == FAIL)
5851 return NULL;
5852 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5853 return NULL;
5854
5855 return p;
5856}
5857
5858/*
5859 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005860 * compile "echomsg expr"
5861 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01005862 * compile "execute expr"
5863 */
5864 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005865compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01005866{
5867 char_u *p = arg;
5868 int count = 0;
5869
5870 for (;;)
5871 {
5872 if (compile_expr1(&p, cctx) == FAIL)
5873 return NULL;
5874 ++count;
5875 p = skipwhite(p);
5876 if (ends_excmd(*p))
5877 break;
5878 }
5879
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005880 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
5881 generate_ECHO(cctx, cmdidx == CMD_echo, count);
5882 else if (cmdidx == CMD_execute)
5883 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
5884 else if (cmdidx == CMD_echomsg)
5885 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
5886 else
5887 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005888 return p;
5889}
5890
5891/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005892 * A command that is not compiled, execute with legacy code.
5893 */
5894 static char_u *
5895compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
5896{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005897 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02005898 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005899
5900 if (cctx->ctx_skip == TRUE)
5901 goto theend;
5902
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02005903 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
5904 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005905 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
5906 {
5907 // expand filename in "syntax include [@group] filename"
5908 has_expr = TRUE;
5909 eap->arg = skipwhite(eap->arg + 7);
5910 if (*eap->arg == '@')
5911 eap->arg = skiptowhite(eap->arg);
5912 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005913
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005914 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005915 {
5916 int count = 0;
5917 char_u *start = skipwhite(line);
5918
5919 // :cmd xxx`=expr1`yyy`=expr2`zzz
5920 // PUSHS ":cmd xxx"
5921 // eval expr1
5922 // PUSHS "yyy"
5923 // eval expr2
5924 // PUSHS "zzz"
5925 // EXECCONCAT 5
5926 for (;;)
5927 {
5928 if (p > start)
5929 {
5930 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
5931 ++count;
5932 }
5933 p += 2;
5934 if (compile_expr1(&p, cctx) == FAIL)
5935 return NULL;
5936 may_generate_2STRING(-1, cctx);
5937 ++count;
5938 p = skipwhite(p);
5939 if (*p != '`')
5940 {
5941 emsg(_("E1083: missing backtick"));
5942 return NULL;
5943 }
5944 start = p + 1;
5945
5946 p = (char_u *)strstr((char *)start, "`=");
5947 if (p == NULL)
5948 {
5949 if (*skipwhite(start) != NUL)
5950 {
5951 generate_PUSHS(cctx, vim_strsave(start));
5952 ++count;
5953 }
5954 break;
5955 }
5956 }
5957 generate_EXECCONCAT(cctx, count);
5958 }
5959 else
5960 generate_EXEC(cctx, line);
5961
5962theend:
5963 return (char_u *)"";
5964}
5965
5966/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005967 * After ex_function() has collected all the function lines: parse and compile
5968 * the lines into instructions.
5969 * Adds the function to "def_functions".
5970 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5971 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02005972 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005973 * This can be used recursively through compile_lambda(), which may reallocate
5974 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975 */
5976 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02005977compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005978{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979 char_u *line = NULL;
5980 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005981 char *errormsg = NULL; // error message
5982 int had_return = FALSE;
5983 cctx_T cctx;
5984 garray_T *instr;
5985 int called_emsg_before = called_emsg;
5986 int ret = FAIL;
5987 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005988 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005991 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005992
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005993 if (ufunc->uf_dfunc_idx >= 0)
5994 {
5995 // Redefining a function that was compiled before.
5996 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5997
5998 // Free old instructions.
5999 delete_def_function_contents(dfunc);
6000 }
6001 else
6002 {
6003 // Add the function to "def_functions".
6004 if (ga_grow(&def_functions, 1) == FAIL)
6005 return;
6006 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006007 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006008 dfunc->df_idx = def_functions.ga_len;
6009 ufunc->uf_dfunc_idx = dfunc->df_idx;
6010 dfunc->df_ufunc = ufunc;
6011 ++def_functions.ga_len;
6012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006013 }
6014
Bram Moolenaara80faa82020-04-12 19:37:17 +02006015 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016 cctx.ctx_ufunc = ufunc;
6017 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006018 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006019 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6020 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6021 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6022 cctx.ctx_type_list = &ufunc->uf_type_list;
6023 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6024 instr = &cctx.ctx_instr;
6025
6026 // Most modern script version.
6027 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6028
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006029 if (ufunc->uf_def_args.ga_len > 0)
6030 {
6031 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006032 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006033 int i;
6034 char_u *arg;
6035 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6036
6037 // Produce instructions for the default values of optional arguments.
6038 // Store the instruction index in uf_def_arg_idx[] so that we know
6039 // where to start when the function is called, depending on the number
6040 // of arguments.
6041 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6042 if (ufunc->uf_def_arg_idx == NULL)
6043 goto erret;
6044 for (i = 0; i < count; ++i)
6045 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006046 garray_T *stack = &cctx.ctx_type_stack;
6047 type_T *val_type;
6048 int arg_idx = first_def_arg + i;
6049
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006050 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6051 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006052 if (compile_expr1(&arg, &cctx) == FAIL)
6053 goto erret;
6054
6055 // If no type specified use the type of the default value.
6056 // Otherwise check that the default value type matches the
6057 // specified type.
6058 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6059 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6060 ufunc->uf_arg_types[arg_idx] = val_type;
6061 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6062 == FAIL)
6063 {
6064 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6065 arg_idx + 1);
6066 goto erret;
6067 }
6068
6069 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006070 goto erret;
6071 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006072 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6073 }
6074
6075 /*
6076 * Loop over all the lines of the function and generate instructions.
6077 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006078 for (;;)
6079 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006080 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006081 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006082
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006083 // Bail out on the first error to avoid a flood of errors and report
6084 // the right line number when inside try/catch.
6085 if (emsg_before != called_emsg)
6086 goto erret;
6087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006088 if (line != NULL && *line == '|')
6089 // the line continues after a '|'
6090 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006091 else if (line != NULL && *line != NUL
6092 && !(*line == '#' && (line == cctx.ctx_line_start
6093 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006094 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006095 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096 goto erret;
6097 }
6098 else
6099 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006100 line = next_line_from_context(&cctx);
6101 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006102 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006103 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006104 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006105 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106
6107 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006108 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109 ea.cmdlinep = &line;
6110 ea.cmd = skipwhite(line);
6111
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006112 // Some things can be recognized by the first character.
6113 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006114 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006115 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006116 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006117 if (ea.cmd[1] != '{')
6118 {
6119 line = (char_u *)"";
6120 continue;
6121 }
6122 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006123
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006124 case '}':
6125 {
6126 // "}" ends a block scope
6127 scopetype_T stype = cctx.ctx_scope == NULL
6128 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006130 if (stype == BLOCK_SCOPE)
6131 {
6132 compile_endblock(&cctx);
6133 line = ea.cmd;
6134 }
6135 else
6136 {
6137 emsg(_("E1025: using } outside of a block scope"));
6138 goto erret;
6139 }
6140 if (line != NULL)
6141 line = skipwhite(ea.cmd + 1);
6142 continue;
6143 }
6144
6145 case '{':
6146 // "{" starts a block scope
6147 // "{'a': 1}->func() is something else
6148 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6149 {
6150 line = compile_block(ea.cmd, &cctx);
6151 continue;
6152 }
6153 break;
6154
6155 case ':':
6156 is_ex_command = TRUE;
6157 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006158 }
6159
6160 /*
6161 * COMMAND MODIFIERS
6162 */
6163 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6164 {
6165 if (errormsg != NULL)
6166 goto erret;
6167 // empty line or comment
6168 line = (char_u *)"";
6169 continue;
6170 }
6171
6172 // Skip ":call" to get to the function name.
6173 if (checkforcmd(&ea.cmd, "call", 3))
6174 ea.cmd = skipwhite(ea.cmd);
6175
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006176 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006178 // Assuming the command starts with a variable or function name,
6179 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6180 // val".
6181 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6182 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006183 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006184 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006186 int oplen;
6187 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006189 oplen = assignment_len(skipwhite(p), &heredoc);
6190 if (oplen > 0)
6191 {
6192 // Recognize an assignment if we recognize the variable
6193 // name:
6194 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006195 // "local = expr" where "local" is a local var.
6196 // "script = expr" where "script" is a script-local var.
6197 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006198 // "&opt = expr"
6199 // "$ENV = expr"
6200 // "@r = expr"
6201 if (*ea.cmd == '&'
6202 || *ea.cmd == '$'
6203 || *ea.cmd == '@'
6204 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006205 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006206 || lookup_script(ea.cmd, p - ea.cmd) == OK
6207 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6208 {
6209 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6210 if (line == NULL)
6211 goto erret;
6212 continue;
6213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214 }
6215 }
6216 }
6217
6218 /*
6219 * COMMAND after range
6220 */
6221 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006222 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6223 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006224 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006225
6226 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6227 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006228 if (cctx.ctx_skip == TRUE)
6229 {
6230 line += STRLEN(line);
6231 continue;
6232 }
6233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006234 // Expression or function call.
6235 if (ea.cmdidx == CMD_eval)
6236 {
6237 p = ea.cmd;
6238 if (compile_expr1(&p, &cctx) == FAIL)
6239 goto erret;
6240
6241 // drop the return value
6242 generate_instr_drop(&cctx, ISN_DROP, 1);
6243 line = p;
6244 continue;
6245 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006246 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247 iemsg("Command from find_ex_command() not handled");
6248 goto erret;
6249 }
6250
6251 p = skipwhite(p);
6252
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006253 if (cctx.ctx_skip == TRUE
6254 && ea.cmdidx != CMD_elseif
6255 && ea.cmdidx != CMD_else
6256 && ea.cmdidx != CMD_endif)
6257 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006258 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006259 continue;
6260 }
6261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262 switch (ea.cmdidx)
6263 {
6264 case CMD_def:
6265 case CMD_function:
6266 // TODO: Nested function
6267 emsg("Nested function not implemented yet");
6268 goto erret;
6269
6270 case CMD_return:
6271 line = compile_return(p, set_return_type, &cctx);
6272 had_return = TRUE;
6273 break;
6274
6275 case CMD_let:
6276 case CMD_const:
6277 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6278 break;
6279
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006280 case CMD_unlet:
6281 case CMD_unlockvar:
6282 case CMD_lockvar:
6283 line = compile_unletlock(p, &ea, &cctx);
6284 break;
6285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 case CMD_import:
6287 line = compile_import(p, &cctx);
6288 break;
6289
6290 case CMD_if:
6291 line = compile_if(p, &cctx);
6292 break;
6293 case CMD_elseif:
6294 line = compile_elseif(p, &cctx);
6295 break;
6296 case CMD_else:
6297 line = compile_else(p, &cctx);
6298 break;
6299 case CMD_endif:
6300 line = compile_endif(p, &cctx);
6301 break;
6302
6303 case CMD_while:
6304 line = compile_while(p, &cctx);
6305 break;
6306 case CMD_endwhile:
6307 line = compile_endwhile(p, &cctx);
6308 break;
6309
6310 case CMD_for:
6311 line = compile_for(p, &cctx);
6312 break;
6313 case CMD_endfor:
6314 line = compile_endfor(p, &cctx);
6315 break;
6316 case CMD_continue:
6317 line = compile_continue(p, &cctx);
6318 break;
6319 case CMD_break:
6320 line = compile_break(p, &cctx);
6321 break;
6322
6323 case CMD_try:
6324 line = compile_try(p, &cctx);
6325 break;
6326 case CMD_catch:
6327 line = compile_catch(p, &cctx);
6328 break;
6329 case CMD_finally:
6330 line = compile_finally(p, &cctx);
6331 break;
6332 case CMD_endtry:
6333 line = compile_endtry(p, &cctx);
6334 break;
6335 case CMD_throw:
6336 line = compile_throw(p, &cctx);
6337 break;
6338
6339 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006340 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006341 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006342 case CMD_echomsg:
6343 case CMD_echoerr:
6344 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006345 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346
6347 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006348 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006349 // Not recognized, execute with do_cmdline_cmd().
6350 ea.arg = p;
6351 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352 break;
6353 }
6354 if (line == NULL)
6355 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006356 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357
6358 if (cctx.ctx_type_stack.ga_len < 0)
6359 {
6360 iemsg("Type stack underflow");
6361 goto erret;
6362 }
6363 }
6364
6365 if (cctx.ctx_scope != NULL)
6366 {
6367 if (cctx.ctx_scope->se_type == IF_SCOPE)
6368 emsg(_(e_endif));
6369 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6370 emsg(_(e_endwhile));
6371 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6372 emsg(_(e_endfor));
6373 else
6374 emsg(_("E1026: Missing }"));
6375 goto erret;
6376 }
6377
6378 if (!had_return)
6379 {
6380 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6381 {
6382 emsg(_("E1027: Missing return statement"));
6383 goto erret;
6384 }
6385
6386 // Return zero if there is no return at the end.
6387 generate_PUSHNR(&cctx, 0);
6388 generate_instr(&cctx, ISN_RETURN);
6389 }
6390
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006391 {
6392 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6393 + ufunc->uf_dfunc_idx;
6394 dfunc->df_deleted = FALSE;
6395 dfunc->df_instr = instr->ga_data;
6396 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006397 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006398 if (cctx.ctx_outer_used)
6399 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006402 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006403 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006404 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006405
6406 // Create a type for the function, with the return type and any
6407 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006408 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6409 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006410 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006411 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006412 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6413 argcount, &ufunc->uf_type_list);
6414 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006415 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006416 argcount + varargs,
6417 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006418 {
6419 ret = FAIL;
6420 goto erret;
6421 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006422 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6423 ufunc->uf_func_type->tt_min_argcount =
6424 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006425 if (ufunc->uf_arg_types == NULL)
6426 {
6427 int i;
6428
6429 // lambda does not have argument types.
6430 for (i = 0; i < argcount; ++i)
6431 ufunc->uf_func_type->tt_args[i] = &t_any;
6432 }
6433 else
6434 mch_memmove(ufunc->uf_func_type->tt_args,
6435 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006436 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006437 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006438 ufunc->uf_func_type->tt_args[argcount] =
6439 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006440 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6441 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006442 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006443 else
6444 // No arguments, can use a predefined type.
6445 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6446 argcount, &ufunc->uf_type_list);
6447
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006448 }
6449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006450 ret = OK;
6451
6452erret:
6453 if (ret == FAIL)
6454 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006455 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006456 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6457 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006458
6459 for (idx = 0; idx < instr->ga_len; ++idx)
6460 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006461 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006463 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006464 if (!dfunc->df_deleted)
6465 --def_functions.ga_len;
6466
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006467 while (cctx.ctx_scope != NULL)
6468 drop_scope(&cctx);
6469
Bram Moolenaar20431c92020-03-20 18:39:46 +01006470 // Don't execute this function body.
6471 ga_clear_strings(&ufunc->uf_lines);
6472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006473 if (errormsg != NULL)
6474 emsg(errormsg);
6475 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006476 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477 }
6478
6479 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006480 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006481 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483}
6484
6485/*
6486 * Delete an instruction, free what it contains.
6487 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006488 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006489delete_instr(isn_T *isn)
6490{
6491 switch (isn->isn_type)
6492 {
6493 case ISN_EXEC:
6494 case ISN_LOADENV:
6495 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006496 case ISN_LOADB:
6497 case ISN_LOADW:
6498 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006499 case ISN_LOADOPT:
6500 case ISN_MEMBER:
6501 case ISN_PUSHEXC:
6502 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006503 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006504 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006505 case ISN_STOREB:
6506 case ISN_STOREW:
6507 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006508 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509 vim_free(isn->isn_arg.string);
6510 break;
6511
6512 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006513 case ISN_STORES:
6514 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006515 break;
6516
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006517 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006518 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006519 vim_free(isn->isn_arg.unlet.ul_name);
6520 break;
6521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 case ISN_STOREOPT:
6523 vim_free(isn->isn_arg.storeopt.so_name);
6524 break;
6525
6526 case ISN_PUSHBLOB: // push blob isn_arg.blob
6527 blob_unref(isn->isn_arg.blob);
6528 break;
6529
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006530 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006531#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006532 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006533#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006534 break;
6535
6536 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006537#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006538 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006539#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006540 break;
6541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 case ISN_UCALL:
6543 vim_free(isn->isn_arg.ufunc.cuf_name);
6544 break;
6545
6546 case ISN_2BOOL:
6547 case ISN_2STRING:
6548 case ISN_ADDBLOB:
6549 case ISN_ADDLIST:
6550 case ISN_BCALL:
6551 case ISN_CATCH:
6552 case ISN_CHECKNR:
6553 case ISN_CHECKTYPE:
6554 case ISN_COMPAREANY:
6555 case ISN_COMPAREBLOB:
6556 case ISN_COMPAREBOOL:
6557 case ISN_COMPAREDICT:
6558 case ISN_COMPAREFLOAT:
6559 case ISN_COMPAREFUNC:
6560 case ISN_COMPARELIST:
6561 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 case ISN_COMPARESPECIAL:
6563 case ISN_COMPARESTRING:
6564 case ISN_CONCAT:
6565 case ISN_DCALL:
6566 case ISN_DROP:
6567 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006568 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006569 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006570 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006571 case ISN_EXECCONCAT:
6572 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 case ISN_FOR:
6574 case ISN_FUNCREF:
6575 case ISN_INDEX:
6576 case ISN_JUMP:
6577 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006578 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006579 case ISN_LOADSCRIPT:
6580 case ISN_LOADREG:
6581 case ISN_LOADV:
6582 case ISN_NEGATENR:
6583 case ISN_NEWDICT:
6584 case ISN_NEWLIST:
6585 case ISN_OPNR:
6586 case ISN_OPFLOAT:
6587 case ISN_OPANY:
6588 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006589 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 case ISN_PUSHF:
6591 case ISN_PUSHNR:
6592 case ISN_PUSHBOOL:
6593 case ISN_PUSHSPEC:
6594 case ISN_RETURN:
6595 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006596 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006598 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 case ISN_STORESCRIPT:
6600 case ISN_THROW:
6601 case ISN_TRY:
6602 // nothing allocated
6603 break;
6604 }
6605}
6606
6607/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006608 * Free all instructions for "dfunc".
6609 */
6610 static void
6611delete_def_function_contents(dfunc_T *dfunc)
6612{
6613 int idx;
6614
6615 ga_clear(&dfunc->df_def_args_isn);
6616
6617 if (dfunc->df_instr != NULL)
6618 {
6619 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6620 delete_instr(dfunc->df_instr + idx);
6621 VIM_CLEAR(dfunc->df_instr);
6622 }
6623
6624 dfunc->df_deleted = TRUE;
6625}
6626
6627/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628 * When a user function is deleted, delete any associated def function.
6629 */
6630 void
6631delete_def_function(ufunc_T *ufunc)
6632{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633 if (ufunc->uf_dfunc_idx >= 0)
6634 {
6635 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6636 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637
Bram Moolenaar20431c92020-03-20 18:39:46 +01006638 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 }
6640}
6641
6642#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006643/*
6644 * Free all functions defined with ":def".
6645 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006646 void
6647free_def_functions(void)
6648{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006649 int idx;
6650
6651 for (idx = 0; idx < def_functions.ga_len; ++idx)
6652 {
6653 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6654
6655 delete_def_function_contents(dfunc);
6656 }
6657
6658 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659}
6660#endif
6661
6662
6663#endif // FEAT_EVAL