blob: 889a5d5c9c7ca9294ac2b716c27198ee6a903a4b [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
Bram Moolenaar127542b2020-08-09 17:22:04 +02001113 // get the member type from all the items on the stack.
1114 member = get_member_type_from_stack(
1115 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1116 cctx->ctx_type_list);
1117 type = get_list_type(member, cctx->ctx_type_list);
1118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 // drop the value types
1120 stack->ga_len -= count;
1121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 // add the list type to the type stack
1123 if (ga_grow(stack, 1) == FAIL)
1124 return FAIL;
1125 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1126 ++stack->ga_len;
1127
1128 return OK;
1129}
1130
1131/*
1132 * Generate an ISN_NEWDICT instruction.
1133 */
1134 static int
1135generate_NEWDICT(cctx_T *cctx, int count)
1136{
1137 isn_T *isn;
1138 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 type_T *type;
1140 type_T *member;
1141
Bram Moolenaar080457c2020-03-03 21:53:32 +01001142 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1144 return FAIL;
1145 isn->isn_arg.number = count;
1146
Bram Moolenaar127542b2020-08-09 17:22:04 +02001147 member = get_member_type_from_stack(
1148 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1149 cctx->ctx_type_list);
1150 type = get_dict_type(member, cctx->ctx_type_list);
1151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 // drop the key and value types
1153 stack->ga_len -= 2 * count;
1154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 // add the dict type to the type stack
1156 if (ga_grow(stack, 1) == FAIL)
1157 return FAIL;
1158 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1159 ++stack->ga_len;
1160
1161 return OK;
1162}
1163
1164/*
1165 * Generate an ISN_FUNCREF instruction.
1166 */
1167 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001168generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169{
1170 isn_T *isn;
1171 garray_T *stack = &cctx->ctx_type_stack;
1172
Bram Moolenaar080457c2020-03-03 21:53:32 +01001173 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1175 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001176 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001177 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178
1179 if (ga_grow(stack, 1) == FAIL)
1180 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001181 ((type_T **)stack->ga_data)[stack->ga_len] =
1182 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 ++stack->ga_len;
1184
1185 return OK;
1186}
1187
1188/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001189 * Generate an ISN_NEWFUNC instruction.
1190 */
1191 static int
1192generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1193{
1194 isn_T *isn;
1195 char_u *name;
1196
1197 RETURN_OK_IF_SKIP(cctx);
1198 name = vim_strsave(lambda_name);
1199 if (name == NULL)
1200 return FAIL;
1201 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1202 return FAIL;
1203 isn->isn_arg.newfunc.nf_lambda = name;
1204 isn->isn_arg.newfunc.nf_global = func_name;
1205
1206 return OK;
1207}
1208
1209/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 * Generate an ISN_JUMP instruction.
1211 */
1212 static int
1213generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1214{
1215 isn_T *isn;
1216 garray_T *stack = &cctx->ctx_type_stack;
1217
Bram Moolenaar080457c2020-03-03 21:53:32 +01001218 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1220 return FAIL;
1221 isn->isn_arg.jump.jump_when = when;
1222 isn->isn_arg.jump.jump_where = where;
1223
1224 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1225 --stack->ga_len;
1226
1227 return OK;
1228}
1229
1230 static int
1231generate_FOR(cctx_T *cctx, int loop_idx)
1232{
1233 isn_T *isn;
1234 garray_T *stack = &cctx->ctx_type_stack;
1235
Bram Moolenaar080457c2020-03-03 21:53:32 +01001236 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1238 return FAIL;
1239 isn->isn_arg.forloop.for_idx = loop_idx;
1240
1241 if (ga_grow(stack, 1) == FAIL)
1242 return FAIL;
1243 // type doesn't matter, will be stored next
1244 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1245 ++stack->ga_len;
1246
1247 return OK;
1248}
1249
1250/*
1251 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001252 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 * Return FAIL if the number of arguments is wrong.
1254 */
1255 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001256generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257{
1258 isn_T *isn;
1259 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001260 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001261 type_T *argtypes[MAX_FUNC_ARGS];
1262 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263
Bram Moolenaar080457c2020-03-03 21:53:32 +01001264 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001265 argoff = check_internal_func(func_idx, argcount);
1266 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 return FAIL;
1268
Bram Moolenaar389df252020-07-09 21:20:47 +02001269 if (method_call && argoff > 1)
1270 {
1271 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1272 return FAIL;
1273 isn->isn_arg.shuffle.shfl_item = argcount;
1274 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1275 }
1276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1278 return FAIL;
1279 isn->isn_arg.bfunc.cbf_idx = func_idx;
1280 isn->isn_arg.bfunc.cbf_argcount = argcount;
1281
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001282 for (i = 0; i < argcount; ++i)
1283 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 stack->ga_len -= argcount; // drop the arguments
1286 if (ga_grow(stack, 1) == FAIL)
1287 return FAIL;
1288 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001289 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 ++stack->ga_len; // add return value
1291
1292 return OK;
1293}
1294
1295/*
1296 * Generate an ISN_DCALL or ISN_UCALL instruction.
1297 * Return FAIL if the number of arguments is wrong.
1298 */
1299 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001300generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301{
1302 isn_T *isn;
1303 garray_T *stack = &cctx->ctx_type_stack;
1304 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001305 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306
Bram Moolenaar080457c2020-03-03 21:53:32 +01001307 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 if (argcount > regular_args && !has_varargs(ufunc))
1309 {
1310 semsg(_(e_toomanyarg), ufunc->uf_name);
1311 return FAIL;
1312 }
1313 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1314 {
1315 semsg(_(e_toofewarg), ufunc->uf_name);
1316 return FAIL;
1317 }
1318
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001319 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001320 {
1321 int i;
1322
1323 for (i = 0; i < argcount; ++i)
1324 {
1325 type_T *expected;
1326 type_T *actual;
1327
1328 if (i < regular_args)
1329 {
1330 if (ufunc->uf_arg_types == NULL)
1331 continue;
1332 expected = ufunc->uf_arg_types[i];
1333 }
1334 else
1335 expected = ufunc->uf_va_type->tt_member;
1336 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001337 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001338 {
1339 arg_type_mismatch(expected, actual, i + 1);
1340 return FAIL;
1341 }
1342 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001343 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001344 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1345 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001346 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001347 }
1348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001350 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001351 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001353 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 {
1355 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1356 isn->isn_arg.dfunc.cdf_argcount = argcount;
1357 }
1358 else
1359 {
1360 // A user function may be deleted and redefined later, can't use the
1361 // ufunc pointer, need to look it up again at runtime.
1362 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1363 isn->isn_arg.ufunc.cuf_argcount = argcount;
1364 }
1365
1366 stack->ga_len -= argcount; // drop the arguments
1367 if (ga_grow(stack, 1) == FAIL)
1368 return FAIL;
1369 // add return value
1370 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1371 ++stack->ga_len;
1372
1373 return OK;
1374}
1375
1376/*
1377 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1378 */
1379 static int
1380generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1381{
1382 isn_T *isn;
1383 garray_T *stack = &cctx->ctx_type_stack;
1384
Bram Moolenaar080457c2020-03-03 21:53:32 +01001385 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1387 return FAIL;
1388 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1389 isn->isn_arg.ufunc.cuf_argcount = argcount;
1390
1391 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001392 if (ga_grow(stack, 1) == FAIL)
1393 return FAIL;
1394 // add return value
1395 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1396 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397
1398 return OK;
1399}
1400
1401/*
1402 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001403 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 */
1405 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001406generate_PCALL(
1407 cctx_T *cctx,
1408 int argcount,
1409 char_u *name,
1410 type_T *type,
1411 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412{
1413 isn_T *isn;
1414 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001415 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416
Bram Moolenaar080457c2020-03-03 21:53:32 +01001417 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001418
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001419 if (type->tt_type == VAR_ANY)
1420 ret_type = &t_any;
1421 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001422 {
1423 if (type->tt_argcount != -1)
1424 {
1425 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1426
1427 if (argcount < type->tt_min_argcount - varargs)
1428 {
1429 semsg(_(e_toofewarg), "[reference]");
1430 return FAIL;
1431 }
1432 if (!varargs && argcount > type->tt_argcount)
1433 {
1434 semsg(_(e_toomanyarg), "[reference]");
1435 return FAIL;
1436 }
1437 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001438 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001439 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001440 else
1441 {
1442 semsg(_("E1085: Not a callable type: %s"), name);
1443 return FAIL;
1444 }
1445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1447 return FAIL;
1448 isn->isn_arg.pfunc.cpf_top = at_top;
1449 isn->isn_arg.pfunc.cpf_argcount = argcount;
1450
1451 stack->ga_len -= argcount; // drop the arguments
1452
1453 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001454 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001456 // If partial is above the arguments it must be cleared and replaced with
1457 // the return value.
1458 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1459 return FAIL;
1460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 return OK;
1462}
1463
1464/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001465 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 */
1467 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001468generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469{
1470 isn_T *isn;
1471 garray_T *stack = &cctx->ctx_type_stack;
1472 type_T *type;
1473
Bram Moolenaar080457c2020-03-03 21:53:32 +01001474 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001475 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001477 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001479 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001481 if (type->tt_type != VAR_DICT && type != &t_any)
1482 {
1483 emsg(_(e_dictreq));
1484 return FAIL;
1485 }
1486 // change dict type to dict member type
1487 if (type->tt_type == VAR_DICT)
1488 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489
1490 return OK;
1491}
1492
1493/*
1494 * Generate an ISN_ECHO instruction.
1495 */
1496 static int
1497generate_ECHO(cctx_T *cctx, int with_white, int count)
1498{
1499 isn_T *isn;
1500
Bram Moolenaar080457c2020-03-03 21:53:32 +01001501 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1503 return FAIL;
1504 isn->isn_arg.echo.echo_with_white = with_white;
1505 isn->isn_arg.echo.echo_count = count;
1506
1507 return OK;
1508}
1509
Bram Moolenaarad39c092020-02-26 18:23:43 +01001510/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001511 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001512 */
1513 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001514generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001515{
1516 isn_T *isn;
1517
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001518 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001519 return FAIL;
1520 isn->isn_arg.number = count;
1521
1522 return OK;
1523}
1524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 static int
1526generate_EXEC(cctx_T *cctx, char_u *line)
1527{
1528 isn_T *isn;
1529
Bram Moolenaar080457c2020-03-03 21:53:32 +01001530 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1532 return FAIL;
1533 isn->isn_arg.string = vim_strsave(line);
1534 return OK;
1535}
1536
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001537 static int
1538generate_EXECCONCAT(cctx_T *cctx, int count)
1539{
1540 isn_T *isn;
1541
1542 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1543 return FAIL;
1544 isn->isn_arg.number = count;
1545 return OK;
1546}
1547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548/*
1549 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001550 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001552 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1554{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 lvar_T *lvar;
1556
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001557 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001559 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001560 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 }
1562
1563 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001564 return NULL;
1565 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001567 // Every local variable uses the next entry on the stack. We could re-use
1568 // the last ones when leaving a scope, but then variables used in a closure
1569 // might get overwritten. To keep things simple do not re-use stack
1570 // entries. This is less efficient, but memory is cheap these days.
1571 lvar->lv_idx = cctx->ctx_locals_count++;
1572
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001573 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 lvar->lv_const = isConst;
1575 lvar->lv_type = type;
1576
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001577 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578}
1579
1580/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001581 * Remove local variables above "new_top".
1582 */
1583 static void
1584unwind_locals(cctx_T *cctx, int new_top)
1585{
1586 if (cctx->ctx_locals.ga_len > new_top)
1587 {
1588 int idx;
1589 lvar_T *lvar;
1590
1591 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1592 {
1593 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1594 vim_free(lvar->lv_name);
1595 }
1596 }
1597 cctx->ctx_locals.ga_len = new_top;
1598}
1599
1600/*
1601 * Free all local variables.
1602 */
1603 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001604free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001605{
1606 unwind_locals(cctx, 0);
1607 ga_clear(&cctx->ctx_locals);
1608}
1609
1610/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 * Find "name" in script-local items of script "sid".
1612 * Returns the index in "sn_var_vals" if found.
1613 * If found but not in "sn_var_vals" returns -1.
1614 * If not found returns -2.
1615 */
1616 int
1617get_script_item_idx(int sid, char_u *name, int check_writable)
1618{
1619 hashtab_T *ht;
1620 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001621 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 int idx;
1623
1624 // First look the name up in the hashtable.
1625 if (sid <= 0 || sid > script_items.ga_len)
1626 return -1;
1627 ht = &SCRIPT_VARS(sid);
1628 di = find_var_in_ht(ht, 0, name, TRUE);
1629 if (di == NULL)
1630 return -2;
1631
1632 // Now find the svar_T index in sn_var_vals.
1633 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1634 {
1635 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1636
1637 if (sv->sv_tv == &di->di_tv)
1638 {
1639 if (check_writable && sv->sv_const)
1640 semsg(_(e_readonlyvar), name);
1641 return idx;
1642 }
1643 }
1644 return -1;
1645}
1646
1647/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001648 * Find "name" in imported items of the current script or in "cctx" if not
1649 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 */
1651 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001652find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 int idx;
1655
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001656 if (current_sctx.sc_sid <= 0)
1657 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 if (cctx != NULL)
1659 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1660 {
1661 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1662 + idx;
1663
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001664 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1665 : STRLEN(import->imp_name) == len
1666 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 return import;
1668 }
1669
Bram Moolenaarefa94442020-08-08 22:16:00 +02001670 return find_imported_in_script(name, len, current_sctx.sc_sid);
1671}
1672
1673 imported_T *
1674find_imported_in_script(char_u *name, size_t len, int sid)
1675{
1676 scriptitem_T *si = SCRIPT_ITEM(sid);
1677 int idx;
1678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1680 {
1681 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1682
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001683 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1684 : STRLEN(import->imp_name) == len
1685 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 return import;
1687 }
1688 return NULL;
1689}
1690
1691/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001692 * Free all imported variables.
1693 */
1694 static void
1695free_imported(cctx_T *cctx)
1696{
1697 int idx;
1698
1699 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1700 {
1701 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1702
1703 vim_free(import->imp_name);
1704 }
1705 ga_clear(&cctx->ctx_imports);
1706}
1707
1708/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001709 * Return TRUE if "p" points at a "#" but not at "#{".
1710 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001711 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001712vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001713{
1714 return p[0] == '#' && p[1] != '{';
1715}
1716
1717/*
1718 * Return a pointer to the next line that isn't empty or only contains a
1719 * comment. Skips over white space.
1720 * Returns NULL if there is none.
1721 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001722 char_u *
1723peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001724{
1725 int lnum = cctx->ctx_lnum;
1726
1727 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1728 {
1729 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001730 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001731
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001732 // ignore NULLs inserted for continuation lines
1733 if (line != NULL)
1734 {
1735 p = skipwhite(line);
1736 if (*p != NUL && !vim9_comment_start(p))
1737 return p;
1738 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001739 }
1740 return NULL;
1741}
1742
1743/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001744 * Called when checking for a following operator at "arg". When the rest of
1745 * the line is empty or only a comment, peek the next line. If there is a next
1746 * line return a pointer to it and set "nextp".
1747 * Otherwise skip over white space.
1748 */
1749 static char_u *
1750may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1751{
1752 char_u *p = skipwhite(arg);
1753
1754 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001755 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001756 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001757 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001758 if (*nextp != NULL)
1759 return *nextp;
1760 }
1761 return p;
1762}
1763
1764/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001765 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001766 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001767 * Returns NULL when at the end.
1768 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001769 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001770next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001771{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001772 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001773
1774 do
1775 {
1776 ++cctx->ctx_lnum;
1777 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001778 {
1779 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001780 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001781 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001782 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001783 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001784 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001785 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001786 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001787 return line;
1788}
1789
1790/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001791 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001792 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001793 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1794 */
1795 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001796may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001797{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001798 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001799 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001800 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001801
1802 if (next == NULL)
1803 return FAIL;
1804 *arg = skipwhite(next);
1805 }
1806 return OK;
1807}
1808
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001809/*
1810 * Idem, and give an error when failed.
1811 */
1812 static int
1813may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1814{
1815 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1816 {
1817 emsg(_("E1097: line incomplete"));
1818 return FAIL;
1819 }
1820 return OK;
1821}
1822
1823
Bram Moolenaara5565e42020-05-09 15:44:01 +02001824// Structure passed between the compile_expr* functions to keep track of
1825// constants that have been parsed but for which no code was produced yet. If
1826// possible expressions on these constants are applied at compile time. If
1827// that is not possible, the code to push the constants needs to be generated
1828// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001829// Using 50 should be more than enough of 5 levels of ().
1830#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001831typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001832 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001833 int pp_used; // active entries in pp_tv[]
1834} ppconst_T;
1835
Bram Moolenaar1c747212020-05-09 18:28:34 +02001836static int compile_expr0(char_u **arg, cctx_T *cctx);
1837static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1838
Bram Moolenaara5565e42020-05-09 15:44:01 +02001839/*
1840 * Generate a PUSH instruction for "tv".
1841 * "tv" will be consumed or cleared.
1842 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1843 */
1844 static int
1845generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1846{
1847 if (tv != NULL)
1848 {
1849 switch (tv->v_type)
1850 {
1851 case VAR_UNKNOWN:
1852 break;
1853 case VAR_BOOL:
1854 generate_PUSHBOOL(cctx, tv->vval.v_number);
1855 break;
1856 case VAR_SPECIAL:
1857 generate_PUSHSPEC(cctx, tv->vval.v_number);
1858 break;
1859 case VAR_NUMBER:
1860 generate_PUSHNR(cctx, tv->vval.v_number);
1861 break;
1862#ifdef FEAT_FLOAT
1863 case VAR_FLOAT:
1864 generate_PUSHF(cctx, tv->vval.v_float);
1865 break;
1866#endif
1867 case VAR_BLOB:
1868 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1869 tv->vval.v_blob = NULL;
1870 break;
1871 case VAR_STRING:
1872 generate_PUSHS(cctx, tv->vval.v_string);
1873 tv->vval.v_string = NULL;
1874 break;
1875 default:
1876 iemsg("constant type not supported");
1877 clear_tv(tv);
1878 return FAIL;
1879 }
1880 tv->v_type = VAR_UNKNOWN;
1881 }
1882 return OK;
1883}
1884
1885/*
1886 * Generate code for any ppconst entries.
1887 */
1888 static int
1889generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1890{
1891 int i;
1892 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001893 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001894
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001895 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001896 for (i = 0; i < ppconst->pp_used; ++i)
1897 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1898 ret = FAIL;
1899 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001900 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001901 return ret;
1902}
1903
1904/*
1905 * Clear ppconst constants. Used when failing.
1906 */
1907 static void
1908clear_ppconst(ppconst_T *ppconst)
1909{
1910 int i;
1911
1912 for (i = 0; i < ppconst->pp_used; ++i)
1913 clear_tv(&ppconst->pp_tv[i]);
1914 ppconst->pp_used = 0;
1915}
1916
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001917/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001918 * Generate an instruction to load script-local variable "name", without the
1919 * leading "s:".
1920 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 */
1922 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001923compile_load_scriptvar(
1924 cctx_T *cctx,
1925 char_u *name, // variable NUL terminated
1926 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001927 char_u **end, // end of variable
1928 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001930 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1932 imported_T *import;
1933
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001934 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001936 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001937 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1938 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 }
1940 if (idx >= 0)
1941 {
1942 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1943
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001944 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 current_sctx.sc_sid, idx, sv->sv_type);
1946 return OK;
1947 }
1948
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001949 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 if (import != NULL)
1951 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001952 if (import->imp_all)
1953 {
1954 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02001955 char_u *exp_name;
1956 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001957 ufunc_T *ufunc;
1958 type_T *type;
1959
1960 // Used "import * as Name", need to lookup the member.
1961 if (*p != '.')
1962 {
1963 semsg(_("E1060: expected dot after name: %s"), start);
1964 return FAIL;
1965 }
1966 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001967 if (VIM_ISWHITE(*p))
1968 {
1969 emsg(_("E1074: no white space allowed after dot"));
1970 return FAIL;
1971 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001972
Bram Moolenaar1c991142020-07-04 13:15:31 +02001973 // isolate one name
1974 exp_name = p;
1975 while (eval_isnamec(*p))
1976 ++p;
1977 cc = *p;
1978 *p = NUL;
1979
1980 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
1981 *p = cc;
1982 p = skipwhite(p);
1983
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001984 // TODO: what if it is a function?
1985 if (idx < 0)
1986 return FAIL;
1987 *end = p;
1988
1989 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1990 import->imp_sid,
1991 idx,
1992 type);
1993 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001994 else if (import->imp_funcname != NULL)
1995 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001996 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001997 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1998 import->imp_sid,
1999 import->imp_var_vals_idx,
2000 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 return OK;
2002 }
2003
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002004 if (error)
2005 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 return FAIL;
2007}
2008
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002009 static int
2010generate_funcref(cctx_T *cctx, char_u *name)
2011{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002012 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002013
2014 if (ufunc == NULL)
2015 return FAIL;
2016
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002017 // Need to compile any default values to get the argument types.
2018 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2019 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2020 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002021 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002022}
2023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024/*
2025 * Compile a variable name into a load instruction.
2026 * "end" points to just after the name.
2027 * When "error" is FALSE do not give an error when not found.
2028 */
2029 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002030compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031{
2032 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002033 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002034 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002036 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037
2038 if (*(*arg + 1) == ':')
2039 {
2040 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002041 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002042 {
2043 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002045 switch (**arg)
2046 {
2047 case 'g': isn_type = ISN_LOADGDICT; break;
2048 case 'w': isn_type = ISN_LOADWDICT; break;
2049 case 't': isn_type = ISN_LOADTDICT; break;
2050 case 'b': isn_type = ISN_LOADBDICT; break;
2051 default:
2052 semsg(_(e_namespace), *arg);
2053 goto theend;
2054 }
2055 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2056 goto theend;
2057 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 else
2060 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002061 isntype_T isn_type = ISN_DROP;
2062
2063 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2064 if (name == NULL)
2065 return FAIL;
2066
2067 switch (**arg)
2068 {
2069 case 'v': res = generate_LOADV(cctx, name, error);
2070 break;
2071 case 's': res = compile_load_scriptvar(cctx, name,
2072 NULL, NULL, error);
2073 break;
2074 case 'g': isn_type = ISN_LOADG; break;
2075 case 'w': isn_type = ISN_LOADW; break;
2076 case 't': isn_type = ISN_LOADT; break;
2077 case 'b': isn_type = ISN_LOADB; break;
2078 default: semsg(_(e_namespace), *arg);
2079 goto theend;
2080 }
2081 if (isn_type != ISN_DROP)
2082 {
2083 // Global, Buffer-local, Window-local and Tabpage-local
2084 // variables can be defined later, thus we don't check if it
2085 // exists, give error at runtime.
2086 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088 }
2089 }
2090 else
2091 {
2092 size_t len = end - *arg;
2093 int idx;
2094 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002095 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096
2097 name = vim_strnsave(*arg, end - *arg);
2098 if (name == NULL)
2099 return FAIL;
2100
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002101 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002103 if (!gen_load_outer)
2104 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 }
2106 else
2107 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002108 lvar_T *lvar = lookup_local(*arg, len, cctx);
2109
2110 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002112 type = lvar->lv_type;
2113 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002114 if (lvar->lv_from_outer)
2115 gen_load_outer = TRUE;
2116 else
2117 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 }
2119 else
2120 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002121 // "var" can be script-local even without using "s:" if it
2122 // already exists.
2123 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2124 == SCRIPT_VERSION_VIM9
2125 || lookup_script(*arg, len) == OK)
2126 res = compile_load_scriptvar(cctx, name, *arg, &end,
2127 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002128
Bram Moolenaara5565e42020-05-09 15:44:01 +02002129 // When the name starts with an uppercase letter or "x:" it
2130 // can be a user defined function.
2131 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2132 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 }
2134 }
2135 if (gen_load)
2136 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002137 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002138 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002139 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002140 cctx->ctx_outer_used = TRUE;
2141 }
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 Moolenaara1773442020-08-12 15:21:22 +02002222 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223
Bram Moolenaara5565e42020-05-09 15:44:01 +02002224 // we can evaluate "has('name')" at compile time
2225 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2226 {
2227 char_u *s = skipwhite(*arg + varlen + 1);
2228 typval_T argvars[2];
2229
2230 argvars[0].v_type = VAR_UNKNOWN;
2231 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002232 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002233 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002234 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002235 s = skipwhite(s);
2236 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2237 {
2238 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2239
2240 *arg = s + 1;
2241 argvars[1].v_type = VAR_UNKNOWN;
2242 tv->v_type = VAR_NUMBER;
2243 tv->vval.v_number = 0;
2244 f_has(argvars, tv);
2245 clear_tv(&argvars[0]);
2246 ++ppconst->pp_used;
2247 return OK;
2248 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002249 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002250 }
2251
2252 if (generate_ppconst(cctx, ppconst) == FAIL)
2253 return FAIL;
2254
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 if (varlen >= sizeof(namebuf))
2256 {
2257 semsg(_("E1011: name too long: %s"), name);
2258 return FAIL;
2259 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002260 vim_strncpy(namebuf, *arg, varlen);
2261 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262
2263 *arg = skipwhite(*arg + varlen + 1);
2264 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002265 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266
Bram Moolenaara1773442020-08-12 15:21:22 +02002267 is_autoload = vim_strchr(name, '#') != NULL;
2268 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 {
2270 int idx;
2271
2272 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002273 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002275 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002276 else
2277 semsg(_(e_unknownfunc), namebuf);
2278 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 }
2280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002282 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002284 {
2285 res = generate_CALL(cctx, ufunc, argcount);
2286 goto theend;
2287 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288
2289 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002290 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002291 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002293 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002294 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002295 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002296 garray_T *stack = &cctx->ctx_type_stack;
2297 type_T *type;
2298
2299 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2300 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002301 goto theend;
2302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002304 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002305 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002306 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002307 res = generate_UCALL(cctx, name, argcount);
2308 else
2309 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002310
2311theend:
2312 vim_free(tofree);
2313 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314}
2315
2316// like NAMESPACE_CHAR but with 'a' and 'l'.
2317#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2318
2319/*
2320 * Find the end of a variable or function name. Unlike find_name_end() this
2321 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002322 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 * Return a pointer to just after the name. Equal to "arg" if there is no
2324 * valid name.
2325 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002326 static char_u *
2327to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328{
2329 char_u *p;
2330
2331 // Quick check for valid starting character.
2332 if (!eval_isnamec1(*arg))
2333 return arg;
2334
2335 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2336 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2337 // and can be used in slice "[n:]".
2338 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002339 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2341 break;
2342 return p;
2343}
2344
2345/*
2346 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002347 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 */
2349 char_u *
2350to_name_const_end(char_u *arg)
2351{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002352 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 typval_T rettv;
2354
2355 if (p == arg && *arg == '[')
2356 {
2357
2358 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002359 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002360 p = arg;
2361 }
2362 else if (p == arg && *arg == '#' && arg[1] == '{')
2363 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002364 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002366 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367 p = arg;
2368 }
2369 else if (p == arg && *arg == '{')
2370 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002371 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002373 // Can be "{x -> ret}()".
2374 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002376 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 if (ret != OK)
2378 p = arg;
2379 }
2380
2381 return p;
2382}
2383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384/*
2385 * parse a list: [expr, expr]
2386 * "*arg" points to the '['.
2387 */
2388 static int
2389compile_list(char_u **arg, cctx_T *cctx)
2390{
2391 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002392 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 int count = 0;
2394
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002395 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002397 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002398 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002399 semsg(_(e_list_end), *arg);
2400 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002401 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002402 if (*p == ',')
2403 {
2404 semsg(_(e_no_white_before), ",");
2405 return FAIL;
2406 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002407 if (*p == ']')
2408 {
2409 ++p;
2410 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002411 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002412 p += STRLEN(p);
2413 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002414 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002415 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 break;
2417 ++count;
2418 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002419 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002421 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2422 {
2423 semsg(_(e_white_after), ",");
2424 return FAIL;
2425 }
2426 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002427 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 p = skipwhite(p);
2429 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002430 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431
2432 generate_NEWLIST(cctx, count);
2433 return OK;
2434}
2435
2436/*
2437 * parse a lambda: {arg, arg -> expr}
2438 * "*arg" points to the '{'.
2439 */
2440 static int
2441compile_lambda(char_u **arg, cctx_T *cctx)
2442{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 typval_T rettv;
2444 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002445 evalarg_T evalarg;
2446
2447 CLEAR_FIELD(evalarg);
2448 evalarg.eval_flags = EVAL_EVALUATE;
2449 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450
2451 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002452 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002456 ++ufunc->uf_refcount;
2457 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002458 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459
2460 // The function will have one line: "return {expr}".
2461 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002462 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002464 clear_evalarg(&evalarg, NULL);
2465
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002466 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002467 {
2468 // The return type will now be known.
2469 set_function_type(ufunc);
2470
2471 return generate_FUNCREF(cctx, ufunc);
2472 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002473
2474 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 return FAIL;
2476}
2477
2478/*
2479 * Compile a lamda call: expr->{lambda}(args)
2480 * "arg" points to the "{".
2481 */
2482 static int
2483compile_lambda_call(char_u **arg, cctx_T *cctx)
2484{
2485 ufunc_T *ufunc;
2486 typval_T rettv;
2487 int argcount = 1;
2488 int ret = FAIL;
2489
2490 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002491 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 return FAIL;
2493
2494 if (**arg != '(')
2495 {
2496 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002497 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 else
2499 semsg(_(e_missing_paren), "lambda");
2500 clear_tv(&rettv);
2501 return FAIL;
2502 }
2503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 ufunc = rettv.vval.v_partial->pt_func;
2505 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002506 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002507 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002508
2509 // The function will have one line: "return {expr}".
2510 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002511 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512
2513 // compile the arguments
2514 *arg = skipwhite(*arg + 1);
2515 if (compile_arguments(arg, cctx, &argcount) == OK)
2516 // call the compiled function
2517 ret = generate_CALL(cctx, ufunc, argcount);
2518
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002519 if (ret == FAIL)
2520 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 return ret;
2522}
2523
2524/*
2525 * parse a dict: {'key': val} or #{key: val}
2526 * "*arg" points to the '{'.
2527 */
2528 static int
2529compile_dict(char_u **arg, cctx_T *cctx, int literal)
2530{
2531 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002532 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 int count = 0;
2534 dict_T *d = dict_alloc();
2535 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002536 char_u *whitep = *arg;
2537 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538
2539 if (d == NULL)
2540 return FAIL;
2541 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002542 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 {
2544 char_u *key = NULL;
2545
Bram Moolenaar23c55272020-06-21 16:58:13 +02002546 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002547 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002548 *arg = NULL;
2549 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002550 }
2551
2552 if (**arg == '}')
2553 break;
2554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 if (literal)
2556 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002557 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558
Bram Moolenaar2c330432020-04-13 14:41:35 +02002559 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 {
2561 semsg(_("E1014: Invalid key: %s"), *arg);
2562 return FAIL;
2563 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002564 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 if (generate_PUSHS(cctx, key) == FAIL)
2566 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002567 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 }
2569 else
2570 {
2571 isn_T *isn;
2572
Bram Moolenaara5565e42020-05-09 15:44:01 +02002573 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2576 if (isn->isn_type == ISN_PUSHS)
2577 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002578 else
2579 {
2580 type_T *keytype = ((type_T **)stack->ga_data)
2581 [stack->ga_len - 1];
2582 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2583 return FAIL;
2584 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 }
2586
2587 // Check for duplicate keys, if using string keys.
2588 if (key != NULL)
2589 {
2590 item = dict_find(d, key, -1);
2591 if (item != NULL)
2592 {
2593 semsg(_(e_duplicate_key), key);
2594 goto failret;
2595 }
2596 item = dictitem_alloc(key);
2597 if (item != NULL)
2598 {
2599 item->di_tv.v_type = VAR_UNKNOWN;
2600 item->di_tv.v_lock = 0;
2601 if (dict_add(d, item) == FAIL)
2602 dictitem_free(item);
2603 }
2604 }
2605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 if (**arg != ':')
2607 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002608 if (*skipwhite(*arg) == ':')
2609 semsg(_(e_no_white_before), ":");
2610 else
2611 semsg(_(e_missing_dict_colon), *arg);
2612 return FAIL;
2613 }
2614 whitep = *arg + 1;
2615 if (!IS_WHITE_OR_NUL(*whitep))
2616 {
2617 semsg(_(e_white_after), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 return FAIL;
2619 }
2620
2621 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002622 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002623 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002624 *arg = NULL;
2625 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002626 }
2627
Bram Moolenaara5565e42020-05-09 15:44:01 +02002628 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 return FAIL;
2630 ++count;
2631
Bram Moolenaar2c330432020-04-13 14:41:35 +02002632 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002633 *arg = skipwhite(*arg);
2634 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002635 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002636 *arg = NULL;
2637 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 if (**arg == '}')
2640 break;
2641 if (**arg != ',')
2642 {
2643 semsg(_(e_missing_dict_comma), *arg);
2644 goto failret;
2645 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002646 if (IS_WHITE_OR_NUL(*whitep))
2647 {
2648 semsg(_(e_no_white_before), ",");
2649 return FAIL;
2650 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002651 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 *arg = skipwhite(*arg + 1);
2653 }
2654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 *arg = *arg + 1;
2656
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002657 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002658 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002659 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002660 *arg += STRLEN(*arg);
2661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 dict_unref(d);
2663 return generate_NEWDICT(cctx, count);
2664
2665failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002666 if (*arg == NULL)
2667 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 dict_unref(d);
2669 return FAIL;
2670}
2671
2672/*
2673 * Compile "&option".
2674 */
2675 static int
2676compile_get_option(char_u **arg, cctx_T *cctx)
2677{
2678 typval_T rettv;
2679 char_u *start = *arg;
2680 int ret;
2681
2682 // parse the option and get the current value to get the type.
2683 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002684 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 if (ret == OK)
2686 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002687 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 char_u *name = vim_strnsave(start, *arg - start);
2689 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2690
2691 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2692 vim_free(name);
2693 }
2694 clear_tv(&rettv);
2695
2696 return ret;
2697}
2698
2699/*
2700 * Compile "$VAR".
2701 */
2702 static int
2703compile_get_env(char_u **arg, cctx_T *cctx)
2704{
2705 char_u *start = *arg;
2706 int len;
2707 int ret;
2708 char_u *name;
2709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 ++*arg;
2711 len = get_env_len(arg);
2712 if (len == 0)
2713 {
2714 semsg(_(e_syntax_at), start - 1);
2715 return FAIL;
2716 }
2717
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002718 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 name = vim_strnsave(start, len + 1);
2720 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2721 vim_free(name);
2722 return ret;
2723}
2724
2725/*
2726 * Compile "@r".
2727 */
2728 static int
2729compile_get_register(char_u **arg, cctx_T *cctx)
2730{
2731 int ret;
2732
2733 ++*arg;
2734 if (**arg == NUL)
2735 {
2736 semsg(_(e_syntax_at), *arg - 1);
2737 return FAIL;
2738 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002739 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 {
2741 emsg_invreg(**arg);
2742 return FAIL;
2743 }
2744 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2745 ++*arg;
2746 return ret;
2747}
2748
2749/*
2750 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002751 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 */
2753 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002754apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002756 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757
2758 // this works from end to start
2759 while (p > start)
2760 {
2761 --p;
2762 if (*p == '-' || *p == '+')
2763 {
2764 // only '-' has an effect, for '+' we only check the type
2765#ifdef FEAT_FLOAT
2766 if (rettv->v_type == VAR_FLOAT)
2767 {
2768 if (*p == '-')
2769 rettv->vval.v_float = -rettv->vval.v_float;
2770 }
2771 else
2772#endif
2773 {
2774 varnumber_T val;
2775 int error = FALSE;
2776
2777 // tv_get_number_chk() accepts a string, but we don't want that
2778 // here
2779 if (check_not_string(rettv) == FAIL)
2780 return FAIL;
2781 val = tv_get_number_chk(rettv, &error);
2782 clear_tv(rettv);
2783 if (error)
2784 return FAIL;
2785 if (*p == '-')
2786 val = -val;
2787 rettv->v_type = VAR_NUMBER;
2788 rettv->vval.v_number = val;
2789 }
2790 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002791 else if (numeric_only)
2792 {
2793 ++p;
2794 break;
2795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 else
2797 {
2798 int v = tv2bool(rettv);
2799
2800 // '!' is permissive in the type.
2801 clear_tv(rettv);
2802 rettv->v_type = VAR_BOOL;
2803 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2804 }
2805 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002806 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 return OK;
2808}
2809
2810/*
2811 * Recognize v: variables that are constants and set "rettv".
2812 */
2813 static void
2814get_vim_constant(char_u **arg, typval_T *rettv)
2815{
2816 if (STRNCMP(*arg, "v:true", 6) == 0)
2817 {
2818 rettv->v_type = VAR_BOOL;
2819 rettv->vval.v_number = VVAL_TRUE;
2820 *arg += 6;
2821 }
2822 else if (STRNCMP(*arg, "v:false", 7) == 0)
2823 {
2824 rettv->v_type = VAR_BOOL;
2825 rettv->vval.v_number = VVAL_FALSE;
2826 *arg += 7;
2827 }
2828 else if (STRNCMP(*arg, "v:null", 6) == 0)
2829 {
2830 rettv->v_type = VAR_SPECIAL;
2831 rettv->vval.v_number = VVAL_NULL;
2832 *arg += 6;
2833 }
2834 else if (STRNCMP(*arg, "v:none", 6) == 0)
2835 {
2836 rettv->v_type = VAR_SPECIAL;
2837 rettv->vval.v_number = VVAL_NONE;
2838 *arg += 6;
2839 }
2840}
2841
Bram Moolenaar696ba232020-07-29 21:20:41 +02002842 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002843get_compare_type(char_u *p, int *len, int *type_is)
2844{
2845 exptype_T type = EXPR_UNKNOWN;
2846 int i;
2847
2848 switch (p[0])
2849 {
2850 case '=': if (p[1] == '=')
2851 type = EXPR_EQUAL;
2852 else if (p[1] == '~')
2853 type = EXPR_MATCH;
2854 break;
2855 case '!': if (p[1] == '=')
2856 type = EXPR_NEQUAL;
2857 else if (p[1] == '~')
2858 type = EXPR_NOMATCH;
2859 break;
2860 case '>': if (p[1] != '=')
2861 {
2862 type = EXPR_GREATER;
2863 *len = 1;
2864 }
2865 else
2866 type = EXPR_GEQUAL;
2867 break;
2868 case '<': if (p[1] != '=')
2869 {
2870 type = EXPR_SMALLER;
2871 *len = 1;
2872 }
2873 else
2874 type = EXPR_SEQUAL;
2875 break;
2876 case 'i': if (p[1] == 's')
2877 {
2878 // "is" and "isnot"; but not a prefix of a name
2879 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2880 *len = 5;
2881 i = p[*len];
2882 if (!isalnum(i) && i != '_')
2883 {
2884 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2885 *type_is = TRUE;
2886 }
2887 }
2888 break;
2889 }
2890 return type;
2891}
2892
2893/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002895 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 */
2897 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002898compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002900 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901
2902 // this works from end to start
2903 while (p > start)
2904 {
2905 --p;
2906 if (*p == '-' || *p == '+')
2907 {
2908 int negate = *p == '-';
2909 isn_T *isn;
2910
2911 // TODO: check type
2912 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2913 {
2914 --p;
2915 if (*p == '-')
2916 negate = !negate;
2917 }
2918 // only '-' has an effect, for '+' we only check the type
2919 if (negate)
2920 isn = generate_instr(cctx, ISN_NEGATENR);
2921 else
2922 isn = generate_instr(cctx, ISN_CHECKNR);
2923 if (isn == NULL)
2924 return FAIL;
2925 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002926 else if (numeric_only)
2927 {
2928 ++p;
2929 break;
2930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 else
2932 {
2933 int invert = TRUE;
2934
2935 while (p > start && p[-1] == '!')
2936 {
2937 --p;
2938 invert = !invert;
2939 }
2940 if (generate_2BOOL(cctx, invert) == FAIL)
2941 return FAIL;
2942 }
2943 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002944 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 return OK;
2946}
2947
2948/*
2949 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002950 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 */
2952 static int
2953compile_subscript(
2954 char_u **arg,
2955 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002956 char_u *start_leader,
2957 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002958 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002960 char_u *name_start = *end_leader;
2961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 for (;;)
2963 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002964 char_u *p = skipwhite(*arg);
2965
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002966 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002967 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002968 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002969
2970 // If a following line starts with "->{" or "->X" advance to that
2971 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002972 // Also if a following line starts with ".x".
2973 if (next != NULL &&
2974 ((next[0] == '-' && next[1] == '>'
2975 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02002976 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002977 {
2978 next = next_line_from_context(cctx, TRUE);
2979 if (next == NULL)
2980 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002981 *arg = next;
2982 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002983 }
2984 }
2985
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002986 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
2987 // not a function call.
2988 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002990 garray_T *stack = &cctx->ctx_type_stack;
2991 type_T *type;
2992 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002994 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02002995 return FAIL;
2996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002998 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2999
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003000 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3002 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003003 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 return FAIL;
3005 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003006 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003008 char_u *pstart = p;
3009
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003010 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003011 return FAIL;
3012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 // something->method()
3014 // Apply the '!', '-' and '+' first:
3015 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003016 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003019 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003020 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003021 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 if (**arg == '{')
3023 {
3024 // lambda call: list->{lambda}
3025 if (compile_lambda_call(arg, cctx) == FAIL)
3026 return FAIL;
3027 }
3028 else
3029 {
3030 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003031 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003032 if (!eval_isnamec1(*p))
3033 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003034 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003035 return FAIL;
3036 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003037 if (ASCII_ISALPHA(*p) && p[1] == ':')
3038 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003039 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 ;
3041 if (*p != '(')
3042 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003043 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 return FAIL;
3045 }
3046 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003047 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 return FAIL;
3049 }
3050 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003051 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003053 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003054 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003055 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003056
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003057 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003058 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003059 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003060 // TODO: blob index
3061 // TODO: more arguments
3062 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003063 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003064 return FAIL;
3065
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003066 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003067 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003068 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003069 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003070 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 return FAIL;
3072
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003073 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3074 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 if (**arg != ']')
3076 {
3077 emsg(_(e_missbrac));
3078 return FAIL;
3079 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003080 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003082 // We can index a list and a dict. If we don't know the type
3083 // we can use the index value type.
3084 // TODO: If we don't know use an instruction to figure it out at
3085 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003086 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003087 vtype = (*typep)->tt_type;
3088 if (*typep == &t_any)
3089 {
3090 type_T *valtype = ((type_T **)stack->ga_data)
3091 [stack->ga_len - 1];
3092 if (valtype == &t_string)
3093 vtype = VAR_DICT;
3094 }
3095 if (vtype == VAR_DICT)
3096 {
3097 if ((*typep)->tt_type == VAR_DICT)
3098 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003099 else
3100 {
3101 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3102 return FAIL;
3103 *typep = &t_any;
3104 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003105 if (may_generate_2STRING(-1, cctx) == FAIL)
3106 return FAIL;
3107 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3108 return FAIL;
3109 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003110 else if (vtype == VAR_STRING)
3111 {
3112 *typep = &t_number;
3113 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3114 return FAIL;
3115 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003116 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003117 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003118 if ((*typep)->tt_type == VAR_LIST)
3119 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003120 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003121 return FAIL;
3122 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003123 else
3124 {
3125 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003126 return FAIL;
3127 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003129 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003131 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003132 return FAIL;
3133
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003134 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003135 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3136 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003138 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003139 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 while (eval_isnamec(*p))
3141 MB_PTR_ADV(p);
3142 if (p == *arg)
3143 {
3144 semsg(_(e_syntax_at), *arg);
3145 return FAIL;
3146 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003147 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 return FAIL;
3149 *arg = p;
3150 }
3151 else
3152 break;
3153 }
3154
3155 // TODO - see handle_subscript():
3156 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3157 // Don't do this when "Func" is already a partial that was bound
3158 // explicitly (pt_auto is FALSE).
3159
3160 return OK;
3161}
3162
3163/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003164 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3165 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003167 * If the value is a constant "ppconst->pp_ret" will be set.
3168 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003169 *
3170 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 */
3172
3173/*
3174 * number number constant
3175 * 0zFFFFFFFF Blob constant
3176 * "string" string constant
3177 * 'string' literal string constant
3178 * &option-name option value
3179 * @r register contents
3180 * identifier variable value
3181 * function() function call
3182 * $VAR environment variable
3183 * (expression) nested expression
3184 * [expr, expr] List
3185 * {key: val, key: val} Dictionary
3186 * #{key: val, key: val} Dictionary with literal keys
3187 *
3188 * Also handle:
3189 * ! in front logical NOT
3190 * - in front unary minus
3191 * + in front unary plus (ignored)
3192 * trailing (arg) funcref/partial call
3193 * trailing [] subscript in String or List
3194 * trailing .name entry in Dictionary
3195 * trailing ->name() method call
3196 */
3197 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003198compile_expr7(
3199 char_u **arg,
3200 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003201 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 char_u *start_leader, *end_leader;
3204 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003205 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003206 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207
3208 /*
3209 * Skip '!', '-' and '+' characters. They are handled later.
3210 */
3211 start_leader = *arg;
3212 while (**arg == '!' || **arg == '-' || **arg == '+')
3213 *arg = skipwhite(*arg + 1);
3214 end_leader = *arg;
3215
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003216 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 switch (**arg)
3218 {
3219 /*
3220 * Number constant.
3221 */
3222 case '0': // also for blob starting with 0z
3223 case '1':
3224 case '2':
3225 case '3':
3226 case '4':
3227 case '5':
3228 case '6':
3229 case '7':
3230 case '8':
3231 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003232 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003234 // Apply "-" and "+" just before the number now, right to
3235 // left. Matters especially when "->" follows. Stops at
3236 // '!'.
3237 if (apply_leader(rettv, TRUE,
3238 start_leader, &end_leader) == FAIL)
3239 {
3240 clear_tv(rettv);
3241 return FAIL;
3242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 break;
3244
3245 /*
3246 * String constant: "string".
3247 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003248 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 return FAIL;
3250 break;
3251
3252 /*
3253 * Literal string constant: 'str''ing'.
3254 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003255 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 return FAIL;
3257 break;
3258
3259 /*
3260 * Constant Vim variable.
3261 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003262 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 ret = NOTDONE;
3264 break;
3265
3266 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003267 * "true" constant
3268 */
3269 case 't': if (STRNCMP(*arg, "true", 4) == 0
3270 && !eval_isnamec((*arg)[4]))
3271 {
3272 *arg += 4;
3273 rettv->v_type = VAR_BOOL;
3274 rettv->vval.v_number = VVAL_TRUE;
3275 }
3276 else
3277 ret = NOTDONE;
3278 break;
3279
3280 /*
3281 * "false" constant
3282 */
3283 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3284 && !eval_isnamec((*arg)[5]))
3285 {
3286 *arg += 5;
3287 rettv->v_type = VAR_BOOL;
3288 rettv->vval.v_number = VVAL_FALSE;
3289 }
3290 else
3291 ret = NOTDONE;
3292 break;
3293
3294 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 * List: [expr, expr]
3296 */
3297 case '[': ret = compile_list(arg, cctx);
3298 break;
3299
3300 /*
3301 * Dictionary: #{key: val, key: val}
3302 */
3303 case '#': if ((*arg)[1] == '{')
3304 {
3305 ++*arg;
3306 ret = compile_dict(arg, cctx, TRUE);
3307 }
3308 else
3309 ret = NOTDONE;
3310 break;
3311
3312 /*
3313 * Lambda: {arg, arg -> expr}
3314 * Dictionary: {'key': val, 'key': val}
3315 */
3316 case '{': {
3317 char_u *start = skipwhite(*arg + 1);
3318
3319 // Find out what comes after the arguments.
3320 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003321 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 if (ret != FAIL && *start == '>')
3323 ret = compile_lambda(arg, cctx);
3324 else
3325 ret = compile_dict(arg, cctx, FALSE);
3326 }
3327 break;
3328
3329 /*
3330 * Option value: &name
3331 */
3332 case '&': ret = compile_get_option(arg, cctx);
3333 break;
3334
3335 /*
3336 * Environment variable: $VAR.
3337 */
3338 case '$': ret = compile_get_env(arg, cctx);
3339 break;
3340
3341 /*
3342 * Register contents: @r.
3343 */
3344 case '@': ret = compile_get_register(arg, cctx);
3345 break;
3346 /*
3347 * nested expression: (expression).
3348 */
3349 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003350
3351 // recursive!
3352 if (ppconst->pp_used <= PPSIZE - 10)
3353 {
3354 ret = compile_expr1(arg, cctx, ppconst);
3355 }
3356 else
3357 {
3358 // Not enough space in ppconst, flush constants.
3359 if (generate_ppconst(cctx, ppconst) == FAIL)
3360 return FAIL;
3361 ret = compile_expr0(arg, cctx);
3362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 *arg = skipwhite(*arg);
3364 if (**arg == ')')
3365 ++*arg;
3366 else if (ret == OK)
3367 {
3368 emsg(_(e_missing_close));
3369 ret = FAIL;
3370 }
3371 break;
3372
3373 default: ret = NOTDONE;
3374 break;
3375 }
3376 if (ret == FAIL)
3377 return FAIL;
3378
Bram Moolenaar1c747212020-05-09 18:28:34 +02003379 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003381 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003382 clear_tv(rettv);
3383 else
3384 // A constant expression can possibly be handled compile time,
3385 // return the value instead of generating code.
3386 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 }
3388 else if (ret == NOTDONE)
3389 {
3390 char_u *p;
3391 int r;
3392
3393 if (!eval_isnamec1(**arg))
3394 {
3395 semsg(_("E1015: Name expected: %s"), *arg);
3396 return FAIL;
3397 }
3398
3399 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003400 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003402 {
3403 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3404 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003406 {
3407 if (generate_ppconst(cctx, ppconst) == FAIL)
3408 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003410 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 if (r == FAIL)
3412 return FAIL;
3413 }
3414
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003415 // Handle following "[]", ".member", etc.
3416 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003417 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003418 ppconst) == FAIL)
3419 return FAIL;
3420 if (ppconst->pp_used > 0)
3421 {
3422 // apply the '!', '-' and '+' before the constant
3423 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003424 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003425 return FAIL;
3426 return OK;
3427 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003428 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003430 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431}
3432
3433/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003434 * Give the "white on both sides" error, taking the operator from "p[len]".
3435 */
3436 void
3437error_white_both(char_u *op, int len)
3438{
3439 char_u buf[10];
3440
3441 vim_strncpy(buf, op, len);
3442 semsg(_(e_white_both), buf);
3443}
3444
3445/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003446 * <type>expr7: runtime type check / conversion
3447 */
3448 static int
3449compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3450{
3451 type_T *want_type = NULL;
3452
3453 // Recognize <type>
3454 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3455 {
3456 int called_emsg_before = called_emsg;
3457
3458 ++*arg;
3459 want_type = parse_type(arg, cctx->ctx_type_list);
3460 if (called_emsg != called_emsg_before)
3461 return FAIL;
3462
3463 if (**arg != '>')
3464 {
3465 if (*skipwhite(*arg) == '>')
3466 semsg(_(e_no_white_before), ">");
3467 else
3468 emsg(_("E1104: Missing >"));
3469 return FAIL;
3470 }
3471 ++*arg;
3472 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3473 return FAIL;
3474 }
3475
3476 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3477 return FAIL;
3478
3479 if (want_type != NULL)
3480 {
3481 garray_T *stack = &cctx->ctx_type_stack;
3482 type_T *actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3483
3484 if (check_type(want_type, actual, FALSE) == FAIL)
3485 {
3486 generate_ppconst(cctx, ppconst);
3487 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3488 return FAIL;
3489 }
3490 }
3491
3492 return OK;
3493}
3494
3495/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 * * number multiplication
3497 * / number division
3498 * % number modulo
3499 */
3500 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003501compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502{
3503 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003504 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003505 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003507 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003508 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 return FAIL;
3510
3511 /*
3512 * Repeat computing, until no "*", "/" or "%" is following.
3513 */
3514 for (;;)
3515 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003516 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 if (*op != '*' && *op != '/' && *op != '%')
3518 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003519 if (next != NULL)
3520 {
3521 *arg = next_line_from_context(cctx, TRUE);
3522 op = skipwhite(*arg);
3523 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003524
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003525 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003527 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003528 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 }
3530 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003531 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003532 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003534 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003535 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003537
3538 if (ppconst->pp_used == ppconst_used + 2
3539 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3540 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003541 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003542 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3543 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003544 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003546 // both are numbers: compute the result
3547 switch (*op)
3548 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003549 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003550 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003551 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003552 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003553 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003554 break;
3555 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003556 tv1->vval.v_number = res;
3557 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003558 }
3559 else
3560 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003561 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003562 generate_two_op(cctx, op);
3563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 }
3565
3566 return OK;
3567}
3568
3569/*
3570 * + number addition
3571 * - number subtraction
3572 * .. string concatenation
3573 */
3574 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003575compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576{
3577 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003578 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003580 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581
3582 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003583 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 return FAIL;
3585
3586 /*
3587 * Repeat computing, until no "+", "-" or ".." is following.
3588 */
3589 for (;;)
3590 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003591 op = may_peek_next_line(cctx, *arg, &next);
3592 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 break;
3594 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003595 if (next != NULL)
3596 {
3597 *arg = next_line_from_context(cctx, TRUE);
3598 op = skipwhite(*arg);
3599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003601 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003603 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003604 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 }
3606
3607 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003608 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003609 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003611 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003612 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 return FAIL;
3614
Bram Moolenaara5565e42020-05-09 15:44:01 +02003615 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003616 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003617 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3618 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3619 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3620 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003622 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3623 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003624
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003625 // concat/subtract/add constant numbers
3626 if (*op == '+')
3627 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3628 else if (*op == '-')
3629 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3630 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003631 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003632 // concatenate constant strings
3633 char_u *s1 = tv1->vval.v_string;
3634 char_u *s2 = tv2->vval.v_string;
3635 size_t len1 = STRLEN(s1);
3636
3637 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3638 if (tv1->vval.v_string == NULL)
3639 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003640 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003641 return FAIL;
3642 }
3643 mch_memmove(tv1->vval.v_string, s1, len1);
3644 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003645 vim_free(s1);
3646 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003647 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003648 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 }
3650 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003651 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003652 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003653 if (*op == '.')
3654 {
3655 if (may_generate_2STRING(-2, cctx) == FAIL
3656 || may_generate_2STRING(-1, cctx) == FAIL)
3657 return FAIL;
3658 generate_instr_drop(cctx, ISN_CONCAT, 1);
3659 }
3660 else
3661 generate_two_op(cctx, op);
3662 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 }
3664
3665 return OK;
3666}
3667
3668/*
3669 * expr5a == expr5b
3670 * expr5a =~ expr5b
3671 * expr5a != expr5b
3672 * expr5a !~ expr5b
3673 * expr5a > expr5b
3674 * expr5a >= expr5b
3675 * expr5a < expr5b
3676 * expr5a <= expr5b
3677 * expr5a is expr5b
3678 * expr5a isnot expr5b
3679 *
3680 * Produces instructions:
3681 * EVAL expr5a Push result of "expr5a"
3682 * EVAL expr5b Push result of "expr5b"
3683 * COMPARE one of the compare instructions
3684 */
3685 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003686compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687{
3688 exptype_T type = EXPR_UNKNOWN;
3689 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003690 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003693 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694
3695 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003696 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 return FAIL;
3698
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003699 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003700 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701
3702 /*
3703 * If there is a comparative operator, use it.
3704 */
3705 if (type != EXPR_UNKNOWN)
3706 {
3707 int ic = FALSE; // Default: do not ignore case
3708
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003709 if (next != NULL)
3710 {
3711 *arg = next_line_from_context(cctx, TRUE);
3712 p = skipwhite(*arg);
3713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 if (type_is && (p[len] == '?' || p[len] == '#'))
3715 {
3716 semsg(_(e_invexpr2), *arg);
3717 return FAIL;
3718 }
3719 // extra question mark appended: ignore case
3720 if (p[len] == '?')
3721 {
3722 ic = TRUE;
3723 ++len;
3724 }
3725 // extra '#' appended: match case (ignored)
3726 else if (p[len] == '#')
3727 ++len;
3728 // nothing appended: match case
3729
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003730 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003732 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003733 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 }
3735
3736 // get the second variable
3737 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003738 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003739 return FAIL;
3740
Bram Moolenaara5565e42020-05-09 15:44:01 +02003741 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 return FAIL;
3743
Bram Moolenaara5565e42020-05-09 15:44:01 +02003744 if (ppconst->pp_used == ppconst_used + 2)
3745 {
3746 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3747 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3748 int ret;
3749
3750 // Both sides are a constant, compute the result now.
3751 // First check for a valid combination of types, this is more
3752 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003753 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003754 ret = FAIL;
3755 else
3756 {
3757 ret = typval_compare(tv1, tv2, type, ic);
3758 tv1->v_type = VAR_BOOL;
3759 tv1->vval.v_number = tv1->vval.v_number
3760 ? VVAL_TRUE : VVAL_FALSE;
3761 clear_tv(tv2);
3762 --ppconst->pp_used;
3763 }
3764 return ret;
3765 }
3766
3767 generate_ppconst(cctx, ppconst);
3768 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 }
3770
3771 return OK;
3772}
3773
Bram Moolenaar7f141552020-05-09 17:35:53 +02003774static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776/*
3777 * Compile || or &&.
3778 */
3779 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003780compile_and_or(
3781 char_u **arg,
3782 cctx_T *cctx,
3783 char *op,
3784 ppconst_T *ppconst,
3785 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003787 char_u *next;
3788 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 int opchar = *op;
3790
3791 if (p[0] == opchar && p[1] == opchar)
3792 {
3793 garray_T *instr = &cctx->ctx_instr;
3794 garray_T end_ga;
3795
3796 /*
3797 * Repeat until there is no following "||" or "&&"
3798 */
3799 ga_init2(&end_ga, sizeof(int), 10);
3800 while (p[0] == opchar && p[1] == opchar)
3801 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003802 if (next != NULL)
3803 {
3804 *arg = next_line_from_context(cctx, TRUE);
3805 p = skipwhite(*arg);
3806 }
3807
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003808 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3809 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003811 return FAIL;
3812 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813
Bram Moolenaara5565e42020-05-09 15:44:01 +02003814 // TODO: use ppconst if the value is a constant
3815 generate_ppconst(cctx, ppconst);
3816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 if (ga_grow(&end_ga, 1) == FAIL)
3818 {
3819 ga_clear(&end_ga);
3820 return FAIL;
3821 }
3822 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3823 ++end_ga.ga_len;
3824 generate_JUMP(cctx, opchar == '|'
3825 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3826
3827 // eval the next expression
3828 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003829 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003830 return FAIL;
3831
Bram Moolenaara5565e42020-05-09 15:44:01 +02003832 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3833 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 {
3835 ga_clear(&end_ga);
3836 return FAIL;
3837 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003838
3839 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003841 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842
3843 // Fill in the end label in all jumps.
3844 while (end_ga.ga_len > 0)
3845 {
3846 isn_T *isn;
3847
3848 --end_ga.ga_len;
3849 isn = ((isn_T *)instr->ga_data)
3850 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3851 isn->isn_arg.jump.jump_where = instr->ga_len;
3852 }
3853 ga_clear(&end_ga);
3854 }
3855
3856 return OK;
3857}
3858
3859/*
3860 * expr4a && expr4a && expr4a logical AND
3861 *
3862 * Produces instructions:
3863 * EVAL expr4a Push result of "expr4a"
3864 * JUMP_AND_KEEP_IF_FALSE end
3865 * EVAL expr4b Push result of "expr4b"
3866 * JUMP_AND_KEEP_IF_FALSE end
3867 * EVAL expr4c Push result of "expr4c"
3868 * end:
3869 */
3870 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003871compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003873 int ppconst_used = ppconst->pp_used;
3874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003876 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 return FAIL;
3878
3879 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003880 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881}
3882
3883/*
3884 * expr3a || expr3b || expr3c logical OR
3885 *
3886 * Produces instructions:
3887 * EVAL expr3a Push result of "expr3a"
3888 * JUMP_AND_KEEP_IF_TRUE end
3889 * EVAL expr3b Push result of "expr3b"
3890 * JUMP_AND_KEEP_IF_TRUE end
3891 * EVAL expr3c Push result of "expr3c"
3892 * end:
3893 */
3894 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003895compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003897 int ppconst_used = ppconst->pp_used;
3898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003900 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 return FAIL;
3902
3903 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003904 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905}
3906
3907/*
3908 * Toplevel expression: expr2 ? expr1a : expr1b
3909 *
3910 * Produces instructions:
3911 * EVAL expr2 Push result of "expr"
3912 * JUMP_IF_FALSE alt jump if false
3913 * EVAL expr1a
3914 * JUMP_ALWAYS end
3915 * alt: EVAL expr1b
3916 * end:
3917 */
3918 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003919compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920{
3921 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003922 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003923 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924
Bram Moolenaar61a89812020-05-07 16:58:17 +02003925 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02003926 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 return FAIL;
3928
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003929 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 if (*p == '?')
3931 {
3932 garray_T *instr = &cctx->ctx_instr;
3933 garray_T *stack = &cctx->ctx_type_stack;
3934 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003935 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003937 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003939 int has_const_expr = FALSE;
3940 int const_value = FALSE;
3941 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003943 if (next != NULL)
3944 {
3945 *arg = next_line_from_context(cctx, TRUE);
3946 p = skipwhite(*arg);
3947 }
3948
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003949 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3950 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003952 return FAIL;
3953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954
Bram Moolenaara5565e42020-05-09 15:44:01 +02003955 if (ppconst->pp_used == ppconst_used + 1)
3956 {
3957 // the condition is a constant, we know whether the ? or the :
3958 // expression is to be evaluated.
3959 has_const_expr = TRUE;
3960 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3961 clear_tv(&ppconst->pp_tv[ppconst_used]);
3962 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003963 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
3964 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003965 }
3966 else
3967 {
3968 generate_ppconst(cctx, ppconst);
3969 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3970 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971
3972 // evaluate the second expression; any type is accepted
3973 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003974 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003975 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003976 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01003977 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978
Bram Moolenaara5565e42020-05-09 15:44:01 +02003979 if (!has_const_expr)
3980 {
3981 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982
Bram Moolenaara5565e42020-05-09 15:44:01 +02003983 // remember the type and drop it
3984 --stack->ga_len;
3985 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986
Bram Moolenaara5565e42020-05-09 15:44:01 +02003987 end_idx = instr->ga_len;
3988 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3989
3990 // jump here from JUMP_IF_FALSE
3991 isn = ((isn_T *)instr->ga_data) + alt_idx;
3992 isn->isn_arg.jump.jump_where = instr->ga_len;
3993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994
3995 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003996 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997 if (*p != ':')
3998 {
3999 emsg(_(e_missing_colon));
4000 return FAIL;
4001 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004002 if (next != NULL)
4003 {
4004 *arg = next_line_from_context(cctx, TRUE);
4005 p = skipwhite(*arg);
4006 }
4007
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004008 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4009 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004011 return FAIL;
4012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013
4014 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004015 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004016 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4017 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004019 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004020 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004021 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004022 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023
Bram Moolenaara5565e42020-05-09 15:44:01 +02004024 if (!has_const_expr)
4025 {
4026 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027
Bram Moolenaara5565e42020-05-09 15:44:01 +02004028 // If the types differ, the result has a more generic type.
4029 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4030 common_type(type1, type2, &type2, cctx->ctx_type_list);
4031
4032 // jump here from JUMP_ALWAYS
4033 isn = ((isn_T *)instr->ga_data) + end_idx;
4034 isn->isn_arg.jump.jump_where = instr->ga_len;
4035 }
4036
4037 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 }
4039 return OK;
4040}
4041
4042/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004043 * Toplevel expression.
4044 */
4045 static int
4046compile_expr0(char_u **arg, cctx_T *cctx)
4047{
4048 ppconst_T ppconst;
4049
4050 CLEAR_FIELD(ppconst);
4051 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4052 {
4053 clear_ppconst(&ppconst);
4054 return FAIL;
4055 }
4056 if (generate_ppconst(cctx, &ppconst) == FAIL)
4057 return FAIL;
4058 return OK;
4059}
4060
4061/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 * compile "return [expr]"
4063 */
4064 static char_u *
4065compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4066{
4067 char_u *p = arg;
4068 garray_T *stack = &cctx->ctx_type_stack;
4069 type_T *stack_type;
4070
4071 if (*p != NUL && *p != '|' && *p != '\n')
4072 {
4073 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004074 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 return NULL;
4076
4077 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4078 if (set_return_type)
4079 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004080 else
4081 {
4082 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4083 && stack_type->tt_type != VAR_VOID
4084 && stack_type->tt_type != VAR_UNKNOWN)
4085 {
4086 emsg(_("E1096: Returning a value in a function without a return type"));
4087 return NULL;
4088 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004089 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4090 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004092 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 }
4094 else
4095 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004096 // "set_return_type" cannot be TRUE, only used for a lambda which
4097 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004098 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4099 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 {
4101 emsg(_("E1003: Missing return value"));
4102 return NULL;
4103 }
4104
4105 // No argument, return zero.
4106 generate_PUSHNR(cctx, 0);
4107 }
4108
4109 if (generate_instr(cctx, ISN_RETURN) == NULL)
4110 return NULL;
4111
4112 // "return val | endif" is possible
4113 return skipwhite(p);
4114}
4115
4116/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004117 * Get a line from the compilation context, compatible with exarg_T getline().
4118 * Return a pointer to the line in allocated memory.
4119 * Return NULL for end-of-file or some error.
4120 */
4121 static char_u *
4122exarg_getline(
4123 int c UNUSED,
4124 void *cookie,
4125 int indent UNUSED,
4126 int do_concat UNUSED)
4127{
4128 cctx_T *cctx = (cctx_T *)cookie;
4129
4130 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4131 {
4132 iemsg("Heredoc got to end");
4133 return NULL;
4134 }
4135 ++cctx->ctx_lnum;
4136 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4137 [cctx->ctx_lnum]);
4138}
4139
4140/*
4141 * Compile a nested :def command.
4142 */
4143 static char_u *
4144compile_nested_function(exarg_T *eap, cctx_T *cctx)
4145{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004146 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004147 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004148 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004149 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004150 lvar_T *lvar;
4151 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004152 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004153
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004154 // Only g:Func() can use a namespace.
4155 if (name_start[1] == ':' && !is_global)
4156 {
4157 semsg(_(e_namespace), name_start);
4158 return NULL;
4159 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004160 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4161 return NULL;
4162
Bram Moolenaar04b12692020-05-04 23:24:44 +02004163 eap->arg = name_end;
4164 eap->getline = exarg_getline;
4165 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004166 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004167 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004168 lambda_name = get_lambda_name();
4169 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004170
Bram Moolenaar822ba242020-05-24 23:00:18 +02004171 if (ufunc == NULL)
4172 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004173 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004174 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004175 return NULL;
4176
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004177 if (is_global)
4178 {
4179 char_u *func_name = vim_strnsave(name_start + 2,
4180 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004181
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004182 if (func_name == NULL)
4183 r = FAIL;
4184 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004185 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004186 }
4187 else
4188 {
4189 // Define a local variable for the function reference.
4190 lvar = reserve_local(cctx, name_start, name_end - name_start,
4191 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004192 if (lvar == NULL)
4193 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004194 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004195 return NULL;
4196 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4197 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004198
Bram Moolenaar61a89812020-05-07 16:58:17 +02004199 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004200 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004201}
4202
4203/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 * Return the length of an assignment operator, or zero if there isn't one.
4205 */
4206 int
4207assignment_len(char_u *p, int *heredoc)
4208{
4209 if (*p == '=')
4210 {
4211 if (p[1] == '<' && p[2] == '<')
4212 {
4213 *heredoc = TRUE;
4214 return 3;
4215 }
4216 return 1;
4217 }
4218 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4219 return 2;
4220 if (STRNCMP(p, "..=", 3) == 0)
4221 return 3;
4222 return 0;
4223}
4224
4225// words that cannot be used as a variable
4226static char *reserved[] = {
4227 "true",
4228 "false",
4229 NULL
4230};
4231
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004232typedef enum {
4233 dest_local,
4234 dest_option,
4235 dest_env,
4236 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004237 dest_buffer,
4238 dest_window,
4239 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004240 dest_vimvar,
4241 dest_script,
4242 dest_reg,
4243} assign_dest_T;
4244
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004246 * Generate the load instruction for "name".
4247 */
4248 static void
4249generate_loadvar(
4250 cctx_T *cctx,
4251 assign_dest_T dest,
4252 char_u *name,
4253 lvar_T *lvar,
4254 type_T *type)
4255{
4256 switch (dest)
4257 {
4258 case dest_option:
4259 // TODO: check the option exists
4260 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4261 break;
4262 case dest_global:
4263 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4264 break;
4265 case dest_buffer:
4266 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4267 break;
4268 case dest_window:
4269 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4270 break;
4271 case dest_tab:
4272 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4273 break;
4274 case dest_script:
4275 compile_load_scriptvar(cctx,
4276 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4277 break;
4278 case dest_env:
4279 // Include $ in the name here
4280 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4281 break;
4282 case dest_reg:
4283 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4284 break;
4285 case dest_vimvar:
4286 generate_LOADV(cctx, name + 2, TRUE);
4287 break;
4288 case dest_local:
4289 if (lvar->lv_from_outer)
4290 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4291 NULL, type);
4292 else
4293 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4294 break;
4295 }
4296}
4297
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004298 void
4299vim9_declare_error(char_u *name)
4300{
4301 char *scope = "";
4302
4303 switch (*name)
4304 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004305 case 'g': scope = _("global"); break;
4306 case 'b': scope = _("buffer"); break;
4307 case 'w': scope = _("window"); break;
4308 case 't': scope = _("tab"); break;
4309 case 'v': scope = "v:"; break;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004310 case '$': semsg(_(e_declare_env_var), name);
4311 return;
4312 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
4313 return;
4314 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
4315 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004316 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004317 }
4318 semsg(_(e_declare_var), scope, name);
4319}
4320
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004321/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004322 * Compile declaration and assignment:
4323 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004325 * Return NULL for an error.
4326 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 */
4328 static char_u *
4329compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4330{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004331 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004333 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 char_u *ret = NULL;
4335 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004336 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004337 int scriptvar_sid = 0;
4338 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004341 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 int oplen = 0;
4344 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004345 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004346 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004347 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004351 // Skip over the "var" or "[var, var]" to get to any "=".
4352 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4353 if (p == NULL)
4354 return *arg == '[' ? arg : NULL;
4355
4356 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004358 // TODO: should we allow this, and figure out type inference from list
4359 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004360 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 return NULL;
4362 }
4363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 sp = p;
4365 p = skipwhite(p);
4366 op = p;
4367 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004368
4369 if (var_count > 0 && oplen == 0)
4370 // can be something like "[1, 2]->func()"
4371 return arg;
4372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4374 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004375 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004376 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004377 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 if (heredoc)
4380 {
4381 list_T *l;
4382 listitem_T *li;
4383
4384 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004385 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004387 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388
4389 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004390 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 {
4392 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4393 li->li_tv.vval.v_string = NULL;
4394 }
4395 generate_NEWLIST(cctx, l->lv_len);
4396 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004397 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 list_free(l);
4399 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004400 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004402 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004404 // for "[var, var] = expr" evaluate the expression here, loop over the
4405 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004406
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004407 p = skipwhite(op + oplen);
4408 if (compile_expr0(&p, cctx) == FAIL)
4409 return NULL;
4410 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004412 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004414 type_T *stacktype;
4415
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004416 stacktype = stack->ga_len == 0 ? &t_void
4417 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004418 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004420 emsg(_(e_cannot_use_void));
4421 goto theend;
4422 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004423 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004424 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004425 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004426 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4427 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004428 }
4429 }
4430
4431 /*
4432 * Loop over variables in "[var, var] = expr".
4433 * For "var = expr" and "let var: type" this is done only once.
4434 */
4435 if (var_count > 0)
4436 var_start = skipwhite(arg + 1); // skip over the "["
4437 else
4438 var_start = arg;
4439 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4440 {
4441 char_u *var_end = skip_var_one(var_start, FALSE);
4442 size_t varlen;
4443 int new_local = FALSE;
4444 int opt_type;
4445 int opt_flags = 0;
4446 assign_dest_T dest = dest_local;
4447 int vimvaridx = -1;
4448 lvar_T *lvar = NULL;
4449 lvar_T arg_lvar;
4450 int has_type = FALSE;
4451 int has_index = FALSE;
4452 int instr_count = -1;
4453
Bram Moolenaar65821722020-08-02 18:58:54 +02004454 if (*var_start == '@')
4455 p = var_start + 2;
4456 else
4457 {
4458 p = (*var_start == '&' || *var_start == '$')
4459 ? var_start + 1 : var_start;
4460 p = to_name_end(p, TRUE);
4461 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004462
4463 // "a: type" is declaring variable "a" with a type, not "a:".
4464 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4465 --var_end;
4466 if (is_decl && p == var_start + 2 && p[-1] == ':')
4467 --p;
4468
4469 varlen = p - var_start;
4470 vim_free(name);
4471 name = vim_strnsave(var_start, varlen);
4472 if (name == NULL)
4473 return NULL;
4474 if (!heredoc)
4475 type = &t_any;
4476
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004477 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004478 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004479 int declare_error = FALSE;
4480
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004481 if (*var_start == '&')
4482 {
4483 int cc;
4484 long numval;
4485
4486 dest = dest_option;
4487 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004489 emsg(_(e_const_option));
4490 goto theend;
4491 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004492 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004493 p = var_start;
4494 p = find_option_end(&p, &opt_flags);
4495 if (p == NULL)
4496 {
4497 // cannot happen?
4498 emsg(_(e_letunexp));
4499 goto theend;
4500 }
4501 cc = *p;
4502 *p = NUL;
4503 opt_type = get_option_value(var_start + 1, &numval,
4504 NULL, opt_flags);
4505 *p = cc;
4506 if (opt_type == -3)
4507 {
4508 semsg(_(e_unknown_option), var_start);
4509 goto theend;
4510 }
4511 if (opt_type == -2 || opt_type == 0)
4512 type = &t_string;
4513 else
4514 type = &t_number; // both number and boolean option
4515 }
4516 else if (*var_start == '$')
4517 {
4518 dest = dest_env;
4519 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004520 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004521 }
4522 else if (*var_start == '@')
4523 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004524 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004525 {
4526 emsg_invreg(var_start[1]);
4527 goto theend;
4528 }
4529 dest = dest_reg;
4530 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004531 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004532 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004533 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004534 {
4535 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004536 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004537 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004538 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004539 {
4540 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004541 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004542 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004543 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004544 {
4545 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004546 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004547 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004548 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004549 {
4550 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004551 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004552 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004553 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004554 {
4555 typval_T *vtv;
4556 int di_flags;
4557
4558 vimvaridx = find_vim_var(name + 2, &di_flags);
4559 if (vimvaridx < 0)
4560 {
4561 semsg(_(e_var_notfound), var_start);
4562 goto theend;
4563 }
4564 // We use the current value of "sandbox" here, is that OK?
4565 if (var_check_ro(di_flags, name, FALSE))
4566 goto theend;
4567 dest = dest_vimvar;
4568 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004569 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004570 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004571 }
4572 else
4573 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004574 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004575
4576 for (idx = 0; reserved[idx] != NULL; ++idx)
4577 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004578 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004579 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004580 goto theend;
4581 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004582
4583 lvar = lookup_local(var_start, varlen, cctx);
4584 if (lvar == NULL)
4585 {
4586 CLEAR_FIELD(arg_lvar);
4587 if (lookup_arg(var_start, varlen,
4588 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4589 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004590 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004591 if (is_decl)
4592 {
4593 semsg(_(e_used_as_arg), name);
4594 goto theend;
4595 }
4596 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004597 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004598 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004599 if (lvar != NULL)
4600 {
4601 if (is_decl)
4602 {
4603 semsg(_("E1017: Variable already declared: %s"), name);
4604 goto theend;
4605 }
4606 else if (lvar->lv_const)
4607 {
4608 semsg(_("E1018: Cannot assign to a constant: %s"),
4609 name);
4610 goto theend;
4611 }
4612 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004613 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004614 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004615 int script_namespace = varlen > 1
4616 && STRNCMP(var_start, "s:", 2) == 0;
4617 int script_var = (script_namespace
4618 ? lookup_script(var_start + 2, varlen - 2)
4619 : lookup_script(var_start, varlen)) == OK;
4620 imported_T *import =
4621 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004622
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004623 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004624 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004625 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4626
4627 if (is_decl)
4628 {
4629 if (script_namespace)
4630 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004631 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004632 else
4633 semsg(_("E1054: Variable already declared in the script: %s"),
4634 name);
4635 goto theend;
4636 }
4637 else if (cctx->ctx_ufunc->uf_script_ctx_version
4638 == SCRIPT_VERSION_VIM9
4639 && script_namespace
4640 && !script_var && import == NULL)
4641 {
4642 semsg(_(e_unknown_var), name);
4643 goto theend;
4644 }
4645
4646 dest = dest_script;
4647
4648 // existing script-local variables should have a type
4649 scriptvar_sid = current_sctx.sc_sid;
4650 if (import != NULL)
4651 scriptvar_sid = import->imp_sid;
4652 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4653 rawname, TRUE);
4654 if (scriptvar_idx >= 0)
4655 {
4656 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4657 svar_T *sv =
4658 ((svar_T *)si->sn_var_vals.ga_data)
4659 + scriptvar_idx;
4660 type = sv->sv_type;
4661 }
4662 }
4663 else if (name[1] == ':' && name[2] != NUL)
4664 {
4665 semsg(_("E1082: Cannot use a namespaced variable: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004666 name);
4667 goto theend;
4668 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004669 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004670 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004671 semsg(_(e_unknown_var), name);
4672 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004673 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004674 else if (check_defined(var_start, varlen, cctx) == FAIL)
4675 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004676 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004677 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004678
4679 if (declare_error)
4680 {
4681 vim9_declare_error(name);
4682 goto theend;
4683 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004684 }
4685
4686 // handle "a:name" as a name, not index "name" on "a"
4687 if (varlen > 1 || var_start[varlen] != ':')
4688 p = var_end;
4689
4690 if (dest != dest_option)
4691 {
4692 if (is_decl && *p == ':')
4693 {
4694 // parse optional type: "let var: type = expr"
4695 if (!VIM_ISWHITE(p[1]))
4696 {
4697 semsg(_(e_white_after), ":");
4698 goto theend;
4699 }
4700 p = skipwhite(p + 1);
4701 type = parse_type(&p, cctx->ctx_type_list);
4702 has_type = TRUE;
4703 }
4704 else if (lvar != NULL)
4705 type = lvar->lv_type;
4706 }
4707
4708 if (oplen == 3 && !heredoc && dest != dest_global
4709 && type->tt_type != VAR_STRING
4710 && type->tt_type != VAR_ANY)
4711 {
4712 emsg(_("E1019: Can only concatenate to string"));
4713 goto theend;
4714 }
4715
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004716 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004717 {
4718 if (oplen > 1 && !heredoc)
4719 {
4720 // +=, /=, etc. require an existing variable
4721 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4722 name);
4723 goto theend;
4724 }
4725
4726 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004727 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4728 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004729 goto theend;
4730 lvar = reserve_local(cctx, var_start, varlen,
4731 cmdidx == CMD_const, type);
4732 if (lvar == NULL)
4733 goto theend;
4734 new_local = TRUE;
4735 }
4736
4737 member_type = type;
4738 if (var_end > var_start + varlen)
4739 {
4740 // Something follows after the variable: "var[idx]".
4741 if (is_decl)
4742 {
4743 emsg(_("E1087: cannot use an index when declaring a variable"));
4744 goto theend;
4745 }
4746
4747 if (var_start[varlen] == '[')
4748 {
4749 has_index = TRUE;
4750 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004751 member_type = &t_any;
4752 else
4753 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004754 }
4755 else
4756 {
4757 semsg("Not supported yet: %s", var_start);
4758 goto theend;
4759 }
4760 }
4761 else if (lvar == &arg_lvar)
4762 {
4763 semsg(_("E1090: Cannot assign to argument %s"), name);
4764 goto theend;
4765 }
4766
4767 if (!heredoc)
4768 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004769 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004770 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004771 if (oplen > 0 && var_count == 0)
4772 {
4773 // skip over the "=" and the expression
4774 p = skipwhite(op + oplen);
4775 compile_expr0(&p, cctx);
4776 }
4777 }
4778 else if (oplen > 0)
4779 {
4780 type_T *stacktype;
4781
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004782 // For "var = expr" evaluate the expression.
4783 if (var_count == 0)
4784 {
4785 int r;
4786
4787 // for "+=", "*=", "..=" etc. first load the current value
4788 if (*op != '=')
4789 {
4790 generate_loadvar(cctx, dest, name, lvar, type);
4791
4792 if (has_index)
4793 {
4794 // TODO: get member from list or dict
4795 emsg("Index with operation not supported yet");
4796 goto theend;
4797 }
4798 }
4799
4800 // Compile the expression. Temporarily hide the new local
4801 // variable here, it is not available to this expression.
4802 if (new_local)
4803 --cctx->ctx_locals.ga_len;
4804 instr_count = instr->ga_len;
4805 p = skipwhite(op + oplen);
4806 r = compile_expr0(&p, cctx);
4807 if (new_local)
4808 ++cctx->ctx_locals.ga_len;
4809 if (r == FAIL)
4810 goto theend;
4811 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004812 else if (semicolon && var_idx == var_count - 1)
4813 {
4814 // For "[var; var] = expr" get the rest of the list
4815 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4816 goto theend;
4817 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004818 else
4819 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004820 // For "[var, var] = expr" get the "var_idx" item from the
4821 // list.
4822 if (generate_GETITEM(cctx, var_idx) == FAIL)
4823 return FAIL;
4824 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004825
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004826 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004827 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004828 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004829 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004830 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004831 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004832 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004833 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004834 emsg(_(e_cannot_use_void));
4835 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004836 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004837 else if ((stacktype->tt_type == VAR_FUNC
4838 || stacktype->tt_type == VAR_PARTIAL)
4839 && var_wrong_func_name(name, TRUE))
4840 {
4841 goto theend;
4842 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004843 else
4844 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004845 // An empty list or dict has a &t_void member,
4846 // for a variable that implies &t_any.
4847 if (stacktype == &t_list_empty)
4848 lvar->lv_type = &t_list_any;
4849 else if (stacktype == &t_dict_empty)
4850 lvar->lv_type = &t_dict_any;
4851 else
4852 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004853 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004854 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004855 else
4856 {
4857 type_T *use_type = lvar->lv_type;
4858
4859 if (has_index)
4860 {
4861 use_type = use_type->tt_member;
4862 if (use_type == NULL)
4863 use_type = &t_void;
4864 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004865 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004866 == FAIL)
4867 goto theend;
4868 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004869 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004870 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004871 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004872 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004874 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004876 emsg(_(e_const_req_value));
4877 goto theend;
4878 }
4879 else if (!has_type || dest == dest_option)
4880 {
4881 emsg(_(e_type_req));
4882 goto theend;
4883 }
4884 else
4885 {
4886 // variables are always initialized
4887 if (ga_grow(instr, 1) == FAIL)
4888 goto theend;
4889 switch (member_type->tt_type)
4890 {
4891 case VAR_BOOL:
4892 generate_PUSHBOOL(cctx, VVAL_FALSE);
4893 break;
4894 case VAR_FLOAT:
4895#ifdef FEAT_FLOAT
4896 generate_PUSHF(cctx, 0.0);
4897#endif
4898 break;
4899 case VAR_STRING:
4900 generate_PUSHS(cctx, NULL);
4901 break;
4902 case VAR_BLOB:
4903 generate_PUSHBLOB(cctx, NULL);
4904 break;
4905 case VAR_FUNC:
4906 generate_PUSHFUNC(cctx, NULL, &t_func_void);
4907 break;
4908 case VAR_LIST:
4909 generate_NEWLIST(cctx, 0);
4910 break;
4911 case VAR_DICT:
4912 generate_NEWDICT(cctx, 0);
4913 break;
4914 case VAR_JOB:
4915 generate_PUSHJOB(cctx, NULL);
4916 break;
4917 case VAR_CHANNEL:
4918 generate_PUSHCHANNEL(cctx, NULL);
4919 break;
4920 case VAR_NUMBER:
4921 case VAR_UNKNOWN:
4922 case VAR_ANY:
4923 case VAR_PARTIAL:
4924 case VAR_VOID:
4925 case VAR_SPECIAL: // cannot happen
4926 generate_PUSHNR(cctx, 0);
4927 break;
4928 }
4929 }
4930 if (var_count == 0)
4931 end = p;
4932 }
4933
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004934 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004935 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004936 break;
4937
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004938 if (oplen > 0 && *op != '=')
4939 {
4940 type_T *expected = &t_number;
4941 type_T *stacktype;
4942
4943 // TODO: if type is known use float or any operation
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004944 // TODO: check operator matches variable type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004945
4946 if (*op == '.')
4947 expected = &t_string;
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004948 else if (*op == '+')
4949 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004950 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004951 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004952 goto theend;
4953
4954 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004955 {
4956 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
4957 goto theend;
4958 }
4959 else if (*op == '+')
4960 {
4961 if (generate_add_instr(cctx,
4962 operator_type(member_type, stacktype),
4963 member_type, stacktype) == FAIL)
4964 goto theend;
4965 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004966 else
4967 {
4968 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4969
4970 if (isn == NULL)
4971 goto theend;
4972 switch (*op)
4973 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004974 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4975 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4976 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4977 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 }
4980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004982 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004983 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004984 int r;
4985
4986 // Compile the "idx" in "var[idx]".
4987 if (new_local)
4988 --cctx->ctx_locals.ga_len;
4989 p = skipwhite(var_start + varlen + 1);
4990 r = compile_expr0(&p, cctx);
4991 if (new_local)
4992 ++cctx->ctx_locals.ga_len;
4993 if (r == FAIL)
4994 goto theend;
4995 if (*skipwhite(p) != ']')
4996 {
4997 emsg(_(e_missbrac));
4998 goto theend;
4999 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005000 if (type == &t_any)
5001 {
5002 type_T *idx_type = ((type_T **)stack->ga_data)[
5003 stack->ga_len - 1];
5004 // Index on variable of unknown type: guess the type from the
5005 // index type: number is dict, otherwise dict.
5006 // TODO: should do the assignment at runtime
5007 if (idx_type->tt_type == VAR_NUMBER)
5008 type = &t_list_any;
5009 else
5010 type = &t_dict_any;
5011 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005012 if (type->tt_type == VAR_DICT
5013 && may_generate_2STRING(-1, cctx) == FAIL)
5014 goto theend;
5015 if (type->tt_type == VAR_LIST
5016 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005017 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018 {
5019 emsg(_(e_number_exp));
5020 goto theend;
5021 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005022
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005023 // Load the dict or list. On the stack we then have:
5024 // - value
5025 // - index
5026 // - variable
5027 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005028
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005029 if (type->tt_type == VAR_LIST)
5030 {
5031 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5032 return FAIL;
5033 }
5034 else if (type->tt_type == VAR_DICT)
5035 {
5036 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5037 return FAIL;
5038 }
5039 else
5040 {
5041 emsg(_(e_listreq));
5042 goto theend;
5043 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005044 }
5045 else
5046 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005047 switch (dest)
5048 {
5049 case dest_option:
5050 generate_STOREOPT(cctx, name + 1, opt_flags);
5051 break;
5052 case dest_global:
5053 // include g: with the name, easier to execute that way
5054 generate_STORE(cctx, ISN_STOREG, 0, name);
5055 break;
5056 case dest_buffer:
5057 // include b: with the name, easier to execute that way
5058 generate_STORE(cctx, ISN_STOREB, 0, name);
5059 break;
5060 case dest_window:
5061 // include w: with the name, easier to execute that way
5062 generate_STORE(cctx, ISN_STOREW, 0, name);
5063 break;
5064 case dest_tab:
5065 // include t: with the name, easier to execute that way
5066 generate_STORE(cctx, ISN_STORET, 0, name);
5067 break;
5068 case dest_env:
5069 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5070 break;
5071 case dest_reg:
5072 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5073 break;
5074 case dest_vimvar:
5075 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5076 break;
5077 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005078 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005079 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005080 {
5081 char_u *name_s = name;
5082
5083 // Include s: in the name for store_var()
5084 if (name[1] != ':')
5085 {
5086 int len = (int)STRLEN(name) + 3;
5087
5088 name_s = alloc(len);
5089 if (name_s == NULL)
5090 name_s = name;
5091 else
5092 vim_snprintf((char *)name_s, len,
5093 "s:%s", name);
5094 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005095 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5096 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005097 if (name_s != name)
5098 vim_free(name_s);
5099 }
5100 else
5101 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005102 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005103 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005104 break;
5105 case dest_local:
5106 if (lvar != NULL)
5107 {
5108 isn_T *isn = ((isn_T *)instr->ga_data)
5109 + instr->ga_len - 1;
5110
5111 // optimization: turn "var = 123" from ISN_PUSHNR +
5112 // ISN_STORE into ISN_STORENR
5113 if (!lvar->lv_from_outer
5114 && instr->ga_len == instr_count + 1
5115 && isn->isn_type == ISN_PUSHNR)
5116 {
5117 varnumber_T val = isn->isn_arg.number;
5118
5119 isn->isn_type = ISN_STORENR;
5120 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5121 isn->isn_arg.storenr.stnr_val = val;
5122 if (stack->ga_len > 0)
5123 --stack->ga_len;
5124 }
5125 else if (lvar->lv_from_outer)
5126 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005127 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005128 else
5129 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5130 }
5131 break;
5132 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005133 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005134
5135 if (var_idx + 1 < var_count)
5136 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005138
5139 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005140 if (var_count > 0 && !semicolon)
5141 {
5142 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5143 goto theend;
5144 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005145
Bram Moolenaarb2097502020-07-19 17:17:02 +02005146 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147
5148theend:
5149 vim_free(name);
5150 return ret;
5151}
5152
5153/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005154 * Check if "name" can be "unlet".
5155 */
5156 int
5157check_vim9_unlet(char_u *name)
5158{
5159 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5160 {
5161 semsg(_("E1081: Cannot unlet %s"), name);
5162 return FAIL;
5163 }
5164 return OK;
5165}
5166
5167/*
5168 * Callback passed to ex_unletlock().
5169 */
5170 static int
5171compile_unlet(
5172 lval_T *lvp,
5173 char_u *name_end,
5174 exarg_T *eap,
5175 int deep UNUSED,
5176 void *coookie)
5177{
5178 cctx_T *cctx = coookie;
5179
5180 if (lvp->ll_tv == NULL)
5181 {
5182 char_u *p = lvp->ll_name;
5183 int cc = *name_end;
5184 int ret = OK;
5185
5186 // Normal name. Only supports g:, w:, t: and b: namespaces.
5187 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005188 if (*p == '$')
5189 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5190 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005191 ret = FAIL;
5192 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005193 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005194
5195 *name_end = cc;
5196 return ret;
5197 }
5198
5199 // TODO: unlet {list}[idx]
5200 // TODO: unlet {dict}[key]
5201 emsg("Sorry, :unlet not fully implemented yet");
5202 return FAIL;
5203}
5204
5205/*
5206 * compile "unlet var", "lock var" and "unlock var"
5207 * "arg" points to "var".
5208 */
5209 static char_u *
5210compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5211{
5212 char_u *p = arg;
5213
5214 if (eap->cmdidx != CMD_unlet)
5215 {
5216 emsg("Sorry, :lock and unlock not implemented yet");
5217 return NULL;
5218 }
5219
5220 if (*p == '!')
5221 {
5222 p = skipwhite(p + 1);
5223 eap->forceit = TRUE;
5224 }
5225
5226 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5227 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5228}
5229
5230/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231 * Compile an :import command.
5232 */
5233 static char_u *
5234compile_import(char_u *arg, cctx_T *cctx)
5235{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005236 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237}
5238
5239/*
5240 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5241 */
5242 static int
5243compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5244{
5245 garray_T *instr = &cctx->ctx_instr;
5246 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5247
5248 if (endlabel == NULL)
5249 return FAIL;
5250 endlabel->el_next = *el;
5251 *el = endlabel;
5252 endlabel->el_end_label = instr->ga_len;
5253
5254 generate_JUMP(cctx, when, 0);
5255 return OK;
5256}
5257
5258 static void
5259compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5260{
5261 garray_T *instr = &cctx->ctx_instr;
5262
5263 while (*el != NULL)
5264 {
5265 endlabel_T *cur = (*el);
5266 isn_T *isn;
5267
5268 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5269 isn->isn_arg.jump.jump_where = instr->ga_len;
5270 *el = cur->el_next;
5271 vim_free(cur);
5272 }
5273}
5274
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005275 static void
5276compile_free_jump_to_end(endlabel_T **el)
5277{
5278 while (*el != NULL)
5279 {
5280 endlabel_T *cur = (*el);
5281
5282 *el = cur->el_next;
5283 vim_free(cur);
5284 }
5285}
5286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005287/*
5288 * Create a new scope and set up the generic items.
5289 */
5290 static scope_T *
5291new_scope(cctx_T *cctx, scopetype_T type)
5292{
5293 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5294
5295 if (scope == NULL)
5296 return NULL;
5297 scope->se_outer = cctx->ctx_scope;
5298 cctx->ctx_scope = scope;
5299 scope->se_type = type;
5300 scope->se_local_count = cctx->ctx_locals.ga_len;
5301 return scope;
5302}
5303
5304/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005305 * Free the current scope and go back to the outer scope.
5306 */
5307 static void
5308drop_scope(cctx_T *cctx)
5309{
5310 scope_T *scope = cctx->ctx_scope;
5311
5312 if (scope == NULL)
5313 {
5314 iemsg("calling drop_scope() without a scope");
5315 return;
5316 }
5317 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005318 switch (scope->se_type)
5319 {
5320 case IF_SCOPE:
5321 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5322 case FOR_SCOPE:
5323 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5324 case WHILE_SCOPE:
5325 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5326 case TRY_SCOPE:
5327 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5328 case NO_SCOPE:
5329 case BLOCK_SCOPE:
5330 break;
5331 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005332 vim_free(scope);
5333}
5334
5335/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336 * compile "if expr"
5337 *
5338 * "if expr" Produces instructions:
5339 * EVAL expr Push result of "expr"
5340 * JUMP_IF_FALSE end
5341 * ... body ...
5342 * end:
5343 *
5344 * "if expr | else" Produces instructions:
5345 * EVAL expr Push result of "expr"
5346 * JUMP_IF_FALSE else
5347 * ... body ...
5348 * JUMP_ALWAYS end
5349 * else:
5350 * ... body ...
5351 * end:
5352 *
5353 * "if expr1 | elseif expr2 | else" Produces instructions:
5354 * EVAL expr Push result of "expr"
5355 * JUMP_IF_FALSE elseif
5356 * ... body ...
5357 * JUMP_ALWAYS end
5358 * elseif:
5359 * EVAL expr Push result of "expr"
5360 * JUMP_IF_FALSE else
5361 * ... body ...
5362 * JUMP_ALWAYS end
5363 * else:
5364 * ... body ...
5365 * end:
5366 */
5367 static char_u *
5368compile_if(char_u *arg, cctx_T *cctx)
5369{
5370 char_u *p = arg;
5371 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005372 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005374 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005375 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005376
Bram Moolenaara5565e42020-05-09 15:44:01 +02005377 CLEAR_FIELD(ppconst);
5378 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005379 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005380 clear_ppconst(&ppconst);
5381 return NULL;
5382 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005383 if (cctx->ctx_skip == SKIP_YES)
5384 clear_ppconst(&ppconst);
5385 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005386 {
5387 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005388 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005389 clear_ppconst(&ppconst);
5390 }
5391 else
5392 {
5393 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005394 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005395 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005396 return NULL;
5397 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398
5399 scope = new_scope(cctx, IF_SCOPE);
5400 if (scope == NULL)
5401 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005402 scope->se_skip_save = skip_save;
5403 // "is_had_return" will be reset if any block does not end in :return
5404 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005406 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005407 {
5408 // "where" is set when ":elseif", "else" or ":endif" is found
5409 scope->se_u.se_if.is_if_label = instr->ga_len;
5410 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5411 }
5412 else
5413 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005414
5415 return p;
5416}
5417
5418 static char_u *
5419compile_elseif(char_u *arg, cctx_T *cctx)
5420{
5421 char_u *p = arg;
5422 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005423 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424 isn_T *isn;
5425 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005426 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427
5428 if (scope == NULL || scope->se_type != IF_SCOPE)
5429 {
5430 emsg(_(e_elseif_without_if));
5431 return NULL;
5432 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005433 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005434 if (!cctx->ctx_had_return)
5435 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005436
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005437 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005438 {
5439 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005440 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005441 return NULL;
5442 // previous "if" or "elseif" jumps here
5443 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5444 isn->isn_arg.jump.jump_where = instr->ga_len;
5445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005446
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005447 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005448 CLEAR_FIELD(ppconst);
5449 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005450 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005451 clear_ppconst(&ppconst);
5452 return NULL;
5453 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005454 if (scope->se_skip_save == SKIP_YES)
5455 clear_ppconst(&ppconst);
5456 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005457 {
5458 // The expression results in a constant.
5459 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005460 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005461 clear_ppconst(&ppconst);
5462 scope->se_u.se_if.is_if_label = -1;
5463 }
5464 else
5465 {
5466 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005467 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005468 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005469 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005471 // "where" is set when ":elseif", "else" or ":endif" is found
5472 scope->se_u.se_if.is_if_label = instr->ga_len;
5473 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5474 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005475
5476 return p;
5477}
5478
5479 static char_u *
5480compile_else(char_u *arg, cctx_T *cctx)
5481{
5482 char_u *p = arg;
5483 garray_T *instr = &cctx->ctx_instr;
5484 isn_T *isn;
5485 scope_T *scope = cctx->ctx_scope;
5486
5487 if (scope == NULL || scope->se_type != IF_SCOPE)
5488 {
5489 emsg(_(e_else_without_if));
5490 return NULL;
5491 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005492 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005493 if (!cctx->ctx_had_return)
5494 scope->se_u.se_if.is_had_return = FALSE;
5495 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496
Bram Moolenaarefd88552020-06-18 20:50:10 +02005497 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005498 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005499 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005500 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005501 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005502 if (!cctx->ctx_had_return
5503 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5504 JUMP_ALWAYS, cctx) == FAIL)
5505 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005506 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005507
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005508 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005509 {
5510 if (scope->se_u.se_if.is_if_label >= 0)
5511 {
5512 // previous "if" or "elseif" jumps here
5513 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5514 isn->isn_arg.jump.jump_where = instr->ga_len;
5515 scope->se_u.se_if.is_if_label = -1;
5516 }
5517 }
5518
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005519 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005520 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5521 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005522
5523 return p;
5524}
5525
5526 static char_u *
5527compile_endif(char_u *arg, cctx_T *cctx)
5528{
5529 scope_T *scope = cctx->ctx_scope;
5530 ifscope_T *ifscope;
5531 garray_T *instr = &cctx->ctx_instr;
5532 isn_T *isn;
5533
5534 if (scope == NULL || scope->se_type != IF_SCOPE)
5535 {
5536 emsg(_(e_endif_without_if));
5537 return NULL;
5538 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005539 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005540 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005541 if (!cctx->ctx_had_return)
5542 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005543
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005544 if (scope->se_u.se_if.is_if_label >= 0)
5545 {
5546 // previous "if" or "elseif" jumps here
5547 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5548 isn->isn_arg.jump.jump_where = instr->ga_len;
5549 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550 // Fill in the "end" label in jumps at the end of the blocks.
5551 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005552 cctx->ctx_skip = scope->se_skip_save;
5553
5554 // If all the blocks end in :return and there is an :else then the
5555 // had_return flag is set.
5556 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005558 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005559 return arg;
5560}
5561
5562/*
5563 * compile "for var in expr"
5564 *
5565 * Produces instructions:
5566 * PUSHNR -1
5567 * STORE loop-idx Set index to -1
5568 * EVAL expr Push result of "expr"
5569 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5570 * - if beyond end, jump to "end"
5571 * - otherwise get item from list and push it
5572 * STORE var Store item in "var"
5573 * ... body ...
5574 * JUMP top Jump back to repeat
5575 * end: DROP Drop the result of "expr"
5576 *
5577 */
5578 static char_u *
5579compile_for(char_u *arg, cctx_T *cctx)
5580{
5581 char_u *p;
5582 size_t varlen;
5583 garray_T *instr = &cctx->ctx_instr;
5584 garray_T *stack = &cctx->ctx_type_stack;
5585 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005586 lvar_T *loop_lvar; // loop iteration variable
5587 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588 type_T *vartype;
5589
5590 // TODO: list of variables: "for [key, value] in dict"
5591 // parse "var"
5592 for (p = arg; eval_isnamec1(*p); ++p)
5593 ;
5594 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005595 var_lvar = lookup_local(arg, varlen, cctx);
5596 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005597 {
5598 semsg(_("E1023: variable already defined: %s"), arg);
5599 return NULL;
5600 }
5601
5602 // consume "in"
5603 p = skipwhite(p);
5604 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5605 {
5606 emsg(_(e_missing_in));
5607 return NULL;
5608 }
5609 p = skipwhite(p + 2);
5610
5611
5612 scope = new_scope(cctx, FOR_SCOPE);
5613 if (scope == NULL)
5614 return NULL;
5615
5616 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005617 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5618 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005619 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005620 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005621 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005622 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005623 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005624
5625 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005626 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5627 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005628 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005629 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005630 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005631 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005634 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005635
5636 // compile "expr", it remains on the stack until "endfor"
5637 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005638 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005639 {
5640 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005642 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005643
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005644 // Now that we know the type of "var", check that it is a list, now or at
5645 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005646 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005647 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005648 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005649 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005650 return NULL;
5651 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005652 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005653 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005654
5655 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005656 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005658 generate_FOR(cctx, loop_lvar->lv_idx);
5659 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005660
5661 return arg;
5662}
5663
5664/*
5665 * compile "endfor"
5666 */
5667 static char_u *
5668compile_endfor(char_u *arg, cctx_T *cctx)
5669{
5670 garray_T *instr = &cctx->ctx_instr;
5671 scope_T *scope = cctx->ctx_scope;
5672 forscope_T *forscope;
5673 isn_T *isn;
5674
5675 if (scope == NULL || scope->se_type != FOR_SCOPE)
5676 {
5677 emsg(_(e_for));
5678 return NULL;
5679 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005680 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005682 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005683
5684 // At end of ":for" scope jump back to the FOR instruction.
5685 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5686
5687 // Fill in the "end" label in the FOR statement so it can jump here
5688 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5689 isn->isn_arg.forloop.for_end = instr->ga_len;
5690
5691 // Fill in the "end" label any BREAK statements
5692 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5693
5694 // Below the ":for" scope drop the "expr" list from the stack.
5695 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5696 return NULL;
5697
5698 vim_free(scope);
5699
5700 return arg;
5701}
5702
5703/*
5704 * compile "while expr"
5705 *
5706 * Produces instructions:
5707 * top: EVAL expr Push result of "expr"
5708 * JUMP_IF_FALSE end jump if false
5709 * ... body ...
5710 * JUMP top Jump back to repeat
5711 * end:
5712 *
5713 */
5714 static char_u *
5715compile_while(char_u *arg, cctx_T *cctx)
5716{
5717 char_u *p = arg;
5718 garray_T *instr = &cctx->ctx_instr;
5719 scope_T *scope;
5720
5721 scope = new_scope(cctx, WHILE_SCOPE);
5722 if (scope == NULL)
5723 return NULL;
5724
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005725 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005726
5727 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005728 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005729 return NULL;
5730
5731 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005732 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005733 JUMP_IF_FALSE, cctx) == FAIL)
5734 return FAIL;
5735
5736 return p;
5737}
5738
5739/*
5740 * compile "endwhile"
5741 */
5742 static char_u *
5743compile_endwhile(char_u *arg, cctx_T *cctx)
5744{
5745 scope_T *scope = cctx->ctx_scope;
5746
5747 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5748 {
5749 emsg(_(e_while));
5750 return NULL;
5751 }
5752 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005753 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005754
5755 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005756 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005757
5758 // Fill in the "end" label in the WHILE statement so it can jump here.
5759 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005760 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005761
5762 vim_free(scope);
5763
5764 return arg;
5765}
5766
5767/*
5768 * compile "continue"
5769 */
5770 static char_u *
5771compile_continue(char_u *arg, cctx_T *cctx)
5772{
5773 scope_T *scope = cctx->ctx_scope;
5774
5775 for (;;)
5776 {
5777 if (scope == NULL)
5778 {
5779 emsg(_(e_continue));
5780 return NULL;
5781 }
5782 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5783 break;
5784 scope = scope->se_outer;
5785 }
5786
5787 // Jump back to the FOR or WHILE instruction.
5788 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005789 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5790 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005791 return arg;
5792}
5793
5794/*
5795 * compile "break"
5796 */
5797 static char_u *
5798compile_break(char_u *arg, cctx_T *cctx)
5799{
5800 scope_T *scope = cctx->ctx_scope;
5801 endlabel_T **el;
5802
5803 for (;;)
5804 {
5805 if (scope == NULL)
5806 {
5807 emsg(_(e_break));
5808 return NULL;
5809 }
5810 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5811 break;
5812 scope = scope->se_outer;
5813 }
5814
5815 // Jump to the end of the FOR or WHILE loop.
5816 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005817 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005818 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005819 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5821 return FAIL;
5822
5823 return arg;
5824}
5825
5826/*
5827 * compile "{" start of block
5828 */
5829 static char_u *
5830compile_block(char_u *arg, cctx_T *cctx)
5831{
5832 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5833 return NULL;
5834 return skipwhite(arg + 1);
5835}
5836
5837/*
5838 * compile end of block: drop one scope
5839 */
5840 static void
5841compile_endblock(cctx_T *cctx)
5842{
5843 scope_T *scope = cctx->ctx_scope;
5844
5845 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005846 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005847 vim_free(scope);
5848}
5849
5850/*
5851 * compile "try"
5852 * Creates a new scope for the try-endtry, pointing to the first catch and
5853 * finally.
5854 * Creates another scope for the "try" block itself.
5855 * TRY instruction sets up exception handling at runtime.
5856 *
5857 * "try"
5858 * TRY -> catch1, -> finally push trystack entry
5859 * ... try block
5860 * "throw {exception}"
5861 * EVAL {exception}
5862 * THROW create exception
5863 * ... try block
5864 * " catch {expr}"
5865 * JUMP -> finally
5866 * catch1: PUSH exeception
5867 * EVAL {expr}
5868 * MATCH
5869 * JUMP nomatch -> catch2
5870 * CATCH remove exception
5871 * ... catch block
5872 * " catch"
5873 * JUMP -> finally
5874 * catch2: CATCH remove exception
5875 * ... catch block
5876 * " finally"
5877 * finally:
5878 * ... finally block
5879 * " endtry"
5880 * ENDTRY pop trystack entry, may rethrow
5881 */
5882 static char_u *
5883compile_try(char_u *arg, cctx_T *cctx)
5884{
5885 garray_T *instr = &cctx->ctx_instr;
5886 scope_T *try_scope;
5887 scope_T *scope;
5888
5889 // scope that holds the jumps that go to catch/finally/endtry
5890 try_scope = new_scope(cctx, TRY_SCOPE);
5891 if (try_scope == NULL)
5892 return NULL;
5893
5894 // "catch" is set when the first ":catch" is found.
5895 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005896 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897 if (generate_instr(cctx, ISN_TRY) == NULL)
5898 return NULL;
5899
5900 // scope for the try block itself
5901 scope = new_scope(cctx, BLOCK_SCOPE);
5902 if (scope == NULL)
5903 return NULL;
5904
5905 return arg;
5906}
5907
5908/*
5909 * compile "catch {expr}"
5910 */
5911 static char_u *
5912compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5913{
5914 scope_T *scope = cctx->ctx_scope;
5915 garray_T *instr = &cctx->ctx_instr;
5916 char_u *p;
5917 isn_T *isn;
5918
5919 // end block scope from :try or :catch
5920 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5921 compile_endblock(cctx);
5922 scope = cctx->ctx_scope;
5923
5924 // Error if not in a :try scope
5925 if (scope == NULL || scope->se_type != TRY_SCOPE)
5926 {
5927 emsg(_(e_catch));
5928 return NULL;
5929 }
5930
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005931 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005932 {
5933 emsg(_("E1033: catch unreachable after catch-all"));
5934 return NULL;
5935 }
5936
5937 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005938 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005939 JUMP_ALWAYS, cctx) == FAIL)
5940 return NULL;
5941
5942 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005943 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005944 if (isn->isn_arg.try.try_catch == 0)
5945 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005946 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005947 {
5948 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005949 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950 isn->isn_arg.jump.jump_where = instr->ga_len;
5951 }
5952
5953 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005954 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005955 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005956 scope->se_u.se_try.ts_caught_all = TRUE;
5957 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005958 }
5959 else
5960 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005961 char_u *end;
5962 char_u *pat;
5963 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005964 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005965 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005967 // Push v:exception, push {expr} and MATCH
5968 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5969
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005970 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005971 if (*end != *p)
5972 {
5973 semsg(_("E1067: Separator mismatch: %s"), p);
5974 vim_free(tofree);
5975 return FAIL;
5976 }
5977 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005978 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005979 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005980 len = (int)(end - tofree);
5981 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005982 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005983 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005984 if (pat == NULL)
5985 return FAIL;
5986 if (generate_PUSHS(cctx, pat) == FAIL)
5987 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005989 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5990 return NULL;
5991
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005992 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5994 return NULL;
5995 }
5996
5997 if (generate_instr(cctx, ISN_CATCH) == NULL)
5998 return NULL;
5999
6000 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6001 return NULL;
6002 return p;
6003}
6004
6005 static char_u *
6006compile_finally(char_u *arg, cctx_T *cctx)
6007{
6008 scope_T *scope = cctx->ctx_scope;
6009 garray_T *instr = &cctx->ctx_instr;
6010 isn_T *isn;
6011
6012 // end block scope from :try or :catch
6013 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6014 compile_endblock(cctx);
6015 scope = cctx->ctx_scope;
6016
6017 // Error if not in a :try scope
6018 if (scope == NULL || scope->se_type != TRY_SCOPE)
6019 {
6020 emsg(_(e_finally));
6021 return NULL;
6022 }
6023
6024 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006025 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026 if (isn->isn_arg.try.try_finally != 0)
6027 {
6028 emsg(_(e_finally_dup));
6029 return NULL;
6030 }
6031
6032 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006033 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034
Bram Moolenaar585fea72020-04-02 22:33:21 +02006035 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006036 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006037 {
6038 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006039 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006040 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006041 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 }
6043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006044 // TODO: set index in ts_finally_label jumps
6045
6046 return arg;
6047}
6048
6049 static char_u *
6050compile_endtry(char_u *arg, cctx_T *cctx)
6051{
6052 scope_T *scope = cctx->ctx_scope;
6053 garray_T *instr = &cctx->ctx_instr;
6054 isn_T *isn;
6055
6056 // end block scope from :catch or :finally
6057 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6058 compile_endblock(cctx);
6059 scope = cctx->ctx_scope;
6060
6061 // Error if not in a :try scope
6062 if (scope == NULL || scope->se_type != TRY_SCOPE)
6063 {
6064 if (scope == NULL)
6065 emsg(_(e_no_endtry));
6066 else if (scope->se_type == WHILE_SCOPE)
6067 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006068 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006069 emsg(_(e_endfor));
6070 else
6071 emsg(_(e_endif));
6072 return NULL;
6073 }
6074
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006075 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6077 {
6078 emsg(_("E1032: missing :catch or :finally"));
6079 return NULL;
6080 }
6081
6082 // Fill in the "end" label in jumps at the end of the blocks, if not done
6083 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006084 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006085
6086 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006087 if (isn->isn_arg.try.try_catch == 0)
6088 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006089 if (isn->isn_arg.try.try_finally == 0)
6090 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006091
6092 if (scope->se_u.se_try.ts_catch_label != 0)
6093 {
6094 // Last catch without match jumps here
6095 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6096 isn->isn_arg.jump.jump_where = instr->ga_len;
6097 }
6098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006099 compile_endblock(cctx);
6100
6101 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6102 return NULL;
6103 return arg;
6104}
6105
6106/*
6107 * compile "throw {expr}"
6108 */
6109 static char_u *
6110compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6111{
6112 char_u *p = skipwhite(arg);
6113
Bram Moolenaara5565e42020-05-09 15:44:01 +02006114 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006115 return NULL;
6116 if (may_generate_2STRING(-1, cctx) == FAIL)
6117 return NULL;
6118 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6119 return NULL;
6120
6121 return p;
6122}
6123
6124/*
6125 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006126 * compile "echomsg expr"
6127 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006128 * compile "execute expr"
6129 */
6130 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006131compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006132{
6133 char_u *p = arg;
6134 int count = 0;
6135
6136 for (;;)
6137 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006138 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006139 return NULL;
6140 ++count;
6141 p = skipwhite(p);
6142 if (ends_excmd(*p))
6143 break;
6144 }
6145
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006146 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6147 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6148 else if (cmdidx == CMD_execute)
6149 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6150 else if (cmdidx == CMD_echomsg)
6151 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6152 else
6153 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006154 return p;
6155}
6156
6157/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006158 * A command that is not compiled, execute with legacy code.
6159 */
6160 static char_u *
6161compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6162{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006163 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006164 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006165 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006166
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006167 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006168 goto theend;
6169
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006170 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006171 {
6172 long argt = excmd_get_argt(eap->cmdidx);
6173 int usefilter = FALSE;
6174
6175 has_expr = argt & (EX_XFILE | EX_EXPAND);
6176
6177 // If the command can be followed by a bar, find the bar and truncate
6178 // it, so that the following command can be compiled.
6179 // The '|' is overwritten with a NUL, it is put back below.
6180 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6181 && *eap->arg == '!')
6182 // :w !filter or :r !filter or :r! filter
6183 usefilter = TRUE;
6184 if ((argt & EX_TRLBAR) && !usefilter)
6185 {
6186 separate_nextcmd(eap);
6187 if (eap->nextcmd != NULL)
6188 nextcmd = eap->nextcmd;
6189 }
6190 }
6191
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006192 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6193 {
6194 // expand filename in "syntax include [@group] filename"
6195 has_expr = TRUE;
6196 eap->arg = skipwhite(eap->arg + 7);
6197 if (*eap->arg == '@')
6198 eap->arg = skiptowhite(eap->arg);
6199 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006200
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006201 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006202 {
6203 int count = 0;
6204 char_u *start = skipwhite(line);
6205
6206 // :cmd xxx`=expr1`yyy`=expr2`zzz
6207 // PUSHS ":cmd xxx"
6208 // eval expr1
6209 // PUSHS "yyy"
6210 // eval expr2
6211 // PUSHS "zzz"
6212 // EXECCONCAT 5
6213 for (;;)
6214 {
6215 if (p > start)
6216 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006217 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006218 ++count;
6219 }
6220 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006221 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006222 return NULL;
6223 may_generate_2STRING(-1, cctx);
6224 ++count;
6225 p = skipwhite(p);
6226 if (*p != '`')
6227 {
6228 emsg(_("E1083: missing backtick"));
6229 return NULL;
6230 }
6231 start = p + 1;
6232
6233 p = (char_u *)strstr((char *)start, "`=");
6234 if (p == NULL)
6235 {
6236 if (*skipwhite(start) != NUL)
6237 {
6238 generate_PUSHS(cctx, vim_strsave(start));
6239 ++count;
6240 }
6241 break;
6242 }
6243 }
6244 generate_EXECCONCAT(cctx, count);
6245 }
6246 else
6247 generate_EXEC(cctx, line);
6248
6249theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006250 if (*nextcmd != NUL)
6251 {
6252 // the parser expects a pointer to the bar, put it back
6253 --nextcmd;
6254 *nextcmd = '|';
6255 }
6256
6257 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006258}
6259
6260/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006261 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006262 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006263 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006264 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006265add_def_function(ufunc_T *ufunc)
6266{
6267 dfunc_T *dfunc;
6268
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006269 if (def_functions.ga_len == 0)
6270 {
6271 // The first position is not used, so that a zero uf_dfunc_idx means it
6272 // wasn't set.
6273 if (ga_grow(&def_functions, 1) == FAIL)
6274 return FAIL;
6275 ++def_functions.ga_len;
6276 }
6277
Bram Moolenaar09689a02020-05-09 22:50:08 +02006278 // Add the function to "def_functions".
6279 if (ga_grow(&def_functions, 1) == FAIL)
6280 return FAIL;
6281 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6282 CLEAR_POINTER(dfunc);
6283 dfunc->df_idx = def_functions.ga_len;
6284 ufunc->uf_dfunc_idx = dfunc->df_idx;
6285 dfunc->df_ufunc = ufunc;
6286 ++def_functions.ga_len;
6287 return OK;
6288}
6289
6290/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291 * After ex_function() has collected all the function lines: parse and compile
6292 * the lines into instructions.
6293 * Adds the function to "def_functions".
6294 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6295 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006296 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006297 * This can be used recursively through compile_lambda(), which may reallocate
6298 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006299 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006300 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006301 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006302compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 char_u *line = NULL;
6305 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 cctx_T cctx;
6308 garray_T *instr;
6309 int called_emsg_before = called_emsg;
6310 int ret = FAIL;
6311 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006312 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006313 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006314 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006316 // When using a function that was compiled before: Free old instructions.
6317 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006318 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006320 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6321 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006322 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006324 else
6325 {
6326 if (add_def_function(ufunc) == FAIL)
6327 return FAIL;
6328 new_def_function = TRUE;
6329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330
Bram Moolenaar985116a2020-07-12 17:31:09 +02006331 ufunc->uf_def_status = UF_COMPILING;
6332
Bram Moolenaara80faa82020-04-12 19:37:17 +02006333 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334 cctx.ctx_ufunc = ufunc;
6335 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006336 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6338 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6339 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6340 cctx.ctx_type_list = &ufunc->uf_type_list;
6341 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6342 instr = &cctx.ctx_instr;
6343
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006344 // Set the context to the function, it may be compiled when called from
6345 // another script. Set the script version to the most modern one.
6346 // The line number will be set in next_line_from_context().
6347 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6349
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006350 // Make sure error messages are OK.
6351 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6352 if (do_estack_push)
6353 estack_push_ufunc(ufunc, 1);
6354
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006355 if (ufunc->uf_def_args.ga_len > 0)
6356 {
6357 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006358 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006359 int i;
6360 char_u *arg;
6361 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006362 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006363
6364 // Produce instructions for the default values of optional arguments.
6365 // Store the instruction index in uf_def_arg_idx[] so that we know
6366 // where to start when the function is called, depending on the number
6367 // of arguments.
6368 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6369 if (ufunc->uf_def_arg_idx == NULL)
6370 goto erret;
6371 for (i = 0; i < count; ++i)
6372 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006373 garray_T *stack = &cctx.ctx_type_stack;
6374 type_T *val_type;
6375 int arg_idx = first_def_arg + i;
6376
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006377 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6378 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006379 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006380 goto erret;
6381
6382 // If no type specified use the type of the default value.
6383 // Otherwise check that the default value type matches the
6384 // specified type.
6385 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6386 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006387 {
6388 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006389 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006390 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006391 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006392 == FAIL)
6393 {
6394 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6395 arg_idx + 1);
6396 goto erret;
6397 }
6398
6399 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006400 goto erret;
6401 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006402 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006403
6404 if (did_set_arg_type)
6405 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006406 }
6407
6408 /*
6409 * Loop over all the lines of the function and generate instructions.
6410 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006411 for (;;)
6412 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006413 exarg_T ea;
6414 cmdmod_T save_cmdmod;
6415 int starts_with_colon = FALSE;
6416 char_u *cmd;
6417 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006418
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006419 // Bail out on the first error to avoid a flood of errors and report
6420 // the right line number when inside try/catch.
6421 if (emsg_before != called_emsg)
6422 goto erret;
6423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006424 if (line != NULL && *line == '|')
6425 // the line continues after a '|'
6426 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006427 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006428 && !(*line == '#' && (line == cctx.ctx_line_start
6429 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006430 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006431 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006432 goto erret;
6433 }
6434 else
6435 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006436 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006437 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006438 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006439 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006440 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006441 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442
Bram Moolenaara80faa82020-04-12 19:37:17 +02006443 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006444 ea.cmdlinep = &line;
6445 ea.cmd = skipwhite(line);
6446
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006447 // Some things can be recognized by the first character.
6448 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006450 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006451 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006452 if (ea.cmd[1] != '{')
6453 {
6454 line = (char_u *)"";
6455 continue;
6456 }
6457 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006458
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006459 case '}':
6460 {
6461 // "}" ends a block scope
6462 scopetype_T stype = cctx.ctx_scope == NULL
6463 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006465 if (stype == BLOCK_SCOPE)
6466 {
6467 compile_endblock(&cctx);
6468 line = ea.cmd;
6469 }
6470 else
6471 {
6472 emsg(_("E1025: using } outside of a block scope"));
6473 goto erret;
6474 }
6475 if (line != NULL)
6476 line = skipwhite(ea.cmd + 1);
6477 continue;
6478 }
6479
6480 case '{':
6481 // "{" starts a block scope
6482 // "{'a': 1}->func() is something else
6483 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6484 {
6485 line = compile_block(ea.cmd, &cctx);
6486 continue;
6487 }
6488 break;
6489
6490 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006491 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006492 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006493 }
6494
6495 /*
6496 * COMMAND MODIFIERS
6497 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006498 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006499 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6500 {
6501 if (errormsg != NULL)
6502 goto erret;
6503 // empty line or comment
6504 line = (char_u *)"";
6505 continue;
6506 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006507 // TODO: use modifiers in the command
6508 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006509 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006510
6511 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006512 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006514 {
6515 if (*ea.cmd == '(')
6516 // not for "call()"
6517 ea.cmd = p;
6518 else
6519 ea.cmd = skipwhite(ea.cmd);
6520 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006522 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006524 char_u *pskip;
6525
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006526 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006527 // find what follows.
6528 // Skip over "var.member", "var[idx]" and the like.
6529 // Also "&opt = val", "$ENV = val" and "@r = val".
6530 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006531 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006532 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006533 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006534 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006535 char_u *var_end;
6536 int oplen;
6537 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538
Bram Moolenaar65821722020-08-02 18:58:54 +02006539 if (ea.cmd[0] == '@')
6540 var_end = ea.cmd + 2;
6541 else
6542 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006543 FNE_CHECK_START | FNE_INCL_BR);
6544 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006545 if (oplen > 0)
6546 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006547 size_t len = p - ea.cmd;
6548
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006549 // Recognize an assignment if we recognize the variable
6550 // name:
6551 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006552 // "local = expr" where "local" is a local var.
6553 // "script = expr" where "script" is a script-local var.
6554 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006555 // "&opt = expr"
6556 // "$ENV = expr"
6557 // "@r = expr"
6558 if (*ea.cmd == '&'
6559 || *ea.cmd == '$'
6560 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006561 || ((len) > 2 && ea.cmd[1] == ':')
6562 || lookup_local(ea.cmd, len, &cctx) != NULL
6563 || lookup_arg(ea.cmd, len, NULL, NULL,
6564 NULL, &cctx) == OK
6565 || lookup_script(ea.cmd, len) == OK
6566 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006567 {
6568 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006569 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006570 goto erret;
6571 continue;
6572 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 }
6574 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006575
6576 if (*ea.cmd == '[')
6577 {
6578 // [var, var] = expr
6579 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6580 if (line == NULL)
6581 goto erret;
6582 if (line != ea.cmd)
6583 continue;
6584 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006585 }
6586
6587 /*
6588 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006589 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006591 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006592 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006593 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006594 ea.cmd = skip_range(ea.cmd, NULL);
6595 if (ea.cmd > cmd && !starts_with_colon)
6596 {
6597 emsg(_(e_colon_required));
6598 goto erret;
6599 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006600 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006601 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006602 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006603 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604
6605 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6606 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006607 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006608 {
6609 line += STRLEN(line);
6610 continue;
6611 }
6612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006614 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006616 // CMD_let cannot happen, compile_assignment() above is used
6617 iemsg("Command from find_ex_command() not handled");
6618 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620 }
6621
6622 p = skipwhite(p);
6623
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006624 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006625 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006626 && ea.cmdidx != CMD_elseif
6627 && ea.cmdidx != CMD_else
6628 && ea.cmdidx != CMD_endif)
6629 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006630 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006631 continue;
6632 }
6633
Bram Moolenaarefd88552020-06-18 20:50:10 +02006634 if (ea.cmdidx != CMD_elseif
6635 && ea.cmdidx != CMD_else
6636 && ea.cmdidx != CMD_endif
6637 && ea.cmdidx != CMD_endfor
6638 && ea.cmdidx != CMD_endwhile
6639 && ea.cmdidx != CMD_catch
6640 && ea.cmdidx != CMD_finally
6641 && ea.cmdidx != CMD_endtry)
6642 {
6643 if (cctx.ctx_had_return)
6644 {
6645 emsg(_("E1095: Unreachable code after :return"));
6646 goto erret;
6647 }
6648 }
6649
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006650 switch (ea.cmdidx)
6651 {
6652 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006653 ea.arg = p;
6654 line = compile_nested_function(&ea, &cctx);
6655 break;
6656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006657 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006658 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659 goto erret;
6660
6661 case CMD_return:
6662 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006663 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 break;
6665
6666 case CMD_let:
6667 case CMD_const:
6668 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006669 if (line == p)
6670 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006671 break;
6672
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006673 case CMD_unlet:
6674 case CMD_unlockvar:
6675 case CMD_lockvar:
6676 line = compile_unletlock(p, &ea, &cctx);
6677 break;
6678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006679 case CMD_import:
6680 line = compile_import(p, &cctx);
6681 break;
6682
6683 case CMD_if:
6684 line = compile_if(p, &cctx);
6685 break;
6686 case CMD_elseif:
6687 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006688 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006689 break;
6690 case CMD_else:
6691 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006692 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693 break;
6694 case CMD_endif:
6695 line = compile_endif(p, &cctx);
6696 break;
6697
6698 case CMD_while:
6699 line = compile_while(p, &cctx);
6700 break;
6701 case CMD_endwhile:
6702 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006703 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 break;
6705
6706 case CMD_for:
6707 line = compile_for(p, &cctx);
6708 break;
6709 case CMD_endfor:
6710 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006711 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712 break;
6713 case CMD_continue:
6714 line = compile_continue(p, &cctx);
6715 break;
6716 case CMD_break:
6717 line = compile_break(p, &cctx);
6718 break;
6719
6720 case CMD_try:
6721 line = compile_try(p, &cctx);
6722 break;
6723 case CMD_catch:
6724 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006725 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726 break;
6727 case CMD_finally:
6728 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006729 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006730 break;
6731 case CMD_endtry:
6732 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006733 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 break;
6735 case CMD_throw:
6736 line = compile_throw(p, &cctx);
6737 break;
6738
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006739 case CMD_eval:
6740 if (compile_expr0(&p, &cctx) == FAIL)
6741 goto erret;
6742
6743 // drop the return value
6744 generate_instr_drop(&cctx, ISN_DROP, 1);
6745
6746 line = skipwhite(p);
6747 break;
6748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006750 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006751 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006752 case CMD_echomsg:
6753 case CMD_echoerr:
6754 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006755 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006757 // TODO: other commands with an expression argument
6758
Bram Moolenaarae616492020-07-28 20:07:27 +02006759 case CMD_append:
6760 case CMD_change:
6761 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006762 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006763 case CMD_xit:
6764 not_in_vim9(&ea);
6765 goto erret;
6766
Bram Moolenaar002262f2020-07-08 17:47:57 +02006767 case CMD_SIZE:
6768 semsg(_("E476: Invalid command: %s"), ea.cmd);
6769 goto erret;
6770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006771 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006772 // Not recognized, execute with do_cmdline_cmd().
6773 ea.arg = p;
6774 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775 break;
6776 }
6777 if (line == NULL)
6778 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006779 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780
6781 if (cctx.ctx_type_stack.ga_len < 0)
6782 {
6783 iemsg("Type stack underflow");
6784 goto erret;
6785 }
6786 }
6787
6788 if (cctx.ctx_scope != NULL)
6789 {
6790 if (cctx.ctx_scope->se_type == IF_SCOPE)
6791 emsg(_(e_endif));
6792 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6793 emsg(_(e_endwhile));
6794 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6795 emsg(_(e_endfor));
6796 else
6797 emsg(_("E1026: Missing }"));
6798 goto erret;
6799 }
6800
Bram Moolenaarefd88552020-06-18 20:50:10 +02006801 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006802 {
6803 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6804 {
6805 emsg(_("E1027: Missing return statement"));
6806 goto erret;
6807 }
6808
6809 // Return zero if there is no return at the end.
6810 generate_PUSHNR(&cctx, 0);
6811 generate_instr(&cctx, ISN_RETURN);
6812 }
6813
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006814 {
6815 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6816 + ufunc->uf_dfunc_idx;
6817 dfunc->df_deleted = FALSE;
6818 dfunc->df_instr = instr->ga_data;
6819 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006820 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006821 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006822 if (cctx.ctx_outer_used)
6823 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006824 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826
6827 ret = OK;
6828
6829erret:
6830 if (ret == FAIL)
6831 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006832 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006833 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6834 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006835
6836 for (idx = 0; idx < instr->ga_len; ++idx)
6837 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006838 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006839
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006840 // If using the last entry in the table and it was added above, we
6841 // might as well remove it.
6842 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006843 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006844 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006845 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006846 ufunc->uf_dfunc_idx = 0;
6847 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006848 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006849
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006850 while (cctx.ctx_scope != NULL)
6851 drop_scope(&cctx);
6852
Bram Moolenaar20431c92020-03-20 18:39:46 +01006853 // Don't execute this function body.
6854 ga_clear_strings(&ufunc->uf_lines);
6855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006856 if (errormsg != NULL)
6857 emsg(errormsg);
6858 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006859 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860 }
6861
6862 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006863 if (do_estack_push)
6864 estack_pop();
6865
Bram Moolenaar20431c92020-03-20 18:39:46 +01006866 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006867 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006869 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870}
6871
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006872 void
6873set_function_type(ufunc_T *ufunc)
6874{
6875 int varargs = ufunc->uf_va_name != NULL;
6876 int argcount = ufunc->uf_args.ga_len;
6877
6878 // Create a type for the function, with the return type and any
6879 // argument types.
6880 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6881 // The type is included in "tt_args".
6882 if (argcount > 0 || varargs)
6883 {
6884 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6885 argcount, &ufunc->uf_type_list);
6886 // Add argument types to the function type.
6887 if (func_type_add_arg_types(ufunc->uf_func_type,
6888 argcount + varargs,
6889 &ufunc->uf_type_list) == FAIL)
6890 return;
6891 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6892 ufunc->uf_func_type->tt_min_argcount =
6893 argcount - ufunc->uf_def_args.ga_len;
6894 if (ufunc->uf_arg_types == NULL)
6895 {
6896 int i;
6897
6898 // lambda does not have argument types.
6899 for (i = 0; i < argcount; ++i)
6900 ufunc->uf_func_type->tt_args[i] = &t_any;
6901 }
6902 else
6903 mch_memmove(ufunc->uf_func_type->tt_args,
6904 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6905 if (varargs)
6906 {
6907 ufunc->uf_func_type->tt_args[argcount] =
6908 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
6909 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6910 }
6911 }
6912 else
6913 // No arguments, can use a predefined type.
6914 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6915 argcount, &ufunc->uf_type_list);
6916}
6917
6918
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919/*
6920 * Delete an instruction, free what it contains.
6921 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006922 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923delete_instr(isn_T *isn)
6924{
6925 switch (isn->isn_type)
6926 {
6927 case ISN_EXEC:
6928 case ISN_LOADENV:
6929 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006930 case ISN_LOADB:
6931 case ISN_LOADW:
6932 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006934 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 case ISN_PUSHEXC:
6936 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006937 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006939 case ISN_STOREB:
6940 case ISN_STOREW:
6941 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006942 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943 vim_free(isn->isn_arg.string);
6944 break;
6945
6946 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006947 case ISN_STORES:
6948 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006949 break;
6950
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006951 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006952 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006953 vim_free(isn->isn_arg.unlet.ul_name);
6954 break;
6955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956 case ISN_STOREOPT:
6957 vim_free(isn->isn_arg.storeopt.so_name);
6958 break;
6959
6960 case ISN_PUSHBLOB: // push blob isn_arg.blob
6961 blob_unref(isn->isn_arg.blob);
6962 break;
6963
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006964 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006965#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006966 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006967#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006968 break;
6969
6970 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006971#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006972 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006973#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006974 break;
6975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976 case ISN_UCALL:
6977 vim_free(isn->isn_arg.ufunc.cuf_name);
6978 break;
6979
Bram Moolenaar221fcc72020-05-05 19:46:20 +02006980 case ISN_FUNCREF:
6981 {
6982 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6983 + isn->isn_arg.funcref.fr_func;
6984 func_ptr_unref(dfunc->df_ufunc);
6985 }
6986 break;
6987
Bram Moolenaar38ddf332020-07-31 22:05:04 +02006988 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02006989 {
6990 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
6991 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
6992
6993 if (ufunc != NULL)
6994 {
6995 // Clear uf_dfunc_idx so that the function is deleted.
6996 clear_def_function(ufunc);
6997 ufunc->uf_dfunc_idx = 0;
6998 func_ptr_unref(ufunc);
6999 }
7000
7001 vim_free(lambda);
7002 vim_free(isn->isn_arg.newfunc.nf_global);
7003 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007004 break;
7005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007006 case ISN_2BOOL:
7007 case ISN_2STRING:
7008 case ISN_ADDBLOB:
7009 case ISN_ADDLIST:
7010 case ISN_BCALL:
7011 case ISN_CATCH:
7012 case ISN_CHECKNR:
7013 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007014 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 case ISN_COMPAREANY:
7016 case ISN_COMPAREBLOB:
7017 case ISN_COMPAREBOOL:
7018 case ISN_COMPAREDICT:
7019 case ISN_COMPAREFLOAT:
7020 case ISN_COMPAREFUNC:
7021 case ISN_COMPARELIST:
7022 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 case ISN_COMPARESPECIAL:
7024 case ISN_COMPARESTRING:
7025 case ISN_CONCAT:
7026 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007027 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028 case ISN_DROP:
7029 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007030 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007031 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007033 case ISN_EXECCONCAT:
7034 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007036 case ISN_LISTINDEX:
7037 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007038 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007039 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007040 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 case ISN_JUMP:
7042 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007043 case ISN_LOADBDICT:
7044 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007045 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007047 case ISN_LOADSCRIPT:
7048 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007050 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051 case ISN_NEGATENR:
7052 case ISN_NEWDICT:
7053 case ISN_NEWLIST:
7054 case ISN_OPNR:
7055 case ISN_OPFLOAT:
7056 case ISN_OPANY:
7057 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007058 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 case ISN_PUSHF:
7060 case ISN_PUSHNR:
7061 case ISN_PUSHBOOL:
7062 case ISN_PUSHSPEC:
7063 case ISN_RETURN:
7064 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007065 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007066 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007067 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007068 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007070 case ISN_STOREDICT:
7071 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 case ISN_THROW:
7073 case ISN_TRY:
7074 // nothing allocated
7075 break;
7076 }
7077}
7078
7079/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007080 * Free all instructions for "dfunc".
7081 */
7082 static void
7083delete_def_function_contents(dfunc_T *dfunc)
7084{
7085 int idx;
7086
7087 ga_clear(&dfunc->df_def_args_isn);
7088
7089 if (dfunc->df_instr != NULL)
7090 {
7091 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7092 delete_instr(dfunc->df_instr + idx);
7093 VIM_CLEAR(dfunc->df_instr);
7094 }
7095
7096 dfunc->df_deleted = TRUE;
7097}
7098
7099/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007100 * When a user function is deleted, clear the contents of any associated def
7101 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102 */
7103 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007104clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007106 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 {
7108 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7109 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110
Bram Moolenaar20431c92020-03-20 18:39:46 +01007111 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007112 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 }
7114}
7115
7116#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007117/*
7118 * Free all functions defined with ":def".
7119 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 void
7121free_def_functions(void)
7122{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007123 int idx;
7124
7125 for (idx = 0; idx < def_functions.ga_len; ++idx)
7126 {
7127 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7128
7129 delete_def_function_contents(dfunc);
7130 }
7131
7132 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133}
7134#endif
7135
7136
7137#endif // FEAT_EVAL