blob: 81206b3aca1b0ea25bfbf7f356938c4ba6063d17 [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 Moolenaaracd4c5e2020-06-22 19:39:03 +02001732 if (line == NULL)
1733 break;
1734 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001735 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02001736 return p;
1737 }
1738 return NULL;
1739}
1740
1741/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001742 * Called when checking for a following operator at "arg". When the rest of
1743 * the line is empty or only a comment, peek the next line. If there is a next
1744 * line return a pointer to it and set "nextp".
1745 * Otherwise skip over white space.
1746 */
1747 static char_u *
1748may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1749{
1750 char_u *p = skipwhite(arg);
1751
1752 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001753 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001754 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001755 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001756 if (*nextp != NULL)
1757 return *nextp;
1758 }
1759 return p;
1760}
1761
1762/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001763 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001764 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001765 * Returns NULL when at the end.
1766 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001767 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001768next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001769{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001770 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001771
1772 do
1773 {
1774 ++cctx->ctx_lnum;
1775 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001776 {
1777 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001778 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001779 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001780 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001781 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001782 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001783 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001784 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001785 return line;
1786}
1787
1788/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001789 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001790 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001791 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1792 */
1793 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001794may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001795{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001796 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001797 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001798 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001799
1800 if (next == NULL)
1801 return FAIL;
1802 *arg = skipwhite(next);
1803 }
1804 return OK;
1805}
1806
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001807/*
1808 * Idem, and give an error when failed.
1809 */
1810 static int
1811may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1812{
1813 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1814 {
1815 emsg(_("E1097: line incomplete"));
1816 return FAIL;
1817 }
1818 return OK;
1819}
1820
1821
Bram Moolenaara5565e42020-05-09 15:44:01 +02001822// Structure passed between the compile_expr* functions to keep track of
1823// constants that have been parsed but for which no code was produced yet. If
1824// possible expressions on these constants are applied at compile time. If
1825// that is not possible, the code to push the constants needs to be generated
1826// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001827// Using 50 should be more than enough of 5 levels of ().
1828#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001829typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001830 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001831 int pp_used; // active entries in pp_tv[]
1832} ppconst_T;
1833
Bram Moolenaar1c747212020-05-09 18:28:34 +02001834static int compile_expr0(char_u **arg, cctx_T *cctx);
1835static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1836
Bram Moolenaara5565e42020-05-09 15:44:01 +02001837/*
1838 * Generate a PUSH instruction for "tv".
1839 * "tv" will be consumed or cleared.
1840 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1841 */
1842 static int
1843generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1844{
1845 if (tv != NULL)
1846 {
1847 switch (tv->v_type)
1848 {
1849 case VAR_UNKNOWN:
1850 break;
1851 case VAR_BOOL:
1852 generate_PUSHBOOL(cctx, tv->vval.v_number);
1853 break;
1854 case VAR_SPECIAL:
1855 generate_PUSHSPEC(cctx, tv->vval.v_number);
1856 break;
1857 case VAR_NUMBER:
1858 generate_PUSHNR(cctx, tv->vval.v_number);
1859 break;
1860#ifdef FEAT_FLOAT
1861 case VAR_FLOAT:
1862 generate_PUSHF(cctx, tv->vval.v_float);
1863 break;
1864#endif
1865 case VAR_BLOB:
1866 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1867 tv->vval.v_blob = NULL;
1868 break;
1869 case VAR_STRING:
1870 generate_PUSHS(cctx, tv->vval.v_string);
1871 tv->vval.v_string = NULL;
1872 break;
1873 default:
1874 iemsg("constant type not supported");
1875 clear_tv(tv);
1876 return FAIL;
1877 }
1878 tv->v_type = VAR_UNKNOWN;
1879 }
1880 return OK;
1881}
1882
1883/*
1884 * Generate code for any ppconst entries.
1885 */
1886 static int
1887generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1888{
1889 int i;
1890 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001891 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001892
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001893 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001894 for (i = 0; i < ppconst->pp_used; ++i)
1895 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1896 ret = FAIL;
1897 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001898 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001899 return ret;
1900}
1901
1902/*
1903 * Clear ppconst constants. Used when failing.
1904 */
1905 static void
1906clear_ppconst(ppconst_T *ppconst)
1907{
1908 int i;
1909
1910 for (i = 0; i < ppconst->pp_used; ++i)
1911 clear_tv(&ppconst->pp_tv[i]);
1912 ppconst->pp_used = 0;
1913}
1914
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001915/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001916 * Generate an instruction to load script-local variable "name", without the
1917 * leading "s:".
1918 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 */
1920 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001921compile_load_scriptvar(
1922 cctx_T *cctx,
1923 char_u *name, // variable NUL terminated
1924 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001925 char_u **end, // end of variable
1926 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001928 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1930 imported_T *import;
1931
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001932 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001934 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001935 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1936 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 }
1938 if (idx >= 0)
1939 {
1940 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1941
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001942 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 current_sctx.sc_sid, idx, sv->sv_type);
1944 return OK;
1945 }
1946
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001947 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 if (import != NULL)
1949 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001950 if (import->imp_all)
1951 {
1952 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02001953 char_u *exp_name;
1954 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001955 ufunc_T *ufunc;
1956 type_T *type;
1957
1958 // Used "import * as Name", need to lookup the member.
1959 if (*p != '.')
1960 {
1961 semsg(_("E1060: expected dot after name: %s"), start);
1962 return FAIL;
1963 }
1964 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001965 if (VIM_ISWHITE(*p))
1966 {
1967 emsg(_("E1074: no white space allowed after dot"));
1968 return FAIL;
1969 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001970
Bram Moolenaar1c991142020-07-04 13:15:31 +02001971 // isolate one name
1972 exp_name = p;
1973 while (eval_isnamec(*p))
1974 ++p;
1975 cc = *p;
1976 *p = NUL;
1977
1978 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
1979 *p = cc;
1980 p = skipwhite(p);
1981
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001982 // TODO: what if it is a function?
1983 if (idx < 0)
1984 return FAIL;
1985 *end = p;
1986
1987 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1988 import->imp_sid,
1989 idx,
1990 type);
1991 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001992 else if (import->imp_funcname != NULL)
1993 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001994 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001995 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1996 import->imp_sid,
1997 import->imp_var_vals_idx,
1998 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 return OK;
2000 }
2001
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002002 if (error)
2003 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 return FAIL;
2005}
2006
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002007 static int
2008generate_funcref(cctx_T *cctx, char_u *name)
2009{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002010 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002011
2012 if (ufunc == NULL)
2013 return FAIL;
2014
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002015 // Need to compile any default values to get the argument types.
2016 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2017 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2018 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002019 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002020}
2021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022/*
2023 * Compile a variable name into a load instruction.
2024 * "end" points to just after the name.
2025 * When "error" is FALSE do not give an error when not found.
2026 */
2027 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002028compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029{
2030 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002031 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002032 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002034 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035
2036 if (*(*arg + 1) == ':')
2037 {
2038 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002039 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002040 {
2041 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002043 switch (**arg)
2044 {
2045 case 'g': isn_type = ISN_LOADGDICT; break;
2046 case 'w': isn_type = ISN_LOADWDICT; break;
2047 case 't': isn_type = ISN_LOADTDICT; break;
2048 case 'b': isn_type = ISN_LOADBDICT; break;
2049 default:
2050 semsg(_(e_namespace), *arg);
2051 goto theend;
2052 }
2053 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2054 goto theend;
2055 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 else
2058 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002059 isntype_T isn_type = ISN_DROP;
2060
2061 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2062 if (name == NULL)
2063 return FAIL;
2064
2065 switch (**arg)
2066 {
2067 case 'v': res = generate_LOADV(cctx, name, error);
2068 break;
2069 case 's': res = compile_load_scriptvar(cctx, name,
2070 NULL, NULL, error);
2071 break;
2072 case 'g': isn_type = ISN_LOADG; break;
2073 case 'w': isn_type = ISN_LOADW; break;
2074 case 't': isn_type = ISN_LOADT; break;
2075 case 'b': isn_type = ISN_LOADB; break;
2076 default: semsg(_(e_namespace), *arg);
2077 goto theend;
2078 }
2079 if (isn_type != ISN_DROP)
2080 {
2081 // Global, Buffer-local, Window-local and Tabpage-local
2082 // variables can be defined later, thus we don't check if it
2083 // exists, give error at runtime.
2084 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 }
2087 }
2088 else
2089 {
2090 size_t len = end - *arg;
2091 int idx;
2092 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002093 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094
2095 name = vim_strnsave(*arg, end - *arg);
2096 if (name == NULL)
2097 return FAIL;
2098
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002099 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002101 if (!gen_load_outer)
2102 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 }
2104 else
2105 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002106 lvar_T *lvar = lookup_local(*arg, len, cctx);
2107
2108 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002110 type = lvar->lv_type;
2111 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002112 if (lvar->lv_from_outer)
2113 gen_load_outer = TRUE;
2114 else
2115 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 }
2117 else
2118 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002119 // "var" can be script-local even without using "s:" if it
2120 // already exists.
2121 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2122 == SCRIPT_VERSION_VIM9
2123 || lookup_script(*arg, len) == OK)
2124 res = compile_load_scriptvar(cctx, name, *arg, &end,
2125 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002126
Bram Moolenaara5565e42020-05-09 15:44:01 +02002127 // When the name starts with an uppercase letter or "x:" it
2128 // can be a user defined function.
2129 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2130 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 }
2132 }
2133 if (gen_load)
2134 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002135 if (gen_load_outer)
2136 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 }
2138
2139 *arg = end;
2140
2141theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002142 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 semsg(_(e_var_notfound), name);
2144 vim_free(name);
2145 return res;
2146}
2147
2148/*
2149 * Compile the argument expressions.
2150 * "arg" points to just after the "(" and is advanced to after the ")"
2151 */
2152 static int
2153compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2154{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002155 char_u *p = *arg;
2156 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157
Bram Moolenaare6085c52020-04-12 20:19:16 +02002158 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002160 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2161 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002162 if (*p == ')')
2163 {
2164 *arg = p + 1;
2165 return OK;
2166 }
2167
Bram Moolenaara5565e42020-05-09 15:44:01 +02002168 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 return FAIL;
2170 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002171
2172 if (*p != ',' && *skipwhite(p) == ',')
2173 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002174 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002175 p = skipwhite(p);
2176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002178 {
2179 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002180 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002181 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002182 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002183 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002184 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002186failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002187 emsg(_(e_missing_close));
2188 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189}
2190
2191/*
2192 * Compile a function call: name(arg1, arg2)
2193 * "arg" points to "name", "arg + varlen" to the "(".
2194 * "argcount_init" is 1 for "value->method()"
2195 * Instructions:
2196 * EVAL arg1
2197 * EVAL arg2
2198 * BCALL / DCALL / UCALL
2199 */
2200 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002201compile_call(
2202 char_u **arg,
2203 size_t varlen,
2204 cctx_T *cctx,
2205 ppconst_T *ppconst,
2206 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207{
2208 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002209 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 int argcount = argcount_init;
2211 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002212 char_u fname_buf[FLEN_FIXED + 1];
2213 char_u *tofree = NULL;
2214 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002216 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217
Bram Moolenaara5565e42020-05-09 15:44:01 +02002218 // we can evaluate "has('name')" at compile time
2219 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2220 {
2221 char_u *s = skipwhite(*arg + varlen + 1);
2222 typval_T argvars[2];
2223
2224 argvars[0].v_type = VAR_UNKNOWN;
2225 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002226 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002227 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002228 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002229 s = skipwhite(s);
2230 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2231 {
2232 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2233
2234 *arg = s + 1;
2235 argvars[1].v_type = VAR_UNKNOWN;
2236 tv->v_type = VAR_NUMBER;
2237 tv->vval.v_number = 0;
2238 f_has(argvars, tv);
2239 clear_tv(&argvars[0]);
2240 ++ppconst->pp_used;
2241 return OK;
2242 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002243 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002244 }
2245
2246 if (generate_ppconst(cctx, ppconst) == FAIL)
2247 return FAIL;
2248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 if (varlen >= sizeof(namebuf))
2250 {
2251 semsg(_("E1011: name too long: %s"), name);
2252 return FAIL;
2253 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002254 vim_strncpy(namebuf, *arg, varlen);
2255 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256
2257 *arg = skipwhite(*arg + varlen + 1);
2258 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002259 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002261 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262 {
2263 int idx;
2264
2265 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002266 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002268 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002269 else
2270 semsg(_(e_unknownfunc), namebuf);
2271 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 }
2273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002275 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002277 {
2278 res = generate_CALL(cctx, ufunc, argcount);
2279 goto theend;
2280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281
2282 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002283 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002285 if (STRNCMP(namebuf, "g:", 2) != 0
2286 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002287 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002288 garray_T *stack = &cctx->ctx_type_stack;
2289 type_T *type;
2290
2291 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2292 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002293 goto theend;
2294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002296 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002297 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002298 if (STRNCMP(namebuf, "g:", 2) == 0)
2299 res = generate_UCALL(cctx, name, argcount);
2300 else
2301 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002302
2303theend:
2304 vim_free(tofree);
2305 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306}
2307
2308// like NAMESPACE_CHAR but with 'a' and 'l'.
2309#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2310
2311/*
2312 * Find the end of a variable or function name. Unlike find_name_end() this
2313 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002314 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 * Return a pointer to just after the name. Equal to "arg" if there is no
2316 * valid name.
2317 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002318 static char_u *
2319to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320{
2321 char_u *p;
2322
2323 // Quick check for valid starting character.
2324 if (!eval_isnamec1(*arg))
2325 return arg;
2326
2327 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2328 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2329 // and can be used in slice "[n:]".
2330 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002331 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2333 break;
2334 return p;
2335}
2336
2337/*
2338 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002339 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 */
2341 char_u *
2342to_name_const_end(char_u *arg)
2343{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002344 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 typval_T rettv;
2346
2347 if (p == arg && *arg == '[')
2348 {
2349
2350 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002351 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 p = arg;
2353 }
2354 else if (p == arg && *arg == '#' && arg[1] == '{')
2355 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002356 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002358 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 p = arg;
2360 }
2361 else if (p == arg && *arg == '{')
2362 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002363 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002365 // Can be "{x -> ret}()".
2366 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002368 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369 if (ret != OK)
2370 p = arg;
2371 }
2372
2373 return p;
2374}
2375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376/*
2377 * parse a list: [expr, expr]
2378 * "*arg" points to the '['.
2379 */
2380 static int
2381compile_list(char_u **arg, cctx_T *cctx)
2382{
2383 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002384 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 int count = 0;
2386
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002387 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002389 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002390 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002391 semsg(_(e_list_end), *arg);
2392 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002393 }
2394 if (*p == ']')
2395 {
2396 ++p;
2397 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002398 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002399 p += STRLEN(p);
2400 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002401 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002402 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 break;
2404 ++count;
2405 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002406 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002408 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2409 {
2410 semsg(_(e_white_after), ",");
2411 return FAIL;
2412 }
2413 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002414 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 p = skipwhite(p);
2416 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002417 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418
2419 generate_NEWLIST(cctx, count);
2420 return OK;
2421}
2422
2423/*
2424 * parse a lambda: {arg, arg -> expr}
2425 * "*arg" points to the '{'.
2426 */
2427 static int
2428compile_lambda(char_u **arg, cctx_T *cctx)
2429{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 typval_T rettv;
2431 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002432 evalarg_T evalarg;
2433
2434 CLEAR_FIELD(evalarg);
2435 evalarg.eval_flags = EVAL_EVALUATE;
2436 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437
2438 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002439 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002443 ++ufunc->uf_refcount;
2444 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002445 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446
2447 // The function will have one line: "return {expr}".
2448 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002449 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002451 clear_evalarg(&evalarg, NULL);
2452
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002453 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002454 {
2455 // The return type will now be known.
2456 set_function_type(ufunc);
2457
2458 return generate_FUNCREF(cctx, ufunc);
2459 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002460
2461 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 return FAIL;
2463}
2464
2465/*
2466 * Compile a lamda call: expr->{lambda}(args)
2467 * "arg" points to the "{".
2468 */
2469 static int
2470compile_lambda_call(char_u **arg, cctx_T *cctx)
2471{
2472 ufunc_T *ufunc;
2473 typval_T rettv;
2474 int argcount = 1;
2475 int ret = FAIL;
2476
2477 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002478 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 return FAIL;
2480
2481 if (**arg != '(')
2482 {
2483 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002484 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 else
2486 semsg(_(e_missing_paren), "lambda");
2487 clear_tv(&rettv);
2488 return FAIL;
2489 }
2490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 ufunc = rettv.vval.v_partial->pt_func;
2492 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002493 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002494 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002495
2496 // The function will have one line: "return {expr}".
2497 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002498 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499
2500 // compile the arguments
2501 *arg = skipwhite(*arg + 1);
2502 if (compile_arguments(arg, cctx, &argcount) == OK)
2503 // call the compiled function
2504 ret = generate_CALL(cctx, ufunc, argcount);
2505
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002506 if (ret == FAIL)
2507 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 return ret;
2509}
2510
2511/*
2512 * parse a dict: {'key': val} or #{key: val}
2513 * "*arg" points to the '{'.
2514 */
2515 static int
2516compile_dict(char_u **arg, cctx_T *cctx, int literal)
2517{
2518 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002519 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 int count = 0;
2521 dict_T *d = dict_alloc();
2522 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002523 char_u *whitep = *arg;
2524 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525
2526 if (d == NULL)
2527 return FAIL;
2528 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002529 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 {
2531 char_u *key = NULL;
2532
Bram Moolenaar23c55272020-06-21 16:58:13 +02002533 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002534 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002535 *arg = NULL;
2536 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002537 }
2538
2539 if (**arg == '}')
2540 break;
2541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 if (literal)
2543 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002544 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545
Bram Moolenaar2c330432020-04-13 14:41:35 +02002546 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 {
2548 semsg(_("E1014: Invalid key: %s"), *arg);
2549 return FAIL;
2550 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002551 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 if (generate_PUSHS(cctx, key) == FAIL)
2553 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002554 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 }
2556 else
2557 {
2558 isn_T *isn;
2559
Bram Moolenaara5565e42020-05-09 15:44:01 +02002560 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2563 if (isn->isn_type == ISN_PUSHS)
2564 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002565 else
2566 {
2567 type_T *keytype = ((type_T **)stack->ga_data)
2568 [stack->ga_len - 1];
2569 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2570 return FAIL;
2571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 }
2573
2574 // Check for duplicate keys, if using string keys.
2575 if (key != NULL)
2576 {
2577 item = dict_find(d, key, -1);
2578 if (item != NULL)
2579 {
2580 semsg(_(e_duplicate_key), key);
2581 goto failret;
2582 }
2583 item = dictitem_alloc(key);
2584 if (item != NULL)
2585 {
2586 item->di_tv.v_type = VAR_UNKNOWN;
2587 item->di_tv.v_lock = 0;
2588 if (dict_add(d, item) == FAIL)
2589 dictitem_free(item);
2590 }
2591 }
2592
2593 *arg = skipwhite(*arg);
2594 if (**arg != ':')
2595 {
2596 semsg(_(e_missing_dict_colon), *arg);
2597 return FAIL;
2598 }
2599
Bram Moolenaar2c330432020-04-13 14:41:35 +02002600 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002602 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002603 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002604 *arg = NULL;
2605 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002606 }
2607
Bram Moolenaara5565e42020-05-09 15:44:01 +02002608 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 return FAIL;
2610 ++count;
2611
Bram Moolenaar2c330432020-04-13 14:41:35 +02002612 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002613 *arg = skipwhite(*arg);
2614 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002615 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002616 *arg = NULL;
2617 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002618 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 if (**arg == '}')
2620 break;
2621 if (**arg != ',')
2622 {
2623 semsg(_(e_missing_dict_comma), *arg);
2624 goto failret;
2625 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002626 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 *arg = skipwhite(*arg + 1);
2628 }
2629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 *arg = *arg + 1;
2631
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002632 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002633 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002634 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002635 *arg += STRLEN(*arg);
2636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 dict_unref(d);
2638 return generate_NEWDICT(cctx, count);
2639
2640failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002641 if (*arg == NULL)
2642 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 dict_unref(d);
2644 return FAIL;
2645}
2646
2647/*
2648 * Compile "&option".
2649 */
2650 static int
2651compile_get_option(char_u **arg, cctx_T *cctx)
2652{
2653 typval_T rettv;
2654 char_u *start = *arg;
2655 int ret;
2656
2657 // parse the option and get the current value to get the type.
2658 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002659 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 if (ret == OK)
2661 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002662 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 char_u *name = vim_strnsave(start, *arg - start);
2664 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2665
2666 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2667 vim_free(name);
2668 }
2669 clear_tv(&rettv);
2670
2671 return ret;
2672}
2673
2674/*
2675 * Compile "$VAR".
2676 */
2677 static int
2678compile_get_env(char_u **arg, cctx_T *cctx)
2679{
2680 char_u *start = *arg;
2681 int len;
2682 int ret;
2683 char_u *name;
2684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 ++*arg;
2686 len = get_env_len(arg);
2687 if (len == 0)
2688 {
2689 semsg(_(e_syntax_at), start - 1);
2690 return FAIL;
2691 }
2692
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002693 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 name = vim_strnsave(start, len + 1);
2695 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2696 vim_free(name);
2697 return ret;
2698}
2699
2700/*
2701 * Compile "@r".
2702 */
2703 static int
2704compile_get_register(char_u **arg, cctx_T *cctx)
2705{
2706 int ret;
2707
2708 ++*arg;
2709 if (**arg == NUL)
2710 {
2711 semsg(_(e_syntax_at), *arg - 1);
2712 return FAIL;
2713 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002714 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 {
2716 emsg_invreg(**arg);
2717 return FAIL;
2718 }
2719 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2720 ++*arg;
2721 return ret;
2722}
2723
2724/*
2725 * Apply leading '!', '-' and '+' to constant "rettv".
2726 */
2727 static int
2728apply_leader(typval_T *rettv, char_u *start, char_u *end)
2729{
2730 char_u *p = end;
2731
2732 // this works from end to start
2733 while (p > start)
2734 {
2735 --p;
2736 if (*p == '-' || *p == '+')
2737 {
2738 // only '-' has an effect, for '+' we only check the type
2739#ifdef FEAT_FLOAT
2740 if (rettv->v_type == VAR_FLOAT)
2741 {
2742 if (*p == '-')
2743 rettv->vval.v_float = -rettv->vval.v_float;
2744 }
2745 else
2746#endif
2747 {
2748 varnumber_T val;
2749 int error = FALSE;
2750
2751 // tv_get_number_chk() accepts a string, but we don't want that
2752 // here
2753 if (check_not_string(rettv) == FAIL)
2754 return FAIL;
2755 val = tv_get_number_chk(rettv, &error);
2756 clear_tv(rettv);
2757 if (error)
2758 return FAIL;
2759 if (*p == '-')
2760 val = -val;
2761 rettv->v_type = VAR_NUMBER;
2762 rettv->vval.v_number = val;
2763 }
2764 }
2765 else
2766 {
2767 int v = tv2bool(rettv);
2768
2769 // '!' is permissive in the type.
2770 clear_tv(rettv);
2771 rettv->v_type = VAR_BOOL;
2772 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2773 }
2774 }
2775 return OK;
2776}
2777
2778/*
2779 * Recognize v: variables that are constants and set "rettv".
2780 */
2781 static void
2782get_vim_constant(char_u **arg, typval_T *rettv)
2783{
2784 if (STRNCMP(*arg, "v:true", 6) == 0)
2785 {
2786 rettv->v_type = VAR_BOOL;
2787 rettv->vval.v_number = VVAL_TRUE;
2788 *arg += 6;
2789 }
2790 else if (STRNCMP(*arg, "v:false", 7) == 0)
2791 {
2792 rettv->v_type = VAR_BOOL;
2793 rettv->vval.v_number = VVAL_FALSE;
2794 *arg += 7;
2795 }
2796 else if (STRNCMP(*arg, "v:null", 6) == 0)
2797 {
2798 rettv->v_type = VAR_SPECIAL;
2799 rettv->vval.v_number = VVAL_NULL;
2800 *arg += 6;
2801 }
2802 else if (STRNCMP(*arg, "v:none", 6) == 0)
2803 {
2804 rettv->v_type = VAR_SPECIAL;
2805 rettv->vval.v_number = VVAL_NONE;
2806 *arg += 6;
2807 }
2808}
2809
Bram Moolenaar696ba232020-07-29 21:20:41 +02002810 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002811get_compare_type(char_u *p, int *len, int *type_is)
2812{
2813 exptype_T type = EXPR_UNKNOWN;
2814 int i;
2815
2816 switch (p[0])
2817 {
2818 case '=': if (p[1] == '=')
2819 type = EXPR_EQUAL;
2820 else if (p[1] == '~')
2821 type = EXPR_MATCH;
2822 break;
2823 case '!': if (p[1] == '=')
2824 type = EXPR_NEQUAL;
2825 else if (p[1] == '~')
2826 type = EXPR_NOMATCH;
2827 break;
2828 case '>': if (p[1] != '=')
2829 {
2830 type = EXPR_GREATER;
2831 *len = 1;
2832 }
2833 else
2834 type = EXPR_GEQUAL;
2835 break;
2836 case '<': if (p[1] != '=')
2837 {
2838 type = EXPR_SMALLER;
2839 *len = 1;
2840 }
2841 else
2842 type = EXPR_SEQUAL;
2843 break;
2844 case 'i': if (p[1] == 's')
2845 {
2846 // "is" and "isnot"; but not a prefix of a name
2847 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2848 *len = 5;
2849 i = p[*len];
2850 if (!isalnum(i) && i != '_')
2851 {
2852 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2853 *type_is = TRUE;
2854 }
2855 }
2856 break;
2857 }
2858 return type;
2859}
2860
2861/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 * Compile code to apply '-', '+' and '!'.
2863 */
2864 static int
2865compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2866{
2867 char_u *p = end;
2868
2869 // this works from end to start
2870 while (p > start)
2871 {
2872 --p;
2873 if (*p == '-' || *p == '+')
2874 {
2875 int negate = *p == '-';
2876 isn_T *isn;
2877
2878 // TODO: check type
2879 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2880 {
2881 --p;
2882 if (*p == '-')
2883 negate = !negate;
2884 }
2885 // only '-' has an effect, for '+' we only check the type
2886 if (negate)
2887 isn = generate_instr(cctx, ISN_NEGATENR);
2888 else
2889 isn = generate_instr(cctx, ISN_CHECKNR);
2890 if (isn == NULL)
2891 return FAIL;
2892 }
2893 else
2894 {
2895 int invert = TRUE;
2896
2897 while (p > start && p[-1] == '!')
2898 {
2899 --p;
2900 invert = !invert;
2901 }
2902 if (generate_2BOOL(cctx, invert) == FAIL)
2903 return FAIL;
2904 }
2905 }
2906 return OK;
2907}
2908
2909/*
2910 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002911 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 */
2913 static int
2914compile_subscript(
2915 char_u **arg,
2916 cctx_T *cctx,
2917 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02002918 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002919 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920{
2921 for (;;)
2922 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002923 char_u *p = skipwhite(*arg);
2924
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002925 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002926 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002927 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002928
2929 // If a following line starts with "->{" or "->X" advance to that
2930 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002931 // Also if a following line starts with ".x".
2932 if (next != NULL &&
2933 ((next[0] == '-' && next[1] == '>'
2934 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02002935 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002936 {
2937 next = next_line_from_context(cctx, TRUE);
2938 if (next == NULL)
2939 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002940 *arg = next;
2941 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002942 }
2943 }
2944
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02002945 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
2946 // not a function call.
2947 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002949 garray_T *stack = &cctx->ctx_type_stack;
2950 type_T *type;
2951 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002953 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02002954 return FAIL;
2955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002957 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2958
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002959 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2961 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002962 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 return FAIL;
2964 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002965 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02002967 char_u *pstart = p;
2968
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002969 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02002970 return FAIL;
2971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 // something->method()
2973 // Apply the '!', '-' and '+' first:
2974 // -1.0->func() works like (-1.0)->func()
2975 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2976 return FAIL;
2977 *start_leader = end_leader; // don't apply again later
2978
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002979 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02002980 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02002981 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 if (**arg == '{')
2983 {
2984 // lambda call: list->{lambda}
2985 if (compile_lambda_call(arg, cctx) == FAIL)
2986 return FAIL;
2987 }
2988 else
2989 {
2990 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002991 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02002992 if (!eval_isnamec1(*p))
2993 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02002994 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02002995 return FAIL;
2996 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002997 if (ASCII_ISALPHA(*p) && p[1] == ':')
2998 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02002999 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 ;
3001 if (*p != '(')
3002 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003003 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 return FAIL;
3005 }
3006 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003007 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 return FAIL;
3009 }
3010 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003011 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003013 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003014 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003015 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003016
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003017 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003018 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003019 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003020 // TODO: blob index
3021 // TODO: more arguments
3022 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003023 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003024 return FAIL;
3025
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003026 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003027 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003028 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003029 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003030 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 return FAIL;
3032
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003033 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3034 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 if (**arg != ']')
3036 {
3037 emsg(_(e_missbrac));
3038 return FAIL;
3039 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003040 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003042 // We can index a list and a dict. If we don't know the type
3043 // we can use the index value type.
3044 // TODO: If we don't know use an instruction to figure it out at
3045 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003046 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003047 vtype = (*typep)->tt_type;
3048 if (*typep == &t_any)
3049 {
3050 type_T *valtype = ((type_T **)stack->ga_data)
3051 [stack->ga_len - 1];
3052 if (valtype == &t_string)
3053 vtype = VAR_DICT;
3054 }
3055 if (vtype == VAR_DICT)
3056 {
3057 if ((*typep)->tt_type == VAR_DICT)
3058 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003059 else
3060 {
3061 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3062 return FAIL;
3063 *typep = &t_any;
3064 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003065 if (may_generate_2STRING(-1, cctx) == FAIL)
3066 return FAIL;
3067 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3068 return FAIL;
3069 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003070 else if (vtype == VAR_STRING)
3071 {
3072 *typep = &t_number;
3073 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3074 return FAIL;
3075 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003076 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003077 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003078 if ((*typep)->tt_type == VAR_LIST)
3079 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003080 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003081 return FAIL;
3082 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003083 else
3084 {
3085 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003086 return FAIL;
3087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003089 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003091 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003092 return FAIL;
3093
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003094 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003095 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3096 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003098 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003099 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 while (eval_isnamec(*p))
3101 MB_PTR_ADV(p);
3102 if (p == *arg)
3103 {
3104 semsg(_(e_syntax_at), *arg);
3105 return FAIL;
3106 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003107 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 return FAIL;
3109 *arg = p;
3110 }
3111 else
3112 break;
3113 }
3114
3115 // TODO - see handle_subscript():
3116 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3117 // Don't do this when "Func" is already a partial that was bound
3118 // explicitly (pt_auto is FALSE).
3119
3120 return OK;
3121}
3122
3123/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003124 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3125 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003127 * If the value is a constant "ppconst->pp_ret" will be set.
3128 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003129 *
3130 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 */
3132
3133/*
3134 * number number constant
3135 * 0zFFFFFFFF Blob constant
3136 * "string" string constant
3137 * 'string' literal string constant
3138 * &option-name option value
3139 * @r register contents
3140 * identifier variable value
3141 * function() function call
3142 * $VAR environment variable
3143 * (expression) nested expression
3144 * [expr, expr] List
3145 * {key: val, key: val} Dictionary
3146 * #{key: val, key: val} Dictionary with literal keys
3147 *
3148 * Also handle:
3149 * ! in front logical NOT
3150 * - in front unary minus
3151 * + in front unary plus (ignored)
3152 * trailing (arg) funcref/partial call
3153 * trailing [] subscript in String or List
3154 * trailing .name entry in Dictionary
3155 * trailing ->name() method call
3156 */
3157 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003158compile_expr7(
3159 char_u **arg,
3160 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003161 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 char_u *start_leader, *end_leader;
3164 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003165 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003166 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167
3168 /*
3169 * Skip '!', '-' and '+' characters. They are handled later.
3170 */
3171 start_leader = *arg;
3172 while (**arg == '!' || **arg == '-' || **arg == '+')
3173 *arg = skipwhite(*arg + 1);
3174 end_leader = *arg;
3175
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003176 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 switch (**arg)
3178 {
3179 /*
3180 * Number constant.
3181 */
3182 case '0': // also for blob starting with 0z
3183 case '1':
3184 case '2':
3185 case '3':
3186 case '4':
3187 case '5':
3188 case '6':
3189 case '7':
3190 case '8':
3191 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003192 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 return FAIL;
3194 break;
3195
3196 /*
3197 * String constant: "string".
3198 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003199 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 return FAIL;
3201 break;
3202
3203 /*
3204 * Literal string constant: 'str''ing'.
3205 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003206 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 return FAIL;
3208 break;
3209
3210 /*
3211 * Constant Vim variable.
3212 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003213 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 ret = NOTDONE;
3215 break;
3216
3217 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003218 * "true" constant
3219 */
3220 case 't': if (STRNCMP(*arg, "true", 4) == 0
3221 && !eval_isnamec((*arg)[4]))
3222 {
3223 *arg += 4;
3224 rettv->v_type = VAR_BOOL;
3225 rettv->vval.v_number = VVAL_TRUE;
3226 }
3227 else
3228 ret = NOTDONE;
3229 break;
3230
3231 /*
3232 * "false" constant
3233 */
3234 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3235 && !eval_isnamec((*arg)[5]))
3236 {
3237 *arg += 5;
3238 rettv->v_type = VAR_BOOL;
3239 rettv->vval.v_number = VVAL_FALSE;
3240 }
3241 else
3242 ret = NOTDONE;
3243 break;
3244
3245 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 * List: [expr, expr]
3247 */
3248 case '[': ret = compile_list(arg, cctx);
3249 break;
3250
3251 /*
3252 * Dictionary: #{key: val, key: val}
3253 */
3254 case '#': if ((*arg)[1] == '{')
3255 {
3256 ++*arg;
3257 ret = compile_dict(arg, cctx, TRUE);
3258 }
3259 else
3260 ret = NOTDONE;
3261 break;
3262
3263 /*
3264 * Lambda: {arg, arg -> expr}
3265 * Dictionary: {'key': val, 'key': val}
3266 */
3267 case '{': {
3268 char_u *start = skipwhite(*arg + 1);
3269
3270 // Find out what comes after the arguments.
3271 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003272 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 if (ret != FAIL && *start == '>')
3274 ret = compile_lambda(arg, cctx);
3275 else
3276 ret = compile_dict(arg, cctx, FALSE);
3277 }
3278 break;
3279
3280 /*
3281 * Option value: &name
3282 */
3283 case '&': ret = compile_get_option(arg, cctx);
3284 break;
3285
3286 /*
3287 * Environment variable: $VAR.
3288 */
3289 case '$': ret = compile_get_env(arg, cctx);
3290 break;
3291
3292 /*
3293 * Register contents: @r.
3294 */
3295 case '@': ret = compile_get_register(arg, cctx);
3296 break;
3297 /*
3298 * nested expression: (expression).
3299 */
3300 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003301
3302 // recursive!
3303 if (ppconst->pp_used <= PPSIZE - 10)
3304 {
3305 ret = compile_expr1(arg, cctx, ppconst);
3306 }
3307 else
3308 {
3309 // Not enough space in ppconst, flush constants.
3310 if (generate_ppconst(cctx, ppconst) == FAIL)
3311 return FAIL;
3312 ret = compile_expr0(arg, cctx);
3313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 *arg = skipwhite(*arg);
3315 if (**arg == ')')
3316 ++*arg;
3317 else if (ret == OK)
3318 {
3319 emsg(_(e_missing_close));
3320 ret = FAIL;
3321 }
3322 break;
3323
3324 default: ret = NOTDONE;
3325 break;
3326 }
3327 if (ret == FAIL)
3328 return FAIL;
3329
Bram Moolenaar1c747212020-05-09 18:28:34 +02003330 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 {
3332 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003333 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003335 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 return FAIL;
3337 }
3338 start_leader = end_leader; // don't apply again below
3339
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003340 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003341 clear_tv(rettv);
3342 else
3343 // A constant expression can possibly be handled compile time,
3344 // return the value instead of generating code.
3345 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 }
3347 else if (ret == NOTDONE)
3348 {
3349 char_u *p;
3350 int r;
3351
3352 if (!eval_isnamec1(**arg))
3353 {
3354 semsg(_("E1015: Name expected: %s"), *arg);
3355 return FAIL;
3356 }
3357
3358 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003359 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003361 {
3362 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3363 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003365 {
3366 if (generate_ppconst(cctx, ppconst) == FAIL)
3367 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 if (r == FAIL)
3371 return FAIL;
3372 }
3373
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003374 // Handle following "[]", ".member", etc.
3375 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003376 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003377 ppconst) == FAIL)
3378 return FAIL;
3379 if (ppconst->pp_used > 0)
3380 {
3381 // apply the '!', '-' and '+' before the constant
3382 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3383 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3384 return FAIL;
3385 return OK;
3386 }
3387 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003389 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390}
3391
3392/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003393 * Give the "white on both sides" error, taking the operator from "p[len]".
3394 */
3395 void
3396error_white_both(char_u *op, int len)
3397{
3398 char_u buf[10];
3399
3400 vim_strncpy(buf, op, len);
3401 semsg(_(e_white_both), buf);
3402}
3403
3404/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003405 * <type>expr7: runtime type check / conversion
3406 */
3407 static int
3408compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3409{
3410 type_T *want_type = NULL;
3411
3412 // Recognize <type>
3413 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3414 {
3415 int called_emsg_before = called_emsg;
3416
3417 ++*arg;
3418 want_type = parse_type(arg, cctx->ctx_type_list);
3419 if (called_emsg != called_emsg_before)
3420 return FAIL;
3421
3422 if (**arg != '>')
3423 {
3424 if (*skipwhite(*arg) == '>')
3425 semsg(_(e_no_white_before), ">");
3426 else
3427 emsg(_("E1104: Missing >"));
3428 return FAIL;
3429 }
3430 ++*arg;
3431 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3432 return FAIL;
3433 }
3434
3435 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3436 return FAIL;
3437
3438 if (want_type != NULL)
3439 {
3440 garray_T *stack = &cctx->ctx_type_stack;
3441 type_T *actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3442
3443 if (check_type(want_type, actual, FALSE) == FAIL)
3444 {
3445 generate_ppconst(cctx, ppconst);
3446 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3447 return FAIL;
3448 }
3449 }
3450
3451 return OK;
3452}
3453
3454/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 * * number multiplication
3456 * / number division
3457 * % number modulo
3458 */
3459 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003460compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461{
3462 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003463 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003464 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003466 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003467 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 return FAIL;
3469
3470 /*
3471 * Repeat computing, until no "*", "/" or "%" is following.
3472 */
3473 for (;;)
3474 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003475 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 if (*op != '*' && *op != '/' && *op != '%')
3477 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003478 if (next != NULL)
3479 {
3480 *arg = next_line_from_context(cctx, TRUE);
3481 op = skipwhite(*arg);
3482 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003483
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003484 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003486 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003487 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 }
3489 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003490 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003491 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003493 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003494 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003496
3497 if (ppconst->pp_used == ppconst_used + 2
3498 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3499 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003500 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003501 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3502 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003503 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003505 // both are numbers: compute the result
3506 switch (*op)
3507 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003508 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003509 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003510 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003511 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003512 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003513 break;
3514 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003515 tv1->vval.v_number = res;
3516 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003517 }
3518 else
3519 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003520 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003521 generate_two_op(cctx, op);
3522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 }
3524
3525 return OK;
3526}
3527
3528/*
3529 * + number addition
3530 * - number subtraction
3531 * .. string concatenation
3532 */
3533 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003534compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535{
3536 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003537 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003539 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540
3541 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003542 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 return FAIL;
3544
3545 /*
3546 * Repeat computing, until no "+", "-" or ".." is following.
3547 */
3548 for (;;)
3549 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003550 op = may_peek_next_line(cctx, *arg, &next);
3551 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 break;
3553 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003554 if (next != NULL)
3555 {
3556 *arg = next_line_from_context(cctx, TRUE);
3557 op = skipwhite(*arg);
3558 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003560 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003562 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003563 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 }
3565
3566 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003567 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003568 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003570 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003571 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 return FAIL;
3573
Bram Moolenaara5565e42020-05-09 15:44:01 +02003574 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003575 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003576 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3577 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3578 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3579 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003581 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3582 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003583
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003584 // concat/subtract/add constant numbers
3585 if (*op == '+')
3586 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3587 else if (*op == '-')
3588 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3589 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003590 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003591 // concatenate constant strings
3592 char_u *s1 = tv1->vval.v_string;
3593 char_u *s2 = tv2->vval.v_string;
3594 size_t len1 = STRLEN(s1);
3595
3596 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3597 if (tv1->vval.v_string == NULL)
3598 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003599 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003600 return FAIL;
3601 }
3602 mch_memmove(tv1->vval.v_string, s1, len1);
3603 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003604 vim_free(s1);
3605 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003606 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003607 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 }
3609 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003610 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003611 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003612 if (*op == '.')
3613 {
3614 if (may_generate_2STRING(-2, cctx) == FAIL
3615 || may_generate_2STRING(-1, cctx) == FAIL)
3616 return FAIL;
3617 generate_instr_drop(cctx, ISN_CONCAT, 1);
3618 }
3619 else
3620 generate_two_op(cctx, op);
3621 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 }
3623
3624 return OK;
3625}
3626
3627/*
3628 * expr5a == expr5b
3629 * expr5a =~ expr5b
3630 * expr5a != expr5b
3631 * expr5a !~ expr5b
3632 * expr5a > expr5b
3633 * expr5a >= expr5b
3634 * expr5a < expr5b
3635 * expr5a <= expr5b
3636 * expr5a is expr5b
3637 * expr5a isnot expr5b
3638 *
3639 * Produces instructions:
3640 * EVAL expr5a Push result of "expr5a"
3641 * EVAL expr5b Push result of "expr5b"
3642 * COMPARE one of the compare instructions
3643 */
3644 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003645compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646{
3647 exptype_T type = EXPR_UNKNOWN;
3648 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003649 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003652 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653
3654 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003655 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 return FAIL;
3657
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003658 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003659 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660
3661 /*
3662 * If there is a comparative operator, use it.
3663 */
3664 if (type != EXPR_UNKNOWN)
3665 {
3666 int ic = FALSE; // Default: do not ignore case
3667
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003668 if (next != NULL)
3669 {
3670 *arg = next_line_from_context(cctx, TRUE);
3671 p = skipwhite(*arg);
3672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 if (type_is && (p[len] == '?' || p[len] == '#'))
3674 {
3675 semsg(_(e_invexpr2), *arg);
3676 return FAIL;
3677 }
3678 // extra question mark appended: ignore case
3679 if (p[len] == '?')
3680 {
3681 ic = TRUE;
3682 ++len;
3683 }
3684 // extra '#' appended: match case (ignored)
3685 else if (p[len] == '#')
3686 ++len;
3687 // nothing appended: match case
3688
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003689 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003691 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003692 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 }
3694
3695 // get the second variable
3696 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003697 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003698 return FAIL;
3699
Bram Moolenaara5565e42020-05-09 15:44:01 +02003700 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 return FAIL;
3702
Bram Moolenaara5565e42020-05-09 15:44:01 +02003703 if (ppconst->pp_used == ppconst_used + 2)
3704 {
3705 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3706 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3707 int ret;
3708
3709 // Both sides are a constant, compute the result now.
3710 // First check for a valid combination of types, this is more
3711 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003712 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003713 ret = FAIL;
3714 else
3715 {
3716 ret = typval_compare(tv1, tv2, type, ic);
3717 tv1->v_type = VAR_BOOL;
3718 tv1->vval.v_number = tv1->vval.v_number
3719 ? VVAL_TRUE : VVAL_FALSE;
3720 clear_tv(tv2);
3721 --ppconst->pp_used;
3722 }
3723 return ret;
3724 }
3725
3726 generate_ppconst(cctx, ppconst);
3727 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 }
3729
3730 return OK;
3731}
3732
Bram Moolenaar7f141552020-05-09 17:35:53 +02003733static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735/*
3736 * Compile || or &&.
3737 */
3738 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003739compile_and_or(
3740 char_u **arg,
3741 cctx_T *cctx,
3742 char *op,
3743 ppconst_T *ppconst,
3744 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003746 char_u *next;
3747 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 int opchar = *op;
3749
3750 if (p[0] == opchar && p[1] == opchar)
3751 {
3752 garray_T *instr = &cctx->ctx_instr;
3753 garray_T end_ga;
3754
3755 /*
3756 * Repeat until there is no following "||" or "&&"
3757 */
3758 ga_init2(&end_ga, sizeof(int), 10);
3759 while (p[0] == opchar && p[1] == opchar)
3760 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003761 if (next != NULL)
3762 {
3763 *arg = next_line_from_context(cctx, TRUE);
3764 p = skipwhite(*arg);
3765 }
3766
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003767 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3768 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003770 return FAIL;
3771 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772
Bram Moolenaara5565e42020-05-09 15:44:01 +02003773 // TODO: use ppconst if the value is a constant
3774 generate_ppconst(cctx, ppconst);
3775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 if (ga_grow(&end_ga, 1) == FAIL)
3777 {
3778 ga_clear(&end_ga);
3779 return FAIL;
3780 }
3781 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3782 ++end_ga.ga_len;
3783 generate_JUMP(cctx, opchar == '|'
3784 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3785
3786 // eval the next expression
3787 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003788 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003789 return FAIL;
3790
Bram Moolenaara5565e42020-05-09 15:44:01 +02003791 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3792 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 {
3794 ga_clear(&end_ga);
3795 return FAIL;
3796 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003797
3798 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003800 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801
3802 // Fill in the end label in all jumps.
3803 while (end_ga.ga_len > 0)
3804 {
3805 isn_T *isn;
3806
3807 --end_ga.ga_len;
3808 isn = ((isn_T *)instr->ga_data)
3809 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3810 isn->isn_arg.jump.jump_where = instr->ga_len;
3811 }
3812 ga_clear(&end_ga);
3813 }
3814
3815 return OK;
3816}
3817
3818/*
3819 * expr4a && expr4a && expr4a logical AND
3820 *
3821 * Produces instructions:
3822 * EVAL expr4a Push result of "expr4a"
3823 * JUMP_AND_KEEP_IF_FALSE end
3824 * EVAL expr4b Push result of "expr4b"
3825 * JUMP_AND_KEEP_IF_FALSE end
3826 * EVAL expr4c Push result of "expr4c"
3827 * end:
3828 */
3829 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003830compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003832 int ppconst_used = ppconst->pp_used;
3833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003835 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 return FAIL;
3837
3838 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003839 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840}
3841
3842/*
3843 * expr3a || expr3b || expr3c logical OR
3844 *
3845 * Produces instructions:
3846 * EVAL expr3a Push result of "expr3a"
3847 * JUMP_AND_KEEP_IF_TRUE end
3848 * EVAL expr3b Push result of "expr3b"
3849 * JUMP_AND_KEEP_IF_TRUE end
3850 * EVAL expr3c Push result of "expr3c"
3851 * end:
3852 */
3853 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003854compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003856 int ppconst_used = ppconst->pp_used;
3857
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003859 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 return FAIL;
3861
3862 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003863 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864}
3865
3866/*
3867 * Toplevel expression: expr2 ? expr1a : expr1b
3868 *
3869 * Produces instructions:
3870 * EVAL expr2 Push result of "expr"
3871 * JUMP_IF_FALSE alt jump if false
3872 * EVAL expr1a
3873 * JUMP_ALWAYS end
3874 * alt: EVAL expr1b
3875 * end:
3876 */
3877 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003878compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879{
3880 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003881 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003882 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883
Bram Moolenaar61a89812020-05-07 16:58:17 +02003884 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02003885 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 return FAIL;
3887
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003888 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 if (*p == '?')
3890 {
3891 garray_T *instr = &cctx->ctx_instr;
3892 garray_T *stack = &cctx->ctx_type_stack;
3893 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003894 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02003896 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003898 int has_const_expr = FALSE;
3899 int const_value = FALSE;
3900 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003902 if (next != NULL)
3903 {
3904 *arg = next_line_from_context(cctx, TRUE);
3905 p = skipwhite(*arg);
3906 }
3907
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003908 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3909 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003911 return FAIL;
3912 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913
Bram Moolenaara5565e42020-05-09 15:44:01 +02003914 if (ppconst->pp_used == ppconst_used + 1)
3915 {
3916 // the condition is a constant, we know whether the ? or the :
3917 // expression is to be evaluated.
3918 has_const_expr = TRUE;
3919 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3920 clear_tv(&ppconst->pp_tv[ppconst_used]);
3921 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003922 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
3923 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003924 }
3925 else
3926 {
3927 generate_ppconst(cctx, ppconst);
3928 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930
3931 // evaluate the second expression; any type is accepted
3932 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003933 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003934 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003935 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01003936 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937
Bram Moolenaara5565e42020-05-09 15:44:01 +02003938 if (!has_const_expr)
3939 {
3940 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941
Bram Moolenaara5565e42020-05-09 15:44:01 +02003942 // remember the type and drop it
3943 --stack->ga_len;
3944 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945
Bram Moolenaara5565e42020-05-09 15:44:01 +02003946 end_idx = instr->ga_len;
3947 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3948
3949 // jump here from JUMP_IF_FALSE
3950 isn = ((isn_T *)instr->ga_data) + alt_idx;
3951 isn->isn_arg.jump.jump_where = instr->ga_len;
3952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953
3954 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003955 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 if (*p != ':')
3957 {
3958 emsg(_(e_missing_colon));
3959 return FAIL;
3960 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003961 if (next != NULL)
3962 {
3963 *arg = next_line_from_context(cctx, TRUE);
3964 p = skipwhite(*arg);
3965 }
3966
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003967 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3968 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003970 return FAIL;
3971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972
3973 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003974 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003975 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3976 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003978 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003979 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003980 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01003981 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982
Bram Moolenaara5565e42020-05-09 15:44:01 +02003983 if (!has_const_expr)
3984 {
3985 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986
Bram Moolenaara5565e42020-05-09 15:44:01 +02003987 // If the types differ, the result has a more generic type.
3988 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3989 common_type(type1, type2, &type2, cctx->ctx_type_list);
3990
3991 // jump here from JUMP_ALWAYS
3992 isn = ((isn_T *)instr->ga_data) + end_idx;
3993 isn->isn_arg.jump.jump_where = instr->ga_len;
3994 }
3995
3996 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997 }
3998 return OK;
3999}
4000
4001/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004002 * Toplevel expression.
4003 */
4004 static int
4005compile_expr0(char_u **arg, cctx_T *cctx)
4006{
4007 ppconst_T ppconst;
4008
4009 CLEAR_FIELD(ppconst);
4010 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4011 {
4012 clear_ppconst(&ppconst);
4013 return FAIL;
4014 }
4015 if (generate_ppconst(cctx, &ppconst) == FAIL)
4016 return FAIL;
4017 return OK;
4018}
4019
4020/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 * compile "return [expr]"
4022 */
4023 static char_u *
4024compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4025{
4026 char_u *p = arg;
4027 garray_T *stack = &cctx->ctx_type_stack;
4028 type_T *stack_type;
4029
4030 if (*p != NUL && *p != '|' && *p != '\n')
4031 {
4032 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004033 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 return NULL;
4035
4036 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4037 if (set_return_type)
4038 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004039 else
4040 {
4041 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4042 && stack_type->tt_type != VAR_VOID
4043 && stack_type->tt_type != VAR_UNKNOWN)
4044 {
4045 emsg(_("E1096: Returning a value in a function without a return type"));
4046 return NULL;
4047 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004048 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4049 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004051 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 }
4053 else
4054 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004055 // "set_return_type" cannot be TRUE, only used for a lambda which
4056 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004057 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4058 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 {
4060 emsg(_("E1003: Missing return value"));
4061 return NULL;
4062 }
4063
4064 // No argument, return zero.
4065 generate_PUSHNR(cctx, 0);
4066 }
4067
4068 if (generate_instr(cctx, ISN_RETURN) == NULL)
4069 return NULL;
4070
4071 // "return val | endif" is possible
4072 return skipwhite(p);
4073}
4074
4075/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004076 * Get a line from the compilation context, compatible with exarg_T getline().
4077 * Return a pointer to the line in allocated memory.
4078 * Return NULL for end-of-file or some error.
4079 */
4080 static char_u *
4081exarg_getline(
4082 int c UNUSED,
4083 void *cookie,
4084 int indent UNUSED,
4085 int do_concat UNUSED)
4086{
4087 cctx_T *cctx = (cctx_T *)cookie;
4088
4089 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4090 {
4091 iemsg("Heredoc got to end");
4092 return NULL;
4093 }
4094 ++cctx->ctx_lnum;
4095 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4096 [cctx->ctx_lnum]);
4097}
4098
4099/*
4100 * Compile a nested :def command.
4101 */
4102 static char_u *
4103compile_nested_function(exarg_T *eap, cctx_T *cctx)
4104{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004105 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004106 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004107 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004108 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004109 lvar_T *lvar;
4110 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004111 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004112
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004113 // Only g:Func() can use a namespace.
4114 if (name_start[1] == ':' && !is_global)
4115 {
4116 semsg(_(e_namespace), name_start);
4117 return NULL;
4118 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004119 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4120 return NULL;
4121
Bram Moolenaar04b12692020-05-04 23:24:44 +02004122 eap->arg = name_end;
4123 eap->getline = exarg_getline;
4124 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004125 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004126 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004127 lambda_name = get_lambda_name();
4128 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004129
Bram Moolenaar822ba242020-05-24 23:00:18 +02004130 if (ufunc == NULL)
4131 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004132 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004133 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004134 return NULL;
4135
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004136 if (is_global)
4137 {
4138 char_u *func_name = vim_strnsave(name_start + 2,
4139 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004140
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004141 if (func_name == NULL)
4142 r = FAIL;
4143 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004144 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004145 }
4146 else
4147 {
4148 // Define a local variable for the function reference.
4149 lvar = reserve_local(cctx, name_start, name_end - name_start,
4150 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004151 if (lvar == NULL)
4152 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004153 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004154 return NULL;
4155 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4156 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004157
Bram Moolenaar61a89812020-05-07 16:58:17 +02004158 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004159 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004160}
4161
4162/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 * Return the length of an assignment operator, or zero if there isn't one.
4164 */
4165 int
4166assignment_len(char_u *p, int *heredoc)
4167{
4168 if (*p == '=')
4169 {
4170 if (p[1] == '<' && p[2] == '<')
4171 {
4172 *heredoc = TRUE;
4173 return 3;
4174 }
4175 return 1;
4176 }
4177 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4178 return 2;
4179 if (STRNCMP(p, "..=", 3) == 0)
4180 return 3;
4181 return 0;
4182}
4183
4184// words that cannot be used as a variable
4185static char *reserved[] = {
4186 "true",
4187 "false",
4188 NULL
4189};
4190
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004191typedef enum {
4192 dest_local,
4193 dest_option,
4194 dest_env,
4195 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004196 dest_buffer,
4197 dest_window,
4198 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004199 dest_vimvar,
4200 dest_script,
4201 dest_reg,
4202} assign_dest_T;
4203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004205 * Generate the load instruction for "name".
4206 */
4207 static void
4208generate_loadvar(
4209 cctx_T *cctx,
4210 assign_dest_T dest,
4211 char_u *name,
4212 lvar_T *lvar,
4213 type_T *type)
4214{
4215 switch (dest)
4216 {
4217 case dest_option:
4218 // TODO: check the option exists
4219 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4220 break;
4221 case dest_global:
4222 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4223 break;
4224 case dest_buffer:
4225 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4226 break;
4227 case dest_window:
4228 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4229 break;
4230 case dest_tab:
4231 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4232 break;
4233 case dest_script:
4234 compile_load_scriptvar(cctx,
4235 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4236 break;
4237 case dest_env:
4238 // Include $ in the name here
4239 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4240 break;
4241 case dest_reg:
4242 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4243 break;
4244 case dest_vimvar:
4245 generate_LOADV(cctx, name + 2, TRUE);
4246 break;
4247 case dest_local:
4248 if (lvar->lv_from_outer)
4249 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4250 NULL, type);
4251 else
4252 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4253 break;
4254 }
4255}
4256
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004257 void
4258vim9_declare_error(char_u *name)
4259{
4260 char *scope = "";
4261
4262 switch (*name)
4263 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004264 case 'g': scope = _("global"); break;
4265 case 'b': scope = _("buffer"); break;
4266 case 'w': scope = _("window"); break;
4267 case 't': scope = _("tab"); break;
4268 case 'v': scope = "v:"; break;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004269 case '$': semsg(_(e_declare_env_var), name);
4270 return;
4271 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
4272 return;
4273 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
4274 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004275 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004276 }
4277 semsg(_(e_declare_var), scope, name);
4278}
4279
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004280/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004281 * Compile declaration and assignment:
4282 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004284 * Return NULL for an error.
4285 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 */
4287 static char_u *
4288compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4289{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004290 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004292 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 char_u *ret = NULL;
4294 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004295 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004296 int scriptvar_sid = 0;
4297 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004300 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 int oplen = 0;
4303 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004304 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004305 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004306 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004310 // Skip over the "var" or "[var, var]" to get to any "=".
4311 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4312 if (p == NULL)
4313 return *arg == '[' ? arg : NULL;
4314
4315 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004317 // TODO: should we allow this, and figure out type inference from list
4318 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004319 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 return NULL;
4321 }
4322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 sp = p;
4324 p = skipwhite(p);
4325 op = p;
4326 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004327
4328 if (var_count > 0 && oplen == 0)
4329 // can be something like "[1, 2]->func()"
4330 return arg;
4331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4333 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004334 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004335 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004336 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 if (heredoc)
4339 {
4340 list_T *l;
4341 listitem_T *li;
4342
4343 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004344 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004346 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347
4348 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004349 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 {
4351 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4352 li->li_tv.vval.v_string = NULL;
4353 }
4354 generate_NEWLIST(cctx, l->lv_len);
4355 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004356 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 list_free(l);
4358 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004359 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004361 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004363 // for "[var, var] = expr" evaluate the expression here, loop over the
4364 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004365
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004366 p = skipwhite(op + oplen);
4367 if (compile_expr0(&p, cctx) == FAIL)
4368 return NULL;
4369 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004371 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004373 type_T *stacktype;
4374
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004375 stacktype = stack->ga_len == 0 ? &t_void
4376 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004377 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004379 emsg(_(e_cannot_use_void));
4380 goto theend;
4381 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004382 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004383 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004384 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004385 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4386 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004387 }
4388 }
4389
4390 /*
4391 * Loop over variables in "[var, var] = expr".
4392 * For "var = expr" and "let var: type" this is done only once.
4393 */
4394 if (var_count > 0)
4395 var_start = skipwhite(arg + 1); // skip over the "["
4396 else
4397 var_start = arg;
4398 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4399 {
4400 char_u *var_end = skip_var_one(var_start, FALSE);
4401 size_t varlen;
4402 int new_local = FALSE;
4403 int opt_type;
4404 int opt_flags = 0;
4405 assign_dest_T dest = dest_local;
4406 int vimvaridx = -1;
4407 lvar_T *lvar = NULL;
4408 lvar_T arg_lvar;
4409 int has_type = FALSE;
4410 int has_index = FALSE;
4411 int instr_count = -1;
4412
Bram Moolenaar65821722020-08-02 18:58:54 +02004413 if (*var_start == '@')
4414 p = var_start + 2;
4415 else
4416 {
4417 p = (*var_start == '&' || *var_start == '$')
4418 ? var_start + 1 : var_start;
4419 p = to_name_end(p, TRUE);
4420 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004421
4422 // "a: type" is declaring variable "a" with a type, not "a:".
4423 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4424 --var_end;
4425 if (is_decl && p == var_start + 2 && p[-1] == ':')
4426 --p;
4427
4428 varlen = p - var_start;
4429 vim_free(name);
4430 name = vim_strnsave(var_start, varlen);
4431 if (name == NULL)
4432 return NULL;
4433 if (!heredoc)
4434 type = &t_any;
4435
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004436 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004437 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004438 int declare_error = FALSE;
4439
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004440 if (*var_start == '&')
4441 {
4442 int cc;
4443 long numval;
4444
4445 dest = dest_option;
4446 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004448 emsg(_(e_const_option));
4449 goto theend;
4450 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004451 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004452 p = var_start;
4453 p = find_option_end(&p, &opt_flags);
4454 if (p == NULL)
4455 {
4456 // cannot happen?
4457 emsg(_(e_letunexp));
4458 goto theend;
4459 }
4460 cc = *p;
4461 *p = NUL;
4462 opt_type = get_option_value(var_start + 1, &numval,
4463 NULL, opt_flags);
4464 *p = cc;
4465 if (opt_type == -3)
4466 {
4467 semsg(_(e_unknown_option), var_start);
4468 goto theend;
4469 }
4470 if (opt_type == -2 || opt_type == 0)
4471 type = &t_string;
4472 else
4473 type = &t_number; // both number and boolean option
4474 }
4475 else if (*var_start == '$')
4476 {
4477 dest = dest_env;
4478 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004479 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004480 }
4481 else if (*var_start == '@')
4482 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004483 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004484 {
4485 emsg_invreg(var_start[1]);
4486 goto theend;
4487 }
4488 dest = dest_reg;
4489 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004490 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004491 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004492 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004493 {
4494 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004495 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004496 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004497 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004498 {
4499 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004500 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004501 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004502 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004503 {
4504 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004505 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004506 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004507 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004508 {
4509 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004510 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004511 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004512 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004513 {
4514 typval_T *vtv;
4515 int di_flags;
4516
4517 vimvaridx = find_vim_var(name + 2, &di_flags);
4518 if (vimvaridx < 0)
4519 {
4520 semsg(_(e_var_notfound), var_start);
4521 goto theend;
4522 }
4523 // We use the current value of "sandbox" here, is that OK?
4524 if (var_check_ro(di_flags, name, FALSE))
4525 goto theend;
4526 dest = dest_vimvar;
4527 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004528 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004529 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004530 }
4531 else
4532 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004533 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004534
4535 for (idx = 0; reserved[idx] != NULL; ++idx)
4536 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004537 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004538 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004539 goto theend;
4540 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004541
4542 lvar = lookup_local(var_start, varlen, cctx);
4543 if (lvar == NULL)
4544 {
4545 CLEAR_FIELD(arg_lvar);
4546 if (lookup_arg(var_start, varlen,
4547 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4548 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004549 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004550 if (is_decl)
4551 {
4552 semsg(_(e_used_as_arg), name);
4553 goto theend;
4554 }
4555 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004556 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004557 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004558 if (lvar != NULL)
4559 {
4560 if (is_decl)
4561 {
4562 semsg(_("E1017: Variable already declared: %s"), name);
4563 goto theend;
4564 }
4565 else if (lvar->lv_const)
4566 {
4567 semsg(_("E1018: Cannot assign to a constant: %s"),
4568 name);
4569 goto theend;
4570 }
4571 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004572 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004573 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004574 int script_namespace = varlen > 1
4575 && STRNCMP(var_start, "s:", 2) == 0;
4576 int script_var = (script_namespace
4577 ? lookup_script(var_start + 2, varlen - 2)
4578 : lookup_script(var_start, varlen)) == OK;
4579 imported_T *import =
4580 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004581
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004582 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004583 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004584 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4585
4586 if (is_decl)
4587 {
4588 if (script_namespace)
4589 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004590 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004591 else
4592 semsg(_("E1054: Variable already declared in the script: %s"),
4593 name);
4594 goto theend;
4595 }
4596 else if (cctx->ctx_ufunc->uf_script_ctx_version
4597 == SCRIPT_VERSION_VIM9
4598 && script_namespace
4599 && !script_var && import == NULL)
4600 {
4601 semsg(_(e_unknown_var), name);
4602 goto theend;
4603 }
4604
4605 dest = dest_script;
4606
4607 // existing script-local variables should have a type
4608 scriptvar_sid = current_sctx.sc_sid;
4609 if (import != NULL)
4610 scriptvar_sid = import->imp_sid;
4611 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4612 rawname, TRUE);
4613 if (scriptvar_idx >= 0)
4614 {
4615 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4616 svar_T *sv =
4617 ((svar_T *)si->sn_var_vals.ga_data)
4618 + scriptvar_idx;
4619 type = sv->sv_type;
4620 }
4621 }
4622 else if (name[1] == ':' && name[2] != NUL)
4623 {
4624 semsg(_("E1082: Cannot use a namespaced variable: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004625 name);
4626 goto theend;
4627 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004628 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004629 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004630 semsg(_(e_unknown_var), name);
4631 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004632 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004633 else if (check_defined(var_start, varlen, cctx) == FAIL)
4634 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004635 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004636 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004637
4638 if (declare_error)
4639 {
4640 vim9_declare_error(name);
4641 goto theend;
4642 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004643 }
4644
4645 // handle "a:name" as a name, not index "name" on "a"
4646 if (varlen > 1 || var_start[varlen] != ':')
4647 p = var_end;
4648
4649 if (dest != dest_option)
4650 {
4651 if (is_decl && *p == ':')
4652 {
4653 // parse optional type: "let var: type = expr"
4654 if (!VIM_ISWHITE(p[1]))
4655 {
4656 semsg(_(e_white_after), ":");
4657 goto theend;
4658 }
4659 p = skipwhite(p + 1);
4660 type = parse_type(&p, cctx->ctx_type_list);
4661 has_type = TRUE;
4662 }
4663 else if (lvar != NULL)
4664 type = lvar->lv_type;
4665 }
4666
4667 if (oplen == 3 && !heredoc && dest != dest_global
4668 && type->tt_type != VAR_STRING
4669 && type->tt_type != VAR_ANY)
4670 {
4671 emsg(_("E1019: Can only concatenate to string"));
4672 goto theend;
4673 }
4674
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004675 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004676 {
4677 if (oplen > 1 && !heredoc)
4678 {
4679 // +=, /=, etc. require an existing variable
4680 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4681 name);
4682 goto theend;
4683 }
4684
4685 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004686 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4687 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004688 goto theend;
4689 lvar = reserve_local(cctx, var_start, varlen,
4690 cmdidx == CMD_const, type);
4691 if (lvar == NULL)
4692 goto theend;
4693 new_local = TRUE;
4694 }
4695
4696 member_type = type;
4697 if (var_end > var_start + varlen)
4698 {
4699 // Something follows after the variable: "var[idx]".
4700 if (is_decl)
4701 {
4702 emsg(_("E1087: cannot use an index when declaring a variable"));
4703 goto theend;
4704 }
4705
4706 if (var_start[varlen] == '[')
4707 {
4708 has_index = TRUE;
4709 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004710 member_type = &t_any;
4711 else
4712 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004713 }
4714 else
4715 {
4716 semsg("Not supported yet: %s", var_start);
4717 goto theend;
4718 }
4719 }
4720 else if (lvar == &arg_lvar)
4721 {
4722 semsg(_("E1090: Cannot assign to argument %s"), name);
4723 goto theend;
4724 }
4725
4726 if (!heredoc)
4727 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004728 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004729 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004730 if (oplen > 0 && var_count == 0)
4731 {
4732 // skip over the "=" and the expression
4733 p = skipwhite(op + oplen);
4734 compile_expr0(&p, cctx);
4735 }
4736 }
4737 else if (oplen > 0)
4738 {
4739 type_T *stacktype;
4740
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004741 // For "var = expr" evaluate the expression.
4742 if (var_count == 0)
4743 {
4744 int r;
4745
4746 // for "+=", "*=", "..=" etc. first load the current value
4747 if (*op != '=')
4748 {
4749 generate_loadvar(cctx, dest, name, lvar, type);
4750
4751 if (has_index)
4752 {
4753 // TODO: get member from list or dict
4754 emsg("Index with operation not supported yet");
4755 goto theend;
4756 }
4757 }
4758
4759 // Compile the expression. Temporarily hide the new local
4760 // variable here, it is not available to this expression.
4761 if (new_local)
4762 --cctx->ctx_locals.ga_len;
4763 instr_count = instr->ga_len;
4764 p = skipwhite(op + oplen);
4765 r = compile_expr0(&p, cctx);
4766 if (new_local)
4767 ++cctx->ctx_locals.ga_len;
4768 if (r == FAIL)
4769 goto theend;
4770 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004771 else if (semicolon && var_idx == var_count - 1)
4772 {
4773 // For "[var; var] = expr" get the rest of the list
4774 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4775 goto theend;
4776 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004777 else
4778 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004779 // For "[var, var] = expr" get the "var_idx" item from the
4780 // list.
4781 if (generate_GETITEM(cctx, var_idx) == FAIL)
4782 return FAIL;
4783 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004784
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004785 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004786 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004787 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004788 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004789 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004790 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004791 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004792 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004793 emsg(_(e_cannot_use_void));
4794 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004795 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004796 else if ((stacktype->tt_type == VAR_FUNC
4797 || stacktype->tt_type == VAR_PARTIAL)
4798 && var_wrong_func_name(name, TRUE))
4799 {
4800 goto theend;
4801 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004802 else
4803 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004804 // An empty list or dict has a &t_void member,
4805 // for a variable that implies &t_any.
4806 if (stacktype == &t_list_empty)
4807 lvar->lv_type = &t_list_any;
4808 else if (stacktype == &t_dict_empty)
4809 lvar->lv_type = &t_dict_any;
4810 else
4811 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004812 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004813 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004814 else
4815 {
4816 type_T *use_type = lvar->lv_type;
4817
4818 if (has_index)
4819 {
4820 use_type = use_type->tt_member;
4821 if (use_type == NULL)
4822 use_type = &t_void;
4823 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004824 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004825 == FAIL)
4826 goto theend;
4827 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004828 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004829 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004830 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004831 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004833 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004835 emsg(_(e_const_req_value));
4836 goto theend;
4837 }
4838 else if (!has_type || dest == dest_option)
4839 {
4840 emsg(_(e_type_req));
4841 goto theend;
4842 }
4843 else
4844 {
4845 // variables are always initialized
4846 if (ga_grow(instr, 1) == FAIL)
4847 goto theend;
4848 switch (member_type->tt_type)
4849 {
4850 case VAR_BOOL:
4851 generate_PUSHBOOL(cctx, VVAL_FALSE);
4852 break;
4853 case VAR_FLOAT:
4854#ifdef FEAT_FLOAT
4855 generate_PUSHF(cctx, 0.0);
4856#endif
4857 break;
4858 case VAR_STRING:
4859 generate_PUSHS(cctx, NULL);
4860 break;
4861 case VAR_BLOB:
4862 generate_PUSHBLOB(cctx, NULL);
4863 break;
4864 case VAR_FUNC:
4865 generate_PUSHFUNC(cctx, NULL, &t_func_void);
4866 break;
4867 case VAR_LIST:
4868 generate_NEWLIST(cctx, 0);
4869 break;
4870 case VAR_DICT:
4871 generate_NEWDICT(cctx, 0);
4872 break;
4873 case VAR_JOB:
4874 generate_PUSHJOB(cctx, NULL);
4875 break;
4876 case VAR_CHANNEL:
4877 generate_PUSHCHANNEL(cctx, NULL);
4878 break;
4879 case VAR_NUMBER:
4880 case VAR_UNKNOWN:
4881 case VAR_ANY:
4882 case VAR_PARTIAL:
4883 case VAR_VOID:
4884 case VAR_SPECIAL: // cannot happen
4885 generate_PUSHNR(cctx, 0);
4886 break;
4887 }
4888 }
4889 if (var_count == 0)
4890 end = p;
4891 }
4892
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004893 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004894 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004895 break;
4896
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004897 if (oplen > 0 && *op != '=')
4898 {
4899 type_T *expected = &t_number;
4900 type_T *stacktype;
4901
4902 // TODO: if type is known use float or any operation
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004903 // TODO: check operator matches variable type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004904
4905 if (*op == '.')
4906 expected = &t_string;
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004907 else if (*op == '+')
4908 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004909 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004910 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004911 goto theend;
4912
4913 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02004914 {
4915 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
4916 goto theend;
4917 }
4918 else if (*op == '+')
4919 {
4920 if (generate_add_instr(cctx,
4921 operator_type(member_type, stacktype),
4922 member_type, stacktype) == FAIL)
4923 goto theend;
4924 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004925 else
4926 {
4927 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4928
4929 if (isn == NULL)
4930 goto theend;
4931 switch (*op)
4932 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004933 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4934 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4935 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4936 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4937 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 }
4939 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004941 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004942 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004943 int r;
4944
4945 // Compile the "idx" in "var[idx]".
4946 if (new_local)
4947 --cctx->ctx_locals.ga_len;
4948 p = skipwhite(var_start + varlen + 1);
4949 r = compile_expr0(&p, cctx);
4950 if (new_local)
4951 ++cctx->ctx_locals.ga_len;
4952 if (r == FAIL)
4953 goto theend;
4954 if (*skipwhite(p) != ']')
4955 {
4956 emsg(_(e_missbrac));
4957 goto theend;
4958 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004959 if (type == &t_any)
4960 {
4961 type_T *idx_type = ((type_T **)stack->ga_data)[
4962 stack->ga_len - 1];
4963 // Index on variable of unknown type: guess the type from the
4964 // index type: number is dict, otherwise dict.
4965 // TODO: should do the assignment at runtime
4966 if (idx_type->tt_type == VAR_NUMBER)
4967 type = &t_list_any;
4968 else
4969 type = &t_dict_any;
4970 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004971 if (type->tt_type == VAR_DICT
4972 && may_generate_2STRING(-1, cctx) == FAIL)
4973 goto theend;
4974 if (type->tt_type == VAR_LIST
4975 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004976 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004977 {
4978 emsg(_(e_number_exp));
4979 goto theend;
4980 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004981
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004982 // Load the dict or list. On the stack we then have:
4983 // - value
4984 // - index
4985 // - variable
4986 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004987
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004988 if (type->tt_type == VAR_LIST)
4989 {
4990 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
4991 return FAIL;
4992 }
4993 else if (type->tt_type == VAR_DICT)
4994 {
4995 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
4996 return FAIL;
4997 }
4998 else
4999 {
5000 emsg(_(e_listreq));
5001 goto theend;
5002 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005003 }
5004 else
5005 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005006 switch (dest)
5007 {
5008 case dest_option:
5009 generate_STOREOPT(cctx, name + 1, opt_flags);
5010 break;
5011 case dest_global:
5012 // include g: with the name, easier to execute that way
5013 generate_STORE(cctx, ISN_STOREG, 0, name);
5014 break;
5015 case dest_buffer:
5016 // include b: with the name, easier to execute that way
5017 generate_STORE(cctx, ISN_STOREB, 0, name);
5018 break;
5019 case dest_window:
5020 // include w: with the name, easier to execute that way
5021 generate_STORE(cctx, ISN_STOREW, 0, name);
5022 break;
5023 case dest_tab:
5024 // include t: with the name, easier to execute that way
5025 generate_STORE(cctx, ISN_STORET, 0, name);
5026 break;
5027 case dest_env:
5028 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5029 break;
5030 case dest_reg:
5031 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5032 break;
5033 case dest_vimvar:
5034 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5035 break;
5036 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005037 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005038 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005039 {
5040 char_u *name_s = name;
5041
5042 // Include s: in the name for store_var()
5043 if (name[1] != ':')
5044 {
5045 int len = (int)STRLEN(name) + 3;
5046
5047 name_s = alloc(len);
5048 if (name_s == NULL)
5049 name_s = name;
5050 else
5051 vim_snprintf((char *)name_s, len,
5052 "s:%s", name);
5053 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005054 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5055 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005056 if (name_s != name)
5057 vim_free(name_s);
5058 }
5059 else
5060 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005061 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005062 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005063 break;
5064 case dest_local:
5065 if (lvar != NULL)
5066 {
5067 isn_T *isn = ((isn_T *)instr->ga_data)
5068 + instr->ga_len - 1;
5069
5070 // optimization: turn "var = 123" from ISN_PUSHNR +
5071 // ISN_STORE into ISN_STORENR
5072 if (!lvar->lv_from_outer
5073 && instr->ga_len == instr_count + 1
5074 && isn->isn_type == ISN_PUSHNR)
5075 {
5076 varnumber_T val = isn->isn_arg.number;
5077
5078 isn->isn_type = ISN_STORENR;
5079 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5080 isn->isn_arg.storenr.stnr_val = val;
5081 if (stack->ga_len > 0)
5082 --stack->ga_len;
5083 }
5084 else if (lvar->lv_from_outer)
5085 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005086 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005087 else
5088 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5089 }
5090 break;
5091 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005092 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005093
5094 if (var_idx + 1 < var_count)
5095 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005097
5098 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005099 if (var_count > 0 && !semicolon)
5100 {
5101 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5102 goto theend;
5103 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005104
Bram Moolenaarb2097502020-07-19 17:17:02 +02005105 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106
5107theend:
5108 vim_free(name);
5109 return ret;
5110}
5111
5112/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005113 * Check if "name" can be "unlet".
5114 */
5115 int
5116check_vim9_unlet(char_u *name)
5117{
5118 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5119 {
5120 semsg(_("E1081: Cannot unlet %s"), name);
5121 return FAIL;
5122 }
5123 return OK;
5124}
5125
5126/*
5127 * Callback passed to ex_unletlock().
5128 */
5129 static int
5130compile_unlet(
5131 lval_T *lvp,
5132 char_u *name_end,
5133 exarg_T *eap,
5134 int deep UNUSED,
5135 void *coookie)
5136{
5137 cctx_T *cctx = coookie;
5138
5139 if (lvp->ll_tv == NULL)
5140 {
5141 char_u *p = lvp->ll_name;
5142 int cc = *name_end;
5143 int ret = OK;
5144
5145 // Normal name. Only supports g:, w:, t: and b: namespaces.
5146 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005147 if (*p == '$')
5148 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5149 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005150 ret = FAIL;
5151 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005152 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005153
5154 *name_end = cc;
5155 return ret;
5156 }
5157
5158 // TODO: unlet {list}[idx]
5159 // TODO: unlet {dict}[key]
5160 emsg("Sorry, :unlet not fully implemented yet");
5161 return FAIL;
5162}
5163
5164/*
5165 * compile "unlet var", "lock var" and "unlock var"
5166 * "arg" points to "var".
5167 */
5168 static char_u *
5169compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5170{
5171 char_u *p = arg;
5172
5173 if (eap->cmdidx != CMD_unlet)
5174 {
5175 emsg("Sorry, :lock and unlock not implemented yet");
5176 return NULL;
5177 }
5178
5179 if (*p == '!')
5180 {
5181 p = skipwhite(p + 1);
5182 eap->forceit = TRUE;
5183 }
5184
5185 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5186 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5187}
5188
5189/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 * Compile an :import command.
5191 */
5192 static char_u *
5193compile_import(char_u *arg, cctx_T *cctx)
5194{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005195 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196}
5197
5198/*
5199 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5200 */
5201 static int
5202compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5203{
5204 garray_T *instr = &cctx->ctx_instr;
5205 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5206
5207 if (endlabel == NULL)
5208 return FAIL;
5209 endlabel->el_next = *el;
5210 *el = endlabel;
5211 endlabel->el_end_label = instr->ga_len;
5212
5213 generate_JUMP(cctx, when, 0);
5214 return OK;
5215}
5216
5217 static void
5218compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5219{
5220 garray_T *instr = &cctx->ctx_instr;
5221
5222 while (*el != NULL)
5223 {
5224 endlabel_T *cur = (*el);
5225 isn_T *isn;
5226
5227 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5228 isn->isn_arg.jump.jump_where = instr->ga_len;
5229 *el = cur->el_next;
5230 vim_free(cur);
5231 }
5232}
5233
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005234 static void
5235compile_free_jump_to_end(endlabel_T **el)
5236{
5237 while (*el != NULL)
5238 {
5239 endlabel_T *cur = (*el);
5240
5241 *el = cur->el_next;
5242 vim_free(cur);
5243 }
5244}
5245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246/*
5247 * Create a new scope and set up the generic items.
5248 */
5249 static scope_T *
5250new_scope(cctx_T *cctx, scopetype_T type)
5251{
5252 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5253
5254 if (scope == NULL)
5255 return NULL;
5256 scope->se_outer = cctx->ctx_scope;
5257 cctx->ctx_scope = scope;
5258 scope->se_type = type;
5259 scope->se_local_count = cctx->ctx_locals.ga_len;
5260 return scope;
5261}
5262
5263/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005264 * Free the current scope and go back to the outer scope.
5265 */
5266 static void
5267drop_scope(cctx_T *cctx)
5268{
5269 scope_T *scope = cctx->ctx_scope;
5270
5271 if (scope == NULL)
5272 {
5273 iemsg("calling drop_scope() without a scope");
5274 return;
5275 }
5276 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005277 switch (scope->se_type)
5278 {
5279 case IF_SCOPE:
5280 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5281 case FOR_SCOPE:
5282 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5283 case WHILE_SCOPE:
5284 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5285 case TRY_SCOPE:
5286 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5287 case NO_SCOPE:
5288 case BLOCK_SCOPE:
5289 break;
5290 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005291 vim_free(scope);
5292}
5293
5294/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295 * compile "if expr"
5296 *
5297 * "if expr" Produces instructions:
5298 * EVAL expr Push result of "expr"
5299 * JUMP_IF_FALSE end
5300 * ... body ...
5301 * end:
5302 *
5303 * "if expr | else" Produces instructions:
5304 * EVAL expr Push result of "expr"
5305 * JUMP_IF_FALSE else
5306 * ... body ...
5307 * JUMP_ALWAYS end
5308 * else:
5309 * ... body ...
5310 * end:
5311 *
5312 * "if expr1 | elseif expr2 | else" Produces instructions:
5313 * EVAL expr Push result of "expr"
5314 * JUMP_IF_FALSE elseif
5315 * ... body ...
5316 * JUMP_ALWAYS end
5317 * elseif:
5318 * EVAL expr Push result of "expr"
5319 * JUMP_IF_FALSE else
5320 * ... body ...
5321 * JUMP_ALWAYS end
5322 * else:
5323 * ... body ...
5324 * end:
5325 */
5326 static char_u *
5327compile_if(char_u *arg, cctx_T *cctx)
5328{
5329 char_u *p = arg;
5330 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005331 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005332 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005333 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005334 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335
Bram Moolenaara5565e42020-05-09 15:44:01 +02005336 CLEAR_FIELD(ppconst);
5337 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005338 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005339 clear_ppconst(&ppconst);
5340 return NULL;
5341 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005342 if (cctx->ctx_skip == SKIP_YES)
5343 clear_ppconst(&ppconst);
5344 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005345 {
5346 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005347 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005348 clear_ppconst(&ppconst);
5349 }
5350 else
5351 {
5352 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005353 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005354 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005355 return NULL;
5356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357
5358 scope = new_scope(cctx, IF_SCOPE);
5359 if (scope == NULL)
5360 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005361 scope->se_skip_save = skip_save;
5362 // "is_had_return" will be reset if any block does not end in :return
5363 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005364
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005365 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005366 {
5367 // "where" is set when ":elseif", "else" or ":endif" is found
5368 scope->se_u.se_if.is_if_label = instr->ga_len;
5369 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5370 }
5371 else
5372 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373
5374 return p;
5375}
5376
5377 static char_u *
5378compile_elseif(char_u *arg, cctx_T *cctx)
5379{
5380 char_u *p = arg;
5381 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005382 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005383 isn_T *isn;
5384 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005385 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386
5387 if (scope == NULL || scope->se_type != IF_SCOPE)
5388 {
5389 emsg(_(e_elseif_without_if));
5390 return NULL;
5391 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005392 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005393 if (!cctx->ctx_had_return)
5394 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005396 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005397 {
5398 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005399 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005400 return NULL;
5401 // previous "if" or "elseif" jumps here
5402 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5403 isn->isn_arg.jump.jump_where = instr->ga_len;
5404 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005406 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005407 CLEAR_FIELD(ppconst);
5408 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005409 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005410 clear_ppconst(&ppconst);
5411 return NULL;
5412 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005413 if (scope->se_skip_save == SKIP_YES)
5414 clear_ppconst(&ppconst);
5415 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005416 {
5417 // The expression results in a constant.
5418 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005419 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005420 clear_ppconst(&ppconst);
5421 scope->se_u.se_if.is_if_label = -1;
5422 }
5423 else
5424 {
5425 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005426 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005427 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005428 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005430 // "where" is set when ":elseif", "else" or ":endif" is found
5431 scope->se_u.se_if.is_if_label = instr->ga_len;
5432 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5433 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434
5435 return p;
5436}
5437
5438 static char_u *
5439compile_else(char_u *arg, cctx_T *cctx)
5440{
5441 char_u *p = arg;
5442 garray_T *instr = &cctx->ctx_instr;
5443 isn_T *isn;
5444 scope_T *scope = cctx->ctx_scope;
5445
5446 if (scope == NULL || scope->se_type != IF_SCOPE)
5447 {
5448 emsg(_(e_else_without_if));
5449 return NULL;
5450 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005451 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005452 if (!cctx->ctx_had_return)
5453 scope->se_u.se_if.is_had_return = FALSE;
5454 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455
Bram Moolenaarefd88552020-06-18 20:50:10 +02005456 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005457 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005458 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005459 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005460 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005461 if (!cctx->ctx_had_return
5462 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5463 JUMP_ALWAYS, cctx) == FAIL)
5464 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005465 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005466
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005467 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005468 {
5469 if (scope->se_u.se_if.is_if_label >= 0)
5470 {
5471 // previous "if" or "elseif" jumps here
5472 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5473 isn->isn_arg.jump.jump_where = instr->ga_len;
5474 scope->se_u.se_if.is_if_label = -1;
5475 }
5476 }
5477
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005478 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005479 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005481
5482 return p;
5483}
5484
5485 static char_u *
5486compile_endif(char_u *arg, cctx_T *cctx)
5487{
5488 scope_T *scope = cctx->ctx_scope;
5489 ifscope_T *ifscope;
5490 garray_T *instr = &cctx->ctx_instr;
5491 isn_T *isn;
5492
5493 if (scope == NULL || scope->se_type != IF_SCOPE)
5494 {
5495 emsg(_(e_endif_without_if));
5496 return NULL;
5497 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005498 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005499 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005500 if (!cctx->ctx_had_return)
5501 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005503 if (scope->se_u.se_if.is_if_label >= 0)
5504 {
5505 // previous "if" or "elseif" jumps here
5506 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5507 isn->isn_arg.jump.jump_where = instr->ga_len;
5508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509 // Fill in the "end" label in jumps at the end of the blocks.
5510 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005511 cctx->ctx_skip = scope->se_skip_save;
5512
5513 // If all the blocks end in :return and there is an :else then the
5514 // had_return flag is set.
5515 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005517 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005518 return arg;
5519}
5520
5521/*
5522 * compile "for var in expr"
5523 *
5524 * Produces instructions:
5525 * PUSHNR -1
5526 * STORE loop-idx Set index to -1
5527 * EVAL expr Push result of "expr"
5528 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5529 * - if beyond end, jump to "end"
5530 * - otherwise get item from list and push it
5531 * STORE var Store item in "var"
5532 * ... body ...
5533 * JUMP top Jump back to repeat
5534 * end: DROP Drop the result of "expr"
5535 *
5536 */
5537 static char_u *
5538compile_for(char_u *arg, cctx_T *cctx)
5539{
5540 char_u *p;
5541 size_t varlen;
5542 garray_T *instr = &cctx->ctx_instr;
5543 garray_T *stack = &cctx->ctx_type_stack;
5544 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005545 lvar_T *loop_lvar; // loop iteration variable
5546 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005547 type_T *vartype;
5548
5549 // TODO: list of variables: "for [key, value] in dict"
5550 // parse "var"
5551 for (p = arg; eval_isnamec1(*p); ++p)
5552 ;
5553 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005554 var_lvar = lookup_local(arg, varlen, cctx);
5555 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556 {
5557 semsg(_("E1023: variable already defined: %s"), arg);
5558 return NULL;
5559 }
5560
5561 // consume "in"
5562 p = skipwhite(p);
5563 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5564 {
5565 emsg(_(e_missing_in));
5566 return NULL;
5567 }
5568 p = skipwhite(p + 2);
5569
5570
5571 scope = new_scope(cctx, FOR_SCOPE);
5572 if (scope == NULL)
5573 return NULL;
5574
5575 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005576 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5577 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005578 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005579 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005580 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005583
5584 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005585 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5586 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005587 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005588 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005589 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005591 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005593 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005594
5595 // compile "expr", it remains on the stack until "endfor"
5596 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005597 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005598 {
5599 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005600 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005602
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005603 // Now that we know the type of "var", check that it is a list, now or at
5604 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005606 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005608 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609 return NULL;
5610 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005611 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005612 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613
5614 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005615 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005616
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005617 generate_FOR(cctx, loop_lvar->lv_idx);
5618 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005619
5620 return arg;
5621}
5622
5623/*
5624 * compile "endfor"
5625 */
5626 static char_u *
5627compile_endfor(char_u *arg, cctx_T *cctx)
5628{
5629 garray_T *instr = &cctx->ctx_instr;
5630 scope_T *scope = cctx->ctx_scope;
5631 forscope_T *forscope;
5632 isn_T *isn;
5633
5634 if (scope == NULL || scope->se_type != FOR_SCOPE)
5635 {
5636 emsg(_(e_for));
5637 return NULL;
5638 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005639 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005641 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642
5643 // At end of ":for" scope jump back to the FOR instruction.
5644 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5645
5646 // Fill in the "end" label in the FOR statement so it can jump here
5647 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5648 isn->isn_arg.forloop.for_end = instr->ga_len;
5649
5650 // Fill in the "end" label any BREAK statements
5651 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5652
5653 // Below the ":for" scope drop the "expr" list from the stack.
5654 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5655 return NULL;
5656
5657 vim_free(scope);
5658
5659 return arg;
5660}
5661
5662/*
5663 * compile "while expr"
5664 *
5665 * Produces instructions:
5666 * top: EVAL expr Push result of "expr"
5667 * JUMP_IF_FALSE end jump if false
5668 * ... body ...
5669 * JUMP top Jump back to repeat
5670 * end:
5671 *
5672 */
5673 static char_u *
5674compile_while(char_u *arg, cctx_T *cctx)
5675{
5676 char_u *p = arg;
5677 garray_T *instr = &cctx->ctx_instr;
5678 scope_T *scope;
5679
5680 scope = new_scope(cctx, WHILE_SCOPE);
5681 if (scope == NULL)
5682 return NULL;
5683
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005684 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005685
5686 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005687 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005688 return NULL;
5689
5690 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005691 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005692 JUMP_IF_FALSE, cctx) == FAIL)
5693 return FAIL;
5694
5695 return p;
5696}
5697
5698/*
5699 * compile "endwhile"
5700 */
5701 static char_u *
5702compile_endwhile(char_u *arg, cctx_T *cctx)
5703{
5704 scope_T *scope = cctx->ctx_scope;
5705
5706 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5707 {
5708 emsg(_(e_while));
5709 return NULL;
5710 }
5711 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005712 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005713
5714 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005715 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005716
5717 // Fill in the "end" label in the WHILE statement so it can jump here.
5718 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005719 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005720
5721 vim_free(scope);
5722
5723 return arg;
5724}
5725
5726/*
5727 * compile "continue"
5728 */
5729 static char_u *
5730compile_continue(char_u *arg, cctx_T *cctx)
5731{
5732 scope_T *scope = cctx->ctx_scope;
5733
5734 for (;;)
5735 {
5736 if (scope == NULL)
5737 {
5738 emsg(_(e_continue));
5739 return NULL;
5740 }
5741 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5742 break;
5743 scope = scope->se_outer;
5744 }
5745
5746 // Jump back to the FOR or WHILE instruction.
5747 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005748 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5749 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005750 return arg;
5751}
5752
5753/*
5754 * compile "break"
5755 */
5756 static char_u *
5757compile_break(char_u *arg, cctx_T *cctx)
5758{
5759 scope_T *scope = cctx->ctx_scope;
5760 endlabel_T **el;
5761
5762 for (;;)
5763 {
5764 if (scope == NULL)
5765 {
5766 emsg(_(e_break));
5767 return NULL;
5768 }
5769 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5770 break;
5771 scope = scope->se_outer;
5772 }
5773
5774 // Jump to the end of the FOR or WHILE loop.
5775 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005776 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005778 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005779 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5780 return FAIL;
5781
5782 return arg;
5783}
5784
5785/*
5786 * compile "{" start of block
5787 */
5788 static char_u *
5789compile_block(char_u *arg, cctx_T *cctx)
5790{
5791 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5792 return NULL;
5793 return skipwhite(arg + 1);
5794}
5795
5796/*
5797 * compile end of block: drop one scope
5798 */
5799 static void
5800compile_endblock(cctx_T *cctx)
5801{
5802 scope_T *scope = cctx->ctx_scope;
5803
5804 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005805 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005806 vim_free(scope);
5807}
5808
5809/*
5810 * compile "try"
5811 * Creates a new scope for the try-endtry, pointing to the first catch and
5812 * finally.
5813 * Creates another scope for the "try" block itself.
5814 * TRY instruction sets up exception handling at runtime.
5815 *
5816 * "try"
5817 * TRY -> catch1, -> finally push trystack entry
5818 * ... try block
5819 * "throw {exception}"
5820 * EVAL {exception}
5821 * THROW create exception
5822 * ... try block
5823 * " catch {expr}"
5824 * JUMP -> finally
5825 * catch1: PUSH exeception
5826 * EVAL {expr}
5827 * MATCH
5828 * JUMP nomatch -> catch2
5829 * CATCH remove exception
5830 * ... catch block
5831 * " catch"
5832 * JUMP -> finally
5833 * catch2: CATCH remove exception
5834 * ... catch block
5835 * " finally"
5836 * finally:
5837 * ... finally block
5838 * " endtry"
5839 * ENDTRY pop trystack entry, may rethrow
5840 */
5841 static char_u *
5842compile_try(char_u *arg, cctx_T *cctx)
5843{
5844 garray_T *instr = &cctx->ctx_instr;
5845 scope_T *try_scope;
5846 scope_T *scope;
5847
5848 // scope that holds the jumps that go to catch/finally/endtry
5849 try_scope = new_scope(cctx, TRY_SCOPE);
5850 if (try_scope == NULL)
5851 return NULL;
5852
5853 // "catch" is set when the first ":catch" is found.
5854 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005855 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005856 if (generate_instr(cctx, ISN_TRY) == NULL)
5857 return NULL;
5858
5859 // scope for the try block itself
5860 scope = new_scope(cctx, BLOCK_SCOPE);
5861 if (scope == NULL)
5862 return NULL;
5863
5864 return arg;
5865}
5866
5867/*
5868 * compile "catch {expr}"
5869 */
5870 static char_u *
5871compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5872{
5873 scope_T *scope = cctx->ctx_scope;
5874 garray_T *instr = &cctx->ctx_instr;
5875 char_u *p;
5876 isn_T *isn;
5877
5878 // end block scope from :try or :catch
5879 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5880 compile_endblock(cctx);
5881 scope = cctx->ctx_scope;
5882
5883 // Error if not in a :try scope
5884 if (scope == NULL || scope->se_type != TRY_SCOPE)
5885 {
5886 emsg(_(e_catch));
5887 return NULL;
5888 }
5889
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005890 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005891 {
5892 emsg(_("E1033: catch unreachable after catch-all"));
5893 return NULL;
5894 }
5895
5896 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005897 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898 JUMP_ALWAYS, cctx) == FAIL)
5899 return NULL;
5900
5901 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005902 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005903 if (isn->isn_arg.try.try_catch == 0)
5904 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005905 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005906 {
5907 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005908 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005909 isn->isn_arg.jump.jump_where = instr->ga_len;
5910 }
5911
5912 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005913 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005914 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005915 scope->se_u.se_try.ts_caught_all = TRUE;
5916 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005917 }
5918 else
5919 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005920 char_u *end;
5921 char_u *pat;
5922 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005923 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005924 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005926 // Push v:exception, push {expr} and MATCH
5927 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5928
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005929 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005930 if (*end != *p)
5931 {
5932 semsg(_("E1067: Separator mismatch: %s"), p);
5933 vim_free(tofree);
5934 return FAIL;
5935 }
5936 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005937 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005938 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005939 len = (int)(end - tofree);
5940 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005941 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005942 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005943 if (pat == NULL)
5944 return FAIL;
5945 if (generate_PUSHS(cctx, pat) == FAIL)
5946 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005948 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5949 return NULL;
5950
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005951 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005952 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5953 return NULL;
5954 }
5955
5956 if (generate_instr(cctx, ISN_CATCH) == NULL)
5957 return NULL;
5958
5959 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5960 return NULL;
5961 return p;
5962}
5963
5964 static char_u *
5965compile_finally(char_u *arg, cctx_T *cctx)
5966{
5967 scope_T *scope = cctx->ctx_scope;
5968 garray_T *instr = &cctx->ctx_instr;
5969 isn_T *isn;
5970
5971 // end block scope from :try or :catch
5972 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5973 compile_endblock(cctx);
5974 scope = cctx->ctx_scope;
5975
5976 // Error if not in a :try scope
5977 if (scope == NULL || scope->se_type != TRY_SCOPE)
5978 {
5979 emsg(_(e_finally));
5980 return NULL;
5981 }
5982
5983 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005984 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005985 if (isn->isn_arg.try.try_finally != 0)
5986 {
5987 emsg(_(e_finally_dup));
5988 return NULL;
5989 }
5990
5991 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005992 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993
Bram Moolenaar585fea72020-04-02 22:33:21 +02005994 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005995 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005996 {
5997 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005998 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006000 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001 }
6002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 // TODO: set index in ts_finally_label jumps
6004
6005 return arg;
6006}
6007
6008 static char_u *
6009compile_endtry(char_u *arg, cctx_T *cctx)
6010{
6011 scope_T *scope = cctx->ctx_scope;
6012 garray_T *instr = &cctx->ctx_instr;
6013 isn_T *isn;
6014
6015 // end block scope from :catch or :finally
6016 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6017 compile_endblock(cctx);
6018 scope = cctx->ctx_scope;
6019
6020 // Error if not in a :try scope
6021 if (scope == NULL || scope->se_type != TRY_SCOPE)
6022 {
6023 if (scope == NULL)
6024 emsg(_(e_no_endtry));
6025 else if (scope->se_type == WHILE_SCOPE)
6026 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006027 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006028 emsg(_(e_endfor));
6029 else
6030 emsg(_(e_endif));
6031 return NULL;
6032 }
6033
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006034 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6036 {
6037 emsg(_("E1032: missing :catch or :finally"));
6038 return NULL;
6039 }
6040
6041 // Fill in the "end" label in jumps at the end of the blocks, if not done
6042 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006043 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006044
6045 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006046 if (isn->isn_arg.try.try_catch == 0)
6047 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048 if (isn->isn_arg.try.try_finally == 0)
6049 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006050
6051 if (scope->se_u.se_try.ts_catch_label != 0)
6052 {
6053 // Last catch without match jumps here
6054 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6055 isn->isn_arg.jump.jump_where = instr->ga_len;
6056 }
6057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058 compile_endblock(cctx);
6059
6060 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6061 return NULL;
6062 return arg;
6063}
6064
6065/*
6066 * compile "throw {expr}"
6067 */
6068 static char_u *
6069compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6070{
6071 char_u *p = skipwhite(arg);
6072
Bram Moolenaara5565e42020-05-09 15:44:01 +02006073 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074 return NULL;
6075 if (may_generate_2STRING(-1, cctx) == FAIL)
6076 return NULL;
6077 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6078 return NULL;
6079
6080 return p;
6081}
6082
6083/*
6084 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006085 * compile "echomsg expr"
6086 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006087 * compile "execute expr"
6088 */
6089 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006090compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006091{
6092 char_u *p = arg;
6093 int count = 0;
6094
6095 for (;;)
6096 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006097 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006098 return NULL;
6099 ++count;
6100 p = skipwhite(p);
6101 if (ends_excmd(*p))
6102 break;
6103 }
6104
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006105 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6106 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6107 else if (cmdidx == CMD_execute)
6108 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6109 else if (cmdidx == CMD_echomsg)
6110 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6111 else
6112 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113 return p;
6114}
6115
6116/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006117 * A command that is not compiled, execute with legacy code.
6118 */
6119 static char_u *
6120compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6121{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006122 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006123 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006124 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006125
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006126 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006127 goto theend;
6128
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006129 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006130 {
6131 long argt = excmd_get_argt(eap->cmdidx);
6132 int usefilter = FALSE;
6133
6134 has_expr = argt & (EX_XFILE | EX_EXPAND);
6135
6136 // If the command can be followed by a bar, find the bar and truncate
6137 // it, so that the following command can be compiled.
6138 // The '|' is overwritten with a NUL, it is put back below.
6139 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6140 && *eap->arg == '!')
6141 // :w !filter or :r !filter or :r! filter
6142 usefilter = TRUE;
6143 if ((argt & EX_TRLBAR) && !usefilter)
6144 {
6145 separate_nextcmd(eap);
6146 if (eap->nextcmd != NULL)
6147 nextcmd = eap->nextcmd;
6148 }
6149 }
6150
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006151 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6152 {
6153 // expand filename in "syntax include [@group] filename"
6154 has_expr = TRUE;
6155 eap->arg = skipwhite(eap->arg + 7);
6156 if (*eap->arg == '@')
6157 eap->arg = skiptowhite(eap->arg);
6158 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006159
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006160 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006161 {
6162 int count = 0;
6163 char_u *start = skipwhite(line);
6164
6165 // :cmd xxx`=expr1`yyy`=expr2`zzz
6166 // PUSHS ":cmd xxx"
6167 // eval expr1
6168 // PUSHS "yyy"
6169 // eval expr2
6170 // PUSHS "zzz"
6171 // EXECCONCAT 5
6172 for (;;)
6173 {
6174 if (p > start)
6175 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006176 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006177 ++count;
6178 }
6179 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006180 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006181 return NULL;
6182 may_generate_2STRING(-1, cctx);
6183 ++count;
6184 p = skipwhite(p);
6185 if (*p != '`')
6186 {
6187 emsg(_("E1083: missing backtick"));
6188 return NULL;
6189 }
6190 start = p + 1;
6191
6192 p = (char_u *)strstr((char *)start, "`=");
6193 if (p == NULL)
6194 {
6195 if (*skipwhite(start) != NUL)
6196 {
6197 generate_PUSHS(cctx, vim_strsave(start));
6198 ++count;
6199 }
6200 break;
6201 }
6202 }
6203 generate_EXECCONCAT(cctx, count);
6204 }
6205 else
6206 generate_EXEC(cctx, line);
6207
6208theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006209 if (*nextcmd != NUL)
6210 {
6211 // the parser expects a pointer to the bar, put it back
6212 --nextcmd;
6213 *nextcmd = '|';
6214 }
6215
6216 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006217}
6218
6219/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006220 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006221 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006222 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006223 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006224add_def_function(ufunc_T *ufunc)
6225{
6226 dfunc_T *dfunc;
6227
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006228 if (def_functions.ga_len == 0)
6229 {
6230 // The first position is not used, so that a zero uf_dfunc_idx means it
6231 // wasn't set.
6232 if (ga_grow(&def_functions, 1) == FAIL)
6233 return FAIL;
6234 ++def_functions.ga_len;
6235 }
6236
Bram Moolenaar09689a02020-05-09 22:50:08 +02006237 // Add the function to "def_functions".
6238 if (ga_grow(&def_functions, 1) == FAIL)
6239 return FAIL;
6240 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6241 CLEAR_POINTER(dfunc);
6242 dfunc->df_idx = def_functions.ga_len;
6243 ufunc->uf_dfunc_idx = dfunc->df_idx;
6244 dfunc->df_ufunc = ufunc;
6245 ++def_functions.ga_len;
6246 return OK;
6247}
6248
6249/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006250 * After ex_function() has collected all the function lines: parse and compile
6251 * the lines into instructions.
6252 * Adds the function to "def_functions".
6253 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6254 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006255 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006256 * This can be used recursively through compile_lambda(), which may reallocate
6257 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006258 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006260 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006261compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263 char_u *line = NULL;
6264 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006266 cctx_T cctx;
6267 garray_T *instr;
6268 int called_emsg_before = called_emsg;
6269 int ret = FAIL;
6270 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006271 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006272 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006273 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006275 // When using a function that was compiled before: Free old instructions.
6276 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006277 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006279 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6280 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006281 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006283 else
6284 {
6285 if (add_def_function(ufunc) == FAIL)
6286 return FAIL;
6287 new_def_function = TRUE;
6288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289
Bram Moolenaar985116a2020-07-12 17:31:09 +02006290 ufunc->uf_def_status = UF_COMPILING;
6291
Bram Moolenaara80faa82020-04-12 19:37:17 +02006292 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 cctx.ctx_ufunc = ufunc;
6294 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006295 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6297 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6298 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6299 cctx.ctx_type_list = &ufunc->uf_type_list;
6300 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6301 instr = &cctx.ctx_instr;
6302
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006303 // Set the context to the function, it may be compiled when called from
6304 // another script. Set the script version to the most modern one.
6305 // The line number will be set in next_line_from_context().
6306 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6308
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006309 // Make sure error messages are OK.
6310 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6311 if (do_estack_push)
6312 estack_push_ufunc(ufunc, 1);
6313
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006314 if (ufunc->uf_def_args.ga_len > 0)
6315 {
6316 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006317 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006318 int i;
6319 char_u *arg;
6320 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006321 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006322
6323 // Produce instructions for the default values of optional arguments.
6324 // Store the instruction index in uf_def_arg_idx[] so that we know
6325 // where to start when the function is called, depending on the number
6326 // of arguments.
6327 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6328 if (ufunc->uf_def_arg_idx == NULL)
6329 goto erret;
6330 for (i = 0; i < count; ++i)
6331 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006332 garray_T *stack = &cctx.ctx_type_stack;
6333 type_T *val_type;
6334 int arg_idx = first_def_arg + i;
6335
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006336 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6337 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006338 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006339 goto erret;
6340
6341 // If no type specified use the type of the default value.
6342 // Otherwise check that the default value type matches the
6343 // specified type.
6344 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6345 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006346 {
6347 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006348 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006349 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006350 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006351 == FAIL)
6352 {
6353 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6354 arg_idx + 1);
6355 goto erret;
6356 }
6357
6358 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006359 goto erret;
6360 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006361 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006362
6363 if (did_set_arg_type)
6364 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006365 }
6366
6367 /*
6368 * Loop over all the lines of the function and generate instructions.
6369 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006370 for (;;)
6371 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006372 exarg_T ea;
6373 cmdmod_T save_cmdmod;
6374 int starts_with_colon = FALSE;
6375 char_u *cmd;
6376 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006377
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006378 // Bail out on the first error to avoid a flood of errors and report
6379 // the right line number when inside try/catch.
6380 if (emsg_before != called_emsg)
6381 goto erret;
6382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383 if (line != NULL && *line == '|')
6384 // the line continues after a '|'
6385 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006386 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006387 && !(*line == '#' && (line == cctx.ctx_line_start
6388 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006389 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006390 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006391 goto erret;
6392 }
6393 else
6394 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006395 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006396 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006397 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006400 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401
Bram Moolenaara80faa82020-04-12 19:37:17 +02006402 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403 ea.cmdlinep = &line;
6404 ea.cmd = skipwhite(line);
6405
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006406 // Some things can be recognized by the first character.
6407 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006409 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006410 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006411 if (ea.cmd[1] != '{')
6412 {
6413 line = (char_u *)"";
6414 continue;
6415 }
6416 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006417
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006418 case '}':
6419 {
6420 // "}" ends a block scope
6421 scopetype_T stype = cctx.ctx_scope == NULL
6422 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006424 if (stype == BLOCK_SCOPE)
6425 {
6426 compile_endblock(&cctx);
6427 line = ea.cmd;
6428 }
6429 else
6430 {
6431 emsg(_("E1025: using } outside of a block scope"));
6432 goto erret;
6433 }
6434 if (line != NULL)
6435 line = skipwhite(ea.cmd + 1);
6436 continue;
6437 }
6438
6439 case '{':
6440 // "{" starts a block scope
6441 // "{'a': 1}->func() is something else
6442 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6443 {
6444 line = compile_block(ea.cmd, &cctx);
6445 continue;
6446 }
6447 break;
6448
6449 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006450 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006451 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006452 }
6453
6454 /*
6455 * COMMAND MODIFIERS
6456 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006457 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006458 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6459 {
6460 if (errormsg != NULL)
6461 goto erret;
6462 // empty line or comment
6463 line = (char_u *)"";
6464 continue;
6465 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006466 // TODO: use modifiers in the command
6467 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006468 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006469
6470 // Skip ":call" to get to the function name.
6471 if (checkforcmd(&ea.cmd, "call", 3))
6472 ea.cmd = skipwhite(ea.cmd);
6473
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006474 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006475 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006476 char_u *pskip;
6477
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006478 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006479 // find what follows.
6480 // Skip over "var.member", "var[idx]" and the like.
6481 // Also "&opt = val", "$ENV = val" and "@r = val".
6482 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006483 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006484 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006485 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006487 char_u *var_end;
6488 int oplen;
6489 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006490
Bram Moolenaar65821722020-08-02 18:58:54 +02006491 if (ea.cmd[0] == '@')
6492 var_end = ea.cmd + 2;
6493 else
6494 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006495 FNE_CHECK_START | FNE_INCL_BR);
6496 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006497 if (oplen > 0)
6498 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006499 size_t len = p - ea.cmd;
6500
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006501 // Recognize an assignment if we recognize the variable
6502 // name:
6503 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006504 // "local = expr" where "local" is a local var.
6505 // "script = expr" where "script" is a script-local var.
6506 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006507 // "&opt = expr"
6508 // "$ENV = expr"
6509 // "@r = expr"
6510 if (*ea.cmd == '&'
6511 || *ea.cmd == '$'
6512 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006513 || ((len) > 2 && ea.cmd[1] == ':')
6514 || lookup_local(ea.cmd, len, &cctx) != NULL
6515 || lookup_arg(ea.cmd, len, NULL, NULL,
6516 NULL, &cctx) == OK
6517 || lookup_script(ea.cmd, len) == OK
6518 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006519 {
6520 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006521 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006522 goto erret;
6523 continue;
6524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525 }
6526 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006527
6528 if (*ea.cmd == '[')
6529 {
6530 // [var, var] = expr
6531 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6532 if (line == NULL)
6533 goto erret;
6534 if (line != ea.cmd)
6535 continue;
6536 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537 }
6538
6539 /*
6540 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006541 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006543 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006544 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006545 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006546 ea.cmd = skip_range(ea.cmd, NULL);
6547 if (ea.cmd > cmd && !starts_with_colon)
6548 {
6549 emsg(_(e_colon_required));
6550 goto erret;
6551 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006552 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006553 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006554 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006555 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556
6557 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6558 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006559 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006560 {
6561 line += STRLEN(line);
6562 continue;
6563 }
6564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006565 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006566 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006567 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006568 // CMD_let cannot happen, compile_assignment() above is used
6569 iemsg("Command from find_ex_command() not handled");
6570 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572 }
6573
6574 p = skipwhite(p);
6575
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006576 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006577 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006578 && ea.cmdidx != CMD_elseif
6579 && ea.cmdidx != CMD_else
6580 && ea.cmdidx != CMD_endif)
6581 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006582 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006583 continue;
6584 }
6585
Bram Moolenaarefd88552020-06-18 20:50:10 +02006586 if (ea.cmdidx != CMD_elseif
6587 && ea.cmdidx != CMD_else
6588 && ea.cmdidx != CMD_endif
6589 && ea.cmdidx != CMD_endfor
6590 && ea.cmdidx != CMD_endwhile
6591 && ea.cmdidx != CMD_catch
6592 && ea.cmdidx != CMD_finally
6593 && ea.cmdidx != CMD_endtry)
6594 {
6595 if (cctx.ctx_had_return)
6596 {
6597 emsg(_("E1095: Unreachable code after :return"));
6598 goto erret;
6599 }
6600 }
6601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 switch (ea.cmdidx)
6603 {
6604 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006605 ea.arg = p;
6606 line = compile_nested_function(&ea, &cctx);
6607 break;
6608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006609 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006610 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 goto erret;
6612
6613 case CMD_return:
6614 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006615 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616 break;
6617
6618 case CMD_let:
6619 case CMD_const:
6620 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006621 if (line == p)
6622 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006623 break;
6624
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006625 case CMD_unlet:
6626 case CMD_unlockvar:
6627 case CMD_lockvar:
6628 line = compile_unletlock(p, &ea, &cctx);
6629 break;
6630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631 case CMD_import:
6632 line = compile_import(p, &cctx);
6633 break;
6634
6635 case CMD_if:
6636 line = compile_if(p, &cctx);
6637 break;
6638 case CMD_elseif:
6639 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006640 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641 break;
6642 case CMD_else:
6643 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006644 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006645 break;
6646 case CMD_endif:
6647 line = compile_endif(p, &cctx);
6648 break;
6649
6650 case CMD_while:
6651 line = compile_while(p, &cctx);
6652 break;
6653 case CMD_endwhile:
6654 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006655 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 break;
6657
6658 case CMD_for:
6659 line = compile_for(p, &cctx);
6660 break;
6661 case CMD_endfor:
6662 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006663 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 break;
6665 case CMD_continue:
6666 line = compile_continue(p, &cctx);
6667 break;
6668 case CMD_break:
6669 line = compile_break(p, &cctx);
6670 break;
6671
6672 case CMD_try:
6673 line = compile_try(p, &cctx);
6674 break;
6675 case CMD_catch:
6676 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006677 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 break;
6679 case CMD_finally:
6680 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006681 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682 break;
6683 case CMD_endtry:
6684 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006685 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686 break;
6687 case CMD_throw:
6688 line = compile_throw(p, &cctx);
6689 break;
6690
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006691 case CMD_eval:
6692 if (compile_expr0(&p, &cctx) == FAIL)
6693 goto erret;
6694
6695 // drop the return value
6696 generate_instr_drop(&cctx, ISN_DROP, 1);
6697
6698 line = skipwhite(p);
6699 break;
6700
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006703 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006704 case CMD_echomsg:
6705 case CMD_echoerr:
6706 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006707 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006708
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006709 // TODO: other commands with an expression argument
6710
Bram Moolenaarae616492020-07-28 20:07:27 +02006711 case CMD_append:
6712 case CMD_change:
6713 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006714 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006715 case CMD_xit:
6716 not_in_vim9(&ea);
6717 goto erret;
6718
Bram Moolenaar002262f2020-07-08 17:47:57 +02006719 case CMD_SIZE:
6720 semsg(_("E476: Invalid command: %s"), ea.cmd);
6721 goto erret;
6722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006724 // Not recognized, execute with do_cmdline_cmd().
6725 ea.arg = p;
6726 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006727 break;
6728 }
6729 if (line == NULL)
6730 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006731 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732
6733 if (cctx.ctx_type_stack.ga_len < 0)
6734 {
6735 iemsg("Type stack underflow");
6736 goto erret;
6737 }
6738 }
6739
6740 if (cctx.ctx_scope != NULL)
6741 {
6742 if (cctx.ctx_scope->se_type == IF_SCOPE)
6743 emsg(_(e_endif));
6744 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6745 emsg(_(e_endwhile));
6746 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6747 emsg(_(e_endfor));
6748 else
6749 emsg(_("E1026: Missing }"));
6750 goto erret;
6751 }
6752
Bram Moolenaarefd88552020-06-18 20:50:10 +02006753 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754 {
6755 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6756 {
6757 emsg(_("E1027: Missing return statement"));
6758 goto erret;
6759 }
6760
6761 // Return zero if there is no return at the end.
6762 generate_PUSHNR(&cctx, 0);
6763 generate_instr(&cctx, ISN_RETURN);
6764 }
6765
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006766 {
6767 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6768 + ufunc->uf_dfunc_idx;
6769 dfunc->df_deleted = FALSE;
6770 dfunc->df_instr = instr->ga_data;
6771 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006772 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006773 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006774 if (cctx.ctx_outer_used)
6775 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006776 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778
6779 ret = OK;
6780
6781erret:
6782 if (ret == FAIL)
6783 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006784 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006785 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6786 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006787
6788 for (idx = 0; idx < instr->ga_len; ++idx)
6789 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006791
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006792 // If using the last entry in the table and it was added above, we
6793 // might as well remove it.
6794 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006795 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006796 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006797 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006798 ufunc->uf_dfunc_idx = 0;
6799 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006800 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006801
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006802 while (cctx.ctx_scope != NULL)
6803 drop_scope(&cctx);
6804
Bram Moolenaar20431c92020-03-20 18:39:46 +01006805 // Don't execute this function body.
6806 ga_clear_strings(&ufunc->uf_lines);
6807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 if (errormsg != NULL)
6809 emsg(errormsg);
6810 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006811 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812 }
6813
6814 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006815 if (do_estack_push)
6816 estack_pop();
6817
Bram Moolenaar20431c92020-03-20 18:39:46 +01006818 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006819 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006821 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006822}
6823
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006824 void
6825set_function_type(ufunc_T *ufunc)
6826{
6827 int varargs = ufunc->uf_va_name != NULL;
6828 int argcount = ufunc->uf_args.ga_len;
6829
6830 // Create a type for the function, with the return type and any
6831 // argument types.
6832 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6833 // The type is included in "tt_args".
6834 if (argcount > 0 || varargs)
6835 {
6836 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6837 argcount, &ufunc->uf_type_list);
6838 // Add argument types to the function type.
6839 if (func_type_add_arg_types(ufunc->uf_func_type,
6840 argcount + varargs,
6841 &ufunc->uf_type_list) == FAIL)
6842 return;
6843 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6844 ufunc->uf_func_type->tt_min_argcount =
6845 argcount - ufunc->uf_def_args.ga_len;
6846 if (ufunc->uf_arg_types == NULL)
6847 {
6848 int i;
6849
6850 // lambda does not have argument types.
6851 for (i = 0; i < argcount; ++i)
6852 ufunc->uf_func_type->tt_args[i] = &t_any;
6853 }
6854 else
6855 mch_memmove(ufunc->uf_func_type->tt_args,
6856 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6857 if (varargs)
6858 {
6859 ufunc->uf_func_type->tt_args[argcount] =
6860 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
6861 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6862 }
6863 }
6864 else
6865 // No arguments, can use a predefined type.
6866 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6867 argcount, &ufunc->uf_type_list);
6868}
6869
6870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006871/*
6872 * Delete an instruction, free what it contains.
6873 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006874 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875delete_instr(isn_T *isn)
6876{
6877 switch (isn->isn_type)
6878 {
6879 case ISN_EXEC:
6880 case ISN_LOADENV:
6881 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006882 case ISN_LOADB:
6883 case ISN_LOADW:
6884 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006886 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887 case ISN_PUSHEXC:
6888 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006889 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006891 case ISN_STOREB:
6892 case ISN_STOREW:
6893 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006894 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895 vim_free(isn->isn_arg.string);
6896 break;
6897
6898 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006899 case ISN_STORES:
6900 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006901 break;
6902
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006903 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006904 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006905 vim_free(isn->isn_arg.unlet.ul_name);
6906 break;
6907
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908 case ISN_STOREOPT:
6909 vim_free(isn->isn_arg.storeopt.so_name);
6910 break;
6911
6912 case ISN_PUSHBLOB: // push blob isn_arg.blob
6913 blob_unref(isn->isn_arg.blob);
6914 break;
6915
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006916 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006917#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006918 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006919#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006920 break;
6921
6922 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006923#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006924 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006925#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006926 break;
6927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006928 case ISN_UCALL:
6929 vim_free(isn->isn_arg.ufunc.cuf_name);
6930 break;
6931
Bram Moolenaar221fcc72020-05-05 19:46:20 +02006932 case ISN_FUNCREF:
6933 {
6934 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6935 + isn->isn_arg.funcref.fr_func;
6936 func_ptr_unref(dfunc->df_ufunc);
6937 }
6938 break;
6939
Bram Moolenaar38ddf332020-07-31 22:05:04 +02006940 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02006941 {
6942 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
6943 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
6944
6945 if (ufunc != NULL)
6946 {
6947 // Clear uf_dfunc_idx so that the function is deleted.
6948 clear_def_function(ufunc);
6949 ufunc->uf_dfunc_idx = 0;
6950 func_ptr_unref(ufunc);
6951 }
6952
6953 vim_free(lambda);
6954 vim_free(isn->isn_arg.newfunc.nf_global);
6955 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02006956 break;
6957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006958 case ISN_2BOOL:
6959 case ISN_2STRING:
6960 case ISN_ADDBLOB:
6961 case ISN_ADDLIST:
6962 case ISN_BCALL:
6963 case ISN_CATCH:
6964 case ISN_CHECKNR:
6965 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02006966 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967 case ISN_COMPAREANY:
6968 case ISN_COMPAREBLOB:
6969 case ISN_COMPAREBOOL:
6970 case ISN_COMPAREDICT:
6971 case ISN_COMPAREFLOAT:
6972 case ISN_COMPAREFUNC:
6973 case ISN_COMPARELIST:
6974 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975 case ISN_COMPARESPECIAL:
6976 case ISN_COMPARESTRING:
6977 case ISN_CONCAT:
6978 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02006979 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006980 case ISN_DROP:
6981 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006982 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006983 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006985 case ISN_EXECCONCAT:
6986 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02006988 case ISN_LISTINDEX:
6989 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006990 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02006991 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006992 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993 case ISN_JUMP:
6994 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02006995 case ISN_LOADBDICT:
6996 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006997 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006998 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02006999 case ISN_LOADSCRIPT:
7000 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007002 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003 case ISN_NEGATENR:
7004 case ISN_NEWDICT:
7005 case ISN_NEWLIST:
7006 case ISN_OPNR:
7007 case ISN_OPFLOAT:
7008 case ISN_OPANY:
7009 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007010 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 case ISN_PUSHF:
7012 case ISN_PUSHNR:
7013 case ISN_PUSHBOOL:
7014 case ISN_PUSHSPEC:
7015 case ISN_RETURN:
7016 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007017 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007018 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007020 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007022 case ISN_STOREDICT:
7023 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024 case ISN_THROW:
7025 case ISN_TRY:
7026 // nothing allocated
7027 break;
7028 }
7029}
7030
7031/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007032 * Free all instructions for "dfunc".
7033 */
7034 static void
7035delete_def_function_contents(dfunc_T *dfunc)
7036{
7037 int idx;
7038
7039 ga_clear(&dfunc->df_def_args_isn);
7040
7041 if (dfunc->df_instr != NULL)
7042 {
7043 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7044 delete_instr(dfunc->df_instr + idx);
7045 VIM_CLEAR(dfunc->df_instr);
7046 }
7047
7048 dfunc->df_deleted = TRUE;
7049}
7050
7051/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007052 * When a user function is deleted, clear the contents of any associated def
7053 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 */
7055 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007056clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007058 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 {
7060 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7061 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062
Bram Moolenaar20431c92020-03-20 18:39:46 +01007063 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007064 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007065 }
7066}
7067
7068#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007069/*
7070 * Free all functions defined with ":def".
7071 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 void
7073free_def_functions(void)
7074{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007075 int idx;
7076
7077 for (idx = 0; idx < def_functions.ga_len; ++idx)
7078 {
7079 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7080
7081 delete_def_function_contents(dfunc);
7082 }
7083
7084 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007085}
7086#endif
7087
7088
7089#endif // FEAT_EVAL