blob: e23dd7a31b0244dbe731817e5d0f3fd0c90145b2 [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
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
146static char e_var_notfound[] = N_("E1001: variable not found: %s");
147static char e_syntax_at[] = N_("E1002: Syntax error at %s");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200148static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200149static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +0200150static char e_namespace[] = N_("E1075: Namespace not supported: %s");
Bram Moolenaarf9b2b492020-08-05 14:34:14 +0200151static char e_unknown_var[] = N_("E1089: unknown variable: %s");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
Bram Moolenaar20431c92020-03-20 18:39:46 +0100153static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154
155/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200156 * Lookup variable "name" in the local scope and return it.
157 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160lookup_local(char_u *name, size_t len, cctx_T *cctx)
161{
162 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200163 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200166 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100172 if (STRNCMP(name, lvar->lv_name, len) == 0
173 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
175 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200176 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100178 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200179
180 // Find local in outer function scope.
181 if (cctx->ctx_outer != NULL)
182 {
183 lvar = lookup_local(name, len, cctx->ctx_outer);
184 if (lvar != NULL)
185 {
186 // TODO: are there situations we should not mark the outer scope as
187 // used?
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 return lvar;
191 }
192 }
193
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195}
196
197/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198 * Lookup an argument in the current function and an enclosing function.
199 * Returns the argument index in "idxp"
200 * Returns the argument type in "type"
201 * Sets "gen_load_outer" to TRUE if found in outer scope.
202 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203 */
204 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200205lookup_arg(
206 char_u *name,
207 size_t len,
208 int *idxp,
209 type_T **type,
210 int *gen_load_outer,
211 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212{
213 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200214 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100216 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
219 {
220 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
221
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200222 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
223 {
224 if (idxp != NULL)
225 {
226 // Arguments are located above the frame pointer. One further
227 // if there is a vararg argument
228 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
229 + STACK_FRAME_SIZE)
230 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
231
232 if (cctx->ctx_ufunc->uf_arg_types != NULL)
233 *type = cctx->ctx_ufunc->uf_arg_types[idx];
234 else
235 *type = &t_any;
236 }
237 return OK;
238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200241 va_name = cctx->ctx_ufunc->uf_va_name;
242 if (va_name != NULL
243 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
244 {
245 if (idxp != NULL)
246 {
247 // varargs is always the last argument
248 *idxp = -STACK_FRAME_SIZE - 1;
249 *type = cctx->ctx_ufunc->uf_va_type;
250 }
251 return OK;
252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200254 if (cctx->ctx_outer != NULL)
255 {
256 // Lookup the name for an argument of the outer function.
257 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
258 == OK)
259 {
260 *gen_load_outer = TRUE;
261 return OK;
262 }
263 }
264
265 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266}
267
268/*
269 * Lookup a variable in the current script.
270 * Returns OK or FAIL.
271 */
272 static int
273lookup_script(char_u *name, size_t len)
274{
275 int cc;
276 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
277 dictitem_T *di;
278
279 cc = name[len];
280 name[len] = NUL;
281 di = find_var_in_ht(ht, 0, name, TRUE);
282 name[len] = cc;
283 return di == NULL ? FAIL: OK;
284}
285
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100286/*
287 * Check if "p[len]" is already defined, either in script "import_sid" or in
288 * compilation context "cctx".
289 * Return FAIL and give an error if it defined.
290 */
291 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200292check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100293{
Bram Moolenaarad486a02020-08-01 23:22:18 +0200294 int c = p[len];
295
296 p[len] = NUL;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100297 if (lookup_script(p, len) == OK
298 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200299 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200300 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200301 || find_imported(p, len, cctx) != NULL
302 || find_func_even_dead(p, FALSE, cctx) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100303 {
Bram Moolenaarad486a02020-08-01 23:22:18 +0200304 p[len] = c;
Bram Moolenaareef21022020-08-01 22:16:43 +0200305 semsg(_(e_already_defined), p);
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100306 return FAIL;
307 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200308 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100309 return OK;
310}
311
Bram Moolenaar65b95452020-07-19 14:03:09 +0200312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313/////////////////////////////////////////////////////////////////////
314// Following generate_ functions expect the caller to call ga_grow().
315
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200316#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
317#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319/*
320 * Generate an instruction without arguments.
321 * Returns a pointer to the new instruction, NULL if failed.
322 */
323 static isn_T *
324generate_instr(cctx_T *cctx, isntype_T isn_type)
325{
326 garray_T *instr = &cctx->ctx_instr;
327 isn_T *isn;
328
Bram Moolenaar080457c2020-03-03 21:53:32 +0100329 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330 if (ga_grow(instr, 1) == FAIL)
331 return NULL;
332 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
333 isn->isn_type = isn_type;
334 isn->isn_lnum = cctx->ctx_lnum + 1;
335 ++instr->ga_len;
336
337 return isn;
338}
339
340/*
341 * Generate an instruction without arguments.
342 * "drop" will be removed from the stack.
343 * Returns a pointer to the new instruction, NULL if failed.
344 */
345 static isn_T *
346generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
347{
348 garray_T *stack = &cctx->ctx_type_stack;
349
Bram Moolenaar080457c2020-03-03 21:53:32 +0100350 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100351 stack->ga_len -= drop;
352 return generate_instr(cctx, isn_type);
353}
354
355/*
356 * Generate instruction "isn_type" and put "type" on the type stack.
357 */
358 static isn_T *
359generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
360{
361 isn_T *isn;
362 garray_T *stack = &cctx->ctx_type_stack;
363
364 if ((isn = generate_instr(cctx, isn_type)) == NULL)
365 return NULL;
366
367 if (ga_grow(stack, 1) == FAIL)
368 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200369 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 ++stack->ga_len;
371
372 return isn;
373}
374
375/*
376 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
377 */
378 static int
379may_generate_2STRING(int offset, cctx_T *cctx)
380{
381 isn_T *isn;
382 garray_T *stack = &cctx->ctx_type_stack;
383 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
384
385 if ((*type)->tt_type == VAR_STRING)
386 return OK;
387 *type = &t_string;
388
389 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
390 return FAIL;
391 isn->isn_arg.number = offset;
392
393 return OK;
394}
395
396 static int
397check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
398{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200399 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200401 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100402 {
403 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200404 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405 else
406 semsg(_("E1036: %c requires number or float arguments"), *op);
407 return FAIL;
408 }
409 return OK;
410}
411
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200412 static int
413generate_add_instr(
414 cctx_T *cctx,
415 vartype_T vartype,
416 type_T *type1,
417 type_T *type2)
418{
419 isn_T *isn = generate_instr_drop(cctx,
420 vartype == VAR_NUMBER ? ISN_OPNR
421 : vartype == VAR_LIST ? ISN_ADDLIST
422 : vartype == VAR_BLOB ? ISN_ADDBLOB
423#ifdef FEAT_FLOAT
424 : vartype == VAR_FLOAT ? ISN_OPFLOAT
425#endif
426 : ISN_OPANY, 1);
427
428 if (vartype != VAR_LIST && vartype != VAR_BLOB
429 && type1->tt_type != VAR_ANY
430 && type2->tt_type != VAR_ANY
431 && check_number_or_float(
432 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
433 return FAIL;
434
435 if (isn != NULL)
436 isn->isn_arg.op.op_type = EXPR_ADD;
437 return isn == NULL ? FAIL : OK;
438}
439
440/*
441 * Get the type to use for an instruction for an operation on "type1" and
442 * "type2". If they are matching use a type-specific instruction. Otherwise
443 * fall back to runtime type checking.
444 */
445 static vartype_T
446operator_type(type_T *type1, type_T *type2)
447{
448 if (type1->tt_type == type2->tt_type
449 && (type1->tt_type == VAR_NUMBER
450 || type1->tt_type == VAR_LIST
451#ifdef FEAT_FLOAT
452 || type1->tt_type == VAR_FLOAT
453#endif
454 || type1->tt_type == VAR_BLOB))
455 return type1->tt_type;
456 return VAR_ANY;
457}
458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100459/*
460 * Generate an instruction with two arguments. The instruction depends on the
461 * type of the arguments.
462 */
463 static int
464generate_two_op(cctx_T *cctx, char_u *op)
465{
466 garray_T *stack = &cctx->ctx_type_stack;
467 type_T *type1;
468 type_T *type2;
469 vartype_T vartype;
470 isn_T *isn;
471
Bram Moolenaar080457c2020-03-03 21:53:32 +0100472 RETURN_OK_IF_SKIP(cctx);
473
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200474 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
476 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200477 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478
479 switch (*op)
480 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200481 case '+':
482 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484 break;
485
486 case '-':
487 case '*':
488 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
489 op) == FAIL)
490 return FAIL;
491 if (vartype == VAR_NUMBER)
492 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
493#ifdef FEAT_FLOAT
494 else if (vartype == VAR_FLOAT)
495 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
496#endif
497 else
498 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
499 if (isn != NULL)
500 isn->isn_arg.op.op_type = *op == '*'
501 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
502 break;
503
Bram Moolenaar4c683752020-04-05 21:38:23 +0200504 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100505 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200506 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507 && type2->tt_type != VAR_NUMBER))
508 {
509 emsg(_("E1035: % requires number arguments"));
510 return FAIL;
511 }
512 isn = generate_instr_drop(cctx,
513 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
514 if (isn != NULL)
515 isn->isn_arg.op.op_type = EXPR_REM;
516 break;
517 }
518
519 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200520 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 {
522 type_T *type = &t_any;
523
524#ifdef FEAT_FLOAT
525 // float+number and number+float results in float
526 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
527 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
528 type = &t_float;
529#endif
530 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
531 }
532
533 return OK;
534}
535
536/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200537 * Get the instruction to use for comparing "type1" with "type2"
538 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200540 static isntype_T
541get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100542{
543 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544
Bram Moolenaar4c683752020-04-05 21:38:23 +0200545 if (type1 == VAR_UNKNOWN)
546 type1 = VAR_ANY;
547 if (type2 == VAR_UNKNOWN)
548 type2 = VAR_ANY;
549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 if (type1 == type2)
551 {
552 switch (type1)
553 {
554 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
555 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
556 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
557 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
558 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
559 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
560 case VAR_LIST: isntype = ISN_COMPARELIST; break;
561 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
562 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 default: isntype = ISN_COMPAREANY; break;
564 }
565 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200566 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
568 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
569 isntype = ISN_COMPAREANY;
570
571 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
572 && (isntype == ISN_COMPAREBOOL
573 || isntype == ISN_COMPARESPECIAL
574 || isntype == ISN_COMPARENR
575 || isntype == ISN_COMPAREFLOAT))
576 {
577 semsg(_("E1037: Cannot use \"%s\" with %s"),
578 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200579 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 }
581 if (isntype == ISN_DROP
582 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
583 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
584 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
585 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
586 && exptype != EXPR_IS && exptype != EXPR_ISNOT
587 && (type1 == VAR_BLOB || type2 == VAR_BLOB
588 || type1 == VAR_LIST || type2 == VAR_LIST))))
589 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100590 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200592 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200594 return isntype;
595}
596
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200597 int
598check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
599{
600 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
601 return FAIL;
602 return OK;
603}
604
Bram Moolenaara5565e42020-05-09 15:44:01 +0200605/*
606 * Generate an ISN_COMPARE* instruction with a boolean result.
607 */
608 static int
609generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
610{
611 isntype_T isntype;
612 isn_T *isn;
613 garray_T *stack = &cctx->ctx_type_stack;
614 vartype_T type1;
615 vartype_T type2;
616
617 RETURN_OK_IF_SKIP(cctx);
618
619 // Get the known type of the two items on the stack. If they are matching
620 // use a type-specific instruction. Otherwise fall back to runtime type
621 // checking.
622 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
623 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
624 isntype = get_compare_isn(exptype, type1, type2);
625 if (isntype == ISN_DROP)
626 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627
628 if ((isn = generate_instr(cctx, isntype)) == NULL)
629 return FAIL;
630 isn->isn_arg.op.op_type = exptype;
631 isn->isn_arg.op.op_ic = ic;
632
633 // takes two arguments, puts one bool back
634 if (stack->ga_len >= 2)
635 {
636 --stack->ga_len;
637 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
638 }
639
640 return OK;
641}
642
643/*
644 * Generate an ISN_2BOOL instruction.
645 */
646 static int
647generate_2BOOL(cctx_T *cctx, int invert)
648{
649 isn_T *isn;
650 garray_T *stack = &cctx->ctx_type_stack;
651
Bram Moolenaar080457c2020-03-03 21:53:32 +0100652 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
654 return FAIL;
655 isn->isn_arg.number = invert;
656
657 // type becomes bool
658 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
659
660 return OK;
661}
662
663 static int
664generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
665{
666 isn_T *isn;
667 garray_T *stack = &cctx->ctx_type_stack;
668
Bram Moolenaar080457c2020-03-03 21:53:32 +0100669 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
671 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200672 // TODO: whole type, e.g. for a function also arg and return types
673 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674 isn->isn_arg.type.ct_off = offset;
675
676 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200677 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678
679 return OK;
680}
681
682/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200683 * Check that
684 * - "actual" is "expected" type or
685 * - "actual" is a type that can be "expected" type: add a runtime check; or
686 * - return FAIL.
687 */
688 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200689need_type(
690 type_T *actual,
691 type_T *expected,
692 int offset,
693 cctx_T *cctx,
694 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200695{
696 if (check_type(expected, actual, FALSE) == OK)
697 return OK;
698 if (actual->tt_type != VAR_ANY
699 && actual->tt_type != VAR_UNKNOWN
700 && !(actual->tt_type == VAR_FUNC
701 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
702 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200703 if (!silent)
704 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200705 return FAIL;
706 }
707 generate_TYPECHECK(cctx, expected, offset);
708 return OK;
709}
710
711/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100712 * Generate an ISN_PUSHNR instruction.
713 */
714 static int
715generate_PUSHNR(cctx_T *cctx, varnumber_T number)
716{
717 isn_T *isn;
718
Bram Moolenaar080457c2020-03-03 21:53:32 +0100719 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
721 return FAIL;
722 isn->isn_arg.number = number;
723
724 return OK;
725}
726
727/*
728 * Generate an ISN_PUSHBOOL instruction.
729 */
730 static int
731generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
732{
733 isn_T *isn;
734
Bram Moolenaar080457c2020-03-03 21:53:32 +0100735 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
737 return FAIL;
738 isn->isn_arg.number = number;
739
740 return OK;
741}
742
743/*
744 * Generate an ISN_PUSHSPEC instruction.
745 */
746 static int
747generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
748{
749 isn_T *isn;
750
Bram Moolenaar080457c2020-03-03 21:53:32 +0100751 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
753 return FAIL;
754 isn->isn_arg.number = number;
755
756 return OK;
757}
758
759#ifdef FEAT_FLOAT
760/*
761 * Generate an ISN_PUSHF instruction.
762 */
763 static int
764generate_PUSHF(cctx_T *cctx, float_T fnumber)
765{
766 isn_T *isn;
767
Bram Moolenaar080457c2020-03-03 21:53:32 +0100768 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
770 return FAIL;
771 isn->isn_arg.fnumber = fnumber;
772
773 return OK;
774}
775#endif
776
777/*
778 * Generate an ISN_PUSHS instruction.
779 * Consumes "str".
780 */
781 static int
782generate_PUSHS(cctx_T *cctx, char_u *str)
783{
784 isn_T *isn;
785
Bram Moolenaar080457c2020-03-03 21:53:32 +0100786 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
788 return FAIL;
789 isn->isn_arg.string = str;
790
791 return OK;
792}
793
794/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100795 * Generate an ISN_PUSHCHANNEL instruction.
796 * Consumes "channel".
797 */
798 static int
799generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
800{
801 isn_T *isn;
802
Bram Moolenaar080457c2020-03-03 21:53:32 +0100803 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100804 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
805 return FAIL;
806 isn->isn_arg.channel = channel;
807
808 return OK;
809}
810
811/*
812 * Generate an ISN_PUSHJOB instruction.
813 * Consumes "job".
814 */
815 static int
816generate_PUSHJOB(cctx_T *cctx, job_T *job)
817{
818 isn_T *isn;
819
Bram Moolenaar080457c2020-03-03 21:53:32 +0100820 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100821 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100822 return FAIL;
823 isn->isn_arg.job = job;
824
825 return OK;
826}
827
828/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 * Generate an ISN_PUSHBLOB instruction.
830 * Consumes "blob".
831 */
832 static int
833generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
834{
835 isn_T *isn;
836
Bram Moolenaar080457c2020-03-03 21:53:32 +0100837 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
839 return FAIL;
840 isn->isn_arg.blob = blob;
841
842 return OK;
843}
844
845/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100846 * Generate an ISN_PUSHFUNC instruction with name "name".
847 * Consumes "name".
848 */
849 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200850generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100851{
852 isn_T *isn;
853
Bram Moolenaar080457c2020-03-03 21:53:32 +0100854 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200855 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100856 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200857 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100858
859 return OK;
860}
861
862/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200863 * Generate an ISN_GETITEM instruction with "index".
864 */
865 static int
866generate_GETITEM(cctx_T *cctx, int index)
867{
868 isn_T *isn;
869 garray_T *stack = &cctx->ctx_type_stack;
870 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
871 type_T *item_type = &t_any;
872
873 RETURN_OK_IF_SKIP(cctx);
874
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200875 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200876 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200877 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200878 emsg(_(e_listreq));
879 return FAIL;
880 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200881 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200882 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
883 return FAIL;
884 isn->isn_arg.number = index;
885
886 // add the item type to the type stack
887 if (ga_grow(stack, 1) == FAIL)
888 return FAIL;
889 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
890 ++stack->ga_len;
891 return OK;
892}
893
894/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200895 * Generate an ISN_SLICE instruction with "count".
896 */
897 static int
898generate_SLICE(cctx_T *cctx, int count)
899{
900 isn_T *isn;
901
902 RETURN_OK_IF_SKIP(cctx);
903 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
904 return FAIL;
905 isn->isn_arg.number = count;
906 return OK;
907}
908
909/*
910 * Generate an ISN_CHECKLEN instruction with "min_len".
911 */
912 static int
913generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
914{
915 isn_T *isn;
916
917 RETURN_OK_IF_SKIP(cctx);
918
919 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
920 return FAIL;
921 isn->isn_arg.checklen.cl_min_len = min_len;
922 isn->isn_arg.checklen.cl_more_OK = more_OK;
923
924 return OK;
925}
926
927/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 * Generate an ISN_STORE instruction.
929 */
930 static int
931generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
932{
933 isn_T *isn;
934
Bram Moolenaar080457c2020-03-03 21:53:32 +0100935 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
937 return FAIL;
938 if (name != NULL)
939 isn->isn_arg.string = vim_strsave(name);
940 else
941 isn->isn_arg.number = idx;
942
943 return OK;
944}
945
946/*
947 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
948 */
949 static int
950generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
951{
952 isn_T *isn;
953
Bram Moolenaar080457c2020-03-03 21:53:32 +0100954 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
956 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100957 isn->isn_arg.storenr.stnr_idx = idx;
958 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959
960 return OK;
961}
962
963/*
964 * Generate an ISN_STOREOPT instruction
965 */
966 static int
967generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
968{
969 isn_T *isn;
970
Bram Moolenaar080457c2020-03-03 21:53:32 +0100971 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +0200972 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 return FAIL;
974 isn->isn_arg.storeopt.so_name = vim_strsave(name);
975 isn->isn_arg.storeopt.so_flags = opt_flags;
976
977 return OK;
978}
979
980/*
981 * Generate an ISN_LOAD or similar instruction.
982 */
983 static int
984generate_LOAD(
985 cctx_T *cctx,
986 isntype_T isn_type,
987 int idx,
988 char_u *name,
989 type_T *type)
990{
991 isn_T *isn;
992
Bram Moolenaar080457c2020-03-03 21:53:32 +0100993 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
995 return FAIL;
996 if (name != NULL)
997 isn->isn_arg.string = vim_strsave(name);
998 else
999 isn->isn_arg.number = idx;
1000
1001 return OK;
1002}
1003
1004/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001005 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001006 */
1007 static int
1008generate_LOADV(
1009 cctx_T *cctx,
1010 char_u *name,
1011 int error)
1012{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001013 int di_flags;
1014 int vidx = find_vim_var(name, &di_flags);
1015 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001016
Bram Moolenaar080457c2020-03-03 21:53:32 +01001017 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001018 if (vidx < 0)
1019 {
1020 if (error)
1021 semsg(_(e_var_notfound), name);
1022 return FAIL;
1023 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001024 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001025
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001026 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001027}
1028
1029/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001030 * Generate an ISN_UNLET instruction.
1031 */
1032 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001033generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001034{
1035 isn_T *isn;
1036
1037 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001038 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001039 return FAIL;
1040 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1041 isn->isn_arg.unlet.ul_forceit = forceit;
1042
1043 return OK;
1044}
1045
1046/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047 * Generate an ISN_LOADS instruction.
1048 */
1049 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001050generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001052 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001054 int sid,
1055 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056{
1057 isn_T *isn;
1058
Bram Moolenaar080457c2020-03-03 21:53:32 +01001059 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001060 if (isn_type == ISN_LOADS)
1061 isn = generate_instr_type(cctx, isn_type, type);
1062 else
1063 isn = generate_instr_drop(cctx, isn_type, 1);
1064 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001066 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1067 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068
1069 return OK;
1070}
1071
1072/*
1073 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1074 */
1075 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001076generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 cctx_T *cctx,
1078 isntype_T isn_type,
1079 int sid,
1080 int idx,
1081 type_T *type)
1082{
1083 isn_T *isn;
1084
Bram Moolenaar080457c2020-03-03 21:53:32 +01001085 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086 if (isn_type == ISN_LOADSCRIPT)
1087 isn = generate_instr_type(cctx, isn_type, type);
1088 else
1089 isn = generate_instr_drop(cctx, isn_type, 1);
1090 if (isn == NULL)
1091 return FAIL;
1092 isn->isn_arg.script.script_sid = sid;
1093 isn->isn_arg.script.script_idx = idx;
1094 return OK;
1095}
1096
1097/*
1098 * Generate an ISN_NEWLIST instruction.
1099 */
1100 static int
1101generate_NEWLIST(cctx_T *cctx, int count)
1102{
1103 isn_T *isn;
1104 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 type_T *type;
1106 type_T *member;
1107
Bram Moolenaar080457c2020-03-03 21:53:32 +01001108 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1110 return FAIL;
1111 isn->isn_arg.number = count;
1112
1113 // drop the value types
1114 stack->ga_len -= count;
1115
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001116 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001117 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 if (count > 0)
1119 member = ((type_T **)stack->ga_data)[stack->ga_len];
1120 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001121 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001122 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123
1124 // add the list type to the type stack
1125 if (ga_grow(stack, 1) == FAIL)
1126 return FAIL;
1127 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1128 ++stack->ga_len;
1129
1130 return OK;
1131}
1132
1133/*
1134 * Generate an ISN_NEWDICT instruction.
1135 */
1136 static int
1137generate_NEWDICT(cctx_T *cctx, int count)
1138{
1139 isn_T *isn;
1140 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 type_T *type;
1142 type_T *member;
1143
Bram Moolenaar080457c2020-03-03 21:53:32 +01001144 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1146 return FAIL;
1147 isn->isn_arg.number = count;
1148
1149 // drop the key and value types
1150 stack->ga_len -= 2 * count;
1151
Bram Moolenaar436472f2020-02-20 22:54:43 +01001152 // Use the first value type for the list member type. Use "void" for an
1153 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 if (count > 0)
1155 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1156 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001157 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001158 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159
1160 // add the dict type to the type stack
1161 if (ga_grow(stack, 1) == FAIL)
1162 return FAIL;
1163 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1164 ++stack->ga_len;
1165
1166 return OK;
1167}
1168
1169/*
1170 * Generate an ISN_FUNCREF instruction.
1171 */
1172 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001173generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174{
1175 isn_T *isn;
1176 garray_T *stack = &cctx->ctx_type_stack;
1177
Bram Moolenaar080457c2020-03-03 21:53:32 +01001178 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1180 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001181 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001182 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183
1184 if (ga_grow(stack, 1) == FAIL)
1185 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001186 ((type_T **)stack->ga_data)[stack->ga_len] =
1187 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 ++stack->ga_len;
1189
1190 return OK;
1191}
1192
1193/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001194 * Generate an ISN_NEWFUNC instruction.
1195 */
1196 static int
1197generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1198{
1199 isn_T *isn;
1200 char_u *name;
1201
1202 RETURN_OK_IF_SKIP(cctx);
1203 name = vim_strsave(lambda_name);
1204 if (name == NULL)
1205 return FAIL;
1206 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1207 return FAIL;
1208 isn->isn_arg.newfunc.nf_lambda = name;
1209 isn->isn_arg.newfunc.nf_global = func_name;
1210
1211 return OK;
1212}
1213
1214/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 * Generate an ISN_JUMP instruction.
1216 */
1217 static int
1218generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1219{
1220 isn_T *isn;
1221 garray_T *stack = &cctx->ctx_type_stack;
1222
Bram Moolenaar080457c2020-03-03 21:53:32 +01001223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1225 return FAIL;
1226 isn->isn_arg.jump.jump_when = when;
1227 isn->isn_arg.jump.jump_where = where;
1228
1229 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1230 --stack->ga_len;
1231
1232 return OK;
1233}
1234
1235 static int
1236generate_FOR(cctx_T *cctx, int loop_idx)
1237{
1238 isn_T *isn;
1239 garray_T *stack = &cctx->ctx_type_stack;
1240
Bram Moolenaar080457c2020-03-03 21:53:32 +01001241 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1243 return FAIL;
1244 isn->isn_arg.forloop.for_idx = loop_idx;
1245
1246 if (ga_grow(stack, 1) == FAIL)
1247 return FAIL;
1248 // type doesn't matter, will be stored next
1249 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1250 ++stack->ga_len;
1251
1252 return OK;
1253}
1254
1255/*
1256 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001257 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 * Return FAIL if the number of arguments is wrong.
1259 */
1260 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001261generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262{
1263 isn_T *isn;
1264 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001265 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001266 type_T *argtypes[MAX_FUNC_ARGS];
1267 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268
Bram Moolenaar080457c2020-03-03 21:53:32 +01001269 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001270 argoff = check_internal_func(func_idx, argcount);
1271 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 return FAIL;
1273
Bram Moolenaar389df252020-07-09 21:20:47 +02001274 if (method_call && argoff > 1)
1275 {
1276 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1277 return FAIL;
1278 isn->isn_arg.shuffle.shfl_item = argcount;
1279 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1280 }
1281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1283 return FAIL;
1284 isn->isn_arg.bfunc.cbf_idx = func_idx;
1285 isn->isn_arg.bfunc.cbf_argcount = argcount;
1286
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001287 for (i = 0; i < argcount; ++i)
1288 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 stack->ga_len -= argcount; // drop the arguments
1291 if (ga_grow(stack, 1) == FAIL)
1292 return FAIL;
1293 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001294 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 ++stack->ga_len; // add return value
1296
1297 return OK;
1298}
1299
1300/*
1301 * Generate an ISN_DCALL or ISN_UCALL instruction.
1302 * Return FAIL if the number of arguments is wrong.
1303 */
1304 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001305generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306{
1307 isn_T *isn;
1308 garray_T *stack = &cctx->ctx_type_stack;
1309 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001310 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001311
Bram Moolenaar080457c2020-03-03 21:53:32 +01001312 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 if (argcount > regular_args && !has_varargs(ufunc))
1314 {
1315 semsg(_(e_toomanyarg), ufunc->uf_name);
1316 return FAIL;
1317 }
1318 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1319 {
1320 semsg(_(e_toofewarg), ufunc->uf_name);
1321 return FAIL;
1322 }
1323
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001324 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001325 {
1326 int i;
1327
1328 for (i = 0; i < argcount; ++i)
1329 {
1330 type_T *expected;
1331 type_T *actual;
1332
1333 if (i < regular_args)
1334 {
1335 if (ufunc->uf_arg_types == NULL)
1336 continue;
1337 expected = ufunc->uf_arg_types[i];
1338 }
1339 else
1340 expected = ufunc->uf_va_type->tt_member;
1341 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001342 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001343 {
1344 arg_type_mismatch(expected, actual, i + 1);
1345 return FAIL;
1346 }
1347 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001348 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001349 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1350 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001351 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001352 }
1353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001355 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001356 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001358 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 {
1360 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1361 isn->isn_arg.dfunc.cdf_argcount = argcount;
1362 }
1363 else
1364 {
1365 // A user function may be deleted and redefined later, can't use the
1366 // ufunc pointer, need to look it up again at runtime.
1367 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1368 isn->isn_arg.ufunc.cuf_argcount = argcount;
1369 }
1370
1371 stack->ga_len -= argcount; // drop the arguments
1372 if (ga_grow(stack, 1) == FAIL)
1373 return FAIL;
1374 // add return value
1375 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1376 ++stack->ga_len;
1377
1378 return OK;
1379}
1380
1381/*
1382 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1383 */
1384 static int
1385generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1386{
1387 isn_T *isn;
1388 garray_T *stack = &cctx->ctx_type_stack;
1389
Bram Moolenaar080457c2020-03-03 21:53:32 +01001390 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1392 return FAIL;
1393 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1394 isn->isn_arg.ufunc.cuf_argcount = argcount;
1395
1396 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001397 if (ga_grow(stack, 1) == FAIL)
1398 return FAIL;
1399 // add return value
1400 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1401 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402
1403 return OK;
1404}
1405
1406/*
1407 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001408 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 */
1410 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001411generate_PCALL(
1412 cctx_T *cctx,
1413 int argcount,
1414 char_u *name,
1415 type_T *type,
1416 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417{
1418 isn_T *isn;
1419 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001420 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421
Bram Moolenaar080457c2020-03-03 21:53:32 +01001422 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001423
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001424 if (type->tt_type == VAR_ANY)
1425 ret_type = &t_any;
1426 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001427 {
1428 if (type->tt_argcount != -1)
1429 {
1430 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1431
1432 if (argcount < type->tt_min_argcount - varargs)
1433 {
1434 semsg(_(e_toofewarg), "[reference]");
1435 return FAIL;
1436 }
1437 if (!varargs && argcount > type->tt_argcount)
1438 {
1439 semsg(_(e_toomanyarg), "[reference]");
1440 return FAIL;
1441 }
1442 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001443 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001444 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001445 else
1446 {
1447 semsg(_("E1085: Not a callable type: %s"), name);
1448 return FAIL;
1449 }
1450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1452 return FAIL;
1453 isn->isn_arg.pfunc.cpf_top = at_top;
1454 isn->isn_arg.pfunc.cpf_argcount = argcount;
1455
1456 stack->ga_len -= argcount; // drop the arguments
1457
1458 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001459 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001461 // If partial is above the arguments it must be cleared and replaced with
1462 // the return value.
1463 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1464 return FAIL;
1465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 return OK;
1467}
1468
1469/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001470 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 */
1472 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001473generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474{
1475 isn_T *isn;
1476 garray_T *stack = &cctx->ctx_type_stack;
1477 type_T *type;
1478
Bram Moolenaar080457c2020-03-03 21:53:32 +01001479 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001480 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001482 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001484 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001486 if (type->tt_type != VAR_DICT && type != &t_any)
1487 {
1488 emsg(_(e_dictreq));
1489 return FAIL;
1490 }
1491 // change dict type to dict member type
1492 if (type->tt_type == VAR_DICT)
1493 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494
1495 return OK;
1496}
1497
1498/*
1499 * Generate an ISN_ECHO instruction.
1500 */
1501 static int
1502generate_ECHO(cctx_T *cctx, int with_white, int count)
1503{
1504 isn_T *isn;
1505
Bram Moolenaar080457c2020-03-03 21:53:32 +01001506 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1508 return FAIL;
1509 isn->isn_arg.echo.echo_with_white = with_white;
1510 isn->isn_arg.echo.echo_count = count;
1511
1512 return OK;
1513}
1514
Bram Moolenaarad39c092020-02-26 18:23:43 +01001515/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001516 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001517 */
1518 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001519generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001520{
1521 isn_T *isn;
1522
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001523 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001524 return FAIL;
1525 isn->isn_arg.number = count;
1526
1527 return OK;
1528}
1529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 static int
1531generate_EXEC(cctx_T *cctx, char_u *line)
1532{
1533 isn_T *isn;
1534
Bram Moolenaar080457c2020-03-03 21:53:32 +01001535 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1537 return FAIL;
1538 isn->isn_arg.string = vim_strsave(line);
1539 return OK;
1540}
1541
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001542 static int
1543generate_EXECCONCAT(cctx_T *cctx, int count)
1544{
1545 isn_T *isn;
1546
1547 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1548 return FAIL;
1549 isn->isn_arg.number = count;
1550 return OK;
1551}
1552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553/*
1554 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001555 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001557 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1559{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 lvar_T *lvar;
1561
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001562 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001564 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001565 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 }
1567
1568 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001569 return NULL;
1570 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001572 // Every local variable uses the next entry on the stack. We could re-use
1573 // the last ones when leaving a scope, but then variables used in a closure
1574 // might get overwritten. To keep things simple do not re-use stack
1575 // entries. This is less efficient, but memory is cheap these days.
1576 lvar->lv_idx = cctx->ctx_locals_count++;
1577
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001578 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 lvar->lv_const = isConst;
1580 lvar->lv_type = type;
1581
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001582 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583}
1584
1585/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001586 * Remove local variables above "new_top".
1587 */
1588 static void
1589unwind_locals(cctx_T *cctx, int new_top)
1590{
1591 if (cctx->ctx_locals.ga_len > new_top)
1592 {
1593 int idx;
1594 lvar_T *lvar;
1595
1596 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1597 {
1598 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1599 vim_free(lvar->lv_name);
1600 }
1601 }
1602 cctx->ctx_locals.ga_len = new_top;
1603}
1604
1605/*
1606 * Free all local variables.
1607 */
1608 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001609free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001610{
1611 unwind_locals(cctx, 0);
1612 ga_clear(&cctx->ctx_locals);
1613}
1614
1615/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 * Find "name" in script-local items of script "sid".
1617 * Returns the index in "sn_var_vals" if found.
1618 * If found but not in "sn_var_vals" returns -1.
1619 * If not found returns -2.
1620 */
1621 int
1622get_script_item_idx(int sid, char_u *name, int check_writable)
1623{
1624 hashtab_T *ht;
1625 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001626 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 int idx;
1628
1629 // First look the name up in the hashtable.
1630 if (sid <= 0 || sid > script_items.ga_len)
1631 return -1;
1632 ht = &SCRIPT_VARS(sid);
1633 di = find_var_in_ht(ht, 0, name, TRUE);
1634 if (di == NULL)
1635 return -2;
1636
1637 // Now find the svar_T index in sn_var_vals.
1638 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1639 {
1640 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1641
1642 if (sv->sv_tv == &di->di_tv)
1643 {
1644 if (check_writable && sv->sv_const)
1645 semsg(_(e_readonlyvar), name);
1646 return idx;
1647 }
1648 }
1649 return -1;
1650}
1651
1652/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001653 * Find "name" in imported items of the current script or in "cctx" if not
1654 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 */
1656 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001657find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 int idx;
1660
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001661 if (current_sctx.sc_sid <= 0)
1662 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 if (cctx != NULL)
1664 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1665 {
1666 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1667 + idx;
1668
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001669 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1670 : STRLEN(import->imp_name) == len
1671 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 return import;
1673 }
1674
Bram Moolenaarefa94442020-08-08 22:16:00 +02001675 return find_imported_in_script(name, len, current_sctx.sc_sid);
1676}
1677
1678 imported_T *
1679find_imported_in_script(char_u *name, size_t len, int sid)
1680{
1681 scriptitem_T *si = SCRIPT_ITEM(sid);
1682 int idx;
1683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1685 {
1686 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1687
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001688 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1689 : STRLEN(import->imp_name) == len
1690 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 return import;
1692 }
1693 return NULL;
1694}
1695
1696/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001697 * Free all imported variables.
1698 */
1699 static void
1700free_imported(cctx_T *cctx)
1701{
1702 int idx;
1703
1704 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1705 {
1706 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1707
1708 vim_free(import->imp_name);
1709 }
1710 ga_clear(&cctx->ctx_imports);
1711}
1712
1713/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001714 * Return TRUE if "p" points at a "#" but not at "#{".
1715 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001716 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001717vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001718{
1719 return p[0] == '#' && p[1] != '{';
1720}
1721
1722/*
1723 * Return a pointer to the next line that isn't empty or only contains a
1724 * comment. Skips over white space.
1725 * Returns NULL if there is none.
1726 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001727 char_u *
1728peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001729{
1730 int lnum = cctx->ctx_lnum;
1731
1732 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1733 {
1734 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001735 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001736
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001737 if (line == NULL)
1738 break;
1739 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001740 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02001741 return p;
1742 }
1743 return NULL;
1744}
1745
1746/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001747 * Called when checking for a following operator at "arg". When the rest of
1748 * the line is empty or only a comment, peek the next line. If there is a next
1749 * line return a pointer to it and set "nextp".
1750 * Otherwise skip over white space.
1751 */
1752 static char_u *
1753may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1754{
1755 char_u *p = skipwhite(arg);
1756
1757 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001758 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001759 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001760 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001761 if (*nextp != NULL)
1762 return *nextp;
1763 }
1764 return p;
1765}
1766
1767/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001768 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001769 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001770 * Returns NULL when at the end.
1771 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001772 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001773next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001774{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001775 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001776
1777 do
1778 {
1779 ++cctx->ctx_lnum;
1780 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001781 {
1782 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001783 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001784 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001785 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001786 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001787 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001788 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001789 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001790 return line;
1791}
1792
1793/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001794 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001795 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001796 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1797 */
1798 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001799may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001800{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001801 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001802 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001803 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001804
1805 if (next == NULL)
1806 return FAIL;
1807 *arg = skipwhite(next);
1808 }
1809 return OK;
1810}
1811
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001812/*
1813 * Idem, and give an error when failed.
1814 */
1815 static int
1816may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1817{
1818 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1819 {
1820 emsg(_("E1097: line incomplete"));
1821 return FAIL;
1822 }
1823 return OK;
1824}
1825
1826
Bram Moolenaara5565e42020-05-09 15:44:01 +02001827// Structure passed between the compile_expr* functions to keep track of
1828// constants that have been parsed but for which no code was produced yet. If
1829// possible expressions on these constants are applied at compile time. If
1830// that is not possible, the code to push the constants needs to be generated
1831// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001832// Using 50 should be more than enough of 5 levels of ().
1833#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001834typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001835 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001836 int pp_used; // active entries in pp_tv[]
1837} ppconst_T;
1838
Bram Moolenaar1c747212020-05-09 18:28:34 +02001839static int compile_expr0(char_u **arg, cctx_T *cctx);
1840static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1841
Bram Moolenaara5565e42020-05-09 15:44:01 +02001842/*
1843 * Generate a PUSH instruction for "tv".
1844 * "tv" will be consumed or cleared.
1845 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1846 */
1847 static int
1848generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1849{
1850 if (tv != NULL)
1851 {
1852 switch (tv->v_type)
1853 {
1854 case VAR_UNKNOWN:
1855 break;
1856 case VAR_BOOL:
1857 generate_PUSHBOOL(cctx, tv->vval.v_number);
1858 break;
1859 case VAR_SPECIAL:
1860 generate_PUSHSPEC(cctx, tv->vval.v_number);
1861 break;
1862 case VAR_NUMBER:
1863 generate_PUSHNR(cctx, tv->vval.v_number);
1864 break;
1865#ifdef FEAT_FLOAT
1866 case VAR_FLOAT:
1867 generate_PUSHF(cctx, tv->vval.v_float);
1868 break;
1869#endif
1870 case VAR_BLOB:
1871 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1872 tv->vval.v_blob = NULL;
1873 break;
1874 case VAR_STRING:
1875 generate_PUSHS(cctx, tv->vval.v_string);
1876 tv->vval.v_string = NULL;
1877 break;
1878 default:
1879 iemsg("constant type not supported");
1880 clear_tv(tv);
1881 return FAIL;
1882 }
1883 tv->v_type = VAR_UNKNOWN;
1884 }
1885 return OK;
1886}
1887
1888/*
1889 * Generate code for any ppconst entries.
1890 */
1891 static int
1892generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1893{
1894 int i;
1895 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001896 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001897
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001898 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001899 for (i = 0; i < ppconst->pp_used; ++i)
1900 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1901 ret = FAIL;
1902 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001903 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001904 return ret;
1905}
1906
1907/*
1908 * Clear ppconst constants. Used when failing.
1909 */
1910 static void
1911clear_ppconst(ppconst_T *ppconst)
1912{
1913 int i;
1914
1915 for (i = 0; i < ppconst->pp_used; ++i)
1916 clear_tv(&ppconst->pp_tv[i]);
1917 ppconst->pp_used = 0;
1918}
1919
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001920/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001921 * Generate an instruction to load script-local variable "name", without the
1922 * leading "s:".
1923 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 */
1925 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001926compile_load_scriptvar(
1927 cctx_T *cctx,
1928 char_u *name, // variable NUL terminated
1929 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001930 char_u **end, // end of variable
1931 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001933 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1935 imported_T *import;
1936
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001937 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001939 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001940 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1941 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 }
1943 if (idx >= 0)
1944 {
1945 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1946
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001947 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 current_sctx.sc_sid, idx, sv->sv_type);
1949 return OK;
1950 }
1951
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001952 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 if (import != NULL)
1954 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001955 if (import->imp_all)
1956 {
1957 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02001958 char_u *exp_name;
1959 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001960 ufunc_T *ufunc;
1961 type_T *type;
1962
1963 // Used "import * as Name", need to lookup the member.
1964 if (*p != '.')
1965 {
1966 semsg(_("E1060: expected dot after name: %s"), start);
1967 return FAIL;
1968 }
1969 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001970 if (VIM_ISWHITE(*p))
1971 {
1972 emsg(_("E1074: no white space allowed after dot"));
1973 return FAIL;
1974 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001975
Bram Moolenaar1c991142020-07-04 13:15:31 +02001976 // isolate one name
1977 exp_name = p;
1978 while (eval_isnamec(*p))
1979 ++p;
1980 cc = *p;
1981 *p = NUL;
1982
1983 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
1984 *p = cc;
1985 p = skipwhite(p);
1986
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001987 // TODO: what if it is a function?
1988 if (idx < 0)
1989 return FAIL;
1990 *end = p;
1991
1992 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1993 import->imp_sid,
1994 idx,
1995 type);
1996 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001997 else if (import->imp_funcname != NULL)
1998 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001999 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002000 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2001 import->imp_sid,
2002 import->imp_var_vals_idx,
2003 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 return OK;
2005 }
2006
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002007 if (error)
2008 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 return FAIL;
2010}
2011
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002012 static int
2013generate_funcref(cctx_T *cctx, char_u *name)
2014{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002015 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002016
2017 if (ufunc == NULL)
2018 return FAIL;
2019
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002020 // Need to compile any default values to get the argument types.
2021 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2022 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2023 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002024 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002025}
2026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027/*
2028 * Compile a variable name into a load instruction.
2029 * "end" points to just after the name.
2030 * When "error" is FALSE do not give an error when not found.
2031 */
2032 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002033compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034{
2035 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002036 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002037 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002039 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040
2041 if (*(*arg + 1) == ':')
2042 {
2043 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002044 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002045 {
2046 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002048 switch (**arg)
2049 {
2050 case 'g': isn_type = ISN_LOADGDICT; break;
2051 case 'w': isn_type = ISN_LOADWDICT; break;
2052 case 't': isn_type = ISN_LOADTDICT; break;
2053 case 'b': isn_type = ISN_LOADBDICT; break;
2054 default:
2055 semsg(_(e_namespace), *arg);
2056 goto theend;
2057 }
2058 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2059 goto theend;
2060 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002061 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 else
2063 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002064 isntype_T isn_type = ISN_DROP;
2065
2066 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2067 if (name == NULL)
2068 return FAIL;
2069
2070 switch (**arg)
2071 {
2072 case 'v': res = generate_LOADV(cctx, name, error);
2073 break;
2074 case 's': res = compile_load_scriptvar(cctx, name,
2075 NULL, NULL, error);
2076 break;
2077 case 'g': isn_type = ISN_LOADG; break;
2078 case 'w': isn_type = ISN_LOADW; break;
2079 case 't': isn_type = ISN_LOADT; break;
2080 case 'b': isn_type = ISN_LOADB; break;
2081 default: semsg(_(e_namespace), *arg);
2082 goto theend;
2083 }
2084 if (isn_type != ISN_DROP)
2085 {
2086 // Global, Buffer-local, Window-local and Tabpage-local
2087 // variables can be defined later, thus we don't check if it
2088 // exists, give error at runtime.
2089 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2090 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091 }
2092 }
2093 else
2094 {
2095 size_t len = end - *arg;
2096 int idx;
2097 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002098 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099
2100 name = vim_strnsave(*arg, end - *arg);
2101 if (name == NULL)
2102 return FAIL;
2103
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002104 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002106 if (!gen_load_outer)
2107 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 }
2109 else
2110 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002111 lvar_T *lvar = lookup_local(*arg, len, cctx);
2112
2113 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002115 type = lvar->lv_type;
2116 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002117 if (lvar->lv_from_outer)
2118 gen_load_outer = TRUE;
2119 else
2120 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 }
2122 else
2123 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002124 // "var" can be script-local even without using "s:" if it
2125 // already exists.
2126 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2127 == SCRIPT_VERSION_VIM9
2128 || lookup_script(*arg, len) == OK)
2129 res = compile_load_scriptvar(cctx, name, *arg, &end,
2130 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002131
Bram Moolenaara5565e42020-05-09 15:44:01 +02002132 // When the name starts with an uppercase letter or "x:" it
2133 // can be a user defined function.
2134 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2135 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 }
2137 }
2138 if (gen_load)
2139 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002140 if (gen_load_outer)
2141 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 }
2143
2144 *arg = end;
2145
2146theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002147 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 semsg(_(e_var_notfound), name);
2149 vim_free(name);
2150 return res;
2151}
2152
2153/*
2154 * Compile the argument expressions.
2155 * "arg" points to just after the "(" and is advanced to after the ")"
2156 */
2157 static int
2158compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2159{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002160 char_u *p = *arg;
2161 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162
Bram Moolenaare6085c52020-04-12 20:19:16 +02002163 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002165 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2166 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002167 if (*p == ')')
2168 {
2169 *arg = p + 1;
2170 return OK;
2171 }
2172
Bram Moolenaara5565e42020-05-09 15:44:01 +02002173 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 return FAIL;
2175 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002176
2177 if (*p != ',' && *skipwhite(p) == ',')
2178 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002179 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002180 p = skipwhite(p);
2181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002183 {
2184 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002185 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002186 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002187 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002188 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002189 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002191failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002192 emsg(_(e_missing_close));
2193 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194}
2195
2196/*
2197 * Compile a function call: name(arg1, arg2)
2198 * "arg" points to "name", "arg + varlen" to the "(".
2199 * "argcount_init" is 1 for "value->method()"
2200 * Instructions:
2201 * EVAL arg1
2202 * EVAL arg2
2203 * BCALL / DCALL / UCALL
2204 */
2205 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002206compile_call(
2207 char_u **arg,
2208 size_t varlen,
2209 cctx_T *cctx,
2210 ppconst_T *ppconst,
2211 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212{
2213 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002214 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 int argcount = argcount_init;
2216 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002217 char_u fname_buf[FLEN_FIXED + 1];
2218 char_u *tofree = NULL;
2219 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002221 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222
Bram Moolenaara5565e42020-05-09 15:44:01 +02002223 // we can evaluate "has('name')" at compile time
2224 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2225 {
2226 char_u *s = skipwhite(*arg + varlen + 1);
2227 typval_T argvars[2];
2228
2229 argvars[0].v_type = VAR_UNKNOWN;
2230 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002231 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002232 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002233 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002234 s = skipwhite(s);
2235 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2236 {
2237 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2238
2239 *arg = s + 1;
2240 argvars[1].v_type = VAR_UNKNOWN;
2241 tv->v_type = VAR_NUMBER;
2242 tv->vval.v_number = 0;
2243 f_has(argvars, tv);
2244 clear_tv(&argvars[0]);
2245 ++ppconst->pp_used;
2246 return OK;
2247 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002248 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002249 }
2250
2251 if (generate_ppconst(cctx, ppconst) == FAIL)
2252 return FAIL;
2253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 if (varlen >= sizeof(namebuf))
2255 {
2256 semsg(_("E1011: name too long: %s"), name);
2257 return FAIL;
2258 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002259 vim_strncpy(namebuf, *arg, varlen);
2260 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261
2262 *arg = skipwhite(*arg + varlen + 1);
2263 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002264 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002266 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 {
2268 int idx;
2269
2270 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002271 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002273 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002274 else
2275 semsg(_(e_unknownfunc), namebuf);
2276 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 }
2278
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002280 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002282 {
2283 res = generate_CALL(cctx, ufunc, argcount);
2284 goto theend;
2285 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286
2287 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002288 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002290 if (STRNCMP(namebuf, "g:", 2) != 0
2291 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002292 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002293 garray_T *stack = &cctx->ctx_type_stack;
2294 type_T *type;
2295
2296 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2297 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002298 goto theend;
2299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002301 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002302 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002303 if (STRNCMP(namebuf, "g:", 2) == 0)
2304 res = generate_UCALL(cctx, name, argcount);
2305 else
2306 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002307
2308theend:
2309 vim_free(tofree);
2310 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311}
2312
2313// like NAMESPACE_CHAR but with 'a' and 'l'.
2314#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2315
2316/*
2317 * Find the end of a variable or function name. Unlike find_name_end() this
2318 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002319 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 * Return a pointer to just after the name. Equal to "arg" if there is no
2321 * valid name.
2322 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002323 static char_u *
2324to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325{
2326 char_u *p;
2327
2328 // Quick check for valid starting character.
2329 if (!eval_isnamec1(*arg))
2330 return arg;
2331
2332 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2333 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2334 // and can be used in slice "[n:]".
2335 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002336 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2338 break;
2339 return p;
2340}
2341
2342/*
2343 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002344 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 */
2346 char_u *
2347to_name_const_end(char_u *arg)
2348{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002349 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 typval_T rettv;
2351
2352 if (p == arg && *arg == '[')
2353 {
2354
2355 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002356 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 p = arg;
2358 }
2359 else if (p == arg && *arg == '#' && arg[1] == '{')
2360 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002361 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002363 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 p = arg;
2365 }
2366 else if (p == arg && *arg == '{')
2367 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002368 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002370 // Can be "{x -> ret}()".
2371 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002373 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 if (ret != OK)
2375 p = arg;
2376 }
2377
2378 return p;
2379}
2380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381/*
2382 * parse a list: [expr, expr]
2383 * "*arg" points to the '['.
2384 */
2385 static int
2386compile_list(char_u **arg, cctx_T *cctx)
2387{
2388 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002389 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 int count = 0;
2391
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002392 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002394 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002395 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002396 semsg(_(e_list_end), *arg);
2397 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002398 }
2399 if (*p == ']')
2400 {
2401 ++p;
2402 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002403 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002404 p += STRLEN(p);
2405 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002406 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002407 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 break;
2409 ++count;
2410 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002411 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002413 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2414 {
2415 semsg(_(e_white_after), ",");
2416 return FAIL;
2417 }
2418 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002419 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 p = skipwhite(p);
2421 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002422 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423
2424 generate_NEWLIST(cctx, count);
2425 return OK;
2426}
2427
2428/*
2429 * parse a lambda: {arg, arg -> expr}
2430 * "*arg" points to the '{'.
2431 */
2432 static int
2433compile_lambda(char_u **arg, cctx_T *cctx)
2434{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 typval_T rettv;
2436 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002437 evalarg_T evalarg;
2438
2439 CLEAR_FIELD(evalarg);
2440 evalarg.eval_flags = EVAL_EVALUATE;
2441 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442
2443 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002444 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002448 ++ufunc->uf_refcount;
2449 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002450 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451
2452 // The function will have one line: "return {expr}".
2453 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002454 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002456 clear_evalarg(&evalarg, NULL);
2457
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002458 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002459 {
2460 // The return type will now be known.
2461 set_function_type(ufunc);
2462
2463 return generate_FUNCREF(cctx, ufunc);
2464 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002465
2466 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 return FAIL;
2468}
2469
2470/*
2471 * Compile a lamda call: expr->{lambda}(args)
2472 * "arg" points to the "{".
2473 */
2474 static int
2475compile_lambda_call(char_u **arg, cctx_T *cctx)
2476{
2477 ufunc_T *ufunc;
2478 typval_T rettv;
2479 int argcount = 1;
2480 int ret = FAIL;
2481
2482 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002483 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 return FAIL;
2485
2486 if (**arg != '(')
2487 {
2488 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002489 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 else
2491 semsg(_(e_missing_paren), "lambda");
2492 clear_tv(&rettv);
2493 return FAIL;
2494 }
2495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 ufunc = rettv.vval.v_partial->pt_func;
2497 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002498 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002499 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002500
2501 // The function will have one line: "return {expr}".
2502 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002503 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504
2505 // compile the arguments
2506 *arg = skipwhite(*arg + 1);
2507 if (compile_arguments(arg, cctx, &argcount) == OK)
2508 // call the compiled function
2509 ret = generate_CALL(cctx, ufunc, argcount);
2510
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002511 if (ret == FAIL)
2512 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 return ret;
2514}
2515
2516/*
2517 * parse a dict: {'key': val} or #{key: val}
2518 * "*arg" points to the '{'.
2519 */
2520 static int
2521compile_dict(char_u **arg, cctx_T *cctx, int literal)
2522{
2523 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002524 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 int count = 0;
2526 dict_T *d = dict_alloc();
2527 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002528 char_u *whitep = *arg;
2529 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530
2531 if (d == NULL)
2532 return FAIL;
2533 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002534 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 {
2536 char_u *key = NULL;
2537
Bram Moolenaar23c55272020-06-21 16:58:13 +02002538 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002539 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002540 *arg = NULL;
2541 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002542 }
2543
2544 if (**arg == '}')
2545 break;
2546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 if (literal)
2548 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002549 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550
Bram Moolenaar2c330432020-04-13 14:41:35 +02002551 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 {
2553 semsg(_("E1014: Invalid key: %s"), *arg);
2554 return FAIL;
2555 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002556 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 if (generate_PUSHS(cctx, key) == FAIL)
2558 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002559 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 }
2561 else
2562 {
2563 isn_T *isn;
2564
Bram Moolenaara5565e42020-05-09 15:44:01 +02002565 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2568 if (isn->isn_type == ISN_PUSHS)
2569 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002570 else
2571 {
2572 type_T *keytype = ((type_T **)stack->ga_data)
2573 [stack->ga_len - 1];
2574 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2575 return FAIL;
2576 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 }
2578
2579 // Check for duplicate keys, if using string keys.
2580 if (key != NULL)
2581 {
2582 item = dict_find(d, key, -1);
2583 if (item != NULL)
2584 {
2585 semsg(_(e_duplicate_key), key);
2586 goto failret;
2587 }
2588 item = dictitem_alloc(key);
2589 if (item != NULL)
2590 {
2591 item->di_tv.v_type = VAR_UNKNOWN;
2592 item->di_tv.v_lock = 0;
2593 if (dict_add(d, item) == FAIL)
2594 dictitem_free(item);
2595 }
2596 }
2597
2598 *arg = skipwhite(*arg);
2599 if (**arg != ':')
2600 {
2601 semsg(_(e_missing_dict_colon), *arg);
2602 return FAIL;
2603 }
2604
Bram Moolenaar2c330432020-04-13 14:41:35 +02002605 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002607 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002608 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002609 *arg = NULL;
2610 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002611 }
2612
Bram Moolenaara5565e42020-05-09 15:44:01 +02002613 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 return FAIL;
2615 ++count;
2616
Bram Moolenaar2c330432020-04-13 14:41:35 +02002617 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002618 *arg = skipwhite(*arg);
2619 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002620 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002621 *arg = NULL;
2622 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002623 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 if (**arg == '}')
2625 break;
2626 if (**arg != ',')
2627 {
2628 semsg(_(e_missing_dict_comma), *arg);
2629 goto failret;
2630 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002631 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 *arg = skipwhite(*arg + 1);
2633 }
2634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 *arg = *arg + 1;
2636
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002637 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002638 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002639 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002640 *arg += STRLEN(*arg);
2641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 dict_unref(d);
2643 return generate_NEWDICT(cctx, count);
2644
2645failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002646 if (*arg == NULL)
2647 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 dict_unref(d);
2649 return FAIL;
2650}
2651
2652/*
2653 * Compile "&option".
2654 */
2655 static int
2656compile_get_option(char_u **arg, cctx_T *cctx)
2657{
2658 typval_T rettv;
2659 char_u *start = *arg;
2660 int ret;
2661
2662 // parse the option and get the current value to get the type.
2663 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002664 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 if (ret == OK)
2666 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002667 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 char_u *name = vim_strnsave(start, *arg - start);
2669 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2670
2671 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2672 vim_free(name);
2673 }
2674 clear_tv(&rettv);
2675
2676 return ret;
2677}
2678
2679/*
2680 * Compile "$VAR".
2681 */
2682 static int
2683compile_get_env(char_u **arg, cctx_T *cctx)
2684{
2685 char_u *start = *arg;
2686 int len;
2687 int ret;
2688 char_u *name;
2689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 ++*arg;
2691 len = get_env_len(arg);
2692 if (len == 0)
2693 {
2694 semsg(_(e_syntax_at), start - 1);
2695 return FAIL;
2696 }
2697
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002698 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 name = vim_strnsave(start, len + 1);
2700 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2701 vim_free(name);
2702 return ret;
2703}
2704
2705/*
2706 * Compile "@r".
2707 */
2708 static int
2709compile_get_register(char_u **arg, cctx_T *cctx)
2710{
2711 int ret;
2712
2713 ++*arg;
2714 if (**arg == NUL)
2715 {
2716 semsg(_(e_syntax_at), *arg - 1);
2717 return FAIL;
2718 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002719 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 {
2721 emsg_invreg(**arg);
2722 return FAIL;
2723 }
2724 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2725 ++*arg;
2726 return ret;
2727}
2728
2729/*
2730 * Apply leading '!', '-' and '+' to constant "rettv".
2731 */
2732 static int
2733apply_leader(typval_T *rettv, char_u *start, char_u *end)
2734{
2735 char_u *p = end;
2736
2737 // this works from end to start
2738 while (p > start)
2739 {
2740 --p;
2741 if (*p == '-' || *p == '+')
2742 {
2743 // only '-' has an effect, for '+' we only check the type
2744#ifdef FEAT_FLOAT
2745 if (rettv->v_type == VAR_FLOAT)
2746 {
2747 if (*p == '-')
2748 rettv->vval.v_float = -rettv->vval.v_float;
2749 }
2750 else
2751#endif
2752 {
2753 varnumber_T val;
2754 int error = FALSE;
2755
2756 // tv_get_number_chk() accepts a string, but we don't want that
2757 // here
2758 if (check_not_string(rettv) == FAIL)
2759 return FAIL;
2760 val = tv_get_number_chk(rettv, &error);
2761 clear_tv(rettv);
2762 if (error)
2763 return FAIL;
2764 if (*p == '-')
2765 val = -val;
2766 rettv->v_type = VAR_NUMBER;
2767 rettv->vval.v_number = val;
2768 }
2769 }
2770 else
2771 {
2772 int v = tv2bool(rettv);
2773
2774 // '!' is permissive in the type.
2775 clear_tv(rettv);
2776 rettv->v_type = VAR_BOOL;
2777 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2778 }
2779 }
2780 return OK;
2781}
2782
2783/*
2784 * Recognize v: variables that are constants and set "rettv".
2785 */
2786 static void
2787get_vim_constant(char_u **arg, typval_T *rettv)
2788{
2789 if (STRNCMP(*arg, "v:true", 6) == 0)
2790 {
2791 rettv->v_type = VAR_BOOL;
2792 rettv->vval.v_number = VVAL_TRUE;
2793 *arg += 6;
2794 }
2795 else if (STRNCMP(*arg, "v:false", 7) == 0)
2796 {
2797 rettv->v_type = VAR_BOOL;
2798 rettv->vval.v_number = VVAL_FALSE;
2799 *arg += 7;
2800 }
2801 else if (STRNCMP(*arg, "v:null", 6) == 0)
2802 {
2803 rettv->v_type = VAR_SPECIAL;
2804 rettv->vval.v_number = VVAL_NULL;
2805 *arg += 6;
2806 }
2807 else if (STRNCMP(*arg, "v:none", 6) == 0)
2808 {
2809 rettv->v_type = VAR_SPECIAL;
2810 rettv->vval.v_number = VVAL_NONE;
2811 *arg += 6;
2812 }
2813}
2814
Bram Moolenaar696ba232020-07-29 21:20:41 +02002815 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002816get_compare_type(char_u *p, int *len, int *type_is)
2817{
2818 exptype_T type = EXPR_UNKNOWN;
2819 int i;
2820
2821 switch (p[0])
2822 {
2823 case '=': if (p[1] == '=')
2824 type = EXPR_EQUAL;
2825 else if (p[1] == '~')
2826 type = EXPR_MATCH;
2827 break;
2828 case '!': if (p[1] == '=')
2829 type = EXPR_NEQUAL;
2830 else if (p[1] == '~')
2831 type = EXPR_NOMATCH;
2832 break;
2833 case '>': if (p[1] != '=')
2834 {
2835 type = EXPR_GREATER;
2836 *len = 1;
2837 }
2838 else
2839 type = EXPR_GEQUAL;
2840 break;
2841 case '<': if (p[1] != '=')
2842 {
2843 type = EXPR_SMALLER;
2844 *len = 1;
2845 }
2846 else
2847 type = EXPR_SEQUAL;
2848 break;
2849 case 'i': if (p[1] == 's')
2850 {
2851 // "is" and "isnot"; but not a prefix of a name
2852 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2853 *len = 5;
2854 i = p[*len];
2855 if (!isalnum(i) && i != '_')
2856 {
2857 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2858 *type_is = TRUE;
2859 }
2860 }
2861 break;
2862 }
2863 return type;
2864}
2865
2866/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 * Compile code to apply '-', '+' and '!'.
2868 */
2869 static int
2870compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2871{
2872 char_u *p = end;
2873
2874 // this works from end to start
2875 while (p > start)
2876 {
2877 --p;
2878 if (*p == '-' || *p == '+')
2879 {
2880 int negate = *p == '-';
2881 isn_T *isn;
2882
2883 // TODO: check type
2884 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2885 {
2886 --p;
2887 if (*p == '-')
2888 negate = !negate;
2889 }
2890 // only '-' has an effect, for '+' we only check the type
2891 if (negate)
2892 isn = generate_instr(cctx, ISN_NEGATENR);
2893 else
2894 isn = generate_instr(cctx, ISN_CHECKNR);
2895 if (isn == NULL)
2896 return FAIL;
2897 }
2898 else
2899 {
2900 int invert = TRUE;
2901
2902 while (p > start && p[-1] == '!')
2903 {
2904 --p;
2905 invert = !invert;
2906 }
2907 if (generate_2BOOL(cctx, invert) == FAIL)
2908 return FAIL;
2909 }
2910 }
2911 return OK;
2912}
2913
2914/*
2915 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002916 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 */
2918 static int
2919compile_subscript(
2920 char_u **arg,
2921 cctx_T *cctx,
2922 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02002923 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002924 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925{
2926 for (;;)
2927 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002928 char_u *p = skipwhite(*arg);
2929
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002930 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002931 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002932 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002933
2934 // If a following line starts with "->{" or "->X" advance to that
2935 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002936 // Also if a following line starts with ".x".
2937 if (next != NULL &&
2938 ((next[0] == '-' && next[1] == '>'
2939 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02002940 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002941 {
2942 next = next_line_from_context(cctx, TRUE);
2943 if (next == NULL)
2944 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002945 *arg = next;
2946 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002947 }
2948 }
2949
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002950 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
2951 // not a function call.
2952 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002954 garray_T *stack = &cctx->ctx_type_stack;
2955 type_T *type;
2956 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002958 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02002959 return FAIL;
2960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002962 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2963
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002964 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2966 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002967 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 return FAIL;
2969 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002970 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02002972 char_u *pstart = p;
2973
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002974 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02002975 return FAIL;
2976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 // something->method()
2978 // Apply the '!', '-' and '+' first:
2979 // -1.0->func() works like (-1.0)->func()
2980 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2981 return FAIL;
2982 *start_leader = end_leader; // don't apply again later
2983
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002984 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02002985 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02002986 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 if (**arg == '{')
2988 {
2989 // lambda call: list->{lambda}
2990 if (compile_lambda_call(arg, cctx) == FAIL)
2991 return FAIL;
2992 }
2993 else
2994 {
2995 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002996 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02002997 if (!eval_isnamec1(*p))
2998 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02002999 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003000 return FAIL;
3001 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003002 if (ASCII_ISALPHA(*p) && p[1] == ':')
3003 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003004 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 ;
3006 if (*p != '(')
3007 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003008 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 return FAIL;
3010 }
3011 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003012 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 return FAIL;
3014 }
3015 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003016 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003018 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003019 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003020 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003021
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003022 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003023 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003024 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003025 // TODO: blob index
3026 // TODO: more arguments
3027 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003028 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003029 return FAIL;
3030
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003031 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003032 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003033 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003034 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003035 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 return FAIL;
3037
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003038 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3039 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 if (**arg != ']')
3041 {
3042 emsg(_(e_missbrac));
3043 return FAIL;
3044 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003045 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003047 // We can index a list and a dict. If we don't know the type
3048 // we can use the index value type.
3049 // TODO: If we don't know use an instruction to figure it out at
3050 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003051 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003052 vtype = (*typep)->tt_type;
3053 if (*typep == &t_any)
3054 {
3055 type_T *valtype = ((type_T **)stack->ga_data)
3056 [stack->ga_len - 1];
3057 if (valtype == &t_string)
3058 vtype = VAR_DICT;
3059 }
3060 if (vtype == VAR_DICT)
3061 {
3062 if ((*typep)->tt_type == VAR_DICT)
3063 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003064 else
3065 {
3066 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3067 return FAIL;
3068 *typep = &t_any;
3069 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003070 if (may_generate_2STRING(-1, cctx) == FAIL)
3071 return FAIL;
3072 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3073 return FAIL;
3074 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003075 else if (vtype == VAR_STRING)
3076 {
3077 *typep = &t_number;
3078 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3079 return FAIL;
3080 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003081 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003082 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003083 if ((*typep)->tt_type == VAR_LIST)
3084 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003085 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003086 return FAIL;
3087 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003088 else
3089 {
3090 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003091 return FAIL;
3092 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003094 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003096 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003097 return FAIL;
3098
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003099 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003100 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3101 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003103 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003104 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 while (eval_isnamec(*p))
3106 MB_PTR_ADV(p);
3107 if (p == *arg)
3108 {
3109 semsg(_(e_syntax_at), *arg);
3110 return FAIL;
3111 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003112 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 return FAIL;
3114 *arg = p;
3115 }
3116 else
3117 break;
3118 }
3119
3120 // TODO - see handle_subscript():
3121 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3122 // Don't do this when "Func" is already a partial that was bound
3123 // explicitly (pt_auto is FALSE).
3124
3125 return OK;
3126}
3127
3128/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003129 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3130 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003132 * If the value is a constant "ppconst->pp_ret" will be set.
3133 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003134 *
3135 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 */
3137
3138/*
3139 * number number constant
3140 * 0zFFFFFFFF Blob constant
3141 * "string" string constant
3142 * 'string' literal string constant
3143 * &option-name option value
3144 * @r register contents
3145 * identifier variable value
3146 * function() function call
3147 * $VAR environment variable
3148 * (expression) nested expression
3149 * [expr, expr] List
3150 * {key: val, key: val} Dictionary
3151 * #{key: val, key: val} Dictionary with literal keys
3152 *
3153 * Also handle:
3154 * ! in front logical NOT
3155 * - in front unary minus
3156 * + in front unary plus (ignored)
3157 * trailing (arg) funcref/partial call
3158 * trailing [] subscript in String or List
3159 * trailing .name entry in Dictionary
3160 * trailing ->name() method call
3161 */
3162 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003163compile_expr7(
3164 char_u **arg,
3165 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003166 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 char_u *start_leader, *end_leader;
3169 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003170 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003171 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172
3173 /*
3174 * Skip '!', '-' and '+' characters. They are handled later.
3175 */
3176 start_leader = *arg;
3177 while (**arg == '!' || **arg == '-' || **arg == '+')
3178 *arg = skipwhite(*arg + 1);
3179 end_leader = *arg;
3180
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003181 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 switch (**arg)
3183 {
3184 /*
3185 * Number constant.
3186 */
3187 case '0': // also for blob starting with 0z
3188 case '1':
3189 case '2':
3190 case '3':
3191 case '4':
3192 case '5':
3193 case '6':
3194 case '7':
3195 case '8':
3196 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003197 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 return FAIL;
3199 break;
3200
3201 /*
3202 * String constant: "string".
3203 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003204 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 return FAIL;
3206 break;
3207
3208 /*
3209 * Literal string constant: 'str''ing'.
3210 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003211 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 return FAIL;
3213 break;
3214
3215 /*
3216 * Constant Vim variable.
3217 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003218 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 ret = NOTDONE;
3220 break;
3221
3222 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003223 * "true" constant
3224 */
3225 case 't': if (STRNCMP(*arg, "true", 4) == 0
3226 && !eval_isnamec((*arg)[4]))
3227 {
3228 *arg += 4;
3229 rettv->v_type = VAR_BOOL;
3230 rettv->vval.v_number = VVAL_TRUE;
3231 }
3232 else
3233 ret = NOTDONE;
3234 break;
3235
3236 /*
3237 * "false" constant
3238 */
3239 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3240 && !eval_isnamec((*arg)[5]))
3241 {
3242 *arg += 5;
3243 rettv->v_type = VAR_BOOL;
3244 rettv->vval.v_number = VVAL_FALSE;
3245 }
3246 else
3247 ret = NOTDONE;
3248 break;
3249
3250 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 * List: [expr, expr]
3252 */
3253 case '[': ret = compile_list(arg, cctx);
3254 break;
3255
3256 /*
3257 * Dictionary: #{key: val, key: val}
3258 */
3259 case '#': if ((*arg)[1] == '{')
3260 {
3261 ++*arg;
3262 ret = compile_dict(arg, cctx, TRUE);
3263 }
3264 else
3265 ret = NOTDONE;
3266 break;
3267
3268 /*
3269 * Lambda: {arg, arg -> expr}
3270 * Dictionary: {'key': val, 'key': val}
3271 */
3272 case '{': {
3273 char_u *start = skipwhite(*arg + 1);
3274
3275 // Find out what comes after the arguments.
3276 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003277 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 if (ret != FAIL && *start == '>')
3279 ret = compile_lambda(arg, cctx);
3280 else
3281 ret = compile_dict(arg, cctx, FALSE);
3282 }
3283 break;
3284
3285 /*
3286 * Option value: &name
3287 */
3288 case '&': ret = compile_get_option(arg, cctx);
3289 break;
3290
3291 /*
3292 * Environment variable: $VAR.
3293 */
3294 case '$': ret = compile_get_env(arg, cctx);
3295 break;
3296
3297 /*
3298 * Register contents: @r.
3299 */
3300 case '@': ret = compile_get_register(arg, cctx);
3301 break;
3302 /*
3303 * nested expression: (expression).
3304 */
3305 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003306
3307 // recursive!
3308 if (ppconst->pp_used <= PPSIZE - 10)
3309 {
3310 ret = compile_expr1(arg, cctx, ppconst);
3311 }
3312 else
3313 {
3314 // Not enough space in ppconst, flush constants.
3315 if (generate_ppconst(cctx, ppconst) == FAIL)
3316 return FAIL;
3317 ret = compile_expr0(arg, cctx);
3318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 *arg = skipwhite(*arg);
3320 if (**arg == ')')
3321 ++*arg;
3322 else if (ret == OK)
3323 {
3324 emsg(_(e_missing_close));
3325 ret = FAIL;
3326 }
3327 break;
3328
3329 default: ret = NOTDONE;
3330 break;
3331 }
3332 if (ret == FAIL)
3333 return FAIL;
3334
Bram Moolenaar1c747212020-05-09 18:28:34 +02003335 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 {
3337 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003338 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003340 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 return FAIL;
3342 }
3343 start_leader = end_leader; // don't apply again below
3344
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003345 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003346 clear_tv(rettv);
3347 else
3348 // A constant expression can possibly be handled compile time,
3349 // return the value instead of generating code.
3350 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 }
3352 else if (ret == NOTDONE)
3353 {
3354 char_u *p;
3355 int r;
3356
3357 if (!eval_isnamec1(**arg))
3358 {
3359 semsg(_("E1015: Name expected: %s"), *arg);
3360 return FAIL;
3361 }
3362
3363 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003364 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003366 {
3367 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003370 {
3371 if (generate_ppconst(cctx, ppconst) == FAIL)
3372 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003374 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 if (r == FAIL)
3376 return FAIL;
3377 }
3378
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003379 // Handle following "[]", ".member", etc.
3380 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003381 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003382 ppconst) == FAIL)
3383 return FAIL;
3384 if (ppconst->pp_used > 0)
3385 {
3386 // apply the '!', '-' and '+' before the constant
3387 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3388 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3389 return FAIL;
3390 return OK;
3391 }
3392 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003394 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395}
3396
3397/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003398 * Give the "white on both sides" error, taking the operator from "p[len]".
3399 */
3400 void
3401error_white_both(char_u *op, int len)
3402{
3403 char_u buf[10];
3404
3405 vim_strncpy(buf, op, len);
3406 semsg(_(e_white_both), buf);
3407}
3408
3409/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 * * number multiplication
3411 * / number division
3412 * % number modulo
3413 */
3414 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003415compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416{
3417 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003418 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003419 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003421 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003422 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 return FAIL;
3424
3425 /*
3426 * Repeat computing, until no "*", "/" or "%" is following.
3427 */
3428 for (;;)
3429 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003430 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 if (*op != '*' && *op != '/' && *op != '%')
3432 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003433 if (next != NULL)
3434 {
3435 *arg = next_line_from_context(cctx, TRUE);
3436 op = skipwhite(*arg);
3437 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003438
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003439 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003441 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003442 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 }
3444 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003445 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003446 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003448 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003449 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003451
3452 if (ppconst->pp_used == ppconst_used + 2
3453 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3454 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003455 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003456 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3457 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003458 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003460 // both are numbers: compute the result
3461 switch (*op)
3462 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003463 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003464 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003465 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003466 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003467 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003468 break;
3469 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003470 tv1->vval.v_number = res;
3471 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003472 }
3473 else
3474 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003475 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003476 generate_two_op(cctx, op);
3477 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 }
3479
3480 return OK;
3481}
3482
3483/*
3484 * + number addition
3485 * - number subtraction
3486 * .. string concatenation
3487 */
3488 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003489compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490{
3491 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003492 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003494 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495
3496 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003497 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 return FAIL;
3499
3500 /*
3501 * Repeat computing, until no "+", "-" or ".." is following.
3502 */
3503 for (;;)
3504 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003505 op = may_peek_next_line(cctx, *arg, &next);
3506 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 break;
3508 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003509 if (next != NULL)
3510 {
3511 *arg = next_line_from_context(cctx, TRUE);
3512 op = skipwhite(*arg);
3513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003515 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003517 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003518 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 }
3520
3521 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003522 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003523 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003525 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003526 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 return FAIL;
3528
Bram Moolenaara5565e42020-05-09 15:44:01 +02003529 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003530 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003531 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3532 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3533 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3534 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003536 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3537 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003538
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003539 // concat/subtract/add constant numbers
3540 if (*op == '+')
3541 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3542 else if (*op == '-')
3543 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3544 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003545 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003546 // concatenate constant strings
3547 char_u *s1 = tv1->vval.v_string;
3548 char_u *s2 = tv2->vval.v_string;
3549 size_t len1 = STRLEN(s1);
3550
3551 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3552 if (tv1->vval.v_string == NULL)
3553 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003554 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003555 return FAIL;
3556 }
3557 mch_memmove(tv1->vval.v_string, s1, len1);
3558 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003559 vim_free(s1);
3560 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003561 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003562 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 }
3564 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003565 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003566 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003567 if (*op == '.')
3568 {
3569 if (may_generate_2STRING(-2, cctx) == FAIL
3570 || may_generate_2STRING(-1, cctx) == FAIL)
3571 return FAIL;
3572 generate_instr_drop(cctx, ISN_CONCAT, 1);
3573 }
3574 else
3575 generate_two_op(cctx, op);
3576 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 }
3578
3579 return OK;
3580}
3581
3582/*
3583 * expr5a == expr5b
3584 * expr5a =~ expr5b
3585 * expr5a != expr5b
3586 * expr5a !~ expr5b
3587 * expr5a > expr5b
3588 * expr5a >= expr5b
3589 * expr5a < expr5b
3590 * expr5a <= expr5b
3591 * expr5a is expr5b
3592 * expr5a isnot expr5b
3593 *
3594 * Produces instructions:
3595 * EVAL expr5a Push result of "expr5a"
3596 * EVAL expr5b Push result of "expr5b"
3597 * COMPARE one of the compare instructions
3598 */
3599 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003600compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601{
3602 exptype_T type = EXPR_UNKNOWN;
3603 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003604 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003607 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608
3609 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003610 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 return FAIL;
3612
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003613 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003614 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615
3616 /*
3617 * If there is a comparative operator, use it.
3618 */
3619 if (type != EXPR_UNKNOWN)
3620 {
3621 int ic = FALSE; // Default: do not ignore case
3622
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003623 if (next != NULL)
3624 {
3625 *arg = next_line_from_context(cctx, TRUE);
3626 p = skipwhite(*arg);
3627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 if (type_is && (p[len] == '?' || p[len] == '#'))
3629 {
3630 semsg(_(e_invexpr2), *arg);
3631 return FAIL;
3632 }
3633 // extra question mark appended: ignore case
3634 if (p[len] == '?')
3635 {
3636 ic = TRUE;
3637 ++len;
3638 }
3639 // extra '#' appended: match case (ignored)
3640 else if (p[len] == '#')
3641 ++len;
3642 // nothing appended: match case
3643
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003644 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003646 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003647 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 }
3649
3650 // get the second variable
3651 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003652 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003653 return FAIL;
3654
Bram Moolenaara5565e42020-05-09 15:44:01 +02003655 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 return FAIL;
3657
Bram Moolenaara5565e42020-05-09 15:44:01 +02003658 if (ppconst->pp_used == ppconst_used + 2)
3659 {
3660 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3661 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3662 int ret;
3663
3664 // Both sides are a constant, compute the result now.
3665 // First check for a valid combination of types, this is more
3666 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003667 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003668 ret = FAIL;
3669 else
3670 {
3671 ret = typval_compare(tv1, tv2, type, ic);
3672 tv1->v_type = VAR_BOOL;
3673 tv1->vval.v_number = tv1->vval.v_number
3674 ? VVAL_TRUE : VVAL_FALSE;
3675 clear_tv(tv2);
3676 --ppconst->pp_used;
3677 }
3678 return ret;
3679 }
3680
3681 generate_ppconst(cctx, ppconst);
3682 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 }
3684
3685 return OK;
3686}
3687
Bram Moolenaar7f141552020-05-09 17:35:53 +02003688static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690/*
3691 * Compile || or &&.
3692 */
3693 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003694compile_and_or(
3695 char_u **arg,
3696 cctx_T *cctx,
3697 char *op,
3698 ppconst_T *ppconst,
3699 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003701 char_u *next;
3702 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 int opchar = *op;
3704
3705 if (p[0] == opchar && p[1] == opchar)
3706 {
3707 garray_T *instr = &cctx->ctx_instr;
3708 garray_T end_ga;
3709
3710 /*
3711 * Repeat until there is no following "||" or "&&"
3712 */
3713 ga_init2(&end_ga, sizeof(int), 10);
3714 while (p[0] == opchar && p[1] == opchar)
3715 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003716 if (next != NULL)
3717 {
3718 *arg = next_line_from_context(cctx, TRUE);
3719 p = skipwhite(*arg);
3720 }
3721
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003722 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3723 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003725 return FAIL;
3726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727
Bram Moolenaara5565e42020-05-09 15:44:01 +02003728 // TODO: use ppconst if the value is a constant
3729 generate_ppconst(cctx, ppconst);
3730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 if (ga_grow(&end_ga, 1) == FAIL)
3732 {
3733 ga_clear(&end_ga);
3734 return FAIL;
3735 }
3736 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3737 ++end_ga.ga_len;
3738 generate_JUMP(cctx, opchar == '|'
3739 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3740
3741 // eval the next expression
3742 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003743 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003744 return FAIL;
3745
Bram Moolenaara5565e42020-05-09 15:44:01 +02003746 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3747 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 {
3749 ga_clear(&end_ga);
3750 return FAIL;
3751 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003752
3753 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003755 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756
3757 // Fill in the end label in all jumps.
3758 while (end_ga.ga_len > 0)
3759 {
3760 isn_T *isn;
3761
3762 --end_ga.ga_len;
3763 isn = ((isn_T *)instr->ga_data)
3764 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3765 isn->isn_arg.jump.jump_where = instr->ga_len;
3766 }
3767 ga_clear(&end_ga);
3768 }
3769
3770 return OK;
3771}
3772
3773/*
3774 * expr4a && expr4a && expr4a logical AND
3775 *
3776 * Produces instructions:
3777 * EVAL expr4a Push result of "expr4a"
3778 * JUMP_AND_KEEP_IF_FALSE end
3779 * EVAL expr4b Push result of "expr4b"
3780 * JUMP_AND_KEEP_IF_FALSE end
3781 * EVAL expr4c Push result of "expr4c"
3782 * end:
3783 */
3784 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003785compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003787 int ppconst_used = ppconst->pp_used;
3788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003790 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 return FAIL;
3792
3793 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003794 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795}
3796
3797/*
3798 * expr3a || expr3b || expr3c logical OR
3799 *
3800 * Produces instructions:
3801 * EVAL expr3a Push result of "expr3a"
3802 * JUMP_AND_KEEP_IF_TRUE end
3803 * EVAL expr3b Push result of "expr3b"
3804 * JUMP_AND_KEEP_IF_TRUE end
3805 * EVAL expr3c Push result of "expr3c"
3806 * end:
3807 */
3808 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003809compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003811 int ppconst_used = ppconst->pp_used;
3812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003814 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 return FAIL;
3816
3817 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003818 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819}
3820
3821/*
3822 * Toplevel expression: expr2 ? expr1a : expr1b
3823 *
3824 * Produces instructions:
3825 * EVAL expr2 Push result of "expr"
3826 * JUMP_IF_FALSE alt jump if false
3827 * EVAL expr1a
3828 * JUMP_ALWAYS end
3829 * alt: EVAL expr1b
3830 * end:
3831 */
3832 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003833compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834{
3835 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003836 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003837 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838
Bram Moolenaar61a89812020-05-07 16:58:17 +02003839 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02003840 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 return FAIL;
3842
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003843 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 if (*p == '?')
3845 {
3846 garray_T *instr = &cctx->ctx_instr;
3847 garray_T *stack = &cctx->ctx_type_stack;
3848 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003849 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003851 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003853 int has_const_expr = FALSE;
3854 int const_value = FALSE;
3855 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003857 if (next != NULL)
3858 {
3859 *arg = next_line_from_context(cctx, TRUE);
3860 p = skipwhite(*arg);
3861 }
3862
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003863 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3864 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003866 return FAIL;
3867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868
Bram Moolenaara5565e42020-05-09 15:44:01 +02003869 if (ppconst->pp_used == ppconst_used + 1)
3870 {
3871 // the condition is a constant, we know whether the ? or the :
3872 // expression is to be evaluated.
3873 has_const_expr = TRUE;
3874 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3875 clear_tv(&ppconst->pp_tv[ppconst_used]);
3876 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003877 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
3878 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003879 }
3880 else
3881 {
3882 generate_ppconst(cctx, ppconst);
3883 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885
3886 // evaluate the second expression; any type is accepted
3887 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003888 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003889 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003890 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01003891 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892
Bram Moolenaara5565e42020-05-09 15:44:01 +02003893 if (!has_const_expr)
3894 {
3895 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896
Bram Moolenaara5565e42020-05-09 15:44:01 +02003897 // remember the type and drop it
3898 --stack->ga_len;
3899 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900
Bram Moolenaara5565e42020-05-09 15:44:01 +02003901 end_idx = instr->ga_len;
3902 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3903
3904 // jump here from JUMP_IF_FALSE
3905 isn = ((isn_T *)instr->ga_data) + alt_idx;
3906 isn->isn_arg.jump.jump_where = instr->ga_len;
3907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908
3909 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003910 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 if (*p != ':')
3912 {
3913 emsg(_(e_missing_colon));
3914 return FAIL;
3915 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003916 if (next != NULL)
3917 {
3918 *arg = next_line_from_context(cctx, TRUE);
3919 p = skipwhite(*arg);
3920 }
3921
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003922 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3923 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003925 return FAIL;
3926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927
3928 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003929 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003930 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3931 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003933 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003934 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003935 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01003936 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937
Bram Moolenaara5565e42020-05-09 15:44:01 +02003938 if (!has_const_expr)
3939 {
3940 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941
Bram Moolenaara5565e42020-05-09 15:44:01 +02003942 // If the types differ, the result has a more generic type.
3943 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3944 common_type(type1, type2, &type2, cctx->ctx_type_list);
3945
3946 // jump here from JUMP_ALWAYS
3947 isn = ((isn_T *)instr->ga_data) + end_idx;
3948 isn->isn_arg.jump.jump_where = instr->ga_len;
3949 }
3950
3951 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 }
3953 return OK;
3954}
3955
3956/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003957 * Toplevel expression.
3958 */
3959 static int
3960compile_expr0(char_u **arg, cctx_T *cctx)
3961{
3962 ppconst_T ppconst;
3963
3964 CLEAR_FIELD(ppconst);
3965 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3966 {
3967 clear_ppconst(&ppconst);
3968 return FAIL;
3969 }
3970 if (generate_ppconst(cctx, &ppconst) == FAIL)
3971 return FAIL;
3972 return OK;
3973}
3974
3975/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 * compile "return [expr]"
3977 */
3978 static char_u *
3979compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3980{
3981 char_u *p = arg;
3982 garray_T *stack = &cctx->ctx_type_stack;
3983 type_T *stack_type;
3984
3985 if (*p != NUL && *p != '|' && *p != '\n')
3986 {
3987 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02003988 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 return NULL;
3990
3991 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3992 if (set_return_type)
3993 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02003994 else
3995 {
3996 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
3997 && stack_type->tt_type != VAR_VOID
3998 && stack_type->tt_type != VAR_UNKNOWN)
3999 {
4000 emsg(_("E1096: Returning a value in a function without a return type"));
4001 return NULL;
4002 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004003 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4004 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004006 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 }
4008 else
4009 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004010 // "set_return_type" cannot be TRUE, only used for a lambda which
4011 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004012 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4013 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 {
4015 emsg(_("E1003: Missing return value"));
4016 return NULL;
4017 }
4018
4019 // No argument, return zero.
4020 generate_PUSHNR(cctx, 0);
4021 }
4022
4023 if (generate_instr(cctx, ISN_RETURN) == NULL)
4024 return NULL;
4025
4026 // "return val | endif" is possible
4027 return skipwhite(p);
4028}
4029
4030/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004031 * Get a line from the compilation context, compatible with exarg_T getline().
4032 * Return a pointer to the line in allocated memory.
4033 * Return NULL for end-of-file or some error.
4034 */
4035 static char_u *
4036exarg_getline(
4037 int c UNUSED,
4038 void *cookie,
4039 int indent UNUSED,
4040 int do_concat UNUSED)
4041{
4042 cctx_T *cctx = (cctx_T *)cookie;
4043
4044 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4045 {
4046 iemsg("Heredoc got to end");
4047 return NULL;
4048 }
4049 ++cctx->ctx_lnum;
4050 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4051 [cctx->ctx_lnum]);
4052}
4053
4054/*
4055 * Compile a nested :def command.
4056 */
4057 static char_u *
4058compile_nested_function(exarg_T *eap, cctx_T *cctx)
4059{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004060 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004061 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004062 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004063 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004064 lvar_T *lvar;
4065 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004066 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004067
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004068 // Only g:Func() can use a namespace.
4069 if (name_start[1] == ':' && !is_global)
4070 {
4071 semsg(_(e_namespace), name_start);
4072 return NULL;
4073 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004074 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4075 return NULL;
4076
Bram Moolenaar04b12692020-05-04 23:24:44 +02004077 eap->arg = name_end;
4078 eap->getline = exarg_getline;
4079 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004080 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004081 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004082 lambda_name = get_lambda_name();
4083 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004084
Bram Moolenaar822ba242020-05-24 23:00:18 +02004085 if (ufunc == NULL)
4086 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004087 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004088 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004089 return NULL;
4090
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004091 if (is_global)
4092 {
4093 char_u *func_name = vim_strnsave(name_start + 2,
4094 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004095
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004096 if (func_name == NULL)
4097 r = FAIL;
4098 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004099 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004100 }
4101 else
4102 {
4103 // Define a local variable for the function reference.
4104 lvar = reserve_local(cctx, name_start, name_end - name_start,
4105 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004106 if (lvar == NULL)
4107 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004108 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004109 return NULL;
4110 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4111 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004112
Bram Moolenaar61a89812020-05-07 16:58:17 +02004113 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004114 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004115}
4116
4117/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 * Return the length of an assignment operator, or zero if there isn't one.
4119 */
4120 int
4121assignment_len(char_u *p, int *heredoc)
4122{
4123 if (*p == '=')
4124 {
4125 if (p[1] == '<' && p[2] == '<')
4126 {
4127 *heredoc = TRUE;
4128 return 3;
4129 }
4130 return 1;
4131 }
4132 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4133 return 2;
4134 if (STRNCMP(p, "..=", 3) == 0)
4135 return 3;
4136 return 0;
4137}
4138
4139// words that cannot be used as a variable
4140static char *reserved[] = {
4141 "true",
4142 "false",
4143 NULL
4144};
4145
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004146typedef enum {
4147 dest_local,
4148 dest_option,
4149 dest_env,
4150 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004151 dest_buffer,
4152 dest_window,
4153 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004154 dest_vimvar,
4155 dest_script,
4156 dest_reg,
4157} assign_dest_T;
4158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004160 * Generate the load instruction for "name".
4161 */
4162 static void
4163generate_loadvar(
4164 cctx_T *cctx,
4165 assign_dest_T dest,
4166 char_u *name,
4167 lvar_T *lvar,
4168 type_T *type)
4169{
4170 switch (dest)
4171 {
4172 case dest_option:
4173 // TODO: check the option exists
4174 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4175 break;
4176 case dest_global:
4177 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4178 break;
4179 case dest_buffer:
4180 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4181 break;
4182 case dest_window:
4183 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4184 break;
4185 case dest_tab:
4186 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4187 break;
4188 case dest_script:
4189 compile_load_scriptvar(cctx,
4190 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4191 break;
4192 case dest_env:
4193 // Include $ in the name here
4194 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4195 break;
4196 case dest_reg:
4197 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4198 break;
4199 case dest_vimvar:
4200 generate_LOADV(cctx, name + 2, TRUE);
4201 break;
4202 case dest_local:
4203 if (lvar->lv_from_outer)
4204 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4205 NULL, type);
4206 else
4207 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4208 break;
4209 }
4210}
4211
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004212 void
4213vim9_declare_error(char_u *name)
4214{
4215 char *scope = "";
4216
4217 switch (*name)
4218 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004219 case 'g': scope = _("global"); break;
4220 case 'b': scope = _("buffer"); break;
4221 case 'w': scope = _("window"); break;
4222 case 't': scope = _("tab"); break;
4223 case 'v': scope = "v:"; break;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004224 case '$': semsg(_(e_declare_env_var), name);
4225 return;
4226 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
4227 return;
4228 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
4229 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004230 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004231 }
4232 semsg(_(e_declare_var), scope, name);
4233}
4234
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004235/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004236 * Compile declaration and assignment:
4237 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004239 * Return NULL for an error.
4240 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 */
4242 static char_u *
4243compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4244{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004245 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004247 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 char_u *ret = NULL;
4249 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004250 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004251 int scriptvar_sid = 0;
4252 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004255 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 int oplen = 0;
4258 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004259 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004260 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004261 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004265 // Skip over the "var" or "[var, var]" to get to any "=".
4266 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4267 if (p == NULL)
4268 return *arg == '[' ? arg : NULL;
4269
4270 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004272 // TODO: should we allow this, and figure out type inference from list
4273 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004274 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 return NULL;
4276 }
4277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 sp = p;
4279 p = skipwhite(p);
4280 op = p;
4281 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004282
4283 if (var_count > 0 && oplen == 0)
4284 // can be something like "[1, 2]->func()"
4285 return arg;
4286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4288 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004289 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004290 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004291 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 if (heredoc)
4294 {
4295 list_T *l;
4296 listitem_T *li;
4297
4298 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004299 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004301 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302
4303 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004304 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 {
4306 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4307 li->li_tv.vval.v_string = NULL;
4308 }
4309 generate_NEWLIST(cctx, l->lv_len);
4310 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004311 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 list_free(l);
4313 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004314 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004316 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004318 // for "[var, var] = expr" evaluate the expression here, loop over the
4319 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004320
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004321 p = skipwhite(op + oplen);
4322 if (compile_expr0(&p, cctx) == FAIL)
4323 return NULL;
4324 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004326 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004328 type_T *stacktype;
4329
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004330 stacktype = stack->ga_len == 0 ? &t_void
4331 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004332 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004334 emsg(_(e_cannot_use_void));
4335 goto theend;
4336 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004337 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004338 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004339 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004340 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4341 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004342 }
4343 }
4344
4345 /*
4346 * Loop over variables in "[var, var] = expr".
4347 * For "var = expr" and "let var: type" this is done only once.
4348 */
4349 if (var_count > 0)
4350 var_start = skipwhite(arg + 1); // skip over the "["
4351 else
4352 var_start = arg;
4353 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4354 {
4355 char_u *var_end = skip_var_one(var_start, FALSE);
4356 size_t varlen;
4357 int new_local = FALSE;
4358 int opt_type;
4359 int opt_flags = 0;
4360 assign_dest_T dest = dest_local;
4361 int vimvaridx = -1;
4362 lvar_T *lvar = NULL;
4363 lvar_T arg_lvar;
4364 int has_type = FALSE;
4365 int has_index = FALSE;
4366 int instr_count = -1;
4367
Bram Moolenaar65821722020-08-02 18:58:54 +02004368 if (*var_start == '@')
4369 p = var_start + 2;
4370 else
4371 {
4372 p = (*var_start == '&' || *var_start == '$')
4373 ? var_start + 1 : var_start;
4374 p = to_name_end(p, TRUE);
4375 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004376
4377 // "a: type" is declaring variable "a" with a type, not "a:".
4378 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4379 --var_end;
4380 if (is_decl && p == var_start + 2 && p[-1] == ':')
4381 --p;
4382
4383 varlen = p - var_start;
4384 vim_free(name);
4385 name = vim_strnsave(var_start, varlen);
4386 if (name == NULL)
4387 return NULL;
4388 if (!heredoc)
4389 type = &t_any;
4390
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004391 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004392 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004393 int declare_error = FALSE;
4394
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004395 if (*var_start == '&')
4396 {
4397 int cc;
4398 long numval;
4399
4400 dest = dest_option;
4401 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004403 emsg(_(e_const_option));
4404 goto theend;
4405 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004406 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004407 p = var_start;
4408 p = find_option_end(&p, &opt_flags);
4409 if (p == NULL)
4410 {
4411 // cannot happen?
4412 emsg(_(e_letunexp));
4413 goto theend;
4414 }
4415 cc = *p;
4416 *p = NUL;
4417 opt_type = get_option_value(var_start + 1, &numval,
4418 NULL, opt_flags);
4419 *p = cc;
4420 if (opt_type == -3)
4421 {
4422 semsg(_(e_unknown_option), var_start);
4423 goto theend;
4424 }
4425 if (opt_type == -2 || opt_type == 0)
4426 type = &t_string;
4427 else
4428 type = &t_number; // both number and boolean option
4429 }
4430 else if (*var_start == '$')
4431 {
4432 dest = dest_env;
4433 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004434 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004435 }
4436 else if (*var_start == '@')
4437 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004438 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004439 {
4440 emsg_invreg(var_start[1]);
4441 goto theend;
4442 }
4443 dest = dest_reg;
4444 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004445 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004446 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004447 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004448 {
4449 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004450 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004451 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004452 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004453 {
4454 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004455 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004456 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004457 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004458 {
4459 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004460 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004461 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004462 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004463 {
4464 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004465 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004466 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004467 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004468 {
4469 typval_T *vtv;
4470 int di_flags;
4471
4472 vimvaridx = find_vim_var(name + 2, &di_flags);
4473 if (vimvaridx < 0)
4474 {
4475 semsg(_(e_var_notfound), var_start);
4476 goto theend;
4477 }
4478 // We use the current value of "sandbox" here, is that OK?
4479 if (var_check_ro(di_flags, name, FALSE))
4480 goto theend;
4481 dest = dest_vimvar;
4482 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004483 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004484 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004485 }
4486 else
4487 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004488 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004489
4490 for (idx = 0; reserved[idx] != NULL; ++idx)
4491 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004492 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004493 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004494 goto theend;
4495 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004496
4497 lvar = lookup_local(var_start, varlen, cctx);
4498 if (lvar == NULL)
4499 {
4500 CLEAR_FIELD(arg_lvar);
4501 if (lookup_arg(var_start, varlen,
4502 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4503 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004504 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004505 if (is_decl)
4506 {
4507 semsg(_(e_used_as_arg), name);
4508 goto theend;
4509 }
4510 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004511 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004512 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004513 if (lvar != NULL)
4514 {
4515 if (is_decl)
4516 {
4517 semsg(_("E1017: Variable already declared: %s"), name);
4518 goto theend;
4519 }
4520 else if (lvar->lv_const)
4521 {
4522 semsg(_("E1018: Cannot assign to a constant: %s"),
4523 name);
4524 goto theend;
4525 }
4526 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004527 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004528 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004529 int script_namespace = varlen > 1
4530 && STRNCMP(var_start, "s:", 2) == 0;
4531 int script_var = (script_namespace
4532 ? lookup_script(var_start + 2, varlen - 2)
4533 : lookup_script(var_start, varlen)) == OK;
4534 imported_T *import =
4535 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004536
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004537 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004538 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004539 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4540
4541 if (is_decl)
4542 {
4543 if (script_namespace)
4544 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004545 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004546 else
4547 semsg(_("E1054: Variable already declared in the script: %s"),
4548 name);
4549 goto theend;
4550 }
4551 else if (cctx->ctx_ufunc->uf_script_ctx_version
4552 == SCRIPT_VERSION_VIM9
4553 && script_namespace
4554 && !script_var && import == NULL)
4555 {
4556 semsg(_(e_unknown_var), name);
4557 goto theend;
4558 }
4559
4560 dest = dest_script;
4561
4562 // existing script-local variables should have a type
4563 scriptvar_sid = current_sctx.sc_sid;
4564 if (import != NULL)
4565 scriptvar_sid = import->imp_sid;
4566 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4567 rawname, TRUE);
4568 if (scriptvar_idx >= 0)
4569 {
4570 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4571 svar_T *sv =
4572 ((svar_T *)si->sn_var_vals.ga_data)
4573 + scriptvar_idx;
4574 type = sv->sv_type;
4575 }
4576 }
4577 else if (name[1] == ':' && name[2] != NUL)
4578 {
4579 semsg(_("E1082: Cannot use a namespaced variable: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004580 name);
4581 goto theend;
4582 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004583 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004584 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004585 semsg(_(e_unknown_var), name);
4586 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004587 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004588 else if (check_defined(var_start, varlen, cctx) == FAIL)
4589 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004590 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004591 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004592
4593 if (declare_error)
4594 {
4595 vim9_declare_error(name);
4596 goto theend;
4597 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004598 }
4599
4600 // handle "a:name" as a name, not index "name" on "a"
4601 if (varlen > 1 || var_start[varlen] != ':')
4602 p = var_end;
4603
4604 if (dest != dest_option)
4605 {
4606 if (is_decl && *p == ':')
4607 {
4608 // parse optional type: "let var: type = expr"
4609 if (!VIM_ISWHITE(p[1]))
4610 {
4611 semsg(_(e_white_after), ":");
4612 goto theend;
4613 }
4614 p = skipwhite(p + 1);
4615 type = parse_type(&p, cctx->ctx_type_list);
4616 has_type = TRUE;
4617 }
4618 else if (lvar != NULL)
4619 type = lvar->lv_type;
4620 }
4621
4622 if (oplen == 3 && !heredoc && dest != dest_global
4623 && type->tt_type != VAR_STRING
4624 && type->tt_type != VAR_ANY)
4625 {
4626 emsg(_("E1019: Can only concatenate to string"));
4627 goto theend;
4628 }
4629
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004630 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004631 {
4632 if (oplen > 1 && !heredoc)
4633 {
4634 // +=, /=, etc. require an existing variable
4635 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4636 name);
4637 goto theend;
4638 }
4639
4640 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004641 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4642 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004643 goto theend;
4644 lvar = reserve_local(cctx, var_start, varlen,
4645 cmdidx == CMD_const, type);
4646 if (lvar == NULL)
4647 goto theend;
4648 new_local = TRUE;
4649 }
4650
4651 member_type = type;
4652 if (var_end > var_start + varlen)
4653 {
4654 // Something follows after the variable: "var[idx]".
4655 if (is_decl)
4656 {
4657 emsg(_("E1087: cannot use an index when declaring a variable"));
4658 goto theend;
4659 }
4660
4661 if (var_start[varlen] == '[')
4662 {
4663 has_index = TRUE;
4664 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004665 member_type = &t_any;
4666 else
4667 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004668 }
4669 else
4670 {
4671 semsg("Not supported yet: %s", var_start);
4672 goto theend;
4673 }
4674 }
4675 else if (lvar == &arg_lvar)
4676 {
4677 semsg(_("E1090: Cannot assign to argument %s"), name);
4678 goto theend;
4679 }
4680
4681 if (!heredoc)
4682 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004683 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004684 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004685 if (oplen > 0 && var_count == 0)
4686 {
4687 // skip over the "=" and the expression
4688 p = skipwhite(op + oplen);
4689 compile_expr0(&p, cctx);
4690 }
4691 }
4692 else if (oplen > 0)
4693 {
4694 type_T *stacktype;
4695
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004696 // For "var = expr" evaluate the expression.
4697 if (var_count == 0)
4698 {
4699 int r;
4700
4701 // for "+=", "*=", "..=" etc. first load the current value
4702 if (*op != '=')
4703 {
4704 generate_loadvar(cctx, dest, name, lvar, type);
4705
4706 if (has_index)
4707 {
4708 // TODO: get member from list or dict
4709 emsg("Index with operation not supported yet");
4710 goto theend;
4711 }
4712 }
4713
4714 // Compile the expression. Temporarily hide the new local
4715 // variable here, it is not available to this expression.
4716 if (new_local)
4717 --cctx->ctx_locals.ga_len;
4718 instr_count = instr->ga_len;
4719 p = skipwhite(op + oplen);
4720 r = compile_expr0(&p, cctx);
4721 if (new_local)
4722 ++cctx->ctx_locals.ga_len;
4723 if (r == FAIL)
4724 goto theend;
4725 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004726 else if (semicolon && var_idx == var_count - 1)
4727 {
4728 // For "[var; var] = expr" get the rest of the list
4729 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4730 goto theend;
4731 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004732 else
4733 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004734 // For "[var, var] = expr" get the "var_idx" item from the
4735 // list.
4736 if (generate_GETITEM(cctx, var_idx) == FAIL)
4737 return FAIL;
4738 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004739
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004740 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004741 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004742 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004743 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004744 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004745 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004746 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004747 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004748 emsg(_(e_cannot_use_void));
4749 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004750 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004751 else if ((stacktype->tt_type == VAR_FUNC
4752 || stacktype->tt_type == VAR_PARTIAL)
4753 && var_wrong_func_name(name, TRUE))
4754 {
4755 goto theend;
4756 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004757 else
4758 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004759 // An empty list or dict has a &t_void member,
4760 // for a variable that implies &t_any.
4761 if (stacktype == &t_list_empty)
4762 lvar->lv_type = &t_list_any;
4763 else if (stacktype == &t_dict_empty)
4764 lvar->lv_type = &t_dict_any;
4765 else
4766 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004767 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004768 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004769 else
4770 {
4771 type_T *use_type = lvar->lv_type;
4772
4773 if (has_index)
4774 {
4775 use_type = use_type->tt_member;
4776 if (use_type == NULL)
4777 use_type = &t_void;
4778 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004779 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004780 == FAIL)
4781 goto theend;
4782 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004783 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004784 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004785 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004786 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004788 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004790 emsg(_(e_const_req_value));
4791 goto theend;
4792 }
4793 else if (!has_type || dest == dest_option)
4794 {
4795 emsg(_(e_type_req));
4796 goto theend;
4797 }
4798 else
4799 {
4800 // variables are always initialized
4801 if (ga_grow(instr, 1) == FAIL)
4802 goto theend;
4803 switch (member_type->tt_type)
4804 {
4805 case VAR_BOOL:
4806 generate_PUSHBOOL(cctx, VVAL_FALSE);
4807 break;
4808 case VAR_FLOAT:
4809#ifdef FEAT_FLOAT
4810 generate_PUSHF(cctx, 0.0);
4811#endif
4812 break;
4813 case VAR_STRING:
4814 generate_PUSHS(cctx, NULL);
4815 break;
4816 case VAR_BLOB:
4817 generate_PUSHBLOB(cctx, NULL);
4818 break;
4819 case VAR_FUNC:
4820 generate_PUSHFUNC(cctx, NULL, &t_func_void);
4821 break;
4822 case VAR_LIST:
4823 generate_NEWLIST(cctx, 0);
4824 break;
4825 case VAR_DICT:
4826 generate_NEWDICT(cctx, 0);
4827 break;
4828 case VAR_JOB:
4829 generate_PUSHJOB(cctx, NULL);
4830 break;
4831 case VAR_CHANNEL:
4832 generate_PUSHCHANNEL(cctx, NULL);
4833 break;
4834 case VAR_NUMBER:
4835 case VAR_UNKNOWN:
4836 case VAR_ANY:
4837 case VAR_PARTIAL:
4838 case VAR_VOID:
4839 case VAR_SPECIAL: // cannot happen
4840 generate_PUSHNR(cctx, 0);
4841 break;
4842 }
4843 }
4844 if (var_count == 0)
4845 end = p;
4846 }
4847
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004848 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004849 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004850 break;
4851
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004852 if (oplen > 0 && *op != '=')
4853 {
4854 type_T *expected = &t_number;
4855 type_T *stacktype;
4856
4857 // TODO: if type is known use float or any operation
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004858 // TODO: check operator matches variable type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004859
4860 if (*op == '.')
4861 expected = &t_string;
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004862 else if (*op == '+')
4863 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004864 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004865 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004866 goto theend;
4867
4868 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004869 {
4870 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
4871 goto theend;
4872 }
4873 else if (*op == '+')
4874 {
4875 if (generate_add_instr(cctx,
4876 operator_type(member_type, stacktype),
4877 member_type, stacktype) == FAIL)
4878 goto theend;
4879 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004880 else
4881 {
4882 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4883
4884 if (isn == NULL)
4885 goto theend;
4886 switch (*op)
4887 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004888 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4889 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4890 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4891 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 }
4894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004896 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004897 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004898 int r;
4899
4900 // Compile the "idx" in "var[idx]".
4901 if (new_local)
4902 --cctx->ctx_locals.ga_len;
4903 p = skipwhite(var_start + varlen + 1);
4904 r = compile_expr0(&p, cctx);
4905 if (new_local)
4906 ++cctx->ctx_locals.ga_len;
4907 if (r == FAIL)
4908 goto theend;
4909 if (*skipwhite(p) != ']')
4910 {
4911 emsg(_(e_missbrac));
4912 goto theend;
4913 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004914 if (type == &t_any)
4915 {
4916 type_T *idx_type = ((type_T **)stack->ga_data)[
4917 stack->ga_len - 1];
4918 // Index on variable of unknown type: guess the type from the
4919 // index type: number is dict, otherwise dict.
4920 // TODO: should do the assignment at runtime
4921 if (idx_type->tt_type == VAR_NUMBER)
4922 type = &t_list_any;
4923 else
4924 type = &t_dict_any;
4925 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004926 if (type->tt_type == VAR_DICT
4927 && may_generate_2STRING(-1, cctx) == FAIL)
4928 goto theend;
4929 if (type->tt_type == VAR_LIST
4930 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004931 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004932 {
4933 emsg(_(e_number_exp));
4934 goto theend;
4935 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004936
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004937 // Load the dict or list. On the stack we then have:
4938 // - value
4939 // - index
4940 // - variable
4941 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004942
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004943 if (type->tt_type == VAR_LIST)
4944 {
4945 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
4946 return FAIL;
4947 }
4948 else if (type->tt_type == VAR_DICT)
4949 {
4950 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
4951 return FAIL;
4952 }
4953 else
4954 {
4955 emsg(_(e_listreq));
4956 goto theend;
4957 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004958 }
4959 else
4960 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004961 switch (dest)
4962 {
4963 case dest_option:
4964 generate_STOREOPT(cctx, name + 1, opt_flags);
4965 break;
4966 case dest_global:
4967 // include g: with the name, easier to execute that way
4968 generate_STORE(cctx, ISN_STOREG, 0, name);
4969 break;
4970 case dest_buffer:
4971 // include b: with the name, easier to execute that way
4972 generate_STORE(cctx, ISN_STOREB, 0, name);
4973 break;
4974 case dest_window:
4975 // include w: with the name, easier to execute that way
4976 generate_STORE(cctx, ISN_STOREW, 0, name);
4977 break;
4978 case dest_tab:
4979 // include t: with the name, easier to execute that way
4980 generate_STORE(cctx, ISN_STORET, 0, name);
4981 break;
4982 case dest_env:
4983 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4984 break;
4985 case dest_reg:
4986 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4987 break;
4988 case dest_vimvar:
4989 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4990 break;
4991 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004992 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004993 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004994 {
4995 char_u *name_s = name;
4996
4997 // Include s: in the name for store_var()
4998 if (name[1] != ':')
4999 {
5000 int len = (int)STRLEN(name) + 3;
5001
5002 name_s = alloc(len);
5003 if (name_s == NULL)
5004 name_s = name;
5005 else
5006 vim_snprintf((char *)name_s, len,
5007 "s:%s", name);
5008 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005009 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5010 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005011 if (name_s != name)
5012 vim_free(name_s);
5013 }
5014 else
5015 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005016 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005017 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018 break;
5019 case dest_local:
5020 if (lvar != NULL)
5021 {
5022 isn_T *isn = ((isn_T *)instr->ga_data)
5023 + instr->ga_len - 1;
5024
5025 // optimization: turn "var = 123" from ISN_PUSHNR +
5026 // ISN_STORE into ISN_STORENR
5027 if (!lvar->lv_from_outer
5028 && instr->ga_len == instr_count + 1
5029 && isn->isn_type == ISN_PUSHNR)
5030 {
5031 varnumber_T val = isn->isn_arg.number;
5032
5033 isn->isn_type = ISN_STORENR;
5034 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5035 isn->isn_arg.storenr.stnr_val = val;
5036 if (stack->ga_len > 0)
5037 --stack->ga_len;
5038 }
5039 else if (lvar->lv_from_outer)
5040 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005041 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005042 else
5043 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5044 }
5045 break;
5046 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005047 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005048
5049 if (var_idx + 1 < var_count)
5050 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005052
5053 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005054 if (var_count > 0 && !semicolon)
5055 {
5056 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5057 goto theend;
5058 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059
Bram Moolenaarb2097502020-07-19 17:17:02 +02005060 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061
5062theend:
5063 vim_free(name);
5064 return ret;
5065}
5066
5067/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005068 * Check if "name" can be "unlet".
5069 */
5070 int
5071check_vim9_unlet(char_u *name)
5072{
5073 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5074 {
5075 semsg(_("E1081: Cannot unlet %s"), name);
5076 return FAIL;
5077 }
5078 return OK;
5079}
5080
5081/*
5082 * Callback passed to ex_unletlock().
5083 */
5084 static int
5085compile_unlet(
5086 lval_T *lvp,
5087 char_u *name_end,
5088 exarg_T *eap,
5089 int deep UNUSED,
5090 void *coookie)
5091{
5092 cctx_T *cctx = coookie;
5093
5094 if (lvp->ll_tv == NULL)
5095 {
5096 char_u *p = lvp->ll_name;
5097 int cc = *name_end;
5098 int ret = OK;
5099
5100 // Normal name. Only supports g:, w:, t: and b: namespaces.
5101 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005102 if (*p == '$')
5103 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5104 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005105 ret = FAIL;
5106 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005107 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005108
5109 *name_end = cc;
5110 return ret;
5111 }
5112
5113 // TODO: unlet {list}[idx]
5114 // TODO: unlet {dict}[key]
5115 emsg("Sorry, :unlet not fully implemented yet");
5116 return FAIL;
5117}
5118
5119/*
5120 * compile "unlet var", "lock var" and "unlock var"
5121 * "arg" points to "var".
5122 */
5123 static char_u *
5124compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5125{
5126 char_u *p = arg;
5127
5128 if (eap->cmdidx != CMD_unlet)
5129 {
5130 emsg("Sorry, :lock and unlock not implemented yet");
5131 return NULL;
5132 }
5133
5134 if (*p == '!')
5135 {
5136 p = skipwhite(p + 1);
5137 eap->forceit = TRUE;
5138 }
5139
5140 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5141 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5142}
5143
5144/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145 * Compile an :import command.
5146 */
5147 static char_u *
5148compile_import(char_u *arg, cctx_T *cctx)
5149{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005150 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151}
5152
5153/*
5154 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5155 */
5156 static int
5157compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5158{
5159 garray_T *instr = &cctx->ctx_instr;
5160 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5161
5162 if (endlabel == NULL)
5163 return FAIL;
5164 endlabel->el_next = *el;
5165 *el = endlabel;
5166 endlabel->el_end_label = instr->ga_len;
5167
5168 generate_JUMP(cctx, when, 0);
5169 return OK;
5170}
5171
5172 static void
5173compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5174{
5175 garray_T *instr = &cctx->ctx_instr;
5176
5177 while (*el != NULL)
5178 {
5179 endlabel_T *cur = (*el);
5180 isn_T *isn;
5181
5182 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5183 isn->isn_arg.jump.jump_where = instr->ga_len;
5184 *el = cur->el_next;
5185 vim_free(cur);
5186 }
5187}
5188
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005189 static void
5190compile_free_jump_to_end(endlabel_T **el)
5191{
5192 while (*el != NULL)
5193 {
5194 endlabel_T *cur = (*el);
5195
5196 *el = cur->el_next;
5197 vim_free(cur);
5198 }
5199}
5200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201/*
5202 * Create a new scope and set up the generic items.
5203 */
5204 static scope_T *
5205new_scope(cctx_T *cctx, scopetype_T type)
5206{
5207 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5208
5209 if (scope == NULL)
5210 return NULL;
5211 scope->se_outer = cctx->ctx_scope;
5212 cctx->ctx_scope = scope;
5213 scope->se_type = type;
5214 scope->se_local_count = cctx->ctx_locals.ga_len;
5215 return scope;
5216}
5217
5218/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005219 * Free the current scope and go back to the outer scope.
5220 */
5221 static void
5222drop_scope(cctx_T *cctx)
5223{
5224 scope_T *scope = cctx->ctx_scope;
5225
5226 if (scope == NULL)
5227 {
5228 iemsg("calling drop_scope() without a scope");
5229 return;
5230 }
5231 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005232 switch (scope->se_type)
5233 {
5234 case IF_SCOPE:
5235 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5236 case FOR_SCOPE:
5237 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5238 case WHILE_SCOPE:
5239 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5240 case TRY_SCOPE:
5241 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5242 case NO_SCOPE:
5243 case BLOCK_SCOPE:
5244 break;
5245 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005246 vim_free(scope);
5247}
5248
5249/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005250 * compile "if expr"
5251 *
5252 * "if expr" Produces instructions:
5253 * EVAL expr Push result of "expr"
5254 * JUMP_IF_FALSE end
5255 * ... body ...
5256 * end:
5257 *
5258 * "if expr | else" Produces instructions:
5259 * EVAL expr Push result of "expr"
5260 * JUMP_IF_FALSE else
5261 * ... body ...
5262 * JUMP_ALWAYS end
5263 * else:
5264 * ... body ...
5265 * end:
5266 *
5267 * "if expr1 | elseif expr2 | else" Produces instructions:
5268 * EVAL expr Push result of "expr"
5269 * JUMP_IF_FALSE elseif
5270 * ... body ...
5271 * JUMP_ALWAYS end
5272 * elseif:
5273 * EVAL expr Push result of "expr"
5274 * JUMP_IF_FALSE else
5275 * ... body ...
5276 * JUMP_ALWAYS end
5277 * else:
5278 * ... body ...
5279 * end:
5280 */
5281 static char_u *
5282compile_if(char_u *arg, cctx_T *cctx)
5283{
5284 char_u *p = arg;
5285 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005286 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005287 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005288 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005289 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005290
Bram Moolenaara5565e42020-05-09 15:44:01 +02005291 CLEAR_FIELD(ppconst);
5292 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005293 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005294 clear_ppconst(&ppconst);
5295 return NULL;
5296 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005297 if (cctx->ctx_skip == SKIP_YES)
5298 clear_ppconst(&ppconst);
5299 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005300 {
5301 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005302 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005303 clear_ppconst(&ppconst);
5304 }
5305 else
5306 {
5307 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005308 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005309 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005310 return NULL;
5311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312
5313 scope = new_scope(cctx, IF_SCOPE);
5314 if (scope == NULL)
5315 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005316 scope->se_skip_save = skip_save;
5317 // "is_had_return" will be reset if any block does not end in :return
5318 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005319
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005320 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005321 {
5322 // "where" is set when ":elseif", "else" or ":endif" is found
5323 scope->se_u.se_if.is_if_label = instr->ga_len;
5324 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5325 }
5326 else
5327 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328
5329 return p;
5330}
5331
5332 static char_u *
5333compile_elseif(char_u *arg, cctx_T *cctx)
5334{
5335 char_u *p = arg;
5336 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005337 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338 isn_T *isn;
5339 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005340 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341
5342 if (scope == NULL || scope->se_type != IF_SCOPE)
5343 {
5344 emsg(_(e_elseif_without_if));
5345 return NULL;
5346 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005347 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005348 if (!cctx->ctx_had_return)
5349 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005351 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005352 {
5353 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005355 return NULL;
5356 // previous "if" or "elseif" jumps here
5357 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5358 isn->isn_arg.jump.jump_where = instr->ga_len;
5359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005361 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005362 CLEAR_FIELD(ppconst);
5363 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005364 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005365 clear_ppconst(&ppconst);
5366 return NULL;
5367 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005368 if (scope->se_skip_save == SKIP_YES)
5369 clear_ppconst(&ppconst);
5370 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005371 {
5372 // The expression results in a constant.
5373 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005374 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005375 clear_ppconst(&ppconst);
5376 scope->se_u.se_if.is_if_label = -1;
5377 }
5378 else
5379 {
5380 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005381 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005382 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005383 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005384
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005385 // "where" is set when ":elseif", "else" or ":endif" is found
5386 scope->se_u.se_if.is_if_label = instr->ga_len;
5387 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389
5390 return p;
5391}
5392
5393 static char_u *
5394compile_else(char_u *arg, cctx_T *cctx)
5395{
5396 char_u *p = arg;
5397 garray_T *instr = &cctx->ctx_instr;
5398 isn_T *isn;
5399 scope_T *scope = cctx->ctx_scope;
5400
5401 if (scope == NULL || scope->se_type != IF_SCOPE)
5402 {
5403 emsg(_(e_else_without_if));
5404 return NULL;
5405 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005406 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005407 if (!cctx->ctx_had_return)
5408 scope->se_u.se_if.is_had_return = FALSE;
5409 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005410
Bram Moolenaarefd88552020-06-18 20:50:10 +02005411 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005412 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005413 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005414 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005415 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005416 if (!cctx->ctx_had_return
5417 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5418 JUMP_ALWAYS, cctx) == FAIL)
5419 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005420 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005421
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005422 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005423 {
5424 if (scope->se_u.se_if.is_if_label >= 0)
5425 {
5426 // previous "if" or "elseif" jumps here
5427 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5428 isn->isn_arg.jump.jump_where = instr->ga_len;
5429 scope->se_u.se_if.is_if_label = -1;
5430 }
5431 }
5432
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005433 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005434 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5435 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005436
5437 return p;
5438}
5439
5440 static char_u *
5441compile_endif(char_u *arg, cctx_T *cctx)
5442{
5443 scope_T *scope = cctx->ctx_scope;
5444 ifscope_T *ifscope;
5445 garray_T *instr = &cctx->ctx_instr;
5446 isn_T *isn;
5447
5448 if (scope == NULL || scope->se_type != IF_SCOPE)
5449 {
5450 emsg(_(e_endif_without_if));
5451 return NULL;
5452 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005453 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005454 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005455 if (!cctx->ctx_had_return)
5456 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005458 if (scope->se_u.se_if.is_if_label >= 0)
5459 {
5460 // previous "if" or "elseif" jumps here
5461 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5462 isn->isn_arg.jump.jump_where = instr->ga_len;
5463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005464 // Fill in the "end" label in jumps at the end of the blocks.
5465 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005466 cctx->ctx_skip = scope->se_skip_save;
5467
5468 // If all the blocks end in :return and there is an :else then the
5469 // had_return flag is set.
5470 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005471
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005472 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005473 return arg;
5474}
5475
5476/*
5477 * compile "for var in expr"
5478 *
5479 * Produces instructions:
5480 * PUSHNR -1
5481 * STORE loop-idx Set index to -1
5482 * EVAL expr Push result of "expr"
5483 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5484 * - if beyond end, jump to "end"
5485 * - otherwise get item from list and push it
5486 * STORE var Store item in "var"
5487 * ... body ...
5488 * JUMP top Jump back to repeat
5489 * end: DROP Drop the result of "expr"
5490 *
5491 */
5492 static char_u *
5493compile_for(char_u *arg, cctx_T *cctx)
5494{
5495 char_u *p;
5496 size_t varlen;
5497 garray_T *instr = &cctx->ctx_instr;
5498 garray_T *stack = &cctx->ctx_type_stack;
5499 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005500 lvar_T *loop_lvar; // loop iteration variable
5501 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502 type_T *vartype;
5503
5504 // TODO: list of variables: "for [key, value] in dict"
5505 // parse "var"
5506 for (p = arg; eval_isnamec1(*p); ++p)
5507 ;
5508 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005509 var_lvar = lookup_local(arg, varlen, cctx);
5510 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005511 {
5512 semsg(_("E1023: variable already defined: %s"), arg);
5513 return NULL;
5514 }
5515
5516 // consume "in"
5517 p = skipwhite(p);
5518 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5519 {
5520 emsg(_(e_missing_in));
5521 return NULL;
5522 }
5523 p = skipwhite(p + 2);
5524
5525
5526 scope = new_scope(cctx, FOR_SCOPE);
5527 if (scope == NULL)
5528 return NULL;
5529
5530 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005531 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5532 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005533 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005534 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005535 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005536 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005538
5539 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005540 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5541 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005542 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005543 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005544 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005545 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005546 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005547
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005548 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005549
5550 // compile "expr", it remains on the stack until "endfor"
5551 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005552 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005553 {
5554 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005555 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005558 // Now that we know the type of "var", check that it is a list, now or at
5559 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005560 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005561 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005562 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005563 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005564 return NULL;
5565 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005566 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005567 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005568
5569 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005570 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005571
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005572 generate_FOR(cctx, loop_lvar->lv_idx);
5573 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005574
5575 return arg;
5576}
5577
5578/*
5579 * compile "endfor"
5580 */
5581 static char_u *
5582compile_endfor(char_u *arg, cctx_T *cctx)
5583{
5584 garray_T *instr = &cctx->ctx_instr;
5585 scope_T *scope = cctx->ctx_scope;
5586 forscope_T *forscope;
5587 isn_T *isn;
5588
5589 if (scope == NULL || scope->se_type != FOR_SCOPE)
5590 {
5591 emsg(_(e_for));
5592 return NULL;
5593 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005594 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005595 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005596 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005597
5598 // At end of ":for" scope jump back to the FOR instruction.
5599 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5600
5601 // Fill in the "end" label in the FOR statement so it can jump here
5602 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5603 isn->isn_arg.forloop.for_end = instr->ga_len;
5604
5605 // Fill in the "end" label any BREAK statements
5606 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5607
5608 // Below the ":for" scope drop the "expr" list from the stack.
5609 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5610 return NULL;
5611
5612 vim_free(scope);
5613
5614 return arg;
5615}
5616
5617/*
5618 * compile "while expr"
5619 *
5620 * Produces instructions:
5621 * top: EVAL expr Push result of "expr"
5622 * JUMP_IF_FALSE end jump if false
5623 * ... body ...
5624 * JUMP top Jump back to repeat
5625 * end:
5626 *
5627 */
5628 static char_u *
5629compile_while(char_u *arg, cctx_T *cctx)
5630{
5631 char_u *p = arg;
5632 garray_T *instr = &cctx->ctx_instr;
5633 scope_T *scope;
5634
5635 scope = new_scope(cctx, WHILE_SCOPE);
5636 if (scope == NULL)
5637 return NULL;
5638
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005639 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640
5641 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005642 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005643 return NULL;
5644
5645 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005646 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647 JUMP_IF_FALSE, cctx) == FAIL)
5648 return FAIL;
5649
5650 return p;
5651}
5652
5653/*
5654 * compile "endwhile"
5655 */
5656 static char_u *
5657compile_endwhile(char_u *arg, cctx_T *cctx)
5658{
5659 scope_T *scope = cctx->ctx_scope;
5660
5661 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5662 {
5663 emsg(_(e_while));
5664 return NULL;
5665 }
5666 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005667 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005668
5669 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005670 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005671
5672 // Fill in the "end" label in the WHILE statement so it can jump here.
5673 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005674 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005675
5676 vim_free(scope);
5677
5678 return arg;
5679}
5680
5681/*
5682 * compile "continue"
5683 */
5684 static char_u *
5685compile_continue(char_u *arg, cctx_T *cctx)
5686{
5687 scope_T *scope = cctx->ctx_scope;
5688
5689 for (;;)
5690 {
5691 if (scope == NULL)
5692 {
5693 emsg(_(e_continue));
5694 return NULL;
5695 }
5696 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5697 break;
5698 scope = scope->se_outer;
5699 }
5700
5701 // Jump back to the FOR or WHILE instruction.
5702 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005703 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5704 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005705 return arg;
5706}
5707
5708/*
5709 * compile "break"
5710 */
5711 static char_u *
5712compile_break(char_u *arg, cctx_T *cctx)
5713{
5714 scope_T *scope = cctx->ctx_scope;
5715 endlabel_T **el;
5716
5717 for (;;)
5718 {
5719 if (scope == NULL)
5720 {
5721 emsg(_(e_break));
5722 return NULL;
5723 }
5724 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5725 break;
5726 scope = scope->se_outer;
5727 }
5728
5729 // Jump to the end of the FOR or WHILE loop.
5730 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005731 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005732 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005733 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005734 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5735 return FAIL;
5736
5737 return arg;
5738}
5739
5740/*
5741 * compile "{" start of block
5742 */
5743 static char_u *
5744compile_block(char_u *arg, cctx_T *cctx)
5745{
5746 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5747 return NULL;
5748 return skipwhite(arg + 1);
5749}
5750
5751/*
5752 * compile end of block: drop one scope
5753 */
5754 static void
5755compile_endblock(cctx_T *cctx)
5756{
5757 scope_T *scope = cctx->ctx_scope;
5758
5759 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005760 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005761 vim_free(scope);
5762}
5763
5764/*
5765 * compile "try"
5766 * Creates a new scope for the try-endtry, pointing to the first catch and
5767 * finally.
5768 * Creates another scope for the "try" block itself.
5769 * TRY instruction sets up exception handling at runtime.
5770 *
5771 * "try"
5772 * TRY -> catch1, -> finally push trystack entry
5773 * ... try block
5774 * "throw {exception}"
5775 * EVAL {exception}
5776 * THROW create exception
5777 * ... try block
5778 * " catch {expr}"
5779 * JUMP -> finally
5780 * catch1: PUSH exeception
5781 * EVAL {expr}
5782 * MATCH
5783 * JUMP nomatch -> catch2
5784 * CATCH remove exception
5785 * ... catch block
5786 * " catch"
5787 * JUMP -> finally
5788 * catch2: CATCH remove exception
5789 * ... catch block
5790 * " finally"
5791 * finally:
5792 * ... finally block
5793 * " endtry"
5794 * ENDTRY pop trystack entry, may rethrow
5795 */
5796 static char_u *
5797compile_try(char_u *arg, cctx_T *cctx)
5798{
5799 garray_T *instr = &cctx->ctx_instr;
5800 scope_T *try_scope;
5801 scope_T *scope;
5802
5803 // scope that holds the jumps that go to catch/finally/endtry
5804 try_scope = new_scope(cctx, TRY_SCOPE);
5805 if (try_scope == NULL)
5806 return NULL;
5807
5808 // "catch" is set when the first ":catch" is found.
5809 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005810 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005811 if (generate_instr(cctx, ISN_TRY) == NULL)
5812 return NULL;
5813
5814 // scope for the try block itself
5815 scope = new_scope(cctx, BLOCK_SCOPE);
5816 if (scope == NULL)
5817 return NULL;
5818
5819 return arg;
5820}
5821
5822/*
5823 * compile "catch {expr}"
5824 */
5825 static char_u *
5826compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5827{
5828 scope_T *scope = cctx->ctx_scope;
5829 garray_T *instr = &cctx->ctx_instr;
5830 char_u *p;
5831 isn_T *isn;
5832
5833 // end block scope from :try or :catch
5834 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5835 compile_endblock(cctx);
5836 scope = cctx->ctx_scope;
5837
5838 // Error if not in a :try scope
5839 if (scope == NULL || scope->se_type != TRY_SCOPE)
5840 {
5841 emsg(_(e_catch));
5842 return NULL;
5843 }
5844
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005845 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005846 {
5847 emsg(_("E1033: catch unreachable after catch-all"));
5848 return NULL;
5849 }
5850
5851 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005852 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005853 JUMP_ALWAYS, cctx) == FAIL)
5854 return NULL;
5855
5856 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005857 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005858 if (isn->isn_arg.try.try_catch == 0)
5859 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005860 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861 {
5862 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005863 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005864 isn->isn_arg.jump.jump_where = instr->ga_len;
5865 }
5866
5867 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005868 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005870 scope->se_u.se_try.ts_caught_all = TRUE;
5871 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005872 }
5873 else
5874 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005875 char_u *end;
5876 char_u *pat;
5877 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005878 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005879 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005881 // Push v:exception, push {expr} and MATCH
5882 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5883
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005884 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005885 if (*end != *p)
5886 {
5887 semsg(_("E1067: Separator mismatch: %s"), p);
5888 vim_free(tofree);
5889 return FAIL;
5890 }
5891 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005892 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005893 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005894 len = (int)(end - tofree);
5895 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005896 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005897 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005898 if (pat == NULL)
5899 return FAIL;
5900 if (generate_PUSHS(cctx, pat) == FAIL)
5901 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005903 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5904 return NULL;
5905
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005906 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005907 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5908 return NULL;
5909 }
5910
5911 if (generate_instr(cctx, ISN_CATCH) == NULL)
5912 return NULL;
5913
5914 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5915 return NULL;
5916 return p;
5917}
5918
5919 static char_u *
5920compile_finally(char_u *arg, cctx_T *cctx)
5921{
5922 scope_T *scope = cctx->ctx_scope;
5923 garray_T *instr = &cctx->ctx_instr;
5924 isn_T *isn;
5925
5926 // end block scope from :try or :catch
5927 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5928 compile_endblock(cctx);
5929 scope = cctx->ctx_scope;
5930
5931 // Error if not in a :try scope
5932 if (scope == NULL || scope->se_type != TRY_SCOPE)
5933 {
5934 emsg(_(e_finally));
5935 return NULL;
5936 }
5937
5938 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005939 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940 if (isn->isn_arg.try.try_finally != 0)
5941 {
5942 emsg(_(e_finally_dup));
5943 return NULL;
5944 }
5945
5946 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005947 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005948
Bram Moolenaar585fea72020-04-02 22:33:21 +02005949 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005950 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005951 {
5952 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005953 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005954 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02005955 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956 }
5957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005958 // TODO: set index in ts_finally_label jumps
5959
5960 return arg;
5961}
5962
5963 static char_u *
5964compile_endtry(char_u *arg, cctx_T *cctx)
5965{
5966 scope_T *scope = cctx->ctx_scope;
5967 garray_T *instr = &cctx->ctx_instr;
5968 isn_T *isn;
5969
5970 // end block scope from :catch or :finally
5971 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5972 compile_endblock(cctx);
5973 scope = cctx->ctx_scope;
5974
5975 // Error if not in a :try scope
5976 if (scope == NULL || scope->se_type != TRY_SCOPE)
5977 {
5978 if (scope == NULL)
5979 emsg(_(e_no_endtry));
5980 else if (scope->se_type == WHILE_SCOPE)
5981 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005982 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005983 emsg(_(e_endfor));
5984 else
5985 emsg(_(e_endif));
5986 return NULL;
5987 }
5988
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005989 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5991 {
5992 emsg(_("E1032: missing :catch or :finally"));
5993 return NULL;
5994 }
5995
5996 // Fill in the "end" label in jumps at the end of the blocks, if not done
5997 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005998 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999
6000 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006001 if (isn->isn_arg.try.try_catch == 0)
6002 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 if (isn->isn_arg.try.try_finally == 0)
6004 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006005
6006 if (scope->se_u.se_try.ts_catch_label != 0)
6007 {
6008 // Last catch without match jumps here
6009 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6010 isn->isn_arg.jump.jump_where = instr->ga_len;
6011 }
6012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006013 compile_endblock(cctx);
6014
6015 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6016 return NULL;
6017 return arg;
6018}
6019
6020/*
6021 * compile "throw {expr}"
6022 */
6023 static char_u *
6024compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6025{
6026 char_u *p = skipwhite(arg);
6027
Bram Moolenaara5565e42020-05-09 15:44:01 +02006028 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029 return NULL;
6030 if (may_generate_2STRING(-1, cctx) == FAIL)
6031 return NULL;
6032 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6033 return NULL;
6034
6035 return p;
6036}
6037
6038/*
6039 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006040 * compile "echomsg expr"
6041 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006042 * compile "execute expr"
6043 */
6044 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006045compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006046{
6047 char_u *p = arg;
6048 int count = 0;
6049
6050 for (;;)
6051 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006052 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006053 return NULL;
6054 ++count;
6055 p = skipwhite(p);
6056 if (ends_excmd(*p))
6057 break;
6058 }
6059
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006060 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6061 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6062 else if (cmdidx == CMD_execute)
6063 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6064 else if (cmdidx == CMD_echomsg)
6065 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6066 else
6067 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006068 return p;
6069}
6070
6071/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006072 * A command that is not compiled, execute with legacy code.
6073 */
6074 static char_u *
6075compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6076{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006077 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006078 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006079 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006080
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006081 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006082 goto theend;
6083
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006084 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006085 {
6086 long argt = excmd_get_argt(eap->cmdidx);
6087 int usefilter = FALSE;
6088
6089 has_expr = argt & (EX_XFILE | EX_EXPAND);
6090
6091 // If the command can be followed by a bar, find the bar and truncate
6092 // it, so that the following command can be compiled.
6093 // The '|' is overwritten with a NUL, it is put back below.
6094 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6095 && *eap->arg == '!')
6096 // :w !filter or :r !filter or :r! filter
6097 usefilter = TRUE;
6098 if ((argt & EX_TRLBAR) && !usefilter)
6099 {
6100 separate_nextcmd(eap);
6101 if (eap->nextcmd != NULL)
6102 nextcmd = eap->nextcmd;
6103 }
6104 }
6105
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006106 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6107 {
6108 // expand filename in "syntax include [@group] filename"
6109 has_expr = TRUE;
6110 eap->arg = skipwhite(eap->arg + 7);
6111 if (*eap->arg == '@')
6112 eap->arg = skiptowhite(eap->arg);
6113 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006114
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006115 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006116 {
6117 int count = 0;
6118 char_u *start = skipwhite(line);
6119
6120 // :cmd xxx`=expr1`yyy`=expr2`zzz
6121 // PUSHS ":cmd xxx"
6122 // eval expr1
6123 // PUSHS "yyy"
6124 // eval expr2
6125 // PUSHS "zzz"
6126 // EXECCONCAT 5
6127 for (;;)
6128 {
6129 if (p > start)
6130 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006131 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006132 ++count;
6133 }
6134 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006135 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006136 return NULL;
6137 may_generate_2STRING(-1, cctx);
6138 ++count;
6139 p = skipwhite(p);
6140 if (*p != '`')
6141 {
6142 emsg(_("E1083: missing backtick"));
6143 return NULL;
6144 }
6145 start = p + 1;
6146
6147 p = (char_u *)strstr((char *)start, "`=");
6148 if (p == NULL)
6149 {
6150 if (*skipwhite(start) != NUL)
6151 {
6152 generate_PUSHS(cctx, vim_strsave(start));
6153 ++count;
6154 }
6155 break;
6156 }
6157 }
6158 generate_EXECCONCAT(cctx, count);
6159 }
6160 else
6161 generate_EXEC(cctx, line);
6162
6163theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006164 if (*nextcmd != NUL)
6165 {
6166 // the parser expects a pointer to the bar, put it back
6167 --nextcmd;
6168 *nextcmd = '|';
6169 }
6170
6171 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006172}
6173
6174/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006175 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006176 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006177 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006178 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006179add_def_function(ufunc_T *ufunc)
6180{
6181 dfunc_T *dfunc;
6182
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006183 if (def_functions.ga_len == 0)
6184 {
6185 // The first position is not used, so that a zero uf_dfunc_idx means it
6186 // wasn't set.
6187 if (ga_grow(&def_functions, 1) == FAIL)
6188 return FAIL;
6189 ++def_functions.ga_len;
6190 }
6191
Bram Moolenaar09689a02020-05-09 22:50:08 +02006192 // Add the function to "def_functions".
6193 if (ga_grow(&def_functions, 1) == FAIL)
6194 return FAIL;
6195 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6196 CLEAR_POINTER(dfunc);
6197 dfunc->df_idx = def_functions.ga_len;
6198 ufunc->uf_dfunc_idx = dfunc->df_idx;
6199 dfunc->df_ufunc = ufunc;
6200 ++def_functions.ga_len;
6201 return OK;
6202}
6203
6204/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006205 * After ex_function() has collected all the function lines: parse and compile
6206 * the lines into instructions.
6207 * Adds the function to "def_functions".
6208 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6209 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006210 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006211 * This can be used recursively through compile_lambda(), which may reallocate
6212 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006213 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006215 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006216compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218 char_u *line = NULL;
6219 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006220 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221 cctx_T cctx;
6222 garray_T *instr;
6223 int called_emsg_before = called_emsg;
6224 int ret = FAIL;
6225 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006226 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006227 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006228 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006230 // When using a function that was compiled before: Free old instructions.
6231 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006232 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006234 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6235 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006236 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006238 else
6239 {
6240 if (add_def_function(ufunc) == FAIL)
6241 return FAIL;
6242 new_def_function = TRUE;
6243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244
Bram Moolenaar985116a2020-07-12 17:31:09 +02006245 ufunc->uf_def_status = UF_COMPILING;
6246
Bram Moolenaara80faa82020-04-12 19:37:17 +02006247 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006248 cctx.ctx_ufunc = ufunc;
6249 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006250 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6252 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6253 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6254 cctx.ctx_type_list = &ufunc->uf_type_list;
6255 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6256 instr = &cctx.ctx_instr;
6257
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006258 // Set the context to the function, it may be compiled when called from
6259 // another script. Set the script version to the most modern one.
6260 // The line number will be set in next_line_from_context().
6261 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6263
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006264 // Make sure error messages are OK.
6265 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6266 if (do_estack_push)
6267 estack_push_ufunc(ufunc, 1);
6268
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006269 if (ufunc->uf_def_args.ga_len > 0)
6270 {
6271 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006272 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006273 int i;
6274 char_u *arg;
6275 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006276 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006277
6278 // Produce instructions for the default values of optional arguments.
6279 // Store the instruction index in uf_def_arg_idx[] so that we know
6280 // where to start when the function is called, depending on the number
6281 // of arguments.
6282 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6283 if (ufunc->uf_def_arg_idx == NULL)
6284 goto erret;
6285 for (i = 0; i < count; ++i)
6286 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006287 garray_T *stack = &cctx.ctx_type_stack;
6288 type_T *val_type;
6289 int arg_idx = first_def_arg + i;
6290
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006291 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6292 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006293 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006294 goto erret;
6295
6296 // If no type specified use the type of the default value.
6297 // Otherwise check that the default value type matches the
6298 // specified type.
6299 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6300 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006301 {
6302 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006303 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006304 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006305 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006306 == FAIL)
6307 {
6308 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6309 arg_idx + 1);
6310 goto erret;
6311 }
6312
6313 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006314 goto erret;
6315 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006316 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006317
6318 if (did_set_arg_type)
6319 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006320 }
6321
6322 /*
6323 * Loop over all the lines of the function and generate instructions.
6324 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 for (;;)
6326 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006327 exarg_T ea;
6328 cmdmod_T save_cmdmod;
6329 int starts_with_colon = FALSE;
6330 char_u *cmd;
6331 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006332
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006333 // Bail out on the first error to avoid a flood of errors and report
6334 // the right line number when inside try/catch.
6335 if (emsg_before != called_emsg)
6336 goto erret;
6337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006338 if (line != NULL && *line == '|')
6339 // the line continues after a '|'
6340 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006341 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006342 && !(*line == '#' && (line == cctx.ctx_line_start
6343 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006345 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346 goto erret;
6347 }
6348 else
6349 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006350 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006351 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006352 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006355 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356
Bram Moolenaara80faa82020-04-12 19:37:17 +02006357 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006358 ea.cmdlinep = &line;
6359 ea.cmd = skipwhite(line);
6360
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006361 // Some things can be recognized by the first character.
6362 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006363 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006364 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006365 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006366 if (ea.cmd[1] != '{')
6367 {
6368 line = (char_u *)"";
6369 continue;
6370 }
6371 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006372
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006373 case '}':
6374 {
6375 // "}" ends a block scope
6376 scopetype_T stype = cctx.ctx_scope == NULL
6377 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006378
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006379 if (stype == BLOCK_SCOPE)
6380 {
6381 compile_endblock(&cctx);
6382 line = ea.cmd;
6383 }
6384 else
6385 {
6386 emsg(_("E1025: using } outside of a block scope"));
6387 goto erret;
6388 }
6389 if (line != NULL)
6390 line = skipwhite(ea.cmd + 1);
6391 continue;
6392 }
6393
6394 case '{':
6395 // "{" starts a block scope
6396 // "{'a': 1}->func() is something else
6397 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6398 {
6399 line = compile_block(ea.cmd, &cctx);
6400 continue;
6401 }
6402 break;
6403
6404 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006405 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006406 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006407 }
6408
6409 /*
6410 * COMMAND MODIFIERS
6411 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006412 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006413 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6414 {
6415 if (errormsg != NULL)
6416 goto erret;
6417 // empty line or comment
6418 line = (char_u *)"";
6419 continue;
6420 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006421 // TODO: use modifiers in the command
6422 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006423 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006424
6425 // Skip ":call" to get to the function name.
6426 if (checkforcmd(&ea.cmd, "call", 3))
6427 ea.cmd = skipwhite(ea.cmd);
6428
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006429 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006430 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006431 char_u *pskip;
6432
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006433 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006434 // find what follows.
6435 // Skip over "var.member", "var[idx]" and the like.
6436 // Also "&opt = val", "$ENV = val" and "@r = val".
6437 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006438 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006439 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006440 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006442 char_u *var_end;
6443 int oplen;
6444 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006445
Bram Moolenaar65821722020-08-02 18:58:54 +02006446 if (ea.cmd[0] == '@')
6447 var_end = ea.cmd + 2;
6448 else
6449 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006450 FNE_CHECK_START | FNE_INCL_BR);
6451 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006452 if (oplen > 0)
6453 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006454 size_t len = p - ea.cmd;
6455
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006456 // Recognize an assignment if we recognize the variable
6457 // name:
6458 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006459 // "local = expr" where "local" is a local var.
6460 // "script = expr" where "script" is a script-local var.
6461 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006462 // "&opt = expr"
6463 // "$ENV = expr"
6464 // "@r = expr"
6465 if (*ea.cmd == '&'
6466 || *ea.cmd == '$'
6467 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006468 || ((len) > 2 && ea.cmd[1] == ':')
6469 || lookup_local(ea.cmd, len, &cctx) != NULL
6470 || lookup_arg(ea.cmd, len, NULL, NULL,
6471 NULL, &cctx) == OK
6472 || lookup_script(ea.cmd, len) == OK
6473 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006474 {
6475 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006476 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006477 goto erret;
6478 continue;
6479 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006480 }
6481 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006482
6483 if (*ea.cmd == '[')
6484 {
6485 // [var, var] = expr
6486 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6487 if (line == NULL)
6488 goto erret;
6489 if (line != ea.cmd)
6490 continue;
6491 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006492 }
6493
6494 /*
6495 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006496 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006498 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006499 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006500 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006501 ea.cmd = skip_range(ea.cmd, NULL);
6502 if (ea.cmd > cmd && !starts_with_colon)
6503 {
6504 emsg(_(e_colon_required));
6505 goto erret;
6506 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006507 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006508 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006509 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006510 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511
6512 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6513 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006514 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006515 {
6516 line += STRLEN(line);
6517 continue;
6518 }
6519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006521 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006523 // CMD_let cannot happen, compile_assignment() above is used
6524 iemsg("Command from find_ex_command() not handled");
6525 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527 }
6528
6529 p = skipwhite(p);
6530
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006531 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006532 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006533 && ea.cmdidx != CMD_elseif
6534 && ea.cmdidx != CMD_else
6535 && ea.cmdidx != CMD_endif)
6536 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006537 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006538 continue;
6539 }
6540
Bram Moolenaarefd88552020-06-18 20:50:10 +02006541 if (ea.cmdidx != CMD_elseif
6542 && ea.cmdidx != CMD_else
6543 && ea.cmdidx != CMD_endif
6544 && ea.cmdidx != CMD_endfor
6545 && ea.cmdidx != CMD_endwhile
6546 && ea.cmdidx != CMD_catch
6547 && ea.cmdidx != CMD_finally
6548 && ea.cmdidx != CMD_endtry)
6549 {
6550 if (cctx.ctx_had_return)
6551 {
6552 emsg(_("E1095: Unreachable code after :return"));
6553 goto erret;
6554 }
6555 }
6556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006557 switch (ea.cmdidx)
6558 {
6559 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006560 ea.arg = p;
6561 line = compile_nested_function(&ea, &cctx);
6562 break;
6563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006565 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 goto erret;
6567
6568 case CMD_return:
6569 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006570 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571 break;
6572
6573 case CMD_let:
6574 case CMD_const:
6575 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006576 if (line == p)
6577 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 break;
6579
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006580 case CMD_unlet:
6581 case CMD_unlockvar:
6582 case CMD_lockvar:
6583 line = compile_unletlock(p, &ea, &cctx);
6584 break;
6585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 case CMD_import:
6587 line = compile_import(p, &cctx);
6588 break;
6589
6590 case CMD_if:
6591 line = compile_if(p, &cctx);
6592 break;
6593 case CMD_elseif:
6594 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006595 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596 break;
6597 case CMD_else:
6598 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006599 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 break;
6601 case CMD_endif:
6602 line = compile_endif(p, &cctx);
6603 break;
6604
6605 case CMD_while:
6606 line = compile_while(p, &cctx);
6607 break;
6608 case CMD_endwhile:
6609 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006610 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 break;
6612
6613 case CMD_for:
6614 line = compile_for(p, &cctx);
6615 break;
6616 case CMD_endfor:
6617 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006618 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619 break;
6620 case CMD_continue:
6621 line = compile_continue(p, &cctx);
6622 break;
6623 case CMD_break:
6624 line = compile_break(p, &cctx);
6625 break;
6626
6627 case CMD_try:
6628 line = compile_try(p, &cctx);
6629 break;
6630 case CMD_catch:
6631 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006632 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633 break;
6634 case CMD_finally:
6635 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006636 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 break;
6638 case CMD_endtry:
6639 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006640 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 break;
6642 case CMD_throw:
6643 line = compile_throw(p, &cctx);
6644 break;
6645
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006646 case CMD_eval:
6647 if (compile_expr0(&p, &cctx) == FAIL)
6648 goto erret;
6649
6650 // drop the return value
6651 generate_instr_drop(&cctx, ISN_DROP, 1);
6652
6653 line = skipwhite(p);
6654 break;
6655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006657 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006658 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006659 case CMD_echomsg:
6660 case CMD_echoerr:
6661 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006662 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006664 // TODO: other commands with an expression argument
6665
Bram Moolenaarae616492020-07-28 20:07:27 +02006666 case CMD_append:
6667 case CMD_change:
6668 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006669 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006670 case CMD_xit:
6671 not_in_vim9(&ea);
6672 goto erret;
6673
Bram Moolenaar002262f2020-07-08 17:47:57 +02006674 case CMD_SIZE:
6675 semsg(_("E476: Invalid command: %s"), ea.cmd);
6676 goto erret;
6677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006679 // Not recognized, execute with do_cmdline_cmd().
6680 ea.arg = p;
6681 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682 break;
6683 }
6684 if (line == NULL)
6685 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006686 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687
6688 if (cctx.ctx_type_stack.ga_len < 0)
6689 {
6690 iemsg("Type stack underflow");
6691 goto erret;
6692 }
6693 }
6694
6695 if (cctx.ctx_scope != NULL)
6696 {
6697 if (cctx.ctx_scope->se_type == IF_SCOPE)
6698 emsg(_(e_endif));
6699 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6700 emsg(_(e_endwhile));
6701 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6702 emsg(_(e_endfor));
6703 else
6704 emsg(_("E1026: Missing }"));
6705 goto erret;
6706 }
6707
Bram Moolenaarefd88552020-06-18 20:50:10 +02006708 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709 {
6710 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6711 {
6712 emsg(_("E1027: Missing return statement"));
6713 goto erret;
6714 }
6715
6716 // Return zero if there is no return at the end.
6717 generate_PUSHNR(&cctx, 0);
6718 generate_instr(&cctx, ISN_RETURN);
6719 }
6720
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006721 {
6722 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6723 + ufunc->uf_dfunc_idx;
6724 dfunc->df_deleted = FALSE;
6725 dfunc->df_instr = instr->ga_data;
6726 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006727 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006728 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006729 if (cctx.ctx_outer_used)
6730 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006731 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006732 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006733
6734 ret = OK;
6735
6736erret:
6737 if (ret == FAIL)
6738 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006739 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006740 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6741 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006742
6743 for (idx = 0; idx < instr->ga_len; ++idx)
6744 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006746
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006747 // If using the last entry in the table and it was added above, we
6748 // might as well remove it.
6749 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006750 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006751 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006752 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006753 ufunc->uf_dfunc_idx = 0;
6754 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006755 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006756
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006757 while (cctx.ctx_scope != NULL)
6758 drop_scope(&cctx);
6759
Bram Moolenaar20431c92020-03-20 18:39:46 +01006760 // Don't execute this function body.
6761 ga_clear_strings(&ufunc->uf_lines);
6762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006763 if (errormsg != NULL)
6764 emsg(errormsg);
6765 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006766 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767 }
6768
6769 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006770 if (do_estack_push)
6771 estack_pop();
6772
Bram Moolenaar20431c92020-03-20 18:39:46 +01006773 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006774 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006776 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777}
6778
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006779 void
6780set_function_type(ufunc_T *ufunc)
6781{
6782 int varargs = ufunc->uf_va_name != NULL;
6783 int argcount = ufunc->uf_args.ga_len;
6784
6785 // Create a type for the function, with the return type and any
6786 // argument types.
6787 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6788 // The type is included in "tt_args".
6789 if (argcount > 0 || varargs)
6790 {
6791 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6792 argcount, &ufunc->uf_type_list);
6793 // Add argument types to the function type.
6794 if (func_type_add_arg_types(ufunc->uf_func_type,
6795 argcount + varargs,
6796 &ufunc->uf_type_list) == FAIL)
6797 return;
6798 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6799 ufunc->uf_func_type->tt_min_argcount =
6800 argcount - ufunc->uf_def_args.ga_len;
6801 if (ufunc->uf_arg_types == NULL)
6802 {
6803 int i;
6804
6805 // lambda does not have argument types.
6806 for (i = 0; i < argcount; ++i)
6807 ufunc->uf_func_type->tt_args[i] = &t_any;
6808 }
6809 else
6810 mch_memmove(ufunc->uf_func_type->tt_args,
6811 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6812 if (varargs)
6813 {
6814 ufunc->uf_func_type->tt_args[argcount] =
6815 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
6816 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6817 }
6818 }
6819 else
6820 // No arguments, can use a predefined type.
6821 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6822 argcount, &ufunc->uf_type_list);
6823}
6824
6825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826/*
6827 * Delete an instruction, free what it contains.
6828 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006829 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006830delete_instr(isn_T *isn)
6831{
6832 switch (isn->isn_type)
6833 {
6834 case ISN_EXEC:
6835 case ISN_LOADENV:
6836 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006837 case ISN_LOADB:
6838 case ISN_LOADW:
6839 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006841 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006842 case ISN_PUSHEXC:
6843 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006844 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006846 case ISN_STOREB:
6847 case ISN_STOREW:
6848 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006849 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006850 vim_free(isn->isn_arg.string);
6851 break;
6852
6853 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006854 case ISN_STORES:
6855 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006856 break;
6857
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006858 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006859 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006860 vim_free(isn->isn_arg.unlet.ul_name);
6861 break;
6862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006863 case ISN_STOREOPT:
6864 vim_free(isn->isn_arg.storeopt.so_name);
6865 break;
6866
6867 case ISN_PUSHBLOB: // push blob isn_arg.blob
6868 blob_unref(isn->isn_arg.blob);
6869 break;
6870
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006871 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006872#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006873 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006874#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006875 break;
6876
6877 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006878#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006879 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006880#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006881 break;
6882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006883 case ISN_UCALL:
6884 vim_free(isn->isn_arg.ufunc.cuf_name);
6885 break;
6886
Bram Moolenaar221fcc72020-05-05 19:46:20 +02006887 case ISN_FUNCREF:
6888 {
6889 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6890 + isn->isn_arg.funcref.fr_func;
6891 func_ptr_unref(dfunc->df_ufunc);
6892 }
6893 break;
6894
Bram Moolenaar38ddf332020-07-31 22:05:04 +02006895 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02006896 {
6897 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
6898 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
6899
6900 if (ufunc != NULL)
6901 {
6902 // Clear uf_dfunc_idx so that the function is deleted.
6903 clear_def_function(ufunc);
6904 ufunc->uf_dfunc_idx = 0;
6905 func_ptr_unref(ufunc);
6906 }
6907
6908 vim_free(lambda);
6909 vim_free(isn->isn_arg.newfunc.nf_global);
6910 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02006911 break;
6912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 case ISN_2BOOL:
6914 case ISN_2STRING:
6915 case ISN_ADDBLOB:
6916 case ISN_ADDLIST:
6917 case ISN_BCALL:
6918 case ISN_CATCH:
6919 case ISN_CHECKNR:
6920 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02006921 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922 case ISN_COMPAREANY:
6923 case ISN_COMPAREBLOB:
6924 case ISN_COMPAREBOOL:
6925 case ISN_COMPAREDICT:
6926 case ISN_COMPAREFLOAT:
6927 case ISN_COMPAREFUNC:
6928 case ISN_COMPARELIST:
6929 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 case ISN_COMPARESPECIAL:
6931 case ISN_COMPARESTRING:
6932 case ISN_CONCAT:
6933 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02006934 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 case ISN_DROP:
6936 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006937 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006938 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006939 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006940 case ISN_EXECCONCAT:
6941 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02006943 case ISN_LISTINDEX:
6944 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006945 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02006946 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006947 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 case ISN_JUMP:
6949 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02006950 case ISN_LOADBDICT:
6951 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006952 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02006954 case ISN_LOADSCRIPT:
6955 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02006957 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006958 case ISN_NEGATENR:
6959 case ISN_NEWDICT:
6960 case ISN_NEWLIST:
6961 case ISN_OPNR:
6962 case ISN_OPFLOAT:
6963 case ISN_OPANY:
6964 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006965 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966 case ISN_PUSHF:
6967 case ISN_PUSHNR:
6968 case ISN_PUSHBOOL:
6969 case ISN_PUSHSPEC:
6970 case ISN_RETURN:
6971 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02006972 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006973 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006975 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006977 case ISN_STOREDICT:
6978 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 case ISN_THROW:
6980 case ISN_TRY:
6981 // nothing allocated
6982 break;
6983 }
6984}
6985
6986/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006987 * Free all instructions for "dfunc".
6988 */
6989 static void
6990delete_def_function_contents(dfunc_T *dfunc)
6991{
6992 int idx;
6993
6994 ga_clear(&dfunc->df_def_args_isn);
6995
6996 if (dfunc->df_instr != NULL)
6997 {
6998 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6999 delete_instr(dfunc->df_instr + idx);
7000 VIM_CLEAR(dfunc->df_instr);
7001 }
7002
7003 dfunc->df_deleted = TRUE;
7004}
7005
7006/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007007 * When a user function is deleted, clear the contents of any associated def
7008 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009 */
7010 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007011clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007012{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007013 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014 {
7015 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7016 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017
Bram Moolenaar20431c92020-03-20 18:39:46 +01007018 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007019 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020 }
7021}
7022
7023#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007024/*
7025 * Free all functions defined with ":def".
7026 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 void
7028free_def_functions(void)
7029{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007030 int idx;
7031
7032 for (idx = 0; idx < def_functions.ga_len; ++idx)
7033 {
7034 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7035
7036 delete_def_function_contents(dfunc);
7037 }
7038
7039 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040}
7041#endif
7042
7043
7044#endif // FEAT_EVAL